blob: ae55642effdca50c64d3b9ff051c58ad0885517c [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
Jeff Brown2013a542012-09-04 21:38:42 -070047#include <media/IRemoteDisplay.h>
48#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080049#include <media/MediaPlayerInterface.h>
50#include <media/mediarecorder.h>
51#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070052#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080053#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070054#include <media/MemoryLeakTrackUtil.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070055#include <media/stagefright/MediaErrors.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056
Dima Zavin64760242011-05-11 14:15:23 -070057#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070058
Gloria Wang7cf180c2011-02-19 18:37:57 -080059#include <private/android_filesystem_config.h>
60
James Dong559bf282012-03-28 10:29:14 -070061#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080062#include "MediaRecorderClient.h"
63#include "MediaPlayerService.h"
64#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070065#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066
67#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070068#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070069#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080070#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070071
Andreas Huber20111aa2009-07-14 16:56:47 -070072#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070073
Andreas Hubered3e3e02012-03-26 11:13:27 -070074#include "Crypto.h"
Andreas Huber35213f12012-08-29 11:41:50 -070075#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070076
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070077namespace {
nikoa64c8c72009-07-20 15:07:26 -070078using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070079using android::status_t;
80using android::OK;
81using android::BAD_VALUE;
82using android::NOT_ENOUGH_DATA;
83using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070084
85// Max number of entries in the filter.
86const int kMaxFilterSize = 64; // I pulled that out of thin air.
87
nikoa64c8c72009-07-20 15:07:26 -070088// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070089
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070090
91// Unmarshall a filter from a Parcel.
92// Filter format in a parcel:
93//
94// 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
95// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96// | number of entries (n) |
97// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98// | metadata type 1 |
99// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100// | metadata type 2 |
101// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102// ....
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104// | metadata type n |
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106//
107// @param p Parcel that should start with a filter.
108// @param[out] filter On exit contains the list of metadata type to be
109// filtered.
110// @param[out] status On exit contains the status code to be returned.
111// @return true if the parcel starts with a valid filter.
112bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700113 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700114 status_t *status)
115{
Nicolas Catania48290382009-07-10 13:53:06 -0700116 int32_t val;
117 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700118 {
Steve Block29357bc2012-01-06 19:20:56 +0000119 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700120 *status = NOT_ENOUGH_DATA;
121 return false;
122 }
123
Nicolas Catania48290382009-07-10 13:53:06 -0700124 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700125 {
Steve Block29357bc2012-01-06 19:20:56 +0000126 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 *status = BAD_VALUE;
128 return false;
129 }
130
Nicolas Catania48290382009-07-10 13:53:06 -0700131 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700132
133 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700134 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135
nikoa64c8c72009-07-20 15:07:26 -0700136 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700137
Nicolas Catania48290382009-07-10 13:53:06 -0700138
139 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140 {
Steve Block29357bc2012-01-06 19:20:56 +0000141 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700142 *status = NOT_ENOUGH_DATA;
143 return false;
144 }
145
nikoa64c8c72009-07-20 15:07:26 -0700146 const Metadata::Type *data =
147 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148
Nicolas Catania48290382009-07-10 13:53:06 -0700149 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 {
Steve Block29357bc2012-01-06 19:20:56 +0000151 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700152 *status = BAD_VALUE;
153 return false;
154 }
155
156 // TODO: The stl impl of vector would be more efficient here
157 // because it degenerates into a memcpy on pod types. Try to
158 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700159 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 {
Nicolas Catania48290382009-07-10 13:53:06 -0700161 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700162 ++data;
163 }
164 *status = OK;
165 return true;
166}
167
Nicolas Catania48290382009-07-10 13:53:06 -0700168// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169// @param val To be searched.
170// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700171bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700172{
173 // Deal with empty and ANY right away
174 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700175 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700176
Nicolas Catania48290382009-07-10 13:53:06 -0700177 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700178}
179
180} // anonymous namespace
181
182
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800183namespace android {
184
Dave Burked681bbb2011-08-30 14:39:17 +0100185static bool checkPermission(const char* permissionString) {
186#ifndef HAVE_ANDROID_OS
187 return true;
188#endif
189 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
190 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000191 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100192 return ok;
193}
194
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800195// 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 -0800196/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
197/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
198
199void MediaPlayerService::instantiate() {
200 defaultServiceManager()->addService(
201 String16("media.player"), new MediaPlayerService());
202}
203
204MediaPlayerService::MediaPlayerService()
205{
Steve Block3856b092011-10-20 11:56:00 +0100206 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800207 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800208
209 mBatteryAudio.refCount = 0;
210 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
211 mBatteryAudio.deviceOn[i] = 0;
212 mBatteryAudio.lastTime[i] = 0;
213 mBatteryAudio.totalTime[i] = 0;
214 }
215 // speaker is on by default
216 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700217
218 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800219}
220
221MediaPlayerService::~MediaPlayerService()
222{
Steve Block3856b092011-10-20 11:56:00 +0100223 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800224}
225
226sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(pid_t pid)
227{
Gloria Wangdac6a312009-10-29 15:46:37 -0700228 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
229 wp<MediaRecorderClient> w = recorder;
230 Mutex::Autolock lock(mLock);
231 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100232 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800233 return recorder;
234}
235
Gloria Wangdac6a312009-10-29 15:46:37 -0700236void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
237{
238 Mutex::Autolock lock(mLock);
239 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100240 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700241}
242
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800243sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever(pid_t pid)
244{
245 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100246 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800247 return retriever;
248}
249
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250sp<IMediaPlayer> MediaPlayerService::create(pid_t pid, const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100251 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252{
253 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700254
255 sp<Client> c = new Client(
256 this, pid, connId, client, audioSessionId,
257 IPCThreadState::self()->getCallingUid());
258
Steve Block3856b092011-10-20 11:56:00 +0100259 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100260 IPCThreadState::self()->getCallingUid());
261
262 wp<Client> w = c;
263 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800264 Mutex::Autolock lock(mLock);
265 mClients.add(w);
266 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800267 return c;
268}
269
Andreas Huber318ad9c2009-10-15 13:46:54 -0700270sp<IOMX> MediaPlayerService::getOMX() {
271 Mutex::Autolock autoLock(mLock);
272
273 if (mOMX.get() == NULL) {
274 mOMX = new OMX;
275 }
276
277 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700278}
279
Andreas Hubered3e3e02012-03-26 11:13:27 -0700280sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700281 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700282}
283
Jeff Brown2013a542012-09-04 21:38:42 -0700284sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
285 const sp<IRemoteDisplayClient>& client, const String8& iface) {
286 // TODO: implement me!
287 return NULL;
288}
289
Andreas Huberab1bd842012-08-30 14:51:40 -0700290status_t MediaPlayerService::enableRemoteDisplay(const char *iface) {
Andreas Huber35213f12012-08-29 11:41:50 -0700291 Mutex::Autolock autoLock(mLock);
292
Andreas Huberab1bd842012-08-30 14:51:40 -0700293 if (iface != NULL) {
294 if (mRemoteDisplay != NULL) {
295 return INVALID_OPERATION;
296 }
297
Andreas Huber35213f12012-08-29 11:41:50 -0700298 mRemoteDisplay = new RemoteDisplay;
299
Andreas Huberab1bd842012-08-30 14:51:40 -0700300 status_t err = mRemoteDisplay->start(iface);
Andreas Huber35213f12012-08-29 11:41:50 -0700301
302 if (err != OK) {
303 mRemoteDisplay.clear();
304 return err;
305 }
306
307 return OK;
Andreas Huberab1bd842012-08-30 14:51:40 -0700308 }
309
310 if (mRemoteDisplay != NULL) {
Andreas Huber35213f12012-08-29 11:41:50 -0700311 mRemoteDisplay->stop();
312 mRemoteDisplay.clear();
313 }
314
315 return OK;
316}
317
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800318status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
319{
320 const size_t SIZE = 256;
321 char buffer[SIZE];
322 String8 result;
323
324 result.append(" AudioCache\n");
325 if (mHeap != 0) {
326 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
327 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
328 result.append(buffer);
329 }
330 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
331 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
332 result.append(buffer);
333 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
334 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
335 result.append(buffer);
336 ::write(fd, result.string(), result.size());
337 return NO_ERROR;
338}
339
340status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
341{
342 const size_t SIZE = 256;
343 char buffer[SIZE];
344 String8 result;
345
346 result.append(" AudioOutput\n");
347 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
348 mStreamType, mLeftVolume, mRightVolume);
349 result.append(buffer);
350 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800351 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800352 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700353 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
354 mAuxEffectId, mSendLevel);
355 result.append(buffer);
356
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800357 ::write(fd, result.string(), result.size());
358 if (mTrack != 0) {
359 mTrack->dump(fd, args);
360 }
361 return NO_ERROR;
362}
363
364status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
365{
366 const size_t SIZE = 256;
367 char buffer[SIZE];
368 String8 result;
369 result.append(" Client\n");
370 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
371 mPid, mConnId, mStatus, mLoop?"true": "false");
372 result.append(buffer);
373 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700374 if (mPlayer != NULL) {
375 mPlayer->dump(fd, args);
376 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800377 if (mAudioOutput != 0) {
378 mAudioOutput->dump(fd, args);
379 }
380 write(fd, "\n", 1);
381 return NO_ERROR;
382}
383
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800384status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
385{
386 const size_t SIZE = 256;
387 char buffer[SIZE];
388 String8 result;
389 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
390 snprintf(buffer, SIZE, "Permission Denial: "
391 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
392 IPCThreadState::self()->getCallingPid(),
393 IPCThreadState::self()->getCallingUid());
394 result.append(buffer);
395 } else {
396 Mutex::Autolock lock(mLock);
397 for (int i = 0, n = mClients.size(); i < n; ++i) {
398 sp<Client> c = mClients[i].promote();
399 if (c != 0) c->dump(fd, args);
400 }
James Dongb9141222010-07-08 11:16:11 -0700401 if (mMediaRecorderClients.size() == 0) {
402 result.append(" No media recorder client\n\n");
403 } else {
404 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
405 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700406 if (c != 0) {
407 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
408 result.append(buffer);
409 write(fd, result.string(), result.size());
410 result = "\n";
411 c->dump(fd, args);
412 }
James Dongb9141222010-07-08 11:16:11 -0700413 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700414 }
415
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700417 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418 FILE *f = fopen(buffer, "r");
419 if (f) {
420 while (!feof(f)) {
421 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700422 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700424 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800425 strstr(buffer, " /system/media/")) {
426 result.append(" ");
427 result.append(buffer);
428 }
429 }
430 fclose(f);
431 } else {
432 result.append("couldn't open ");
433 result.append(buffer);
434 result.append("\n");
435 }
436
Glenn Kasten0512ab52011-05-04 17:58:57 -0700437 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 DIR *d = opendir(buffer);
439 if (d) {
440 struct dirent *ent;
441 while((ent = readdir(d)) != NULL) {
442 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700443 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 struct stat s;
445 if (lstat(buffer, &s) == 0) {
446 if ((s.st_mode & S_IFMT) == S_IFLNK) {
447 char linkto[256];
448 int len = readlink(buffer, linkto, sizeof(linkto));
449 if(len > 0) {
450 if(len > 255) {
451 linkto[252] = '.';
452 linkto[253] = '.';
453 linkto[254] = '.';
454 linkto[255] = 0;
455 } else {
456 linkto[len] = 0;
457 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700458 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800459 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700460 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800461 strstr(linkto, "/system/media/") == linkto) {
462 result.append(" ");
463 result.append(buffer);
464 result.append(" -> ");
465 result.append(linkto);
466 result.append("\n");
467 }
468 }
469 } else {
470 result.append(" unexpected type for ");
471 result.append(buffer);
472 result.append("\n");
473 }
474 }
475 }
476 }
477 closedir(d);
478 } else {
479 result.append("couldn't open ");
480 result.append(buffer);
481 result.append("\n");
482 }
483
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 bool dumpMem = false;
485 for (size_t i = 0; i < args.size(); i++) {
486 if (args[i] == String16("-m")) {
487 dumpMem = true;
488 }
489 }
490 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700491 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 }
494 write(fd, result.string(), result.size());
495 return NO_ERROR;
496}
497
498void MediaPlayerService::removeClient(wp<Client> client)
499{
500 Mutex::Autolock lock(mLock);
501 mClients.remove(client);
502}
503
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700504MediaPlayerService::Client::Client(
505 const sp<MediaPlayerService>& service, pid_t pid,
506 int32_t connId, const sp<IMediaPlayerClient>& client,
507 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800508{
Steve Block3856b092011-10-20 11:56:00 +0100509 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 mPid = pid;
511 mConnId = connId;
512 mService = service;
513 mClient = client;
514 mLoop = false;
515 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700516 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700517 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800518 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700519
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000521 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522 mAntagonizer = new Antagonizer(notify, this);
523#endif
524}
525
526MediaPlayerService::Client::~Client()
527{
Steve Block3856b092011-10-20 11:56:00 +0100528 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 mAudioOutput.clear();
530 wp<Client> client(this);
531 disconnect();
532 mService->removeClient(client);
533}
534
535void MediaPlayerService::Client::disconnect()
536{
Steve Block3856b092011-10-20 11:56:00 +0100537 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 // grab local reference and clear main reference to prevent future
539 // access to object
540 sp<MediaPlayerBase> p;
541 {
542 Mutex::Autolock l(mLock);
543 p = mPlayer;
544 }
Dave Sparks795fa582009-03-24 17:57:12 -0700545 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700546
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 mPlayer.clear();
548
549 // clear the notification to prevent callbacks to dead client
550 // and reset the player. We assume the player will serialize
551 // access to itself if necessary.
552 if (p != 0) {
553 p->setNotifyCallback(0, 0);
554#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000555 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800556 mAntagonizer->kill();
557#endif
558 p->reset();
559 }
560
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700561 disconnectNativeWindow();
562
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 IPCThreadState::self()->flushCommands();
564}
565
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
567{
568 // determine if we have the right player type
569 sp<MediaPlayerBase> p = mPlayer;
570 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100571 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800572 p.clear();
573 }
574 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700575 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700577
Jason Simmonsdb29e522011-08-12 13:46:55 -0700578 if (p != NULL) {
579 p->setUID(mUID);
580 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700581
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582 return p;
583}
584
John Grossmanc795b642012-02-22 15:38:35 -0800585sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
586 player_type playerType)
587{
588 ALOGV("player type = %d", playerType);
589
590 // create the right type of player
591 sp<MediaPlayerBase> p = createPlayer(playerType);
592 if (p == NULL) {
593 return p;
594 }
595
596 if (!p->hardwareOutput()) {
597 mAudioOutput = new AudioOutput(mAudioSessionId);
598 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
599 }
600
601 return p;
602}
603
604void MediaPlayerService::Client::setDataSource_post(
605 const sp<MediaPlayerBase>& p,
606 status_t status)
607{
608 ALOGV(" setDataSource");
609 mStatus = status;
610 if (mStatus != OK) {
611 ALOGE(" error: %d", mStatus);
612 return;
613 }
614
615 // Set the re-transmission endpoint if one was chosen.
616 if (mRetransmitEndpointValid) {
617 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
618 if (mStatus != NO_ERROR) {
619 ALOGE("setRetransmitEndpoint error: %d", mStatus);
620 }
621 }
622
623 if (mStatus == OK) {
624 mPlayer = p;
625 }
626}
627
Andreas Huber2db84552010-01-28 11:19:57 -0800628status_t MediaPlayerService::Client::setDataSource(
629 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800630{
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632 if (url == NULL)
633 return UNKNOWN_ERROR;
634
Dave Burked681bbb2011-08-30 14:39:17 +0100635 if ((strncmp(url, "http://", 7) == 0) ||
636 (strncmp(url, "https://", 8) == 0) ||
637 (strncmp(url, "rtsp://", 7) == 0)) {
638 if (!checkPermission("android.permission.INTERNET")) {
639 return PERMISSION_DENIED;
640 }
641 }
642
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643 if (strncmp(url, "content://", 10) == 0) {
644 // get a filedescriptor for the content Uri and
645 // pass it to the setDataSource(fd) method
646
647 String16 url16(url);
648 int fd = android::openContentProviderFile(url16);
649 if (fd < 0)
650 {
Steve Block29357bc2012-01-06 19:20:56 +0000651 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652 return UNKNOWN_ERROR;
653 }
654 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
655 close(fd);
656 return mStatus;
657 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700658 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800659 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
660 if (p == NULL) {
661 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662 }
663
John Grossmanc795b642012-02-22 15:38:35 -0800664 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 return mStatus;
666 }
667}
668
669status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
670{
Steve Block3856b092011-10-20 11:56:00 +0100671 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672 struct stat sb;
673 int ret = fstat(fd, &sb);
674 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000675 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 return UNKNOWN_ERROR;
677 }
678
Steve Block3856b092011-10-20 11:56:00 +0100679 ALOGV("st_dev = %llu", sb.st_dev);
680 ALOGV("st_mode = %u", sb.st_mode);
681 ALOGV("st_uid = %lu", sb.st_uid);
682 ALOGV("st_gid = %lu", sb.st_gid);
683 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684
685 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000686 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687 ::close(fd);
688 return UNKNOWN_ERROR;
689 }
690 if (offset + length > sb.st_size) {
691 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100692 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 }
694
John Grossman44a7e422012-06-21 17:29:24 -0700695 player_type playerType = MediaPlayerFactory::getPlayerType(this,
696 fd,
697 offset,
698 length);
John Grossmanc795b642012-02-22 15:38:35 -0800699 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
700 if (p == NULL) {
701 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800702 }
703
704 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800705 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 return mStatus;
707}
708
Andreas Hubere2b10282010-11-23 11:41:34 -0800709status_t MediaPlayerService::Client::setDataSource(
710 const sp<IStreamSource> &source) {
711 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700712 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800713 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800714 if (p == NULL) {
715 return NO_INIT;
716 }
717
Andreas Hubere2b10282010-11-23 11:41:34 -0800718 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800719 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800720 return mStatus;
721}
722
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700723void MediaPlayerService::Client::disconnectNativeWindow() {
724 if (mConnectedWindow != NULL) {
725 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
726 NATIVE_WINDOW_API_MEDIA);
727
728 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000729 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700730 strerror(-err), err);
731 }
732 }
733 mConnectedWindow.clear();
734}
735
Glenn Kasten11731182011-02-08 17:26:17 -0800736status_t MediaPlayerService::Client::setVideoSurfaceTexture(
737 const sp<ISurfaceTexture>& surfaceTexture)
738{
Steve Block3856b092011-10-20 11:56:00 +0100739 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800740 sp<MediaPlayerBase> p = getPlayer();
741 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700742
743 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
744 surfaceTexture->asBinder());
745 if (mConnectedWindowBinder == binder) {
746 return OK;
747 }
748
749 sp<ANativeWindow> anw;
750 if (surfaceTexture != NULL) {
751 anw = new SurfaceTextureClient(surfaceTexture);
752 status_t err = native_window_api_connect(anw.get(),
753 NATIVE_WINDOW_API_MEDIA);
754
755 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000756 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700757 // Note that we must do the reset before disconnecting from the ANW.
758 // Otherwise queue/dequeue calls could be made on the disconnected
759 // ANW, which may result in errors.
760 reset();
761
762 disconnectNativeWindow();
763
764 return err;
765 }
766 }
767
768 // Note that we must set the player's new SurfaceTexture before
769 // disconnecting the old one. Otherwise queue/dequeue calls could be made
770 // on the disconnected ANW, which may result in errors.
771 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
772
773 disconnectNativeWindow();
774
775 mConnectedWindow = anw;
776
777 if (err == OK) {
778 mConnectedWindowBinder = binder;
779 } else {
780 disconnectNativeWindow();
781 }
782
783 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800784}
785
Nicolas Catania1d187f12009-05-12 23:25:55 -0700786status_t MediaPlayerService::Client::invoke(const Parcel& request,
787 Parcel *reply)
788{
789 sp<MediaPlayerBase> p = getPlayer();
790 if (p == NULL) return UNKNOWN_ERROR;
791 return p->invoke(request, reply);
792}
793
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700794// This call doesn't need to access the native player.
795status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
796{
797 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700798 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700799
Nicolas Catania48290382009-07-10 13:53:06 -0700800 if (unmarshallFilter(filter, &allow, &status) &&
801 unmarshallFilter(filter, &drop, &status)) {
802 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700803
804 mMetadataAllow = allow;
805 mMetadataDrop = drop;
806 }
807 return status;
808}
809
Nicolas Catania48290382009-07-10 13:53:06 -0700810status_t MediaPlayerService::Client::getMetadata(
811 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700812{
nikoa64c8c72009-07-20 15:07:26 -0700813 sp<MediaPlayerBase> player = getPlayer();
814 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700815
nikod608a812009-07-16 16:39:53 -0700816 status_t status;
817 // Placeholder for the return code, updated by the caller.
818 reply->writeInt32(-1);
819
nikoa64c8c72009-07-20 15:07:26 -0700820 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700821
822 // We don't block notifications while we fetch the data. We clear
823 // mMetadataUpdated first so we don't lose notifications happening
824 // during the rest of this call.
825 {
826 Mutex::Autolock lock(mLock);
827 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700828 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700829 }
830 mMetadataUpdated.clear();
831 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700832
nikoa64c8c72009-07-20 15:07:26 -0700833 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700834
nikoa64c8c72009-07-20 15:07:26 -0700835 metadata.appendHeader();
836 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700837
838 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700839 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000840 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700841 return status;
842 }
843
844 // FIXME: Implement filtering on the result. Not critical since
845 // filtering takes place on the update notifications already. This
846 // would be when all the metadata are fetch and a filter is set.
847
nikod608a812009-07-16 16:39:53 -0700848 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700849 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700850 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700851}
852
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800853status_t MediaPlayerService::Client::prepareAsync()
854{
Steve Block3856b092011-10-20 11:56:00 +0100855 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800856 sp<MediaPlayerBase> p = getPlayer();
857 if (p == 0) return UNKNOWN_ERROR;
858 status_t ret = p->prepareAsync();
859#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000860 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 if (ret == NO_ERROR) mAntagonizer->start();
862#endif
863 return ret;
864}
865
866status_t MediaPlayerService::Client::start()
867{
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800869 sp<MediaPlayerBase> p = getPlayer();
870 if (p == 0) return UNKNOWN_ERROR;
871 p->setLooping(mLoop);
872 return p->start();
873}
874
875status_t MediaPlayerService::Client::stop()
876{
Steve Block3856b092011-10-20 11:56:00 +0100877 ALOGV("[%d] stop", 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->stop();
881}
882
883status_t MediaPlayerService::Client::pause()
884{
Steve Block3856b092011-10-20 11:56:00 +0100885 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800886 sp<MediaPlayerBase> p = getPlayer();
887 if (p == 0) return UNKNOWN_ERROR;
888 return p->pause();
889}
890
891status_t MediaPlayerService::Client::isPlaying(bool* state)
892{
893 *state = false;
894 sp<MediaPlayerBase> p = getPlayer();
895 if (p == 0) return UNKNOWN_ERROR;
896 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100897 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800898 return NO_ERROR;
899}
900
901status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
902{
Steve Block3856b092011-10-20 11:56:00 +0100903 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800904 sp<MediaPlayerBase> p = getPlayer();
905 if (p == 0) return UNKNOWN_ERROR;
906 status_t ret = p->getCurrentPosition(msec);
907 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000910 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800911 }
912 return ret;
913}
914
915status_t MediaPlayerService::Client::getDuration(int *msec)
916{
Steve Block3856b092011-10-20 11:56:00 +0100917 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800918 sp<MediaPlayerBase> p = getPlayer();
919 if (p == 0) return UNKNOWN_ERROR;
920 status_t ret = p->getDuration(msec);
921 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100922 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800923 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000924 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800925 }
926 return ret;
927}
928
Marco Nelissen6b74d672012-02-28 16:07:44 -0800929status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
930 ALOGV("setNextPlayer");
931 Mutex::Autolock l(mLock);
932 sp<Client> c = static_cast<Client*>(player.get());
933 mNextClient = c;
934 if (mAudioOutput != NULL && c != NULL) {
935 mAudioOutput->setNextOutput(c->mAudioOutput);
936 } else {
937 ALOGE("no current audio output");
938 }
939 return OK;
940}
941
942
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800943status_t MediaPlayerService::Client::seekTo(int msec)
944{
Steve Block3856b092011-10-20 11:56:00 +0100945 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800946 sp<MediaPlayerBase> p = getPlayer();
947 if (p == 0) return UNKNOWN_ERROR;
948 return p->seekTo(msec);
949}
950
951status_t MediaPlayerService::Client::reset()
952{
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800954 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800955 sp<MediaPlayerBase> p = getPlayer();
956 if (p == 0) return UNKNOWN_ERROR;
957 return p->reset();
958}
959
Glenn Kastenfff6d712012-01-12 16:38:12 -0800960status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800961{
Steve Block3856b092011-10-20 11:56:00 +0100962 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800963 // TODO: for hardware output, call player instead
964 Mutex::Autolock l(mLock);
965 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
966 return NO_ERROR;
967}
968
969status_t MediaPlayerService::Client::setLooping(int loop)
970{
Steve Block3856b092011-10-20 11:56:00 +0100971 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800972 mLoop = loop;
973 sp<MediaPlayerBase> p = getPlayer();
974 if (p != 0) return p->setLooping(loop);
975 return NO_ERROR;
976}
977
978status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
979{
Steve Block3856b092011-10-20 11:56:00 +0100980 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800981
982 // for hardware output, call player instead
983 sp<MediaPlayerBase> p = getPlayer();
984 {
985 Mutex::Autolock l(mLock);
986 if (p != 0 && p->hardwareOutput()) {
987 MediaPlayerHWInterface* hwp =
988 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
989 return hwp->setVolume(leftVolume, rightVolume);
990 } else {
991 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
992 return NO_ERROR;
993 }
994 }
995
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800996 return NO_ERROR;
997}
998
Eric Laurent2beeb502010-07-16 07:43:46 -0700999status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1000{
Steve Block3856b092011-10-20 11:56:00 +01001001 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001002 Mutex::Autolock l(mLock);
1003 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1004 return NO_ERROR;
1005}
1006
1007status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1008{
Steve Block3856b092011-10-20 11:56:00 +01001009 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001010 Mutex::Autolock l(mLock);
1011 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1012 return NO_ERROR;
1013}
Nicolas Catania48290382009-07-10 13:53:06 -07001014
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001015status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001016 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001017 sp<MediaPlayerBase> p = getPlayer();
1018 if (p == 0) return UNKNOWN_ERROR;
1019 return p->setParameter(key, request);
1020}
1021
1022status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001023 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001024 sp<MediaPlayerBase> p = getPlayer();
1025 if (p == 0) return UNKNOWN_ERROR;
1026 return p->getParameter(key, reply);
1027}
1028
John Grossmanc795b642012-02-22 15:38:35 -08001029status_t MediaPlayerService::Client::setRetransmitEndpoint(
1030 const struct sockaddr_in* endpoint) {
1031
1032 if (NULL != endpoint) {
1033 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1034 uint16_t p = ntohs(endpoint->sin_port);
1035 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1036 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1037 } else {
1038 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1039 }
1040
1041 sp<MediaPlayerBase> p = getPlayer();
1042
1043 // Right now, the only valid time to set a retransmit endpoint is before
1044 // player selection has been made (since the presence or absence of a
1045 // retransmit endpoint is going to determine which player is selected during
1046 // setDataSource).
1047 if (p != 0) return INVALID_OPERATION;
1048
1049 if (NULL != endpoint) {
1050 mRetransmitEndpoint = *endpoint;
1051 mRetransmitEndpointValid = true;
1052 } else {
1053 mRetransmitEndpointValid = false;
1054 }
1055
1056 return NO_ERROR;
1057}
1058
John Grossman44a7e422012-06-21 17:29:24 -07001059status_t MediaPlayerService::Client::getRetransmitEndpoint(
1060 struct sockaddr_in* endpoint)
1061{
1062 if (NULL == endpoint)
1063 return BAD_VALUE;
1064
1065 sp<MediaPlayerBase> p = getPlayer();
1066
1067 if (p != NULL)
1068 return p->getRetransmitEndpoint(endpoint);
1069
1070 if (!mRetransmitEndpointValid)
1071 return NO_INIT;
1072
1073 *endpoint = mRetransmitEndpoint;
1074
1075 return NO_ERROR;
1076}
1077
Gloria Wangb483c472011-04-11 17:23:27 -07001078void MediaPlayerService::Client::notify(
1079 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001080{
1081 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001082 if (client == NULL) {
1083 return;
1084 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001085
James Dongb8a98252012-08-26 16:13:03 -07001086 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001087 {
1088 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001089 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001090 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001091 if (client->mAudioOutput != NULL)
1092 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001093 client->mNextClient->start();
1094 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1095 }
1096 }
1097
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001098 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001099 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001100 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001101
1102 if(client->shouldDropMetadata(metadata_type)) {
1103 return;
1104 }
1105
1106 // Update the list of metadata that have changed. getMetadata
1107 // also access mMetadataUpdated and clears it.
1108 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001109 }
James Dongb8a98252012-08-26 16:13:03 -07001110
1111 if (c != NULL) {
1112 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1113 c->notify(msg, ext1, ext2, obj);
1114 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001115}
1116
Nicolas Catania48290382009-07-10 13:53:06 -07001117
nikoa64c8c72009-07-20 15:07:26 -07001118bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001119{
Nicolas Catania48290382009-07-10 13:53:06 -07001120 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001121
Nicolas Catania48290382009-07-10 13:53:06 -07001122 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001123 return true;
1124 }
1125
Nicolas Catania48290382009-07-10 13:53:06 -07001126 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001127 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001128 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001129 return true;
1130 }
1131}
1132
Nicolas Catania48290382009-07-10 13:53:06 -07001133
nikoa64c8c72009-07-20 15:07:26 -07001134void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001135 Mutex::Autolock lock(mLock);
1136 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1137 mMetadataUpdated.add(metadata_type);
1138 }
1139}
1140
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001141#if CALLBACK_ANTAGONIZER
1142const int Antagonizer::interval = 10000; // 10 msecs
1143
1144Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1145 mExit(false), mActive(false), mClient(client), mCb(cb)
1146{
1147 createThread(callbackThread, this);
1148}
1149
1150void Antagonizer::kill()
1151{
1152 Mutex::Autolock _l(mLock);
1153 mActive = false;
1154 mExit = true;
1155 mCondition.wait(mLock);
1156}
1157
1158int Antagonizer::callbackThread(void* user)
1159{
Steve Blockb8a80522011-12-20 16:23:08 +00001160 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001161 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1162 while (!p->mExit) {
1163 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001164 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001165 p->mCb(p->mClient, 0, 0, 0);
1166 }
1167 usleep(interval);
1168 }
1169 Mutex::Autolock _l(p->mLock);
1170 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001171 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001172 return 0;
1173}
1174#endif
1175
1176static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1177
Glenn Kastene1c39622012-01-04 09:36:37 -08001178sp<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 -08001179{
Steve Block3856b092011-10-20 11:56:00 +01001180 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001181 sp<MemoryBase> mem;
1182 sp<MediaPlayerBase> player;
1183
1184 // Protect our precious, precious DRMd ringtones by only allowing
1185 // decoding of http, but not filesystem paths or content Uris.
1186 // If the application wants to decode those, it should open a
1187 // filedescriptor for them and use that.
1188 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001189 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001190 return mem;
1191 }
1192
John Grossman44a7e422012-06-21 17:29:24 -07001193 player_type playerType =
1194 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001195 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001196
1197 // create the right type of player
1198 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001199 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001200 if (player == NULL) goto Exit;
1201 if (player->hardwareOutput()) goto Exit;
1202
1203 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1204
1205 // set data source
1206 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1207
Steve Block3856b092011-10-20 11:56:00 +01001208 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001209 player->prepareAsync();
1210
Steve Block3856b092011-10-20 11:56:00 +01001211 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001212 if (cache->wait() != NO_ERROR) goto Exit;
1213
Steve Block3856b092011-10-20 11:56:00 +01001214 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001215 player->start();
1216
Steve Block3856b092011-10-20 11:56:00 +01001217 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001218 cache->wait();
1219 // in case of error, return what was successfully decoded.
1220 if (cache->size() == 0) {
1221 goto Exit;
1222 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001223
1224 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1225 *pSampleRate = cache->sampleRate();
1226 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001227 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001228 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 -08001229
1230Exit:
1231 if (player != 0) player->reset();
1232 return mem;
1233}
1234
Glenn Kastene1c39622012-01-04 09:36:37 -08001235sp<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 -08001236{
Steve Block3856b092011-10-20 11:56:00 +01001237 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001238 sp<MemoryBase> mem;
1239 sp<MediaPlayerBase> player;
1240
John Grossman44a7e422012-06-21 17:29:24 -07001241 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1242 fd,
1243 offset,
1244 length);
Steve Block3856b092011-10-20 11:56:00 +01001245 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001246
1247 // create the right type of player
1248 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001249 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250 if (player == NULL) goto Exit;
1251 if (player->hardwareOutput()) goto Exit;
1252
1253 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1254
1255 // set data source
1256 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1257
Steve Block3856b092011-10-20 11:56:00 +01001258 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001259 player->prepareAsync();
1260
Steve Block3856b092011-10-20 11:56:00 +01001261 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001262 if (cache->wait() != NO_ERROR) goto Exit;
1263
Steve Block3856b092011-10-20 11:56:00 +01001264 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 player->start();
1266
Steve Block3856b092011-10-20 11:56:00 +01001267 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001268 cache->wait();
1269 // in case of error, return what was successfully decoded.
1270 if (cache->size() == 0) {
1271 goto Exit;
1272 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001273
1274 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1275 *pSampleRate = cache->sampleRate();
1276 *pNumChannels = cache->channelCount();
1277 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001278 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 -08001279
1280Exit:
1281 if (player != 0) player->reset();
1282 ::close(fd);
1283 return mem;
1284}
1285
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001286
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287#undef LOG_TAG
1288#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001289MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001290 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001291 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001292 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001293 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001294 mSessionId(sessionId),
1295 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001296 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001298 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001299 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300 mLeftVolume = 1.0;
1301 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001302 mPlaybackRatePermille = 1000;
1303 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001304 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001305 mAuxEffectId = 0;
1306 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001307 setMinBufferCount();
1308}
1309
1310MediaPlayerService::AudioOutput::~AudioOutput()
1311{
1312 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001313 delete mRecycledTrack;
1314 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001315}
1316
1317void MediaPlayerService::AudioOutput::setMinBufferCount()
1318{
1319 char value[PROPERTY_VALUE_MAX];
1320 if (property_get("ro.kernel.qemu", value, 0)) {
1321 mIsOnEmulator = true;
1322 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1323 }
1324}
1325
1326bool MediaPlayerService::AudioOutput::isOnEmulator()
1327{
1328 setMinBufferCount();
1329 return mIsOnEmulator;
1330}
1331
1332int MediaPlayerService::AudioOutput::getMinBufferCount()
1333{
1334 setMinBufferCount();
1335 return mMinBufferCount;
1336}
1337
1338ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1339{
1340 if (mTrack == 0) return NO_INIT;
1341 return mTrack->frameCount() * frameSize();
1342}
1343
1344ssize_t MediaPlayerService::AudioOutput::frameCount() const
1345{
1346 if (mTrack == 0) return NO_INIT;
1347 return mTrack->frameCount();
1348}
1349
1350ssize_t MediaPlayerService::AudioOutput::channelCount() const
1351{
1352 if (mTrack == 0) return NO_INIT;
1353 return mTrack->channelCount();
1354}
1355
1356ssize_t MediaPlayerService::AudioOutput::frameSize() const
1357{
1358 if (mTrack == 0) return NO_INIT;
1359 return mTrack->frameSize();
1360}
1361
1362uint32_t MediaPlayerService::AudioOutput::latency () const
1363{
Eric Laurentdb354e52012-03-05 17:27:11 -08001364 if (mTrack == 0) return 0;
1365 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001366}
1367
1368float MediaPlayerService::AudioOutput::msecsPerFrame() const
1369{
1370 return mMsecsPerFrame;
1371}
1372
Marco Nelissen4110c102012-03-29 09:31:28 -07001373status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001374{
1375 if (mTrack == 0) return NO_INIT;
1376 return mTrack->getPosition(position);
1377}
1378
Marco Nelissen4110c102012-03-29 09:31:28 -07001379status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1380{
1381 if (mTrack == 0) return NO_INIT;
1382 *frameswritten = mBytesWritten / frameSize();
1383 return OK;
1384}
1385
Andreas Huber20111aa2009-07-14 16:56:47 -07001386status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001387 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1388 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001389 AudioCallback cb, void *cookie,
1390 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001391{
Andreas Huber20111aa2009-07-14 16:56:47 -07001392 mCallback = cb;
1393 mCallbackCookie = cookie;
1394
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001395 // Check argument "bufferCount" against the mininum buffer count
1396 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001397 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001398 bufferCount = mMinBufferCount;
1399
1400 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001401 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1402 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001403 int afSampleRate;
1404 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001405 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001406
1407 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1408 return NO_INIT;
1409 }
1410 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1411 return NO_INIT;
1412 }
1413
1414 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001415
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001416 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001417 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001418 if (0 == channelMask) {
1419 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1420 return NO_INIT;
1421 }
1422 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001423
Andreas Huber20111aa2009-07-14 16:56:47 -07001424 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001425 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001426 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001427 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001428 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001429 mStreamType,
1430 sampleRate,
1431 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001432 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001433 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001434 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001435 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001436 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001437 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001438 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001439 } else {
1440 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001441 mStreamType,
1442 sampleRate,
1443 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001444 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001445 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001446 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001447 NULL,
1448 NULL,
1449 0,
1450 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001451 }
1452
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001453 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001454 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001455 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001456 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001457 return NO_INIT;
1458 }
1459
Marco Nelissen67295b52012-06-11 14:52:53 -07001460
1461 if (mRecycledTrack) {
1462 // check if the existing track can be reused as-is, or if a new track needs to be created.
1463
1464 bool reuse = true;
1465 if ((mCallbackData == NULL && mCallback != NULL) ||
1466 (mCallbackData != NULL && mCallback == NULL)) {
1467 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1468 ALOGV("can't chain callback and write");
1469 reuse = false;
1470 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1471 (mRecycledTrack->channelCount() != channelCount) ||
1472 (mRecycledTrack->frameCount() != t->frameCount())) {
1473 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1474 mRecycledTrack->getSampleRate(), sampleRate,
1475 mRecycledTrack->channelCount(), channelCount,
1476 mRecycledTrack->frameCount(), t->frameCount());
1477 reuse = false;
1478 } else if (flags != mFlags) {
1479 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1480 reuse = false;
1481 }
1482 if (reuse) {
1483 ALOGV("chaining to next output");
1484 close();
1485 mTrack = mRecycledTrack;
1486 mRecycledTrack = NULL;
1487 if (mCallbackData != NULL) {
1488 mCallbackData->setOutput(this);
1489 }
1490 delete t;
1491 delete newcbd;
1492 return OK;
1493 }
1494
1495 // if we're not going to reuse the track, unblock and flush it
1496 if (mCallbackData != NULL) {
1497 mCallbackData->setOutput(NULL);
1498 mCallbackData->endTrackSwitch();
1499 }
1500 mRecycledTrack->flush();
1501 delete mRecycledTrack;
1502 mRecycledTrack = NULL;
1503 delete mCallbackData;
1504 mCallbackData = NULL;
1505 close();
1506 }
1507
1508 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001509 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001510 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001511
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001512 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001513 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001514 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001515 uint32_t pos;
1516 if (t->getPosition(&pos) == OK) {
1517 mBytesWritten = uint64_t(pos) * t->frameSize();
1518 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001519 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001520
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001521 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1522 if (res != NO_ERROR) {
1523 return res;
1524 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001525 t->setAuxEffectSendLevel(mSendLevel);
1526 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001527}
1528
1529void MediaPlayerService::AudioOutput::start()
1530{
Steve Block3856b092011-10-20 11:56:00 +01001531 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001532 if (mCallbackData != NULL) {
1533 mCallbackData->endTrackSwitch();
1534 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001535 if (mTrack) {
1536 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001537 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001538 mTrack->start();
1539 }
1540}
1541
Marco Nelissen6b74d672012-02-28 16:07:44 -08001542void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1543 mNextOutput = nextOutput;
1544}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001545
1546
Marco Nelissen6b74d672012-02-28 16:07:44 -08001547void MediaPlayerService::AudioOutput::switchToNextOutput() {
1548 ALOGV("switchToNextOutput");
1549 if (mNextOutput != NULL) {
1550 if (mCallbackData != NULL) {
1551 mCallbackData->beginTrackSwitch();
1552 }
1553 delete mNextOutput->mCallbackData;
1554 mNextOutput->mCallbackData = mCallbackData;
1555 mCallbackData = NULL;
1556 mNextOutput->mRecycledTrack = mTrack;
1557 mTrack = NULL;
1558 mNextOutput->mSampleRateHz = mSampleRateHz;
1559 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001560 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001561 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001562 }
1563}
1564
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001565ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1566{
Andreas Huber20111aa2009-07-14 16:56:47 -07001567 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1568
Steve Block3856b092011-10-20 11:56:00 +01001569 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001570 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001571 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001572 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001573 return ret;
1574 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001575 return NO_INIT;
1576}
1577
1578void MediaPlayerService::AudioOutput::stop()
1579{
Steve Block3856b092011-10-20 11:56:00 +01001580 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001581 if (mTrack) mTrack->stop();
1582}
1583
1584void MediaPlayerService::AudioOutput::flush()
1585{
Steve Block3856b092011-10-20 11:56:00 +01001586 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001587 if (mTrack) mTrack->flush();
1588}
1589
1590void MediaPlayerService::AudioOutput::pause()
1591{
Steve Block3856b092011-10-20 11:56:00 +01001592 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001593 if (mTrack) mTrack->pause();
1594}
1595
1596void MediaPlayerService::AudioOutput::close()
1597{
Steve Block3856b092011-10-20 11:56:00 +01001598 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001599 delete mTrack;
1600 mTrack = 0;
1601}
1602
1603void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1604{
Steve Block3856b092011-10-20 11:56:00 +01001605 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001606 mLeftVolume = left;
1607 mRightVolume = right;
1608 if (mTrack) {
1609 mTrack->setVolume(left, right);
1610 }
1611}
1612
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001613status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1614{
1615 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1616 status_t res = NO_ERROR;
1617 if (mTrack) {
1618 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1619 } else {
1620 res = NO_INIT;
1621 }
1622 mPlaybackRatePermille = ratePermille;
1623 if (mSampleRateHz != 0) {
1624 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1625 }
1626 return res;
1627}
1628
Eric Laurent2beeb502010-07-16 07:43:46 -07001629status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1630{
Steve Block3856b092011-10-20 11:56:00 +01001631 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001632 mSendLevel = level;
1633 if (mTrack) {
1634 return mTrack->setAuxEffectSendLevel(level);
1635 }
1636 return NO_ERROR;
1637}
1638
1639status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1640{
Steve Block3856b092011-10-20 11:56:00 +01001641 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001642 mAuxEffectId = effectId;
1643 if (mTrack) {
1644 return mTrack->attachAuxEffect(effectId);
1645 }
1646 return NO_ERROR;
1647}
1648
Andreas Huber20111aa2009-07-14 16:56:47 -07001649// static
1650void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001651 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001652 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001653 if (event != AudioTrack::EVENT_MORE_DATA) {
1654 return;
1655 }
1656
Marco Nelissen6b74d672012-02-28 16:07:44 -08001657 CallbackData *data = (CallbackData*)cookie;
1658 data->lock();
1659 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001660 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001661 if (me == NULL) {
1662 // no output set, likely because the track was scheduled to be reused
1663 // by another player, but the format turned out to be incompatible.
1664 data->unlock();
1665 buffer->size = 0;
1666 return;
1667 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001668
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001669 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001670 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001671
Marco Nelissen6b74d672012-02-28 16:07:44 -08001672 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001673 // We've reached EOS but the audio track is not stopped yet,
1674 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001675
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001676 memset(buffer->raw, 0, buffer->size);
1677 actualSize = buffer->size;
1678 }
1679
1680 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001681 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001682}
1683
Marco Nelissen4110c102012-03-29 09:31:28 -07001684int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001685{
1686 return mSessionId;
1687}
1688
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001689#undef LOG_TAG
1690#define LOG_TAG "AudioCache"
1691MediaPlayerService::AudioCache::AudioCache(const char* name) :
1692 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1693 mError(NO_ERROR), mCommandComplete(false)
1694{
1695 // create ashmem heap
1696 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1697}
1698
1699uint32_t MediaPlayerService::AudioCache::latency () const
1700{
1701 return 0;
1702}
1703
1704float MediaPlayerService::AudioCache::msecsPerFrame() const
1705{
1706 return mMsecsPerFrame;
1707}
1708
Marco Nelissen4110c102012-03-29 09:31:28 -07001709status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001710{
1711 if (position == 0) return BAD_VALUE;
1712 *position = mSize;
1713 return NO_ERROR;
1714}
1715
Marco Nelissen4110c102012-03-29 09:31:28 -07001716status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1717{
1718 if (written == 0) return BAD_VALUE;
1719 *written = mSize;
1720 return NO_ERROR;
1721}
1722
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001723////////////////////////////////////////////////////////////////////////////////
1724
1725struct CallbackThread : public Thread {
1726 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1727 MediaPlayerBase::AudioSink::AudioCallback cb,
1728 void *cookie);
1729
1730protected:
1731 virtual ~CallbackThread();
1732
1733 virtual bool threadLoop();
1734
1735private:
1736 wp<MediaPlayerBase::AudioSink> mSink;
1737 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1738 void *mCookie;
1739 void *mBuffer;
1740 size_t mBufferSize;
1741
1742 CallbackThread(const CallbackThread &);
1743 CallbackThread &operator=(const CallbackThread &);
1744};
1745
1746CallbackThread::CallbackThread(
1747 const wp<MediaPlayerBase::AudioSink> &sink,
1748 MediaPlayerBase::AudioSink::AudioCallback cb,
1749 void *cookie)
1750 : mSink(sink),
1751 mCallback(cb),
1752 mCookie(cookie),
1753 mBuffer(NULL),
1754 mBufferSize(0) {
1755}
1756
1757CallbackThread::~CallbackThread() {
1758 if (mBuffer) {
1759 free(mBuffer);
1760 mBuffer = NULL;
1761 }
1762}
1763
1764bool CallbackThread::threadLoop() {
1765 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1766 if (sink == NULL) {
1767 return false;
1768 }
1769
1770 if (mBuffer == NULL) {
1771 mBufferSize = sink->bufferSize();
1772 mBuffer = malloc(mBufferSize);
1773 }
1774
1775 size_t actualSize =
1776 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1777
1778 if (actualSize > 0) {
1779 sink->write(mBuffer, actualSize);
1780 }
1781
1782 return true;
1783}
1784
1785////////////////////////////////////////////////////////////////////////////////
1786
Andreas Huber20111aa2009-07-14 16:56:47 -07001787status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001788 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1789 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001790 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001791{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001792 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001793 if (mHeap->getHeapID() < 0) {
1794 return NO_INIT;
1795 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001796
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001797 mSampleRate = sampleRate;
1798 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001799 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001800 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001801
1802 if (cb != NULL) {
1803 mCallbackThread = new CallbackThread(this, cb, cookie);
1804 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001805 return NO_ERROR;
1806}
1807
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001808void MediaPlayerService::AudioCache::start() {
1809 if (mCallbackThread != NULL) {
1810 mCallbackThread->run("AudioCache callback");
1811 }
1812}
1813
1814void MediaPlayerService::AudioCache::stop() {
1815 if (mCallbackThread != NULL) {
1816 mCallbackThread->requestExitAndWait();
1817 }
1818}
1819
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001820ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1821{
Steve Block3856b092011-10-20 11:56:00 +01001822 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001823 if ((buffer == 0) || (size == 0)) return size;
1824
1825 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1826 if (p == NULL) return NO_INIT;
1827 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001828 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001830 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001831 size = mHeap->getSize() - mSize;
1832 }
1833 memcpy(p, buffer, size);
1834 mSize += size;
1835 return size;
1836}
1837
1838// call with lock held
1839status_t MediaPlayerService::AudioCache::wait()
1840{
1841 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001842 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001843 mSignal.wait(mLock);
1844 }
1845 mCommandComplete = false;
1846
1847 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001848 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001849 } else {
Steve Block3856b092011-10-20 11:56:00 +01001850 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001851 }
1852 return mError;
1853}
1854
Gloria Wangb483c472011-04-11 17:23:27 -07001855void MediaPlayerService::AudioCache::notify(
1856 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001857{
Steve Block3856b092011-10-20 11:56:00 +01001858 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001859 AudioCache* p = static_cast<AudioCache*>(cookie);
1860
1861 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001862 switch (msg)
1863 {
1864 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001865 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001866 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001867 break;
1868 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001869 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001870 break;
1871 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001872 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001873 break;
1874 default:
Steve Block3856b092011-10-20 11:56:00 +01001875 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001876 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001877 }
1878
1879 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001880 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001881 p->mCommandComplete = true;
1882 p->mSignal.signal();
1883}
1884
Marco Nelissen4110c102012-03-29 09:31:28 -07001885int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001886{
1887 return 0;
1888}
1889
Gloria Wang7cf180c2011-02-19 18:37:57 -08001890void MediaPlayerService::addBatteryData(uint32_t params)
1891{
1892 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001893
1894 int32_t time = systemTime() / 1000000L;
1895
1896 // change audio output devices. This notification comes from AudioFlinger
1897 if ((params & kBatteryDataSpeakerOn)
1898 || (params & kBatteryDataOtherAudioDeviceOn)) {
1899
1900 int deviceOn[NUM_AUDIO_DEVICES];
1901 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1902 deviceOn[i] = 0;
1903 }
1904
1905 if ((params & kBatteryDataSpeakerOn)
1906 && (params & kBatteryDataOtherAudioDeviceOn)) {
1907 deviceOn[SPEAKER_AND_OTHER] = 1;
1908 } else if (params & kBatteryDataSpeakerOn) {
1909 deviceOn[SPEAKER] = 1;
1910 } else {
1911 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1912 }
1913
1914 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1915 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1916
1917 if (mBatteryAudio.refCount > 0) { // if playing audio
1918 if (!deviceOn[i]) {
1919 mBatteryAudio.lastTime[i] += time;
1920 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1921 mBatteryAudio.lastTime[i] = 0;
1922 } else {
1923 mBatteryAudio.lastTime[i] = 0 - time;
1924 }
1925 }
1926
1927 mBatteryAudio.deviceOn[i] = deviceOn[i];
1928 }
1929 }
1930 return;
1931 }
1932
1933 // an sudio stream is started
1934 if (params & kBatteryDataAudioFlingerStart) {
1935 // record the start time only if currently no other audio
1936 // is being played
1937 if (mBatteryAudio.refCount == 0) {
1938 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1939 if (mBatteryAudio.deviceOn[i]) {
1940 mBatteryAudio.lastTime[i] -= time;
1941 }
1942 }
1943 }
1944
1945 mBatteryAudio.refCount ++;
1946 return;
1947
1948 } else if (params & kBatteryDataAudioFlingerStop) {
1949 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001950 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001951 return;
1952 }
1953
1954 // record the stop time only if currently this is the only
1955 // audio being played
1956 if (mBatteryAudio.refCount == 1) {
1957 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1958 if (mBatteryAudio.deviceOn[i]) {
1959 mBatteryAudio.lastTime[i] += time;
1960 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1961 mBatteryAudio.lastTime[i] = 0;
1962 }
1963 }
1964 }
1965
1966 mBatteryAudio.refCount --;
1967 return;
1968 }
1969
Gloria Wang7cf180c2011-02-19 18:37:57 -08001970 int uid = IPCThreadState::self()->getCallingUid();
1971 if (uid == AID_MEDIA) {
1972 return;
1973 }
1974 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001975
1976 if (index < 0) { // create a new entry for this UID
1977 BatteryUsageInfo info;
1978 info.audioTotalTime = 0;
1979 info.videoTotalTime = 0;
1980 info.audioLastTime = 0;
1981 info.videoLastTime = 0;
1982 info.refCount = 0;
1983
Gloria Wang9ee159b2011-02-24 14:51:45 -08001984 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001985 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001986 return;
1987 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001988 }
1989
1990 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1991
1992 if (params & kBatteryDataCodecStarted) {
1993 if (params & kBatteryDataTrackAudio) {
1994 info.audioLastTime -= time;
1995 info.refCount ++;
1996 }
1997 if (params & kBatteryDataTrackVideo) {
1998 info.videoLastTime -= time;
1999 info.refCount ++;
2000 }
2001 } else {
2002 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002003 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002004 return;
2005 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002006 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002007 mBatteryData.removeItem(uid);
2008 return;
2009 }
2010
2011 if (params & kBatteryDataTrackAudio) {
2012 info.audioLastTime += time;
2013 info.refCount --;
2014 }
2015 if (params & kBatteryDataTrackVideo) {
2016 info.videoLastTime += time;
2017 info.refCount --;
2018 }
2019
2020 // no stream is being played by this UID
2021 if (info.refCount == 0) {
2022 info.audioTotalTime += info.audioLastTime;
2023 info.audioLastTime = 0;
2024 info.videoTotalTime += info.videoLastTime;
2025 info.videoLastTime = 0;
2026 }
2027 }
2028}
2029
2030status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2031 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002032
2033 // audio output devices usage
2034 int32_t time = systemTime() / 1000000L; //in ms
2035 int32_t totalTime;
2036
2037 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2038 totalTime = mBatteryAudio.totalTime[i];
2039
2040 if (mBatteryAudio.deviceOn[i]
2041 && (mBatteryAudio.lastTime[i] != 0)) {
2042 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2043 totalTime += tmpTime;
2044 }
2045
2046 reply->writeInt32(totalTime);
2047 // reset the total time
2048 mBatteryAudio.totalTime[i] = 0;
2049 }
2050
2051 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002052 BatteryUsageInfo info;
2053 int size = mBatteryData.size();
2054
2055 reply->writeInt32(size);
2056 int i = 0;
2057
2058 while (i < size) {
2059 info = mBatteryData.valueAt(i);
2060
2061 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2062 reply->writeInt32(info.audioTotalTime);
2063 reply->writeInt32(info.videoTotalTime);
2064
2065 info.audioTotalTime = 0;
2066 info.videoTotalTime = 0;
2067
2068 // remove the UID entry where no stream is being played
2069 if (info.refCount <= 0) {
2070 mBatteryData.removeItemsAt(i);
2071 size --;
2072 i --;
2073 }
2074 i++;
2075 }
2076 return NO_ERROR;
2077}
nikoa64c8c72009-07-20 15:07:26 -07002078} // namespace android