blob: 2cdc351b0f8760e3170c72f70bb691947814735b [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"
41
Dave Sparks7c0d20a2009-10-26 16:28:26 -070042/* desktop Linux needs a little help with gettid() */
43#if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS)
44#define __KERNEL__
45# include <linux/unistd.h>
46#ifdef _syscall0
47_syscall0(pid_t,gettid)
48#else
49pid_t gettid() { return syscall(__NR_gettid);}
50#endif
51#undef __KERNEL__
52#endif
53
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080054namespace android {
55
James Dong392ff3b2009-09-06 14:29:45 -070056extern player_type getPlayerType(const char* url);
57extern player_type getPlayerType(int fd, int64_t offset, int64_t length);
58
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080059MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
60{
61 LOGV("MetadataRetrieverClient constructor pid(%d)", pid);
62 mPid = pid;
63 mThumbnailDealer = NULL;
64 mAlbumArtDealer = NULL;
65 mThumbnail = NULL;
66 mAlbumArt = NULL;
Jean-Baptiste Queru680f8c72009-03-21 11:40:18 -070067 mRetriever = NULL;
James Dong67fc2342009-09-29 10:45:27 -070068 mMode = METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080069}
70
71MetadataRetrieverClient::~MetadataRetrieverClient()
72{
73 LOGV("MetadataRetrieverClient destructor");
74 disconnect();
75}
76
77status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& args) const
78{
79 const size_t SIZE = 256;
80 char buffer[SIZE];
81 String8 result;
82 result.append(" MetadataRetrieverClient\n");
James Dong67fc2342009-09-29 10:45:27 -070083 snprintf(buffer, 255, " pid(%d) mode(%d)\n", mPid, mMode);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080084 result.append(buffer);
85 write(fd, result.string(), result.size());
86 write(fd, "\n", 1);
87 return NO_ERROR;
88}
89
90void MetadataRetrieverClient::disconnect()
91{
92 LOGV("disconnect from pid %d", mPid);
93 Mutex::Autolock lock(mLock);
94 mRetriever.clear();
95 mThumbnailDealer.clear();
96 mAlbumArtDealer.clear();
97 mThumbnail.clear();
98 mAlbumArt.clear();
James Dong67fc2342009-09-29 10:45:27 -070099 mMode = METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800100 IPCThreadState::self()->flushCommands();
101}
102
James Dong392ff3b2009-09-06 14:29:45 -0700103static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
104{
105 sp<MediaMetadataRetrieverBase> p;
106 switch (playerType) {
107#ifndef NO_OPENCORE
108 case PV_PLAYER:
109 LOGV("create pv metadata retriever");
110 p = new PVMetadataRetriever();
111 break;
112#endif
113 case VORBIS_PLAYER:
114 LOGV("create vorbis metadata retriever");
115 p = new VorbisMetadataRetriever();
116 break;
117 case SONIVOX_PLAYER:
118 LOGV("create midi metadata retriever");
119 p = new MidiMetadataRetriever();
120 break;
121 default:
122 // TODO:
123 // support for STAGEFRIGHT_PLAYER and TEST_PLAYER
124 LOGE("player type %d is not supported", playerType);
125 break;
126 }
127 if (p == NULL) {
128 LOGE("failed to create a retriever object");
129 }
130 return p;
131}
132
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800133status_t MetadataRetrieverClient::setDataSource(const char *url)
134{
135 LOGV("setDataSource(%s)", url);
136 Mutex::Autolock lock(mLock);
137 if (url == NULL) {
138 return UNKNOWN_ERROR;
139 }
James Dong392ff3b2009-09-06 14:29:45 -0700140 player_type playerType = getPlayerType(url);
Andreas Huberdcaa2202009-11-05 12:04:22 -0800141#if !defined(NO_OPENCORE) && defined(BUILD_WITH_FULL_STAGEFRIGHT)
142 if (playerType == STAGEFRIGHT_PLAYER) {
143 // Stagefright doesn't support metadata in this branch yet.
144 playerType = PV_PLAYER;
145 }
146#endif
James Dong392ff3b2009-09-06 14:29:45 -0700147 LOGV("player type = %d", playerType);
148 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
149 if (p == NULL) return NO_INIT;
James Dong67fc2342009-09-29 10:45:27 -0700150 status_t ret = p->setMode(mMode);
151 if (ret == NO_ERROR) {
152 ret = p->setDataSource(url);
153 }
James Dong392ff3b2009-09-06 14:29:45 -0700154 if (ret == NO_ERROR) mRetriever = p;
155 return ret;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800156}
157
158status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
159{
160 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
161 Mutex::Autolock lock(mLock);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800162 struct stat sb;
163 int ret = fstat(fd, &sb);
164 if (ret != 0) {
165 LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
James Dong392ff3b2009-09-06 14:29:45 -0700166 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800167 }
168 LOGV("st_dev = %llu", sb.st_dev);
169 LOGV("st_mode = %u", sb.st_mode);
170 LOGV("st_uid = %lu", sb.st_uid);
171 LOGV("st_gid = %lu", sb.st_gid);
172 LOGV("st_size = %llu", sb.st_size);
173
174 if (offset >= sb.st_size) {
175 LOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size);
176 ::close(fd);
James Dong392ff3b2009-09-06 14:29:45 -0700177 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800178 }
179 if (offset + length > sb.st_size) {
180 length = sb.st_size - offset;
James Dong392ff3b2009-09-06 14:29:45 -0700181 LOGV("calculated length = %lld", length);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800182 }
James Dong392ff3b2009-09-06 14:29:45 -0700183
184 player_type playerType = getPlayerType(fd, offset, length);
Andreas Huberdcaa2202009-11-05 12:04:22 -0800185#if !defined(NO_OPENCORE) && defined(BUILD_WITH_FULL_STAGEFRIGHT)
186 if (playerType == STAGEFRIGHT_PLAYER) {
187 // Stagefright doesn't support metadata in this branch yet.
188 playerType = PV_PLAYER;
189 }
190#endif
James Dong392ff3b2009-09-06 14:29:45 -0700191 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);
Dave Sparks7c0d20a2009-10-26 16:28:26 -0700240 Priority priority(ANDROID_PRIORITY_BACKGROUND);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800241 mThumbnail.clear();
242 mThumbnailDealer.clear();
243 if (mRetriever == NULL) {
244 LOGE("retriever is not initialized");
245 return NULL;
246 }
247 VideoFrame *frame = mRetriever->captureFrame();
248 if (frame == NULL) {
249 LOGE("failed to capture a video frame");
250 return NULL;
251 }
252 size_t size = sizeof(VideoFrame) + frame->mSize;
253 mThumbnailDealer = new MemoryDealer(size);
254 if (mThumbnailDealer == NULL) {
255 LOGE("failed to create MemoryDealer");
256 delete frame;
257 return NULL;
258 }
259 mThumbnail = mThumbnailDealer->allocate(size);
260 if (mThumbnail == NULL) {
261 LOGE("not enough memory for VideoFrame size=%u", size);
262 mThumbnailDealer.clear();
263 delete frame;
264 return NULL;
265 }
266 VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer());
267 frameCopy->mWidth = frame->mWidth;
268 frameCopy->mHeight = frame->mHeight;
269 frameCopy->mDisplayWidth = frame->mDisplayWidth;
270 frameCopy->mDisplayHeight = frame->mDisplayHeight;
271 frameCopy->mSize = frame->mSize;
272 frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame);
273 memcpy(frameCopy->mData, frame->mData, frame->mSize);
274 delete frame; // Fix memory leakage
275 return mThumbnail;
276}
277
278sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
279{
280 LOGV("extractAlbumArt");
281 Mutex::Autolock lock(mLock);
Dave Sparks7c0d20a2009-10-26 16:28:26 -0700282 Priority priority(ANDROID_PRIORITY_BACKGROUND);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800283 mAlbumArt.clear();
284 mAlbumArtDealer.clear();
285 if (mRetriever == NULL) {
286 LOGE("retriever is not initialized");
287 return NULL;
288 }
289 MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
290 if (albumArt == NULL) {
291 LOGE("failed to extract an album art");
292 return NULL;
293 }
294 size_t size = sizeof(MediaAlbumArt) + albumArt->mSize;
295 mAlbumArtDealer = new MemoryDealer(size);
296 if (mAlbumArtDealer == NULL) {
297 LOGE("failed to create MemoryDealer object");
298 delete albumArt;
299 return NULL;
300 }
301 mAlbumArt = mAlbumArtDealer->allocate(size);
302 if (mAlbumArt == NULL) {
303 LOGE("not enough memory for MediaAlbumArt size=%u", size);
304 mAlbumArtDealer.clear();
305 delete albumArt;
306 return NULL;
307 }
308 MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer());
309 albumArtCopy->mSize = albumArt->mSize;
310 albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt);
311 memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize);
312 delete albumArt; // Fix memory leakage
313 return mAlbumArt;
314}
315
316const char* MetadataRetrieverClient::extractMetadata(int keyCode)
317{
318 LOGV("extractMetadata");
319 Mutex::Autolock lock(mLock);
320 if (mRetriever == NULL) {
321 LOGE("retriever is not initialized");
322 return NULL;
323 }
Dave Sparks7c0d20a2009-10-26 16:28:26 -0700324 Priority priority(ANDROID_PRIORITY_BACKGROUND);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800325 return mRetriever->extractMetadata(keyCode);
326}
327
Dave Sparks7c0d20a2009-10-26 16:28:26 -0700328MetadataRetrieverClient::Priority::Priority(int newPriority)
329{
330 mOldPriority = getpriority(PRIO_PROCESS, 0);
331 setpriority(PRIO_PROCESS, 0, newPriority);
332}
333
334MetadataRetrieverClient::Priority::~Priority()
335{
336 setpriority(PRIO_PROCESS, 0, mOldPriority);
337}
338
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800339}; // namespace android