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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | #define CALLBACK_ANTAGONIZER 0 |
| 40 | #if CALLBACK_ANTAGONIZER |
| 41 | class Antagonizer { |
| 42 | public: |
| 43 | Antagonizer(notify_callback_f cb, void* client); |
| 44 | void start() { mActive = true; } |
| 45 | void stop() { mActive = false; } |
| 46 | void kill(); |
| 47 | private: |
| 48 | static const int interval; |
| 49 | Antagonizer(); |
| 50 | static int callbackThread(void* cookie); |
| 51 | Mutex mLock; |
| 52 | Condition mCondition; |
| 53 | bool mExit; |
| 54 | bool mActive; |
| 55 | void* mClient; |
| 56 | notify_callback_f mCb; |
| 57 | }; |
| 58 | #endif |
| 59 | |
| 60 | class MediaPlayerService : public BnMediaPlayerService |
| 61 | { |
| 62 | class Client; |
| 63 | |
| 64 | class AudioOutput : public MediaPlayerBase::AudioSink |
| 65 | { |
| 66 | public: |
| 67 | AudioOutput(); |
| 68 | virtual ~AudioOutput(); |
| 69 | |
| 70 | virtual bool ready() const { return mTrack != NULL; } |
| 71 | virtual bool realtime() const { return true; } |
| 72 | virtual ssize_t bufferSize() const; |
| 73 | virtual ssize_t frameCount() const; |
| 74 | virtual ssize_t channelCount() const; |
| 75 | virtual ssize_t frameSize() const; |
| 76 | virtual uint32_t latency() const; |
| 77 | virtual float msecsPerFrame() const; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 78 | |
| 79 | virtual status_t open( |
| 80 | uint32_t sampleRate, int channelCount, |
| 81 | int format, int bufferCount, |
| 82 | AudioCallback cb, void *cookie); |
| 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | virtual void start(); |
| 85 | virtual ssize_t write(const void* buffer, size_t size); |
| 86 | virtual void stop(); |
| 87 | virtual void flush(); |
| 88 | virtual void pause(); |
| 89 | virtual void close(); |
| 90 | void setAudioStreamType(int streamType) { mStreamType = streamType; } |
| 91 | void setVolume(float left, float right); |
| 92 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 93 | |
| 94 | static bool isOnEmulator(); |
| 95 | static int getMinBufferCount(); |
| 96 | private: |
| 97 | static void setMinBufferCount(); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 98 | static void CallbackWrapper( |
| 99 | int event, void *me, void *info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | |
| 101 | AudioTrack* mTrack; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 102 | AudioCallback mCallback; |
| 103 | void * mCallbackCookie; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 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 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 163 | uint16_t mFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | ssize_t mFrameCount; |
| 165 | uint32_t mSampleRate; |
| 166 | uint32_t mSize; |
| 167 | int mError; |
| 168 | bool mCommandComplete; |
| 169 | }; |
| 170 | |
| 171 | public: |
| 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); |
Andreas Huber | e46b7be | 2009-07-14 16:56:47 -0700 | [diff] [blame] | 183 | virtual sp<IOMX> createOMX(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | |
| 185 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 186 | |
| 187 | void removeClient(wp<Client> client); |
| 188 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | private: |
| 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); |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame] | 209 | virtual status_t invoke(const Parcel& request, Parcel *reply); |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 210 | virtual status_t setMetadataFilter(const Parcel& filter); |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 211 | virtual status_t getMetadata(bool update_only, |
| 212 | bool apply_filter, |
| 213 | Parcel *reply); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 236 | |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 237 | |
| 238 | // @param type Of the metadata to be tested. |
| 239 | // @return true if the metadata should be dropped according to |
| 240 | // the filters. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 241 | bool shouldDropMetadata(media::Metadata::Type type) const; |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 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. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 246 | void addNewMetadataUpdate(media::Metadata::Type type); |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 247 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 257 | |
Nicolas Catania | b2c6939 | 2009-07-08 08:57:42 -0700 | [diff] [blame] | 258 | // Metadata filters. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 259 | media::Metadata::Filter mMetadataAllow; // protected by mLock |
| 260 | media::Metadata::Filter mMetadataDrop; // protected by mLock |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 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. |
niko | bc72692 | 2009-07-20 15:07:26 -0700 | [diff] [blame] | 266 | media::Metadata::Filter mMetadataUpdated; // protected by mLock |
Nicolas Catania | 4df8b2c | 2009-07-10 13:53:06 -0700 | [diff] [blame] | 267 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 |