blob: d243b9638f55b09ded9ac70d63e13adf77f64134 [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 Cataniaa7e0e8b2009-07-08 08:57:42 -070026#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <ui/SurfaceComposerClient.h>
28
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:
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;
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);
94 virtual status_t dump(int fd, const Vector<String16>& args) const;
95
96 static bool isOnEmulator();
97 static int getMinBufferCount();
98 private:
99 static void setMinBufferCount();
Andreas Huber20111aa2009-07-14 16:56:47 -0700100 static void CallbackWrapper(
101 int event, void *me, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800102
103 AudioTrack* mTrack;
Andreas Huber20111aa2009-07-14 16:56:47 -0700104 AudioCallback mCallback;
105 void * mCallbackCookie;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800106 int mStreamType;
107 float mLeftVolume;
108 float mRightVolume;
109 float mMsecsPerFrame;
110 uint32_t mLatency;
111
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800112 static bool mIsOnEmulator;
113 static int mMinBufferCount; // 12 for emulator; otherwise 4
114
Marco Nelissen10dbb8e2009-09-20 10:42:13 -0700115 public: // visualization hack support
116 uint32_t mNumFramesWritten;
Marco Nelissen7ee8ac92010-01-12 09:23:54 -0800117 void snoopWrite(const void*, size_t);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800118 };
119
120 class AudioCache : public MediaPlayerBase::AudioSink
121 {
122 public:
123 AudioCache(const char* name);
124 virtual ~AudioCache() {}
125
126 virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
127 virtual bool realtime() const { return false; }
128 virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; }
129 virtual ssize_t frameCount() const { return mFrameCount; }
130 virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; }
131 virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); }
132 virtual uint32_t latency() const;
133 virtual float msecsPerFrame() const;
Eric Laurent342e9cf2010-01-19 17:37:09 -0800134 virtual status_t getPosition(uint32_t *position);
Andreas Huber20111aa2009-07-14 16:56:47 -0700135
136 virtual status_t open(
137 uint32_t sampleRate, int channelCount, int format,
138 int bufferCount = 1,
139 AudioCallback cb = NULL, void *cookie = NULL);
140
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800141 virtual void start() {}
142 virtual ssize_t write(const void* buffer, size_t size);
143 virtual void stop() {}
144 virtual void flush() {}
145 virtual void pause() {}
146 virtual void close() {}
147 void setAudioStreamType(int streamType) {}
148 void setVolume(float left, float right) {}
149 uint32_t sampleRate() const { return mSampleRate; }
150 uint32_t format() const { return (uint32_t)mFormat; }
151 size_t size() const { return mSize; }
152 status_t wait();
153
154 sp<IMemoryHeap> getHeap() const { return mHeap; }
155
156 static void notify(void* cookie, int msg, int ext1, int ext2);
157 virtual status_t dump(int fd, const Vector<String16>& args) const;
158
159 private:
160 AudioCache();
161
162 Mutex mLock;
163 Condition mSignal;
164 sp<MemoryHeapBase> mHeap;
165 float mMsecsPerFrame;
166 uint16_t mChannelCount;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700167 uint16_t mFormat;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800168 ssize_t mFrameCount;
169 uint32_t mSampleRate;
170 uint32_t mSize;
171 int mError;
172 bool mCommandComplete;
173 };
174
175public:
176 static void instantiate();
177
178 // IMediaPlayerService interface
179 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid);
Gloria Wangdac6a312009-10-29 15:46:37 -0700180 void removeMediaRecorderClient(wp<MediaRecorderClient> client);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
182
183 // House keeping for media player clients
184 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url);
185 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length);
186 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
187 virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -0700188 virtual sp<IMemory> snoop();
Andreas Huber318ad9c2009-10-15 13:46:54 -0700189 virtual sp<IOMX> getOMX();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800190
191 virtual status_t dump(int fd, const Vector<String16>& args);
192
193 void removeClient(wp<Client> client);
194
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700195
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196private:
197
198 class Client : public BnMediaPlayer {
199
200 // IMediaPlayer interface
201 virtual void disconnect();
202 virtual status_t setVideoSurface(const sp<ISurface>& surface);
203 virtual status_t prepareAsync();
204 virtual status_t start();
205 virtual status_t stop();
206 virtual status_t pause();
207 virtual status_t isPlaying(bool* state);
208 virtual status_t seekTo(int msec);
209 virtual status_t getCurrentPosition(int* msec);
210 virtual status_t getDuration(int* msec);
211 virtual status_t reset();
212 virtual status_t setAudioStreamType(int type);
213 virtual status_t setLooping(int loop);
214 virtual status_t setVolume(float leftVolume, float rightVolume);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700215 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700216 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania48290382009-07-10 13:53:06 -0700217 virtual status_t getMetadata(bool update_only,
218 bool apply_filter,
219 Parcel *reply);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220
221 sp<MediaPlayerBase> createPlayer(player_type playerType);
222 status_t setDataSource(const char *url);
223 status_t setDataSource(int fd, int64_t offset, int64_t length);
224 static void notify(void* cookie, int msg, int ext1, int ext2);
225
226 pid_t pid() const { return mPid; }
227 virtual status_t dump(int fd, const Vector<String16>& args) const;
228
229 private:
230 friend class MediaPlayerService;
231 Client( const sp<MediaPlayerService>& service,
232 pid_t pid,
233 int32_t connId,
234 const sp<IMediaPlayerClient>& client);
235 Client();
236 virtual ~Client();
237
238 void deletePlayer();
239
240 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
241
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700242
Nicolas Catania48290382009-07-10 13:53:06 -0700243
244 // @param type Of the metadata to be tested.
245 // @return true if the metadata should be dropped according to
246 // the filters.
nikoa64c8c72009-07-20 15:07:26 -0700247 bool shouldDropMetadata(media::Metadata::Type type) const;
Nicolas Catania48290382009-07-10 13:53:06 -0700248
249 // Add a new element to the set of metadata updated. Noop if
250 // the element exists already.
251 // @param type Of the metadata to be recorded.
nikoa64c8c72009-07-20 15:07:26 -0700252 void addNewMetadataUpdate(media::Metadata::Type type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700253
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254 mutable Mutex mLock;
255 sp<MediaPlayerBase> mPlayer;
256 sp<MediaPlayerService> mService;
257 sp<IMediaPlayerClient> mClient;
258 sp<AudioOutput> mAudioOutput;
259 pid_t mPid;
260 status_t mStatus;
261 bool mLoop;
262 int32_t mConnId;
Nicolas Catania48290382009-07-10 13:53:06 -0700263
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700264 // Metadata filters.
nikoa64c8c72009-07-20 15:07:26 -0700265 media::Metadata::Filter mMetadataAllow; // protected by mLock
266 media::Metadata::Filter mMetadataDrop; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700267
268 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
269 // notification we try to update mMetadataUpdated which is a
270 // set: no duplicate.
271 // getMetadata clears this set.
nikoa64c8c72009-07-20 15:07:26 -0700272 media::Metadata::Filter mMetadataUpdated; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700273
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274#if CALLBACK_ANTAGONIZER
275 Antagonizer* mAntagonizer;
276#endif
277 };
278
279// ----------------------------------------------------------------------------
280
281 MediaPlayerService();
282 virtual ~MediaPlayerService();
283
284 mutable Mutex mLock;
285 SortedVector< wp<Client> > mClients;
Gloria Wangdac6a312009-10-29 15:46:37 -0700286 SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800287 int32_t mNextConnId;
Andreas Huber318ad9c2009-10-15 13:46:54 -0700288 sp<IOMX> mOMX;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800289};
290
291// ----------------------------------------------------------------------------
292
293}; // namespace android
294
295#endif // ANDROID_MEDIAPLAYERSERVICE_H