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 | |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 25 | #include <binder/Parcel.h> |
ray-cy.lee | c20825b | 2017-03-07 18:25:15 +0800 | [diff] [blame] | 26 | #include <binder/PermissionCache.h> |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 27 | #include <media/IMediaExtractor.h> |
| 28 | #include <media/stagefright/MetaData.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | enum { |
| 33 | COUNTTRACKS = IBinder::FIRST_CALL_TRANSACTION, |
| 34 | GETTRACK, |
| 35 | GETTRACKMETADATA, |
| 36 | GETMETADATA, |
| 37 | FLAGS, |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 38 | GETDRMTRACKINFO, |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 39 | SETMEDIACAS, |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 40 | SETUID, |
Ray Essick | ba13b7b | 2017-02-07 10:03:18 -0800 | [diff] [blame] | 41 | NAME, |
Chong Zhang | 4c6398b | 2017-09-07 17:43:19 -0700 | [diff] [blame] | 42 | GETMETRICS, |
| 43 | RELEASE, |
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 | |
Chong Zhang | d5a416a | 2017-05-16 11:16:34 -0700 | [diff] [blame] | 121 | virtual status_t setMediaCas(const HInterfaceToken &casToken) { |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 122 | ALOGV("setMediaCas"); |
| 123 | |
| 124 | Parcel data, reply; |
| 125 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
Chong Zhang | d5a416a | 2017-05-16 11:16:34 -0700 | [diff] [blame] | 126 | data.writeByteVector(casToken); |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 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 | } |
Chong Zhang | 4c6398b | 2017-09-07 17:43:19 -0700 | [diff] [blame] | 143 | |
| 144 | virtual void release() { |
| 145 | ALOGV("release"); |
| 146 | Parcel data, reply; |
| 147 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 148 | remote()->transact(RELEASE, data, &reply); |
| 149 | } |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | IMPLEMENT_META_INTERFACE(MediaExtractor, "android.media.IMediaExtractor"); |
| 153 | |
| 154 | #undef LOG_TAG |
| 155 | #define LOG_TAG "BnMediaExtractor" |
| 156 | |
| 157 | status_t BnMediaExtractor::onTransact( |
| 158 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 159 | { |
| 160 | switch (code) { |
| 161 | case COUNTTRACKS: { |
| 162 | ALOGV("countTracks"); |
| 163 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 164 | size_t numTracks = countTracks(); |
| 165 | if (numTracks > INT32_MAX) { |
| 166 | numTracks = 0; |
| 167 | } |
| 168 | reply->writeUint32(uint32_t(numTracks)); |
| 169 | return NO_ERROR; |
| 170 | } |
| 171 | case GETTRACK: { |
| 172 | ALOGV("getTrack()"); |
| 173 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 174 | uint32_t idx; |
| 175 | if (data.readUint32(&idx) == NO_ERROR) { |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 176 | const sp<IMediaSource> track = getTrack(size_t(idx)); |
| 177 | registerMediaSource(this, track); |
| 178 | return reply->writeStrongBinder(IInterface::asBinder(track)); |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 179 | } |
| 180 | return UNKNOWN_ERROR; |
| 181 | } |
| 182 | case GETTRACKMETADATA: { |
| 183 | ALOGV("getTrackMetaData"); |
| 184 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 185 | uint32_t idx; |
| 186 | uint32_t flags; |
| 187 | if (data.readUint32(&idx) == NO_ERROR && |
| 188 | data.readUint32(&flags) == NO_ERROR) { |
| 189 | sp<MetaData> meta = getTrackMetaData(idx, flags); |
Marco Nelissen | 0d13824 | 2016-10-06 15:31:52 -0700 | [diff] [blame] | 190 | if (meta == NULL) { |
| 191 | return UNKNOWN_ERROR; |
| 192 | } |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 193 | meta->writeToParcel(*reply); |
| 194 | return NO_ERROR; |
| 195 | } |
| 196 | return UNKNOWN_ERROR; |
| 197 | } |
| 198 | case GETMETADATA: { |
| 199 | ALOGV("getMetaData"); |
| 200 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 201 | sp<MetaData> meta = getMetaData(); |
| 202 | if (meta != NULL) { |
| 203 | meta->writeToParcel(*reply); |
| 204 | return NO_ERROR; |
| 205 | } |
| 206 | return UNKNOWN_ERROR; |
| 207 | } |
Ray Essick | ba13b7b | 2017-02-07 10:03:18 -0800 | [diff] [blame] | 208 | case GETMETRICS: { |
| 209 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 210 | status_t ret = getMetrics(reply); |
| 211 | return ret; |
| 212 | } |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 213 | case SETMEDIACAS: { |
| 214 | ALOGV("setMediaCas"); |
| 215 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 216 | |
Chong Zhang | d5a416a | 2017-05-16 11:16:34 -0700 | [diff] [blame] | 217 | HInterfaceToken casToken; |
| 218 | status_t err = data.readByteVector(&casToken); |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 219 | if (err != NO_ERROR) { |
Chong Zhang | d5a416a | 2017-05-16 11:16:34 -0700 | [diff] [blame] | 220 | ALOGE("Error reading casToken from parcel"); |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 221 | return err; |
| 222 | } |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 223 | |
Chong Zhang | d5a416a | 2017-05-16 11:16:34 -0700 | [diff] [blame] | 224 | reply->writeInt32(setMediaCas(casToken)); |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 225 | return OK; |
| 226 | } |
Chong Zhang | 4c6398b | 2017-09-07 17:43:19 -0700 | [diff] [blame] | 227 | case RELEASE: { |
| 228 | ALOGV("release"); |
| 229 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 230 | release(); |
| 231 | return OK; |
| 232 | } |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 233 | default: |
| 234 | return BBinder::onTransact(code, data, reply, flags); |
| 235 | } |
| 236 | } |
| 237 | |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 238 | typedef struct { |
| 239 | String8 mime; |
| 240 | String8 name; |
| 241 | String8 sourceDescription; |
| 242 | pid_t owner; |
| 243 | wp<IMediaExtractor> extractor; |
| 244 | Vector<wp<IMediaSource>> tracks; |
| 245 | Vector<String8> trackDescriptions; |
| 246 | String8 toString() const; |
| 247 | } ExtractorInstance; |
| 248 | |
| 249 | String8 ExtractorInstance::toString() const { |
| 250 | String8 str = name; |
| 251 | str.append(" for mime "); |
| 252 | str.append(mime); |
| 253 | str.append(", source "); |
| 254 | str.append(sourceDescription); |
| 255 | str.append(String8::format(", pid %d: ", owner)); |
| 256 | if (extractor.promote() == NULL) { |
| 257 | str.append("deleted\n"); |
| 258 | } else { |
| 259 | str.append("active\n"); |
| 260 | } |
| 261 | for (size_t i = 0; i < tracks.size(); i++) { |
| 262 | const String8 desc = trackDescriptions.itemAt(i); |
| 263 | str.appendFormat(" track {%s} ", desc.string()); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 264 | wp<IMediaSource> wSource = tracks.itemAt(i); |
| 265 | if (wSource == NULL) { |
| 266 | str.append(": null\n"); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 267 | } else { |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 268 | const sp<IMediaSource> source = wSource.promote(); |
| 269 | if (source == NULL) { |
| 270 | str.append(": deleted\n"); |
| 271 | } else { |
| 272 | str.appendFormat(": active\n"); |
| 273 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | return str; |
| 277 | } |
| 278 | |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 279 | static Vector<ExtractorInstance> sExtractors; |
| 280 | static Mutex sExtractorsLock; |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 281 | |
| 282 | void registerMediaSource( |
| 283 | const sp<IMediaExtractor> &ex, |
| 284 | const sp<IMediaSource> &source) { |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 285 | Mutex::Autolock lock(sExtractorsLock); |
| 286 | for (size_t i = 0; i < sExtractors.size(); i++) { |
| 287 | ExtractorInstance &instance = sExtractors.editItemAt(i); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 288 | sp<IMediaExtractor> extractor = instance.extractor.promote(); |
| 289 | if (extractor != NULL && extractor == ex) { |
| 290 | if (instance.tracks.size() > 5) { |
| 291 | instance.tracks.resize(5); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 292 | instance.trackDescriptions.resize(5); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 293 | } |
| 294 | instance.tracks.push_front(source); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 295 | if (source != NULL) { |
| 296 | instance.trackDescriptions.push_front(source->getFormat()->toString()); |
| 297 | } else { |
| 298 | instance.trackDescriptions.push_front(String8::empty()); |
| 299 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 300 | break; |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | void registerMediaExtractor( |
| 306 | const sp<IMediaExtractor> &extractor, |
Marco Nelissen | a321469 | 2016-05-06 10:57:20 -0700 | [diff] [blame] | 307 | const sp<DataSource> &source, |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 308 | const char *mime) { |
| 309 | ExtractorInstance ex; |
| 310 | ex.mime = mime == NULL ? "NULL" : mime; |
| 311 | ex.name = extractor->name(); |
| 312 | ex.sourceDescription = source->toString(); |
| 313 | ex.owner = IPCThreadState::self()->getCallingPid(); |
| 314 | ex.extractor = extractor; |
| 315 | |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 316 | { |
| 317 | Mutex::Autolock lock(sExtractorsLock); |
| 318 | if (sExtractors.size() > 10) { |
| 319 | sExtractors.resize(10); |
| 320 | } |
| 321 | sExtractors.push_front(ex); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 322 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | status_t dumpExtractors(int fd, const Vector<String16>&) { |
| 326 | String8 out; |
ray-cy.lee | c20825b | 2017-03-07 18:25:15 +0800 | [diff] [blame] | 327 | const IPCThreadState* ipc = IPCThreadState::self(); |
| 328 | const int pid = ipc->getCallingPid(); |
| 329 | const int uid = ipc->getCallingUid(); |
| 330 | if (!PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) { |
| 331 | out.appendFormat("Permission Denial: " |
| 332 | "can't dump MediaExtractor from pid=%d, uid=%d\n", pid, uid); |
| 333 | } else { |
| 334 | out.append("Recent extractors, most recent first:\n"); |
| 335 | { |
| 336 | Mutex::Autolock lock(sExtractorsLock); |
| 337 | for (size_t i = 0; i < sExtractors.size(); i++) { |
| 338 | const ExtractorInstance &instance = sExtractors.itemAt(i); |
| 339 | out.append(" "); |
| 340 | out.append(instance.toString()); |
| 341 | } |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 342 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 343 | } |
| 344 | write(fd, out.string(), out.size()); |
| 345 | return OK; |
| 346 | } |
| 347 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 348 | |
| 349 | } // namespace android |
| 350 | |