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
38 Mutex MediaMetadataRetriever::sLock;
39
getService()40 const sp<IMediaPlayerService> MediaMetadataRetriever::getService()
41 {
42 Mutex::Autolock lock(sServiceLock);
43 if (sService == 0) {
44 sp<IServiceManager> sm = defaultServiceManager();
45 sp<IBinder> binder;
46 binder = sm->waitForService(String16("media.player"));
47 if (binder == nullptr) {
48 return nullptr;
49 }
50 if (sDeathNotifier == NULL) {
51 sDeathNotifier = new DeathNotifier();
52 }
53 binder->linkToDeath(sDeathNotifier);
54 sService = interface_cast<IMediaPlayerService>(binder);
55 }
56 ALOGE_IF(sService == 0, "no MediaPlayerService!?");
57 return sService;
58 }
59
MediaMetadataRetriever()60 MediaMetadataRetriever::MediaMetadataRetriever()
61 {
62 ALOGV("constructor");
63 const sp<IMediaPlayerService> service(getService());
64 if (service == 0) {
65 ALOGE("failed to obtain MediaMetadataRetrieverService");
66 return;
67 }
68 sp<IMediaMetadataRetriever> retriever(service->createMetadataRetriever());
69 if (retriever == 0) {
70 ALOGE("failed to create IMediaMetadataRetriever object from server");
71 }
72 mRetriever = retriever;
73 }
74
~MediaMetadataRetriever()75 MediaMetadataRetriever::~MediaMetadataRetriever()
76 {
77 ALOGV("destructor");
78 disconnect();
79 IPCThreadState::self()->flushCommands();
80 }
81
disconnect()82 void MediaMetadataRetriever::disconnect()
83 {
84 ALOGV("disconnect");
85 sp<IMediaMetadataRetriever> retriever;
86 {
87 Mutex::Autolock _l(mLock);
88 retriever = mRetriever;
89 mRetriever.clear();
90 }
91 if (retriever != 0) {
92 retriever->disconnect();
93 }
94 }
95
setDataSource(const sp<IMediaHTTPService> & httpService,const char * srcUrl,const KeyedVector<String8,String8> * headers)96 status_t MediaMetadataRetriever::setDataSource(
97 const sp<IMediaHTTPService> &httpService,
98 const char *srcUrl,
99 const KeyedVector<String8, String8> *headers)
100 {
101 ALOGV("setDataSource");
102 Mutex::Autolock _l(mLock);
103 if (mRetriever == 0) {
104 ALOGE("retriever is not initialized");
105 return INVALID_OPERATION;
106 }
107 if (srcUrl == NULL) {
108 ALOGE("data source is a null pointer");
109 return UNKNOWN_ERROR;
110 }
111 ALOGV("data source (%s)", srcUrl);
112 return mRetriever->setDataSource(httpService, srcUrl, headers);
113 }
114
setDataSource(int fd,int64_t offset,int64_t length)115 status_t MediaMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
116 {
117 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
118 Mutex::Autolock _l(mLock);
119 if (mRetriever == 0) {
120 ALOGE("retriever is not initialized");
121 return INVALID_OPERATION;
122 }
123 if (fd < 0 || offset < 0 || length < 0) {
124 ALOGE("Invalid negative argument");
125 return UNKNOWN_ERROR;
126 }
127 return mRetriever->setDataSource(fd, offset, length);
128 }
129
setDataSource(const sp<IDataSource> & dataSource,const char * mime)130 status_t MediaMetadataRetriever::setDataSource(
131 const sp<IDataSource>& dataSource, const char *mime)
132 {
133 ALOGV("setDataSource(IDataSource)");
134 Mutex::Autolock _l(mLock);
135 if (mRetriever == 0) {
136 ALOGE("retriever is not initialized");
137 return INVALID_OPERATION;
138 }
139 return mRetriever->setDataSource(dataSource, mime);
140 }
141
getFrameAtTime(int64_t timeUs,int option,int colorFormat,bool metaOnly)142 sp<IMemory> MediaMetadataRetriever::getFrameAtTime(
143 int64_t timeUs, int option, int colorFormat, bool metaOnly)
144 {
145 ALOGV("getFrameAtTime: time(%" PRId64 " us) option(%d) colorFormat(%d) metaOnly(%d)",
146 timeUs, option, colorFormat, metaOnly);
147 Mutex::Autolock _l(mLock);
148 Mutex::Autolock _gLock(sLock);
149 if (mRetriever == 0) {
150 ALOGE("retriever is not initialized");
151 return NULL;
152 }
153 return mRetriever->getFrameAtTime(timeUs, option, colorFormat, metaOnly);
154 }
155
getImageAtIndex(int index,int colorFormat,bool metaOnly,bool thumbnail)156 sp<IMemory> MediaMetadataRetriever::getImageAtIndex(
157 int index, int colorFormat, bool metaOnly, bool thumbnail) {
158 ALOGV("getImageAtIndex: index(%d) colorFormat(%d) metaOnly(%d) thumbnail(%d)",
159 index, colorFormat, metaOnly, thumbnail);
160 Mutex::Autolock _l(mLock);
161 Mutex::Autolock _gLock(sLock);
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 Mutex::Autolock _gLock(sLock);
175 if (mRetriever == 0) {
176 ALOGE("retriever is not initialized");
177 return NULL;
178 }
179 return mRetriever->getImageRectAtIndex(
180 index, colorFormat, left, top, right, bottom);
181 }
182
getFrameAtIndex(int index,int colorFormat,bool metaOnly)183 sp<IMemory> MediaMetadataRetriever::getFrameAtIndex(
184 int index, int colorFormat, bool metaOnly) {
185 ALOGV("getFrameAtIndex: index(%d), colorFormat(%d) metaOnly(%d)",
186 index, colorFormat, metaOnly);
187 Mutex::Autolock _l(mLock);
188 Mutex::Autolock _gLock(sLock);
189 if (mRetriever == 0) {
190 ALOGE("retriever is not initialized");
191 return NULL;
192 }
193 return mRetriever->getFrameAtIndex(index, 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