blob: 16f131738c73df2f50ff1ebab34afcc3a3e1ea56 [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>
Mathias Agopian1a2952a2013-02-14 17:11:27 -080041#include <gui/Surface.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 Browne1045962012-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 Huberefbb7812012-09-18 10:36:32 -070075#include "HDCP.h"
Andreas Huberd7bee3a2012-08-29 11:41:50 -070076#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070077
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070078namespace {
nikoa64c8c72009-07-20 15:07:26 -070079using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070080using android::status_t;
81using android::OK;
82using android::BAD_VALUE;
83using android::NOT_ENOUGH_DATA;
84using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070085
86// Max number of entries in the filter.
87const int kMaxFilterSize = 64; // I pulled that out of thin air.
88
nikoa64c8c72009-07-20 15:07:26 -070089// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070090
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070091
92// Unmarshall a filter from a Parcel.
93// Filter format in a parcel:
94//
95// 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
96// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97// | number of entries (n) |
98// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99// | metadata type 1 |
100// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101// | metadata type 2 |
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103// ....
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | metadata type n |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107//
108// @param p Parcel that should start with a filter.
109// @param[out] filter On exit contains the list of metadata type to be
110// filtered.
111// @param[out] status On exit contains the status code to be returned.
112// @return true if the parcel starts with a valid filter.
113bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700114 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700115 status_t *status)
116{
Nicolas Catania48290382009-07-10 13:53:06 -0700117 int32_t val;
118 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700119 {
Steve Block29357bc2012-01-06 19:20:56 +0000120 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700121 *status = NOT_ENOUGH_DATA;
122 return false;
123 }
124
Nicolas Catania48290382009-07-10 13:53:06 -0700125 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 {
Steve Block29357bc2012-01-06 19:20:56 +0000127 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 *status = BAD_VALUE;
129 return false;
130 }
131
Nicolas Catania48290382009-07-10 13:53:06 -0700132 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133
134 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700135 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700136
nikoa64c8c72009-07-20 15:07:26 -0700137 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138
Nicolas Catania48290382009-07-10 13:53:06 -0700139
140 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700141 {
Steve Block29357bc2012-01-06 19:20:56 +0000142 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143 *status = NOT_ENOUGH_DATA;
144 return false;
145 }
146
nikoa64c8c72009-07-20 15:07:26 -0700147 const Metadata::Type *data =
148 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149
Nicolas Catania48290382009-07-10 13:53:06 -0700150 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151 {
Steve Block29357bc2012-01-06 19:20:56 +0000152 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700153 *status = BAD_VALUE;
154 return false;
155 }
156
157 // TODO: The stl impl of vector would be more efficient here
158 // because it degenerates into a memcpy on pod types. Try to
159 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700160 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161 {
Nicolas Catania48290382009-07-10 13:53:06 -0700162 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700163 ++data;
164 }
165 *status = OK;
166 return true;
167}
168
Nicolas Catania48290382009-07-10 13:53:06 -0700169// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170// @param val To be searched.
171// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700172bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700173{
174 // Deal with empty and ANY right away
175 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700176 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700177
Nicolas Catania48290382009-07-10 13:53:06 -0700178 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700179}
180
181} // anonymous namespace
182
183
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800184namespace android {
185
Dave Burked681bbb2011-08-30 14:39:17 +0100186static bool checkPermission(const char* permissionString) {
187#ifndef HAVE_ANDROID_OS
188 return true;
189#endif
190 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
191 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000192 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100193 return ok;
194}
195
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800196// 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 -0800197/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
198/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
199
200void MediaPlayerService::instantiate() {
201 defaultServiceManager()->addService(
202 String16("media.player"), new MediaPlayerService());
203}
204
205MediaPlayerService::MediaPlayerService()
206{
Steve Block3856b092011-10-20 11:56:00 +0100207 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800208 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800209
210 mBatteryAudio.refCount = 0;
211 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
212 mBatteryAudio.deviceOn[i] = 0;
213 mBatteryAudio.lastTime[i] = 0;
214 mBatteryAudio.totalTime[i] = 0;
215 }
216 // speaker is on by default
217 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700218
219 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800220}
221
222MediaPlayerService::~MediaPlayerService()
223{
Steve Block3856b092011-10-20 11:56:00 +0100224 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800225}
226
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800227sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800228{
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800229 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700230 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
231 wp<MediaRecorderClient> w = recorder;
232 Mutex::Autolock lock(mLock);
233 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100234 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800235 return recorder;
236}
237
Gloria Wangdac6a312009-10-29 15:46:37 -0700238void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
239{
240 Mutex::Autolock lock(mLock);
241 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100242 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700243}
244
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800245sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800246{
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800247 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100249 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250 return retriever;
251}
252
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800253sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100254 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800255{
Glenn Kasten8d6cc842012-02-03 11:06:53 -0800256 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700258
259 sp<Client> c = new Client(
260 this, pid, connId, client, audioSessionId,
261 IPCThreadState::self()->getCallingUid());
262
Steve Block3856b092011-10-20 11:56:00 +0100263 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100264 IPCThreadState::self()->getCallingUid());
265
266 wp<Client> w = c;
267 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 Mutex::Autolock lock(mLock);
269 mClients.add(w);
270 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800271 return c;
272}
273
Andreas Huber318ad9c2009-10-15 13:46:54 -0700274sp<IOMX> MediaPlayerService::getOMX() {
275 Mutex::Autolock autoLock(mLock);
276
277 if (mOMX.get() == NULL) {
278 mOMX = new OMX;
279 }
280
281 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700282}
283
Andreas Hubered3e3e02012-03-26 11:13:27 -0700284sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700285 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700286}
287
Andreas Hubera6a88d92013-01-30 10:41:25 -0800288sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
289 return new HDCP(createEncryptionModule);
Andreas Huberefbb7812012-09-18 10:36:32 -0700290}
291
Jeff Browne1045962012-09-04 21:38:42 -0700292sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
293 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownb0192502012-09-07 17:38:58 -0700294 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
295 return NULL;
296 }
297
Jeff Brown455d02e2012-09-05 17:48:03 -0700298 return new RemoteDisplay(client, iface.string());
Jeff Browne1045962012-09-04 21:38:42 -0700299}
300
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800301status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
302{
303 const size_t SIZE = 256;
304 char buffer[SIZE];
305 String8 result;
306
307 result.append(" AudioCache\n");
308 if (mHeap != 0) {
309 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
310 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
311 result.append(buffer);
312 }
313 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%ld)\n",
314 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
315 result.append(buffer);
316 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
317 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
318 result.append(buffer);
319 ::write(fd, result.string(), result.size());
320 return NO_ERROR;
321}
322
323status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
324{
325 const size_t SIZE = 256;
326 char buffer[SIZE];
327 String8 result;
328
329 result.append(" AudioOutput\n");
330 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
331 mStreamType, mLeftVolume, mRightVolume);
332 result.append(buffer);
333 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800334 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800335 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700336 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
337 mAuxEffectId, mSendLevel);
338 result.append(buffer);
339
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800340 ::write(fd, result.string(), result.size());
341 if (mTrack != 0) {
342 mTrack->dump(fd, args);
343 }
344 return NO_ERROR;
345}
346
347status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
348{
349 const size_t SIZE = 256;
350 char buffer[SIZE];
351 String8 result;
352 result.append(" Client\n");
353 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
354 mPid, mConnId, mStatus, mLoop?"true": "false");
355 result.append(buffer);
356 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700357 if (mPlayer != NULL) {
358 mPlayer->dump(fd, args);
359 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800360 if (mAudioOutput != 0) {
361 mAudioOutput->dump(fd, args);
362 }
363 write(fd, "\n", 1);
364 return NO_ERROR;
365}
366
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800367status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
368{
369 const size_t SIZE = 256;
370 char buffer[SIZE];
371 String8 result;
372 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
373 snprintf(buffer, SIZE, "Permission Denial: "
374 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
375 IPCThreadState::self()->getCallingPid(),
376 IPCThreadState::self()->getCallingUid());
377 result.append(buffer);
378 } else {
379 Mutex::Autolock lock(mLock);
380 for (int i = 0, n = mClients.size(); i < n; ++i) {
381 sp<Client> c = mClients[i].promote();
382 if (c != 0) c->dump(fd, args);
383 }
James Dongb9141222010-07-08 11:16:11 -0700384 if (mMediaRecorderClients.size() == 0) {
385 result.append(" No media recorder client\n\n");
386 } else {
387 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
388 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700389 if (c != 0) {
390 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
391 result.append(buffer);
392 write(fd, result.string(), result.size());
393 result = "\n";
394 c->dump(fd, args);
395 }
James Dongb9141222010-07-08 11:16:11 -0700396 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700397 }
398
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800399 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700400 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800401 FILE *f = fopen(buffer, "r");
402 if (f) {
403 while (!feof(f)) {
404 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700405 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800406 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700407 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800408 strstr(buffer, " /system/media/")) {
409 result.append(" ");
410 result.append(buffer);
411 }
412 }
413 fclose(f);
414 } else {
415 result.append("couldn't open ");
416 result.append(buffer);
417 result.append("\n");
418 }
419
Glenn Kasten0512ab52011-05-04 17:58:57 -0700420 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800421 DIR *d = opendir(buffer);
422 if (d) {
423 struct dirent *ent;
424 while((ent = readdir(d)) != NULL) {
425 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700426 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800427 struct stat s;
428 if (lstat(buffer, &s) == 0) {
429 if ((s.st_mode & S_IFMT) == S_IFLNK) {
430 char linkto[256];
431 int len = readlink(buffer, linkto, sizeof(linkto));
432 if(len > 0) {
433 if(len > 255) {
434 linkto[252] = '.';
435 linkto[253] = '.';
436 linkto[254] = '.';
437 linkto[255] = 0;
438 } else {
439 linkto[len] = 0;
440 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700441 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800442 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700443 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800444 strstr(linkto, "/system/media/") == linkto) {
445 result.append(" ");
446 result.append(buffer);
447 result.append(" -> ");
448 result.append(linkto);
449 result.append("\n");
450 }
451 }
452 } else {
453 result.append(" unexpected type for ");
454 result.append(buffer);
455 result.append("\n");
456 }
457 }
458 }
459 }
460 closedir(d);
461 } else {
462 result.append("couldn't open ");
463 result.append(buffer);
464 result.append("\n");
465 }
466
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467 bool dumpMem = false;
468 for (size_t i = 0; i < args.size(); i++) {
469 if (args[i] == String16("-m")) {
470 dumpMem = true;
471 }
472 }
473 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700474 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800475 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800476 }
477 write(fd, result.string(), result.size());
478 return NO_ERROR;
479}
480
481void MediaPlayerService::removeClient(wp<Client> client)
482{
483 Mutex::Autolock lock(mLock);
484 mClients.remove(client);
485}
486
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700487MediaPlayerService::Client::Client(
488 const sp<MediaPlayerService>& service, pid_t pid,
489 int32_t connId, const sp<IMediaPlayerClient>& client,
490 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491{
Steve Block3856b092011-10-20 11:56:00 +0100492 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800493 mPid = pid;
494 mConnId = connId;
495 mService = service;
496 mClient = client;
497 mLoop = false;
498 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700499 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700500 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800501 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700502
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800503#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000504 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505 mAntagonizer = new Antagonizer(notify, this);
506#endif
507}
508
509MediaPlayerService::Client::~Client()
510{
Steve Block3856b092011-10-20 11:56:00 +0100511 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800512 mAudioOutput.clear();
513 wp<Client> client(this);
514 disconnect();
515 mService->removeClient(client);
516}
517
518void MediaPlayerService::Client::disconnect()
519{
Steve Block3856b092011-10-20 11:56:00 +0100520 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800521 // grab local reference and clear main reference to prevent future
522 // access to object
523 sp<MediaPlayerBase> p;
524 {
525 Mutex::Autolock l(mLock);
526 p = mPlayer;
527 }
Dave Sparks795fa582009-03-24 17:57:12 -0700528 mClient.clear();
Andreas Huber20111aa2009-07-14 16:56:47 -0700529
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800530 mPlayer.clear();
531
532 // clear the notification to prevent callbacks to dead client
533 // and reset the player. We assume the player will serialize
534 // access to itself if necessary.
535 if (p != 0) {
536 p->setNotifyCallback(0, 0);
537#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000538 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 mAntagonizer->kill();
540#endif
541 p->reset();
542 }
543
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700544 disconnectNativeWindow();
545
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800546 IPCThreadState::self()->flushCommands();
547}
548
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800549sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
550{
551 // determine if we have the right player type
552 sp<MediaPlayerBase> p = mPlayer;
553 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100554 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 p.clear();
556 }
557 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700558 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700560
Jason Simmonsdb29e522011-08-12 13:46:55 -0700561 if (p != NULL) {
562 p->setUID(mUID);
563 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700564
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800565 return p;
566}
567
John Grossmanc795b642012-02-22 15:38:35 -0800568sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
569 player_type playerType)
570{
571 ALOGV("player type = %d", playerType);
572
573 // create the right type of player
574 sp<MediaPlayerBase> p = createPlayer(playerType);
575 if (p == NULL) {
576 return p;
577 }
578
579 if (!p->hardwareOutput()) {
580 mAudioOutput = new AudioOutput(mAudioSessionId);
581 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
582 }
583
584 return p;
585}
586
587void MediaPlayerService::Client::setDataSource_post(
588 const sp<MediaPlayerBase>& p,
589 status_t status)
590{
591 ALOGV(" setDataSource");
592 mStatus = status;
593 if (mStatus != OK) {
594 ALOGE(" error: %d", mStatus);
595 return;
596 }
597
598 // Set the re-transmission endpoint if one was chosen.
599 if (mRetransmitEndpointValid) {
600 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
601 if (mStatus != NO_ERROR) {
602 ALOGE("setRetransmitEndpoint error: %d", mStatus);
603 }
604 }
605
606 if (mStatus == OK) {
607 mPlayer = p;
608 }
609}
610
Andreas Huber2db84552010-01-28 11:19:57 -0800611status_t MediaPlayerService::Client::setDataSource(
612 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800613{
Steve Block3856b092011-10-20 11:56:00 +0100614 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615 if (url == NULL)
616 return UNKNOWN_ERROR;
617
Dave Burked681bbb2011-08-30 14:39:17 +0100618 if ((strncmp(url, "http://", 7) == 0) ||
619 (strncmp(url, "https://", 8) == 0) ||
620 (strncmp(url, "rtsp://", 7) == 0)) {
621 if (!checkPermission("android.permission.INTERNET")) {
622 return PERMISSION_DENIED;
623 }
624 }
625
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626 if (strncmp(url, "content://", 10) == 0) {
627 // get a filedescriptor for the content Uri and
628 // pass it to the setDataSource(fd) method
629
630 String16 url16(url);
631 int fd = android::openContentProviderFile(url16);
632 if (fd < 0)
633 {
Steve Block29357bc2012-01-06 19:20:56 +0000634 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800635 return UNKNOWN_ERROR;
636 }
637 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
638 close(fd);
639 return mStatus;
640 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700641 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800642 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
643 if (p == NULL) {
644 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800645 }
646
John Grossmanc795b642012-02-22 15:38:35 -0800647 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800648 return mStatus;
649 }
650}
651
652status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
653{
Steve Block3856b092011-10-20 11:56:00 +0100654 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800655 struct stat sb;
656 int ret = fstat(fd, &sb);
657 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000658 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800659 return UNKNOWN_ERROR;
660 }
661
Steve Block3856b092011-10-20 11:56:00 +0100662 ALOGV("st_dev = %llu", sb.st_dev);
663 ALOGV("st_mode = %u", sb.st_mode);
664 ALOGV("st_uid = %lu", sb.st_uid);
665 ALOGV("st_gid = %lu", sb.st_gid);
666 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667
668 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000669 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670 ::close(fd);
671 return UNKNOWN_ERROR;
672 }
673 if (offset + length > sb.st_size) {
674 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100675 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800676 }
677
John Grossman44a7e422012-06-21 17:29:24 -0700678 player_type playerType = MediaPlayerFactory::getPlayerType(this,
679 fd,
680 offset,
681 length);
John Grossmanc795b642012-02-22 15:38:35 -0800682 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
683 if (p == NULL) {
684 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800685 }
686
687 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800688 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689 return mStatus;
690}
691
Andreas Hubere2b10282010-11-23 11:41:34 -0800692status_t MediaPlayerService::Client::setDataSource(
693 const sp<IStreamSource> &source) {
694 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700695 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800696 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800697 if (p == NULL) {
698 return NO_INIT;
699 }
700
Andreas Hubere2b10282010-11-23 11:41:34 -0800701 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800702 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800703 return mStatus;
704}
705
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700706void MediaPlayerService::Client::disconnectNativeWindow() {
707 if (mConnectedWindow != NULL) {
708 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
709 NATIVE_WINDOW_API_MEDIA);
710
711 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000712 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700713 strerror(-err), err);
714 }
715 }
716 mConnectedWindow.clear();
717}
718
Glenn Kasten11731182011-02-08 17:26:17 -0800719status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden8ba01022012-12-18 09:46:54 -0800720 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800721{
Andy McFadden8ba01022012-12-18 09:46:54 -0800722 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800723 sp<MediaPlayerBase> p = getPlayer();
724 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700725
Andy McFadden8ba01022012-12-18 09:46:54 -0800726 sp<IBinder> binder(bufferProducer == NULL ? NULL :
727 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700728 if (mConnectedWindowBinder == binder) {
729 return OK;
730 }
731
732 sp<ANativeWindow> anw;
Andy McFadden8ba01022012-12-18 09:46:54 -0800733 if (bufferProducer != NULL) {
Mathias Agopian1a2952a2013-02-14 17:11:27 -0800734 anw = new Surface(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700735 status_t err = native_window_api_connect(anw.get(),
736 NATIVE_WINDOW_API_MEDIA);
737
738 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000739 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700740 // Note that we must do the reset before disconnecting from the ANW.
741 // Otherwise queue/dequeue calls could be made on the disconnected
742 // ANW, which may result in errors.
743 reset();
744
745 disconnectNativeWindow();
746
747 return err;
748 }
749 }
750
Andy McFadden8ba01022012-12-18 09:46:54 -0800751 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700752 // disconnecting the old one. Otherwise queue/dequeue calls could be made
753 // on the disconnected ANW, which may result in errors.
Andy McFadden8ba01022012-12-18 09:46:54 -0800754 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700755
756 disconnectNativeWindow();
757
758 mConnectedWindow = anw;
759
760 if (err == OK) {
761 mConnectedWindowBinder = binder;
762 } else {
763 disconnectNativeWindow();
764 }
765
766 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800767}
768
Nicolas Catania1d187f12009-05-12 23:25:55 -0700769status_t MediaPlayerService::Client::invoke(const Parcel& request,
770 Parcel *reply)
771{
772 sp<MediaPlayerBase> p = getPlayer();
773 if (p == NULL) return UNKNOWN_ERROR;
774 return p->invoke(request, reply);
775}
776
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700777// This call doesn't need to access the native player.
778status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
779{
780 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700781 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700782
Nicolas Catania48290382009-07-10 13:53:06 -0700783 if (unmarshallFilter(filter, &allow, &status) &&
784 unmarshallFilter(filter, &drop, &status)) {
785 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700786
787 mMetadataAllow = allow;
788 mMetadataDrop = drop;
789 }
790 return status;
791}
792
Nicolas Catania48290382009-07-10 13:53:06 -0700793status_t MediaPlayerService::Client::getMetadata(
794 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700795{
nikoa64c8c72009-07-20 15:07:26 -0700796 sp<MediaPlayerBase> player = getPlayer();
797 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700798
nikod608a812009-07-16 16:39:53 -0700799 status_t status;
800 // Placeholder for the return code, updated by the caller.
801 reply->writeInt32(-1);
802
nikoa64c8c72009-07-20 15:07:26 -0700803 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700804
805 // We don't block notifications while we fetch the data. We clear
806 // mMetadataUpdated first so we don't lose notifications happening
807 // during the rest of this call.
808 {
809 Mutex::Autolock lock(mLock);
810 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700811 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700812 }
813 mMetadataUpdated.clear();
814 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700815
nikoa64c8c72009-07-20 15:07:26 -0700816 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700817
nikoa64c8c72009-07-20 15:07:26 -0700818 metadata.appendHeader();
819 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700820
821 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700822 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000823 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700824 return status;
825 }
826
827 // FIXME: Implement filtering on the result. Not critical since
828 // filtering takes place on the update notifications already. This
829 // would be when all the metadata are fetch and a filter is set.
830
nikod608a812009-07-16 16:39:53 -0700831 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700832 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700833 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700834}
835
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800836status_t MediaPlayerService::Client::prepareAsync()
837{
Steve Block3856b092011-10-20 11:56:00 +0100838 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800839 sp<MediaPlayerBase> p = getPlayer();
840 if (p == 0) return UNKNOWN_ERROR;
841 status_t ret = p->prepareAsync();
842#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000843 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844 if (ret == NO_ERROR) mAntagonizer->start();
845#endif
846 return ret;
847}
848
849status_t MediaPlayerService::Client::start()
850{
Steve Block3856b092011-10-20 11:56:00 +0100851 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800852 sp<MediaPlayerBase> p = getPlayer();
853 if (p == 0) return UNKNOWN_ERROR;
854 p->setLooping(mLoop);
855 return p->start();
856}
857
858status_t MediaPlayerService::Client::stop()
859{
Steve Block3856b092011-10-20 11:56:00 +0100860 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 sp<MediaPlayerBase> p = getPlayer();
862 if (p == 0) return UNKNOWN_ERROR;
863 return p->stop();
864}
865
866status_t MediaPlayerService::Client::pause()
867{
Steve Block3856b092011-10-20 11:56:00 +0100868 ALOGV("[%d] pause", 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->pause();
872}
873
874status_t MediaPlayerService::Client::isPlaying(bool* state)
875{
876 *state = false;
877 sp<MediaPlayerBase> p = getPlayer();
878 if (p == 0) return UNKNOWN_ERROR;
879 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100880 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800881 return NO_ERROR;
882}
883
884status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
885{
Steve Block3856b092011-10-20 11:56:00 +0100886 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800887 sp<MediaPlayerBase> p = getPlayer();
888 if (p == 0) return UNKNOWN_ERROR;
889 status_t ret = p->getCurrentPosition(msec);
890 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800892 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000893 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800894 }
895 return ret;
896}
897
898status_t MediaPlayerService::Client::getDuration(int *msec)
899{
Steve Block3856b092011-10-20 11:56:00 +0100900 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800901 sp<MediaPlayerBase> p = getPlayer();
902 if (p == 0) return UNKNOWN_ERROR;
903 status_t ret = p->getDuration(msec);
904 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100905 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800906 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000907 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800908 }
909 return ret;
910}
911
Marco Nelissen6b74d672012-02-28 16:07:44 -0800912status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
913 ALOGV("setNextPlayer");
914 Mutex::Autolock l(mLock);
915 sp<Client> c = static_cast<Client*>(player.get());
916 mNextClient = c;
John Grossman2e5f22e2012-08-24 14:47:25 -0700917
918 if (c != NULL) {
919 if (mAudioOutput != NULL) {
920 mAudioOutput->setNextOutput(c->mAudioOutput);
921 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
922 ALOGE("no current audio output");
923 }
924
925 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
926 mPlayer->setNextPlayer(mNextClient->getPlayer());
927 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800928 }
John Grossman2e5f22e2012-08-24 14:47:25 -0700929
Marco Nelissen6b74d672012-02-28 16:07:44 -0800930 return OK;
931}
932
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800933status_t MediaPlayerService::Client::seekTo(int msec)
934{
Steve Block3856b092011-10-20 11:56:00 +0100935 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800936 sp<MediaPlayerBase> p = getPlayer();
937 if (p == 0) return UNKNOWN_ERROR;
938 return p->seekTo(msec);
939}
940
941status_t MediaPlayerService::Client::reset()
942{
Steve Block3856b092011-10-20 11:56:00 +0100943 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800944 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800945 sp<MediaPlayerBase> p = getPlayer();
946 if (p == 0) return UNKNOWN_ERROR;
947 return p->reset();
948}
949
Glenn Kastenfff6d712012-01-12 16:38:12 -0800950status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800951{
Steve Block3856b092011-10-20 11:56:00 +0100952 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800953 // TODO: for hardware output, call player instead
954 Mutex::Autolock l(mLock);
955 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
956 return NO_ERROR;
957}
958
959status_t MediaPlayerService::Client::setLooping(int loop)
960{
Steve Block3856b092011-10-20 11:56:00 +0100961 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962 mLoop = loop;
963 sp<MediaPlayerBase> p = getPlayer();
964 if (p != 0) return p->setLooping(loop);
965 return NO_ERROR;
966}
967
968status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
969{
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800971
972 // for hardware output, call player instead
973 sp<MediaPlayerBase> p = getPlayer();
974 {
975 Mutex::Autolock l(mLock);
976 if (p != 0 && p->hardwareOutput()) {
977 MediaPlayerHWInterface* hwp =
978 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
979 return hwp->setVolume(leftVolume, rightVolume);
980 } else {
981 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
982 return NO_ERROR;
983 }
984 }
985
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986 return NO_ERROR;
987}
988
Eric Laurent2beeb502010-07-16 07:43:46 -0700989status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
990{
Steve Block3856b092011-10-20 11:56:00 +0100991 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -0700992 Mutex::Autolock l(mLock);
993 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
994 return NO_ERROR;
995}
996
997status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
998{
Steve Block3856b092011-10-20 11:56:00 +0100999 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001000 Mutex::Autolock l(mLock);
1001 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1002 return NO_ERROR;
1003}
Nicolas Catania48290382009-07-10 13:53:06 -07001004
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001005status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001006 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001007 sp<MediaPlayerBase> p = getPlayer();
1008 if (p == 0) return UNKNOWN_ERROR;
1009 return p->setParameter(key, request);
1010}
1011
1012status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001013 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001014 sp<MediaPlayerBase> p = getPlayer();
1015 if (p == 0) return UNKNOWN_ERROR;
1016 return p->getParameter(key, reply);
1017}
1018
John Grossmanc795b642012-02-22 15:38:35 -08001019status_t MediaPlayerService::Client::setRetransmitEndpoint(
1020 const struct sockaddr_in* endpoint) {
1021
1022 if (NULL != endpoint) {
1023 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1024 uint16_t p = ntohs(endpoint->sin_port);
1025 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1026 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1027 } else {
1028 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1029 }
1030
1031 sp<MediaPlayerBase> p = getPlayer();
1032
1033 // Right now, the only valid time to set a retransmit endpoint is before
1034 // player selection has been made (since the presence or absence of a
1035 // retransmit endpoint is going to determine which player is selected during
1036 // setDataSource).
1037 if (p != 0) return INVALID_OPERATION;
1038
1039 if (NULL != endpoint) {
1040 mRetransmitEndpoint = *endpoint;
1041 mRetransmitEndpointValid = true;
1042 } else {
1043 mRetransmitEndpointValid = false;
1044 }
1045
1046 return NO_ERROR;
1047}
1048
John Grossman44a7e422012-06-21 17:29:24 -07001049status_t MediaPlayerService::Client::getRetransmitEndpoint(
1050 struct sockaddr_in* endpoint)
1051{
1052 if (NULL == endpoint)
1053 return BAD_VALUE;
1054
1055 sp<MediaPlayerBase> p = getPlayer();
1056
1057 if (p != NULL)
1058 return p->getRetransmitEndpoint(endpoint);
1059
1060 if (!mRetransmitEndpointValid)
1061 return NO_INIT;
1062
1063 *endpoint = mRetransmitEndpoint;
1064
1065 return NO_ERROR;
1066}
1067
Gloria Wangb483c472011-04-11 17:23:27 -07001068void MediaPlayerService::Client::notify(
1069 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001070{
1071 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001072 if (client == NULL) {
1073 return;
1074 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001075
James Dongb8a98252012-08-26 16:13:03 -07001076 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001077 {
1078 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001079 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001080 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001081 if (client->mAudioOutput != NULL)
1082 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001083 client->mNextClient->start();
1084 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1085 }
1086 }
1087
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001088 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001089 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001090 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001091
1092 if(client->shouldDropMetadata(metadata_type)) {
1093 return;
1094 }
1095
1096 // Update the list of metadata that have changed. getMetadata
1097 // also access mMetadataUpdated and clears it.
1098 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001099 }
James Dongb8a98252012-08-26 16:13:03 -07001100
1101 if (c != NULL) {
1102 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1103 c->notify(msg, ext1, ext2, obj);
1104 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001105}
1106
Nicolas Catania48290382009-07-10 13:53:06 -07001107
nikoa64c8c72009-07-20 15:07:26 -07001108bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001109{
Nicolas Catania48290382009-07-10 13:53:06 -07001110 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001111
Nicolas Catania48290382009-07-10 13:53:06 -07001112 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001113 return true;
1114 }
1115
Nicolas Catania48290382009-07-10 13:53:06 -07001116 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001117 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001118 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001119 return true;
1120 }
1121}
1122
Nicolas Catania48290382009-07-10 13:53:06 -07001123
nikoa64c8c72009-07-20 15:07:26 -07001124void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001125 Mutex::Autolock lock(mLock);
1126 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1127 mMetadataUpdated.add(metadata_type);
1128 }
1129}
1130
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001131#if CALLBACK_ANTAGONIZER
1132const int Antagonizer::interval = 10000; // 10 msecs
1133
1134Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1135 mExit(false), mActive(false), mClient(client), mCb(cb)
1136{
1137 createThread(callbackThread, this);
1138}
1139
1140void Antagonizer::kill()
1141{
1142 Mutex::Autolock _l(mLock);
1143 mActive = false;
1144 mExit = true;
1145 mCondition.wait(mLock);
1146}
1147
1148int Antagonizer::callbackThread(void* user)
1149{
Steve Blockb8a80522011-12-20 16:23:08 +00001150 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001151 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1152 while (!p->mExit) {
1153 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001154 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001155 p->mCb(p->mClient, 0, 0, 0);
1156 }
1157 usleep(interval);
1158 }
1159 Mutex::Autolock _l(p->mLock);
1160 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001161 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001162 return 0;
1163}
1164#endif
1165
1166static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1167
Glenn Kastene1c39622012-01-04 09:36:37 -08001168sp<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 -08001169{
Steve Block3856b092011-10-20 11:56:00 +01001170 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001171 sp<MemoryBase> mem;
1172 sp<MediaPlayerBase> player;
1173
1174 // Protect our precious, precious DRMd ringtones by only allowing
1175 // decoding of http, but not filesystem paths or content Uris.
1176 // If the application wants to decode those, it should open a
1177 // filedescriptor for them and use that.
1178 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001179 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001180 return mem;
1181 }
1182
John Grossman44a7e422012-06-21 17:29:24 -07001183 player_type playerType =
1184 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001185 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001186
1187 // create the right type of player
1188 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001189 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001190 if (player == NULL) goto Exit;
1191 if (player->hardwareOutput()) goto Exit;
1192
1193 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1194
1195 // set data source
1196 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1197
Steve Block3856b092011-10-20 11:56:00 +01001198 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001199 player->prepareAsync();
1200
Steve Block3856b092011-10-20 11:56:00 +01001201 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001202 if (cache->wait() != NO_ERROR) goto Exit;
1203
Steve Block3856b092011-10-20 11:56:00 +01001204 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001205 player->start();
1206
Steve Block3856b092011-10-20 11:56:00 +01001207 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001208 cache->wait();
1209 // in case of error, return what was successfully decoded.
1210 if (cache->size() == 0) {
1211 goto Exit;
1212 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213
1214 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1215 *pSampleRate = cache->sampleRate();
1216 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001217 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001218 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 -08001219
1220Exit:
1221 if (player != 0) player->reset();
1222 return mem;
1223}
1224
Glenn Kastene1c39622012-01-04 09:36:37 -08001225sp<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 -08001226{
Steve Block3856b092011-10-20 11:56:00 +01001227 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001228 sp<MemoryBase> mem;
1229 sp<MediaPlayerBase> player;
1230
John Grossman44a7e422012-06-21 17:29:24 -07001231 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1232 fd,
1233 offset,
1234 length);
Steve Block3856b092011-10-20 11:56:00 +01001235 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001236
1237 // create the right type of player
1238 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001239 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240 if (player == NULL) goto Exit;
1241 if (player->hardwareOutput()) goto Exit;
1242
1243 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1244
1245 // set data source
1246 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1247
Steve Block3856b092011-10-20 11:56:00 +01001248 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001249 player->prepareAsync();
1250
Steve Block3856b092011-10-20 11:56:00 +01001251 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001252 if (cache->wait() != NO_ERROR) goto Exit;
1253
Steve Block3856b092011-10-20 11:56:00 +01001254 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001255 player->start();
1256
Steve Block3856b092011-10-20 11:56:00 +01001257 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001258 cache->wait();
1259 // in case of error, return what was successfully decoded.
1260 if (cache->size() == 0) {
1261 goto Exit;
1262 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263
1264 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1265 *pSampleRate = cache->sampleRate();
1266 *pNumChannels = cache->channelCount();
1267 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001268 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 -08001269
1270Exit:
1271 if (player != 0) player->reset();
1272 ::close(fd);
1273 return mem;
1274}
1275
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001276
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001277#undef LOG_TAG
1278#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001279MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001280 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001281 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001282 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001283 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001284 mSessionId(sessionId),
1285 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001286 ALOGV("AudioOutput(%d)", sessionId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001288 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001289 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290 mLeftVolume = 1.0;
1291 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001292 mPlaybackRatePermille = 1000;
1293 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001294 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001295 mAuxEffectId = 0;
1296 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 setMinBufferCount();
1298}
1299
1300MediaPlayerService::AudioOutput::~AudioOutput()
1301{
1302 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001303 delete mRecycledTrack;
1304 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305}
1306
1307void MediaPlayerService::AudioOutput::setMinBufferCount()
1308{
1309 char value[PROPERTY_VALUE_MAX];
1310 if (property_get("ro.kernel.qemu", value, 0)) {
1311 mIsOnEmulator = true;
1312 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1313 }
1314}
1315
1316bool MediaPlayerService::AudioOutput::isOnEmulator()
1317{
1318 setMinBufferCount();
1319 return mIsOnEmulator;
1320}
1321
1322int MediaPlayerService::AudioOutput::getMinBufferCount()
1323{
1324 setMinBufferCount();
1325 return mMinBufferCount;
1326}
1327
1328ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1329{
1330 if (mTrack == 0) return NO_INIT;
1331 return mTrack->frameCount() * frameSize();
1332}
1333
1334ssize_t MediaPlayerService::AudioOutput::frameCount() const
1335{
1336 if (mTrack == 0) return NO_INIT;
1337 return mTrack->frameCount();
1338}
1339
1340ssize_t MediaPlayerService::AudioOutput::channelCount() const
1341{
1342 if (mTrack == 0) return NO_INIT;
1343 return mTrack->channelCount();
1344}
1345
1346ssize_t MediaPlayerService::AudioOutput::frameSize() const
1347{
1348 if (mTrack == 0) return NO_INIT;
1349 return mTrack->frameSize();
1350}
1351
1352uint32_t MediaPlayerService::AudioOutput::latency () const
1353{
Eric Laurentdb354e52012-03-05 17:27:11 -08001354 if (mTrack == 0) return 0;
1355 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001356}
1357
1358float MediaPlayerService::AudioOutput::msecsPerFrame() const
1359{
1360 return mMsecsPerFrame;
1361}
1362
Marco Nelissen4110c102012-03-29 09:31:28 -07001363status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001364{
1365 if (mTrack == 0) return NO_INIT;
1366 return mTrack->getPosition(position);
1367}
1368
Marco Nelissen4110c102012-03-29 09:31:28 -07001369status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1370{
1371 if (mTrack == 0) return NO_INIT;
1372 *frameswritten = mBytesWritten / frameSize();
1373 return OK;
1374}
1375
Andreas Huber20111aa2009-07-14 16:56:47 -07001376status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001377 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1378 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001379 AudioCallback cb, void *cookie,
1380 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001381{
Andreas Huber20111aa2009-07-14 16:56:47 -07001382 mCallback = cb;
1383 mCallbackCookie = cookie;
1384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001385 // Check argument "bufferCount" against the mininum buffer count
1386 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001387 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001388 bufferCount = mMinBufferCount;
1389
1390 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001391 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1392 format, bufferCount, mSessionId);
Glenn Kasten3b16c762012-11-14 08:44:39 -08001393 uint32_t afSampleRate;
Glenn Kastene33054e2012-11-14 12:54:39 -08001394 size_t afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001395 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001396
1397 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1398 return NO_INIT;
1399 }
1400 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1401 return NO_INIT;
1402 }
1403
1404 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001405
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001406 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001407 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001408 if (0 == channelMask) {
1409 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1410 return NO_INIT;
1411 }
1412 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001413
Andreas Huber20111aa2009-07-14 16:56:47 -07001414 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001415 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001416 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001417 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001418 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001419 mStreamType,
1420 sampleRate,
1421 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001422 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001423 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001424 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001425 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001426 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001427 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001428 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001429 } else {
1430 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001431 mStreamType,
1432 sampleRate,
1433 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001434 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001435 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001436 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001437 NULL,
1438 NULL,
1439 0,
1440 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001441 }
1442
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001443 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001444 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001445 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001446 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001447 return NO_INIT;
1448 }
1449
Marco Nelissen67295b52012-06-11 14:52:53 -07001450
1451 if (mRecycledTrack) {
1452 // check if the existing track can be reused as-is, or if a new track needs to be created.
1453
1454 bool reuse = true;
1455 if ((mCallbackData == NULL && mCallback != NULL) ||
1456 (mCallbackData != NULL && mCallback == NULL)) {
1457 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1458 ALOGV("can't chain callback and write");
1459 reuse = false;
1460 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1461 (mRecycledTrack->channelCount() != channelCount) ||
1462 (mRecycledTrack->frameCount() != t->frameCount())) {
1463 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1464 mRecycledTrack->getSampleRate(), sampleRate,
1465 mRecycledTrack->channelCount(), channelCount,
1466 mRecycledTrack->frameCount(), t->frameCount());
1467 reuse = false;
1468 } else if (flags != mFlags) {
1469 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1470 reuse = false;
1471 }
1472 if (reuse) {
1473 ALOGV("chaining to next output");
1474 close();
1475 mTrack = mRecycledTrack;
1476 mRecycledTrack = NULL;
1477 if (mCallbackData != NULL) {
1478 mCallbackData->setOutput(this);
1479 }
1480 delete t;
1481 delete newcbd;
1482 return OK;
1483 }
1484
1485 // if we're not going to reuse the track, unblock and flush it
1486 if (mCallbackData != NULL) {
1487 mCallbackData->setOutput(NULL);
1488 mCallbackData->endTrackSwitch();
1489 }
1490 mRecycledTrack->flush();
1491 delete mRecycledTrack;
1492 mRecycledTrack = NULL;
1493 delete mCallbackData;
1494 mCallbackData = NULL;
1495 close();
1496 }
1497
1498 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001499 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001500 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001501
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001502 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001503 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001504 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001505 uint32_t pos;
1506 if (t->getPosition(&pos) == OK) {
1507 mBytesWritten = uint64_t(pos) * t->frameSize();
1508 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001509 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001510
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001511 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1512 if (res != NO_ERROR) {
1513 return res;
1514 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001515 t->setAuxEffectSendLevel(mSendLevel);
1516 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001517}
1518
1519void MediaPlayerService::AudioOutput::start()
1520{
Steve Block3856b092011-10-20 11:56:00 +01001521 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001522 if (mCallbackData != NULL) {
1523 mCallbackData->endTrackSwitch();
1524 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001525 if (mTrack) {
1526 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001527 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528 mTrack->start();
1529 }
1530}
1531
Marco Nelissen6b74d672012-02-28 16:07:44 -08001532void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1533 mNextOutput = nextOutput;
1534}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001535
1536
Marco Nelissen6b74d672012-02-28 16:07:44 -08001537void MediaPlayerService::AudioOutput::switchToNextOutput() {
1538 ALOGV("switchToNextOutput");
1539 if (mNextOutput != NULL) {
1540 if (mCallbackData != NULL) {
1541 mCallbackData->beginTrackSwitch();
1542 }
1543 delete mNextOutput->mCallbackData;
1544 mNextOutput->mCallbackData = mCallbackData;
1545 mCallbackData = NULL;
1546 mNextOutput->mRecycledTrack = mTrack;
1547 mTrack = NULL;
1548 mNextOutput->mSampleRateHz = mSampleRateHz;
1549 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001550 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001551 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001552 }
1553}
1554
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001555ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1556{
Andreas Huber20111aa2009-07-14 16:56:47 -07001557 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1558
Steve Block3856b092011-10-20 11:56:00 +01001559 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001560 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001561 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001562 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001563 return ret;
1564 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001565 return NO_INIT;
1566}
1567
1568void MediaPlayerService::AudioOutput::stop()
1569{
Steve Block3856b092011-10-20 11:56:00 +01001570 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001571 if (mTrack) mTrack->stop();
1572}
1573
1574void MediaPlayerService::AudioOutput::flush()
1575{
Steve Block3856b092011-10-20 11:56:00 +01001576 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001577 if (mTrack) mTrack->flush();
1578}
1579
1580void MediaPlayerService::AudioOutput::pause()
1581{
Steve Block3856b092011-10-20 11:56:00 +01001582 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001583 if (mTrack) mTrack->pause();
1584}
1585
1586void MediaPlayerService::AudioOutput::close()
1587{
Steve Block3856b092011-10-20 11:56:00 +01001588 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001589 delete mTrack;
1590 mTrack = 0;
1591}
1592
1593void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1594{
Steve Block3856b092011-10-20 11:56:00 +01001595 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596 mLeftVolume = left;
1597 mRightVolume = right;
1598 if (mTrack) {
1599 mTrack->setVolume(left, right);
1600 }
1601}
1602
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001603status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1604{
1605 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1606 status_t res = NO_ERROR;
1607 if (mTrack) {
1608 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1609 } else {
1610 res = NO_INIT;
1611 }
1612 mPlaybackRatePermille = ratePermille;
1613 if (mSampleRateHz != 0) {
1614 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1615 }
1616 return res;
1617}
1618
Eric Laurent2beeb502010-07-16 07:43:46 -07001619status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1620{
Steve Block3856b092011-10-20 11:56:00 +01001621 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001622 mSendLevel = level;
1623 if (mTrack) {
1624 return mTrack->setAuxEffectSendLevel(level);
1625 }
1626 return NO_ERROR;
1627}
1628
1629status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1630{
Steve Block3856b092011-10-20 11:56:00 +01001631 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001632 mAuxEffectId = effectId;
1633 if (mTrack) {
1634 return mTrack->attachAuxEffect(effectId);
1635 }
1636 return NO_ERROR;
1637}
1638
Andreas Huber20111aa2009-07-14 16:56:47 -07001639// static
1640void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001641 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001642 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001643 if (event != AudioTrack::EVENT_MORE_DATA) {
1644 return;
1645 }
1646
Marco Nelissen6b74d672012-02-28 16:07:44 -08001647 CallbackData *data = (CallbackData*)cookie;
1648 data->lock();
1649 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001650 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001651 if (me == NULL) {
1652 // no output set, likely because the track was scheduled to be reused
1653 // by another player, but the format turned out to be incompatible.
1654 data->unlock();
1655 buffer->size = 0;
1656 return;
1657 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001658
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001659 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001660 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001661
Marco Nelissen6b74d672012-02-28 16:07:44 -08001662 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001663 // We've reached EOS but the audio track is not stopped yet,
1664 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001665
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001666 memset(buffer->raw, 0, buffer->size);
1667 actualSize = buffer->size;
1668 }
1669
1670 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001671 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001672}
1673
Marco Nelissen4110c102012-03-29 09:31:28 -07001674int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001675{
1676 return mSessionId;
1677}
1678
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001679#undef LOG_TAG
1680#define LOG_TAG "AudioCache"
1681MediaPlayerService::AudioCache::AudioCache(const char* name) :
1682 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1683 mError(NO_ERROR), mCommandComplete(false)
1684{
1685 // create ashmem heap
1686 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1687}
1688
1689uint32_t MediaPlayerService::AudioCache::latency () const
1690{
1691 return 0;
1692}
1693
1694float MediaPlayerService::AudioCache::msecsPerFrame() const
1695{
1696 return mMsecsPerFrame;
1697}
1698
Marco Nelissen4110c102012-03-29 09:31:28 -07001699status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001700{
1701 if (position == 0) return BAD_VALUE;
1702 *position = mSize;
1703 return NO_ERROR;
1704}
1705
Marco Nelissen4110c102012-03-29 09:31:28 -07001706status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1707{
1708 if (written == 0) return BAD_VALUE;
1709 *written = mSize;
1710 return NO_ERROR;
1711}
1712
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001713////////////////////////////////////////////////////////////////////////////////
1714
1715struct CallbackThread : public Thread {
1716 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1717 MediaPlayerBase::AudioSink::AudioCallback cb,
1718 void *cookie);
1719
1720protected:
1721 virtual ~CallbackThread();
1722
1723 virtual bool threadLoop();
1724
1725private:
1726 wp<MediaPlayerBase::AudioSink> mSink;
1727 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1728 void *mCookie;
1729 void *mBuffer;
1730 size_t mBufferSize;
1731
1732 CallbackThread(const CallbackThread &);
1733 CallbackThread &operator=(const CallbackThread &);
1734};
1735
1736CallbackThread::CallbackThread(
1737 const wp<MediaPlayerBase::AudioSink> &sink,
1738 MediaPlayerBase::AudioSink::AudioCallback cb,
1739 void *cookie)
1740 : mSink(sink),
1741 mCallback(cb),
1742 mCookie(cookie),
1743 mBuffer(NULL),
1744 mBufferSize(0) {
1745}
1746
1747CallbackThread::~CallbackThread() {
1748 if (mBuffer) {
1749 free(mBuffer);
1750 mBuffer = NULL;
1751 }
1752}
1753
1754bool CallbackThread::threadLoop() {
1755 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1756 if (sink == NULL) {
1757 return false;
1758 }
1759
1760 if (mBuffer == NULL) {
1761 mBufferSize = sink->bufferSize();
1762 mBuffer = malloc(mBufferSize);
1763 }
1764
1765 size_t actualSize =
1766 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1767
1768 if (actualSize > 0) {
1769 sink->write(mBuffer, actualSize);
1770 }
1771
1772 return true;
1773}
1774
1775////////////////////////////////////////////////////////////////////////////////
1776
Andreas Huber20111aa2009-07-14 16:56:47 -07001777status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001778 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1779 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001780 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001781{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001782 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001783 if (mHeap->getHeapID() < 0) {
1784 return NO_INIT;
1785 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001786
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001787 mSampleRate = sampleRate;
1788 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001789 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001790 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001791
1792 if (cb != NULL) {
1793 mCallbackThread = new CallbackThread(this, cb, cookie);
1794 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001795 return NO_ERROR;
1796}
1797
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001798void MediaPlayerService::AudioCache::start() {
1799 if (mCallbackThread != NULL) {
1800 mCallbackThread->run("AudioCache callback");
1801 }
1802}
1803
1804void MediaPlayerService::AudioCache::stop() {
1805 if (mCallbackThread != NULL) {
1806 mCallbackThread->requestExitAndWait();
1807 }
1808}
1809
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001810ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1811{
Steve Block3856b092011-10-20 11:56:00 +01001812 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001813 if ((buffer == 0) || (size == 0)) return size;
1814
1815 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1816 if (p == NULL) return NO_INIT;
1817 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001818 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001819 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001820 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001821 size = mHeap->getSize() - mSize;
1822 }
1823 memcpy(p, buffer, size);
1824 mSize += size;
1825 return size;
1826}
1827
1828// call with lock held
1829status_t MediaPlayerService::AudioCache::wait()
1830{
1831 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001832 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001833 mSignal.wait(mLock);
1834 }
1835 mCommandComplete = false;
1836
1837 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001838 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001839 } else {
Steve Block3856b092011-10-20 11:56:00 +01001840 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001841 }
1842 return mError;
1843}
1844
Gloria Wangb483c472011-04-11 17:23:27 -07001845void MediaPlayerService::AudioCache::notify(
1846 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001847{
Steve Block3856b092011-10-20 11:56:00 +01001848 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001849 AudioCache* p = static_cast<AudioCache*>(cookie);
1850
1851 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001852 switch (msg)
1853 {
1854 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001855 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001856 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001857 break;
1858 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001859 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001860 break;
1861 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001862 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001863 break;
1864 default:
Steve Block3856b092011-10-20 11:56:00 +01001865 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001866 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001867 }
1868
1869 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001870 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001871 p->mCommandComplete = true;
1872 p->mSignal.signal();
1873}
1874
Marco Nelissen4110c102012-03-29 09:31:28 -07001875int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001876{
1877 return 0;
1878}
1879
Gloria Wang7cf180c2011-02-19 18:37:57 -08001880void MediaPlayerService::addBatteryData(uint32_t params)
1881{
1882 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001883
1884 int32_t time = systemTime() / 1000000L;
1885
1886 // change audio output devices. This notification comes from AudioFlinger
1887 if ((params & kBatteryDataSpeakerOn)
1888 || (params & kBatteryDataOtherAudioDeviceOn)) {
1889
1890 int deviceOn[NUM_AUDIO_DEVICES];
1891 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1892 deviceOn[i] = 0;
1893 }
1894
1895 if ((params & kBatteryDataSpeakerOn)
1896 && (params & kBatteryDataOtherAudioDeviceOn)) {
1897 deviceOn[SPEAKER_AND_OTHER] = 1;
1898 } else if (params & kBatteryDataSpeakerOn) {
1899 deviceOn[SPEAKER] = 1;
1900 } else {
1901 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1902 }
1903
1904 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1905 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1906
1907 if (mBatteryAudio.refCount > 0) { // if playing audio
1908 if (!deviceOn[i]) {
1909 mBatteryAudio.lastTime[i] += time;
1910 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1911 mBatteryAudio.lastTime[i] = 0;
1912 } else {
1913 mBatteryAudio.lastTime[i] = 0 - time;
1914 }
1915 }
1916
1917 mBatteryAudio.deviceOn[i] = deviceOn[i];
1918 }
1919 }
1920 return;
1921 }
1922
1923 // an sudio stream is started
1924 if (params & kBatteryDataAudioFlingerStart) {
1925 // record the start time only if currently no other audio
1926 // is being played
1927 if (mBatteryAudio.refCount == 0) {
1928 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1929 if (mBatteryAudio.deviceOn[i]) {
1930 mBatteryAudio.lastTime[i] -= time;
1931 }
1932 }
1933 }
1934
1935 mBatteryAudio.refCount ++;
1936 return;
1937
1938 } else if (params & kBatteryDataAudioFlingerStop) {
1939 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001940 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001941 return;
1942 }
1943
1944 // record the stop time only if currently this is the only
1945 // audio being played
1946 if (mBatteryAudio.refCount == 1) {
1947 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1948 if (mBatteryAudio.deviceOn[i]) {
1949 mBatteryAudio.lastTime[i] += time;
1950 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1951 mBatteryAudio.lastTime[i] = 0;
1952 }
1953 }
1954 }
1955
1956 mBatteryAudio.refCount --;
1957 return;
1958 }
1959
Gloria Wang7cf180c2011-02-19 18:37:57 -08001960 int uid = IPCThreadState::self()->getCallingUid();
1961 if (uid == AID_MEDIA) {
1962 return;
1963 }
1964 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001965
1966 if (index < 0) { // create a new entry for this UID
1967 BatteryUsageInfo info;
1968 info.audioTotalTime = 0;
1969 info.videoTotalTime = 0;
1970 info.audioLastTime = 0;
1971 info.videoLastTime = 0;
1972 info.refCount = 0;
1973
Gloria Wang9ee159b2011-02-24 14:51:45 -08001974 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001975 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001976 return;
1977 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001978 }
1979
1980 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1981
1982 if (params & kBatteryDataCodecStarted) {
1983 if (params & kBatteryDataTrackAudio) {
1984 info.audioLastTime -= time;
1985 info.refCount ++;
1986 }
1987 if (params & kBatteryDataTrackVideo) {
1988 info.videoLastTime -= time;
1989 info.refCount ++;
1990 }
1991 } else {
1992 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001993 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001994 return;
1995 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001996 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08001997 mBatteryData.removeItem(uid);
1998 return;
1999 }
2000
2001 if (params & kBatteryDataTrackAudio) {
2002 info.audioLastTime += time;
2003 info.refCount --;
2004 }
2005 if (params & kBatteryDataTrackVideo) {
2006 info.videoLastTime += time;
2007 info.refCount --;
2008 }
2009
2010 // no stream is being played by this UID
2011 if (info.refCount == 0) {
2012 info.audioTotalTime += info.audioLastTime;
2013 info.audioLastTime = 0;
2014 info.videoTotalTime += info.videoLastTime;
2015 info.videoLastTime = 0;
2016 }
2017 }
2018}
2019
2020status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2021 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002022
2023 // audio output devices usage
2024 int32_t time = systemTime() / 1000000L; //in ms
2025 int32_t totalTime;
2026
2027 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2028 totalTime = mBatteryAudio.totalTime[i];
2029
2030 if (mBatteryAudio.deviceOn[i]
2031 && (mBatteryAudio.lastTime[i] != 0)) {
2032 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2033 totalTime += tmpTime;
2034 }
2035
2036 reply->writeInt32(totalTime);
2037 // reset the total time
2038 mBatteryAudio.totalTime[i] = 0;
2039 }
2040
2041 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002042 BatteryUsageInfo info;
2043 int size = mBatteryData.size();
2044
2045 reply->writeInt32(size);
2046 int i = 0;
2047
2048 while (i < size) {
2049 info = mBatteryData.valueAt(i);
2050
2051 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2052 reply->writeInt32(info.audioTotalTime);
2053 reply->writeInt32(info.videoTotalTime);
2054
2055 info.audioTotalTime = 0;
2056 info.videoTotalTime = 0;
2057
2058 // remove the UID entry where no stream is being played
2059 if (info.refCount <= 0) {
2060 mBatteryData.removeItemsAt(i);
2061 size --;
2062 i --;
2063 }
2064 i++;
2065 }
2066 return NO_ERROR;
2067}
nikoa64c8c72009-07-20 15:07:26 -07002068} // namespace android