blob: 423d6ce11935898c89952b9c7854202e58929b3d [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) {
Jeff Brownced24b32012-09-05 17:48:03 -0700286 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700287}
288
Andreas Huberab1bd842012-08-30 14:51:40 -0700289status_t MediaPlayerService::enableRemoteDisplay(const char *iface) {
Andreas Huber35213f12012-08-29 11:41:50 -0700290 Mutex::Autolock autoLock(mLock);
291
Andreas Huberab1bd842012-08-30 14:51:40 -0700292 if (iface != NULL) {
293 if (mRemoteDisplay != NULL) {
294 return INVALID_OPERATION;
295 }
296
Andreas Huber28169b12012-09-05 10:26:52 -0700297 mRemoteDisplay = new RemoteDisplay(NULL /* client */, iface);
Andreas Huber35213f12012-08-29 11:41:50 -0700298 return OK;
Andreas Huberab1bd842012-08-30 14:51:40 -0700299 }
300
301 if (mRemoteDisplay != NULL) {
Jeff Brownced24b32012-09-05 17:48:03 -0700302 mRemoteDisplay->dispose();
Andreas Huber35213f12012-08-29 11:41:50 -0700303 mRemoteDisplay.clear();
304 }
305
306 return OK;
307}
308
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
310{
311 const size_t SIZE = 256;
312 char buffer[SIZE];
313 String8 result;
314
315 result.append(" AudioCache\n");
316 if (mHeap != 0) {
317 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
318 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
319 result.append(buffer);
320 }
321 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
322 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
323 result.append(buffer);
324 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
325 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
326 result.append(buffer);
327 ::write(fd, result.string(), result.size());
328 return NO_ERROR;
329}
330
331status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
332{
333 const size_t SIZE = 256;
334 char buffer[SIZE];
335 String8 result;
336
337 result.append(" AudioOutput\n");
338 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
339 mStreamType, mLeftVolume, mRightVolume);
340 result.append(buffer);
341 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800342 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700344 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
345 mAuxEffectId, mSendLevel);
346 result.append(buffer);
347
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800348 ::write(fd, result.string(), result.size());
349 if (mTrack != 0) {
350 mTrack->dump(fd, args);
351 }
352 return NO_ERROR;
353}
354
355status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
356{
357 const size_t SIZE = 256;
358 char buffer[SIZE];
359 String8 result;
360 result.append(" Client\n");
361 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
362 mPid, mConnId, mStatus, mLoop?"true": "false");
363 result.append(buffer);
364 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700365 if (mPlayer != NULL) {
366 mPlayer->dump(fd, args);
367 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800368 if (mAudioOutput != 0) {
369 mAudioOutput->dump(fd, args);
370 }
371 write(fd, "\n", 1);
372 return NO_ERROR;
373}
374
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800375status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
376{
377 const size_t SIZE = 256;
378 char buffer[SIZE];
379 String8 result;
380 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
381 snprintf(buffer, SIZE, "Permission Denial: "
382 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
383 IPCThreadState::self()->getCallingPid(),
384 IPCThreadState::self()->getCallingUid());
385 result.append(buffer);
386 } else {
387 Mutex::Autolock lock(mLock);
388 for (int i = 0, n = mClients.size(); i < n; ++i) {
389 sp<Client> c = mClients[i].promote();
390 if (c != 0) c->dump(fd, args);
391 }
James Dongb9141222010-07-08 11:16:11 -0700392 if (mMediaRecorderClients.size() == 0) {
393 result.append(" No media recorder client\n\n");
394 } else {
395 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
396 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700397 if (c != 0) {
398 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
399 result.append(buffer);
400 write(fd, result.string(), result.size());
401 result = "\n";
402 c->dump(fd, args);
403 }
James Dongb9141222010-07-08 11:16:11 -0700404 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700405 }
406
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800407 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700408 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800409 FILE *f = fopen(buffer, "r");
410 if (f) {
411 while (!feof(f)) {
412 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700413 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800414 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700415 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800416 strstr(buffer, " /system/media/")) {
417 result.append(" ");
418 result.append(buffer);
419 }
420 }
421 fclose(f);
422 } else {
423 result.append("couldn't open ");
424 result.append(buffer);
425 result.append("\n");
426 }
427
Glenn Kasten0512ab52011-05-04 17:58:57 -0700428 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800429 DIR *d = opendir(buffer);
430 if (d) {
431 struct dirent *ent;
432 while((ent = readdir(d)) != NULL) {
433 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700434 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800435 struct stat s;
436 if (lstat(buffer, &s) == 0) {
437 if ((s.st_mode & S_IFMT) == S_IFLNK) {
438 char linkto[256];
439 int len = readlink(buffer, linkto, sizeof(linkto));
440 if(len > 0) {
441 if(len > 255) {
442 linkto[252] = '.';
443 linkto[253] = '.';
444 linkto[254] = '.';
445 linkto[255] = 0;
446 } else {
447 linkto[len] = 0;
448 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700449 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700451 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800452 strstr(linkto, "/system/media/") == linkto) {
453 result.append(" ");
454 result.append(buffer);
455 result.append(" -> ");
456 result.append(linkto);
457 result.append("\n");
458 }
459 }
460 } else {
461 result.append(" unexpected type for ");
462 result.append(buffer);
463 result.append("\n");
464 }
465 }
466 }
467 }
468 closedir(d);
469 } else {
470 result.append("couldn't open ");
471 result.append(buffer);
472 result.append("\n");
473 }
474
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475 bool dumpMem = false;
476 for (size_t i = 0; i < args.size(); i++) {
477 if (args[i] == String16("-m")) {
478 dumpMem = true;
479 }
480 }
481 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700482 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 }
485 write(fd, result.string(), result.size());
486 return NO_ERROR;
487}
488
489void MediaPlayerService::removeClient(wp<Client> client)
490{
491 Mutex::Autolock lock(mLock);
492 mClients.remove(client);
493}
494
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700495MediaPlayerService::Client::Client(
496 const sp<MediaPlayerService>& service, pid_t pid,
497 int32_t connId, const sp<IMediaPlayerClient>& client,
498 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800499{
Steve Block3856b092011-10-20 11:56:00 +0100500 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501 mPid = pid;
502 mConnId = connId;
503 mService = service;
504 mClient = client;
505 mLoop = false;
506 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700507 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700508 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800509 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700510
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000512 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800513 mAntagonizer = new Antagonizer(notify, this);
514#endif
515}
516
517MediaPlayerService::Client::~Client()
518{
Steve Block3856b092011-10-20 11:56:00 +0100519 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520 mAudioOutput.clear();
521 wp<Client> client(this);
522 disconnect();
523 mService->removeClient(client);
524}
525
526void MediaPlayerService::Client::disconnect()
527{
Steve Block3856b092011-10-20 11:56:00 +0100528 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800529 // grab local reference and clear main reference to prevent future
530 // access to object
531 sp<MediaPlayerBase> p;
532 {
533 Mutex::Autolock l(mLock);
534 p = mPlayer;
535 }
Dave Sparks795fa582009-03-24 17:57:12 -0700536 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700537
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800538 mPlayer.clear();
539
540 // clear the notification to prevent callbacks to dead client
541 // and reset the player. We assume the player will serialize
542 // access to itself if necessary.
543 if (p != 0) {
544 p->setNotifyCallback(0, 0);
545#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000546 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800547 mAntagonizer->kill();
548#endif
549 p->reset();
550 }
551
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700552 disconnectNativeWindow();
553
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 IPCThreadState::self()->flushCommands();
555}
556
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
558{
559 // determine if we have the right player type
560 sp<MediaPlayerBase> p = mPlayer;
561 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100562 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800563 p.clear();
564 }
565 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700566 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700568
Jason Simmonsdb29e522011-08-12 13:46:55 -0700569 if (p != NULL) {
570 p->setUID(mUID);
571 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700572
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573 return p;
574}
575
John Grossmanc795b642012-02-22 15:38:35 -0800576sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
577 player_type playerType)
578{
579 ALOGV("player type = %d", playerType);
580
581 // create the right type of player
582 sp<MediaPlayerBase> p = createPlayer(playerType);
583 if (p == NULL) {
584 return p;
585 }
586
587 if (!p->hardwareOutput()) {
588 mAudioOutput = new AudioOutput(mAudioSessionId);
589 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
590 }
591
592 return p;
593}
594
595void MediaPlayerService::Client::setDataSource_post(
596 const sp<MediaPlayerBase>& p,
597 status_t status)
598{
599 ALOGV(" setDataSource");
600 mStatus = status;
601 if (mStatus != OK) {
602 ALOGE(" error: %d", mStatus);
603 return;
604 }
605
606 // Set the re-transmission endpoint if one was chosen.
607 if (mRetransmitEndpointValid) {
608 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
609 if (mStatus != NO_ERROR) {
610 ALOGE("setRetransmitEndpoint error: %d", mStatus);
611 }
612 }
613
614 if (mStatus == OK) {
615 mPlayer = p;
616 }
617}
618
Andreas Huber2db84552010-01-28 11:19:57 -0800619status_t MediaPlayerService::Client::setDataSource(
620 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621{
Steve Block3856b092011-10-20 11:56:00 +0100622 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800623 if (url == NULL)
624 return UNKNOWN_ERROR;
625
Dave Burked681bbb2011-08-30 14:39:17 +0100626 if ((strncmp(url, "http://", 7) == 0) ||
627 (strncmp(url, "https://", 8) == 0) ||
628 (strncmp(url, "rtsp://", 7) == 0)) {
629 if (!checkPermission("android.permission.INTERNET")) {
630 return PERMISSION_DENIED;
631 }
632 }
633
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800634 if (strncmp(url, "content://", 10) == 0) {
635 // get a filedescriptor for the content Uri and
636 // pass it to the setDataSource(fd) method
637
638 String16 url16(url);
639 int fd = android::openContentProviderFile(url16);
640 if (fd < 0)
641 {
Steve Block29357bc2012-01-06 19:20:56 +0000642 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800643 return UNKNOWN_ERROR;
644 }
645 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
646 close(fd);
647 return mStatus;
648 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700649 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800650 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
651 if (p == NULL) {
652 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800653 }
654
John Grossmanc795b642012-02-22 15:38:35 -0800655 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 return mStatus;
657 }
658}
659
660status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
661{
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663 struct stat sb;
664 int ret = fstat(fd, &sb);
665 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000666 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667 return UNKNOWN_ERROR;
668 }
669
Steve Block3856b092011-10-20 11:56:00 +0100670 ALOGV("st_dev = %llu", sb.st_dev);
671 ALOGV("st_mode = %u", sb.st_mode);
672 ALOGV("st_uid = %lu", sb.st_uid);
673 ALOGV("st_gid = %lu", sb.st_gid);
674 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800675
676 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000677 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678 ::close(fd);
679 return UNKNOWN_ERROR;
680 }
681 if (offset + length > sb.st_size) {
682 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100683 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800684 }
685
John Grossman44a7e422012-06-21 17:29:24 -0700686 player_type playerType = MediaPlayerFactory::getPlayerType(this,
687 fd,
688 offset,
689 length);
John Grossmanc795b642012-02-22 15:38:35 -0800690 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
691 if (p == NULL) {
692 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800693 }
694
695 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800696 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800697 return mStatus;
698}
699
Andreas Hubere2b10282010-11-23 11:41:34 -0800700status_t MediaPlayerService::Client::setDataSource(
701 const sp<IStreamSource> &source) {
702 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700703 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800704 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800705 if (p == NULL) {
706 return NO_INIT;
707 }
708
Andreas Hubere2b10282010-11-23 11:41:34 -0800709 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800710 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800711 return mStatus;
712}
713
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700714void MediaPlayerService::Client::disconnectNativeWindow() {
715 if (mConnectedWindow != NULL) {
716 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
717 NATIVE_WINDOW_API_MEDIA);
718
719 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000720 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700721 strerror(-err), err);
722 }
723 }
724 mConnectedWindow.clear();
725}
726
Glenn Kasten11731182011-02-08 17:26:17 -0800727status_t MediaPlayerService::Client::setVideoSurfaceTexture(
728 const sp<ISurfaceTexture>& surfaceTexture)
729{
Steve Block3856b092011-10-20 11:56:00 +0100730 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, surfaceTexture.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800731 sp<MediaPlayerBase> p = getPlayer();
732 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700733
734 sp<IBinder> binder(surfaceTexture == NULL ? NULL :
735 surfaceTexture->asBinder());
736 if (mConnectedWindowBinder == binder) {
737 return OK;
738 }
739
740 sp<ANativeWindow> anw;
741 if (surfaceTexture != NULL) {
742 anw = new SurfaceTextureClient(surfaceTexture);
743 status_t err = native_window_api_connect(anw.get(),
744 NATIVE_WINDOW_API_MEDIA);
745
746 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000747 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700748 // Note that we must do the reset before disconnecting from the ANW.
749 // Otherwise queue/dequeue calls could be made on the disconnected
750 // ANW, which may result in errors.
751 reset();
752
753 disconnectNativeWindow();
754
755 return err;
756 }
757 }
758
759 // Note that we must set the player's new SurfaceTexture before
760 // disconnecting the old one. Otherwise queue/dequeue calls could be made
761 // on the disconnected ANW, which may result in errors.
762 status_t err = p->setVideoSurfaceTexture(surfaceTexture);
763
764 disconnectNativeWindow();
765
766 mConnectedWindow = anw;
767
768 if (err == OK) {
769 mConnectedWindowBinder = binder;
770 } else {
771 disconnectNativeWindow();
772 }
773
774 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800775}
776
Nicolas Catania1d187f12009-05-12 23:25:55 -0700777status_t MediaPlayerService::Client::invoke(const Parcel& request,
778 Parcel *reply)
779{
780 sp<MediaPlayerBase> p = getPlayer();
781 if (p == NULL) return UNKNOWN_ERROR;
782 return p->invoke(request, reply);
783}
784
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700785// This call doesn't need to access the native player.
786status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
787{
788 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700789 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700790
Nicolas Catania48290382009-07-10 13:53:06 -0700791 if (unmarshallFilter(filter, &allow, &status) &&
792 unmarshallFilter(filter, &drop, &status)) {
793 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700794
795 mMetadataAllow = allow;
796 mMetadataDrop = drop;
797 }
798 return status;
799}
800
Nicolas Catania48290382009-07-10 13:53:06 -0700801status_t MediaPlayerService::Client::getMetadata(
802 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700803{
nikoa64c8c72009-07-20 15:07:26 -0700804 sp<MediaPlayerBase> player = getPlayer();
805 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700806
nikod608a812009-07-16 16:39:53 -0700807 status_t status;
808 // Placeholder for the return code, updated by the caller.
809 reply->writeInt32(-1);
810
nikoa64c8c72009-07-20 15:07:26 -0700811 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700812
813 // We don't block notifications while we fetch the data. We clear
814 // mMetadataUpdated first so we don't lose notifications happening
815 // during the rest of this call.
816 {
817 Mutex::Autolock lock(mLock);
818 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700819 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700820 }
821 mMetadataUpdated.clear();
822 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700823
nikoa64c8c72009-07-20 15:07:26 -0700824 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700825
nikoa64c8c72009-07-20 15:07:26 -0700826 metadata.appendHeader();
827 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700828
829 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700830 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000831 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700832 return status;
833 }
834
835 // FIXME: Implement filtering on the result. Not critical since
836 // filtering takes place on the update notifications already. This
837 // would be when all the metadata are fetch and a filter is set.
838
nikod608a812009-07-16 16:39:53 -0700839 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700840 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700841 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700842}
843
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844status_t MediaPlayerService::Client::prepareAsync()
845{
Steve Block3856b092011-10-20 11:56:00 +0100846 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800847 sp<MediaPlayerBase> p = getPlayer();
848 if (p == 0) return UNKNOWN_ERROR;
849 status_t ret = p->prepareAsync();
850#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000851 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800852 if (ret == NO_ERROR) mAntagonizer->start();
853#endif
854 return ret;
855}
856
857status_t MediaPlayerService::Client::start()
858{
Steve Block3856b092011-10-20 11:56:00 +0100859 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800860 sp<MediaPlayerBase> p = getPlayer();
861 if (p == 0) return UNKNOWN_ERROR;
862 p->setLooping(mLoop);
863 return p->start();
864}
865
866status_t MediaPlayerService::Client::stop()
867{
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800869 sp<MediaPlayerBase> p = getPlayer();
870 if (p == 0) return UNKNOWN_ERROR;
871 return p->stop();
872}
873
874status_t MediaPlayerService::Client::pause()
875{
Steve Block3856b092011-10-20 11:56:00 +0100876 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800877 sp<MediaPlayerBase> p = getPlayer();
878 if (p == 0) return UNKNOWN_ERROR;
879 return p->pause();
880}
881
882status_t MediaPlayerService::Client::isPlaying(bool* state)
883{
884 *state = false;
885 sp<MediaPlayerBase> p = getPlayer();
886 if (p == 0) return UNKNOWN_ERROR;
887 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100888 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800889 return NO_ERROR;
890}
891
892status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
893{
Steve Block3856b092011-10-20 11:56:00 +0100894 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800895 sp<MediaPlayerBase> p = getPlayer();
896 if (p == 0) return UNKNOWN_ERROR;
897 status_t ret = p->getCurrentPosition(msec);
898 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100899 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800900 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000901 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800902 }
903 return ret;
904}
905
906status_t MediaPlayerService::Client::getDuration(int *msec)
907{
Steve Block3856b092011-10-20 11:56:00 +0100908 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800909 sp<MediaPlayerBase> p = getPlayer();
910 if (p == 0) return UNKNOWN_ERROR;
911 status_t ret = p->getDuration(msec);
912 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100913 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800914 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000915 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800916 }
917 return ret;
918}
919
Marco Nelissen6b74d672012-02-28 16:07:44 -0800920status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
921 ALOGV("setNextPlayer");
922 Mutex::Autolock l(mLock);
923 sp<Client> c = static_cast<Client*>(player.get());
924 mNextClient = c;
925 if (mAudioOutput != NULL && c != NULL) {
926 mAudioOutput->setNextOutput(c->mAudioOutput);
927 } else {
928 ALOGE("no current audio output");
929 }
930 return OK;
931}
932
933
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800934status_t MediaPlayerService::Client::seekTo(int msec)
935{
Steve Block3856b092011-10-20 11:56:00 +0100936 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937 sp<MediaPlayerBase> p = getPlayer();
938 if (p == 0) return UNKNOWN_ERROR;
939 return p->seekTo(msec);
940}
941
942status_t MediaPlayerService::Client::reset()
943{
Steve Block3856b092011-10-20 11:56:00 +0100944 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800945 mRetransmitEndpointValid = false;
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->reset();
949}
950
Glenn Kastenfff6d712012-01-12 16:38:12 -0800951status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800952{
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800954 // TODO: for hardware output, call player instead
955 Mutex::Autolock l(mLock);
956 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
957 return NO_ERROR;
958}
959
960status_t MediaPlayerService::Client::setLooping(int loop)
961{
Steve Block3856b092011-10-20 11:56:00 +0100962 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800963 mLoop = loop;
964 sp<MediaPlayerBase> p = getPlayer();
965 if (p != 0) return p->setLooping(loop);
966 return NO_ERROR;
967}
968
969status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
970{
Steve Block3856b092011-10-20 11:56:00 +0100971 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800972
973 // for hardware output, call player instead
974 sp<MediaPlayerBase> p = getPlayer();
975 {
976 Mutex::Autolock l(mLock);
977 if (p != 0 && p->hardwareOutput()) {
978 MediaPlayerHWInterface* hwp =
979 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
980 return hwp->setVolume(leftVolume, rightVolume);
981 } else {
982 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
983 return NO_ERROR;
984 }
985 }
986
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800987 return NO_ERROR;
988}
989
Eric Laurent2beeb502010-07-16 07:43:46 -0700990status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
991{
Steve Block3856b092011-10-20 11:56:00 +0100992 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700993 Mutex::Autolock l(mLock);
994 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
995 return NO_ERROR;
996}
997
998status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
999{
Steve Block3856b092011-10-20 11:56:00 +01001000 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001001 Mutex::Autolock l(mLock);
1002 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1003 return NO_ERROR;
1004}
Nicolas Catania48290382009-07-10 13:53:06 -07001005
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001006status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001007 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001008 sp<MediaPlayerBase> p = getPlayer();
1009 if (p == 0) return UNKNOWN_ERROR;
1010 return p->setParameter(key, request);
1011}
1012
1013status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001014 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001015 sp<MediaPlayerBase> p = getPlayer();
1016 if (p == 0) return UNKNOWN_ERROR;
1017 return p->getParameter(key, reply);
1018}
1019
John Grossmanc795b642012-02-22 15:38:35 -08001020status_t MediaPlayerService::Client::setRetransmitEndpoint(
1021 const struct sockaddr_in* endpoint) {
1022
1023 if (NULL != endpoint) {
1024 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1025 uint16_t p = ntohs(endpoint->sin_port);
1026 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1027 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1028 } else {
1029 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1030 }
1031
1032 sp<MediaPlayerBase> p = getPlayer();
1033
1034 // Right now, the only valid time to set a retransmit endpoint is before
1035 // player selection has been made (since the presence or absence of a
1036 // retransmit endpoint is going to determine which player is selected during
1037 // setDataSource).
1038 if (p != 0) return INVALID_OPERATION;
1039
1040 if (NULL != endpoint) {
1041 mRetransmitEndpoint = *endpoint;
1042 mRetransmitEndpointValid = true;
1043 } else {
1044 mRetransmitEndpointValid = false;
1045 }
1046
1047 return NO_ERROR;
1048}
1049
John Grossman44a7e422012-06-21 17:29:24 -07001050status_t MediaPlayerService::Client::getRetransmitEndpoint(
1051 struct sockaddr_in* endpoint)
1052{
1053 if (NULL == endpoint)
1054 return BAD_VALUE;
1055
1056 sp<MediaPlayerBase> p = getPlayer();
1057
1058 if (p != NULL)
1059 return p->getRetransmitEndpoint(endpoint);
1060
1061 if (!mRetransmitEndpointValid)
1062 return NO_INIT;
1063
1064 *endpoint = mRetransmitEndpoint;
1065
1066 return NO_ERROR;
1067}
1068
Gloria Wangb483c472011-04-11 17:23:27 -07001069void MediaPlayerService::Client::notify(
1070 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001071{
1072 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001073 if (client == NULL) {
1074 return;
1075 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001076
James Dongb8a98252012-08-26 16:13:03 -07001077 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001078 {
1079 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001080 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001081 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001082 if (client->mAudioOutput != NULL)
1083 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001084 client->mNextClient->start();
1085 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1086 }
1087 }
1088
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001089 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001090 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001091 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001092
1093 if(client->shouldDropMetadata(metadata_type)) {
1094 return;
1095 }
1096
1097 // Update the list of metadata that have changed. getMetadata
1098 // also access mMetadataUpdated and clears it.
1099 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001100 }
James Dongb8a98252012-08-26 16:13:03 -07001101
1102 if (c != NULL) {
1103 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1104 c->notify(msg, ext1, ext2, obj);
1105 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001106}
1107
Nicolas Catania48290382009-07-10 13:53:06 -07001108
nikoa64c8c72009-07-20 15:07:26 -07001109bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001110{
Nicolas Catania48290382009-07-10 13:53:06 -07001111 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001112
Nicolas Catania48290382009-07-10 13:53:06 -07001113 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001114 return true;
1115 }
1116
Nicolas Catania48290382009-07-10 13:53:06 -07001117 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001118 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001119 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001120 return true;
1121 }
1122}
1123
Nicolas Catania48290382009-07-10 13:53:06 -07001124
nikoa64c8c72009-07-20 15:07:26 -07001125void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001126 Mutex::Autolock lock(mLock);
1127 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1128 mMetadataUpdated.add(metadata_type);
1129 }
1130}
1131
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001132#if CALLBACK_ANTAGONIZER
1133const int Antagonizer::interval = 10000; // 10 msecs
1134
1135Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1136 mExit(false), mActive(false), mClient(client), mCb(cb)
1137{
1138 createThread(callbackThread, this);
1139}
1140
1141void Antagonizer::kill()
1142{
1143 Mutex::Autolock _l(mLock);
1144 mActive = false;
1145 mExit = true;
1146 mCondition.wait(mLock);
1147}
1148
1149int Antagonizer::callbackThread(void* user)
1150{
Steve Blockb8a80522011-12-20 16:23:08 +00001151 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001152 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1153 while (!p->mExit) {
1154 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001155 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001156 p->mCb(p->mClient, 0, 0, 0);
1157 }
1158 usleep(interval);
1159 }
1160 Mutex::Autolock _l(p->mLock);
1161 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001162 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001163 return 0;
1164}
1165#endif
1166
1167static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1168
Glenn Kastene1c39622012-01-04 09:36:37 -08001169sp<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 -08001170{
Steve Block3856b092011-10-20 11:56:00 +01001171 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001172 sp<MemoryBase> mem;
1173 sp<MediaPlayerBase> player;
1174
1175 // Protect our precious, precious DRMd ringtones by only allowing
1176 // decoding of http, but not filesystem paths or content Uris.
1177 // If the application wants to decode those, it should open a
1178 // filedescriptor for them and use that.
1179 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001180 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001181 return mem;
1182 }
1183
John Grossman44a7e422012-06-21 17:29:24 -07001184 player_type playerType =
1185 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001186 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001187
1188 // create the right type of player
1189 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001190 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001191 if (player == NULL) goto Exit;
1192 if (player->hardwareOutput()) goto Exit;
1193
1194 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1195
1196 // set data source
1197 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1198
Steve Block3856b092011-10-20 11:56:00 +01001199 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001200 player->prepareAsync();
1201
Steve Block3856b092011-10-20 11:56:00 +01001202 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203 if (cache->wait() != NO_ERROR) goto Exit;
1204
Steve Block3856b092011-10-20 11:56:00 +01001205 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001206 player->start();
1207
Steve Block3856b092011-10-20 11:56:00 +01001208 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001209 cache->wait();
1210 // in case of error, return what was successfully decoded.
1211 if (cache->size() == 0) {
1212 goto Exit;
1213 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214
1215 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1216 *pSampleRate = cache->sampleRate();
1217 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001218 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001219 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 -08001220
1221Exit:
1222 if (player != 0) player->reset();
1223 return mem;
1224}
1225
Glenn Kastene1c39622012-01-04 09:36:37 -08001226sp<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 -08001227{
Steve Block3856b092011-10-20 11:56:00 +01001228 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001229 sp<MemoryBase> mem;
1230 sp<MediaPlayerBase> player;
1231
John Grossman44a7e422012-06-21 17:29:24 -07001232 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1233 fd,
1234 offset,
1235 length);
Steve Block3856b092011-10-20 11:56:00 +01001236 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001237
1238 // create the right type of player
1239 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001240 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001241 if (player == NULL) goto Exit;
1242 if (player->hardwareOutput()) goto Exit;
1243
1244 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1245
1246 // set data source
1247 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1248
Steve Block3856b092011-10-20 11:56:00 +01001249 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001250 player->prepareAsync();
1251
Steve Block3856b092011-10-20 11:56:00 +01001252 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001253 if (cache->wait() != NO_ERROR) goto Exit;
1254
Steve Block3856b092011-10-20 11:56:00 +01001255 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001256 player->start();
1257
Steve Block3856b092011-10-20 11:56:00 +01001258 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001259 cache->wait();
1260 // in case of error, return what was successfully decoded.
1261 if (cache->size() == 0) {
1262 goto Exit;
1263 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264
1265 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1266 *pSampleRate = cache->sampleRate();
1267 *pNumChannels = cache->channelCount();
1268 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001269 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 -08001270
1271Exit:
1272 if (player != 0) player->reset();
1273 ::close(fd);
1274 return mem;
1275}
1276
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001277
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001278#undef LOG_TAG
1279#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001280MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001281 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001282 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001283 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001284 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001285 mSessionId(sessionId),
1286 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001287 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001288 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001289 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001290 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001291 mLeftVolume = 1.0;
1292 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001293 mPlaybackRatePermille = 1000;
1294 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001296 mAuxEffectId = 0;
1297 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298 setMinBufferCount();
1299}
1300
1301MediaPlayerService::AudioOutput::~AudioOutput()
1302{
1303 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001304 delete mRecycledTrack;
1305 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001306}
1307
1308void MediaPlayerService::AudioOutput::setMinBufferCount()
1309{
1310 char value[PROPERTY_VALUE_MAX];
1311 if (property_get("ro.kernel.qemu", value, 0)) {
1312 mIsOnEmulator = true;
1313 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1314 }
1315}
1316
1317bool MediaPlayerService::AudioOutput::isOnEmulator()
1318{
1319 setMinBufferCount();
1320 return mIsOnEmulator;
1321}
1322
1323int MediaPlayerService::AudioOutput::getMinBufferCount()
1324{
1325 setMinBufferCount();
1326 return mMinBufferCount;
1327}
1328
1329ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1330{
1331 if (mTrack == 0) return NO_INIT;
1332 return mTrack->frameCount() * frameSize();
1333}
1334
1335ssize_t MediaPlayerService::AudioOutput::frameCount() const
1336{
1337 if (mTrack == 0) return NO_INIT;
1338 return mTrack->frameCount();
1339}
1340
1341ssize_t MediaPlayerService::AudioOutput::channelCount() const
1342{
1343 if (mTrack == 0) return NO_INIT;
1344 return mTrack->channelCount();
1345}
1346
1347ssize_t MediaPlayerService::AudioOutput::frameSize() const
1348{
1349 if (mTrack == 0) return NO_INIT;
1350 return mTrack->frameSize();
1351}
1352
1353uint32_t MediaPlayerService::AudioOutput::latency () const
1354{
Eric Laurentdb354e52012-03-05 17:27:11 -08001355 if (mTrack == 0) return 0;
1356 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001357}
1358
1359float MediaPlayerService::AudioOutput::msecsPerFrame() const
1360{
1361 return mMsecsPerFrame;
1362}
1363
Marco Nelissen4110c102012-03-29 09:31:28 -07001364status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001365{
1366 if (mTrack == 0) return NO_INIT;
1367 return mTrack->getPosition(position);
1368}
1369
Marco Nelissen4110c102012-03-29 09:31:28 -07001370status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1371{
1372 if (mTrack == 0) return NO_INIT;
1373 *frameswritten = mBytesWritten / frameSize();
1374 return OK;
1375}
1376
Andreas Huber20111aa2009-07-14 16:56:47 -07001377status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001378 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1379 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001380 AudioCallback cb, void *cookie,
1381 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001382{
Andreas Huber20111aa2009-07-14 16:56:47 -07001383 mCallback = cb;
1384 mCallbackCookie = cookie;
1385
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001386 // Check argument "bufferCount" against the mininum buffer count
1387 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001388 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001389 bufferCount = mMinBufferCount;
1390
1391 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001392 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1393 format, bufferCount, mSessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001394 int afSampleRate;
1395 int afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001396 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001397
1398 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1399 return NO_INIT;
1400 }
1401 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1402 return NO_INIT;
1403 }
1404
1405 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001406
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001407 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001408 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001409 if (0 == channelMask) {
1410 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1411 return NO_INIT;
1412 }
1413 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001414
Andreas Huber20111aa2009-07-14 16:56:47 -07001415 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001416 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001417 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001418 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001419 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001420 mStreamType,
1421 sampleRate,
1422 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001423 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001424 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001425 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001426 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001427 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001428 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001429 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001430 } else {
1431 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001432 mStreamType,
1433 sampleRate,
1434 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001435 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001436 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001437 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001438 NULL,
1439 NULL,
1440 0,
1441 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001442 }
1443
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001444 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001445 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001446 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001447 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001448 return NO_INIT;
1449 }
1450
Marco Nelissen67295b52012-06-11 14:52:53 -07001451
1452 if (mRecycledTrack) {
1453 // check if the existing track can be reused as-is, or if a new track needs to be created.
1454
1455 bool reuse = true;
1456 if ((mCallbackData == NULL && mCallback != NULL) ||
1457 (mCallbackData != NULL && mCallback == NULL)) {
1458 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1459 ALOGV("can't chain callback and write");
1460 reuse = false;
1461 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1462 (mRecycledTrack->channelCount() != channelCount) ||
1463 (mRecycledTrack->frameCount() != t->frameCount())) {
1464 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1465 mRecycledTrack->getSampleRate(), sampleRate,
1466 mRecycledTrack->channelCount(), channelCount,
1467 mRecycledTrack->frameCount(), t->frameCount());
1468 reuse = false;
1469 } else if (flags != mFlags) {
1470 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1471 reuse = false;
1472 }
1473 if (reuse) {
1474 ALOGV("chaining to next output");
1475 close();
1476 mTrack = mRecycledTrack;
1477 mRecycledTrack = NULL;
1478 if (mCallbackData != NULL) {
1479 mCallbackData->setOutput(this);
1480 }
1481 delete t;
1482 delete newcbd;
1483 return OK;
1484 }
1485
1486 // if we're not going to reuse the track, unblock and flush it
1487 if (mCallbackData != NULL) {
1488 mCallbackData->setOutput(NULL);
1489 mCallbackData->endTrackSwitch();
1490 }
1491 mRecycledTrack->flush();
1492 delete mRecycledTrack;
1493 mRecycledTrack = NULL;
1494 delete mCallbackData;
1495 mCallbackData = NULL;
1496 close();
1497 }
1498
1499 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001500 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001501 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001502
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001503 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001504 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001505 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001506 uint32_t pos;
1507 if (t->getPosition(&pos) == OK) {
1508 mBytesWritten = uint64_t(pos) * t->frameSize();
1509 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001510 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001511
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001512 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1513 if (res != NO_ERROR) {
1514 return res;
1515 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001516 t->setAuxEffectSendLevel(mSendLevel);
1517 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001518}
1519
1520void MediaPlayerService::AudioOutput::start()
1521{
Steve Block3856b092011-10-20 11:56:00 +01001522 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001523 if (mCallbackData != NULL) {
1524 mCallbackData->endTrackSwitch();
1525 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001526 if (mTrack) {
1527 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001528 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001529 mTrack->start();
1530 }
1531}
1532
Marco Nelissen6b74d672012-02-28 16:07:44 -08001533void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1534 mNextOutput = nextOutput;
1535}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001536
1537
Marco Nelissen6b74d672012-02-28 16:07:44 -08001538void MediaPlayerService::AudioOutput::switchToNextOutput() {
1539 ALOGV("switchToNextOutput");
1540 if (mNextOutput != NULL) {
1541 if (mCallbackData != NULL) {
1542 mCallbackData->beginTrackSwitch();
1543 }
1544 delete mNextOutput->mCallbackData;
1545 mNextOutput->mCallbackData = mCallbackData;
1546 mCallbackData = NULL;
1547 mNextOutput->mRecycledTrack = mTrack;
1548 mTrack = NULL;
1549 mNextOutput->mSampleRateHz = mSampleRateHz;
1550 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001551 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001552 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001553 }
1554}
1555
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001556ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1557{
Andreas Huber20111aa2009-07-14 16:56:47 -07001558 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1559
Steve Block3856b092011-10-20 11:56:00 +01001560 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001561 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001562 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001563 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001564 return ret;
1565 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001566 return NO_INIT;
1567}
1568
1569void MediaPlayerService::AudioOutput::stop()
1570{
Steve Block3856b092011-10-20 11:56:00 +01001571 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001572 if (mTrack) mTrack->stop();
1573}
1574
1575void MediaPlayerService::AudioOutput::flush()
1576{
Steve Block3856b092011-10-20 11:56:00 +01001577 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001578 if (mTrack) mTrack->flush();
1579}
1580
1581void MediaPlayerService::AudioOutput::pause()
1582{
Steve Block3856b092011-10-20 11:56:00 +01001583 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001584 if (mTrack) mTrack->pause();
1585}
1586
1587void MediaPlayerService::AudioOutput::close()
1588{
Steve Block3856b092011-10-20 11:56:00 +01001589 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001590 delete mTrack;
1591 mTrack = 0;
1592}
1593
1594void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1595{
Steve Block3856b092011-10-20 11:56:00 +01001596 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001597 mLeftVolume = left;
1598 mRightVolume = right;
1599 if (mTrack) {
1600 mTrack->setVolume(left, right);
1601 }
1602}
1603
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001604status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1605{
1606 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1607 status_t res = NO_ERROR;
1608 if (mTrack) {
1609 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1610 } else {
1611 res = NO_INIT;
1612 }
1613 mPlaybackRatePermille = ratePermille;
1614 if (mSampleRateHz != 0) {
1615 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1616 }
1617 return res;
1618}
1619
Eric Laurent2beeb502010-07-16 07:43:46 -07001620status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1621{
Steve Block3856b092011-10-20 11:56:00 +01001622 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001623 mSendLevel = level;
1624 if (mTrack) {
1625 return mTrack->setAuxEffectSendLevel(level);
1626 }
1627 return NO_ERROR;
1628}
1629
1630status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1631{
Steve Block3856b092011-10-20 11:56:00 +01001632 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001633 mAuxEffectId = effectId;
1634 if (mTrack) {
1635 return mTrack->attachAuxEffect(effectId);
1636 }
1637 return NO_ERROR;
1638}
1639
Andreas Huber20111aa2009-07-14 16:56:47 -07001640// static
1641void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001642 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001643 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001644 if (event != AudioTrack::EVENT_MORE_DATA) {
1645 return;
1646 }
1647
Marco Nelissen6b74d672012-02-28 16:07:44 -08001648 CallbackData *data = (CallbackData*)cookie;
1649 data->lock();
1650 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001651 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001652 if (me == NULL) {
1653 // no output set, likely because the track was scheduled to be reused
1654 // by another player, but the format turned out to be incompatible.
1655 data->unlock();
1656 buffer->size = 0;
1657 return;
1658 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001659
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001660 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001661 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001662
Marco Nelissen6b74d672012-02-28 16:07:44 -08001663 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001664 // We've reached EOS but the audio track is not stopped yet,
1665 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001666
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001667 memset(buffer->raw, 0, buffer->size);
1668 actualSize = buffer->size;
1669 }
1670
1671 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001672 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001673}
1674
Marco Nelissen4110c102012-03-29 09:31:28 -07001675int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001676{
1677 return mSessionId;
1678}
1679
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001680#undef LOG_TAG
1681#define LOG_TAG "AudioCache"
1682MediaPlayerService::AudioCache::AudioCache(const char* name) :
1683 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1684 mError(NO_ERROR), mCommandComplete(false)
1685{
1686 // create ashmem heap
1687 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1688}
1689
1690uint32_t MediaPlayerService::AudioCache::latency () const
1691{
1692 return 0;
1693}
1694
1695float MediaPlayerService::AudioCache::msecsPerFrame() const
1696{
1697 return mMsecsPerFrame;
1698}
1699
Marco Nelissen4110c102012-03-29 09:31:28 -07001700status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001701{
1702 if (position == 0) return BAD_VALUE;
1703 *position = mSize;
1704 return NO_ERROR;
1705}
1706
Marco Nelissen4110c102012-03-29 09:31:28 -07001707status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1708{
1709 if (written == 0) return BAD_VALUE;
1710 *written = mSize;
1711 return NO_ERROR;
1712}
1713
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001714////////////////////////////////////////////////////////////////////////////////
1715
1716struct CallbackThread : public Thread {
1717 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1718 MediaPlayerBase::AudioSink::AudioCallback cb,
1719 void *cookie);
1720
1721protected:
1722 virtual ~CallbackThread();
1723
1724 virtual bool threadLoop();
1725
1726private:
1727 wp<MediaPlayerBase::AudioSink> mSink;
1728 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1729 void *mCookie;
1730 void *mBuffer;
1731 size_t mBufferSize;
1732
1733 CallbackThread(const CallbackThread &);
1734 CallbackThread &operator=(const CallbackThread &);
1735};
1736
1737CallbackThread::CallbackThread(
1738 const wp<MediaPlayerBase::AudioSink> &sink,
1739 MediaPlayerBase::AudioSink::AudioCallback cb,
1740 void *cookie)
1741 : mSink(sink),
1742 mCallback(cb),
1743 mCookie(cookie),
1744 mBuffer(NULL),
1745 mBufferSize(0) {
1746}
1747
1748CallbackThread::~CallbackThread() {
1749 if (mBuffer) {
1750 free(mBuffer);
1751 mBuffer = NULL;
1752 }
1753}
1754
1755bool CallbackThread::threadLoop() {
1756 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1757 if (sink == NULL) {
1758 return false;
1759 }
1760
1761 if (mBuffer == NULL) {
1762 mBufferSize = sink->bufferSize();
1763 mBuffer = malloc(mBufferSize);
1764 }
1765
1766 size_t actualSize =
1767 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1768
1769 if (actualSize > 0) {
1770 sink->write(mBuffer, actualSize);
1771 }
1772
1773 return true;
1774}
1775
1776////////////////////////////////////////////////////////////////////////////////
1777
Andreas Huber20111aa2009-07-14 16:56:47 -07001778status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001779 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1780 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001781 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001782{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001783 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001784 if (mHeap->getHeapID() < 0) {
1785 return NO_INIT;
1786 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001787
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001788 mSampleRate = sampleRate;
1789 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001790 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001791 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001792
1793 if (cb != NULL) {
1794 mCallbackThread = new CallbackThread(this, cb, cookie);
1795 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001796 return NO_ERROR;
1797}
1798
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001799void MediaPlayerService::AudioCache::start() {
1800 if (mCallbackThread != NULL) {
1801 mCallbackThread->run("AudioCache callback");
1802 }
1803}
1804
1805void MediaPlayerService::AudioCache::stop() {
1806 if (mCallbackThread != NULL) {
1807 mCallbackThread->requestExitAndWait();
1808 }
1809}
1810
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001811ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1812{
Steve Block3856b092011-10-20 11:56:00 +01001813 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001814 if ((buffer == 0) || (size == 0)) return size;
1815
1816 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1817 if (p == NULL) return NO_INIT;
1818 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001819 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001820 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001821 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001822 size = mHeap->getSize() - mSize;
1823 }
1824 memcpy(p, buffer, size);
1825 mSize += size;
1826 return size;
1827}
1828
1829// call with lock held
1830status_t MediaPlayerService::AudioCache::wait()
1831{
1832 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001833 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001834 mSignal.wait(mLock);
1835 }
1836 mCommandComplete = false;
1837
1838 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001839 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001840 } else {
Steve Block3856b092011-10-20 11:56:00 +01001841 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001842 }
1843 return mError;
1844}
1845
Gloria Wangb483c472011-04-11 17:23:27 -07001846void MediaPlayerService::AudioCache::notify(
1847 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001848{
Steve Block3856b092011-10-20 11:56:00 +01001849 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001850 AudioCache* p = static_cast<AudioCache*>(cookie);
1851
1852 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001853 switch (msg)
1854 {
1855 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001856 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001857 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001858 break;
1859 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001860 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001861 break;
1862 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001863 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001864 break;
1865 default:
Steve Block3856b092011-10-20 11:56:00 +01001866 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001867 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001868 }
1869
1870 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001871 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001872 p->mCommandComplete = true;
1873 p->mSignal.signal();
1874}
1875
Marco Nelissen4110c102012-03-29 09:31:28 -07001876int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001877{
1878 return 0;
1879}
1880
Gloria Wang7cf180c2011-02-19 18:37:57 -08001881void MediaPlayerService::addBatteryData(uint32_t params)
1882{
1883 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001884
1885 int32_t time = systemTime() / 1000000L;
1886
1887 // change audio output devices. This notification comes from AudioFlinger
1888 if ((params & kBatteryDataSpeakerOn)
1889 || (params & kBatteryDataOtherAudioDeviceOn)) {
1890
1891 int deviceOn[NUM_AUDIO_DEVICES];
1892 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1893 deviceOn[i] = 0;
1894 }
1895
1896 if ((params & kBatteryDataSpeakerOn)
1897 && (params & kBatteryDataOtherAudioDeviceOn)) {
1898 deviceOn[SPEAKER_AND_OTHER] = 1;
1899 } else if (params & kBatteryDataSpeakerOn) {
1900 deviceOn[SPEAKER] = 1;
1901 } else {
1902 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1903 }
1904
1905 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1906 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1907
1908 if (mBatteryAudio.refCount > 0) { // if playing audio
1909 if (!deviceOn[i]) {
1910 mBatteryAudio.lastTime[i] += time;
1911 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1912 mBatteryAudio.lastTime[i] = 0;
1913 } else {
1914 mBatteryAudio.lastTime[i] = 0 - time;
1915 }
1916 }
1917
1918 mBatteryAudio.deviceOn[i] = deviceOn[i];
1919 }
1920 }
1921 return;
1922 }
1923
1924 // an sudio stream is started
1925 if (params & kBatteryDataAudioFlingerStart) {
1926 // record the start time only if currently no other audio
1927 // is being played
1928 if (mBatteryAudio.refCount == 0) {
1929 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1930 if (mBatteryAudio.deviceOn[i]) {
1931 mBatteryAudio.lastTime[i] -= time;
1932 }
1933 }
1934 }
1935
1936 mBatteryAudio.refCount ++;
1937 return;
1938
1939 } else if (params & kBatteryDataAudioFlingerStop) {
1940 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001941 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001942 return;
1943 }
1944
1945 // record the stop time only if currently this is the only
1946 // audio being played
1947 if (mBatteryAudio.refCount == 1) {
1948 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1949 if (mBatteryAudio.deviceOn[i]) {
1950 mBatteryAudio.lastTime[i] += time;
1951 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1952 mBatteryAudio.lastTime[i] = 0;
1953 }
1954 }
1955 }
1956
1957 mBatteryAudio.refCount --;
1958 return;
1959 }
1960
Gloria Wang7cf180c2011-02-19 18:37:57 -08001961 int uid = IPCThreadState::self()->getCallingUid();
1962 if (uid == AID_MEDIA) {
1963 return;
1964 }
1965 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001966
1967 if (index < 0) { // create a new entry for this UID
1968 BatteryUsageInfo info;
1969 info.audioTotalTime = 0;
1970 info.videoTotalTime = 0;
1971 info.audioLastTime = 0;
1972 info.videoLastTime = 0;
1973 info.refCount = 0;
1974
Gloria Wang9ee159b2011-02-24 14:51:45 -08001975 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001976 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001977 return;
1978 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001979 }
1980
1981 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1982
1983 if (params & kBatteryDataCodecStarted) {
1984 if (params & kBatteryDataTrackAudio) {
1985 info.audioLastTime -= time;
1986 info.refCount ++;
1987 }
1988 if (params & kBatteryDataTrackVideo) {
1989 info.videoLastTime -= time;
1990 info.refCount ++;
1991 }
1992 } else {
1993 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001994 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001995 return;
1996 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001997 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001998 mBatteryData.removeItem(uid);
1999 return;
2000 }
2001
2002 if (params & kBatteryDataTrackAudio) {
2003 info.audioLastTime += time;
2004 info.refCount --;
2005 }
2006 if (params & kBatteryDataTrackVideo) {
2007 info.videoLastTime += time;
2008 info.refCount --;
2009 }
2010
2011 // no stream is being played by this UID
2012 if (info.refCount == 0) {
2013 info.audioTotalTime += info.audioLastTime;
2014 info.audioLastTime = 0;
2015 info.videoTotalTime += info.videoLastTime;
2016 info.videoLastTime = 0;
2017 }
2018 }
2019}
2020
2021status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2022 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002023
2024 // audio output devices usage
2025 int32_t time = systemTime() / 1000000L; //in ms
2026 int32_t totalTime;
2027
2028 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2029 totalTime = mBatteryAudio.totalTime[i];
2030
2031 if (mBatteryAudio.deviceOn[i]
2032 && (mBatteryAudio.lastTime[i] != 0)) {
2033 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2034 totalTime += tmpTime;
2035 }
2036
2037 reply->writeInt32(totalTime);
2038 // reset the total time
2039 mBatteryAudio.totalTime[i] = 0;
2040 }
2041
2042 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002043 BatteryUsageInfo info;
2044 int size = mBatteryData.size();
2045
2046 reply->writeInt32(size);
2047 int i = 0;
2048
2049 while (i < size) {
2050 info = mBatteryData.valueAt(i);
2051
2052 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2053 reply->writeInt32(info.audioTotalTime);
2054 reply->writeInt32(info.videoTotalTime);
2055
2056 info.audioTotalTime = 0;
2057 info.videoTotalTime = 0;
2058
2059 // remove the UID entry where no stream is being played
2060 if (info.refCount <= 0) {
2061 mBatteryData.removeItemsAt(i);
2062 size --;
2063 i --;
2064 }
2065 i++;
2066 }
2067 return NO_ERROR;
2068}
nikoa64c8c72009-07-20 15:07:26 -07002069} // namespace android