blob: 98e69741b83123f8c5fb67110a1ddcbf9d362ffb [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>
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080038#include "MetadataRetrieverClient.h"
Andreas Huber2a4a7d52009-10-06 16:20:44 -070039#include "StagefrightMetadataRetriever.h"
John Grossman44a7e422012-06-21 17:29:24 -070040#include "MediaPlayerFactory.h"
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080041
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080042namespace android {
43
44MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
45{
Steve Block3856b092011-10-20 11:56:00 +010046 ALOGV("MetadataRetrieverClient constructor pid(%d)", pid);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080047 mPid = pid;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080048 mThumbnail = NULL;
49 mAlbumArt = NULL;
Jean-Baptiste Queru6c5b2102009-03-21 11:40:18 -070050 mRetriever = NULL;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080051}
52
53MetadataRetrieverClient::~MetadataRetrieverClient()
54{
Steve Block3856b092011-10-20 11:56:00 +010055 ALOGV("MetadataRetrieverClient destructor");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080056 disconnect();
57}
58
Mark Salyzyn247d9eb2014-06-23 14:14:40 -070059status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& /*args*/) const
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080060{
61 const size_t SIZE = 256;
62 char buffer[SIZE];
63 String8 result;
64 result.append(" MetadataRetrieverClient\n");
James Dong7f7d52a2011-01-06 12:20:35 -080065 snprintf(buffer, 255, " pid(%d)\n", mPid);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080066 result.append(buffer);
67 write(fd, result.string(), result.size());
68 write(fd, "\n", 1);
69 return NO_ERROR;
70}
71
72void MetadataRetrieverClient::disconnect()
73{
Steve Block3856b092011-10-20 11:56:00 +010074 ALOGV("disconnect from pid %d", mPid);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080075 Mutex::Autolock lock(mLock);
76 mRetriever.clear();
The Android Open Source Project7b5eb022008-12-17 18:05:43 -080077 mThumbnail.clear();
78 mAlbumArt.clear();
79 IPCThreadState::self()->flushCommands();
80}
81
James Dong148c1a22009-09-06 14:29:45 -070082static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
83{
84 sp<MediaMetadataRetrieverBase> p;
85 switch (playerType) {
Andreas Huber47945ea2009-12-17 13:31:13 -080086 case STAGEFRIGHT_PLAYER:
Andreas Huberafed0e12011-09-20 15:39:58 -070087 case NU_PLAYER:
Andreas Huber065c05e2010-01-04 15:02:02 -080088 {
Andreas Huber608d77b2010-06-23 16:40:57 -070089 p = new StagefrightMetadataRetriever;
90 break;
Andreas Huber065c05e2010-01-04 15:02:02 -080091 }
James Dong148c1a22009-09-06 14:29:45 -070092 default:
93 // TODO:
Andreas Huber2a4a7d52009-10-06 16:20:44 -070094 // support for TEST_PLAYER
Steve Block29357bc2012-01-06 19:20:56 +000095 ALOGE("player type %d is not supported", playerType);
James Dong148c1a22009-09-06 14:29:45 -070096 break;
97 }
98 if (p == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +000099 ALOGE("failed to create a retriever object");
James Dong148c1a22009-09-06 14:29:45 -0700100 }
101 return p;
102}
103
Andreas Huberaf8791e2011-03-21 10:25:44 -0700104status_t MetadataRetrieverClient::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800105 const sp<IMediaHTTPService> &httpService,
106 const char *url,
107 const KeyedVector<String8, String8> *headers)
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800108{
Steve Block3856b092011-10-20 11:56:00 +0100109 ALOGV("setDataSource(%s)", url);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800110 Mutex::Autolock lock(mLock);
111 if (url == NULL) {
112 return UNKNOWN_ERROR;
113 }
John Grossman44a7e422012-06-21 17:29:24 -0700114
115 // When asking the MediaPlayerFactory subsystem to choose a media player for
116 // a given URL, a pointer to an outer IMediaPlayer can be passed to the
117 // factory system to be taken into consideration along with the URL. In the
118 // case of choosing an instance of a MediaPlayerBase for a
119 // MetadataRetrieverClient, there is no outer IMediaPlayer which will
120 // eventually encapsulate the result of this selection. In this case, just
121 // pass NULL to getPlayerType to indicate that there is no outer
122 // IMediaPlayer to consider during selection.
123 player_type playerType =
124 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +0100125 ALOGV("player type = %d", playerType);
James Dong148c1a22009-09-06 14:29:45 -0700126 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
127 if (p == NULL) return NO_INIT;
Andreas Huber1b86fe02014-01-29 11:13:26 -0800128 status_t ret = p->setDataSource(httpService, url, headers);
James Dong148c1a22009-09-06 14:29:45 -0700129 if (ret == NO_ERROR) mRetriever = p;
130 return ret;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800131}
132
133status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
134{
Steve Block3856b092011-10-20 11:56:00 +0100135 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800136 Mutex::Autolock lock(mLock);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800137 struct stat sb;
138 int ret = fstat(fd, &sb);
139 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000140 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
James Dong148c1a22009-09-06 14:29:45 -0700141 return BAD_VALUE;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800142 }
Steve Block3856b092011-10-20 11:56:00 +0100143 ALOGV("st_dev = %llu", sb.st_dev);
144 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700145 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
146 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100147 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800148
149 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000150 ALOGE("offset (%lld) bigger than file size (%llu)", offset, sb.st_size);
James Dong148c1a22009-09-06 14:29:45 -0700151 return BAD_VALUE;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800152 }
153 if (offset + length > sb.st_size) {
154 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100155 ALOGV("calculated length = %lld", length);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800156 }
James Dong148c1a22009-09-06 14:29:45 -0700157
John Grossman44a7e422012-06-21 17:29:24 -0700158 player_type playerType =
159 MediaPlayerFactory::getPlayerType(NULL /* client */,
160 fd,
161 offset,
162 length);
Steve Block3856b092011-10-20 11:56:00 +0100163 ALOGV("player type = %d", playerType);
James Dong148c1a22009-09-06 14:29:45 -0700164 sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
165 if (p == NULL) {
James Dong148c1a22009-09-06 14:29:45 -0700166 return NO_INIT;
167 }
James Dong7f7d52a2011-01-06 12:20:35 -0800168 status_t status = p->setDataSource(fd, offset, length);
James Dong148c1a22009-09-06 14:29:45 -0700169 if (status == NO_ERROR) mRetriever = p;
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800170 return status;
171}
172
James Dong16afe2f2010-12-02 17:42:08 -0800173sp<IMemory> MetadataRetrieverClient::getFrameAtTime(int64_t timeUs, int option)
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800174{
Steve Block3856b092011-10-20 11:56:00 +0100175 ALOGV("getFrameAtTime: time(%lld us) option(%d)", timeUs, option);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800176 Mutex::Autolock lock(mLock);
177 mThumbnail.clear();
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800178 if (mRetriever == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000179 ALOGE("retriever is not initialized");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800180 return NULL;
181 }
James Dong16afe2f2010-12-02 17:42:08 -0800182 VideoFrame *frame = mRetriever->getFrameAtTime(timeUs, option);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800183 if (frame == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000184 ALOGE("failed to capture a video frame");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800185 return NULL;
186 }
187 size_t size = sizeof(VideoFrame) + frame->mSize;
Mathias Agopian6b3359d2010-01-29 17:16:30 -0800188 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
189 if (heap == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000190 ALOGE("failed to create MemoryDealer");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800191 delete frame;
192 return NULL;
193 }
Mathias Agopian6b3359d2010-01-29 17:16:30 -0800194 mThumbnail = new MemoryBase(heap, 0, size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800195 if (mThumbnail == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000196 ALOGE("not enough memory for VideoFrame size=%u", size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800197 delete frame;
198 return NULL;
199 }
200 VideoFrame *frameCopy = static_cast<VideoFrame *>(mThumbnail->pointer());
201 frameCopy->mWidth = frame->mWidth;
202 frameCopy->mHeight = frame->mHeight;
203 frameCopy->mDisplayWidth = frame->mDisplayWidth;
204 frameCopy->mDisplayHeight = frame->mDisplayHeight;
205 frameCopy->mSize = frame->mSize;
James Dongce0feba2010-11-08 16:04:27 -0800206 frameCopy->mRotationAngle = frame->mRotationAngle;
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("rotation: %d", frameCopy->mRotationAngle);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800208 frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame);
209 memcpy(frameCopy->mData, frame->mData, frame->mSize);
210 delete frame; // Fix memory leakage
211 return mThumbnail;
212}
213
214sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
215{
Steve Block3856b092011-10-20 11:56:00 +0100216 ALOGV("extractAlbumArt");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800217 Mutex::Autolock lock(mLock);
218 mAlbumArt.clear();
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800219 if (mRetriever == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000220 ALOGE("retriever is not initialized");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800221 return NULL;
222 }
223 MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
224 if (albumArt == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000225 ALOGE("failed to extract an album art");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800226 return NULL;
227 }
Elliott Hughesf3e80dd2014-06-10 16:55:38 -0700228 size_t size = sizeof(MediaAlbumArt) + albumArt->size();
Mathias Agopian6b3359d2010-01-29 17:16:30 -0800229 sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
230 if (heap == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000231 ALOGE("failed to create MemoryDealer object");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800232 delete albumArt;
233 return NULL;
234 }
Mathias Agopian6b3359d2010-01-29 17:16:30 -0800235 mAlbumArt = new MemoryBase(heap, 0, size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800236 if (mAlbumArt == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000237 ALOGE("not enough memory for MediaAlbumArt size=%u", size);
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800238 delete albumArt;
239 return NULL;
240 }
Elliott Hughesf3e80dd2014-06-10 16:55:38 -0700241 MediaAlbumArt::init((MediaAlbumArt *) mAlbumArt->pointer(),
242 albumArt->size(), albumArt->data());
243 delete albumArt; // We've taken our copy.
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800244 return mAlbumArt;
245}
246
247const char* MetadataRetrieverClient::extractMetadata(int keyCode)
248{
Steve Block3856b092011-10-20 11:56:00 +0100249 ALOGV("extractMetadata");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800250 Mutex::Autolock lock(mLock);
251 if (mRetriever == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000252 ALOGE("retriever is not initialized");
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800253 return NULL;
254 }
255 return mRetriever->extractMetadata(keyCode);
256}
257
The Android Open Source Project7b5eb022008-12-17 18:05:43 -0800258}; // namespace android