blob: 94cb9178a3ec8e61f8013096c25160eb6ecc6a08 [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>
Nicolas Catania48290382009-07-10 13:53:06 -070026#include <utils/SortedVector.h>
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070027#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028#include <ui/SurfaceComposerClient.h>
29
30#include <media/IMediaPlayerService.h>
31#include <media/MediaPlayerInterface.h>
32
33namespace android {
Nicolas Catania48290382009-07-10 13:53:06 -070034typedef int32_t MetadataType;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035
36class IMediaRecorder;
37class IMediaMetadataRetriever;
Andreas Huber20111aa2009-07-14 16:56:47 -070038class IOMX;
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:
68 AudioOutput();
69 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;
Andreas Huber20111aa2009-07-14 16:56:47 -070079
80 virtual status_t open(
81 uint32_t sampleRate, int channelCount,
82 int format, int bufferCount,
83 AudioCallback cb, void *cookie);
84
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080085 virtual void start();
86 virtual ssize_t write(const void* buffer, size_t size);
87 virtual void stop();
88 virtual void flush();
89 virtual void pause();
90 virtual void close();
91 void setAudioStreamType(int streamType) { mStreamType = streamType; }
92 void setVolume(float left, float right);
93 virtual status_t dump(int fd, const Vector<String16>& args) const;
94
95 static bool isOnEmulator();
96 static int getMinBufferCount();
97 private:
98 static void setMinBufferCount();
Andreas Huber20111aa2009-07-14 16:56:47 -070099 static void CallbackWrapper(
100 int event, void *me, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800101
102 AudioTrack* mTrack;
Andreas Huber20111aa2009-07-14 16:56:47 -0700103 AudioCallback mCallback;
104 void * mCallbackCookie;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800105 int mStreamType;
106 float mLeftVolume;
107 float mRightVolume;
108 float mMsecsPerFrame;
109 uint32_t mLatency;
110
111 // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
112 static const uint32_t kAudioVideoDelayMs;
113 static bool mIsOnEmulator;
114 static int mMinBufferCount; // 12 for emulator; otherwise 4
115
116 };
117
118 class AudioCache : public MediaPlayerBase::AudioSink
119 {
120 public:
121 AudioCache(const char* name);
122 virtual ~AudioCache() {}
123
124 virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
125 virtual bool realtime() const { return false; }
126 virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; }
127 virtual ssize_t frameCount() const { return mFrameCount; }
128 virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; }
129 virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); }
130 virtual uint32_t latency() const;
131 virtual float msecsPerFrame() const;
Andreas Huber20111aa2009-07-14 16:56:47 -0700132
133 virtual status_t open(
134 uint32_t sampleRate, int channelCount, int format,
135 int bufferCount = 1,
136 AudioCallback cb = NULL, void *cookie = NULL);
137
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800138 virtual void start() {}
139 virtual ssize_t write(const void* buffer, size_t size);
140 virtual void stop() {}
141 virtual void flush() {}
142 virtual void pause() {}
143 virtual void close() {}
144 void setAudioStreamType(int streamType) {}
145 void setVolume(float left, float right) {}
146 uint32_t sampleRate() const { return mSampleRate; }
147 uint32_t format() const { return (uint32_t)mFormat; }
148 size_t size() const { return mSize; }
149 status_t wait();
150
151 sp<IMemoryHeap> getHeap() const { return mHeap; }
152
153 static void notify(void* cookie, int msg, int ext1, int ext2);
154 virtual status_t dump(int fd, const Vector<String16>& args) const;
155
156 private:
157 AudioCache();
158
159 Mutex mLock;
160 Condition mSignal;
161 sp<MemoryHeapBase> mHeap;
162 float mMsecsPerFrame;
163 uint16_t mChannelCount;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700164 uint16_t mFormat;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800165 ssize_t mFrameCount;
166 uint32_t mSampleRate;
167 uint32_t mSize;
168 int mError;
169 bool mCommandComplete;
170 };
171
172public:
173 static void instantiate();
174
175 // IMediaPlayerService interface
176 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid);
177 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
178
179 // House keeping for media player clients
180 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url);
181 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length);
182 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
183 virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
Andreas Huber20111aa2009-07-14 16:56:47 -0700184 virtual sp<IOMX> createOMX();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800185
186 virtual status_t dump(int fd, const Vector<String16>& args);
187
188 void removeClient(wp<Client> client);
189
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700190
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800191private:
192
193 class Client : public BnMediaPlayer {
194
195 // IMediaPlayer interface
196 virtual void disconnect();
197 virtual status_t setVideoSurface(const sp<ISurface>& surface);
198 virtual status_t prepareAsync();
199 virtual status_t start();
200 virtual status_t stop();
201 virtual status_t pause();
202 virtual status_t isPlaying(bool* state);
203 virtual status_t seekTo(int msec);
204 virtual status_t getCurrentPosition(int* msec);
205 virtual status_t getDuration(int* msec);
206 virtual status_t reset();
207 virtual status_t setAudioStreamType(int type);
208 virtual status_t setLooping(int loop);
209 virtual status_t setVolume(float leftVolume, float rightVolume);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700210 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700211 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania48290382009-07-10 13:53:06 -0700212 virtual status_t getMetadata(bool update_only,
213 bool apply_filter,
214 Parcel *reply);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800215
216 sp<MediaPlayerBase> createPlayer(player_type playerType);
217 status_t setDataSource(const char *url);
218 status_t setDataSource(int fd, int64_t offset, int64_t length);
219 static void notify(void* cookie, int msg, int ext1, int ext2);
220
221 pid_t pid() const { return mPid; }
222 virtual status_t dump(int fd, const Vector<String16>& args) const;
223
224 private:
225 friend class MediaPlayerService;
226 Client( const sp<MediaPlayerService>& service,
227 pid_t pid,
228 int32_t connId,
229 const sp<IMediaPlayerClient>& client);
230 Client();
231 virtual ~Client();
232
233 void deletePlayer();
234
235 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
236
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700237
Nicolas Catania48290382009-07-10 13:53:06 -0700238
239 // @param type Of the metadata to be tested.
240 // @return true if the metadata should be dropped according to
241 // the filters.
242 bool shouldDropMetadata(MetadataType type) const;
243
244 // Add a new element to the set of metadata updated. Noop if
245 // the element exists already.
246 // @param type Of the metadata to be recorded.
247 void addNewMetadataUpdate(MetadataType type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700248
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249 mutable Mutex mLock;
250 sp<MediaPlayerBase> mPlayer;
251 sp<MediaPlayerService> mService;
252 sp<IMediaPlayerClient> mClient;
253 sp<AudioOutput> mAudioOutput;
254 pid_t mPid;
255 status_t mStatus;
256 bool mLoop;
257 int32_t mConnId;
Nicolas Catania48290382009-07-10 13:53:06 -0700258
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700259 // Metadata filters.
Nicolas Catania48290382009-07-10 13:53:06 -0700260 SortedVector<int32_t> mMetadataAllow; // protected by mLock
261 SortedVector<int32_t> mMetadataDrop; // protected by mLock
262
263 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
264 // notification we try to update mMetadataUpdated which is a
265 // set: no duplicate.
266 // getMetadata clears this set.
267 SortedVector<int32_t> mMetadataUpdated; // protected by mLock
268
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269#if CALLBACK_ANTAGONIZER
270 Antagonizer* mAntagonizer;
271#endif
272 };
273
274// ----------------------------------------------------------------------------
275
276 MediaPlayerService();
277 virtual ~MediaPlayerService();
278
279 mutable Mutex mLock;
280 SortedVector< wp<Client> > mClients;
281 int32_t mNextConnId;
282};
283
284// ----------------------------------------------------------------------------
285
286}; // namespace android
287
288#endif // ANDROID_MEDIAPLAYERSERVICE_H