blob: afde373e67eb6339dfcf2ab8aee16e8c004ec80b [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 Agopianb1e7cd12013-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 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"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080075#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070076#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070077#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070078#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070079
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070080namespace {
nikoa64c8c72009-07-20 15:07:26 -070081using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070082using android::status_t;
83using android::OK;
84using android::BAD_VALUE;
85using android::NOT_ENOUGH_DATA;
86using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070087
88// Max number of entries in the filter.
89const int kMaxFilterSize = 64; // I pulled that out of thin air.
90
nikoa64c8c72009-07-20 15:07:26 -070091// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070092
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070093
94// Unmarshall a filter from a Parcel.
95// Filter format in a parcel:
96//
97// 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
98// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99// | number of entries (n) |
100// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101// | metadata type 1 |
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103// | metadata type 2 |
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// ....
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// | metadata type n |
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109//
110// @param p Parcel that should start with a filter.
111// @param[out] filter On exit contains the list of metadata type to be
112// filtered.
113// @param[out] status On exit contains the status code to be returned.
114// @return true if the parcel starts with a valid filter.
115bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700116 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700117 status_t *status)
118{
Nicolas Catania48290382009-07-10 13:53:06 -0700119 int32_t val;
120 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700121 {
Steve Block29357bc2012-01-06 19:20:56 +0000122 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700123 *status = NOT_ENOUGH_DATA;
124 return false;
125 }
126
Nicolas Catania48290382009-07-10 13:53:06 -0700127 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 {
Steve Block29357bc2012-01-06 19:20:56 +0000129 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700130 *status = BAD_VALUE;
131 return false;
132 }
133
Nicolas Catania48290382009-07-10 13:53:06 -0700134 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135
136 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700137 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138
nikoa64c8c72009-07-20 15:07:26 -0700139 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140
Nicolas Catania48290382009-07-10 13:53:06 -0700141
142 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143 {
Steve Block29357bc2012-01-06 19:20:56 +0000144 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145 *status = NOT_ENOUGH_DATA;
146 return false;
147 }
148
nikoa64c8c72009-07-20 15:07:26 -0700149 const Metadata::Type *data =
150 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700151
Nicolas Catania48290382009-07-10 13:53:06 -0700152 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700153 {
Steve Block29357bc2012-01-06 19:20:56 +0000154 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700155 *status = BAD_VALUE;
156 return false;
157 }
158
159 // TODO: The stl impl of vector would be more efficient here
160 // because it degenerates into a memcpy on pod types. Try to
161 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700162 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700163 {
Nicolas Catania48290382009-07-10 13:53:06 -0700164 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700165 ++data;
166 }
167 *status = OK;
168 return true;
169}
170
Nicolas Catania48290382009-07-10 13:53:06 -0700171// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700172// @param val To be searched.
173// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700174bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700175{
176 // Deal with empty and ANY right away
177 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700178 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700179
Nicolas Catania48290382009-07-10 13:53:06 -0700180 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700181}
182
183} // anonymous namespace
184
185
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800186namespace android {
187
Dave Burked681bbb2011-08-30 14:39:17 +0100188static bool checkPermission(const char* permissionString) {
189#ifndef HAVE_ANDROID_OS
190 return true;
191#endif
192 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
193 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000194 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100195 return ok;
196}
197
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800198// 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 -0800199/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
200/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
201
202void MediaPlayerService::instantiate() {
203 defaultServiceManager()->addService(
204 String16("media.player"), new MediaPlayerService());
205}
206
207MediaPlayerService::MediaPlayerService()
208{
Steve Block3856b092011-10-20 11:56:00 +0100209 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800210 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800211
212 mBatteryAudio.refCount = 0;
213 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
214 mBatteryAudio.deviceOn[i] = 0;
215 mBatteryAudio.lastTime[i] = 0;
216 mBatteryAudio.totalTime[i] = 0;
217 }
218 // speaker is on by default
219 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700220
221 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800222}
223
224MediaPlayerService::~MediaPlayerService()
225{
Steve Block3856b092011-10-20 11:56:00 +0100226 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800227}
228
Glenn Kastenf37971f2012-02-03 11:06:53 -0800229sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800230{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800231 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700232 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
233 wp<MediaRecorderClient> w = recorder;
234 Mutex::Autolock lock(mLock);
235 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100236 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800237 return recorder;
238}
239
Gloria Wangdac6a312009-10-29 15:46:37 -0700240void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
241{
242 Mutex::Autolock lock(mLock);
243 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100244 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700245}
246
Glenn Kastenf37971f2012-02-03 11:06:53 -0800247sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800248{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800249 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800250 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100251 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800252 return retriever;
253}
254
Glenn Kastenf37971f2012-02-03 11:06:53 -0800255sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100256 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800258 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800259 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700260
261 sp<Client> c = new Client(
262 this, pid, connId, client, audioSessionId,
263 IPCThreadState::self()->getCallingUid());
264
Steve Block3856b092011-10-20 11:56:00 +0100265 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100266 IPCThreadState::self()->getCallingUid());
267
268 wp<Client> w = c;
269 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800270 Mutex::Autolock lock(mLock);
271 mClients.add(w);
272 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800273 return c;
274}
275
Andreas Huber318ad9c2009-10-15 13:46:54 -0700276sp<IOMX> MediaPlayerService::getOMX() {
277 Mutex::Autolock autoLock(mLock);
278
279 if (mOMX.get() == NULL) {
280 mOMX = new OMX;
281 }
282
283 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700284}
285
Andreas Hubered3e3e02012-03-26 11:13:27 -0700286sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700287 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700288}
289
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800290sp<IDrm> MediaPlayerService::makeDrm() {
291 return new Drm;
292}
293
Andreas Huber279dcd82013-01-30 10:41:25 -0800294sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
295 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700296}
297
Jeff Brown2013a542012-09-04 21:38:42 -0700298sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
299 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700300 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
301 return NULL;
302 }
303
Jeff Brownced24b32012-09-05 17:48:03 -0700304 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700305}
306
Andreas Huberb7319a72013-05-29 14:20:52 -0700307status_t MediaPlayerService::updateProxyConfig(
308 const char *host, int32_t port, const char *exclusionList) {
309 return HTTPBase::UpdateProxyConfig(host, port, exclusionList);
310}
311
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800312status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& args) const
313{
314 const size_t SIZE = 256;
315 char buffer[SIZE];
316 String8 result;
317
318 result.append(" AudioCache\n");
319 if (mHeap != 0) {
320 snprintf(buffer, 255, " heap base(%p), size(%d), flags(%d), device(%s)\n",
321 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags(), mHeap->getDevice());
322 result.append(buffer);
323 }
Scott Fan7d409692013-04-28 10:13:54 +0800324 snprintf(buffer, 255, " msec per frame(%f), channel count(%d), format(%d), frame count(%zd)\n",
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
326 result.append(buffer);
327 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
328 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
329 result.append(buffer);
330 ::write(fd, result.string(), result.size());
331 return NO_ERROR;
332}
333
334status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
335{
336 const size_t SIZE = 256;
337 char buffer[SIZE];
338 String8 result;
339
340 result.append(" AudioOutput\n");
341 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
342 mStreamType, mLeftVolume, mRightVolume);
343 result.append(buffer);
344 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800345 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800346 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700347 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
348 mAuxEffectId, mSendLevel);
349 result.append(buffer);
350
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800351 ::write(fd, result.string(), result.size());
352 if (mTrack != 0) {
353 mTrack->dump(fd, args);
354 }
355 return NO_ERROR;
356}
357
358status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
359{
360 const size_t SIZE = 256;
361 char buffer[SIZE];
362 String8 result;
363 result.append(" Client\n");
364 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
365 mPid, mConnId, mStatus, mLoop?"true": "false");
366 result.append(buffer);
367 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700368 if (mPlayer != NULL) {
369 mPlayer->dump(fd, args);
370 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800371 if (mAudioOutput != 0) {
372 mAudioOutput->dump(fd, args);
373 }
374 write(fd, "\n", 1);
375 return NO_ERROR;
376}
377
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
379{
380 const size_t SIZE = 256;
381 char buffer[SIZE];
382 String8 result;
383 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
384 snprintf(buffer, SIZE, "Permission Denial: "
385 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
386 IPCThreadState::self()->getCallingPid(),
387 IPCThreadState::self()->getCallingUid());
388 result.append(buffer);
389 } else {
390 Mutex::Autolock lock(mLock);
391 for (int i = 0, n = mClients.size(); i < n; ++i) {
392 sp<Client> c = mClients[i].promote();
393 if (c != 0) c->dump(fd, args);
394 }
James Dongb9141222010-07-08 11:16:11 -0700395 if (mMediaRecorderClients.size() == 0) {
396 result.append(" No media recorder client\n\n");
397 } else {
398 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
399 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700400 if (c != 0) {
401 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
402 result.append(buffer);
403 write(fd, result.string(), result.size());
404 result = "\n";
405 c->dump(fd, args);
406 }
James Dongb9141222010-07-08 11:16:11 -0700407 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700408 }
409
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800410 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700411 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800412 FILE *f = fopen(buffer, "r");
413 if (f) {
414 while (!feof(f)) {
415 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700416 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800417 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700418 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800419 strstr(buffer, " /system/media/")) {
420 result.append(" ");
421 result.append(buffer);
422 }
423 }
424 fclose(f);
425 } else {
426 result.append("couldn't open ");
427 result.append(buffer);
428 result.append("\n");
429 }
430
Glenn Kasten0512ab52011-05-04 17:58:57 -0700431 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800432 DIR *d = opendir(buffer);
433 if (d) {
434 struct dirent *ent;
435 while((ent = readdir(d)) != NULL) {
436 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700437 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 struct stat s;
439 if (lstat(buffer, &s) == 0) {
440 if ((s.st_mode & S_IFMT) == S_IFLNK) {
441 char linkto[256];
442 int len = readlink(buffer, linkto, sizeof(linkto));
443 if(len > 0) {
444 if(len > 255) {
445 linkto[252] = '.';
446 linkto[253] = '.';
447 linkto[254] = '.';
448 linkto[255] = 0;
449 } else {
450 linkto[len] = 0;
451 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700452 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800453 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700454 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 strstr(linkto, "/system/media/") == linkto) {
456 result.append(" ");
457 result.append(buffer);
458 result.append(" -> ");
459 result.append(linkto);
460 result.append("\n");
461 }
462 }
463 } else {
464 result.append(" unexpected type for ");
465 result.append(buffer);
466 result.append("\n");
467 }
468 }
469 }
470 }
471 closedir(d);
472 } else {
473 result.append("couldn't open ");
474 result.append(buffer);
475 result.append("\n");
476 }
477
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800478 bool dumpMem = false;
479 for (size_t i = 0; i < args.size(); i++) {
480 if (args[i] == String16("-m")) {
481 dumpMem = true;
482 }
483 }
484 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700485 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800487 }
488 write(fd, result.string(), result.size());
489 return NO_ERROR;
490}
491
492void MediaPlayerService::removeClient(wp<Client> client)
493{
494 Mutex::Autolock lock(mLock);
495 mClients.remove(client);
496}
497
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700498MediaPlayerService::Client::Client(
499 const sp<MediaPlayerService>& service, pid_t pid,
500 int32_t connId, const sp<IMediaPlayerClient>& client,
501 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800502{
Steve Block3856b092011-10-20 11:56:00 +0100503 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504 mPid = pid;
505 mConnId = connId;
506 mService = service;
507 mClient = client;
508 mLoop = false;
509 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700510 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700511 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800512 mRetransmitEndpointValid = false;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700513
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800514#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000515 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800516 mAntagonizer = new Antagonizer(notify, this);
517#endif
518}
519
520MediaPlayerService::Client::~Client()
521{
Steve Block3856b092011-10-20 11:56:00 +0100522 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800523 mAudioOutput.clear();
524 wp<Client> client(this);
525 disconnect();
526 mService->removeClient(client);
527}
528
529void MediaPlayerService::Client::disconnect()
530{
Steve Block3856b092011-10-20 11:56:00 +0100531 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800532 // grab local reference and clear main reference to prevent future
533 // access to object
534 sp<MediaPlayerBase> p;
535 {
536 Mutex::Autolock l(mLock);
537 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800538 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800539 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700540
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800541 mPlayer.clear();
542
543 // clear the notification to prevent callbacks to dead client
544 // and reset the player. We assume the player will serialize
545 // access to itself if necessary.
546 if (p != 0) {
547 p->setNotifyCallback(0, 0);
548#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000549 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 mAntagonizer->kill();
551#endif
552 p->reset();
553 }
554
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700555 disconnectNativeWindow();
556
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800557 IPCThreadState::self()->flushCommands();
558}
559
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
561{
562 // determine if we have the right player type
563 sp<MediaPlayerBase> p = mPlayer;
564 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100565 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 p.clear();
567 }
568 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700569 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800570 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700571
Jason Simmonsdb29e522011-08-12 13:46:55 -0700572 if (p != NULL) {
573 p->setUID(mUID);
574 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700575
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 return p;
577}
578
John Grossmanc795b642012-02-22 15:38:35 -0800579sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
580 player_type playerType)
581{
582 ALOGV("player type = %d", playerType);
583
584 // create the right type of player
585 sp<MediaPlayerBase> p = createPlayer(playerType);
586 if (p == NULL) {
587 return p;
588 }
589
590 if (!p->hardwareOutput()) {
591 mAudioOutput = new AudioOutput(mAudioSessionId);
592 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
593 }
594
595 return p;
596}
597
598void MediaPlayerService::Client::setDataSource_post(
599 const sp<MediaPlayerBase>& p,
600 status_t status)
601{
602 ALOGV(" setDataSource");
603 mStatus = status;
604 if (mStatus != OK) {
605 ALOGE(" error: %d", mStatus);
606 return;
607 }
608
609 // Set the re-transmission endpoint if one was chosen.
610 if (mRetransmitEndpointValid) {
611 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
612 if (mStatus != NO_ERROR) {
613 ALOGE("setRetransmitEndpoint error: %d", mStatus);
614 }
615 }
616
617 if (mStatus == OK) {
618 mPlayer = p;
619 }
620}
621
Andreas Huber2db84552010-01-28 11:19:57 -0800622status_t MediaPlayerService::Client::setDataSource(
623 const char *url, const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800624{
Steve Block3856b092011-10-20 11:56:00 +0100625 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626 if (url == NULL)
627 return UNKNOWN_ERROR;
628
Dave Burked681bbb2011-08-30 14:39:17 +0100629 if ((strncmp(url, "http://", 7) == 0) ||
630 (strncmp(url, "https://", 8) == 0) ||
631 (strncmp(url, "rtsp://", 7) == 0)) {
632 if (!checkPermission("android.permission.INTERNET")) {
633 return PERMISSION_DENIED;
634 }
635 }
636
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637 if (strncmp(url, "content://", 10) == 0) {
638 // get a filedescriptor for the content Uri and
639 // pass it to the setDataSource(fd) method
640
641 String16 url16(url);
642 int fd = android::openContentProviderFile(url16);
643 if (fd < 0)
644 {
Steve Block29357bc2012-01-06 19:20:56 +0000645 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646 return UNKNOWN_ERROR;
647 }
648 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
649 close(fd);
650 return mStatus;
651 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700652 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800653 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
654 if (p == NULL) {
655 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800656 }
657
John Grossmanc795b642012-02-22 15:38:35 -0800658 setDataSource_post(p, p->setDataSource(url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800659 return mStatus;
660 }
661}
662
663status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
664{
Steve Block3856b092011-10-20 11:56:00 +0100665 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666 struct stat sb;
667 int ret = fstat(fd, &sb);
668 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000669 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670 return UNKNOWN_ERROR;
671 }
672
Steve Block3856b092011-10-20 11:56:00 +0100673 ALOGV("st_dev = %llu", sb.st_dev);
674 ALOGV("st_mode = %u", sb.st_mode);
675 ALOGV("st_uid = %lu", sb.st_uid);
676 ALOGV("st_gid = %lu", sb.st_gid);
677 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678
679 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000680 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800681 ::close(fd);
682 return UNKNOWN_ERROR;
683 }
684 if (offset + length > sb.st_size) {
685 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100686 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800687 }
688
John Grossman44a7e422012-06-21 17:29:24 -0700689 player_type playerType = MediaPlayerFactory::getPlayerType(this,
690 fd,
691 offset,
692 length);
John Grossmanc795b642012-02-22 15:38:35 -0800693 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
694 if (p == NULL) {
695 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800696 }
697
698 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800699 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700 return mStatus;
701}
702
Andreas Hubere2b10282010-11-23 11:41:34 -0800703status_t MediaPlayerService::Client::setDataSource(
704 const sp<IStreamSource> &source) {
705 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700706 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800707 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800708 if (p == NULL) {
709 return NO_INIT;
710 }
711
Andreas Hubere2b10282010-11-23 11:41:34 -0800712 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800713 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800714 return mStatus;
715}
716
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700717void MediaPlayerService::Client::disconnectNativeWindow() {
718 if (mConnectedWindow != NULL) {
719 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
720 NATIVE_WINDOW_API_MEDIA);
721
722 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000723 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700724 strerror(-err), err);
725 }
726 }
727 mConnectedWindow.clear();
728}
729
Glenn Kasten11731182011-02-08 17:26:17 -0800730status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800731 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800732{
Andy McFadden484566c2012-12-18 09:46:54 -0800733 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800734 sp<MediaPlayerBase> p = getPlayer();
735 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700736
Andy McFadden484566c2012-12-18 09:46:54 -0800737 sp<IBinder> binder(bufferProducer == NULL ? NULL :
738 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700739 if (mConnectedWindowBinder == binder) {
740 return OK;
741 }
742
743 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800744 if (bufferProducer != NULL) {
Mathias Agopianb1e7cd12013-02-14 17:11:27 -0800745 anw = new Surface(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700746 status_t err = native_window_api_connect(anw.get(),
747 NATIVE_WINDOW_API_MEDIA);
748
749 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000750 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700751 // Note that we must do the reset before disconnecting from the ANW.
752 // Otherwise queue/dequeue calls could be made on the disconnected
753 // ANW, which may result in errors.
754 reset();
755
756 disconnectNativeWindow();
757
758 return err;
759 }
760 }
761
Andy McFadden484566c2012-12-18 09:46:54 -0800762 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700763 // disconnecting the old one. Otherwise queue/dequeue calls could be made
764 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800765 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700766
767 disconnectNativeWindow();
768
769 mConnectedWindow = anw;
770
771 if (err == OK) {
772 mConnectedWindowBinder = binder;
773 } else {
774 disconnectNativeWindow();
775 }
776
777 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800778}
779
Nicolas Catania1d187f12009-05-12 23:25:55 -0700780status_t MediaPlayerService::Client::invoke(const Parcel& request,
781 Parcel *reply)
782{
783 sp<MediaPlayerBase> p = getPlayer();
784 if (p == NULL) return UNKNOWN_ERROR;
785 return p->invoke(request, reply);
786}
787
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700788// This call doesn't need to access the native player.
789status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
790{
791 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700792 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700793
Nicolas Catania48290382009-07-10 13:53:06 -0700794 if (unmarshallFilter(filter, &allow, &status) &&
795 unmarshallFilter(filter, &drop, &status)) {
796 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700797
798 mMetadataAllow = allow;
799 mMetadataDrop = drop;
800 }
801 return status;
802}
803
Nicolas Catania48290382009-07-10 13:53:06 -0700804status_t MediaPlayerService::Client::getMetadata(
805 bool update_only, bool apply_filter, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700806{
nikoa64c8c72009-07-20 15:07:26 -0700807 sp<MediaPlayerBase> player = getPlayer();
808 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700809
nikod608a812009-07-16 16:39:53 -0700810 status_t status;
811 // Placeholder for the return code, updated by the caller.
812 reply->writeInt32(-1);
813
nikoa64c8c72009-07-20 15:07:26 -0700814 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700815
816 // We don't block notifications while we fetch the data. We clear
817 // mMetadataUpdated first so we don't lose notifications happening
818 // during the rest of this call.
819 {
820 Mutex::Autolock lock(mLock);
821 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700822 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700823 }
824 mMetadataUpdated.clear();
825 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700826
nikoa64c8c72009-07-20 15:07:26 -0700827 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700828
nikoa64c8c72009-07-20 15:07:26 -0700829 metadata.appendHeader();
830 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700831
832 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700833 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000834 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700835 return status;
836 }
837
838 // FIXME: Implement filtering on the result. Not critical since
839 // filtering takes place on the update notifications already. This
840 // would be when all the metadata are fetch and a filter is set.
841
nikod608a812009-07-16 16:39:53 -0700842 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700843 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700844 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700845}
846
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800847status_t MediaPlayerService::Client::prepareAsync()
848{
Steve Block3856b092011-10-20 11:56:00 +0100849 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800850 sp<MediaPlayerBase> p = getPlayer();
851 if (p == 0) return UNKNOWN_ERROR;
852 status_t ret = p->prepareAsync();
853#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000854 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855 if (ret == NO_ERROR) mAntagonizer->start();
856#endif
857 return ret;
858}
859
860status_t MediaPlayerService::Client::start()
861{
Steve Block3856b092011-10-20 11:56:00 +0100862 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800863 sp<MediaPlayerBase> p = getPlayer();
864 if (p == 0) return UNKNOWN_ERROR;
865 p->setLooping(mLoop);
866 return p->start();
867}
868
869status_t MediaPlayerService::Client::stop()
870{
Steve Block3856b092011-10-20 11:56:00 +0100871 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800872 sp<MediaPlayerBase> p = getPlayer();
873 if (p == 0) return UNKNOWN_ERROR;
874 return p->stop();
875}
876
877status_t MediaPlayerService::Client::pause()
878{
Steve Block3856b092011-10-20 11:56:00 +0100879 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800880 sp<MediaPlayerBase> p = getPlayer();
881 if (p == 0) return UNKNOWN_ERROR;
882 return p->pause();
883}
884
885status_t MediaPlayerService::Client::isPlaying(bool* state)
886{
887 *state = false;
888 sp<MediaPlayerBase> p = getPlayer();
889 if (p == 0) return UNKNOWN_ERROR;
890 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100891 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800892 return NO_ERROR;
893}
894
895status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
896{
Steve Block3856b092011-10-20 11:56:00 +0100897 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800898 sp<MediaPlayerBase> p = getPlayer();
899 if (p == 0) return UNKNOWN_ERROR;
900 status_t ret = p->getCurrentPosition(msec);
901 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100902 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800903 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000904 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800905 }
906 return ret;
907}
908
909status_t MediaPlayerService::Client::getDuration(int *msec)
910{
Steve Block3856b092011-10-20 11:56:00 +0100911 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800912 sp<MediaPlayerBase> p = getPlayer();
913 if (p == 0) return UNKNOWN_ERROR;
914 status_t ret = p->getDuration(msec);
915 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100916 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800917 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000918 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800919 }
920 return ret;
921}
922
Marco Nelissen6b74d672012-02-28 16:07:44 -0800923status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
924 ALOGV("setNextPlayer");
925 Mutex::Autolock l(mLock);
926 sp<Client> c = static_cast<Client*>(player.get());
927 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -0700928
929 if (c != NULL) {
930 if (mAudioOutput != NULL) {
931 mAudioOutput->setNextOutput(c->mAudioOutput);
932 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
933 ALOGE("no current audio output");
934 }
935
936 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
937 mPlayer->setNextPlayer(mNextClient->getPlayer());
938 }
Marco Nelissen6b74d672012-02-28 16:07:44 -0800939 }
John Grossman5f7e55e2012-08-24 14:47:25 -0700940
Marco Nelissen6b74d672012-02-28 16:07:44 -0800941 return OK;
942}
943
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800944status_t MediaPlayerService::Client::seekTo(int msec)
945{
Steve Block3856b092011-10-20 11:56:00 +0100946 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800947 sp<MediaPlayerBase> p = getPlayer();
948 if (p == 0) return UNKNOWN_ERROR;
949 return p->seekTo(msec);
950}
951
952status_t MediaPlayerService::Client::reset()
953{
Steve Block3856b092011-10-20 11:56:00 +0100954 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -0800955 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800956 sp<MediaPlayerBase> p = getPlayer();
957 if (p == 0) return UNKNOWN_ERROR;
958 return p->reset();
959}
960
Glenn Kastenfff6d712012-01-12 16:38:12 -0800961status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800962{
Steve Block3856b092011-10-20 11:56:00 +0100963 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800964 // TODO: for hardware output, call player instead
965 Mutex::Autolock l(mLock);
966 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
967 return NO_ERROR;
968}
969
970status_t MediaPlayerService::Client::setLooping(int loop)
971{
Steve Block3856b092011-10-20 11:56:00 +0100972 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800973 mLoop = loop;
974 sp<MediaPlayerBase> p = getPlayer();
975 if (p != 0) return p->setLooping(loop);
976 return NO_ERROR;
977}
978
979status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
980{
Steve Block3856b092011-10-20 11:56:00 +0100981 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -0800982
983 // for hardware output, call player instead
984 sp<MediaPlayerBase> p = getPlayer();
985 {
986 Mutex::Autolock l(mLock);
987 if (p != 0 && p->hardwareOutput()) {
988 MediaPlayerHWInterface* hwp =
989 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
990 return hwp->setVolume(leftVolume, rightVolume);
991 } else {
992 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
993 return NO_ERROR;
994 }
995 }
996
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800997 return NO_ERROR;
998}
999
Eric Laurent2beeb502010-07-16 07:43:46 -07001000status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1001{
Steve Block3856b092011-10-20 11:56:00 +01001002 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001003 Mutex::Autolock l(mLock);
1004 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1005 return NO_ERROR;
1006}
1007
1008status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1009{
Steve Block3856b092011-10-20 11:56:00 +01001010 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001011 Mutex::Autolock l(mLock);
1012 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1013 return NO_ERROR;
1014}
Nicolas Catania48290382009-07-10 13:53:06 -07001015
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001016status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001017 ALOGV("[%d] setParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001018 sp<MediaPlayerBase> p = getPlayer();
1019 if (p == 0) return UNKNOWN_ERROR;
1020 return p->setParameter(key, request);
1021}
1022
1023status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001024 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001025 sp<MediaPlayerBase> p = getPlayer();
1026 if (p == 0) return UNKNOWN_ERROR;
1027 return p->getParameter(key, reply);
1028}
1029
John Grossmanc795b642012-02-22 15:38:35 -08001030status_t MediaPlayerService::Client::setRetransmitEndpoint(
1031 const struct sockaddr_in* endpoint) {
1032
1033 if (NULL != endpoint) {
1034 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1035 uint16_t p = ntohs(endpoint->sin_port);
1036 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1037 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1038 } else {
1039 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1040 }
1041
1042 sp<MediaPlayerBase> p = getPlayer();
1043
1044 // Right now, the only valid time to set a retransmit endpoint is before
1045 // player selection has been made (since the presence or absence of a
1046 // retransmit endpoint is going to determine which player is selected during
1047 // setDataSource).
1048 if (p != 0) return INVALID_OPERATION;
1049
1050 if (NULL != endpoint) {
1051 mRetransmitEndpoint = *endpoint;
1052 mRetransmitEndpointValid = true;
1053 } else {
1054 mRetransmitEndpointValid = false;
1055 }
1056
1057 return NO_ERROR;
1058}
1059
John Grossman44a7e422012-06-21 17:29:24 -07001060status_t MediaPlayerService::Client::getRetransmitEndpoint(
1061 struct sockaddr_in* endpoint)
1062{
1063 if (NULL == endpoint)
1064 return BAD_VALUE;
1065
1066 sp<MediaPlayerBase> p = getPlayer();
1067
1068 if (p != NULL)
1069 return p->getRetransmitEndpoint(endpoint);
1070
1071 if (!mRetransmitEndpointValid)
1072 return NO_INIT;
1073
1074 *endpoint = mRetransmitEndpoint;
1075
1076 return NO_ERROR;
1077}
1078
Gloria Wangb483c472011-04-11 17:23:27 -07001079void MediaPlayerService::Client::notify(
1080 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001081{
1082 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001083 if (client == NULL) {
1084 return;
1085 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001086
James Dongb8a98252012-08-26 16:13:03 -07001087 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001088 {
1089 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001090 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001091 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001092 if (client->mAudioOutput != NULL)
1093 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001094 client->mNextClient->start();
1095 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1096 }
1097 }
1098
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001099 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001100 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001101 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001102
1103 if(client->shouldDropMetadata(metadata_type)) {
1104 return;
1105 }
1106
1107 // Update the list of metadata that have changed. getMetadata
1108 // also access mMetadataUpdated and clears it.
1109 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001110 }
James Dongb8a98252012-08-26 16:13:03 -07001111
1112 if (c != NULL) {
1113 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1114 c->notify(msg, ext1, ext2, obj);
1115 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116}
1117
Nicolas Catania48290382009-07-10 13:53:06 -07001118
nikoa64c8c72009-07-20 15:07:26 -07001119bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001120{
Nicolas Catania48290382009-07-10 13:53:06 -07001121 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001122
Nicolas Catania48290382009-07-10 13:53:06 -07001123 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001124 return true;
1125 }
1126
Nicolas Catania48290382009-07-10 13:53:06 -07001127 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001128 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001129 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001130 return true;
1131 }
1132}
1133
Nicolas Catania48290382009-07-10 13:53:06 -07001134
nikoa64c8c72009-07-20 15:07:26 -07001135void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001136 Mutex::Autolock lock(mLock);
1137 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1138 mMetadataUpdated.add(metadata_type);
1139 }
1140}
1141
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001142#if CALLBACK_ANTAGONIZER
1143const int Antagonizer::interval = 10000; // 10 msecs
1144
1145Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1146 mExit(false), mActive(false), mClient(client), mCb(cb)
1147{
1148 createThread(callbackThread, this);
1149}
1150
1151void Antagonizer::kill()
1152{
1153 Mutex::Autolock _l(mLock);
1154 mActive = false;
1155 mExit = true;
1156 mCondition.wait(mLock);
1157}
1158
1159int Antagonizer::callbackThread(void* user)
1160{
Steve Blockb8a80522011-12-20 16:23:08 +00001161 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001162 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1163 while (!p->mExit) {
1164 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001165 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001166 p->mCb(p->mClient, 0, 0, 0);
1167 }
1168 usleep(interval);
1169 }
1170 Mutex::Autolock _l(p->mLock);
1171 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001172 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001173 return 0;
1174}
1175#endif
1176
1177static size_t kDefaultHeapSize = 1024 * 1024; // 1MB
1178
Glenn Kastene1c39622012-01-04 09:36:37 -08001179sp<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 -08001180{
Steve Block3856b092011-10-20 11:56:00 +01001181 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001182 sp<MemoryBase> mem;
1183 sp<MediaPlayerBase> player;
1184
1185 // Protect our precious, precious DRMd ringtones by only allowing
1186 // decoding of http, but not filesystem paths or content Uris.
1187 // If the application wants to decode those, it should open a
1188 // filedescriptor for them and use that.
1189 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001190 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001191 return mem;
1192 }
1193
John Grossman44a7e422012-06-21 17:29:24 -07001194 player_type playerType =
1195 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001196 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001197
1198 // create the right type of player
1199 sp<AudioCache> cache = new AudioCache(url);
John Grossman44a7e422012-06-21 17:29:24 -07001200 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001201 if (player == NULL) goto Exit;
1202 if (player->hardwareOutput()) goto Exit;
1203
1204 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1205
1206 // set data source
1207 if (player->setDataSource(url) != NO_ERROR) goto Exit;
1208
Steve Block3856b092011-10-20 11:56:00 +01001209 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001210 player->prepareAsync();
1211
Steve Block3856b092011-10-20 11:56:00 +01001212 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001213 if (cache->wait() != NO_ERROR) goto Exit;
1214
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001216 player->start();
1217
Steve Block3856b092011-10-20 11:56:00 +01001218 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001219 cache->wait();
1220 // in case of error, return what was successfully decoded.
1221 if (cache->size() == 0) {
1222 goto Exit;
1223 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001224
1225 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1226 *pSampleRate = cache->sampleRate();
1227 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001228 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001229 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 -08001230
1231Exit:
1232 if (player != 0) player->reset();
1233 return mem;
1234}
1235
Glenn Kastene1c39622012-01-04 09:36:37 -08001236sp<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 -08001237{
Steve Block3856b092011-10-20 11:56:00 +01001238 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001239 sp<MemoryBase> mem;
1240 sp<MediaPlayerBase> player;
1241
John Grossman44a7e422012-06-21 17:29:24 -07001242 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1243 fd,
1244 offset,
1245 length);
Steve Block3856b092011-10-20 11:56:00 +01001246 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001247
1248 // create the right type of player
1249 sp<AudioCache> cache = new AudioCache("decode_fd");
John Grossman44a7e422012-06-21 17:29:24 -07001250 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001251 if (player == NULL) goto Exit;
1252 if (player->hardwareOutput()) goto Exit;
1253
1254 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1255
1256 // set data source
1257 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1258
Steve Block3856b092011-10-20 11:56:00 +01001259 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 player->prepareAsync();
1261
Steve Block3856b092011-10-20 11:56:00 +01001262 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001263 if (cache->wait() != NO_ERROR) goto Exit;
1264
Steve Block3856b092011-10-20 11:56:00 +01001265 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001266 player->start();
1267
Steve Block3856b092011-10-20 11:56:00 +01001268 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001269 cache->wait();
1270 // in case of error, return what was successfully decoded.
1271 if (cache->size() == 0) {
1272 goto Exit;
1273 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001274
1275 mem = new MemoryBase(cache->getHeap(), 0, cache->size());
1276 *pSampleRate = cache->sampleRate();
1277 *pNumChannels = cache->channelCount();
1278 *pFormat = cache->format();
Steve Block3856b092011-10-20 11:56:00 +01001279 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 -08001280
1281Exit:
1282 if (player != 0) player->reset();
1283 ::close(fd);
1284 return mem;
1285}
1286
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001287
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001288#undef LOG_TAG
1289#define LOG_TAG "AudioSink"
Eric Laurenta514bdb2010-06-21 09:27:30 -07001290MediaPlayerService::AudioOutput::AudioOutput(int sessionId)
Andreas Huber20111aa2009-07-14 16:56:47 -07001291 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001292 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001293 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001294 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001295 mSessionId(sessionId),
1296 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001297 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001298 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001299 mLeftVolume = 1.0;
1300 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001301 mPlaybackRatePermille = 1000;
1302 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001303 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001304 mAuxEffectId = 0;
1305 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001306 setMinBufferCount();
1307}
1308
1309MediaPlayerService::AudioOutput::~AudioOutput()
1310{
1311 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001312 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001313}
1314
1315void MediaPlayerService::AudioOutput::setMinBufferCount()
1316{
1317 char value[PROPERTY_VALUE_MAX];
1318 if (property_get("ro.kernel.qemu", value, 0)) {
1319 mIsOnEmulator = true;
1320 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1321 }
1322}
1323
1324bool MediaPlayerService::AudioOutput::isOnEmulator()
1325{
1326 setMinBufferCount();
1327 return mIsOnEmulator;
1328}
1329
1330int MediaPlayerService::AudioOutput::getMinBufferCount()
1331{
1332 setMinBufferCount();
1333 return mMinBufferCount;
1334}
1335
1336ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1337{
1338 if (mTrack == 0) return NO_INIT;
1339 return mTrack->frameCount() * frameSize();
1340}
1341
1342ssize_t MediaPlayerService::AudioOutput::frameCount() const
1343{
1344 if (mTrack == 0) return NO_INIT;
1345 return mTrack->frameCount();
1346}
1347
1348ssize_t MediaPlayerService::AudioOutput::channelCount() const
1349{
1350 if (mTrack == 0) return NO_INIT;
1351 return mTrack->channelCount();
1352}
1353
1354ssize_t MediaPlayerService::AudioOutput::frameSize() const
1355{
1356 if (mTrack == 0) return NO_INIT;
1357 return mTrack->frameSize();
1358}
1359
1360uint32_t MediaPlayerService::AudioOutput::latency () const
1361{
Eric Laurentdb354e52012-03-05 17:27:11 -08001362 if (mTrack == 0) return 0;
1363 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001364}
1365
1366float MediaPlayerService::AudioOutput::msecsPerFrame() const
1367{
1368 return mMsecsPerFrame;
1369}
1370
Marco Nelissen4110c102012-03-29 09:31:28 -07001371status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001372{
1373 if (mTrack == 0) return NO_INIT;
1374 return mTrack->getPosition(position);
1375}
1376
Marco Nelissen4110c102012-03-29 09:31:28 -07001377status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1378{
1379 if (mTrack == 0) return NO_INIT;
1380 *frameswritten = mBytesWritten / frameSize();
1381 return OK;
1382}
1383
Andreas Huber20111aa2009-07-14 16:56:47 -07001384status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001385 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1386 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001387 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001388 audio_output_flags_t flags,
1389 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390{
Andreas Huber20111aa2009-07-14 16:56:47 -07001391 mCallback = cb;
1392 mCallbackCookie = cookie;
1393
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001394 // Check argument "bufferCount" against the mininum buffer count
1395 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001396 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001397 bufferCount = mMinBufferCount;
1398
1399 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001400 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1401 format, bufferCount, mSessionId);
Glenn Kasten1127d652012-11-14 08:44:39 -08001402 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001403 size_t afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001404 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405
1406 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1407 return NO_INIT;
1408 }
1409 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1410 return NO_INIT;
1411 }
1412
1413 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001414
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001415 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001416 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001417 if (0 == channelMask) {
1418 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1419 return NO_INIT;
1420 }
1421 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001422
Glenn Kasten2799d742013-05-30 14:33:29 -07001423 sp<AudioTrack> t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001424 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001425 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001426 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001427 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001428 mStreamType,
1429 sampleRate,
1430 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001431 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001432 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001433 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001434 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001435 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001436 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001437 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001438 } else {
1439 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001440 mStreamType,
1441 sampleRate,
1442 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001443 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001444 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001445 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001446 NULL,
1447 NULL,
1448 0,
1449 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001450 }
1451
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001452 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001453 ALOGE("Unable to create audio track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001454 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001455 return NO_INIT;
1456 }
1457
Marco Nelissen67295b52012-06-11 14:52:53 -07001458
Glenn Kasten2799d742013-05-30 14:33:29 -07001459 if (mRecycledTrack != 0) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001460 // check if the existing track can be reused as-is, or if a new track needs to be created.
1461
1462 bool reuse = true;
1463 if ((mCallbackData == NULL && mCallback != NULL) ||
1464 (mCallbackData != NULL && mCallback == NULL)) {
1465 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1466 ALOGV("can't chain callback and write");
1467 reuse = false;
1468 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1469 (mRecycledTrack->channelCount() != channelCount) ||
1470 (mRecycledTrack->frameCount() != t->frameCount())) {
1471 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1472 mRecycledTrack->getSampleRate(), sampleRate,
1473 mRecycledTrack->channelCount(), channelCount,
1474 mRecycledTrack->frameCount(), t->frameCount());
1475 reuse = false;
1476 } else if (flags != mFlags) {
1477 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1478 reuse = false;
1479 }
1480 if (reuse) {
1481 ALOGV("chaining to next output");
1482 close();
1483 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001484 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001485 if (mCallbackData != NULL) {
1486 mCallbackData->setOutput(this);
1487 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001488 delete newcbd;
1489 return OK;
1490 }
1491
1492 // if we're not going to reuse the track, unblock and flush it
1493 if (mCallbackData != NULL) {
1494 mCallbackData->setOutput(NULL);
1495 mCallbackData->endTrackSwitch();
1496 }
1497 mRecycledTrack->flush();
Glenn Kasten2799d742013-05-30 14:33:29 -07001498 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001499 delete mCallbackData;
1500 mCallbackData = NULL;
1501 close();
1502 }
1503
1504 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001505 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001506 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001507
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001508 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001509 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001510 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001511 uint32_t pos;
1512 if (t->getPosition(&pos) == OK) {
1513 mBytesWritten = uint64_t(pos) * t->frameSize();
1514 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001515 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001516
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001517 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1518 if (res != NO_ERROR) {
1519 return res;
1520 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001521 t->setAuxEffectSendLevel(mSendLevel);
1522 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001523}
1524
1525void MediaPlayerService::AudioOutput::start()
1526{
Steve Block3856b092011-10-20 11:56:00 +01001527 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001528 if (mCallbackData != NULL) {
1529 mCallbackData->endTrackSwitch();
1530 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001531 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001532 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001533 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001534 mTrack->start();
1535 }
1536}
1537
Marco Nelissen6b74d672012-02-28 16:07:44 -08001538void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1539 mNextOutput = nextOutput;
1540}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001541
1542
Marco Nelissen6b74d672012-02-28 16:07:44 -08001543void MediaPlayerService::AudioOutput::switchToNextOutput() {
1544 ALOGV("switchToNextOutput");
1545 if (mNextOutput != NULL) {
1546 if (mCallbackData != NULL) {
1547 mCallbackData->beginTrackSwitch();
1548 }
1549 delete mNextOutput->mCallbackData;
1550 mNextOutput->mCallbackData = mCallbackData;
1551 mCallbackData = NULL;
1552 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001553 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001554 mNextOutput->mSampleRateHz = mSampleRateHz;
1555 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001556 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001557 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001558 }
1559}
1560
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001561ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1562{
Andreas Huber20111aa2009-07-14 16:56:47 -07001563 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1564
Steve Block3856b092011-10-20 11:56:00 +01001565 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001566 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001567 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001568 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001569 return ret;
1570 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001571 return NO_INIT;
1572}
1573
1574void MediaPlayerService::AudioOutput::stop()
1575{
Steve Block3856b092011-10-20 11:56:00 +01001576 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001577 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001578}
1579
1580void MediaPlayerService::AudioOutput::flush()
1581{
Steve Block3856b092011-10-20 11:56:00 +01001582 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001583 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001584}
1585
1586void MediaPlayerService::AudioOutput::pause()
1587{
Steve Block3856b092011-10-20 11:56:00 +01001588 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001589 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001590}
1591
1592void MediaPlayerService::AudioOutput::close()
1593{
Steve Block3856b092011-10-20 11:56:00 +01001594 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001595 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001596}
1597
1598void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1599{
Steve Block3856b092011-10-20 11:56:00 +01001600 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001601 mLeftVolume = left;
1602 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001603 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001604 mTrack->setVolume(left, right);
1605 }
1606}
1607
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001608status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1609{
1610 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1611 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001612 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001613 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1614 } else {
1615 res = NO_INIT;
1616 }
1617 mPlaybackRatePermille = ratePermille;
1618 if (mSampleRateHz != 0) {
1619 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1620 }
1621 return res;
1622}
1623
Eric Laurent2beeb502010-07-16 07:43:46 -07001624status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1625{
Steve Block3856b092011-10-20 11:56:00 +01001626 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001627 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001628 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001629 return mTrack->setAuxEffectSendLevel(level);
1630 }
1631 return NO_ERROR;
1632}
1633
1634status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1635{
Steve Block3856b092011-10-20 11:56:00 +01001636 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001637 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001638 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001639 return mTrack->attachAuxEffect(effectId);
1640 }
1641 return NO_ERROR;
1642}
1643
Andreas Huber20111aa2009-07-14 16:56:47 -07001644// static
1645void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001646 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001647 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001648 if (event != AudioTrack::EVENT_MORE_DATA) {
1649 return;
1650 }
1651
Marco Nelissen6b74d672012-02-28 16:07:44 -08001652 CallbackData *data = (CallbackData*)cookie;
1653 data->lock();
1654 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001655 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001656 if (me == NULL) {
1657 // no output set, likely because the track was scheduled to be reused
1658 // by another player, but the format turned out to be incompatible.
1659 data->unlock();
1660 buffer->size = 0;
1661 return;
1662 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001663
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001664 size_t actualSize = (*me->mCallback)(
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001665 me, buffer->raw, buffer->size, me->mCallbackCookie,
1666 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001667
Marco Nelissen6b74d672012-02-28 16:07:44 -08001668 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001669 // We've reached EOS but the audio track is not stopped yet,
1670 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001671
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001672 memset(buffer->raw, 0, buffer->size);
1673 actualSize = buffer->size;
1674 }
1675
1676 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001677 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001678}
1679
Marco Nelissen4110c102012-03-29 09:31:28 -07001680int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001681{
1682 return mSessionId;
1683}
1684
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001685#undef LOG_TAG
1686#define LOG_TAG "AudioCache"
1687MediaPlayerService::AudioCache::AudioCache(const char* name) :
1688 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1689 mError(NO_ERROR), mCommandComplete(false)
1690{
1691 // create ashmem heap
1692 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1693}
1694
1695uint32_t MediaPlayerService::AudioCache::latency () const
1696{
1697 return 0;
1698}
1699
1700float MediaPlayerService::AudioCache::msecsPerFrame() const
1701{
1702 return mMsecsPerFrame;
1703}
1704
Marco Nelissen4110c102012-03-29 09:31:28 -07001705status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001706{
1707 if (position == 0) return BAD_VALUE;
1708 *position = mSize;
1709 return NO_ERROR;
1710}
1711
Marco Nelissen4110c102012-03-29 09:31:28 -07001712status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1713{
1714 if (written == 0) return BAD_VALUE;
1715 *written = mSize;
1716 return NO_ERROR;
1717}
1718
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001719////////////////////////////////////////////////////////////////////////////////
1720
1721struct CallbackThread : public Thread {
1722 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1723 MediaPlayerBase::AudioSink::AudioCallback cb,
1724 void *cookie);
1725
1726protected:
1727 virtual ~CallbackThread();
1728
1729 virtual bool threadLoop();
1730
1731private:
1732 wp<MediaPlayerBase::AudioSink> mSink;
1733 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1734 void *mCookie;
1735 void *mBuffer;
1736 size_t mBufferSize;
1737
1738 CallbackThread(const CallbackThread &);
1739 CallbackThread &operator=(const CallbackThread &);
1740};
1741
1742CallbackThread::CallbackThread(
1743 const wp<MediaPlayerBase::AudioSink> &sink,
1744 MediaPlayerBase::AudioSink::AudioCallback cb,
1745 void *cookie)
1746 : mSink(sink),
1747 mCallback(cb),
1748 mCookie(cookie),
1749 mBuffer(NULL),
1750 mBufferSize(0) {
1751}
1752
1753CallbackThread::~CallbackThread() {
1754 if (mBuffer) {
1755 free(mBuffer);
1756 mBuffer = NULL;
1757 }
1758}
1759
1760bool CallbackThread::threadLoop() {
1761 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1762 if (sink == NULL) {
1763 return false;
1764 }
1765
1766 if (mBuffer == NULL) {
1767 mBufferSize = sink->bufferSize();
1768 mBuffer = malloc(mBufferSize);
1769 }
1770
1771 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001772 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
1773 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001774
1775 if (actualSize > 0) {
1776 sink->write(mBuffer, actualSize);
1777 }
1778
1779 return true;
1780}
1781
1782////////////////////////////////////////////////////////////////////////////////
1783
Andreas Huber20111aa2009-07-14 16:56:47 -07001784status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001785 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1786 audio_format_t format, int bufferCount,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001787 AudioCallback cb, void *cookie, audio_output_flags_t flags,
1788 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001789{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001790 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001791 if (mHeap->getHeapID() < 0) {
1792 return NO_INIT;
1793 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001794
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001795 mSampleRate = sampleRate;
1796 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001797 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001798 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001799
1800 if (cb != NULL) {
1801 mCallbackThread = new CallbackThread(this, cb, cookie);
1802 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001803 return NO_ERROR;
1804}
1805
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001806void MediaPlayerService::AudioCache::start() {
1807 if (mCallbackThread != NULL) {
1808 mCallbackThread->run("AudioCache callback");
1809 }
1810}
1811
1812void MediaPlayerService::AudioCache::stop() {
1813 if (mCallbackThread != NULL) {
1814 mCallbackThread->requestExitAndWait();
1815 }
1816}
1817
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001818ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1819{
Steve Block3856b092011-10-20 11:56:00 +01001820 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001821 if ((buffer == 0) || (size == 0)) return size;
1822
1823 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1824 if (p == NULL) return NO_INIT;
1825 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001826 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001827 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001828 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829 size = mHeap->getSize() - mSize;
1830 }
1831 memcpy(p, buffer, size);
1832 mSize += size;
1833 return size;
1834}
1835
1836// call with lock held
1837status_t MediaPlayerService::AudioCache::wait()
1838{
1839 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001840 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001841 mSignal.wait(mLock);
1842 }
1843 mCommandComplete = false;
1844
1845 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001846 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001847 } else {
Steve Block3856b092011-10-20 11:56:00 +01001848 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001849 }
1850 return mError;
1851}
1852
Gloria Wangb483c472011-04-11 17:23:27 -07001853void MediaPlayerService::AudioCache::notify(
1854 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001855{
Steve Block3856b092011-10-20 11:56:00 +01001856 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001857 AudioCache* p = static_cast<AudioCache*>(cookie);
1858
1859 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001860 switch (msg)
1861 {
1862 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001863 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001864 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001865 break;
1866 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001867 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001868 break;
1869 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001871 break;
1872 default:
Steve Block3856b092011-10-20 11:56:00 +01001873 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001874 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001875 }
1876
1877 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001878 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001879 p->mCommandComplete = true;
1880 p->mSignal.signal();
1881}
1882
Marco Nelissen4110c102012-03-29 09:31:28 -07001883int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001884{
1885 return 0;
1886}
1887
Gloria Wang7cf180c2011-02-19 18:37:57 -08001888void MediaPlayerService::addBatteryData(uint32_t params)
1889{
1890 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001891
1892 int32_t time = systemTime() / 1000000L;
1893
1894 // change audio output devices. This notification comes from AudioFlinger
1895 if ((params & kBatteryDataSpeakerOn)
1896 || (params & kBatteryDataOtherAudioDeviceOn)) {
1897
1898 int deviceOn[NUM_AUDIO_DEVICES];
1899 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1900 deviceOn[i] = 0;
1901 }
1902
1903 if ((params & kBatteryDataSpeakerOn)
1904 && (params & kBatteryDataOtherAudioDeviceOn)) {
1905 deviceOn[SPEAKER_AND_OTHER] = 1;
1906 } else if (params & kBatteryDataSpeakerOn) {
1907 deviceOn[SPEAKER] = 1;
1908 } else {
1909 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1910 }
1911
1912 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1913 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1914
1915 if (mBatteryAudio.refCount > 0) { // if playing audio
1916 if (!deviceOn[i]) {
1917 mBatteryAudio.lastTime[i] += time;
1918 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1919 mBatteryAudio.lastTime[i] = 0;
1920 } else {
1921 mBatteryAudio.lastTime[i] = 0 - time;
1922 }
1923 }
1924
1925 mBatteryAudio.deviceOn[i] = deviceOn[i];
1926 }
1927 }
1928 return;
1929 }
1930
1931 // an sudio stream is started
1932 if (params & kBatteryDataAudioFlingerStart) {
1933 // record the start time only if currently no other audio
1934 // is being played
1935 if (mBatteryAudio.refCount == 0) {
1936 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1937 if (mBatteryAudio.deviceOn[i]) {
1938 mBatteryAudio.lastTime[i] -= time;
1939 }
1940 }
1941 }
1942
1943 mBatteryAudio.refCount ++;
1944 return;
1945
1946 } else if (params & kBatteryDataAudioFlingerStop) {
1947 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001948 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001949 return;
1950 }
1951
1952 // record the stop time only if currently this is the only
1953 // audio being played
1954 if (mBatteryAudio.refCount == 1) {
1955 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1956 if (mBatteryAudio.deviceOn[i]) {
1957 mBatteryAudio.lastTime[i] += time;
1958 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1959 mBatteryAudio.lastTime[i] = 0;
1960 }
1961 }
1962 }
1963
1964 mBatteryAudio.refCount --;
1965 return;
1966 }
1967
Gloria Wang7cf180c2011-02-19 18:37:57 -08001968 int uid = IPCThreadState::self()->getCallingUid();
1969 if (uid == AID_MEDIA) {
1970 return;
1971 }
1972 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001973
1974 if (index < 0) { // create a new entry for this UID
1975 BatteryUsageInfo info;
1976 info.audioTotalTime = 0;
1977 info.videoTotalTime = 0;
1978 info.audioLastTime = 0;
1979 info.videoLastTime = 0;
1980 info.refCount = 0;
1981
Gloria Wang9ee159b2011-02-24 14:51:45 -08001982 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001983 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001984 return;
1985 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001986 }
1987
1988 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1989
1990 if (params & kBatteryDataCodecStarted) {
1991 if (params & kBatteryDataTrackAudio) {
1992 info.audioLastTime -= time;
1993 info.refCount ++;
1994 }
1995 if (params & kBatteryDataTrackVideo) {
1996 info.videoLastTime -= time;
1997 info.refCount ++;
1998 }
1999 } else {
2000 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002001 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002002 return;
2003 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002004 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002005 mBatteryData.removeItem(uid);
2006 return;
2007 }
2008
2009 if (params & kBatteryDataTrackAudio) {
2010 info.audioLastTime += time;
2011 info.refCount --;
2012 }
2013 if (params & kBatteryDataTrackVideo) {
2014 info.videoLastTime += time;
2015 info.refCount --;
2016 }
2017
2018 // no stream is being played by this UID
2019 if (info.refCount == 0) {
2020 info.audioTotalTime += info.audioLastTime;
2021 info.audioLastTime = 0;
2022 info.videoTotalTime += info.videoLastTime;
2023 info.videoLastTime = 0;
2024 }
2025 }
2026}
2027
2028status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2029 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002030
2031 // audio output devices usage
2032 int32_t time = systemTime() / 1000000L; //in ms
2033 int32_t totalTime;
2034
2035 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2036 totalTime = mBatteryAudio.totalTime[i];
2037
2038 if (mBatteryAudio.deviceOn[i]
2039 && (mBatteryAudio.lastTime[i] != 0)) {
2040 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2041 totalTime += tmpTime;
2042 }
2043
2044 reply->writeInt32(totalTime);
2045 // reset the total time
2046 mBatteryAudio.totalTime[i] = 0;
2047 }
2048
2049 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002050 BatteryUsageInfo info;
2051 int size = mBatteryData.size();
2052
2053 reply->writeInt32(size);
2054 int i = 0;
2055
2056 while (i < size) {
2057 info = mBatteryData.valueAt(i);
2058
2059 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2060 reply->writeInt32(info.audioTotalTime);
2061 reply->writeInt32(info.videoTotalTime);
2062
2063 info.audioTotalTime = 0;
2064 info.videoTotalTime = 0;
2065
2066 // remove the UID entry where no stream is being played
2067 if (info.refCount <= 0) {
2068 mBatteryData.removeItemsAt(i);
2069 size --;
2070 i --;
2071 }
2072 i++;
2073 }
2074 return NO_ERROR;
2075}
nikoa64c8c72009-07-20 15:07:26 -07002076} // namespace android