blob: 200c5613727ebac85be89b6db427469f70ebf486 [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>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080041#include <gui/Surface.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
Andreas Huber1b86fe02014-01-29 11:13:26 -080047#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070048#include <media/IRemoteDisplay.h>
49#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050#include <media/MediaPlayerInterface.h>
51#include <media/mediarecorder.h>
52#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070053#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070055#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070056#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010057#include <media/stagefright/AudioPlayer.h>
58#include <media/stagefright/foundation/ADebug.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080059
Dima Zavin64760242011-05-11 14:15:23 -070060#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070061
Gloria Wang7cf180c2011-02-19 18:37:57 -080062#include <private/android_filesystem_config.h>
63
James Dong559bf282012-03-28 10:29:14 -070064#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080065#include "MediaRecorderClient.h"
66#include "MediaPlayerService.h"
67#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070068#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080069
70#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070071#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070072#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080073#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070074
Andreas Huber20111aa2009-07-14 16:56:47 -070075#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070076
Andreas Hubered3e3e02012-03-26 11:13:27 -070077#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080078#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070079#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070080#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070081#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070082
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070083namespace {
nikoa64c8c72009-07-20 15:07:26 -070084using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070085using android::status_t;
86using android::OK;
87using android::BAD_VALUE;
88using android::NOT_ENOUGH_DATA;
89using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070090
91// Max number of entries in the filter.
92const int kMaxFilterSize = 64; // I pulled that out of thin air.
93
nikoa64c8c72009-07-20 15:07:26 -070094// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070095
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070096
97// Unmarshall a filter from a Parcel.
98// Filter format in a parcel:
99//
100// 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
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102// | number of entries (n) |
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104// | metadata type 1 |
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106// | metadata type 2 |
107// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108// ....
109// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110// | metadata type n |
111// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112//
113// @param p Parcel that should start with a filter.
114// @param[out] filter On exit contains the list of metadata type to be
115// filtered.
116// @param[out] status On exit contains the status code to be returned.
117// @return true if the parcel starts with a valid filter.
118bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700119 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700120 status_t *status)
121{
Nicolas Catania48290382009-07-10 13:53:06 -0700122 int32_t val;
123 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700124 {
Steve Block29357bc2012-01-06 19:20:56 +0000125 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 *status = NOT_ENOUGH_DATA;
127 return false;
128 }
129
Nicolas Catania48290382009-07-10 13:53:06 -0700130 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700131 {
Steve Block29357bc2012-01-06 19:20:56 +0000132 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133 *status = BAD_VALUE;
134 return false;
135 }
136
Nicolas Catania48290382009-07-10 13:53:06 -0700137 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138
139 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700140 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141
nikoa64c8c72009-07-20 15:07:26 -0700142 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143
Nicolas Catania48290382009-07-10 13:53:06 -0700144
145 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146 {
Steve Block29357bc2012-01-06 19:20:56 +0000147 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148 *status = NOT_ENOUGH_DATA;
149 return false;
150 }
151
nikoa64c8c72009-07-20 15:07:26 -0700152 const Metadata::Type *data =
153 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700154
Nicolas Catania48290382009-07-10 13:53:06 -0700155 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700156 {
Steve Block29357bc2012-01-06 19:20:56 +0000157 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700158 *status = BAD_VALUE;
159 return false;
160 }
161
162 // TODO: The stl impl of vector would be more efficient here
163 // because it degenerates into a memcpy on pod types. Try to
164 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700165 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700166 {
Nicolas Catania48290382009-07-10 13:53:06 -0700167 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700168 ++data;
169 }
170 *status = OK;
171 return true;
172}
173
Nicolas Catania48290382009-07-10 13:53:06 -0700174// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700175// @param val To be searched.
176// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700177bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178{
179 // Deal with empty and ANY right away
180 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700181 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700182
Nicolas Catania48290382009-07-10 13:53:06 -0700183 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700184}
185
186} // anonymous namespace
187
188
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800189namespace android {
190
Dave Burked681bbb2011-08-30 14:39:17 +0100191static bool checkPermission(const char* permissionString) {
192#ifndef HAVE_ANDROID_OS
193 return true;
194#endif
195 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
196 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000197 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100198 return ok;
199}
200
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800201// 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 -0800202/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
203/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
204
205void MediaPlayerService::instantiate() {
206 defaultServiceManager()->addService(
207 String16("media.player"), new MediaPlayerService());
208}
209
210MediaPlayerService::MediaPlayerService()
211{
Steve Block3856b092011-10-20 11:56:00 +0100212 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800213 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800214
215 mBatteryAudio.refCount = 0;
216 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
217 mBatteryAudio.deviceOn[i] = 0;
218 mBatteryAudio.lastTime[i] = 0;
219 mBatteryAudio.totalTime[i] = 0;
220 }
221 // speaker is on by default
222 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700223
224 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225}
226
227MediaPlayerService::~MediaPlayerService()
228{
Steve Block3856b092011-10-20 11:56:00 +0100229 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230}
231
Glenn Kastenf37971f2012-02-03 11:06:53 -0800232sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800234 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700235 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
236 wp<MediaRecorderClient> w = recorder;
237 Mutex::Autolock lock(mLock);
238 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100239 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240 return recorder;
241}
242
Gloria Wangdac6a312009-10-29 15:46:37 -0700243void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
244{
245 Mutex::Autolock lock(mLock);
246 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100247 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700248}
249
Glenn Kastenf37971f2012-02-03 11:06:53 -0800250sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800251{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800252 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100254 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255 return retriever;
256}
257
Glenn Kastenf37971f2012-02-03 11:06:53 -0800258sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100259 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800261 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700263
264 sp<Client> c = new Client(
265 this, pid, connId, client, audioSessionId,
266 IPCThreadState::self()->getCallingUid());
267
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100269 IPCThreadState::self()->getCallingUid());
270
271 wp<Client> w = c;
272 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800273 Mutex::Autolock lock(mLock);
274 mClients.add(w);
275 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800276 return c;
277}
278
Andreas Huber318ad9c2009-10-15 13:46:54 -0700279sp<IOMX> MediaPlayerService::getOMX() {
280 Mutex::Autolock autoLock(mLock);
281
282 if (mOMX.get() == NULL) {
283 mOMX = new OMX;
284 }
285
286 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700287}
288
Andreas Hubered3e3e02012-03-26 11:13:27 -0700289sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700290 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700291}
292
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800293sp<IDrm> MediaPlayerService::makeDrm() {
294 return new Drm;
295}
296
Andreas Huber279dcd82013-01-30 10:41:25 -0800297sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
298 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700299}
300
Jeff Brown2013a542012-09-04 21:38:42 -0700301sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
302 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700303 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
304 return NULL;
305 }
306
Jeff Brownced24b32012-09-05 17:48:03 -0700307 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700308}
309
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
311{
312 const size_t SIZE = 256;
313 char buffer[SIZE];
314 String8 result;
315
316 result.append(" AudioCache\n");
317 if (mHeap != 0) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000318 snprintf(buffer, 255, " heap base(%p), size(%zu), flags(%d)\n",
Eric Laurent3d00aa62013-09-24 09:53:27 -0700319 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 result.append(buffer);
321 }
Scott Fan7d409692013-04-28 10:13:54 +0800322 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%zd)\n",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
324 result.append(buffer);
325 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
326 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
327 result.append(buffer);
328 ::write(fd, result.string(), result.size());
329 return NO_ERROR;
330}
331
332status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
333{
334 const size_t SIZE = 256;
335 char buffer[SIZE];
336 String8 result;
337
338 result.append(" AudioOutput\n");
339 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
340 mStreamType, mLeftVolume, mRightVolume);
341 result.append(buffer);
342 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800343 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700345 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
346 mAuxEffectId, mSendLevel);
347 result.append(buffer);
348
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349 ::write(fd, result.string(), result.size());
350 if (mTrack != 0) {
351 mTrack->dump(fd, args);
352 }
353 return NO_ERROR;
354}
355
356status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
357{
358 const size_t SIZE = 256;
359 char buffer[SIZE];
360 String8 result;
361 result.append(" Client\n");
362 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
363 mPid, mConnId, mStatus, mLoop?"true": "false");
364 result.append(buffer);
365 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700366 if (mPlayer != NULL) {
367 mPlayer->dump(fd, args);
368 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800369 if (mAudioOutput != 0) {
370 mAudioOutput->dump(fd, args);
371 }
372 write(fd, "\n", 1);
373 return NO_ERROR;
374}
375
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
377{
378 const size_t SIZE = 256;
379 char buffer[SIZE];
380 String8 result;
381 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
382 snprintf(buffer, SIZE, "Permission Denial: "
383 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
384 IPCThreadState::self()->getCallingPid(),
385 IPCThreadState::self()->getCallingUid());
386 result.append(buffer);
387 } else {
388 Mutex::Autolock lock(mLock);
389 for (int i = 0, n = mClients.size(); i < n; ++i) {
390 sp<Client> c = mClients[i].promote();
391 if (c != 0) c->dump(fd, args);
392 }
James Dongb9141222010-07-08 11:16:11 -0700393 if (mMediaRecorderClients.size() == 0) {
394 result.append(" No media recorder client\n\n");
395 } else {
396 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
397 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700398 if (c != 0) {
399 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
400 result.append(buffer);
401 write(fd, result.string(), result.size());
402 result = "\n";
403 c->dump(fd, args);
404 }
James Dongb9141222010-07-08 11:16:11 -0700405 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700406 }
407
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700409 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410 FILE *f = fopen(buffer, "r");
411 if (f) {
412 while (!feof(f)) {
413 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700414 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700416 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417 strstr(buffer, " /system/media/")) {
418 result.append(" ");
419 result.append(buffer);
420 }
421 }
422 fclose(f);
423 } else {
424 result.append("couldn't open ");
425 result.append(buffer);
426 result.append("\n");
427 }
428
Glenn Kasten0512ab52011-05-04 17:58:57 -0700429 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 DIR *d = opendir(buffer);
431 if (d) {
432 struct dirent *ent;
433 while((ent = readdir(d)) != NULL) {
434 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700435 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 struct stat s;
437 if (lstat(buffer, &s) == 0) {
438 if ((s.st_mode & S_IFMT) == S_IFLNK) {
439 char linkto[256];
440 int len = readlink(buffer, linkto, sizeof(linkto));
441 if(len > 0) {
442 if(len > 255) {
443 linkto[252] = '.';
444 linkto[253] = '.';
445 linkto[254] = '.';
446 linkto[255] = 0;
447 } else {
448 linkto[len] = 0;
449 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700450 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700452 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 strstr(linkto, "/system/media/") == linkto) {
454 result.append(" ");
455 result.append(buffer);
456 result.append(" -> ");
457 result.append(linkto);
458 result.append("\n");
459 }
460 }
461 } else {
462 result.append(" unexpected type for ");
463 result.append(buffer);
464 result.append("\n");
465 }
466 }
467 }
468 }
469 closedir(d);
470 } else {
471 result.append("couldn't open ");
472 result.append(buffer);
473 result.append("\n");
474 }
475
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 bool dumpMem = false;
477 for (size_t i = 0; i < args.size(); i++) {
478 if (args[i] == String16("-m")) {
479 dumpMem = true;
480 }
481 }
482 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700483 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 }
486 write(fd, result.string(), result.size());
487 return NO_ERROR;
488}
489
490void MediaPlayerService::removeClient(wp<Client> client)
491{
492 Mutex::Autolock lock(mLock);
493 mClients.remove(client);
494}
495
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700496MediaPlayerService::Client::Client(
497 const sp<MediaPlayerService>& service, pid_t pid,
498 int32_t connId, const sp<IMediaPlayerClient>& client,
499 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500{
Steve Block3856b092011-10-20 11:56:00 +0100501 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 mPid = pid;
503 mConnId = connId;
504 mService = service;
505 mClient = client;
506 mLoop = false;
507 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700508 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700509 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800510 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700511
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000513 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 mAntagonizer = new Antagonizer(notify, this);
515#endif
516}
517
518MediaPlayerService::Client::~Client()
519{
Steve Block3856b092011-10-20 11:56:00 +0100520 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521 mAudioOutput.clear();
522 wp<Client> client(this);
523 disconnect();
524 mService->removeClient(client);
525}
526
527void MediaPlayerService::Client::disconnect()
528{
Steve Block3856b092011-10-20 11:56:00 +0100529 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530 // grab local reference and clear main reference to prevent future
531 // access to object
532 sp<MediaPlayerBase> p;
533 {
534 Mutex::Autolock l(mLock);
535 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800536 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800537 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700538
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 mPlayer.clear();
540
541 // clear the notification to prevent callbacks to dead client
542 // and reset the player. We assume the player will serialize
543 // access to itself if necessary.
544 if (p != 0) {
545 p->setNotifyCallback(0, 0);
546#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000547 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 mAntagonizer->kill();
549#endif
550 p->reset();
551 }
552
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700553 disconnectNativeWindow();
554
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 IPCThreadState::self()->flushCommands();
556}
557
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
559{
560 // determine if we have the right player type
561 sp<MediaPlayerBase> p = mPlayer;
562 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100563 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 p.clear();
565 }
566 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700567 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700569
Jason Simmonsdb29e522011-08-12 13:46:55 -0700570 if (p != NULL) {
571 p->setUID(mUID);
572 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700573
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 return p;
575}
576
John Grossmanc795b642012-02-22 15:38:35 -0800577sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
578 player_type playerType)
579{
580 ALOGV("player type = %d", playerType);
581
582 // create the right type of player
583 sp<MediaPlayerBase> p = createPlayer(playerType);
584 if (p == NULL) {
585 return p;
586 }
587
588 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800589 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
590 mPid);
John Grossmanc795b642012-02-22 15:38:35 -0800591 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
592 }
593
594 return p;
595}
596
597void MediaPlayerService::Client::setDataSource_post(
598 const sp<MediaPlayerBase>& p,
599 status_t status)
600{
601 ALOGV(" setDataSource");
602 mStatus = status;
603 if (mStatus != OK) {
604 ALOGE(" error: %d", mStatus);
605 return;
606 }
607
608 // Set the re-transmission endpoint if one was chosen.
609 if (mRetransmitEndpointValid) {
610 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
611 if (mStatus != NO_ERROR) {
612 ALOGE("setRetransmitEndpoint error: %d", mStatus);
613 }
614 }
615
616 if (mStatus == OK) {
617 mPlayer = p;
618 }
619}
620
Andreas Huber2db84552010-01-28 11:19:57 -0800621status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800622 const sp<IMediaHTTPService> &httpService,
623 const char *url,
624 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625{
Steve Block3856b092011-10-20 11:56:00 +0100626 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627 if (url == NULL)
628 return UNKNOWN_ERROR;
629
Dave Burked681bbb2011-08-30 14:39:17 +0100630 if ((strncmp(url, "http://", 7) == 0) ||
631 (strncmp(url, "https://", 8) == 0) ||
632 (strncmp(url, "rtsp://", 7) == 0)) {
633 if (!checkPermission("android.permission.INTERNET")) {
634 return PERMISSION_DENIED;
635 }
636 }
637
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800638 if (strncmp(url, "content://", 10) == 0) {
639 // get a filedescriptor for the content Uri and
640 // pass it to the setDataSource(fd) method
641
642 String16 url16(url);
643 int fd = android::openContentProviderFile(url16);
644 if (fd < 0)
645 {
Steve Block29357bc2012-01-06 19:20:56 +0000646 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647 return UNKNOWN_ERROR;
648 }
649 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
650 close(fd);
651 return mStatus;
652 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700653 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800654 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
655 if (p == NULL) {
656 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800657 }
658
Andreas Huber1b86fe02014-01-29 11:13:26 -0800659 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800660 return mStatus;
661 }
662}
663
664status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
665{
Steve Block3856b092011-10-20 11:56:00 +0100666 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667 struct stat sb;
668 int ret = fstat(fd, &sb);
669 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000670 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671 return UNKNOWN_ERROR;
672 }
673
Steve Block3856b092011-10-20 11:56:00 +0100674 ALOGV("st_dev = %llu", sb.st_dev);
675 ALOGV("st_mode = %u", sb.st_mode);
676 ALOGV("st_uid = %lu", sb.st_uid);
677 ALOGV("st_gid = %lu", sb.st_gid);
678 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679
680 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000681 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800682 ::close(fd);
683 return UNKNOWN_ERROR;
684 }
685 if (offset + length > sb.st_size) {
686 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100687 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800688 }
689
John Grossman44a7e422012-06-21 17:29:24 -0700690 player_type playerType = MediaPlayerFactory::getPlayerType(this,
691 fd,
692 offset,
693 length);
John Grossmanc795b642012-02-22 15:38:35 -0800694 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
695 if (p == NULL) {
696 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697 }
698
699 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800700 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800701 return mStatus;
702}
703
Andreas Hubere2b10282010-11-23 11:41:34 -0800704status_t MediaPlayerService::Client::setDataSource(
705 const sp<IStreamSource> &source) {
706 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700707 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800708 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800709 if (p == NULL) {
710 return NO_INIT;
711 }
712
Andreas Hubere2b10282010-11-23 11:41:34 -0800713 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800714 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800715 return mStatus;
716}
717
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700718void MediaPlayerService::Client::disconnectNativeWindow() {
719 if (mConnectedWindow != NULL) {
720 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
721 NATIVE_WINDOW_API_MEDIA);
722
723 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000724 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700725 strerror(-err), err);
726 }
727 }
728 mConnectedWindow.clear();
729}
730
Glenn Kasten11731182011-02-08 17:26:17 -0800731status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800732 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800733{
Andy McFadden484566c2012-12-18 09:46:54 -0800734 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800735 sp<MediaPlayerBase> p = getPlayer();
736 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700737
Andy McFadden484566c2012-12-18 09:46:54 -0800738 sp<IBinder> binder(bufferProducer == NULL ? NULL :
739 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700740 if (mConnectedWindowBinder == binder) {
741 return OK;
742 }
743
744 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800745 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700746 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700747 status_t err = native_window_api_connect(anw.get(),
748 NATIVE_WINDOW_API_MEDIA);
749
750 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000751 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700752 // Note that we must do the reset before disconnecting from the ANW.
753 // Otherwise queue/dequeue calls could be made on the disconnected
754 // ANW, which may result in errors.
755 reset();
756
757 disconnectNativeWindow();
758
759 return err;
760 }
761 }
762
Andy McFadden484566c2012-12-18 09:46:54 -0800763 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700764 // disconnecting the old one. Otherwise queue/dequeue calls could be made
765 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800766 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700767
768 disconnectNativeWindow();
769
770 mConnectedWindow = anw;
771
772 if (err == OK) {
773 mConnectedWindowBinder = binder;
774 } else {
775 disconnectNativeWindow();
776 }
777
778 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800779}
780
Nicolas Catania1d187f12009-05-12 23:25:55 -0700781status_t MediaPlayerService::Client::invoke(const Parcel& request,
782 Parcel *reply)
783{
784 sp<MediaPlayerBase> p = getPlayer();
785 if (p == NULL) return UNKNOWN_ERROR;
786 return p->invoke(request, reply);
787}
788
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700789// This call doesn't need to access the native player.
790status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
791{
792 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700793 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700794
Nicolas Catania48290382009-07-10 13:53:06 -0700795 if (unmarshallFilter(filter, &allow, &status) &&
796 unmarshallFilter(filter, &drop, &status)) {
797 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700798
799 mMetadataAllow = allow;
800 mMetadataDrop = drop;
801 }
802 return status;
803}
804
Nicolas Catania48290382009-07-10 13:53:06 -0700805status_t MediaPlayerService::Client::getMetadata(
806 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700807{
nikoa64c8c72009-07-20 15:07:26 -0700808 sp<MediaPlayerBase> player = getPlayer();
809 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700810
nikod608a812009-07-16 16:39:53 -0700811 status_t status;
812 // Placeholder for the return code, updated by the caller.
813 reply->writeInt32(-1);
814
nikoa64c8c72009-07-20 15:07:26 -0700815 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700816
817 // We don't block notifications while we fetch the data. We clear
818 // mMetadataUpdated first so we don't lose notifications happening
819 // during the rest of this call.
820 {
821 Mutex::Autolock lock(mLock);
822 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700823 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700824 }
825 mMetadataUpdated.clear();
826 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700827
nikoa64c8c72009-07-20 15:07:26 -0700828 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700829
nikoa64c8c72009-07-20 15:07:26 -0700830 metadata.appendHeader();
831 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700832
833 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700834 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000835 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700836 return status;
837 }
838
839 // FIXME: Implement filtering on the result. Not critical since
840 // filtering takes place on the update notifications already. This
841 // would be when all the metadata are fetch and a filter is set.
842
nikod608a812009-07-16 16:39:53 -0700843 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700844 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700845 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700846}
847
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800848status_t MediaPlayerService::Client::prepareAsync()
849{
Steve Block3856b092011-10-20 11:56:00 +0100850 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800851 sp<MediaPlayerBase> p = getPlayer();
852 if (p == 0) return UNKNOWN_ERROR;
853 status_t ret = p->prepareAsync();
854#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000855 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856 if (ret == NO_ERROR) mAntagonizer->start();
857#endif
858 return ret;
859}
860
861status_t MediaPlayerService::Client::start()
862{
Steve Block3856b092011-10-20 11:56:00 +0100863 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800864 sp<MediaPlayerBase> p = getPlayer();
865 if (p == 0) return UNKNOWN_ERROR;
866 p->setLooping(mLoop);
867 return p->start();
868}
869
870status_t MediaPlayerService::Client::stop()
871{
Steve Block3856b092011-10-20 11:56:00 +0100872 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800873 sp<MediaPlayerBase> p = getPlayer();
874 if (p == 0) return UNKNOWN_ERROR;
875 return p->stop();
876}
877
878status_t MediaPlayerService::Client::pause()
879{
Steve Block3856b092011-10-20 11:56:00 +0100880 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800881 sp<MediaPlayerBase> p = getPlayer();
882 if (p == 0) return UNKNOWN_ERROR;
883 return p->pause();
884}
885
886status_t MediaPlayerService::Client::isPlaying(bool* state)
887{
888 *state = false;
889 sp<MediaPlayerBase> p = getPlayer();
890 if (p == 0) return UNKNOWN_ERROR;
891 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100892 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800893 return NO_ERROR;
894}
895
896status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
897{
Steve Block3856b092011-10-20 11:56:00 +0100898 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800899 sp<MediaPlayerBase> p = getPlayer();
900 if (p == 0) return UNKNOWN_ERROR;
901 status_t ret = p->getCurrentPosition(msec);
902 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800904 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000905 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800906 }
907 return ret;
908}
909
910status_t MediaPlayerService::Client::getDuration(int *msec)
911{
Steve Block3856b092011-10-20 11:56:00 +0100912 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800913 sp<MediaPlayerBase> p = getPlayer();
914 if (p == 0) return UNKNOWN_ERROR;
915 status_t ret = p->getDuration(msec);
916 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000919 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800920 }
921 return ret;
922}
923
Marco Nelissen6b74d672012-02-28 16:07:44 -0800924status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
925 ALOGV("setNextPlayer");
926 Mutex::Autolock l(mLock);
927 sp<Client> c = static_cast<Client*>(player.get());
928 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -0700929
930 if (c != NULL) {
931 if (mAudioOutput != NULL) {
932 mAudioOutput->setNextOutput(c->mAudioOutput);
933 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
934 ALOGE("no current audio output");
935 }
936
937 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
938 mPlayer->setNextPlayer(mNextClient->getPlayer());
939 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800940 }
John Grossman5f7e55e2012-08-24 14:47:25 -0700941
Marco Nelissen6b74d672012-02-28 16:07:44 -0800942 return OK;
943}
944
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945status_t MediaPlayerService::Client::seekTo(int msec)
946{
Steve Block3856b092011-10-20 11:56:00 +0100947 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800948 sp<MediaPlayerBase> p = getPlayer();
949 if (p == 0) return UNKNOWN_ERROR;
950 return p->seekTo(msec);
951}
952
953status_t MediaPlayerService::Client::reset()
954{
Steve Block3856b092011-10-20 11:56:00 +0100955 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800956 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800957 sp<MediaPlayerBase> p = getPlayer();
958 if (p == 0) return UNKNOWN_ERROR;
959 return p->reset();
960}
961
Glenn Kastenfff6d712012-01-12 16:38:12 -0800962status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800963{
Steve Block3856b092011-10-20 11:56:00 +0100964 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800965 // TODO: for hardware output, call player instead
966 Mutex::Autolock l(mLock);
967 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
968 return NO_ERROR;
969}
970
971status_t MediaPlayerService::Client::setLooping(int loop)
972{
Steve Block3856b092011-10-20 11:56:00 +0100973 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974 mLoop = loop;
975 sp<MediaPlayerBase> p = getPlayer();
976 if (p != 0) return p->setLooping(loop);
977 return NO_ERROR;
978}
979
980status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
981{
Steve Block3856b092011-10-20 11:56:00 +0100982 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800983
984 // for hardware output, call player instead
985 sp<MediaPlayerBase> p = getPlayer();
986 {
987 Mutex::Autolock l(mLock);
988 if (p != 0 && p->hardwareOutput()) {
989 MediaPlayerHWInterface* hwp =
990 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
991 return hwp->setVolume(leftVolume, rightVolume);
992 } else {
993 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
994 return NO_ERROR;
995 }
996 }
997
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800998 return NO_ERROR;
999}
1000
Eric Laurent2beeb502010-07-16 07:43:46 -07001001status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1002{
Steve Block3856b092011-10-20 11:56:00 +01001003 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001004 Mutex::Autolock l(mLock);
1005 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1006 return NO_ERROR;
1007}
1008
1009status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1010{
Steve Block3856b092011-10-20 11:56:00 +01001011 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001012 Mutex::Autolock l(mLock);
1013 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1014 return NO_ERROR;
1015}
Nicolas Catania48290382009-07-10 13:53:06 -07001016
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001017status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001018 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001019 sp<MediaPlayerBase> p = getPlayer();
1020 if (p == 0) return UNKNOWN_ERROR;
1021 return p->setParameter(key, request);
1022}
1023
1024status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001025 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001026 sp<MediaPlayerBase> p = getPlayer();
1027 if (p == 0) return UNKNOWN_ERROR;
1028 return p->getParameter(key, reply);
1029}
1030
John Grossmanc795b642012-02-22 15:38:35 -08001031status_t MediaPlayerService::Client::setRetransmitEndpoint(
1032 const struct sockaddr_in* endpoint) {
1033
1034 if (NULL != endpoint) {
1035 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1036 uint16_t p = ntohs(endpoint->sin_port);
1037 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1038 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1039 } else {
1040 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1041 }
1042
1043 sp<MediaPlayerBase> p = getPlayer();
1044
1045 // Right now, the only valid time to set a retransmit endpoint is before
1046 // player selection has been made (since the presence or absence of a
1047 // retransmit endpoint is going to determine which player is selected during
1048 // setDataSource).
1049 if (p != 0) return INVALID_OPERATION;
1050
1051 if (NULL != endpoint) {
1052 mRetransmitEndpoint = *endpoint;
1053 mRetransmitEndpointValid = true;
1054 } else {
1055 mRetransmitEndpointValid = false;
1056 }
1057
1058 return NO_ERROR;
1059}
1060
John Grossman44a7e422012-06-21 17:29:24 -07001061status_t MediaPlayerService::Client::getRetransmitEndpoint(
1062 struct sockaddr_in* endpoint)
1063{
1064 if (NULL == endpoint)
1065 return BAD_VALUE;
1066
1067 sp<MediaPlayerBase> p = getPlayer();
1068
1069 if (p != NULL)
1070 return p->getRetransmitEndpoint(endpoint);
1071
1072 if (!mRetransmitEndpointValid)
1073 return NO_INIT;
1074
1075 *endpoint = mRetransmitEndpoint;
1076
1077 return NO_ERROR;
1078}
1079
Gloria Wangb483c472011-04-11 17:23:27 -07001080void MediaPlayerService::Client::notify(
1081 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001082{
1083 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001084 if (client == NULL) {
1085 return;
1086 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001087
James Dongb8a98252012-08-26 16:13:03 -07001088 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001089 {
1090 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001091 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001092 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001093 if (client->mAudioOutput != NULL)
1094 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001095 client->mNextClient->start();
1096 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1097 }
1098 }
1099
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001100 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001101 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001102 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001103
1104 if(client->shouldDropMetadata(metadata_type)) {
1105 return;
1106 }
1107
1108 // Update the list of metadata that have changed. getMetadata
1109 // also access mMetadataUpdated and clears it.
1110 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001111 }
James Dongb8a98252012-08-26 16:13:03 -07001112
1113 if (c != NULL) {
1114 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1115 c->notify(msg, ext1, ext2, obj);
1116 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001117}
1118
Nicolas Catania48290382009-07-10 13:53:06 -07001119
nikoa64c8c72009-07-20 15:07:26 -07001120bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001121{
Nicolas Catania48290382009-07-10 13:53:06 -07001122 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001123
Nicolas Catania48290382009-07-10 13:53:06 -07001124 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001125 return true;
1126 }
1127
Nicolas Catania48290382009-07-10 13:53:06 -07001128 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001129 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001130 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001131 return true;
1132 }
1133}
1134
Nicolas Catania48290382009-07-10 13:53:06 -07001135
nikoa64c8c72009-07-20 15:07:26 -07001136void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001137 Mutex::Autolock lock(mLock);
1138 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1139 mMetadataUpdated.add(metadata_type);
1140 }
1141}
1142
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001143#if CALLBACK_ANTAGONIZER
1144const int Antagonizer::interval = 10000; // 10 msecs
1145
1146Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1147 mExit(false), mActive(false), mClient(client), mCb(cb)
1148{
1149 createThread(callbackThread, this);
1150}
1151
1152void Antagonizer::kill()
1153{
1154 Mutex::Autolock _l(mLock);
1155 mActive = false;
1156 mExit = true;
1157 mCondition.wait(mLock);
1158}
1159
1160int Antagonizer::callbackThread(void* user)
1161{
Steve Blockb8a80522011-12-20 16:23:08 +00001162 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001163 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1164 while (!p->mExit) {
1165 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001166 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 p->mCb(p->mClient, 0, 0, 0);
1168 }
1169 usleep(interval);
1170 }
1171 Mutex::Autolock _l(p->mLock);
1172 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001173 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001174 return 0;
1175}
1176#endif
1177
Andreas Huber1b86fe02014-01-29 11:13:26 -08001178status_t MediaPlayerService::decode(
1179 const sp<IMediaHTTPService> &httpService,
1180 const char* url,
1181 uint32_t *pSampleRate,
1182 int* pNumChannels,
1183 audio_format_t* pFormat,
1184 const sp<IMemoryHeap>& heap,
1185 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001186{
Steve Block3856b092011-10-20 11:56:00 +01001187 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001189 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001190
1191 // Protect our precious, precious DRMd ringtones by only allowing
1192 // decoding of http, but not filesystem paths or content Uris.
1193 // If the application wants to decode those, it should open a
1194 // filedescriptor for them and use that.
1195 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001196 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001197 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001198 }
1199
John Grossman44a7e422012-06-21 17:29:24 -07001200 player_type playerType =
1201 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001202 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203
1204 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001205 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001206 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 if (player == NULL) goto Exit;
1208 if (player->hardwareOutput()) goto Exit;
1209
1210 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1211
1212 // set data source
Andreas Huber1b86fe02014-01-29 11:13:26 -08001213 if (player->setDataSource(httpService, url) != NO_ERROR) goto Exit;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 player->prepareAsync();
1217
Steve Block3856b092011-10-20 11:56:00 +01001218 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001219 if (cache->wait() != NO_ERROR) goto Exit;
1220
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222 player->start();
1223
Steve Block3856b092011-10-20 11:56:00 +01001224 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001225 cache->wait();
1226 // in case of error, return what was successfully decoded.
1227 if (cache->size() == 0) {
1228 goto Exit;
1229 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230
Eric Laurent3d00aa62013-09-24 09:53:27 -07001231 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001232 *pSampleRate = cache->sampleRate();
1233 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001234 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001235 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1236 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1237 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001238
1239Exit:
1240 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001241 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001242}
1243
Eric Laurent3d00aa62013-09-24 09:53:27 -07001244status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1245 uint32_t *pSampleRate, int* pNumChannels,
1246 audio_format_t* pFormat,
1247 const sp<IMemoryHeap>& heap, size_t *pSize)
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<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001251 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001252
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
Eric Laurent3d00aa62013-09-24 09:53:27 -07001260 sp<AudioCache> cache = new AudioCache(heap);
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
Eric Laurent3d00aa62013-09-24 09:53:27 -07001286 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287 *pSampleRate = cache->sampleRate();
1288 *pNumChannels = cache->channelCount();
1289 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001290 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1291 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1292 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293
1294Exit:
1295 if (player != 0) player->reset();
1296 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001297 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298}
1299
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001300
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301#undef LOG_TAG
1302#define LOG_TAG "AudioSink"
Marco Nelissend457c972014-02-11 08:47:07 -08001303MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid)
Andreas Huber20111aa2009-07-14 16:56:47 -07001304 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001305 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001306 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001307 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001308 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001309 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001310 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001311 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001312 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001313 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001314 mLeftVolume = 1.0;
1315 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001316 mPlaybackRatePermille = 1000;
1317 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001318 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001319 mAuxEffectId = 0;
1320 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321 setMinBufferCount();
1322}
1323
1324MediaPlayerService::AudioOutput::~AudioOutput()
1325{
1326 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001327 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001328}
1329
1330void MediaPlayerService::AudioOutput::setMinBufferCount()
1331{
1332 char value[PROPERTY_VALUE_MAX];
1333 if (property_get("ro.kernel.qemu", value, 0)) {
1334 mIsOnEmulator = true;
1335 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1336 }
1337}
1338
1339bool MediaPlayerService::AudioOutput::isOnEmulator()
1340{
1341 setMinBufferCount();
1342 return mIsOnEmulator;
1343}
1344
1345int MediaPlayerService::AudioOutput::getMinBufferCount()
1346{
1347 setMinBufferCount();
1348 return mMinBufferCount;
1349}
1350
1351ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1352{
1353 if (mTrack == 0) return NO_INIT;
1354 return mTrack->frameCount() * frameSize();
1355}
1356
1357ssize_t MediaPlayerService::AudioOutput::frameCount() const
1358{
1359 if (mTrack == 0) return NO_INIT;
1360 return mTrack->frameCount();
1361}
1362
1363ssize_t MediaPlayerService::AudioOutput::channelCount() const
1364{
1365 if (mTrack == 0) return NO_INIT;
1366 return mTrack->channelCount();
1367}
1368
1369ssize_t MediaPlayerService::AudioOutput::frameSize() const
1370{
1371 if (mTrack == 0) return NO_INIT;
1372 return mTrack->frameSize();
1373}
1374
1375uint32_t MediaPlayerService::AudioOutput::latency () const
1376{
Eric Laurentdb354e52012-03-05 17:27:11 -08001377 if (mTrack == 0) return 0;
1378 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001379}
1380
1381float MediaPlayerService::AudioOutput::msecsPerFrame() const
1382{
1383 return mMsecsPerFrame;
1384}
1385
Marco Nelissen4110c102012-03-29 09:31:28 -07001386status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001387{
1388 if (mTrack == 0) return NO_INIT;
1389 return mTrack->getPosition(position);
1390}
1391
Marco Nelissen4110c102012-03-29 09:31:28 -07001392status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1393{
1394 if (mTrack == 0) return NO_INIT;
1395 *frameswritten = mBytesWritten / frameSize();
1396 return OK;
1397}
1398
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001399status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1400{
1401 if (mTrack == 0) return NO_INIT;
1402 return mTrack->setParameters(keyValuePairs);
1403}
1404
1405String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1406{
1407 if (mTrack == 0) return String8::empty();
1408 return mTrack->getParameters(keys);
1409}
1410
1411void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1412{
1413 ALOGV("deleteRecycledTrack");
1414
1415 if (mRecycledTrack != 0) {
1416
1417 if (mCallbackData != NULL) {
1418 mCallbackData->setOutput(NULL);
1419 mCallbackData->endTrackSwitch();
1420 }
1421
1422 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1423 mRecycledTrack->flush();
1424 }
1425 // An offloaded track isn't flushed because the STREAM_END is reported
1426 // slightly prematurely to allow time for the gapless track switch
1427 // but this means that if we decide not to recycle the track there
1428 // could be a small amount of residual data still playing. We leave
1429 // AudioFlinger to drain the track.
1430
1431 mRecycledTrack.clear();
1432 delete mCallbackData;
1433 mCallbackData = NULL;
1434 close();
1435 }
1436}
1437
Andreas Huber20111aa2009-07-14 16:56:47 -07001438status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001439 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1440 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001441 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001442 audio_output_flags_t flags,
1443 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444{
Andreas Huber20111aa2009-07-14 16:56:47 -07001445 mCallback = cb;
1446 mCallbackCookie = cookie;
1447
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001448 // Check argument "bufferCount" against the mininum buffer count
1449 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001450 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001451 bufferCount = mMinBufferCount;
1452
1453 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001454 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1455 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001456 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001457 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001458 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001459
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001460 // offloading is only supported in callback mode for now.
1461 // offloadInfo must be present if offload flag is set
1462 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1463 ((cb == NULL) || (offloadInfo == NULL))) {
1464 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001465 }
1466
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001467 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1468 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1469 } else {
1470 uint32_t afSampleRate;
1471 size_t afFrameCount;
1472
1473 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1474 return NO_INIT;
1475 }
1476 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1477 return NO_INIT;
1478 }
1479
1480 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1481 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001482
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001483 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001484 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001485 if (0 == channelMask) {
1486 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1487 return NO_INIT;
1488 }
1489 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001490
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001491 // Check whether we can recycle the track
1492 bool reuse = false;
1493 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001494
Glenn Kasten2799d742013-05-30 14:33:29 -07001495 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001496 // check whether we are switching between two offloaded tracks
1497 bothOffloaded = (flags & mRecycledTrack->getFlags()
1498 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001499
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001500 // check if the existing track can be reused as-is, or if a new track needs to be created.
1501 reuse = true;
1502
Marco Nelissen67295b52012-06-11 14:52:53 -07001503 if ((mCallbackData == NULL && mCallback != NULL) ||
1504 (mCallbackData != NULL && mCallback == NULL)) {
1505 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1506 ALOGV("can't chain callback and write");
1507 reuse = false;
1508 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001509 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1510 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001511 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001512 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001513 reuse = false;
1514 } else if (flags != mFlags) {
1515 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1516 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001517 } else if (mRecycledTrack->format() != format) {
1518 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001519 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001520 } else {
1521 ALOGV("no track available to recycle");
1522 }
1523
1524 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1525
1526 // If we can't recycle and both tracks are offloaded
1527 // we must close the previous output before opening a new one
1528 if (bothOffloaded && !reuse) {
1529 ALOGV("both offloaded and not recycling");
1530 deleteRecycledTrack();
1531 }
1532
1533 sp<AudioTrack> t;
1534 CallbackData *newcbd = NULL;
1535
1536 // We don't attempt to create a new track if we are recycling an
1537 // offloaded track. But, if we are recycling a non-offloaded or we
1538 // are switching where one is offloaded and one isn't then we create
1539 // the new track in advance so that we can read additional stream info
1540
1541 if (!(reuse && bothOffloaded)) {
1542 ALOGV("creating new AudioTrack");
1543
1544 if (mCallback != NULL) {
1545 newcbd = new CallbackData(this);
1546 t = new AudioTrack(
1547 mStreamType,
1548 sampleRate,
1549 format,
1550 channelMask,
1551 frameCount,
1552 flags,
1553 CallbackWrapper,
1554 newcbd,
1555 0, // notification frames
1556 mSessionId,
1557 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001558 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001559 mUid,
1560 mPid);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001561 } else {
1562 t = new AudioTrack(
1563 mStreamType,
1564 sampleRate,
1565 format,
1566 channelMask,
1567 frameCount,
1568 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001569 NULL, // callback
1570 NULL, // user data
1571 0, // notification frames
1572 mSessionId,
1573 AudioTrack::TRANSFER_DEFAULT,
1574 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001575 mUid,
1576 mPid);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001577 }
1578
1579 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1580 ALOGE("Unable to create audio track");
1581 delete newcbd;
1582 return NO_INIT;
1583 }
1584 }
1585
1586 if (reuse) {
1587 CHECK(mRecycledTrack != NULL);
1588
1589 if (!bothOffloaded) {
1590 if (mRecycledTrack->frameCount() != t->frameCount()) {
1591 ALOGV("framecount differs: %u/%u frames",
1592 mRecycledTrack->frameCount(), t->frameCount());
1593 reuse = false;
1594 }
1595 }
1596
Marco Nelissen67295b52012-06-11 14:52:53 -07001597 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001598 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001599 close();
1600 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001601 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001602 if (mCallbackData != NULL) {
1603 mCallbackData->setOutput(this);
1604 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001605 delete newcbd;
1606 return OK;
1607 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001608 }
1609
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001610 // we're not going to reuse the track, unblock and flush it
1611 // this was done earlier if both tracks are offloaded
1612 if (!bothOffloaded) {
1613 deleteRecycledTrack();
1614 }
1615
1616 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1617
Marco Nelissen67295b52012-06-11 14:52:53 -07001618 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001619 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001620 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001621
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001622 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001623 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001624 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001625 uint32_t pos;
1626 if (t->getPosition(&pos) == OK) {
1627 mBytesWritten = uint64_t(pos) * t->frameSize();
1628 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001629 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001630
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001631 status_t res = NO_ERROR;
1632 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1633 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1634 if (res == NO_ERROR) {
1635 t->setAuxEffectSendLevel(mSendLevel);
1636 res = t->attachAuxEffect(mAuxEffectId);
1637 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001638 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001639 ALOGV("open() DONE status %d", res);
1640 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001641}
1642
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001643status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001644{
Steve Block3856b092011-10-20 11:56:00 +01001645 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001646 if (mCallbackData != NULL) {
1647 mCallbackData->endTrackSwitch();
1648 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001649 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001650 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001651 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001652 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001653 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001654 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001655}
1656
Marco Nelissen6b74d672012-02-28 16:07:44 -08001657void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1658 mNextOutput = nextOutput;
1659}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001660
1661
Marco Nelissen6b74d672012-02-28 16:07:44 -08001662void MediaPlayerService::AudioOutput::switchToNextOutput() {
1663 ALOGV("switchToNextOutput");
1664 if (mNextOutput != NULL) {
1665 if (mCallbackData != NULL) {
1666 mCallbackData->beginTrackSwitch();
1667 }
1668 delete mNextOutput->mCallbackData;
1669 mNextOutput->mCallbackData = mCallbackData;
1670 mCallbackData = NULL;
1671 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001672 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001673 mNextOutput->mSampleRateHz = mSampleRateHz;
1674 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001675 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001676 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001677 }
1678}
1679
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001680ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1681{
Andreas Huber20111aa2009-07-14 16:56:47 -07001682 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1683
Steve Block3856b092011-10-20 11:56:00 +01001684 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001685 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001686 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001687 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001688 return ret;
1689 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001690 return NO_INIT;
1691}
1692
1693void MediaPlayerService::AudioOutput::stop()
1694{
Steve Block3856b092011-10-20 11:56:00 +01001695 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001696 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001697}
1698
1699void MediaPlayerService::AudioOutput::flush()
1700{
Steve Block3856b092011-10-20 11:56:00 +01001701 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001702 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001703}
1704
1705void MediaPlayerService::AudioOutput::pause()
1706{
Steve Block3856b092011-10-20 11:56:00 +01001707 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001708 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001709}
1710
1711void MediaPlayerService::AudioOutput::close()
1712{
Steve Block3856b092011-10-20 11:56:00 +01001713 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001714 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001715}
1716
1717void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1718{
Steve Block3856b092011-10-20 11:56:00 +01001719 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001720 mLeftVolume = left;
1721 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001722 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001723 mTrack->setVolume(left, right);
1724 }
1725}
1726
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001727status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1728{
1729 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1730 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001731 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001732 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1733 } else {
1734 res = NO_INIT;
1735 }
1736 mPlaybackRatePermille = ratePermille;
1737 if (mSampleRateHz != 0) {
1738 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1739 }
1740 return res;
1741}
1742
Eric Laurent2beeb502010-07-16 07:43:46 -07001743status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1744{
Steve Block3856b092011-10-20 11:56:00 +01001745 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001746 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001747 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001748 return mTrack->setAuxEffectSendLevel(level);
1749 }
1750 return NO_ERROR;
1751}
1752
1753status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1754{
Steve Block3856b092011-10-20 11:56:00 +01001755 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001756 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001757 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001758 return mTrack->attachAuxEffect(effectId);
1759 }
1760 return NO_ERROR;
1761}
1762
Andreas Huber20111aa2009-07-14 16:56:47 -07001763// static
1764void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001765 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001766 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001767 CallbackData *data = (CallbackData*)cookie;
1768 data->lock();
1769 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001770 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001771 if (me == NULL) {
1772 // no output set, likely because the track was scheduled to be reused
1773 // by another player, but the format turned out to be incompatible.
1774 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001775 if (buffer != NULL) {
1776 buffer->size = 0;
1777 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001778 return;
1779 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001780
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001781 switch(event) {
1782 case AudioTrack::EVENT_MORE_DATA: {
1783 size_t actualSize = (*me->mCallback)(
1784 me, buffer->raw, buffer->size, me->mCallbackCookie,
1785 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001786
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001787 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
1788 // We've reached EOS but the audio track is not stopped yet,
1789 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001790
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001791 memset(buffer->raw, 0, buffer->size);
1792 actualSize = buffer->size;
1793 }
1794
1795 buffer->size = actualSize;
1796 } break;
1797
1798
1799 case AudioTrack::EVENT_STREAM_END:
1800 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1801 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1802 me->mCallbackCookie, CB_EVENT_STREAM_END);
1803 break;
1804
1805 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1806 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1807 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1808 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1809 break;
1810
1811 default:
1812 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001813 }
1814
Marco Nelissen6b74d672012-02-28 16:07:44 -08001815 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001816}
1817
Marco Nelissen4110c102012-03-29 09:31:28 -07001818int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001819{
1820 return mSessionId;
1821}
1822
Eric Laurent6f59db12013-07-26 17:16:50 -07001823uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1824{
1825 if (mTrack == 0) return 0;
1826 return mTrack->getSampleRate();
1827}
1828
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829#undef LOG_TAG
1830#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001831MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1832 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1833 mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001834{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835}
1836
1837uint32_t MediaPlayerService::AudioCache::latency () const
1838{
1839 return 0;
1840}
1841
1842float MediaPlayerService::AudioCache::msecsPerFrame() const
1843{
1844 return mMsecsPerFrame;
1845}
1846
Marco Nelissen4110c102012-03-29 09:31:28 -07001847status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001848{
1849 if (position == 0) return BAD_VALUE;
1850 *position = mSize;
1851 return NO_ERROR;
1852}
1853
Marco Nelissen4110c102012-03-29 09:31:28 -07001854status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1855{
1856 if (written == 0) return BAD_VALUE;
1857 *written = mSize;
1858 return NO_ERROR;
1859}
1860
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001861////////////////////////////////////////////////////////////////////////////////
1862
1863struct CallbackThread : public Thread {
1864 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1865 MediaPlayerBase::AudioSink::AudioCallback cb,
1866 void *cookie);
1867
1868protected:
1869 virtual ~CallbackThread();
1870
1871 virtual bool threadLoop();
1872
1873private:
1874 wp<MediaPlayerBase::AudioSink> mSink;
1875 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1876 void *mCookie;
1877 void *mBuffer;
1878 size_t mBufferSize;
1879
1880 CallbackThread(const CallbackThread &);
1881 CallbackThread &operator=(const CallbackThread &);
1882};
1883
1884CallbackThread::CallbackThread(
1885 const wp<MediaPlayerBase::AudioSink> &sink,
1886 MediaPlayerBase::AudioSink::AudioCallback cb,
1887 void *cookie)
1888 : mSink(sink),
1889 mCallback(cb),
1890 mCookie(cookie),
1891 mBuffer(NULL),
1892 mBufferSize(0) {
1893}
1894
1895CallbackThread::~CallbackThread() {
1896 if (mBuffer) {
1897 free(mBuffer);
1898 mBuffer = NULL;
1899 }
1900}
1901
1902bool CallbackThread::threadLoop() {
1903 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1904 if (sink == NULL) {
1905 return false;
1906 }
1907
1908 if (mBuffer == NULL) {
1909 mBufferSize = sink->bufferSize();
1910 mBuffer = malloc(mBufferSize);
1911 }
1912
1913 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001914 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1915 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001916
1917 if (actualSize > 0) {
1918 sink->write(mBuffer, actualSize);
1919 }
1920
1921 return true;
1922}
1923
1924////////////////////////////////////////////////////////////////////////////////
1925
Andreas Huber20111aa2009-07-14 16:56:47 -07001926status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001927 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1928 audio_format_t format, int bufferCount,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001929 AudioCallback cb, void *cookie, audio_output_flags_t flags,
1930 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001931{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001932 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001933 if (mHeap->getHeapID() < 0) {
1934 return NO_INIT;
1935 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001936
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001937 mSampleRate = sampleRate;
1938 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001939 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001940 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001941
1942 if (cb != NULL) {
1943 mCallbackThread = new CallbackThread(this, cb, cookie);
1944 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001945 return NO_ERROR;
1946}
1947
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001948status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001949 if (mCallbackThread != NULL) {
1950 mCallbackThread->run("AudioCache callback");
1951 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001952 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001953}
1954
1955void MediaPlayerService::AudioCache::stop() {
1956 if (mCallbackThread != NULL) {
1957 mCallbackThread->requestExitAndWait();
1958 }
1959}
1960
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001961ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1962{
Steve Block3856b092011-10-20 11:56:00 +01001963 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001964 if ((buffer == 0) || (size == 0)) return size;
1965
1966 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1967 if (p == NULL) return NO_INIT;
1968 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001969 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001970 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001971 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001972 size = mHeap->getSize() - mSize;
1973 }
1974 memcpy(p, buffer, size);
1975 mSize += size;
1976 return size;
1977}
1978
1979// call with lock held
1980status_t MediaPlayerService::AudioCache::wait()
1981{
1982 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001983 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001984 mSignal.wait(mLock);
1985 }
1986 mCommandComplete = false;
1987
1988 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001989 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001990 } else {
Steve Block3856b092011-10-20 11:56:00 +01001991 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001992 }
1993 return mError;
1994}
1995
Gloria Wangb483c472011-04-11 17:23:27 -07001996void MediaPlayerService::AudioCache::notify(
1997 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001998{
Steve Block3856b092011-10-20 11:56:00 +01001999 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002000 AudioCache* p = static_cast<AudioCache*>(cookie);
2001
2002 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002003 switch (msg)
2004 {
2005 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002006 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002007 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08002008 break;
2009 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002010 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002011 break;
2012 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002013 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002014 break;
2015 default:
Steve Block3856b092011-10-20 11:56:00 +01002016 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002017 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002018 }
2019
2020 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002021 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002022 p->mCommandComplete = true;
2023 p->mSignal.signal();
2024}
2025
Marco Nelissen4110c102012-03-29 09:31:28 -07002026int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002027{
2028 return 0;
2029}
2030
Eric Laurent6f59db12013-07-26 17:16:50 -07002031uint32_t MediaPlayerService::AudioCache::getSampleRate() const
2032{
2033 if (mMsecsPerFrame == 0) {
2034 return 0;
2035 }
2036 return (uint32_t)(1.e3 / mMsecsPerFrame);
2037}
2038
Gloria Wang7cf180c2011-02-19 18:37:57 -08002039void MediaPlayerService::addBatteryData(uint32_t params)
2040{
2041 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002042
2043 int32_t time = systemTime() / 1000000L;
2044
2045 // change audio output devices. This notification comes from AudioFlinger
2046 if ((params & kBatteryDataSpeakerOn)
2047 || (params & kBatteryDataOtherAudioDeviceOn)) {
2048
2049 int deviceOn[NUM_AUDIO_DEVICES];
2050 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2051 deviceOn[i] = 0;
2052 }
2053
2054 if ((params & kBatteryDataSpeakerOn)
2055 && (params & kBatteryDataOtherAudioDeviceOn)) {
2056 deviceOn[SPEAKER_AND_OTHER] = 1;
2057 } else if (params & kBatteryDataSpeakerOn) {
2058 deviceOn[SPEAKER] = 1;
2059 } else {
2060 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2061 }
2062
2063 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2064 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2065
2066 if (mBatteryAudio.refCount > 0) { // if playing audio
2067 if (!deviceOn[i]) {
2068 mBatteryAudio.lastTime[i] += time;
2069 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2070 mBatteryAudio.lastTime[i] = 0;
2071 } else {
2072 mBatteryAudio.lastTime[i] = 0 - time;
2073 }
2074 }
2075
2076 mBatteryAudio.deviceOn[i] = deviceOn[i];
2077 }
2078 }
2079 return;
2080 }
2081
2082 // an sudio stream is started
2083 if (params & kBatteryDataAudioFlingerStart) {
2084 // record the start time only if currently no other audio
2085 // is being played
2086 if (mBatteryAudio.refCount == 0) {
2087 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2088 if (mBatteryAudio.deviceOn[i]) {
2089 mBatteryAudio.lastTime[i] -= time;
2090 }
2091 }
2092 }
2093
2094 mBatteryAudio.refCount ++;
2095 return;
2096
2097 } else if (params & kBatteryDataAudioFlingerStop) {
2098 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002099 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002100 return;
2101 }
2102
2103 // record the stop time only if currently this is the only
2104 // audio being played
2105 if (mBatteryAudio.refCount == 1) {
2106 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2107 if (mBatteryAudio.deviceOn[i]) {
2108 mBatteryAudio.lastTime[i] += time;
2109 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2110 mBatteryAudio.lastTime[i] = 0;
2111 }
2112 }
2113 }
2114
2115 mBatteryAudio.refCount --;
2116 return;
2117 }
2118
Gloria Wang7cf180c2011-02-19 18:37:57 -08002119 int uid = IPCThreadState::self()->getCallingUid();
2120 if (uid == AID_MEDIA) {
2121 return;
2122 }
2123 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002124
2125 if (index < 0) { // create a new entry for this UID
2126 BatteryUsageInfo info;
2127 info.audioTotalTime = 0;
2128 info.videoTotalTime = 0;
2129 info.audioLastTime = 0;
2130 info.videoLastTime = 0;
2131 info.refCount = 0;
2132
Gloria Wang9ee159b2011-02-24 14:51:45 -08002133 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002134 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002135 return;
2136 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002137 }
2138
2139 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2140
2141 if (params & kBatteryDataCodecStarted) {
2142 if (params & kBatteryDataTrackAudio) {
2143 info.audioLastTime -= time;
2144 info.refCount ++;
2145 }
2146 if (params & kBatteryDataTrackVideo) {
2147 info.videoLastTime -= time;
2148 info.refCount ++;
2149 }
2150 } else {
2151 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002152 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002153 return;
2154 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002155 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002156 mBatteryData.removeItem(uid);
2157 return;
2158 }
2159
2160 if (params & kBatteryDataTrackAudio) {
2161 info.audioLastTime += time;
2162 info.refCount --;
2163 }
2164 if (params & kBatteryDataTrackVideo) {
2165 info.videoLastTime += time;
2166 info.refCount --;
2167 }
2168
2169 // no stream is being played by this UID
2170 if (info.refCount == 0) {
2171 info.audioTotalTime += info.audioLastTime;
2172 info.audioLastTime = 0;
2173 info.videoTotalTime += info.videoLastTime;
2174 info.videoLastTime = 0;
2175 }
2176 }
2177}
2178
2179status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2180 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002181
2182 // audio output devices usage
2183 int32_t time = systemTime() / 1000000L; //in ms
2184 int32_t totalTime;
2185
2186 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2187 totalTime = mBatteryAudio.totalTime[i];
2188
2189 if (mBatteryAudio.deviceOn[i]
2190 && (mBatteryAudio.lastTime[i] != 0)) {
2191 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2192 totalTime += tmpTime;
2193 }
2194
2195 reply->writeInt32(totalTime);
2196 // reset the total time
2197 mBatteryAudio.totalTime[i] = 0;
2198 }
2199
2200 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002201 BatteryUsageInfo info;
2202 int size = mBatteryData.size();
2203
2204 reply->writeInt32(size);
2205 int i = 0;
2206
2207 while (i < size) {
2208 info = mBatteryData.valueAt(i);
2209
2210 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2211 reply->writeInt32(info.audioTotalTime);
2212 reply->writeInt32(info.videoTotalTime);
2213
2214 info.audioTotalTime = 0;
2215 info.videoTotalTime = 0;
2216
2217 // remove the UID entry where no stream is being played
2218 if (info.refCount <= 0) {
2219 mBatteryData.removeItemsAt(i);
2220 size --;
2221 i --;
2222 }
2223 i++;
2224 }
2225 return NO_ERROR;
2226}
nikoa64c8c72009-07-20 15:07:26 -07002227} // namespace android