blob: ca229fa52ba286617d495e16a0910f0f0ef87031 [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>
Andreas Huber7782fdf2010-01-04 15:02:02 -080030#include <cutils/properties.h>
Mathias Agopian1875b7e2010-01-29 17:16:30 -080031#include <binder/MemoryBase.h>
32#include <binder/MemoryHeapBase.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080033#include <android_runtime/ActivityManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070034#include <binder/IPCThreadState.h>
35#include <binder/IServiceManager.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080036#include <media/MediaMetadataRetrieverInterface.h>
37#include <media/MediaPlayerInterface.h>
38#include <media/PVMetadataRetriever.h>
39#include <private/media/VideoFrame.h>
James Dong392ff3b2009-09-06 14:29:45 -070040#include "MidiMetadataRetriever.h"
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080041#include "MetadataRetrieverClient.h"
Andreas Huber53a76bd2009-10-06 16:20:44 -070042#include "StagefrightMetadataRetriever.h"
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080043
Dave Sparks7c0d20a2009-10-26 16:28:26 -070044/* desktop Linux needs a little help with gettid() */
45#if defined(HAVE_GETTID) && !defined(HAVE_ANDROID_OS)
46#define __KERNEL__
47# include <linux/unistd.h>
48#ifdef _syscall0
49_syscall0(pid_t,gettid)
50#else
51pid_t gettid() { return syscall(__NR_gettid);}
52#endif
53#undef __KERNEL__
54#endif
55
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080056namespace android {
57
James Dong392ff3b2009-09-06 14:29:45 -070058extern player_type getPlayerType(const char* url);
59extern player_type getPlayerType(int fd, int64_t offset, int64_t length);
60
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080061MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
62{
63 LOGV("MetadataRetrieverClient constructor pid(%d)", pid);
64 mPid = pid;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080065 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();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080095 mThumbnail.clear();
96 mAlbumArt.clear();
James Dong67fc2342009-09-29 10:45:27 -070097 mMode = METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080098 IPCThreadState::self()->flushCommands();
99}
100
James Dong392ff3b2009-09-06 14:29:45 -0700101static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
102{
103 sp<MediaMetadataRetrieverBase> p;
104 switch (playerType) {
Andreas Huber6a3a0182009-12-17 13:31:13 -0800105 case STAGEFRIGHT_PLAYER:
Andreas Huber7782fdf2010-01-04 15:02:02 -0800106 {
Andreas Huber8d65dd22010-06-23 16:40:57 -0700107 p = new StagefrightMetadataRetriever;
108 break;
Andreas Huber7782fdf2010-01-04 15:02:02 -0800109 }
James Dong392ff3b2009-09-06 14:29:45 -0700110#ifndef NO_OPENCORE
111 case PV_PLAYER:
112 LOGV("create pv metadata retriever");
113 p = new PVMetadataRetriever();
114 break;
115#endif
James Dong392ff3b2009-09-06 14:29:45 -0700116 case SONIVOX_PLAYER:
117 LOGV("create midi metadata retriever");
118 p = new MidiMetadataRetriever();
119 break;
120 default:
121 // TODO:
Andreas Huber53a76bd2009-10-06 16:20:44 -0700122 // support for TEST_PLAYER
James Dong392ff3b2009-09-06 14:29:45 -0700123 LOGE("player type %d is not supported", playerType);
124 break;
125 }
126 if (p == NULL) {
127 LOGE("failed to create a retriever object");
128 }
129 return p;
130}
131
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800132status_t MetadataRetrieverClient::setDataSource(const char *url)
133{
134 LOGV("setDataSource(%s)", url);
135 Mutex::Autolock lock(mLock);
136 if (url == NULL) {
137 return UNKNOWN_ERROR;
138 }
James Dong392ff3b2009-09-06 14:29:45 -0700139 player_type playerType = getPlayerType(url);
140 LOGV("player type = %d", playerType);
141 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
142 if (p == NULL) return NO_INIT;
James Dong67fc2342009-09-29 10:45:27 -0700143 status_t ret = p->setMode(mMode);
144 if (ret == NO_ERROR) {
145 ret = p->setDataSource(url);
146 }
James Dong392ff3b2009-09-06 14:29:45 -0700147 if (ret == NO_ERROR) mRetriever = p;
148 return ret;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800149}
150
151status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
152{
153 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
154 Mutex::Autolock lock(mLock);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800155 struct stat sb;
156 int ret = fstat(fd, &sb);
157 if (ret != 0) {
158 LOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
James Dong392ff3b2009-09-06 14:29:45 -0700159 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800160 }
161 LOGV("st_dev = %llu", sb.st_dev);
162 LOGV("st_mode = %u", sb.st_mode);
163 LOGV("st_uid = %lu", sb.st_uid);
164 LOGV("st_gid = %lu", sb.st_gid);
165 LOGV("st_size = %llu", sb.st_size);
166
167 if (offset >= sb.st_size) {
168 LOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size);
169 ::close(fd);
James Dong392ff3b2009-09-06 14:29:45 -0700170 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800171 }
172 if (offset + length > sb.st_size) {
173 length = sb.st_size - offset;
James Dong392ff3b2009-09-06 14:29:45 -0700174 LOGV("calculated length = %lld", length);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800175 }
James Dong392ff3b2009-09-06 14:29:45 -0700176
177 player_type playerType = getPlayerType(fd, offset, length);
178 LOGV("player type = %d", playerType);
179 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
180 if (p == NULL) {
181 ::close(fd);
182 return NO_INIT;
183 }
James Dong67fc2342009-09-29 10:45:27 -0700184 status_t status = p->setMode(mMode);
185 if (status == NO_ERROR) {
186 p->setDataSource(fd, offset, length);
187 }
James Dong392ff3b2009-09-06 14:29:45 -0700188 if (status == NO_ERROR) mRetriever = p;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800189 ::close(fd);
190 return status;
191}
192
193status_t MetadataRetrieverClient::setMode(int mode)
194{
195 LOGV("setMode");
196 Mutex::Autolock lock(mLock);
James Dong67fc2342009-09-29 10:45:27 -0700197 if (mode < METADATA_MODE_NOOP ||
198 mode > METADATA_MODE_FRAME_CAPTURE_AND_METADATA_RETRIEVAL) {
199 LOGE("invalid mode %d", mode);
200 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800201 }
James Dong67fc2342009-09-29 10:45:27 -0700202 mMode = mode;
203 return NO_ERROR;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800204}
205
206status_t MetadataRetrieverClient::getMode(int* mode) const
207{
208 LOGV("getMode");
209 Mutex::Autolock lock(mLock);
James Dong67fc2342009-09-29 10:45:27 -0700210
211 // TODO:
212 // This may not be necessary.
213 // If setDataSource() has not been called, return the cached value
214 // otherwise, return the value retrieved from the retriever
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800215 if (mRetriever == NULL) {
James Dong67fc2342009-09-29 10:45:27 -0700216 *mode = mMode;
217 } else {
218 mRetriever->getMode(mode);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800219 }
James Dong67fc2342009-09-29 10:45:27 -0700220 return NO_ERROR;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800221}
222
223sp<IMemory> MetadataRetrieverClient::captureFrame()
224{
225 LOGV("captureFrame");
226 Mutex::Autolock lock(mLock);
227 mThumbnail.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800228 if (mRetriever == NULL) {
229 LOGE("retriever is not initialized");
230 return NULL;
231 }
232 VideoFrame *frame = mRetriever->captureFrame();
233 if (frame == NULL) {
234 LOGE("failed to capture a video frame");
235 return NULL;
236 }
237 size_t size = sizeof(VideoFrame) + frame->mSize;
Mathias Agopian1875b7e2010-01-29 17:16:30 -0800238 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
239 if (heap == NULL) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800240 LOGE("failed to create MemoryDealer");
241 delete frame;
242 return NULL;
243 }
Mathias Agopian1875b7e2010-01-29 17:16:30 -0800244 mThumbnail = new MemoryBase(heap, 0, size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800245 if (mThumbnail == NULL) {
246 LOGE("not enough memory for VideoFrame size=%u", size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800247 delete frame;
248 return NULL;
249 }
250 VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer());
251 frameCopy->mWidth = frame->mWidth;
252 frameCopy->mHeight = frame->mHeight;
253 frameCopy->mDisplayWidth = frame->mDisplayWidth;
254 frameCopy->mDisplayHeight = frame->mDisplayHeight;
255 frameCopy->mSize = frame->mSize;
256 frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame);
257 memcpy(frameCopy->mData, frame->mData, frame->mSize);
258 delete frame; // Fix memory leakage
259 return mThumbnail;
260}
261
262sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
263{
264 LOGV("extractAlbumArt");
265 Mutex::Autolock lock(mLock);
266 mAlbumArt.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800267 if (mRetriever == NULL) {
268 LOGE("retriever is not initialized");
269 return NULL;
270 }
271 MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
272 if (albumArt == NULL) {
273 LOGE("failed to extract an album art");
274 return NULL;
275 }
276 size_t size = sizeof(MediaAlbumArt) + albumArt->mSize;
Mathias Agopian1875b7e2010-01-29 17:16:30 -0800277 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
278 if (heap == NULL) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800279 LOGE("failed to create MemoryDealer object");
280 delete albumArt;
281 return NULL;
282 }
Mathias Agopian1875b7e2010-01-29 17:16:30 -0800283 mAlbumArt = new MemoryBase(heap, 0, size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800284 if (mAlbumArt == NULL) {
285 LOGE("not enough memory for MediaAlbumArt size=%u", size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800286 delete albumArt;
287 return NULL;
288 }
289 MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer());
290 albumArtCopy->mSize = albumArt->mSize;
291 albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt);
292 memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize);
293 delete albumArt; // Fix memory leakage
294 return mAlbumArt;
295}
296
297const char* MetadataRetrieverClient::extractMetadata(int keyCode)
298{
299 LOGV("extractMetadata");
300 Mutex::Autolock lock(mLock);
301 if (mRetriever == NULL) {
302 LOGE("retriever is not initialized");
303 return NULL;
304 }
305 return mRetriever->extractMetadata(keyCode);
306}
307
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800308}; // namespace android