Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Marco Nelissen | b65990f | 2015-11-09 15:39:49 -0800 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 18 | #define LOG_TAG "BpMediaExtractor" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 24 | #include <android/media/ICas.h> |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 25 | #include <binder/IPCThreadState.h> |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 26 | #include <binder/Parcel.h> |
ray-cy.lee | c20825b | 2017-03-07 18:25:15 +0800 | [diff] [blame] | 27 | #include <binder/PermissionCache.h> |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 28 | #include <media/IMediaExtractor.h> |
| 29 | #include <media/stagefright/MetaData.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | enum { |
| 34 | COUNTTRACKS = IBinder::FIRST_CALL_TRANSACTION, |
| 35 | GETTRACK, |
| 36 | GETTRACKMETADATA, |
| 37 | GETMETADATA, |
| 38 | FLAGS, |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 39 | GETDRMTRACKINFO, |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 40 | SETMEDIACAS, |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 41 | SETUID, |
Ray Essick | ba13b7b | 2017-02-07 10:03:18 -0800 | [diff] [blame] | 42 | NAME, |
| 43 | GETMETRICS |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | class BpMediaExtractor : public BpInterface<IMediaExtractor> { |
| 47 | public: |
Chih-Hung Hsieh | 64a2870 | 2016-05-03 09:52:51 -0700 | [diff] [blame] | 48 | explicit BpMediaExtractor(const sp<IBinder>& impl) |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 49 | : BpInterface<IMediaExtractor>(impl) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | virtual size_t countTracks() { |
| 54 | ALOGV("countTracks"); |
| 55 | Parcel data, reply; |
| 56 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 57 | status_t ret = remote()->transact(COUNTTRACKS, data, &reply); |
| 58 | size_t numTracks = 0; |
| 59 | if (ret == NO_ERROR) { |
| 60 | numTracks = reply.readUint32(); |
| 61 | } |
| 62 | return numTracks; |
| 63 | } |
| 64 | virtual sp<IMediaSource> getTrack(size_t index) { |
| 65 | ALOGV("getTrack(%zu)", index); |
| 66 | Parcel data, reply; |
| 67 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 68 | data.writeUint32(index); |
| 69 | status_t ret = remote()->transact(GETTRACK, data, &reply); |
| 70 | if (ret == NO_ERROR) { |
| 71 | return interface_cast<IMediaSource>(reply.readStrongBinder()); |
| 72 | } |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | virtual sp<MetaData> getTrackMetaData( |
| 77 | size_t index, uint32_t flags) { |
| 78 | ALOGV("getTrackMetaData(%zu, %u)", index, flags); |
| 79 | Parcel data, reply; |
| 80 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 81 | data.writeUint32(index); |
| 82 | data.writeUint32(flags); |
| 83 | status_t ret = remote()->transact(GETTRACKMETADATA, data, &reply); |
| 84 | if (ret == NO_ERROR) { |
| 85 | return MetaData::createFromParcel(reply); |
| 86 | } |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | virtual sp<MetaData> getMetaData() { |
| 91 | ALOGV("getMetaData"); |
| 92 | Parcel data, reply; |
| 93 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 94 | status_t ret = remote()->transact(GETMETADATA, data, &reply); |
| 95 | if (ret == NO_ERROR) { |
| 96 | return MetaData::createFromParcel(reply); |
| 97 | } |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Ray Essick | ba13b7b | 2017-02-07 10:03:18 -0800 | [diff] [blame] | 101 | virtual status_t getMetrics(Parcel * reply) { |
| 102 | Parcel data; |
| 103 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 104 | status_t ret = remote()->transact(GETMETRICS, data, reply); |
| 105 | if (ret == NO_ERROR) { |
| 106 | return OK; |
| 107 | } |
| 108 | return UNKNOWN_ERROR; |
| 109 | } |
| 110 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 111 | virtual uint32_t flags() const { |
| 112 | ALOGV("flags NOT IMPLEMENTED"); |
| 113 | return 0; |
| 114 | } |
| 115 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 116 | virtual char* getDrmTrackInfo(size_t trackID __unused, int *len __unused) { |
| 117 | ALOGV("getDrmTrackInfo NOT IMPLEMENTED"); |
| 118 | return NULL; |
| 119 | } |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 120 | |
| 121 | virtual status_t setMediaCas(const sp<ICas> & cas) { |
| 122 | ALOGV("setMediaCas"); |
| 123 | |
| 124 | Parcel data, reply; |
| 125 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 126 | data.writeStrongBinder(IInterface::asBinder(cas)); |
| 127 | |
| 128 | status_t err = remote()->transact(SETMEDIACAS, data, &reply); |
| 129 | if (err != NO_ERROR) { |
| 130 | return err; |
| 131 | } |
| 132 | return reply.readInt32(); |
| 133 | } |
| 134 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 135 | virtual void setUID(uid_t uid __unused) { |
| 136 | ALOGV("setUID NOT IMPLEMENTED"); |
| 137 | } |
| 138 | |
| 139 | virtual const char * name() { |
| 140 | ALOGV("name NOT IMPLEMENTED"); |
| 141 | return NULL; |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | IMPLEMENT_META_INTERFACE(MediaExtractor, "android.media.IMediaExtractor"); |
| 146 | |
| 147 | #undef LOG_TAG |
| 148 | #define LOG_TAG "BnMediaExtractor" |
| 149 | |
| 150 | status_t BnMediaExtractor::onTransact( |
| 151 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 152 | { |
| 153 | switch (code) { |
| 154 | case COUNTTRACKS: { |
| 155 | ALOGV("countTracks"); |
| 156 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 157 | size_t numTracks = countTracks(); |
| 158 | if (numTracks > INT32_MAX) { |
| 159 | numTracks = 0; |
| 160 | } |
| 161 | reply->writeUint32(uint32_t(numTracks)); |
| 162 | return NO_ERROR; |
| 163 | } |
| 164 | case GETTRACK: { |
| 165 | ALOGV("getTrack()"); |
| 166 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 167 | uint32_t idx; |
| 168 | if (data.readUint32(&idx) == NO_ERROR) { |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 169 | const sp<IMediaSource> track = getTrack(size_t(idx)); |
| 170 | registerMediaSource(this, track); |
| 171 | return reply->writeStrongBinder(IInterface::asBinder(track)); |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 172 | } |
| 173 | return UNKNOWN_ERROR; |
| 174 | } |
| 175 | case GETTRACKMETADATA: { |
| 176 | ALOGV("getTrackMetaData"); |
| 177 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 178 | uint32_t idx; |
| 179 | uint32_t flags; |
| 180 | if (data.readUint32(&idx) == NO_ERROR && |
| 181 | data.readUint32(&flags) == NO_ERROR) { |
| 182 | sp<MetaData> meta = getTrackMetaData(idx, flags); |
Marco Nelissen | 0d13824 | 2016-10-06 15:31:52 -0700 | [diff] [blame] | 183 | if (meta == NULL) { |
| 184 | return UNKNOWN_ERROR; |
| 185 | } |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 186 | meta->writeToParcel(*reply); |
| 187 | return NO_ERROR; |
| 188 | } |
| 189 | return UNKNOWN_ERROR; |
| 190 | } |
| 191 | case GETMETADATA: { |
| 192 | ALOGV("getMetaData"); |
| 193 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 194 | sp<MetaData> meta = getMetaData(); |
| 195 | if (meta != NULL) { |
| 196 | meta->writeToParcel(*reply); |
| 197 | return NO_ERROR; |
| 198 | } |
| 199 | return UNKNOWN_ERROR; |
| 200 | } |
Ray Essick | ba13b7b | 2017-02-07 10:03:18 -0800 | [diff] [blame] | 201 | case GETMETRICS: { |
| 202 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 203 | status_t ret = getMetrics(reply); |
| 204 | return ret; |
| 205 | } |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 206 | case SETMEDIACAS: { |
| 207 | ALOGV("setMediaCas"); |
| 208 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 209 | |
| 210 | sp<IBinder> casBinder; |
| 211 | status_t err = data.readNullableStrongBinder(&casBinder); |
| 212 | if (err != NO_ERROR) { |
| 213 | ALOGE("Error reading cas from parcel"); |
| 214 | return err; |
| 215 | } |
| 216 | sp<ICas> cas = interface_cast<ICas>(casBinder); |
| 217 | |
| 218 | reply->writeInt32(setMediaCas(cas)); |
| 219 | return OK; |
| 220 | } |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 221 | default: |
| 222 | return BBinder::onTransact(code, data, reply, flags); |
| 223 | } |
| 224 | } |
| 225 | |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 226 | typedef struct { |
| 227 | String8 mime; |
| 228 | String8 name; |
| 229 | String8 sourceDescription; |
| 230 | pid_t owner; |
| 231 | wp<IMediaExtractor> extractor; |
| 232 | Vector<wp<IMediaSource>> tracks; |
| 233 | Vector<String8> trackDescriptions; |
| 234 | String8 toString() const; |
| 235 | } ExtractorInstance; |
| 236 | |
| 237 | String8 ExtractorInstance::toString() const { |
| 238 | String8 str = name; |
| 239 | str.append(" for mime "); |
| 240 | str.append(mime); |
| 241 | str.append(", source "); |
| 242 | str.append(sourceDescription); |
| 243 | str.append(String8::format(", pid %d: ", owner)); |
| 244 | if (extractor.promote() == NULL) { |
| 245 | str.append("deleted\n"); |
| 246 | } else { |
| 247 | str.append("active\n"); |
| 248 | } |
| 249 | for (size_t i = 0; i < tracks.size(); i++) { |
| 250 | const String8 desc = trackDescriptions.itemAt(i); |
| 251 | str.appendFormat(" track {%s} ", desc.string()); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 252 | wp<IMediaSource> wSource = tracks.itemAt(i); |
| 253 | if (wSource == NULL) { |
| 254 | str.append(": null\n"); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 255 | } else { |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 256 | const sp<IMediaSource> source = wSource.promote(); |
| 257 | if (source == NULL) { |
| 258 | str.append(": deleted\n"); |
| 259 | } else { |
| 260 | str.appendFormat(": active\n"); |
| 261 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | return str; |
| 265 | } |
| 266 | |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 267 | static Vector<ExtractorInstance> sExtractors; |
| 268 | static Mutex sExtractorsLock; |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 269 | |
| 270 | void registerMediaSource( |
| 271 | const sp<IMediaExtractor> &ex, |
| 272 | const sp<IMediaSource> &source) { |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 273 | Mutex::Autolock lock(sExtractorsLock); |
| 274 | for (size_t i = 0; i < sExtractors.size(); i++) { |
| 275 | ExtractorInstance &instance = sExtractors.editItemAt(i); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 276 | sp<IMediaExtractor> extractor = instance.extractor.promote(); |
| 277 | if (extractor != NULL && extractor == ex) { |
| 278 | if (instance.tracks.size() > 5) { |
| 279 | instance.tracks.resize(5); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 280 | instance.trackDescriptions.resize(5); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 281 | } |
| 282 | instance.tracks.push_front(source); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 283 | if (source != NULL) { |
| 284 | instance.trackDescriptions.push_front(source->getFormat()->toString()); |
| 285 | } else { |
| 286 | instance.trackDescriptions.push_front(String8::empty()); |
| 287 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | void registerMediaExtractor( |
| 294 | const sp<IMediaExtractor> &extractor, |
Marco Nelissen | a321469 | 2016-05-06 10:57:20 -0700 | [diff] [blame] | 295 | const sp<DataSource> &source, |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 296 | const char *mime) { |
| 297 | ExtractorInstance ex; |
| 298 | ex.mime = mime == NULL ? "NULL" : mime; |
| 299 | ex.name = extractor->name(); |
| 300 | ex.sourceDescription = source->toString(); |
| 301 | ex.owner = IPCThreadState::self()->getCallingPid(); |
| 302 | ex.extractor = extractor; |
| 303 | |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 304 | { |
| 305 | Mutex::Autolock lock(sExtractorsLock); |
| 306 | if (sExtractors.size() > 10) { |
| 307 | sExtractors.resize(10); |
| 308 | } |
| 309 | sExtractors.push_front(ex); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 310 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | status_t dumpExtractors(int fd, const Vector<String16>&) { |
| 314 | String8 out; |
ray-cy.lee | c20825b | 2017-03-07 18:25:15 +0800 | [diff] [blame] | 315 | const IPCThreadState* ipc = IPCThreadState::self(); |
| 316 | const int pid = ipc->getCallingPid(); |
| 317 | const int uid = ipc->getCallingUid(); |
| 318 | if (!PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) { |
| 319 | out.appendFormat("Permission Denial: " |
| 320 | "can't dump MediaExtractor from pid=%d, uid=%d\n", pid, uid); |
| 321 | } else { |
| 322 | out.append("Recent extractors, most recent first:\n"); |
| 323 | { |
| 324 | Mutex::Autolock lock(sExtractorsLock); |
| 325 | for (size_t i = 0; i < sExtractors.size(); i++) { |
| 326 | const ExtractorInstance &instance = sExtractors.itemAt(i); |
| 327 | out.append(" "); |
| 328 | out.append(instance.toString()); |
| 329 | } |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 330 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 331 | } |
| 332 | write(fd, out.string(), out.size()); |
| 333 | return OK; |
| 334 | } |
| 335 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 336 | |
| 337 | } // namespace android |
| 338 | |