The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | #ifndef ANDROID_MEDIAPLAYERSERVICE_H |
| 19 | #define ANDROID_MEDIAPLAYERSERVICE_H |
| 20 | |
Mathias Agopian | 3b4062e | 2009-05-31 19:13:00 -0700 | [diff] [blame] | 21 | #include <utils/Log.h> |
| 22 | #include <utils/threads.h> |
| 23 | #include <utils/List.h> |
| 24 | #include <utils/Errors.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | #include <utils/KeyedVector.h> |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 26 | #include <utils/String8.h> |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 27 | #include <utils/Vector.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | |
| 29 | #include <media/IMediaPlayerService.h> |
| 30 | #include <media/MediaPlayerInterface.h> |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 31 | #include <media/Metadata.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | class IMediaRecorder; |
| 36 | class IMediaMetadataRetriever; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 37 | class IOMX; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 38 | class MediaRecorderClient; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
| 40 | #define CALLBACK_ANTAGONIZER 0 |
| 41 | #if CALLBACK_ANTAGONIZER |
| 42 | class Antagonizer { |
| 43 | public: |
| 44 | Antagonizer(notify_callback_f cb, void* client); |
| 45 | void start() { mActive = true; } |
| 46 | void stop() { mActive = false; } |
| 47 | void kill(); |
| 48 | private: |
| 49 | static const int interval; |
| 50 | Antagonizer(); |
| 51 | static int callbackThread(void* cookie); |
| 52 | Mutex mLock; |
| 53 | Condition mCondition; |
| 54 | bool mExit; |
| 55 | bool mActive; |
| 56 | void* mClient; |
| 57 | notify_callback_f mCb; |
| 58 | }; |
| 59 | #endif |
| 60 | |
| 61 | class MediaPlayerService : public BnMediaPlayerService |
| 62 | { |
| 63 | class Client; |
| 64 | |
| 65 | class AudioOutput : public MediaPlayerBase::AudioSink |
| 66 | { |
| 67 | public: |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 68 | AudioOutput(int sessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | virtual ~AudioOutput(); |
| 70 | |
| 71 | virtual bool ready() const { return mTrack != NULL; } |
| 72 | virtual bool realtime() const { return true; } |
| 73 | virtual ssize_t bufferSize() const; |
| 74 | virtual ssize_t frameCount() const; |
| 75 | virtual ssize_t channelCount() const; |
| 76 | virtual ssize_t frameSize() const; |
| 77 | virtual uint32_t latency() const; |
| 78 | virtual float msecsPerFrame() const; |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 79 | virtual status_t getPosition(uint32_t *position); |
Eric Laurent | b3bdf3f | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 80 | virtual int getSessionId(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 81 | |
| 82 | virtual status_t open( |
| 83 | uint32_t sampleRate, int channelCount, |
| 84 | int format, int bufferCount, |
| 85 | AudioCallback cb, void *cookie); |
| 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | virtual void start(); |
| 88 | virtual ssize_t write(const void* buffer, size_t size); |
| 89 | virtual void stop(); |
| 90 | virtual void flush(); |
| 91 | virtual void pause(); |
| 92 | virtual void close(); |
| 93 | void setAudioStreamType(int streamType) { mStreamType = streamType; } |
| 94 | void setVolume(float left, float right); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 95 | status_t setAuxEffectSendLevel(float level); |
| 96 | status_t attachAuxEffect(int effectId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 98 | |
| 99 | static bool isOnEmulator(); |
| 100 | static int getMinBufferCount(); |
| 101 | private: |
| 102 | static void setMinBufferCount(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 103 | static void CallbackWrapper( |
| 104 | int event, void *me, void *info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | |
| 106 | AudioTrack* mTrack; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 107 | AudioCallback mCallback; |
| 108 | void * mCallbackCookie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | int mStreamType; |
| 110 | float mLeftVolume; |
| 111 | float mRightVolume; |
| 112 | float mMsecsPerFrame; |
| 113 | uint32_t mLatency; |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 114 | int mSessionId; |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 115 | float mSendLevel; |
| 116 | int mAuxEffectId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | static bool mIsOnEmulator; |
| 118 | static int mMinBufferCount; // 12 for emulator; otherwise 4 |
| 119 | |
| 120 | }; |
| 121 | |
| 122 | class AudioCache : public MediaPlayerBase::AudioSink |
| 123 | { |
| 124 | public: |
| 125 | AudioCache(const char* name); |
| 126 | virtual ~AudioCache() {} |
| 127 | |
| 128 | virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); } |
| 129 | virtual bool realtime() const { return false; } |
| 130 | virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; } |
| 131 | virtual ssize_t frameCount() const { return mFrameCount; } |
| 132 | virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; } |
| 133 | virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); } |
| 134 | virtual uint32_t latency() const; |
| 135 | virtual float msecsPerFrame() const; |
Eric Laurent | 0986e79 | 2010-01-19 17:37:09 -0800 | [diff] [blame] | 136 | virtual status_t getPosition(uint32_t *position); |
Eric Laurent | b3bdf3f | 2010-10-07 18:23:03 -0700 | [diff] [blame] | 137 | virtual int getSessionId(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 138 | |
| 139 | virtual status_t open( |
| 140 | uint32_t sampleRate, int channelCount, int format, |
| 141 | int bufferCount = 1, |
| 142 | AudioCallback cb = NULL, void *cookie = NULL); |
| 143 | |
Andreas Huber | 6ed937e | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 144 | virtual void start(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | virtual ssize_t write(const void* buffer, size_t size); |
Andreas Huber | 6ed937e | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 146 | virtual void stop(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | virtual void flush() {} |
| 148 | virtual void pause() {} |
| 149 | virtual void close() {} |
| 150 | void setAudioStreamType(int streamType) {} |
| 151 | void setVolume(float left, float right) {} |
| 152 | uint32_t sampleRate() const { return mSampleRate; } |
| 153 | uint32_t format() const { return (uint32_t)mFormat; } |
| 154 | size_t size() const { return mSize; } |
| 155 | status_t wait(); |
| 156 | |
| 157 | sp<IMemoryHeap> getHeap() const { return mHeap; } |
| 158 | |
| 159 | static void notify(void* cookie, int msg, int ext1, int ext2); |
| 160 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 161 | |
| 162 | private: |
| 163 | AudioCache(); |
| 164 | |
| 165 | Mutex mLock; |
| 166 | Condition mSignal; |
| 167 | sp<MemoryHeapBase> mHeap; |
| 168 | float mMsecsPerFrame; |
| 169 | uint16_t mChannelCount; |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 170 | uint16_t mFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | ssize_t mFrameCount; |
| 172 | uint32_t mSampleRate; |
| 173 | uint32_t mSize; |
| 174 | int mError; |
| 175 | bool mCommandComplete; |
Andreas Huber | 6ed937e | 2010-02-09 16:59:18 -0800 | [diff] [blame] | 176 | |
| 177 | sp<Thread> mCallbackThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | public: |
| 181 | static void instantiate(); |
| 182 | |
| 183 | // IMediaPlayerService interface |
| 184 | virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 185 | void removeMediaRecorderClient(wp<MediaRecorderClient> client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid); |
| 187 | |
| 188 | // House keeping for media player clients |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 189 | virtual sp<IMediaPlayer> create( |
| 190 | pid_t pid, const sp<IMediaPlayerClient>& client, const char* url, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 191 | const KeyedVector<String8, String8> *headers, int audioSessionId); |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 192 | |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 193 | virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length, int audioSessionId); |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 194 | |
| 195 | virtual sp<IMediaPlayer> create( |
| 196 | pid_t pid, const sp<IMediaPlayerClient> &client, |
| 197 | const sp<IStreamSource> &source, int audioSessionId); |
| 198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat); |
| 200 | virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat); |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 201 | virtual sp<IOMX> getOMX(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | |
| 203 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 204 | |
| 205 | void removeClient(wp<Client> client); |
| 206 | |
Gloria Wang | d211f41 | 2011-02-19 18:37:57 -0800 | [diff] [blame^] | 207 | // For battery usage tracking purpose |
| 208 | struct BatteryUsageInfo { |
| 209 | // how many streams are being played by one UID |
| 210 | int refCount; |
| 211 | // a temp variable to store the duration(ms) of audio codecs |
| 212 | // when we start a audio codec, we minus the system time from audioLastTime |
| 213 | // when we pause it, we add the system time back to the audioLastTime |
| 214 | // so after the pause, audioLastTime = pause time - start time |
| 215 | // if multiple audio streams are played (or recorded), then audioLastTime |
| 216 | // = the total playing time of all the streams |
| 217 | int32_t audioLastTime; |
| 218 | // when all the audio streams are being paused, we assign audioLastTime to |
| 219 | // this variable, so this value could be provided to the battery app |
| 220 | // in the next pullBatteryData call |
| 221 | int32_t audioTotalTime; |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 222 | |
Gloria Wang | d211f41 | 2011-02-19 18:37:57 -0800 | [diff] [blame^] | 223 | int32_t videoLastTime; |
| 224 | int32_t videoTotalTime; |
| 225 | }; |
| 226 | KeyedVector<int, BatteryUsageInfo> mBatteryData; |
| 227 | |
| 228 | // Collect info of the codec usage from media player and media recorder |
| 229 | virtual void addBatteryData(uint32_t params); |
| 230 | // API for the Battery app to pull the data of codecs usage |
| 231 | virtual status_t pullBatteryData(Parcel* reply); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | private: |
| 233 | |
| 234 | class Client : public BnMediaPlayer { |
| 235 | |
| 236 | // IMediaPlayer interface |
| 237 | virtual void disconnect(); |
Andreas Huber | e3c0183 | 2010-08-16 08:49:37 -0700 | [diff] [blame] | 238 | virtual status_t setVideoSurface(const sp<Surface>& surface); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | virtual status_t prepareAsync(); |
| 240 | virtual status_t start(); |
| 241 | virtual status_t stop(); |
| 242 | virtual status_t pause(); |
| 243 | virtual status_t isPlaying(bool* state); |
| 244 | virtual status_t seekTo(int msec); |
| 245 | virtual status_t getCurrentPosition(int* msec); |
| 246 | virtual status_t getDuration(int* msec); |
| 247 | virtual status_t reset(); |
| 248 | virtual status_t setAudioStreamType(int type); |
| 249 | virtual status_t setLooping(int loop); |
| 250 | virtual status_t setVolume(float leftVolume, float rightVolume); |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 251 | virtual status_t invoke(const Parcel& request, Parcel *reply); |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 252 | virtual status_t setMetadataFilter(const Parcel& filter); |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 253 | virtual status_t getMetadata(bool update_only, |
| 254 | bool apply_filter, |
| 255 | Parcel *reply); |
Eric Laurent | 7070b36 | 2010-07-16 07:43:46 -0700 | [diff] [blame] | 256 | virtual status_t setAuxEffectSendLevel(float level); |
| 257 | virtual status_t attachAuxEffect(int effectId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
| 259 | sp<MediaPlayerBase> createPlayer(player_type playerType); |
Andreas Huber | 2564300 | 2010-01-28 11:19:57 -0800 | [diff] [blame] | 260 | |
| 261 | status_t setDataSource( |
| 262 | const char *url, |
| 263 | const KeyedVector<String8, String8> *headers); |
| 264 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | status_t setDataSource(int fd, int64_t offset, int64_t length); |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 266 | |
| 267 | status_t setDataSource(const sp<IStreamSource> &source); |
| 268 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | static void notify(void* cookie, int msg, int ext1, int ext2); |
| 270 | |
| 271 | pid_t pid() const { return mPid; } |
| 272 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 273 | |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 274 | int getAudioSessionId() { return mAudioSessionId; } |
| 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | private: |
| 277 | friend class MediaPlayerService; |
| 278 | Client( const sp<MediaPlayerService>& service, |
| 279 | pid_t pid, |
| 280 | int32_t connId, |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 281 | const sp<IMediaPlayerClient>& client, |
| 282 | int audioSessionId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | Client(); |
| 284 | virtual ~Client(); |
| 285 | |
| 286 | void deletePlayer(); |
| 287 | |
| 288 | sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; } |
| 289 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 290 | |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 291 | |
| 292 | // @param type Of the metadata to be tested. |
| 293 | // @return true if the metadata should be dropped according to |
| 294 | // the filters. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 295 | bool shouldDropMetadata(media::Metadata::Type type) const; |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 296 | |
| 297 | // Add a new element to the set of metadata updated. Noop if |
| 298 | // the element exists already. |
| 299 | // @param type Of the metadata to be recorded. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 300 | void addNewMetadataUpdate(media::Metadata::Type type); |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 301 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | mutable Mutex mLock; |
| 303 | sp<MediaPlayerBase> mPlayer; |
| 304 | sp<MediaPlayerService> mService; |
| 305 | sp<IMediaPlayerClient> mClient; |
| 306 | sp<AudioOutput> mAudioOutput; |
| 307 | pid_t mPid; |
| 308 | status_t mStatus; |
| 309 | bool mLoop; |
| 310 | int32_t mConnId; |
Eric Laurent | 619346f | 2010-06-21 09:27:30 -0700 | [diff] [blame] | 311 | int mAudioSessionId; |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 312 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 313 | // Metadata filters. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 314 | media::Metadata::Filter mMetadataAllow; // protected by mLock |
| 315 | media::Metadata::Filter mMetadataDrop; // protected by mLock |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 316 | |
| 317 | // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE |
| 318 | // notification we try to update mMetadataUpdated which is a |
| 319 | // set: no duplicate. |
| 320 | // getMetadata clears this set. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 321 | media::Metadata::Filter mMetadataUpdated; // protected by mLock |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 322 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | #if CALLBACK_ANTAGONIZER |
| 324 | Antagonizer* mAntagonizer; |
| 325 | #endif |
| 326 | }; |
| 327 | |
| 328 | // ---------------------------------------------------------------------------- |
| 329 | |
| 330 | MediaPlayerService(); |
| 331 | virtual ~MediaPlayerService(); |
| 332 | |
| 333 | mutable Mutex mLock; |
| 334 | SortedVector< wp<Client> > mClients; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 335 | SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | int32_t mNextConnId; |
Andreas Huber | 784202e | 2009-10-15 13:46:54 -0700 | [diff] [blame] | 337 | sp<IOMX> mOMX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | }; |
| 339 | |
| 340 | // ---------------------------------------------------------------------------- |
| 341 | |
| 342 | }; // namespace android |
| 343 | |
| 344 | #endif // ANDROID_MEDIAPLAYERSERVICE_H |