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> |
Marco Nelissen | b589437 | 2019-01-10 14:44:13 -0800 | [diff] [blame] | 22 | #include <time.h> |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | |
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 | b596c34 | 2017-03-07 18:25:15 +0800 | [diff] [blame] | 27 | #include <binder/PermissionCache.h> |
Marco Nelissen | dab79b3 | 2019-11-18 08:25:47 -0800 | [diff] [blame] | 28 | #include <android/IMediaExtractor.h> |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 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, |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 39 | SETMEDIACAS, |
Ray Essick | ba13b7b | 2017-02-07 10:03:18 -0800 | [diff] [blame] | 40 | NAME, |
Hangyu Kuang | 6606267 | 2020-12-03 19:30:25 +0000 | [diff] [blame] | 41 | GETMETRICS |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | class BpMediaExtractor : public BpInterface<IMediaExtractor> { |
| 45 | public: |
Chih-Hung Hsieh | 64a2870 | 2016-05-03 09:52:51 -0700 | [diff] [blame] | 46 | explicit BpMediaExtractor(const sp<IBinder>& impl) |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 47 | : BpInterface<IMediaExtractor>(impl) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | virtual size_t countTracks() { |
| 52 | ALOGV("countTracks"); |
| 53 | Parcel data, reply; |
| 54 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 55 | status_t ret = remote()->transact(COUNTTRACKS, data, &reply); |
| 56 | size_t numTracks = 0; |
| 57 | if (ret == NO_ERROR) { |
| 58 | numTracks = reply.readUint32(); |
| 59 | } |
| 60 | return numTracks; |
| 61 | } |
| 62 | virtual sp<IMediaSource> getTrack(size_t index) { |
| 63 | ALOGV("getTrack(%zu)", index); |
| 64 | Parcel data, reply; |
| 65 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 66 | data.writeUint32(index); |
| 67 | status_t ret = remote()->transact(GETTRACK, data, &reply); |
| 68 | if (ret == NO_ERROR) { |
| 69 | return interface_cast<IMediaSource>(reply.readStrongBinder()); |
| 70 | } |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | virtual sp<MetaData> getTrackMetaData( |
| 75 | size_t index, uint32_t flags) { |
| 76 | ALOGV("getTrackMetaData(%zu, %u)", index, flags); |
| 77 | Parcel data, reply; |
| 78 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 79 | data.writeUint32(index); |
| 80 | data.writeUint32(flags); |
| 81 | status_t ret = remote()->transact(GETTRACKMETADATA, data, &reply); |
| 82 | if (ret == NO_ERROR) { |
| 83 | return MetaData::createFromParcel(reply); |
| 84 | } |
| 85 | return NULL; |
| 86 | } |
| 87 | |
| 88 | virtual sp<MetaData> getMetaData() { |
| 89 | ALOGV("getMetaData"); |
| 90 | Parcel data, reply; |
| 91 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 92 | status_t ret = remote()->transact(GETMETADATA, data, &reply); |
| 93 | if (ret == NO_ERROR) { |
| 94 | return MetaData::createFromParcel(reply); |
| 95 | } |
| 96 | return NULL; |
| 97 | } |
| 98 | |
Ray Essick | ba13b7b | 2017-02-07 10:03:18 -0800 | [diff] [blame] | 99 | virtual status_t getMetrics(Parcel * reply) { |
| 100 | Parcel data; |
| 101 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 102 | status_t ret = remote()->transact(GETMETRICS, data, reply); |
| 103 | if (ret == NO_ERROR) { |
| 104 | return OK; |
| 105 | } |
| 106 | return UNKNOWN_ERROR; |
| 107 | } |
| 108 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 109 | virtual uint32_t flags() const { |
Marco Nelissen | e4f4b79 | 2020-01-17 13:15:18 -0800 | [diff] [blame] | 110 | ALOGV("flags"); |
| 111 | Parcel data, reply; |
| 112 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 113 | status_t ret = remote()->transact(FLAGS, data, &reply); |
| 114 | int flgs = 0; |
| 115 | if (ret == NO_ERROR) { |
| 116 | flgs = reply.readUint32(); |
| 117 | } |
| 118 | return flgs; |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 119 | } |
| 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 | e4f4b79 | 2020-01-17 13:15:18 -0800 | [diff] [blame] | 135 | virtual String8 name() { |
| 136 | Parcel data, reply; |
| 137 | data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor()); |
| 138 | status_t ret = remote()->transact(NAME, data, &reply); |
| 139 | String8 nm; |
| 140 | if (ret == NO_ERROR) { |
| 141 | nm = reply.readString8(); |
| 142 | } |
| 143 | return nm; |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 144 | } |
| 145 | }; |
| 146 | |
| 147 | IMPLEMENT_META_INTERFACE(MediaExtractor, "android.media.IMediaExtractor"); |
| 148 | |
| 149 | #undef LOG_TAG |
| 150 | #define LOG_TAG "BnMediaExtractor" |
| 151 | |
| 152 | status_t BnMediaExtractor::onTransact( |
| 153 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) |
| 154 | { |
| 155 | switch (code) { |
| 156 | case COUNTTRACKS: { |
| 157 | ALOGV("countTracks"); |
| 158 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 159 | size_t numTracks = countTracks(); |
| 160 | if (numTracks > INT32_MAX) { |
| 161 | numTracks = 0; |
| 162 | } |
| 163 | reply->writeUint32(uint32_t(numTracks)); |
| 164 | return NO_ERROR; |
| 165 | } |
| 166 | case GETTRACK: { |
| 167 | ALOGV("getTrack()"); |
| 168 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 169 | uint32_t idx; |
| 170 | if (data.readUint32(&idx) == NO_ERROR) { |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 171 | const sp<IMediaSource> track = getTrack(size_t(idx)); |
| 172 | registerMediaSource(this, track); |
| 173 | return reply->writeStrongBinder(IInterface::asBinder(track)); |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 174 | } |
| 175 | return UNKNOWN_ERROR; |
| 176 | } |
| 177 | case GETTRACKMETADATA: { |
| 178 | ALOGV("getTrackMetaData"); |
| 179 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 180 | uint32_t idx; |
| 181 | uint32_t flags; |
| 182 | if (data.readUint32(&idx) == NO_ERROR && |
| 183 | data.readUint32(&flags) == NO_ERROR) { |
| 184 | sp<MetaData> meta = getTrackMetaData(idx, flags); |
Marco Nelissen | 0d13824 | 2016-10-06 15:31:52 -0700 | [diff] [blame] | 185 | if (meta == NULL) { |
| 186 | return UNKNOWN_ERROR; |
| 187 | } |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 188 | meta->writeToParcel(*reply); |
| 189 | return NO_ERROR; |
| 190 | } |
| 191 | return UNKNOWN_ERROR; |
| 192 | } |
| 193 | case GETMETADATA: { |
| 194 | ALOGV("getMetaData"); |
| 195 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 196 | sp<MetaData> meta = getMetaData(); |
| 197 | if (meta != NULL) { |
| 198 | meta->writeToParcel(*reply); |
| 199 | return NO_ERROR; |
| 200 | } |
| 201 | return UNKNOWN_ERROR; |
| 202 | } |
Ray Essick | ba13b7b | 2017-02-07 10:03:18 -0800 | [diff] [blame] | 203 | case GETMETRICS: { |
| 204 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 205 | status_t ret = getMetrics(reply); |
| 206 | return ret; |
| 207 | } |
Marco Nelissen | e4f4b79 | 2020-01-17 13:15:18 -0800 | [diff] [blame] | 208 | case FLAGS: { |
| 209 | ALOGV("flags"); |
| 210 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 211 | reply->writeUint32(this->flags()); |
| 212 | return NO_ERROR; |
| 213 | } |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 214 | case SETMEDIACAS: { |
| 215 | ALOGV("setMediaCas"); |
| 216 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 217 | |
Chong Zhang | d5a416a | 2017-05-16 11:16:34 -0700 | [diff] [blame] | 218 | HInterfaceToken casToken; |
| 219 | status_t err = data.readByteVector(&casToken); |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 220 | if (err != NO_ERROR) { |
Chong Zhang | d5a416a | 2017-05-16 11:16:34 -0700 | [diff] [blame] | 221 | ALOGE("Error reading casToken from parcel"); |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 222 | return err; |
| 223 | } |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 224 | |
Chong Zhang | d5a416a | 2017-05-16 11:16:34 -0700 | [diff] [blame] | 225 | reply->writeInt32(setMediaCas(casToken)); |
Chong Zhang | 9dbe9a5 | 2017-01-03 11:35:15 -0800 | [diff] [blame] | 226 | return OK; |
| 227 | } |
Marco Nelissen | e4f4b79 | 2020-01-17 13:15:18 -0800 | [diff] [blame] | 228 | case NAME: { |
| 229 | ALOGV("name"); |
| 230 | CHECK_INTERFACE(IMediaExtractor, data, reply); |
| 231 | String8 nm = name(); |
| 232 | reply->writeString8(nm); |
| 233 | return NO_ERROR; |
| 234 | } |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 235 | default: |
| 236 | return BBinder::onTransact(code, data, reply, flags); |
| 237 | } |
| 238 | } |
| 239 | |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 240 | typedef struct { |
| 241 | String8 mime; |
| 242 | String8 name; |
| 243 | String8 sourceDescription; |
| 244 | pid_t owner; |
| 245 | wp<IMediaExtractor> extractor; |
| 246 | Vector<wp<IMediaSource>> tracks; |
| 247 | Vector<String8> trackDescriptions; |
| 248 | String8 toString() const; |
Marco Nelissen | b589437 | 2019-01-10 14:44:13 -0800 | [diff] [blame] | 249 | time_t when; |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 250 | } ExtractorInstance; |
| 251 | |
| 252 | String8 ExtractorInstance::toString() const { |
Marco Nelissen | b589437 | 2019-01-10 14:44:13 -0800 | [diff] [blame] | 253 | String8 str; |
| 254 | char timeString[32]; |
| 255 | strftime(timeString, sizeof(timeString), "%m-%d %T", localtime(&when)); |
| 256 | str.append(timeString); |
| 257 | str.append(": "); |
| 258 | str.append(name); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 259 | str.append(" for mime "); |
| 260 | str.append(mime); |
| 261 | str.append(", source "); |
| 262 | str.append(sourceDescription); |
| 263 | str.append(String8::format(", pid %d: ", owner)); |
| 264 | if (extractor.promote() == NULL) { |
| 265 | str.append("deleted\n"); |
| 266 | } else { |
| 267 | str.append("active\n"); |
| 268 | } |
| 269 | for (size_t i = 0; i < tracks.size(); i++) { |
| 270 | const String8 desc = trackDescriptions.itemAt(i); |
| 271 | str.appendFormat(" track {%s} ", desc.string()); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 272 | wp<IMediaSource> wSource = tracks.itemAt(i); |
| 273 | if (wSource == NULL) { |
| 274 | str.append(": null\n"); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 275 | } else { |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 276 | const sp<IMediaSource> source = wSource.promote(); |
| 277 | if (source == NULL) { |
| 278 | str.append(": deleted\n"); |
| 279 | } else { |
| 280 | str.appendFormat(": active\n"); |
| 281 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | return str; |
| 285 | } |
| 286 | |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 287 | static Vector<ExtractorInstance> sExtractors; |
| 288 | static Mutex sExtractorsLock; |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 289 | |
| 290 | void registerMediaSource( |
| 291 | const sp<IMediaExtractor> &ex, |
| 292 | const sp<IMediaSource> &source) { |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 293 | Mutex::Autolock lock(sExtractorsLock); |
| 294 | for (size_t i = 0; i < sExtractors.size(); i++) { |
| 295 | ExtractorInstance &instance = sExtractors.editItemAt(i); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 296 | sp<IMediaExtractor> extractor = instance.extractor.promote(); |
| 297 | if (extractor != NULL && extractor == ex) { |
| 298 | if (instance.tracks.size() > 5) { |
| 299 | instance.tracks.resize(5); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 300 | instance.trackDescriptions.resize(5); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 301 | } |
| 302 | instance.tracks.push_front(source); |
Marco Nelissen | 460b7e8 | 2016-12-19 14:06:30 -0800 | [diff] [blame] | 303 | if (source != NULL) { |
| 304 | instance.trackDescriptions.push_front(source->getFormat()->toString()); |
| 305 | } else { |
| 306 | instance.trackDescriptions.push_front(String8::empty()); |
| 307 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void registerMediaExtractor( |
| 314 | const sp<IMediaExtractor> &extractor, |
Marco Nelissen | a321469 | 2016-05-06 10:57:20 -0700 | [diff] [blame] | 315 | const sp<DataSource> &source, |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 316 | const char *mime) { |
| 317 | ExtractorInstance ex; |
| 318 | ex.mime = mime == NULL ? "NULL" : mime; |
| 319 | ex.name = extractor->name(); |
| 320 | ex.sourceDescription = source->toString(); |
| 321 | ex.owner = IPCThreadState::self()->getCallingPid(); |
| 322 | ex.extractor = extractor; |
Marco Nelissen | b589437 | 2019-01-10 14:44:13 -0800 | [diff] [blame] | 323 | ex.when = time(NULL); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 324 | |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 325 | { |
| 326 | Mutex::Autolock lock(sExtractorsLock); |
| 327 | if (sExtractors.size() > 10) { |
| 328 | sExtractors.resize(10); |
| 329 | } |
| 330 | sExtractors.push_front(ex); |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 331 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | status_t dumpExtractors(int fd, const Vector<String16>&) { |
| 335 | String8 out; |
ray-cy.lee | b596c34 | 2017-03-07 18:25:15 +0800 | [diff] [blame] | 336 | const IPCThreadState* ipc = IPCThreadState::self(); |
| 337 | const int pid = ipc->getCallingPid(); |
| 338 | const int uid = ipc->getCallingUid(); |
| 339 | if (!PermissionCache::checkPermission(String16("android.permission.DUMP"), pid, uid)) { |
| 340 | out.appendFormat("Permission Denial: " |
| 341 | "can't dump MediaExtractor from pid=%d, uid=%d\n", pid, uid); |
| 342 | } else { |
| 343 | out.append("Recent extractors, most recent first:\n"); |
| 344 | { |
| 345 | Mutex::Autolock lock(sExtractorsLock); |
| 346 | for (size_t i = 0; i < sExtractors.size(); i++) { |
| 347 | const ExtractorInstance &instance = sExtractors.itemAt(i); |
| 348 | out.append(" "); |
| 349 | out.append(instance.toString()); |
| 350 | } |
Marco Nelissen | 8d27f5e | 2016-04-14 08:06:26 -0700 | [diff] [blame] | 351 | } |
Marco Nelissen | 69d3d8a | 2016-03-07 13:20:01 -0800 | [diff] [blame] | 352 | } |
| 353 | write(fd, out.string(), out.size()); |
| 354 | return OK; |
| 355 | } |
| 356 | |
Marco Nelissen | b2487f0 | 2015-09-01 13:23:23 -0700 | [diff] [blame] | 357 | |
| 358 | } // namespace android |
| 359 | |