blob: 1d6bb6f6bf6a04059563f4be83ef84737d6776f9 [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
Mathias Agopian75624082009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
22#include <binder/IPCThreadState.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080023#include <media/mediametadataretriever.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080024#include <media/IMediaHTTPService.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <media/IMediaPlayerService.h>
26#include <utils/Log.h>
27#include <dlfcn.h>
28
29namespace android {
30
31// client singleton for binder interface to service
32Mutex MediaMetadataRetriever::sServiceLock;
33sp<IMediaPlayerService> MediaMetadataRetriever::sService;
34sp<MediaMetadataRetriever::DeathNotifier> MediaMetadataRetriever::sDeathNotifier;
35
36const sp<IMediaPlayerService>& MediaMetadataRetriever::getService()
37{
38 Mutex::Autolock lock(sServiceLock);
Glenn Kasten7fc9a6f2012-01-10 10:46:34 -080039 if (sService == 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080040 sp<IServiceManager> sm = defaultServiceManager();
41 sp<IBinder> binder;
42 do {
43 binder = sm->getService(String16("media.player"));
44 if (binder != 0) {
45 break;
46 }
Steve Block5ff1dd52012-01-05 23:22:43 +000047 ALOGW("MediaPlayerService not published, waiting...");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080048 usleep(500000); // 0.5 s
Glenn Kastene53b9ea2012-03-12 16:29:55 -070049 } while (true);
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");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080063 const sp<IMediaPlayerService>& service(getService());
64 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{
Steve Block3856b092011-10-20 11:56:00 +0100117 ALOGV("setDataSource(%d, %lld, %lld)", 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
James Dong16afe2f2010-12-02 17:42:08 -0800130sp<IMemory> MediaMetadataRetriever::getFrameAtTime(int64_t timeUs, int option)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800131{
Steve Block3856b092011-10-20 11:56:00 +0100132 ALOGV("getFrameAtTime: time(%lld us) option(%d)", timeUs, option);
Dave Sparksa17a1342010-04-01 18:00:58 -0700133 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800134 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000135 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800136 return NULL;
137 }
James Dong16afe2f2010-12-02 17:42:08 -0800138 return mRetriever->getFrameAtTime(timeUs, option);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139}
140
141const char* MediaMetadataRetriever::extractMetadata(int keyCode)
142{
Steve Block3856b092011-10-20 11:56:00 +0100143 ALOGV("extractMetadata(%d)", keyCode);
Dave Sparksa17a1342010-04-01 18:00:58 -0700144 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000146 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 return NULL;
148 }
149 return mRetriever->extractMetadata(keyCode);
150}
151
152sp<IMemory> MediaMetadataRetriever::extractAlbumArt()
153{
Steve Block3856b092011-10-20 11:56:00 +0100154 ALOGV("extractAlbumArt");
Dave Sparksa17a1342010-04-01 18:00:58 -0700155 Mutex::Autolock _l(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800156 if (mRetriever == 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000157 ALOGE("retriever is not initialized");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800158 return NULL;
159 }
160 return mRetriever->extractAlbumArt();
161}
162
Glenn Kasten7c7be1e2013-12-19 16:34:04 -0800163void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who __unused) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800164 Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock);
165 MediaMetadataRetriever::sService.clear();
Steve Block5ff1dd52012-01-05 23:22:43 +0000166 ALOGW("MediaMetadataRetriever server died!");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800167}
168
169MediaMetadataRetriever::DeathNotifier::~DeathNotifier()
170{
171 Mutex::Autolock lock(sServiceLock);
172 if (sService != 0) {
173 sService->asBinder()->unlinkToDeath(this);
174 }
175}
176
177}; // namespace android