blob: deb458cd841b43b3d1e2b24195b2f1834d279c63 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
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 Agopian273d0982009-05-31 19:13:00 -070021#include <utils/Log.h>
22#include <utils/threads.h>
23#include <utils/List.h>
24#include <utils/Errors.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <utils/KeyedVector.h>
Andreas Huber2db84552010-01-28 11:19:57 -080026#include <utils/String8.h>
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070027#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028
29#include <media/IMediaPlayerService.h>
30#include <media/MediaPlayerInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070031#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032
33namespace android {
34
35class IMediaRecorder;
36class IMediaMetadataRetriever;
Andreas Huber20111aa2009-07-14 16:56:47 -070037class IOMX;
Gloria Wangdac6a312009-10-29 15:46:37 -070038class MediaRecorderClient;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
40#define CALLBACK_ANTAGONIZER 0
41#if CALLBACK_ANTAGONIZER
42class Antagonizer {
43public:
44 Antagonizer(notify_callback_f cb, void* client);
45 void start() { mActive = true; }
46 void stop() { mActive = false; }
47 void kill();
48private:
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
61class MediaPlayerService : public BnMediaPlayerService
62{
63 class Client;
64
65 class AudioOutput : public MediaPlayerBase::AudioSink
66 {
67 public:
Eric Laurenta514bdb2010-06-21 09:27:30 -070068 AudioOutput(int sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069 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 Laurent342e9cf2010-01-19 17:37:09 -080079 virtual status_t getPosition(uint32_t *position);
Andreas Huber20111aa2009-07-14 16:56:47 -070080
81 virtual status_t open(
82 uint32_t sampleRate, int channelCount,
83 int format, int bufferCount,
84 AudioCallback cb, void *cookie);
85
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080086 virtual void start();
87 virtual ssize_t write(const void* buffer, size_t size);
88 virtual void stop();
89 virtual void flush();
90 virtual void pause();
91 virtual void close();
92 void setAudioStreamType(int streamType) { mStreamType = streamType; }
93 void setVolume(float left, float right);
Eric Laurent2beeb502010-07-16 07:43:46 -070094 status_t setAuxEffectSendLevel(float level);
95 status_t attachAuxEffect(int effectId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080096 virtual status_t dump(int fd, const Vector<String16>& args) const;
97
98 static bool isOnEmulator();
99 static int getMinBufferCount();
100 private:
101 static void setMinBufferCount();
Andreas Huber20111aa2009-07-14 16:56:47 -0700102 static void CallbackWrapper(
103 int event, void *me, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800104
105 AudioTrack* mTrack;
Andreas Huber20111aa2009-07-14 16:56:47 -0700106 AudioCallback mCallback;
107 void * mCallbackCookie;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800108 int mStreamType;
109 float mLeftVolume;
110 float mRightVolume;
111 float mMsecsPerFrame;
112 uint32_t mLatency;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700113 int mSessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700114 float mSendLevel;
115 int mAuxEffectId;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800116 static bool mIsOnEmulator;
117 static int mMinBufferCount; // 12 for emulator; otherwise 4
118
119 };
120
121 class AudioCache : public MediaPlayerBase::AudioSink
122 {
123 public:
124 AudioCache(const char* name);
125 virtual ~AudioCache() {}
126
127 virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
128 virtual bool realtime() const { return false; }
129 virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; }
130 virtual ssize_t frameCount() const { return mFrameCount; }
131 virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; }
132 virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); }
133 virtual uint32_t latency() const;
134 virtual float msecsPerFrame() const;
Eric Laurent342e9cf2010-01-19 17:37:09 -0800135 virtual status_t getPosition(uint32_t *position);
Andreas Huber20111aa2009-07-14 16:56:47 -0700136
137 virtual status_t open(
138 uint32_t sampleRate, int channelCount, int format,
139 int bufferCount = 1,
140 AudioCallback cb = NULL, void *cookie = NULL);
141
Andreas Huber7d5b8a72010-02-09 16:59:18 -0800142 virtual void start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800143 virtual ssize_t write(const void* buffer, size_t size);
Andreas Huber7d5b8a72010-02-09 16:59:18 -0800144 virtual void stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 virtual void flush() {}
146 virtual void pause() {}
147 virtual void close() {}
148 void setAudioStreamType(int streamType) {}
149 void setVolume(float left, float right) {}
150 uint32_t sampleRate() const { return mSampleRate; }
151 uint32_t format() const { return (uint32_t)mFormat; }
152 size_t size() const { return mSize; }
153 status_t wait();
154
155 sp<IMemoryHeap> getHeap() const { return mHeap; }
156
157 static void notify(void* cookie, int msg, int ext1, int ext2);
158 virtual status_t dump(int fd, const Vector<String16>& args) const;
159
160 private:
161 AudioCache();
162
163 Mutex mLock;
164 Condition mSignal;
165 sp<MemoryHeapBase> mHeap;
166 float mMsecsPerFrame;
167 uint16_t mChannelCount;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700168 uint16_t mFormat;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 ssize_t mFrameCount;
170 uint32_t mSampleRate;
171 uint32_t mSize;
172 int mError;
173 bool mCommandComplete;
Andreas Huber7d5b8a72010-02-09 16:59:18 -0800174
175 sp<Thread> mCallbackThread;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800176 };
177
178public:
179 static void instantiate();
180
181 // IMediaPlayerService interface
182 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid);
Gloria Wangdac6a312009-10-29 15:46:37 -0700183 void removeMediaRecorderClient(wp<MediaRecorderClient> client);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
185
186 // House keeping for media player clients
Andreas Huber2db84552010-01-28 11:19:57 -0800187 virtual sp<IMediaPlayer> create(
188 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700189 const KeyedVector<String8, String8> *headers, int audioSessionId);
Andreas Huber2db84552010-01-28 11:19:57 -0800190
Eric Laurenta514bdb2010-06-21 09:27:30 -0700191 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length, int audioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
193 virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
Andreas Huber318ad9c2009-10-15 13:46:54 -0700194 virtual sp<IOMX> getOMX();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195
196 virtual status_t dump(int fd, const Vector<String16>& args);
197
198 void removeClient(wp<Client> client);
199
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700200
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201private:
202
203 class Client : public BnMediaPlayer {
204
205 // IMediaPlayer interface
206 virtual void disconnect();
Andreas Huber5daeb122010-08-16 08:49:37 -0700207 virtual status_t setVideoISurface(const sp<ISurface>& surface);
208 virtual status_t setVideoSurface(const sp<Surface>& surface);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800209 virtual status_t prepareAsync();
210 virtual status_t start();
211 virtual status_t stop();
212 virtual status_t pause();
213 virtual status_t isPlaying(bool* state);
214 virtual status_t seekTo(int msec);
215 virtual status_t getCurrentPosition(int* msec);
216 virtual status_t getDuration(int* msec);
217 virtual status_t reset();
218 virtual status_t setAudioStreamType(int type);
219 virtual status_t setLooping(int loop);
220 virtual status_t setVolume(float leftVolume, float rightVolume);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700221 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700222 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania48290382009-07-10 13:53:06 -0700223 virtual status_t getMetadata(bool update_only,
224 bool apply_filter,
225 Parcel *reply);
Andreas Huber4e92c7e2010-02-12 12:35:58 -0800226 virtual status_t suspend();
227 virtual status_t resume();
Eric Laurent2beeb502010-07-16 07:43:46 -0700228 virtual status_t setAuxEffectSendLevel(float level);
229 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230
231 sp<MediaPlayerBase> createPlayer(player_type playerType);
Andreas Huber2db84552010-01-28 11:19:57 -0800232
233 status_t setDataSource(
234 const char *url,
235 const KeyedVector<String8, String8> *headers);
236
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800237 status_t setDataSource(int fd, int64_t offset, int64_t length);
238 static void notify(void* cookie, int msg, int ext1, int ext2);
239
240 pid_t pid() const { return mPid; }
241 virtual status_t dump(int fd, const Vector<String16>& args) const;
242
Eric Laurenta514bdb2010-06-21 09:27:30 -0700243 int getAudioSessionId() { return mAudioSessionId; }
244
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 private:
246 friend class MediaPlayerService;
247 Client( const sp<MediaPlayerService>& service,
248 pid_t pid,
249 int32_t connId,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700250 const sp<IMediaPlayerClient>& client,
251 int audioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 Client();
253 virtual ~Client();
254
255 void deletePlayer();
256
257 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
258
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700259
Nicolas Catania48290382009-07-10 13:53:06 -0700260
261 // @param type Of the metadata to be tested.
262 // @return true if the metadata should be dropped according to
263 // the filters.
nikoa64c8c72009-07-20 15:07:26 -0700264 bool shouldDropMetadata(media::Metadata::Type type) const;
Nicolas Catania48290382009-07-10 13:53:06 -0700265
266 // Add a new element to the set of metadata updated. Noop if
267 // the element exists already.
268 // @param type Of the metadata to be recorded.
nikoa64c8c72009-07-20 15:07:26 -0700269 void addNewMetadataUpdate(media::Metadata::Type type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700270
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271 mutable Mutex mLock;
272 sp<MediaPlayerBase> mPlayer;
273 sp<MediaPlayerService> mService;
274 sp<IMediaPlayerClient> mClient;
275 sp<AudioOutput> mAudioOutput;
276 pid_t mPid;
277 status_t mStatus;
278 bool mLoop;
279 int32_t mConnId;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700280 int mAudioSessionId;
Nicolas Catania48290382009-07-10 13:53:06 -0700281
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700282 // Metadata filters.
nikoa64c8c72009-07-20 15:07:26 -0700283 media::Metadata::Filter mMetadataAllow; // protected by mLock
284 media::Metadata::Filter mMetadataDrop; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700285
286 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
287 // notification we try to update mMetadataUpdated which is a
288 // set: no duplicate.
289 // getMetadata clears this set.
nikoa64c8c72009-07-20 15:07:26 -0700290 media::Metadata::Filter mMetadataUpdated; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700291
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800292#if CALLBACK_ANTAGONIZER
293 Antagonizer* mAntagonizer;
294#endif
295 };
296
297// ----------------------------------------------------------------------------
298
299 MediaPlayerService();
300 virtual ~MediaPlayerService();
301
302 mutable Mutex mLock;
303 SortedVector< wp<Client> > mClients;
Gloria Wangdac6a312009-10-29 15:46:37 -0700304 SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800305 int32_t mNextConnId;
Andreas Huber318ad9c2009-10-15 13:46:54 -0700306 sp<IOMX> mOMX;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800307};
308
309// ----------------------------------------------------------------------------
310
311}; // namespace android
312
313#endif // ANDROID_MEDIAPLAYERSERVICE_H