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> |
| 26 | #include <ui/SurfaceComposerClient.h> |
| 27 | |
| 28 | #include <media/IMediaPlayerService.h> |
| 29 | #include <media/MediaPlayerInterface.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | class IMediaRecorder; |
| 34 | class IMediaMetadataRetriever; |
| 35 | |
| 36 | #define CALLBACK_ANTAGONIZER 0 |
| 37 | #if CALLBACK_ANTAGONIZER |
| 38 | class Antagonizer { |
| 39 | public: |
| 40 | Antagonizer(notify_callback_f cb, void* client); |
| 41 | void start() { mActive = true; } |
| 42 | void stop() { mActive = false; } |
| 43 | void kill(); |
| 44 | private: |
| 45 | static const int interval; |
| 46 | Antagonizer(); |
| 47 | static int callbackThread(void* cookie); |
| 48 | Mutex mLock; |
| 49 | Condition mCondition; |
| 50 | bool mExit; |
| 51 | bool mActive; |
| 52 | void* mClient; |
| 53 | notify_callback_f mCb; |
| 54 | }; |
| 55 | #endif |
| 56 | |
| 57 | class MediaPlayerService : public BnMediaPlayerService |
| 58 | { |
| 59 | class Client; |
| 60 | |
| 61 | class AudioOutput : public MediaPlayerBase::AudioSink |
| 62 | { |
| 63 | public: |
| 64 | AudioOutput(); |
| 65 | virtual ~AudioOutput(); |
| 66 | |
| 67 | virtual bool ready() const { return mTrack != NULL; } |
| 68 | virtual bool realtime() const { return true; } |
| 69 | virtual ssize_t bufferSize() const; |
| 70 | virtual ssize_t frameCount() const; |
| 71 | virtual ssize_t channelCount() const; |
| 72 | virtual ssize_t frameSize() const; |
| 73 | virtual uint32_t latency() const; |
| 74 | virtual float msecsPerFrame() const; |
| 75 | virtual status_t open(uint32_t sampleRate, int channelCount, int format, int bufferCount=4); |
| 76 | virtual void start(); |
| 77 | virtual ssize_t write(const void* buffer, size_t size); |
| 78 | virtual void stop(); |
| 79 | virtual void flush(); |
| 80 | virtual void pause(); |
| 81 | virtual void close(); |
| 82 | void setAudioStreamType(int streamType) { mStreamType = streamType; } |
| 83 | void setVolume(float left, float right); |
| 84 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 85 | |
| 86 | static bool isOnEmulator(); |
| 87 | static int getMinBufferCount(); |
| 88 | private: |
| 89 | static void setMinBufferCount(); |
| 90 | |
| 91 | AudioTrack* mTrack; |
| 92 | int mStreamType; |
| 93 | float mLeftVolume; |
| 94 | float mRightVolume; |
| 95 | float mMsecsPerFrame; |
| 96 | uint32_t mLatency; |
| 97 | |
| 98 | // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround |
| 99 | static const uint32_t kAudioVideoDelayMs; |
| 100 | static bool mIsOnEmulator; |
| 101 | static int mMinBufferCount; // 12 for emulator; otherwise 4 |
| 102 | |
| 103 | }; |
| 104 | |
| 105 | class AudioCache : public MediaPlayerBase::AudioSink |
| 106 | { |
| 107 | public: |
| 108 | AudioCache(const char* name); |
| 109 | virtual ~AudioCache() {} |
| 110 | |
| 111 | virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); } |
| 112 | virtual bool realtime() const { return false; } |
| 113 | virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; } |
| 114 | virtual ssize_t frameCount() const { return mFrameCount; } |
| 115 | virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; } |
| 116 | virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); } |
| 117 | virtual uint32_t latency() const; |
| 118 | virtual float msecsPerFrame() const; |
| 119 | virtual status_t open(uint32_t sampleRate, int channelCount, int format, int bufferCount=1); |
| 120 | virtual void start() {} |
| 121 | virtual ssize_t write(const void* buffer, size_t size); |
| 122 | virtual void stop() {} |
| 123 | virtual void flush() {} |
| 124 | virtual void pause() {} |
| 125 | virtual void close() {} |
| 126 | void setAudioStreamType(int streamType) {} |
| 127 | void setVolume(float left, float right) {} |
| 128 | uint32_t sampleRate() const { return mSampleRate; } |
| 129 | uint32_t format() const { return (uint32_t)mFormat; } |
| 130 | size_t size() const { return mSize; } |
| 131 | status_t wait(); |
| 132 | |
| 133 | sp<IMemoryHeap> getHeap() const { return mHeap; } |
| 134 | |
| 135 | static void notify(void* cookie, int msg, int ext1, int ext2); |
| 136 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 137 | |
| 138 | private: |
| 139 | AudioCache(); |
| 140 | |
| 141 | Mutex mLock; |
| 142 | Condition mSignal; |
| 143 | sp<MemoryHeapBase> mHeap; |
| 144 | float mMsecsPerFrame; |
| 145 | uint16_t mChannelCount; |
| 146 | uint16_t mFormat; |
| 147 | ssize_t mFrameCount; |
| 148 | uint32_t mSampleRate; |
| 149 | uint32_t mSize; |
| 150 | int mError; |
| 151 | bool mCommandComplete; |
| 152 | }; |
| 153 | |
| 154 | public: |
| 155 | static void instantiate(); |
| 156 | |
| 157 | // IMediaPlayerService interface |
| 158 | virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid); |
| 159 | virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid); |
| 160 | |
| 161 | // House keeping for media player clients |
| 162 | virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url); |
| 163 | virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length); |
| 164 | virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat); |
| 165 | virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat); |
| 166 | |
| 167 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 168 | |
| 169 | void removeClient(wp<Client> client); |
| 170 | |
| 171 | private: |
| 172 | |
| 173 | class Client : public BnMediaPlayer { |
| 174 | |
| 175 | // IMediaPlayer interface |
| 176 | virtual void disconnect(); |
| 177 | virtual status_t setVideoSurface(const sp<ISurface>& surface); |
| 178 | virtual status_t prepareAsync(); |
| 179 | virtual status_t start(); |
| 180 | virtual status_t stop(); |
| 181 | virtual status_t pause(); |
| 182 | virtual status_t isPlaying(bool* state); |
| 183 | virtual status_t seekTo(int msec); |
| 184 | virtual status_t getCurrentPosition(int* msec); |
| 185 | virtual status_t getDuration(int* msec); |
| 186 | virtual status_t reset(); |
| 187 | virtual status_t setAudioStreamType(int type); |
| 188 | virtual status_t setLooping(int loop); |
| 189 | virtual status_t setVolume(float leftVolume, float rightVolume); |
Nicolas Catania | 20cb94e | 2009-05-12 23:25:55 -0700 | [diff] [blame^] | 190 | virtual status_t invoke(const Parcel& request, Parcel *reply); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
| 192 | sp<MediaPlayerBase> createPlayer(player_type playerType); |
| 193 | status_t setDataSource(const char *url); |
| 194 | status_t setDataSource(int fd, int64_t offset, int64_t length); |
| 195 | static void notify(void* cookie, int msg, int ext1, int ext2); |
| 196 | |
| 197 | pid_t pid() const { return mPid; } |
| 198 | virtual status_t dump(int fd, const Vector<String16>& args) const; |
| 199 | |
| 200 | private: |
| 201 | friend class MediaPlayerService; |
| 202 | Client( const sp<MediaPlayerService>& service, |
| 203 | pid_t pid, |
| 204 | int32_t connId, |
| 205 | const sp<IMediaPlayerClient>& client); |
| 206 | Client(); |
| 207 | virtual ~Client(); |
| 208 | |
| 209 | void deletePlayer(); |
| 210 | |
| 211 | sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; } |
| 212 | |
| 213 | mutable Mutex mLock; |
| 214 | sp<MediaPlayerBase> mPlayer; |
| 215 | sp<MediaPlayerService> mService; |
| 216 | sp<IMediaPlayerClient> mClient; |
| 217 | sp<AudioOutput> mAudioOutput; |
| 218 | pid_t mPid; |
| 219 | status_t mStatus; |
| 220 | bool mLoop; |
| 221 | int32_t mConnId; |
| 222 | #if CALLBACK_ANTAGONIZER |
| 223 | Antagonizer* mAntagonizer; |
| 224 | #endif |
| 225 | }; |
| 226 | |
| 227 | // ---------------------------------------------------------------------------- |
| 228 | |
| 229 | MediaPlayerService(); |
| 230 | virtual ~MediaPlayerService(); |
| 231 | |
| 232 | mutable Mutex mLock; |
| 233 | SortedVector< wp<Client> > mClients; |
| 234 | int32_t mNextConnId; |
| 235 | }; |
| 236 | |
| 237 | // ---------------------------------------------------------------------------- |
| 238 | |
| 239 | }; // namespace android |
| 240 | |
| 241 | #endif // ANDROID_MEDIAPLAYERSERVICE_H |