1 /*
2 **
3 ** Copyright (C) 2008 The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #include <inttypes.h>
19 #include <stdint.h>
20 #include <sys/types.h>
21 
22 #include <android/IDataSource.h>
23 #include <binder/Parcel.h>
24 #include <media/IMediaHTTPService.h>
25 #include <media/IMediaMetadataRetriever.h>
26 #include <processgroup/sched_policy.h>
27 #include <utils/String8.h>
28 #include <utils/KeyedVector.h>
29 
30 // The binder is supposed to propagate the scheduler group across
31 // the binder interface so that remote calls are executed with
32 // the same priority as local calls. This is currently not working
33 // so this change puts in a temporary hack to fix the issue with
34 // metadata retrieval which can be a huge CPU hit if done on a
35 // foreground thread.
36 #ifndef DISABLE_GROUP_SCHEDULE_HACK
37 
38 #undef LOG_TAG
39 #define LOG_TAG "IMediaMetadataRetriever"
40 #include <utils/Log.h>
41 #include <cutils/sched_policy.h>
42 
43 namespace android {
44 
sendSchedPolicy(Parcel & data)45 static void sendSchedPolicy(Parcel& data)
46 {
47     SchedPolicy policy;
48     get_sched_policy(gettid(), &policy);
49     data.writeInt32(policy);
50 }
51 
setSchedPolicy(const Parcel & data)52 static void setSchedPolicy(const Parcel& data)
53 {
54     SchedPolicy policy = (SchedPolicy) data.readInt32();
55     set_sched_policy(gettid(), policy);
56 }
restoreSchedPolicy()57 static void restoreSchedPolicy()
58 {
59     set_sched_policy(gettid(), SP_FOREGROUND);
60 }
61 }; // end namespace android
62 #endif
63 
64 namespace android {
65 
66 enum {
67     DISCONNECT = IBinder::FIRST_CALL_TRANSACTION,
68     SET_DATA_SOURCE_URL,
69     SET_DATA_SOURCE_FD,
70     SET_DATA_SOURCE_CALLBACK,
71     GET_FRAME_AT_TIME,
72     GET_IMAGE_AT_INDEX,
73     GET_IMAGE_RECT_AT_INDEX,
74     GET_FRAME_AT_INDEX,
75     EXTRACT_ALBUM_ART,
76     EXTRACT_METADATA,
77 };
78 
79 class BpMediaMetadataRetriever: public BpInterface<IMediaMetadataRetriever>
80 {
81 public:
BpMediaMetadataRetriever(const sp<IBinder> & impl)82     explicit BpMediaMetadataRetriever(const sp<IBinder>& impl)
83         : BpInterface<IMediaMetadataRetriever>(impl)
84     {
85     }
86 
87     // disconnect from media metadata retriever service
disconnect()88     void disconnect()
89     {
90         Parcel data, reply;
91         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
92         remote()->transact(DISCONNECT, data, &reply);
93     }
94 
setDataSource(const sp<IMediaHTTPService> & httpService,const char * srcUrl,const KeyedVector<String8,String8> * headers)95     status_t setDataSource(
96             const sp<IMediaHTTPService> &httpService,
97             const char *srcUrl,
98             const KeyedVector<String8, String8> *headers)
99     {
100         Parcel data, reply;
101         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
102         data.writeInt32(httpService != NULL);
103         if (httpService != NULL) {
104             data.writeStrongBinder(IInterface::asBinder(httpService));
105         }
106         data.writeCString(srcUrl);
107 
108         if (headers == NULL) {
109             data.writeInt32(0);
110         } else {
111             // serialize the headers
112             data.writeInt32(headers->size());
113             for (size_t i = 0; i < headers->size(); ++i) {
114                 data.writeString8(headers->keyAt(i));
115                 data.writeString8(headers->valueAt(i));
116             }
117         }
118 
119         remote()->transact(SET_DATA_SOURCE_URL, data, &reply);
120         return reply.readInt32();
121     }
122 
setDataSource(int fd,int64_t offset,int64_t length)123     status_t setDataSource(int fd, int64_t offset, int64_t length)
124     {
125         Parcel data, reply;
126         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
127         data.writeFileDescriptor(fd);
128         data.writeInt64(offset);
129         data.writeInt64(length);
130         remote()->transact(SET_DATA_SOURCE_FD, data, &reply);
131         return reply.readInt32();
132     }
133 
setDataSource(const sp<IDataSource> & source,const char * mime)134     status_t setDataSource(const sp<IDataSource>& source, const char *mime)
135     {
136         Parcel data, reply;
137         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
138         data.writeStrongBinder(IInterface::asBinder(source));
139 
140         if (mime != NULL) {
141             data.writeInt32(1);
142             data.writeCString(mime);
143         } else {
144             data.writeInt32(0);
145         }
146         remote()->transact(SET_DATA_SOURCE_CALLBACK, data, &reply);
147         return reply.readInt32();
148     }
149 
getFrameAtTime(int64_t timeUs,int option,int colorFormat,bool metaOnly)150     sp<IMemory> getFrameAtTime(int64_t timeUs, int option, int colorFormat, bool metaOnly)
151     {
152         ALOGV("getTimeAtTime: time(%" PRId64 " us), option(%d), colorFormat(%d) metaOnly(%d)",
153                 timeUs, option, colorFormat, metaOnly);
154         Parcel data, reply;
155         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
156         data.writeInt64(timeUs);
157         data.writeInt32(option);
158         data.writeInt32(colorFormat);
159         data.writeInt32(metaOnly);
160 #ifndef DISABLE_GROUP_SCHEDULE_HACK
161         sendSchedPolicy(data);
162 #endif
163         remote()->transact(GET_FRAME_AT_TIME, data, &reply);
164         status_t ret = reply.readInt32();
165         if (ret != NO_ERROR) {
166             return NULL;
167         }
168         return interface_cast<IMemory>(reply.readStrongBinder());
169     }
170 
getImageAtIndex(int index,int colorFormat,bool metaOnly,bool thumbnail)171     sp<IMemory> getImageAtIndex(int index, int colorFormat, bool metaOnly, bool thumbnail)
172     {
173         ALOGV("getImageAtIndex: index %d, colorFormat(%d) metaOnly(%d) thumbnail(%d)",
174                 index, colorFormat, metaOnly, thumbnail);
175         Parcel data, reply;
176         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
177         data.writeInt32(index);
178         data.writeInt32(colorFormat);
179         data.writeInt32(metaOnly);
180         data.writeInt32(thumbnail);
181 #ifndef DISABLE_GROUP_SCHEDULE_HACK
182         sendSchedPolicy(data);
183 #endif
184         remote()->transact(GET_IMAGE_AT_INDEX, data, &reply);
185         status_t ret = reply.readInt32();
186         if (ret != NO_ERROR) {
187             return NULL;
188         }
189         return interface_cast<IMemory>(reply.readStrongBinder());
190     }
191 
getImageRectAtIndex(int index,int colorFormat,int left,int top,int right,int bottom)192     sp<IMemory> getImageRectAtIndex(
193             int index, int colorFormat, int left, int top, int right, int bottom)
194     {
195         ALOGV("getImageRectAtIndex: index %d, colorFormat(%d) rect {%d, %d, %d, %d}",
196                 index, colorFormat, left, top, right, bottom);
197         Parcel data, reply;
198         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
199         data.writeInt32(index);
200         data.writeInt32(colorFormat);
201         data.writeInt32(left);
202         data.writeInt32(top);
203         data.writeInt32(right);
204         data.writeInt32(bottom);
205 #ifndef DISABLE_GROUP_SCHEDULE_HACK
206         sendSchedPolicy(data);
207 #endif
208         remote()->transact(GET_IMAGE_RECT_AT_INDEX, data, &reply);
209         status_t ret = reply.readInt32();
210         if (ret != NO_ERROR) {
211             return NULL;
212         }
213         return interface_cast<IMemory>(reply.readStrongBinder());
214     }
215 
getFrameAtIndex(int index,int colorFormat,bool metaOnly)216     sp<IMemory> getFrameAtIndex(
217             int index, int colorFormat, bool metaOnly)
218     {
219         ALOGV("getFrameAtIndex: index(%d), colorFormat(%d) metaOnly(%d)",
220                 index, colorFormat, metaOnly);
221         Parcel data, reply;
222         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
223         data.writeInt32(index);
224         data.writeInt32(colorFormat);
225         data.writeInt32(metaOnly);
226 #ifndef DISABLE_GROUP_SCHEDULE_HACK
227         sendSchedPolicy(data);
228 #endif
229         remote()->transact(GET_FRAME_AT_INDEX, data, &reply);
230         status_t ret = reply.readInt32();
231         if (ret != NO_ERROR) {
232             return NULL;
233         }
234         return interface_cast<IMemory>(reply.readStrongBinder());
235     }
236 
extractAlbumArt()237     sp<IMemory> extractAlbumArt()
238     {
239         Parcel data, reply;
240         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
241 #ifndef DISABLE_GROUP_SCHEDULE_HACK
242         sendSchedPolicy(data);
243 #endif
244         remote()->transact(EXTRACT_ALBUM_ART, data, &reply);
245         status_t ret = reply.readInt32();
246         if (ret != NO_ERROR) {
247             return NULL;
248         }
249         return interface_cast<IMemory>(reply.readStrongBinder());
250     }
251 
extractMetadata(int keyCode)252     const char* extractMetadata(int keyCode)
253     {
254         Parcel data, reply;
255         data.writeInterfaceToken(IMediaMetadataRetriever::getInterfaceDescriptor());
256 #ifndef DISABLE_GROUP_SCHEDULE_HACK
257         sendSchedPolicy(data);
258 #endif
259         data.writeInt32(keyCode);
260         remote()->transact(EXTRACT_METADATA, data, &reply);
261         status_t ret = reply.readInt32();
262         if (ret != NO_ERROR) {
263             return NULL;
264         }
265         const char* str = reply.readCString();
266         if (str != NULL) {
267             String8 value(str);
268             if (mMetadata.indexOfKey(keyCode) < 0) {
269                 mMetadata.add(keyCode, value);
270             } else {
271                 mMetadata.replaceValueFor(keyCode, value);
272             }
273             return mMetadata.valueFor(keyCode).string();
274         } else {
275             return NULL;
276         }
277     }
278 
279 private:
280     KeyedVector<int, String8> mMetadata;
281 };
282 
283 IMPLEMENT_META_INTERFACE(MediaMetadataRetriever, "android.media.IMediaMetadataRetriever");
284 
285 // ----------------------------------------------------------------------
286 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)287 status_t BnMediaMetadataRetriever::onTransact(
288     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
289 {
290     switch (code) {
291         case DISCONNECT: {
292             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
293             disconnect();
294             return NO_ERROR;
295         } break;
296         case SET_DATA_SOURCE_URL: {
297             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
298 
299             sp<IMediaHTTPService> httpService;
300             if (data.readInt32()) {
301                 httpService =
302                     interface_cast<IMediaHTTPService>(data.readStrongBinder());
303             }
304 
305             const char* srcUrl = data.readCString();
306 
307             if (httpService == NULL || srcUrl == NULL) {
308                 reply->writeInt32(BAD_VALUE);
309                 return NO_ERROR;
310             }
311 
312             KeyedVector<String8, String8> headers;
313             size_t numHeaders = (size_t) data.readInt32();
314             for (size_t i = 0; i < numHeaders; ++i) {
315                 String8 key;
316                 String8 value;
317                 status_t status;
318                 status = data.readString8(&key);
319                 if (status != OK) {
320                     return status;
321                 }
322                 status = data.readString8(&value);
323                 if (status != OK) {
324                     return status;
325                 }
326                 if (headers.add(key, value) < 0) {
327                     return UNKNOWN_ERROR;
328                 }
329             }
330 
331             reply->writeInt32(
332                     setDataSource(
333                         httpService, srcUrl, numHeaders > 0 ? &headers : NULL));
334 
335             return NO_ERROR;
336         } break;
337         case SET_DATA_SOURCE_FD: {
338             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
339             int fd = data.readFileDescriptor();
340             int64_t offset = data.readInt64();
341             int64_t length = data.readInt64();
342             reply->writeInt32(setDataSource(fd, offset, length));
343             return NO_ERROR;
344         } break;
345         case SET_DATA_SOURCE_CALLBACK: {
346             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
347             sp<IDataSource> source =
348                 interface_cast<IDataSource>(data.readStrongBinder());
349             if (source == NULL) {
350                 reply->writeInt32(BAD_VALUE);
351             } else {
352                 int32_t hasMime = data.readInt32();
353                 const char *mime = NULL;
354                 if (hasMime) {
355                     mime = data.readCString();
356                 }
357                 reply->writeInt32(setDataSource(source, mime));
358             }
359             return NO_ERROR;
360         } break;
361         case GET_FRAME_AT_TIME: {
362             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
363             int64_t timeUs = data.readInt64();
364             int option = data.readInt32();
365             int colorFormat = data.readInt32();
366             bool metaOnly = (data.readInt32() != 0);
367             ALOGV("getTimeAtTime: time(%" PRId64 " us), option(%d), colorFormat(%d), metaOnly(%d)",
368                     timeUs, option, colorFormat, metaOnly);
369 #ifndef DISABLE_GROUP_SCHEDULE_HACK
370             setSchedPolicy(data);
371 #endif
372             sp<IMemory> bitmap = getFrameAtTime(timeUs, option, colorFormat, metaOnly);
373             if (bitmap != 0) {  // Don't send NULL across the binder interface
374                 reply->writeInt32(NO_ERROR);
375                 reply->writeStrongBinder(IInterface::asBinder(bitmap));
376             } else {
377                 reply->writeInt32(UNKNOWN_ERROR);
378             }
379 #ifndef DISABLE_GROUP_SCHEDULE_HACK
380             restoreSchedPolicy();
381 #endif
382             return NO_ERROR;
383         } break;
384         case GET_IMAGE_AT_INDEX: {
385             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
386             int index = data.readInt32();
387             int colorFormat = data.readInt32();
388             bool metaOnly = (data.readInt32() != 0);
389             bool thumbnail = (data.readInt32() != 0);
390             ALOGV("getImageAtIndex: index(%d), colorFormat(%d), metaOnly(%d), thumbnail(%d)",
391                     index, colorFormat, metaOnly, thumbnail);
392 #ifndef DISABLE_GROUP_SCHEDULE_HACK
393             setSchedPolicy(data);
394 #endif
395             sp<IMemory> bitmap = getImageAtIndex(index, colorFormat, metaOnly, thumbnail);
396             if (bitmap != 0) {  // Don't send NULL across the binder interface
397                 reply->writeInt32(NO_ERROR);
398                 reply->writeStrongBinder(IInterface::asBinder(bitmap));
399             } else {
400                 reply->writeInt32(UNKNOWN_ERROR);
401             }
402 #ifndef DISABLE_GROUP_SCHEDULE_HACK
403             restoreSchedPolicy();
404 #endif
405             return NO_ERROR;
406         } break;
407 
408         case GET_IMAGE_RECT_AT_INDEX: {
409             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
410             int index = data.readInt32();
411             int colorFormat = data.readInt32();
412             int left = data.readInt32();
413             int top = data.readInt32();
414             int right = data.readInt32();
415             int bottom = data.readInt32();
416             ALOGV("getImageRectAtIndex: index(%d), colorFormat(%d), rect {%d, %d, %d, %d}",
417                     index, colorFormat, left, top, right, bottom);
418 #ifndef DISABLE_GROUP_SCHEDULE_HACK
419             setSchedPolicy(data);
420 #endif
421             sp<IMemory> bitmap = getImageRectAtIndex(
422                     index, colorFormat, left, top, right, bottom);
423             if (bitmap != 0) {  // Don't send NULL across the binder interface
424                 reply->writeInt32(NO_ERROR);
425                 reply->writeStrongBinder(IInterface::asBinder(bitmap));
426             } else {
427                 reply->writeInt32(UNKNOWN_ERROR);
428             }
429 #ifndef DISABLE_GROUP_SCHEDULE_HACK
430             restoreSchedPolicy();
431 #endif
432             return NO_ERROR;
433         } break;
434 
435         case GET_FRAME_AT_INDEX: {
436             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
437             int index = data.readInt32();
438             int colorFormat = data.readInt32();
439             bool metaOnly = (data.readInt32() != 0);
440             ALOGV("getFrameAtIndex: index(%d), colorFormat(%d), metaOnly(%d)",
441                     index, colorFormat, metaOnly);
442 #ifndef DISABLE_GROUP_SCHEDULE_HACK
443             setSchedPolicy(data);
444 #endif
445             sp<IMemory> frame = getFrameAtIndex(index, colorFormat, metaOnly);
446             if (frame != nullptr) {  // Don't send NULL across the binder interface
447                 reply->writeInt32(NO_ERROR);
448                 reply->writeStrongBinder(IInterface::asBinder(frame));
449             } else {
450                 reply->writeInt32(UNKNOWN_ERROR);
451             }
452 #ifndef DISABLE_GROUP_SCHEDULE_HACK
453             restoreSchedPolicy();
454 #endif
455             return NO_ERROR;
456         } break;
457         case EXTRACT_ALBUM_ART: {
458             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
459 #ifndef DISABLE_GROUP_SCHEDULE_HACK
460             setSchedPolicy(data);
461 #endif
462             sp<IMemory> albumArt = extractAlbumArt();
463             if (albumArt != 0) {  // Don't send NULL across the binder interface
464                 reply->writeInt32(NO_ERROR);
465                 reply->writeStrongBinder(IInterface::asBinder(albumArt));
466             } else {
467                 reply->writeInt32(UNKNOWN_ERROR);
468             }
469 #ifndef DISABLE_GROUP_SCHEDULE_HACK
470             restoreSchedPolicy();
471 #endif
472             return NO_ERROR;
473         } break;
474         case EXTRACT_METADATA: {
475             CHECK_INTERFACE(IMediaMetadataRetriever, data, reply);
476 #ifndef DISABLE_GROUP_SCHEDULE_HACK
477             setSchedPolicy(data);
478 #endif
479             int keyCode = data.readInt32();
480             const char* value = extractMetadata(keyCode);
481             if (value != NULL) {  // Don't send NULL across the binder interface
482                 reply->writeInt32(NO_ERROR);
483                 reply->writeCString(value);
484             } else {
485                 reply->writeInt32(UNKNOWN_ERROR);
486             }
487 #ifndef DISABLE_GROUP_SCHEDULE_HACK
488             restoreSchedPolicy();
489 #endif
490             return NO_ERROR;
491         } break;
492         default:
493             return BBinder::onTransact(code, data, reply, flags);
494     }
495 }
496 
497 // ----------------------------------------------------------------------------
498 
499 } // namespace android
500