blob: b0241aab3f1bfadb7ab8393e762af3ad742129b2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Agopian07952722009-05-19 19:08:10 -070021#include <binder/IServiceManager.h>
22#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <media/mediametadataretriever.h>
24#include <media/IMediaPlayerService.h>
25#include <utils/Log.h>
26#include <dlfcn.h>
27
28namespace android {
29
30// client singleton for binder interface to service
31Mutex MediaMetadataRetriever::sServiceLock;
32sp<IMediaPlayerService> MediaMetadataRetriever::sService;
33sp<MediaMetadataRetriever::DeathNotifier> MediaMetadataRetriever::sDeathNotifier;
34
35const sp<IMediaPlayerService>& MediaMetadataRetriever::getService()
36{
37 Mutex::Autolock lock(sServiceLock);
Glenn Kasten70ed6b72012-01-10 10:46:34 -080038 if (sService == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 sp<IServiceManager> sm = defaultServiceManager();
40 sp<IBinder> binder;
41 do {
42 binder = sm->getService(String16("media.player"));
43 if (binder != 0) {
44 break;
45 }
Steve Block8564c8d2012-01-05 23:22:43 +000046 ALOGW("MediaPlayerService not published, waiting...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 usleep(500000); // 0.5 s
Glenn Kasten18db49a2012-03-12 16:29:55 -070048 } while (true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 if (sDeathNotifier == NULL) {
50 sDeathNotifier = new DeathNotifier();
51 }
52 binder->linkToDeath(sDeathNotifier);
53 sService = interface_cast<IMediaPlayerService>(binder);
54 }
Steve Block3762c312012-01-06 19:20:56 +000055 ALOGE_IF(sService == 0, "no MediaPlayerService!?");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 return sService;
57}
58
59MediaMetadataRetriever::MediaMetadataRetriever()
60{
Steve Block71f2cf12011-10-20 11:56:00 +010061 ALOGV("constructor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 const sp<IMediaPlayerService>& service(getService());
63 if (service == 0) {
Steve Block3762c312012-01-06 19:20:56 +000064 ALOGE("failed to obtain MediaMetadataRetrieverService");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 return;
66 }
67 sp<IMediaMetadataRetriever> retriever(service->createMetadataRetriever(getpid()));
68 if (retriever == 0) {
Steve Block3762c312012-01-06 19:20:56 +000069 ALOGE("failed to create IMediaMetadataRetriever object from server");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 }
71 mRetriever = retriever;
72}
73
74MediaMetadataRetriever::~MediaMetadataRetriever()
75{
Steve Block71f2cf12011-10-20 11:56:00 +010076 ALOGV("destructor");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 disconnect();
78 IPCThreadState::self()->flushCommands();
79}
80
81void MediaMetadataRetriever::disconnect()
82{
Steve Block71f2cf12011-10-20 11:56:00 +010083 ALOGV("disconnect");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 sp<IMediaMetadataRetriever> retriever;
85 {
86 Mutex::Autolock _l(mLock);
87 retriever = mRetriever;
88 mRetriever.clear();
89 }
90 if (retriever != 0) {
91 retriever->disconnect();
92 }
93}
94
Andreas Huber5b7ced62011-03-21 10:25:44 -070095status_t MediaMetadataRetriever::setDataSource(
96 const char *srcUrl, const KeyedVector<String8, String8> *headers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097{
Steve Block71f2cf12011-10-20 11:56:00 +010098 ALOGV("setDataSource");
Dave Sparksdb6cc0b2010-04-01 18:00:58 -070099 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 if (mRetriever == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000101 ALOGE("retriever is not initialized");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 return INVALID_OPERATION;
103 }
104 if (srcUrl == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000105 ALOGE("data source is a null pointer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 return UNKNOWN_ERROR;
107 }
Steve Block71f2cf12011-10-20 11:56:00 +0100108 ALOGV("data source (%s)", srcUrl);
Andreas Huber5b7ced62011-03-21 10:25:44 -0700109 return mRetriever->setDataSource(srcUrl, headers);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110}
111
112status_t MediaMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length)
113{
Steve Block71f2cf12011-10-20 11:56:00 +0100114 ALOGV("setDataSource(%d, %lld, %lld)", fd, offset, length);
Dave Sparksdb6cc0b2010-04-01 18:00:58 -0700115 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 if (mRetriever == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000117 ALOGE("retriever is not initialized");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 return INVALID_OPERATION;
119 }
120 if (fd < 0 || offset < 0 || length < 0) {
Steve Block3762c312012-01-06 19:20:56 +0000121 ALOGE("Invalid negative argument");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 return UNKNOWN_ERROR;
123 }
124 return mRetriever->setDataSource(fd, offset, length);
125}
126
James Dongfaf09ba2010-12-02 17:42:08 -0800127sp<IMemory> MediaMetadataRetriever::getFrameAtTime(int64_t timeUs, int option)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128{
Steve Block71f2cf12011-10-20 11:56:00 +0100129 ALOGV("getFrameAtTime: time(%lld us) option(%d)", timeUs, option);
Dave Sparksdb6cc0b2010-04-01 18:00:58 -0700130 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 if (mRetriever == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000132 ALOGE("retriever is not initialized");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 return NULL;
134 }
James Dongfaf09ba2010-12-02 17:42:08 -0800135 return mRetriever->getFrameAtTime(timeUs, option);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136}
137
138const char* MediaMetadataRetriever::extractMetadata(int keyCode)
139{
Steve Block71f2cf12011-10-20 11:56:00 +0100140 ALOGV("extractMetadata(%d)", keyCode);
Dave Sparksdb6cc0b2010-04-01 18:00:58 -0700141 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 if (mRetriever == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000143 ALOGE("retriever is not initialized");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 return NULL;
145 }
146 return mRetriever->extractMetadata(keyCode);
147}
148
149sp<IMemory> MediaMetadataRetriever::extractAlbumArt()
150{
Steve Block71f2cf12011-10-20 11:56:00 +0100151 ALOGV("extractAlbumArt");
Dave Sparksdb6cc0b2010-04-01 18:00:58 -0700152 Mutex::Autolock _l(mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 if (mRetriever == 0) {
Steve Block3762c312012-01-06 19:20:56 +0000154 ALOGE("retriever is not initialized");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 return NULL;
156 }
157 return mRetriever->extractAlbumArt();
158}
159
160void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who) {
161 Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock);
162 MediaMetadataRetriever::sService.clear();
Steve Block8564c8d2012-01-05 23:22:43 +0000163 ALOGW("MediaMetadataRetriever server died!");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164}
165
166MediaMetadataRetriever::DeathNotifier::~DeathNotifier()
167{
168 Mutex::Autolock lock(sServiceLock);
169 if (sService != 0) {
170 sService->asBinder()->unlinkToDeath(this);
171 }
172}
173
174}; // namespace android