blob: 8620856b59a253f3282c71aac8a936cd4823a102 [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);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001045
Marco Nelissen6b74d672012-02-28 16:07:44 -08001046 {
1047 Mutex::Autolock l(client->mLock);
1048 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
1049 client->mAudioOutput->switchToNextOutput();
1050 client->mNextClient->start();
1051 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1052 }
1053 }
1054
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001055 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001056 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001057 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001058
1059 if(client->shouldDropMetadata(metadata_type)) {
1060 return;
1061 }
1062
1063 // Update the list of metadata that have changed. getMetadata
1064 // also access mMetadataUpdated and clears it.
1065 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001066 }
Steve Block3856b092011-10-20 11:56:00 +01001067 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
Gloria Wangb483c472011-04-11 17:23:27 -07001068 client->mClient->notify(msg, ext1, ext2, obj);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001069}
1070
Nicolas Catania48290382009-07-10 13:53:06 -07001071
nikoa64c8c72009-07-20 15:07:26 -07001072bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001073{
Nicolas Catania48290382009-07-10 13:53:06 -07001074 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001075
Nicolas Catania48290382009-07-10 13:53:06 -07001076 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001077 return true;
1078 }
1079
Nicolas Catania48290382009-07-10 13:53:06 -07001080 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001081 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001082 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001083 return true;
1084 }
1085}
1086
Nicolas Catania48290382009-07-10 13:53:06 -07001087
nikoa64c8c72009-07-20 15:07:26 -07001088void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001089 Mutex::Autolock lock(mLock);
1090 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1091 mMetadataUpdated.add(metadata_type);
1092 }
1093}
1094
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001095#if CALLBACK_ANTAGONIZER
1096const int Antagonizer::interval = 10000; // 10 msecs
1097
1098Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1099 mExit(false), mActive(false), mClient(client), mCb(cb)
1100{
1101 createThread(callbackThread, this);
1102}
1103
1104void Antagonizer::kill()
1105{
1106 Mutex::Autolock _l(mLock);
1107 mActive = false;
1108 mExit = true;
1109 mCondition.wait(mLock);
1110}
1111
1112int Antagonizer::callbackThread(void* user)
1113{
Steve Blockb8a80522011-12-20 16:23:08 +00001114 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001115 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1116 while (!p->mExit) {
1117 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001118 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001119 p->mCb(p->mClient, 0, 0, 0);
1120 }
1121 usleep(interval);
1122 }
1123 Mutex::Autolock _l(p->mLock);
1124 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001125 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001126 return 0;
1127}
1128#endif
1129
1130static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1131
Glenn Kastene1c39622012-01-04 09:36:37 -08001132sp<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 -08001133{
Steve Block3856b092011-10-20 11:56:00 +01001134 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001135 sp<MemoryBase> mem;
1136 sp<MediaPlayerBase> player;
1137
1138 // Protect our precious, precious DRMd ringtones by only allowing
1139 // decoding of http, but not filesystem paths or content Uris.
1140 // If the application wants to decode those, it should open a
1141 // filedescriptor for them and use that.
1142 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001143 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001144 return mem;
1145 }
1146
John Grossman44a7e422012-06-21 17:29:24 -07001147 player_type playerType =
1148 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001149 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001150
1151 // create the right type of player
1152 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001153 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001154 if (player == NULL) goto Exit;
1155 if (player->hardwareOutput()) goto Exit;
1156
1157 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1158
1159 // set data source
1160 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1161
Steve Block3856b092011-10-20 11:56:00 +01001162 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001163 player->prepareAsync();
1164
Steve Block3856b092011-10-20 11:56:00 +01001165 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001166 if (cache->wait() != NO_ERROR) goto Exit;
1167
Steve Block3856b092011-10-20 11:56:00 +01001168 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001169 player->start();
1170
Steve Block3856b092011-10-20 11:56:00 +01001171 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001172 cache->wait();
1173 // in case of error, return what was successfully decoded.
1174 if (cache->size() == 0) {
1175 goto Exit;
1176 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001177
1178 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1179 *pSampleRate = cache->sampleRate();
1180 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001181 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001182 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 -08001183
1184Exit:
1185 if (player != 0) player->reset();
1186 return mem;
1187}
1188
Glenn Kastene1c39622012-01-04 09:36:37 -08001189sp<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 -08001190{
Steve Block3856b092011-10-20 11:56:00 +01001191 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001192 sp<MemoryBase> mem;
1193 sp<MediaPlayerBase> player;
1194
John Grossman44a7e422012-06-21 17:29:24 -07001195 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1196 fd,
1197 offset,
1198 length);
Steve Block3856b092011-10-20 11:56:00 +01001199 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001200
1201 // create the right type of player
1202 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001203 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204 if (player == NULL) goto Exit;
1205 if (player->hardwareOutput()) goto Exit;
1206
1207 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1208
1209 // set data source
1210 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1211
Steve Block3856b092011-10-20 11:56:00 +01001212 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 player->prepareAsync();
1214
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 if (cache->wait() != NO_ERROR) goto Exit;
1217
Steve Block3856b092011-10-20 11:56:00 +01001218 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001219 player->start();
1220
Steve Block3856b092011-10-20 11:56:00 +01001221 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001222 cache->wait();
1223 // in case of error, return what was successfully decoded.
1224 if (cache->size() == 0) {
1225 goto Exit;
1226 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001227
1228 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1229 *pSampleRate = cache->sampleRate();
1230 *pNumChannels = cache->channelCount();
1231 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001232 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 -08001233
1234Exit:
1235 if (player != 0) player->reset();
1236 ::close(fd);
1237 return mem;
1238}
1239
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001240
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001241#undef LOG_TAG
1242#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001243MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001244 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001245 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001246 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001247 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001248 mSessionId(sessionId),
1249 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001250 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001252 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001253 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254 mLeftVolume = 1.0;
1255 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001256 mPlaybackRatePermille = 1000;
1257 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001258 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001259 mAuxEffectId = 0;
1260 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001261 setMinBufferCount();
1262}
1263
1264MediaPlayerService::AudioOutput::~AudioOutput()
1265{
1266 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001267 delete mRecycledTrack;
1268 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269}
1270
1271void MediaPlayerService::AudioOutput::setMinBufferCount()
1272{
1273 char value[PROPERTY_VALUE_MAX];
1274 if (property_get("ro.kernel.qemu", value, 0)) {
1275 mIsOnEmulator = true;
1276 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1277 }
1278}
1279
1280bool MediaPlayerService::AudioOutput::isOnEmulator()
1281{
1282 setMinBufferCount();
1283 return mIsOnEmulator;
1284}
1285
1286int MediaPlayerService::AudioOutput::getMinBufferCount()
1287{
1288 setMinBufferCount();
1289 return mMinBufferCount;
1290}
1291
1292ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1293{
1294 if (mTrack == 0) return NO_INIT;
1295 return mTrack->frameCount() * frameSize();
1296}
1297
1298ssize_t MediaPlayerService::AudioOutput::frameCount() const
1299{
1300 if (mTrack == 0) return NO_INIT;
1301 return mTrack->frameCount();
1302}
1303
1304ssize_t MediaPlayerService::AudioOutput::channelCount() const
1305{
1306 if (mTrack == 0) return NO_INIT;
1307 return mTrack->channelCount();
1308}
1309
1310ssize_t MediaPlayerService::AudioOutput::frameSize() const
1311{
1312 if (mTrack == 0) return NO_INIT;
1313 return mTrack->frameSize();
1314}
1315
1316uint32_t MediaPlayerService::AudioOutput::latency () const
1317{
Eric Laurentdb354e52012-03-05 17:27:11 -08001318 if (mTrack == 0) return 0;
1319 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001320}
1321
1322float MediaPlayerService::AudioOutput::msecsPerFrame() const
1323{
1324 return mMsecsPerFrame;
1325}
1326
Marco Nelissen4110c102012-03-29 09:31:28 -07001327status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001328{
1329 if (mTrack == 0) return NO_INIT;
1330 return mTrack->getPosition(position);
1331}
1332
Marco Nelissen4110c102012-03-29 09:31:28 -07001333status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1334{
1335 if (mTrack == 0) return NO_INIT;
1336 *frameswritten = mBytesWritten / frameSize();
1337 return OK;
1338}
1339
Andreas Huber20111aa2009-07-14 16:56:47 -07001340status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001341 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1342 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001343 AudioCallback cb, void *cookie,
1344 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001345{
Andreas Huber20111aa2009-07-14 16:56:47 -07001346 mCallback = cb;
1347 mCallbackCookie = cookie;
1348
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001349 // Check argument "bufferCount" against the mininum buffer count
1350 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001351 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001352 bufferCount = mMinBufferCount;
1353
1354 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001355 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1356 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001357 int afSampleRate;
1358 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001359 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001360
1361 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1362 return NO_INIT;
1363 }
1364 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1365 return NO_INIT;
1366 }
1367
1368 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001369
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001370 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001371 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001372 if (0 == channelMask) {
1373 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1374 return NO_INIT;
1375 }
1376 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001377
Andreas Huber20111aa2009-07-14 16:56:47 -07001378 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001379 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001380 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001381 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001382 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001383 mStreamType,
1384 sampleRate,
1385 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001386 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001387 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001388 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001389 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001390 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001391 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001392 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001393 } else {
1394 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001395 mStreamType,
1396 sampleRate,
1397 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001398 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001399 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001400 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001401 NULL,
1402 NULL,
1403 0,
1404 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001405 }
1406
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001407 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001408 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001409 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001410 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001411 return NO_INIT;
1412 }
1413
Marco Nelissen67295b52012-06-11 14:52:53 -07001414
1415 if (mRecycledTrack) {
1416 // check if the existing track can be reused as-is, or if a new track needs to be created.
1417
1418 bool reuse = true;
1419 if ((mCallbackData == NULL && mCallback != NULL) ||
1420 (mCallbackData != NULL && mCallback == NULL)) {
1421 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1422 ALOGV("can't chain callback and write");
1423 reuse = false;
1424 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1425 (mRecycledTrack->channelCount() != channelCount) ||
1426 (mRecycledTrack->frameCount() != t->frameCount())) {
1427 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1428 mRecycledTrack->getSampleRate(), sampleRate,
1429 mRecycledTrack->channelCount(), channelCount,
1430 mRecycledTrack->frameCount(), t->frameCount());
1431 reuse = false;
1432 } else if (flags != mFlags) {
1433 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1434 reuse = false;
1435 }
1436 if (reuse) {
1437 ALOGV("chaining to next output");
1438 close();
1439 mTrack = mRecycledTrack;
1440 mRecycledTrack = NULL;
1441 if (mCallbackData != NULL) {
1442 mCallbackData->setOutput(this);
1443 }
1444 delete t;
1445 delete newcbd;
1446 return OK;
1447 }
1448
1449 // if we're not going to reuse the track, unblock and flush it
1450 if (mCallbackData != NULL) {
1451 mCallbackData->setOutput(NULL);
1452 mCallbackData->endTrackSwitch();
1453 }
1454 mRecycledTrack->flush();
1455 delete mRecycledTrack;
1456 mRecycledTrack = NULL;
1457 delete mCallbackData;
1458 mCallbackData = NULL;
1459 close();
1460 }
1461
1462 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001463 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001464 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001465
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001466 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001467 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001468 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001469 uint32_t pos;
1470 if (t->getPosition(&pos) == OK) {
1471 mBytesWritten = uint64_t(pos) * t->frameSize();
1472 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001474
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001475 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1476 if (res != NO_ERROR) {
1477 return res;
1478 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001479 t->setAuxEffectSendLevel(mSendLevel);
1480 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001481}
1482
1483void MediaPlayerService::AudioOutput::start()
1484{
Steve Block3856b092011-10-20 11:56:00 +01001485 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001486 if (mCallbackData != NULL) {
1487 mCallbackData->endTrackSwitch();
1488 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001489 if (mTrack) {
1490 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001491 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001492 mTrack->start();
1493 }
1494}
1495
Marco Nelissen6b74d672012-02-28 16:07:44 -08001496void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1497 mNextOutput = nextOutput;
1498}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001499
1500
Marco Nelissen6b74d672012-02-28 16:07:44 -08001501void MediaPlayerService::AudioOutput::switchToNextOutput() {
1502 ALOGV("switchToNextOutput");
1503 if (mNextOutput != NULL) {
1504 if (mCallbackData != NULL) {
1505 mCallbackData->beginTrackSwitch();
1506 }
1507 delete mNextOutput->mCallbackData;
1508 mNextOutput->mCallbackData = mCallbackData;
1509 mCallbackData = NULL;
1510 mNextOutput->mRecycledTrack = mTrack;
1511 mTrack = NULL;
1512 mNextOutput->mSampleRateHz = mSampleRateHz;
1513 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001514 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001515 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001516 }
1517}
1518
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001519ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1520{
Andreas Huber20111aa2009-07-14 16:56:47 -07001521 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1522
Steve Block3856b092011-10-20 11:56:00 +01001523 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001524 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001525 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001526 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001527 return ret;
1528 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001529 return NO_INIT;
1530}
1531
1532void MediaPlayerService::AudioOutput::stop()
1533{
Steve Block3856b092011-10-20 11:56:00 +01001534 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001535 if (mTrack) mTrack->stop();
1536}
1537
1538void MediaPlayerService::AudioOutput::flush()
1539{
Steve Block3856b092011-10-20 11:56:00 +01001540 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001541 if (mTrack) mTrack->flush();
1542}
1543
1544void MediaPlayerService::AudioOutput::pause()
1545{
Steve Block3856b092011-10-20 11:56:00 +01001546 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001547 if (mTrack) mTrack->pause();
1548}
1549
1550void MediaPlayerService::AudioOutput::close()
1551{
Steve Block3856b092011-10-20 11:56:00 +01001552 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001553 delete mTrack;
1554 mTrack = 0;
1555}
1556
1557void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1558{
Steve Block3856b092011-10-20 11:56:00 +01001559 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001560 mLeftVolume = left;
1561 mRightVolume = right;
1562 if (mTrack) {
1563 mTrack->setVolume(left, right);
1564 }
1565}
1566
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001567status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1568{
1569 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1570 status_t res = NO_ERROR;
1571 if (mTrack) {
1572 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1573 } else {
1574 res = NO_INIT;
1575 }
1576 mPlaybackRatePermille = ratePermille;
1577 if (mSampleRateHz != 0) {
1578 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1579 }
1580 return res;
1581}
1582
Eric Laurent2beeb502010-07-16 07:43:46 -07001583status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1584{
Steve Block3856b092011-10-20 11:56:00 +01001585 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001586 mSendLevel = level;
1587 if (mTrack) {
1588 return mTrack->setAuxEffectSendLevel(level);
1589 }
1590 return NO_ERROR;
1591}
1592
1593status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1594{
Steve Block3856b092011-10-20 11:56:00 +01001595 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001596 mAuxEffectId = effectId;
1597 if (mTrack) {
1598 return mTrack->attachAuxEffect(effectId);
1599 }
1600 return NO_ERROR;
1601}
1602
Andreas Huber20111aa2009-07-14 16:56:47 -07001603// static
1604void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001605 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001606 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001607 if (event != AudioTrack::EVENT_MORE_DATA) {
1608 return;
1609 }
1610
Marco Nelissen6b74d672012-02-28 16:07:44 -08001611 CallbackData *data = (CallbackData*)cookie;
1612 data->lock();
1613 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001614 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001615 if (me == NULL) {
1616 // no output set, likely because the track was scheduled to be reused
1617 // by another player, but the format turned out to be incompatible.
1618 data->unlock();
1619 buffer->size = 0;
1620 return;
1621 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001622
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001623 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001624 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001625
Marco Nelissen6b74d672012-02-28 16:07:44 -08001626 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001627 // We've reached EOS but the audio track is not stopped yet,
1628 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001629
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001630 memset(buffer->raw, 0, buffer->size);
1631 actualSize = buffer->size;
1632 }
1633
1634 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001635 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001636}
1637
Marco Nelissen4110c102012-03-29 09:31:28 -07001638int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001639{
1640 return mSessionId;
1641}
1642
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001643#undef LOG_TAG
1644#define LOG_TAG "AudioCache"
1645MediaPlayerService::AudioCache::AudioCache(const char* name) :
1646 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1647 mError(NO_ERROR), mCommandComplete(false)
1648{
1649 // create ashmem heap
1650 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1651}
1652
1653uint32_t MediaPlayerService::AudioCache::latency () const
1654{
1655 return 0;
1656}
1657
1658float MediaPlayerService::AudioCache::msecsPerFrame() const
1659{
1660 return mMsecsPerFrame;
1661}
1662
Marco Nelissen4110c102012-03-29 09:31:28 -07001663status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001664{
1665 if (position == 0) return BAD_VALUE;
1666 *position = mSize;
1667 return NO_ERROR;
1668}
1669
Marco Nelissen4110c102012-03-29 09:31:28 -07001670status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1671{
1672 if (written == 0) return BAD_VALUE;
1673 *written = mSize;
1674 return NO_ERROR;
1675}
1676
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001677////////////////////////////////////////////////////////////////////////////////
1678
1679struct CallbackThread : public Thread {
1680 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1681 MediaPlayerBase::AudioSink::AudioCallback cb,
1682 void *cookie);
1683
1684protected:
1685 virtual ~CallbackThread();
1686
1687 virtual bool threadLoop();
1688
1689private:
1690 wp<MediaPlayerBase::AudioSink> mSink;
1691 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1692 void *mCookie;
1693 void *mBuffer;
1694 size_t mBufferSize;
1695
1696 CallbackThread(const CallbackThread &);
1697 CallbackThread &operator=(const CallbackThread &);
1698};
1699
1700CallbackThread::CallbackThread(
1701 const wp<MediaPlayerBase::AudioSink> &sink,
1702 MediaPlayerBase::AudioSink::AudioCallback cb,
1703 void *cookie)
1704 : mSink(sink),
1705 mCallback(cb),
1706 mCookie(cookie),
1707 mBuffer(NULL),
1708 mBufferSize(0) {
1709}
1710
1711CallbackThread::~CallbackThread() {
1712 if (mBuffer) {
1713 free(mBuffer);
1714 mBuffer = NULL;
1715 }
1716}
1717
1718bool CallbackThread::threadLoop() {
1719 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1720 if (sink == NULL) {
1721 return false;
1722 }
1723
1724 if (mBuffer == NULL) {
1725 mBufferSize = sink->bufferSize();
1726 mBuffer = malloc(mBufferSize);
1727 }
1728
1729 size_t actualSize =
1730 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1731
1732 if (actualSize > 0) {
1733 sink->write(mBuffer, actualSize);
1734 }
1735
1736 return true;
1737}
1738
1739////////////////////////////////////////////////////////////////////////////////
1740
Andreas Huber20111aa2009-07-14 16:56:47 -07001741status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001742 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1743 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001744 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001745{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001746 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001747 if (mHeap->getHeapID() < 0) {
1748 return NO_INIT;
1749 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001750
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001751 mSampleRate = sampleRate;
1752 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001753 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001754 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001755
1756 if (cb != NULL) {
1757 mCallbackThread = new CallbackThread(this, cb, cookie);
1758 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001759 return NO_ERROR;
1760}
1761
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001762void MediaPlayerService::AudioCache::start() {
1763 if (mCallbackThread != NULL) {
1764 mCallbackThread->run("AudioCache callback");
1765 }
1766}
1767
1768void MediaPlayerService::AudioCache::stop() {
1769 if (mCallbackThread != NULL) {
1770 mCallbackThread->requestExitAndWait();
1771 }
1772}
1773
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001774ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1775{
Steve Block3856b092011-10-20 11:56:00 +01001776 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001777 if ((buffer == 0) || (size == 0)) return size;
1778
1779 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1780 if (p == NULL) return NO_INIT;
1781 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001782 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001783 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001784 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001785 size = mHeap->getSize() - mSize;
1786 }
1787 memcpy(p, buffer, size);
1788 mSize += size;
1789 return size;
1790}
1791
1792// call with lock held
1793status_t MediaPlayerService::AudioCache::wait()
1794{
1795 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001796 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001797 mSignal.wait(mLock);
1798 }
1799 mCommandComplete = false;
1800
1801 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001802 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001803 } else {
Steve Block3856b092011-10-20 11:56:00 +01001804 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001805 }
1806 return mError;
1807}
1808
Gloria Wangb483c472011-04-11 17:23:27 -07001809void MediaPlayerService::AudioCache::notify(
1810 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001811{
Steve Block3856b092011-10-20 11:56:00 +01001812 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001813 AudioCache* p = static_cast<AudioCache*>(cookie);
1814
1815 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001816 switch (msg)
1817 {
1818 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001819 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001820 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001821 break;
1822 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001823 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001824 break;
1825 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001826 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001827 break;
1828 default:
Steve Block3856b092011-10-20 11:56:00 +01001829 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001830 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001831 }
1832
1833 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001834 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835 p->mCommandComplete = true;
1836 p->mSignal.signal();
1837}
1838
Marco Nelissen4110c102012-03-29 09:31:28 -07001839int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001840{
1841 return 0;
1842}
1843
Gloria Wang7cf180c2011-02-19 18:37:57 -08001844void MediaPlayerService::addBatteryData(uint32_t params)
1845{
1846 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001847
1848 int32_t time = systemTime() / 1000000L;
1849
1850 // change audio output devices. This notification comes from AudioFlinger
1851 if ((params & kBatteryDataSpeakerOn)
1852 || (params & kBatteryDataOtherAudioDeviceOn)) {
1853
1854 int deviceOn[NUM_AUDIO_DEVICES];
1855 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1856 deviceOn[i] = 0;
1857 }
1858
1859 if ((params & kBatteryDataSpeakerOn)
1860 && (params & kBatteryDataOtherAudioDeviceOn)) {
1861 deviceOn[SPEAKER_AND_OTHER] = 1;
1862 } else if (params & kBatteryDataSpeakerOn) {
1863 deviceOn[SPEAKER] = 1;
1864 } else {
1865 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1866 }
1867
1868 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1869 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1870
1871 if (mBatteryAudio.refCount > 0) { // if playing audio
1872 if (!deviceOn[i]) {
1873 mBatteryAudio.lastTime[i] += time;
1874 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1875 mBatteryAudio.lastTime[i] = 0;
1876 } else {
1877 mBatteryAudio.lastTime[i] = 0 - time;
1878 }
1879 }
1880
1881 mBatteryAudio.deviceOn[i] = deviceOn[i];
1882 }
1883 }
1884 return;
1885 }
1886
1887 // an sudio stream is started
1888 if (params & kBatteryDataAudioFlingerStart) {
1889 // record the start time only if currently no other audio
1890 // is being played
1891 if (mBatteryAudio.refCount == 0) {
1892 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1893 if (mBatteryAudio.deviceOn[i]) {
1894 mBatteryAudio.lastTime[i] -= time;
1895 }
1896 }
1897 }
1898
1899 mBatteryAudio.refCount ++;
1900 return;
1901
1902 } else if (params & kBatteryDataAudioFlingerStop) {
1903 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001904 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001905 return;
1906 }
1907
1908 // record the stop time only if currently this is the only
1909 // audio being played
1910 if (mBatteryAudio.refCount == 1) {
1911 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1912 if (mBatteryAudio.deviceOn[i]) {
1913 mBatteryAudio.lastTime[i] += time;
1914 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1915 mBatteryAudio.lastTime[i] = 0;
1916 }
1917 }
1918 }
1919
1920 mBatteryAudio.refCount --;
1921 return;
1922 }
1923
Gloria Wang7cf180c2011-02-19 18:37:57 -08001924 int uid = IPCThreadState::self()->getCallingUid();
1925 if (uid == AID_MEDIA) {
1926 return;
1927 }
1928 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001929
1930 if (index < 0) { // create a new entry for this UID
1931 BatteryUsageInfo info;
1932 info.audioTotalTime = 0;
1933 info.videoTotalTime = 0;
1934 info.audioLastTime = 0;
1935 info.videoLastTime = 0;
1936 info.refCount = 0;
1937
Gloria Wang9ee159b2011-02-24 14:51:45 -08001938 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001939 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001940 return;
1941 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001942 }
1943
1944 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1945
1946 if (params & kBatteryDataCodecStarted) {
1947 if (params & kBatteryDataTrackAudio) {
1948 info.audioLastTime -= time;
1949 info.refCount ++;
1950 }
1951 if (params & kBatteryDataTrackVideo) {
1952 info.videoLastTime -= time;
1953 info.refCount ++;
1954 }
1955 } else {
1956 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001957 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001958 return;
1959 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001960 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001961 mBatteryData.removeItem(uid);
1962 return;
1963 }
1964
1965 if (params & kBatteryDataTrackAudio) {
1966 info.audioLastTime += time;
1967 info.refCount --;
1968 }
1969 if (params & kBatteryDataTrackVideo) {
1970 info.videoLastTime += time;
1971 info.refCount --;
1972 }
1973
1974 // no stream is being played by this UID
1975 if (info.refCount == 0) {
1976 info.audioTotalTime += info.audioLastTime;
1977 info.audioLastTime = 0;
1978 info.videoTotalTime += info.videoLastTime;
1979 info.videoLastTime = 0;
1980 }
1981 }
1982}
1983
1984status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
1985 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001986
1987 // audio output devices usage
1988 int32_t time = systemTime() / 1000000L; //in ms
1989 int32_t totalTime;
1990
1991 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1992 totalTime = mBatteryAudio.totalTime[i];
1993
1994 if (mBatteryAudio.deviceOn[i]
1995 && (mBatteryAudio.lastTime[i] != 0)) {
1996 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
1997 totalTime += tmpTime;
1998 }
1999
2000 reply->writeInt32(totalTime);
2001 // reset the total time
2002 mBatteryAudio.totalTime[i] = 0;
2003 }
2004
2005 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002006 BatteryUsageInfo info;
2007 int size = mBatteryData.size();
2008
2009 reply->writeInt32(size);
2010 int i = 0;
2011
2012 while (i < size) {
2013 info = mBatteryData.valueAt(i);
2014
2015 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2016 reply->writeInt32(info.audioTotalTime);
2017 reply->writeInt32(info.videoTotalTime);
2018
2019 info.audioTotalTime = 0;
2020 info.videoTotalTime = 0;
2021
2022 // remove the UID entry where no stream is being played
2023 if (info.refCount <= 0) {
2024 mBatteryData.removeItemsAt(i);
2025 size --;
2026 i --;
2027 }
2028 i++;
2029 }
2030 return NO_ERROR;
2031}
nikoa64c8c72009-07-20 15:07:26 -07002032} // namespace android