blob: 57ec7ea93256a7dc0cd218fa3784fbcf9b5cb8f3 [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);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001298 mTrack = 0;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001299 mRecycledTrack = 0;
Dima Zavinfce7a472011-04-19 22:30:36 -07001300 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001301 mLeftVolume = 1.0;
1302 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001303 mPlaybackRatePermille = 1000;
1304 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001306 mAuxEffectId = 0;
1307 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001308 setMinBufferCount();
1309}
1310
1311MediaPlayerService::AudioOutput::~AudioOutput()
1312{
1313 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001314 delete mRecycledTrack;
1315 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316}
1317
1318void MediaPlayerService::AudioOutput::setMinBufferCount()
1319{
1320 char value[PROPERTY_VALUE_MAX];
1321 if (property_get("ro.kernel.qemu", value, 0)) {
1322 mIsOnEmulator = true;
1323 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1324 }
1325}
1326
1327bool MediaPlayerService::AudioOutput::isOnEmulator()
1328{
1329 setMinBufferCount();
1330 return mIsOnEmulator;
1331}
1332
1333int MediaPlayerService::AudioOutput::getMinBufferCount()
1334{
1335 setMinBufferCount();
1336 return mMinBufferCount;
1337}
1338
1339ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1340{
1341 if (mTrack == 0) return NO_INIT;
1342 return mTrack->frameCount() * frameSize();
1343}
1344
1345ssize_t MediaPlayerService::AudioOutput::frameCount() const
1346{
1347 if (mTrack == 0) return NO_INIT;
1348 return mTrack->frameCount();
1349}
1350
1351ssize_t MediaPlayerService::AudioOutput::channelCount() const
1352{
1353 if (mTrack == 0) return NO_INIT;
1354 return mTrack->channelCount();
1355}
1356
1357ssize_t MediaPlayerService::AudioOutput::frameSize() const
1358{
1359 if (mTrack == 0) return NO_INIT;
1360 return mTrack->frameSize();
1361}
1362
1363uint32_t MediaPlayerService::AudioOutput::latency () const
1364{
Eric Laurentdb354e52012-03-05 17:27:11 -08001365 if (mTrack == 0) return 0;
1366 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001367}
1368
1369float MediaPlayerService::AudioOutput::msecsPerFrame() const
1370{
1371 return mMsecsPerFrame;
1372}
1373
Marco Nelissen4110c102012-03-29 09:31:28 -07001374status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001375{
1376 if (mTrack == 0) return NO_INIT;
1377 return mTrack->getPosition(position);
1378}
1379
Marco Nelissen4110c102012-03-29 09:31:28 -07001380status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1381{
1382 if (mTrack == 0) return NO_INIT;
1383 *frameswritten = mBytesWritten / frameSize();
1384 return OK;
1385}
1386
Andreas Huber20111aa2009-07-14 16:56:47 -07001387status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001388 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1389 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001390 AudioCallback cb, void *cookie,
1391 audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001392{
Andreas Huber20111aa2009-07-14 16:56:47 -07001393 mCallback = cb;
1394 mCallbackCookie = cookie;
1395
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001396 // Check argument "bufferCount" against the mininum buffer count
1397 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001398 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001399 bufferCount = mMinBufferCount;
1400
1401 }
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001402 ALOGV("open(%u, %d, 0x%x, %d, %d, %d)", sampleRate, channelCount, channelMask,
1403 format, bufferCount, mSessionId);
Glenn Kasten1127d652012-11-14 08:44:39 -08001404 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001405 size_t afFrameCount;
Eric Laurent1948eb32012-04-13 16:50:19 -07001406 uint32_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001407
1408 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1409 return NO_INIT;
1410 }
1411 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1412 return NO_INIT;
1413 }
1414
1415 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
Andreas Huber20111aa2009-07-14 16:56:47 -07001416
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001417 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001418 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001419 if (0 == channelMask) {
1420 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1421 return NO_INIT;
1422 }
1423 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001424
Andreas Huber20111aa2009-07-14 16:56:47 -07001425 AudioTrack *t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001426 CallbackData *newcbd = NULL;
Andreas Huber20111aa2009-07-14 16:56:47 -07001427 if (mCallback != NULL) {
Marco Nelissen67295b52012-06-11 14:52:53 -07001428 newcbd = new CallbackData(this);
Andreas Huber20111aa2009-07-14 16:56:47 -07001429 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001430 mStreamType,
1431 sampleRate,
1432 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001433 channelMask,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001434 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001435 flags,
Eric Laurentc2f1f072009-07-17 12:17:14 -07001436 CallbackWrapper,
Marco Nelissen67295b52012-06-11 14:52:53 -07001437 newcbd,
Glenn Kasten17a736c2012-02-14 08:52:15 -08001438 0, // notification frames
Eric Laurenta514bdb2010-06-21 09:27:30 -07001439 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001440 } else {
1441 t = new AudioTrack(
Eric Laurentc2f1f072009-07-17 12:17:14 -07001442 mStreamType,
1443 sampleRate,
1444 format,
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001445 channelMask,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001446 frameCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001447 flags,
Eric Laurenta514bdb2010-06-21 09:27:30 -07001448 NULL,
1449 NULL,
1450 0,
1451 mSessionId);
Andreas Huber20111aa2009-07-14 16:56:47 -07001452 }
1453
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001454 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
Steve Block29357bc2012-01-06 19:20:56 +00001455 ALOGE("Unable to create audio track");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456 delete t;
Marco Nelissen67295b52012-06-11 14:52:53 -07001457 delete newcbd;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001458 return NO_INIT;
1459 }
1460
Marco Nelissen67295b52012-06-11 14:52:53 -07001461
1462 if (mRecycledTrack) {
1463 // check if the existing track can be reused as-is, or if a new track needs to be created.
1464
1465 bool reuse = true;
1466 if ((mCallbackData == NULL && mCallback != NULL) ||
1467 (mCallbackData != NULL && mCallback == NULL)) {
1468 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1469 ALOGV("can't chain callback and write");
1470 reuse = false;
1471 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1472 (mRecycledTrack->channelCount() != channelCount) ||
1473 (mRecycledTrack->frameCount() != t->frameCount())) {
1474 ALOGV("samplerate, channelcount or framecount differ: %d/%d Hz, %d/%d ch, %d/%d frames",
1475 mRecycledTrack->getSampleRate(), sampleRate,
1476 mRecycledTrack->channelCount(), channelCount,
1477 mRecycledTrack->frameCount(), t->frameCount());
1478 reuse = false;
1479 } else if (flags != mFlags) {
1480 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1481 reuse = false;
1482 }
1483 if (reuse) {
1484 ALOGV("chaining to next output");
1485 close();
1486 mTrack = mRecycledTrack;
1487 mRecycledTrack = NULL;
1488 if (mCallbackData != NULL) {
1489 mCallbackData->setOutput(this);
1490 }
1491 delete t;
1492 delete newcbd;
1493 return OK;
1494 }
1495
1496 // if we're not going to reuse the track, unblock and flush it
1497 if (mCallbackData != NULL) {
1498 mCallbackData->setOutput(NULL);
1499 mCallbackData->endTrackSwitch();
1500 }
1501 mRecycledTrack->flush();
1502 delete mRecycledTrack;
1503 mRecycledTrack = NULL;
1504 delete mCallbackData;
1505 mCallbackData = NULL;
1506 close();
1507 }
1508
1509 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001510 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001511 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001512
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001513 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001514 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001515 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001516 uint32_t pos;
1517 if (t->getPosition(&pos) == OK) {
1518 mBytesWritten = uint64_t(pos) * t->frameSize();
1519 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001520 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001521
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001522 status_t res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1523 if (res != NO_ERROR) {
1524 return res;
1525 }
Eric Laurent2beeb502010-07-16 07:43:46 -07001526 t->setAuxEffectSendLevel(mSendLevel);
1527 return t->attachAuxEffect(mAuxEffectId);;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528}
1529
1530void MediaPlayerService::AudioOutput::start()
1531{
Steve Block3856b092011-10-20 11:56:00 +01001532 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001533 if (mCallbackData != NULL) {
1534 mCallbackData->endTrackSwitch();
1535 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001536 if (mTrack) {
1537 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001538 mTrack->setAuxEffectSendLevel(mSendLevel);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001539 mTrack->start();
1540 }
1541}
1542
Marco Nelissen6b74d672012-02-28 16:07:44 -08001543void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1544 mNextOutput = nextOutput;
1545}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001546
1547
Marco Nelissen6b74d672012-02-28 16:07:44 -08001548void MediaPlayerService::AudioOutput::switchToNextOutput() {
1549 ALOGV("switchToNextOutput");
1550 if (mNextOutput != NULL) {
1551 if (mCallbackData != NULL) {
1552 mCallbackData->beginTrackSwitch();
1553 }
1554 delete mNextOutput->mCallbackData;
1555 mNextOutput->mCallbackData = mCallbackData;
1556 mCallbackData = NULL;
1557 mNextOutput->mRecycledTrack = mTrack;
1558 mTrack = NULL;
1559 mNextOutput->mSampleRateHz = mSampleRateHz;
1560 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001561 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001562 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001563 }
1564}
1565
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001566ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1567{
Andreas Huber20111aa2009-07-14 16:56:47 -07001568 LOG_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
1569
Steve Block3856b092011-10-20 11:56:00 +01001570 //ALOGV("write(%p, %u)", buffer, size);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001571 if (mTrack) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001572 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001573 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001574 return ret;
1575 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001576 return NO_INIT;
1577}
1578
1579void MediaPlayerService::AudioOutput::stop()
1580{
Steve Block3856b092011-10-20 11:56:00 +01001581 ALOGV("stop");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001582 if (mTrack) mTrack->stop();
1583}
1584
1585void MediaPlayerService::AudioOutput::flush()
1586{
Steve Block3856b092011-10-20 11:56:00 +01001587 ALOGV("flush");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001588 if (mTrack) mTrack->flush();
1589}
1590
1591void MediaPlayerService::AudioOutput::pause()
1592{
Steve Block3856b092011-10-20 11:56:00 +01001593 ALOGV("pause");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001594 if (mTrack) mTrack->pause();
1595}
1596
1597void MediaPlayerService::AudioOutput::close()
1598{
Steve Block3856b092011-10-20 11:56:00 +01001599 ALOGV("close");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001600 delete mTrack;
1601 mTrack = 0;
1602}
1603
1604void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1605{
Steve Block3856b092011-10-20 11:56:00 +01001606 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001607 mLeftVolume = left;
1608 mRightVolume = right;
1609 if (mTrack) {
1610 mTrack->setVolume(left, right);
1611 }
1612}
1613
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001614status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1615{
1616 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1617 status_t res = NO_ERROR;
1618 if (mTrack) {
1619 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1620 } else {
1621 res = NO_INIT;
1622 }
1623 mPlaybackRatePermille = ratePermille;
1624 if (mSampleRateHz != 0) {
1625 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1626 }
1627 return res;
1628}
1629
Eric Laurent2beeb502010-07-16 07:43:46 -07001630status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1631{
Steve Block3856b092011-10-20 11:56:00 +01001632 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001633 mSendLevel = level;
1634 if (mTrack) {
1635 return mTrack->setAuxEffectSendLevel(level);
1636 }
1637 return NO_ERROR;
1638}
1639
1640status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1641{
Steve Block3856b092011-10-20 11:56:00 +01001642 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001643 mAuxEffectId = effectId;
1644 if (mTrack) {
1645 return mTrack->attachAuxEffect(effectId);
1646 }
1647 return NO_ERROR;
1648}
1649
Andreas Huber20111aa2009-07-14 16:56:47 -07001650// static
1651void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001652 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001653 //ALOGV("callbackwrapper");
Andreas Huber20111aa2009-07-14 16:56:47 -07001654 if (event != AudioTrack::EVENT_MORE_DATA) {
1655 return;
1656 }
1657
Marco Nelissen6b74d672012-02-28 16:07:44 -08001658 CallbackData *data = (CallbackData*)cookie;
1659 data->lock();
1660 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001661 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001662 if (me == NULL) {
1663 // no output set, likely because the track was scheduled to be reused
1664 // by another player, but the format turned out to be incompatible.
1665 data->unlock();
1666 buffer->size = 0;
1667 return;
1668 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001669
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001670 size_t actualSize = (*me->mCallback)(
Andreas Huber20111aa2009-07-14 16:56:47 -07001671 me, buffer->raw, buffer->size, me->mCallbackCookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001672
Marco Nelissen6b74d672012-02-28 16:07:44 -08001673 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001674 // We've reached EOS but the audio track is not stopped yet,
1675 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001676
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001677 memset(buffer->raw, 0, buffer->size);
1678 actualSize = buffer->size;
1679 }
1680
1681 buffer->size = actualSize;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001682 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001683}
1684
Marco Nelissen4110c102012-03-29 09:31:28 -07001685int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001686{
1687 return mSessionId;
1688}
1689
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001690#undef LOG_TAG
1691#define LOG_TAG "AudioCache"
1692MediaPlayerService::AudioCache::AudioCache(const char* name) :
1693 mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1694 mError(NO_ERROR), mCommandComplete(false)
1695{
1696 // create ashmem heap
1697 mHeap = new MemoryHeapBase(kDefaultHeapSize, 0, name);
1698}
1699
1700uint32_t MediaPlayerService::AudioCache::latency () const
1701{
1702 return 0;
1703}
1704
1705float MediaPlayerService::AudioCache::msecsPerFrame() const
1706{
1707 return mMsecsPerFrame;
1708}
1709
Marco Nelissen4110c102012-03-29 09:31:28 -07001710status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001711{
1712 if (position == 0) return BAD_VALUE;
1713 *position = mSize;
1714 return NO_ERROR;
1715}
1716
Marco Nelissen4110c102012-03-29 09:31:28 -07001717status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1718{
1719 if (written == 0) return BAD_VALUE;
1720 *written = mSize;
1721 return NO_ERROR;
1722}
1723
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001724////////////////////////////////////////////////////////////////////////////////
1725
1726struct CallbackThread : public Thread {
1727 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1728 MediaPlayerBase::AudioSink::AudioCallback cb,
1729 void *cookie);
1730
1731protected:
1732 virtual ~CallbackThread();
1733
1734 virtual bool threadLoop();
1735
1736private:
1737 wp<MediaPlayerBase::AudioSink> mSink;
1738 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1739 void *mCookie;
1740 void *mBuffer;
1741 size_t mBufferSize;
1742
1743 CallbackThread(const CallbackThread &);
1744 CallbackThread &operator=(const CallbackThread &);
1745};
1746
1747CallbackThread::CallbackThread(
1748 const wp<MediaPlayerBase::AudioSink> &sink,
1749 MediaPlayerBase::AudioSink::AudioCallback cb,
1750 void *cookie)
1751 : mSink(sink),
1752 mCallback(cb),
1753 mCookie(cookie),
1754 mBuffer(NULL),
1755 mBufferSize(0) {
1756}
1757
1758CallbackThread::~CallbackThread() {
1759 if (mBuffer) {
1760 free(mBuffer);
1761 mBuffer = NULL;
1762 }
1763}
1764
1765bool CallbackThread::threadLoop() {
1766 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
1767 if (sink == NULL) {
1768 return false;
1769 }
1770
1771 if (mBuffer == NULL) {
1772 mBufferSize = sink->bufferSize();
1773 mBuffer = malloc(mBufferSize);
1774 }
1775
1776 size_t actualSize =
1777 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie);
1778
1779 if (actualSize > 0) {
1780 sink->write(mBuffer, actualSize);
1781 }
1782
1783 return true;
1784}
1785
1786////////////////////////////////////////////////////////////////////////////////
1787
Andreas Huber20111aa2009-07-14 16:56:47 -07001788status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001789 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1790 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001791 AudioCallback cb, void *cookie, audio_output_flags_t flags)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001792{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001793 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08001794 if (mHeap->getHeapID() < 0) {
1795 return NO_INIT;
1796 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001797
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001798 mSampleRate = sampleRate;
1799 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08001800 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001801 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001802
1803 if (cb != NULL) {
1804 mCallbackThread = new CallbackThread(this, cb, cookie);
1805 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806 return NO_ERROR;
1807}
1808
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001809void MediaPlayerService::AudioCache::start() {
1810 if (mCallbackThread != NULL) {
1811 mCallbackThread->run("AudioCache callback");
1812 }
1813}
1814
1815void MediaPlayerService::AudioCache::stop() {
1816 if (mCallbackThread != NULL) {
1817 mCallbackThread->requestExitAndWait();
1818 }
1819}
1820
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001821ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
1822{
Steve Block3856b092011-10-20 11:56:00 +01001823 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824 if ((buffer == 0) || (size == 0)) return size;
1825
1826 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
1827 if (p == NULL) return NO_INIT;
1828 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01001829 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001830 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00001831 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001832 size = mHeap->getSize() - mSize;
1833 }
1834 memcpy(p, buffer, size);
1835 mSize += size;
1836 return size;
1837}
1838
1839// call with lock held
1840status_t MediaPlayerService::AudioCache::wait()
1841{
1842 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08001843 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001844 mSignal.wait(mLock);
1845 }
1846 mCommandComplete = false;
1847
1848 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001849 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001850 } else {
Steve Block3856b092011-10-20 11:56:00 +01001851 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001852 }
1853 return mError;
1854}
1855
Gloria Wangb483c472011-04-11 17:23:27 -07001856void MediaPlayerService::AudioCache::notify(
1857 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001858{
Steve Block3856b092011-10-20 11:56:00 +01001859 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001860 AudioCache* p = static_cast<AudioCache*>(cookie);
1861
1862 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08001863 switch (msg)
1864 {
1865 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00001866 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001867 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08001868 break;
1869 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01001870 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08001871 break;
1872 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01001873 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08001874 break;
1875 default:
Steve Block3856b092011-10-20 11:56:00 +01001876 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08001877 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001878 }
1879
1880 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08001881 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001882 p->mCommandComplete = true;
1883 p->mSignal.signal();
1884}
1885
Marco Nelissen4110c102012-03-29 09:31:28 -07001886int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001887{
1888 return 0;
1889}
1890
Gloria Wang7cf180c2011-02-19 18:37:57 -08001891void MediaPlayerService::addBatteryData(uint32_t params)
1892{
1893 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08001894
1895 int32_t time = systemTime() / 1000000L;
1896
1897 // change audio output devices. This notification comes from AudioFlinger
1898 if ((params & kBatteryDataSpeakerOn)
1899 || (params & kBatteryDataOtherAudioDeviceOn)) {
1900
1901 int deviceOn[NUM_AUDIO_DEVICES];
1902 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1903 deviceOn[i] = 0;
1904 }
1905
1906 if ((params & kBatteryDataSpeakerOn)
1907 && (params & kBatteryDataOtherAudioDeviceOn)) {
1908 deviceOn[SPEAKER_AND_OTHER] = 1;
1909 } else if (params & kBatteryDataSpeakerOn) {
1910 deviceOn[SPEAKER] = 1;
1911 } else {
1912 deviceOn[OTHER_AUDIO_DEVICE] = 1;
1913 }
1914
1915 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1916 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
1917
1918 if (mBatteryAudio.refCount > 0) { // if playing audio
1919 if (!deviceOn[i]) {
1920 mBatteryAudio.lastTime[i] += time;
1921 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1922 mBatteryAudio.lastTime[i] = 0;
1923 } else {
1924 mBatteryAudio.lastTime[i] = 0 - time;
1925 }
1926 }
1927
1928 mBatteryAudio.deviceOn[i] = deviceOn[i];
1929 }
1930 }
1931 return;
1932 }
1933
1934 // an sudio stream is started
1935 if (params & kBatteryDataAudioFlingerStart) {
1936 // record the start time only if currently no other audio
1937 // is being played
1938 if (mBatteryAudio.refCount == 0) {
1939 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1940 if (mBatteryAudio.deviceOn[i]) {
1941 mBatteryAudio.lastTime[i] -= time;
1942 }
1943 }
1944 }
1945
1946 mBatteryAudio.refCount ++;
1947 return;
1948
1949 } else if (params & kBatteryDataAudioFlingerStop) {
1950 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00001951 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001952 return;
1953 }
1954
1955 // record the stop time only if currently this is the only
1956 // audio being played
1957 if (mBatteryAudio.refCount == 1) {
1958 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
1959 if (mBatteryAudio.deviceOn[i]) {
1960 mBatteryAudio.lastTime[i] += time;
1961 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
1962 mBatteryAudio.lastTime[i] = 0;
1963 }
1964 }
1965 }
1966
1967 mBatteryAudio.refCount --;
1968 return;
1969 }
1970
Gloria Wang7cf180c2011-02-19 18:37:57 -08001971 int uid = IPCThreadState::self()->getCallingUid();
1972 if (uid == AID_MEDIA) {
1973 return;
1974 }
1975 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08001976
1977 if (index < 0) { // create a new entry for this UID
1978 BatteryUsageInfo info;
1979 info.audioTotalTime = 0;
1980 info.videoTotalTime = 0;
1981 info.audioLastTime = 0;
1982 info.videoLastTime = 0;
1983 info.refCount = 0;
1984
Gloria Wang9ee159b2011-02-24 14:51:45 -08001985 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00001986 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08001987 return;
1988 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08001989 }
1990
1991 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
1992
1993 if (params & kBatteryDataCodecStarted) {
1994 if (params & kBatteryDataTrackAudio) {
1995 info.audioLastTime -= time;
1996 info.refCount ++;
1997 }
1998 if (params & kBatteryDataTrackVideo) {
1999 info.videoLastTime -= time;
2000 info.refCount ++;
2001 }
2002 } else {
2003 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002004 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002005 return;
2006 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002007 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002008 mBatteryData.removeItem(uid);
2009 return;
2010 }
2011
2012 if (params & kBatteryDataTrackAudio) {
2013 info.audioLastTime += time;
2014 info.refCount --;
2015 }
2016 if (params & kBatteryDataTrackVideo) {
2017 info.videoLastTime += time;
2018 info.refCount --;
2019 }
2020
2021 // no stream is being played by this UID
2022 if (info.refCount == 0) {
2023 info.audioTotalTime += info.audioLastTime;
2024 info.audioLastTime = 0;
2025 info.videoTotalTime += info.videoLastTime;
2026 info.videoLastTime = 0;
2027 }
2028 }
2029}
2030
2031status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2032 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002033
2034 // audio output devices usage
2035 int32_t time = systemTime() / 1000000L; //in ms
2036 int32_t totalTime;
2037
2038 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2039 totalTime = mBatteryAudio.totalTime[i];
2040
2041 if (mBatteryAudio.deviceOn[i]
2042 && (mBatteryAudio.lastTime[i] != 0)) {
2043 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2044 totalTime += tmpTime;
2045 }
2046
2047 reply->writeInt32(totalTime);
2048 // reset the total time
2049 mBatteryAudio.totalTime[i] = 0;
2050 }
2051
2052 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002053 BatteryUsageInfo info;
2054 int size = mBatteryData.size();
2055
2056 reply->writeInt32(size);
2057 int i = 0;
2058
2059 while (i < size) {
2060 info = mBatteryData.valueAt(i);
2061
2062 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2063 reply->writeInt32(info.audioTotalTime);
2064 reply->writeInt32(info.videoTotalTime);
2065
2066 info.audioTotalTime = 0;
2067 info.videoTotalTime = 0;
2068
2069 // remove the UID entry where no stream is being played
2070 if (info.refCount <= 0) {
2071 mBatteryData.removeItemsAt(i);
2072 size --;
2073 i --;
2074 }
2075 i++;
2076 }
2077 return NO_ERROR;
2078}
nikoa64c8c72009-07-20 15:07:26 -07002079} // namespace android