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