blob: 9196f9f97b3a79f80427d422234745da0dbf3607 [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
Sungtak Leed33e3192024-09-25 23:27:58 +000038Mutex MediaMetadataRetriever::sLock;
39
Marco Nelissen61a6d262016-02-18 08:25:47 -080040const sp<IMediaPlayerService> MediaMetadataRetriever::getService()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080041{
42 Mutex::Autolock lock(sServiceLock);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -080043 if (sService == 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080044 sp<IServiceManager> sm = defaultServiceManager();
45 sp<IBinder> binder;
Yifei Zhangff212092023-06-02 11:59:41 -070046 binder = sm->waitForService(String16("media.player"));
47 if (binder == nullptr) {
48 return nullptr;
49 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050 if (sDeathNotifier == NULL) {
51 sDeathNotifier = new DeathNotifier();
52 }
53 binder->linkToDeath(sDeathNotifier);
54 sService = interface_cast<IMediaPlayerService>(binder);
55 }
Steve Block29357bc2012-01-06 19:20:56 +000056 ALOGE_IF(sService == 0, "no MediaPlayerService!?");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057 return sService;
58}
59
60MediaMetadataRetriever::MediaMetadataRetriever()
61{
Steve Block3856b092011-10-20 11:56:00 +010062 ALOGV("constructor");
Marco Nelissen61a6d262016-02-18 08:25:47 -080063 const sp<IMediaPlayerService> service(getService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 if (service == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000065 ALOGE("failed to obtain MediaMetadataRetrieverService");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066 return;
67 }
Glenn Kasten8d6cc842012-02-03 11:06:53 -080068 sp<IMediaMetadataRetriever> retriever(service->createMetadataRetriever());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069 if (retriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000070 ALOGE("failed to create IMediaMetadataRetriever object from server");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071 }
72 mRetriever = retriever;
73}
74
75MediaMetadataRetriever::~MediaMetadataRetriever()
76{
Steve Block3856b092011-10-20 11:56:00 +010077 ALOGV("destructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080078 disconnect();
79 IPCThreadState::self()->flushCommands();
80}
81
82void MediaMetadataRetriever::disconnect()
83{
Steve Block3856b092011-10-20 11:56:00 +010084 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 sp<IMediaMetadataRetriever> retriever;
86 {
87 Mutex::Autolock _l(mLock);
88 retriever = mRetriever;
89 mRetriever.clear();
90 }
91 if (retriever != 0) {
92 retriever->disconnect();
93 }
94}
95
Andreas Huberaf8791e2011-03-21 10:25:44 -070096status_t MediaMetadataRetriever::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -080097 const sp<IMediaHTTPService> &httpService,
98 const char *srcUrl,
99 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100{
Steve Block3856b092011-10-20 11:56:00 +0100101 ALOGV("setDataSource");
Dave Sparksa17a1342010-04-01 18:00:58 -0700102 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000104 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 return INVALID_OPERATION;
106 }
107 if (srcUrl == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000108 ALOGE("data source is a null pointer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 return UNKNOWN_ERROR;
110 }
Steve Block3856b092011-10-20 11:56:00 +0100111 ALOGV("data source (%s)", srcUrl);
Andreas Huber1b86fe02014-01-29 11:13:26 -0800112 return mRetriever->setDataSource(httpService, srcUrl, headers);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800113}
114
115status_t MediaMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
116{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700117 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Dave Sparksa17a1342010-04-01 18:00:58 -0700118 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000120 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800121 return INVALID_OPERATION;
122 }
123 if (fd < 0 || offset < 0 || length < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000124 ALOGE("Invalid negative argument");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800125 return UNKNOWN_ERROR;
126 }
127 return mRetriever->setDataSource(fd, offset, length);
128}
129
Chris Watkins99f31602015-03-20 13:06:33 -0700130status_t MediaMetadataRetriever::setDataSource(
Chong Zhang24c15772017-07-26 16:25:28 -0700131 const sp<IDataSource>& dataSource, const char *mime)
Chris Watkins99f31602015-03-20 13:06:33 -0700132{
133 ALOGV("setDataSource(IDataSource)");
134 Mutex::Autolock _l(mLock);
135 if (mRetriever == 0) {
136 ALOGE("retriever is not initialized");
137 return INVALID_OPERATION;
138 }
Chong Zhang24c15772017-07-26 16:25:28 -0700139 return mRetriever->setDataSource(dataSource, mime);
Chris Watkins99f31602015-03-20 13:06:33 -0700140}
141
Chong Zhang24c15772017-07-26 16:25:28 -0700142sp<IMemory> MediaMetadataRetriever::getFrameAtTime(
143 int64_t timeUs, int option, int colorFormat, bool metaOnly)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800144{
Chong Zhang24c15772017-07-26 16:25:28 -0700145 ALOGV("getFrameAtTime: time(%" PRId64 " us) option(%d) colorFormat(%d) metaOnly(%d)",
146 timeUs, option, colorFormat, metaOnly);
Dave Sparksa17a1342010-04-01 18:00:58 -0700147 Mutex::Autolock _l(mLock);
Sungtak Leed33e3192024-09-25 23:27:58 +0000148 Mutex::Autolock _gLock(sLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800149 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000150 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151 return NULL;
152 }
Chong Zhang24c15772017-07-26 16:25:28 -0700153 return mRetriever->getFrameAtTime(timeUs, option, colorFormat, metaOnly);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800154}
155
Chong Zhangd3e0d862017-10-03 13:17:13 -0700156sp<IMemory> MediaMetadataRetriever::getImageAtIndex(
Chong Zhangd5fa3572018-04-09 19:03:10 -0700157 int index, int colorFormat, bool metaOnly, bool thumbnail) {
158 ALOGV("getImageAtIndex: index(%d) colorFormat(%d) metaOnly(%d) thumbnail(%d)",
159 index, colorFormat, metaOnly, thumbnail);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700160 Mutex::Autolock _l(mLock);
Sungtak Leed33e3192024-09-25 23:27:58 +0000161 Mutex::Autolock _gLock(sLock);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700162 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
Chong Zhang0c1407f2018-05-02 17:09:05 -0700169sp<IMemory> MediaMetadataRetriever::getImageRectAtIndex(
170 int index, int colorFormat, int left, int top, int right, int bottom) {
171 ALOGV("getImageRectAtIndex: index(%d) colorFormat(%d) rect {%d, %d, %d, %d}",
172 index, colorFormat, left, top, right, bottom);
173 Mutex::Autolock _l(mLock);
Sungtak Leed33e3192024-09-25 23:27:58 +0000174 Mutex::Autolock _gLock(sLock);
Chong Zhang0c1407f2018-05-02 17:09:05 -0700175 if (mRetriever == 0) {
176 ALOGE("retriever is not initialized");
177 return NULL;
178 }
179 return mRetriever->getImageRectAtIndex(
180 index, colorFormat, left, top, right, bottom);
181}
182
Chong Zhang76a49d62019-07-12 11:20:33 -0700183sp<IMemory> MediaMetadataRetriever::getFrameAtIndex(
184 int index, int colorFormat, bool metaOnly) {
185 ALOGV("getFrameAtIndex: index(%d), colorFormat(%d) metaOnly(%d)",
186 index, colorFormat, metaOnly);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700187 Mutex::Autolock _l(mLock);
Sungtak Leed33e3192024-09-25 23:27:58 +0000188 Mutex::Autolock _gLock(sLock);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700189 if (mRetriever == 0) {
190 ALOGE("retriever is not initialized");
Chong Zhang76a49d62019-07-12 11:20:33 -0700191 return NULL;
Chong Zhangd3e0d862017-10-03 13:17:13 -0700192 }
Chong Zhang76a49d62019-07-12 11:20:33 -0700193 return mRetriever->getFrameAtIndex(index, colorFormat, metaOnly);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700194}
195
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196const char* MediaMetadataRetriever::extractMetadata(int keyCode)
197{
Steve Block3856b092011-10-20 11:56:00 +0100198 ALOGV("extractMetadata(%d)", keyCode);
Dave Sparksa17a1342010-04-01 18:00:58 -0700199 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000201 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800202 return NULL;
203 }
204 return mRetriever->extractMetadata(keyCode);
205}
206
207sp<IMemory> MediaMetadataRetriever::extractAlbumArt()
208{
Steve Block3856b092011-10-20 11:56:00 +0100209 ALOGV("extractAlbumArt");
Dave Sparksa17a1342010-04-01 18:00:58 -0700210 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800211 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000212 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 return NULL;
214 }
215 return mRetriever->extractAlbumArt();
216}
217
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800218void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who __unused) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock);
220 MediaMetadataRetriever::sService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000221 ALOGW("MediaMetadataRetriever server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222}
223
224MediaMetadataRetriever::DeathNotifier::~DeathNotifier()
225{
226 Mutex::Autolock lock(sServiceLock);
227 if (sService != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800228 IInterface::asBinder(sService)->unlinkToDeath(this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229 }
230}
231
Glenn Kasten40bc9062015-03-20 09:09:33 -0700232} // namespace android