blob: 3572b52e8c1dbcbd34b7067c51767fb312b35f5c [file] [log] [blame]
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08001/*
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>
Dave Sparks7c0d20a2009-10-26 16:28:26 -070024#include <sys/resource.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080025#include <dirent.h>
26#include <unistd.h>
27
28#include <string.h>
29#include <cutils/atomic.h>
Mathias Agopian07952722009-05-19 19:08:10 -070030#include <binder/MemoryDealer.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080031#include <android_runtime/ActivityManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080034#include <media/MediaMetadataRetrieverInterface.h>
35#include <media/MediaPlayerInterface.h>
36#include <media/PVMetadataRetriever.h>
37#include <private/media/VideoFrame.h>
James Dong392ff3b2009-09-06 14:29:45 -070038#include "VorbisMetadataRetriever.h"
39#include "MidiMetadataRetriever.h"
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080040#include "MetadataRetrieverClient.h"
Andreas Huber53a76bd2009-10-06 16:20:44 -070041#include "StagefrightMetadataRetriever.h"
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080042
Dave Sparks7c0d20a2009-10-26 16:28:26 -070043/* desktop Linux needs a little help with gettid() */
44#if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS)
45#define __KERNEL__
46# include <linux/unistd.h>
47#ifdef _syscall0
48_syscall0(pid_t,gettid)
49#else
50pid_t gettid() { return syscall(__NR_gettid);}
51#endif
52#undef __KERNEL__
53#endif
54
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080055namespace android {
56
James Dong392ff3b2009-09-06 14:29:45 -070057extern player_type getPlayerType(const char* url);
58extern player_type getPlayerType(int fd, int64_t offset, int64_t length);
59
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080060MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
61{
62 LOGV("MetadataRetrieverClient constructor pid(%d)", pid);
63 mPid = pid;
64 mThumbnailDealer = NULL;
65 mAlbumArtDealer = NULL;
66 mThumbnail = NULL;
67 mAlbumArt = NULL;
Jean-Baptiste Queru680f8c72009-03-21 11:40:18 -070068 mRetriever = NULL;
James Dong67fc2342009-09-29 10:45:27 -070069 mMode = METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080070}
71
72MetadataRetrieverClient::~MetadataRetrieverClient()
73{
74 LOGV("MetadataRetrieverClient destructor");
75 disconnect();
76}
77
78status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& args) const
79{
80 const size_t SIZE = 256;
81 char buffer[SIZE];
82 String8 result;
83 result.append(" MetadataRetrieverClient\n");
James Dong67fc2342009-09-29 10:45:27 -070084 snprintf(buffer, 255, " pid(%d) mode(%d)\n", mPid, mMode);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080085 result.append(buffer);
86 write(fd, result.string(), result.size());
87 write(fd, "\n", 1);
88 return NO_ERROR;
89}
90
91void MetadataRetrieverClient::disconnect()
92{
93 LOGV("disconnect from pid %d", mPid);
94 Mutex::Autolock lock(mLock);
95 mRetriever.clear();
96 mThumbnailDealer.clear();
97 mAlbumArtDealer.clear();
98 mThumbnail.clear();
99 mAlbumArt.clear();
James Dong67fc2342009-09-29 10:45:27 -0700100 mMode = METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800101 IPCThreadState::self()->flushCommands();
102}
103
James Dong392ff3b2009-09-06 14:29:45 -0700104static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
105{
106 sp<MediaMetadataRetrieverBase> p;
107 switch (playerType) {
Andreas Huber6a3a0182009-12-17 13:31:13 -0800108#if BUILD_WITH_FULL_STAGEFRIGHT
109 case STAGEFRIGHT_PLAYER:
110 // For now we are going to keep using PV for meta-data support
111 // until stagefright is up to par.
112
113 // LOGV("create StagefrightMetadataRetriever");
114 // p = new StagefrightMetadataRetriever;
115 // break;
116
117 // fall through to PV_PLAYER
118#endif
James Dong392ff3b2009-09-06 14:29:45 -0700119#ifndef NO_OPENCORE
120 case PV_PLAYER:
121 LOGV("create pv metadata retriever");
122 p = new PVMetadataRetriever();
123 break;
124#endif
125 case VORBIS_PLAYER:
126 LOGV("create vorbis metadata retriever");
127 p = new VorbisMetadataRetriever();
128 break;
129 case SONIVOX_PLAYER:
130 LOGV("create midi metadata retriever");
131 p = new MidiMetadataRetriever();
132 break;
133 default:
134 // TODO:
Andreas Huber53a76bd2009-10-06 16:20:44 -0700135 // support for TEST_PLAYER
James Dong392ff3b2009-09-06 14:29:45 -0700136 LOGE("player type %d is not supported", playerType);
137 break;
138 }
139 if (p == NULL) {
140 LOGE("failed to create a retriever object");
141 }
142 return p;
143}
144
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800145status_t MetadataRetrieverClient::setDataSource(const char *url)
146{
147 LOGV("setDataSource(%s)", url);
148 Mutex::Autolock lock(mLock);
149 if (url == NULL) {
150 return UNKNOWN_ERROR;
151 }
James Dong392ff3b2009-09-06 14:29:45 -0700152 player_type playerType = getPlayerType(url);
153 LOGV("player type = %d", playerType);
154 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
155 if (p == NULL) return NO_INIT;
James Dong67fc2342009-09-29 10:45:27 -0700156 status_t ret = p->setMode(mMode);
157 if (ret == NO_ERROR) {
158 ret = p->setDataSource(url);
159 }
James Dong392ff3b2009-09-06 14:29:45 -0700160 if (ret == NO_ERROR) mRetriever = p;
161 return ret;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800162}
163
164status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
165{
166 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
167 Mutex::Autolock lock(mLock);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800168 struct stat sb;
169 int ret = fstat(fd, &sb);
170 if (ret != 0) {
171 LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
James Dong392ff3b2009-09-06 14:29:45 -0700172 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800173 }
174 LOGV("st_dev = %llu", sb.st_dev);
175 LOGV("st_mode = %u", sb.st_mode);
176 LOGV("st_uid = %lu", sb.st_uid);
177 LOGV("st_gid = %lu", sb.st_gid);
178 LOGV("st_size = %llu", sb.st_size);
179
180 if (offset >= sb.st_size) {
181 LOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size);
182 ::close(fd);
James Dong392ff3b2009-09-06 14:29:45 -0700183 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800184 }
185 if (offset + length > sb.st_size) {
186 length = sb.st_size - offset;
James Dong392ff3b2009-09-06 14:29:45 -0700187 LOGV("calculated length = %lld", length);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800188 }
James Dong392ff3b2009-09-06 14:29:45 -0700189
190 player_type playerType = getPlayerType(fd, offset, length);
191 LOGV("player type = %d", playerType);
192 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
193 if (p == NULL) {
194 ::close(fd);
195 return NO_INIT;
196 }
James Dong67fc2342009-09-29 10:45:27 -0700197 status_t status = p->setMode(mMode);
198 if (status == NO_ERROR) {
199 p->setDataSource(fd, offset, length);
200 }
James Dong392ff3b2009-09-06 14:29:45 -0700201 if (status == NO_ERROR) mRetriever = p;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800202 ::close(fd);
203 return status;
204}
205
206status_t MetadataRetrieverClient::setMode(int mode)
207{
208 LOGV("setMode");
209 Mutex::Autolock lock(mLock);
James Dong67fc2342009-09-29 10:45:27 -0700210 if (mode < METADATA_MODE_NOOP ||
211 mode > METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL) {
212 LOGE("invalid mode %d", mode);
213 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800214 }
James Dong67fc2342009-09-29 10:45:27 -0700215 mMode = mode;
216 return NO_ERROR;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800217}
218
219status_t MetadataRetrieverClient::getMode(int* mode) const
220{
221 LOGV("getMode");
222 Mutex::Autolock lock(mLock);
James Dong67fc2342009-09-29 10:45:27 -0700223
224 // TODO:
225 // This may not be necessary.
226 // If setDataSource() has not been called, return the cached value
227 // otherwise, return the value retrieved from the retriever
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800228 if (mRetriever == NULL) {
James Dong67fc2342009-09-29 10:45:27 -0700229 *mode = mMode;
230 } else {
231 mRetriever->getMode(mode);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800232 }
James Dong67fc2342009-09-29 10:45:27 -0700233 return NO_ERROR;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800234}
235
236sp<IMemory> MetadataRetrieverClient::captureFrame()
237{
238 LOGV("captureFrame");
239 Mutex::Autolock lock(mLock);
240 mThumbnail.clear();
241 mThumbnailDealer.clear();
242 if (mRetriever == NULL) {
243 LOGE("retriever is not initialized");
244 return NULL;
245 }
246 VideoFrame *frame = mRetriever->captureFrame();
247 if (frame == NULL) {
248 LOGE("failed to capture a video frame");
249 return NULL;
250 }
251 size_t size = sizeof(VideoFrame) + frame->mSize;
252 mThumbnailDealer = new MemoryDealer(size);
253 if (mThumbnailDealer == NULL) {
254 LOGE("failed to create MemoryDealer");
255 delete frame;
256 return NULL;
257 }
258 mThumbnail = mThumbnailDealer->allocate(size);
259 if (mThumbnail == NULL) {
260 LOGE("not enough memory for VideoFrame size=%u", size);
261 mThumbnailDealer.clear();
262 delete frame;
263 return NULL;
264 }
265 VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer());
266 frameCopy->mWidth = frame->mWidth;
267 frameCopy->mHeight = frame->mHeight;
268 frameCopy->mDisplayWidth = frame->mDisplayWidth;
269 frameCopy->mDisplayHeight = frame->mDisplayHeight;
270 frameCopy->mSize = frame->mSize;
271 frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame);
272 memcpy(frameCopy->mData, frame->mData, frame->mSize);
273 delete frame; // Fix memory leakage
274 return mThumbnail;
275}
276
277sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
278{
279 LOGV("extractAlbumArt");
280 Mutex::Autolock lock(mLock);
281 mAlbumArt.clear();
282 mAlbumArtDealer.clear();
283 if (mRetriever == NULL) {
284 LOGE("retriever is not initialized");
285 return NULL;
286 }
287 MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
288 if (albumArt == NULL) {
289 LOGE("failed to extract an album art");
290 return NULL;
291 }
292 size_t size = sizeof(MediaAlbumArt) + albumArt->mSize;
293 mAlbumArtDealer = new MemoryDealer(size);
294 if (mAlbumArtDealer == NULL) {
295 LOGE("failed to create MemoryDealer object");
296 delete albumArt;
297 return NULL;
298 }
299 mAlbumArt = mAlbumArtDealer->allocate(size);
300 if (mAlbumArt == NULL) {
301 LOGE("not enough memory for MediaAlbumArt size=%u", size);
302 mAlbumArtDealer.clear();
303 delete albumArt;
304 return NULL;
305 }
306 MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer());
307 albumArtCopy->mSize = albumArt->mSize;
308 albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt);
309 memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize);
310 delete albumArt; // Fix memory leakage
311 return mAlbumArt;
312}
313
314const char* MetadataRetrieverClient::extractMetadata(int keyCode)
315{
316 LOGV("extractMetadata");
317 Mutex::Autolock lock(mLock);
318 if (mRetriever == NULL) {
319 LOGE("retriever is not initialized");
320 return NULL;
321 }
322 return mRetriever->extractMetadata(keyCode);
323}
324
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800325}; // namespace android