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 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "BpMediaExtractor"
19 #include <utils/Log.h>
20 
21 #include <stdint.h>
22 #include <time.h>
23 #include <sys/types.h>
24 
25 #include <binder/IPCThreadState.h>
26 #include <binder/Parcel.h>
27 #include <binder/PermissionCache.h>
28 #include <android/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,
39     SETMEDIACAS,
40     NAME,
41     GETMETRICS
42 };
43 
44 class BpMediaExtractor : public BpInterface<IMediaExtractor> {
45 public:
BpMediaExtractor(const sp<IBinder> & impl)46     explicit BpMediaExtractor(const sp<IBinder>& impl)
47         : BpInterface<IMediaExtractor>(impl)
48     {
49     }
50 
countTracks()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     }
getTrack(size_t index)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 
getTrackMetaData(size_t index,uint32_t flags)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 
getMetaData()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 
getMetrics(Parcel * reply)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 
flags() const109     virtual uint32_t flags() const {
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;
119     }
120 
setMediaCas(const HInterfaceToken & casToken)121     virtual status_t setMediaCas(const HInterfaceToken &casToken) {
122         ALOGV("setMediaCas");
123 
124         Parcel data, reply;
125         data.writeInterfaceToken(BpMediaExtractor::getInterfaceDescriptor());
126         data.writeByteVector(casToken);
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 
name()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;
144     }
145 };
146 
147 IMPLEMENT_META_INTERFACE(MediaExtractor, "android.media.IMediaExtractor");
148 
149 #undef LOG_TAG
150 #define LOG_TAG "BnMediaExtractor"
151 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)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) {
171                 const sp<IMediaSource> track = getTrack(size_t(idx));
172                 registerMediaSource(this, track);
173                 return reply->writeStrongBinder(IInterface::asBinder(track));
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);
185                 if (meta == NULL) {
186                     return UNKNOWN_ERROR;
187                 }
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         }
203         case GETMETRICS: {
204             CHECK_INTERFACE(IMediaExtractor, data, reply);
205             status_t ret = getMetrics(reply);
206             return ret;
207         }
208         case FLAGS: {
209             ALOGV("flags");
210             CHECK_INTERFACE(IMediaExtractor, data, reply);
211             reply->writeUint32(this->flags());
212             return NO_ERROR;
213         }
214         case SETMEDIACAS: {
215             ALOGV("setMediaCas");
216             CHECK_INTERFACE(IMediaExtractor, data, reply);
217 
218             HInterfaceToken casToken;
219             status_t err = data.readByteVector(&casToken);
220             if (err != NO_ERROR) {
221                 ALOGE("Error reading casToken from parcel");
222                 return err;
223             }
224 
225             reply->writeInt32(setMediaCas(casToken));
226             return OK;
227         }
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         }
235         default:
236             return BBinder::onTransact(code, data, reply, flags);
237     }
238 }
239 
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;
249     time_t when;
250 } ExtractorInstance;
251 
toString() const252 String8 ExtractorInstance::toString() const {
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);
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());
272         wp<IMediaSource> wSource = tracks.itemAt(i);
273         if (wSource == NULL) {
274             str.append(": null\n");
275         } else {
276             const sp<IMediaSource> source = wSource.promote();
277             if (source == NULL) {
278                 str.append(": deleted\n");
279             } else {
280                 str.appendFormat(": active\n");
281             }
282         }
283     }
284     return str;
285 }
286 
287 static Vector<ExtractorInstance> sExtractors;
288 static Mutex sExtractorsLock;
289 
registerMediaSource(const sp<IMediaExtractor> & ex,const sp<IMediaSource> & source)290 void registerMediaSource(
291         const sp<IMediaExtractor> &ex,
292         const sp<IMediaSource> &source) {
293     Mutex::Autolock lock(sExtractorsLock);
294     for (size_t i = 0; i < sExtractors.size(); i++) {
295         ExtractorInstance &instance = sExtractors.editItemAt(i);
296         sp<IMediaExtractor> extractor = instance.extractor.promote();
297         if (extractor != NULL && extractor == ex) {
298             if (instance.tracks.size() > 5) {
299                 instance.tracks.resize(5);
300                 instance.trackDescriptions.resize(5);
301             }
302             instance.tracks.push_front(source);
303             if (source != NULL) {
304                 instance.trackDescriptions.push_front(source->getFormat()->toString());
305             } else {
306                 instance.trackDescriptions.push_front(String8::empty());
307             }
308             break;
309         }
310     }
311 }
312 
registerMediaExtractor(const sp<IMediaExtractor> & extractor,const sp<DataSource> & source,const char * mime)313 void registerMediaExtractor(
314         const sp<IMediaExtractor> &extractor,
315         const sp<DataSource> &source,
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;
323     ex.when = time(NULL);
324 
325     {
326         Mutex::Autolock lock(sExtractorsLock);
327         if (sExtractors.size() > 10) {
328             sExtractors.resize(10);
329         }
330         sExtractors.push_front(ex);
331     }
332 }
333 
dumpExtractors(int fd,const Vector<String16> &)334 status_t dumpExtractors(int fd, const Vector<String16>&) {
335     String8 out;
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             }
351         }
352     }
353     write(fd, out.string(), out.size());
354     return OK;
355 }
356 
357 
358 }  // namespace android
359 
360