blob: 7dbb57f47c06213a0a11b23d5b37a69e0cd6e78e [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>
24#include <dirent.h>
25#include <unistd.h>
26
27#include <string.h>
28#include <cutils/atomic.h>
Andreas Huber7782fdf2010-01-04 15:02:02 -080029#include <cutils/properties.h>
Mathias Agopian1875b7e2010-01-29 17:16:30 -080030#include <binder/MemoryBase.h>
31#include <binder/MemoryHeapBase.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080032#include <android_runtime/ActivityManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070033#include <binder/IPCThreadState.h>
34#include <binder/IServiceManager.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080035#include <media/MediaMetadataRetrieverInterface.h>
36#include <media/MediaPlayerInterface.h>
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080037#include <private/media/VideoFrame.h>
James Dong392ff3b2009-09-06 14:29:45 -070038#include "MidiMetadataRetriever.h"
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080039#include "MetadataRetrieverClient.h"
Andreas Huber53a76bd2009-10-06 16:20:44 -070040#include "StagefrightMetadataRetriever.h"
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080041
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080042namespace android {
43
James Dong392ff3b2009-09-06 14:29:45 -070044extern player_type getPlayerType(const char* url);
45extern player_type getPlayerType(int fd, int64_t offset, int64_t length);
46
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080047MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
48{
Steve Block06ade6a2011-10-20 11:56:00 +010049 ALOGV("MetadataRetrieverClient constructor pid(%d)", pid);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080050 mPid = pid;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080051 mThumbnail = NULL;
52 mAlbumArt = NULL;
Jean-Baptiste Queru680f8c72009-03-21 11:40:18 -070053 mRetriever = NULL;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080054}
55
56MetadataRetrieverClient::~MetadataRetrieverClient()
57{
Steve Block06ade6a2011-10-20 11:56:00 +010058 ALOGV("MetadataRetrieverClient destructor");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080059 disconnect();
60}
61
62status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& args) const
63{
64 const size_t SIZE = 256;
65 char buffer[SIZE];
66 String8 result;
67 result.append(" MetadataRetrieverClient\n");
James Dong11eab052011-01-06 12:20:35 -080068 snprintf(buffer, 255, " pid(%d)\n", mPid);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080069 result.append(buffer);
70 write(fd, result.string(), result.size());
71 write(fd, "\n", 1);
72 return NO_ERROR;
73}
74
75void MetadataRetrieverClient::disconnect()
76{
Steve Block06ade6a2011-10-20 11:56:00 +010077 ALOGV("disconnect from pid %d", mPid);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080078 Mutex::Autolock lock(mLock);
79 mRetriever.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080080 mThumbnail.clear();
81 mAlbumArt.clear();
82 IPCThreadState::self()->flushCommands();
83}
84
James Dong392ff3b2009-09-06 14:29:45 -070085static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
86{
87 sp<MediaMetadataRetrieverBase> p;
88 switch (playerType) {
Andreas Huber6a3a0182009-12-17 13:31:13 -080089 case STAGEFRIGHT_PLAYER:
Andreas Huber7782fdf2010-01-04 15:02:02 -080090 {
Andreas Huber8d65dd22010-06-23 16:40:57 -070091 p = new StagefrightMetadataRetriever;
92 break;
Andreas Huber7782fdf2010-01-04 15:02:02 -080093 }
James Dong392ff3b2009-09-06 14:29:45 -070094 case SONIVOX_PLAYER:
Steve Block06ade6a2011-10-20 11:56:00 +010095 ALOGV("create midi metadata retriever");
James Dong392ff3b2009-09-06 14:29:45 -070096 p = new MidiMetadataRetriever();
97 break;
98 default:
99 // TODO:
Andreas Huber53a76bd2009-10-06 16:20:44 -0700100 // support for TEST_PLAYER
Steve Blockc6aacce2012-01-06 19:20:56 +0000101 ALOGE("player type %d is not supported", playerType);
James Dong392ff3b2009-09-06 14:29:45 -0700102 break;
103 }
104 if (p == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000105 ALOGE("failed to create a retriever object");
James Dong392ff3b2009-09-06 14:29:45 -0700106 }
107 return p;
108}
109
Andreas Huber5b7ced62011-03-21 10:25:44 -0700110status_t MetadataRetrieverClient::setDataSource(
111 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800112{
Steve Block06ade6a2011-10-20 11:56:00 +0100113 ALOGV("setDataSource(%s)", url);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800114 Mutex::Autolock lock(mLock);
115 if (url == NULL) {
116 return UNKNOWN_ERROR;
117 }
James Dong392ff3b2009-09-06 14:29:45 -0700118 player_type playerType = getPlayerType(url);
Steve Block06ade6a2011-10-20 11:56:00 +0100119 ALOGV("player type = %d", playerType);
James Dong392ff3b2009-09-06 14:29:45 -0700120 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
121 if (p == NULL) return NO_INIT;
Andreas Huber5b7ced62011-03-21 10:25:44 -0700122 status_t ret = p->setDataSource(url, headers);
James Dong392ff3b2009-09-06 14:29:45 -0700123 if (ret == NO_ERROR) mRetriever = p;
124 return ret;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800125}
126
127status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
128{
Steve Block06ade6a2011-10-20 11:56:00 +0100129 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800130 Mutex::Autolock lock(mLock);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800131 struct stat sb;
132 int ret = fstat(fd, &sb);
133 if (ret != 0) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000134 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
James Dong392ff3b2009-09-06 14:29:45 -0700135 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800136 }
Steve Block06ade6a2011-10-20 11:56:00 +0100137 ALOGV("st_dev = %llu", sb.st_dev);
138 ALOGV("st_mode = %u", sb.st_mode);
139 ALOGV("st_uid = %lu", sb.st_uid);
140 ALOGV("st_gid = %lu", sb.st_gid);
141 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800142
143 if (offset >= sb.st_size) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000144 ALOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800145 ::close(fd);
James Dong392ff3b2009-09-06 14:29:45 -0700146 return BAD_VALUE;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800147 }
148 if (offset + length > sb.st_size) {
149 length = sb.st_size - offset;
Steve Block06ade6a2011-10-20 11:56:00 +0100150 ALOGV("calculated length = %lld", length);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800151 }
James Dong392ff3b2009-09-06 14:29:45 -0700152
153 player_type playerType = getPlayerType(fd, offset, length);
Steve Block06ade6a2011-10-20 11:56:00 +0100154 ALOGV("player type = %d", playerType);
James Dong392ff3b2009-09-06 14:29:45 -0700155 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
156 if (p == NULL) {
157 ::close(fd);
158 return NO_INIT;
159 }
James Dong11eab052011-01-06 12:20:35 -0800160 status_t status = p->setDataSource(fd, offset, length);
James Dong392ff3b2009-09-06 14:29:45 -0700161 if (status == NO_ERROR) mRetriever = p;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800162 ::close(fd);
163 return status;
164}
165
James Dongfaf09ba2010-12-02 17:42:08 -0800166sp<IMemory> MetadataRetrieverClient::getFrameAtTime(int64_t timeUs, int option)
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800167{
Steve Block06ade6a2011-10-20 11:56:00 +0100168 ALOGV("getFrameAtTime: time(%lld us) option(%d)", timeUs, option);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800169 Mutex::Autolock lock(mLock);
170 mThumbnail.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800171 if (mRetriever == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000172 ALOGE("retriever is not initialized");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800173 return NULL;
174 }
James Dongfaf09ba2010-12-02 17:42:08 -0800175 VideoFrame *frame = mRetriever->getFrameAtTime(timeUs, option);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800176 if (frame == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000177 ALOGE("failed to capture a video frame");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800178 return NULL;
179 }
180 size_t size = sizeof(VideoFrame) + frame->mSize;
Mathias Agopian1875b7e2010-01-29 17:16:30 -0800181 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
182 if (heap == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000183 ALOGE("failed to create MemoryDealer");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800184 delete frame;
185 return NULL;
186 }
Mathias Agopian1875b7e2010-01-29 17:16:30 -0800187 mThumbnail = new MemoryBase(heap, 0, size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800188 if (mThumbnail == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000189 ALOGE("not enough memory for VideoFrame size=%u", size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800190 delete frame;
191 return NULL;
192 }
193 VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer());
194 frameCopy->mWidth = frame->mWidth;
195 frameCopy->mHeight = frame->mHeight;
196 frameCopy->mDisplayWidth = frame->mDisplayWidth;
197 frameCopy->mDisplayHeight = frame->mDisplayHeight;
198 frameCopy->mSize = frame->mSize;
James Dong53ebc722010-11-08 16:04:27 -0800199 frameCopy->mRotationAngle = frame->mRotationAngle;
Steve Block06ade6a2011-10-20 11:56:00 +0100200 ALOGV("rotation: %d", frameCopy->mRotationAngle);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800201 frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame);
202 memcpy(frameCopy->mData, frame->mData, frame->mSize);
203 delete frame; // Fix memory leakage
204 return mThumbnail;
205}
206
207sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
208{
Steve Block06ade6a2011-10-20 11:56:00 +0100209 ALOGV("extractAlbumArt");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800210 Mutex::Autolock lock(mLock);
211 mAlbumArt.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800212 if (mRetriever == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000213 ALOGE("retriever is not initialized");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800214 return NULL;
215 }
216 MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
217 if (albumArt == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000218 ALOGE("failed to extract an album art");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800219 return NULL;
220 }
221 size_t size = sizeof(MediaAlbumArt) + albumArt->mSize;
Mathias Agopian1875b7e2010-01-29 17:16:30 -0800222 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
223 if (heap == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000224 ALOGE("failed to create MemoryDealer object");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800225 delete albumArt;
226 return NULL;
227 }
Mathias Agopian1875b7e2010-01-29 17:16:30 -0800228 mAlbumArt = new MemoryBase(heap, 0, size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800229 if (mAlbumArt == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000230 ALOGE("not enough memory for MediaAlbumArt size=%u", size);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800231 delete albumArt;
232 return NULL;
233 }
234 MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer());
235 albumArtCopy->mSize = albumArt->mSize;
236 albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt);
237 memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize);
238 delete albumArt; // Fix memory leakage
239 return mAlbumArt;
240}
241
242const char* MetadataRetrieverClient::extractMetadata(int keyCode)
243{
Steve Block06ade6a2011-10-20 11:56:00 +0100244 ALOGV("extractMetadata");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800245 Mutex::Autolock lock(mLock);
246 if (mRetriever == NULL) {
Steve Blockc6aacce2012-01-06 19:20:56 +0000247 ALOGE("retriever is not initialized");
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800248 return NULL;
249 }
250 return mRetriever->extractMetadata(keyCode);
251}
252
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800253}; // namespace android