blob: d574ea3904f05825f54c0cce250e03a57ac94a3a [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{
49 LOGV("MetadataRetrieverClient constructor pid(%d)", pid);
50 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{
58 LOGV("MetadataRetrieverClient destructor");
59 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{
77 LOGV("disconnect from pid %d", mPid);
78 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:
95 LOGV("create midi metadata retriever");
96 p = new MidiMetadataRetriever();
97 break;
98 default:
99 // TODO:
Andreas Huber53a76bd2009-10-06 16:20:44 -0700100 // support for TEST_PLAYER
James Dong392ff3b2009-09-06 14:29:45 -0700101 LOGE("player type %d is not supported", playerType);
102 break;
103 }
104 if (p == NULL) {
105 LOGE("failed to create a retriever object");
106 }
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{
113 LOGV("setDataSource(%s)", url);
114 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);
119 LOGV("player type = %d", playerType);
120 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{
129 LOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
130 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) {
134 LOGE("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 }
137 LOGV("st_dev = %llu", sb.st_dev);
138 LOGV("st_mode = %u", sb.st_mode);
139 LOGV("st_uid = %lu", sb.st_uid);
140 LOGV("st_gid = %lu", sb.st_gid);
141 LOGV("st_size = %llu", sb.st_size);
142
143 if (offset >= sb.st_size) {
144 LOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size);
145 ::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;
James Dong392ff3b2009-09-06 14:29:45 -0700150 LOGV("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);
154 LOGV("player type = %d", playerType);
155 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{
James Dongfaf09ba2010-12-02 17:42:08 -0800168 LOGV("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) {
172 LOGE("retriever is not initialized");
173 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) {
177 LOGE("failed to capture a video frame");
178 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) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800183 LOGE("failed to create MemoryDealer");
184 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) {
189 LOGE("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;
200 LOGV("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{
209 LOGV("extractAlbumArt");
210 Mutex::Autolock lock(mLock);
211 mAlbumArt.clear();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800212 if (mRetriever == NULL) {
213 LOGE("retriever is not initialized");
214 return NULL;
215 }
216 MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
217 if (albumArt == NULL) {
218 LOGE("failed to extract an album art");
219 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) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800224 LOGE("failed to create MemoryDealer object");
225 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) {
230 LOGE("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{
244 LOGV("extractMetadata");
245 Mutex::Autolock lock(mLock);
246 if (mRetriever == NULL) {
247 LOGE("retriever is not initialized");
248 return NULL;
249 }
250 return mRetriever->extractMetadata(keyCode);
251}
252
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800253}; // namespace android