blob: 4df7f3d654c4a1fc87f4d0c7290d4e29ac6de5af [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
Gloria Wang7cf180c2011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
37#include <android_runtime/ActivityManager.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070038
Mathias Agopian75624082009-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 Gennis7dae00b2011-10-26 18:36:31 -070043#include <gui/SurfaceTextureClient.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070044#include <utils/Errors.h> // for status_t
45#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070046#include <utils/SystemClock.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070047#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-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>
nikoa64c8c72009-07-20 15:07:26 -070053#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070055#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070056#include <media/stagefright/MediaErrors.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057
Dima Zavin64760242011-05-11 14:15:23 -070058#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070059
Gloria Wang7cf180c2011-02-19 18:37:57 -080060#include <private/android_filesystem_config.h>
61
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062#include "MediaRecorderClient.h"
63#include "MediaPlayerService.h"
64#include "MetadataRetrieverClient.h"
65
66#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070067#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070068#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080069#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070070
Andreas Huber20111aa2009-07-14 16:56:47 -070071#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070072
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070073namespace {
nikoa64c8c72009-07-20 15:07:26 -070074using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-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 Cataniaa7e0e8b2009-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
nikoa64c8c72009-07-20 15:07:26 -070084// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070085
Nicolas Cataniaa7e0e8b2009-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,
nikoa64c8c72009-07-20 15:07:26 -0700109 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700110 status_t *status)
111{
Nicolas Catania48290382009-07-10 13:53:06 -0700112 int32_t val;
113 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700114 {
Steve Block29357bc2012-01-06 19:20:56 +0000115 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700116 *status = NOT_ENOUGH_DATA;
117 return false;
118 }
119
Nicolas Catania48290382009-07-10 13:53:06 -0700120 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700121 {
Steve Block29357bc2012-01-06 19:20:56 +0000122 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 *status = BAD_VALUE;
124 return false;
125 }
126
Nicolas Catania48290382009-07-10 13:53:06 -0700127 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128
129 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700130 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700131
nikoa64c8c72009-07-20 15:07:26 -0700132 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133
Nicolas Catania48290382009-07-10 13:53:06 -0700134
135 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136 {
Steve Block29357bc2012-01-06 19:20:56 +0000137 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138 *status = NOT_ENOUGH_DATA;
139 return false;
140 }
141
nikoa64c8c72009-07-20 15:07:26 -0700142 const Metadata::Type *data =
143 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144
Nicolas Catania48290382009-07-10 13:53:06 -0700145 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146 {
Steve Block29357bc2012-01-06 19:20:56 +0000147 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-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 Catania48290382009-07-10 13:53:06 -0700155 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700156 {
Nicolas Catania48290382009-07-10 13:53:06 -0700157 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700158 ++data;
159 }
160 *status = OK;
161 return true;
162}
163
Nicolas Catania48290382009-07-10 13:53:06 -0700164// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700165// @param val To be searched.
166// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700167bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700168{
169 // Deal with empty and ANY right away
170 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700171 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700172
Nicolas Catania48290382009-07-10 13:53:06 -0700173 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700174}
175
176} // anonymous namespace
177
178
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800179namespace android {
180
Dave Burked681bbb2011-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 Block29357bc2012-01-06 19:20:56 +0000187 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100188 return ok;
189}
190
The Android Open Source Project89fa4ad2009-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 Project89fa4ad2009-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 Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100218 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219 mNextConnId = 1;
Gloria Wang9ee159b2011-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 Project89fa4ad2009-03-03 19:31:44 -0800229}
230
231MediaPlayerService::~MediaPlayerService()
232{
Steve Block3856b092011-10-20 11:56:00 +0100233 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234}
235
236sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
237{
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
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800253sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
254{
255 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257 return retriever;
258}
259
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800260sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100261 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262{
263 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700264
265 sp<Client> c = new Client(
266 this, pid, connId, client, audioSessionId,
267 IPCThreadState::self()->getCallingUid());
268
Steve Block3856b092011-10-20 11:56:00 +0100269 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100270 IPCThreadState::self()->getCallingUid());
271
272 wp<Client> w = c;
273 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800274 Mutex::Autolock lock(mLock);
275 mClients.add(w);
276 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800277 return c;
278}
279
Andreas Huber318ad9c2009-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 Huber20111aa2009-07-14 16:56:47 -0700288}
289
The Android Open Source Project89fa4ad2009-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 Laurent2beeb502010-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 Project89fa4ad2009-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 Hubera0b1d4b2011-06-07 15:52:25 -0700346 if (mPlayer != NULL) {
347 mPlayer->dump(fd, args);
348 }
The Android Open Source Project89fa4ad2009-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 Project89fa4ad2009-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 Dongb9141222010-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 Donge579e282011-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 Dongb9141222010-07-08 11:16:11 -0700385 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700386 }
387
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700389 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 FILE *f = fopen(buffer, "r");
391 if (f) {
392 while (!feof(f)) {
393 fgets(buffer, SIZE, f);
Dave Sparks02fa8342010-09-27 16:55:18 -0700394 if (strstr(buffer, " /mnt/sdcard/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700396 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-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 Kasten0512ab52011-05-04 17:58:57 -0700409 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-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 Kasten0512ab52011-05-04 17:58:57 -0700415 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-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 Sparks02fa8342010-09-27 16:55:18 -0700430 if (strstr(linkto, "/mnt/sdcard/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800431 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700432 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-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 Project89fa4ad2009-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 Dong8635b7b2011-03-14 17:01:38 -0700463 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800464 }
The Android Open Source Project89fa4ad2009-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 Huber9b80c2b2011-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 Project89fa4ad2009-03-03 19:31:44 -0800480{
Steve Block3856b092011-10-20 11:56:00 +0100481 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-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 Laurenta514bdb2010-06-21 09:27:30 -0700488 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700489 mUID = uid;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700490
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000492 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 mAntagonizer = new Antagonizer(notify, this);
494#endif
495}
496
497MediaPlayerService::Client::~Client()
498{
Steve Block3856b092011-10-20 11:56:00 +0100499 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100508 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-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 Sparks795fa582009-03-24 17:57:12 -0700516 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700517
The Android Open Source Project89fa4ad2009-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 Blockb8a80522011-12-20 16:23:08 +0000526 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527 mAntagonizer->kill();
528#endif
529 p->reset();
530 }
531
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700532 disconnectNativeWindow();
533
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800534 IPCThreadState::self()->flushCommands();
535}
536
Andreas Huber47f59cf2009-08-07 09:30:32 -0700537static player_type getDefaultPlayerType() {
Andreas Huber608d77b2010-06-23 16:40:57 -0700538 return STAGEFRIGHT_PLAYER;
Andreas Huber47f59cf2009-08-07 09:30:32 -0700539}
540
James Dong148c1a22009-09-06 14:29:45 -0700541player_type getPlayerType(int fd, int64_t offset, int64_t length)
The Android Open Source Project89fa4ad2009-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 Huber608d77b2010-06-23 16:40:57 -0700552 return STAGEFRIGHT_PLAYER;
The Android Open Source Project89fa4ad2009-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 Huber47f59cf2009-08-07 09:30:32 -0700571 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572}
573
James Dong148c1a22009-09-06 14:29:45 -0700574player_type getPlayerType(const char* url)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800575{
Nicolas Catania14d27472009-07-13 14:37:49 -0700576 if (TestPlayerStub::canBeUsed(url)) {
577 return TEST_PLAYER;
578 }
579
Andreas Huber0a73d812011-03-21 16:28:04 -0700580 if (!strncasecmp("http://", url, 7)
581 || !strncasecmp("https://", url, 8)) {
Andreas Hubered8d14f2011-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 Huber5bc087c2010-12-23 10:27:40 -0800586
Andreas Hubered8d14f2011-02-16 09:05:38 -0800587 if (strstr(url,"m3u8")) {
588 return NU_PLAYER;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800589 }
590 }
591
Andreas Huber2bfdd422011-10-11 15:24:07 -0700592 if (!strncasecmp("rtsp://", url, 7)) {
593 return NU_PLAYER;
594 }
595
The Android Open Source Project89fa4ad2009-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 Enofc1c7b92010-03-19 23:18:02 +0900602 if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
Andreas Huber608d77b2010-06-23 16:40:57 -0700603 return FILE_EXTS[i].playertype;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800604 }
605 }
606 }
607
Andreas Huber47f59cf2009-08-07 09:30:32 -0700608 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-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 Project89fa4ad2009-03-03 19:31:44 -0800616 case SONIVOX_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100617 ALOGV(" create MidiFile");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618 p = new MidiFile();
619 break;
Andreas Huber20111aa2009-07-14 16:56:47 -0700620 case STAGEFRIGHT_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100621 ALOGV(" create StagefrightPlayer");
Andreas Huber20111aa2009-07-14 16:56:47 -0700622 p = new StagefrightPlayer;
623 break;
Andreas Huberf9334412010-12-15 15:17:42 -0800624 case NU_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100625 ALOGV(" create NuPlayer");
Andreas Huberf9334412010-12-15 15:17:42 -0800626 p = new NuPlayerDriver;
627 break;
Nicolas Catania14d27472009-07-13 14:37:49 -0700628 case TEST_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100629 ALOGV("Create Test Player stub");
Nicolas Catania14d27472009-07-13 14:37:49 -0700630 p = new TestPlayerStub();
631 break;
James Dong30d713a2010-11-10 18:42:40 -0800632 default:
Steve Block29357bc2012-01-06 19:20:56 +0000633 ALOGE("Unknown player type: %d", playerType);
James Dong30d713a2010-11-10 18:42:40 -0800634 return NULL;
The Android Open Source Project89fa4ad2009-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 Block29357bc2012-01-06 19:20:56 +0000644 ALOGE("Failed to create player object");
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100654 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655 p.clear();
656 }
657 if (p == NULL) {
658 p = android::createPlayer(playerType, this, notify);
659 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700660
Jason Simmonsdb29e522011-08-12 13:46:55 -0700661 if (p != NULL) {
662 p->setUID(mUID);
663 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700664
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 return p;
666}
667
Andreas Huber2db84552010-01-28 11:19:57 -0800668status_t MediaPlayerService::Client::setDataSource(
669 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670{
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672 if (url == NULL)
673 return UNKNOWN_ERROR;
674
Dave Burked681bbb2011-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 Project89fa4ad2009-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 Block29357bc2012-01-06 19:20:56 +0000691 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100699 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-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 Laurenta514bdb2010-06-21 09:27:30 -0700706 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800707 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
708 }
709
710 // now set data source
Steve Block3856b092011-10-20 11:56:00 +0100711 ALOGV(" setDataSource");
Andreas Huber2db84552010-01-28 11:19:57 -0800712 mStatus = p->setDataSource(url, headers);
Nicolas Catania14d27472009-07-13 14:37:49 -0700713 if (mStatus == NO_ERROR) {
714 mPlayer = p;
715 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000716 ALOGE(" error: %d", mStatus);
Nicolas Catania14d27472009-07-13 14:37:49 -0700717 }
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100724 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800725 struct stat sb;
726 int ret = fstat(fd, &sb);
727 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000728 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800729 return UNKNOWN_ERROR;
730 }
731
Steve Block3856b092011-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 Project89fa4ad2009-03-03 19:31:44 -0800737
738 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000739 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100745 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746 }
747
748 player_type playerType = getPlayerType(fd, offset, length);
Steve Block3856b092011-10-20 11:56:00 +0100749 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-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 Laurenta514bdb2010-06-21 09:27:30 -0700756 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project89fa4ad2009-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 Burked681bbb2011-08-30 14:39:17 +0100763
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800764 return mStatus;
765}
766
Andreas Hubere2b10282010-11-23 11:41:34 -0800767status_t MediaPlayerService::Client::setDataSource(
768 const sp<IStreamSource> &source) {
769 // create the right type of player
Andreas Huberf9334412010-12-15 15:17:42 -0800770 sp<MediaPlayerBase> p = createPlayer(NU_PLAYER);
Andreas Hubere2b10282010-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 Gennis7dae00b2011-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 Block5ff1dd52012-01-05 23:22:43 +0000797 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700798 strerror(-err), err);
799 }
800 }
801 mConnectedWindow.clear();
802}
803
Glenn Kasten11731182011-02-08 17:26:17 -0800804status_t MediaPlayerService::Client::setVideoSurfaceTexture(
805 const sp<ISurfaceTexture>& surfaceTexture)
806{
Steve Block3856b092011-10-20 11:56:00 +0100807 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800808 sp<MediaPlayerBase> p = getPlayer();
809 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-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 Block29357bc2012-01-06 19:20:56 +0000824 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-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 Kasten11731182011-02-08 17:26:17 -0800852}
853
Nicolas Catania1d187f12009-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 Cataniaa7e0e8b2009-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;
nikoa64c8c72009-07-20 15:07:26 -0700866 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700867
Nicolas Catania48290382009-07-10 13:53:06 -0700868 if (unmarshallFilter(filter, &allow, &status) &&
869 unmarshallFilter(filter, &drop, &status)) {
870 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700871
872 mMetadataAllow = allow;
873 mMetadataDrop = drop;
874 }
875 return status;
876}
877
Nicolas Catania48290382009-07-10 13:53:06 -0700878status_t MediaPlayerService::Client::getMetadata(
879 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700880{
nikoa64c8c72009-07-20 15:07:26 -0700881 sp<MediaPlayerBase> player = getPlayer();
882 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700883
nikod608a812009-07-16 16:39:53 -0700884 status_t status;
885 // Placeholder for the return code, updated by the caller.
886 reply->writeInt32(-1);
887
nikoa64c8c72009-07-20 15:07:26 -0700888 media::Metadata::Filter ids;
Nicolas Catania48290382009-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) {
nikod608a812009-07-16 16:39:53 -0700896 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700897 }
898 mMetadataUpdated.clear();
899 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700900
nikoa64c8c72009-07-20 15:07:26 -0700901 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700902
nikoa64c8c72009-07-20 15:07:26 -0700903 metadata.appendHeader();
904 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700905
906 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700907 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000908 ALOGE("getMetadata failed %d", status);
nikod608a812009-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
nikod608a812009-07-16 16:39:53 -0700916 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700917 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700918 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700919}
920
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800921status_t MediaPlayerService::Client::prepareAsync()
922{
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-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 Blockb8a80522011-12-20 16:23:08 +0000928 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100936 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100945 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100953 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100965 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800966 return NO_ERROR;
967}
968
969status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
970{
Steve Block3856b092011-10-20 11:56:00 +0100971 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100976 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000978 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 }
980 return ret;
981}
982
983status_t MediaPlayerService::Client::getDuration(int *msec)
984{
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +0100990 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800991 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000992 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800993 }
994 return ret;
995}
996
997status_t MediaPlayerService::Client::seekTo(int msec)
998{
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +01001007 ALOGV("[%d] reset", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001008 sp<MediaPlayerBase> p = getPlayer();
1009 if (p == 0) return UNKNOWN_ERROR;
1010 return p->reset();
1011}
1012
Glenn Kastenfff6d712012-01-12 16:38:12 -08001013status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001014{
Steve Block3856b092011-10-20 11:56:00 +01001015 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +01001024 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +01001033 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
The Android Open Source Project89fa4ad2009-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 Laurent2beeb502010-07-16 07:43:46 -07001040status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1041{
Steve Block3856b092011-10-20 11:56:00 +01001042 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-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 Block3856b092011-10-20 11:56:00 +01001050 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001051 Mutex::Autolock l(mLock);
1052 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1053 return NO_ERROR;
1054}
Nicolas Catania48290382009-07-10 13:53:06 -07001055
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001056status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001057 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-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 Block3856b092011-10-20 11:56:00 +01001064 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-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 Wangb483c472011-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 Project89fa4ad2009-03-03 19:31:44 -08001072{
1073 Client* client = static_cast<Client*>(cookie);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001074
1075 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001076 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001077 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-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 Cataniaa7e0e8b2009-07-08 08:57:42 -07001086 }
Steve Block3856b092011-10-20 11:56:00 +01001087 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
Gloria Wangb483c472011-04-11 17:23:27 -07001088 client->mClient->notify(msg, ext1, ext2, obj);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001089}
1090
Nicolas Catania48290382009-07-10 13:53:06 -07001091
nikoa64c8c72009-07-20 15:07:26 -07001092bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001093{
Nicolas Catania48290382009-07-10 13:53:06 -07001094 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001095
Nicolas Catania48290382009-07-10 13:53:06 -07001096 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001097 return true;
1098 }
1099
Nicolas Catania48290382009-07-10 13:53:06 -07001100 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001101 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001102 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001103 return true;
1104 }
1105}
1106
Nicolas Catania48290382009-07-10 13:53:06 -07001107
nikoa64c8c72009-07-20 15:07:26 -07001108void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-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 Project89fa4ad2009-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 Blockb8a80522011-12-20 16:23:08 +00001134 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001135 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1136 while (!p->mExit) {
1137 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001138 ALOGV("send event");
The Android Open Source Project89fa4ad2009-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 Blockb8a80522011-12-20 16:23:08 +00001145 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001146 return 0;
1147}
1148#endif
1149
1150static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1151
Glenn Kastene1c39622012-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 Project89fa4ad2009-03-03 19:31:44 -08001153{
Steve Block3856b092011-10-20 11:56:00 +01001154 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-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 Blockb8a80522011-12-20 16:23:08 +00001163 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001164 return mem;
1165 }
1166
1167 player_type playerType = getPlayerType(url);
Steve Block3856b092011-10-20 11:56:00 +01001168 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +01001181 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001182 player->prepareAsync();
1183
Steve Block3856b092011-10-20 11:56:00 +01001184 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001185 if (cache->wait() != NO_ERROR) goto Exit;
1186
Steve Block3856b092011-10-20 11:56:00 +01001187 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188 player->start();
1189
Steve Block3856b092011-10-20 11:56:00 +01001190 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-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 Project89fa4ad2009-03-03 19:31:44 -08001196
1197 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1198 *pSampleRate = cache->sampleRate();
1199 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001200 *pFormat = cache->format();
Steve Block3856b092011-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 Project89fa4ad2009-03-03 19:31:44 -08001202
1203Exit:
1204 if (player != 0) player->reset();
1205 return mem;
1206}
1207
Glenn Kastene1c39622012-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 Project89fa4ad2009-03-03 19:31:44 -08001209{
Steve Block3856b092011-10-20 11:56:00 +01001210 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001211 sp<MemoryBase> mem;
1212 sp<MediaPlayerBase> player;
1213
1214 player_type playerType = getPlayerType(fd, offset, length);
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-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 Block3856b092011-10-20 11:56:00 +01001228 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001229 player->prepareAsync();
1230
Steve Block3856b092011-10-20 11:56:00 +01001231 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001232 if (cache->wait() != NO_ERROR) goto Exit;
1233
Steve Block3856b092011-10-20 11:56:00 +01001234 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001235 player->start();
1236
Steve Block3856b092011-10-20 11:56:00 +01001237 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-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 Project89fa4ad2009-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 Block3856b092011-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 Project89fa4ad2009-03-03 19:31:44 -08001249
1250Exit:
1251 if (player != 0) player->reset();
1252 ::close(fd);
1253 return mem;
1254}
1255
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001256
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001257#undef LOG_TAG
1258#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001259MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001260 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001261 mCallbackCookie(NULL),
1262 mSessionId(sessionId) {
Steve Block3856b092011-10-20 11:56:00 +01001263 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 mTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001265 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 mLeftVolume = 1.0;
1267 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001268 mPlaybackRatePermille = 1000;
1269 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270 mLatency = 0;
1271 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001272 mAuxEffectId = 0;
1273 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274 setMinBufferCount();
1275}
1276
1277MediaPlayerService::AudioOutput::~AudioOutput()
1278{
1279 close();
1280}
1281
1282void MediaPlayerService::AudioOutput::setMinBufferCount()
1283{
1284 char value[PROPERTY_VALUE_MAX];
1285 if (property_get("ro.kernel.qemu", value, 0)) {
1286 mIsOnEmulator = true;
1287 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1288 }
1289}
1290
1291bool MediaPlayerService::AudioOutput::isOnEmulator()
1292{
1293 setMinBufferCount();
1294 return mIsOnEmulator;
1295}
1296
1297int MediaPlayerService::AudioOutput::getMinBufferCount()
1298{
1299 setMinBufferCount();
1300 return mMinBufferCount;
1301}
1302
1303ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1304{
1305 if (mTrack == 0) return NO_INIT;
1306 return mTrack->frameCount() * frameSize();
1307}
1308
1309ssize_t MediaPlayerService::AudioOutput::frameCount() const
1310{
1311 if (mTrack == 0) return NO_INIT;
1312 return mTrack->frameCount();
1313}
1314
1315ssize_t MediaPlayerService::AudioOutput::channelCount() const
1316{
1317 if (mTrack == 0) return NO_INIT;
1318 return mTrack->channelCount();
1319}
1320
1321ssize_t MediaPlayerService::AudioOutput::frameSize() const
1322{
1323 if (mTrack == 0) return NO_INIT;
1324 return mTrack->frameSize();
1325}
1326
1327uint32_t MediaPlayerService::AudioOutput::latency () const
1328{
1329 return mLatency;
1330}
1331
1332float MediaPlayerService::AudioOutput::msecsPerFrame() const
1333{
1334 return mMsecsPerFrame;
1335}
1336
Eric Laurent342e9cf2010-01-19 17:37:09 -08001337status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1338{
1339 if (mTrack == 0) return NO_INIT;
1340 return mTrack->getPosition(position);
1341}
1342
Andreas Huber20111aa2009-07-14 16:56:47 -07001343status_t MediaPlayerService::AudioOutput::open(
Glenn Kastene1c39622012-01-04 09:36:37 -08001344 uint32_t sampleRate, int channelCount, audio_format_t format, int bufferCount,
Andreas Huber20111aa2009-07-14 16:56:47 -07001345 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001346{
Andreas Huber20111aa2009-07-14 16:56:47 -07001347 mCallback = cb;
1348 mCallbackCookie = cookie;
1349
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001350 // Check argument "bufferCount" against the mininum buffer count
1351 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001352 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001353 bufferCount = mMinBufferCount;
1354
1355 }
Steve Block3856b092011-10-20 11:56:00 +01001356 ALOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001357 if (mTrack) close();
1358 int afSampleRate;
1359 int afFrameCount;
1360 int frameCount;
1361
1362 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1363 return NO_INIT;
1364 }
1365 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1366 return NO_INIT;
1367 }
1368
1369 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001370
1371 AudioTrack *t;
1372 if (mCallback != NULL) {
1373 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001374 mStreamType,
1375 sampleRate,
1376 format,
Dima Zavinfce7a472011-04-19 22:30:36 -07001377 (channelCount == 2) ? AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001378 frameCount,
1379 0 /* flags */,
1380 CallbackWrapper,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001381 this,
1382 0,
1383 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001384 } else {
1385 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001386 mStreamType,
1387 sampleRate,
1388 format,
Dima Zavinfce7a472011-04-19 22:30:36 -07001389 (channelCount == 2) ? AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001390 frameCount,
1391 0,
1392 NULL,
1393 NULL,
1394 0,
1395 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001396 }
1397
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001398 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001399 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001400 delete t;
1401 return NO_INIT;
1402 }
1403
Steve Block3856b092011-10-20 11:56:00 +01001404 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001406
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001407 mSampleRateHz = sampleRate;
1408 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Dave Sparks1d711f62009-12-03 10:13:32 -08001409 mLatency = t->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001410 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001411
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001412 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1413 if (res != NO_ERROR) {
1414 return res;
1415 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001416 t->setAuxEffectSendLevel(mSendLevel);
1417 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001418}
1419
1420void MediaPlayerService::AudioOutput::start()
1421{
Steve Block3856b092011-10-20 11:56:00 +01001422 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001423 if (mTrack) {
1424 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001425 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001426 mTrack->start();
1427 }
1428}
1429
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001430
1431
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1433{
Andreas Huber20111aa2009-07-14 16:56:47 -07001434 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1435
Steve Block3856b092011-10-20 11:56:00 +01001436 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001437 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001438 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001439 return ret;
1440 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001441 return NO_INIT;
1442}
1443
1444void MediaPlayerService::AudioOutput::stop()
1445{
Steve Block3856b092011-10-20 11:56:00 +01001446 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001447 if (mTrack) mTrack->stop();
1448}
1449
1450void MediaPlayerService::AudioOutput::flush()
1451{
Steve Block3856b092011-10-20 11:56:00 +01001452 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001453 if (mTrack) mTrack->flush();
1454}
1455
1456void MediaPlayerService::AudioOutput::pause()
1457{
Steve Block3856b092011-10-20 11:56:00 +01001458 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001459 if (mTrack) mTrack->pause();
1460}
1461
1462void MediaPlayerService::AudioOutput::close()
1463{
Steve Block3856b092011-10-20 11:56:00 +01001464 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001465 delete mTrack;
1466 mTrack = 0;
1467}
1468
1469void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1470{
Steve Block3856b092011-10-20 11:56:00 +01001471 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001472 mLeftVolume = left;
1473 mRightVolume = right;
1474 if (mTrack) {
1475 mTrack->setVolume(left, right);
1476 }
1477}
1478
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001479status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1480{
1481 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1482 status_t res = NO_ERROR;
1483 if (mTrack) {
1484 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1485 } else {
1486 res = NO_INIT;
1487 }
1488 mPlaybackRatePermille = ratePermille;
1489 if (mSampleRateHz != 0) {
1490 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1491 }
1492 return res;
1493}
1494
Eric Laurent2beeb502010-07-16 07:43:46 -07001495status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1496{
Steve Block3856b092011-10-20 11:56:00 +01001497 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001498 mSendLevel = level;
1499 if (mTrack) {
1500 return mTrack->setAuxEffectSendLevel(level);
1501 }
1502 return NO_ERROR;
1503}
1504
1505status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1506{
Steve Block3856b092011-10-20 11:56:00 +01001507 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001508 mAuxEffectId = effectId;
1509 if (mTrack) {
1510 return mTrack->attachAuxEffect(effectId);
1511 }
1512 return NO_ERROR;
1513}
1514
Andreas Huber20111aa2009-07-14 16:56:47 -07001515// static
1516void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001517 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001518 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001519 if (event != AudioTrack::EVENT_MORE_DATA) {
1520 return;
1521 }
1522
1523 AudioOutput *me = (AudioOutput *)cookie;
1524 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1525
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001526 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001527 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001528
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001529 if (actualSize == 0 && buffer->size > 0) {
1530 // We've reached EOS but the audio track is not stopped yet,
1531 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001532
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001533 memset(buffer->raw, 0, buffer->size);
1534 actualSize = buffer->size;
1535 }
1536
1537 buffer->size = actualSize;
Andreas Huber20111aa2009-07-14 16:56:47 -07001538}
1539
Eric Laurent8c563ed2010-10-07 18:23:03 -07001540int MediaPlayerService::AudioOutput::getSessionId()
1541{
1542 return mSessionId;
1543}
1544
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001545#undef LOG_TAG
1546#define LOG_TAG "AudioCache"
1547MediaPlayerService::AudioCache::AudioCache(const char* name) :
1548 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1549 mError(NO_ERROR), mCommandComplete(false)
1550{
1551 // create ashmem heap
1552 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1553}
1554
1555uint32_t MediaPlayerService::AudioCache::latency () const
1556{
1557 return 0;
1558}
1559
1560float MediaPlayerService::AudioCache::msecsPerFrame() const
1561{
1562 return mMsecsPerFrame;
1563}
1564
Eric Laurent342e9cf2010-01-19 17:37:09 -08001565status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1566{
1567 if (position == 0) return BAD_VALUE;
1568 *position = mSize;
1569 return NO_ERROR;
1570}
1571
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001572////////////////////////////////////////////////////////////////////////////////
1573
1574struct CallbackThread : public Thread {
1575 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1576 MediaPlayerBase::AudioSink::AudioCallback cb,
1577 void *cookie);
1578
1579protected:
1580 virtual ~CallbackThread();
1581
1582 virtual bool threadLoop();
1583
1584private:
1585 wp<MediaPlayerBase::AudioSink> mSink;
1586 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1587 void *mCookie;
1588 void *mBuffer;
1589 size_t mBufferSize;
1590
1591 CallbackThread(const CallbackThread &);
1592 CallbackThread &operator=(const CallbackThread &);
1593};
1594
1595CallbackThread::CallbackThread(
1596 const wp<MediaPlayerBase::AudioSink> &sink,
1597 MediaPlayerBase::AudioSink::AudioCallback cb,
1598 void *cookie)
1599 : mSink(sink),
1600 mCallback(cb),
1601 mCookie(cookie),
1602 mBuffer(NULL),
1603 mBufferSize(0) {
1604}
1605
1606CallbackThread::~CallbackThread() {
1607 if (mBuffer) {
1608 free(mBuffer);
1609 mBuffer = NULL;
1610 }
1611}
1612
1613bool CallbackThread::threadLoop() {
1614 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1615 if (sink == NULL) {
1616 return false;
1617 }
1618
1619 if (mBuffer == NULL) {
1620 mBufferSize = sink->bufferSize();
1621 mBuffer = malloc(mBufferSize);
1622 }
1623
1624 size_t actualSize =
1625 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1626
1627 if (actualSize > 0) {
1628 sink->write(mBuffer, actualSize);
1629 }
1630
1631 return true;
1632}
1633
1634////////////////////////////////////////////////////////////////////////////////
1635
Andreas Huber20111aa2009-07-14 16:56:47 -07001636status_t MediaPlayerService::AudioCache::open(
Glenn Kastene1c39622012-01-04 09:36:37 -08001637 uint32_t sampleRate, int channelCount, audio_format_t format, int bufferCount,
Andreas Huber20111aa2009-07-14 16:56:47 -07001638 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001639{
Steve Block3856b092011-10-20 11:56:00 +01001640 ALOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001641 if (mHeap->getHeapID() < 0) {
1642 return NO_INIT;
1643 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001644
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001645 mSampleRate = sampleRate;
1646 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001647 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001648 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001649
1650 if (cb != NULL) {
1651 mCallbackThread = new CallbackThread(this, cb, cookie);
1652 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001653 return NO_ERROR;
1654}
1655
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001656void MediaPlayerService::AudioCache::start() {
1657 if (mCallbackThread != NULL) {
1658 mCallbackThread->run("AudioCache callback");
1659 }
1660}
1661
1662void MediaPlayerService::AudioCache::stop() {
1663 if (mCallbackThread != NULL) {
1664 mCallbackThread->requestExitAndWait();
1665 }
1666}
1667
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001668ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1669{
Steve Block3856b092011-10-20 11:56:00 +01001670 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001671 if ((buffer == 0) || (size == 0)) return size;
1672
1673 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1674 if (p == NULL) return NO_INIT;
1675 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001676 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001677 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001678 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001679 size = mHeap->getSize() - mSize;
1680 }
1681 memcpy(p, buffer, size);
1682 mSize += size;
1683 return size;
1684}
1685
1686// call with lock held
1687status_t MediaPlayerService::AudioCache::wait()
1688{
1689 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001690 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001691 mSignal.wait(mLock);
1692 }
1693 mCommandComplete = false;
1694
1695 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001696 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001697 } else {
Steve Block3856b092011-10-20 11:56:00 +01001698 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001699 }
1700 return mError;
1701}
1702
Gloria Wangb483c472011-04-11 17:23:27 -07001703void MediaPlayerService::AudioCache::notify(
1704 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001705{
Steve Block3856b092011-10-20 11:56:00 +01001706 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001707 AudioCache* p = static_cast<AudioCache*>(cookie);
1708
1709 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001710 switch (msg)
1711 {
1712 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001713 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001714 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001715 break;
1716 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001717 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001718 break;
1719 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001720 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001721 break;
1722 default:
Steve Block3856b092011-10-20 11:56:00 +01001723 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001724 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001725 }
1726
1727 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001728 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001729 p->mCommandComplete = true;
1730 p->mSignal.signal();
1731}
1732
Eric Laurent8c563ed2010-10-07 18:23:03 -07001733int MediaPlayerService::AudioCache::getSessionId()
1734{
1735 return 0;
1736}
1737
Gloria Wang7cf180c2011-02-19 18:37:57 -08001738void MediaPlayerService::addBatteryData(uint32_t params)
1739{
1740 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001741
1742 int32_t time = systemTime() / 1000000L;
1743
1744 // change audio output devices. This notification comes from AudioFlinger
1745 if ((params & kBatteryDataSpeakerOn)
1746 || (params & kBatteryDataOtherAudioDeviceOn)) {
1747
1748 int deviceOn[NUM_AUDIO_DEVICES];
1749 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1750 deviceOn[i] = 0;
1751 }
1752
1753 if ((params & kBatteryDataSpeakerOn)
1754 && (params & kBatteryDataOtherAudioDeviceOn)) {
1755 deviceOn[SPEAKER_AND_OTHER] = 1;
1756 } else if (params & kBatteryDataSpeakerOn) {
1757 deviceOn[SPEAKER] = 1;
1758 } else {
1759 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1760 }
1761
1762 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1763 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1764
1765 if (mBatteryAudio.refCount > 0) { // if playing audio
1766 if (!deviceOn[i]) {
1767 mBatteryAudio.lastTime[i] += time;
1768 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1769 mBatteryAudio.lastTime[i] = 0;
1770 } else {
1771 mBatteryAudio.lastTime[i] = 0 - time;
1772 }
1773 }
1774
1775 mBatteryAudio.deviceOn[i] = deviceOn[i];
1776 }
1777 }
1778 return;
1779 }
1780
1781 // an sudio stream is started
1782 if (params & kBatteryDataAudioFlingerStart) {
1783 // record the start time only if currently no other audio
1784 // is being played
1785 if (mBatteryAudio.refCount == 0) {
1786 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1787 if (mBatteryAudio.deviceOn[i]) {
1788 mBatteryAudio.lastTime[i] -= time;
1789 }
1790 }
1791 }
1792
1793 mBatteryAudio.refCount ++;
1794 return;
1795
1796 } else if (params & kBatteryDataAudioFlingerStop) {
1797 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001798 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001799 return;
1800 }
1801
1802 // record the stop time only if currently this is the only
1803 // audio being played
1804 if (mBatteryAudio.refCount == 1) {
1805 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1806 if (mBatteryAudio.deviceOn[i]) {
1807 mBatteryAudio.lastTime[i] += time;
1808 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1809 mBatteryAudio.lastTime[i] = 0;
1810 }
1811 }
1812 }
1813
1814 mBatteryAudio.refCount --;
1815 return;
1816 }
1817
Gloria Wang7cf180c2011-02-19 18:37:57 -08001818 int uid = IPCThreadState::self()->getCallingUid();
1819 if (uid == AID_MEDIA) {
1820 return;
1821 }
1822 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001823
1824 if (index < 0) { // create a new entry for this UID
1825 BatteryUsageInfo info;
1826 info.audioTotalTime = 0;
1827 info.videoTotalTime = 0;
1828 info.audioLastTime = 0;
1829 info.videoLastTime = 0;
1830 info.refCount = 0;
1831
Gloria Wang9ee159b2011-02-24 14:51:45 -08001832 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001833 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001834 return;
1835 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001836 }
1837
1838 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1839
1840 if (params & kBatteryDataCodecStarted) {
1841 if (params & kBatteryDataTrackAudio) {
1842 info.audioLastTime -= time;
1843 info.refCount ++;
1844 }
1845 if (params & kBatteryDataTrackVideo) {
1846 info.videoLastTime -= time;
1847 info.refCount ++;
1848 }
1849 } else {
1850 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001851 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001852 return;
1853 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001854 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001855 mBatteryData.removeItem(uid);
1856 return;
1857 }
1858
1859 if (params & kBatteryDataTrackAudio) {
1860 info.audioLastTime += time;
1861 info.refCount --;
1862 }
1863 if (params & kBatteryDataTrackVideo) {
1864 info.videoLastTime += time;
1865 info.refCount --;
1866 }
1867
1868 // no stream is being played by this UID
1869 if (info.refCount == 0) {
1870 info.audioTotalTime += info.audioLastTime;
1871 info.audioLastTime = 0;
1872 info.videoTotalTime += info.videoLastTime;
1873 info.videoLastTime = 0;
1874 }
1875 }
1876}
1877
1878status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
1879 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001880
1881 // audio output devices usage
1882 int32_t time = systemTime() / 1000000L; //in ms
1883 int32_t totalTime;
1884
1885 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1886 totalTime = mBatteryAudio.totalTime[i];
1887
1888 if (mBatteryAudio.deviceOn[i]
1889 && (mBatteryAudio.lastTime[i] != 0)) {
1890 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
1891 totalTime += tmpTime;
1892 }
1893
1894 reply->writeInt32(totalTime);
1895 // reset the total time
1896 mBatteryAudio.totalTime[i] = 0;
1897 }
1898
1899 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08001900 BatteryUsageInfo info;
1901 int size = mBatteryData.size();
1902
1903 reply->writeInt32(size);
1904 int i = 0;
1905
1906 while (i < size) {
1907 info = mBatteryData.valueAt(i);
1908
1909 reply->writeInt32(mBatteryData.keyAt(i)); //UID
1910 reply->writeInt32(info.audioTotalTime);
1911 reply->writeInt32(info.videoTotalTime);
1912
1913 info.audioTotalTime = 0;
1914 info.videoTotalTime = 0;
1915
1916 // remove the UID entry where no stream is being played
1917 if (info.refCount <= 0) {
1918 mBatteryData.removeItemsAt(i);
1919 size --;
1920 i --;
1921 }
1922 i++;
1923 }
1924 return NO_ERROR;
1925}
nikoa64c8c72009-07-20 15:07:26 -07001926} // namespace android