blob: db3d5d7c506c0b83685a502f4794a24504f467a8 [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;
38
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;
78 virtual status_t open(uint32_t sampleRate, int channelCount, int format, int bufferCount=4);
79 virtual void start();
80 virtual ssize_t write(const void* buffer, size_t size);
81 virtual void stop();
82 virtual void flush();
83 virtual void pause();
84 virtual void close();
85 void setAudioStreamType(int streamType) { mStreamType = streamType; }
86 void setVolume(float left, float right);
87 virtual status_t dump(int fd, const Vector<String16>& args) const;
88
89 static bool isOnEmulator();
90 static int getMinBufferCount();
91 private:
92 static void setMinBufferCount();
93
94 AudioTrack* mTrack;
95 int mStreamType;
96 float mLeftVolume;
97 float mRightVolume;
98 float mMsecsPerFrame;
99 uint32_t mLatency;
100
101 // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
102 static const uint32_t kAudioVideoDelayMs;
103 static bool mIsOnEmulator;
104 static int mMinBufferCount; // 12 for emulator; otherwise 4
105
106 };
107
108 class AudioCache : public MediaPlayerBase::AudioSink
109 {
110 public:
111 AudioCache(const char* name);
112 virtual ~AudioCache() {}
113
114 virtual bool ready() const { return (mChannelCount > 0) && (mHeap->getHeapID() > 0); }
115 virtual bool realtime() const { return false; }
116 virtual ssize_t bufferSize() const { return frameSize() * mFrameCount; }
117 virtual ssize_t frameCount() const { return mFrameCount; }
118 virtual ssize_t channelCount() const { return (ssize_t)mChannelCount; }
119 virtual ssize_t frameSize() const { return ssize_t(mChannelCount * ((mFormat == AudioSystem::PCM_16_BIT)?sizeof(int16_t):sizeof(u_int8_t))); }
120 virtual uint32_t latency() const;
121 virtual float msecsPerFrame() const;
122 virtual status_t open(uint32_t sampleRate, int channelCount, int format, int bufferCount=1);
123 virtual void start() {}
124 virtual ssize_t write(const void* buffer, size_t size);
125 virtual void stop() {}
126 virtual void flush() {}
127 virtual void pause() {}
128 virtual void close() {}
129 void setAudioStreamType(int streamType) {}
130 void setVolume(float left, float right) {}
131 uint32_t sampleRate() const { return mSampleRate; }
132 uint32_t format() const { return (uint32_t)mFormat; }
133 size_t size() const { return mSize; }
134 status_t wait();
135
136 sp<IMemoryHeap> getHeap() const { return mHeap; }
137
138 static void notify(void* cookie, int msg, int ext1, int ext2);
139 virtual status_t dump(int fd, const Vector<String16>& args) const;
140
141 private:
142 AudioCache();
143
144 Mutex mLock;
145 Condition mSignal;
146 sp<MemoryHeapBase> mHeap;
147 float mMsecsPerFrame;
148 uint16_t mChannelCount;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149 uint16_t mFormat;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800150 ssize_t mFrameCount;
151 uint32_t mSampleRate;
152 uint32_t mSize;
153 int mError;
154 bool mCommandComplete;
155 };
156
157public:
158 static void instantiate();
159
160 // IMediaPlayerService interface
161 virtual sp<IMediaRecorder> createMediaRecorder(pid_t pid);
162 virtual sp<IMediaMetadataRetriever> createMetadataRetriever(pid_t pid);
163
164 // House keeping for media player clients
165 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, const char* url);
166 virtual sp<IMediaPlayer> create(pid_t pid, const sp<IMediaPlayerClient>& client, int fd, int64_t offset, int64_t length);
167 virtual sp<IMemory> decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
168 virtual sp<IMemory> decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, int* pFormat);
169
170 virtual status_t dump(int fd, const Vector<String16>& args);
171
172 void removeClient(wp<Client> client);
173
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700174
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800175private:
176
177 class Client : public BnMediaPlayer {
178
179 // IMediaPlayer interface
180 virtual void disconnect();
181 virtual status_t setVideoSurface(const sp<ISurface>& surface);
182 virtual status_t prepareAsync();
183 virtual status_t start();
184 virtual status_t stop();
185 virtual status_t pause();
186 virtual status_t isPlaying(bool* state);
187 virtual status_t seekTo(int msec);
188 virtual status_t getCurrentPosition(int* msec);
189 virtual status_t getDuration(int* msec);
190 virtual status_t reset();
191 virtual status_t setAudioStreamType(int type);
192 virtual status_t setLooping(int loop);
193 virtual status_t setVolume(float leftVolume, float rightVolume);
Nicolas Catania1d187f12009-05-12 23:25:55 -0700194 virtual status_t invoke(const Parcel& request, Parcel *reply);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700195 virtual status_t setMetadataFilter(const Parcel& filter);
Nicolas Catania48290382009-07-10 13:53:06 -0700196 virtual status_t getMetadata(bool update_only,
197 bool apply_filter,
198 Parcel *reply);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800199
200 sp<MediaPlayerBase> createPlayer(player_type playerType);
201 status_t setDataSource(const char *url);
202 status_t setDataSource(int fd, int64_t offset, int64_t length);
203 static void notify(void* cookie, int msg, int ext1, int ext2);
204
205 pid_t pid() const { return mPid; }
206 virtual status_t dump(int fd, const Vector<String16>& args) const;
207
208 private:
209 friend class MediaPlayerService;
210 Client( const sp<MediaPlayerService>& service,
211 pid_t pid,
212 int32_t connId,
213 const sp<IMediaPlayerClient>& client);
214 Client();
215 virtual ~Client();
216
217 void deletePlayer();
218
219 sp<MediaPlayerBase> getPlayer() const { Mutex::Autolock lock(mLock); return mPlayer; }
220
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700221
Nicolas Catania48290382009-07-10 13:53:06 -0700222
223 // @param type Of the metadata to be tested.
224 // @return true if the metadata should be dropped according to
225 // the filters.
226 bool shouldDropMetadata(MetadataType type) const;
227
228 // Add a new element to the set of metadata updated. Noop if
229 // the element exists already.
230 // @param type Of the metadata to be recorded.
231 void addNewMetadataUpdate(MetadataType type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700232
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233 mutable Mutex mLock;
234 sp<MediaPlayerBase> mPlayer;
235 sp<MediaPlayerService> mService;
236 sp<IMediaPlayerClient> mClient;
237 sp<AudioOutput> mAudioOutput;
238 pid_t mPid;
239 status_t mStatus;
240 bool mLoop;
241 int32_t mConnId;
Nicolas Catania48290382009-07-10 13:53:06 -0700242
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700243 // Metadata filters.
Nicolas Catania48290382009-07-10 13:53:06 -0700244 SortedVector<int32_t> mMetadataAllow; // protected by mLock
245 SortedVector<int32_t> mMetadataDrop; // protected by mLock
246
247 // Metadata updated. For each MEDIA_INFO_METADATA_UPDATE
248 // notification we try to update mMetadataUpdated which is a
249 // set: no duplicate.
250 // getMetadata clears this set.
251 SortedVector<int32_t> mMetadataUpdated; // protected by mLock
252
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253#if CALLBACK_ANTAGONIZER
254 Antagonizer* mAntagonizer;
255#endif
256 };
257
258// ----------------------------------------------------------------------------
259
260 MediaPlayerService();
261 virtual ~MediaPlayerService();
262
263 mutable Mutex mLock;
264 SortedVector< wp<Client> > mClients;
265 int32_t mNextConnId;
266};
267
268// ----------------------------------------------------------------------------
269
270}; // namespace android
271
272#endif // ANDROID_MEDIAPLAYERSERVICE_H