blob: c3dbc1bb5d424a72132eec7b201f18fcfa19cb20 [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
Jeff Brown2013a542012-09-04 21:38:42 -070047#include <media/IRemoteDisplay.h>
48#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049#include <media/MediaPlayerInterface.h>
50#include <media/mediarecorder.h>
51#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070052#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070054#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070055#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010056#include <media/stagefright/AudioPlayer.h>
57#include <media/stagefright/foundation/ADebug.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080058
Dima Zavin64760242011-05-11 14:15:23 -070059#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070060
Gloria Wang7cf180c2011-02-19 18:37:57 -080061#include <private/android_filesystem_config.h>
62
James Dong559bf282012-03-28 10:29:14 -070063#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064#include "MediaRecorderClient.h"
65#include "MediaPlayerService.h"
66#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070067#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080068
69#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070070#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070071#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080072#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070073
Andreas Huber20111aa2009-07-14 16:56:47 -070074#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070075
Andreas Hubered3e3e02012-03-26 11:13:27 -070076#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080077#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070078#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070079#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070080#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070081
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070082namespace {
nikoa64c8c72009-07-20 15:07:26 -070083using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070084using android::status_t;
85using android::OK;
86using android::BAD_VALUE;
87using android::NOT_ENOUGH_DATA;
88using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070089
90// Max number of entries in the filter.
91const int kMaxFilterSize = 64; // I pulled that out of thin air.
92
nikoa64c8c72009-07-20 15:07:26 -070093// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070094
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070095
96// Unmarshall a filter from a Parcel.
97// Filter format in a parcel:
98//
99// 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
100// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101// | number of entries (n) |
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103// | metadata type 1 |
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | metadata type 2 |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// ....
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109// | metadata type n |
110// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111//
112// @param p Parcel that should start with a filter.
113// @param[out] filter On exit contains the list of metadata type to be
114// filtered.
115// @param[out] status On exit contains the status code to be returned.
116// @return true if the parcel starts with a valid filter.
117bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700118 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700119 status_t *status)
120{
Nicolas Catania48290382009-07-10 13:53:06 -0700121 int32_t val;
122 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 {
Steve Block29357bc2012-01-06 19:20:56 +0000124 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700125 *status = NOT_ENOUGH_DATA;
126 return false;
127 }
128
Nicolas Catania48290382009-07-10 13:53:06 -0700129 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700130 {
Steve Block29357bc2012-01-06 19:20:56 +0000131 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700132 *status = BAD_VALUE;
133 return false;
134 }
135
Nicolas Catania48290382009-07-10 13:53:06 -0700136 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700137
138 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700139 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140
nikoa64c8c72009-07-20 15:07:26 -0700141 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700142
Nicolas Catania48290382009-07-10 13:53:06 -0700143
144 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145 {
Steve Block29357bc2012-01-06 19:20:56 +0000146 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700147 *status = NOT_ENOUGH_DATA;
148 return false;
149 }
150
nikoa64c8c72009-07-20 15:07:26 -0700151 const Metadata::Type *data =
152 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700153
Nicolas Catania48290382009-07-10 13:53:06 -0700154 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700155 {
Steve Block29357bc2012-01-06 19:20:56 +0000156 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157 *status = BAD_VALUE;
158 return false;
159 }
160
161 // TODO: The stl impl of vector would be more efficient here
162 // because it degenerates into a memcpy on pod types. Try to
163 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700164 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700165 {
Nicolas Catania48290382009-07-10 13:53:06 -0700166 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700167 ++data;
168 }
169 *status = OK;
170 return true;
171}
172
Nicolas Catania48290382009-07-10 13:53:06 -0700173// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700174// @param val To be searched.
175// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700176bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700177{
178 // Deal with empty and ANY right away
179 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700180 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700181
Nicolas Catania48290382009-07-10 13:53:06 -0700182 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700183}
184
185} // anonymous namespace
186
187
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800188namespace android {
189
Dave Burked681bbb2011-08-30 14:39:17 +0100190static bool checkPermission(const char* permissionString) {
191#ifndef HAVE_ANDROID_OS
192 return true;
193#endif
194 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
195 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000196 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100197 return ok;
198}
199
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800200// 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 -0800201/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
202/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
203
204void MediaPlayerService::instantiate() {
205 defaultServiceManager()->addService(
206 String16("media.player"), new MediaPlayerService());
207}
208
209MediaPlayerService::MediaPlayerService()
210{
Steve Block3856b092011-10-20 11:56:00 +0100211 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800212 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800213
214 mBatteryAudio.refCount = 0;
215 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
216 mBatteryAudio.deviceOn[i] = 0;
217 mBatteryAudio.lastTime[i] = 0;
218 mBatteryAudio.totalTime[i] = 0;
219 }
220 // speaker is on by default
221 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700222
223 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224}
225
226MediaPlayerService::~MediaPlayerService()
227{
Steve Block3856b092011-10-20 11:56:00 +0100228 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800229}
230
Glenn Kastenf37971f2012-02-03 11:06:53 -0800231sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800232{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800233 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700234 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
235 wp<MediaRecorderClient> w = recorder;
236 Mutex::Autolock lock(mLock);
237 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800239 return recorder;
240}
241
Gloria Wangdac6a312009-10-29 15:46:37 -0700242void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
243{
244 Mutex::Autolock lock(mLock);
245 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100246 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700247}
248
Glenn Kastenf37971f2012-02-03 11:06:53 -0800249sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800251 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100253 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254 return retriever;
255}
256
Glenn Kastenf37971f2012-02-03 11:06:53 -0800257sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100258 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800260 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700262
263 sp<Client> c = new Client(
264 this, pid, connId, client, audioSessionId,
265 IPCThreadState::self()->getCallingUid());
266
Steve Block3856b092011-10-20 11:56:00 +0100267 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100268 IPCThreadState::self()->getCallingUid());
269
270 wp<Client> w = c;
271 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800272 Mutex::Autolock lock(mLock);
273 mClients.add(w);
274 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800275 return c;
276}
277
Andreas Huber318ad9c2009-10-15 13:46:54 -0700278sp<IOMX> MediaPlayerService::getOMX() {
279 Mutex::Autolock autoLock(mLock);
280
281 if (mOMX.get() == NULL) {
282 mOMX = new OMX;
283 }
284
285 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700286}
287
Andreas Hubered3e3e02012-03-26 11:13:27 -0700288sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700289 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700290}
291
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800292sp<IDrm> MediaPlayerService::makeDrm() {
293 return new Drm;
294}
295
Andreas Huber279dcd82013-01-30 10:41:25 -0800296sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
297 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700298}
299
Jeff Brown2013a542012-09-04 21:38:42 -0700300sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
301 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700302 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
303 return NULL;
304 }
305
Jeff Brownced24b32012-09-05 17:48:03 -0700306 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700307}
308
Andreas Huberb7319a72013-05-29 14:20:52 -0700309status_t MediaPlayerService::updateProxyConfig(
310 const char *host, int32_t port, const char *exclusionList) {
311 return HTTPBase::UpdateProxyConfig(host, port, exclusionList);
312}
313
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800314status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
315{
316 const size_t SIZE = 256;
317 char buffer[SIZE];
318 String8 result;
319
320 result.append(" AudioCache\n");
321 if (mHeap != 0) {
Eric Laurent3d00aa62013-09-24 09:53:27 -0700322 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d)\n",
323 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324 result.append(buffer);
325 }
Scott Fan7d409692013-04-28 10:13:54 +0800326 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 -0800327 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
328 result.append(buffer);
329 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
330 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
331 result.append(buffer);
332 ::write(fd, result.string(), result.size());
333 return NO_ERROR;
334}
335
336status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
341
342 result.append(" AudioOutput\n");
343 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
344 mStreamType, mLeftVolume, mRightVolume);
345 result.append(buffer);
346 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800347 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700349 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
350 mAuxEffectId, mSendLevel);
351 result.append(buffer);
352
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800353 ::write(fd, result.string(), result.size());
354 if (mTrack != 0) {
355 mTrack->dump(fd, args);
356 }
357 return NO_ERROR;
358}
359
360status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
361{
362 const size_t SIZE = 256;
363 char buffer[SIZE];
364 String8 result;
365 result.append(" Client\n");
366 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
367 mPid, mConnId, mStatus, mLoop?"true": "false");
368 result.append(buffer);
369 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700370 if (mPlayer != NULL) {
371 mPlayer->dump(fd, args);
372 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800373 if (mAudioOutput != 0) {
374 mAudioOutput->dump(fd, args);
375 }
376 write(fd, "\n", 1);
377 return NO_ERROR;
378}
379
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
381{
382 const size_t SIZE = 256;
383 char buffer[SIZE];
384 String8 result;
385 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
386 snprintf(buffer, SIZE, "Permission Denial: "
387 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
388 IPCThreadState::self()->getCallingPid(),
389 IPCThreadState::self()->getCallingUid());
390 result.append(buffer);
391 } else {
392 Mutex::Autolock lock(mLock);
393 for (int i = 0, n = mClients.size(); i < n; ++i) {
394 sp<Client> c = mClients[i].promote();
395 if (c != 0) c->dump(fd, args);
396 }
James Dongb9141222010-07-08 11:16:11 -0700397 if (mMediaRecorderClients.size() == 0) {
398 result.append(" No media recorder client\n\n");
399 } else {
400 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
401 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700402 if (c != 0) {
403 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
404 result.append(buffer);
405 write(fd, result.string(), result.size());
406 result = "\n";
407 c->dump(fd, args);
408 }
James Dongb9141222010-07-08 11:16:11 -0700409 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700410 }
411
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700413 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414 FILE *f = fopen(buffer, "r");
415 if (f) {
416 while (!feof(f)) {
417 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700418 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800419 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700420 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 strstr(buffer, " /system/media/")) {
422 result.append(" ");
423 result.append(buffer);
424 }
425 }
426 fclose(f);
427 } else {
428 result.append("couldn't open ");
429 result.append(buffer);
430 result.append("\n");
431 }
432
Glenn Kasten0512ab52011-05-04 17:58:57 -0700433 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800434 DIR *d = opendir(buffer);
435 if (d) {
436 struct dirent *ent;
437 while((ent = readdir(d)) != NULL) {
438 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700439 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800440 struct stat s;
441 if (lstat(buffer, &s) == 0) {
442 if ((s.st_mode & S_IFMT) == S_IFLNK) {
443 char linkto[256];
444 int len = readlink(buffer, linkto, sizeof(linkto));
445 if(len > 0) {
446 if(len > 255) {
447 linkto[252] = '.';
448 linkto[253] = '.';
449 linkto[254] = '.';
450 linkto[255] = 0;
451 } else {
452 linkto[len] = 0;
453 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700454 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700456 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800457 strstr(linkto, "/system/media/") == linkto) {
458 result.append(" ");
459 result.append(buffer);
460 result.append(" -> ");
461 result.append(linkto);
462 result.append("\n");
463 }
464 }
465 } else {
466 result.append(" unexpected type for ");
467 result.append(buffer);
468 result.append("\n");
469 }
470 }
471 }
472 }
473 closedir(d);
474 } else {
475 result.append("couldn't open ");
476 result.append(buffer);
477 result.append("\n");
478 }
479
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800480 bool dumpMem = false;
481 for (size_t i = 0; i < args.size(); i++) {
482 if (args[i] == String16("-m")) {
483 dumpMem = true;
484 }
485 }
486 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700487 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800488 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489 }
490 write(fd, result.string(), result.size());
491 return NO_ERROR;
492}
493
494void MediaPlayerService::removeClient(wp<Client> client)
495{
496 Mutex::Autolock lock(mLock);
497 mClients.remove(client);
498}
499
Robert Shihc159a5a2016-08-16 16:50:54 -0700500bool MediaPlayerService::hasClient(wp<Client> client)
501{
502 Mutex::Autolock lock(mLock);
503 return mClients.indexOf(client) != NAME_NOT_FOUND;
504}
505
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700506MediaPlayerService::Client::Client(
507 const sp<MediaPlayerService>& service, pid_t pid,
508 int32_t connId, const sp<IMediaPlayerClient>& client,
509 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510{
Steve Block3856b092011-10-20 11:56:00 +0100511 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512 mPid = pid;
513 mConnId = connId;
514 mService = service;
515 mClient = client;
516 mLoop = false;
517 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700518 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700519 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800520 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700521
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000523 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800524 mAntagonizer = new Antagonizer(notify, this);
525#endif
526}
527
528MediaPlayerService::Client::~Client()
529{
Steve Block3856b092011-10-20 11:56:00 +0100530 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800531 mAudioOutput.clear();
532 wp<Client> client(this);
533 disconnect();
534 mService->removeClient(client);
535}
536
537void MediaPlayerService::Client::disconnect()
538{
Steve Block3856b092011-10-20 11:56:00 +0100539 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800540 // grab local reference and clear main reference to prevent future
541 // access to object
542 sp<MediaPlayerBase> p;
543 {
544 Mutex::Autolock l(mLock);
545 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800546 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700548
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549 mPlayer.clear();
550
551 // clear the notification to prevent callbacks to dead client
552 // and reset the player. We assume the player will serialize
553 // access to itself if necessary.
554 if (p != 0) {
555 p->setNotifyCallback(0, 0);
556#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000557 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558 mAntagonizer->kill();
559#endif
560 p->reset();
561 }
562
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700563 disconnectNativeWindow();
564
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565 IPCThreadState::self()->flushCommands();
566}
567
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
569{
570 // determine if we have the right player type
571 sp<MediaPlayerBase> p = mPlayer;
572 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100573 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 p.clear();
575 }
576 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700577 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800578 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700579
Jason Simmonsdb29e522011-08-12 13:46:55 -0700580 if (p != NULL) {
581 p->setUID(mUID);
582 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700583
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 return p;
585}
586
John Grossmanc795b642012-02-22 15:38:35 -0800587sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
588 player_type playerType)
589{
590 ALOGV("player type = %d", playerType);
591
592 // create the right type of player
593 sp<MediaPlayerBase> p = createPlayer(playerType);
594 if (p == NULL) {
595 return p;
596 }
597
598 if (!p->hardwareOutput()) {
Marco Nelissen9cae2172013-01-14 14:12:05 -0800599 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid());
John Grossmanc795b642012-02-22 15:38:35 -0800600 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
601 }
602
603 return p;
604}
605
606void MediaPlayerService::Client::setDataSource_post(
607 const sp<MediaPlayerBase>& p,
608 status_t status)
609{
610 ALOGV(" setDataSource");
611 mStatus = status;
612 if (mStatus != OK) {
613 ALOGE(" error: %d", mStatus);
614 return;
615 }
616
617 // Set the re-transmission endpoint if one was chosen.
618 if (mRetransmitEndpointValid) {
619 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
620 if (mStatus != NO_ERROR) {
621 ALOGE("setRetransmitEndpoint error: %d", mStatus);
622 }
623 }
624
625 if (mStatus == OK) {
626 mPlayer = p;
627 }
628}
629
Andreas Huber2db84552010-01-28 11:19:57 -0800630status_t MediaPlayerService::Client::setDataSource(
631 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632{
Steve Block3856b092011-10-20 11:56:00 +0100633 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 if (url == NULL)
635 return UNKNOWN_ERROR;
636
Dave Burked681bbb2011-08-30 14:39:17 +0100637 if ((strncmp(url, "http://", 7) == 0) ||
638 (strncmp(url, "https://", 8) == 0) ||
639 (strncmp(url, "rtsp://", 7) == 0)) {
640 if (!checkPermission("android.permission.INTERNET")) {
641 return PERMISSION_DENIED;
642 }
643 }
644
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645 if (strncmp(url, "content://", 10) == 0) {
646 // get a filedescriptor for the content Uri and
647 // pass it to the setDataSource(fd) method
648
649 String16 url16(url);
650 int fd = android::openContentProviderFile(url16);
651 if (fd < 0)
652 {
Steve Block29357bc2012-01-06 19:20:56 +0000653 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800654 return UNKNOWN_ERROR;
655 }
656 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
657 close(fd);
658 return mStatus;
659 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700660 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800661 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
662 if (p == NULL) {
663 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664 }
665
John Grossmanc795b642012-02-22 15:38:35 -0800666 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667 return mStatus;
668 }
669}
670
671status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
672{
Steve Block3856b092011-10-20 11:56:00 +0100673 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800674 struct stat sb;
675 int ret = fstat(fd, &sb);
676 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000677 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678 return UNKNOWN_ERROR;
679 }
680
Steve Block3856b092011-10-20 11:56:00 +0100681 ALOGV("st_dev = %llu", sb.st_dev);
682 ALOGV("st_mode = %u", sb.st_mode);
683 ALOGV("st_uid = %lu", sb.st_uid);
684 ALOGV("st_gid = %lu", sb.st_gid);
685 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686
687 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000688 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689 ::close(fd);
690 return UNKNOWN_ERROR;
691 }
692 if (offset + length > sb.st_size) {
693 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100694 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800695 }
696
John Grossman44a7e422012-06-21 17:29:24 -0700697 player_type playerType = MediaPlayerFactory::getPlayerType(this,
698 fd,
699 offset,
700 length);
John Grossmanc795b642012-02-22 15:38:35 -0800701 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
702 if (p == NULL) {
703 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704 }
705
706 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800707 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800708 return mStatus;
709}
710
Andreas Hubere2b10282010-11-23 11:41:34 -0800711status_t MediaPlayerService::Client::setDataSource(
712 const sp<IStreamSource> &source) {
713 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700714 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800715 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800716 if (p == NULL) {
717 return NO_INIT;
718 }
719
Andreas Hubere2b10282010-11-23 11:41:34 -0800720 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800721 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800722 return mStatus;
723}
724
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700725void MediaPlayerService::Client::disconnectNativeWindow() {
726 if (mConnectedWindow != NULL) {
727 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
728 NATIVE_WINDOW_API_MEDIA);
729
730 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000731 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700732 strerror(-err), err);
733 }
734 }
735 mConnectedWindow.clear();
736}
737
Glenn Kasten11731182011-02-08 17:26:17 -0800738status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800739 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800740{
Andy McFadden484566c2012-12-18 09:46:54 -0800741 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800742 sp<MediaPlayerBase> p = getPlayer();
743 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700744
Andy McFadden484566c2012-12-18 09:46:54 -0800745 sp<IBinder> binder(bufferProducer == NULL ? NULL :
746 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700747 if (mConnectedWindowBinder == binder) {
748 return OK;
749 }
750
751 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800752 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700753 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700754 status_t err = native_window_api_connect(anw.get(),
755 NATIVE_WINDOW_API_MEDIA);
756
757 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000758 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700759 // Note that we must do the reset before disconnecting from the ANW.
760 // Otherwise queue/dequeue calls could be made on the disconnected
761 // ANW, which may result in errors.
762 reset();
763
764 disconnectNativeWindow();
765
766 return err;
767 }
768 }
769
Andy McFadden484566c2012-12-18 09:46:54 -0800770 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700771 // disconnecting the old one. Otherwise queue/dequeue calls could be made
772 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800773 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700774
775 disconnectNativeWindow();
776
777 mConnectedWindow = anw;
778
779 if (err == OK) {
780 mConnectedWindowBinder = binder;
781 } else {
782 disconnectNativeWindow();
783 }
784
785 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800786}
787
Nicolas Catania1d187f12009-05-12 23:25:55 -0700788status_t MediaPlayerService::Client::invoke(const Parcel& request,
789 Parcel *reply)
790{
791 sp<MediaPlayerBase> p = getPlayer();
792 if (p == NULL) return UNKNOWN_ERROR;
793 return p->invoke(request, reply);
794}
795
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700796// This call doesn't need to access the native player.
797status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
798{
799 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700800 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700801
Nicolas Catania48290382009-07-10 13:53:06 -0700802 if (unmarshallFilter(filter, &allow, &status) &&
803 unmarshallFilter(filter, &drop, &status)) {
804 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700805
806 mMetadataAllow = allow;
807 mMetadataDrop = drop;
808 }
809 return status;
810}
811
Nicolas Catania48290382009-07-10 13:53:06 -0700812status_t MediaPlayerService::Client::getMetadata(
813 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700814{
nikoa64c8c72009-07-20 15:07:26 -0700815 sp<MediaPlayerBase> player = getPlayer();
816 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700817
nikod608a812009-07-16 16:39:53 -0700818 status_t status;
819 // Placeholder for the return code, updated by the caller.
820 reply->writeInt32(-1);
821
nikoa64c8c72009-07-20 15:07:26 -0700822 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700823
824 // We don't block notifications while we fetch the data. We clear
825 // mMetadataUpdated first so we don't lose notifications happening
826 // during the rest of this call.
827 {
828 Mutex::Autolock lock(mLock);
829 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700830 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700831 }
832 mMetadataUpdated.clear();
833 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700834
nikoa64c8c72009-07-20 15:07:26 -0700835 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700836
nikoa64c8c72009-07-20 15:07:26 -0700837 metadata.appendHeader();
838 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700839
840 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700841 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000842 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700843 return status;
844 }
845
846 // FIXME: Implement filtering on the result. Not critical since
847 // filtering takes place on the update notifications already. This
848 // would be when all the metadata are fetch and a filter is set.
849
nikod608a812009-07-16 16:39:53 -0700850 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700851 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700852 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700853}
854
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855status_t MediaPlayerService::Client::prepareAsync()
856{
Steve Block3856b092011-10-20 11:56:00 +0100857 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800858 sp<MediaPlayerBase> p = getPlayer();
859 if (p == 0) return UNKNOWN_ERROR;
860 status_t ret = p->prepareAsync();
861#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000862 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800863 if (ret == NO_ERROR) mAntagonizer->start();
864#endif
865 return ret;
866}
867
868status_t MediaPlayerService::Client::start()
869{
Steve Block3856b092011-10-20 11:56:00 +0100870 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800871 sp<MediaPlayerBase> p = getPlayer();
872 if (p == 0) return UNKNOWN_ERROR;
873 p->setLooping(mLoop);
874 return p->start();
875}
876
877status_t MediaPlayerService::Client::stop()
878{
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800880 sp<MediaPlayerBase> p = getPlayer();
881 if (p == 0) return UNKNOWN_ERROR;
882 return p->stop();
883}
884
885status_t MediaPlayerService::Client::pause()
886{
Steve Block3856b092011-10-20 11:56:00 +0100887 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800888 sp<MediaPlayerBase> p = getPlayer();
889 if (p == 0) return UNKNOWN_ERROR;
890 return p->pause();
891}
892
893status_t MediaPlayerService::Client::isPlaying(bool* state)
894{
895 *state = false;
896 sp<MediaPlayerBase> p = getPlayer();
897 if (p == 0) return UNKNOWN_ERROR;
898 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800900 return NO_ERROR;
901}
902
903status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
904{
Steve Block3856b092011-10-20 11:56:00 +0100905 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800906 sp<MediaPlayerBase> p = getPlayer();
907 if (p == 0) return UNKNOWN_ERROR;
908 status_t ret = p->getCurrentPosition(msec);
909 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100910 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800911 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000912 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800913 }
914 return ret;
915}
916
917status_t MediaPlayerService::Client::getDuration(int *msec)
918{
Steve Block3856b092011-10-20 11:56:00 +0100919 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800920 sp<MediaPlayerBase> p = getPlayer();
921 if (p == 0) return UNKNOWN_ERROR;
922 status_t ret = p->getDuration(msec);
923 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100924 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000926 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800927 }
928 return ret;
929}
930
Marco Nelissen6b74d672012-02-28 16:07:44 -0800931status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
932 ALOGV("setNextPlayer");
933 Mutex::Autolock l(mLock);
934 sp<Client> c = static_cast<Client*>(player.get());
Wei Jia28284122016-08-30 13:49:06 -0700935 if (c != NULL && !mService->hasClient(c)) {
Robert Shihc159a5a2016-08-16 16:50:54 -0700936 return BAD_VALUE;
937 }
938
Marco Nelissen6b74d672012-02-28 16:07:44 -0800939 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -0700940
941 if (c != NULL) {
942 if (mAudioOutput != NULL) {
943 mAudioOutput->setNextOutput(c->mAudioOutput);
944 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
945 ALOGE("no current audio output");
946 }
947
948 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
949 mPlayer->setNextPlayer(mNextClient->getPlayer());
950 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800951 }
John Grossman5f7e55e2012-08-24 14:47:25 -0700952
Marco Nelissen6b74d672012-02-28 16:07:44 -0800953 return OK;
954}
955
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956status_t MediaPlayerService::Client::seekTo(int msec)
957{
Steve Block3856b092011-10-20 11:56:00 +0100958 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800959 sp<MediaPlayerBase> p = getPlayer();
960 if (p == 0) return UNKNOWN_ERROR;
961 return p->seekTo(msec);
962}
963
964status_t MediaPlayerService::Client::reset()
965{
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800967 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800968 sp<MediaPlayerBase> p = getPlayer();
969 if (p == 0) return UNKNOWN_ERROR;
970 return p->reset();
971}
972
Glenn Kastenfff6d712012-01-12 16:38:12 -0800973status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800974{
Steve Block3856b092011-10-20 11:56:00 +0100975 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800976 // TODO: for hardware output, call player instead
977 Mutex::Autolock l(mLock);
978 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
979 return NO_ERROR;
980}
981
982status_t MediaPlayerService::Client::setLooping(int loop)
983{
Steve Block3856b092011-10-20 11:56:00 +0100984 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800985 mLoop = loop;
986 sp<MediaPlayerBase> p = getPlayer();
987 if (p != 0) return p->setLooping(loop);
988 return NO_ERROR;
989}
990
991status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
992{
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800994
995 // for hardware output, call player instead
996 sp<MediaPlayerBase> p = getPlayer();
997 {
998 Mutex::Autolock l(mLock);
999 if (p != 0 && p->hardwareOutput()) {
1000 MediaPlayerHWInterface* hwp =
1001 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1002 return hwp->setVolume(leftVolume, rightVolume);
1003 } else {
1004 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1005 return NO_ERROR;
1006 }
1007 }
1008
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001009 return NO_ERROR;
1010}
1011
Eric Laurent2beeb502010-07-16 07:43:46 -07001012status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1013{
Steve Block3856b092011-10-20 11:56:00 +01001014 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001015 Mutex::Autolock l(mLock);
1016 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1017 return NO_ERROR;
1018}
1019
1020status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1021{
Steve Block3856b092011-10-20 11:56:00 +01001022 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001023 Mutex::Autolock l(mLock);
1024 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1025 return NO_ERROR;
1026}
Nicolas Catania48290382009-07-10 13:53:06 -07001027
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001028status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001029 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001030 sp<MediaPlayerBase> p = getPlayer();
1031 if (p == 0) return UNKNOWN_ERROR;
1032 return p->setParameter(key, request);
1033}
1034
1035status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001036 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001037 sp<MediaPlayerBase> p = getPlayer();
1038 if (p == 0) return UNKNOWN_ERROR;
1039 return p->getParameter(key, reply);
1040}
1041
John Grossmanc795b642012-02-22 15:38:35 -08001042status_t MediaPlayerService::Client::setRetransmitEndpoint(
1043 const struct sockaddr_in* endpoint) {
1044
1045 if (NULL != endpoint) {
1046 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1047 uint16_t p = ntohs(endpoint->sin_port);
1048 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1049 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1050 } else {
1051 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1052 }
1053
1054 sp<MediaPlayerBase> p = getPlayer();
1055
1056 // Right now, the only valid time to set a retransmit endpoint is before
1057 // player selection has been made (since the presence or absence of a
1058 // retransmit endpoint is going to determine which player is selected during
1059 // setDataSource).
1060 if (p != 0) return INVALID_OPERATION;
1061
1062 if (NULL != endpoint) {
1063 mRetransmitEndpoint = *endpoint;
1064 mRetransmitEndpointValid = true;
1065 } else {
1066 mRetransmitEndpointValid = false;
1067 }
1068
1069 return NO_ERROR;
1070}
1071
John Grossman44a7e422012-06-21 17:29:24 -07001072status_t MediaPlayerService::Client::getRetransmitEndpoint(
1073 struct sockaddr_in* endpoint)
1074{
1075 if (NULL == endpoint)
1076 return BAD_VALUE;
1077
1078 sp<MediaPlayerBase> p = getPlayer();
1079
1080 if (p != NULL)
1081 return p->getRetransmitEndpoint(endpoint);
1082
1083 if (!mRetransmitEndpointValid)
1084 return NO_INIT;
1085
1086 *endpoint = mRetransmitEndpoint;
1087
1088 return NO_ERROR;
1089}
1090
Gloria Wangb483c472011-04-11 17:23:27 -07001091void MediaPlayerService::Client::notify(
1092 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001093{
1094 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001095 if (client == NULL) {
1096 return;
1097 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001098
James Dongb8a98252012-08-26 16:13:03 -07001099 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001100 {
1101 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001102 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001103 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001104 if (client->mAudioOutput != NULL)
1105 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001106 client->mNextClient->start();
1107 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1108 }
1109 }
1110
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001111 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001112 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001113 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001114
1115 if(client->shouldDropMetadata(metadata_type)) {
1116 return;
1117 }
1118
1119 // Update the list of metadata that have changed. getMetadata
1120 // also access mMetadataUpdated and clears it.
1121 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001122 }
James Dongb8a98252012-08-26 16:13:03 -07001123
1124 if (c != NULL) {
1125 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1126 c->notify(msg, ext1, ext2, obj);
1127 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001128}
1129
Nicolas Catania48290382009-07-10 13:53:06 -07001130
nikoa64c8c72009-07-20 15:07:26 -07001131bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001132{
Nicolas Catania48290382009-07-10 13:53:06 -07001133 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001134
Nicolas Catania48290382009-07-10 13:53:06 -07001135 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001136 return true;
1137 }
1138
Nicolas Catania48290382009-07-10 13:53:06 -07001139 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001140 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001141 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001142 return true;
1143 }
1144}
1145
Nicolas Catania48290382009-07-10 13:53:06 -07001146
nikoa64c8c72009-07-20 15:07:26 -07001147void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001148 Mutex::Autolock lock(mLock);
1149 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1150 mMetadataUpdated.add(metadata_type);
1151 }
1152}
1153
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001154#if CALLBACK_ANTAGONIZER
1155const int Antagonizer::interval = 10000; // 10 msecs
1156
1157Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1158 mExit(false), mActive(false), mClient(client), mCb(cb)
1159{
1160 createThread(callbackThread, this);
1161}
1162
1163void Antagonizer::kill()
1164{
1165 Mutex::Autolock _l(mLock);
1166 mActive = false;
1167 mExit = true;
1168 mCondition.wait(mLock);
1169}
1170
1171int Antagonizer::callbackThread(void* user)
1172{
Steve Blockb8a80522011-12-20 16:23:08 +00001173 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001174 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1175 while (!p->mExit) {
1176 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001177 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001178 p->mCb(p->mClient, 0, 0, 0);
1179 }
1180 usleep(interval);
1181 }
1182 Mutex::Autolock _l(p->mLock);
1183 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001184 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001185 return 0;
1186}
1187#endif
1188
Eric Laurent3d00aa62013-09-24 09:53:27 -07001189status_t MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels,
1190 audio_format_t* pFormat,
1191 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001192{
Steve Block3856b092011-10-20 11:56:00 +01001193 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001194 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001195 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001196
1197 // Protect our precious, precious DRMd ringtones by only allowing
1198 // decoding of http, but not filesystem paths or content Uris.
1199 // If the application wants to decode those, it should open a
1200 // filedescriptor for them and use that.
1201 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001202 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001203 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204 }
1205
John Grossman44a7e422012-06-21 17:29:24 -07001206 player_type playerType =
1207 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001208 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209
1210 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001211 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001212 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 if (player == NULL) goto Exit;
1214 if (player->hardwareOutput()) goto Exit;
1215
1216 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1217
1218 // set data source
1219 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1220
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222 player->prepareAsync();
1223
Steve Block3856b092011-10-20 11:56:00 +01001224 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001225 if (cache->wait() != NO_ERROR) goto Exit;
1226
Steve Block3856b092011-10-20 11:56:00 +01001227 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228 player->start();
1229
Steve Block3856b092011-10-20 11:56:00 +01001230 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001231 cache->wait();
1232 // in case of error, return what was successfully decoded.
1233 if (cache->size() == 0) {
1234 goto Exit;
1235 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001236
Eric Laurent3d00aa62013-09-24 09:53:27 -07001237 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001238 *pSampleRate = cache->sampleRate();
1239 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001240 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001241 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1242 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1243 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001244
1245Exit:
1246 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001247 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001248}
1249
Eric Laurent3d00aa62013-09-24 09:53:27 -07001250status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1251 uint32_t *pSampleRate, int* pNumChannels,
1252 audio_format_t* pFormat,
1253 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254{
Steve Block3856b092011-10-20 11:56:00 +01001255 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001256 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001257 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258
John Grossman44a7e422012-06-21 17:29:24 -07001259 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1260 fd,
1261 offset,
1262 length);
Steve Block3856b092011-10-20 11:56:00 +01001263 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264
1265 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001266 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001267 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 if (player == NULL) goto Exit;
1269 if (player->hardwareOutput()) goto Exit;
1270
1271 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1272
1273 // set data source
1274 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1275
Steve Block3856b092011-10-20 11:56:00 +01001276 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277 player->prepareAsync();
1278
Steve Block3856b092011-10-20 11:56:00 +01001279 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001280 if (cache->wait() != NO_ERROR) goto Exit;
1281
Steve Block3856b092011-10-20 11:56:00 +01001282 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001283 player->start();
1284
Steve Block3856b092011-10-20 11:56:00 +01001285 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001286 cache->wait();
1287 // in case of error, return what was successfully decoded.
1288 if (cache->size() == 0) {
1289 goto Exit;
1290 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001291
Eric Laurent3d00aa62013-09-24 09:53:27 -07001292 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293 *pSampleRate = cache->sampleRate();
1294 *pNumChannels = cache->channelCount();
1295 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001296 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1297 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1298 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299
1300Exit:
1301 if (player != 0) player->reset();
1302 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001303 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001304}
1305
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001306
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307#undef LOG_TAG
1308#define LOG_TAG "AudioSink"
Marco Nelissen9cae2172013-01-14 14:12:05 -08001309MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid)
Andreas Huber20111aa2009-07-14 16:56:47 -07001310 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001311 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001312 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001313 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001314 mSessionId(sessionId),
Marco Nelissen9cae2172013-01-14 14:12:05 -08001315 mUid(uid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001316 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001317 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001318 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 mLeftVolume = 1.0;
1320 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001321 mPlaybackRatePermille = 1000;
1322 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001323 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001324 mAuxEffectId = 0;
1325 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326 setMinBufferCount();
1327}
1328
1329MediaPlayerService::AudioOutput::~AudioOutput()
1330{
1331 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001332 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001333}
1334
1335void MediaPlayerService::AudioOutput::setMinBufferCount()
1336{
1337 char value[PROPERTY_VALUE_MAX];
1338 if (property_get("ro.kernel.qemu", value, 0)) {
1339 mIsOnEmulator = true;
1340 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1341 }
1342}
1343
1344bool MediaPlayerService::AudioOutput::isOnEmulator()
1345{
1346 setMinBufferCount();
1347 return mIsOnEmulator;
1348}
1349
1350int MediaPlayerService::AudioOutput::getMinBufferCount()
1351{
1352 setMinBufferCount();
1353 return mMinBufferCount;
1354}
1355
1356ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1357{
1358 if (mTrack == 0) return NO_INIT;
1359 return mTrack->frameCount() * frameSize();
1360}
1361
1362ssize_t MediaPlayerService::AudioOutput::frameCount() const
1363{
1364 if (mTrack == 0) return NO_INIT;
1365 return mTrack->frameCount();
1366}
1367
1368ssize_t MediaPlayerService::AudioOutput::channelCount() const
1369{
1370 if (mTrack == 0) return NO_INIT;
1371 return mTrack->channelCount();
1372}
1373
1374ssize_t MediaPlayerService::AudioOutput::frameSize() const
1375{
1376 if (mTrack == 0) return NO_INIT;
1377 return mTrack->frameSize();
1378}
1379
1380uint32_t MediaPlayerService::AudioOutput::latency () const
1381{
Eric Laurentdb354e52012-03-05 17:27:11 -08001382 if (mTrack == 0) return 0;
1383 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001384}
1385
1386float MediaPlayerService::AudioOutput::msecsPerFrame() const
1387{
1388 return mMsecsPerFrame;
1389}
1390
Marco Nelissen4110c102012-03-29 09:31:28 -07001391status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001392{
1393 if (mTrack == 0) return NO_INIT;
1394 return mTrack->getPosition(position);
1395}
1396
Marco Nelissen4110c102012-03-29 09:31:28 -07001397status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1398{
1399 if (mTrack == 0) return NO_INIT;
1400 *frameswritten = mBytesWritten / frameSize();
1401 return OK;
1402}
1403
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001404status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1405{
1406 if (mTrack == 0) return NO_INIT;
1407 return mTrack->setParameters(keyValuePairs);
1408}
1409
1410String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1411{
1412 if (mTrack == 0) return String8::empty();
1413 return mTrack->getParameters(keys);
1414}
1415
1416void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1417{
1418 ALOGV("deleteRecycledTrack");
1419
1420 if (mRecycledTrack != 0) {
1421
1422 if (mCallbackData != NULL) {
1423 mCallbackData->setOutput(NULL);
1424 mCallbackData->endTrackSwitch();
1425 }
1426
1427 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1428 mRecycledTrack->flush();
1429 }
1430 // An offloaded track isn't flushed because the STREAM_END is reported
1431 // slightly prematurely to allow time for the gapless track switch
1432 // but this means that if we decide not to recycle the track there
1433 // could be a small amount of residual data still playing. We leave
1434 // AudioFlinger to drain the track.
1435
1436 mRecycledTrack.clear();
1437 delete mCallbackData;
1438 mCallbackData = NULL;
1439 close();
1440 }
1441}
1442
Andreas Huber20111aa2009-07-14 16:56:47 -07001443status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001444 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1445 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001446 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001447 audio_output_flags_t flags,
1448 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001449{
Andreas Huber20111aa2009-07-14 16:56:47 -07001450 mCallback = cb;
1451 mCallbackCookie = cookie;
1452
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001453 // Check argument "bufferCount" against the mininum buffer count
1454 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001455 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456 bufferCount = mMinBufferCount;
1457
1458 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001459 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1460 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001461 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001462 size_t afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001463 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001464
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001465 // offloading is only supported in callback mode for now.
1466 // offloadInfo must be present if offload flag is set
1467 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1468 ((cb == NULL) || (offloadInfo == NULL))) {
1469 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001470 }
1471
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001472 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1473 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1474 } else {
1475 uint32_t afSampleRate;
1476 size_t afFrameCount;
1477
1478 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1479 return NO_INIT;
1480 }
1481 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1482 return NO_INIT;
1483 }
1484
1485 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1486 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001487
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001488 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001489 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001490 if (0 == channelMask) {
1491 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1492 return NO_INIT;
1493 }
1494 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001495
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001496 // Check whether we can recycle the track
1497 bool reuse = false;
1498 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001499
Glenn Kasten2799d742013-05-30 14:33:29 -07001500 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001501 // check whether we are switching between two offloaded tracks
1502 bothOffloaded = (flags & mRecycledTrack->getFlags()
1503 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001504
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001505 // check if the existing track can be reused as-is, or if a new track needs to be created.
1506 reuse = true;
1507
Marco Nelissen67295b52012-06-11 14:52:53 -07001508 if ((mCallbackData == NULL && mCallback != NULL) ||
1509 (mCallbackData != NULL && mCallback == NULL)) {
1510 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1511 ALOGV("can't chain callback and write");
1512 reuse = false;
1513 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001514 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1515 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001516 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001517 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001518 reuse = false;
1519 } else if (flags != mFlags) {
1520 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1521 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001522 } else if (mRecycledTrack->format() != format) {
1523 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001524 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001525 } else {
1526 ALOGV("no track available to recycle");
1527 }
1528
1529 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1530
1531 // If we can't recycle and both tracks are offloaded
1532 // we must close the previous output before opening a new one
1533 if (bothOffloaded && !reuse) {
1534 ALOGV("both offloaded and not recycling");
1535 deleteRecycledTrack();
1536 }
1537
1538 sp<AudioTrack> t;
1539 CallbackData *newcbd = NULL;
1540
1541 // We don't attempt to create a new track if we are recycling an
1542 // offloaded track. But, if we are recycling a non-offloaded or we
1543 // are switching where one is offloaded and one isn't then we create
1544 // the new track in advance so that we can read additional stream info
1545
1546 if (!(reuse && bothOffloaded)) {
1547 ALOGV("creating new AudioTrack");
1548
1549 if (mCallback != NULL) {
1550 newcbd = new CallbackData(this);
1551 t = new AudioTrack(
1552 mStreamType,
1553 sampleRate,
1554 format,
1555 channelMask,
1556 frameCount,
1557 flags,
1558 CallbackWrapper,
1559 newcbd,
1560 0, // notification frames
1561 mSessionId,
1562 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen9cae2172013-01-14 14:12:05 -08001563 offloadInfo,
1564 mUid);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001565 } else {
1566 t = new AudioTrack(
1567 mStreamType,
1568 sampleRate,
1569 format,
1570 channelMask,
1571 frameCount,
1572 flags,
Marco Nelissen9cae2172013-01-14 14:12:05 -08001573 NULL, // callback
1574 NULL, // user data
1575 0, // notification frames
1576 mSessionId,
1577 AudioTrack::TRANSFER_DEFAULT,
1578 NULL, // offload info
1579 mUid);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001580 }
1581
1582 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1583 ALOGE("Unable to create audio track");
1584 delete newcbd;
1585 return NO_INIT;
1586 }
1587 }
1588
1589 if (reuse) {
1590 CHECK(mRecycledTrack != NULL);
1591
1592 if (!bothOffloaded) {
1593 if (mRecycledTrack->frameCount() != t->frameCount()) {
1594 ALOGV("framecount differs: %u/%u frames",
1595 mRecycledTrack->frameCount(), t->frameCount());
1596 reuse = false;
1597 }
1598 }
1599
Marco Nelissen67295b52012-06-11 14:52:53 -07001600 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001601 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001602 close();
1603 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001604 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001605 if (mCallbackData != NULL) {
1606 mCallbackData->setOutput(this);
1607 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001608 delete newcbd;
1609 return OK;
1610 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001611 }
1612
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001613 // we're not going to reuse the track, unblock and flush it
1614 // this was done earlier if both tracks are offloaded
1615 if (!bothOffloaded) {
1616 deleteRecycledTrack();
1617 }
1618
1619 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1620
Marco Nelissen67295b52012-06-11 14:52:53 -07001621 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001622 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001623 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001624
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001625 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001626 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001627 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001628 uint32_t pos;
1629 if (t->getPosition(&pos) == OK) {
1630 mBytesWritten = uint64_t(pos) * t->frameSize();
1631 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001632 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001633
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001634 status_t res = NO_ERROR;
1635 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1636 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1637 if (res == NO_ERROR) {
1638 t->setAuxEffectSendLevel(mSendLevel);
1639 res = t->attachAuxEffect(mAuxEffectId);
1640 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001641 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001642 ALOGV("open() DONE status %d", res);
1643 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001644}
1645
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001646status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001647{
Steve Block3856b092011-10-20 11:56:00 +01001648 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001649 if (mCallbackData != NULL) {
1650 mCallbackData->endTrackSwitch();
1651 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001652 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001653 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001654 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001655 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001656 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001657 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001658}
1659
Marco Nelissen6b74d672012-02-28 16:07:44 -08001660void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1661 mNextOutput = nextOutput;
1662}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001663
1664
Marco Nelissen6b74d672012-02-28 16:07:44 -08001665void MediaPlayerService::AudioOutput::switchToNextOutput() {
1666 ALOGV("switchToNextOutput");
1667 if (mNextOutput != NULL) {
1668 if (mCallbackData != NULL) {
1669 mCallbackData->beginTrackSwitch();
1670 }
1671 delete mNextOutput->mCallbackData;
1672 mNextOutput->mCallbackData = mCallbackData;
1673 mCallbackData = NULL;
1674 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001675 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001676 mNextOutput->mSampleRateHz = mSampleRateHz;
1677 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001678 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001679 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001680 }
1681}
1682
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001683ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1684{
Andreas Huber20111aa2009-07-14 16:56:47 -07001685 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1686
Steve Block3856b092011-10-20 11:56:00 +01001687 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001688 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001689 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001690 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001691 return ret;
1692 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001693 return NO_INIT;
1694}
1695
1696void MediaPlayerService::AudioOutput::stop()
1697{
Steve Block3856b092011-10-20 11:56:00 +01001698 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001699 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001700}
1701
1702void MediaPlayerService::AudioOutput::flush()
1703{
Steve Block3856b092011-10-20 11:56:00 +01001704 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001705 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001706}
1707
1708void MediaPlayerService::AudioOutput::pause()
1709{
Steve Block3856b092011-10-20 11:56:00 +01001710 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001711 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001712}
1713
1714void MediaPlayerService::AudioOutput::close()
1715{
Steve Block3856b092011-10-20 11:56:00 +01001716 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001717 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001718}
1719
1720void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1721{
Steve Block3856b092011-10-20 11:56:00 +01001722 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001723 mLeftVolume = left;
1724 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001725 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001726 mTrack->setVolume(left, right);
1727 }
1728}
1729
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001730status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1731{
1732 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1733 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001734 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001735 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1736 } else {
1737 res = NO_INIT;
1738 }
1739 mPlaybackRatePermille = ratePermille;
1740 if (mSampleRateHz != 0) {
1741 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1742 }
1743 return res;
1744}
1745
Eric Laurent2beeb502010-07-16 07:43:46 -07001746status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1747{
Steve Block3856b092011-10-20 11:56:00 +01001748 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001749 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001750 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001751 return mTrack->setAuxEffectSendLevel(level);
1752 }
1753 return NO_ERROR;
1754}
1755
1756status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1757{
Steve Block3856b092011-10-20 11:56:00 +01001758 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001759 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001760 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001761 return mTrack->attachAuxEffect(effectId);
1762 }
1763 return NO_ERROR;
1764}
1765
Andreas Huber20111aa2009-07-14 16:56:47 -07001766// static
1767void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001768 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001769 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001770 CallbackData *data = (CallbackData*)cookie;
1771 data->lock();
1772 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001773 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001774 if (me == NULL) {
1775 // no output set, likely because the track was scheduled to be reused
1776 // by another player, but the format turned out to be incompatible.
1777 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001778 if (buffer != NULL) {
1779 buffer->size = 0;
1780 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001781 return;
1782 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001783
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001784 switch(event) {
1785 case AudioTrack::EVENT_MORE_DATA: {
1786 size_t actualSize = (*me->mCallback)(
1787 me, buffer->raw, buffer->size, me->mCallbackCookie,
1788 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001789
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001790 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
1791 // We've reached EOS but the audio track is not stopped yet,
1792 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001793
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001794 memset(buffer->raw, 0, buffer->size);
1795 actualSize = buffer->size;
1796 }
1797
1798 buffer->size = actualSize;
1799 } break;
1800
1801
1802 case AudioTrack::EVENT_STREAM_END:
1803 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1804 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1805 me->mCallbackCookie, CB_EVENT_STREAM_END);
1806 break;
1807
1808 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1809 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1810 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1811 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1812 break;
1813
1814 default:
1815 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001816 }
1817
Marco Nelissen6b74d672012-02-28 16:07:44 -08001818 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001819}
1820
Marco Nelissen4110c102012-03-29 09:31:28 -07001821int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001822{
1823 return mSessionId;
1824}
1825
Eric Laurentd0115d82013-07-26 17:16:50 -07001826uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1827{
1828 if (mTrack == 0) return 0;
1829 return mTrack->getSampleRate();
1830}
1831
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001832#undef LOG_TAG
1833#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001834MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1835 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1836 mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001837{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001838}
1839
1840uint32_t MediaPlayerService::AudioCache::latency () const
1841{
1842 return 0;
1843}
1844
1845float MediaPlayerService::AudioCache::msecsPerFrame() const
1846{
1847 return mMsecsPerFrame;
1848}
1849
Marco Nelissen4110c102012-03-29 09:31:28 -07001850status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001851{
1852 if (position == 0) return BAD_VALUE;
1853 *position = mSize;
1854 return NO_ERROR;
1855}
1856
Marco Nelissen4110c102012-03-29 09:31:28 -07001857status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1858{
1859 if (written == 0) return BAD_VALUE;
1860 *written = mSize;
1861 return NO_ERROR;
1862}
1863
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001864////////////////////////////////////////////////////////////////////////////////
1865
1866struct CallbackThread : public Thread {
1867 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1868 MediaPlayerBase::AudioSink::AudioCallback cb,
1869 void *cookie);
1870
1871protected:
1872 virtual ~CallbackThread();
1873
1874 virtual bool threadLoop();
1875
1876private:
1877 wp<MediaPlayerBase::AudioSink> mSink;
1878 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1879 void *mCookie;
1880 void *mBuffer;
1881 size_t mBufferSize;
1882
1883 CallbackThread(const CallbackThread &);
1884 CallbackThread &operator=(const CallbackThread &);
1885};
1886
1887CallbackThread::CallbackThread(
1888 const wp<MediaPlayerBase::AudioSink> &sink,
1889 MediaPlayerBase::AudioSink::AudioCallback cb,
1890 void *cookie)
1891 : mSink(sink),
1892 mCallback(cb),
1893 mCookie(cookie),
1894 mBuffer(NULL),
1895 mBufferSize(0) {
1896}
1897
1898CallbackThread::~CallbackThread() {
1899 if (mBuffer) {
1900 free(mBuffer);
1901 mBuffer = NULL;
1902 }
1903}
1904
1905bool CallbackThread::threadLoop() {
1906 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1907 if (sink == NULL) {
1908 return false;
1909 }
1910
1911 if (mBuffer == NULL) {
1912 mBufferSize = sink->bufferSize();
1913 mBuffer = malloc(mBufferSize);
1914 }
1915
1916 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001917 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1918 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001919
1920 if (actualSize > 0) {
1921 sink->write(mBuffer, actualSize);
1922 }
1923
1924 return true;
1925}
1926
1927////////////////////////////////////////////////////////////////////////////////
1928
Andreas Huber20111aa2009-07-14 16:56:47 -07001929status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001930 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1931 audio_format_t format, int bufferCount,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001932 AudioCallback cb, void *cookie, audio_output_flags_t flags,
1933 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001934{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001935 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001936 if (mHeap->getHeapID() < 0) {
1937 return NO_INIT;
1938 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001939
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001940 mSampleRate = sampleRate;
1941 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001942 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001943 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001944
1945 if (cb != NULL) {
1946 mCallbackThread = new CallbackThread(this, cb, cookie);
1947 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001948 return NO_ERROR;
1949}
1950
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001951status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001952 if (mCallbackThread != NULL) {
1953 mCallbackThread->run("AudioCache callback");
1954 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001955 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001956}
1957
1958void MediaPlayerService::AudioCache::stop() {
1959 if (mCallbackThread != NULL) {
1960 mCallbackThread->requestExitAndWait();
1961 }
1962}
1963
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001964ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1965{
Steve Block3856b092011-10-20 11:56:00 +01001966 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001967 if ((buffer == 0) || (size == 0)) return size;
1968
1969 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1970 if (p == NULL) return NO_INIT;
1971 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001972 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001973 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001974 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001975 size = mHeap->getSize() - mSize;
1976 }
1977 memcpy(p, buffer, size);
1978 mSize += size;
1979 return size;
1980}
1981
1982// call with lock held
1983status_t MediaPlayerService::AudioCache::wait()
1984{
1985 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001986 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001987 mSignal.wait(mLock);
1988 }
1989 mCommandComplete = false;
1990
1991 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001992 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001993 } else {
Steve Block3856b092011-10-20 11:56:00 +01001994 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001995 }
1996 return mError;
1997}
1998
Gloria Wangb483c472011-04-11 17:23:27 -07001999void MediaPlayerService::AudioCache::notify(
2000 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002001{
Steve Block3856b092011-10-20 11:56:00 +01002002 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002003 AudioCache* p = static_cast<AudioCache*>(cookie);
2004
2005 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002006 switch (msg)
2007 {
2008 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002009 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002010 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08002011 break;
2012 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002013 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002014 break;
2015 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002016 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002017 break;
2018 default:
Steve Block3856b092011-10-20 11:56:00 +01002019 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002020 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002021 }
2022
2023 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002024 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002025 p->mCommandComplete = true;
2026 p->mSignal.signal();
2027}
2028
Marco Nelissen4110c102012-03-29 09:31:28 -07002029int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002030{
2031 return 0;
2032}
2033
Eric Laurentd0115d82013-07-26 17:16:50 -07002034uint32_t MediaPlayerService::AudioCache::getSampleRate() const
2035{
2036 if (mMsecsPerFrame == 0) {
2037 return 0;
2038 }
2039 return (uint32_t)(1.e3 / mMsecsPerFrame);
2040}
2041
Gloria Wang7cf180c2011-02-19 18:37:57 -08002042void MediaPlayerService::addBatteryData(uint32_t params)
2043{
2044 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002045
2046 int32_t time = systemTime() / 1000000L;
2047
2048 // change audio output devices. This notification comes from AudioFlinger
2049 if ((params & kBatteryDataSpeakerOn)
2050 || (params & kBatteryDataOtherAudioDeviceOn)) {
2051
2052 int deviceOn[NUM_AUDIO_DEVICES];
2053 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2054 deviceOn[i] = 0;
2055 }
2056
2057 if ((params & kBatteryDataSpeakerOn)
2058 && (params & kBatteryDataOtherAudioDeviceOn)) {
2059 deviceOn[SPEAKER_AND_OTHER] = 1;
2060 } else if (params & kBatteryDataSpeakerOn) {
2061 deviceOn[SPEAKER] = 1;
2062 } else {
2063 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2064 }
2065
2066 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2067 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2068
2069 if (mBatteryAudio.refCount > 0) { // if playing audio
2070 if (!deviceOn[i]) {
2071 mBatteryAudio.lastTime[i] += time;
2072 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2073 mBatteryAudio.lastTime[i] = 0;
2074 } else {
2075 mBatteryAudio.lastTime[i] = 0 - time;
2076 }
2077 }
2078
2079 mBatteryAudio.deviceOn[i] = deviceOn[i];
2080 }
2081 }
2082 return;
2083 }
2084
2085 // an sudio stream is started
2086 if (params & kBatteryDataAudioFlingerStart) {
2087 // record the start time only if currently no other audio
2088 // is being played
2089 if (mBatteryAudio.refCount == 0) {
2090 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2091 if (mBatteryAudio.deviceOn[i]) {
2092 mBatteryAudio.lastTime[i] -= time;
2093 }
2094 }
2095 }
2096
2097 mBatteryAudio.refCount ++;
2098 return;
2099
2100 } else if (params & kBatteryDataAudioFlingerStop) {
2101 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002102 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002103 return;
2104 }
2105
2106 // record the stop time only if currently this is the only
2107 // audio being played
2108 if (mBatteryAudio.refCount == 1) {
2109 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2110 if (mBatteryAudio.deviceOn[i]) {
2111 mBatteryAudio.lastTime[i] += time;
2112 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2113 mBatteryAudio.lastTime[i] = 0;
2114 }
2115 }
2116 }
2117
2118 mBatteryAudio.refCount --;
2119 return;
2120 }
2121
Gloria Wang7cf180c2011-02-19 18:37:57 -08002122 int uid = IPCThreadState::self()->getCallingUid();
2123 if (uid == AID_MEDIA) {
2124 return;
2125 }
2126 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002127
2128 if (index < 0) { // create a new entry for this UID
2129 BatteryUsageInfo info;
2130 info.audioTotalTime = 0;
2131 info.videoTotalTime = 0;
2132 info.audioLastTime = 0;
2133 info.videoLastTime = 0;
2134 info.refCount = 0;
2135
Gloria Wang9ee159b2011-02-24 14:51:45 -08002136 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002137 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002138 return;
2139 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002140 }
2141
2142 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2143
2144 if (params & kBatteryDataCodecStarted) {
2145 if (params & kBatteryDataTrackAudio) {
2146 info.audioLastTime -= time;
2147 info.refCount ++;
2148 }
2149 if (params & kBatteryDataTrackVideo) {
2150 info.videoLastTime -= time;
2151 info.refCount ++;
2152 }
2153 } else {
2154 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002155 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002156 return;
2157 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002158 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002159 mBatteryData.removeItem(uid);
2160 return;
2161 }
2162
2163 if (params & kBatteryDataTrackAudio) {
2164 info.audioLastTime += time;
2165 info.refCount --;
2166 }
2167 if (params & kBatteryDataTrackVideo) {
2168 info.videoLastTime += time;
2169 info.refCount --;
2170 }
2171
2172 // no stream is being played by this UID
2173 if (info.refCount == 0) {
2174 info.audioTotalTime += info.audioLastTime;
2175 info.audioLastTime = 0;
2176 info.videoTotalTime += info.videoLastTime;
2177 info.videoLastTime = 0;
2178 }
2179 }
2180}
2181
2182status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2183 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002184
2185 // audio output devices usage
2186 int32_t time = systemTime() / 1000000L; //in ms
2187 int32_t totalTime;
2188
2189 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2190 totalTime = mBatteryAudio.totalTime[i];
2191
2192 if (mBatteryAudio.deviceOn[i]
2193 && (mBatteryAudio.lastTime[i] != 0)) {
2194 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2195 totalTime += tmpTime;
2196 }
2197
2198 reply->writeInt32(totalTime);
2199 // reset the total time
2200 mBatteryAudio.totalTime[i] = 0;
2201 }
2202
2203 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002204 BatteryUsageInfo info;
2205 int size = mBatteryData.size();
2206
2207 reply->writeInt32(size);
2208 int i = 0;
2209
2210 while (i < size) {
2211 info = mBatteryData.valueAt(i);
2212
2213 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2214 reply->writeInt32(info.audioTotalTime);
2215 reply->writeInt32(info.videoTotalTime);
2216
2217 info.audioTotalTime = 0;
2218 info.videoTotalTime = 0;
2219
2220 // remove the UID entry where no stream is being played
2221 if (info.refCount <= 0) {
2222 mBatteryData.removeItemsAt(i);
2223 size --;
2224 i --;
2225 }
2226 i++;
2227 }
2228 return NO_ERROR;
2229}
nikoa64c8c72009-07-20 15:07:26 -07002230} // namespace android