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 //#define LOG_NDEBUG 0
19 #define LOG_TAG "MetadataRetrieverClient"
20 #include <utils/Log.h>
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <unistd.h>
26
27 #include <string.h>
28 #include <cutils/atomic.h>
29 #include <cutils/properties.h>
30 #include <binder/MemoryBase.h>
31 #include <binder/MemoryHeapBase.h>
32 #include <binder/IPCThreadState.h>
33 #include <binder/IServiceManager.h>
34 #include <media/DataSource.h>
35 #include <media/IMediaHTTPService.h>
36 #include <media/MediaMetadataRetrieverInterface.h>
37 #include <media/MediaPlayerInterface.h>
38 #include <media/stagefright/InterfaceUtils.h>
39 #include <media/stagefright/Utils.h>
40 #include <private/media/VideoFrame.h>
41 #include "MetadataRetrieverClient.h"
42 #include "StagefrightMetadataRetriever.h"
43 #include "MediaPlayerFactory.h"
44
45 namespace android {
46
MetadataRetrieverClient(pid_t pid)47 MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
48 {
49 ALOGV("MetadataRetrieverClient constructor pid(%d)", pid);
50 mPid = pid;
51 mAlbumArt = NULL;
52 mRetriever = NULL;
53 }
54
~MetadataRetrieverClient()55 MetadataRetrieverClient::~MetadataRetrieverClient()
56 {
57 ALOGV("MetadataRetrieverClient destructor");
58 disconnect();
59 }
60
dump(int fd,const Vector<String16> &)61 status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& /*args*/)
62 {
63 const size_t SIZE = 256;
64 char buffer[SIZE];
65 String8 result;
66 result.append(" MetadataRetrieverClient\n");
67 snprintf(buffer, 255, " pid(%d)\n", mPid);
68 result.append(buffer);
69 write(fd, result.string(), result.size());
70 write(fd, "\n", 1);
71 return NO_ERROR;
72 }
73
disconnect()74 void MetadataRetrieverClient::disconnect()
75 {
76 ALOGV("disconnect from pid %d", mPid);
77 Mutex::Autolock lock(mLock);
78 mRetriever.clear();
79 mAlbumArt.clear();
80 IPCThreadState::self()->flushCommands();
81 }
82
createRetriever(player_type playerType)83 static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
84 {
85 sp<MediaMetadataRetrieverBase> p;
86 switch (playerType) {
87 case STAGEFRIGHT_PLAYER:
88 case NU_PLAYER:
89 {
90 p = new StagefrightMetadataRetriever;
91 break;
92 }
93 default:
94 // TODO:
95 // support for TEST_PLAYER
96 ALOGE("player type %d is not supported", playerType);
97 break;
98 }
99 if (p == NULL) {
100 ALOGE("failed to create a retriever object");
101 }
102 return p;
103 }
104
setDataSource(const sp<IMediaHTTPService> & httpService,const char * url,const KeyedVector<String8,String8> * headers)105 status_t MetadataRetrieverClient::setDataSource(
106 const sp<IMediaHTTPService> &httpService,
107 const char *url,
108 const KeyedVector<String8, String8> *headers)
109 {
110 ALOGV("setDataSource(%s)", url);
111 Mutex::Autolock lock(mLock);
112 if (url == NULL) {
113 return UNKNOWN_ERROR;
114 }
115
116 // When asking the MediaPlayerFactory subsystem to choose a media player for
117 // a given URL, a pointer to an outer IMediaPlayer can be passed to the
118 // factory system to be taken into consideration along with the URL. In the
119 // case of choosing an instance of a MediaPlayerBase for a
120 // MetadataRetrieverClient, there is no outer IMediaPlayer which will
121 // eventually encapsulate the result of this selection. In this case, just
122 // pass NULL to getPlayerType to indicate that there is no outer
123 // IMediaPlayer to consider during selection.
124 player_type playerType =
125 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
126 ALOGV("player type = %d", playerType);
127 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
128 if (p == NULL) return NO_INIT;
129 status_t ret = p->setDataSource(httpService, url, headers);
130 if (ret == NO_ERROR) mRetriever = p;
131 return ret;
132 }
133
setDataSource(int fd,int64_t offset,int64_t length)134 status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
135 {
136 ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
137 fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
138 Mutex::Autolock lock(mLock);
139 struct stat sb;
140 int ret = fstat(fd, &sb);
141 if (ret != 0) {
142 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
143 return BAD_VALUE;
144 }
145 ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev));
146 ALOGV("st_mode = %u", sb.st_mode);
147 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
148 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
149 ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
150
151 if (offset >= sb.st_size) {
152 ALOGE("offset (%lld) bigger than file size (%llu)",
153 (long long)offset, (unsigned long long)sb.st_size);
154 return BAD_VALUE;
155 }
156 if (offset + length > sb.st_size) {
157 length = sb.st_size - offset;
158 ALOGV("calculated length = %lld", (long long)length);
159 }
160
161 player_type playerType =
162 MediaPlayerFactory::getPlayerType(NULL /* client */,
163 fd,
164 offset,
165 length);
166 ALOGV("player type = %d", playerType);
167 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
168 if (p == NULL) {
169 return NO_INIT;
170 }
171 status_t status = p->setDataSource(fd, offset, length);
172 if (status == NO_ERROR) mRetriever = p;
173 return status;
174 }
175
setDataSource(const sp<IDataSource> & source,const char * mime)176 status_t MetadataRetrieverClient::setDataSource(
177 const sp<IDataSource>& source, const char *mime)
178 {
179 ALOGV("setDataSource(IDataSource)");
180 Mutex::Autolock lock(mLock);
181
182 sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source);
183 player_type playerType =
184 MediaPlayerFactory::getPlayerType(NULL /* client */, dataSource);
185 ALOGV("player type = %d", playerType);
186 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
187 if (p == NULL) return NO_INIT;
188 status_t ret = p->setDataSource(dataSource, mime);
189 if (ret == NO_ERROR) mRetriever = p;
190 return ret;
191 }
192
193 Mutex MetadataRetrieverClient::sLock;
194
getFrameAtTime(int64_t timeUs,int option,int colorFormat,bool metaOnly)195 sp<IMemory> MetadataRetrieverClient::getFrameAtTime(
196 int64_t timeUs, int option, int colorFormat, bool metaOnly)
197 {
198 ALOGV("getFrameAtTime: time(%lld us) option(%d) colorFormat(%d), metaOnly(%d)",
199 (long long)timeUs, option, colorFormat, metaOnly);
200 Mutex::Autolock lock(mLock);
201 Mutex::Autolock glock(sLock);
202 if (mRetriever == NULL) {
203 ALOGE("retriever is not initialized");
204 return NULL;
205 }
206 sp<IMemory> frame = mRetriever->getFrameAtTime(timeUs, option, colorFormat, metaOnly);
207 if (frame == NULL) {
208 ALOGE("failed to capture a video frame");
209 return NULL;
210 }
211 return frame;
212 }
213
getImageAtIndex(int index,int colorFormat,bool metaOnly,bool thumbnail)214 sp<IMemory> MetadataRetrieverClient::getImageAtIndex(
215 int index, int colorFormat, bool metaOnly, bool thumbnail) {
216 ALOGV("getImageAtIndex: index(%d) colorFormat(%d), metaOnly(%d) thumbnail(%d)",
217 index, colorFormat, metaOnly, thumbnail);
218 Mutex::Autolock lock(mLock);
219 Mutex::Autolock glock(sLock);
220 if (mRetriever == NULL) {
221 ALOGE("retriever is not initialized");
222 return NULL;
223 }
224 sp<IMemory> frame = mRetriever->getImageAtIndex(index, colorFormat, metaOnly, thumbnail);
225 if (frame == NULL) {
226 ALOGE("failed to extract image");
227 return NULL;
228 }
229 return frame;
230 }
231
getImageRectAtIndex(int index,int colorFormat,int left,int top,int right,int bottom)232 sp<IMemory> MetadataRetrieverClient::getImageRectAtIndex(
233 int index, int colorFormat, int left, int top, int right, int bottom) {
234 ALOGV("getImageRectAtIndex: index(%d) colorFormat(%d), rect {%d, %d, %d, %d}",
235 index, colorFormat, left, top, right, bottom);
236 Mutex::Autolock lock(mLock);
237 Mutex::Autolock glock(sLock);
238 if (mRetriever == NULL) {
239 ALOGE("retriever is not initialized");
240 return NULL;
241 }
242 sp<IMemory> frame = mRetriever->getImageRectAtIndex(
243 index, colorFormat, left, top, right, bottom);
244 if (frame == NULL) {
245 ALOGE("failed to extract image");
246 return NULL;
247 }
248 return frame;
249 }
250
getFrameAtIndex(std::vector<sp<IMemory>> * frames,int frameIndex,int numFrames,int colorFormat,bool metaOnly)251 status_t MetadataRetrieverClient::getFrameAtIndex(
252 std::vector<sp<IMemory> > *frames,
253 int frameIndex, int numFrames, int colorFormat, bool metaOnly) {
254 ALOGV("getFrameAtIndex: frameIndex(%d), numFrames(%d), colorFormat(%d), metaOnly(%d)",
255 frameIndex, numFrames, colorFormat, metaOnly);
256 Mutex::Autolock lock(mLock);
257 Mutex::Autolock glock(sLock);
258 if (mRetriever == NULL) {
259 ALOGE("retriever is not initialized");
260 return INVALID_OPERATION;
261 }
262
263 status_t err = mRetriever->getFrameAtIndex(
264 frames, frameIndex, numFrames, colorFormat, metaOnly);
265 if (err != OK) {
266 frames->clear();
267 return err;
268 }
269 return OK;
270 }
271
extractAlbumArt()272 sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
273 {
274 ALOGV("extractAlbumArt");
275 Mutex::Autolock lock(mLock);
276 mAlbumArt.clear();
277 if (mRetriever == NULL) {
278 ALOGE("retriever is not initialized");
279 return NULL;
280 }
281 MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
282 if (albumArt == NULL) {
283 ALOGE("failed to extract an album art");
284 return NULL;
285 }
286 size_t size = sizeof(MediaAlbumArt) + albumArt->size();
287 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
288 if (heap == NULL) {
289 ALOGE("failed to create MemoryDealer object");
290 delete albumArt;
291 return NULL;
292 }
293 mAlbumArt = new MemoryBase(heap, 0, size);
294 if (mAlbumArt == NULL) {
295 ALOGE("not enough memory for MediaAlbumArt size=%zu", size);
296 delete albumArt;
297 return NULL;
298 }
299 MediaAlbumArt::init((MediaAlbumArt *) mAlbumArt->pointer(),
300 albumArt->size(), albumArt->data());
301 delete albumArt; // We've taken our copy.
302 return mAlbumArt;
303 }
304
extractMetadata(int keyCode)305 const char* MetadataRetrieverClient::extractMetadata(int keyCode)
306 {
307 ALOGV("extractMetadata");
308 Mutex::Autolock lock(mLock);
309 if (mRetriever == NULL) {
310 ALOGE("retriever is not initialized");
311 return NULL;
312 }
313 return mRetriever->extractMetadata(keyCode);
314 }
315
316 }; // namespace android
317