blob: a0c20ae4a947593700bf3ad8a9ef09a00974514b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
Gloria Wangd211f412011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopiana650aaa2009-06-03 17:32:49 -070031
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopiana650aaa2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
37#include <android_runtime/ActivityManager.h>
Mathias Agopiana650aaa2009-06-03 17:32:49 -070038
Mathias Agopian07952722009-05-19 19:08:10 -070039#include <binder/IPCThreadState.h>
40#include <binder/IServiceManager.h>
41#include <binder/MemoryHeapBase.h>
42#include <binder/MemoryBase.h>
Jamie Gennis2fa0ac22011-10-26 18:36:31 -070043#include <gui/SurfaceTextureClient.h>
Nicolas Catania20cb94e2009-05-12 23:25:55 -070044#include <utils/Errors.h> // for status_t
45#include <utils/String8.h>
Marco Nelissenc39d2e32009-09-20 10:42:13 -070046#include <utils/SystemClock.h>
Nicolas Catania20cb94e2009-05-12 23:25:55 -070047#include <utils/Vector.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048#include <cutils/properties.h>
49
50#include <media/MediaPlayerInterface.h>
51#include <media/mediarecorder.h>
52#include <media/MediaMetadataRetrieverInterface.h>
nikobc726922009-07-20 15:07:26 -070053#include <media/Metadata.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054#include <media/AudioTrack.h>
James Donged732462011-03-14 17:01:38 -070055#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9968a362011-09-27 09:48:56 -070056#include <media/stagefright/MediaErrors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057
Dima Zavin34bb4192011-05-11 14:15:23 -070058#include <system/audio.h>
Dima Zavin24fc2fb2011-04-19 22:30:36 -070059
Gloria Wangd211f412011-02-19 18:37:57 -080060#include <private/android_filesystem_config.h>
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062#include "MediaRecorderClient.h"
63#include "MediaPlayerService.h"
64#include "MetadataRetrieverClient.h"
65
66#include "MidiFile.h"
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070067#include "TestPlayerStub.h"
Andreas Hubere46b7be2009-07-14 16:56:47 -070068#include "StagefrightPlayer.h"
Andreas Hubera1587462010-12-15 15:17:42 -080069#include "nuplayer/NuPlayerDriver.h"
Andreas Hubere46b7be2009-07-14 16:56:47 -070070
Andreas Hubere46b7be2009-07-14 16:56:47 -070071#include <OMX.h>
Nicolas Catania8f5fcab2009-07-13 14:37:49 -070072
Nicolas Cataniab2c69392009-07-08 08:57:42 -070073namespace {
nikobc726922009-07-20 15:07:26 -070074using android::media::Metadata;
Nicolas Cataniab2c69392009-07-08 08:57:42 -070075using android::status_t;
76using android::OK;
77using android::BAD_VALUE;
78using android::NOT_ENOUGH_DATA;
79using android::Parcel;
Nicolas Cataniab2c69392009-07-08 08:57:42 -070080
81// Max number of entries in the filter.
82const int kMaxFilterSize = 64; // I pulled that out of thin air.
83
nikobc726922009-07-20 15:07:26 -070084// FIXME: Move all the metadata related function in the Metadata.cpp
niko89948372009-07-16 16:39:53 -070085
Nicolas Cataniab2c69392009-07-08 08:57:42 -070086
87// Unmarshall a filter from a Parcel.
88// Filter format in a parcel:
89//
90// 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
91// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92// | number of entries (n) |
93// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94// | metadata type 1 |
95// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96// | metadata type 2 |
97// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98// ....
99// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100// | metadata type n |
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102//
103// @param p Parcel that should start with a filter.
104// @param[out] filter On exit contains the list of metadata type to be
105// filtered.
106// @param[out] status On exit contains the status code to be returned.
107// @return true if the parcel starts with a valid filter.
108bool unmarshallFilter(const Parcel& p,
nikobc726922009-07-20 15:07:26 -0700109 Metadata::Filter *filter,
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700110 status_t *status)
111{
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700112 int32_t val;
113 if (p.readInt32(&val) != OK)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700114 {
Steve Block3762c312012-01-06 19:20:56 +0000115 ALOGE("Failed to read filter's length");
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700116 *status = NOT_ENOUGH_DATA;
117 return false;
118 }
119
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700120 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700121 {
Steve Block3762c312012-01-06 19:20:56 +0000122 ALOGE("Invalid filter len %d", val);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700123 *status = BAD_VALUE;
124 return false;
125 }
126
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700127 const size_t num = val;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700128
129 filter->clear();
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700130 filter->setCapacity(num);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700131
nikobc726922009-07-20 15:07:26 -0700132 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700133
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700134
135 if (p.dataAvail() < size)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700136 {
Steve Block3762c312012-01-06 19:20:56 +0000137 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700138 *status = NOT_ENOUGH_DATA;
139 return false;
140 }
141
nikobc726922009-07-20 15:07:26 -0700142 const Metadata::Type *data =
143 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700144
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700145 if (NULL == data)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700146 {
Steve Block3762c312012-01-06 19:20:56 +0000147 ALOGE("Filter had no data");
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700148 *status = BAD_VALUE;
149 return false;
150 }
151
152 // TODO: The stl impl of vector would be more efficient here
153 // because it degenerates into a memcpy on pod types. Try to
154 // replace later or use stl::set.
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700155 for (size_t i = 0; i < num; ++i)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700156 {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700157 filter->add(*data);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700158 ++data;
159 }
160 *status = OK;
161 return true;
162}
163
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700164// @param filter Of metadata type.
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700165// @param val To be searched.
166// @return true if a match was found.
nikobc726922009-07-20 15:07:26 -0700167bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700168{
169 // Deal with empty and ANY right away
170 if (filter.isEmpty()) return false;
nikobc726922009-07-20 15:07:26 -0700171 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700172
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700173 return filter.indexOf(val) >= 0;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700174}
175
176} // anonymous namespace
177
178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179namespace android {
180
Dave Burkefc301b02011-08-30 14:39:17 +0100181static bool checkPermission(const char* permissionString) {
182#ifndef HAVE_ANDROID_OS
183 return true;
184#endif
185 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
186 bool ok = checkCallingPermission(String16(permissionString));
Steve Block3762c312012-01-06 19:20:56 +0000187 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burkefc301b02011-08-30 14:39:17 +0100188 return ok;
189}
190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191// TODO: Temp hack until we can register players
192typedef struct {
193 const char *extension;
194 const player_type playertype;
195} extmap;
196extmap FILE_EXTS [] = {
197 {".mid", SONIVOX_PLAYER},
198 {".midi", SONIVOX_PLAYER},
199 {".smf", SONIVOX_PLAYER},
200 {".xmf", SONIVOX_PLAYER},
201 {".imy", SONIVOX_PLAYER},
202 {".rtttl", SONIVOX_PLAYER},
203 {".rtx", SONIVOX_PLAYER},
204 {".ota", SONIVOX_PLAYER},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205};
206
207// TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
209/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
210
211void MediaPlayerService::instantiate() {
212 defaultServiceManager()->addService(
213 String16("media.player"), new MediaPlayerService());
214}
215
216MediaPlayerService::MediaPlayerService()
217{
Steve Block71f2cf12011-10-20 11:56:00 +0100218 ALOGV("MediaPlayerService created");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 mNextConnId = 1;
Gloria Wang9b3f1522011-02-24 14:51:45 -0800220
221 mBatteryAudio.refCount = 0;
222 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
223 mBatteryAudio.deviceOn[i] = 0;
224 mBatteryAudio.lastTime[i] = 0;
225 mBatteryAudio.totalTime[i] = 0;
226 }
227 // speaker is on by default
228 mBatteryAudio.deviceOn[SPEAKER] = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229}
230
231MediaPlayerService::~MediaPlayerService()
232{
Steve Block71f2cf12011-10-20 11:56:00 +0100233 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234}
235
236sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
237{
Gloria Wang608a2632009-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 Block71f2cf12011-10-20 11:56:00 +0100242 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 return recorder;
244}
245
Gloria Wang608a2632009-10-29 15:46:37 -0700246void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
247{
248 Mutex::Autolock lock(mLock);
249 mMediaRecorderClients.remove(client);
Steve Block71f2cf12011-10-20 11:56:00 +0100250 ALOGV("Delete media recorder client");
Gloria Wang608a2632009-10-29 15:46:37 -0700251}
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
254{
255 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block71f2cf12011-10-20 11:56:00 +0100256 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 return retriever;
258}
259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burkefc301b02011-08-30 14:39:17 +0100261 int audioSessionId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262{
263 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber603d7392011-06-30 15:47:02 -0700264
265 sp<Client> c = new Client(
266 this, pid, connId, client, audioSessionId,
267 IPCThreadState::self()->getCallingUid());
268
Steve Block71f2cf12011-10-20 11:56:00 +0100269 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burkefc301b02011-08-30 14:39:17 +0100270 IPCThreadState::self()->getCallingUid());
271
272 wp<Client> w = c;
273 {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 Mutex::Autolock lock(mLock);
275 mClients.add(w);
276 }
Andreas Huber52b52cd2010-11-23 11:41:34 -0800277 return c;
278}
279
Andreas Huber784202e2009-10-15 13:46:54 -0700280sp<IOMX> MediaPlayerService::getOMX() {
281 Mutex::Autolock autoLock(mLock);
282
283 if (mOMX.get() == NULL) {
284 mOMX = new OMX;
285 }
286
287 return mOMX;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700288}
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
291{
292 const size_t SIZE = 256;
293 char buffer[SIZE];
294 String8 result;
295
296 result.append(" AudioCache\n");
297 if (mHeap != 0) {
298 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
299 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
300 result.append(buffer);
301 }
302 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
303 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
304 result.append(buffer);
305 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
306 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
307 result.append(buffer);
308 ::write(fd, result.string(), result.size());
309 return NO_ERROR;
310}
311
312status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
313{
314 const size_t SIZE = 256;
315 char buffer[SIZE];
316 String8 result;
317
318 result.append(" AudioOutput\n");
319 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
320 mStreamType, mLeftVolume, mRightVolume);
321 result.append(buffer);
322 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
323 mMsecsPerFrame, mLatency);
324 result.append(buffer);
Eric Laurent7070b362010-07-16 07:43:46 -0700325 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
326 mAuxEffectId, mSendLevel);
327 result.append(buffer);
328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 ::write(fd, result.string(), result.size());
330 if (mTrack != 0) {
331 mTrack->dump(fd, args);
332 }
333 return NO_ERROR;
334}
335
336status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
337{
338 const size_t SIZE = 256;
339 char buffer[SIZE];
340 String8 result;
341 result.append(" Client\n");
342 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
343 mPid, mConnId, mStatus, mLoop?"true": "false");
344 result.append(buffer);
345 write(fd, result.string(), result.size());
Andreas Huberfddf5d92011-06-07 15:52:25 -0700346 if (mPlayer != NULL) {
347 mPlayer->dump(fd, args);
348 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 if (mAudioOutput != 0) {
350 mAudioOutput->dump(fd, args);
351 }
352 write(fd, "\n", 1);
353 return NO_ERROR;
354}
355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
357{
358 const size_t SIZE = 256;
359 char buffer[SIZE];
360 String8 result;
361 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
362 snprintf(buffer, SIZE, "Permission Denial: "
363 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
364 IPCThreadState::self()->getCallingPid(),
365 IPCThreadState::self()->getCallingUid());
366 result.append(buffer);
367 } else {
368 Mutex::Autolock lock(mLock);
369 for (int i = 0, n = mClients.size(); i < n; ++i) {
370 sp<Client> c = mClients[i].promote();
371 if (c != 0) c->dump(fd, args);
372 }
James Dong929642e2010-07-08 11:16:11 -0700373 if (mMediaRecorderClients.size() == 0) {
374 result.append(" No media recorder client\n\n");
375 } else {
376 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
377 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Dongdc8bbe792011-10-18 22:29:20 -0700378 if (c != 0) {
379 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
380 result.append(buffer);
381 write(fd, result.string(), result.size());
382 result = "\n";
383 c->dump(fd, args);
384 }
James Dong929642e2010-07-08 11:16:11 -0700385 }
Gloria Wang608a2632009-10-29 15:46:37 -0700386 }
387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 result.append(" Files opened and/or mapped:\n");
Glenn Kasten6af763b2011-05-04 17:58:57 -0700389 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 FILE *f = fopen(buffer, "r");
391 if (f) {
392 while (!feof(f)) {
393 fgets(buffer, SIZE, f);
Dave Sparks124a3462010-09-27 16:55:18 -0700394 if (strstr(buffer, " /mnt/sdcard/") ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 strstr(buffer, " /system/sounds/") ||
Dave Sparks124a3462010-09-27 16:55:18 -0700396 strstr(buffer, " /data/") ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 strstr(buffer, " /system/media/")) {
398 result.append(" ");
399 result.append(buffer);
400 }
401 }
402 fclose(f);
403 } else {
404 result.append("couldn't open ");
405 result.append(buffer);
406 result.append("\n");
407 }
408
Glenn Kasten6af763b2011-05-04 17:58:57 -0700409 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 DIR *d = opendir(buffer);
411 if (d) {
412 struct dirent *ent;
413 while((ent = readdir(d)) != NULL) {
414 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten6af763b2011-05-04 17:58:57 -0700415 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 struct stat s;
417 if (lstat(buffer, &s) == 0) {
418 if ((s.st_mode & S_IFMT) == S_IFLNK) {
419 char linkto[256];
420 int len = readlink(buffer, linkto, sizeof(linkto));
421 if(len > 0) {
422 if(len > 255) {
423 linkto[252] = '.';
424 linkto[253] = '.';
425 linkto[254] = '.';
426 linkto[255] = 0;
427 } else {
428 linkto[len] = 0;
429 }
Dave Sparks124a3462010-09-27 16:55:18 -0700430 if (strstr(linkto, "/mnt/sdcard/") == linkto ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks124a3462010-09-27 16:55:18 -0700432 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 strstr(linkto, "/system/media/") == linkto) {
434 result.append(" ");
435 result.append(buffer);
436 result.append(" -> ");
437 result.append(linkto);
438 result.append("\n");
439 }
440 }
441 } else {
442 result.append(" unexpected type for ");
443 result.append(buffer);
444 result.append("\n");
445 }
446 }
447 }
448 }
449 closedir(d);
450 } else {
451 result.append("couldn't open ");
452 result.append(buffer);
453 result.append("\n");
454 }
455
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 bool dumpMem = false;
457 for (size_t i = 0; i < args.size(); i++) {
458 if (args[i] == String16("-m")) {
459 dumpMem = true;
460 }
461 }
462 if (dumpMem) {
James Donged732462011-03-14 17:01:38 -0700463 dumpMemoryAddresses(fd);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466 write(fd, result.string(), result.size());
467 return NO_ERROR;
468}
469
470void MediaPlayerService::removeClient(wp<Client> client)
471{
472 Mutex::Autolock lock(mLock);
473 mClients.remove(client);
474}
475
Andreas Huber603d7392011-06-30 15:47:02 -0700476MediaPlayerService::Client::Client(
477 const sp<MediaPlayerService>& service, pid_t pid,
478 int32_t connId, const sp<IMediaPlayerClient>& client,
479 int audioSessionId, uid_t uid)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480{
Steve Block71f2cf12011-10-20 11:56:00 +0100481 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 mPid = pid;
483 mConnId = connId;
484 mService = service;
485 mClient = client;
486 mLoop = false;
487 mStatus = NO_INIT;
Eric Laurent619346f2010-06-21 09:27:30 -0700488 mAudioSessionId = audioSessionId;
Andreas Huber603d7392011-06-30 15:47:02 -0700489 mUID = uid;
Eric Laurent619346f2010-06-21 09:27:30 -0700490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491#if CALLBACK_ANTAGONIZER
Steve Block5baa3a62011-12-20 16:23:08 +0000492 ALOGD("create Antagonizer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 mAntagonizer = new Antagonizer(notify, this);
494#endif
495}
496
497MediaPlayerService::Client::~Client()
498{
Steve Block71f2cf12011-10-20 11:56:00 +0100499 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 mAudioOutput.clear();
501 wp<Client> client(this);
502 disconnect();
503 mService->removeClient(client);
504}
505
506void MediaPlayerService::Client::disconnect()
507{
Steve Block71f2cf12011-10-20 11:56:00 +0100508 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 // grab local reference and clear main reference to prevent future
510 // access to object
511 sp<MediaPlayerBase> p;
512 {
513 Mutex::Autolock l(mLock);
514 p = mPlayer;
515 }
Dave Sparkscb9a44e2009-03-24 17:57:12 -0700516 mClient.clear();
Andreas Hubere46b7be2009-07-14 16:56:47 -0700517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 mPlayer.clear();
519
520 // clear the notification to prevent callbacks to dead client
521 // and reset the player. We assume the player will serialize
522 // access to itself if necessary.
523 if (p != 0) {
524 p->setNotifyCallback(0, 0);
525#if CALLBACK_ANTAGONIZER
Steve Block5baa3a62011-12-20 16:23:08 +0000526 ALOGD("kill Antagonizer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 mAntagonizer->kill();
528#endif
529 p->reset();
530 }
531
Jamie Gennis2fa0ac22011-10-26 18:36:31 -0700532 disconnectNativeWindow();
533
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 IPCThreadState::self()->flushCommands();
535}
536
Andreas Huber0d596d42009-08-07 09:30:32 -0700537static player_type getDefaultPlayerType() {
Andreas Huber8d65dd22010-06-23 16:40:57 -0700538 return STAGEFRIGHT_PLAYER;
Andreas Huber0d596d42009-08-07 09:30:32 -0700539}
540
James Dong392ff3b2009-09-06 14:29:45 -0700541player_type getPlayerType(int fd, int64_t offset, int64_t length)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542{
543 char buf[20];
544 lseek(fd, offset, SEEK_SET);
545 read(fd, buf, sizeof(buf));
546 lseek(fd, offset, SEEK_SET);
547
548 long ident = *((long*)buf);
549
550 // Ogg vorbis?
551 if (ident == 0x5367674f) // 'OggS'
Andreas Huber8d65dd22010-06-23 16:40:57 -0700552 return STAGEFRIGHT_PLAYER;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553
554 // Some kind of MIDI?
555 EAS_DATA_HANDLE easdata;
556 if (EAS_Init(&easdata) == EAS_SUCCESS) {
557 EAS_FILE locator;
558 locator.path = NULL;
559 locator.fd = fd;
560 locator.offset = offset;
561 locator.length = length;
562 EAS_HANDLE eashandle;
563 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
564 EAS_CloseFile(easdata, eashandle);
565 EAS_Shutdown(easdata);
566 return SONIVOX_PLAYER;
567 }
568 EAS_Shutdown(easdata);
569 }
570
Andreas Huber0d596d42009-08-07 09:30:32 -0700571 return getDefaultPlayerType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572}
573
James Dong392ff3b2009-09-06 14:29:45 -0700574player_type getPlayerType(const char* url)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575{
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700576 if (TestPlayerStub::canBeUsed(url)) {
577 return TEST_PLAYER;
578 }
579
Andreas Huber554d0ea2011-03-21 16:28:04 -0700580 if (!strncasecmp("http://", url, 7)
581 || !strncasecmp("https://", url, 8)) {
Andreas Huberd6c421f2011-02-16 09:05:38 -0800582 size_t len = strlen(url);
583 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
584 return NU_PLAYER;
585 }
Andreas Huber54e66492010-12-23 10:27:40 -0800586
Andreas Huberd6c421f2011-02-16 09:05:38 -0800587 if (strstr(url,"m3u8")) {
588 return NU_PLAYER;
Andreas Huber54e66492010-12-23 10:27:40 -0800589 }
590 }
591
Andreas Hubera6be6dc2011-10-11 15:24:07 -0700592 if (!strncasecmp("rtsp://", url, 7)) {
593 return NU_PLAYER;
594 }
595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 // use MidiFile for MIDI extensions
597 int lenURL = strlen(url);
598 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
599 int len = strlen(FILE_EXTS[i].extension);
600 int start = lenURL - len;
601 if (start > 0) {
Atsushi Enoebcc51d2010-03-19 23:18:02 +0900602 if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
Andreas Huber8d65dd22010-06-23 16:40:57 -0700603 return FILE_EXTS[i].playertype;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 }
605 }
606 }
607
Andreas Huber0d596d42009-08-07 09:30:32 -0700608 return getDefaultPlayerType();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609}
610
611static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
612 notify_callback_f notifyFunc)
613{
614 sp<MediaPlayerBase> p;
615 switch (playerType) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 case SONIVOX_PLAYER:
Steve Block71f2cf12011-10-20 11:56:00 +0100617 ALOGV(" create MidiFile");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 p = new MidiFile();
619 break;
Andreas Hubere46b7be2009-07-14 16:56:47 -0700620 case STAGEFRIGHT_PLAYER:
Steve Block71f2cf12011-10-20 11:56:00 +0100621 ALOGV(" create StagefrightPlayer");
Andreas Hubere46b7be2009-07-14 16:56:47 -0700622 p = new StagefrightPlayer;
623 break;
Andreas Hubera1587462010-12-15 15:17:42 -0800624 case NU_PLAYER:
Steve Block71f2cf12011-10-20 11:56:00 +0100625 ALOGV(" create NuPlayer");
Andreas Hubera1587462010-12-15 15:17:42 -0800626 p = new NuPlayerDriver;
627 break;
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700628 case TEST_PLAYER:
Steve Block71f2cf12011-10-20 11:56:00 +0100629 ALOGV("Create Test Player stub");
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700630 p = new TestPlayerStub();
631 break;
James Dong8ec2d9a2010-11-10 18:42:40 -0800632 default:
Steve Block3762c312012-01-06 19:20:56 +0000633 ALOGE("Unknown player type: %d", playerType);
James Dong8ec2d9a2010-11-10 18:42:40 -0800634 return NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 }
636 if (p != NULL) {
637 if (p->initCheck() == NO_ERROR) {
638 p->setNotifyCallback(cookie, notifyFunc);
639 } else {
640 p.clear();
641 }
642 }
643 if (p == NULL) {
Steve Block3762c312012-01-06 19:20:56 +0000644 ALOGE("Failed to create player object");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800645 }
646 return p;
647}
648
649sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
650{
651 // determine if we have the right player type
652 sp<MediaPlayerBase> p = mPlayer;
653 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block71f2cf12011-10-20 11:56:00 +0100654 ALOGV("delete player");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 p.clear();
656 }
657 if (p == NULL) {
658 p = android::createPlayer(playerType, this, notify);
659 }
Andreas Huber603d7392011-06-30 15:47:02 -0700660
Jason Simmonsd9792ac2011-08-12 13:46:55 -0700661 if (p != NULL) {
662 p->setUID(mUID);
663 }
Andreas Huber603d7392011-06-30 15:47:02 -0700664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 return p;
666}
667
Andreas Huber25643002010-01-28 11:19:57 -0800668status_t MediaPlayerService::Client::setDataSource(
669 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670{
Steve Block71f2cf12011-10-20 11:56:00 +0100671 ALOGV("setDataSource(%s)", url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 if (url == NULL)
673 return UNKNOWN_ERROR;
674
Dave Burkefc301b02011-08-30 14:39:17 +0100675 if ((strncmp(url, "http://", 7) == 0) ||
676 (strncmp(url, "https://", 8) == 0) ||
677 (strncmp(url, "rtsp://", 7) == 0)) {
678 if (!checkPermission("android.permission.INTERNET")) {
679 return PERMISSION_DENIED;
680 }
681 }
682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 if (strncmp(url, "content://", 10) == 0) {
684 // get a filedescriptor for the content Uri and
685 // pass it to the setDataSource(fd) method
686
687 String16 url16(url);
688 int fd = android::openContentProviderFile(url16);
689 if (fd < 0)
690 {
Steve Block3762c312012-01-06 19:20:56 +0000691 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 return UNKNOWN_ERROR;
693 }
694 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
695 close(fd);
696 return mStatus;
697 } else {
698 player_type playerType = getPlayerType(url);
Steve Block71f2cf12011-10-20 11:56:00 +0100699 ALOGV("player type = %d", playerType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700
701 // create the right type of player
702 sp<MediaPlayerBase> p = createPlayer(playerType);
703 if (p == NULL) return NO_INIT;
704
705 if (!p->hardwareOutput()) {
Eric Laurent619346f2010-06-21 09:27:30 -0700706 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
708 }
709
710 // now set data source
Steve Block71f2cf12011-10-20 11:56:00 +0100711 ALOGV(" setDataSource");
Andreas Huber25643002010-01-28 11:19:57 -0800712 mStatus = p->setDataSource(url, headers);
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700713 if (mStatus == NO_ERROR) {
714 mPlayer = p;
715 } else {
Steve Block3762c312012-01-06 19:20:56 +0000716 ALOGE(" error: %d", mStatus);
Nicolas Catania8f5fcab2009-07-13 14:37:49 -0700717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 return mStatus;
719 }
720}
721
722status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
723{
Steve Block71f2cf12011-10-20 11:56:00 +0100724 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 struct stat sb;
726 int ret = fstat(fd, &sb);
727 if (ret != 0) {
Steve Block3762c312012-01-06 19:20:56 +0000728 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 return UNKNOWN_ERROR;
730 }
731
Steve Block71f2cf12011-10-20 11:56:00 +0100732 ALOGV("st_dev = %llu", sb.st_dev);
733 ALOGV("st_mode = %u", sb.st_mode);
734 ALOGV("st_uid = %lu", sb.st_uid);
735 ALOGV("st_gid = %lu", sb.st_gid);
736 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737
738 if (offset >= sb.st_size) {
Steve Block3762c312012-01-06 19:20:56 +0000739 ALOGE("offset error");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 ::close(fd);
741 return UNKNOWN_ERROR;
742 }
743 if (offset + length > sb.st_size) {
744 length = sb.st_size - offset;
Steve Block71f2cf12011-10-20 11:56:00 +0100745 ALOGV("calculated length = %lld", length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747
748 player_type playerType = getPlayerType(fd, offset, length);
Steve Block71f2cf12011-10-20 11:56:00 +0100749 ALOGV("player type = %d", playerType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750
751 // create the right type of player
752 sp<MediaPlayerBase> p = createPlayer(playerType);
753 if (p == NULL) return NO_INIT;
754
755 if (!p->hardwareOutput()) {
Eric Laurent619346f2010-06-21 09:27:30 -0700756 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800757 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
758 }
759
760 // now set data source
761 mStatus = p->setDataSource(fd, offset, length);
762 if (mStatus == NO_ERROR) mPlayer = p;
Dave Burkefc301b02011-08-30 14:39:17 +0100763
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 return mStatus;
765}
766
Andreas Huber52b52cd2010-11-23 11:41:34 -0800767status_t MediaPlayerService::Client::setDataSource(
768 const sp<IStreamSource> &source) {
769 // create the right type of player
Andreas Hubera1587462010-12-15 15:17:42 -0800770 sp<MediaPlayerBase> p = createPlayer(NU_PLAYER);
Andreas Huber52b52cd2010-11-23 11:41:34 -0800771
772 if (p == NULL) {
773 return NO_INIT;
774 }
775
776 if (!p->hardwareOutput()) {
777 mAudioOutput = new AudioOutput(mAudioSessionId);
778 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
779 }
780
781 // now set data source
782 mStatus = p->setDataSource(source);
783
784 if (mStatus == OK) {
785 mPlayer = p;
786 }
787
788 return mStatus;
789}
790
Jamie Gennis2fa0ac22011-10-26 18:36:31 -0700791void MediaPlayerService::Client::disconnectNativeWindow() {
792 if (mConnectedWindow != NULL) {
793 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
794 NATIVE_WINDOW_API_MEDIA);
795
796 if (err != OK) {
Steve Block8564c8d2012-01-05 23:22:43 +0000797 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis2fa0ac22011-10-26 18:36:31 -0700798 strerror(-err), err);
799 }
800 }
801 mConnectedWindow.clear();
802}
803
Glenn Kastencc562a32011-02-08 17:26:17 -0800804status_t MediaPlayerService::Client::setVideoSurfaceTexture(
805 const sp<ISurfaceTexture>& surfaceTexture)
806{
Steve Block71f2cf12011-10-20 11:56:00 +0100807 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kastencc562a32011-02-08 17:26:17 -0800808 sp<MediaPlayerBase> p = getPlayer();
809 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis2fa0ac22011-10-26 18:36:31 -0700810
811 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
812 surfaceTexture->asBinder());
813 if (mConnectedWindowBinder == binder) {
814 return OK;
815 }
816
817 sp<ANativeWindow> anw;
818 if (surfaceTexture != NULL) {
819 anw = new SurfaceTextureClient(surfaceTexture);
820 status_t err = native_window_api_connect(anw.get(),
821 NATIVE_WINDOW_API_MEDIA);
822
823 if (err != OK) {
Steve Block3762c312012-01-06 19:20:56 +0000824 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis2fa0ac22011-10-26 18:36:31 -0700825 // Note that we must do the reset before disconnecting from the ANW.
826 // Otherwise queue/dequeue calls could be made on the disconnected
827 // ANW, which may result in errors.
828 reset();
829
830 disconnectNativeWindow();
831
832 return err;
833 }
834 }
835
836 // Note that we must set the player's new SurfaceTexture before
837 // disconnecting the old one. Otherwise queue/dequeue calls could be made
838 // on the disconnected ANW, which may result in errors.
839 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
840
841 disconnectNativeWindow();
842
843 mConnectedWindow = anw;
844
845 if (err == OK) {
846 mConnectedWindowBinder = binder;
847 } else {
848 disconnectNativeWindow();
849 }
850
851 return err;
Glenn Kastencc562a32011-02-08 17:26:17 -0800852}
853
Nicolas Catania20cb94e2009-05-12 23:25:55 -0700854status_t MediaPlayerService::Client::invoke(const Parcel& request,
855 Parcel *reply)
856{
857 sp<MediaPlayerBase> p = getPlayer();
858 if (p == NULL) return UNKNOWN_ERROR;
859 return p->invoke(request, reply);
860}
861
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700862// This call doesn't need to access the native player.
863status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
864{
865 status_t status;
nikobc726922009-07-20 15:07:26 -0700866 media::Metadata::Filter allow, drop;
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700867
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700868 if (unmarshallFilter(filter, &allow, &status) &&
869 unmarshallFilter(filter, &drop, &status)) {
870 Mutex::Autolock lock(mLock);
Nicolas Cataniab2c69392009-07-08 08:57:42 -0700871
872 mMetadataAllow = allow;
873 mMetadataDrop = drop;
874 }
875 return status;
876}
877
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700878status_t MediaPlayerService::Client::getMetadata(
879 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania5d55c712009-07-09 09:21:33 -0700880{
nikobc726922009-07-20 15:07:26 -0700881 sp<MediaPlayerBase> player = getPlayer();
882 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700883
niko89948372009-07-16 16:39:53 -0700884 status_t status;
885 // Placeholder for the return code, updated by the caller.
886 reply->writeInt32(-1);
887
nikobc726922009-07-20 15:07:26 -0700888 media::Metadata::Filter ids;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700889
890 // We don't block notifications while we fetch the data. We clear
891 // mMetadataUpdated first so we don't lose notifications happening
892 // during the rest of this call.
893 {
894 Mutex::Autolock lock(mLock);
895 if (update_only) {
niko89948372009-07-16 16:39:53 -0700896 ids = mMetadataUpdated;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700897 }
898 mMetadataUpdated.clear();
899 }
Nicolas Catania5d55c712009-07-09 09:21:33 -0700900
nikobc726922009-07-20 15:07:26 -0700901 media::Metadata metadata(reply);
Nicolas Catania4df8b2c2009-07-10 13:53:06 -0700902
nikobc726922009-07-20 15:07:26 -0700903 metadata.appendHeader();
904 status = player->getMetadata(ids, reply);
niko89948372009-07-16 16:39:53 -0700905
906 if (status != OK) {
nikobc726922009-07-20 15:07:26 -0700907 metadata.resetParcel();
Steve Block3762c312012-01-06 19:20:56 +0000908 ALOGE("getMetadata failed %d", status);
niko89948372009-07-16 16:39:53 -0700909 return status;
910 }
911
912 // FIXME: Implement filtering on the result. Not critical since
913 // filtering takes place on the update notifications already. This
914 // would be when all the metadata are fetch and a filter is set.
915
niko89948372009-07-16 16:39:53 -0700916 // Everything is fine, update the metadata length.
nikobc726922009-07-20 15:07:26 -0700917 metadata.updateLength();
niko89948372009-07-16 16:39:53 -0700918 return OK;
Nicolas Catania5d55c712009-07-09 09:21:33 -0700919}
920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921status_t MediaPlayerService::Client::prepareAsync()
922{
Steve Block71f2cf12011-10-20 11:56:00 +0100923 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 sp<MediaPlayerBase> p = getPlayer();
925 if (p == 0) return UNKNOWN_ERROR;
926 status_t ret = p->prepareAsync();
927#if CALLBACK_ANTAGONIZER
Steve Block5baa3a62011-12-20 16:23:08 +0000928 ALOGD("start Antagonizer");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 if (ret == NO_ERROR) mAntagonizer->start();
930#endif
931 return ret;
932}
933
934status_t MediaPlayerService::Client::start()
935{
Steve Block71f2cf12011-10-20 11:56:00 +0100936 ALOGV("[%d] start", mConnId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 sp<MediaPlayerBase> p = getPlayer();
938 if (p == 0) return UNKNOWN_ERROR;
939 p->setLooping(mLoop);
940 return p->start();
941}
942
943status_t MediaPlayerService::Client::stop()
944{
Steve Block71f2cf12011-10-20 11:56:00 +0100945 ALOGV("[%d] stop", mConnId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 sp<MediaPlayerBase> p = getPlayer();
947 if (p == 0) return UNKNOWN_ERROR;
948 return p->stop();
949}
950
951status_t MediaPlayerService::Client::pause()
952{
Steve Block71f2cf12011-10-20 11:56:00 +0100953 ALOGV("[%d] pause", mConnId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 sp<MediaPlayerBase> p = getPlayer();
955 if (p == 0) return UNKNOWN_ERROR;
956 return p->pause();
957}
958
959status_t MediaPlayerService::Client::isPlaying(bool* state)
960{
961 *state = false;
962 sp<MediaPlayerBase> p = getPlayer();
963 if (p == 0) return UNKNOWN_ERROR;
964 *state = p->isPlaying();
Steve Block71f2cf12011-10-20 11:56:00 +0100965 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 return NO_ERROR;
967}
968
969status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
970{
Steve Block71f2cf12011-10-20 11:56:00 +0100971 ALOGV("getCurrentPosition");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 sp<MediaPlayerBase> p = getPlayer();
973 if (p == 0) return UNKNOWN_ERROR;
974 status_t ret = p->getCurrentPosition(msec);
975 if (ret == NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100976 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 } else {
Steve Block3762c312012-01-06 19:20:56 +0000978 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 }
980 return ret;
981}
982
983status_t MediaPlayerService::Client::getDuration(int *msec)
984{
Steve Block71f2cf12011-10-20 11:56:00 +0100985 ALOGV("getDuration");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 sp<MediaPlayerBase> p = getPlayer();
987 if (p == 0) return UNKNOWN_ERROR;
988 status_t ret = p->getDuration(msec);
989 if (ret == NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +0100990 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800991 } else {
Steve Block3762c312012-01-06 19:20:56 +0000992 ALOGE("getDuration returned %d", ret);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 }
994 return ret;
995}
996
997status_t MediaPlayerService::Client::seekTo(int msec)
998{
Steve Block71f2cf12011-10-20 11:56:00 +0100999 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 sp<MediaPlayerBase> p = getPlayer();
1001 if (p == 0) return UNKNOWN_ERROR;
1002 return p->seekTo(msec);
1003}
1004
1005status_t MediaPlayerService::Client::reset()
1006{
Steve Block71f2cf12011-10-20 11:56:00 +01001007 ALOGV("[%d] reset", mConnId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 sp<MediaPlayerBase> p = getPlayer();
1009 if (p == 0) return UNKNOWN_ERROR;
1010 return p->reset();
1011}
1012
Glenn Kastenbc1d77b2012-01-12 16:38:12 -08001013status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014{
Steve Block71f2cf12011-10-20 11:56:00 +01001015 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 // TODO: for hardware output, call player instead
1017 Mutex::Autolock l(mLock);
1018 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1019 return NO_ERROR;
1020}
1021
1022status_t MediaPlayerService::Client::setLooping(int loop)
1023{
Steve Block71f2cf12011-10-20 11:56:00 +01001024 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 mLoop = loop;
1026 sp<MediaPlayerBase> p = getPlayer();
1027 if (p != 0) return p->setLooping(loop);
1028 return NO_ERROR;
1029}
1030
1031status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1032{
Steve Block71f2cf12011-10-20 11:56:00 +01001033 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 // TODO: for hardware output, call player instead
1035 Mutex::Autolock l(mLock);
1036 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1037 return NO_ERROR;
1038}
1039
Eric Laurent7070b362010-07-16 07:43:46 -07001040status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1041{
Steve Block71f2cf12011-10-20 11:56:00 +01001042 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent7070b362010-07-16 07:43:46 -07001043 Mutex::Autolock l(mLock);
1044 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1045 return NO_ERROR;
1046}
1047
1048status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1049{
Steve Block71f2cf12011-10-20 11:56:00 +01001050 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent7070b362010-07-16 07:43:46 -07001051 Mutex::Autolock l(mLock);
1052 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1053 return NO_ERROR;
1054}
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001055
Gloria Wangd01ec6e2011-04-25 17:28:22 -07001056status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block71f2cf12011-10-20 11:56:00 +01001057 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wangd01ec6e2011-04-25 17:28:22 -07001058 sp<MediaPlayerBase> p = getPlayer();
1059 if (p == 0) return UNKNOWN_ERROR;
1060 return p->setParameter(key, request);
1061}
1062
1063status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block71f2cf12011-10-20 11:56:00 +01001064 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wangd01ec6e2011-04-25 17:28:22 -07001065 sp<MediaPlayerBase> p = getPlayer();
1066 if (p == 0) return UNKNOWN_ERROR;
1067 return p->getParameter(key, reply);
1068}
1069
Gloria Wang162ee492011-04-11 17:23:27 -07001070void MediaPlayerService::Client::notify(
1071 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072{
1073 Client* client = static_cast<Client*>(cookie);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001074
1075 if (MEDIA_INFO == msg &&
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001076 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikobc726922009-07-20 15:07:26 -07001077 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001078
1079 if(client->shouldDropMetadata(metadata_type)) {
1080 return;
1081 }
1082
1083 // Update the list of metadata that have changed. getMetadata
1084 // also access mMetadataUpdated and clears it.
1085 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001086 }
Steve Block71f2cf12011-10-20 11:56:00 +01001087 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
Gloria Wang162ee492011-04-11 17:23:27 -07001088 client->mClient->notify(msg, ext1, ext2, obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089}
1090
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001091
nikobc726922009-07-20 15:07:26 -07001092bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001093{
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001094 Mutex::Autolock lock(mLock);
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001095
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001096 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001097 return true;
1098 }
1099
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001100 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001101 return false;
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001102 } else {
Nicolas Cataniab2c69392009-07-08 08:57:42 -07001103 return true;
1104 }
1105}
1106
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001107
nikobc726922009-07-20 15:07:26 -07001108void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania4df8b2c2009-07-10 13:53:06 -07001109 Mutex::Autolock lock(mLock);
1110 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1111 mMetadataUpdated.add(metadata_type);
1112 }
1113}
1114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115#if CALLBACK_ANTAGONIZER
1116const int Antagonizer::interval = 10000; // 10 msecs
1117
1118Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1119 mExit(false), mActive(false), mClient(client), mCb(cb)
1120{
1121 createThread(callbackThread, this);
1122}
1123
1124void Antagonizer::kill()
1125{
1126 Mutex::Autolock _l(mLock);
1127 mActive = false;
1128 mExit = true;
1129 mCondition.wait(mLock);
1130}
1131
1132int Antagonizer::callbackThread(void* user)
1133{
Steve Block5baa3a62011-12-20 16:23:08 +00001134 ALOGD("Antagonizer started");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1136 while (!p->mExit) {
1137 if (p->mActive) {
Steve Block71f2cf12011-10-20 11:56:00 +01001138 ALOGV("send event");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 p->mCb(p->mClient, 0, 0, 0);
1140 }
1141 usleep(interval);
1142 }
1143 Mutex::Autolock _l(p->mLock);
1144 p->mCondition.signal();
Steve Block5baa3a62011-12-20 16:23:08 +00001145 ALOGD("Antagonizer stopped");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 return 0;
1147}
1148#endif
1149
1150static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1151
Glenn Kasten1c5a89d2012-01-04 09:36:37 -08001152sp<IMemory> MediaPlayerService::decode(const char* url, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153{
Steve Block71f2cf12011-10-20 11:56:00 +01001154 ALOGV("decode(%s)", url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001155 sp<MemoryBase> mem;
1156 sp<MediaPlayerBase> player;
1157
1158 // Protect our precious, precious DRMd ringtones by only allowing
1159 // decoding of http, but not filesystem paths or content Uris.
1160 // If the application wants to decode those, it should open a
1161 // filedescriptor for them and use that.
1162 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Block5baa3a62011-12-20 16:23:08 +00001163 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001164 return mem;
1165 }
1166
1167 player_type playerType = getPlayerType(url);
Steve Block71f2cf12011-10-20 11:56:00 +01001168 ALOGV("player type = %d", playerType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169
1170 // create the right type of player
1171 sp<AudioCache> cache = new AudioCache(url);
1172 player = android::createPlayer(playerType, cache.get(), cache->notify);
1173 if (player == NULL) goto Exit;
1174 if (player->hardwareOutput()) goto Exit;
1175
1176 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1177
1178 // set data source
1179 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1180
Steve Block71f2cf12011-10-20 11:56:00 +01001181 ALOGV("prepare");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 player->prepareAsync();
1183
Steve Block71f2cf12011-10-20 11:56:00 +01001184 ALOGV("wait for prepare");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001185 if (cache->wait() != NO_ERROR) goto Exit;
1186
Steve Block71f2cf12011-10-20 11:56:00 +01001187 ALOGV("start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 player->start();
1189
Steve Block71f2cf12011-10-20 11:56:00 +01001190 ALOGV("wait for playback complete");
Eric Laurent9968a362011-09-27 09:48:56 -07001191 cache->wait();
1192 // in case of error, return what was successfully decoded.
1193 if (cache->size() == 0) {
1194 goto Exit;
1195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196
1197 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1198 *pSampleRate = cache->sampleRate();
1199 *pNumChannels = cache->channelCount();
Glenn Kasten1c5a89d2012-01-04 09:36:37 -08001200 *pFormat = cache->format();
Steve Block71f2cf12011-10-20 11:56:00 +01001201 ALOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202
1203Exit:
1204 if (player != 0) player->reset();
1205 return mem;
1206}
1207
Glenn Kasten1c5a89d2012-01-04 09:36:37 -08001208sp<IMemory> MediaPlayerService::decode(int fd, int64_t offset, int64_t length, uint32_t *pSampleRate, int* pNumChannels, audio_format_t* pFormat)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209{
Steve Block71f2cf12011-10-20 11:56:00 +01001210 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 sp<MemoryBase> mem;
1212 sp<MediaPlayerBase> player;
1213
1214 player_type playerType = getPlayerType(fd, offset, length);
Steve Block71f2cf12011-10-20 11:56:00 +01001215 ALOGV("player type = %d", playerType);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216
1217 // create the right type of player
1218 sp<AudioCache> cache = new AudioCache("decode_fd");
1219 player = android::createPlayer(playerType, cache.get(), cache->notify);
1220 if (player == NULL) goto Exit;
1221 if (player->hardwareOutput()) goto Exit;
1222
1223 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1224
1225 // set data source
1226 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1227
Steve Block71f2cf12011-10-20 11:56:00 +01001228 ALOGV("prepare");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 player->prepareAsync();
1230
Steve Block71f2cf12011-10-20 11:56:00 +01001231 ALOGV("wait for prepare");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 if (cache->wait() != NO_ERROR) goto Exit;
1233
Steve Block71f2cf12011-10-20 11:56:00 +01001234 ALOGV("start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 player->start();
1236
Steve Block71f2cf12011-10-20 11:56:00 +01001237 ALOGV("wait for playback complete");
Eric Laurent9968a362011-09-27 09:48:56 -07001238 cache->wait();
1239 // in case of error, return what was successfully decoded.
1240 if (cache->size() == 0) {
1241 goto Exit;
1242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243
1244 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1245 *pSampleRate = cache->sampleRate();
1246 *pNumChannels = cache->channelCount();
1247 *pFormat = cache->format();
Steve Block71f2cf12011-10-20 11:56:00 +01001248 ALOGV("return memory @ %p, sampleRate=%u, channelCount = %d, format = %d", mem->pointer(), *pSampleRate, *pNumChannels, *pFormat);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249
1250Exit:
1251 if (player != 0) player->reset();
1252 ::close(fd);
1253 return mem;
1254}
1255
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257#undef LOG_TAG
1258#define LOG_TAG "AudioSink"
Eric Laurent619346f2010-06-21 09:27:30 -07001259MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Hubere46b7be2009-07-14 16:56:47 -07001260 : mCallback(NULL),
Eric Laurent619346f2010-06-21 09:27:30 -07001261 mCallbackCookie(NULL),
1262 mSessionId(sessionId) {
Steve Block71f2cf12011-10-20 11:56:00 +01001263 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 mTrack = 0;
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001265 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 mLeftVolume = 1.0;
1267 mRightVolume = 1.0;
1268 mLatency = 0;
1269 mMsecsPerFrame = 0;
Eric Laurent7070b362010-07-16 07:43:46 -07001270 mAuxEffectId = 0;
1271 mSendLevel = 0.0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 setMinBufferCount();
1273}
1274
1275MediaPlayerService::AudioOutput::~AudioOutput()
1276{
1277 close();
1278}
1279
1280void MediaPlayerService::AudioOutput::setMinBufferCount()
1281{
1282 char value[PROPERTY_VALUE_MAX];
1283 if (property_get("ro.kernel.qemu", value, 0)) {
1284 mIsOnEmulator = true;
1285 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1286 }
1287}
1288
1289bool MediaPlayerService::AudioOutput::isOnEmulator()
1290{
1291 setMinBufferCount();
1292 return mIsOnEmulator;
1293}
1294
1295int MediaPlayerService::AudioOutput::getMinBufferCount()
1296{
1297 setMinBufferCount();
1298 return mMinBufferCount;
1299}
1300
1301ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1302{
1303 if (mTrack == 0) return NO_INIT;
1304 return mTrack->frameCount() * frameSize();
1305}
1306
1307ssize_t MediaPlayerService::AudioOutput::frameCount() const
1308{
1309 if (mTrack == 0) return NO_INIT;
1310 return mTrack->frameCount();
1311}
1312
1313ssize_t MediaPlayerService::AudioOutput::channelCount() const
1314{
1315 if (mTrack == 0) return NO_INIT;
1316 return mTrack->channelCount();
1317}
1318
1319ssize_t MediaPlayerService::AudioOutput::frameSize() const
1320{
1321 if (mTrack == 0) return NO_INIT;
1322 return mTrack->frameSize();
1323}
1324
1325uint32_t MediaPlayerService::AudioOutput::latency () const
1326{
1327 return mLatency;
1328}
1329
1330float MediaPlayerService::AudioOutput::msecsPerFrame() const
1331{
1332 return mMsecsPerFrame;
1333}
1334
Eric Laurent0986e792010-01-19 17:37:09 -08001335status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1336{
1337 if (mTrack == 0) return NO_INIT;
1338 return mTrack->getPosition(position);
1339}
1340
Andreas Hubere46b7be2009-07-14 16:56:47 -07001341status_t MediaPlayerService::AudioOutput::open(
Glenn Kasten1c5a89d2012-01-04 09:36:37 -08001342 uint32_t sampleRate, int channelCount, audio_format_t format, int bufferCount,
Andreas Hubere46b7be2009-07-14 16:56:47 -07001343 AudioCallback cb, void *cookie)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344{
Andreas Hubere46b7be2009-07-14 16:56:47 -07001345 mCallback = cb;
1346 mCallbackCookie = cookie;
1347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 // Check argument "bufferCount" against the mininum buffer count
1349 if (bufferCount < mMinBufferCount) {
Steve Block5baa3a62011-12-20 16:23:08 +00001350 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 bufferCount = mMinBufferCount;
1352
1353 }
Steve Block71f2cf12011-10-20 11:56:00 +01001354 ALOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 if (mTrack) close();
1356 int afSampleRate;
1357 int afFrameCount;
1358 int frameCount;
1359
1360 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1361 return NO_INIT;
1362 }
1363 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1364 return NO_INIT;
1365 }
1366
1367 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Hubere46b7be2009-07-14 16:56:47 -07001368
1369 AudioTrack *t;
1370 if (mCallback != NULL) {
1371 t = new AudioTrack(
Eric Laurenta553c252009-07-17 12:17:14 -07001372 mStreamType,
1373 sampleRate,
1374 format,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001375 (channelCount == 2) ? AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO,
Eric Laurenta553c252009-07-17 12:17:14 -07001376 frameCount,
1377 0 /* flags */,
1378 CallbackWrapper,
Eric Laurent619346f2010-06-21 09:27:30 -07001379 this,
1380 0,
1381 mSessionId);
Andreas Hubere46b7be2009-07-14 16:56:47 -07001382 } else {
1383 t = new AudioTrack(
Eric Laurenta553c252009-07-17 12:17:14 -07001384 mStreamType,
1385 sampleRate,
1386 format,
Dima Zavin24fc2fb2011-04-19 22:30:36 -07001387 (channelCount == 2) ? AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO,
Eric Laurent619346f2010-06-21 09:27:30 -07001388 frameCount,
1389 0,
1390 NULL,
1391 NULL,
1392 0,
1393 mSessionId);
Andreas Hubere46b7be2009-07-14 16:56:47 -07001394 }
1395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001396 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block3762c312012-01-06 19:20:56 +00001397 ALOGE("Unable to create audio track");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001398 delete t;
1399 return NO_INIT;
1400 }
1401
Steve Block71f2cf12011-10-20 11:56:00 +01001402 ALOGV("setVolume");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -07001404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Dave Sparksb904c2a2009-12-03 10:13:32 -08001406 mLatency = t->latency();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 mTrack = t;
Eric Laurent7070b362010-07-16 07:43:46 -07001408
1409 t->setAuxEffectSendLevel(mSendLevel);
1410 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411}
1412
1413void MediaPlayerService::AudioOutput::start()
1414{
Steve Block71f2cf12011-10-20 11:56:00 +01001415 ALOGV("start");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 if (mTrack) {
1417 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent7070b362010-07-16 07:43:46 -07001418 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 mTrack->start();
1420 }
1421}
1422
Marco Nelissene274db12010-01-12 09:23:54 -08001423
1424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1426{
Andreas Hubere46b7be2009-07-14 16:56:47 -07001427 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1428
Steve Block71f2cf12011-10-20 11:56:00 +01001429 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001430 if (mTrack) {
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001431 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissenc39d2e32009-09-20 10:42:13 -07001432 return ret;
1433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 return NO_INIT;
1435}
1436
1437void MediaPlayerService::AudioOutput::stop()
1438{
Steve Block71f2cf12011-10-20 11:56:00 +01001439 ALOGV("stop");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 if (mTrack) mTrack->stop();
1441}
1442
1443void MediaPlayerService::AudioOutput::flush()
1444{
Steve Block71f2cf12011-10-20 11:56:00 +01001445 ALOGV("flush");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001446 if (mTrack) mTrack->flush();
1447}
1448
1449void MediaPlayerService::AudioOutput::pause()
1450{
Steve Block71f2cf12011-10-20 11:56:00 +01001451 ALOGV("pause");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452 if (mTrack) mTrack->pause();
1453}
1454
1455void MediaPlayerService::AudioOutput::close()
1456{
Steve Block71f2cf12011-10-20 11:56:00 +01001457 ALOGV("close");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 delete mTrack;
1459 mTrack = 0;
1460}
1461
1462void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1463{
Steve Block71f2cf12011-10-20 11:56:00 +01001464 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001465 mLeftVolume = left;
1466 mRightVolume = right;
1467 if (mTrack) {
1468 mTrack->setVolume(left, right);
1469 }
1470}
1471
Eric Laurent7070b362010-07-16 07:43:46 -07001472status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1473{
Steve Block71f2cf12011-10-20 11:56:00 +01001474 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent7070b362010-07-16 07:43:46 -07001475 mSendLevel = level;
1476 if (mTrack) {
1477 return mTrack->setAuxEffectSendLevel(level);
1478 }
1479 return NO_ERROR;
1480}
1481
1482status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1483{
Steve Block71f2cf12011-10-20 11:56:00 +01001484 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent7070b362010-07-16 07:43:46 -07001485 mAuxEffectId = effectId;
1486 if (mTrack) {
1487 return mTrack->attachAuxEffect(effectId);
1488 }
1489 return NO_ERROR;
1490}
1491
Andreas Hubere46b7be2009-07-14 16:56:47 -07001492// static
1493void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastene46a86f2011-06-01 15:20:35 -07001494 int event, void *cookie, void *info) {
Steve Block71f2cf12011-10-20 11:56:00 +01001495 //ALOGV("callbackwrapper");
Andreas Hubere46b7be2009-07-14 16:56:47 -07001496 if (event != AudioTrack::EVENT_MORE_DATA) {
1497 return;
1498 }
1499
1500 AudioOutput *me = (AudioOutput *)cookie;
1501 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1502
Andreas Huber6ed937e2010-02-09 16:59:18 -08001503 size_t actualSize = (*me->mCallback)(
Andreas Hubere46b7be2009-07-14 16:56:47 -07001504 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber6ed937e2010-02-09 16:59:18 -08001505
Andreas Huberc6e3b272011-04-04 11:43:40 -07001506 if (actualSize == 0 && buffer->size > 0) {
1507 // We've reached EOS but the audio track is not stopped yet,
1508 // keep playing silence.
Andreas Huber406a18b2010-02-18 16:45:13 -08001509
Andreas Huberc6e3b272011-04-04 11:43:40 -07001510 memset(buffer->raw, 0, buffer->size);
1511 actualSize = buffer->size;
1512 }
1513
1514 buffer->size = actualSize;
Andreas Hubere46b7be2009-07-14 16:56:47 -07001515}
1516
Eric Laurentb3bdf3f2010-10-07 18:23:03 -07001517int MediaPlayerService::AudioOutput::getSessionId()
1518{
1519 return mSessionId;
1520}
1521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522#undef LOG_TAG
1523#define LOG_TAG "AudioCache"
1524MediaPlayerService::AudioCache::AudioCache(const char* name) :
1525 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1526 mError(NO_ERROR), mCommandComplete(false)
1527{
1528 // create ashmem heap
1529 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1530}
1531
1532uint32_t MediaPlayerService::AudioCache::latency () const
1533{
1534 return 0;
1535}
1536
1537float MediaPlayerService::AudioCache::msecsPerFrame() const
1538{
1539 return mMsecsPerFrame;
1540}
1541
Eric Laurent0986e792010-01-19 17:37:09 -08001542status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1543{
1544 if (position == 0) return BAD_VALUE;
1545 *position = mSize;
1546 return NO_ERROR;
1547}
1548
Andreas Huber6ed937e2010-02-09 16:59:18 -08001549////////////////////////////////////////////////////////////////////////////////
1550
1551struct CallbackThread : public Thread {
1552 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1553 MediaPlayerBase::AudioSink::AudioCallback cb,
1554 void *cookie);
1555
1556protected:
1557 virtual ~CallbackThread();
1558
1559 virtual bool threadLoop();
1560
1561private:
1562 wp<MediaPlayerBase::AudioSink> mSink;
1563 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1564 void *mCookie;
1565 void *mBuffer;
1566 size_t mBufferSize;
1567
1568 CallbackThread(const CallbackThread &);
1569 CallbackThread &operator=(const CallbackThread &);
1570};
1571
1572CallbackThread::CallbackThread(
1573 const wp<MediaPlayerBase::AudioSink> &sink,
1574 MediaPlayerBase::AudioSink::AudioCallback cb,
1575 void *cookie)
1576 : mSink(sink),
1577 mCallback(cb),
1578 mCookie(cookie),
1579 mBuffer(NULL),
1580 mBufferSize(0) {
1581}
1582
1583CallbackThread::~CallbackThread() {
1584 if (mBuffer) {
1585 free(mBuffer);
1586 mBuffer = NULL;
1587 }
1588}
1589
1590bool CallbackThread::threadLoop() {
1591 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1592 if (sink == NULL) {
1593 return false;
1594 }
1595
1596 if (mBuffer == NULL) {
1597 mBufferSize = sink->bufferSize();
1598 mBuffer = malloc(mBufferSize);
1599 }
1600
1601 size_t actualSize =
1602 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1603
1604 if (actualSize > 0) {
1605 sink->write(mBuffer, actualSize);
1606 }
1607
1608 return true;
1609}
1610
1611////////////////////////////////////////////////////////////////////////////////
1612
Andreas Hubere46b7be2009-07-14 16:56:47 -07001613status_t MediaPlayerService::AudioCache::open(
Glenn Kasten1c5a89d2012-01-04 09:36:37 -08001614 uint32_t sampleRate, int channelCount, audio_format_t format, int bufferCount,
Andreas Hubere46b7be2009-07-14 16:56:47 -07001615 AudioCallback cb, void *cookie)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616{
Steve Block71f2cf12011-10-20 11:56:00 +01001617 ALOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
Dave Sparks14f89402009-12-09 20:20:26 -08001618 if (mHeap->getHeapID() < 0) {
1619 return NO_INIT;
1620 }
Andreas Hubere46b7be2009-07-14 16:56:47 -07001621
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001622 mSampleRate = sampleRate;
1623 mChannelCount = (uint16_t)channelCount;
Glenn Kasten1c5a89d2012-01-04 09:36:37 -08001624 mFormat = format;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber6ed937e2010-02-09 16:59:18 -08001626
1627 if (cb != NULL) {
1628 mCallbackThread = new CallbackThread(this, cb, cookie);
1629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 return NO_ERROR;
1631}
1632
Andreas Huber6ed937e2010-02-09 16:59:18 -08001633void MediaPlayerService::AudioCache::start() {
1634 if (mCallbackThread != NULL) {
1635 mCallbackThread->run("AudioCache callback");
1636 }
1637}
1638
1639void MediaPlayerService::AudioCache::stop() {
1640 if (mCallbackThread != NULL) {
1641 mCallbackThread->requestExitAndWait();
1642 }
1643}
1644
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001645ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1646{
Steve Block71f2cf12011-10-20 11:56:00 +01001647 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001648 if ((buffer == 0) || (size == 0)) return size;
1649
1650 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1651 if (p == NULL) return NO_INIT;
1652 p += mSize;
Steve Block71f2cf12011-10-20 11:56:00 +01001653 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 if (mSize + size > mHeap->getSize()) {
Steve Block3762c312012-01-06 19:20:56 +00001655 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 size = mHeap->getSize() - mSize;
1657 }
1658 memcpy(p, buffer, size);
1659 mSize += size;
1660 return size;
1661}
1662
1663// call with lock held
1664status_t MediaPlayerService::AudioCache::wait()
1665{
1666 Mutex::Autolock lock(mLock);
Dave Sparks16433e22010-03-01 19:29:58 -08001667 while (!mCommandComplete) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 mSignal.wait(mLock);
1669 }
1670 mCommandComplete = false;
1671
1672 if (mError == NO_ERROR) {
Steve Block71f2cf12011-10-20 11:56:00 +01001673 ALOGV("wait - success");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001674 } else {
Steve Block71f2cf12011-10-20 11:56:00 +01001675 ALOGV("wait - error");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 }
1677 return mError;
1678}
1679
Gloria Wang162ee492011-04-11 17:23:27 -07001680void MediaPlayerService::AudioCache::notify(
1681 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001682{
Steve Block71f2cf12011-10-20 11:56:00 +01001683 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001684 AudioCache* p = static_cast<AudioCache*>(cookie);
1685
1686 // ignore buffering messages
Dave Sparks14f89402009-12-09 20:20:26 -08001687 switch (msg)
1688 {
1689 case MEDIA_ERROR:
Steve Block3762c312012-01-06 19:20:56 +00001690 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 p->mError = ext1;
Dave Sparks14f89402009-12-09 20:20:26 -08001692 break;
1693 case MEDIA_PREPARED:
Steve Block71f2cf12011-10-20 11:56:00 +01001694 ALOGV("prepared");
Dave Sparks14f89402009-12-09 20:20:26 -08001695 break;
1696 case MEDIA_PLAYBACK_COMPLETE:
Steve Block71f2cf12011-10-20 11:56:00 +01001697 ALOGV("playback complete");
Dave Sparks14f89402009-12-09 20:20:26 -08001698 break;
1699 default:
Steve Block71f2cf12011-10-20 11:56:00 +01001700 ALOGV("ignored");
Dave Sparks14f89402009-12-09 20:20:26 -08001701 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001702 }
1703
1704 // wake up thread
Dave Sparks6c26fe42010-03-02 12:56:37 -08001705 Mutex::Autolock lock(p->mLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001706 p->mCommandComplete = true;
1707 p->mSignal.signal();
1708}
1709
Eric Laurentb3bdf3f2010-10-07 18:23:03 -07001710int MediaPlayerService::AudioCache::getSessionId()
1711{
1712 return 0;
1713}
1714
Gloria Wangd211f412011-02-19 18:37:57 -08001715void MediaPlayerService::addBatteryData(uint32_t params)
1716{
1717 Mutex::Autolock lock(mLock);
Gloria Wang9b3f1522011-02-24 14:51:45 -08001718
1719 int32_t time = systemTime() / 1000000L;
1720
1721 // change audio output devices. This notification comes from AudioFlinger
1722 if ((params & kBatteryDataSpeakerOn)
1723 || (params & kBatteryDataOtherAudioDeviceOn)) {
1724
1725 int deviceOn[NUM_AUDIO_DEVICES];
1726 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1727 deviceOn[i] = 0;
1728 }
1729
1730 if ((params & kBatteryDataSpeakerOn)
1731 && (params & kBatteryDataOtherAudioDeviceOn)) {
1732 deviceOn[SPEAKER_AND_OTHER] = 1;
1733 } else if (params & kBatteryDataSpeakerOn) {
1734 deviceOn[SPEAKER] = 1;
1735 } else {
1736 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1737 }
1738
1739 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1740 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1741
1742 if (mBatteryAudio.refCount > 0) { // if playing audio
1743 if (!deviceOn[i]) {
1744 mBatteryAudio.lastTime[i] += time;
1745 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1746 mBatteryAudio.lastTime[i] = 0;
1747 } else {
1748 mBatteryAudio.lastTime[i] = 0 - time;
1749 }
1750 }
1751
1752 mBatteryAudio.deviceOn[i] = deviceOn[i];
1753 }
1754 }
1755 return;
1756 }
1757
1758 // an sudio stream is started
1759 if (params & kBatteryDataAudioFlingerStart) {
1760 // record the start time only if currently no other audio
1761 // is being played
1762 if (mBatteryAudio.refCount == 0) {
1763 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1764 if (mBatteryAudio.deviceOn[i]) {
1765 mBatteryAudio.lastTime[i] -= time;
1766 }
1767 }
1768 }
1769
1770 mBatteryAudio.refCount ++;
1771 return;
1772
1773 } else if (params & kBatteryDataAudioFlingerStop) {
1774 if (mBatteryAudio.refCount <= 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001775 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9b3f1522011-02-24 14:51:45 -08001776 return;
1777 }
1778
1779 // record the stop time only if currently this is the only
1780 // audio being played
1781 if (mBatteryAudio.refCount == 1) {
1782 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1783 if (mBatteryAudio.deviceOn[i]) {
1784 mBatteryAudio.lastTime[i] += time;
1785 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1786 mBatteryAudio.lastTime[i] = 0;
1787 }
1788 }
1789 }
1790
1791 mBatteryAudio.refCount --;
1792 return;
1793 }
1794
Gloria Wangd211f412011-02-19 18:37:57 -08001795 int uid = IPCThreadState::self()->getCallingUid();
1796 if (uid == AID_MEDIA) {
1797 return;
1798 }
1799 int index = mBatteryData.indexOfKey(uid);
Gloria Wangd211f412011-02-19 18:37:57 -08001800
1801 if (index < 0) { // create a new entry for this UID
1802 BatteryUsageInfo info;
1803 info.audioTotalTime = 0;
1804 info.videoTotalTime = 0;
1805 info.audioLastTime = 0;
1806 info.videoLastTime = 0;
1807 info.refCount = 0;
1808
Gloria Wang9b3f1522011-02-24 14:51:45 -08001809 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block3762c312012-01-06 19:20:56 +00001810 ALOGE("Battery track error: no memory for new app");
Gloria Wang9b3f1522011-02-24 14:51:45 -08001811 return;
1812 }
Gloria Wangd211f412011-02-19 18:37:57 -08001813 }
1814
1815 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1816
1817 if (params & kBatteryDataCodecStarted) {
1818 if (params & kBatteryDataTrackAudio) {
1819 info.audioLastTime -= time;
1820 info.refCount ++;
1821 }
1822 if (params & kBatteryDataTrackVideo) {
1823 info.videoLastTime -= time;
1824 info.refCount ++;
1825 }
1826 } else {
1827 if (info.refCount == 0) {
Steve Block8564c8d2012-01-05 23:22:43 +00001828 ALOGW("Battery track warning: refCount is already 0");
Gloria Wangd211f412011-02-19 18:37:57 -08001829 return;
1830 } else if (info.refCount < 0) {
Steve Block3762c312012-01-06 19:20:56 +00001831 ALOGE("Battery track error: refCount < 0");
Gloria Wangd211f412011-02-19 18:37:57 -08001832 mBatteryData.removeItem(uid);
1833 return;
1834 }
1835
1836 if (params & kBatteryDataTrackAudio) {
1837 info.audioLastTime += time;
1838 info.refCount --;
1839 }
1840 if (params & kBatteryDataTrackVideo) {
1841 info.videoLastTime += time;
1842 info.refCount --;
1843 }
1844
1845 // no stream is being played by this UID
1846 if (info.refCount == 0) {
1847 info.audioTotalTime += info.audioLastTime;
1848 info.audioLastTime = 0;
1849 info.videoTotalTime += info.videoLastTime;
1850 info.videoLastTime = 0;
1851 }
1852 }
1853}
1854
1855status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
1856 Mutex::Autolock lock(mLock);
Gloria Wang9b3f1522011-02-24 14:51:45 -08001857
1858 // audio output devices usage
1859 int32_t time = systemTime() / 1000000L; //in ms
1860 int32_t totalTime;
1861
1862 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1863 totalTime = mBatteryAudio.totalTime[i];
1864
1865 if (mBatteryAudio.deviceOn[i]
1866 && (mBatteryAudio.lastTime[i] != 0)) {
1867 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
1868 totalTime += tmpTime;
1869 }
1870
1871 reply->writeInt32(totalTime);
1872 // reset the total time
1873 mBatteryAudio.totalTime[i] = 0;
1874 }
1875
1876 // codec usage
Gloria Wangd211f412011-02-19 18:37:57 -08001877 BatteryUsageInfo info;
1878 int size = mBatteryData.size();
1879
1880 reply->writeInt32(size);
1881 int i = 0;
1882
1883 while (i < size) {
1884 info = mBatteryData.valueAt(i);
1885
1886 reply->writeInt32(mBatteryData.keyAt(i)); //UID
1887 reply->writeInt32(info.audioTotalTime);
1888 reply->writeInt32(info.videoTotalTime);
1889
1890 info.audioTotalTime = 0;
1891 info.videoTotalTime = 0;
1892
1893 // remove the UID entry where no stream is being played
1894 if (info.refCount <= 0) {
1895 mBatteryData.removeItemsAt(i);
1896 size --;
1897 i --;
1898 }
1899 i++;
1900 }
1901 return NO_ERROR;
1902}
nikobc726922009-07-20 15:07:26 -07001903} // namespace android