blob: ffe1ba0f2ec11ec7b2a2cc7633615b086d1bc616 [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#include <ui/SurfaceComposerClient.h>
29
30#include <media/IMediaPlayerService.h>
31#include <media/MediaPlayerInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070032#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080033
34namespace android {
35
36class IMediaRecorder;
37class IMediaMetadataRetriever;
Andreas Huber20111aa2009-07-14 16:56:47 -070038class IOMX;
Gloria Wangdac6a312009-10-29 15:46:37 -070039class MediaRecorderClient;
The Android Open Source Project89fa4ad2009-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 Laurent342e9cf2010-01-19 17:37:09 -080080 virtual status_t getPosition(uint32_t *position);
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);
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 Huber20111aa2009-07-14 16:56:47 -0700101 static void CallbackWrapper(
102 int event, void *me, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800103
104 AudioTrack* mTrack;
Andreas Huber20111aa2009-07-14 16:56:47 -0700105 AudioCallback mCallback;
106 void * mCallbackCookie;
The Android Open Source Project89fa4ad2009-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 Project89fa4ad2009-03-03 19:31:44 -0800113 static bool mIsOnEmulator;
114 static int mMinBufferCount; // 12 for emulator; otherwise 4
115
Marco Nelissen10dbb8e2009-09-20 10:42:13 -0700116 public: // visualization hack support
117 uint32_t mNumFramesWritten;
Marco Nelissen7ee8ac92010-01-12 09:23:54 -0800118 void snoopWrite(const void*, size_t);
The Android Open Source Project89fa4ad2009-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 Laurent342e9cf2010-01-19 17:37:09 -0800135 virtual status_t getPosition(uint32_t *position);
Andreas Huber20111aa2009-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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800142 virtual void start() {}
143 virtual ssize_t write(const void* buffer, size_t size);
144 virtual void stop() {}
145 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 Cataniaa7e0e8b2009-07-08 08:57:42 -0700168 uint16_t mFormat;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800169 ssize_t mFrameCount;
170 uint32_t mSampleRate;
171 uint32_t mSize;
172 int mError;
173 bool mCommandComplete;
174 };
175
176public:
177 static void instantiate();
178
179 // IMediaPlayerService interface
180 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid);
Gloria Wangdac6a312009-10-29 15:46:37 -0700181 void removeMediaRecorderClient(wp<MediaRecorderClient> client);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800182 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
183
184 // House keeping for media player clients
Andreas Huber2db84552010-01-28 11:19:57 -0800185 virtual sp<IMediaPlayer> create(
186 pid_t pid, const sp<IMediaPlayerClient>& client, const char* url,
187 const KeyedVector<String8, String8> *headers);
188
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length);
190 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
191 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 -0700192 virtual sp<IMemory> snoop();
Andreas Huber318ad9c2009-10-15 13:46:54 -0700193 virtual sp<IOMX> getOMX();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800194
195 virtual status_t dump(int fd, const Vector<String16>& args);
196
197 void removeClient(wp<Client> client);
198
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700199
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200private:
201
202 class Client : public BnMediaPlayer {
203
204 // IMediaPlayer interface
205 virtual void disconnect();
206 virtual status_t setVideoSurface(const sp<ISurface>& surface);
207 virtual status_t prepareAsync();
208 virtual status_t start();
209 virtual status_t stop();
210 virtual status_t pause();
211 virtual status_t isPlaying(bool* state);
212 virtual status_t seekTo(int msec);
213 virtual status_t getCurrentPosition(int* msec);
214 virtual status_t getDuration(int* msec);
215 virtual status_t reset();
216 virtual status_t setAudioStreamType(int type);
217 virtual status_t setLooping(int loop);
218 virtual status_t setVolume(float leftVolume, float rightVolume);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700219 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700220 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania48290382009-07-10 13:53:06 -0700221 virtual status_t getMetadata(bool update_only,
222 bool apply_filter,
223 Parcel *reply);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224
225 sp<MediaPlayerBase> createPlayer(player_type playerType);
Andreas Huber2db84552010-01-28 11:19:57 -0800226
227 status_t setDataSource(
228 const char *url,
229 const KeyedVector<String8, String8> *headers);
230
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231 status_t setDataSource(int fd, int64_t offset, int64_t length);
232 static void notify(void* cookie, int msg, int ext1, int ext2);
233
234 pid_t pid() const { return mPid; }
235 virtual status_t dump(int fd, const Vector<String16>& args) const;
236
237 private:
238 friend class MediaPlayerService;
239 Client( const sp<MediaPlayerService>& service,
240 pid_t pid,
241 int32_t connId,
242 const sp<IMediaPlayerClient>& client);
243 Client();
244 virtual ~Client();
245
246 void deletePlayer();
247
248 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
249
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700250
Nicolas Catania48290382009-07-10 13:53:06 -0700251
252 // @param type Of the metadata to be tested.
253 // @return true if the metadata should be dropped according to
254 // the filters.
nikoa64c8c72009-07-20 15:07:26 -0700255 bool shouldDropMetadata(media::Metadata::Type type) const;
Nicolas Catania48290382009-07-10 13:53:06 -0700256
257 // Add a new element to the set of metadata updated. Noop if
258 // the element exists already.
259 // @param type Of the metadata to be recorded.
nikoa64c8c72009-07-20 15:07:26 -0700260 void addNewMetadataUpdate(media::Metadata::Type type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700261
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 mutable Mutex mLock;
263 sp<MediaPlayerBase> mPlayer;
264 sp<MediaPlayerService> mService;
265 sp<IMediaPlayerClient> mClient;
266 sp<AudioOutput> mAudioOutput;
267 pid_t mPid;
268 status_t mStatus;
269 bool mLoop;
270 int32_t mConnId;
Nicolas Catania48290382009-07-10 13:53:06 -0700271
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700272 // Metadata filters.
nikoa64c8c72009-07-20 15:07:26 -0700273 media::Metadata::Filter mMetadataAllow; // protected by mLock
274 media::Metadata::Filter mMetadataDrop; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700275
276 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
277 // notification we try to update mMetadataUpdated which is a
278 // set: no duplicate.
279 // getMetadata clears this set.
nikoa64c8c72009-07-20 15:07:26 -0700280 media::Metadata::Filter mMetadataUpdated; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700281
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800282#if CALLBACK_ANTAGONIZER
283 Antagonizer* mAntagonizer;
284#endif
285 };
286
287// ----------------------------------------------------------------------------
288
289 MediaPlayerService();
290 virtual ~MediaPlayerService();
291
292 mutable Mutex mLock;
293 SortedVector< wp<Client> > mClients;
Gloria Wangdac6a312009-10-29 15:46:37 -0700294 SortedVector< wp<MediaRecorderClient> > mMediaRecorderClients;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295 int32_t mNextConnId;
Andreas Huber318ad9c2009-10-15 13:46:54 -0700296 sp<IOMX> mOMX;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800297};
298
299// ----------------------------------------------------------------------------
300
301}; // namespace android
302
303#endif // ANDROID_MEDIAPLAYERSERVICE_H