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