MediaPlayerService.h revision a64c8c79af1a15911c55306d83a797fa50969f77
16047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar/* 26047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** 36047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** Copyright 2008, The Android Open Source Project 46047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** 56047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** Licensed under the Apache License, Version 2.0 (the "License"); 66047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** you may not use this file except in compliance with the License. 76047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** You may obtain a copy of the License at 86047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** 96047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** http://www.apache.org/licenses/LICENSE-2.0 106047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** 116047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** Unless required by applicable law or agreed to in writing, software 126047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** distributed under the License is distributed on an "AS IS" BASIS, 136047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 146047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** See the License for the specific language governing permissions and 156047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar** limitations under the License. 166047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar*/ 176047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 186047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#ifndef ANDROID_MEDIAPLAYERSERVICE_H 196047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#define ANDROID_MEDIAPLAYERSERVICE_H 206047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 216047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <utils/Log.h> 226047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <utils/threads.h> 236047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <utils/List.h> 246047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <utils/Errors.h> 256047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <utils/KeyedVector.h> 266047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <utils/Vector.h> 276047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <ui/SurfaceComposerClient.h> 286047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 296047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <media/IMediaPlayerService.h> 306047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <media/MediaPlayerInterface.h> 316047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#include <media/Metadata.h> 326047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 336047998943beebd81e0ae1068df39c0cbee38628Yigit Boyarnamespace android { 346047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 356047998943beebd81e0ae1068df39c0cbee38628Yigit Boyarclass IMediaRecorder; 366047998943beebd81e0ae1068df39c0cbee38628Yigit Boyarclass IMediaMetadataRetriever; 376047998943beebd81e0ae1068df39c0cbee38628Yigit Boyarclass IOMX; 386047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 396047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#define CALLBACK_ANTAGONIZER 0 406047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#if CALLBACK_ANTAGONIZER 416047998943beebd81e0ae1068df39c0cbee38628Yigit Boyarclass Antagonizer { 426047998943beebd81e0ae1068df39c0cbee38628Yigit Boyarpublic: 436047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar Antagonizer(notify_callback_f cb, void* client); 446047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar void start() { mActive = true; } 456047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar void stop() { mActive = false; } 466047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar void kill(); 476047998943beebd81e0ae1068df39c0cbee38628Yigit Boyarprivate: 486047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar static const int interval; 496047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar Antagonizer(); 506047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar static int callbackThread(void* cookie); 516047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar Mutex mLock; 526047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar Condition mCondition; 536047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar bool mExit; 546047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar bool mActive; 556047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar void* mClient; 566047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar notify_callback_f mCb; 576047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar}; 586047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar#endif 596047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 606047998943beebd81e0ae1068df39c0cbee38628Yigit Boyarclass MediaPlayerService : public BnMediaPlayerService 616047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar{ 626047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar class Client; 636047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 646047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar class AudioOutput : public MediaPlayerBase::AudioSink 656047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar { 666047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar public: 676047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar AudioOutput(); 686047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual ~AudioOutput(); 696047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 706047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual bool ready() const { return mTrack != NULL; } 71bb4a033fcd5cd20e5be46ef8ead442dc7db2454dGeorge Mount virtual bool realtime() const { return true; } 726047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual ssize_t bufferSize() const; 736047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual ssize_t frameCount() const; 746047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual ssize_t channelCount() const; 756047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual ssize_t frameSize() const; 766047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual uint32_t latency() const; 776047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual float msecsPerFrame() const; 786047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 796047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual status_t open( 806047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar uint32_t sampleRate, int channelCount, 816047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar int format, int bufferCount, 826047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar AudioCallback cb, void *cookie); 836047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 846047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual void start(); 856047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual ssize_t write(const void* buffer, size_t size); 866047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual void stop(); 876047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar virtual void flush(); 88bb4a033fcd5cd20e5be46ef8ead442dc7db2454dGeorge Mount virtual void pause(); 89bb4a033fcd5cd20e5be46ef8ead442dc7db2454dGeorge Mount virtual void close(); 90bb4a033fcd5cd20e5be46ef8ead442dc7db2454dGeorge Mount void setAudioStreamType(int streamType) { mStreamType = streamType; } 91bb4a033fcd5cd20e5be46ef8ead442dc7db2454dGeorge Mount void setVolume(float left, float right); 92bb4a033fcd5cd20e5be46ef8ead442dc7db2454dGeorge Mount virtual status_t dump(int fd, const Vector<String16>& args) const; 936047998943beebd81e0ae1068df39c0cbee38628Yigit Boyar 94 static bool isOnEmulator(); 95 static int getMinBufferCount(); 96 private: 97 static void setMinBufferCount(); 98 static void CallbackWrapper( 99 int event, void *me, void *info); 100 101 AudioTrack* mTrack; 102 AudioCallback mCallback; 103 void * mCallbackCookie; 104 int mStreamType; 105 float mLeftVolume; 106 float mRightVolume; 107 float mMsecsPerFrame; 108 uint32_t mLatency; 109 110 // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround 111 static const uint32_t kAudioVideoDelayMs; 112 static bool mIsOnEmulator; 113 static int mMinBufferCount; // 12 for emulator; otherwise 4 114 115 }; 116 117 class AudioCache : public MediaPlayerBase::AudioSink 118 { 119 public: 120 AudioCache(const char* name); 121 virtual ~AudioCache() {} 122 123 virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); } 124 virtual bool realtime() const { return false; } 125 virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; } 126 virtual ssize_t frameCount() const { return mFrameCount; } 127 virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; } 128 virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); } 129 virtual uint32_t latency() const; 130 virtual float msecsPerFrame() const; 131 132 virtual status_t open( 133 uint32_t sampleRate, int channelCount, int format, 134 int bufferCount = 1, 135 AudioCallback cb = NULL, void *cookie = NULL); 136 137 virtual void start() {} 138 virtual ssize_t write(const void* buffer, size_t size); 139 virtual void stop() {} 140 virtual void flush() {} 141 virtual void pause() {} 142 virtual void close() {} 143 void setAudioStreamType(int streamType) {} 144 void setVolume(float left, float right) {} 145 uint32_t sampleRate() const { return mSampleRate; } 146 uint32_t format() const { return (uint32_t)mFormat; } 147 size_t size() const { return mSize; } 148 status_t wait(); 149 150 sp<IMemoryHeap> getHeap() const { return mHeap; } 151 152 static void notify(void* cookie, int msg, int ext1, int ext2); 153 virtual status_t dump(int fd, const Vector<String16>& args) const; 154 155 private: 156 AudioCache(); 157 158 Mutex mLock; 159 Condition mSignal; 160 sp<MemoryHeapBase> mHeap; 161 float mMsecsPerFrame; 162 uint16_t mChannelCount; 163 uint16_t mFormat; 164 ssize_t mFrameCount; 165 uint32_t mSampleRate; 166 uint32_t mSize; 167 int mError; 168 bool mCommandComplete; 169 }; 170 171public: 172 static void instantiate(); 173 174 // IMediaPlayerService interface 175 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid); 176 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid); 177 178 // House keeping for media player clients 179 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url); 180 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length); 181 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat); 182 virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat); 183 virtual sp<IOMX> createOMX(); 184 185 virtual status_t dump(int fd, const Vector<String16>& args); 186 187 void removeClient(wp<Client> client); 188 189 190private: 191 192 class Client : public BnMediaPlayer { 193 194 // IMediaPlayer interface 195 virtual void disconnect(); 196 virtual status_t setVideoSurface(const sp<ISurface>& surface); 197 virtual status_t prepareAsync(); 198 virtual status_t start(); 199 virtual status_t stop(); 200 virtual status_t pause(); 201 virtual status_t isPlaying(bool* state); 202 virtual status_t seekTo(int msec); 203 virtual status_t getCurrentPosition(int* msec); 204 virtual status_t getDuration(int* msec); 205 virtual status_t reset(); 206 virtual status_t setAudioStreamType(int type); 207 virtual status_t setLooping(int loop); 208 virtual status_t setVolume(float leftVolume, float rightVolume); 209 virtual status_t invoke(const Parcel& request, Parcel *reply); 210 virtual status_t setMetadataFilter(const Parcel& filter); 211 virtual status_t getMetadata(bool update_only, 212 bool apply_filter, 213 Parcel *reply); 214 215 sp<MediaPlayerBase> createPlayer(player_type playerType); 216 status_t setDataSource(const char *url); 217 status_t setDataSource(int fd, int64_t offset, int64_t length); 218 static void notify(void* cookie, int msg, int ext1, int ext2); 219 220 pid_t pid() const { return mPid; } 221 virtual status_t dump(int fd, const Vector<String16>& args) const; 222 223 private: 224 friend class MediaPlayerService; 225 Client( const sp<MediaPlayerService>& service, 226 pid_t pid, 227 int32_t connId, 228 const sp<IMediaPlayerClient>& client); 229 Client(); 230 virtual ~Client(); 231 232 void deletePlayer(); 233 234 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; } 235 236 237 238 // @param type Of the metadata to be tested. 239 // @return true if the metadata should be dropped according to 240 // the filters. 241 bool shouldDropMetadata(media::Metadata::Type type) const; 242 243 // Add a new element to the set of metadata updated. Noop if 244 // the element exists already. 245 // @param type Of the metadata to be recorded. 246 void addNewMetadataUpdate(media::Metadata::Type type); 247 248 mutable Mutex mLock; 249 sp<MediaPlayerBase> mPlayer; 250 sp<MediaPlayerService> mService; 251 sp<IMediaPlayerClient> mClient; 252 sp<AudioOutput> mAudioOutput; 253 pid_t mPid; 254 status_t mStatus; 255 bool mLoop; 256 int32_t mConnId; 257 258 // Metadata filters. 259 media::Metadata::Filter mMetadataAllow; // protected by mLock 260 media::Metadata::Filter mMetadataDrop; // protected by mLock 261 262 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE 263 // notification we try to update mMetadataUpdated which is a 264 // set: no duplicate. 265 // getMetadata clears this set. 266 media::Metadata::Filter mMetadataUpdated; // protected by mLock 267 268#if CALLBACK_ANTAGONIZER 269 Antagonizer* mAntagonizer; 270#endif 271 }; 272 273// ---------------------------------------------------------------------------- 274 275 MediaPlayerService(); 276 virtual ~MediaPlayerService(); 277 278 mutable Mutex mLock; 279 SortedVector< wp<Client> > mClients; 280 int32_t mNextConnId; 281}; 282 283// ---------------------------------------------------------------------------- 284 285}; // namespace android 286 287#endif // ANDROID_MEDIAPLAYERSERVICE_H 288