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> |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 26 | #include <utils/Vector.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <ui/SurfaceComposerClient.h> |
| 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: |
| 68 | AudioOutput(); |
| 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; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 79 | |
| 80 | virtual status_t open( |
| 81 | uint32_t sampleRate, int channelCount, |
| 82 | int format, int bufferCount, |
| 83 | AudioCallback cb, void *cookie); |
| 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | virtual void start(); |
| 86 | virtual ssize_t write(const void* buffer, size_t size); |
| 87 | virtual void stop(); |
| 88 | virtual void flush(); |
| 89 | virtual void pause(); |
| 90 | virtual void close(); |
| 91 | void setAudioStreamType(int streamType) { mStreamType = streamType; } |
| 92 | void setVolume(float left, float right); |
| 93 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 94 | |
| 95 | static bool isOnEmulator(); |
| 96 | static int getMinBufferCount(); |
| 97 | private: |
| 98 | static void setMinBufferCount(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 99 | static void CallbackWrapper( |
| 100 | int event, void *me, void *info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
| 102 | AudioTrack* mTrack; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 103 | AudioCallback mCallback; |
| 104 | void * mCallbackCookie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | int mStreamType; |
| 106 | float mLeftVolume; |
| 107 | float mRightVolume; |
| 108 | float mMsecsPerFrame; |
| 109 | uint32_t mLatency; |
| 110 | |
| 111 | // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround |
| 112 | static const uint32_t kAudioVideoDelayMs; |
| 113 | static bool mIsOnEmulator; |
| 114 | static int mMinBufferCount; // 12 for emulator; otherwise 4 |
| 115 | |
Marco Nelissen | c39d2e3 | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 116 | public: // visualization hack support |
| 117 | uint32_t mNumFramesWritten; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | class AudioCache : public MediaPlayerBase::AudioSink |
| 121 | { |
| 122 | public: |
| 123 | AudioCache(const char* name); |
| 124 | virtual ~AudioCache() {} |
| 125 | |
| 126 | virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); } |
| 127 | virtual bool realtime() const { return false; } |
| 128 | virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; } |
| 129 | virtual ssize_t frameCount() const { return mFrameCount; } |
| 130 | virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; } |
| 131 | virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); } |
| 132 | virtual uint32_t latency() const; |
| 133 | virtual float msecsPerFrame() const; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 134 | |
| 135 | virtual status_t open( |
| 136 | uint32_t sampleRate, int channelCount, int format, |
| 137 | int bufferCount = 1, |
| 138 | AudioCallback cb = NULL, void *cookie = NULL); |
| 139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | virtual void start() {} |
| 141 | virtual ssize_t write(const void* buffer, size_t size); |
| 142 | virtual void stop() {} |
| 143 | virtual void flush() {} |
| 144 | virtual void pause() {} |
| 145 | virtual void close() {} |
| 146 | void setAudioStreamType(int streamType) {} |
| 147 | void setVolume(float left, float right) {} |
| 148 | uint32_t sampleRate() const { return mSampleRate; } |
| 149 | uint32_t format() const { return (uint32_t)mFormat; } |
| 150 | size_t size() const { return mSize; } |
| 151 | status_t wait(); |
| 152 | |
| 153 | sp<IMemoryHeap> getHeap() const { return mHeap; } |
| 154 | |
| 155 | static void notify(void* cookie, int msg, int ext1, int ext2); |
| 156 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 157 | |
| 158 | private: |
| 159 | AudioCache(); |
| 160 | |
| 161 | Mutex mLock; |
| 162 | Condition mSignal; |
| 163 | sp<MemoryHeapBase> mHeap; |
| 164 | float mMsecsPerFrame; |
| 165 | uint16_t mChannelCount; |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 166 | uint16_t mFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | ssize_t mFrameCount; |
| 168 | uint32_t mSampleRate; |
| 169 | uint32_t mSize; |
| 170 | int mError; |
| 171 | bool mCommandComplete; |
| 172 | }; |
| 173 | |
| 174 | public: |
| 175 | static void instantiate(); |
| 176 | |
| 177 | // IMediaPlayerService interface |
| 178 | virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid); |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 179 | void removeMediaRecorderClient(wp<MediaRecorderClient> client); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid); |
| 181 | |
| 182 | // House keeping for media player clients |
| 183 | virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url); |
| 184 | virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length); |
| 185 | virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat); |
| 186 | virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat); |
Marco Nelissen | c39d2e3 | 2009-09-20 10:42:13 -0700 | [diff] [blame] | 187 | virtual sp<IMemory> snoop(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 188 | virtual sp<IOMX> createOMX(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
| 190 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 191 | |
| 192 | void removeClient(wp<Client> client); |
| 193 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 194 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | private: |
| 196 | |
| 197 | class Client : public BnMediaPlayer { |
| 198 | |
| 199 | // IMediaPlayer interface |
| 200 | virtual void disconnect(); |
| 201 | virtual status_t setVideoSurface(const sp<ISurface>& surface); |
| 202 | virtual status_t prepareAsync(); |
| 203 | virtual status_t start(); |
| 204 | virtual status_t stop(); |
| 205 | virtual status_t pause(); |
| 206 | virtual status_t isPlaying(bool* state); |
| 207 | virtual status_t seekTo(int msec); |
| 208 | virtual status_t getCurrentPosition(int* msec); |
| 209 | virtual status_t getDuration(int* msec); |
| 210 | virtual status_t reset(); |
| 211 | virtual status_t setAudioStreamType(int type); |
| 212 | virtual status_t setLooping(int loop); |
| 213 | virtual status_t setVolume(float leftVolume, float rightVolume); |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 214 | virtual status_t invoke(const Parcel& request, Parcel *reply); |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 215 | virtual status_t setMetadataFilter(const Parcel& filter); |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 216 | virtual status_t getMetadata(bool update_only, |
| 217 | bool apply_filter, |
| 218 | Parcel *reply); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | |
| 220 | sp<MediaPlayerBase> createPlayer(player_type playerType); |
| 221 | status_t setDataSource(const char *url); |
| 222 | status_t setDataSource(int fd, int64_t offset, int64_t length); |
| 223 | static void notify(void* cookie, int msg, int ext1, int ext2); |
| 224 | |
| 225 | pid_t pid() const { return mPid; } |
| 226 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 227 | |
| 228 | private: |
| 229 | friend class MediaPlayerService; |
| 230 | Client( const sp<MediaPlayerService>& service, |
| 231 | pid_t pid, |
| 232 | int32_t connId, |
| 233 | const sp<IMediaPlayerClient>& client); |
| 234 | Client(); |
| 235 | virtual ~Client(); |
| 236 | |
| 237 | void deletePlayer(); |
| 238 | |
| 239 | sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; } |
| 240 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 241 | |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 242 | |
| 243 | // @param type Of the metadata to be tested. |
| 244 | // @return true if the metadata should be dropped according to |
| 245 | // the filters. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 246 | bool shouldDropMetadata(media::Metadata::Type type) const; |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 247 | |
| 248 | // Add a new element to the set of metadata updated. Noop if |
| 249 | // the element exists already. |
| 250 | // @param type Of the metadata to be recorded. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 251 | void addNewMetadataUpdate(media::Metadata::Type type); |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 252 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | mutable Mutex mLock; |
| 254 | sp<MediaPlayerBase> mPlayer; |
| 255 | sp<MediaPlayerService> mService; |
| 256 | sp<IMediaPlayerClient> mClient; |
| 257 | sp<AudioOutput> mAudioOutput; |
| 258 | pid_t mPid; |
| 259 | status_t mStatus; |
| 260 | bool mLoop; |
| 261 | int32_t mConnId; |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 262 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 263 | // Metadata filters. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 264 | media::Metadata::Filter mMetadataAllow; // protected by mLock |
| 265 | media::Metadata::Filter mMetadataDrop; // protected by mLock |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 266 | |
| 267 | // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE |
| 268 | // notification we try to update mMetadataUpdated which is a |
| 269 | // set: no duplicate. |
| 270 | // getMetadata clears this set. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 271 | media::Metadata::Filter mMetadataUpdated; // protected by mLock |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | #if CALLBACK_ANTAGONIZER |
| 274 | Antagonizer* mAntagonizer; |
| 275 | #endif |
| 276 | }; |
| 277 | |
| 278 | // ---------------------------------------------------------------------------- |
| 279 | |
| 280 | MediaPlayerService(); |
| 281 | virtual ~MediaPlayerService(); |
| 282 | |
| 283 | mutable Mutex mLock; |
| 284 | SortedVector< wp<Client> > mClients; |
Gloria Wang | 608a263 | 2009-10-29 15:46:37 -0700 | [diff] [blame] | 285 | SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | int32_t mNextConnId; |
| 287 | }; |
| 288 | |
| 289 | // ---------------------------------------------------------------------------- |
| 290 | |
| 291 | }; // namespace android |
| 292 | |
| 293 | #endif // ANDROID_MEDIAPLAYERSERVICE_H |