blob: c10a9079c3b9786ab414063ba20759cc6cb896b8 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2**
3** Copyright 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 "MediaMetadataRetriever"
20
Mark Salyzyn34fb2962014-06-18 16:30:56 -070021#include <inttypes.h>
22
Mathias Agopian75624082009-05-19 19:08:10 -070023#include <binder/IServiceManager.h>
24#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <media/mediametadataretriever.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080026#include <media/IMediaHTTPService.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <media/IMediaPlayerService.h>
28#include <utils/Log.h>
29#include <dlfcn.h>
30
31namespace android {
32
33// client singleton for binder interface to service
34Mutex MediaMetadataRetriever::sServiceLock;
35sp<IMediaPlayerService> MediaMetadataRetriever::sService;
36sp<MediaMetadataRetriever::DeathNotifier> MediaMetadataRetriever::sDeathNotifier;
37
Marco Nelissen61a6d262016-02-18 08:25:47 -080038const sp<IMediaPlayerService> MediaMetadataRetriever::getService()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039{
40 Mutex::Autolock lock(sServiceLock);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -080041 if (sService == 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080042 sp<IServiceManager> sm = defaultServiceManager();
43 sp<IBinder> binder;
44 do {
45 binder = sm->getService(String16("media.player"));
46 if (binder != 0) {
47 break;
48 }
Steve Block5ff1dd52012-01-05 23:22:43 +000049 ALOGW("MediaPlayerService not published, waiting...");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -070051 } while (true);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052 if (sDeathNotifier == NULL) {
53 sDeathNotifier = new DeathNotifier();
54 }
55 binder->linkToDeath(sDeathNotifier);
56 sService = interface_cast<IMediaPlayerService>(binder);
57 }
Steve Block29357bc2012-01-06 19:20:56 +000058 ALOGE_IF(sService == 0, "no MediaPlayerService!?");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080059 return sService;
60}
61
62MediaMetadataRetriever::MediaMetadataRetriever()
63{
Steve Block3856b092011-10-20 11:56:00 +010064 ALOGV("constructor");
Marco Nelissen61a6d262016-02-18 08:25:47 -080065 const sp<IMediaPlayerService> service(getService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066 if (service == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000067 ALOGE("failed to obtain MediaMetadataRetrieverService");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068 return;
69 }
Glenn Kasten8d6cc842012-02-03 11:06:53 -080070 sp<IMediaMetadataRetriever> retriever(service->createMetadataRetriever());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071 if (retriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000072 ALOGE("failed to create IMediaMetadataRetriever object from server");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080073 }
74 mRetriever = retriever;
75}
76
77MediaMetadataRetriever::~MediaMetadataRetriever()
78{
Steve Block3856b092011-10-20 11:56:00 +010079 ALOGV("destructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080080 disconnect();
81 IPCThreadState::self()->flushCommands();
82}
83
84void MediaMetadataRetriever::disconnect()
85{
Steve Block3856b092011-10-20 11:56:00 +010086 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080087 sp<IMediaMetadataRetriever> retriever;
88 {
89 Mutex::Autolock _l(mLock);
90 retriever = mRetriever;
91 mRetriever.clear();
92 }
93 if (retriever != 0) {
94 retriever->disconnect();
95 }
96}
97
Andreas Huberaf8791e2011-03-21 10:25:44 -070098status_t MediaMetadataRetriever::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -080099 const sp<IMediaHTTPService> &httpService,
100 const char *srcUrl,
101 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102{
Steve Block3856b092011-10-20 11:56:00 +0100103 ALOGV("setDataSource");
Dave Sparksa17a1342010-04-01 18:00:58 -0700104 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000106 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 return INVALID_OPERATION;
108 }
109 if (srcUrl == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000110 ALOGE("data source is a null pointer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111 return UNKNOWN_ERROR;
112 }
Steve Block3856b092011-10-20 11:56:00 +0100113 ALOGV("data source (%s)", srcUrl);
Andreas Huber1b86fe02014-01-29 11:13:26 -0800114 return mRetriever->setDataSource(httpService, srcUrl, headers);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800115}
116
117status_t MediaMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
118{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700119 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Dave Sparksa17a1342010-04-01 18:00:58 -0700120 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000122 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123 return INVALID_OPERATION;
124 }
125 if (fd < 0 || offset < 0 || length < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000126 ALOGE("Invalid negative argument");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800127 return UNKNOWN_ERROR;
128 }
129 return mRetriever->setDataSource(fd, offset, length);
130}
131
Chris Watkins99f31602015-03-20 13:06:33 -0700132status_t MediaMetadataRetriever::setDataSource(
Chong Zhang24c15772017-07-26 16:25:28 -0700133 const sp<IDataSource>& dataSource, const char *mime)
Chris Watkins99f31602015-03-20 13:06:33 -0700134{
135 ALOGV("setDataSource(IDataSource)");
136 Mutex::Autolock _l(mLock);
137 if (mRetriever == 0) {
138 ALOGE("retriever is not initialized");
139 return INVALID_OPERATION;
140 }
Chong Zhang24c15772017-07-26 16:25:28 -0700141 return mRetriever->setDataSource(dataSource, mime);
Chris Watkins99f31602015-03-20 13:06:33 -0700142}
143
Chong Zhang24c15772017-07-26 16:25:28 -0700144sp<IMemory> MediaMetadataRetriever::getFrameAtTime(
145 int64_t timeUs, int option, int colorFormat, bool metaOnly)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146{
Chong Zhang24c15772017-07-26 16:25:28 -0700147 ALOGV("getFrameAtTime: time(%" PRId64 " us) option(%d) colorFormat(%d) metaOnly(%d)",
148 timeUs, option, colorFormat, metaOnly);
Dave Sparksa17a1342010-04-01 18:00:58 -0700149 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000151 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800152 return NULL;
153 }
Chong Zhang24c15772017-07-26 16:25:28 -0700154 return mRetriever->getFrameAtTime(timeUs, option, colorFormat, metaOnly);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800155}
156
Chong Zhangd3e0d862017-10-03 13:17:13 -0700157sp<IMemory> MediaMetadataRetriever::getImageAtIndex(
Chong Zhangd5fa3572018-04-09 19:03:10 -0700158 int index, int colorFormat, bool metaOnly, bool thumbnail) {
159 ALOGV("getImageAtIndex: index(%d) colorFormat(%d) metaOnly(%d) thumbnail(%d)",
160 index, colorFormat, metaOnly, thumbnail);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700161 Mutex::Autolock _l(mLock);
162 if (mRetriever == 0) {
163 ALOGE("retriever is not initialized");
164 return NULL;
165 }
Chong Zhangd5fa3572018-04-09 19:03:10 -0700166 return mRetriever->getImageAtIndex(index, colorFormat, metaOnly, thumbnail);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700167}
168
169status_t MediaMetadataRetriever::getFrameAtIndex(
170 std::vector<sp<IMemory> > *frames,
171 int frameIndex, int numFrames, int colorFormat, bool metaOnly) {
172 ALOGV("getFrameAtIndex: frameIndex(%d), numFrames(%d), colorFormat(%d) metaOnly(%d)",
173 frameIndex, numFrames, colorFormat, metaOnly);
174 Mutex::Autolock _l(mLock);
175 if (mRetriever == 0) {
176 ALOGE("retriever is not initialized");
177 return INVALID_OPERATION;
178 }
179 return mRetriever->getFrameAtIndex(
180 frames, frameIndex, numFrames, colorFormat, metaOnly);
181}
182
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183const char* MediaMetadataRetriever::extractMetadata(int keyCode)
184{
Steve Block3856b092011-10-20 11:56:00 +0100185 ALOGV("extractMetadata(%d)", keyCode);
Dave Sparksa17a1342010-04-01 18:00:58 -0700186 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000188 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189 return NULL;
190 }
191 return mRetriever->extractMetadata(keyCode);
192}
193
194sp<IMemory> MediaMetadataRetriever::extractAlbumArt()
195{
Steve Block3856b092011-10-20 11:56:00 +0100196 ALOGV("extractAlbumArt");
Dave Sparksa17a1342010-04-01 18:00:58 -0700197 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000199 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 return NULL;
201 }
202 return mRetriever->extractAlbumArt();
203}
204
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800205void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who __unused) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800206 Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock);
207 MediaMetadataRetriever::sService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000208 ALOGW("MediaMetadataRetriever server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209}
210
211MediaMetadataRetriever::DeathNotifier::~DeathNotifier()
212{
213 Mutex::Autolock lock(sServiceLock);
214 if (sService != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800215 IInterface::asBinder(sService)->unlinkToDeath(this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 }
217}
218
Glenn Kasten40bc9062015-03-20 09:09:33 -0700219} // namespace android