blob: 931667e78f948795c3e2ffed59c8c41ecd2bf679 [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;
The Android Open Source Project89fa4ad2009-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 Huber20111aa2009-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 Project89fa4ad2009-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 Huber20111aa2009-07-14 16:56:47 -070098 static void CallbackWrapper(
99 int event, void *me, void *info);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800100
101 AudioTrack* mTrack;
Andreas Huber20111aa2009-07-14 16:56:47 -0700102 AudioCallback mCallback;
103 void * mCallbackCookie;
The Android Open Source Project89fa4ad2009-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
Marco Nelissen10dbb8e2009-09-20 10:42:13 -0700115 public: // visualization hack support
116 uint32_t mNumFramesWritten;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800117 };
118
119 class AudioCache : public MediaPlayerBase::AudioSink
120 {
121 public:
122 AudioCache(const char* name);
123 virtual ~AudioCache() {}
124
125 virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
126 virtual bool realtime() const { return false; }
127 virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; }
128 virtual ssize_t frameCount() const { return mFrameCount; }
129 virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; }
130 virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); }
131 virtual uint32_t latency() const;
132 virtual float msecsPerFrame() const;
Andreas Huber20111aa2009-07-14 16:56:47 -0700133
134 virtual status_t open(
135 uint32_t sampleRate, int channelCount, int format,
136 int bufferCount = 1,
137 AudioCallback cb = NULL, void *cookie = NULL);
138
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800139 virtual void start() {}
140 virtual ssize_t write(const void* buffer, size_t size);
141 virtual void stop() {}
142 virtual void flush() {}
143 virtual void pause() {}
144 virtual void close() {}
145 void setAudioStreamType(int streamType) {}
146 void setVolume(float left, float right) {}
147 uint32_t sampleRate() const { return mSampleRate; }
148 uint32_t format() const { return (uint32_t)mFormat; }
149 size_t size() const { return mSize; }
150 status_t wait();
151
152 sp<IMemoryHeap> getHeap() const { return mHeap; }
153
154 static void notify(void* cookie, int msg, int ext1, int ext2);
155 virtual status_t dump(int fd, const Vector<String16>& args) const;
156
157 private:
158 AudioCache();
159
160 Mutex mLock;
161 Condition mSignal;
162 sp<MemoryHeapBase> mHeap;
163 float mMsecsPerFrame;
164 uint16_t mChannelCount;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700165 uint16_t mFormat;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800166 ssize_t mFrameCount;
167 uint32_t mSampleRate;
168 uint32_t mSize;
169 int mError;
170 bool mCommandComplete;
171 };
172
173public:
174 static void instantiate();
175
176 // IMediaPlayerService interface
177 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid);
178 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
179
180 // House keeping for media player clients
181 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url);
182 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length);
183 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
184 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 -0700185 virtual sp<IMemory> snoop();
Andreas Huber318ad9c2009-10-15 13:46:54 -0700186 virtual sp<IOMX> getOMX();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800187
188 virtual status_t dump(int fd, const Vector<String16>& args);
189
190 void removeClient(wp<Client> client);
191
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700192
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193private:
194
195 class Client : public BnMediaPlayer {
196
197 // IMediaPlayer interface
198 virtual void disconnect();
199 virtual status_t setVideoSurface(const sp<ISurface>& surface);
200 virtual status_t prepareAsync();
201 virtual status_t start();
202 virtual status_t stop();
203 virtual status_t pause();
204 virtual status_t isPlaying(bool* state);
205 virtual status_t seekTo(int msec);
206 virtual status_t getCurrentPosition(int* msec);
207 virtual status_t getDuration(int* msec);
208 virtual status_t reset();
209 virtual status_t setAudioStreamType(int type);
210 virtual status_t setLooping(int loop);
211 virtual status_t setVolume(float leftVolume, float rightVolume);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700212 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700213 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania48290382009-07-10 13:53:06 -0700214 virtual status_t getMetadata(bool update_only,
215 bool apply_filter,
216 Parcel *reply);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217
218 sp<MediaPlayerBase> createPlayer(player_type playerType);
219 status_t setDataSource(const char *url);
220 status_t setDataSource(int fd, int64_t offset, int64_t length);
221 static void notify(void* cookie, int msg, int ext1, int ext2);
222
223 pid_t pid() const { return mPid; }
224 virtual status_t dump(int fd, const Vector<String16>& args) const;
225
226 private:
227 friend class MediaPlayerService;
228 Client( const sp<MediaPlayerService>& service,
229 pid_t pid,
230 int32_t connId,
231 const sp<IMediaPlayerClient>& client);
232 Client();
233 virtual ~Client();
234
235 void deletePlayer();
236
237 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
238
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700239
Nicolas Catania48290382009-07-10 13:53:06 -0700240
241 // @param type Of the metadata to be tested.
242 // @return true if the metadata should be dropped according to
243 // the filters.
nikoa64c8c72009-07-20 15:07:26 -0700244 bool shouldDropMetadata(media::Metadata::Type type) const;
Nicolas Catania48290382009-07-10 13:53:06 -0700245
246 // Add a new element to the set of metadata updated. Noop if
247 // the element exists already.
248 // @param type Of the metadata to be recorded.
nikoa64c8c72009-07-20 15:07:26 -0700249 void addNewMetadataUpdate(media::Metadata::Type type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700250
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251 mutable Mutex mLock;
252 sp<MediaPlayerBase> mPlayer;
253 sp<MediaPlayerService> mService;
254 sp<IMediaPlayerClient> mClient;
255 sp<AudioOutput> mAudioOutput;
256 pid_t mPid;
257 status_t mStatus;
258 bool mLoop;
259 int32_t mConnId;
Nicolas Catania48290382009-07-10 13:53:06 -0700260
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700261 // Metadata filters.
nikoa64c8c72009-07-20 15:07:26 -0700262 media::Metadata::Filter mMetadataAllow; // protected by mLock
263 media::Metadata::Filter mMetadataDrop; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700264
265 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
266 // notification we try to update mMetadataUpdated which is a
267 // set: no duplicate.
268 // getMetadata clears this set.
nikoa64c8c72009-07-20 15:07:26 -0700269 media::Metadata::Filter mMetadataUpdated; // protected by mLock
Nicolas Catania48290382009-07-10 13:53:06 -0700270
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800271#if CALLBACK_ANTAGONIZER
272 Antagonizer* mAntagonizer;
273#endif
274 };
275
276// ----------------------------------------------------------------------------
277
278 MediaPlayerService();
279 virtual ~MediaPlayerService();
280
281 mutable Mutex mLock;
282 SortedVector< wp<Client> > mClients;
283 int32_t mNextConnId;
Andreas Huber318ad9c2009-10-15 13:46:54 -0700284 sp<IOMX> mOMX;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800285};
286
287// ----------------------------------------------------------------------------
288
289}; // namespace android
290
291#endif // ANDROID_MEDIAPLAYERSERVICE_H