blob: 4caa79eae190cd83c50ed5f9f68e67a4126d225b [file] [log] [blame]
Marco Nelissenb2487f02015-09-01 13:23:23 -07001/*
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 Nelissenb65990f2015-11-09 15:39:49 -080017//#define LOG_NDEBUG 0
Marco Nelissenb2487f02015-09-01 13:23:23 -070018#define LOG_TAG "BpMediaExtractor"
19#include <utils/Log.h>
20
21#include <stdint.h>
22#include <sys/types.h>
23
Chong Zhang9dbe9a52017-01-03 11:35:15 -080024#include <android/media/ICas.h>
Marco Nelissen69d3d8a2016-03-07 13:20:01 -080025#include <binder/IPCThreadState.h>
Marco Nelissenb2487f02015-09-01 13:23:23 -070026#include <binder/Parcel.h>
ray-cy.leec20825b2017-03-07 18:25:15 +080027#include <binder/PermissionCache.h>
Marco Nelissenb2487f02015-09-01 13:23:23 -070028#include <media/IMediaExtractor.h>
29#include <media/stagefright/MetaData.h>
30
31namespace android {
32
33enum {
34 COUNTTRACKS = IBinder::FIRST_CALL_TRANSACTION,
35 GETTRACK,
36 GETTRACKMETADATA,
37 GETMETADATA,
38 FLAGS,
Marco Nelissenb2487f02015-09-01 13:23:23 -070039 GETDRMTRACKINFO,
Chong Zhang9dbe9a52017-01-03 11:35:15 -080040 SETMEDIACAS,
Marco Nelissenb2487f02015-09-01 13:23:23 -070041 SETUID,
Ray Essickba13b7b2017-02-07 10:03:18 -080042 NAME,
43 GETMETRICS
Marco Nelissenb2487f02015-09-01 13:23:23 -070044};
45
46class BpMediaExtractor : public BpInterface<IMediaExtractor> {
47public:
Chih-Hung Hsieh64a28702016-05-03 09:52:51 -070048 explicit BpMediaExtractor(const sp<IBinder>& impl)
Marco Nelissenb2487f02015-09-01 13:23:23 -070049 : 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 Essickba13b7b2017-02-07 10:03:18 -0800101 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 Nelissenb2487f02015-09-01 13:23:23 -0700111 virtual uint32_t flags() const {
112 ALOGV("flags NOT IMPLEMENTED");
113 return 0;
114 }
115
Marco Nelissenb2487f02015-09-01 13:23:23 -0700116 virtual char* getDrmTrackInfo(size_t trackID __unused, int *len __unused) {
117 ALOGV("getDrmTrackInfo NOT IMPLEMENTED");
118 return NULL;
119 }
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800120
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 Nelissenb2487f02015-09-01 13:23:23 -0700135 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
145IMPLEMENT_META_INTERFACE(MediaExtractor, "android.media.IMediaExtractor");
146
147#undef LOG_TAG
148#define LOG_TAG "BnMediaExtractor"
149
150status_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 Nelissen69d3d8a2016-03-07 13:20:01 -0800169 const sp<IMediaSource> track = getTrack(size_t(idx));
170 registerMediaSource(this, track);
171 return reply->writeStrongBinder(IInterface::asBinder(track));
Marco Nelissenb2487f02015-09-01 13:23:23 -0700172 }
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 Nelissen0d138242016-10-06 15:31:52 -0700183 if (meta == NULL) {
184 return UNKNOWN_ERROR;
185 }
Marco Nelissenb2487f02015-09-01 13:23:23 -0700186 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 Essickba13b7b2017-02-07 10:03:18 -0800201 case GETMETRICS: {
202 CHECK_INTERFACE(IMediaExtractor, data, reply);
203 status_t ret = getMetrics(reply);
204 return ret;
205 }
Chong Zhang9dbe9a52017-01-03 11:35:15 -0800206 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 Nelissenb2487f02015-09-01 13:23:23 -0700221 default:
222 return BBinder::onTransact(code, data, reply, flags);
223 }
224}
225
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800226typedef 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
237String8 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 Nelissen460b7e82016-12-19 14:06:30 -0800252 wp<IMediaSource> wSource = tracks.itemAt(i);
253 if (wSource == NULL) {
254 str.append(": null\n");
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800255 } else {
Marco Nelissen460b7e82016-12-19 14:06:30 -0800256 const sp<IMediaSource> source = wSource.promote();
257 if (source == NULL) {
258 str.append(": deleted\n");
259 } else {
260 str.appendFormat(": active\n");
261 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800262 }
263 }
264 return str;
265}
266
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700267static Vector<ExtractorInstance> sExtractors;
268static Mutex sExtractorsLock;
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800269
270void registerMediaSource(
271 const sp<IMediaExtractor> &ex,
272 const sp<IMediaSource> &source) {
Marco Nelissen8d27f5e2016-04-14 08:06:26 -0700273 Mutex::Autolock lock(sExtractorsLock);
274 for (size_t i = 0; i < sExtractors.size(); i++) {
275 ExtractorInstance &instance = sExtractors.editItemAt(i);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800276 sp<IMediaExtractor> extractor = instance.extractor.promote();
277 if (extractor != NULL && extractor == ex) {
278 if (instance.tracks.size() > 5) {
279 instance.tracks.resize(5);
Marco Nelissen460b7e82016-12-19 14:06:30 -0800280 instance.trackDescriptions.resize(5);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800281 }
282 instance.tracks.push_front(source);
Marco Nelissen460b7e82016-12-19 14:06:30 -0800283 if (source != NULL) {
284 instance.trackDescriptions.push_front(source->getFormat()->toString());
285 } else {
286 instance.trackDescriptions.push_front(String8::empty());
287 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800288 break;
289 }
290 }
291}
292
293void registerMediaExtractor(
294 const sp<IMediaExtractor> &extractor,
Marco Nelissena3214692016-05-06 10:57:20 -0700295 const sp<DataSource> &source,
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800296 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 Nelissen8d27f5e2016-04-14 08:06:26 -0700304 {
305 Mutex::Autolock lock(sExtractorsLock);
306 if (sExtractors.size() > 10) {
307 sExtractors.resize(10);
308 }
309 sExtractors.push_front(ex);
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800310 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800311}
312
313status_t dumpExtractors(int fd, const Vector<String16>&) {
314 String8 out;
ray-cy.leec20825b2017-03-07 18:25:15 +0800315 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 Nelissen8d27f5e2016-04-14 08:06:26 -0700330 }
Marco Nelissen69d3d8a2016-03-07 13:20:01 -0800331 }
332 write(fd, out.string(), out.size());
333 return OK;
334}
335
Marco Nelissenb2487f02015-09-01 13:23:23 -0700336
337} // namespace android
338