blob: 764eddc840a823abfb829ee3b14ebe3254ed1c33 [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
John Grossman761defc2012-02-09 15:09:05 -080073namespace android {
74sp<MediaPlayerBase> createAAH_TXPlayer();
75sp<MediaPlayerBase> createAAH_RXPlayer();
76}
77
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070078namespace {
nikoa64c8c72009-07-20 15:07:26 -070079using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070080using android::status_t;
81using android::OK;
82using android::BAD_VALUE;
83using android::NOT_ENOUGH_DATA;
84using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070085
86// Max number of entries in the filter.
87const int kMaxFilterSize = 64; // I pulled that out of thin air.
88
nikoa64c8c72009-07-20 15:07:26 -070089// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070090
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070091
92// Unmarshall a filter from a Parcel.
93// Filter format in a parcel:
94//
95// 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
96// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97// | number of entries (n) |
98// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99// | metadata type 1 |
100// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101// | metadata type 2 |
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103// ....
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | metadata type n |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107//
108// @param p Parcel that should start with a filter.
109// @param[out] filter On exit contains the list of metadata type to be
110// filtered.
111// @param[out] status On exit contains the status code to be returned.
112// @return true if the parcel starts with a valid filter.
113bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700114 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700115 status_t *status)
116{
Nicolas Catania48290382009-07-10 13:53:06 -0700117 int32_t val;
118 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700119 {
Steve Block29357bc2012-01-06 19:20:56 +0000120 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700121 *status = NOT_ENOUGH_DATA;
122 return false;
123 }
124
Nicolas Catania48290382009-07-10 13:53:06 -0700125 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 {
Steve Block29357bc2012-01-06 19:20:56 +0000127 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 *status = BAD_VALUE;
129 return false;
130 }
131
Nicolas Catania48290382009-07-10 13:53:06 -0700132 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133
134 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700135 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136
nikoa64c8c72009-07-20 15:07:26 -0700137 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138
Nicolas Catania48290382009-07-10 13:53:06 -0700139
140 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141 {
Steve Block29357bc2012-01-06 19:20:56 +0000142 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143 *status = NOT_ENOUGH_DATA;
144 return false;
145 }
146
nikoa64c8c72009-07-20 15:07:26 -0700147 const Metadata::Type *data =
148 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149
Nicolas Catania48290382009-07-10 13:53:06 -0700150 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 {
Steve Block29357bc2012-01-06 19:20:56 +0000152 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700153 *status = BAD_VALUE;
154 return false;
155 }
156
157 // TODO: The stl impl of vector would be more efficient here
158 // because it degenerates into a memcpy on pod types. Try to
159 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700160 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 {
Nicolas Catania48290382009-07-10 13:53:06 -0700162 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700163 ++data;
164 }
165 *status = OK;
166 return true;
167}
168
Nicolas Catania48290382009-07-10 13:53:06 -0700169// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170// @param val To be searched.
171// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700172bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700173{
174 // Deal with empty and ANY right away
175 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700176 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700177
Nicolas Catania48290382009-07-10 13:53:06 -0700178 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700179}
180
181} // anonymous namespace
182
183
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184namespace android {
185
Dave Burked681bbb2011-08-30 14:39:17 +0100186static bool checkPermission(const char* permissionString) {
187#ifndef HAVE_ANDROID_OS
188 return true;
189#endif
190 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
191 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000192 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100193 return ok;
194}
195
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196// TODO: Temp hack until we can register players
197typedef struct {
198 const char *extension;
199 const player_type playertype;
200} extmap;
201extmap FILE_EXTS [] = {
202 {".mid", SONIVOX_PLAYER},
203 {".midi", SONIVOX_PLAYER},
204 {".smf", SONIVOX_PLAYER},
205 {".xmf", SONIVOX_PLAYER},
206 {".imy", SONIVOX_PLAYER},
207 {".rtttl", SONIVOX_PLAYER},
208 {".rtx", SONIVOX_PLAYER},
209 {".ota", SONIVOX_PLAYER},
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210};
211
212// 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 -0800213/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
214/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
215
216void MediaPlayerService::instantiate() {
217 defaultServiceManager()->addService(
218 String16("media.player"), new MediaPlayerService());
219}
220
221MediaPlayerService::MediaPlayerService()
222{
Steve Block3856b092011-10-20 11:56:00 +0100223 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800225
226 mBatteryAudio.refCount = 0;
227 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
228 mBatteryAudio.deviceOn[i] = 0;
229 mBatteryAudio.lastTime[i] = 0;
230 mBatteryAudio.totalTime[i] = 0;
231 }
232 // speaker is on by default
233 mBatteryAudio.deviceOn[SPEAKER] = 1;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800234}
235
236MediaPlayerService::~MediaPlayerService()
237{
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800239}
240
241sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
242{
Gloria Wangdac6a312009-10-29 15:46:37 -0700243 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
244 wp<MediaRecorderClient> w = recorder;
245 Mutex::Autolock lock(mLock);
246 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100247 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 return recorder;
249}
250
Gloria Wangdac6a312009-10-29 15:46:37 -0700251void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
252{
253 Mutex::Autolock lock(mLock);
254 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100255 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700256}
257
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800258sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
259{
260 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100261 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 return retriever;
263}
264
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800265sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100266 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800267{
268 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700269
270 sp<Client> c = new Client(
271 this, pid, connId, client, audioSessionId,
272 IPCThreadState::self()->getCallingUid());
273
Steve Block3856b092011-10-20 11:56:00 +0100274 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100275 IPCThreadState::self()->getCallingUid());
276
277 wp<Client> w = c;
278 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800279 Mutex::Autolock lock(mLock);
280 mClients.add(w);
281 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800282 return c;
283}
284
Andreas Huber318ad9c2009-10-15 13:46:54 -0700285sp<IOMX> MediaPlayerService::getOMX() {
286 Mutex::Autolock autoLock(mLock);
287
288 if (mOMX.get() == NULL) {
289 mOMX = new OMX;
290 }
291
292 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700293}
294
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
296{
297 const size_t SIZE = 256;
298 char buffer[SIZE];
299 String8 result;
300
301 result.append(" AudioCache\n");
302 if (mHeap != 0) {
303 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
304 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
305 result.append(buffer);
306 }
307 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
308 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
309 result.append(buffer);
310 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
311 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
312 result.append(buffer);
313 ::write(fd, result.string(), result.size());
314 return NO_ERROR;
315}
316
317status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
318{
319 const size_t SIZE = 256;
320 char buffer[SIZE];
321 String8 result;
322
323 result.append(" AudioOutput\n");
324 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
325 mStreamType, mLeftVolume, mRightVolume);
326 result.append(buffer);
327 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
328 mMsecsPerFrame, mLatency);
329 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700330 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
331 mAuxEffectId, mSendLevel);
332 result.append(buffer);
333
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800334 ::write(fd, result.string(), result.size());
335 if (mTrack != 0) {
336 mTrack->dump(fd, args);
337 }
338 return NO_ERROR;
339}
340
341status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
342{
343 const size_t SIZE = 256;
344 char buffer[SIZE];
345 String8 result;
346 result.append(" Client\n");
347 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
348 mPid, mConnId, mStatus, mLoop?"true": "false");
349 result.append(buffer);
350 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700351 if (mPlayer != NULL) {
352 mPlayer->dump(fd, args);
353 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800354 if (mAudioOutput != 0) {
355 mAudioOutput->dump(fd, args);
356 }
357 write(fd, "\n", 1);
358 return NO_ERROR;
359}
360
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800361status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
362{
363 const size_t SIZE = 256;
364 char buffer[SIZE];
365 String8 result;
366 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
367 snprintf(buffer, SIZE, "Permission Denial: "
368 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
369 IPCThreadState::self()->getCallingPid(),
370 IPCThreadState::self()->getCallingUid());
371 result.append(buffer);
372 } else {
373 Mutex::Autolock lock(mLock);
374 for (int i = 0, n = mClients.size(); i < n; ++i) {
375 sp<Client> c = mClients[i].promote();
376 if (c != 0) c->dump(fd, args);
377 }
James Dongb9141222010-07-08 11:16:11 -0700378 if (mMediaRecorderClients.size() == 0) {
379 result.append(" No media recorder client\n\n");
380 } else {
381 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
382 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700383 if (c != 0) {
384 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
385 result.append(buffer);
386 write(fd, result.string(), result.size());
387 result = "\n";
388 c->dump(fd, args);
389 }
James Dongb9141222010-07-08 11:16:11 -0700390 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700391 }
392
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800393 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700394 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395 FILE *f = fopen(buffer, "r");
396 if (f) {
397 while (!feof(f)) {
398 fgets(buffer, SIZE, f);
Dave Sparks02fa8342010-09-27 16:55:18 -0700399 if (strstr(buffer, " /mnt/sdcard/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800400 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700401 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800402 strstr(buffer, " /system/media/")) {
403 result.append(" ");
404 result.append(buffer);
405 }
406 }
407 fclose(f);
408 } else {
409 result.append("couldn't open ");
410 result.append(buffer);
411 result.append("\n");
412 }
413
Glenn Kasten0512ab52011-05-04 17:58:57 -0700414 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415 DIR *d = opendir(buffer);
416 if (d) {
417 struct dirent *ent;
418 while((ent = readdir(d)) != NULL) {
419 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700420 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 struct stat s;
422 if (lstat(buffer, &s) == 0) {
423 if ((s.st_mode & S_IFMT) == S_IFLNK) {
424 char linkto[256];
425 int len = readlink(buffer, linkto, sizeof(linkto));
426 if(len > 0) {
427 if(len > 255) {
428 linkto[252] = '.';
429 linkto[253] = '.';
430 linkto[254] = '.';
431 linkto[255] = 0;
432 } else {
433 linkto[len] = 0;
434 }
Dave Sparks02fa8342010-09-27 16:55:18 -0700435 if (strstr(linkto, "/mnt/sdcard/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700437 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 strstr(linkto, "/system/media/") == linkto) {
439 result.append(" ");
440 result.append(buffer);
441 result.append(" -> ");
442 result.append(linkto);
443 result.append("\n");
444 }
445 }
446 } else {
447 result.append(" unexpected type for ");
448 result.append(buffer);
449 result.append("\n");
450 }
451 }
452 }
453 }
454 closedir(d);
455 } else {
456 result.append("couldn't open ");
457 result.append(buffer);
458 result.append("\n");
459 }
460
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 bool dumpMem = false;
462 for (size_t i = 0; i < args.size(); i++) {
463 if (args[i] == String16("-m")) {
464 dumpMem = true;
465 }
466 }
467 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700468 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800469 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800470 }
471 write(fd, result.string(), result.size());
472 return NO_ERROR;
473}
474
475void MediaPlayerService::removeClient(wp<Client> client)
476{
477 Mutex::Autolock lock(mLock);
478 mClients.remove(client);
479}
480
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700481MediaPlayerService::Client::Client(
482 const sp<MediaPlayerService>& service, pid_t pid,
483 int32_t connId, const sp<IMediaPlayerClient>& client,
484 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485{
Steve Block3856b092011-10-20 11:56:00 +0100486 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 mPid = pid;
488 mConnId = connId;
489 mService = service;
490 mClient = client;
491 mLoop = false;
492 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700493 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700494 mUID = uid;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700495
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000497 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800498 mAntagonizer = new Antagonizer(notify, this);
499#endif
500}
501
502MediaPlayerService::Client::~Client()
503{
Steve Block3856b092011-10-20 11:56:00 +0100504 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505 mAudioOutput.clear();
506 wp<Client> client(this);
507 disconnect();
508 mService->removeClient(client);
509}
510
511void MediaPlayerService::Client::disconnect()
512{
Steve Block3856b092011-10-20 11:56:00 +0100513 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 // grab local reference and clear main reference to prevent future
515 // access to object
516 sp<MediaPlayerBase> p;
517 {
518 Mutex::Autolock l(mLock);
519 p = mPlayer;
520 }
Dave Sparks795fa582009-03-24 17:57:12 -0700521 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700522
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523 mPlayer.clear();
524
525 // clear the notification to prevent callbacks to dead client
526 // and reset the player. We assume the player will serialize
527 // access to itself if necessary.
528 if (p != 0) {
529 p->setNotifyCallback(0, 0);
530#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000531 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 mAntagonizer->kill();
533#endif
534 p->reset();
535 }
536
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700537 disconnectNativeWindow();
538
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 IPCThreadState::self()->flushCommands();
540}
541
Andreas Huber47f59cf2009-08-07 09:30:32 -0700542static player_type getDefaultPlayerType() {
Andreas Huber608d77b2010-06-23 16:40:57 -0700543 return STAGEFRIGHT_PLAYER;
Andreas Huber47f59cf2009-08-07 09:30:32 -0700544}
545
James Dong148c1a22009-09-06 14:29:45 -0700546player_type getPlayerType(int fd, int64_t offset, int64_t length)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547{
548 char buf[20];
549 lseek(fd, offset, SEEK_SET);
550 read(fd, buf, sizeof(buf));
551 lseek(fd, offset, SEEK_SET);
552
553 long ident = *((long*)buf);
554
555 // Ogg vorbis?
556 if (ident == 0x5367674f) // 'OggS'
Andreas Huber608d77b2010-06-23 16:40:57 -0700557 return STAGEFRIGHT_PLAYER;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558
559 // Some kind of MIDI?
560 EAS_DATA_HANDLE easdata;
561 if (EAS_Init(&easdata) == EAS_SUCCESS) {
562 EAS_FILE locator;
563 locator.path = NULL;
564 locator.fd = fd;
565 locator.offset = offset;
566 locator.length = length;
567 EAS_HANDLE eashandle;
568 if (EAS_OpenFile(easdata, &locator, &eashandle) == EAS_SUCCESS) {
569 EAS_CloseFile(easdata, eashandle);
570 EAS_Shutdown(easdata);
571 return SONIVOX_PLAYER;
572 }
573 EAS_Shutdown(easdata);
574 }
575
Andreas Huber47f59cf2009-08-07 09:30:32 -0700576 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800577}
578
James Dong148c1a22009-09-06 14:29:45 -0700579player_type getPlayerType(const char* url)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800580{
Nicolas Catania14d27472009-07-13 14:37:49 -0700581 if (TestPlayerStub::canBeUsed(url)) {
582 return TEST_PLAYER;
583 }
584
Andreas Huber0a73d812011-03-21 16:28:04 -0700585 if (!strncasecmp("http://", url, 7)
586 || !strncasecmp("https://", url, 8)) {
Andreas Hubered8d14f2011-02-16 09:05:38 -0800587 size_t len = strlen(url);
588 if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
589 return NU_PLAYER;
590 }
Andreas Huber5bc087c2010-12-23 10:27:40 -0800591
Andreas Hubered8d14f2011-02-16 09:05:38 -0800592 if (strstr(url,"m3u8")) {
593 return NU_PLAYER;
Andreas Huber5bc087c2010-12-23 10:27:40 -0800594 }
595 }
596
Andreas Huber2bfdd422011-10-11 15:24:07 -0700597 if (!strncasecmp("rtsp://", url, 7)) {
598 return NU_PLAYER;
599 }
600
John Grossman761defc2012-02-09 15:09:05 -0800601 if (!strncasecmp("aahRX://", url, 8)) {
602 return AAH_RX_PLAYER;
603 }
604
605 if (!strncasecmp("aahTX://", url, 8)) {
606 return AAH_TX_PLAYER;
607 }
608
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609 // use MidiFile for MIDI extensions
610 int lenURL = strlen(url);
611 for (int i = 0; i < NELEM(FILE_EXTS); ++i) {
612 int len = strlen(FILE_EXTS[i].extension);
613 int start = lenURL - len;
614 if (start > 0) {
Atsushi Enofc1c7b92010-03-19 23:18:02 +0900615 if (!strncasecmp(url + start, FILE_EXTS[i].extension, len)) {
Andreas Huber608d77b2010-06-23 16:40:57 -0700616 return FILE_EXTS[i].playertype;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617 }
618 }
619 }
620
Andreas Huber47f59cf2009-08-07 09:30:32 -0700621 return getDefaultPlayerType();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800622}
623
624static sp<MediaPlayerBase> createPlayer(player_type playerType, void* cookie,
625 notify_callback_f notifyFunc)
626{
627 sp<MediaPlayerBase> p;
628 switch (playerType) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800629 case SONIVOX_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100630 ALOGV(" create MidiFile");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 p = new MidiFile();
632 break;
Andreas Huber20111aa2009-07-14 16:56:47 -0700633 case STAGEFRIGHT_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100634 ALOGV(" create StagefrightPlayer");
Andreas Huber20111aa2009-07-14 16:56:47 -0700635 p = new StagefrightPlayer;
636 break;
Andreas Huberf9334412010-12-15 15:17:42 -0800637 case NU_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100638 ALOGV(" create NuPlayer");
Andreas Huberf9334412010-12-15 15:17:42 -0800639 p = new NuPlayerDriver;
640 break;
Nicolas Catania14d27472009-07-13 14:37:49 -0700641 case TEST_PLAYER:
Steve Block3856b092011-10-20 11:56:00 +0100642 ALOGV("Create Test Player stub");
Nicolas Catania14d27472009-07-13 14:37:49 -0700643 p = new TestPlayerStub();
644 break;
John Grossman761defc2012-02-09 15:09:05 -0800645 case AAH_RX_PLAYER:
646 ALOGV(" create A@H RX Player");
647 p = createAAH_RXPlayer();
648 break;
649 case AAH_TX_PLAYER:
650 ALOGV(" create A@H TX Player");
651 p = createAAH_TXPlayer();
652 break;
James Dong30d713a2010-11-10 18:42:40 -0800653 default:
Steve Block29357bc2012-01-06 19:20:56 +0000654 ALOGE("Unknown player type: %d", playerType);
James Dong30d713a2010-11-10 18:42:40 -0800655 return NULL;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 }
657 if (p != NULL) {
658 if (p->initCheck() == NO_ERROR) {
659 p->setNotifyCallback(cookie, notifyFunc);
660 } else {
661 p.clear();
662 }
663 }
664 if (p == NULL) {
Steve Block29357bc2012-01-06 19:20:56 +0000665 ALOGE("Failed to create player object");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666 }
667 return p;
668}
669
670sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
671{
672 // determine if we have the right player type
673 sp<MediaPlayerBase> p = mPlayer;
674 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100675 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 p.clear();
677 }
678 if (p == NULL) {
679 p = android::createPlayer(playerType, this, notify);
680 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700681
Jason Simmonsdb29e522011-08-12 13:46:55 -0700682 if (p != NULL) {
683 p->setUID(mUID);
684 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700685
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800686 return p;
687}
688
Andreas Huber2db84552010-01-28 11:19:57 -0800689status_t MediaPlayerService::Client::setDataSource(
690 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800691{
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 if (url == NULL)
694 return UNKNOWN_ERROR;
695
Dave Burked681bbb2011-08-30 14:39:17 +0100696 if ((strncmp(url, "http://", 7) == 0) ||
697 (strncmp(url, "https://", 8) == 0) ||
698 (strncmp(url, "rtsp://", 7) == 0)) {
699 if (!checkPermission("android.permission.INTERNET")) {
700 return PERMISSION_DENIED;
701 }
702 }
703
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704 if (strncmp(url, "content://", 10) == 0) {
705 // get a filedescriptor for the content Uri and
706 // pass it to the setDataSource(fd) method
707
708 String16 url16(url);
709 int fd = android::openContentProviderFile(url16);
710 if (fd < 0)
711 {
Steve Block29357bc2012-01-06 19:20:56 +0000712 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800713 return UNKNOWN_ERROR;
714 }
715 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
716 close(fd);
717 return mStatus;
718 } else {
719 player_type playerType = getPlayerType(url);
Steve Block3856b092011-10-20 11:56:00 +0100720 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800721
722 // create the right type of player
723 sp<MediaPlayerBase> p = createPlayer(playerType);
724 if (p == NULL) return NO_INIT;
725
726 if (!p->hardwareOutput()) {
Eric Laurenta514bdb2010-06-21 09:27:30 -0700727 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800728 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
729 }
730
731 // now set data source
Steve Block3856b092011-10-20 11:56:00 +0100732 ALOGV(" setDataSource");
Andreas Huber2db84552010-01-28 11:19:57 -0800733 mStatus = p->setDataSource(url, headers);
Nicolas Catania14d27472009-07-13 14:37:49 -0700734 if (mStatus == NO_ERROR) {
735 mPlayer = p;
736 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000737 ALOGE(" error: %d", mStatus);
Nicolas Catania14d27472009-07-13 14:37:49 -0700738 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800739 return mStatus;
740 }
741}
742
743status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
744{
Steve Block3856b092011-10-20 11:56:00 +0100745 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800746 struct stat sb;
747 int ret = fstat(fd, &sb);
748 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000749 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800750 return UNKNOWN_ERROR;
751 }
752
Steve Block3856b092011-10-20 11:56:00 +0100753 ALOGV("st_dev = %llu", sb.st_dev);
754 ALOGV("st_mode = %u", sb.st_mode);
755 ALOGV("st_uid = %lu", sb.st_uid);
756 ALOGV("st_gid = %lu", sb.st_gid);
757 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800758
759 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000760 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800761 ::close(fd);
762 return UNKNOWN_ERROR;
763 }
764 if (offset + length > sb.st_size) {
765 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100766 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800767 }
768
769 player_type playerType = getPlayerType(fd, offset, length);
Steve Block3856b092011-10-20 11:56:00 +0100770 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800771
772 // create the right type of player
773 sp<MediaPlayerBase> p = createPlayer(playerType);
774 if (p == NULL) return NO_INIT;
775
776 if (!p->hardwareOutput()) {
Eric Laurenta514bdb2010-06-21 09:27:30 -0700777 mAudioOutput = new AudioOutput(mAudioSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
779 }
780
781 // now set data source
782 mStatus = p->setDataSource(fd, offset, length);
783 if (mStatus == NO_ERROR) mPlayer = p;
Dave Burked681bbb2011-08-30 14:39:17 +0100784
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800785 return mStatus;
786}
787
Andreas Hubere2b10282010-11-23 11:41:34 -0800788status_t MediaPlayerService::Client::setDataSource(
789 const sp<IStreamSource> &source) {
790 // create the right type of player
Andreas Huberf9334412010-12-15 15:17:42 -0800791 sp<MediaPlayerBase> p = createPlayer(NU_PLAYER);
Andreas Hubere2b10282010-11-23 11:41:34 -0800792
793 if (p == NULL) {
794 return NO_INIT;
795 }
796
797 if (!p->hardwareOutput()) {
798 mAudioOutput = new AudioOutput(mAudioSessionId);
799 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
800 }
801
802 // now set data source
803 mStatus = p->setDataSource(source);
804
805 if (mStatus == OK) {
806 mPlayer = p;
807 }
808
809 return mStatus;
810}
811
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700812void MediaPlayerService::Client::disconnectNativeWindow() {
813 if (mConnectedWindow != NULL) {
814 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
815 NATIVE_WINDOW_API_MEDIA);
816
817 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000818 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700819 strerror(-err), err);
820 }
821 }
822 mConnectedWindow.clear();
823}
824
Glenn Kasten11731182011-02-08 17:26:17 -0800825status_t MediaPlayerService::Client::setVideoSurfaceTexture(
826 const sp<ISurfaceTexture>& surfaceTexture)
827{
Steve Block3856b092011-10-20 11:56:00 +0100828 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800829 sp<MediaPlayerBase> p = getPlayer();
830 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700831
832 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
833 surfaceTexture->asBinder());
834 if (mConnectedWindowBinder == binder) {
835 return OK;
836 }
837
838 sp<ANativeWindow> anw;
839 if (surfaceTexture != NULL) {
840 anw = new SurfaceTextureClient(surfaceTexture);
841 status_t err = native_window_api_connect(anw.get(),
842 NATIVE_WINDOW_API_MEDIA);
843
844 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000845 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700846 // Note that we must do the reset before disconnecting from the ANW.
847 // Otherwise queue/dequeue calls could be made on the disconnected
848 // ANW, which may result in errors.
849 reset();
850
851 disconnectNativeWindow();
852
853 return err;
854 }
855 }
856
857 // Note that we must set the player's new SurfaceTexture before
858 // disconnecting the old one. Otherwise queue/dequeue calls could be made
859 // on the disconnected ANW, which may result in errors.
860 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
861
862 disconnectNativeWindow();
863
864 mConnectedWindow = anw;
865
866 if (err == OK) {
867 mConnectedWindowBinder = binder;
868 } else {
869 disconnectNativeWindow();
870 }
871
872 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800873}
874
Nicolas Catania1d187f12009-05-12 23:25:55 -0700875status_t MediaPlayerService::Client::invoke(const Parcel& request,
876 Parcel *reply)
877{
878 sp<MediaPlayerBase> p = getPlayer();
879 if (p == NULL) return UNKNOWN_ERROR;
880 return p->invoke(request, reply);
881}
882
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700883// This call doesn't need to access the native player.
884status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
885{
886 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700887 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700888
Nicolas Catania48290382009-07-10 13:53:06 -0700889 if (unmarshallFilter(filter, &allow, &status) &&
890 unmarshallFilter(filter, &drop, &status)) {
891 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700892
893 mMetadataAllow = allow;
894 mMetadataDrop = drop;
895 }
896 return status;
897}
898
Nicolas Catania48290382009-07-10 13:53:06 -0700899status_t MediaPlayerService::Client::getMetadata(
900 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700901{
nikoa64c8c72009-07-20 15:07:26 -0700902 sp<MediaPlayerBase> player = getPlayer();
903 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700904
nikod608a812009-07-16 16:39:53 -0700905 status_t status;
906 // Placeholder for the return code, updated by the caller.
907 reply->writeInt32(-1);
908
nikoa64c8c72009-07-20 15:07:26 -0700909 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700910
911 // We don't block notifications while we fetch the data. We clear
912 // mMetadataUpdated first so we don't lose notifications happening
913 // during the rest of this call.
914 {
915 Mutex::Autolock lock(mLock);
916 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700917 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700918 }
919 mMetadataUpdated.clear();
920 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700921
nikoa64c8c72009-07-20 15:07:26 -0700922 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700923
nikoa64c8c72009-07-20 15:07:26 -0700924 metadata.appendHeader();
925 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700926
927 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700928 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000929 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700930 return status;
931 }
932
933 // FIXME: Implement filtering on the result. Not critical since
934 // filtering takes place on the update notifications already. This
935 // would be when all the metadata are fetch and a filter is set.
936
nikod608a812009-07-16 16:39:53 -0700937 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700938 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700939 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700940}
941
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942status_t MediaPlayerService::Client::prepareAsync()
943{
Steve Block3856b092011-10-20 11:56:00 +0100944 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 sp<MediaPlayerBase> p = getPlayer();
946 if (p == 0) return UNKNOWN_ERROR;
947 status_t ret = p->prepareAsync();
948#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000949 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800950 if (ret == NO_ERROR) mAntagonizer->start();
951#endif
952 return ret;
953}
954
955status_t MediaPlayerService::Client::start()
956{
Steve Block3856b092011-10-20 11:56:00 +0100957 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800958 sp<MediaPlayerBase> p = getPlayer();
959 if (p == 0) return UNKNOWN_ERROR;
960 p->setLooping(mLoop);
961 return p->start();
962}
963
964status_t MediaPlayerService::Client::stop()
965{
Steve Block3856b092011-10-20 11:56:00 +0100966 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800967 sp<MediaPlayerBase> p = getPlayer();
968 if (p == 0) return UNKNOWN_ERROR;
969 return p->stop();
970}
971
972status_t MediaPlayerService::Client::pause()
973{
Steve Block3856b092011-10-20 11:56:00 +0100974 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800975 sp<MediaPlayerBase> p = getPlayer();
976 if (p == 0) return UNKNOWN_ERROR;
977 return p->pause();
978}
979
980status_t MediaPlayerService::Client::isPlaying(bool* state)
981{
982 *state = false;
983 sp<MediaPlayerBase> p = getPlayer();
984 if (p == 0) return UNKNOWN_ERROR;
985 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800987 return NO_ERROR;
988}
989
990status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
991{
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800993 sp<MediaPlayerBase> p = getPlayer();
994 if (p == 0) return UNKNOWN_ERROR;
995 status_t ret = p->getCurrentPosition(msec);
996 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100997 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800998 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000999 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001000 }
1001 return ret;
1002}
1003
1004status_t MediaPlayerService::Client::getDuration(int *msec)
1005{
Steve Block3856b092011-10-20 11:56:00 +01001006 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001007 sp<MediaPlayerBase> p = getPlayer();
1008 if (p == 0) return UNKNOWN_ERROR;
1009 status_t ret = p->getDuration(msec);
1010 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001011 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001012 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001013 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001014 }
1015 return ret;
1016}
1017
1018status_t MediaPlayerService::Client::seekTo(int msec)
1019{
Steve Block3856b092011-10-20 11:56:00 +01001020 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 sp<MediaPlayerBase> p = getPlayer();
1022 if (p == 0) return UNKNOWN_ERROR;
1023 return p->seekTo(msec);
1024}
1025
1026status_t MediaPlayerService::Client::reset()
1027{
Steve Block3856b092011-10-20 11:56:00 +01001028 ALOGV("[%d] reset", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001029 sp<MediaPlayerBase> p = getPlayer();
1030 if (p == 0) return UNKNOWN_ERROR;
1031 return p->reset();
1032}
1033
Glenn Kastenfff6d712012-01-12 16:38:12 -08001034status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001035{
Steve Block3856b092011-10-20 11:56:00 +01001036 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001037 // TODO: for hardware output, call player instead
1038 Mutex::Autolock l(mLock);
1039 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1040 return NO_ERROR;
1041}
1042
1043status_t MediaPlayerService::Client::setLooping(int loop)
1044{
Steve Block3856b092011-10-20 11:56:00 +01001045 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001046 mLoop = loop;
1047 sp<MediaPlayerBase> p = getPlayer();
1048 if (p != 0) return p->setLooping(loop);
1049 return NO_ERROR;
1050}
1051
1052status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1053{
Steve Block3856b092011-10-20 11:56:00 +01001054 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001055
1056 // for hardware output, call player instead
1057 sp<MediaPlayerBase> p = getPlayer();
1058 {
1059 Mutex::Autolock l(mLock);
1060 if (p != 0 && p->hardwareOutput()) {
1061 MediaPlayerHWInterface* hwp =
1062 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1063 return hwp->setVolume(leftVolume, rightVolume);
1064 } else {
1065 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1066 return NO_ERROR;
1067 }
1068 }
1069
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001070 return NO_ERROR;
1071}
1072
Eric Laurent2beeb502010-07-16 07:43:46 -07001073status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1074{
Steve Block3856b092011-10-20 11:56:00 +01001075 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001076 Mutex::Autolock l(mLock);
1077 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1078 return NO_ERROR;
1079}
1080
1081status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1082{
Steve Block3856b092011-10-20 11:56:00 +01001083 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001084 Mutex::Autolock l(mLock);
1085 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1086 return NO_ERROR;
1087}
Nicolas Catania48290382009-07-10 13:53:06 -07001088
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001089status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001090 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001091 sp<MediaPlayerBase> p = getPlayer();
1092 if (p == 0) return UNKNOWN_ERROR;
1093 return p->setParameter(key, request);
1094}
1095
1096status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001097 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001098 sp<MediaPlayerBase> p = getPlayer();
1099 if (p == 0) return UNKNOWN_ERROR;
1100 return p->getParameter(key, reply);
1101}
1102
Gloria Wangb483c472011-04-11 17:23:27 -07001103void MediaPlayerService::Client::notify(
1104 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001105{
1106 Client* client = static_cast<Client*>(cookie);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001107
1108 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001109 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001110 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001111
1112 if(client->shouldDropMetadata(metadata_type)) {
1113 return;
1114 }
1115
1116 // Update the list of metadata that have changed. getMetadata
1117 // also access mMetadataUpdated and clears it.
1118 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001119 }
Steve Block3856b092011-10-20 11:56:00 +01001120 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
Gloria Wangb483c472011-04-11 17:23:27 -07001121 client->mClient->notify(msg, ext1, ext2, obj);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001122}
1123
Nicolas Catania48290382009-07-10 13:53:06 -07001124
nikoa64c8c72009-07-20 15:07:26 -07001125bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001126{
Nicolas Catania48290382009-07-10 13:53:06 -07001127 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001128
Nicolas Catania48290382009-07-10 13:53:06 -07001129 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001130 return true;
1131 }
1132
Nicolas Catania48290382009-07-10 13:53:06 -07001133 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001134 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001135 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001136 return true;
1137 }
1138}
1139
Nicolas Catania48290382009-07-10 13:53:06 -07001140
nikoa64c8c72009-07-20 15:07:26 -07001141void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001142 Mutex::Autolock lock(mLock);
1143 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1144 mMetadataUpdated.add(metadata_type);
1145 }
1146}
1147
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001148#if CALLBACK_ANTAGONIZER
1149const int Antagonizer::interval = 10000; // 10 msecs
1150
1151Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1152 mExit(false), mActive(false), mClient(client), mCb(cb)
1153{
1154 createThread(callbackThread, this);
1155}
1156
1157void Antagonizer::kill()
1158{
1159 Mutex::Autolock _l(mLock);
1160 mActive = false;
1161 mExit = true;
1162 mCondition.wait(mLock);
1163}
1164
1165int Antagonizer::callbackThread(void* user)
1166{
Steve Blockb8a80522011-12-20 16:23:08 +00001167 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001168 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1169 while (!p->mExit) {
1170 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001171 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001172 p->mCb(p->mClient, 0, 0, 0);
1173 }
1174 usleep(interval);
1175 }
1176 Mutex::Autolock _l(p->mLock);
1177 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001178 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001179 return 0;
1180}
1181#endif
1182
1183static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1184
Glenn Kastene1c39622012-01-04 09:36:37 -08001185sp<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 -08001186{
Steve Block3856b092011-10-20 11:56:00 +01001187 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188 sp<MemoryBase> mem;
1189 sp<MediaPlayerBase> player;
1190
1191 // Protect our precious, precious DRMd ringtones by only allowing
1192 // decoding of http, but not filesystem paths or content Uris.
1193 // If the application wants to decode those, it should open a
1194 // filedescriptor for them and use that.
1195 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001196 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001197 return mem;
1198 }
1199
1200 player_type playerType = getPlayerType(url);
Steve Block3856b092011-10-20 11:56:00 +01001201 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001202
1203 // create the right type of player
1204 sp<AudioCache> cache = new AudioCache(url);
1205 player = android::createPlayer(playerType, cache.get(), cache->notify);
1206 if (player == NULL) goto Exit;
1207 if (player->hardwareOutput()) goto Exit;
1208
1209 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1210
1211 // set data source
1212 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1213
Steve Block3856b092011-10-20 11:56:00 +01001214 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 player->prepareAsync();
1216
Steve Block3856b092011-10-20 11:56:00 +01001217 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001218 if (cache->wait() != NO_ERROR) goto Exit;
1219
Steve Block3856b092011-10-20 11:56:00 +01001220 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001221 player->start();
1222
Steve Block3856b092011-10-20 11:56:00 +01001223 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001224 cache->wait();
1225 // in case of error, return what was successfully decoded.
1226 if (cache->size() == 0) {
1227 goto Exit;
1228 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001229
1230 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1231 *pSampleRate = cache->sampleRate();
1232 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001233 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001234 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 -08001235
1236Exit:
1237 if (player != 0) player->reset();
1238 return mem;
1239}
1240
Glenn Kastene1c39622012-01-04 09:36:37 -08001241sp<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 -08001242{
Steve Block3856b092011-10-20 11:56:00 +01001243 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001244 sp<MemoryBase> mem;
1245 sp<MediaPlayerBase> player;
1246
1247 player_type playerType = getPlayerType(fd, offset, length);
Steve Block3856b092011-10-20 11:56:00 +01001248 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001249
1250 // create the right type of player
1251 sp<AudioCache> cache = new AudioCache("decode_fd");
1252 player = android::createPlayer(playerType, cache.get(), cache->notify);
1253 if (player == NULL) goto Exit;
1254 if (player->hardwareOutput()) goto Exit;
1255
1256 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1257
1258 // set data source
1259 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1260
Steve Block3856b092011-10-20 11:56:00 +01001261 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 player->prepareAsync();
1263
Steve Block3856b092011-10-20 11:56:00 +01001264 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 if (cache->wait() != NO_ERROR) goto Exit;
1266
Steve Block3856b092011-10-20 11:56:00 +01001267 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001268 player->start();
1269
Steve Block3856b092011-10-20 11:56:00 +01001270 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001271 cache->wait();
1272 // in case of error, return what was successfully decoded.
1273 if (cache->size() == 0) {
1274 goto Exit;
1275 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276
1277 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1278 *pSampleRate = cache->sampleRate();
1279 *pNumChannels = cache->channelCount();
1280 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001281 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 -08001282
1283Exit:
1284 if (player != 0) player->reset();
1285 ::close(fd);
1286 return mem;
1287}
1288
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001289
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290#undef LOG_TAG
1291#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001292MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001293 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001294 mCallbackCookie(NULL),
1295 mSessionId(sessionId) {
Steve Block3856b092011-10-20 11:56:00 +01001296 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 mTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001298 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 mLeftVolume = 1.0;
1300 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001301 mPlaybackRatePermille = 1000;
1302 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001303 mLatency = 0;
1304 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001305 mAuxEffectId = 0;
1306 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307 setMinBufferCount();
1308}
1309
1310MediaPlayerService::AudioOutput::~AudioOutput()
1311{
1312 close();
1313}
1314
1315void MediaPlayerService::AudioOutput::setMinBufferCount()
1316{
1317 char value[PROPERTY_VALUE_MAX];
1318 if (property_get("ro.kernel.qemu", value, 0)) {
1319 mIsOnEmulator = true;
1320 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1321 }
1322}
1323
1324bool MediaPlayerService::AudioOutput::isOnEmulator()
1325{
1326 setMinBufferCount();
1327 return mIsOnEmulator;
1328}
1329
1330int MediaPlayerService::AudioOutput::getMinBufferCount()
1331{
1332 setMinBufferCount();
1333 return mMinBufferCount;
1334}
1335
1336ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1337{
1338 if (mTrack == 0) return NO_INIT;
1339 return mTrack->frameCount() * frameSize();
1340}
1341
1342ssize_t MediaPlayerService::AudioOutput::frameCount() const
1343{
1344 if (mTrack == 0) return NO_INIT;
1345 return mTrack->frameCount();
1346}
1347
1348ssize_t MediaPlayerService::AudioOutput::channelCount() const
1349{
1350 if (mTrack == 0) return NO_INIT;
1351 return mTrack->channelCount();
1352}
1353
1354ssize_t MediaPlayerService::AudioOutput::frameSize() const
1355{
1356 if (mTrack == 0) return NO_INIT;
1357 return mTrack->frameSize();
1358}
1359
1360uint32_t MediaPlayerService::AudioOutput::latency () const
1361{
1362 return mLatency;
1363}
1364
1365float MediaPlayerService::AudioOutput::msecsPerFrame() const
1366{
1367 return mMsecsPerFrame;
1368}
1369
Eric Laurent342e9cf2010-01-19 17:37:09 -08001370status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position)
1371{
1372 if (mTrack == 0) return NO_INIT;
1373 return mTrack->getPosition(position);
1374}
1375
Andreas Huber20111aa2009-07-14 16:56:47 -07001376status_t MediaPlayerService::AudioOutput::open(
Glenn Kastene1c39622012-01-04 09:36:37 -08001377 uint32_t sampleRate, int channelCount, audio_format_t format, int bufferCount,
Andreas Huber20111aa2009-07-14 16:56:47 -07001378 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001379{
Andreas Huber20111aa2009-07-14 16:56:47 -07001380 mCallback = cb;
1381 mCallbackCookie = cookie;
1382
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001383 // Check argument "bufferCount" against the mininum buffer count
1384 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001385 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001386 bufferCount = mMinBufferCount;
1387
1388 }
Steve Block3856b092011-10-20 11:56:00 +01001389 ALOGV("open(%u, %d, %d, %d, %d)", sampleRate, channelCount, format, bufferCount,mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390 if (mTrack) close();
1391 int afSampleRate;
1392 int afFrameCount;
1393 int frameCount;
1394
1395 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1396 return NO_INIT;
1397 }
1398 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1399 return NO_INIT;
1400 }
1401
1402 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001403
1404 AudioTrack *t;
1405 if (mCallback != NULL) {
1406 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001407 mStreamType,
1408 sampleRate,
1409 format,
Dima Zavinfce7a472011-04-19 22:30:36 -07001410 (channelCount == 2) ? AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001411 frameCount,
1412 0 /* flags */,
1413 CallbackWrapper,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001414 this,
1415 0,
1416 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001417 } else {
1418 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001419 mStreamType,
1420 sampleRate,
1421 format,
Dima Zavinfce7a472011-04-19 22:30:36 -07001422 (channelCount == 2) ? AUDIO_CHANNEL_OUT_STEREO : AUDIO_CHANNEL_OUT_MONO,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001423 frameCount,
1424 0,
1425 NULL,
1426 NULL,
1427 0,
1428 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001429 }
1430
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001431 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001432 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001433 delete t;
1434 return NO_INIT;
1435 }
1436
Steve Block3856b092011-10-20 11:56:00 +01001437 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001438 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001439
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001440 mSampleRateHz = sampleRate;
1441 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Dave Sparks1d711f62009-12-03 10:13:32 -08001442 mLatency = t->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001444
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001445 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1446 if (res != NO_ERROR) {
1447 return res;
1448 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001449 t->setAuxEffectSendLevel(mSendLevel);
1450 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001451}
1452
1453void MediaPlayerService::AudioOutput::start()
1454{
Steve Block3856b092011-10-20 11:56:00 +01001455 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456 if (mTrack) {
1457 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001458 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001459 mTrack->start();
1460 }
1461}
1462
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001463
1464
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001465ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1466{
Andreas Huber20111aa2009-07-14 16:56:47 -07001467 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1468
Steve Block3856b092011-10-20 11:56:00 +01001469 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001470 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001471 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001472 return ret;
1473 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001474 return NO_INIT;
1475}
1476
1477void MediaPlayerService::AudioOutput::stop()
1478{
Steve Block3856b092011-10-20 11:56:00 +01001479 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001480 if (mTrack) mTrack->stop();
1481}
1482
1483void MediaPlayerService::AudioOutput::flush()
1484{
Steve Block3856b092011-10-20 11:56:00 +01001485 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486 if (mTrack) mTrack->flush();
1487}
1488
1489void MediaPlayerService::AudioOutput::pause()
1490{
Steve Block3856b092011-10-20 11:56:00 +01001491 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001492 if (mTrack) mTrack->pause();
1493}
1494
1495void MediaPlayerService::AudioOutput::close()
1496{
Steve Block3856b092011-10-20 11:56:00 +01001497 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001498 delete mTrack;
1499 mTrack = 0;
1500}
1501
1502void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1503{
Steve Block3856b092011-10-20 11:56:00 +01001504 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001505 mLeftVolume = left;
1506 mRightVolume = right;
1507 if (mTrack) {
1508 mTrack->setVolume(left, right);
1509 }
1510}
1511
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001512status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1513{
1514 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1515 status_t res = NO_ERROR;
1516 if (mTrack) {
1517 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1518 } else {
1519 res = NO_INIT;
1520 }
1521 mPlaybackRatePermille = ratePermille;
1522 if (mSampleRateHz != 0) {
1523 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1524 }
1525 return res;
1526}
1527
Eric Laurent2beeb502010-07-16 07:43:46 -07001528status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1529{
Steve Block3856b092011-10-20 11:56:00 +01001530 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001531 mSendLevel = level;
1532 if (mTrack) {
1533 return mTrack->setAuxEffectSendLevel(level);
1534 }
1535 return NO_ERROR;
1536}
1537
1538status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1539{
Steve Block3856b092011-10-20 11:56:00 +01001540 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001541 mAuxEffectId = effectId;
1542 if (mTrack) {
1543 return mTrack->attachAuxEffect(effectId);
1544 }
1545 return NO_ERROR;
1546}
1547
Andreas Huber20111aa2009-07-14 16:56:47 -07001548// static
1549void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001550 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001551 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001552 if (event != AudioTrack::EVENT_MORE_DATA) {
1553 return;
1554 }
1555
1556 AudioOutput *me = (AudioOutput *)cookie;
1557 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
1558
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001559 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001560 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001561
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001562 if (actualSize == 0 && buffer->size > 0) {
1563 // We've reached EOS but the audio track is not stopped yet,
1564 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001565
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001566 memset(buffer->raw, 0, buffer->size);
1567 actualSize = buffer->size;
1568 }
1569
1570 buffer->size = actualSize;
Andreas Huber20111aa2009-07-14 16:56:47 -07001571}
1572
Eric Laurent8c563ed2010-10-07 18:23:03 -07001573int MediaPlayerService::AudioOutput::getSessionId()
1574{
1575 return mSessionId;
1576}
1577
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001578#undef LOG_TAG
1579#define LOG_TAG "AudioCache"
1580MediaPlayerService::AudioCache::AudioCache(const char* name) :
1581 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1582 mError(NO_ERROR), mCommandComplete(false)
1583{
1584 // create ashmem heap
1585 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1586}
1587
1588uint32_t MediaPlayerService::AudioCache::latency () const
1589{
1590 return 0;
1591}
1592
1593float MediaPlayerService::AudioCache::msecsPerFrame() const
1594{
1595 return mMsecsPerFrame;
1596}
1597
Eric Laurent342e9cf2010-01-19 17:37:09 -08001598status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position)
1599{
1600 if (position == 0) return BAD_VALUE;
1601 *position = mSize;
1602 return NO_ERROR;
1603}
1604
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001605////////////////////////////////////////////////////////////////////////////////
1606
1607struct CallbackThread : public Thread {
1608 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1609 MediaPlayerBase::AudioSink::AudioCallback cb,
1610 void *cookie);
1611
1612protected:
1613 virtual ~CallbackThread();
1614
1615 virtual bool threadLoop();
1616
1617private:
1618 wp<MediaPlayerBase::AudioSink> mSink;
1619 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1620 void *mCookie;
1621 void *mBuffer;
1622 size_t mBufferSize;
1623
1624 CallbackThread(const CallbackThread &);
1625 CallbackThread &operator=(const CallbackThread &);
1626};
1627
1628CallbackThread::CallbackThread(
1629 const wp<MediaPlayerBase::AudioSink> &sink,
1630 MediaPlayerBase::AudioSink::AudioCallback cb,
1631 void *cookie)
1632 : mSink(sink),
1633 mCallback(cb),
1634 mCookie(cookie),
1635 mBuffer(NULL),
1636 mBufferSize(0) {
1637}
1638
1639CallbackThread::~CallbackThread() {
1640 if (mBuffer) {
1641 free(mBuffer);
1642 mBuffer = NULL;
1643 }
1644}
1645
1646bool CallbackThread::threadLoop() {
1647 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1648 if (sink == NULL) {
1649 return false;
1650 }
1651
1652 if (mBuffer == NULL) {
1653 mBufferSize = sink->bufferSize();
1654 mBuffer = malloc(mBufferSize);
1655 }
1656
1657 size_t actualSize =
1658 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1659
1660 if (actualSize > 0) {
1661 sink->write(mBuffer, actualSize);
1662 }
1663
1664 return true;
1665}
1666
1667////////////////////////////////////////////////////////////////////////////////
1668
Andreas Huber20111aa2009-07-14 16:56:47 -07001669status_t MediaPlayerService::AudioCache::open(
Glenn Kastene1c39622012-01-04 09:36:37 -08001670 uint32_t sampleRate, int channelCount, audio_format_t format, int bufferCount,
Andreas Huber20111aa2009-07-14 16:56:47 -07001671 AudioCallback cb, void *cookie)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001672{
Steve Block3856b092011-10-20 11:56:00 +01001673 ALOGV("open(%u, %d, %d, %d)", sampleRate, channelCount, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001674 if (mHeap->getHeapID() < 0) {
1675 return NO_INIT;
1676 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001677
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001678 mSampleRate = sampleRate;
1679 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001680 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001681 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001682
1683 if (cb != NULL) {
1684 mCallbackThread = new CallbackThread(this, cb, cookie);
1685 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001686 return NO_ERROR;
1687}
1688
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001689void MediaPlayerService::AudioCache::start() {
1690 if (mCallbackThread != NULL) {
1691 mCallbackThread->run("AudioCache callback");
1692 }
1693}
1694
1695void MediaPlayerService::AudioCache::stop() {
1696 if (mCallbackThread != NULL) {
1697 mCallbackThread->requestExitAndWait();
1698 }
1699}
1700
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001701ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1702{
Steve Block3856b092011-10-20 11:56:00 +01001703 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001704 if ((buffer == 0) || (size == 0)) return size;
1705
1706 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1707 if (p == NULL) return NO_INIT;
1708 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001709 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001710 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001711 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001712 size = mHeap->getSize() - mSize;
1713 }
1714 memcpy(p, buffer, size);
1715 mSize += size;
1716 return size;
1717}
1718
1719// call with lock held
1720status_t MediaPlayerService::AudioCache::wait()
1721{
1722 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001723 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001724 mSignal.wait(mLock);
1725 }
1726 mCommandComplete = false;
1727
1728 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001729 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001730 } else {
Steve Block3856b092011-10-20 11:56:00 +01001731 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001732 }
1733 return mError;
1734}
1735
Gloria Wangb483c472011-04-11 17:23:27 -07001736void MediaPlayerService::AudioCache::notify(
1737 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001738{
Steve Block3856b092011-10-20 11:56:00 +01001739 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001740 AudioCache* p = static_cast<AudioCache*>(cookie);
1741
1742 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001743 switch (msg)
1744 {
1745 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001746 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001747 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001748 break;
1749 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001750 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001751 break;
1752 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001753 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001754 break;
1755 default:
Steve Block3856b092011-10-20 11:56:00 +01001756 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001757 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001758 }
1759
1760 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001761 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001762 p->mCommandComplete = true;
1763 p->mSignal.signal();
1764}
1765
Eric Laurent8c563ed2010-10-07 18:23:03 -07001766int MediaPlayerService::AudioCache::getSessionId()
1767{
1768 return 0;
1769}
1770
Gloria Wang7cf180c2011-02-19 18:37:57 -08001771void MediaPlayerService::addBatteryData(uint32_t params)
1772{
1773 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001774
1775 int32_t time = systemTime() / 1000000L;
1776
1777 // change audio output devices. This notification comes from AudioFlinger
1778 if ((params & kBatteryDataSpeakerOn)
1779 || (params & kBatteryDataOtherAudioDeviceOn)) {
1780
1781 int deviceOn[NUM_AUDIO_DEVICES];
1782 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1783 deviceOn[i] = 0;
1784 }
1785
1786 if ((params & kBatteryDataSpeakerOn)
1787 && (params & kBatteryDataOtherAudioDeviceOn)) {
1788 deviceOn[SPEAKER_AND_OTHER] = 1;
1789 } else if (params & kBatteryDataSpeakerOn) {
1790 deviceOn[SPEAKER] = 1;
1791 } else {
1792 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1793 }
1794
1795 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1796 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1797
1798 if (mBatteryAudio.refCount > 0) { // if playing audio
1799 if (!deviceOn[i]) {
1800 mBatteryAudio.lastTime[i] += time;
1801 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1802 mBatteryAudio.lastTime[i] = 0;
1803 } else {
1804 mBatteryAudio.lastTime[i] = 0 - time;
1805 }
1806 }
1807
1808 mBatteryAudio.deviceOn[i] = deviceOn[i];
1809 }
1810 }
1811 return;
1812 }
1813
1814 // an sudio stream is started
1815 if (params & kBatteryDataAudioFlingerStart) {
1816 // record the start time only if currently no other audio
1817 // is being played
1818 if (mBatteryAudio.refCount == 0) {
1819 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1820 if (mBatteryAudio.deviceOn[i]) {
1821 mBatteryAudio.lastTime[i] -= time;
1822 }
1823 }
1824 }
1825
1826 mBatteryAudio.refCount ++;
1827 return;
1828
1829 } else if (params & kBatteryDataAudioFlingerStop) {
1830 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001831 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001832 return;
1833 }
1834
1835 // record the stop time only if currently this is the only
1836 // audio being played
1837 if (mBatteryAudio.refCount == 1) {
1838 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1839 if (mBatteryAudio.deviceOn[i]) {
1840 mBatteryAudio.lastTime[i] += time;
1841 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1842 mBatteryAudio.lastTime[i] = 0;
1843 }
1844 }
1845 }
1846
1847 mBatteryAudio.refCount --;
1848 return;
1849 }
1850
Gloria Wang7cf180c2011-02-19 18:37:57 -08001851 int uid = IPCThreadState::self()->getCallingUid();
1852 if (uid == AID_MEDIA) {
1853 return;
1854 }
1855 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001856
1857 if (index < 0) { // create a new entry for this UID
1858 BatteryUsageInfo info;
1859 info.audioTotalTime = 0;
1860 info.videoTotalTime = 0;
1861 info.audioLastTime = 0;
1862 info.videoLastTime = 0;
1863 info.refCount = 0;
1864
Gloria Wang9ee159b2011-02-24 14:51:45 -08001865 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001866 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001867 return;
1868 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001869 }
1870
1871 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1872
1873 if (params & kBatteryDataCodecStarted) {
1874 if (params & kBatteryDataTrackAudio) {
1875 info.audioLastTime -= time;
1876 info.refCount ++;
1877 }
1878 if (params & kBatteryDataTrackVideo) {
1879 info.videoLastTime -= time;
1880 info.refCount ++;
1881 }
1882 } else {
1883 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001884 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001885 return;
1886 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001887 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001888 mBatteryData.removeItem(uid);
1889 return;
1890 }
1891
1892 if (params & kBatteryDataTrackAudio) {
1893 info.audioLastTime += time;
1894 info.refCount --;
1895 }
1896 if (params & kBatteryDataTrackVideo) {
1897 info.videoLastTime += time;
1898 info.refCount --;
1899 }
1900
1901 // no stream is being played by this UID
1902 if (info.refCount == 0) {
1903 info.audioTotalTime += info.audioLastTime;
1904 info.audioLastTime = 0;
1905 info.videoTotalTime += info.videoLastTime;
1906 info.videoLastTime = 0;
1907 }
1908 }
1909}
1910
1911status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
1912 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001913
1914 // audio output devices usage
1915 int32_t time = systemTime() / 1000000L; //in ms
1916 int32_t totalTime;
1917
1918 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1919 totalTime = mBatteryAudio.totalTime[i];
1920
1921 if (mBatteryAudio.deviceOn[i]
1922 && (mBatteryAudio.lastTime[i] != 0)) {
1923 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
1924 totalTime += tmpTime;
1925 }
1926
1927 reply->writeInt32(totalTime);
1928 // reset the total time
1929 mBatteryAudio.totalTime[i] = 0;
1930 }
1931
1932 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08001933 BatteryUsageInfo info;
1934 int size = mBatteryData.size();
1935
1936 reply->writeInt32(size);
1937 int i = 0;
1938
1939 while (i < size) {
1940 info = mBatteryData.valueAt(i);
1941
1942 reply->writeInt32(mBatteryData.keyAt(i)); //UID
1943 reply->writeInt32(info.audioTotalTime);
1944 reply->writeInt32(info.videoTotalTime);
1945
1946 info.audioTotalTime = 0;
1947 info.videoTotalTime = 0;
1948
1949 // remove the UID entry where no stream is being played
1950 if (info.refCount <= 0) {
1951 mBatteryData.removeItemsAt(i);
1952 size --;
1953 i --;
1954 }
1955 i++;
1956 }
1957 return NO_ERROR;
1958}
nikoa64c8c72009-07-20 15:07:26 -07001959} // namespace android