blob: 58e472357ec7e1b0e225291b7ca01eb60395c84a [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) {
John Grossmancb0b7552012-08-23 17:47:31 -07001049 if (client->mAudioOutput != NULL)
1050 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001051 client->mNextClient->start();
1052 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1053 }
1054 }
1055
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001056 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001057 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001058 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001059
1060 if(client->shouldDropMetadata(metadata_type)) {
1061 return;
1062 }
1063
1064 // Update the list of metadata that have changed. getMetadata
1065 // also access mMetadataUpdated and clears it.
1066 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001067 }
Steve Block3856b092011-10-20 11:56:00 +01001068 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
Gloria Wangb483c472011-04-11 17:23:27 -07001069 client->mClient->notify(msg, ext1, ext2, obj);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001070}
1071
Nicolas Catania48290382009-07-10 13:53:06 -07001072
nikoa64c8c72009-07-20 15:07:26 -07001073bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001074{
Nicolas Catania48290382009-07-10 13:53:06 -07001075 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001076
Nicolas Catania48290382009-07-10 13:53:06 -07001077 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001078 return true;
1079 }
1080
Nicolas Catania48290382009-07-10 13:53:06 -07001081 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001082 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001083 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001084 return true;
1085 }
1086}
1087
Nicolas Catania48290382009-07-10 13:53:06 -07001088
nikoa64c8c72009-07-20 15:07:26 -07001089void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001090 Mutex::Autolock lock(mLock);
1091 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1092 mMetadataUpdated.add(metadata_type);
1093 }
1094}
1095
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001096#if CALLBACK_ANTAGONIZER
1097const int Antagonizer::interval = 10000; // 10 msecs
1098
1099Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1100 mExit(false), mActive(false), mClient(client), mCb(cb)
1101{
1102 createThread(callbackThread, this);
1103}
1104
1105void Antagonizer::kill()
1106{
1107 Mutex::Autolock _l(mLock);
1108 mActive = false;
1109 mExit = true;
1110 mCondition.wait(mLock);
1111}
1112
1113int Antagonizer::callbackThread(void* user)
1114{
Steve Blockb8a80522011-12-20 16:23:08 +00001115 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1117 while (!p->mExit) {
1118 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001119 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001120 p->mCb(p->mClient, 0, 0, 0);
1121 }
1122 usleep(interval);
1123 }
1124 Mutex::Autolock _l(p->mLock);
1125 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001126 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001127 return 0;
1128}
1129#endif
1130
1131static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1132
Glenn Kastene1c39622012-01-04 09:36:37 -08001133sp<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 -08001134{
Steve Block3856b092011-10-20 11:56:00 +01001135 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001136 sp<MemoryBase> mem;
1137 sp<MediaPlayerBase> player;
1138
1139 // Protect our precious, precious DRMd ringtones by only allowing
1140 // decoding of http, but not filesystem paths or content Uris.
1141 // If the application wants to decode those, it should open a
1142 // filedescriptor for them and use that.
1143 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001144 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001145 return mem;
1146 }
1147
John Grossman44a7e422012-06-21 17:29:24 -07001148 player_type playerType =
1149 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001150 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001151
1152 // create the right type of player
1153 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001154 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001155 if (player == NULL) goto Exit;
1156 if (player->hardwareOutput()) goto Exit;
1157
1158 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1159
1160 // set data source
1161 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1162
Steve Block3856b092011-10-20 11:56:00 +01001163 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001164 player->prepareAsync();
1165
Steve Block3856b092011-10-20 11:56:00 +01001166 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001167 if (cache->wait() != NO_ERROR) goto Exit;
1168
Steve Block3856b092011-10-20 11:56:00 +01001169 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001170 player->start();
1171
Steve Block3856b092011-10-20 11:56:00 +01001172 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001173 cache->wait();
1174 // in case of error, return what was successfully decoded.
1175 if (cache->size() == 0) {
1176 goto Exit;
1177 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001178
1179 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1180 *pSampleRate = cache->sampleRate();
1181 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001182 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001183 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 -08001184
1185Exit:
1186 if (player != 0) player->reset();
1187 return mem;
1188}
1189
Glenn Kastene1c39622012-01-04 09:36:37 -08001190sp<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 -08001191{
Steve Block3856b092011-10-20 11:56:00 +01001192 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001193 sp<MemoryBase> mem;
1194 sp<MediaPlayerBase> player;
1195
John Grossman44a7e422012-06-21 17:29:24 -07001196 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1197 fd,
1198 offset,
1199 length);
Steve Block3856b092011-10-20 11:56:00 +01001200 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201
1202 // create the right type of player
1203 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001204 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205 if (player == NULL) goto Exit;
1206 if (player->hardwareOutput()) goto Exit;
1207
1208 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1209
1210 // set data source
1211 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1212
Steve Block3856b092011-10-20 11:56:00 +01001213 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214 player->prepareAsync();
1215
Steve Block3856b092011-10-20 11:56:00 +01001216 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001217 if (cache->wait() != NO_ERROR) goto Exit;
1218
Steve Block3856b092011-10-20 11:56:00 +01001219 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001220 player->start();
1221
Steve Block3856b092011-10-20 11:56:00 +01001222 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001223 cache->wait();
1224 // in case of error, return what was successfully decoded.
1225 if (cache->size() == 0) {
1226 goto Exit;
1227 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228
1229 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1230 *pSampleRate = cache->sampleRate();
1231 *pNumChannels = cache->channelCount();
1232 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001233 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 -08001234
1235Exit:
1236 if (player != 0) player->reset();
1237 ::close(fd);
1238 return mem;
1239}
1240
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001241
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001242#undef LOG_TAG
1243#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001244MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001245 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001246 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001247 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001248 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001249 mSessionId(sessionId),
1250 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001251 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001252 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001253 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001254 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 mLeftVolume = 1.0;
1256 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001257 mPlaybackRatePermille = 1000;
1258 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001260 mAuxEffectId = 0;
1261 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 setMinBufferCount();
1263}
1264
1265MediaPlayerService::AudioOutput::~AudioOutput()
1266{
1267 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001268 delete mRecycledTrack;
1269 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001270}
1271
1272void MediaPlayerService::AudioOutput::setMinBufferCount()
1273{
1274 char value[PROPERTY_VALUE_MAX];
1275 if (property_get("ro.kernel.qemu", value, 0)) {
1276 mIsOnEmulator = true;
1277 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1278 }
1279}
1280
1281bool MediaPlayerService::AudioOutput::isOnEmulator()
1282{
1283 setMinBufferCount();
1284 return mIsOnEmulator;
1285}
1286
1287int MediaPlayerService::AudioOutput::getMinBufferCount()
1288{
1289 setMinBufferCount();
1290 return mMinBufferCount;
1291}
1292
1293ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1294{
1295 if (mTrack == 0) return NO_INIT;
1296 return mTrack->frameCount() * frameSize();
1297}
1298
1299ssize_t MediaPlayerService::AudioOutput::frameCount() const
1300{
1301 if (mTrack == 0) return NO_INIT;
1302 return mTrack->frameCount();
1303}
1304
1305ssize_t MediaPlayerService::AudioOutput::channelCount() const
1306{
1307 if (mTrack == 0) return NO_INIT;
1308 return mTrack->channelCount();
1309}
1310
1311ssize_t MediaPlayerService::AudioOutput::frameSize() const
1312{
1313 if (mTrack == 0) return NO_INIT;
1314 return mTrack->frameSize();
1315}
1316
1317uint32_t MediaPlayerService::AudioOutput::latency () const
1318{
Eric Laurentdb354e52012-03-05 17:27:11 -08001319 if (mTrack == 0) return 0;
1320 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321}
1322
1323float MediaPlayerService::AudioOutput::msecsPerFrame() const
1324{
1325 return mMsecsPerFrame;
1326}
1327
Marco Nelissen4110c102012-03-29 09:31:28 -07001328status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001329{
1330 if (mTrack == 0) return NO_INIT;
1331 return mTrack->getPosition(position);
1332}
1333
Marco Nelissen4110c102012-03-29 09:31:28 -07001334status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1335{
1336 if (mTrack == 0) return NO_INIT;
1337 *frameswritten = mBytesWritten / frameSize();
1338 return OK;
1339}
1340
Andreas Huber20111aa2009-07-14 16:56:47 -07001341status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001342 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1343 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001344 AudioCallback cb, void *cookie,
1345 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001346{
Andreas Huber20111aa2009-07-14 16:56:47 -07001347 mCallback = cb;
1348 mCallbackCookie = cookie;
1349
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001350 // Check argument "bufferCount" against the mininum buffer count
1351 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001352 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001353 bufferCount = mMinBufferCount;
1354
1355 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001356 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1357 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001358 int afSampleRate;
1359 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001360 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001361
1362 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1363 return NO_INIT;
1364 }
1365 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1366 return NO_INIT;
1367 }
1368
1369 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001370
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001371 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001372 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001373 if (0 == channelMask) {
1374 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1375 return NO_INIT;
1376 }
1377 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001378
Andreas Huber20111aa2009-07-14 16:56:47 -07001379 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001380 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001381 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001382 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001383 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001384 mStreamType,
1385 sampleRate,
1386 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001387 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001388 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001389 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001390 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001391 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001392 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001393 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001394 } else {
1395 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001396 mStreamType,
1397 sampleRate,
1398 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001399 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001400 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001401 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001402 NULL,
1403 NULL,
1404 0,
1405 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001406 }
1407
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001408 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001409 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001410 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001411 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001412 return NO_INIT;
1413 }
1414
Marco Nelissen67295b52012-06-11 14:52:53 -07001415
1416 if (mRecycledTrack) {
1417 // check if the existing track can be reused as-is, or if a new track needs to be created.
1418
1419 bool reuse = true;
1420 if ((mCallbackData == NULL && mCallback != NULL) ||
1421 (mCallbackData != NULL && mCallback == NULL)) {
1422 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1423 ALOGV("can't chain callback and write");
1424 reuse = false;
1425 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1426 (mRecycledTrack->channelCount() != channelCount) ||
1427 (mRecycledTrack->frameCount() != t->frameCount())) {
1428 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1429 mRecycledTrack->getSampleRate(), sampleRate,
1430 mRecycledTrack->channelCount(), channelCount,
1431 mRecycledTrack->frameCount(), t->frameCount());
1432 reuse = false;
1433 } else if (flags != mFlags) {
1434 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1435 reuse = false;
1436 }
1437 if (reuse) {
1438 ALOGV("chaining to next output");
1439 close();
1440 mTrack = mRecycledTrack;
1441 mRecycledTrack = NULL;
1442 if (mCallbackData != NULL) {
1443 mCallbackData->setOutput(this);
1444 }
1445 delete t;
1446 delete newcbd;
1447 return OK;
1448 }
1449
1450 // if we're not going to reuse the track, unblock and flush it
1451 if (mCallbackData != NULL) {
1452 mCallbackData->setOutput(NULL);
1453 mCallbackData->endTrackSwitch();
1454 }
1455 mRecycledTrack->flush();
1456 delete mRecycledTrack;
1457 mRecycledTrack = NULL;
1458 delete mCallbackData;
1459 mCallbackData = NULL;
1460 close();
1461 }
1462
1463 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001464 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001465 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001466
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001467 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001468 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001469 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001470 uint32_t pos;
1471 if (t->getPosition(&pos) == OK) {
1472 mBytesWritten = uint64_t(pos) * t->frameSize();
1473 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001474 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001475
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001476 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1477 if (res != NO_ERROR) {
1478 return res;
1479 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001480 t->setAuxEffectSendLevel(mSendLevel);
1481 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001482}
1483
1484void MediaPlayerService::AudioOutput::start()
1485{
Steve Block3856b092011-10-20 11:56:00 +01001486 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001487 if (mCallbackData != NULL) {
1488 mCallbackData->endTrackSwitch();
1489 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001490 if (mTrack) {
1491 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001492 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001493 mTrack->start();
1494 }
1495}
1496
Marco Nelissen6b74d672012-02-28 16:07:44 -08001497void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1498 mNextOutput = nextOutput;
1499}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001500
1501
Marco Nelissen6b74d672012-02-28 16:07:44 -08001502void MediaPlayerService::AudioOutput::switchToNextOutput() {
1503 ALOGV("switchToNextOutput");
1504 if (mNextOutput != NULL) {
1505 if (mCallbackData != NULL) {
1506 mCallbackData->beginTrackSwitch();
1507 }
1508 delete mNextOutput->mCallbackData;
1509 mNextOutput->mCallbackData = mCallbackData;
1510 mCallbackData = NULL;
1511 mNextOutput->mRecycledTrack = mTrack;
1512 mTrack = NULL;
1513 mNextOutput->mSampleRateHz = mSampleRateHz;
1514 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001515 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001516 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001517 }
1518}
1519
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001520ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1521{
Andreas Huber20111aa2009-07-14 16:56:47 -07001522 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1523
Steve Block3856b092011-10-20 11:56:00 +01001524 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001525 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001526 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001527 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001528 return ret;
1529 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001530 return NO_INIT;
1531}
1532
1533void MediaPlayerService::AudioOutput::stop()
1534{
Steve Block3856b092011-10-20 11:56:00 +01001535 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001536 if (mTrack) mTrack->stop();
1537}
1538
1539void MediaPlayerService::AudioOutput::flush()
1540{
Steve Block3856b092011-10-20 11:56:00 +01001541 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001542 if (mTrack) mTrack->flush();
1543}
1544
1545void MediaPlayerService::AudioOutput::pause()
1546{
Steve Block3856b092011-10-20 11:56:00 +01001547 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001548 if (mTrack) mTrack->pause();
1549}
1550
1551void MediaPlayerService::AudioOutput::close()
1552{
Steve Block3856b092011-10-20 11:56:00 +01001553 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001554 delete mTrack;
1555 mTrack = 0;
1556}
1557
1558void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1559{
Steve Block3856b092011-10-20 11:56:00 +01001560 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561 mLeftVolume = left;
1562 mRightVolume = right;
1563 if (mTrack) {
1564 mTrack->setVolume(left, right);
1565 }
1566}
1567
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001568status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1569{
1570 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1571 status_t res = NO_ERROR;
1572 if (mTrack) {
1573 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1574 } else {
1575 res = NO_INIT;
1576 }
1577 mPlaybackRatePermille = ratePermille;
1578 if (mSampleRateHz != 0) {
1579 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1580 }
1581 return res;
1582}
1583
Eric Laurent2beeb502010-07-16 07:43:46 -07001584status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1585{
Steve Block3856b092011-10-20 11:56:00 +01001586 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001587 mSendLevel = level;
1588 if (mTrack) {
1589 return mTrack->setAuxEffectSendLevel(level);
1590 }
1591 return NO_ERROR;
1592}
1593
1594status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1595{
Steve Block3856b092011-10-20 11:56:00 +01001596 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001597 mAuxEffectId = effectId;
1598 if (mTrack) {
1599 return mTrack->attachAuxEffect(effectId);
1600 }
1601 return NO_ERROR;
1602}
1603
Andreas Huber20111aa2009-07-14 16:56:47 -07001604// static
1605void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001606 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001607 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001608 if (event != AudioTrack::EVENT_MORE_DATA) {
1609 return;
1610 }
1611
Marco Nelissen6b74d672012-02-28 16:07:44 -08001612 CallbackData *data = (CallbackData*)cookie;
1613 data->lock();
1614 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001615 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001616 if (me == NULL) {
1617 // no output set, likely because the track was scheduled to be reused
1618 // by another player, but the format turned out to be incompatible.
1619 data->unlock();
1620 buffer->size = 0;
1621 return;
1622 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001623
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001624 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001625 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001626
Marco Nelissen6b74d672012-02-28 16:07:44 -08001627 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001628 // We've reached EOS but the audio track is not stopped yet,
1629 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001630
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001631 memset(buffer->raw, 0, buffer->size);
1632 actualSize = buffer->size;
1633 }
1634
1635 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001636 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001637}
1638
Marco Nelissen4110c102012-03-29 09:31:28 -07001639int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001640{
1641 return mSessionId;
1642}
1643
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001644#undef LOG_TAG
1645#define LOG_TAG "AudioCache"
1646MediaPlayerService::AudioCache::AudioCache(const char* name) :
1647 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1648 mError(NO_ERROR), mCommandComplete(false)
1649{
1650 // create ashmem heap
1651 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1652}
1653
1654uint32_t MediaPlayerService::AudioCache::latency () const
1655{
1656 return 0;
1657}
1658
1659float MediaPlayerService::AudioCache::msecsPerFrame() const
1660{
1661 return mMsecsPerFrame;
1662}
1663
Marco Nelissen4110c102012-03-29 09:31:28 -07001664status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001665{
1666 if (position == 0) return BAD_VALUE;
1667 *position = mSize;
1668 return NO_ERROR;
1669}
1670
Marco Nelissen4110c102012-03-29 09:31:28 -07001671status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1672{
1673 if (written == 0) return BAD_VALUE;
1674 *written = mSize;
1675 return NO_ERROR;
1676}
1677
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001678////////////////////////////////////////////////////////////////////////////////
1679
1680struct CallbackThread : public Thread {
1681 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1682 MediaPlayerBase::AudioSink::AudioCallback cb,
1683 void *cookie);
1684
1685protected:
1686 virtual ~CallbackThread();
1687
1688 virtual bool threadLoop();
1689
1690private:
1691 wp<MediaPlayerBase::AudioSink> mSink;
1692 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1693 void *mCookie;
1694 void *mBuffer;
1695 size_t mBufferSize;
1696
1697 CallbackThread(const CallbackThread &);
1698 CallbackThread &operator=(const CallbackThread &);
1699};
1700
1701CallbackThread::CallbackThread(
1702 const wp<MediaPlayerBase::AudioSink> &sink,
1703 MediaPlayerBase::AudioSink::AudioCallback cb,
1704 void *cookie)
1705 : mSink(sink),
1706 mCallback(cb),
1707 mCookie(cookie),
1708 mBuffer(NULL),
1709 mBufferSize(0) {
1710}
1711
1712CallbackThread::~CallbackThread() {
1713 if (mBuffer) {
1714 free(mBuffer);
1715 mBuffer = NULL;
1716 }
1717}
1718
1719bool CallbackThread::threadLoop() {
1720 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1721 if (sink == NULL) {
1722 return false;
1723 }
1724
1725 if (mBuffer == NULL) {
1726 mBufferSize = sink->bufferSize();
1727 mBuffer = malloc(mBufferSize);
1728 }
1729
1730 size_t actualSize =
1731 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1732
1733 if (actualSize > 0) {
1734 sink->write(mBuffer, actualSize);
1735 }
1736
1737 return true;
1738}
1739
1740////////////////////////////////////////////////////////////////////////////////
1741
Andreas Huber20111aa2009-07-14 16:56:47 -07001742status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001743 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1744 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001745 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001746{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001747 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001748 if (mHeap->getHeapID() < 0) {
1749 return NO_INIT;
1750 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001751
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001752 mSampleRate = sampleRate;
1753 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001754 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001755 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001756
1757 if (cb != NULL) {
1758 mCallbackThread = new CallbackThread(this, cb, cookie);
1759 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001760 return NO_ERROR;
1761}
1762
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001763void MediaPlayerService::AudioCache::start() {
1764 if (mCallbackThread != NULL) {
1765 mCallbackThread->run("AudioCache callback");
1766 }
1767}
1768
1769void MediaPlayerService::AudioCache::stop() {
1770 if (mCallbackThread != NULL) {
1771 mCallbackThread->requestExitAndWait();
1772 }
1773}
1774
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001775ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1776{
Steve Block3856b092011-10-20 11:56:00 +01001777 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001778 if ((buffer == 0) || (size == 0)) return size;
1779
1780 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1781 if (p == NULL) return NO_INIT;
1782 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001783 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001784 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001785 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001786 size = mHeap->getSize() - mSize;
1787 }
1788 memcpy(p, buffer, size);
1789 mSize += size;
1790 return size;
1791}
1792
1793// call with lock held
1794status_t MediaPlayerService::AudioCache::wait()
1795{
1796 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001797 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001798 mSignal.wait(mLock);
1799 }
1800 mCommandComplete = false;
1801
1802 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001803 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001804 } else {
Steve Block3856b092011-10-20 11:56:00 +01001805 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806 }
1807 return mError;
1808}
1809
Gloria Wangb483c472011-04-11 17:23:27 -07001810void MediaPlayerService::AudioCache::notify(
1811 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812{
Steve Block3856b092011-10-20 11:56:00 +01001813 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001814 AudioCache* p = static_cast<AudioCache*>(cookie);
1815
1816 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001817 switch (msg)
1818 {
1819 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001820 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001821 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001822 break;
1823 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001824 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001825 break;
1826 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001827 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001828 break;
1829 default:
Steve Block3856b092011-10-20 11:56:00 +01001830 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001831 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001832 }
1833
1834 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001835 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001836 p->mCommandComplete = true;
1837 p->mSignal.signal();
1838}
1839
Marco Nelissen4110c102012-03-29 09:31:28 -07001840int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001841{
1842 return 0;
1843}
1844
Gloria Wang7cf180c2011-02-19 18:37:57 -08001845void MediaPlayerService::addBatteryData(uint32_t params)
1846{
1847 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001848
1849 int32_t time = systemTime() / 1000000L;
1850
1851 // change audio output devices. This notification comes from AudioFlinger
1852 if ((params & kBatteryDataSpeakerOn)
1853 || (params & kBatteryDataOtherAudioDeviceOn)) {
1854
1855 int deviceOn[NUM_AUDIO_DEVICES];
1856 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1857 deviceOn[i] = 0;
1858 }
1859
1860 if ((params & kBatteryDataSpeakerOn)
1861 && (params & kBatteryDataOtherAudioDeviceOn)) {
1862 deviceOn[SPEAKER_AND_OTHER] = 1;
1863 } else if (params & kBatteryDataSpeakerOn) {
1864 deviceOn[SPEAKER] = 1;
1865 } else {
1866 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1867 }
1868
1869 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1870 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1871
1872 if (mBatteryAudio.refCount > 0) { // if playing audio
1873 if (!deviceOn[i]) {
1874 mBatteryAudio.lastTime[i] += time;
1875 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1876 mBatteryAudio.lastTime[i] = 0;
1877 } else {
1878 mBatteryAudio.lastTime[i] = 0 - time;
1879 }
1880 }
1881
1882 mBatteryAudio.deviceOn[i] = deviceOn[i];
1883 }
1884 }
1885 return;
1886 }
1887
1888 // an sudio stream is started
1889 if (params & kBatteryDataAudioFlingerStart) {
1890 // record the start time only if currently no other audio
1891 // is being played
1892 if (mBatteryAudio.refCount == 0) {
1893 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1894 if (mBatteryAudio.deviceOn[i]) {
1895 mBatteryAudio.lastTime[i] -= time;
1896 }
1897 }
1898 }
1899
1900 mBatteryAudio.refCount ++;
1901 return;
1902
1903 } else if (params & kBatteryDataAudioFlingerStop) {
1904 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001905 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001906 return;
1907 }
1908
1909 // record the stop time only if currently this is the only
1910 // audio being played
1911 if (mBatteryAudio.refCount == 1) {
1912 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1913 if (mBatteryAudio.deviceOn[i]) {
1914 mBatteryAudio.lastTime[i] += time;
1915 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1916 mBatteryAudio.lastTime[i] = 0;
1917 }
1918 }
1919 }
1920
1921 mBatteryAudio.refCount --;
1922 return;
1923 }
1924
Gloria Wang7cf180c2011-02-19 18:37:57 -08001925 int uid = IPCThreadState::self()->getCallingUid();
1926 if (uid == AID_MEDIA) {
1927 return;
1928 }
1929 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001930
1931 if (index < 0) { // create a new entry for this UID
1932 BatteryUsageInfo info;
1933 info.audioTotalTime = 0;
1934 info.videoTotalTime = 0;
1935 info.audioLastTime = 0;
1936 info.videoLastTime = 0;
1937 info.refCount = 0;
1938
Gloria Wang9ee159b2011-02-24 14:51:45 -08001939 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001940 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001941 return;
1942 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001943 }
1944
1945 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1946
1947 if (params & kBatteryDataCodecStarted) {
1948 if (params & kBatteryDataTrackAudio) {
1949 info.audioLastTime -= time;
1950 info.refCount ++;
1951 }
1952 if (params & kBatteryDataTrackVideo) {
1953 info.videoLastTime -= time;
1954 info.refCount ++;
1955 }
1956 } else {
1957 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001958 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001959 return;
1960 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001961 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001962 mBatteryData.removeItem(uid);
1963 return;
1964 }
1965
1966 if (params & kBatteryDataTrackAudio) {
1967 info.audioLastTime += time;
1968 info.refCount --;
1969 }
1970 if (params & kBatteryDataTrackVideo) {
1971 info.videoLastTime += time;
1972 info.refCount --;
1973 }
1974
1975 // no stream is being played by this UID
1976 if (info.refCount == 0) {
1977 info.audioTotalTime += info.audioLastTime;
1978 info.audioLastTime = 0;
1979 info.videoTotalTime += info.videoLastTime;
1980 info.videoLastTime = 0;
1981 }
1982 }
1983}
1984
1985status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
1986 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001987
1988 // audio output devices usage
1989 int32_t time = systemTime() / 1000000L; //in ms
1990 int32_t totalTime;
1991
1992 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1993 totalTime = mBatteryAudio.totalTime[i];
1994
1995 if (mBatteryAudio.deviceOn[i]
1996 && (mBatteryAudio.lastTime[i] != 0)) {
1997 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
1998 totalTime += tmpTime;
1999 }
2000
2001 reply->writeInt32(totalTime);
2002 // reset the total time
2003 mBatteryAudio.totalTime[i] = 0;
2004 }
2005
2006 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002007 BatteryUsageInfo info;
2008 int size = mBatteryData.size();
2009
2010 reply->writeInt32(size);
2011 int i = 0;
2012
2013 while (i < size) {
2014 info = mBatteryData.valueAt(i);
2015
2016 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2017 reply->writeInt32(info.audioTotalTime);
2018 reply->writeInt32(info.videoTotalTime);
2019
2020 info.audioTotalTime = 0;
2021 info.videoTotalTime = 0;
2022
2023 // remove the UID entry where no stream is being played
2024 if (info.refCount <= 0) {
2025 mBatteryData.removeItemsAt(i);
2026 size --;
2027 i --;
2028 }
2029 i++;
2030 }
2031 return NO_ERROR;
2032}
nikoa64c8c72009-07-20 15:07:26 -07002033} // namespace android