blob: 166bae99f6c788ce3ae4230122cefd980f0b507a [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"
Andreas Huber35213f12012-08-29 11:41:50 -070073#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070074
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070075namespace {
nikoa64c8c72009-07-20 15:07:26 -070076using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070077using android::status_t;
78using android::OK;
79using android::BAD_VALUE;
80using android::NOT_ENOUGH_DATA;
81using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070082
83// Max number of entries in the filter.
84const int kMaxFilterSize = 64; // I pulled that out of thin air.
85
nikoa64c8c72009-07-20 15:07:26 -070086// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070087
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070088
89// Unmarshall a filter from a Parcel.
90// Filter format in a parcel:
91//
92// 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
93// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
94// | number of entries (n) |
95// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96// | metadata type 1 |
97// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98// | metadata type 2 |
99// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100// ....
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102// | metadata type n |
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104//
105// @param p Parcel that should start with a filter.
106// @param[out] filter On exit contains the list of metadata type to be
107// filtered.
108// @param[out] status On exit contains the status code to be returned.
109// @return true if the parcel starts with a valid filter.
110bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700111 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700112 status_t *status)
113{
Nicolas Catania48290382009-07-10 13:53:06 -0700114 int32_t val;
115 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700116 {
Steve Block29357bc2012-01-06 19:20:56 +0000117 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700118 *status = NOT_ENOUGH_DATA;
119 return false;
120 }
121
Nicolas Catania48290382009-07-10 13:53:06 -0700122 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 {
Steve Block29357bc2012-01-06 19:20:56 +0000124 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700125 *status = BAD_VALUE;
126 return false;
127 }
128
Nicolas Catania48290382009-07-10 13:53:06 -0700129 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700130
131 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700132 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133
nikoa64c8c72009-07-20 15:07:26 -0700134 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135
Nicolas Catania48290382009-07-10 13:53:06 -0700136
137 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138 {
Steve Block29357bc2012-01-06 19:20:56 +0000139 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140 *status = NOT_ENOUGH_DATA;
141 return false;
142 }
143
nikoa64c8c72009-07-20 15:07:26 -0700144 const Metadata::Type *data =
145 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700146
Nicolas Catania48290382009-07-10 13:53:06 -0700147 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148 {
Steve Block29357bc2012-01-06 19:20:56 +0000149 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 *status = BAD_VALUE;
151 return false;
152 }
153
154 // TODO: The stl impl of vector would be more efficient here
155 // because it degenerates into a memcpy on pod types. Try to
156 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700157 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700158 {
Nicolas Catania48290382009-07-10 13:53:06 -0700159 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 ++data;
161 }
162 *status = OK;
163 return true;
164}
165
Nicolas Catania48290382009-07-10 13:53:06 -0700166// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700167// @param val To be searched.
168// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700169bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170{
171 // Deal with empty and ANY right away
172 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700173 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700174
Nicolas Catania48290382009-07-10 13:53:06 -0700175 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700176}
177
178} // anonymous namespace
179
180
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800181namespace android {
182
Dave Burked681bbb2011-08-30 14:39:17 +0100183static bool checkPermission(const char* permissionString) {
184#ifndef HAVE_ANDROID_OS
185 return true;
186#endif
187 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
188 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000189 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100190 return ok;
191}
192
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800193// 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 -0800194/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
195/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
196
197void MediaPlayerService::instantiate() {
198 defaultServiceManager()->addService(
199 String16("media.player"), new MediaPlayerService());
200}
201
202MediaPlayerService::MediaPlayerService()
203{
Steve Block3856b092011-10-20 11:56:00 +0100204 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800205 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800206
207 mBatteryAudio.refCount = 0;
208 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
209 mBatteryAudio.deviceOn[i] = 0;
210 mBatteryAudio.lastTime[i] = 0;
211 mBatteryAudio.totalTime[i] = 0;
212 }
213 // speaker is on by default
214 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700215
216 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800217}
218
219MediaPlayerService::~MediaPlayerService()
220{
Steve Block3856b092011-10-20 11:56:00 +0100221 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222}
223
224sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
225{
Gloria Wangdac6a312009-10-29 15:46:37 -0700226 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
227 wp<MediaRecorderClient> w = recorder;
228 Mutex::Autolock lock(mLock);
229 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100230 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800231 return recorder;
232}
233
Gloria Wangdac6a312009-10-29 15:46:37 -0700234void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
235{
236 Mutex::Autolock lock(mLock);
237 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100238 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700239}
240
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800241sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
242{
243 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245 return retriever;
246}
247
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100249 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250{
251 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700252
253 sp<Client> c = new Client(
254 this, pid, connId, client, audioSessionId,
255 IPCThreadState::self()->getCallingUid());
256
Steve Block3856b092011-10-20 11:56:00 +0100257 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100258 IPCThreadState::self()->getCallingUid());
259
260 wp<Client> w = c;
261 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800262 Mutex::Autolock lock(mLock);
263 mClients.add(w);
264 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800265 return c;
266}
267
Andreas Huber318ad9c2009-10-15 13:46:54 -0700268sp<IOMX> MediaPlayerService::getOMX() {
269 Mutex::Autolock autoLock(mLock);
270
271 if (mOMX.get() == NULL) {
272 mOMX = new OMX;
273 }
274
275 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700276}
277
Andreas Hubered3e3e02012-03-26 11:13:27 -0700278sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700279 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700280}
281
Andreas Huberab1bd842012-08-30 14:51:40 -0700282status_t MediaPlayerService::enableRemoteDisplay(const char *iface) {
Andreas Huber35213f12012-08-29 11:41:50 -0700283 Mutex::Autolock autoLock(mLock);
284
Andreas Huberab1bd842012-08-30 14:51:40 -0700285 if (iface != NULL) {
286 if (mRemoteDisplay != NULL) {
287 return INVALID_OPERATION;
288 }
289
Andreas Huber35213f12012-08-29 11:41:50 -0700290 mRemoteDisplay = new RemoteDisplay;
291
Andreas Huberab1bd842012-08-30 14:51:40 -0700292 status_t err = mRemoteDisplay->start(iface);
Andreas Huber35213f12012-08-29 11:41:50 -0700293
294 if (err != OK) {
295 mRemoteDisplay.clear();
296 return err;
297 }
298
299 return OK;
Andreas Huberab1bd842012-08-30 14:51:40 -0700300 }
301
302 if (mRemoteDisplay != NULL) {
Andreas Huber35213f12012-08-29 11:41:50 -0700303 mRemoteDisplay->stop();
304 mRemoteDisplay.clear();
305 }
306
307 return OK;
308}
309
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
311{
312 const size_t SIZE = 256;
313 char buffer[SIZE];
314 String8 result;
315
316 result.append(" AudioCache\n");
317 if (mHeap != 0) {
318 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
319 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
320 result.append(buffer);
321 }
322 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
323 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
324 result.append(buffer);
325 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
326 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
327 result.append(buffer);
328 ::write(fd, result.string(), result.size());
329 return NO_ERROR;
330}
331
332status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
333{
334 const size_t SIZE = 256;
335 char buffer[SIZE];
336 String8 result;
337
338 result.append(" AudioOutput\n");
339 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
340 mStreamType, mLeftVolume, mRightVolume);
341 result.append(buffer);
342 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800343 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800344 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700345 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
346 mAuxEffectId, mSendLevel);
347 result.append(buffer);
348
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800349 ::write(fd, result.string(), result.size());
350 if (mTrack != 0) {
351 mTrack->dump(fd, args);
352 }
353 return NO_ERROR;
354}
355
356status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
357{
358 const size_t SIZE = 256;
359 char buffer[SIZE];
360 String8 result;
361 result.append(" Client\n");
362 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
363 mPid, mConnId, mStatus, mLoop?"true": "false");
364 result.append(buffer);
365 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700366 if (mPlayer != NULL) {
367 mPlayer->dump(fd, args);
368 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800369 if (mAudioOutput != 0) {
370 mAudioOutput->dump(fd, args);
371 }
372 write(fd, "\n", 1);
373 return NO_ERROR;
374}
375
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800376status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
377{
378 const size_t SIZE = 256;
379 char buffer[SIZE];
380 String8 result;
381 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
382 snprintf(buffer, SIZE, "Permission Denial: "
383 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
384 IPCThreadState::self()->getCallingPid(),
385 IPCThreadState::self()->getCallingUid());
386 result.append(buffer);
387 } else {
388 Mutex::Autolock lock(mLock);
389 for (int i = 0, n = mClients.size(); i < n; ++i) {
390 sp<Client> c = mClients[i].promote();
391 if (c != 0) c->dump(fd, args);
392 }
James Dongb9141222010-07-08 11:16:11 -0700393 if (mMediaRecorderClients.size() == 0) {
394 result.append(" No media recorder client\n\n");
395 } else {
396 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
397 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700398 if (c != 0) {
399 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
400 result.append(buffer);
401 write(fd, result.string(), result.size());
402 result = "\n";
403 c->dump(fd, args);
404 }
James Dongb9141222010-07-08 11:16:11 -0700405 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700406 }
407
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700409 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410 FILE *f = fopen(buffer, "r");
411 if (f) {
412 while (!feof(f)) {
413 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700414 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800415 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700416 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417 strstr(buffer, " /system/media/")) {
418 result.append(" ");
419 result.append(buffer);
420 }
421 }
422 fclose(f);
423 } else {
424 result.append("couldn't open ");
425 result.append(buffer);
426 result.append("\n");
427 }
428
Glenn Kasten0512ab52011-05-04 17:58:57 -0700429 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800430 DIR *d = opendir(buffer);
431 if (d) {
432 struct dirent *ent;
433 while((ent = readdir(d)) != NULL) {
434 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700435 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800436 struct stat s;
437 if (lstat(buffer, &s) == 0) {
438 if ((s.st_mode & S_IFMT) == S_IFLNK) {
439 char linkto[256];
440 int len = readlink(buffer, linkto, sizeof(linkto));
441 if(len > 0) {
442 if(len > 255) {
443 linkto[252] = '.';
444 linkto[253] = '.';
445 linkto[254] = '.';
446 linkto[255] = 0;
447 } else {
448 linkto[len] = 0;
449 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700450 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800451 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700452 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 strstr(linkto, "/system/media/") == linkto) {
454 result.append(" ");
455 result.append(buffer);
456 result.append(" -> ");
457 result.append(linkto);
458 result.append("\n");
459 }
460 }
461 } else {
462 result.append(" unexpected type for ");
463 result.append(buffer);
464 result.append("\n");
465 }
466 }
467 }
468 }
469 closedir(d);
470 } else {
471 result.append("couldn't open ");
472 result.append(buffer);
473 result.append("\n");
474 }
475
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 bool dumpMem = false;
477 for (size_t i = 0; i < args.size(); i++) {
478 if (args[i] == String16("-m")) {
479 dumpMem = true;
480 }
481 }
482 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700483 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 }
486 write(fd, result.string(), result.size());
487 return NO_ERROR;
488}
489
490void MediaPlayerService::removeClient(wp<Client> client)
491{
492 Mutex::Autolock lock(mLock);
493 mClients.remove(client);
494}
495
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700496MediaPlayerService::Client::Client(
497 const sp<MediaPlayerService>& service, pid_t pid,
498 int32_t connId, const sp<IMediaPlayerClient>& client,
499 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800500{
Steve Block3856b092011-10-20 11:56:00 +0100501 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502 mPid = pid;
503 mConnId = connId;
504 mService = service;
505 mClient = client;
506 mLoop = false;
507 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700508 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700509 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800510 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700511
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000513 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514 mAntagonizer = new Antagonizer(notify, this);
515#endif
516}
517
518MediaPlayerService::Client::~Client()
519{
Steve Block3856b092011-10-20 11:56:00 +0100520 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521 mAudioOutput.clear();
522 wp<Client> client(this);
523 disconnect();
524 mService->removeClient(client);
525}
526
527void MediaPlayerService::Client::disconnect()
528{
Steve Block3856b092011-10-20 11:56:00 +0100529 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530 // grab local reference and clear main reference to prevent future
531 // access to object
532 sp<MediaPlayerBase> p;
533 {
534 Mutex::Autolock l(mLock);
535 p = mPlayer;
536 }
Dave Sparks795fa582009-03-24 17:57:12 -0700537 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700538
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 mPlayer.clear();
540
541 // clear the notification to prevent callbacks to dead client
542 // and reset the player. We assume the player will serialize
543 // access to itself if necessary.
544 if (p != 0) {
545 p->setNotifyCallback(0, 0);
546#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000547 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800548 mAntagonizer->kill();
549#endif
550 p->reset();
551 }
552
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700553 disconnectNativeWindow();
554
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 IPCThreadState::self()->flushCommands();
556}
557
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
559{
560 // determine if we have the right player type
561 sp<MediaPlayerBase> p = mPlayer;
562 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100563 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800564 p.clear();
565 }
566 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700567 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800568 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700569
Jason Simmonsdb29e522011-08-12 13:46:55 -0700570 if (p != NULL) {
571 p->setUID(mUID);
572 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700573
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 return p;
575}
576
John Grossmanc795b642012-02-22 15:38:35 -0800577sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
578 player_type playerType)
579{
580 ALOGV("player type = %d", playerType);
581
582 // create the right type of player
583 sp<MediaPlayerBase> p = createPlayer(playerType);
584 if (p == NULL) {
585 return p;
586 }
587
588 if (!p->hardwareOutput()) {
589 mAudioOutput = new AudioOutput(mAudioSessionId);
590 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
591 }
592
593 return p;
594}
595
596void MediaPlayerService::Client::setDataSource_post(
597 const sp<MediaPlayerBase>& p,
598 status_t status)
599{
600 ALOGV(" setDataSource");
601 mStatus = status;
602 if (mStatus != OK) {
603 ALOGE(" error: %d", mStatus);
604 return;
605 }
606
607 // Set the re-transmission endpoint if one was chosen.
608 if (mRetransmitEndpointValid) {
609 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
610 if (mStatus != NO_ERROR) {
611 ALOGE("setRetransmitEndpoint error: %d", mStatus);
612 }
613 }
614
615 if (mStatus == OK) {
616 mPlayer = p;
617 }
618}
619
Andreas Huber2db84552010-01-28 11:19:57 -0800620status_t MediaPlayerService::Client::setDataSource(
621 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800622{
Steve Block3856b092011-10-20 11:56:00 +0100623 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624 if (url == NULL)
625 return UNKNOWN_ERROR;
626
Dave Burked681bbb2011-08-30 14:39:17 +0100627 if ((strncmp(url, "http://", 7) == 0) ||
628 (strncmp(url, "https://", 8) == 0) ||
629 (strncmp(url, "rtsp://", 7) == 0)) {
630 if (!checkPermission("android.permission.INTERNET")) {
631 return PERMISSION_DENIED;
632 }
633 }
634
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635 if (strncmp(url, "content://", 10) == 0) {
636 // get a filedescriptor for the content Uri and
637 // pass it to the setDataSource(fd) method
638
639 String16 url16(url);
640 int fd = android::openContentProviderFile(url16);
641 if (fd < 0)
642 {
Steve Block29357bc2012-01-06 19:20:56 +0000643 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800644 return UNKNOWN_ERROR;
645 }
646 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
647 close(fd);
648 return mStatus;
649 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700650 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800651 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
652 if (p == NULL) {
653 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800654 }
655
John Grossmanc795b642012-02-22 15:38:35 -0800656 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800657 return mStatus;
658 }
659}
660
661status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
662{
Steve Block3856b092011-10-20 11:56:00 +0100663 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800664 struct stat sb;
665 int ret = fstat(fd, &sb);
666 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000667 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668 return UNKNOWN_ERROR;
669 }
670
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("st_dev = %llu", sb.st_dev);
672 ALOGV("st_mode = %u", sb.st_mode);
673 ALOGV("st_uid = %lu", sb.st_uid);
674 ALOGV("st_gid = %lu", sb.st_gid);
675 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676
677 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000678 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800679 ::close(fd);
680 return UNKNOWN_ERROR;
681 }
682 if (offset + length > sb.st_size) {
683 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100684 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685 }
686
John Grossman44a7e422012-06-21 17:29:24 -0700687 player_type playerType = MediaPlayerFactory::getPlayerType(this,
688 fd,
689 offset,
690 length);
John Grossmanc795b642012-02-22 15:38:35 -0800691 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
692 if (p == NULL) {
693 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800694 }
695
696 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800697 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698 return mStatus;
699}
700
Andreas Hubere2b10282010-11-23 11:41:34 -0800701status_t MediaPlayerService::Client::setDataSource(
702 const sp<IStreamSource> &source) {
703 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700704 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800705 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800706 if (p == NULL) {
707 return NO_INIT;
708 }
709
Andreas Hubere2b10282010-11-23 11:41:34 -0800710 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800711 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800712 return mStatus;
713}
714
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700715void MediaPlayerService::Client::disconnectNativeWindow() {
716 if (mConnectedWindow != NULL) {
717 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
718 NATIVE_WINDOW_API_MEDIA);
719
720 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000721 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700722 strerror(-err), err);
723 }
724 }
725 mConnectedWindow.clear();
726}
727
Glenn Kasten11731182011-02-08 17:26:17 -0800728status_t MediaPlayerService::Client::setVideoSurfaceTexture(
729 const sp<ISurfaceTexture>& surfaceTexture)
730{
Steve Block3856b092011-10-20 11:56:00 +0100731 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800732 sp<MediaPlayerBase> p = getPlayer();
733 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700734
735 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
736 surfaceTexture->asBinder());
737 if (mConnectedWindowBinder == binder) {
738 return OK;
739 }
740
741 sp<ANativeWindow> anw;
742 if (surfaceTexture != NULL) {
743 anw = new SurfaceTextureClient(surfaceTexture);
744 status_t err = native_window_api_connect(anw.get(),
745 NATIVE_WINDOW_API_MEDIA);
746
747 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000748 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700749 // Note that we must do the reset before disconnecting from the ANW.
750 // Otherwise queue/dequeue calls could be made on the disconnected
751 // ANW, which may result in errors.
752 reset();
753
754 disconnectNativeWindow();
755
756 return err;
757 }
758 }
759
760 // Note that we must set the player's new SurfaceTexture before
761 // disconnecting the old one. Otherwise queue/dequeue calls could be made
762 // on the disconnected ANW, which may result in errors.
763 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
764
765 disconnectNativeWindow();
766
767 mConnectedWindow = anw;
768
769 if (err == OK) {
770 mConnectedWindowBinder = binder;
771 } else {
772 disconnectNativeWindow();
773 }
774
775 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800776}
777
Nicolas Catania1d187f12009-05-12 23:25:55 -0700778status_t MediaPlayerService::Client::invoke(const Parcel& request,
779 Parcel *reply)
780{
781 sp<MediaPlayerBase> p = getPlayer();
782 if (p == NULL) return UNKNOWN_ERROR;
783 return p->invoke(request, reply);
784}
785
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700786// This call doesn't need to access the native player.
787status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
788{
789 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700790 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700791
Nicolas Catania48290382009-07-10 13:53:06 -0700792 if (unmarshallFilter(filter, &allow, &status) &&
793 unmarshallFilter(filter, &drop, &status)) {
794 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700795
796 mMetadataAllow = allow;
797 mMetadataDrop = drop;
798 }
799 return status;
800}
801
Nicolas Catania48290382009-07-10 13:53:06 -0700802status_t MediaPlayerService::Client::getMetadata(
803 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700804{
nikoa64c8c72009-07-20 15:07:26 -0700805 sp<MediaPlayerBase> player = getPlayer();
806 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700807
nikod608a812009-07-16 16:39:53 -0700808 status_t status;
809 // Placeholder for the return code, updated by the caller.
810 reply->writeInt32(-1);
811
nikoa64c8c72009-07-20 15:07:26 -0700812 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700813
814 // We don't block notifications while we fetch the data. We clear
815 // mMetadataUpdated first so we don't lose notifications happening
816 // during the rest of this call.
817 {
818 Mutex::Autolock lock(mLock);
819 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700820 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700821 }
822 mMetadataUpdated.clear();
823 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700824
nikoa64c8c72009-07-20 15:07:26 -0700825 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700826
nikoa64c8c72009-07-20 15:07:26 -0700827 metadata.appendHeader();
828 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700829
830 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700831 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000832 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700833 return status;
834 }
835
836 // FIXME: Implement filtering on the result. Not critical since
837 // filtering takes place on the update notifications already. This
838 // would be when all the metadata are fetch and a filter is set.
839
nikod608a812009-07-16 16:39:53 -0700840 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700841 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700842 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700843}
844
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800845status_t MediaPlayerService::Client::prepareAsync()
846{
Steve Block3856b092011-10-20 11:56:00 +0100847 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800848 sp<MediaPlayerBase> p = getPlayer();
849 if (p == 0) return UNKNOWN_ERROR;
850 status_t ret = p->prepareAsync();
851#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000852 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853 if (ret == NO_ERROR) mAntagonizer->start();
854#endif
855 return ret;
856}
857
858status_t MediaPlayerService::Client::start()
859{
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 sp<MediaPlayerBase> p = getPlayer();
862 if (p == 0) return UNKNOWN_ERROR;
863 p->setLooping(mLoop);
864 return p->start();
865}
866
867status_t MediaPlayerService::Client::stop()
868{
Steve Block3856b092011-10-20 11:56:00 +0100869 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800870 sp<MediaPlayerBase> p = getPlayer();
871 if (p == 0) return UNKNOWN_ERROR;
872 return p->stop();
873}
874
875status_t MediaPlayerService::Client::pause()
876{
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800878 sp<MediaPlayerBase> p = getPlayer();
879 if (p == 0) return UNKNOWN_ERROR;
880 return p->pause();
881}
882
883status_t MediaPlayerService::Client::isPlaying(bool* state)
884{
885 *state = false;
886 sp<MediaPlayerBase> p = getPlayer();
887 if (p == 0) return UNKNOWN_ERROR;
888 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100889 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800890 return NO_ERROR;
891}
892
893status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
894{
Steve Block3856b092011-10-20 11:56:00 +0100895 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800896 sp<MediaPlayerBase> p = getPlayer();
897 if (p == 0) return UNKNOWN_ERROR;
898 status_t ret = p->getCurrentPosition(msec);
899 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100900 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800901 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000902 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903 }
904 return ret;
905}
906
907status_t MediaPlayerService::Client::getDuration(int *msec)
908{
Steve Block3856b092011-10-20 11:56:00 +0100909 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800910 sp<MediaPlayerBase> p = getPlayer();
911 if (p == 0) return UNKNOWN_ERROR;
912 status_t ret = p->getDuration(msec);
913 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100914 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800915 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000916 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800917 }
918 return ret;
919}
920
Marco Nelissen6b74d672012-02-28 16:07:44 -0800921status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
922 ALOGV("setNextPlayer");
923 Mutex::Autolock l(mLock);
924 sp<Client> c = static_cast<Client*>(player.get());
925 mNextClient = c;
926 if (mAudioOutput != NULL && c != NULL) {
927 mAudioOutput->setNextOutput(c->mAudioOutput);
928 } else {
929 ALOGE("no current audio output");
930 }
931 return OK;
932}
933
934
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800935status_t MediaPlayerService::Client::seekTo(int msec)
936{
Steve Block3856b092011-10-20 11:56:00 +0100937 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800938 sp<MediaPlayerBase> p = getPlayer();
939 if (p == 0) return UNKNOWN_ERROR;
940 return p->seekTo(msec);
941}
942
943status_t MediaPlayerService::Client::reset()
944{
Steve Block3856b092011-10-20 11:56:00 +0100945 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800946 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 sp<MediaPlayerBase> p = getPlayer();
948 if (p == 0) return UNKNOWN_ERROR;
949 return p->reset();
950}
951
Glenn Kastenfff6d712012-01-12 16:38:12 -0800952status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953{
Steve Block3856b092011-10-20 11:56:00 +0100954 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800955 // TODO: for hardware output, call player instead
956 Mutex::Autolock l(mLock);
957 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
958 return NO_ERROR;
959}
960
961status_t MediaPlayerService::Client::setLooping(int loop)
962{
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 mLoop = loop;
965 sp<MediaPlayerBase> p = getPlayer();
966 if (p != 0) return p->setLooping(loop);
967 return NO_ERROR;
968}
969
970status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
971{
Steve Block3856b092011-10-20 11:56:00 +0100972 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800973
974 // for hardware output, call player instead
975 sp<MediaPlayerBase> p = getPlayer();
976 {
977 Mutex::Autolock l(mLock);
978 if (p != 0 && p->hardwareOutput()) {
979 MediaPlayerHWInterface* hwp =
980 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
981 return hwp->setVolume(leftVolume, rightVolume);
982 } else {
983 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
984 return NO_ERROR;
985 }
986 }
987
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800988 return NO_ERROR;
989}
990
Eric Laurent2beeb502010-07-16 07:43:46 -0700991status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
992{
Steve Block3856b092011-10-20 11:56:00 +0100993 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700994 Mutex::Autolock l(mLock);
995 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
996 return NO_ERROR;
997}
998
999status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1000{
Steve Block3856b092011-10-20 11:56:00 +01001001 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001002 Mutex::Autolock l(mLock);
1003 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1004 return NO_ERROR;
1005}
Nicolas Catania48290382009-07-10 13:53:06 -07001006
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001007status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001008 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001009 sp<MediaPlayerBase> p = getPlayer();
1010 if (p == 0) return UNKNOWN_ERROR;
1011 return p->setParameter(key, request);
1012}
1013
1014status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001015 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001016 sp<MediaPlayerBase> p = getPlayer();
1017 if (p == 0) return UNKNOWN_ERROR;
1018 return p->getParameter(key, reply);
1019}
1020
John Grossmanc795b642012-02-22 15:38:35 -08001021status_t MediaPlayerService::Client::setRetransmitEndpoint(
1022 const struct sockaddr_in* endpoint) {
1023
1024 if (NULL != endpoint) {
1025 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1026 uint16_t p = ntohs(endpoint->sin_port);
1027 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1028 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1029 } else {
1030 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1031 }
1032
1033 sp<MediaPlayerBase> p = getPlayer();
1034
1035 // Right now, the only valid time to set a retransmit endpoint is before
1036 // player selection has been made (since the presence or absence of a
1037 // retransmit endpoint is going to determine which player is selected during
1038 // setDataSource).
1039 if (p != 0) return INVALID_OPERATION;
1040
1041 if (NULL != endpoint) {
1042 mRetransmitEndpoint = *endpoint;
1043 mRetransmitEndpointValid = true;
1044 } else {
1045 mRetransmitEndpointValid = false;
1046 }
1047
1048 return NO_ERROR;
1049}
1050
John Grossman44a7e422012-06-21 17:29:24 -07001051status_t MediaPlayerService::Client::getRetransmitEndpoint(
1052 struct sockaddr_in* endpoint)
1053{
1054 if (NULL == endpoint)
1055 return BAD_VALUE;
1056
1057 sp<MediaPlayerBase> p = getPlayer();
1058
1059 if (p != NULL)
1060 return p->getRetransmitEndpoint(endpoint);
1061
1062 if (!mRetransmitEndpointValid)
1063 return NO_INIT;
1064
1065 *endpoint = mRetransmitEndpoint;
1066
1067 return NO_ERROR;
1068}
1069
Gloria Wangb483c472011-04-11 17:23:27 -07001070void MediaPlayerService::Client::notify(
1071 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001072{
1073 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001074 if (client == NULL) {
1075 return;
1076 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001077
James Dongb8a98252012-08-26 16:13:03 -07001078 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001079 {
1080 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001081 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001082 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001083 if (client->mAudioOutput != NULL)
1084 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001085 client->mNextClient->start();
1086 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1087 }
1088 }
1089
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001090 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001091 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001092 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001093
1094 if(client->shouldDropMetadata(metadata_type)) {
1095 return;
1096 }
1097
1098 // Update the list of metadata that have changed. getMetadata
1099 // also access mMetadataUpdated and clears it.
1100 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001101 }
James Dongb8a98252012-08-26 16:13:03 -07001102
1103 if (c != NULL) {
1104 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1105 c->notify(msg, ext1, ext2, obj);
1106 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001107}
1108
Nicolas Catania48290382009-07-10 13:53:06 -07001109
nikoa64c8c72009-07-20 15:07:26 -07001110bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001111{
Nicolas Catania48290382009-07-10 13:53:06 -07001112 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001113
Nicolas Catania48290382009-07-10 13:53:06 -07001114 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001115 return true;
1116 }
1117
Nicolas Catania48290382009-07-10 13:53:06 -07001118 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001119 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001120 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001121 return true;
1122 }
1123}
1124
Nicolas Catania48290382009-07-10 13:53:06 -07001125
nikoa64c8c72009-07-20 15:07:26 -07001126void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001127 Mutex::Autolock lock(mLock);
1128 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1129 mMetadataUpdated.add(metadata_type);
1130 }
1131}
1132
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001133#if CALLBACK_ANTAGONIZER
1134const int Antagonizer::interval = 10000; // 10 msecs
1135
1136Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1137 mExit(false), mActive(false), mClient(client), mCb(cb)
1138{
1139 createThread(callbackThread, this);
1140}
1141
1142void Antagonizer::kill()
1143{
1144 Mutex::Autolock _l(mLock);
1145 mActive = false;
1146 mExit = true;
1147 mCondition.wait(mLock);
1148}
1149
1150int Antagonizer::callbackThread(void* user)
1151{
Steve Blockb8a80522011-12-20 16:23:08 +00001152 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001153 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1154 while (!p->mExit) {
1155 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001156 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001157 p->mCb(p->mClient, 0, 0, 0);
1158 }
1159 usleep(interval);
1160 }
1161 Mutex::Autolock _l(p->mLock);
1162 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001163 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001164 return 0;
1165}
1166#endif
1167
1168static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1169
Glenn Kastene1c39622012-01-04 09:36:37 -08001170sp<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 -08001171{
Steve Block3856b092011-10-20 11:56:00 +01001172 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001173 sp<MemoryBase> mem;
1174 sp<MediaPlayerBase> player;
1175
1176 // Protect our precious, precious DRMd ringtones by only allowing
1177 // decoding of http, but not filesystem paths or content Uris.
1178 // If the application wants to decode those, it should open a
1179 // filedescriptor for them and use that.
1180 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001181 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001182 return mem;
1183 }
1184
John Grossman44a7e422012-06-21 17:29:24 -07001185 player_type playerType =
1186 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001187 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001188
1189 // create the right type of player
1190 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001191 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001192 if (player == NULL) goto Exit;
1193 if (player->hardwareOutput()) goto Exit;
1194
1195 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1196
1197 // set data source
1198 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1199
Steve Block3856b092011-10-20 11:56:00 +01001200 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 player->prepareAsync();
1202
Steve Block3856b092011-10-20 11:56:00 +01001203 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204 if (cache->wait() != NO_ERROR) goto Exit;
1205
Steve Block3856b092011-10-20 11:56:00 +01001206 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 player->start();
1208
Steve Block3856b092011-10-20 11:56:00 +01001209 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001210 cache->wait();
1211 // in case of error, return what was successfully decoded.
1212 if (cache->size() == 0) {
1213 goto Exit;
1214 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215
1216 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1217 *pSampleRate = cache->sampleRate();
1218 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001219 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001220 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 -08001221
1222Exit:
1223 if (player != 0) player->reset();
1224 return mem;
1225}
1226
Glenn Kastene1c39622012-01-04 09:36:37 -08001227sp<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 -08001228{
Steve Block3856b092011-10-20 11:56:00 +01001229 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230 sp<MemoryBase> mem;
1231 sp<MediaPlayerBase> player;
1232
John Grossman44a7e422012-06-21 17:29:24 -07001233 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1234 fd,
1235 offset,
1236 length);
Steve Block3856b092011-10-20 11:56:00 +01001237 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001238
1239 // create the right type of player
1240 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001241 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001242 if (player == NULL) goto Exit;
1243 if (player->hardwareOutput()) goto Exit;
1244
1245 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1246
1247 // set data source
1248 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1249
Steve Block3856b092011-10-20 11:56:00 +01001250 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 player->prepareAsync();
1252
Steve Block3856b092011-10-20 11:56:00 +01001253 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001254 if (cache->wait() != NO_ERROR) goto Exit;
1255
Steve Block3856b092011-10-20 11:56:00 +01001256 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001257 player->start();
1258
Steve Block3856b092011-10-20 11:56:00 +01001259 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001260 cache->wait();
1261 // in case of error, return what was successfully decoded.
1262 if (cache->size() == 0) {
1263 goto Exit;
1264 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265
1266 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1267 *pSampleRate = cache->sampleRate();
1268 *pNumChannels = cache->channelCount();
1269 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001270 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 -08001271
1272Exit:
1273 if (player != 0) player->reset();
1274 ::close(fd);
1275 return mem;
1276}
1277
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001278
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001279#undef LOG_TAG
1280#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001281MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001282 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001283 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001284 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001285 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001286 mSessionId(sessionId),
1287 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001288 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001289 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001290 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001291 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292 mLeftVolume = 1.0;
1293 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001294 mPlaybackRatePermille = 1000;
1295 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001296 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001297 mAuxEffectId = 0;
1298 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 setMinBufferCount();
1300}
1301
1302MediaPlayerService::AudioOutput::~AudioOutput()
1303{
1304 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001305 delete mRecycledTrack;
1306 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307}
1308
1309void MediaPlayerService::AudioOutput::setMinBufferCount()
1310{
1311 char value[PROPERTY_VALUE_MAX];
1312 if (property_get("ro.kernel.qemu", value, 0)) {
1313 mIsOnEmulator = true;
1314 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1315 }
1316}
1317
1318bool MediaPlayerService::AudioOutput::isOnEmulator()
1319{
1320 setMinBufferCount();
1321 return mIsOnEmulator;
1322}
1323
1324int MediaPlayerService::AudioOutput::getMinBufferCount()
1325{
1326 setMinBufferCount();
1327 return mMinBufferCount;
1328}
1329
1330ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1331{
1332 if (mTrack == 0) return NO_INIT;
1333 return mTrack->frameCount() * frameSize();
1334}
1335
1336ssize_t MediaPlayerService::AudioOutput::frameCount() const
1337{
1338 if (mTrack == 0) return NO_INIT;
1339 return mTrack->frameCount();
1340}
1341
1342ssize_t MediaPlayerService::AudioOutput::channelCount() const
1343{
1344 if (mTrack == 0) return NO_INIT;
1345 return mTrack->channelCount();
1346}
1347
1348ssize_t MediaPlayerService::AudioOutput::frameSize() const
1349{
1350 if (mTrack == 0) return NO_INIT;
1351 return mTrack->frameSize();
1352}
1353
1354uint32_t MediaPlayerService::AudioOutput::latency () const
1355{
Eric Laurentdb354e52012-03-05 17:27:11 -08001356 if (mTrack == 0) return 0;
1357 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001358}
1359
1360float MediaPlayerService::AudioOutput::msecsPerFrame() const
1361{
1362 return mMsecsPerFrame;
1363}
1364
Marco Nelissen4110c102012-03-29 09:31:28 -07001365status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001366{
1367 if (mTrack == 0) return NO_INIT;
1368 return mTrack->getPosition(position);
1369}
1370
Marco Nelissen4110c102012-03-29 09:31:28 -07001371status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1372{
1373 if (mTrack == 0) return NO_INIT;
1374 *frameswritten = mBytesWritten / frameSize();
1375 return OK;
1376}
1377
Andreas Huber20111aa2009-07-14 16:56:47 -07001378status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001379 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1380 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001381 AudioCallback cb, void *cookie,
1382 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001383{
Andreas Huber20111aa2009-07-14 16:56:47 -07001384 mCallback = cb;
1385 mCallbackCookie = cookie;
1386
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001387 // Check argument "bufferCount" against the mininum buffer count
1388 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001389 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390 bufferCount = mMinBufferCount;
1391
1392 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001393 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1394 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001395 int afSampleRate;
1396 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001397 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001398
1399 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1400 return NO_INIT;
1401 }
1402 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1403 return NO_INIT;
1404 }
1405
1406 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001407
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001408 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001409 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001410 if (0 == channelMask) {
1411 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1412 return NO_INIT;
1413 }
1414 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001415
Andreas Huber20111aa2009-07-14 16:56:47 -07001416 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001417 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001418 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001419 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001420 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001421 mStreamType,
1422 sampleRate,
1423 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001424 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001425 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001426 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001427 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001428 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001429 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001430 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001431 } else {
1432 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001433 mStreamType,
1434 sampleRate,
1435 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001436 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001437 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001438 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001439 NULL,
1440 NULL,
1441 0,
1442 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001443 }
1444
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001445 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001446 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001447 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001448 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001449 return NO_INIT;
1450 }
1451
Marco Nelissen67295b52012-06-11 14:52:53 -07001452
1453 if (mRecycledTrack) {
1454 // check if the existing track can be reused as-is, or if a new track needs to be created.
1455
1456 bool reuse = true;
1457 if ((mCallbackData == NULL && mCallback != NULL) ||
1458 (mCallbackData != NULL && mCallback == NULL)) {
1459 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1460 ALOGV("can't chain callback and write");
1461 reuse = false;
1462 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1463 (mRecycledTrack->channelCount() != channelCount) ||
1464 (mRecycledTrack->frameCount() != t->frameCount())) {
1465 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1466 mRecycledTrack->getSampleRate(), sampleRate,
1467 mRecycledTrack->channelCount(), channelCount,
1468 mRecycledTrack->frameCount(), t->frameCount());
1469 reuse = false;
1470 } else if (flags != mFlags) {
1471 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1472 reuse = false;
1473 }
1474 if (reuse) {
1475 ALOGV("chaining to next output");
1476 close();
1477 mTrack = mRecycledTrack;
1478 mRecycledTrack = NULL;
1479 if (mCallbackData != NULL) {
1480 mCallbackData->setOutput(this);
1481 }
1482 delete t;
1483 delete newcbd;
1484 return OK;
1485 }
1486
1487 // if we're not going to reuse the track, unblock and flush it
1488 if (mCallbackData != NULL) {
1489 mCallbackData->setOutput(NULL);
1490 mCallbackData->endTrackSwitch();
1491 }
1492 mRecycledTrack->flush();
1493 delete mRecycledTrack;
1494 mRecycledTrack = NULL;
1495 delete mCallbackData;
1496 mCallbackData = NULL;
1497 close();
1498 }
1499
1500 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001501 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001503
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001504 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001505 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001506 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001507 uint32_t pos;
1508 if (t->getPosition(&pos) == OK) {
1509 mBytesWritten = uint64_t(pos) * t->frameSize();
1510 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001511 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001512
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001513 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1514 if (res != NO_ERROR) {
1515 return res;
1516 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001517 t->setAuxEffectSendLevel(mSendLevel);
1518 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001519}
1520
1521void MediaPlayerService::AudioOutput::start()
1522{
Steve Block3856b092011-10-20 11:56:00 +01001523 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001524 if (mCallbackData != NULL) {
1525 mCallbackData->endTrackSwitch();
1526 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001527 if (mTrack) {
1528 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001529 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001530 mTrack->start();
1531 }
1532}
1533
Marco Nelissen6b74d672012-02-28 16:07:44 -08001534void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1535 mNextOutput = nextOutput;
1536}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001537
1538
Marco Nelissen6b74d672012-02-28 16:07:44 -08001539void MediaPlayerService::AudioOutput::switchToNextOutput() {
1540 ALOGV("switchToNextOutput");
1541 if (mNextOutput != NULL) {
1542 if (mCallbackData != NULL) {
1543 mCallbackData->beginTrackSwitch();
1544 }
1545 delete mNextOutput->mCallbackData;
1546 mNextOutput->mCallbackData = mCallbackData;
1547 mCallbackData = NULL;
1548 mNextOutput->mRecycledTrack = mTrack;
1549 mTrack = NULL;
1550 mNextOutput->mSampleRateHz = mSampleRateHz;
1551 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001552 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001553 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001554 }
1555}
1556
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001557ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1558{
Andreas Huber20111aa2009-07-14 16:56:47 -07001559 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1560
Steve Block3856b092011-10-20 11:56:00 +01001561 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001562 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001563 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001564 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001565 return ret;
1566 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567 return NO_INIT;
1568}
1569
1570void MediaPlayerService::AudioOutput::stop()
1571{
Steve Block3856b092011-10-20 11:56:00 +01001572 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001573 if (mTrack) mTrack->stop();
1574}
1575
1576void MediaPlayerService::AudioOutput::flush()
1577{
Steve Block3856b092011-10-20 11:56:00 +01001578 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001579 if (mTrack) mTrack->flush();
1580}
1581
1582void MediaPlayerService::AudioOutput::pause()
1583{
Steve Block3856b092011-10-20 11:56:00 +01001584 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001585 if (mTrack) mTrack->pause();
1586}
1587
1588void MediaPlayerService::AudioOutput::close()
1589{
Steve Block3856b092011-10-20 11:56:00 +01001590 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001591 delete mTrack;
1592 mTrack = 0;
1593}
1594
1595void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1596{
Steve Block3856b092011-10-20 11:56:00 +01001597 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001598 mLeftVolume = left;
1599 mRightVolume = right;
1600 if (mTrack) {
1601 mTrack->setVolume(left, right);
1602 }
1603}
1604
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001605status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1606{
1607 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1608 status_t res = NO_ERROR;
1609 if (mTrack) {
1610 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1611 } else {
1612 res = NO_INIT;
1613 }
1614 mPlaybackRatePermille = ratePermille;
1615 if (mSampleRateHz != 0) {
1616 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1617 }
1618 return res;
1619}
1620
Eric Laurent2beeb502010-07-16 07:43:46 -07001621status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1622{
Steve Block3856b092011-10-20 11:56:00 +01001623 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001624 mSendLevel = level;
1625 if (mTrack) {
1626 return mTrack->setAuxEffectSendLevel(level);
1627 }
1628 return NO_ERROR;
1629}
1630
1631status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1632{
Steve Block3856b092011-10-20 11:56:00 +01001633 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001634 mAuxEffectId = effectId;
1635 if (mTrack) {
1636 return mTrack->attachAuxEffect(effectId);
1637 }
1638 return NO_ERROR;
1639}
1640
Andreas Huber20111aa2009-07-14 16:56:47 -07001641// static
1642void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001643 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001644 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001645 if (event != AudioTrack::EVENT_MORE_DATA) {
1646 return;
1647 }
1648
Marco Nelissen6b74d672012-02-28 16:07:44 -08001649 CallbackData *data = (CallbackData*)cookie;
1650 data->lock();
1651 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001652 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001653 if (me == NULL) {
1654 // no output set, likely because the track was scheduled to be reused
1655 // by another player, but the format turned out to be incompatible.
1656 data->unlock();
1657 buffer->size = 0;
1658 return;
1659 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001660
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001661 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001662 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001663
Marco Nelissen6b74d672012-02-28 16:07:44 -08001664 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001665 // We've reached EOS but the audio track is not stopped yet,
1666 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001667
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001668 memset(buffer->raw, 0, buffer->size);
1669 actualSize = buffer->size;
1670 }
1671
1672 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001673 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001674}
1675
Marco Nelissen4110c102012-03-29 09:31:28 -07001676int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001677{
1678 return mSessionId;
1679}
1680
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001681#undef LOG_TAG
1682#define LOG_TAG "AudioCache"
1683MediaPlayerService::AudioCache::AudioCache(const char* name) :
1684 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1685 mError(NO_ERROR), mCommandComplete(false)
1686{
1687 // create ashmem heap
1688 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1689}
1690
1691uint32_t MediaPlayerService::AudioCache::latency () const
1692{
1693 return 0;
1694}
1695
1696float MediaPlayerService::AudioCache::msecsPerFrame() const
1697{
1698 return mMsecsPerFrame;
1699}
1700
Marco Nelissen4110c102012-03-29 09:31:28 -07001701status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001702{
1703 if (position == 0) return BAD_VALUE;
1704 *position = mSize;
1705 return NO_ERROR;
1706}
1707
Marco Nelissen4110c102012-03-29 09:31:28 -07001708status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1709{
1710 if (written == 0) return BAD_VALUE;
1711 *written = mSize;
1712 return NO_ERROR;
1713}
1714
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001715////////////////////////////////////////////////////////////////////////////////
1716
1717struct CallbackThread : public Thread {
1718 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1719 MediaPlayerBase::AudioSink::AudioCallback cb,
1720 void *cookie);
1721
1722protected:
1723 virtual ~CallbackThread();
1724
1725 virtual bool threadLoop();
1726
1727private:
1728 wp<MediaPlayerBase::AudioSink> mSink;
1729 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1730 void *mCookie;
1731 void *mBuffer;
1732 size_t mBufferSize;
1733
1734 CallbackThread(const CallbackThread &);
1735 CallbackThread &operator=(const CallbackThread &);
1736};
1737
1738CallbackThread::CallbackThread(
1739 const wp<MediaPlayerBase::AudioSink> &sink,
1740 MediaPlayerBase::AudioSink::AudioCallback cb,
1741 void *cookie)
1742 : mSink(sink),
1743 mCallback(cb),
1744 mCookie(cookie),
1745 mBuffer(NULL),
1746 mBufferSize(0) {
1747}
1748
1749CallbackThread::~CallbackThread() {
1750 if (mBuffer) {
1751 free(mBuffer);
1752 mBuffer = NULL;
1753 }
1754}
1755
1756bool CallbackThread::threadLoop() {
1757 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1758 if (sink == NULL) {
1759 return false;
1760 }
1761
1762 if (mBuffer == NULL) {
1763 mBufferSize = sink->bufferSize();
1764 mBuffer = malloc(mBufferSize);
1765 }
1766
1767 size_t actualSize =
1768 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1769
1770 if (actualSize > 0) {
1771 sink->write(mBuffer, actualSize);
1772 }
1773
1774 return true;
1775}
1776
1777////////////////////////////////////////////////////////////////////////////////
1778
Andreas Huber20111aa2009-07-14 16:56:47 -07001779status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001780 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1781 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001782 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001783{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001784 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001785 if (mHeap->getHeapID() < 0) {
1786 return NO_INIT;
1787 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001788
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001789 mSampleRate = sampleRate;
1790 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001791 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001792 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001793
1794 if (cb != NULL) {
1795 mCallbackThread = new CallbackThread(this, cb, cookie);
1796 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001797 return NO_ERROR;
1798}
1799
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001800void MediaPlayerService::AudioCache::start() {
1801 if (mCallbackThread != NULL) {
1802 mCallbackThread->run("AudioCache callback");
1803 }
1804}
1805
1806void MediaPlayerService::AudioCache::stop() {
1807 if (mCallbackThread != NULL) {
1808 mCallbackThread->requestExitAndWait();
1809 }
1810}
1811
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1813{
Steve Block3856b092011-10-20 11:56:00 +01001814 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001815 if ((buffer == 0) || (size == 0)) return size;
1816
1817 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1818 if (p == NULL) return NO_INIT;
1819 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001820 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001821 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001822 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001823 size = mHeap->getSize() - mSize;
1824 }
1825 memcpy(p, buffer, size);
1826 mSize += size;
1827 return size;
1828}
1829
1830// call with lock held
1831status_t MediaPlayerService::AudioCache::wait()
1832{
1833 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001834 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001835 mSignal.wait(mLock);
1836 }
1837 mCommandComplete = false;
1838
1839 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001840 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001841 } else {
Steve Block3856b092011-10-20 11:56:00 +01001842 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001843 }
1844 return mError;
1845}
1846
Gloria Wangb483c472011-04-11 17:23:27 -07001847void MediaPlayerService::AudioCache::notify(
1848 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001849{
Steve Block3856b092011-10-20 11:56:00 +01001850 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001851 AudioCache* p = static_cast<AudioCache*>(cookie);
1852
1853 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001854 switch (msg)
1855 {
1856 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001857 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001858 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001859 break;
1860 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001861 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001862 break;
1863 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001864 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001865 break;
1866 default:
Steve Block3856b092011-10-20 11:56:00 +01001867 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001868 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001869 }
1870
1871 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001872 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001873 p->mCommandComplete = true;
1874 p->mSignal.signal();
1875}
1876
Marco Nelissen4110c102012-03-29 09:31:28 -07001877int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001878{
1879 return 0;
1880}
1881
Gloria Wang7cf180c2011-02-19 18:37:57 -08001882void MediaPlayerService::addBatteryData(uint32_t params)
1883{
1884 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001885
1886 int32_t time = systemTime() / 1000000L;
1887
1888 // change audio output devices. This notification comes from AudioFlinger
1889 if ((params & kBatteryDataSpeakerOn)
1890 || (params & kBatteryDataOtherAudioDeviceOn)) {
1891
1892 int deviceOn[NUM_AUDIO_DEVICES];
1893 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1894 deviceOn[i] = 0;
1895 }
1896
1897 if ((params & kBatteryDataSpeakerOn)
1898 && (params & kBatteryDataOtherAudioDeviceOn)) {
1899 deviceOn[SPEAKER_AND_OTHER] = 1;
1900 } else if (params & kBatteryDataSpeakerOn) {
1901 deviceOn[SPEAKER] = 1;
1902 } else {
1903 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1904 }
1905
1906 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1907 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1908
1909 if (mBatteryAudio.refCount > 0) { // if playing audio
1910 if (!deviceOn[i]) {
1911 mBatteryAudio.lastTime[i] += time;
1912 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1913 mBatteryAudio.lastTime[i] = 0;
1914 } else {
1915 mBatteryAudio.lastTime[i] = 0 - time;
1916 }
1917 }
1918
1919 mBatteryAudio.deviceOn[i] = deviceOn[i];
1920 }
1921 }
1922 return;
1923 }
1924
1925 // an sudio stream is started
1926 if (params & kBatteryDataAudioFlingerStart) {
1927 // record the start time only if currently no other audio
1928 // is being played
1929 if (mBatteryAudio.refCount == 0) {
1930 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1931 if (mBatteryAudio.deviceOn[i]) {
1932 mBatteryAudio.lastTime[i] -= time;
1933 }
1934 }
1935 }
1936
1937 mBatteryAudio.refCount ++;
1938 return;
1939
1940 } else if (params & kBatteryDataAudioFlingerStop) {
1941 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001942 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001943 return;
1944 }
1945
1946 // record the stop time only if currently this is the only
1947 // audio being played
1948 if (mBatteryAudio.refCount == 1) {
1949 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1950 if (mBatteryAudio.deviceOn[i]) {
1951 mBatteryAudio.lastTime[i] += time;
1952 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1953 mBatteryAudio.lastTime[i] = 0;
1954 }
1955 }
1956 }
1957
1958 mBatteryAudio.refCount --;
1959 return;
1960 }
1961
Gloria Wang7cf180c2011-02-19 18:37:57 -08001962 int uid = IPCThreadState::self()->getCallingUid();
1963 if (uid == AID_MEDIA) {
1964 return;
1965 }
1966 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001967
1968 if (index < 0) { // create a new entry for this UID
1969 BatteryUsageInfo info;
1970 info.audioTotalTime = 0;
1971 info.videoTotalTime = 0;
1972 info.audioLastTime = 0;
1973 info.videoLastTime = 0;
1974 info.refCount = 0;
1975
Gloria Wang9ee159b2011-02-24 14:51:45 -08001976 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001977 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001978 return;
1979 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001980 }
1981
1982 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1983
1984 if (params & kBatteryDataCodecStarted) {
1985 if (params & kBatteryDataTrackAudio) {
1986 info.audioLastTime -= time;
1987 info.refCount ++;
1988 }
1989 if (params & kBatteryDataTrackVideo) {
1990 info.videoLastTime -= time;
1991 info.refCount ++;
1992 }
1993 } else {
1994 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001995 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001996 return;
1997 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001998 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001999 mBatteryData.removeItem(uid);
2000 return;
2001 }
2002
2003 if (params & kBatteryDataTrackAudio) {
2004 info.audioLastTime += time;
2005 info.refCount --;
2006 }
2007 if (params & kBatteryDataTrackVideo) {
2008 info.videoLastTime += time;
2009 info.refCount --;
2010 }
2011
2012 // no stream is being played by this UID
2013 if (info.refCount == 0) {
2014 info.audioTotalTime += info.audioLastTime;
2015 info.audioLastTime = 0;
2016 info.videoTotalTime += info.videoLastTime;
2017 info.videoLastTime = 0;
2018 }
2019 }
2020}
2021
2022status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2023 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002024
2025 // audio output devices usage
2026 int32_t time = systemTime() / 1000000L; //in ms
2027 int32_t totalTime;
2028
2029 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2030 totalTime = mBatteryAudio.totalTime[i];
2031
2032 if (mBatteryAudio.deviceOn[i]
2033 && (mBatteryAudio.lastTime[i] != 0)) {
2034 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2035 totalTime += tmpTime;
2036 }
2037
2038 reply->writeInt32(totalTime);
2039 // reset the total time
2040 mBatteryAudio.totalTime[i] = 0;
2041 }
2042
2043 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002044 BatteryUsageInfo info;
2045 int size = mBatteryData.size();
2046
2047 reply->writeInt32(size);
2048 int i = 0;
2049
2050 while (i < size) {
2051 info = mBatteryData.valueAt(i);
2052
2053 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2054 reply->writeInt32(info.audioTotalTime);
2055 reply->writeInt32(info.videoTotalTime);
2056
2057 info.audioTotalTime = 0;
2058 info.videoTotalTime = 0;
2059
2060 // remove the UID entry where no stream is being played
2061 if (info.refCount <= 0) {
2062 mBatteryData.removeItemsAt(i);
2063 size --;
2064 i --;
2065 }
2066 i++;
2067 }
2068 return NO_ERROR;
2069}
nikoa64c8c72009-07-20 15:07:26 -07002070} // namespace android