blob: c61cf897a9b836d2dc364253c2531417ce9bc910 [file] [log] [blame]
The Android Open Source Project7b5eb022008-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 Huber065c05e2010-01-04 15:02:02 -080029#include <cutils/properties.h>
Mathias Agopian6b3359d2010-01-29 17:16:30 -080030#include <binder/MemoryBase.h>
31#include <binder/MemoryHeapBase.h>
Mathias Agopian75624082009-05-19 19:08:10 -070032#include <binder/IPCThreadState.h>
33#include <binder/IServiceManager.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080034#include <media/IMediaHTTPService.h>
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080035#include <media/MediaMetadataRetrieverInterface.h>
36#include <media/MediaPlayerInterface.h>
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080037#include <private/media/VideoFrame.h>
James Dong148c1a22009-09-06 14:29:45 -070038#include "MidiMetadataRetriever.h"
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080039#include "MetadataRetrieverClient.h"
Andreas Huber2a4a7d52009-10-06 16:20:44 -070040#include "StagefrightMetadataRetriever.h"
John Grossman44a7e422012-06-21 17:29:24 -070041#include "MediaPlayerFactory.h"
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080042
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080043namespace android {
44
45MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
46{
Steve Block3856b092011-10-20 11:56:00 +010047 ALOGV("MetadataRetrieverClient constructor pid(%d)", pid);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080048 mPid = pid;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080049 mThumbnail = NULL;
50 mAlbumArt = NULL;
Jean-Baptiste Queru6c5b2102009-03-21 11:40:18 -070051 mRetriever = NULL;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080052}
53
54MetadataRetrieverClient::~MetadataRetrieverClient()
55{
Steve Block3856b092011-10-20 11:56:00 +010056 ALOGV("MetadataRetrieverClient destructor");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080057 disconnect();
58}
59
60status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& args) const
61{
62 const size_t SIZE = 256;
63 char buffer[SIZE];
64 String8 result;
65 result.append(" MetadataRetrieverClient\n");
James Dong7f7d52a2011-01-06 12:20:35 -080066 snprintf(buffer, 255, " pid(%d)\n", mPid);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080067 result.append(buffer);
68 write(fd, result.string(), result.size());
69 write(fd, "\n", 1);
70 return NO_ERROR;
71}
72
73void MetadataRetrieverClient::disconnect()
74{
Steve Block3856b092011-10-20 11:56:00 +010075 ALOGV("disconnect from pid %d", mPid);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080076 Mutex::Autolock lock(mLock);
77 mRetriever.clear();
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080078 mThumbnail.clear();
79 mAlbumArt.clear();
80 IPCThreadState::self()->flushCommands();
81}
82
James Dong148c1a22009-09-06 14:29:45 -070083static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
84{
85 sp<MediaMetadataRetrieverBase> p;
86 switch (playerType) {
Andreas Huber47945ea2009-12-17 13:31:13 -080087 case STAGEFRIGHT_PLAYER:
Andreas Huberafed0e12011-09-20 15:39:58 -070088 case NU_PLAYER:
Andreas Huber065c05e2010-01-04 15:02:02 -080089 {
Andreas Huber608d77b2010-06-23 16:40:57 -070090 p = new StagefrightMetadataRetriever;
91 break;
Andreas Huber065c05e2010-01-04 15:02:02 -080092 }
James Dong148c1a22009-09-06 14:29:45 -070093 case SONIVOX_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +010094 ALOGV("create midi metadata retriever");
James Dong148c1a22009-09-06 14:29:45 -070095 p = new MidiMetadataRetriever();
96 break;
97 default:
98 // TODO:
Andreas Huber2a4a7d52009-10-06 16:20:44 -070099 // support for TEST_PLAYER
Steve Block29357bc2012-01-06 19:20:56 +0000100 ALOGE("player type %d is not supported", playerType);
James Dong148c1a22009-09-06 14:29:45 -0700101 break;
102 }
103 if (p == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000104 ALOGE("failed to create a retriever object");
James Dong148c1a22009-09-06 14:29:45 -0700105 }
106 return p;
107}
108
Andreas Huberaf8791e2011-03-21 10:25:44 -0700109status_t MetadataRetrieverClient::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800110 const sp<IMediaHTTPService> &httpService,
111 const char *url,
112 const KeyedVector<String8, String8> *headers)
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800113{
Steve Block3856b092011-10-20 11:56:00 +0100114 ALOGV("setDataSource(%s)", url);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800115 Mutex::Autolock lock(mLock);
116 if (url == NULL) {
117 return UNKNOWN_ERROR;
118 }
John Grossman44a7e422012-06-21 17:29:24 -0700119
120 // When asking the MediaPlayerFactory subsystem to choose a media player for
121 // a given URL, a pointer to an outer IMediaPlayer can be passed to the
122 // factory system to be taken into consideration along with the URL. In the
123 // case of choosing an instance of a MediaPlayerBase for a
124 // MetadataRetrieverClient, there is no outer IMediaPlayer which will
125 // eventually encapsulate the result of this selection. In this case, just
126 // pass NULL to getPlayerType to indicate that there is no outer
127 // IMediaPlayer to consider during selection.
128 player_type playerType =
129 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +0100130 ALOGV("player type = %d", playerType);
James Dong148c1a22009-09-06 14:29:45 -0700131 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
132 if (p == NULL) return NO_INIT;
Andreas Huber1b86fe02014-01-29 11:13:26 -0800133 status_t ret = p->setDataSource(httpService, url, headers);
James Dong148c1a22009-09-06 14:29:45 -0700134 if (ret == NO_ERROR) mRetriever = p;
135 return ret;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800136}
137
138status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
139{
Steve Block3856b092011-10-20 11:56:00 +0100140 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800141 Mutex::Autolock lock(mLock);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800142 struct stat sb;
143 int ret = fstat(fd, &sb);
144 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000145 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
James Dong148c1a22009-09-06 14:29:45 -0700146 return BAD_VALUE;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800147 }
Steve Block3856b092011-10-20 11:56:00 +0100148 ALOGV("st_dev = %llu", sb.st_dev);
149 ALOGV("st_mode = %u", sb.st_mode);
150 ALOGV("st_uid = %lu", sb.st_uid);
151 ALOGV("st_gid = %lu", sb.st_gid);
152 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800153
154 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000155 ALOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800156 ::close(fd);
James Dong148c1a22009-09-06 14:29:45 -0700157 return BAD_VALUE;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800158 }
159 if (offset + length > sb.st_size) {
160 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100161 ALOGV("calculated length = %lld", length);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800162 }
James Dong148c1a22009-09-06 14:29:45 -0700163
John Grossman44a7e422012-06-21 17:29:24 -0700164 player_type playerType =
165 MediaPlayerFactory::getPlayerType(NULL /* client */,
166 fd,
167 offset,
168 length);
Steve Block3856b092011-10-20 11:56:00 +0100169 ALOGV("player type = %d", playerType);
James Dong148c1a22009-09-06 14:29:45 -0700170 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
171 if (p == NULL) {
172 ::close(fd);
173 return NO_INIT;
174 }
James Dong7f7d52a2011-01-06 12:20:35 -0800175 status_t status = p->setDataSource(fd, offset, length);
James Dong148c1a22009-09-06 14:29:45 -0700176 if (status == NO_ERROR) mRetriever = p;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800177 ::close(fd);
178 return status;
179}
180
James Dong16afe2f2010-12-02 17:42:08 -0800181sp<IMemory> MetadataRetrieverClient::getFrameAtTime(int64_t timeUs, int option)
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800182{
Steve Block3856b092011-10-20 11:56:00 +0100183 ALOGV("getFrameAtTime: time(%lld us) option(%d)", timeUs, option);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800184 Mutex::Autolock lock(mLock);
185 mThumbnail.clear();
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800186 if (mRetriever == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000187 ALOGE("retriever is not initialized");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800188 return NULL;
189 }
James Dong16afe2f2010-12-02 17:42:08 -0800190 VideoFrame *frame = mRetriever->getFrameAtTime(timeUs, option);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800191 if (frame == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000192 ALOGE("failed to capture a video frame");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800193 return NULL;
194 }
195 size_t size = sizeof(VideoFrame) + frame->mSize;
Mathias Agopian6b3359d2010-01-29 17:16:30 -0800196 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
197 if (heap == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000198 ALOGE("failed to create MemoryDealer");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800199 delete frame;
200 return NULL;
201 }
Mathias Agopian6b3359d2010-01-29 17:16:30 -0800202 mThumbnail = new MemoryBase(heap, 0, size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800203 if (mThumbnail == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000204 ALOGE("not enough memory for VideoFrame size=%u", size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800205 delete frame;
206 return NULL;
207 }
208 VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer());
209 frameCopy->mWidth = frame->mWidth;
210 frameCopy->mHeight = frame->mHeight;
211 frameCopy->mDisplayWidth = frame->mDisplayWidth;
212 frameCopy->mDisplayHeight = frame->mDisplayHeight;
213 frameCopy->mSize = frame->mSize;
James Dongce0feba2010-11-08 16:04:27 -0800214 frameCopy->mRotationAngle = frame->mRotationAngle;
Steve Block3856b092011-10-20 11:56:00 +0100215 ALOGV("rotation: %d", frameCopy->mRotationAngle);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800216 frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame);
217 memcpy(frameCopy->mData, frame->mData, frame->mSize);
218 delete frame; // Fix memory leakage
219 return mThumbnail;
220}
221
222sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
223{
Steve Block3856b092011-10-20 11:56:00 +0100224 ALOGV("extractAlbumArt");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800225 Mutex::Autolock lock(mLock);
226 mAlbumArt.clear();
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800227 if (mRetriever == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000228 ALOGE("retriever is not initialized");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800229 return NULL;
230 }
231 MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
232 if (albumArt == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000233 ALOGE("failed to extract an album art");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800234 return NULL;
235 }
236 size_t size = sizeof(MediaAlbumArt) + albumArt->mSize;
Mathias Agopian6b3359d2010-01-29 17:16:30 -0800237 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
238 if (heap == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000239 ALOGE("failed to create MemoryDealer object");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800240 delete albumArt;
241 return NULL;
242 }
Mathias Agopian6b3359d2010-01-29 17:16:30 -0800243 mAlbumArt = new MemoryBase(heap, 0, size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800244 if (mAlbumArt == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000245 ALOGE("not enough memory for MediaAlbumArt size=%u", size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800246 delete albumArt;
247 return NULL;
248 }
249 MediaAlbumArt *albumArtCopy = static_cast<MediaAlbumArt *>(mAlbumArt->pointer());
250 albumArtCopy->mSize = albumArt->mSize;
251 albumArtCopy->mData = (uint8_t *)albumArtCopy + sizeof(MediaAlbumArt);
252 memcpy(albumArtCopy->mData, albumArt->mData, albumArt->mSize);
253 delete albumArt; // Fix memory leakage
254 return mAlbumArt;
255}
256
257const char* MetadataRetrieverClient::extractMetadata(int keyCode)
258{
Steve Block3856b092011-10-20 11:56:00 +0100259 ALOGV("extractMetadata");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800260 Mutex::Autolock lock(mLock);
261 if (mRetriever == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000262 ALOGE("retriever is not initialized");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800263 return NULL;
264 }
265 return mRetriever->extractMetadata(keyCode);
266}
267
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800268}; // namespace android