blob: 951ca9736e7f5484b8b7f9ba28ea140b36e23a6d [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2**
Alok Kediya601e9b52013-09-27 11:55:10 +05303** Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4** Not a Contribution.
5**
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08006** Copyright 2008, The Android Open Source Project
7**
8** Licensed under the Apache License, Version 2.0 (the "License");
9** you may not use this file except in compliance with the License.
10** You may obtain a copy of the License at
11**
12** http://www.apache.org/licenses/LICENSE-2.0
13**
14** Unless required by applicable law or agreed to in writing, software
15** distributed under the License is distributed on an "AS IS" BASIS,
16** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17** See the License for the specific language governing permissions and
18** limitations under the License.
19*/
20
21// Proxy for media player implementations
22
23//#define LOG_NDEBUG 0
24#define LOG_TAG "MediaPlayerService"
25#include <utils/Log.h>
26
27#include <sys/types.h>
28#include <sys/stat.h>
Gloria Wang7cf180c2011-02-19 18:37:57 -080029#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080030#include <dirent.h>
31#include <unistd.h>
32
33#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080035#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070036#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070037
38#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080039
Mathias Agopian75624082009-05-19 19:08:10 -070040#include <binder/IPCThreadState.h>
41#include <binder/IServiceManager.h>
42#include <binder/MemoryHeapBase.h>
43#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080044#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070045#include <utils/Errors.h> // for status_t
46#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070047#include <utils/SystemClock.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070048#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049
Jeff Brown2013a542012-09-04 21:38:42 -070050#include <media/IRemoteDisplay.h>
51#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052#include <media/MediaPlayerInterface.h>
53#include <media/mediarecorder.h>
54#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070055#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070057#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070058#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010059#include <media/stagefright/AudioPlayer.h>
60#include <media/stagefright/foundation/ADebug.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080061
Dima Zavin64760242011-05-11 14:15:23 -070062#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070063
Gloria Wang7cf180c2011-02-19 18:37:57 -080064#include <private/android_filesystem_config.h>
65
James Dong559bf282012-03-28 10:29:14 -070066#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067#include "MediaRecorderClient.h"
68#include "MediaPlayerService.h"
69#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070070#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071
72#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070073#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070074#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080075#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070076
Andreas Huber20111aa2009-07-14 16:56:47 -070077#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070078
Andreas Hubered3e3e02012-03-26 11:13:27 -070079#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080080#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070081#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070082#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070083#include "RemoteDisplay.h"
Alok Kediya601e9b52013-09-27 11:55:10 +053084#define DEFAULT_SAMPLE_RATE 44100
Andreas Hubered3e3e02012-03-26 11:13:27 -070085
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070086namespace {
nikoa64c8c72009-07-20 15:07:26 -070087using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070088using android::status_t;
89using android::OK;
90using android::BAD_VALUE;
91using android::NOT_ENOUGH_DATA;
92using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070093
94// Max number of entries in the filter.
95const int kMaxFilterSize = 64; // I pulled that out of thin air.
96
nikoa64c8c72009-07-20 15:07:26 -070097// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070098
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070099
100// Unmarshall a filter from a Parcel.
101// Filter format in a parcel:
102//
103// 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
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | number of entries (n) |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// | metadata type 1 |
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109// | metadata type 2 |
110// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111// ....
112// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113// | metadata type n |
114// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115//
116// @param p Parcel that should start with a filter.
117// @param[out] filter On exit contains the list of metadata type to be
118// filtered.
119// @param[out] status On exit contains the status code to be returned.
120// @return true if the parcel starts with a valid filter.
121bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700122 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 status_t *status)
124{
Nicolas Catania48290382009-07-10 13:53:06 -0700125 int32_t val;
126 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 {
Steve Block29357bc2012-01-06 19:20:56 +0000128 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700129 *status = NOT_ENOUGH_DATA;
130 return false;
131 }
132
Nicolas Catania48290382009-07-10 13:53:06 -0700133 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134 {
Steve Block29357bc2012-01-06 19:20:56 +0000135 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136 *status = BAD_VALUE;
137 return false;
138 }
139
Nicolas Catania48290382009-07-10 13:53:06 -0700140 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141
142 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700143 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144
nikoa64c8c72009-07-20 15:07:26 -0700145 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146
Nicolas Catania48290382009-07-10 13:53:06 -0700147
148 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149 {
Steve Block29357bc2012-01-06 19:20:56 +0000150 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 *status = NOT_ENOUGH_DATA;
152 return false;
153 }
154
nikoa64c8c72009-07-20 15:07:26 -0700155 const Metadata::Type *data =
156 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157
Nicolas Catania48290382009-07-10 13:53:06 -0700158 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700159 {
Steve Block29357bc2012-01-06 19:20:56 +0000160 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 *status = BAD_VALUE;
162 return false;
163 }
164
165 // TODO: The stl impl of vector would be more efficient here
166 // because it degenerates into a memcpy on pod types. Try to
167 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700168 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169 {
Nicolas Catania48290382009-07-10 13:53:06 -0700170 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700171 ++data;
172 }
173 *status = OK;
174 return true;
175}
176
Nicolas Catania48290382009-07-10 13:53:06 -0700177// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178// @param val To be searched.
179// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700180bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700181{
182 // Deal with empty and ANY right away
183 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700184 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700185
Nicolas Catania48290382009-07-10 13:53:06 -0700186 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700187}
188
189} // anonymous namespace
190
191
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192namespace android {
193
Dave Burked681bbb2011-08-30 14:39:17 +0100194static bool checkPermission(const char* permissionString) {
195#ifndef HAVE_ANDROID_OS
196 return true;
197#endif
198 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
199 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000200 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100201 return ok;
202}
203
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204// 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 -0800205/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
206/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
207
208void MediaPlayerService::instantiate() {
209 defaultServiceManager()->addService(
210 String16("media.player"), new MediaPlayerService());
211}
212
213MediaPlayerService::MediaPlayerService()
214{
Steve Block3856b092011-10-20 11:56:00 +0100215 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800217
218 mBatteryAudio.refCount = 0;
219 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
220 mBatteryAudio.deviceOn[i] = 0;
221 mBatteryAudio.lastTime[i] = 0;
222 mBatteryAudio.totalTime[i] = 0;
223 }
224 // speaker is on by default
225 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700226
227 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228}
229
230MediaPlayerService::~MediaPlayerService()
231{
Steve Block3856b092011-10-20 11:56:00 +0100232 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233}
234
Glenn Kastenf37971f2012-02-03 11:06:53 -0800235sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800236{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800237 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700238 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
239 wp<MediaRecorderClient> w = recorder;
240 Mutex::Autolock lock(mLock);
241 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100242 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243 return recorder;
244}
245
Gloria Wangdac6a312009-10-29 15:46:37 -0700246void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
247{
248 Mutex::Autolock lock(mLock);
249 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100250 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700251}
252
Glenn Kastenf37971f2012-02-03 11:06:53 -0800253sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800255 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100257 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258 return retriever;
259}
260
Glenn Kastenf37971f2012-02-03 11:06:53 -0800261sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100262 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800263{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800264 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700266
267 sp<Client> c = new Client(
268 this, pid, connId, client, audioSessionId,
269 IPCThreadState::self()->getCallingUid());
270
Steve Block3856b092011-10-20 11:56:00 +0100271 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100272 IPCThreadState::self()->getCallingUid());
273
274 wp<Client> w = c;
275 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800276 Mutex::Autolock lock(mLock);
277 mClients.add(w);
278 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800279 return c;
280}
281
Andreas Huber318ad9c2009-10-15 13:46:54 -0700282sp<IOMX> MediaPlayerService::getOMX() {
283 Mutex::Autolock autoLock(mLock);
284
285 if (mOMX.get() == NULL) {
286 mOMX = new OMX;
287 }
288
289 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700290}
291
Andreas Hubered3e3e02012-03-26 11:13:27 -0700292sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700293 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700294}
295
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800296sp<IDrm> MediaPlayerService::makeDrm() {
297 return new Drm;
298}
299
Andreas Huber279dcd82013-01-30 10:41:25 -0800300sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
301 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700302}
303
Jeff Brown2013a542012-09-04 21:38:42 -0700304sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
305 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700306 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
307 return NULL;
308 }
309
Jeff Brownced24b32012-09-05 17:48:03 -0700310 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700311}
312
Andreas Huberb7319a72013-05-29 14:20:52 -0700313status_t MediaPlayerService::updateProxyConfig(
314 const char *host, int32_t port, const char *exclusionList) {
315 return HTTPBase::UpdateProxyConfig(host, port, exclusionList);
316}
317
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
319{
320 const size_t SIZE = 256;
321 char buffer[SIZE];
322 String8 result;
323
324 result.append(" AudioCache\n");
325 if (mHeap != 0) {
Eric Laurent3d00aa62013-09-24 09:53:27 -0700326 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d)\n",
327 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800328 result.append(buffer);
329 }
Scott Fan7d409692013-04-28 10:13:54 +0800330 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 -0800331 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
332 result.append(buffer);
333 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
334 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
335 result.append(buffer);
336 ::write(fd, result.string(), result.size());
337 return NO_ERROR;
338}
339
340status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
341{
342 const size_t SIZE = 256;
343 char buffer[SIZE];
344 String8 result;
345
346 result.append(" AudioOutput\n");
347 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
348 mStreamType, mLeftVolume, mRightVolume);
349 result.append(buffer);
350 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800351 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700353 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
354 mAuxEffectId, mSendLevel);
355 result.append(buffer);
356
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357 ::write(fd, result.string(), result.size());
358 if (mTrack != 0) {
359 mTrack->dump(fd, args);
360 }
361 return NO_ERROR;
362}
363
364status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
365{
366 const size_t SIZE = 256;
367 char buffer[SIZE];
368 String8 result;
369 result.append(" Client\n");
370 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
371 mPid, mConnId, mStatus, mLoop?"true": "false");
372 result.append(buffer);
373 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700374 if (mPlayer != NULL) {
375 mPlayer->dump(fd, args);
376 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377 if (mAudioOutput != 0) {
378 mAudioOutput->dump(fd, args);
379 }
380 write(fd, "\n", 1);
381 return NO_ERROR;
382}
383
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
385{
386 const size_t SIZE = 256;
387 char buffer[SIZE];
388 String8 result;
389 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
390 snprintf(buffer, SIZE, "Permission Denial: "
391 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
392 IPCThreadState::self()->getCallingPid(),
393 IPCThreadState::self()->getCallingUid());
394 result.append(buffer);
395 } else {
396 Mutex::Autolock lock(mLock);
397 for (int i = 0, n = mClients.size(); i < n; ++i) {
398 sp<Client> c = mClients[i].promote();
399 if (c != 0) c->dump(fd, args);
400 }
James Dongb9141222010-07-08 11:16:11 -0700401 if (mMediaRecorderClients.size() == 0) {
402 result.append(" No media recorder client\n\n");
403 } else {
404 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
405 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700406 if (c != 0) {
407 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
408 result.append(buffer);
409 write(fd, result.string(), result.size());
410 result = "\n";
411 c->dump(fd, args);
412 }
James Dongb9141222010-07-08 11:16:11 -0700413 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700414 }
415
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700417 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418 FILE *f = fopen(buffer, "r");
419 if (f) {
420 while (!feof(f)) {
421 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700422 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700424 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800425 strstr(buffer, " /system/media/")) {
426 result.append(" ");
427 result.append(buffer);
428 }
429 }
430 fclose(f);
431 } else {
432 result.append("couldn't open ");
433 result.append(buffer);
434 result.append("\n");
435 }
436
Glenn Kasten0512ab52011-05-04 17:58:57 -0700437 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 DIR *d = opendir(buffer);
439 if (d) {
440 struct dirent *ent;
441 while((ent = readdir(d)) != NULL) {
442 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700443 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 struct stat s;
445 if (lstat(buffer, &s) == 0) {
446 if ((s.st_mode & S_IFMT) == S_IFLNK) {
447 char linkto[256];
448 int len = readlink(buffer, linkto, sizeof(linkto));
449 if(len > 0) {
450 if(len > 255) {
451 linkto[252] = '.';
452 linkto[253] = '.';
453 linkto[254] = '.';
454 linkto[255] = 0;
455 } else {
456 linkto[len] = 0;
457 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700458 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700460 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 strstr(linkto, "/system/media/") == linkto) {
462 result.append(" ");
463 result.append(buffer);
464 result.append(" -> ");
465 result.append(linkto);
466 result.append("\n");
467 }
468 }
469 } else {
470 result.append(" unexpected type for ");
471 result.append(buffer);
472 result.append("\n");
473 }
474 }
475 }
476 }
477 closedir(d);
478 } else {
479 result.append("couldn't open ");
480 result.append(buffer);
481 result.append("\n");
482 }
483
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 bool dumpMem = false;
485 for (size_t i = 0; i < args.size(); i++) {
486 if (args[i] == String16("-m")) {
487 dumpMem = true;
488 }
489 }
490 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700491 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 }
494 write(fd, result.string(), result.size());
495 return NO_ERROR;
496}
497
498void MediaPlayerService::removeClient(wp<Client> client)
499{
500 Mutex::Autolock lock(mLock);
501 mClients.remove(client);
502}
503
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700504MediaPlayerService::Client::Client(
505 const sp<MediaPlayerService>& service, pid_t pid,
506 int32_t connId, const sp<IMediaPlayerClient>& client,
507 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508{
Steve Block3856b092011-10-20 11:56:00 +0100509 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 mPid = pid;
511 mConnId = connId;
512 mService = service;
513 mClient = client;
514 mLoop = false;
515 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700516 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700517 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800518 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700519
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000521 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522 mAntagonizer = new Antagonizer(notify, this);
523#endif
524}
525
526MediaPlayerService::Client::~Client()
527{
Steve Block3856b092011-10-20 11:56:00 +0100528 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 mAudioOutput.clear();
530 wp<Client> client(this);
531 disconnect();
532 mService->removeClient(client);
533}
534
535void MediaPlayerService::Client::disconnect()
536{
Steve Block3856b092011-10-20 11:56:00 +0100537 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 // grab local reference and clear main reference to prevent future
539 // access to object
540 sp<MediaPlayerBase> p;
541 {
542 Mutex::Autolock l(mLock);
543 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800544 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700546
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 mPlayer.clear();
548
549 // clear the notification to prevent callbacks to dead client
550 // and reset the player. We assume the player will serialize
551 // access to itself if necessary.
552 if (p != 0) {
553 p->setNotifyCallback(0, 0);
554#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000555 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 mAntagonizer->kill();
557#endif
558 p->reset();
559 }
560
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700561 disconnectNativeWindow();
562
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 IPCThreadState::self()->flushCommands();
564}
565
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
567{
568 // determine if we have the right player type
569 sp<MediaPlayerBase> p = mPlayer;
570 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100571 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572 p.clear();
573 }
574 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700575 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700577
Jason Simmonsdb29e522011-08-12 13:46:55 -0700578 if (p != NULL) {
579 p->setUID(mUID);
580 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700581
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 return p;
583}
584
John Grossmanc795b642012-02-22 15:38:35 -0800585sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
586 player_type playerType)
587{
588 ALOGV("player type = %d", playerType);
589
590 // create the right type of player
591 sp<MediaPlayerBase> p = createPlayer(playerType);
592 if (p == NULL) {
593 return p;
594 }
595
596 if (!p->hardwareOutput()) {
597 mAudioOutput = new AudioOutput(mAudioSessionId);
598 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
599 }
600
601 return p;
602}
603
604void MediaPlayerService::Client::setDataSource_post(
605 const sp<MediaPlayerBase>& p,
606 status_t status)
607{
608 ALOGV(" setDataSource");
609 mStatus = status;
610 if (mStatus != OK) {
611 ALOGE(" error: %d", mStatus);
612 return;
613 }
614
615 // Set the re-transmission endpoint if one was chosen.
616 if (mRetransmitEndpointValid) {
617 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
618 if (mStatus != NO_ERROR) {
619 ALOGE("setRetransmitEndpoint error: %d", mStatus);
620 }
621 }
622
623 if (mStatus == OK) {
624 mPlayer = p;
625 }
626}
627
Andreas Huber2db84552010-01-28 11:19:57 -0800628status_t MediaPlayerService::Client::setDataSource(
629 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630{
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632 if (url == NULL)
633 return UNKNOWN_ERROR;
634
Dave Burked681bbb2011-08-30 14:39:17 +0100635 if ((strncmp(url, "http://", 7) == 0) ||
636 (strncmp(url, "https://", 8) == 0) ||
637 (strncmp(url, "rtsp://", 7) == 0)) {
638 if (!checkPermission("android.permission.INTERNET")) {
639 return PERMISSION_DENIED;
640 }
641 }
642
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643 if (strncmp(url, "content://", 10) == 0) {
644 // get a filedescriptor for the content Uri and
645 // pass it to the setDataSource(fd) method
646
647 String16 url16(url);
648 int fd = android::openContentProviderFile(url16);
649 if (fd < 0)
650 {
Steve Block29357bc2012-01-06 19:20:56 +0000651 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652 return UNKNOWN_ERROR;
653 }
654 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
655 close(fd);
656 return mStatus;
657 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700658 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800659 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
660 if (p == NULL) {
661 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662 }
663
John Grossmanc795b642012-02-22 15:38:35 -0800664 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 return mStatus;
666 }
667}
668
669status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
670{
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672 struct stat sb;
673 int ret = fstat(fd, &sb);
674 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000675 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 return UNKNOWN_ERROR;
677 }
678
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("st_dev = %llu", sb.st_dev);
680 ALOGV("st_mode = %u", sb.st_mode);
681 ALOGV("st_uid = %lu", sb.st_uid);
682 ALOGV("st_gid = %lu", sb.st_gid);
683 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684
685 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000686 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687 ::close(fd);
688 return UNKNOWN_ERROR;
689 }
690 if (offset + length > sb.st_size) {
691 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 }
694
John Grossman44a7e422012-06-21 17:29:24 -0700695 player_type playerType = MediaPlayerFactory::getPlayerType(this,
696 fd,
697 offset,
698 length);
John Grossmanc795b642012-02-22 15:38:35 -0800699 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
700 if (p == NULL) {
701 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 }
703
704 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800705 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 return mStatus;
707}
708
Andreas Hubere2b10282010-11-23 11:41:34 -0800709status_t MediaPlayerService::Client::setDataSource(
710 const sp<IStreamSource> &source) {
711 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700712 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800713 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800714 if (p == NULL) {
715 return NO_INIT;
716 }
717
Andreas Hubere2b10282010-11-23 11:41:34 -0800718 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800719 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800720 return mStatus;
721}
722
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700723void MediaPlayerService::Client::disconnectNativeWindow() {
724 if (mConnectedWindow != NULL) {
725 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
726 NATIVE_WINDOW_API_MEDIA);
727
728 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000729 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700730 strerror(-err), err);
731 }
732 }
733 mConnectedWindow.clear();
734}
735
Glenn Kasten11731182011-02-08 17:26:17 -0800736status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800737 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800738{
Andy McFadden484566c2012-12-18 09:46:54 -0800739 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800740 sp<MediaPlayerBase> p = getPlayer();
741 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700742
Andy McFadden484566c2012-12-18 09:46:54 -0800743 sp<IBinder> binder(bufferProducer == NULL ? NULL :
744 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700745 if (mConnectedWindowBinder == binder) {
746 return OK;
747 }
748
749 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800750 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700751 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700752 status_t err = native_window_api_connect(anw.get(),
753 NATIVE_WINDOW_API_MEDIA);
754
755 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000756 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700757 // Note that we must do the reset before disconnecting from the ANW.
758 // Otherwise queue/dequeue calls could be made on the disconnected
759 // ANW, which may result in errors.
760 reset();
761
762 disconnectNativeWindow();
763
764 return err;
765 }
766 }
767
Andy McFadden484566c2012-12-18 09:46:54 -0800768 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700769 // disconnecting the old one. Otherwise queue/dequeue calls could be made
770 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800771 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700772
773 disconnectNativeWindow();
774
775 mConnectedWindow = anw;
776
777 if (err == OK) {
778 mConnectedWindowBinder = binder;
779 } else {
780 disconnectNativeWindow();
781 }
782
783 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800784}
785
Nicolas Catania1d187f12009-05-12 23:25:55 -0700786status_t MediaPlayerService::Client::invoke(const Parcel& request,
787 Parcel *reply)
788{
789 sp<MediaPlayerBase> p = getPlayer();
790 if (p == NULL) return UNKNOWN_ERROR;
791 return p->invoke(request, reply);
792}
793
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700794// This call doesn't need to access the native player.
795status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
796{
797 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700798 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700799
Nicolas Catania48290382009-07-10 13:53:06 -0700800 if (unmarshallFilter(filter, &allow, &status) &&
801 unmarshallFilter(filter, &drop, &status)) {
802 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700803
804 mMetadataAllow = allow;
805 mMetadataDrop = drop;
806 }
807 return status;
808}
809
Nicolas Catania48290382009-07-10 13:53:06 -0700810status_t MediaPlayerService::Client::getMetadata(
811 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700812{
nikoa64c8c72009-07-20 15:07:26 -0700813 sp<MediaPlayerBase> player = getPlayer();
814 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700815
nikod608a812009-07-16 16:39:53 -0700816 status_t status;
817 // Placeholder for the return code, updated by the caller.
818 reply->writeInt32(-1);
819
nikoa64c8c72009-07-20 15:07:26 -0700820 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700821
822 // We don't block notifications while we fetch the data. We clear
823 // mMetadataUpdated first so we don't lose notifications happening
824 // during the rest of this call.
825 {
826 Mutex::Autolock lock(mLock);
827 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700828 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700829 }
830 mMetadataUpdated.clear();
831 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700832
nikoa64c8c72009-07-20 15:07:26 -0700833 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700834
nikoa64c8c72009-07-20 15:07:26 -0700835 metadata.appendHeader();
836 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700837
838 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700839 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000840 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700841 return status;
842 }
843
844 // FIXME: Implement filtering on the result. Not critical since
845 // filtering takes place on the update notifications already. This
846 // would be when all the metadata are fetch and a filter is set.
847
nikod608a812009-07-16 16:39:53 -0700848 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700849 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700850 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700851}
852
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853status_t MediaPlayerService::Client::prepareAsync()
854{
Steve Block3856b092011-10-20 11:56:00 +0100855 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856 sp<MediaPlayerBase> p = getPlayer();
857 if (p == 0) return UNKNOWN_ERROR;
858 status_t ret = p->prepareAsync();
859#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000860 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 if (ret == NO_ERROR) mAntagonizer->start();
862#endif
863 return ret;
864}
865
866status_t MediaPlayerService::Client::start()
867{
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800869 sp<MediaPlayerBase> p = getPlayer();
870 if (p == 0) return UNKNOWN_ERROR;
871 p->setLooping(mLoop);
872 return p->start();
873}
874
875status_t MediaPlayerService::Client::stop()
876{
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800878 sp<MediaPlayerBase> p = getPlayer();
879 if (p == 0) return UNKNOWN_ERROR;
880 return p->stop();
881}
882
883status_t MediaPlayerService::Client::pause()
884{
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 sp<MediaPlayerBase> p = getPlayer();
887 if (p == 0) return UNKNOWN_ERROR;
888 return p->pause();
889}
890
891status_t MediaPlayerService::Client::isPlaying(bool* state)
892{
893 *state = false;
894 sp<MediaPlayerBase> p = getPlayer();
895 if (p == 0) return UNKNOWN_ERROR;
896 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100897 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800898 return NO_ERROR;
899}
900
901status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
902{
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800904 sp<MediaPlayerBase> p = getPlayer();
905 if (p == 0) return UNKNOWN_ERROR;
906 status_t ret = p->getCurrentPosition(msec);
907 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000910 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800911 }
912 return ret;
913}
914
915status_t MediaPlayerService::Client::getDuration(int *msec)
916{
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 sp<MediaPlayerBase> p = getPlayer();
919 if (p == 0) return UNKNOWN_ERROR;
920 status_t ret = p->getDuration(msec);
921 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800923 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000924 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925 }
926 return ret;
927}
928
Marco Nelissen6b74d672012-02-28 16:07:44 -0800929status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
930 ALOGV("setNextPlayer");
931 Mutex::Autolock l(mLock);
932 sp<Client> c = static_cast<Client*>(player.get());
933 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -0700934
935 if (c != NULL) {
936 if (mAudioOutput != NULL) {
937 mAudioOutput->setNextOutput(c->mAudioOutput);
938 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
939 ALOGE("no current audio output");
940 }
941
942 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
943 mPlayer->setNextPlayer(mNextClient->getPlayer());
944 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800945 }
John Grossman5f7e55e2012-08-24 14:47:25 -0700946
Marco Nelissen6b74d672012-02-28 16:07:44 -0800947 return OK;
948}
949
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950status_t MediaPlayerService::Client::seekTo(int msec)
951{
Steve Block3856b092011-10-20 11:56:00 +0100952 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953 sp<MediaPlayerBase> p = getPlayer();
954 if (p == 0) return UNKNOWN_ERROR;
955 return p->seekTo(msec);
956}
957
958status_t MediaPlayerService::Client::reset()
959{
Steve Block3856b092011-10-20 11:56:00 +0100960 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800961 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 sp<MediaPlayerBase> p = getPlayer();
963 if (p == 0) return UNKNOWN_ERROR;
964 return p->reset();
965}
966
Glenn Kastenfff6d712012-01-12 16:38:12 -0800967status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800968{
Steve Block3856b092011-10-20 11:56:00 +0100969 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800970 // TODO: for hardware output, call player instead
971 Mutex::Autolock l(mLock);
972 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
973 return NO_ERROR;
974}
975
976status_t MediaPlayerService::Client::setLooping(int loop)
977{
Steve Block3856b092011-10-20 11:56:00 +0100978 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 mLoop = loop;
980 sp<MediaPlayerBase> p = getPlayer();
981 if (p != 0) return p->setLooping(loop);
982 return NO_ERROR;
983}
984
985status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
986{
Steve Block3856b092011-10-20 11:56:00 +0100987 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800988
989 // for hardware output, call player instead
990 sp<MediaPlayerBase> p = getPlayer();
991 {
992 Mutex::Autolock l(mLock);
993 if (p != 0 && p->hardwareOutput()) {
994 MediaPlayerHWInterface* hwp =
995 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
996 return hwp->setVolume(leftVolume, rightVolume);
997 } else {
998 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
999 return NO_ERROR;
1000 }
1001 }
1002
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001003 return NO_ERROR;
1004}
1005
Eric Laurent2beeb502010-07-16 07:43:46 -07001006status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1007{
Steve Block3856b092011-10-20 11:56:00 +01001008 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001009 Mutex::Autolock l(mLock);
1010 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1011 return NO_ERROR;
1012}
1013
1014status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1015{
Steve Block3856b092011-10-20 11:56:00 +01001016 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001017 Mutex::Autolock l(mLock);
1018 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1019 return NO_ERROR;
1020}
Nicolas Catania48290382009-07-10 13:53:06 -07001021
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001022status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001023 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001024 sp<MediaPlayerBase> p = getPlayer();
1025 if (p == 0) return UNKNOWN_ERROR;
1026 return p->setParameter(key, request);
1027}
1028
1029status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001030 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001031 sp<MediaPlayerBase> p = getPlayer();
1032 if (p == 0) return UNKNOWN_ERROR;
1033 return p->getParameter(key, reply);
1034}
1035
John Grossmanc795b642012-02-22 15:38:35 -08001036status_t MediaPlayerService::Client::setRetransmitEndpoint(
1037 const struct sockaddr_in* endpoint) {
1038
1039 if (NULL != endpoint) {
1040 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1041 uint16_t p = ntohs(endpoint->sin_port);
1042 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1043 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1044 } else {
1045 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1046 }
1047
1048 sp<MediaPlayerBase> p = getPlayer();
1049
1050 // Right now, the only valid time to set a retransmit endpoint is before
1051 // player selection has been made (since the presence or absence of a
1052 // retransmit endpoint is going to determine which player is selected during
1053 // setDataSource).
1054 if (p != 0) return INVALID_OPERATION;
1055
1056 if (NULL != endpoint) {
1057 mRetransmitEndpoint = *endpoint;
1058 mRetransmitEndpointValid = true;
1059 } else {
1060 mRetransmitEndpointValid = false;
1061 }
1062
1063 return NO_ERROR;
1064}
1065
John Grossman44a7e422012-06-21 17:29:24 -07001066status_t MediaPlayerService::Client::getRetransmitEndpoint(
1067 struct sockaddr_in* endpoint)
1068{
1069 if (NULL == endpoint)
1070 return BAD_VALUE;
1071
1072 sp<MediaPlayerBase> p = getPlayer();
1073
1074 if (p != NULL)
1075 return p->getRetransmitEndpoint(endpoint);
1076
1077 if (!mRetransmitEndpointValid)
1078 return NO_INIT;
1079
1080 *endpoint = mRetransmitEndpoint;
1081
1082 return NO_ERROR;
1083}
1084
Gloria Wangb483c472011-04-11 17:23:27 -07001085void MediaPlayerService::Client::notify(
1086 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001087{
1088 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001089 if (client == NULL) {
1090 return;
1091 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001092
James Dongb8a98252012-08-26 16:13:03 -07001093 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001094 {
1095 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001096 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001097 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001098 if (client->mAudioOutput != NULL)
1099 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001100 client->mNextClient->start();
1101 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1102 }
1103 }
1104
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001105 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001106 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001107 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001108
1109 if(client->shouldDropMetadata(metadata_type)) {
1110 return;
1111 }
1112
1113 // Update the list of metadata that have changed. getMetadata
1114 // also access mMetadataUpdated and clears it.
1115 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001116 }
James Dongb8a98252012-08-26 16:13:03 -07001117
1118 if (c != NULL) {
1119 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1120 c->notify(msg, ext1, ext2, obj);
1121 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001122}
1123
Nicolas Catania48290382009-07-10 13:53:06 -07001124
nikoa64c8c72009-07-20 15:07:26 -07001125bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001126{
Nicolas Catania48290382009-07-10 13:53:06 -07001127 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001128
Nicolas Catania48290382009-07-10 13:53:06 -07001129 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001130 return true;
1131 }
1132
Nicolas Catania48290382009-07-10 13:53:06 -07001133 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001134 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001135 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001136 return true;
1137 }
1138}
1139
Nicolas Catania48290382009-07-10 13:53:06 -07001140
nikoa64c8c72009-07-20 15:07:26 -07001141void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001142 Mutex::Autolock lock(mLock);
1143 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1144 mMetadataUpdated.add(metadata_type);
1145 }
1146}
1147
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001148#if CALLBACK_ANTAGONIZER
1149const int Antagonizer::interval = 10000; // 10 msecs
1150
1151Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1152 mExit(false), mActive(false), mClient(client), mCb(cb)
1153{
1154 createThread(callbackThread, this);
1155}
1156
1157void Antagonizer::kill()
1158{
1159 Mutex::Autolock _l(mLock);
1160 mActive = false;
1161 mExit = true;
1162 mCondition.wait(mLock);
1163}
1164
1165int Antagonizer::callbackThread(void* user)
1166{
Steve Blockb8a80522011-12-20 16:23:08 +00001167 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001168 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1169 while (!p->mExit) {
1170 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001171 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001172 p->mCb(p->mClient, 0, 0, 0);
1173 }
1174 usleep(interval);
1175 }
1176 Mutex::Autolock _l(p->mLock);
1177 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001178 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001179 return 0;
1180}
1181#endif
1182
Eric Laurent3d00aa62013-09-24 09:53:27 -07001183status_t MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels,
1184 audio_format_t* pFormat,
1185 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001186{
Steve Block3856b092011-10-20 11:56:00 +01001187 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001189 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001190
1191 // Protect our precious, precious DRMd ringtones by only allowing
1192 // decoding of http, but not filesystem paths or content Uris.
1193 // If the application wants to decode those, it should open a
1194 // filedescriptor for them and use that.
1195 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001196 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001197 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001198 }
1199
John Grossman44a7e422012-06-21 17:29:24 -07001200 player_type playerType =
1201 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001202 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203
1204 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001205 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001206 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 if (player == NULL) goto Exit;
1208 if (player->hardwareOutput()) goto Exit;
1209
1210 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1211
1212 // set data source
1213 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1214
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 player->prepareAsync();
1217
Steve Block3856b092011-10-20 11:56:00 +01001218 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001219 if (cache->wait() != NO_ERROR) goto Exit;
1220
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222 player->start();
1223
Steve Block3856b092011-10-20 11:56:00 +01001224 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001225 cache->wait();
1226 // in case of error, return what was successfully decoded.
1227 if (cache->size() == 0) {
1228 goto Exit;
1229 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230
Eric Laurent3d00aa62013-09-24 09:53:27 -07001231 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001232 *pSampleRate = cache->sampleRate();
1233 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001234 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001235 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1236 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1237 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001238
1239Exit:
1240 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001241 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001242}
1243
Eric Laurent3d00aa62013-09-24 09:53:27 -07001244status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1245 uint32_t *pSampleRate, int* pNumChannels,
1246 audio_format_t* pFormat,
1247 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001248{
Steve Block3856b092011-10-20 11:56:00 +01001249 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001251 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001252
John Grossman44a7e422012-06-21 17:29:24 -07001253 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1254 fd,
1255 offset,
1256 length);
Steve Block3856b092011-10-20 11:56:00 +01001257 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258
1259 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001260 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001261 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 if (player == NULL) goto Exit;
1263 if (player->hardwareOutput()) goto Exit;
1264
1265 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1266
1267 // set data source
1268 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1269
Steve Block3856b092011-10-20 11:56:00 +01001270 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271 player->prepareAsync();
1272
Steve Block3856b092011-10-20 11:56:00 +01001273 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 if (cache->wait() != NO_ERROR) goto Exit;
1275
Steve Block3856b092011-10-20 11:56:00 +01001276 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277 player->start();
1278
Steve Block3856b092011-10-20 11:56:00 +01001279 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001280 cache->wait();
1281 // in case of error, return what was successfully decoded.
1282 if (cache->size() == 0) {
1283 goto Exit;
1284 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285
Eric Laurent3d00aa62013-09-24 09:53:27 -07001286 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287 *pSampleRate = cache->sampleRate();
1288 *pNumChannels = cache->channelCount();
1289 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001290 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1291 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1292 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001293
1294Exit:
1295 if (player != 0) player->reset();
1296 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001297 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298}
1299
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001300
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301#undef LOG_TAG
1302#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001303MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001304 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001305 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001306 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001307 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001308 mSessionId(sessionId),
1309 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001310 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001311 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001312 mLeftVolume = 1.0;
1313 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001314 mPlaybackRatePermille = 1000;
1315 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001317 mAuxEffectId = 0;
1318 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 setMinBufferCount();
1320}
1321
1322MediaPlayerService::AudioOutput::~AudioOutput()
1323{
1324 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001325 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326}
1327
1328void MediaPlayerService::AudioOutput::setMinBufferCount()
1329{
1330 char value[PROPERTY_VALUE_MAX];
1331 if (property_get("ro.kernel.qemu", value, 0)) {
1332 mIsOnEmulator = true;
1333 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1334 }
1335}
1336
1337bool MediaPlayerService::AudioOutput::isOnEmulator()
1338{
1339 setMinBufferCount();
1340 return mIsOnEmulator;
1341}
1342
1343int MediaPlayerService::AudioOutput::getMinBufferCount()
1344{
1345 setMinBufferCount();
1346 return mMinBufferCount;
1347}
1348
1349ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1350{
1351 if (mTrack == 0) return NO_INIT;
1352 return mTrack->frameCount() * frameSize();
1353}
1354
1355ssize_t MediaPlayerService::AudioOutput::frameCount() const
1356{
1357 if (mTrack == 0) return NO_INIT;
1358 return mTrack->frameCount();
1359}
1360
1361ssize_t MediaPlayerService::AudioOutput::channelCount() const
1362{
1363 if (mTrack == 0) return NO_INIT;
1364 return mTrack->channelCount();
1365}
1366
1367ssize_t MediaPlayerService::AudioOutput::frameSize() const
1368{
1369 if (mTrack == 0) return NO_INIT;
1370 return mTrack->frameSize();
1371}
1372
1373uint32_t MediaPlayerService::AudioOutput::latency () const
1374{
Eric Laurentdb354e52012-03-05 17:27:11 -08001375 if (mTrack == 0) return 0;
1376 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001377}
1378
Alok Kediya601e9b52013-09-27 11:55:10 +05301379audio_stream_type_t MediaPlayerService::AudioOutput::streamType () const
1380{
1381 return mStreamType;
1382}
1383
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001384float MediaPlayerService::AudioOutput::msecsPerFrame() const
1385{
1386 return mMsecsPerFrame;
1387}
1388
Marco Nelissen4110c102012-03-29 09:31:28 -07001389status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001390{
1391 if (mTrack == 0) return NO_INIT;
1392 return mTrack->getPosition(position);
1393}
1394
Alok Kediya601e9b52013-09-27 11:55:10 +05301395ssize_t MediaPlayerService::AudioOutput::sampleRate() const
1396{
1397 if (mTrack == 0) return NO_INIT;
1398 return DEFAULT_SAMPLE_RATE;
1399}
1400
1401status_t MediaPlayerService::AudioOutput::getTimeStamp(uint64_t *tstamp)
1402{
1403 if (tstamp == 0) return BAD_VALUE;
1404 if (mTrack == 0) return NO_INIT;
1405 mTrack->getTimeStamp(tstamp);
1406 return NO_ERROR;
1407}
1408
Marco Nelissen4110c102012-03-29 09:31:28 -07001409status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1410{
1411 if (mTrack == 0) return NO_INIT;
1412 *frameswritten = mBytesWritten / frameSize();
1413 return OK;
1414}
1415
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001416status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1417{
1418 if (mTrack == 0) return NO_INIT;
1419 return mTrack->setParameters(keyValuePairs);
1420}
1421
1422String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1423{
1424 if (mTrack == 0) return String8::empty();
1425 return mTrack->getParameters(keys);
1426}
1427
1428void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1429{
1430 ALOGV("deleteRecycledTrack");
1431
1432 if (mRecycledTrack != 0) {
1433
1434 if (mCallbackData != NULL) {
1435 mCallbackData->setOutput(NULL);
1436 mCallbackData->endTrackSwitch();
1437 }
1438
1439 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1440 mRecycledTrack->flush();
1441 }
1442 // An offloaded track isn't flushed because the STREAM_END is reported
1443 // slightly prematurely to allow time for the gapless track switch
1444 // but this means that if we decide not to recycle the track there
1445 // could be a small amount of residual data still playing. We leave
1446 // AudioFlinger to drain the track.
1447
1448 mRecycledTrack.clear();
1449 delete mCallbackData;
1450 mCallbackData = NULL;
1451 close();
1452 }
1453}
1454
Andreas Huber20111aa2009-07-14 16:56:47 -07001455status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001456 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1457 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001458 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001459 audio_output_flags_t flags,
1460 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001461{
Andreas Huber20111aa2009-07-14 16:56:47 -07001462 mCallback = cb;
1463 mCallbackCookie = cookie;
Alok Kediya601e9b52013-09-27 11:55:10 +05301464 if (flags & AUDIO_OUTPUT_FLAG_LPA || flags & AUDIO_OUTPUT_FLAG_TUNNEL) {
1465 ALOGV("AudioOutput open: with flags %x",flags);
1466 channelMask = audio_channel_out_mask_from_count(channelCount);
1467 if (0 == channelMask) {
1468 ALOGE("open() error, can't derive mask for %d audio channels", channelCount);
1469 return NO_INIT;
1470 }
1471 AudioTrack *audioTrack = NULL;
1472 CallbackData *newcbd = NULL;
1473 if (mCallback != NULL) {
1474 newcbd = new CallbackData(this);
1475 audioTrack = new AudioTrack(
1476 mStreamType,
1477 sampleRate,
1478 format,
1479 channelMask,
1480 0,
1481 flags,
1482 CallbackWrapper,
1483 newcbd,
1484 0,
1485 mSessionId);
1486 if ((audioTrack == 0) || (audioTrack->initCheck() != NO_ERROR)) {
1487 ALOGE("Unable to create audio track");
1488 delete audioTrack;
1489 delete newcbd;
1490 return NO_INIT;
1491 }
1492 } else {
1493 ALOGE("no callback supplied");
1494 return NO_INIT;
1495 }
1496 deleteRecycledTrack();
Andreas Huber20111aa2009-07-14 16:56:47 -07001497
Alok Kediya601e9b52013-09-27 11:55:10 +05301498 ALOGV("setVolume");
1499 mCallbackData = newcbd;
1500 audioTrack->setVolume(mLeftVolume, mRightVolume);
1501 mSampleRateHz = sampleRate;
1502 mFlags = flags;
1503 mTrack = audioTrack;
1504 return NO_ERROR;
1505 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001506 // Check argument "bufferCount" against the mininum buffer count
1507 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001508 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001509 bufferCount = mMinBufferCount;
1510
1511 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001512 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1513 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001514 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001515 size_t afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001516 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001517
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001518 // offloading is only supported in callback mode for now.
1519 // offloadInfo must be present if offload flag is set
1520 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1521 ((cb == NULL) || (offloadInfo == NULL))) {
1522 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001523 }
1524
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001525 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1526 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1527 } else {
1528 uint32_t afSampleRate;
1529 size_t afFrameCount;
1530
1531 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1532 return NO_INIT;
1533 }
1534 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1535 return NO_INIT;
1536 }
1537
1538 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1539 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001540
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001541 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001542 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001543 if (0 == channelMask) {
1544 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1545 return NO_INIT;
1546 }
1547 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001548
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001549 // Check whether we can recycle the track
1550 bool reuse = false;
1551 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001552
Glenn Kasten2799d742013-05-30 14:33:29 -07001553 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001554 // check whether we are switching between two offloaded tracks
1555 bothOffloaded = (flags & mRecycledTrack->getFlags()
1556 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001557
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001558 // check if the existing track can be reused as-is, or if a new track needs to be created.
1559 reuse = true;
1560
Marco Nelissen67295b52012-06-11 14:52:53 -07001561 if ((mCallbackData == NULL && mCallback != NULL) ||
1562 (mCallbackData != NULL && mCallback == NULL)) {
1563 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1564 ALOGV("can't chain callback and write");
1565 reuse = false;
1566 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001567 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1568 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001569 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001570 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001571 reuse = false;
1572 } else if (flags != mFlags) {
1573 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1574 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001575 } else if (mRecycledTrack->format() != format) {
1576 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001577 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001578 } else {
1579 ALOGV("no track available to recycle");
1580 }
1581
1582 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1583
1584 // If we can't recycle and both tracks are offloaded
1585 // we must close the previous output before opening a new one
1586 if (bothOffloaded && !reuse) {
1587 ALOGV("both offloaded and not recycling");
1588 deleteRecycledTrack();
1589 }
1590
1591 sp<AudioTrack> t;
1592 CallbackData *newcbd = NULL;
1593
1594 // We don't attempt to create a new track if we are recycling an
1595 // offloaded track. But, if we are recycling a non-offloaded or we
1596 // are switching where one is offloaded and one isn't then we create
1597 // the new track in advance so that we can read additional stream info
1598
1599 if (!(reuse && bothOffloaded)) {
1600 ALOGV("creating new AudioTrack");
1601
1602 if (mCallback != NULL) {
1603 newcbd = new CallbackData(this);
1604 t = new AudioTrack(
1605 mStreamType,
1606 sampleRate,
1607 format,
1608 channelMask,
1609 frameCount,
1610 flags,
1611 CallbackWrapper,
1612 newcbd,
1613 0, // notification frames
1614 mSessionId,
1615 AudioTrack::TRANSFER_CALLBACK,
1616 offloadInfo);
1617 } else {
1618 t = new AudioTrack(
1619 mStreamType,
1620 sampleRate,
1621 format,
1622 channelMask,
1623 frameCount,
1624 flags,
1625 NULL,
1626 NULL,
1627 0,
1628 mSessionId);
1629 }
1630
1631 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1632 ALOGE("Unable to create audio track");
1633 delete newcbd;
1634 return NO_INIT;
1635 }
1636 }
1637
1638 if (reuse) {
1639 CHECK(mRecycledTrack != NULL);
1640
1641 if (!bothOffloaded) {
1642 if (mRecycledTrack->frameCount() != t->frameCount()) {
1643 ALOGV("framecount differs: %u/%u frames",
1644 mRecycledTrack->frameCount(), t->frameCount());
1645 reuse = false;
1646 }
1647 }
1648
Marco Nelissen67295b52012-06-11 14:52:53 -07001649 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001650 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001651 close();
1652 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001653 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001654 if (mCallbackData != NULL) {
1655 mCallbackData->setOutput(this);
1656 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001657 delete newcbd;
1658 return OK;
1659 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001660 }
1661
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001662 // we're not going to reuse the track, unblock and flush it
1663 // this was done earlier if both tracks are offloaded
1664 if (!bothOffloaded) {
1665 deleteRecycledTrack();
1666 }
1667
1668 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1669
Marco Nelissen67295b52012-06-11 14:52:53 -07001670 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001671 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001672 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001673
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001674 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001675 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001676 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001677 uint32_t pos;
1678 if (t->getPosition(&pos) == OK) {
1679 mBytesWritten = uint64_t(pos) * t->frameSize();
1680 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001681 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001682
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001683 status_t res = NO_ERROR;
1684 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1685 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1686 if (res == NO_ERROR) {
1687 t->setAuxEffectSendLevel(mSendLevel);
1688 res = t->attachAuxEffect(mAuxEffectId);
1689 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001690 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001691 ALOGV("open() DONE status %d", res);
1692 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001693}
1694
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001695status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001696{
Steve Block3856b092011-10-20 11:56:00 +01001697 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001698 if (mCallbackData != NULL) {
1699 mCallbackData->endTrackSwitch();
1700 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001701 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001702 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001703 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001704 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001705 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001706 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001707}
1708
Marco Nelissen6b74d672012-02-28 16:07:44 -08001709void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1710 mNextOutput = nextOutput;
1711}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001712
1713
Marco Nelissen6b74d672012-02-28 16:07:44 -08001714void MediaPlayerService::AudioOutput::switchToNextOutput() {
1715 ALOGV("switchToNextOutput");
1716 if (mNextOutput != NULL) {
1717 if (mCallbackData != NULL) {
1718 mCallbackData->beginTrackSwitch();
1719 }
1720 delete mNextOutput->mCallbackData;
1721 mNextOutput->mCallbackData = mCallbackData;
1722 mCallbackData = NULL;
1723 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001724 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001725 mNextOutput->mSampleRateHz = mSampleRateHz;
1726 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001727 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001728 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001729 }
1730}
1731
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001732ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1733{
Andreas Huber20111aa2009-07-14 16:56:47 -07001734
Steve Block3856b092011-10-20 11:56:00 +01001735 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001736 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001737 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001738 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001739 return ret;
1740 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001741 return NO_INIT;
1742}
1743
1744void MediaPlayerService::AudioOutput::stop()
1745{
Steve Block3856b092011-10-20 11:56:00 +01001746 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001747 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001748}
1749
1750void MediaPlayerService::AudioOutput::flush()
1751{
Steve Block3856b092011-10-20 11:56:00 +01001752 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001753 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001754}
1755
1756void MediaPlayerService::AudioOutput::pause()
1757{
Steve Block3856b092011-10-20 11:56:00 +01001758 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001759 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001760}
1761
1762void MediaPlayerService::AudioOutput::close()
1763{
Steve Block3856b092011-10-20 11:56:00 +01001764 ALOGV("close");
Alok Kediya601e9b52013-09-27 11:55:10 +05301765 if (mTrack != 0) mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001766}
1767
1768void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1769{
Steve Block3856b092011-10-20 11:56:00 +01001770 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001771 mLeftVolume = left;
1772 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001773 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001774 mTrack->setVolume(left, right);
1775 }
1776}
1777
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001778status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1779{
1780 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1781 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001782 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001783 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1784 } else {
1785 res = NO_INIT;
1786 }
1787 mPlaybackRatePermille = ratePermille;
1788 if (mSampleRateHz != 0) {
1789 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1790 }
1791 return res;
1792}
1793
Eric Laurent2beeb502010-07-16 07:43:46 -07001794status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1795{
Steve Block3856b092011-10-20 11:56:00 +01001796 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001797 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001798 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001799 return mTrack->setAuxEffectSendLevel(level);
1800 }
1801 return NO_ERROR;
1802}
1803
1804status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1805{
Steve Block3856b092011-10-20 11:56:00 +01001806 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001807 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001808 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001809 return mTrack->attachAuxEffect(effectId);
1810 }
1811 return NO_ERROR;
1812}
1813
Andreas Huber20111aa2009-07-14 16:56:47 -07001814// static
1815void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001816 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001817 //ALOGV("callbackwrapper");
Alok Kediya601e9b52013-09-27 11:55:10 +05301818 if (event == AudioTrack::EVENT_UNDERRUN) {
1819 ALOGW("Event underrun");
1820 CallbackData *data = (CallbackData*)cookie;
1821 data->lock();
1822 AudioOutput *me = data->getOutput();
1823 if (me == NULL) {
1824 // no output set, likely because the track was scheduled to be reused
1825 // by another player, but the format turned out to be incompatible.
1826 data->unlock();
1827 return;
1828 }
1829 ALOGD("Callback!!!");
1830 (*me->mCallback)(
1831 me, NULL, (size_t)AudioTrack::EVENT_UNDERRUN, me->mCallbackCookie, CB_EVENT_UNDERRUN);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001832 data->unlock();
Alok Kediya601e9b52013-09-27 11:55:10 +05301833 return;
1834 }
1835 if (event == AudioTrack::EVENT_HW_FAIL) {
1836 ALOGW("Event hardware failure");
1837 CallbackData *data = (CallbackData*)cookie;
1838 if (data != NULL) {
1839 data->lock();
1840 AudioOutput *me = data->getOutput();
1841 if (me == NULL) {
1842 // no output set, likely because the track was
1843 // scheduled to be reused
1844 // by another player, but the format turned out
1845 // to be incompatible.
1846 data->unlock();
1847 return;
1848 }
1849 ALOGV("Callback!!!");
1850 (*me->mCallback)(me, NULL, (size_t)AudioTrack::EVENT_HW_FAIL,
1851 me->mCallbackCookie, CB_EVENT_HW_FAIL);
1852 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001853 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001854 return;
1855 }
Alok Kediya601e9b52013-09-27 11:55:10 +05301856 if (event == AudioTrack::EVENT_MORE_DATA) {
1857 CallbackData *data = (CallbackData*)cookie;
1858 data->lock();
1859 AudioOutput *me = data->getOutput();
1860 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1861 if (me == NULL) {
1862 // no output set, likely because the track was scheduled to be reused
1863 // by another player, but the format turned out to be incompatible.
1864 data->unlock();
1865 if (buffer != NULL) {
1866 buffer->size = 0;
1867 }
1868 return;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001869 }
1870
Alok Kediya601e9b52013-09-27 11:55:10 +05301871 switch(event) {
1872 case AudioTrack::EVENT_MORE_DATA: {
1873 size_t actualSize = (*me->mCallback)(
1874 me, buffer->raw, buffer->size, me->mCallbackCookie,
1875 CB_EVENT_FILL_BUFFER);
1876
1877 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
1878 // We've reached EOS but the audio track is not stopped yet,
1879 // keep playing silence.
1880
1881 memset(buffer->raw, 0, buffer->size);
1882 actualSize = buffer->size;
1883 }
1884
1885 buffer->size = actualSize;
1886 } break;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001887
1888
Alok Kediya601e9b52013-09-27 11:55:10 +05301889 case AudioTrack::EVENT_STREAM_END:
1890 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1891 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1892 me->mCallbackCookie, CB_EVENT_STREAM_END);
1893 break;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001894
Alok Kediya601e9b52013-09-27 11:55:10 +05301895 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1896 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1897 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1898 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1899 break;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001900
Alok Kediya601e9b52013-09-27 11:55:10 +05301901 default:
1902 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
1903 }
1904
1905 data->unlock();
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001906 }
1907
Alok Kediya601e9b52013-09-27 11:55:10 +05301908 return;
Andreas Huber20111aa2009-07-14 16:56:47 -07001909}
1910
Marco Nelissen4110c102012-03-29 09:31:28 -07001911int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001912{
1913 return mSessionId;
1914}
1915
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001916#undef LOG_TAG
1917#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001918MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1919 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1920 mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001921{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001922}
1923
1924uint32_t MediaPlayerService::AudioCache::latency () const
1925{
1926 return 0;
1927}
1928
1929float MediaPlayerService::AudioCache::msecsPerFrame() const
1930{
1931 return mMsecsPerFrame;
1932}
1933
Marco Nelissen4110c102012-03-29 09:31:28 -07001934status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001935{
1936 if (position == 0) return BAD_VALUE;
1937 *position = mSize;
1938 return NO_ERROR;
1939}
1940
Alok Kediya601e9b52013-09-27 11:55:10 +05301941ssize_t MediaPlayerService::AudioCache::sampleRate() const
1942{
1943 return mSampleRate;
1944}
1945
Marco Nelissen4110c102012-03-29 09:31:28 -07001946status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1947{
1948 if (written == 0) return BAD_VALUE;
1949 *written = mSize;
1950 return NO_ERROR;
1951}
1952
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001953////////////////////////////////////////////////////////////////////////////////
1954
1955struct CallbackThread : public Thread {
1956 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1957 MediaPlayerBase::AudioSink::AudioCallback cb,
1958 void *cookie);
1959
1960protected:
1961 virtual ~CallbackThread();
1962
1963 virtual bool threadLoop();
1964
1965private:
1966 wp<MediaPlayerBase::AudioSink> mSink;
1967 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1968 void *mCookie;
1969 void *mBuffer;
1970 size_t mBufferSize;
1971
1972 CallbackThread(const CallbackThread &);
1973 CallbackThread &operator=(const CallbackThread &);
1974};
1975
1976CallbackThread::CallbackThread(
1977 const wp<MediaPlayerBase::AudioSink> &sink,
1978 MediaPlayerBase::AudioSink::AudioCallback cb,
1979 void *cookie)
1980 : mSink(sink),
1981 mCallback(cb),
1982 mCookie(cookie),
1983 mBuffer(NULL),
1984 mBufferSize(0) {
1985}
1986
1987CallbackThread::~CallbackThread() {
1988 if (mBuffer) {
1989 free(mBuffer);
1990 mBuffer = NULL;
1991 }
1992}
1993
1994bool CallbackThread::threadLoop() {
1995 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1996 if (sink == NULL) {
1997 return false;
1998 }
1999
2000 if (mBuffer == NULL) {
2001 mBufferSize = sink->bufferSize();
2002 mBuffer = malloc(mBufferSize);
2003 }
2004
2005 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002006 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2007 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002008
2009 if (actualSize > 0) {
2010 sink->write(mBuffer, actualSize);
2011 }
2012
2013 return true;
2014}
2015
2016////////////////////////////////////////////////////////////////////////////////
2017
Andreas Huber20111aa2009-07-14 16:56:47 -07002018status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002019 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2020 audio_format_t format, int bufferCount,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002021 AudioCallback cb, void *cookie, audio_output_flags_t flags,
2022 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002023{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002024 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08002025 if (mHeap->getHeapID() < 0) {
2026 return NO_INIT;
2027 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002028
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002029 mSampleRate = sampleRate;
2030 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08002031 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002032 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002033
2034 if (cb != NULL) {
2035 mCallbackThread = new CallbackThread(this, cb, cookie);
2036 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002037 return NO_ERROR;
2038}
2039
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002040status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002041 if (mCallbackThread != NULL) {
2042 mCallbackThread->run("AudioCache callback");
2043 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002044 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002045}
2046
2047void MediaPlayerService::AudioCache::stop() {
2048 if (mCallbackThread != NULL) {
2049 mCallbackThread->requestExitAndWait();
2050 }
2051}
2052
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002053ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
2054{
Steve Block3856b092011-10-20 11:56:00 +01002055 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002056 if ((buffer == 0) || (size == 0)) return size;
2057
2058 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
2059 if (p == NULL) return NO_INIT;
2060 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01002061 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002062 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00002063 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002064 size = mHeap->getSize() - mSize;
2065 }
2066 memcpy(p, buffer, size);
2067 mSize += size;
2068 return size;
2069}
2070
2071// call with lock held
2072status_t MediaPlayerService::AudioCache::wait()
2073{
2074 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08002075 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002076 mSignal.wait(mLock);
2077 }
2078 mCommandComplete = false;
2079
2080 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01002081 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002082 } else {
Steve Block3856b092011-10-20 11:56:00 +01002083 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002084 }
2085 return mError;
2086}
2087
Gloria Wangb483c472011-04-11 17:23:27 -07002088void MediaPlayerService::AudioCache::notify(
2089 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002090{
Steve Block3856b092011-10-20 11:56:00 +01002091 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002092 AudioCache* p = static_cast<AudioCache*>(cookie);
2093
2094 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002095 switch (msg)
2096 {
2097 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002098 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002099 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08002100 break;
2101 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002102 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002103 break;
2104 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002105 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002106 break;
2107 default:
Steve Block3856b092011-10-20 11:56:00 +01002108 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002109 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002110 }
2111
2112 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002113 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002114 p->mCommandComplete = true;
2115 p->mSignal.signal();
2116}
2117
Marco Nelissen4110c102012-03-29 09:31:28 -07002118int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002119{
2120 return 0;
2121}
2122
Gloria Wang7cf180c2011-02-19 18:37:57 -08002123void MediaPlayerService::addBatteryData(uint32_t params)
2124{
2125 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002126
2127 int32_t time = systemTime() / 1000000L;
2128
2129 // change audio output devices. This notification comes from AudioFlinger
2130 if ((params & kBatteryDataSpeakerOn)
2131 || (params & kBatteryDataOtherAudioDeviceOn)) {
2132
2133 int deviceOn[NUM_AUDIO_DEVICES];
2134 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2135 deviceOn[i] = 0;
2136 }
2137
2138 if ((params & kBatteryDataSpeakerOn)
2139 && (params & kBatteryDataOtherAudioDeviceOn)) {
2140 deviceOn[SPEAKER_AND_OTHER] = 1;
2141 } else if (params & kBatteryDataSpeakerOn) {
2142 deviceOn[SPEAKER] = 1;
2143 } else {
2144 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2145 }
2146
2147 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2148 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2149
2150 if (mBatteryAudio.refCount > 0) { // if playing audio
2151 if (!deviceOn[i]) {
2152 mBatteryAudio.lastTime[i] += time;
2153 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2154 mBatteryAudio.lastTime[i] = 0;
2155 } else {
2156 mBatteryAudio.lastTime[i] = 0 - time;
2157 }
2158 }
2159
2160 mBatteryAudio.deviceOn[i] = deviceOn[i];
2161 }
2162 }
2163 return;
2164 }
2165
2166 // an sudio stream is started
2167 if (params & kBatteryDataAudioFlingerStart) {
2168 // record the start time only if currently no other audio
2169 // is being played
2170 if (mBatteryAudio.refCount == 0) {
2171 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2172 if (mBatteryAudio.deviceOn[i]) {
2173 mBatteryAudio.lastTime[i] -= time;
2174 }
2175 }
2176 }
2177
2178 mBatteryAudio.refCount ++;
2179 return;
2180
2181 } else if (params & kBatteryDataAudioFlingerStop) {
2182 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002183 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002184 return;
2185 }
2186
2187 // record the stop time only if currently this is the only
2188 // audio being played
2189 if (mBatteryAudio.refCount == 1) {
2190 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2191 if (mBatteryAudio.deviceOn[i]) {
2192 mBatteryAudio.lastTime[i] += time;
2193 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2194 mBatteryAudio.lastTime[i] = 0;
2195 }
2196 }
2197 }
2198
2199 mBatteryAudio.refCount --;
2200 return;
2201 }
2202
Gloria Wang7cf180c2011-02-19 18:37:57 -08002203 int uid = IPCThreadState::self()->getCallingUid();
2204 if (uid == AID_MEDIA) {
2205 return;
2206 }
2207 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002208
2209 if (index < 0) { // create a new entry for this UID
2210 BatteryUsageInfo info;
2211 info.audioTotalTime = 0;
2212 info.videoTotalTime = 0;
2213 info.audioLastTime = 0;
2214 info.videoLastTime = 0;
2215 info.refCount = 0;
2216
Gloria Wang9ee159b2011-02-24 14:51:45 -08002217 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002218 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002219 return;
2220 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002221 }
2222
2223 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2224
2225 if (params & kBatteryDataCodecStarted) {
2226 if (params & kBatteryDataTrackAudio) {
2227 info.audioLastTime -= time;
2228 info.refCount ++;
2229 }
2230 if (params & kBatteryDataTrackVideo) {
2231 info.videoLastTime -= time;
2232 info.refCount ++;
2233 }
2234 } else {
2235 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002236 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002237 return;
2238 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002239 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002240 mBatteryData.removeItem(uid);
2241 return;
2242 }
2243
2244 if (params & kBatteryDataTrackAudio) {
2245 info.audioLastTime += time;
2246 info.refCount --;
2247 }
2248 if (params & kBatteryDataTrackVideo) {
2249 info.videoLastTime += time;
2250 info.refCount --;
2251 }
2252
2253 // no stream is being played by this UID
2254 if (info.refCount == 0) {
2255 info.audioTotalTime += info.audioLastTime;
2256 info.audioLastTime = 0;
2257 info.videoTotalTime += info.videoLastTime;
2258 info.videoLastTime = 0;
2259 }
2260 }
2261}
2262
2263status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2264 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002265
2266 // audio output devices usage
2267 int32_t time = systemTime() / 1000000L; //in ms
2268 int32_t totalTime;
2269
2270 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2271 totalTime = mBatteryAudio.totalTime[i];
2272
2273 if (mBatteryAudio.deviceOn[i]
2274 && (mBatteryAudio.lastTime[i] != 0)) {
2275 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2276 totalTime += tmpTime;
2277 }
2278
2279 reply->writeInt32(totalTime);
2280 // reset the total time
2281 mBatteryAudio.totalTime[i] = 0;
2282 }
2283
2284 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002285 BatteryUsageInfo info;
2286 int size = mBatteryData.size();
2287
2288 reply->writeInt32(size);
2289 int i = 0;
2290
2291 while (i < size) {
2292 info = mBatteryData.valueAt(i);
2293
2294 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2295 reply->writeInt32(info.audioTotalTime);
2296 reply->writeInt32(info.videoTotalTime);
2297
2298 info.audioTotalTime = 0;
2299 info.videoTotalTime = 0;
2300
2301 // remove the UID entry where no stream is being played
2302 if (info.refCount <= 0) {
2303 mBatteryData.removeItemsAt(i);
2304 size --;
2305 i --;
2306 }
2307 i++;
2308 }
2309 return NO_ERROR;
2310}
nikoa64c8c72009-07-20 15:07:26 -07002311} // namespace android