blob: eaa44e29280f374f1fd15ed31f6d589a44e7f55b [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// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
Gloria Wang7cf180c2011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Mathias Agopian75624082009-05-19 19:08:10 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryHeapBase.h>
40#include <binder/MemoryBase.h>
Jamie Gennis7dae00b2011-10-26 18:36:31 -070041#include <gui/SurfaceTextureClient.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070042#include <utils/Errors.h> // for status_t
43#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070044#include <utils/SystemClock.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070045#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046
Jeff Brown2013a542012-09-04 21:38:42 -070047#include <media/IRemoteDisplay.h>
48#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049#include <media/MediaPlayerInterface.h>
50#include <media/mediarecorder.h>
51#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070052#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070054#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070055#include <media/stagefright/MediaErrors.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056
Dima Zavin64760242011-05-11 14:15:23 -070057#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070058
Gloria Wang7cf180c2011-02-19 18:37:57 -080059#include <private/android_filesystem_config.h>
60
James Dong559bf282012-03-28 10:29:14 -070061#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062#include "MediaRecorderClient.h"
63#include "MediaPlayerService.h"
64#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070065#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066
67#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070068#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070069#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080070#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070071
Andreas Huber20111aa2009-07-14 16:56:47 -070072#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070073
Andreas Hubered3e3e02012-03-26 11:13:27 -070074#include "Crypto.h"
Andreas Huber59451f82012-09-18 10:36:32 -070075#include "HDCP.h"
Andreas Huber35213f12012-08-29 11:41:50 -070076#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070077
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070078namespace {
nikoa64c8c72009-07-20 15:07:26 -070079using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070080using android::status_t;
81using android::OK;
82using android::BAD_VALUE;
83using android::NOT_ENOUGH_DATA;
84using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070085
86// Max number of entries in the filter.
87const int kMaxFilterSize = 64; // I pulled that out of thin air.
88
nikoa64c8c72009-07-20 15:07:26 -070089// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070090
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070091
92// Unmarshall a filter from a Parcel.
93// Filter format in a parcel:
94//
95// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
96// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97// | number of entries (n) |
98// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99// | metadata type 1 |
100// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101// | metadata type 2 |
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103// ....
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | metadata type n |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107//
108// @param p Parcel that should start with a filter.
109// @param[out] filter On exit contains the list of metadata type to be
110// filtered.
111// @param[out] status On exit contains the status code to be returned.
112// @return true if the parcel starts with a valid filter.
113bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700114 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700115 status_t *status)
116{
Nicolas Catania48290382009-07-10 13:53:06 -0700117 int32_t val;
118 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700119 {
Steve Block29357bc2012-01-06 19:20:56 +0000120 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700121 *status = NOT_ENOUGH_DATA;
122 return false;
123 }
124
Nicolas Catania48290382009-07-10 13:53:06 -0700125 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 {
Steve Block29357bc2012-01-06 19:20:56 +0000127 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 *status = BAD_VALUE;
129 return false;
130 }
131
Nicolas Catania48290382009-07-10 13:53:06 -0700132 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133
134 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700135 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136
nikoa64c8c72009-07-20 15:07:26 -0700137 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138
Nicolas Catania48290382009-07-10 13:53:06 -0700139
140 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141 {
Steve Block29357bc2012-01-06 19:20:56 +0000142 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143 *status = NOT_ENOUGH_DATA;
144 return false;
145 }
146
nikoa64c8c72009-07-20 15:07:26 -0700147 const Metadata::Type *data =
148 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149
Nicolas Catania48290382009-07-10 13:53:06 -0700150 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 {
Steve Block29357bc2012-01-06 19:20:56 +0000152 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700153 *status = BAD_VALUE;
154 return false;
155 }
156
157 // TODO: The stl impl of vector would be more efficient here
158 // because it degenerates into a memcpy on pod types. Try to
159 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700160 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 {
Nicolas Catania48290382009-07-10 13:53:06 -0700162 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700163 ++data;
164 }
165 *status = OK;
166 return true;
167}
168
Nicolas Catania48290382009-07-10 13:53:06 -0700169// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170// @param val To be searched.
171// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700172bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700173{
174 // Deal with empty and ANY right away
175 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700176 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700177
Nicolas Catania48290382009-07-10 13:53:06 -0700178 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700179}
180
181} // anonymous namespace
182
183
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184namespace android {
185
Dave Burked681bbb2011-08-30 14:39:17 +0100186static bool checkPermission(const char* permissionString) {
187#ifndef HAVE_ANDROID_OS
188 return true;
189#endif
190 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
191 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000192 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100193 return ok;
194}
195
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196// TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800197/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
198/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
199
200void MediaPlayerService::instantiate() {
201 defaultServiceManager()->addService(
202 String16("media.player"), new MediaPlayerService());
203}
204
205MediaPlayerService::MediaPlayerService()
206{
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800209
210 mBatteryAudio.refCount = 0;
211 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
212 mBatteryAudio.deviceOn[i] = 0;
213 mBatteryAudio.lastTime[i] = 0;
214 mBatteryAudio.totalTime[i] = 0;
215 }
216 // speaker is on by default
217 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700218
219 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220}
221
222MediaPlayerService::~MediaPlayerService()
223{
Steve Block3856b092011-10-20 11:56:00 +0100224 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225}
226
227sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
228{
Gloria Wangdac6a312009-10-29 15:46:37 -0700229 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
230 wp<MediaRecorderClient> w = recorder;
231 Mutex::Autolock lock(mLock);
232 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100233 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234 return recorder;
235}
236
Gloria Wangdac6a312009-10-29 15:46:37 -0700237void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
238{
239 Mutex::Autolock lock(mLock);
240 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100241 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700242}
243
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
245{
246 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100247 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 return retriever;
249}
250
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100252 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253{
254 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700255
256 sp<Client> c = new Client(
257 this, pid, connId, client, audioSessionId,
258 IPCThreadState::self()->getCallingUid());
259
Steve Block3856b092011-10-20 11:56:00 +0100260 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100261 IPCThreadState::self()->getCallingUid());
262
263 wp<Client> w = c;
264 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 Mutex::Autolock lock(mLock);
266 mClients.add(w);
267 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800268 return c;
269}
270
Andreas Huber318ad9c2009-10-15 13:46:54 -0700271sp<IOMX> MediaPlayerService::getOMX() {
272 Mutex::Autolock autoLock(mLock);
273
274 if (mOMX.get() == NULL) {
275 mOMX = new OMX;
276 }
277
278 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700279}
280
Andreas Hubered3e3e02012-03-26 11:13:27 -0700281sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700282 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700283}
284
Andreas Huber59451f82012-09-18 10:36:32 -0700285sp<IHDCP> MediaPlayerService::makeHDCP() {
286 return new HDCP;
287}
288
Jeff Brown2013a542012-09-04 21:38:42 -0700289sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
290 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700291 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
292 return NULL;
293 }
294
Jeff Brownced24b32012-09-05 17:48:03 -0700295 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700296}
297
Jeff Brownaba33d52012-09-07 17:38:58 -0700298// TODO: delete this method
Andreas Huberab1bd842012-08-30 14:51:40 -0700299status_t MediaPlayerService::enableRemoteDisplay(const char *iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700300 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
301 return PERMISSION_DENIED;
302 }
303
Andreas Huber35213f12012-08-29 11:41:50 -0700304 Mutex::Autolock autoLock(mLock);
305
Andreas Huberab1bd842012-08-30 14:51:40 -0700306 if (iface != NULL) {
307 if (mRemoteDisplay != NULL) {
308 return INVALID_OPERATION;
309 }
310
Andreas Huber28169b12012-09-05 10:26:52 -0700311 mRemoteDisplay = new RemoteDisplay(NULL /* client */, iface);
Andreas Huber35213f12012-08-29 11:41:50 -0700312 return OK;
Andreas Huberab1bd842012-08-30 14:51:40 -0700313 }
314
315 if (mRemoteDisplay != NULL) {
Jeff Brownced24b32012-09-05 17:48:03 -0700316 mRemoteDisplay->dispose();
Andreas Huber35213f12012-08-29 11:41:50 -0700317 mRemoteDisplay.clear();
318 }
319
320 return OK;
321}
322
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
324{
325 const size_t SIZE = 256;
326 char buffer[SIZE];
327 String8 result;
328
329 result.append(" AudioCache\n");
330 if (mHeap != 0) {
331 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
332 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
333 result.append(buffer);
334 }
335 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
336 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
337 result.append(buffer);
338 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
339 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
340 result.append(buffer);
341 ::write(fd, result.string(), result.size());
342 return NO_ERROR;
343}
344
345status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
346{
347 const size_t SIZE = 256;
348 char buffer[SIZE];
349 String8 result;
350
351 result.append(" AudioOutput\n");
352 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
353 mStreamType, mLeftVolume, mRightVolume);
354 result.append(buffer);
355 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800356 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700358 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
359 mAuxEffectId, mSendLevel);
360 result.append(buffer);
361
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800362 ::write(fd, result.string(), result.size());
363 if (mTrack != 0) {
364 mTrack->dump(fd, args);
365 }
366 return NO_ERROR;
367}
368
369status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
370{
371 const size_t SIZE = 256;
372 char buffer[SIZE];
373 String8 result;
374 result.append(" Client\n");
375 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
376 mPid, mConnId, mStatus, mLoop?"true": "false");
377 result.append(buffer);
378 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700379 if (mPlayer != NULL) {
380 mPlayer->dump(fd, args);
381 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800382 if (mAudioOutput != 0) {
383 mAudioOutput->dump(fd, args);
384 }
385 write(fd, "\n", 1);
386 return NO_ERROR;
387}
388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
390{
391 const size_t SIZE = 256;
392 char buffer[SIZE];
393 String8 result;
394 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
395 snprintf(buffer, SIZE, "Permission Denial: "
396 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
397 IPCThreadState::self()->getCallingPid(),
398 IPCThreadState::self()->getCallingUid());
399 result.append(buffer);
400 } else {
401 Mutex::Autolock lock(mLock);
402 for (int i = 0, n = mClients.size(); i < n; ++i) {
403 sp<Client> c = mClients[i].promote();
404 if (c != 0) c->dump(fd, args);
405 }
James Dongb9141222010-07-08 11:16:11 -0700406 if (mMediaRecorderClients.size() == 0) {
407 result.append(" No media recorder client\n\n");
408 } else {
409 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
410 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700411 if (c != 0) {
412 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
413 result.append(buffer);
414 write(fd, result.string(), result.size());
415 result = "\n";
416 c->dump(fd, args);
417 }
James Dongb9141222010-07-08 11:16:11 -0700418 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700419 }
420
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700422 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 FILE *f = fopen(buffer, "r");
424 if (f) {
425 while (!feof(f)) {
426 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700427 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800428 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700429 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 strstr(buffer, " /system/media/")) {
431 result.append(" ");
432 result.append(buffer);
433 }
434 }
435 fclose(f);
436 } else {
437 result.append("couldn't open ");
438 result.append(buffer);
439 result.append("\n");
440 }
441
Glenn Kasten0512ab52011-05-04 17:58:57 -0700442 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443 DIR *d = opendir(buffer);
444 if (d) {
445 struct dirent *ent;
446 while((ent = readdir(d)) != NULL) {
447 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700448 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800449 struct stat s;
450 if (lstat(buffer, &s) == 0) {
451 if ((s.st_mode & S_IFMT) == S_IFLNK) {
452 char linkto[256];
453 int len = readlink(buffer, linkto, sizeof(linkto));
454 if(len > 0) {
455 if(len > 255) {
456 linkto[252] = '.';
457 linkto[253] = '.';
458 linkto[254] = '.';
459 linkto[255] = 0;
460 } else {
461 linkto[len] = 0;
462 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700463 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700465 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466 strstr(linkto, "/system/media/") == linkto) {
467 result.append(" ");
468 result.append(buffer);
469 result.append(" -> ");
470 result.append(linkto);
471 result.append("\n");
472 }
473 }
474 } else {
475 result.append(" unexpected type for ");
476 result.append(buffer);
477 result.append("\n");
478 }
479 }
480 }
481 }
482 closedir(d);
483 } else {
484 result.append("couldn't open ");
485 result.append(buffer);
486 result.append("\n");
487 }
488
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489 bool dumpMem = false;
490 for (size_t i = 0; i < args.size(); i++) {
491 if (args[i] == String16("-m")) {
492 dumpMem = true;
493 }
494 }
495 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700496 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800497 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498 }
499 write(fd, result.string(), result.size());
500 return NO_ERROR;
501}
502
503void MediaPlayerService::removeClient(wp<Client> client)
504{
505 Mutex::Autolock lock(mLock);
506 mClients.remove(client);
507}
508
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700509MediaPlayerService::Client::Client(
510 const sp<MediaPlayerService>& service, pid_t pid,
511 int32_t connId, const sp<IMediaPlayerClient>& client,
512 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513{
Steve Block3856b092011-10-20 11:56:00 +0100514 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800515 mPid = pid;
516 mConnId = connId;
517 mService = service;
518 mClient = client;
519 mLoop = false;
520 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700521 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700522 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800523 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700524
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000526 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527 mAntagonizer = new Antagonizer(notify, this);
528#endif
529}
530
531MediaPlayerService::Client::~Client()
532{
Steve Block3856b092011-10-20 11:56:00 +0100533 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534 mAudioOutput.clear();
535 wp<Client> client(this);
536 disconnect();
537 mService->removeClient(client);
538}
539
540void MediaPlayerService::Client::disconnect()
541{
Steve Block3856b092011-10-20 11:56:00 +0100542 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800543 // grab local reference and clear main reference to prevent future
544 // access to object
545 sp<MediaPlayerBase> p;
546 {
547 Mutex::Autolock l(mLock);
548 p = mPlayer;
549 }
Dave Sparks795fa582009-03-24 17:57:12 -0700550 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700551
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800552 mPlayer.clear();
553
554 // clear the notification to prevent callbacks to dead client
555 // and reset the player. We assume the player will serialize
556 // access to itself if necessary.
557 if (p != 0) {
558 p->setNotifyCallback(0, 0);
559#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000560 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800561 mAntagonizer->kill();
562#endif
563 p->reset();
564 }
565
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700566 disconnectNativeWindow();
567
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 IPCThreadState::self()->flushCommands();
569}
570
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
572{
573 // determine if we have the right player type
574 sp<MediaPlayerBase> p = mPlayer;
575 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100576 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577 p.clear();
578 }
579 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700580 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800581 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700582
Jason Simmonsdb29e522011-08-12 13:46:55 -0700583 if (p != NULL) {
584 p->setUID(mUID);
585 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700586
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587 return p;
588}
589
John Grossmanc795b642012-02-22 15:38:35 -0800590sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
591 player_type playerType)
592{
593 ALOGV("player type = %d", playerType);
594
595 // create the right type of player
596 sp<MediaPlayerBase> p = createPlayer(playerType);
597 if (p == NULL) {
598 return p;
599 }
600
601 if (!p->hardwareOutput()) {
602 mAudioOutput = new AudioOutput(mAudioSessionId);
603 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
604 }
605
606 return p;
607}
608
609void MediaPlayerService::Client::setDataSource_post(
610 const sp<MediaPlayerBase>& p,
611 status_t status)
612{
613 ALOGV(" setDataSource");
614 mStatus = status;
615 if (mStatus != OK) {
616 ALOGE(" error: %d", mStatus);
617 return;
618 }
619
620 // Set the re-transmission endpoint if one was chosen.
621 if (mRetransmitEndpointValid) {
622 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
623 if (mStatus != NO_ERROR) {
624 ALOGE("setRetransmitEndpoint error: %d", mStatus);
625 }
626 }
627
628 if (mStatus == OK) {
629 mPlayer = p;
630 }
631}
632
Andreas Huber2db84552010-01-28 11:19:57 -0800633status_t MediaPlayerService::Client::setDataSource(
634 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635{
Steve Block3856b092011-10-20 11:56:00 +0100636 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637 if (url == NULL)
638 return UNKNOWN_ERROR;
639
Dave Burked681bbb2011-08-30 14:39:17 +0100640 if ((strncmp(url, "http://", 7) == 0) ||
641 (strncmp(url, "https://", 8) == 0) ||
642 (strncmp(url, "rtsp://", 7) == 0)) {
643 if (!checkPermission("android.permission.INTERNET")) {
644 return PERMISSION_DENIED;
645 }
646 }
647
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800648 if (strncmp(url, "content://", 10) == 0) {
649 // get a filedescriptor for the content Uri and
650 // pass it to the setDataSource(fd) method
651
652 String16 url16(url);
653 int fd = android::openContentProviderFile(url16);
654 if (fd < 0)
655 {
Steve Block29357bc2012-01-06 19:20:56 +0000656 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800657 return UNKNOWN_ERROR;
658 }
659 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
660 close(fd);
661 return mStatus;
662 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700663 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800664 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
665 if (p == NULL) {
666 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667 }
668
John Grossmanc795b642012-02-22 15:38:35 -0800669 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670 return mStatus;
671 }
672}
673
674status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
675{
Steve Block3856b092011-10-20 11:56:00 +0100676 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677 struct stat sb;
678 int ret = fstat(fd, &sb);
679 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000680 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681 return UNKNOWN_ERROR;
682 }
683
Steve Block3856b092011-10-20 11:56:00 +0100684 ALOGV("st_dev = %llu", sb.st_dev);
685 ALOGV("st_mode = %u", sb.st_mode);
686 ALOGV("st_uid = %lu", sb.st_uid);
687 ALOGV("st_gid = %lu", sb.st_gid);
688 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689
690 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000691 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800692 ::close(fd);
693 return UNKNOWN_ERROR;
694 }
695 if (offset + length > sb.st_size) {
696 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100697 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698 }
699
John Grossman44a7e422012-06-21 17:29:24 -0700700 player_type playerType = MediaPlayerFactory::getPlayerType(this,
701 fd,
702 offset,
703 length);
John Grossmanc795b642012-02-22 15:38:35 -0800704 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
705 if (p == NULL) {
706 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 }
708
709 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800710 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711 return mStatus;
712}
713
Andreas Hubere2b10282010-11-23 11:41:34 -0800714status_t MediaPlayerService::Client::setDataSource(
715 const sp<IStreamSource> &source) {
716 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700717 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800718 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800719 if (p == NULL) {
720 return NO_INIT;
721 }
722
Andreas Hubere2b10282010-11-23 11:41:34 -0800723 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800724 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800725 return mStatus;
726}
727
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700728void MediaPlayerService::Client::disconnectNativeWindow() {
729 if (mConnectedWindow != NULL) {
730 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
731 NATIVE_WINDOW_API_MEDIA);
732
733 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000734 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700735 strerror(-err), err);
736 }
737 }
738 mConnectedWindow.clear();
739}
740
Glenn Kasten11731182011-02-08 17:26:17 -0800741status_t MediaPlayerService::Client::setVideoSurfaceTexture(
742 const sp<ISurfaceTexture>& surfaceTexture)
743{
Steve Block3856b092011-10-20 11:56:00 +0100744 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800745 sp<MediaPlayerBase> p = getPlayer();
746 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700747
748 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
749 surfaceTexture->asBinder());
750 if (mConnectedWindowBinder == binder) {
751 return OK;
752 }
753
754 sp<ANativeWindow> anw;
755 if (surfaceTexture != NULL) {
756 anw = new SurfaceTextureClient(surfaceTexture);
757 status_t err = native_window_api_connect(anw.get(),
758 NATIVE_WINDOW_API_MEDIA);
759
760 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000761 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700762 // Note that we must do the reset before disconnecting from the ANW.
763 // Otherwise queue/dequeue calls could be made on the disconnected
764 // ANW, which may result in errors.
765 reset();
766
767 disconnectNativeWindow();
768
769 return err;
770 }
771 }
772
773 // Note that we must set the player's new SurfaceTexture before
774 // disconnecting the old one. Otherwise queue/dequeue calls could be made
775 // on the disconnected ANW, which may result in errors.
776 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
777
778 disconnectNativeWindow();
779
780 mConnectedWindow = anw;
781
782 if (err == OK) {
783 mConnectedWindowBinder = binder;
784 } else {
785 disconnectNativeWindow();
786 }
787
788 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800789}
790
Nicolas Catania1d187f12009-05-12 23:25:55 -0700791status_t MediaPlayerService::Client::invoke(const Parcel& request,
792 Parcel *reply)
793{
794 sp<MediaPlayerBase> p = getPlayer();
795 if (p == NULL) return UNKNOWN_ERROR;
796 return p->invoke(request, reply);
797}
798
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700799// This call doesn't need to access the native player.
800status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
801{
802 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700803 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700804
Nicolas Catania48290382009-07-10 13:53:06 -0700805 if (unmarshallFilter(filter, &allow, &status) &&
806 unmarshallFilter(filter, &drop, &status)) {
807 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700808
809 mMetadataAllow = allow;
810 mMetadataDrop = drop;
811 }
812 return status;
813}
814
Nicolas Catania48290382009-07-10 13:53:06 -0700815status_t MediaPlayerService::Client::getMetadata(
816 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700817{
nikoa64c8c72009-07-20 15:07:26 -0700818 sp<MediaPlayerBase> player = getPlayer();
819 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700820
nikod608a812009-07-16 16:39:53 -0700821 status_t status;
822 // Placeholder for the return code, updated by the caller.
823 reply->writeInt32(-1);
824
nikoa64c8c72009-07-20 15:07:26 -0700825 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700826
827 // We don't block notifications while we fetch the data. We clear
828 // mMetadataUpdated first so we don't lose notifications happening
829 // during the rest of this call.
830 {
831 Mutex::Autolock lock(mLock);
832 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700833 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700834 }
835 mMetadataUpdated.clear();
836 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700837
nikoa64c8c72009-07-20 15:07:26 -0700838 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700839
nikoa64c8c72009-07-20 15:07:26 -0700840 metadata.appendHeader();
841 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700842
843 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700844 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000845 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700846 return status;
847 }
848
849 // FIXME: Implement filtering on the result. Not critical since
850 // filtering takes place on the update notifications already. This
851 // would be when all the metadata are fetch and a filter is set.
852
nikod608a812009-07-16 16:39:53 -0700853 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700854 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700855 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700856}
857
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800858status_t MediaPlayerService::Client::prepareAsync()
859{
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 sp<MediaPlayerBase> p = getPlayer();
862 if (p == 0) return UNKNOWN_ERROR;
863 status_t ret = p->prepareAsync();
864#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000865 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800866 if (ret == NO_ERROR) mAntagonizer->start();
867#endif
868 return ret;
869}
870
871status_t MediaPlayerService::Client::start()
872{
Steve Block3856b092011-10-20 11:56:00 +0100873 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800874 sp<MediaPlayerBase> p = getPlayer();
875 if (p == 0) return UNKNOWN_ERROR;
876 p->setLooping(mLoop);
877 return p->start();
878}
879
880status_t MediaPlayerService::Client::stop()
881{
Steve Block3856b092011-10-20 11:56:00 +0100882 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800883 sp<MediaPlayerBase> p = getPlayer();
884 if (p == 0) return UNKNOWN_ERROR;
885 return p->stop();
886}
887
888status_t MediaPlayerService::Client::pause()
889{
Steve Block3856b092011-10-20 11:56:00 +0100890 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800891 sp<MediaPlayerBase> p = getPlayer();
892 if (p == 0) return UNKNOWN_ERROR;
893 return p->pause();
894}
895
896status_t MediaPlayerService::Client::isPlaying(bool* state)
897{
898 *state = false;
899 sp<MediaPlayerBase> p = getPlayer();
900 if (p == 0) return UNKNOWN_ERROR;
901 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100902 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903 return NO_ERROR;
904}
905
906status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
907{
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 sp<MediaPlayerBase> p = getPlayer();
910 if (p == 0) return UNKNOWN_ERROR;
911 status_t ret = p->getCurrentPosition(msec);
912 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100913 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800914 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000915 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800916 }
917 return ret;
918}
919
920status_t MediaPlayerService::Client::getDuration(int *msec)
921{
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800923 sp<MediaPlayerBase> p = getPlayer();
924 if (p == 0) return UNKNOWN_ERROR;
925 status_t ret = p->getDuration(msec);
926 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100927 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800928 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000929 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800930 }
931 return ret;
932}
933
Marco Nelissen6b74d672012-02-28 16:07:44 -0800934status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
935 ALOGV("setNextPlayer");
936 Mutex::Autolock l(mLock);
937 sp<Client> c = static_cast<Client*>(player.get());
938 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -0700939
940 if (c != NULL) {
941 if (mAudioOutput != NULL) {
942 mAudioOutput->setNextOutput(c->mAudioOutput);
943 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
944 ALOGE("no current audio output");
945 }
946
947 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
948 mPlayer->setNextPlayer(mNextClient->getPlayer());
949 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800950 }
John Grossman5f7e55e2012-08-24 14:47:25 -0700951
Marco Nelissen6b74d672012-02-28 16:07:44 -0800952 return OK;
953}
954
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800955status_t MediaPlayerService::Client::seekTo(int msec)
956{
Steve Block3856b092011-10-20 11:56:00 +0100957 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800958 sp<MediaPlayerBase> p = getPlayer();
959 if (p == 0) return UNKNOWN_ERROR;
960 return p->seekTo(msec);
961}
962
963status_t MediaPlayerService::Client::reset()
964{
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800966 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967 sp<MediaPlayerBase> p = getPlayer();
968 if (p == 0) return UNKNOWN_ERROR;
969 return p->reset();
970}
971
Glenn Kastenfff6d712012-01-12 16:38:12 -0800972status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800973{
Steve Block3856b092011-10-20 11:56:00 +0100974 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800975 // TODO: for hardware output, call player instead
976 Mutex::Autolock l(mLock);
977 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
978 return NO_ERROR;
979}
980
981status_t MediaPlayerService::Client::setLooping(int loop)
982{
Steve Block3856b092011-10-20 11:56:00 +0100983 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800984 mLoop = loop;
985 sp<MediaPlayerBase> p = getPlayer();
986 if (p != 0) return p->setLooping(loop);
987 return NO_ERROR;
988}
989
990status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
991{
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800993
994 // for hardware output, call player instead
995 sp<MediaPlayerBase> p = getPlayer();
996 {
997 Mutex::Autolock l(mLock);
998 if (p != 0 && p->hardwareOutput()) {
999 MediaPlayerHWInterface* hwp =
1000 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1001 return hwp->setVolume(leftVolume, rightVolume);
1002 } else {
1003 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1004 return NO_ERROR;
1005 }
1006 }
1007
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001008 return NO_ERROR;
1009}
1010
Eric Laurent2beeb502010-07-16 07:43:46 -07001011status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1012{
Steve Block3856b092011-10-20 11:56:00 +01001013 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001014 Mutex::Autolock l(mLock);
1015 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1016 return NO_ERROR;
1017}
1018
1019status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1020{
Steve Block3856b092011-10-20 11:56:00 +01001021 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001022 Mutex::Autolock l(mLock);
1023 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1024 return NO_ERROR;
1025}
Nicolas Catania48290382009-07-10 13:53:06 -07001026
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001027status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001028 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001029 sp<MediaPlayerBase> p = getPlayer();
1030 if (p == 0) return UNKNOWN_ERROR;
1031 return p->setParameter(key, request);
1032}
1033
1034status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001035 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001036 sp<MediaPlayerBase> p = getPlayer();
1037 if (p == 0) return UNKNOWN_ERROR;
1038 return p->getParameter(key, reply);
1039}
1040
John Grossmanc795b642012-02-22 15:38:35 -08001041status_t MediaPlayerService::Client::setRetransmitEndpoint(
1042 const struct sockaddr_in* endpoint) {
1043
1044 if (NULL != endpoint) {
1045 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1046 uint16_t p = ntohs(endpoint->sin_port);
1047 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1048 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1049 } else {
1050 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1051 }
1052
1053 sp<MediaPlayerBase> p = getPlayer();
1054
1055 // Right now, the only valid time to set a retransmit endpoint is before
1056 // player selection has been made (since the presence or absence of a
1057 // retransmit endpoint is going to determine which player is selected during
1058 // setDataSource).
1059 if (p != 0) return INVALID_OPERATION;
1060
1061 if (NULL != endpoint) {
1062 mRetransmitEndpoint = *endpoint;
1063 mRetransmitEndpointValid = true;
1064 } else {
1065 mRetransmitEndpointValid = false;
1066 }
1067
1068 return NO_ERROR;
1069}
1070
John Grossman44a7e422012-06-21 17:29:24 -07001071status_t MediaPlayerService::Client::getRetransmitEndpoint(
1072 struct sockaddr_in* endpoint)
1073{
1074 if (NULL == endpoint)
1075 return BAD_VALUE;
1076
1077 sp<MediaPlayerBase> p = getPlayer();
1078
1079 if (p != NULL)
1080 return p->getRetransmitEndpoint(endpoint);
1081
1082 if (!mRetransmitEndpointValid)
1083 return NO_INIT;
1084
1085 *endpoint = mRetransmitEndpoint;
1086
1087 return NO_ERROR;
1088}
1089
Gloria Wangb483c472011-04-11 17:23:27 -07001090void MediaPlayerService::Client::notify(
1091 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001092{
1093 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001094 if (client == NULL) {
1095 return;
1096 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001097
James Dongb8a98252012-08-26 16:13:03 -07001098 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001099 {
1100 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001101 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001102 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001103 if (client->mAudioOutput != NULL)
1104 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001105 client->mNextClient->start();
1106 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1107 }
1108 }
1109
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001110 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001111 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001112 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001113
1114 if(client->shouldDropMetadata(metadata_type)) {
1115 return;
1116 }
1117
1118 // Update the list of metadata that have changed. getMetadata
1119 // also access mMetadataUpdated and clears it.
1120 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001121 }
James Dongb8a98252012-08-26 16:13:03 -07001122
1123 if (c != NULL) {
1124 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1125 c->notify(msg, ext1, ext2, obj);
1126 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001127}
1128
Nicolas Catania48290382009-07-10 13:53:06 -07001129
nikoa64c8c72009-07-20 15:07:26 -07001130bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001131{
Nicolas Catania48290382009-07-10 13:53:06 -07001132 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001133
Nicolas Catania48290382009-07-10 13:53:06 -07001134 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001135 return true;
1136 }
1137
Nicolas Catania48290382009-07-10 13:53:06 -07001138 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001139 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001140 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001141 return true;
1142 }
1143}
1144
Nicolas Catania48290382009-07-10 13:53:06 -07001145
nikoa64c8c72009-07-20 15:07:26 -07001146void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001147 Mutex::Autolock lock(mLock);
1148 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1149 mMetadataUpdated.add(metadata_type);
1150 }
1151}
1152
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001153#if CALLBACK_ANTAGONIZER
1154const int Antagonizer::interval = 10000; // 10 msecs
1155
1156Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1157 mExit(false), mActive(false), mClient(client), mCb(cb)
1158{
1159 createThread(callbackThread, this);
1160}
1161
1162void Antagonizer::kill()
1163{
1164 Mutex::Autolock _l(mLock);
1165 mActive = false;
1166 mExit = true;
1167 mCondition.wait(mLock);
1168}
1169
1170int Antagonizer::callbackThread(void* user)
1171{
Steve Blockb8a80522011-12-20 16:23:08 +00001172 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001173 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1174 while (!p->mExit) {
1175 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001176 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001177 p->mCb(p->mClient, 0, 0, 0);
1178 }
1179 usleep(interval);
1180 }
1181 Mutex::Autolock _l(p->mLock);
1182 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001183 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001184 return 0;
1185}
1186#endif
1187
1188static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1189
Glenn Kastene1c39622012-01-04 09:36:37 -08001190sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001191{
Steve Block3856b092011-10-20 11:56:00 +01001192 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001193 sp<MemoryBase> mem;
1194 sp<MediaPlayerBase> player;
1195
1196 // Protect our precious, precious DRMd ringtones by only allowing
1197 // decoding of http, but not filesystem paths or content Uris.
1198 // If the application wants to decode those, it should open a
1199 // filedescriptor for them and use that.
1200 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001201 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001202 return mem;
1203 }
1204
John Grossman44a7e422012-06-21 17:29:24 -07001205 player_type playerType =
1206 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001207 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001208
1209 // create the right type of player
1210 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001211 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001212 if (player == NULL) goto Exit;
1213 if (player->hardwareOutput()) goto Exit;
1214
1215 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1216
1217 // set data source
1218 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1219
Steve Block3856b092011-10-20 11:56:00 +01001220 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001221 player->prepareAsync();
1222
Steve Block3856b092011-10-20 11:56:00 +01001223 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001224 if (cache->wait() != NO_ERROR) goto Exit;
1225
Steve Block3856b092011-10-20 11:56:00 +01001226 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227 player->start();
1228
Steve Block3856b092011-10-20 11:56:00 +01001229 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001230 cache->wait();
1231 // in case of error, return what was successfully decoded.
1232 if (cache->size() == 0) {
1233 goto Exit;
1234 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001235
1236 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1237 *pSampleRate = cache->sampleRate();
1238 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001239 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001240 ALOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001241
1242Exit:
1243 if (player != 0) player->reset();
1244 return mem;
1245}
1246
Glenn Kastene1c39622012-01-04 09:36:37 -08001247sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001248{
Steve Block3856b092011-10-20 11:56:00 +01001249 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250 sp<MemoryBase> mem;
1251 sp<MediaPlayerBase> player;
1252
John Grossman44a7e422012-06-21 17:29:24 -07001253 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1254 fd,
1255 offset,
1256 length);
Steve Block3856b092011-10-20 11:56:00 +01001257 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258
1259 // create the right type of player
1260 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001261 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 if (player == NULL) goto Exit;
1263 if (player->hardwareOutput()) goto Exit;
1264
1265 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1266
1267 // set data source
1268 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1269
Steve Block3856b092011-10-20 11:56:00 +01001270 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271 player->prepareAsync();
1272
Steve Block3856b092011-10-20 11:56:00 +01001273 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 if (cache->wait() != NO_ERROR) goto Exit;
1275
Steve Block3856b092011-10-20 11:56:00 +01001276 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277 player->start();
1278
Steve Block3856b092011-10-20 11:56:00 +01001279 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001280 cache->wait();
1281 // in case of error, return what was successfully decoded.
1282 if (cache->size() == 0) {
1283 goto Exit;
1284 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285
1286 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1287 *pSampleRate = cache->sampleRate();
1288 *pNumChannels = cache->channelCount();
1289 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001290 ALOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001291
1292Exit:
1293 if (player != 0) player->reset();
1294 ::close(fd);
1295 return mem;
1296}
1297
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001298
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299#undef LOG_TAG
1300#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001301MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001302 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001303 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001304 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001305 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001306 mSessionId(sessionId),
1307 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001308 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001309 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001310 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001311 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001312 mLeftVolume = 1.0;
1313 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001314 mPlaybackRatePermille = 1000;
1315 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001317 mAuxEffectId = 0;
1318 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 setMinBufferCount();
1320}
1321
1322MediaPlayerService::AudioOutput::~AudioOutput()
1323{
1324 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001325 delete mRecycledTrack;
1326 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001327}
1328
1329void MediaPlayerService::AudioOutput::setMinBufferCount()
1330{
1331 char value[PROPERTY_VALUE_MAX];
1332 if (property_get("ro.kernel.qemu", value, 0)) {
1333 mIsOnEmulator = true;
1334 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1335 }
1336}
1337
1338bool MediaPlayerService::AudioOutput::isOnEmulator()
1339{
1340 setMinBufferCount();
1341 return mIsOnEmulator;
1342}
1343
1344int MediaPlayerService::AudioOutput::getMinBufferCount()
1345{
1346 setMinBufferCount();
1347 return mMinBufferCount;
1348}
1349
1350ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1351{
1352 if (mTrack == 0) return NO_INIT;
1353 return mTrack->frameCount() * frameSize();
1354}
1355
1356ssize_t MediaPlayerService::AudioOutput::frameCount() const
1357{
1358 if (mTrack == 0) return NO_INIT;
1359 return mTrack->frameCount();
1360}
1361
1362ssize_t MediaPlayerService::AudioOutput::channelCount() const
1363{
1364 if (mTrack == 0) return NO_INIT;
1365 return mTrack->channelCount();
1366}
1367
1368ssize_t MediaPlayerService::AudioOutput::frameSize() const
1369{
1370 if (mTrack == 0) return NO_INIT;
1371 return mTrack->frameSize();
1372}
1373
1374uint32_t MediaPlayerService::AudioOutput::latency () const
1375{
Eric Laurentdb354e52012-03-05 17:27:11 -08001376 if (mTrack == 0) return 0;
1377 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001378}
1379
1380float MediaPlayerService::AudioOutput::msecsPerFrame() const
1381{
1382 return mMsecsPerFrame;
1383}
1384
Marco Nelissen4110c102012-03-29 09:31:28 -07001385status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001386{
1387 if (mTrack == 0) return NO_INIT;
1388 return mTrack->getPosition(position);
1389}
1390
Marco Nelissen4110c102012-03-29 09:31:28 -07001391status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1392{
1393 if (mTrack == 0) return NO_INIT;
1394 *frameswritten = mBytesWritten / frameSize();
1395 return OK;
1396}
1397
Andreas Huber20111aa2009-07-14 16:56:47 -07001398status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001399 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1400 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001401 AudioCallback cb, void *cookie,
1402 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001403{
Andreas Huber20111aa2009-07-14 16:56:47 -07001404 mCallback = cb;
1405 mCallbackCookie = cookie;
1406
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001407 // Check argument "bufferCount" against the mininum buffer count
1408 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001409 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001410 bufferCount = mMinBufferCount;
1411
1412 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001413 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1414 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001415 int afSampleRate;
1416 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001417 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001418
1419 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1420 return NO_INIT;
1421 }
1422 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1423 return NO_INIT;
1424 }
1425
1426 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001427
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001428 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001429 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001430 if (0 == channelMask) {
1431 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1432 return NO_INIT;
1433 }
1434 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001435
Andreas Huber20111aa2009-07-14 16:56:47 -07001436 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001437 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001438 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001439 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001440 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001441 mStreamType,
1442 sampleRate,
1443 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001444 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001445 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001446 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001447 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001448 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001449 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001450 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001451 } else {
1452 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001453 mStreamType,
1454 sampleRate,
1455 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001456 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001457 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001458 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001459 NULL,
1460 NULL,
1461 0,
1462 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001463 }
1464
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001465 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001466 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001467 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001468 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001469 return NO_INIT;
1470 }
1471
Marco Nelissen67295b52012-06-11 14:52:53 -07001472
1473 if (mRecycledTrack) {
1474 // check if the existing track can be reused as-is, or if a new track needs to be created.
1475
1476 bool reuse = true;
1477 if ((mCallbackData == NULL && mCallback != NULL) ||
1478 (mCallbackData != NULL && mCallback == NULL)) {
1479 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1480 ALOGV("can't chain callback and write");
1481 reuse = false;
1482 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1483 (mRecycledTrack->channelCount() != channelCount) ||
1484 (mRecycledTrack->frameCount() != t->frameCount())) {
1485 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1486 mRecycledTrack->getSampleRate(), sampleRate,
1487 mRecycledTrack->channelCount(), channelCount,
1488 mRecycledTrack->frameCount(), t->frameCount());
1489 reuse = false;
1490 } else if (flags != mFlags) {
1491 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1492 reuse = false;
1493 }
1494 if (reuse) {
1495 ALOGV("chaining to next output");
1496 close();
1497 mTrack = mRecycledTrack;
1498 mRecycledTrack = NULL;
1499 if (mCallbackData != NULL) {
1500 mCallbackData->setOutput(this);
1501 }
1502 delete t;
1503 delete newcbd;
1504 return OK;
1505 }
1506
1507 // if we're not going to reuse the track, unblock and flush it
1508 if (mCallbackData != NULL) {
1509 mCallbackData->setOutput(NULL);
1510 mCallbackData->endTrackSwitch();
1511 }
1512 mRecycledTrack->flush();
1513 delete mRecycledTrack;
1514 mRecycledTrack = NULL;
1515 delete mCallbackData;
1516 mCallbackData = NULL;
1517 close();
1518 }
1519
1520 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001521 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001522 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001523
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001524 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001525 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001526 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001527 uint32_t pos;
1528 if (t->getPosition(&pos) == OK) {
1529 mBytesWritten = uint64_t(pos) * t->frameSize();
1530 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001531 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001532
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001533 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1534 if (res != NO_ERROR) {
1535 return res;
1536 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001537 t->setAuxEffectSendLevel(mSendLevel);
1538 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001539}
1540
1541void MediaPlayerService::AudioOutput::start()
1542{
Steve Block3856b092011-10-20 11:56:00 +01001543 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001544 if (mCallbackData != NULL) {
1545 mCallbackData->endTrackSwitch();
1546 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001547 if (mTrack) {
1548 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001549 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001550 mTrack->start();
1551 }
1552}
1553
Marco Nelissen6b74d672012-02-28 16:07:44 -08001554void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1555 mNextOutput = nextOutput;
1556}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001557
1558
Marco Nelissen6b74d672012-02-28 16:07:44 -08001559void MediaPlayerService::AudioOutput::switchToNextOutput() {
1560 ALOGV("switchToNextOutput");
1561 if (mNextOutput != NULL) {
1562 if (mCallbackData != NULL) {
1563 mCallbackData->beginTrackSwitch();
1564 }
1565 delete mNextOutput->mCallbackData;
1566 mNextOutput->mCallbackData = mCallbackData;
1567 mCallbackData = NULL;
1568 mNextOutput->mRecycledTrack = mTrack;
1569 mTrack = NULL;
1570 mNextOutput->mSampleRateHz = mSampleRateHz;
1571 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001572 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001573 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001574 }
1575}
1576
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1578{
Andreas Huber20111aa2009-07-14 16:56:47 -07001579 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1580
Steve Block3856b092011-10-20 11:56:00 +01001581 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001582 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001583 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001584 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001585 return ret;
1586 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001587 return NO_INIT;
1588}
1589
1590void MediaPlayerService::AudioOutput::stop()
1591{
Steve Block3856b092011-10-20 11:56:00 +01001592 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001593 if (mTrack) mTrack->stop();
1594}
1595
1596void MediaPlayerService::AudioOutput::flush()
1597{
Steve Block3856b092011-10-20 11:56:00 +01001598 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599 if (mTrack) mTrack->flush();
1600}
1601
1602void MediaPlayerService::AudioOutput::pause()
1603{
Steve Block3856b092011-10-20 11:56:00 +01001604 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001605 if (mTrack) mTrack->pause();
1606}
1607
1608void MediaPlayerService::AudioOutput::close()
1609{
Steve Block3856b092011-10-20 11:56:00 +01001610 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001611 delete mTrack;
1612 mTrack = 0;
1613}
1614
1615void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1616{
Steve Block3856b092011-10-20 11:56:00 +01001617 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001618 mLeftVolume = left;
1619 mRightVolume = right;
1620 if (mTrack) {
1621 mTrack->setVolume(left, right);
1622 }
1623}
1624
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001625status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1626{
1627 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1628 status_t res = NO_ERROR;
1629 if (mTrack) {
1630 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1631 } else {
1632 res = NO_INIT;
1633 }
1634 mPlaybackRatePermille = ratePermille;
1635 if (mSampleRateHz != 0) {
1636 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1637 }
1638 return res;
1639}
1640
Eric Laurent2beeb502010-07-16 07:43:46 -07001641status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1642{
Steve Block3856b092011-10-20 11:56:00 +01001643 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001644 mSendLevel = level;
1645 if (mTrack) {
1646 return mTrack->setAuxEffectSendLevel(level);
1647 }
1648 return NO_ERROR;
1649}
1650
1651status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1652{
Steve Block3856b092011-10-20 11:56:00 +01001653 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001654 mAuxEffectId = effectId;
1655 if (mTrack) {
1656 return mTrack->attachAuxEffect(effectId);
1657 }
1658 return NO_ERROR;
1659}
1660
Andreas Huber20111aa2009-07-14 16:56:47 -07001661// static
1662void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001663 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001664 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001665 if (event != AudioTrack::EVENT_MORE_DATA) {
1666 return;
1667 }
1668
Marco Nelissen6b74d672012-02-28 16:07:44 -08001669 CallbackData *data = (CallbackData*)cookie;
1670 data->lock();
1671 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001672 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001673 if (me == NULL) {
1674 // no output set, likely because the track was scheduled to be reused
1675 // by another player, but the format turned out to be incompatible.
1676 data->unlock();
1677 buffer->size = 0;
1678 return;
1679 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001680
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001681 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001682 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001683
Marco Nelissen6b74d672012-02-28 16:07:44 -08001684 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001685 // We've reached EOS but the audio track is not stopped yet,
1686 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001687
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001688 memset(buffer->raw, 0, buffer->size);
1689 actualSize = buffer->size;
1690 }
1691
1692 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001693 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001694}
1695
Marco Nelissen4110c102012-03-29 09:31:28 -07001696int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001697{
1698 return mSessionId;
1699}
1700
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001701#undef LOG_TAG
1702#define LOG_TAG "AudioCache"
1703MediaPlayerService::AudioCache::AudioCache(const char* name) :
1704 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1705 mError(NO_ERROR), mCommandComplete(false)
1706{
1707 // create ashmem heap
1708 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1709}
1710
1711uint32_t MediaPlayerService::AudioCache::latency () const
1712{
1713 return 0;
1714}
1715
1716float MediaPlayerService::AudioCache::msecsPerFrame() const
1717{
1718 return mMsecsPerFrame;
1719}
1720
Marco Nelissen4110c102012-03-29 09:31:28 -07001721status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001722{
1723 if (position == 0) return BAD_VALUE;
1724 *position = mSize;
1725 return NO_ERROR;
1726}
1727
Marco Nelissen4110c102012-03-29 09:31:28 -07001728status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1729{
1730 if (written == 0) return BAD_VALUE;
1731 *written = mSize;
1732 return NO_ERROR;
1733}
1734
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001735////////////////////////////////////////////////////////////////////////////////
1736
1737struct CallbackThread : public Thread {
1738 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1739 MediaPlayerBase::AudioSink::AudioCallback cb,
1740 void *cookie);
1741
1742protected:
1743 virtual ~CallbackThread();
1744
1745 virtual bool threadLoop();
1746
1747private:
1748 wp<MediaPlayerBase::AudioSink> mSink;
1749 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1750 void *mCookie;
1751 void *mBuffer;
1752 size_t mBufferSize;
1753
1754 CallbackThread(const CallbackThread &);
1755 CallbackThread &operator=(const CallbackThread &);
1756};
1757
1758CallbackThread::CallbackThread(
1759 const wp<MediaPlayerBase::AudioSink> &sink,
1760 MediaPlayerBase::AudioSink::AudioCallback cb,
1761 void *cookie)
1762 : mSink(sink),
1763 mCallback(cb),
1764 mCookie(cookie),
1765 mBuffer(NULL),
1766 mBufferSize(0) {
1767}
1768
1769CallbackThread::~CallbackThread() {
1770 if (mBuffer) {
1771 free(mBuffer);
1772 mBuffer = NULL;
1773 }
1774}
1775
1776bool CallbackThread::threadLoop() {
1777 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1778 if (sink == NULL) {
1779 return false;
1780 }
1781
1782 if (mBuffer == NULL) {
1783 mBufferSize = sink->bufferSize();
1784 mBuffer = malloc(mBufferSize);
1785 }
1786
1787 size_t actualSize =
1788 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1789
1790 if (actualSize > 0) {
1791 sink->write(mBuffer, actualSize);
1792 }
1793
1794 return true;
1795}
1796
1797////////////////////////////////////////////////////////////////////////////////
1798
Andreas Huber20111aa2009-07-14 16:56:47 -07001799status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001800 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1801 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001802 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001803{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001804 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001805 if (mHeap->getHeapID() < 0) {
1806 return NO_INIT;
1807 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001808
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001809 mSampleRate = sampleRate;
1810 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001811 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001813
1814 if (cb != NULL) {
1815 mCallbackThread = new CallbackThread(this, cb, cookie);
1816 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001817 return NO_ERROR;
1818}
1819
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001820void MediaPlayerService::AudioCache::start() {
1821 if (mCallbackThread != NULL) {
1822 mCallbackThread->run("AudioCache callback");
1823 }
1824}
1825
1826void MediaPlayerService::AudioCache::stop() {
1827 if (mCallbackThread != NULL) {
1828 mCallbackThread->requestExitAndWait();
1829 }
1830}
1831
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001832ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1833{
Steve Block3856b092011-10-20 11:56:00 +01001834 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835 if ((buffer == 0) || (size == 0)) return size;
1836
1837 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1838 if (p == NULL) return NO_INIT;
1839 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001840 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001841 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001842 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001843 size = mHeap->getSize() - mSize;
1844 }
1845 memcpy(p, buffer, size);
1846 mSize += size;
1847 return size;
1848}
1849
1850// call with lock held
1851status_t MediaPlayerService::AudioCache::wait()
1852{
1853 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001854 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001855 mSignal.wait(mLock);
1856 }
1857 mCommandComplete = false;
1858
1859 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001860 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001861 } else {
Steve Block3856b092011-10-20 11:56:00 +01001862 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001863 }
1864 return mError;
1865}
1866
Gloria Wangb483c472011-04-11 17:23:27 -07001867void MediaPlayerService::AudioCache::notify(
1868 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001869{
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001871 AudioCache* p = static_cast<AudioCache*>(cookie);
1872
1873 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001874 switch (msg)
1875 {
1876 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001877 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001878 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001879 break;
1880 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001881 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001882 break;
1883 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001884 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001885 break;
1886 default:
Steve Block3856b092011-10-20 11:56:00 +01001887 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001888 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001889 }
1890
1891 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001892 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001893 p->mCommandComplete = true;
1894 p->mSignal.signal();
1895}
1896
Marco Nelissen4110c102012-03-29 09:31:28 -07001897int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001898{
1899 return 0;
1900}
1901
Gloria Wang7cf180c2011-02-19 18:37:57 -08001902void MediaPlayerService::addBatteryData(uint32_t params)
1903{
1904 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001905
1906 int32_t time = systemTime() / 1000000L;
1907
1908 // change audio output devices. This notification comes from AudioFlinger
1909 if ((params & kBatteryDataSpeakerOn)
1910 || (params & kBatteryDataOtherAudioDeviceOn)) {
1911
1912 int deviceOn[NUM_AUDIO_DEVICES];
1913 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1914 deviceOn[i] = 0;
1915 }
1916
1917 if ((params & kBatteryDataSpeakerOn)
1918 && (params & kBatteryDataOtherAudioDeviceOn)) {
1919 deviceOn[SPEAKER_AND_OTHER] = 1;
1920 } else if (params & kBatteryDataSpeakerOn) {
1921 deviceOn[SPEAKER] = 1;
1922 } else {
1923 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1924 }
1925
1926 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1927 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1928
1929 if (mBatteryAudio.refCount > 0) { // if playing audio
1930 if (!deviceOn[i]) {
1931 mBatteryAudio.lastTime[i] += time;
1932 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1933 mBatteryAudio.lastTime[i] = 0;
1934 } else {
1935 mBatteryAudio.lastTime[i] = 0 - time;
1936 }
1937 }
1938
1939 mBatteryAudio.deviceOn[i] = deviceOn[i];
1940 }
1941 }
1942 return;
1943 }
1944
1945 // an sudio stream is started
1946 if (params & kBatteryDataAudioFlingerStart) {
1947 // record the start time only if currently no other audio
1948 // is being played
1949 if (mBatteryAudio.refCount == 0) {
1950 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1951 if (mBatteryAudio.deviceOn[i]) {
1952 mBatteryAudio.lastTime[i] -= time;
1953 }
1954 }
1955 }
1956
1957 mBatteryAudio.refCount ++;
1958 return;
1959
1960 } else if (params & kBatteryDataAudioFlingerStop) {
1961 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001962 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001963 return;
1964 }
1965
1966 // record the stop time only if currently this is the only
1967 // audio being played
1968 if (mBatteryAudio.refCount == 1) {
1969 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1970 if (mBatteryAudio.deviceOn[i]) {
1971 mBatteryAudio.lastTime[i] += time;
1972 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1973 mBatteryAudio.lastTime[i] = 0;
1974 }
1975 }
1976 }
1977
1978 mBatteryAudio.refCount --;
1979 return;
1980 }
1981
Gloria Wang7cf180c2011-02-19 18:37:57 -08001982 int uid = IPCThreadState::self()->getCallingUid();
1983 if (uid == AID_MEDIA) {
1984 return;
1985 }
1986 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001987
1988 if (index < 0) { // create a new entry for this UID
1989 BatteryUsageInfo info;
1990 info.audioTotalTime = 0;
1991 info.videoTotalTime = 0;
1992 info.audioLastTime = 0;
1993 info.videoLastTime = 0;
1994 info.refCount = 0;
1995
Gloria Wang9ee159b2011-02-24 14:51:45 -08001996 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001997 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001998 return;
1999 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002000 }
2001
2002 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2003
2004 if (params & kBatteryDataCodecStarted) {
2005 if (params & kBatteryDataTrackAudio) {
2006 info.audioLastTime -= time;
2007 info.refCount ++;
2008 }
2009 if (params & kBatteryDataTrackVideo) {
2010 info.videoLastTime -= time;
2011 info.refCount ++;
2012 }
2013 } else {
2014 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002015 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002016 return;
2017 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002018 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002019 mBatteryData.removeItem(uid);
2020 return;
2021 }
2022
2023 if (params & kBatteryDataTrackAudio) {
2024 info.audioLastTime += time;
2025 info.refCount --;
2026 }
2027 if (params & kBatteryDataTrackVideo) {
2028 info.videoLastTime += time;
2029 info.refCount --;
2030 }
2031
2032 // no stream is being played by this UID
2033 if (info.refCount == 0) {
2034 info.audioTotalTime += info.audioLastTime;
2035 info.audioLastTime = 0;
2036 info.videoTotalTime += info.videoLastTime;
2037 info.videoLastTime = 0;
2038 }
2039 }
2040}
2041
2042status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2043 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002044
2045 // audio output devices usage
2046 int32_t time = systemTime() / 1000000L; //in ms
2047 int32_t totalTime;
2048
2049 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2050 totalTime = mBatteryAudio.totalTime[i];
2051
2052 if (mBatteryAudio.deviceOn[i]
2053 && (mBatteryAudio.lastTime[i] != 0)) {
2054 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2055 totalTime += tmpTime;
2056 }
2057
2058 reply->writeInt32(totalTime);
2059 // reset the total time
2060 mBatteryAudio.totalTime[i] = 0;
2061 }
2062
2063 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002064 BatteryUsageInfo info;
2065 int size = mBatteryData.size();
2066
2067 reply->writeInt32(size);
2068 int i = 0;
2069
2070 while (i < size) {
2071 info = mBatteryData.valueAt(i);
2072
2073 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2074 reply->writeInt32(info.audioTotalTime);
2075 reply->writeInt32(info.videoTotalTime);
2076
2077 info.audioTotalTime = 0;
2078 info.videoTotalTime = 0;
2079
2080 // remove the UID entry where no stream is being played
2081 if (info.refCount <= 0) {
2082 mBatteryData.removeItemsAt(i);
2083 size --;
2084 i --;
2085 }
2086 i++;
2087 }
2088 return NO_ERROR;
2089}
nikoa64c8c72009-07-20 15:07:26 -07002090} // namespace android