1 /*
2 **
3 ** Copyright 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 //#define LOG_NDEBUG 0
19 #define LOG_TAG "MediaMetadataRetriever"
20 
21 #include <inttypes.h>
22 
23 #include <binder/IServiceManager.h>
24 #include <binder/IPCThreadState.h>
25 #include <media/mediametadataretriever.h>
26 #include <media/IMediaHTTPService.h>
27 #include <media/IMediaPlayerService.h>
28 #include <utils/Log.h>
29 #include <dlfcn.h>
30 
31 namespace android {
32 
33 // client singleton for binder interface to service
34 Mutex MediaMetadataRetriever::sServiceLock;
35 sp<IMediaPlayerService> MediaMetadataRetriever::sService;
36 sp<MediaMetadataRetriever::DeathNotifier> MediaMetadataRetriever::sDeathNotifier;
37 
getService()38 const sp<IMediaPlayerService> MediaMetadataRetriever::getService()
39 {
40     Mutex::Autolock lock(sServiceLock);
41     if (sService == 0) {
42         sp<IServiceManager> sm = defaultServiceManager();
43         sp<IBinder> binder;
44         do {
45             binder = sm->getService(String16("media.player"));
46             if (binder != 0) {
47                 break;
48             }
49             ALOGW("MediaPlayerService not published, waiting...");
50             usleep(500000); // 0.5 s
51         } while (true);
52         if (sDeathNotifier == NULL) {
53             sDeathNotifier = new DeathNotifier();
54         }
55         binder->linkToDeath(sDeathNotifier);
56         sService = interface_cast<IMediaPlayerService>(binder);
57     }
58     ALOGE_IF(sService == 0, "no MediaPlayerService!?");
59     return sService;
60 }
61 
MediaMetadataRetriever()62 MediaMetadataRetriever::MediaMetadataRetriever()
63 {
64     ALOGV("constructor");
65     const sp<IMediaPlayerService> service(getService());
66     if (service == 0) {
67         ALOGE("failed to obtain MediaMetadataRetrieverService");
68         return;
69     }
70     sp<IMediaMetadataRetriever> retriever(service->createMetadataRetriever());
71     if (retriever == 0) {
72         ALOGE("failed to create IMediaMetadataRetriever object from server");
73     }
74     mRetriever = retriever;
75 }
76 
~MediaMetadataRetriever()77 MediaMetadataRetriever::~MediaMetadataRetriever()
78 {
79     ALOGV("destructor");
80     disconnect();
81     IPCThreadState::self()->flushCommands();
82 }
83 
disconnect()84 void MediaMetadataRetriever::disconnect()
85 {
86     ALOGV("disconnect");
87     sp<IMediaMetadataRetriever> retriever;
88     {
89         Mutex::Autolock _l(mLock);
90         retriever = mRetriever;
91         mRetriever.clear();
92     }
93     if (retriever != 0) {
94         retriever->disconnect();
95     }
96 }
97 
setDataSource(const sp<IMediaHTTPService> & httpService,const char * srcUrl,const KeyedVector<String8,String8> * headers)98 status_t MediaMetadataRetriever::setDataSource(
99         const sp<IMediaHTTPService> &httpService,
100         const char *srcUrl,
101         const KeyedVector<String8, String8> *headers)
102 {
103     ALOGV("setDataSource");
104     Mutex::Autolock _l(mLock);
105     if (mRetriever == 0) {
106         ALOGE("retriever is not initialized");
107         return INVALID_OPERATION;
108     }
109     if (srcUrl == NULL) {
110         ALOGE("data source is a null pointer");
111         return UNKNOWN_ERROR;
112     }
113     ALOGV("data source (%s)", srcUrl);
114     return mRetriever->setDataSource(httpService, srcUrl, headers);
115 }
116 
setDataSource(int fd,int64_t offset,int64_t length)117 status_t MediaMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
118 {
119     ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
120     Mutex::Autolock _l(mLock);
121     if (mRetriever == 0) {
122         ALOGE("retriever is not initialized");
123         return INVALID_OPERATION;
124     }
125     if (fd < 0 || offset < 0 || length < 0) {
126         ALOGE("Invalid negative argument");
127         return UNKNOWN_ERROR;
128     }
129     return mRetriever->setDataSource(fd, offset, length);
130 }
131 
setDataSource(const sp<IDataSource> & dataSource,const char * mime)132 status_t MediaMetadataRetriever::setDataSource(
133     const sp<IDataSource>& dataSource, const char *mime)
134 {
135     ALOGV("setDataSource(IDataSource)");
136     Mutex::Autolock _l(mLock);
137     if (mRetriever == 0) {
138         ALOGE("retriever is not initialized");
139         return INVALID_OPERATION;
140     }
141     return mRetriever->setDataSource(dataSource, mime);
142 }
143 
getFrameAtTime(int64_t timeUs,int option,int colorFormat,bool metaOnly)144 sp<IMemory> MediaMetadataRetriever::getFrameAtTime(
145         int64_t timeUs, int option, int colorFormat, bool metaOnly)
146 {
147     ALOGV("getFrameAtTime: time(%" PRId64 " us) option(%d) colorFormat(%d) metaOnly(%d)",
148             timeUs, option, colorFormat, metaOnly);
149     Mutex::Autolock _l(mLock);
150     if (mRetriever == 0) {
151         ALOGE("retriever is not initialized");
152         return NULL;
153     }
154     return mRetriever->getFrameAtTime(timeUs, option, colorFormat, metaOnly);
155 }
156 
getImageAtIndex(int index,int colorFormat,bool metaOnly,bool thumbnail)157 sp<IMemory> MediaMetadataRetriever::getImageAtIndex(
158         int index, int colorFormat, bool metaOnly, bool thumbnail) {
159     ALOGV("getImageAtIndex: index(%d) colorFormat(%d) metaOnly(%d) thumbnail(%d)",
160             index, colorFormat, metaOnly, thumbnail);
161     Mutex::Autolock _l(mLock);
162     if (mRetriever == 0) {
163         ALOGE("retriever is not initialized");
164         return NULL;
165     }
166     return mRetriever->getImageAtIndex(index, colorFormat, metaOnly, thumbnail);
167 }
168 
getImageRectAtIndex(int index,int colorFormat,int left,int top,int right,int bottom)169 sp<IMemory> MediaMetadataRetriever::getImageRectAtIndex(
170         int index, int colorFormat, int left, int top, int right, int bottom) {
171     ALOGV("getImageRectAtIndex: index(%d) colorFormat(%d) rect {%d, %d, %d, %d}",
172             index, colorFormat, left, top, right, bottom);
173     Mutex::Autolock _l(mLock);
174     if (mRetriever == 0) {
175         ALOGE("retriever is not initialized");
176         return NULL;
177     }
178     return mRetriever->getImageRectAtIndex(
179             index, colorFormat, left, top, right, bottom);
180 }
181 
getFrameAtIndex(int index,int colorFormat,bool metaOnly)182 sp<IMemory>  MediaMetadataRetriever::getFrameAtIndex(
183         int index, int colorFormat, bool metaOnly) {
184     ALOGV("getFrameAtIndex: index(%d), colorFormat(%d) metaOnly(%d)",
185             index, colorFormat, metaOnly);
186     Mutex::Autolock _l(mLock);
187     if (mRetriever == 0) {
188         ALOGE("retriever is not initialized");
189         return NULL;
190     }
191     return mRetriever->getFrameAtIndex(index, colorFormat, metaOnly);
192 }
193 
extractMetadata(int keyCode)194 const char* MediaMetadataRetriever::extractMetadata(int keyCode)
195 {
196     ALOGV("extractMetadata(%d)", keyCode);
197     Mutex::Autolock _l(mLock);
198     if (mRetriever == 0) {
199         ALOGE("retriever is not initialized");
200         return NULL;
201     }
202     return mRetriever->extractMetadata(keyCode);
203 }
204 
extractAlbumArt()205 sp<IMemory> MediaMetadataRetriever::extractAlbumArt()
206 {
207     ALOGV("extractAlbumArt");
208     Mutex::Autolock _l(mLock);
209     if (mRetriever == 0) {
210         ALOGE("retriever is not initialized");
211         return NULL;
212     }
213     return mRetriever->extractAlbumArt();
214 }
215 
binderDied(const wp<IBinder> & who __unused)216 void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who __unused) {
217     Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock);
218     MediaMetadataRetriever::sService.clear();
219     ALOGW("MediaMetadataRetriever server died!");
220 }
221 
~DeathNotifier()222 MediaMetadataRetriever::DeathNotifier::~DeathNotifier()
223 {
224     Mutex::Autolock lock(sServiceLock);
225     if (sService != 0) {
226         IInterface::asBinder(sService)->unlinkToDeath(this);
227     }
228 }
229 
230 } // namespace android
231