blob: 40fd022812fe91d783106bde04141f8129831f72 [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;
Yifei Zhangff212092023-06-02 11:59:41 -070044 binder = sm->waitForService(String16("media.player"));
45 if (binder == nullptr) {
46 return nullptr;
47 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048 if (sDeathNotifier == NULL) {
49 sDeathNotifier = new DeathNotifier();
50 }
51 binder->linkToDeath(sDeathNotifier);
52 sService = interface_cast<IMediaPlayerService>(binder);
53 }
Steve Block29357bc2012-01-06 19:20:56 +000054 ALOGE_IF(sService == 0, "no MediaPlayerService!?");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055 return sService;
56}
57
58MediaMetadataRetriever::MediaMetadataRetriever()
59{
Steve Block3856b092011-10-20 11:56:00 +010060 ALOGV("constructor");
Marco Nelissen61a6d262016-02-18 08:25:47 -080061 const sp<IMediaPlayerService> service(getService());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062 if (service == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000063 ALOGE("failed to obtain MediaMetadataRetrieverService");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064 return;
65 }
Glenn Kasten8d6cc842012-02-03 11:06:53 -080066 sp<IMediaMetadataRetriever> retriever(service->createMetadataRetriever());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067 if (retriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +000068 ALOGE("failed to create IMediaMetadataRetriever object from server");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069 }
70 mRetriever = retriever;
71}
72
73MediaMetadataRetriever::~MediaMetadataRetriever()
74{
Steve Block3856b092011-10-20 11:56:00 +010075 ALOGV("destructor");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080076 disconnect();
77 IPCThreadState::self()->flushCommands();
78}
79
80void MediaMetadataRetriever::disconnect()
81{
Steve Block3856b092011-10-20 11:56:00 +010082 ALOGV("disconnect");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083 sp<IMediaMetadataRetriever> retriever;
84 {
85 Mutex::Autolock _l(mLock);
86 retriever = mRetriever;
87 mRetriever.clear();
88 }
89 if (retriever != 0) {
90 retriever->disconnect();
91 }
92}
93
Andreas Huberaf8791e2011-03-21 10:25:44 -070094status_t MediaMetadataRetriever::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -080095 const sp<IMediaHTTPService> &httpService,
96 const char *srcUrl,
97 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080098{
Steve Block3856b092011-10-20 11:56:00 +010099 ALOGV("setDataSource");
Dave Sparksa17a1342010-04-01 18:00:58 -0700100 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000102 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103 return INVALID_OPERATION;
104 }
105 if (srcUrl == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000106 ALOGE("data source is a null pointer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800107 return UNKNOWN_ERROR;
108 }
Steve Block3856b092011-10-20 11:56:00 +0100109 ALOGV("data source (%s)", srcUrl);
Andreas Huber1b86fe02014-01-29 11:13:26 -0800110 return mRetriever->setDataSource(httpService, srcUrl, headers);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800111}
112
113status_t MediaMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
114{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700115 ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
Dave Sparksa17a1342010-04-01 18:00:58 -0700116 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000118 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800119 return INVALID_OPERATION;
120 }
121 if (fd < 0 || offset < 0 || length < 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000122 ALOGE("Invalid negative argument");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800123 return UNKNOWN_ERROR;
124 }
125 return mRetriever->setDataSource(fd, offset, length);
126}
127
Chris Watkins99f31602015-03-20 13:06:33 -0700128status_t MediaMetadataRetriever::setDataSource(
Chong Zhang24c15772017-07-26 16:25:28 -0700129 const sp<IDataSource>& dataSource, const char *mime)
Chris Watkins99f31602015-03-20 13:06:33 -0700130{
131 ALOGV("setDataSource(IDataSource)");
132 Mutex::Autolock _l(mLock);
133 if (mRetriever == 0) {
134 ALOGE("retriever is not initialized");
135 return INVALID_OPERATION;
136 }
Chong Zhang24c15772017-07-26 16:25:28 -0700137 return mRetriever->setDataSource(dataSource, mime);
Chris Watkins99f31602015-03-20 13:06:33 -0700138}
139
Chong Zhang24c15772017-07-26 16:25:28 -0700140sp<IMemory> MediaMetadataRetriever::getFrameAtTime(
141 int64_t timeUs, int option, int colorFormat, bool metaOnly)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142{
Chong Zhang24c15772017-07-26 16:25:28 -0700143 ALOGV("getFrameAtTime: time(%" PRId64 " us) option(%d) colorFormat(%d) metaOnly(%d)",
144 timeUs, option, colorFormat, metaOnly);
Dave Sparksa17a1342010-04-01 18:00:58 -0700145 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800146 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000147 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800148 return NULL;
149 }
Chong Zhang24c15772017-07-26 16:25:28 -0700150 return mRetriever->getFrameAtTime(timeUs, option, colorFormat, metaOnly);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800151}
152
Chong Zhangd3e0d862017-10-03 13:17:13 -0700153sp<IMemory> MediaMetadataRetriever::getImageAtIndex(
Chong Zhangd5fa3572018-04-09 19:03:10 -0700154 int index, int colorFormat, bool metaOnly, bool thumbnail) {
155 ALOGV("getImageAtIndex: index(%d) colorFormat(%d) metaOnly(%d) thumbnail(%d)",
156 index, colorFormat, metaOnly, thumbnail);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700157 Mutex::Autolock _l(mLock);
158 if (mRetriever == 0) {
159 ALOGE("retriever is not initialized");
160 return NULL;
161 }
Chong Zhangd5fa3572018-04-09 19:03:10 -0700162 return mRetriever->getImageAtIndex(index, colorFormat, metaOnly, thumbnail);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700163}
164
Chong Zhang0c1407f2018-05-02 17:09:05 -0700165sp<IMemory> MediaMetadataRetriever::getImageRectAtIndex(
166 int index, int colorFormat, int left, int top, int right, int bottom) {
167 ALOGV("getImageRectAtIndex: index(%d) colorFormat(%d) rect {%d, %d, %d, %d}",
168 index, colorFormat, left, top, right, bottom);
169 Mutex::Autolock _l(mLock);
170 if (mRetriever == 0) {
171 ALOGE("retriever is not initialized");
172 return NULL;
173 }
174 return mRetriever->getImageRectAtIndex(
175 index, colorFormat, left, top, right, bottom);
176}
177
Chong Zhang76a49d62019-07-12 11:20:33 -0700178sp<IMemory> MediaMetadataRetriever::getFrameAtIndex(
179 int index, int colorFormat, bool metaOnly) {
180 ALOGV("getFrameAtIndex: index(%d), colorFormat(%d) metaOnly(%d)",
181 index, colorFormat, metaOnly);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700182 Mutex::Autolock _l(mLock);
183 if (mRetriever == 0) {
184 ALOGE("retriever is not initialized");
Chong Zhang76a49d62019-07-12 11:20:33 -0700185 return NULL;
Chong Zhangd3e0d862017-10-03 13:17:13 -0700186 }
Chong Zhang76a49d62019-07-12 11:20:33 -0700187 return mRetriever->getFrameAtIndex(index, colorFormat, metaOnly);
Chong Zhangd3e0d862017-10-03 13:17:13 -0700188}
189
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190const char* MediaMetadataRetriever::extractMetadata(int keyCode)
191{
Steve Block3856b092011-10-20 11:56:00 +0100192 ALOGV("extractMetadata(%d)", keyCode);
Dave Sparksa17a1342010-04-01 18:00:58 -0700193 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000195 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196 return NULL;
197 }
198 return mRetriever->extractMetadata(keyCode);
199}
200
201sp<IMemory> MediaMetadataRetriever::extractAlbumArt()
202{
Steve Block3856b092011-10-20 11:56:00 +0100203 ALOGV("extractAlbumArt");
Dave Sparksa17a1342010-04-01 18:00:58 -0700204 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000206 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 return NULL;
208 }
209 return mRetriever->extractAlbumArt();
210}
211
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800212void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who __unused) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock);
214 MediaMetadataRetriever::sService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000215 ALOGW("MediaMetadataRetriever server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216}
217
218MediaMetadataRetriever::DeathNotifier::~DeathNotifier()
219{
220 Mutex::Autolock lock(sServiceLock);
221 if (sService != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800222 IInterface::asBinder(sService)->unlinkToDeath(this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800223 }
224}
225
Glenn Kasten40bc9062015-03-20 09:09:33 -0700226} // namespace android