blob: 9a76f58e7a4a8342af484562ccb313c038832ac3 [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
38const sp<IMediaPlayerService>& MediaMetadataRetriever::getService()
39{
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");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080065 const sp<IMediaPlayerService>& service(getService());
66 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(
133 const sp<IDataSource>& dataSource)
134{
135 ALOGV("setDataSource(IDataSource)");
136 Mutex::Autolock _l(mLock);
137 if (mRetriever == 0) {
138 ALOGE("retriever is not initialized");
139 return INVALID_OPERATION;
140 }
141 return mRetriever->setDataSource(dataSource);
142}
143
James Dong16afe2f2010-12-02 17:42:08 -0800144sp<IMemory> MediaMetadataRetriever::getFrameAtTime(int64_t timeUs, int option)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145{
Mark Salyzyn34fb2962014-06-18 16:30:56 -0700146 ALOGV("getFrameAtTime: time(%" PRId64 " us) option(%d)", timeUs, option);
Dave Sparksa17a1342010-04-01 18:00:58 -0700147 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800148 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000149 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 return NULL;
151 }
James Dong16afe2f2010-12-02 17:42:08 -0800152 return mRetriever->getFrameAtTime(timeUs, option);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800153}
154
155const char* MediaMetadataRetriever::extractMetadata(int keyCode)
156{
Steve Block3856b092011-10-20 11:56:00 +0100157 ALOGV("extractMetadata(%d)", keyCode);
Dave Sparksa17a1342010-04-01 18:00:58 -0700158 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800159 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000160 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 return NULL;
162 }
163 return mRetriever->extractMetadata(keyCode);
164}
165
166sp<IMemory> MediaMetadataRetriever::extractAlbumArt()
167{
Steve Block3856b092011-10-20 11:56:00 +0100168 ALOGV("extractAlbumArt");
Dave Sparksa17a1342010-04-01 18:00:58 -0700169 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800170 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000171 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 return NULL;
173 }
174 return mRetriever->extractAlbumArt();
175}
176
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800177void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who __unused) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800178 Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock);
179 MediaMetadataRetriever::sService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000180 ALOGW("MediaMetadataRetriever server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181}
182
183MediaMetadataRetriever::DeathNotifier::~DeathNotifier()
184{
185 Mutex::Autolock lock(sServiceLock);
186 if (sService != 0) {
Marco Nelissen06b46062014-11-14 07:58:25 -0800187 IInterface::asBinder(sService)->unlinkToDeath(this);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188 }
189}
190
Glenn Kasten40bc9062015-03-20 09:09:33 -0700191} // namespace android