blob: 5539a37affeadbfc0f8260f6b9461e068f016f56 [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);
Eric Laurent8c563ed2010-10-07 18:23:03 -070080 virtual int getSessionId();
Andreas Huber20111aa2009-07-14 16:56:47 -070081
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 Project89fa4ad2009-03-03 19:31:44 -080087 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 Laurent2beeb502010-07-16 07:43:46 -070095 status_t setAuxEffectSendLevel(float level);
96 status_t attachAuxEffect(int effectId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080097 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 Huber20111aa2009-07-14 16:56:47 -0700103 static void CallbackWrapper(
104 int event, void *me, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105
106 AudioTrack* mTrack;
Andreas Huber20111aa2009-07-14 16:56:47 -0700107 AudioCallback mCallback;
108 void * mCallbackCookie;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800109 int mStreamType;
110 float mLeftVolume;
111 float mRightVolume;
112 float mMsecsPerFrame;
113 uint32_t mLatency;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700114 int mSessionId;
Eric Laurent2beeb502010-07-16 07:43:46 -0700115 float mSendLevel;
116 int mAuxEffectId;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117 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 Laurent342e9cf2010-01-19 17:37:09 -0800136 virtual status_t getPosition(uint32_t *position);
Eric Laurent8c563ed2010-10-07 18:23:03 -0700137 virtual int getSessionId();
Andreas Huber20111aa2009-07-14 16:56:47 -0700138
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 Huber7d5b8a72010-02-09 16:59:18 -0800144 virtual void start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800145 virtual ssize_t write(const void* buffer, size_t size);
Andreas Huber7d5b8a72010-02-09 16:59:18 -0800146 virtual void stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800147 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
Gloria Wangb483c472011-04-11 17:23:27 -0700159 static void notify(void* cookie, int msg,
160 int ext1, int ext2, const Parcel *obj);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800161 virtual status_t dump(int fd, const Vector<String16>& args) const;
162
163 private:
164 AudioCache();
165
166 Mutex mLock;
167 Condition mSignal;
168 sp<MemoryHeapBase> mHeap;
169 float mMsecsPerFrame;
170 uint16_t mChannelCount;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700171 uint16_t mFormat;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800172 ssize_t mFrameCount;
173 uint32_t mSampleRate;
174 uint32_t mSize;
175 int mError;
176 bool mCommandComplete;
Andreas Huber7d5b8a72010-02-09 16:59:18 -0800177
178 sp<Thread> mCallbackThread;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179 };
180
181public:
182 static void instantiate();
183
184 // IMediaPlayerService interface
185 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid);
Gloria Wangdac6a312009-10-29 15:46:37 -0700186 void removeMediaRecorderClient(wp<MediaRecorderClient> client);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
188
189 // House keeping for media player clients
Andreas Huber2db84552010-01-28 11:19:57 -0800190 virtual sp<IMediaPlayer> create(
191 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700192 const KeyedVector<String8, String8> *headers, int audioSessionId);
Andreas Huber2db84552010-01-28 11:19:57 -0800193
Eric Laurenta514bdb2010-06-21 09:27:30 -0700194 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length, int audioSessionId);
Andreas Hubere2b10282010-11-23 11:41:34 -0800195
196 virtual sp<IMediaPlayer> create(
197 pid_t pid, const sp<IMediaPlayerClient> &client,
198 const sp<IStreamSource> &source, int audioSessionId);
199
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
201 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 -0700202 virtual sp<IOMX> getOMX();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800203
204 virtual status_t dump(int fd, const Vector<String16>& args);
205
206 void removeClient(wp<Client> client);
207
Gloria Wang7cf180c2011-02-19 18:37:57 -0800208 // For battery usage tracking purpose
209 struct BatteryUsageInfo {
210 // how many streams are being played by one UID
211 int refCount;
212 // a temp variable to store the duration(ms) of audio codecs
213 // when we start a audio codec, we minus the system time from audioLastTime
214 // when we pause it, we add the system time back to the audioLastTime
215 // so after the pause, audioLastTime = pause time - start time
216 // if multiple audio streams are played (or recorded), then audioLastTime
217 // = the total playing time of all the streams
218 int32_t audioLastTime;
219 // when all the audio streams are being paused, we assign audioLastTime to
220 // this variable, so this value could be provided to the battery app
221 // in the next pullBatteryData call
222 int32_t audioTotalTime;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700223
Gloria Wang7cf180c2011-02-19 18:37:57 -0800224 int32_t videoLastTime;
225 int32_t videoTotalTime;
226 };
227 KeyedVector<int, BatteryUsageInfo> mBatteryData;
228
Gloria Wang9ee159b2011-02-24 14:51:45 -0800229 enum {
230 SPEAKER,
231 OTHER_AUDIO_DEVICE,
232 SPEAKER_AND_OTHER,
233 NUM_AUDIO_DEVICES
234 };
235
236 struct BatteryAudioFlingerUsageInfo {
237 int refCount; // how many audio streams are being played
238 int deviceOn[NUM_AUDIO_DEVICES]; // whether the device is currently used
239 int32_t lastTime[NUM_AUDIO_DEVICES]; // in ms
240 // totalTime[]: total time of audio output devices usage
241 int32_t totalTime[NUM_AUDIO_DEVICES]; // in ms
242 };
243
244 // This varialble is used to record the usage of audio output device
245 // for battery app
246 BatteryAudioFlingerUsageInfo mBatteryAudio;
247
Gloria Wang7cf180c2011-02-19 18:37:57 -0800248 // Collect info of the codec usage from media player and media recorder
249 virtual void addBatteryData(uint32_t params);
250 // API for the Battery app to pull the data of codecs usage
251 virtual status_t pullBatteryData(Parcel* reply);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252private:
253
254 class Client : public BnMediaPlayer {
255
256 // IMediaPlayer interface
257 virtual void disconnect();
Andreas Huber5daeb122010-08-16 08:49:37 -0700258 virtual status_t setVideoSurface(const sp<Surface>& surface);
Glenn Kasten11731182011-02-08 17:26:17 -0800259 virtual status_t setVideoSurfaceTexture(
260 const sp<ISurfaceTexture>& surfaceTexture);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 virtual status_t prepareAsync();
262 virtual status_t start();
263 virtual status_t stop();
264 virtual status_t pause();
265 virtual status_t isPlaying(bool* state);
266 virtual status_t seekTo(int msec);
267 virtual status_t getCurrentPosition(int* msec);
268 virtual status_t getDuration(int* msec);
269 virtual status_t reset();
270 virtual status_t setAudioStreamType(int type);
271 virtual status_t setLooping(int loop);
272 virtual status_t setVolume(float leftVolume, float rightVolume);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700273 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700274 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania48290382009-07-10 13:53:06 -0700275 virtual status_t getMetadata(bool update_only,
276 bool apply_filter,
277 Parcel *reply);
Eric Laurent2beeb502010-07-16 07:43:46 -0700278 virtual status_t setAuxEffectSendLevel(float level);
279 virtual status_t attachAuxEffect(int effectId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800280
281 sp<MediaPlayerBase> createPlayer(player_type playerType);
Andreas Huber2db84552010-01-28 11:19:57 -0800282
283 status_t setDataSource(
284 const char *url,
285 const KeyedVector<String8, String8> *headers);
286
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287 status_t setDataSource(int fd, int64_t offset, int64_t length);
Andreas Hubere2b10282010-11-23 11:41:34 -0800288
289 status_t setDataSource(const sp<IStreamSource> &source);
290
Gloria Wangb483c472011-04-11 17:23:27 -0700291 static void notify(void* cookie, int msg,
292 int ext1, int ext2, const Parcel *obj);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800293
294 pid_t pid() const { return mPid; }
295 virtual status_t dump(int fd, const Vector<String16>& args) const;
296
Eric Laurenta514bdb2010-06-21 09:27:30 -0700297 int getAudioSessionId() { return mAudioSessionId; }
298
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299 private:
300 friend class MediaPlayerService;
301 Client( const sp<MediaPlayerService>& service,
302 pid_t pid,
303 int32_t connId,
Eric Laurenta514bdb2010-06-21 09:27:30 -0700304 const sp<IMediaPlayerClient>& client,
305 int audioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800306 Client();
307 virtual ~Client();
308
309 void deletePlayer();
310
311 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
312
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700313
Nicolas Catania48290382009-07-10 13:53:06 -0700314
315 // @param type Of the metadata to be tested.
316 // @return true if the metadata should be dropped according to
317 // the filters.
nikoa64c8c72009-07-20 15:07:26 -0700318 bool shouldDropMetadata(media::Metadata::Type type) const;
Nicolas Catania48290382009-07-10 13:53:06 -0700319
320 // Add a new element to the set of metadata updated. Noop if
321 // the element exists already.
322 // @param type Of the metadata to be recorded.
nikoa64c8c72009-07-20 15:07:26 -0700323 void addNewMetadataUpdate(media::Metadata::Type type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700324
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 mutable Mutex mLock;
326 sp<MediaPlayerBase> mPlayer;
327 sp<MediaPlayerService> mService;
328 sp<IMediaPlayerClient> mClient;
329 sp<AudioOutput> mAudioOutput;
330 pid_t mPid;
331 status_t mStatus;
332 bool mLoop;
333 int32_t mConnId;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700334 int mAudioSessionId;
Nicolas Catania48290382009-07-10 13:53:06 -0700335
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700336 // Metadata filters.
nikoa64c8c72009-07-20 15:07:26 -0700337 media::Metadata::Filter mMetadataAllow; // protected by mLock
338 media::Metadata::Filter mMetadataDrop; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700339
340 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
341 // notification we try to update mMetadataUpdated which is a
342 // set: no duplicate.
343 // getMetadata clears this set.
nikoa64c8c72009-07-20 15:07:26 -0700344 media::Metadata::Filter mMetadataUpdated; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700345
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346#if CALLBACK_ANTAGONIZER
347 Antagonizer* mAntagonizer;
348#endif
349 };
350
351// ----------------------------------------------------------------------------
352
353 MediaPlayerService();
354 virtual ~MediaPlayerService();
355
356 mutable Mutex mLock;
357 SortedVector< wp<Client> > mClients;
Gloria Wangdac6a312009-10-29 15:46:37 -0700358 SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800359 int32_t mNextConnId;
Andreas Huber318ad9c2009-10-15 13:46:54 -0700360 sp<IOMX> mOMX;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361};
362
363// ----------------------------------------------------------------------------
364
365}; // namespace android
366
367#endif // ANDROID_MEDIAPLAYERSERVICE_H