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(std::vector<sp<IMemory>> * frames,int frameIndex,int numFrames,int colorFormat,bool metaOnly)182 status_t MediaMetadataRetriever::getFrameAtIndex(
183         std::vector<sp<IMemory> > *frames,
184         int frameIndex, int numFrames, int colorFormat, bool metaOnly) {
185     ALOGV("getFrameAtIndex: frameIndex(%d), numFrames(%d), colorFormat(%d) metaOnly(%d)",
186             frameIndex, numFrames, colorFormat, metaOnly);
187     Mutex::Autolock _l(mLock);
188     if (mRetriever == 0) {
189         ALOGE("retriever is not initialized");
190         return INVALID_OPERATION;
191     }
192     return mRetriever->getFrameAtIndex(
193             frames, frameIndex, numFrames, colorFormat, metaOnly);
194 }
195 
extractMetadata(int keyCode)196 const char* MediaMetadataRetriever::extractMetadata(int keyCode)
197 {
198     ALOGV("extractMetadata(%d)", keyCode);
199     Mutex::Autolock _l(mLock);
200     if (mRetriever == 0) {
201         ALOGE("retriever is not initialized");
202         return NULL;
203     }
204     return mRetriever->extractMetadata(keyCode);
205 }
206 
extractAlbumArt()207 sp<IMemory> MediaMetadataRetriever::extractAlbumArt()
208 {
209     ALOGV("extractAlbumArt");
210     Mutex::Autolock _l(mLock);
211     if (mRetriever == 0) {
212         ALOGE("retriever is not initialized");
213         return NULL;
214     }
215     return mRetriever->extractAlbumArt();
216 }
217 
binderDied(const wp<IBinder> & who __unused)218 void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who __unused) {
219     Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock);
220     MediaMetadataRetriever::sService.clear();
221     ALOGW("MediaMetadataRetriever server died!");
222 }
223 
~DeathNotifier()224 MediaMetadataRetriever::DeathNotifier::~DeathNotifier()
225 {
226     Mutex::Autolock lock(sServiceLock);
227     if (sService != 0) {
228         IInterface::asBinder(sService)->unlinkToDeath(this);
229     }
230 }
231 
232 } // namespace android
233