blob: 6346363952747ef6d313c460ed276c4468e7030c [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
Gloria Wang7cf180c2011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Mathias Agopian75624082009-05-19 19:08:10 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryHeapBase.h>
40#include <binder/MemoryBase.h>
Jamie Gennis7dae00b2011-10-26 18:36:31 -070041#include <gui/SurfaceTextureClient.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070042#include <utils/Errors.h> // for status_t
43#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070044#include <utils/SystemClock.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070045#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080046
47#include <media/MediaPlayerInterface.h>
48#include <media/mediarecorder.h>
49#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070050#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070052#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070053#include <media/stagefright/MediaErrors.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080054
Dima Zavin64760242011-05-11 14:15:23 -070055#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070056
Gloria Wang7cf180c2011-02-19 18:37:57 -080057#include <private/android_filesystem_config.h>
58
James Dong559bf282012-03-28 10:29:14 -070059#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080060#include "MediaRecorderClient.h"
61#include "MediaPlayerService.h"
62#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070063#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064
65#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070066#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070067#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080068#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070069
Andreas Huber20111aa2009-07-14 16:56:47 -070070#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070071
Andreas Hubered3e3e02012-03-26 11:13:27 -070072#include "Crypto.h"
73
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070074namespace {
nikoa64c8c72009-07-20 15:07:26 -070075using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070076using android::status_t;
77using android::OK;
78using android::BAD_VALUE;
79using android::NOT_ENOUGH_DATA;
80using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070081
82// Max number of entries in the filter.
83const int kMaxFilterSize = 64; // I pulled that out of thin air.
84
nikoa64c8c72009-07-20 15:07:26 -070085// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070086
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070087
88// Unmarshall a filter from a Parcel.
89// Filter format in a parcel:
90//
91// 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
92// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93// | number of entries (n) |
94// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95// | metadata type 1 |
96// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97// | metadata type 2 |
98// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99// ....
100// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101// | metadata type n |
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103//
104// @param p Parcel that should start with a filter.
105// @param[out] filter On exit contains the list of metadata type to be
106// filtered.
107// @param[out] status On exit contains the status code to be returned.
108// @return true if the parcel starts with a valid filter.
109bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700110 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700111 status_t *status)
112{
Nicolas Catania48290382009-07-10 13:53:06 -0700113 int32_t val;
114 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700115 {
Steve Block29357bc2012-01-06 19:20:56 +0000116 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700117 *status = NOT_ENOUGH_DATA;
118 return false;
119 }
120
Nicolas Catania48290382009-07-10 13:53:06 -0700121 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700122 {
Steve Block29357bc2012-01-06 19:20:56 +0000123 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700124 *status = BAD_VALUE;
125 return false;
126 }
127
Nicolas Catania48290382009-07-10 13:53:06 -0700128 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700129
130 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700131 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700132
nikoa64c8c72009-07-20 15:07:26 -0700133 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134
Nicolas Catania48290382009-07-10 13:53:06 -0700135
136 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700137 {
Steve Block29357bc2012-01-06 19:20:56 +0000138 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700139 *status = NOT_ENOUGH_DATA;
140 return false;
141 }
142
nikoa64c8c72009-07-20 15:07:26 -0700143 const Metadata::Type *data =
144 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145
Nicolas Catania48290382009-07-10 13:53:06 -0700146 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700147 {
Steve Block29357bc2012-01-06 19:20:56 +0000148 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149 *status = BAD_VALUE;
150 return false;
151 }
152
153 // TODO: The stl impl of vector would be more efficient here
154 // because it degenerates into a memcpy on pod types. Try to
155 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700156 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157 {
Nicolas Catania48290382009-07-10 13:53:06 -0700158 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700159 ++data;
160 }
161 *status = OK;
162 return true;
163}
164
Nicolas Catania48290382009-07-10 13:53:06 -0700165// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700166// @param val To be searched.
167// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700168bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169{
170 // Deal with empty and ANY right away
171 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700172 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700173
Nicolas Catania48290382009-07-10 13:53:06 -0700174 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700175}
176
177} // anonymous namespace
178
179
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800180namespace android {
181
Dave Burked681bbb2011-08-30 14:39:17 +0100182static bool checkPermission(const char* permissionString) {
183#ifndef HAVE_ANDROID_OS
184 return true;
185#endif
186 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
187 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000188 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100189 return ok;
190}
191
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800192// 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 -0800193/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
194/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
195
196void MediaPlayerService::instantiate() {
197 defaultServiceManager()->addService(
198 String16("media.player"), new MediaPlayerService());
199}
200
201MediaPlayerService::MediaPlayerService()
202{
Steve Block3856b092011-10-20 11:56:00 +0100203 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800204 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800205
206 mBatteryAudio.refCount = 0;
207 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
208 mBatteryAudio.deviceOn[i] = 0;
209 mBatteryAudio.lastTime[i] = 0;
210 mBatteryAudio.totalTime[i] = 0;
211 }
212 // speaker is on by default
213 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700214
215 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800216}
217
218MediaPlayerService::~MediaPlayerService()
219{
Steve Block3856b092011-10-20 11:56:00 +0100220 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800221}
222
223sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
224{
Gloria Wangdac6a312009-10-29 15:46:37 -0700225 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
226 wp<MediaRecorderClient> w = recorder;
227 Mutex::Autolock lock(mLock);
228 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100229 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230 return recorder;
231}
232
Gloria Wangdac6a312009-10-29 15:46:37 -0700233void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
234{
235 Mutex::Autolock lock(mLock);
236 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100237 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700238}
239
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800240sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
241{
242 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100243 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244 return retriever;
245}
246
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100248 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800249{
250 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700251
252 sp<Client> c = new Client(
253 this, pid, connId, client, audioSessionId,
254 IPCThreadState::self()->getCallingUid());
255
Steve Block3856b092011-10-20 11:56:00 +0100256 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100257 IPCThreadState::self()->getCallingUid());
258
259 wp<Client> w = c;
260 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261 Mutex::Autolock lock(mLock);
262 mClients.add(w);
263 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800264 return c;
265}
266
Andreas Huber318ad9c2009-10-15 13:46:54 -0700267sp<IOMX> MediaPlayerService::getOMX() {
268 Mutex::Autolock autoLock(mLock);
269
270 if (mOMX.get() == NULL) {
271 mOMX = new OMX;
272 }
273
274 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700275}
276
Andreas Hubered3e3e02012-03-26 11:13:27 -0700277sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700278 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700279}
280
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800281status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
282{
283 const size_t SIZE = 256;
284 char buffer[SIZE];
285 String8 result;
286
287 result.append(" AudioCache\n");
288 if (mHeap != 0) {
289 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
290 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
291 result.append(buffer);
292 }
293 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
294 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
295 result.append(buffer);
296 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
297 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
298 result.append(buffer);
299 ::write(fd, result.string(), result.size());
300 return NO_ERROR;
301}
302
303status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
304{
305 const size_t SIZE = 256;
306 char buffer[SIZE];
307 String8 result;
308
309 result.append(" AudioOutput\n");
310 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
311 mStreamType, mLeftVolume, mRightVolume);
312 result.append(buffer);
313 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800314 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800315 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700316 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
317 mAuxEffectId, mSendLevel);
318 result.append(buffer);
319
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320 ::write(fd, result.string(), result.size());
321 if (mTrack != 0) {
322 mTrack->dump(fd, args);
323 }
324 return NO_ERROR;
325}
326
327status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
328{
329 const size_t SIZE = 256;
330 char buffer[SIZE];
331 String8 result;
332 result.append(" Client\n");
333 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
334 mPid, mConnId, mStatus, mLoop?"true": "false");
335 result.append(buffer);
336 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700337 if (mPlayer != NULL) {
338 mPlayer->dump(fd, args);
339 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 if (mAudioOutput != 0) {
341 mAudioOutput->dump(fd, args);
342 }
343 write(fd, "\n", 1);
344 return NO_ERROR;
345}
346
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800347status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
348{
349 const size_t SIZE = 256;
350 char buffer[SIZE];
351 String8 result;
352 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
353 snprintf(buffer, SIZE, "Permission Denial: "
354 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
355 IPCThreadState::self()->getCallingPid(),
356 IPCThreadState::self()->getCallingUid());
357 result.append(buffer);
358 } else {
359 Mutex::Autolock lock(mLock);
360 for (int i = 0, n = mClients.size(); i < n; ++i) {
361 sp<Client> c = mClients[i].promote();
362 if (c != 0) c->dump(fd, args);
363 }
James Dongb9141222010-07-08 11:16:11 -0700364 if (mMediaRecorderClients.size() == 0) {
365 result.append(" No media recorder client\n\n");
366 } else {
367 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
368 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700369 if (c != 0) {
370 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
371 result.append(buffer);
372 write(fd, result.string(), result.size());
373 result = "\n";
374 c->dump(fd, args);
375 }
James Dongb9141222010-07-08 11:16:11 -0700376 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700377 }
378
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800379 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700380 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800381 FILE *f = fopen(buffer, "r");
382 if (f) {
383 while (!feof(f)) {
384 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700385 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800386 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700387 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800388 strstr(buffer, " /system/media/")) {
389 result.append(" ");
390 result.append(buffer);
391 }
392 }
393 fclose(f);
394 } else {
395 result.append("couldn't open ");
396 result.append(buffer);
397 result.append("\n");
398 }
399
Glenn Kasten0512ab52011-05-04 17:58:57 -0700400 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 DIR *d = opendir(buffer);
402 if (d) {
403 struct dirent *ent;
404 while((ent = readdir(d)) != NULL) {
405 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700406 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 struct stat s;
408 if (lstat(buffer, &s) == 0) {
409 if ((s.st_mode & S_IFMT) == S_IFLNK) {
410 char linkto[256];
411 int len = readlink(buffer, linkto, sizeof(linkto));
412 if(len > 0) {
413 if(len > 255) {
414 linkto[252] = '.';
415 linkto[253] = '.';
416 linkto[254] = '.';
417 linkto[255] = 0;
418 } else {
419 linkto[len] = 0;
420 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700421 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800422 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700423 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800424 strstr(linkto, "/system/media/") == linkto) {
425 result.append(" ");
426 result.append(buffer);
427 result.append(" -> ");
428 result.append(linkto);
429 result.append("\n");
430 }
431 }
432 } else {
433 result.append(" unexpected type for ");
434 result.append(buffer);
435 result.append("\n");
436 }
437 }
438 }
439 }
440 closedir(d);
441 } else {
442 result.append("couldn't open ");
443 result.append(buffer);
444 result.append("\n");
445 }
446
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800447 bool dumpMem = false;
448 for (size_t i = 0; i < args.size(); i++) {
449 if (args[i] == String16("-m")) {
450 dumpMem = true;
451 }
452 }
453 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700454 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800456 }
457 write(fd, result.string(), result.size());
458 return NO_ERROR;
459}
460
461void MediaPlayerService::removeClient(wp<Client> client)
462{
463 Mutex::Autolock lock(mLock);
464 mClients.remove(client);
465}
466
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700467MediaPlayerService::Client::Client(
468 const sp<MediaPlayerService>& service, pid_t pid,
469 int32_t connId, const sp<IMediaPlayerClient>& client,
470 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800471{
Steve Block3856b092011-10-20 11:56:00 +0100472 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800473 mPid = pid;
474 mConnId = connId;
475 mService = service;
476 mClient = client;
477 mLoop = false;
478 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700479 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700480 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800481 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700482
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000484 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 mAntagonizer = new Antagonizer(notify, this);
486#endif
487}
488
489MediaPlayerService::Client::~Client()
490{
Steve Block3856b092011-10-20 11:56:00 +0100491 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 mAudioOutput.clear();
493 wp<Client> client(this);
494 disconnect();
495 mService->removeClient(client);
496}
497
498void MediaPlayerService::Client::disconnect()
499{
Steve Block3856b092011-10-20 11:56:00 +0100500 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501 // grab local reference and clear main reference to prevent future
502 // access to object
503 sp<MediaPlayerBase> p;
504 {
505 Mutex::Autolock l(mLock);
506 p = mPlayer;
507 }
Dave Sparks795fa582009-03-24 17:57:12 -0700508 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700509
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 mPlayer.clear();
511
512 // clear the notification to prevent callbacks to dead client
513 // and reset the player. We assume the player will serialize
514 // access to itself if necessary.
515 if (p != 0) {
516 p->setNotifyCallback(0, 0);
517#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000518 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800519 mAntagonizer->kill();
520#endif
521 p->reset();
522 }
523
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700524 disconnectNativeWindow();
525
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 IPCThreadState::self()->flushCommands();
527}
528
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
530{
531 // determine if we have the right player type
532 sp<MediaPlayerBase> p = mPlayer;
533 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100534 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800535 p.clear();
536 }
537 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700538 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700540
Jason Simmonsdb29e522011-08-12 13:46:55 -0700541 if (p != NULL) {
542 p->setUID(mUID);
543 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700544
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 return p;
546}
547
John Grossmanc795b642012-02-22 15:38:35 -0800548sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
549 player_type playerType)
550{
551 ALOGV("player type = %d", playerType);
552
553 // create the right type of player
554 sp<MediaPlayerBase> p = createPlayer(playerType);
555 if (p == NULL) {
556 return p;
557 }
558
559 if (!p->hardwareOutput()) {
560 mAudioOutput = new AudioOutput(mAudioSessionId);
561 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
562 }
563
564 return p;
565}
566
567void MediaPlayerService::Client::setDataSource_post(
568 const sp<MediaPlayerBase>& p,
569 status_t status)
570{
571 ALOGV(" setDataSource");
572 mStatus = status;
573 if (mStatus != OK) {
574 ALOGE(" error: %d", mStatus);
575 return;
576 }
577
578 // Set the re-transmission endpoint if one was chosen.
579 if (mRetransmitEndpointValid) {
580 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
581 if (mStatus != NO_ERROR) {
582 ALOGE("setRetransmitEndpoint error: %d", mStatus);
583 }
584 }
585
586 if (mStatus == OK) {
587 mPlayer = p;
588 }
589}
590
Andreas Huber2db84552010-01-28 11:19:57 -0800591status_t MediaPlayerService::Client::setDataSource(
592 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800593{
Steve Block3856b092011-10-20 11:56:00 +0100594 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800595 if (url == NULL)
596 return UNKNOWN_ERROR;
597
Dave Burked681bbb2011-08-30 14:39:17 +0100598 if ((strncmp(url, "http://", 7) == 0) ||
599 (strncmp(url, "https://", 8) == 0) ||
600 (strncmp(url, "rtsp://", 7) == 0)) {
601 if (!checkPermission("android.permission.INTERNET")) {
602 return PERMISSION_DENIED;
603 }
604 }
605
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800606 if (strncmp(url, "content://", 10) == 0) {
607 // get a filedescriptor for the content Uri and
608 // pass it to the setDataSource(fd) method
609
610 String16 url16(url);
611 int fd = android::openContentProviderFile(url16);
612 if (fd < 0)
613 {
Steve Block29357bc2012-01-06 19:20:56 +0000614 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615 return UNKNOWN_ERROR;
616 }
617 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
618 close(fd);
619 return mStatus;
620 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700621 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800622 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
623 if (p == NULL) {
624 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800625 }
626
John Grossmanc795b642012-02-22 15:38:35 -0800627 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628 return mStatus;
629 }
630}
631
632status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
633{
Steve Block3856b092011-10-20 11:56:00 +0100634 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635 struct stat sb;
636 int ret = fstat(fd, &sb);
637 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000638 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639 return UNKNOWN_ERROR;
640 }
641
Steve Block3856b092011-10-20 11:56:00 +0100642 ALOGV("st_dev = %llu", sb.st_dev);
643 ALOGV("st_mode = %u", sb.st_mode);
644 ALOGV("st_uid = %lu", sb.st_uid);
645 ALOGV("st_gid = %lu", sb.st_gid);
646 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647
648 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000649 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800650 ::close(fd);
651 return UNKNOWN_ERROR;
652 }
653 if (offset + length > sb.st_size) {
654 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100655 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 }
657
John Grossman44a7e422012-06-21 17:29:24 -0700658 player_type playerType = MediaPlayerFactory::getPlayerType(this,
659 fd,
660 offset,
661 length);
John Grossmanc795b642012-02-22 15:38:35 -0800662 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
663 if (p == NULL) {
664 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 }
666
667 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800668 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800669 return mStatus;
670}
671
Andreas Hubere2b10282010-11-23 11:41:34 -0800672status_t MediaPlayerService::Client::setDataSource(
673 const sp<IStreamSource> &source) {
674 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700675 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800676 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800677 if (p == NULL) {
678 return NO_INIT;
679 }
680
Andreas Hubere2b10282010-11-23 11:41:34 -0800681 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800682 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800683 return mStatus;
684}
685
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700686void MediaPlayerService::Client::disconnectNativeWindow() {
687 if (mConnectedWindow != NULL) {
688 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
689 NATIVE_WINDOW_API_MEDIA);
690
691 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000692 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700693 strerror(-err), err);
694 }
695 }
696 mConnectedWindow.clear();
697}
698
Glenn Kasten11731182011-02-08 17:26:17 -0800699status_t MediaPlayerService::Client::setVideoSurfaceTexture(
700 const sp<ISurfaceTexture>& surfaceTexture)
701{
Steve Block3856b092011-10-20 11:56:00 +0100702 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800703 sp<MediaPlayerBase> p = getPlayer();
704 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700705
706 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
707 surfaceTexture->asBinder());
708 if (mConnectedWindowBinder == binder) {
709 return OK;
710 }
711
712 sp<ANativeWindow> anw;
713 if (surfaceTexture != NULL) {
714 anw = new SurfaceTextureClient(surfaceTexture);
715 status_t err = native_window_api_connect(anw.get(),
716 NATIVE_WINDOW_API_MEDIA);
717
718 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000719 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700720 // Note that we must do the reset before disconnecting from the ANW.
721 // Otherwise queue/dequeue calls could be made on the disconnected
722 // ANW, which may result in errors.
723 reset();
724
725 disconnectNativeWindow();
726
727 return err;
728 }
729 }
730
731 // Note that we must set the player's new SurfaceTexture before
732 // disconnecting the old one. Otherwise queue/dequeue calls could be made
733 // on the disconnected ANW, which may result in errors.
734 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
735
736 disconnectNativeWindow();
737
738 mConnectedWindow = anw;
739
740 if (err == OK) {
741 mConnectedWindowBinder = binder;
742 } else {
743 disconnectNativeWindow();
744 }
745
746 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800747}
748
Nicolas Catania1d187f12009-05-12 23:25:55 -0700749status_t MediaPlayerService::Client::invoke(const Parcel& request,
750 Parcel *reply)
751{
752 sp<MediaPlayerBase> p = getPlayer();
753 if (p == NULL) return UNKNOWN_ERROR;
754 return p->invoke(request, reply);
755}
756
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700757// This call doesn't need to access the native player.
758status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
759{
760 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700761 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700762
Nicolas Catania48290382009-07-10 13:53:06 -0700763 if (unmarshallFilter(filter, &allow, &status) &&
764 unmarshallFilter(filter, &drop, &status)) {
765 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700766
767 mMetadataAllow = allow;
768 mMetadataDrop = drop;
769 }
770 return status;
771}
772
Nicolas Catania48290382009-07-10 13:53:06 -0700773status_t MediaPlayerService::Client::getMetadata(
774 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700775{
nikoa64c8c72009-07-20 15:07:26 -0700776 sp<MediaPlayerBase> player = getPlayer();
777 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700778
nikod608a812009-07-16 16:39:53 -0700779 status_t status;
780 // Placeholder for the return code, updated by the caller.
781 reply->writeInt32(-1);
782
nikoa64c8c72009-07-20 15:07:26 -0700783 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700784
785 // We don't block notifications while we fetch the data. We clear
786 // mMetadataUpdated first so we don't lose notifications happening
787 // during the rest of this call.
788 {
789 Mutex::Autolock lock(mLock);
790 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700791 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700792 }
793 mMetadataUpdated.clear();
794 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700795
nikoa64c8c72009-07-20 15:07:26 -0700796 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700797
nikoa64c8c72009-07-20 15:07:26 -0700798 metadata.appendHeader();
799 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700800
801 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700802 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000803 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700804 return status;
805 }
806
807 // FIXME: Implement filtering on the result. Not critical since
808 // filtering takes place on the update notifications already. This
809 // would be when all the metadata are fetch and a filter is set.
810
nikod608a812009-07-16 16:39:53 -0700811 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700812 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700813 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700814}
815
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800816status_t MediaPlayerService::Client::prepareAsync()
817{
Steve Block3856b092011-10-20 11:56:00 +0100818 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800819 sp<MediaPlayerBase> p = getPlayer();
820 if (p == 0) return UNKNOWN_ERROR;
821 status_t ret = p->prepareAsync();
822#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000823 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800824 if (ret == NO_ERROR) mAntagonizer->start();
825#endif
826 return ret;
827}
828
829status_t MediaPlayerService::Client::start()
830{
Steve Block3856b092011-10-20 11:56:00 +0100831 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800832 sp<MediaPlayerBase> p = getPlayer();
833 if (p == 0) return UNKNOWN_ERROR;
834 p->setLooping(mLoop);
835 return p->start();
836}
837
838status_t MediaPlayerService::Client::stop()
839{
Steve Block3856b092011-10-20 11:56:00 +0100840 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800841 sp<MediaPlayerBase> p = getPlayer();
842 if (p == 0) return UNKNOWN_ERROR;
843 return p->stop();
844}
845
846status_t MediaPlayerService::Client::pause()
847{
Steve Block3856b092011-10-20 11:56:00 +0100848 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800849 sp<MediaPlayerBase> p = getPlayer();
850 if (p == 0) return UNKNOWN_ERROR;
851 return p->pause();
852}
853
854status_t MediaPlayerService::Client::isPlaying(bool* state)
855{
856 *state = false;
857 sp<MediaPlayerBase> p = getPlayer();
858 if (p == 0) return UNKNOWN_ERROR;
859 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 return NO_ERROR;
862}
863
864status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
865{
Steve Block3856b092011-10-20 11:56:00 +0100866 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800867 sp<MediaPlayerBase> p = getPlayer();
868 if (p == 0) return UNKNOWN_ERROR;
869 status_t ret = p->getCurrentPosition(msec);
870 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100871 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800872 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000873 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800874 }
875 return ret;
876}
877
878status_t MediaPlayerService::Client::getDuration(int *msec)
879{
Steve Block3856b092011-10-20 11:56:00 +0100880 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800881 sp<MediaPlayerBase> p = getPlayer();
882 if (p == 0) return UNKNOWN_ERROR;
883 status_t ret = p->getDuration(msec);
884 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000887 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800888 }
889 return ret;
890}
891
Marco Nelissen6b74d672012-02-28 16:07:44 -0800892status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
893 ALOGV("setNextPlayer");
894 Mutex::Autolock l(mLock);
895 sp<Client> c = static_cast<Client*>(player.get());
896 mNextClient = c;
897 if (mAudioOutput != NULL && c != NULL) {
898 mAudioOutput->setNextOutput(c->mAudioOutput);
899 } else {
900 ALOGE("no current audio output");
901 }
902 return OK;
903}
904
905
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800906status_t MediaPlayerService::Client::seekTo(int msec)
907{
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 sp<MediaPlayerBase> p = getPlayer();
910 if (p == 0) return UNKNOWN_ERROR;
911 return p->seekTo(msec);
912}
913
914status_t MediaPlayerService::Client::reset()
915{
Steve Block3856b092011-10-20 11:56:00 +0100916 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800917 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 sp<MediaPlayerBase> p = getPlayer();
919 if (p == 0) return UNKNOWN_ERROR;
920 return p->reset();
921}
922
Glenn Kastenfff6d712012-01-12 16:38:12 -0800923status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800924{
Steve Block3856b092011-10-20 11:56:00 +0100925 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800926 // TODO: for hardware output, call player instead
927 Mutex::Autolock l(mLock);
928 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
929 return NO_ERROR;
930}
931
932status_t MediaPlayerService::Client::setLooping(int loop)
933{
Steve Block3856b092011-10-20 11:56:00 +0100934 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800935 mLoop = loop;
936 sp<MediaPlayerBase> p = getPlayer();
937 if (p != 0) return p->setLooping(loop);
938 return NO_ERROR;
939}
940
941status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
942{
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800944
945 // for hardware output, call player instead
946 sp<MediaPlayerBase> p = getPlayer();
947 {
948 Mutex::Autolock l(mLock);
949 if (p != 0 && p->hardwareOutput()) {
950 MediaPlayerHWInterface* hwp =
951 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
952 return hwp->setVolume(leftVolume, rightVolume);
953 } else {
954 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
955 return NO_ERROR;
956 }
957 }
958
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800959 return NO_ERROR;
960}
961
Eric Laurent2beeb502010-07-16 07:43:46 -0700962status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
963{
Steve Block3856b092011-10-20 11:56:00 +0100964 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700965 Mutex::Autolock l(mLock);
966 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
967 return NO_ERROR;
968}
969
970status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
971{
Steve Block3856b092011-10-20 11:56:00 +0100972 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -0700973 Mutex::Autolock l(mLock);
974 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
975 return NO_ERROR;
976}
Nicolas Catania48290382009-07-10 13:53:06 -0700977
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700978status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +0100979 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700980 sp<MediaPlayerBase> p = getPlayer();
981 if (p == 0) return UNKNOWN_ERROR;
982 return p->setParameter(key, request);
983}
984
985status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +0100986 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -0700987 sp<MediaPlayerBase> p = getPlayer();
988 if (p == 0) return UNKNOWN_ERROR;
989 return p->getParameter(key, reply);
990}
991
John Grossmanc795b642012-02-22 15:38:35 -0800992status_t MediaPlayerService::Client::setRetransmitEndpoint(
993 const struct sockaddr_in* endpoint) {
994
995 if (NULL != endpoint) {
996 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
997 uint16_t p = ntohs(endpoint->sin_port);
998 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
999 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1000 } else {
1001 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1002 }
1003
1004 sp<MediaPlayerBase> p = getPlayer();
1005
1006 // Right now, the only valid time to set a retransmit endpoint is before
1007 // player selection has been made (since the presence or absence of a
1008 // retransmit endpoint is going to determine which player is selected during
1009 // setDataSource).
1010 if (p != 0) return INVALID_OPERATION;
1011
1012 if (NULL != endpoint) {
1013 mRetransmitEndpoint = *endpoint;
1014 mRetransmitEndpointValid = true;
1015 } else {
1016 mRetransmitEndpointValid = false;
1017 }
1018
1019 return NO_ERROR;
1020}
1021
John Grossman44a7e422012-06-21 17:29:24 -07001022status_t MediaPlayerService::Client::getRetransmitEndpoint(
1023 struct sockaddr_in* endpoint)
1024{
1025 if (NULL == endpoint)
1026 return BAD_VALUE;
1027
1028 sp<MediaPlayerBase> p = getPlayer();
1029
1030 if (p != NULL)
1031 return p->getRetransmitEndpoint(endpoint);
1032
1033 if (!mRetransmitEndpointValid)
1034 return NO_INIT;
1035
1036 *endpoint = mRetransmitEndpoint;
1037
1038 return NO_ERROR;
1039}
1040
Gloria Wangb483c472011-04-11 17:23:27 -07001041void MediaPlayerService::Client::notify(
1042 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001043{
1044 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001045 if (client == NULL) {
1046 return;
1047 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001048
James Dongb8a98252012-08-26 16:13:03 -07001049 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001050 {
1051 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001052 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001053 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001054 if (client->mAudioOutput != NULL)
1055 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001056 client->mNextClient->start();
1057 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1058 }
1059 }
1060
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001061 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001062 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001063 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001064
1065 if(client->shouldDropMetadata(metadata_type)) {
1066 return;
1067 }
1068
1069 // Update the list of metadata that have changed. getMetadata
1070 // also access mMetadataUpdated and clears it.
1071 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001072 }
James Dongb8a98252012-08-26 16:13:03 -07001073
1074 if (c != NULL) {
1075 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1076 c->notify(msg, ext1, ext2, obj);
1077 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001078}
1079
Nicolas Catania48290382009-07-10 13:53:06 -07001080
nikoa64c8c72009-07-20 15:07:26 -07001081bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001082{
Nicolas Catania48290382009-07-10 13:53:06 -07001083 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001084
Nicolas Catania48290382009-07-10 13:53:06 -07001085 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001086 return true;
1087 }
1088
Nicolas Catania48290382009-07-10 13:53:06 -07001089 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001090 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001091 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001092 return true;
1093 }
1094}
1095
Nicolas Catania48290382009-07-10 13:53:06 -07001096
nikoa64c8c72009-07-20 15:07:26 -07001097void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001098 Mutex::Autolock lock(mLock);
1099 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1100 mMetadataUpdated.add(metadata_type);
1101 }
1102}
1103
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001104#if CALLBACK_ANTAGONIZER
1105const int Antagonizer::interval = 10000; // 10 msecs
1106
1107Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1108 mExit(false), mActive(false), mClient(client), mCb(cb)
1109{
1110 createThread(callbackThread, this);
1111}
1112
1113void Antagonizer::kill()
1114{
1115 Mutex::Autolock _l(mLock);
1116 mActive = false;
1117 mExit = true;
1118 mCondition.wait(mLock);
1119}
1120
1121int Antagonizer::callbackThread(void* user)
1122{
Steve Blockb8a80522011-12-20 16:23:08 +00001123 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001124 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1125 while (!p->mExit) {
1126 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001127 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001128 p->mCb(p->mClient, 0, 0, 0);
1129 }
1130 usleep(interval);
1131 }
1132 Mutex::Autolock _l(p->mLock);
1133 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001134 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001135 return 0;
1136}
1137#endif
1138
1139static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1140
Glenn Kastene1c39622012-01-04 09:36:37 -08001141sp<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 -08001142{
Steve Block3856b092011-10-20 11:56:00 +01001143 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001144 sp<MemoryBase> mem;
1145 sp<MediaPlayerBase> player;
1146
1147 // Protect our precious, precious DRMd ringtones by only allowing
1148 // decoding of http, but not filesystem paths or content Uris.
1149 // If the application wants to decode those, it should open a
1150 // filedescriptor for them and use that.
1151 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001152 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001153 return mem;
1154 }
1155
John Grossman44a7e422012-06-21 17:29:24 -07001156 player_type playerType =
1157 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001158 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001159
1160 // create the right type of player
1161 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001162 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001163 if (player == NULL) goto Exit;
1164 if (player->hardwareOutput()) goto Exit;
1165
1166 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1167
1168 // set data source
1169 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1170
Steve Block3856b092011-10-20 11:56:00 +01001171 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001172 player->prepareAsync();
1173
Steve Block3856b092011-10-20 11:56:00 +01001174 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001175 if (cache->wait() != NO_ERROR) goto Exit;
1176
Steve Block3856b092011-10-20 11:56:00 +01001177 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001178 player->start();
1179
Steve Block3856b092011-10-20 11:56:00 +01001180 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001181 cache->wait();
1182 // in case of error, return what was successfully decoded.
1183 if (cache->size() == 0) {
1184 goto Exit;
1185 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001186
1187 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1188 *pSampleRate = cache->sampleRate();
1189 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001190 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001191 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 -08001192
1193Exit:
1194 if (player != 0) player->reset();
1195 return mem;
1196}
1197
Glenn Kastene1c39622012-01-04 09:36:37 -08001198sp<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 -08001199{
Steve Block3856b092011-10-20 11:56:00 +01001200 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 sp<MemoryBase> mem;
1202 sp<MediaPlayerBase> player;
1203
John Grossman44a7e422012-06-21 17:29:24 -07001204 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1205 fd,
1206 offset,
1207 length);
Steve Block3856b092011-10-20 11:56:00 +01001208 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209
1210 // create the right type of player
1211 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001212 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 if (player == NULL) goto Exit;
1214 if (player->hardwareOutput()) goto Exit;
1215
1216 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1217
1218 // set data source
1219 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1220
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001222 player->prepareAsync();
1223
Steve Block3856b092011-10-20 11:56:00 +01001224 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001225 if (cache->wait() != NO_ERROR) goto Exit;
1226
Steve Block3856b092011-10-20 11:56:00 +01001227 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228 player->start();
1229
Steve Block3856b092011-10-20 11:56:00 +01001230 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001231 cache->wait();
1232 // in case of error, return what was successfully decoded.
1233 if (cache->size() == 0) {
1234 goto Exit;
1235 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001236
1237 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1238 *pSampleRate = cache->sampleRate();
1239 *pNumChannels = cache->channelCount();
1240 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001241 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 -08001242
1243Exit:
1244 if (player != 0) player->reset();
1245 ::close(fd);
1246 return mem;
1247}
1248
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001249
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250#undef LOG_TAG
1251#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001252MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001253 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001254 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001255 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001256 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001257 mSessionId(sessionId),
1258 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001259 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001261 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001262 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263 mLeftVolume = 1.0;
1264 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001265 mPlaybackRatePermille = 1000;
1266 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001267 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001268 mAuxEffectId = 0;
1269 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270 setMinBufferCount();
1271}
1272
1273MediaPlayerService::AudioOutput::~AudioOutput()
1274{
1275 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001276 delete mRecycledTrack;
1277 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278}
1279
1280void MediaPlayerService::AudioOutput::setMinBufferCount()
1281{
1282 char value[PROPERTY_VALUE_MAX];
1283 if (property_get("ro.kernel.qemu", value, 0)) {
1284 mIsOnEmulator = true;
1285 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1286 }
1287}
1288
1289bool MediaPlayerService::AudioOutput::isOnEmulator()
1290{
1291 setMinBufferCount();
1292 return mIsOnEmulator;
1293}
1294
1295int MediaPlayerService::AudioOutput::getMinBufferCount()
1296{
1297 setMinBufferCount();
1298 return mMinBufferCount;
1299}
1300
1301ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1302{
1303 if (mTrack == 0) return NO_INIT;
1304 return mTrack->frameCount() * frameSize();
1305}
1306
1307ssize_t MediaPlayerService::AudioOutput::frameCount() const
1308{
1309 if (mTrack == 0) return NO_INIT;
1310 return mTrack->frameCount();
1311}
1312
1313ssize_t MediaPlayerService::AudioOutput::channelCount() const
1314{
1315 if (mTrack == 0) return NO_INIT;
1316 return mTrack->channelCount();
1317}
1318
1319ssize_t MediaPlayerService::AudioOutput::frameSize() const
1320{
1321 if (mTrack == 0) return NO_INIT;
1322 return mTrack->frameSize();
1323}
1324
1325uint32_t MediaPlayerService::AudioOutput::latency () const
1326{
Eric Laurentdb354e52012-03-05 17:27:11 -08001327 if (mTrack == 0) return 0;
1328 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001329}
1330
1331float MediaPlayerService::AudioOutput::msecsPerFrame() const
1332{
1333 return mMsecsPerFrame;
1334}
1335
Marco Nelissen4110c102012-03-29 09:31:28 -07001336status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001337{
1338 if (mTrack == 0) return NO_INIT;
1339 return mTrack->getPosition(position);
1340}
1341
Marco Nelissen4110c102012-03-29 09:31:28 -07001342status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1343{
1344 if (mTrack == 0) return NO_INIT;
1345 *frameswritten = mBytesWritten / frameSize();
1346 return OK;
1347}
1348
Andreas Huber20111aa2009-07-14 16:56:47 -07001349status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001350 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1351 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001352 AudioCallback cb, void *cookie,
1353 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001354{
Andreas Huber20111aa2009-07-14 16:56:47 -07001355 mCallback = cb;
1356 mCallbackCookie = cookie;
1357
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001358 // Check argument "bufferCount" against the mininum buffer count
1359 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001360 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001361 bufferCount = mMinBufferCount;
1362
1363 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001364 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1365 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001366 int afSampleRate;
1367 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001368 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001369
1370 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1371 return NO_INIT;
1372 }
1373 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1374 return NO_INIT;
1375 }
1376
1377 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001378
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001379 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001380 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001381 if (0 == channelMask) {
1382 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1383 return NO_INIT;
1384 }
1385 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001386
Andreas Huber20111aa2009-07-14 16:56:47 -07001387 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001388 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001389 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001390 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001391 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001392 mStreamType,
1393 sampleRate,
1394 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001395 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001396 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001397 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001398 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001399 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001400 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001401 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001402 } else {
1403 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001404 mStreamType,
1405 sampleRate,
1406 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001407 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001408 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001409 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001410 NULL,
1411 NULL,
1412 0,
1413 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001414 }
1415
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001416 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001417 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001418 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001419 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 return NO_INIT;
1421 }
1422
Marco Nelissen67295b52012-06-11 14:52:53 -07001423
1424 if (mRecycledTrack) {
1425 // check if the existing track can be reused as-is, or if a new track needs to be created.
1426
1427 bool reuse = true;
1428 if ((mCallbackData == NULL && mCallback != NULL) ||
1429 (mCallbackData != NULL && mCallback == NULL)) {
1430 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1431 ALOGV("can't chain callback and write");
1432 reuse = false;
1433 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1434 (mRecycledTrack->channelCount() != channelCount) ||
1435 (mRecycledTrack->frameCount() != t->frameCount())) {
1436 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1437 mRecycledTrack->getSampleRate(), sampleRate,
1438 mRecycledTrack->channelCount(), channelCount,
1439 mRecycledTrack->frameCount(), t->frameCount());
1440 reuse = false;
1441 } else if (flags != mFlags) {
1442 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1443 reuse = false;
1444 }
1445 if (reuse) {
1446 ALOGV("chaining to next output");
1447 close();
1448 mTrack = mRecycledTrack;
1449 mRecycledTrack = NULL;
1450 if (mCallbackData != NULL) {
1451 mCallbackData->setOutput(this);
1452 }
1453 delete t;
1454 delete newcbd;
1455 return OK;
1456 }
1457
1458 // if we're not going to reuse the track, unblock and flush it
1459 if (mCallbackData != NULL) {
1460 mCallbackData->setOutput(NULL);
1461 mCallbackData->endTrackSwitch();
1462 }
1463 mRecycledTrack->flush();
1464 delete mRecycledTrack;
1465 mRecycledTrack = NULL;
1466 delete mCallbackData;
1467 mCallbackData = NULL;
1468 close();
1469 }
1470
1471 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001472 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001474
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001475 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001476 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001477 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001478 uint32_t pos;
1479 if (t->getPosition(&pos) == OK) {
1480 mBytesWritten = uint64_t(pos) * t->frameSize();
1481 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001482 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001483
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001484 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1485 if (res != NO_ERROR) {
1486 return res;
1487 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001488 t->setAuxEffectSendLevel(mSendLevel);
1489 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001490}
1491
1492void MediaPlayerService::AudioOutput::start()
1493{
Steve Block3856b092011-10-20 11:56:00 +01001494 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001495 if (mCallbackData != NULL) {
1496 mCallbackData->endTrackSwitch();
1497 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001498 if (mTrack) {
1499 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001500 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001501 mTrack->start();
1502 }
1503}
1504
Marco Nelissen6b74d672012-02-28 16:07:44 -08001505void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1506 mNextOutput = nextOutput;
1507}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001508
1509
Marco Nelissen6b74d672012-02-28 16:07:44 -08001510void MediaPlayerService::AudioOutput::switchToNextOutput() {
1511 ALOGV("switchToNextOutput");
1512 if (mNextOutput != NULL) {
1513 if (mCallbackData != NULL) {
1514 mCallbackData->beginTrackSwitch();
1515 }
1516 delete mNextOutput->mCallbackData;
1517 mNextOutput->mCallbackData = mCallbackData;
1518 mCallbackData = NULL;
1519 mNextOutput->mRecycledTrack = mTrack;
1520 mTrack = NULL;
1521 mNextOutput->mSampleRateHz = mSampleRateHz;
1522 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001523 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001524 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001525 }
1526}
1527
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1529{
Andreas Huber20111aa2009-07-14 16:56:47 -07001530 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1531
Steve Block3856b092011-10-20 11:56:00 +01001532 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001533 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001534 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001535 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001536 return ret;
1537 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001538 return NO_INIT;
1539}
1540
1541void MediaPlayerService::AudioOutput::stop()
1542{
Steve Block3856b092011-10-20 11:56:00 +01001543 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001544 if (mTrack) mTrack->stop();
1545}
1546
1547void MediaPlayerService::AudioOutput::flush()
1548{
Steve Block3856b092011-10-20 11:56:00 +01001549 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001550 if (mTrack) mTrack->flush();
1551}
1552
1553void MediaPlayerService::AudioOutput::pause()
1554{
Steve Block3856b092011-10-20 11:56:00 +01001555 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001556 if (mTrack) mTrack->pause();
1557}
1558
1559void MediaPlayerService::AudioOutput::close()
1560{
Steve Block3856b092011-10-20 11:56:00 +01001561 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001562 delete mTrack;
1563 mTrack = 0;
1564}
1565
1566void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1567{
Steve Block3856b092011-10-20 11:56:00 +01001568 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001569 mLeftVolume = left;
1570 mRightVolume = right;
1571 if (mTrack) {
1572 mTrack->setVolume(left, right);
1573 }
1574}
1575
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001576status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1577{
1578 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1579 status_t res = NO_ERROR;
1580 if (mTrack) {
1581 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1582 } else {
1583 res = NO_INIT;
1584 }
1585 mPlaybackRatePermille = ratePermille;
1586 if (mSampleRateHz != 0) {
1587 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1588 }
1589 return res;
1590}
1591
Eric Laurent2beeb502010-07-16 07:43:46 -07001592status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1593{
Steve Block3856b092011-10-20 11:56:00 +01001594 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001595 mSendLevel = level;
1596 if (mTrack) {
1597 return mTrack->setAuxEffectSendLevel(level);
1598 }
1599 return NO_ERROR;
1600}
1601
1602status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1603{
Steve Block3856b092011-10-20 11:56:00 +01001604 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001605 mAuxEffectId = effectId;
1606 if (mTrack) {
1607 return mTrack->attachAuxEffect(effectId);
1608 }
1609 return NO_ERROR;
1610}
1611
Andreas Huber20111aa2009-07-14 16:56:47 -07001612// static
1613void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001614 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001615 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001616 if (event != AudioTrack::EVENT_MORE_DATA) {
1617 return;
1618 }
1619
Marco Nelissen6b74d672012-02-28 16:07:44 -08001620 CallbackData *data = (CallbackData*)cookie;
1621 data->lock();
1622 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001623 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001624 if (me == NULL) {
1625 // no output set, likely because the track was scheduled to be reused
1626 // by another player, but the format turned out to be incompatible.
1627 data->unlock();
1628 buffer->size = 0;
1629 return;
1630 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001631
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001632 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001633 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001634
Marco Nelissen6b74d672012-02-28 16:07:44 -08001635 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001636 // We've reached EOS but the audio track is not stopped yet,
1637 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001638
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001639 memset(buffer->raw, 0, buffer->size);
1640 actualSize = buffer->size;
1641 }
1642
1643 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001644 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001645}
1646
Marco Nelissen4110c102012-03-29 09:31:28 -07001647int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001648{
1649 return mSessionId;
1650}
1651
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001652#undef LOG_TAG
1653#define LOG_TAG "AudioCache"
1654MediaPlayerService::AudioCache::AudioCache(const char* name) :
1655 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1656 mError(NO_ERROR), mCommandComplete(false)
1657{
1658 // create ashmem heap
1659 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1660}
1661
1662uint32_t MediaPlayerService::AudioCache::latency () const
1663{
1664 return 0;
1665}
1666
1667float MediaPlayerService::AudioCache::msecsPerFrame() const
1668{
1669 return mMsecsPerFrame;
1670}
1671
Marco Nelissen4110c102012-03-29 09:31:28 -07001672status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001673{
1674 if (position == 0) return BAD_VALUE;
1675 *position = mSize;
1676 return NO_ERROR;
1677}
1678
Marco Nelissen4110c102012-03-29 09:31:28 -07001679status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1680{
1681 if (written == 0) return BAD_VALUE;
1682 *written = mSize;
1683 return NO_ERROR;
1684}
1685
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001686////////////////////////////////////////////////////////////////////////////////
1687
1688struct CallbackThread : public Thread {
1689 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1690 MediaPlayerBase::AudioSink::AudioCallback cb,
1691 void *cookie);
1692
1693protected:
1694 virtual ~CallbackThread();
1695
1696 virtual bool threadLoop();
1697
1698private:
1699 wp<MediaPlayerBase::AudioSink> mSink;
1700 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1701 void *mCookie;
1702 void *mBuffer;
1703 size_t mBufferSize;
1704
1705 CallbackThread(const CallbackThread &);
1706 CallbackThread &operator=(const CallbackThread &);
1707};
1708
1709CallbackThread::CallbackThread(
1710 const wp<MediaPlayerBase::AudioSink> &sink,
1711 MediaPlayerBase::AudioSink::AudioCallback cb,
1712 void *cookie)
1713 : mSink(sink),
1714 mCallback(cb),
1715 mCookie(cookie),
1716 mBuffer(NULL),
1717 mBufferSize(0) {
1718}
1719
1720CallbackThread::~CallbackThread() {
1721 if (mBuffer) {
1722 free(mBuffer);
1723 mBuffer = NULL;
1724 }
1725}
1726
1727bool CallbackThread::threadLoop() {
1728 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1729 if (sink == NULL) {
1730 return false;
1731 }
1732
1733 if (mBuffer == NULL) {
1734 mBufferSize = sink->bufferSize();
1735 mBuffer = malloc(mBufferSize);
1736 }
1737
1738 size_t actualSize =
1739 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1740
1741 if (actualSize > 0) {
1742 sink->write(mBuffer, actualSize);
1743 }
1744
1745 return true;
1746}
1747
1748////////////////////////////////////////////////////////////////////////////////
1749
Andreas Huber20111aa2009-07-14 16:56:47 -07001750status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001751 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1752 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001753 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001754{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001755 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001756 if (mHeap->getHeapID() < 0) {
1757 return NO_INIT;
1758 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001759
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001760 mSampleRate = sampleRate;
1761 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001762 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001763 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001764
1765 if (cb != NULL) {
1766 mCallbackThread = new CallbackThread(this, cb, cookie);
1767 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001768 return NO_ERROR;
1769}
1770
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001771void MediaPlayerService::AudioCache::start() {
1772 if (mCallbackThread != NULL) {
1773 mCallbackThread->run("AudioCache callback");
1774 }
1775}
1776
1777void MediaPlayerService::AudioCache::stop() {
1778 if (mCallbackThread != NULL) {
1779 mCallbackThread->requestExitAndWait();
1780 }
1781}
1782
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001783ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1784{
Steve Block3856b092011-10-20 11:56:00 +01001785 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001786 if ((buffer == 0) || (size == 0)) return size;
1787
1788 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1789 if (p == NULL) return NO_INIT;
1790 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001791 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001792 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001793 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001794 size = mHeap->getSize() - mSize;
1795 }
1796 memcpy(p, buffer, size);
1797 mSize += size;
1798 return size;
1799}
1800
1801// call with lock held
1802status_t MediaPlayerService::AudioCache::wait()
1803{
1804 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001805 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806 mSignal.wait(mLock);
1807 }
1808 mCommandComplete = false;
1809
1810 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001811 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812 } else {
Steve Block3856b092011-10-20 11:56:00 +01001813 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001814 }
1815 return mError;
1816}
1817
Gloria Wangb483c472011-04-11 17:23:27 -07001818void MediaPlayerService::AudioCache::notify(
1819 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001820{
Steve Block3856b092011-10-20 11:56:00 +01001821 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001822 AudioCache* p = static_cast<AudioCache*>(cookie);
1823
1824 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001825 switch (msg)
1826 {
1827 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001828 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001830 break;
1831 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001832 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001833 break;
1834 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001835 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001836 break;
1837 default:
Steve Block3856b092011-10-20 11:56:00 +01001838 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001839 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001840 }
1841
1842 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001843 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001844 p->mCommandComplete = true;
1845 p->mSignal.signal();
1846}
1847
Marco Nelissen4110c102012-03-29 09:31:28 -07001848int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001849{
1850 return 0;
1851}
1852
Gloria Wang7cf180c2011-02-19 18:37:57 -08001853void MediaPlayerService::addBatteryData(uint32_t params)
1854{
1855 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001856
1857 int32_t time = systemTime() / 1000000L;
1858
1859 // change audio output devices. This notification comes from AudioFlinger
1860 if ((params & kBatteryDataSpeakerOn)
1861 || (params & kBatteryDataOtherAudioDeviceOn)) {
1862
1863 int deviceOn[NUM_AUDIO_DEVICES];
1864 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1865 deviceOn[i] = 0;
1866 }
1867
1868 if ((params & kBatteryDataSpeakerOn)
1869 && (params & kBatteryDataOtherAudioDeviceOn)) {
1870 deviceOn[SPEAKER_AND_OTHER] = 1;
1871 } else if (params & kBatteryDataSpeakerOn) {
1872 deviceOn[SPEAKER] = 1;
1873 } else {
1874 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1875 }
1876
1877 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1878 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1879
1880 if (mBatteryAudio.refCount > 0) { // if playing audio
1881 if (!deviceOn[i]) {
1882 mBatteryAudio.lastTime[i] += time;
1883 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1884 mBatteryAudio.lastTime[i] = 0;
1885 } else {
1886 mBatteryAudio.lastTime[i] = 0 - time;
1887 }
1888 }
1889
1890 mBatteryAudio.deviceOn[i] = deviceOn[i];
1891 }
1892 }
1893 return;
1894 }
1895
1896 // an sudio stream is started
1897 if (params & kBatteryDataAudioFlingerStart) {
1898 // record the start time only if currently no other audio
1899 // is being played
1900 if (mBatteryAudio.refCount == 0) {
1901 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1902 if (mBatteryAudio.deviceOn[i]) {
1903 mBatteryAudio.lastTime[i] -= time;
1904 }
1905 }
1906 }
1907
1908 mBatteryAudio.refCount ++;
1909 return;
1910
1911 } else if (params & kBatteryDataAudioFlingerStop) {
1912 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001913 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001914 return;
1915 }
1916
1917 // record the stop time only if currently this is the only
1918 // audio being played
1919 if (mBatteryAudio.refCount == 1) {
1920 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1921 if (mBatteryAudio.deviceOn[i]) {
1922 mBatteryAudio.lastTime[i] += time;
1923 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1924 mBatteryAudio.lastTime[i] = 0;
1925 }
1926 }
1927 }
1928
1929 mBatteryAudio.refCount --;
1930 return;
1931 }
1932
Gloria Wang7cf180c2011-02-19 18:37:57 -08001933 int uid = IPCThreadState::self()->getCallingUid();
1934 if (uid == AID_MEDIA) {
1935 return;
1936 }
1937 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001938
1939 if (index < 0) { // create a new entry for this UID
1940 BatteryUsageInfo info;
1941 info.audioTotalTime = 0;
1942 info.videoTotalTime = 0;
1943 info.audioLastTime = 0;
1944 info.videoLastTime = 0;
1945 info.refCount = 0;
1946
Gloria Wang9ee159b2011-02-24 14:51:45 -08001947 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001948 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001949 return;
1950 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001951 }
1952
1953 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1954
1955 if (params & kBatteryDataCodecStarted) {
1956 if (params & kBatteryDataTrackAudio) {
1957 info.audioLastTime -= time;
1958 info.refCount ++;
1959 }
1960 if (params & kBatteryDataTrackVideo) {
1961 info.videoLastTime -= time;
1962 info.refCount ++;
1963 }
1964 } else {
1965 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001966 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001967 return;
1968 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001969 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001970 mBatteryData.removeItem(uid);
1971 return;
1972 }
1973
1974 if (params & kBatteryDataTrackAudio) {
1975 info.audioLastTime += time;
1976 info.refCount --;
1977 }
1978 if (params & kBatteryDataTrackVideo) {
1979 info.videoLastTime += time;
1980 info.refCount --;
1981 }
1982
1983 // no stream is being played by this UID
1984 if (info.refCount == 0) {
1985 info.audioTotalTime += info.audioLastTime;
1986 info.audioLastTime = 0;
1987 info.videoTotalTime += info.videoLastTime;
1988 info.videoLastTime = 0;
1989 }
1990 }
1991}
1992
1993status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
1994 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001995
1996 // audio output devices usage
1997 int32_t time = systemTime() / 1000000L; //in ms
1998 int32_t totalTime;
1999
2000 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2001 totalTime = mBatteryAudio.totalTime[i];
2002
2003 if (mBatteryAudio.deviceOn[i]
2004 && (mBatteryAudio.lastTime[i] != 0)) {
2005 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2006 totalTime += tmpTime;
2007 }
2008
2009 reply->writeInt32(totalTime);
2010 // reset the total time
2011 mBatteryAudio.totalTime[i] = 0;
2012 }
2013
2014 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002015 BatteryUsageInfo info;
2016 int size = mBatteryData.size();
2017
2018 reply->writeInt32(size);
2019 int i = 0;
2020
2021 while (i < size) {
2022 info = mBatteryData.valueAt(i);
2023
2024 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2025 reply->writeInt32(info.audioTotalTime);
2026 reply->writeInt32(info.videoTotalTime);
2027
2028 info.audioTotalTime = 0;
2029 info.videoTotalTime = 0;
2030
2031 // remove the UID entry where no stream is being played
2032 if (info.refCount <= 0) {
2033 mBatteryData.removeItemsAt(i);
2034 size --;
2035 i --;
2036 }
2037 i++;
2038 }
2039 return NO_ERROR;
2040}
nikoa64c8c72009-07-20 15:07:26 -07002041} // namespace android