mediametadataretriever.cpp revision 16afe2fb439cab6125bb46a07a8078d4ce1c1ea5
1/* 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 21#include <binder/IServiceManager.h> 22#include <binder/IPCThreadState.h> 23#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); 38 if (sService.get() == 0) { 39 sp<IServiceManager> sm = defaultServiceManager(); 40 sp<IBinder> binder; 41 do { 42 binder = sm->getService(String16("media.player")); 43 if (binder != 0) { 44 break; 45 } 46 LOGW("MediaPlayerService not published, waiting..."); 47 usleep(500000); // 0.5 s 48 } while(true); 49 if (sDeathNotifier == NULL) { 50 sDeathNotifier = new DeathNotifier(); 51 } 52 binder->linkToDeath(sDeathNotifier); 53 sService = interface_cast<IMediaPlayerService>(binder); 54 } 55 LOGE_IF(sService == 0, "no MediaPlayerService!?"); 56 return sService; 57} 58 59MediaMetadataRetriever::MediaMetadataRetriever() 60{ 61 LOGV("constructor"); 62 const sp<IMediaPlayerService>& service(getService()); 63 if (service == 0) { 64 LOGE("failed to obtain MediaMetadataRetrieverService"); 65 return; 66 } 67 sp<IMediaMetadataRetriever> retriever(service->createMetadataRetriever(getpid())); 68 if (retriever == 0) { 69 LOGE("failed to create IMediaMetadataRetriever object from server"); 70 } 71 mRetriever = retriever; 72} 73 74MediaMetadataRetriever::~MediaMetadataRetriever() 75{ 76 LOGV("destructor"); 77 disconnect(); 78 IPCThreadState::self()->flushCommands(); 79} 80 81void MediaMetadataRetriever::disconnect() 82{ 83 LOGV("disconnect"); 84 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 95status_t MediaMetadataRetriever::setDataSource(const char* srcUrl) 96{ 97 LOGV("setDataSource"); 98 Mutex::Autolock _l(mLock); 99 if (mRetriever == 0) { 100 LOGE("retriever is not initialized"); 101 return INVALID_OPERATION; 102 } 103 if (srcUrl == NULL) { 104 LOGE("data source is a null pointer"); 105 return UNKNOWN_ERROR; 106 } 107 LOGV("data source (%s)", srcUrl); 108 return mRetriever->setDataSource(srcUrl); 109} 110 111status_t MediaMetadataRetriever::setDataSource(int fd, int64_t offset, int64_t length) 112{ 113 LOGV("setDataSource(%d, %lld, %lld)", fd, offset, length); 114 Mutex::Autolock _l(mLock); 115 if (mRetriever == 0) { 116 LOGE("retriever is not initialized"); 117 return INVALID_OPERATION; 118 } 119 if (fd < 0 || offset < 0 || length < 0) { 120 LOGE("Invalid negative argument"); 121 return UNKNOWN_ERROR; 122 } 123 return mRetriever->setDataSource(fd, offset, length); 124} 125 126status_t MediaMetadataRetriever::setMode(int mode) 127{ 128 LOGV("setMode(%d)", mode); 129 Mutex::Autolock _l(mLock); 130 if (mRetriever == 0) { 131 LOGE("retriever is not initialized"); 132 return INVALID_OPERATION; 133 } 134 return mRetriever->setMode(mode); 135} 136 137sp<IMemory> MediaMetadataRetriever::getFrameAtTime(int64_t timeUs, int option) 138{ 139 LOGV("getFrameAtTime: time(%lld us) option(%d)", timeUs, option); 140 Mutex::Autolock _l(mLock); 141 if (mRetriever == 0) { 142 LOGE("retriever is not initialized"); 143 return NULL; 144 } 145 return mRetriever->getFrameAtTime(timeUs, option); 146} 147 148const char* MediaMetadataRetriever::extractMetadata(int keyCode) 149{ 150 LOGV("extractMetadata(%d)", keyCode); 151 Mutex::Autolock _l(mLock); 152 if (mRetriever == 0) { 153 LOGE("retriever is not initialized"); 154 return NULL; 155 } 156 return mRetriever->extractMetadata(keyCode); 157} 158 159sp<IMemory> MediaMetadataRetriever::extractAlbumArt() 160{ 161 LOGV("extractAlbumArt"); 162 Mutex::Autolock _l(mLock); 163 if (mRetriever == 0) { 164 LOGE("retriever is not initialized"); 165 return NULL; 166 } 167 return mRetriever->extractAlbumArt(); 168} 169 170void MediaMetadataRetriever::DeathNotifier::binderDied(const wp<IBinder>& who) { 171 Mutex::Autolock lock(MediaMetadataRetriever::sServiceLock); 172 MediaMetadataRetriever::sService.clear(); 173 LOGW("MediaMetadataRetriever server died!"); 174} 175 176MediaMetadataRetriever::DeathNotifier::~DeathNotifier() 177{ 178 Mutex::Autolock lock(sServiceLock); 179 if (sService != 0) { 180 sService->asBinder()->unlinkToDeath(this); 181 } 182} 183 184}; // namespace android 185