blob: a4be4142fb2ea0871748c47b1e03fca8f7f8b51c [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>
Nicolas Cataniab2c69392009-07-08 08:57:42 -070026#include <utils/Vector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <ui/SurfaceComposerClient.h>
28
29#include <media/IMediaPlayerService.h>
30#include <media/MediaPlayerInterface.h>
nikobc726922009-07-20 15:07:26 -070031#include <media/Metadata.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
33namespace android {
34
35class IMediaRecorder;
36class IMediaMetadataRetriever;
Andreas Hubere46b7be2009-07-14 16:56:47 -070037class IOMX;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39#define CALLBACK_ANTAGONIZER 0
40#if CALLBACK_ANTAGONIZER
41class Antagonizer {
42public:
43 Antagonizer(notify_callback_f cb, void* client);
44 void start() { mActive = true; }
45 void stop() { mActive = false; }
46 void kill();
47private:
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
60class 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 Hubere46b7be2009-07-14 16:56:47 -070078
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 Project9066cfe2009-03-03 19:31:44 -080084 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 Hubere46b7be2009-07-14 16:56:47 -070098 static void CallbackWrapper(
99 int event, void *me, void *info);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101 AudioTrack* mTrack;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700102 AudioCallback mCallback;
103 void * mCallbackCookie;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 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 Hubere46b7be2009-07-14 16:56:47 -0700131
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 Project9066cfe2009-03-03 19:31:44 -0800137 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 Cataniab2c69392009-07-08 08:57:42 -0700163 uint16_t mFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 ssize_t mFrameCount;
165 uint32_t mSampleRate;
166 uint32_t mSize;
167 int mError;
168 bool mCommandComplete;
169 };
170
171public:
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 Hubere46b7be2009-07-14 16:56:47 -0700183 virtual sp<IOMX> createOMX();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184
185 virtual status_t dump(int fd, const Vector<String16>& args);
186
187 void removeClient(wp<Client> client);
188
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190private:
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 Catania20cb94e2009-05-12 23:25:55 -0700209 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700210 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700211 virtual status_t getMetadata(bool update_only,
212 bool apply_filter,
213 Parcel *reply);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214
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 Cataniab2c69392009-07-08 08:57:42 -0700236
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700237
238 // @param type Of the metadata to be tested.
239 // @return true if the metadata should be dropped according to
240 // the filters.
nikobc726922009-07-20 15:07:26 -0700241 bool shouldDropMetadata(media::Metadata::Type type) const;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700242
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.
nikobc726922009-07-20 15:07:26 -0700246 void addNewMetadataUpdate(media::Metadata::Type type);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 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 Catania4df8b2c2009-07-10 13:53:06 -0700257
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700258 // Metadata filters.
nikobc726922009-07-20 15:07:26 -0700259 media::Metadata::Filter mMetadataAllow; // protected by mLock
260 media::Metadata::Filter mMetadataDrop; // protected by mLock
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700261
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.
nikobc726922009-07-20 15:07:26 -0700266 media::Metadata::Filter mMetadataUpdated; // protected by mLock
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268#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