blob: 5c03e47687628481b6d3e7b9cec820ee75d31a9a [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Agopian3b4062e2009-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 Project9066cfe2009-03-03 19:31:44 -080025#include <utils/KeyedVector.h>
Andreas Huber25643002010-01-28 11:19:57 -080026#include <utils/String8.h>
Nicolas Cataniab2c69392009-07-08 08:57:42 -070027#include <utils/Vector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <ui/SurfaceComposerClient.h>
29
30#include <media/IMediaPlayerService.h>
31#include <media/MediaPlayerInterface.h>
nikobc726922009-07-20 15:07:26 -070032#include <media/Metadata.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
34namespace android {
35
36class IMediaRecorder;
37class IMediaMetadataRetriever;
Andreas Hubere46b7be2009-07-14 16:56:47 -070038class IOMX;
Gloria Wang608a2632009-10-29 15:46:37 -070039class MediaRecorderClient;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41#define CALLBACK_ANTAGONIZER 0
42#if CALLBACK_ANTAGONIZER
43class Antagonizer {
44public:
45 Antagonizer(notify_callback_f cb, void* client);
46 void start() { mActive = true; }
47 void stop() { mActive = false; }
48 void kill();
49private:
50 static const int interval;
51 Antagonizer();
52 static int callbackThread(void* cookie);
53 Mutex mLock;
54 Condition mCondition;
55 bool mExit;
56 bool mActive;
57 void* mClient;
58 notify_callback_f mCb;
59};
60#endif
61
62class MediaPlayerService : public BnMediaPlayerService
63{
64 class Client;
65
66 class AudioOutput : public MediaPlayerBase::AudioSink
67 {
68 public:
69 AudioOutput();
70 virtual ~AudioOutput();
71
72 virtual bool ready() const { return mTrack != NULL; }
73 virtual bool realtime() const { return true; }
74 virtual ssize_t bufferSize() const;
75 virtual ssize_t frameCount() const;
76 virtual ssize_t channelCount() const;
77 virtual ssize_t frameSize() const;
78 virtual uint32_t latency() const;
79 virtual float msecsPerFrame() const;
Eric Laurent0986e792010-01-19 17:37:09 -080080 virtual status_t getPosition(uint32_t *position);
Andreas Hubere46b7be2009-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 Project9066cfe2009-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);
95 virtual status_t dump(int fd, const Vector<String16>& args) const;
96
97 static bool isOnEmulator();
98 static int getMinBufferCount();
99 private:
100 static void setMinBufferCount();
Andreas Hubere46b7be2009-07-14 16:56:47 -0700101 static void CallbackWrapper(
102 int event, void *me, void *info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
104 AudioTrack* mTrack;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700105 AudioCallback mCallback;
106 void * mCallbackCookie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 int mStreamType;
108 float mLeftVolume;
109 float mRightVolume;
110 float mMsecsPerFrame;
111 uint32_t mLatency;
112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 static bool mIsOnEmulator;
114 static int mMinBufferCount; // 12 for emulator; otherwise 4
115
Marco Nelissenc39d2e32009-09-20 10:42:13 -0700116 public: // visualization hack support
117 uint32_t mNumFramesWritten;
Marco Nelissene274db12010-01-12 09:23:54 -0800118 void snoopWrite(const void*, size_t);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 };
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 Laurent0986e792010-01-19 17:37:09 -0800135 virtual status_t getPosition(uint32_t *position);
Andreas Hubere46b7be2009-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 Huber6ed937e2010-02-09 16:59:18 -0800142 virtual void start();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 virtual ssize_t write(const void* buffer, size_t size);
Andreas Huber6ed937e2010-02-09 16:59:18 -0800144 virtual void stop();
The Android Open Source Project9066cfe2009-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 Cataniab2c69392009-07-08 08:57:42 -0700168 uint16_t mFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 ssize_t mFrameCount;
170 uint32_t mSampleRate;
171 uint32_t mSize;
172 int mError;
173 bool mCommandComplete;
Andreas Huber6ed937e2010-02-09 16:59:18 -0800174
175 sp<Thread> mCallbackThread;
The Android Open Source Project9066cfe2009-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 Wang608a2632009-10-29 15:46:37 -0700183 void removeMediaRecorderClient(wp<MediaRecorderClient> client);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
185
186 // House keeping for media player clients
Andreas Huber25643002010-01-28 11:19:57 -0800187 virtual sp<IMediaPlayer> create(
188 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
189 const KeyedVector<String8, String8> *headers);
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length);
192 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);
Marco Nelissenc39d2e32009-09-20 10:42:13 -0700194 virtual sp<IMemory> snoop();
Andreas Huber784202e2009-10-15 13:46:54 -0700195 virtual sp<IOMX> getOMX();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
197 virtual status_t dump(int fd, const Vector<String16>& args);
198
199 void removeClient(wp<Client> client);
200
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202private:
203
204 class Client : public BnMediaPlayer {
205
206 // IMediaPlayer interface
207 virtual void disconnect();
208 virtual status_t setVideoSurface(const sp<ISurface>& surface);
209 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 Catania20cb94e2009-05-12 23:25:55 -0700221 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700222 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700223 virtual status_t getMetadata(bool update_only,
224 bool apply_filter,
225 Parcel *reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
227 sp<MediaPlayerBase> createPlayer(player_type playerType);
Andreas Huber25643002010-01-28 11:19:57 -0800228
229 status_t setDataSource(
230 const char *url,
231 const KeyedVector<String8, String8> *headers);
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 status_t setDataSource(int fd, int64_t offset, int64_t length);
234 static void notify(void* cookie, int msg, int ext1, int ext2);
235
236 pid_t pid() const { return mPid; }
237 virtual status_t dump(int fd, const Vector<String16>& args) const;
238
239 private:
240 friend class MediaPlayerService;
241 Client( const sp<MediaPlayerService>& service,
242 pid_t pid,
243 int32_t connId,
244 const sp<IMediaPlayerClient>& client);
245 Client();
246 virtual ~Client();
247
248 void deletePlayer();
249
250 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
251
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700252
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700253
254 // @param type Of the metadata to be tested.
255 // @return true if the metadata should be dropped according to
256 // the filters.
nikobc726922009-07-20 15:07:26 -0700257 bool shouldDropMetadata(media::Metadata::Type type) const;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700258
259 // Add a new element to the set of metadata updated. Noop if
260 // the element exists already.
261 // @param type Of the metadata to be recorded.
nikobc726922009-07-20 15:07:26 -0700262 void addNewMetadataUpdate(media::Metadata::Type type);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 mutable Mutex mLock;
265 sp<MediaPlayerBase> mPlayer;
266 sp<MediaPlayerService> mService;
267 sp<IMediaPlayerClient> mClient;
268 sp<AudioOutput> mAudioOutput;
269 pid_t mPid;
270 status_t mStatus;
271 bool mLoop;
272 int32_t mConnId;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700273
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700274 // Metadata filters.
nikobc726922009-07-20 15:07:26 -0700275 media::Metadata::Filter mMetadataAllow; // protected by mLock
276 media::Metadata::Filter mMetadataDrop; // protected by mLock
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700277
278 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
279 // notification we try to update mMetadataUpdated which is a
280 // set: no duplicate.
281 // getMetadata clears this set.
nikobc726922009-07-20 15:07:26 -0700282 media::Metadata::Filter mMetadataUpdated; // protected by mLock
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284#if CALLBACK_ANTAGONIZER
285 Antagonizer* mAntagonizer;
286#endif
287 };
288
289// ----------------------------------------------------------------------------
290
291 MediaPlayerService();
292 virtual ~MediaPlayerService();
293
294 mutable Mutex mLock;
295 SortedVector< wp<Client> > mClients;
Gloria Wang608a2632009-10-29 15:46:37 -0700296 SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 int32_t mNextConnId;
Andreas Huber784202e2009-10-15 13:46:54 -0700298 sp<IOMX> mOMX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299};
300
301// ----------------------------------------------------------------------------
302
303}; // namespace android
304
305#endif // ANDROID_MEDIAPLAYERSERVICE_H