blob: 2c48306ffcae1f06ad54fe318a0b9f8ab74910d0 [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
Chong Zhang0b30fd42014-07-23 14:46:05 -070037#include <binder/IBatteryStats.h>
Mathias Agopian75624082009-05-19 19:08:10 -070038#include <binder/IPCThreadState.h>
39#include <binder/IServiceManager.h>
40#include <binder/MemoryHeapBase.h>
41#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080042#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070043#include <utils/Errors.h> // for status_t
44#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070045#include <utils/SystemClock.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070046#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047
Andreas Huber1b86fe02014-01-29 11:13:26 -080048#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070049#include <media/IRemoteDisplay.h>
50#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080051#include <media/MediaPlayerInterface.h>
52#include <media/mediarecorder.h>
53#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070054#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080055#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070056#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070057#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070058#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010059#include <media/stagefright/AudioPlayer.h>
60#include <media/stagefright/foundation/ADebug.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080061
Dima Zavin64760242011-05-11 14:15:23 -070062#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070063
Gloria Wang7cf180c2011-02-19 18:37:57 -080064#include <private/android_filesystem_config.h>
65
James Dong559bf282012-03-28 10:29:14 -070066#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080067#include "MediaRecorderClient.h"
68#include "MediaPlayerService.h"
69#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070070#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071
72#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070073#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070074#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080075#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070076
Andreas Huber20111aa2009-07-14 16:56:47 -070077#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070078
Andreas Hubered3e3e02012-03-26 11:13:27 -070079#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080080#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070081#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070082#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070083#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070084
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070085namespace {
nikoa64c8c72009-07-20 15:07:26 -070086using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070087using android::status_t;
88using android::OK;
89using android::BAD_VALUE;
90using android::NOT_ENOUGH_DATA;
91using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070092
93// Max number of entries in the filter.
94const int kMaxFilterSize = 64; // I pulled that out of thin air.
95
nikoa64c8c72009-07-20 15:07:26 -070096// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070097
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070098
99// Unmarshall a filter from a Parcel.
100// Filter format in a parcel:
101//
102// 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
103// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
104// | number of entries (n) |
105// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106// | metadata type 1 |
107// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108// | metadata type 2 |
109// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110// ....
111// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112// | metadata type n |
113// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114//
115// @param p Parcel that should start with a filter.
116// @param[out] filter On exit contains the list of metadata type to be
117// filtered.
118// @param[out] status On exit contains the status code to be returned.
119// @return true if the parcel starts with a valid filter.
120bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700121 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700122 status_t *status)
123{
Nicolas Catania48290382009-07-10 13:53:06 -0700124 int32_t val;
125 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700126 {
Steve Block29357bc2012-01-06 19:20:56 +0000127 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700128 *status = NOT_ENOUGH_DATA;
129 return false;
130 }
131
Nicolas Catania48290382009-07-10 13:53:06 -0700132 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133 {
Steve Block29357bc2012-01-06 19:20:56 +0000134 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700135 *status = BAD_VALUE;
136 return false;
137 }
138
Nicolas Catania48290382009-07-10 13:53:06 -0700139 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140
141 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700142 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700143
nikoa64c8c72009-07-20 15:07:26 -0700144 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145
Nicolas Catania48290382009-07-10 13:53:06 -0700146
147 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148 {
Steve Block29357bc2012-01-06 19:20:56 +0000149 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150 *status = NOT_ENOUGH_DATA;
151 return false;
152 }
153
nikoa64c8c72009-07-20 15:07:26 -0700154 const Metadata::Type *data =
155 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700156
Nicolas Catania48290382009-07-10 13:53:06 -0700157 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700158 {
Steve Block29357bc2012-01-06 19:20:56 +0000159 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 *status = BAD_VALUE;
161 return false;
162 }
163
164 // TODO: The stl impl of vector would be more efficient here
165 // because it degenerates into a memcpy on pod types. Try to
166 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700167 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700168 {
Nicolas Catania48290382009-07-10 13:53:06 -0700169 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170 ++data;
171 }
172 *status = OK;
173 return true;
174}
175
Nicolas Catania48290382009-07-10 13:53:06 -0700176// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700177// @param val To be searched.
178// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700179bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700180{
181 // Deal with empty and ANY right away
182 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700183 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700184
Nicolas Catania48290382009-07-10 13:53:06 -0700185 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700186}
187
188} // anonymous namespace
189
190
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700191namespace {
192using android::Parcel;
193using android::String16;
194
195// marshalling tag indicating flattened utf16 tags
196// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
197const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
198
199// Audio attributes format in a parcel:
200//
201// 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
202// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
203// | usage |
204// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205// | content_type |
206// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
207// | flags |
208// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
209// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
210// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
211// | flattened tags in UTF16 |
212// | ... |
213// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
214//
215// @param p Parcel that contains audio attributes.
216// @param[out] attributes On exit points to an initialized audio_attributes_t structure
217// @param[out] status On exit contains the status code to be returned.
218void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
219{
220 attributes->usage = (audio_usage_t) parcel.readInt32();
221 attributes->content_type = (audio_content_type_t) parcel.readInt32();
222 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
223 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
224 if (hasFlattenedTag) {
225 // the tags are UTF16, convert to UTF8
226 String16 tags = parcel.readString16();
227 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
228 if (realTagSize <= 0) {
229 strcpy(attributes->tags, "");
230 } else {
231 // copy the flattened string into the attributes as the destination for the conversion:
232 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
233 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
234 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
235 utf16_to_utf8(tags.string(), tagSize, attributes->tags);
236 }
237 } else {
238 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
239 strcpy(attributes->tags, "");
240 }
241}
242} // anonymous namespace
243
244
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800245namespace android {
246
Dave Burked681bbb2011-08-30 14:39:17 +0100247static bool checkPermission(const char* permissionString) {
248#ifndef HAVE_ANDROID_OS
249 return true;
250#endif
251 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
252 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000253 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100254 return ok;
255}
256
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800257// 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 -0800258/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
259/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
260
261void MediaPlayerService::instantiate() {
262 defaultServiceManager()->addService(
263 String16("media.player"), new MediaPlayerService());
264}
265
266MediaPlayerService::MediaPlayerService()
267{
Steve Block3856b092011-10-20 11:56:00 +0100268 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800269 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800270
271 mBatteryAudio.refCount = 0;
272 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
273 mBatteryAudio.deviceOn[i] = 0;
274 mBatteryAudio.lastTime[i] = 0;
275 mBatteryAudio.totalTime[i] = 0;
276 }
277 // speaker is on by default
278 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700279
Chong Zhang0b30fd42014-07-23 14:46:05 -0700280 // reset battery stats
281 // if the mediaserver has crashed, battery stats could be left
282 // in bad state, reset the state upon service start.
283 const sp<IServiceManager> sm(defaultServiceManager());
284 if (sm != NULL) {
285 const String16 name("batterystats");
286 sp<IBatteryStats> batteryStats =
287 interface_cast<IBatteryStats>(sm->getService(name));
288 if (batteryStats != NULL) {
289 batteryStats->noteResetVideo();
290 batteryStats->noteResetAudio();
291 }
292 }
293
John Grossman44a7e422012-06-21 17:29:24 -0700294 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295}
296
297MediaPlayerService::~MediaPlayerService()
298{
Steve Block3856b092011-10-20 11:56:00 +0100299 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300}
301
Glenn Kastenf37971f2012-02-03 11:06:53 -0800302sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800304 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700305 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
306 wp<MediaRecorderClient> w = recorder;
307 Mutex::Autolock lock(mLock);
308 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100309 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310 return recorder;
311}
312
Gloria Wangdac6a312009-10-29 15:46:37 -0700313void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
314{
315 Mutex::Autolock lock(mLock);
316 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700318}
319
Glenn Kastenf37971f2012-02-03 11:06:53 -0800320sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800322 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100324 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 return retriever;
326}
327
Glenn Kastenf37971f2012-02-03 11:06:53 -0800328sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100329 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800331 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700333
334 sp<Client> c = new Client(
335 this, pid, connId, client, audioSessionId,
336 IPCThreadState::self()->getCallingUid());
337
Steve Block3856b092011-10-20 11:56:00 +0100338 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100339 IPCThreadState::self()->getCallingUid());
340
341 wp<Client> w = c;
342 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 Mutex::Autolock lock(mLock);
344 mClients.add(w);
345 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800346 return c;
347}
348
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700349sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
350 return MediaCodecList::getLocalInstance();
351}
352
Andreas Huber318ad9c2009-10-15 13:46:54 -0700353sp<IOMX> MediaPlayerService::getOMX() {
354 Mutex::Autolock autoLock(mLock);
355
356 if (mOMX.get() == NULL) {
357 mOMX = new OMX;
358 }
359
360 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700361}
362
Andreas Hubered3e3e02012-03-26 11:13:27 -0700363sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700364 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700365}
366
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800367sp<IDrm> MediaPlayerService::makeDrm() {
368 return new Drm;
369}
370
Andreas Huber279dcd82013-01-30 10:41:25 -0800371sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
372 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700373}
374
Jeff Brown2013a542012-09-04 21:38:42 -0700375sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
376 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700377 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
378 return NULL;
379 }
380
Jeff Brownced24b32012-09-05 17:48:03 -0700381 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700382}
383
Mark Salyzyn77342f72014-06-18 16:31:32 -0700384status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& /*args*/) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800385{
386 const size_t SIZE = 256;
387 char buffer[SIZE];
388 String8 result;
389
390 result.append(" AudioCache\n");
391 if (mHeap != 0) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000392 snprintf(buffer, 255, " heap base(%p), size(%zu), flags(%d)\n",
Eric Laurent3d00aa62013-09-24 09:53:27 -0700393 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800394 result.append(buffer);
395 }
Scott Fan7d409692013-04-28 10:13:54 +0800396 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 -0800397 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
398 result.append(buffer);
399 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
400 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
401 result.append(buffer);
402 ::write(fd, result.string(), result.size());
403 return NO_ERROR;
404}
405
406status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
407{
408 const size_t SIZE = 256;
409 char buffer[SIZE];
410 String8 result;
411
412 result.append(" AudioOutput\n");
413 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
414 mStreamType, mLeftVolume, mRightVolume);
415 result.append(buffer);
416 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800417 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700419 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
420 mAuxEffectId, mSendLevel);
421 result.append(buffer);
422
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800423 ::write(fd, result.string(), result.size());
424 if (mTrack != 0) {
425 mTrack->dump(fd, args);
426 }
427 return NO_ERROR;
428}
429
430status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
431{
432 const size_t SIZE = 256;
433 char buffer[SIZE];
434 String8 result;
435 result.append(" Client\n");
436 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
437 mPid, mConnId, mStatus, mLoop?"true": "false");
438 result.append(buffer);
439 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700440 if (mPlayer != NULL) {
441 mPlayer->dump(fd, args);
442 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443 if (mAudioOutput != 0) {
444 mAudioOutput->dump(fd, args);
445 }
446 write(fd, "\n", 1);
447 return NO_ERROR;
448}
449
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800450status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
451{
452 const size_t SIZE = 256;
453 char buffer[SIZE];
454 String8 result;
455 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
456 snprintf(buffer, SIZE, "Permission Denial: "
457 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
458 IPCThreadState::self()->getCallingPid(),
459 IPCThreadState::self()->getCallingUid());
460 result.append(buffer);
461 } else {
462 Mutex::Autolock lock(mLock);
463 for (int i = 0, n = mClients.size(); i < n; ++i) {
464 sp<Client> c = mClients[i].promote();
465 if (c != 0) c->dump(fd, args);
466 }
James Dongb9141222010-07-08 11:16:11 -0700467 if (mMediaRecorderClients.size() == 0) {
468 result.append(" No media recorder client\n\n");
469 } else {
470 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
471 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700472 if (c != 0) {
473 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
474 result.append(buffer);
475 write(fd, result.string(), result.size());
476 result = "\n";
477 c->dump(fd, args);
478 }
James Dongb9141222010-07-08 11:16:11 -0700479 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700480 }
481
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800482 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700483 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 FILE *f = fopen(buffer, "r");
485 if (f) {
486 while (!feof(f)) {
487 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700488 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800489 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700490 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800491 strstr(buffer, " /system/media/")) {
492 result.append(" ");
493 result.append(buffer);
494 }
495 }
496 fclose(f);
497 } else {
498 result.append("couldn't open ");
499 result.append(buffer);
500 result.append("\n");
501 }
502
Glenn Kasten0512ab52011-05-04 17:58:57 -0700503 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800504 DIR *d = opendir(buffer);
505 if (d) {
506 struct dirent *ent;
507 while((ent = readdir(d)) != NULL) {
508 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700509 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800510 struct stat s;
511 if (lstat(buffer, &s) == 0) {
512 if ((s.st_mode & S_IFMT) == S_IFLNK) {
513 char linkto[256];
514 int len = readlink(buffer, linkto, sizeof(linkto));
515 if(len > 0) {
516 if(len > 255) {
517 linkto[252] = '.';
518 linkto[253] = '.';
519 linkto[254] = '.';
520 linkto[255] = 0;
521 } else {
522 linkto[len] = 0;
523 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700524 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800525 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700526 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800527 strstr(linkto, "/system/media/") == linkto) {
528 result.append(" ");
529 result.append(buffer);
530 result.append(" -> ");
531 result.append(linkto);
532 result.append("\n");
533 }
534 }
535 } else {
536 result.append(" unexpected type for ");
537 result.append(buffer);
538 result.append("\n");
539 }
540 }
541 }
542 }
543 closedir(d);
544 } else {
545 result.append("couldn't open ");
546 result.append(buffer);
547 result.append("\n");
548 }
549
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800550 bool dumpMem = false;
551 for (size_t i = 0; i < args.size(); i++) {
552 if (args[i] == String16("-m")) {
553 dumpMem = true;
554 }
555 }
556 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700557 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800558 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800559 }
560 write(fd, result.string(), result.size());
561 return NO_ERROR;
562}
563
564void MediaPlayerService::removeClient(wp<Client> client)
565{
566 Mutex::Autolock lock(mLock);
567 mClients.remove(client);
568}
569
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700570MediaPlayerService::Client::Client(
571 const sp<MediaPlayerService>& service, pid_t pid,
572 int32_t connId, const sp<IMediaPlayerClient>& client,
573 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574{
Steve Block3856b092011-10-20 11:56:00 +0100575 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800576 mPid = pid;
577 mConnId = connId;
578 mService = service;
579 mClient = client;
580 mLoop = false;
581 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700582 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700583 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800584 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700585 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700586
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800587#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000588 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800589 mAntagonizer = new Antagonizer(notify, this);
590#endif
591}
592
593MediaPlayerService::Client::~Client()
594{
Steve Block3856b092011-10-20 11:56:00 +0100595 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596 mAudioOutput.clear();
597 wp<Client> client(this);
598 disconnect();
599 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700600 if (mAudioAttributes != NULL) {
601 free(mAudioAttributes);
602 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603}
604
605void MediaPlayerService::Client::disconnect()
606{
Steve Block3856b092011-10-20 11:56:00 +0100607 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608 // grab local reference and clear main reference to prevent future
609 // access to object
610 sp<MediaPlayerBase> p;
611 {
612 Mutex::Autolock l(mLock);
613 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800614 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800615 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700616
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800617 mPlayer.clear();
618
619 // clear the notification to prevent callbacks to dead client
620 // and reset the player. We assume the player will serialize
621 // access to itself if necessary.
622 if (p != 0) {
623 p->setNotifyCallback(0, 0);
624#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000625 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626 mAntagonizer->kill();
627#endif
628 p->reset();
629 }
630
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700631 disconnectNativeWindow();
632
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800633 IPCThreadState::self()->flushCommands();
634}
635
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800636sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
637{
638 // determine if we have the right player type
639 sp<MediaPlayerBase> p = mPlayer;
640 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100641 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800642 p.clear();
643 }
644 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700645 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800646 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700647
Jason Simmonsdb29e522011-08-12 13:46:55 -0700648 if (p != NULL) {
649 p->setUID(mUID);
650 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700651
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800652 return p;
653}
654
John Grossmanc795b642012-02-22 15:38:35 -0800655sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
656 player_type playerType)
657{
658 ALOGV("player type = %d", playerType);
659
660 // create the right type of player
661 sp<MediaPlayerBase> p = createPlayer(playerType);
662 if (p == NULL) {
663 return p;
664 }
665
666 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800667 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700668 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800669 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
670 }
671
672 return p;
673}
674
675void MediaPlayerService::Client::setDataSource_post(
676 const sp<MediaPlayerBase>& p,
677 status_t status)
678{
679 ALOGV(" setDataSource");
680 mStatus = status;
681 if (mStatus != OK) {
682 ALOGE(" error: %d", mStatus);
683 return;
684 }
685
686 // Set the re-transmission endpoint if one was chosen.
687 if (mRetransmitEndpointValid) {
688 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
689 if (mStatus != NO_ERROR) {
690 ALOGE("setRetransmitEndpoint error: %d", mStatus);
691 }
692 }
693
694 if (mStatus == OK) {
695 mPlayer = p;
696 }
697}
698
Andreas Huber2db84552010-01-28 11:19:57 -0800699status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800700 const sp<IMediaHTTPService> &httpService,
701 const char *url,
702 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800703{
Steve Block3856b092011-10-20 11:56:00 +0100704 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800705 if (url == NULL)
706 return UNKNOWN_ERROR;
707
Dave Burked681bbb2011-08-30 14:39:17 +0100708 if ((strncmp(url, "http://", 7) == 0) ||
709 (strncmp(url, "https://", 8) == 0) ||
710 (strncmp(url, "rtsp://", 7) == 0)) {
711 if (!checkPermission("android.permission.INTERNET")) {
712 return PERMISSION_DENIED;
713 }
714 }
715
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800716 if (strncmp(url, "content://", 10) == 0) {
717 // get a filedescriptor for the content Uri and
718 // pass it to the setDataSource(fd) method
719
720 String16 url16(url);
721 int fd = android::openContentProviderFile(url16);
722 if (fd < 0)
723 {
Steve Block29357bc2012-01-06 19:20:56 +0000724 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800725 return UNKNOWN_ERROR;
726 }
727 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
728 close(fd);
729 return mStatus;
730 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700731 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800732 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
733 if (p == NULL) {
734 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800735 }
736
Andreas Huber1b86fe02014-01-29 11:13:26 -0800737 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800738 return mStatus;
739 }
740}
741
742status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
743{
Steve Block3856b092011-10-20 11:56:00 +0100744 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800745 struct stat sb;
746 int ret = fstat(fd, &sb);
747 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000748 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800749 return UNKNOWN_ERROR;
750 }
751
Steve Block3856b092011-10-20 11:56:00 +0100752 ALOGV("st_dev = %llu", sb.st_dev);
753 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700754 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
755 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100756 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800757
758 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000759 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800760 ::close(fd);
761 return UNKNOWN_ERROR;
762 }
763 if (offset + length > sb.st_size) {
764 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100765 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800766 }
767
John Grossman44a7e422012-06-21 17:29:24 -0700768 player_type playerType = MediaPlayerFactory::getPlayerType(this,
769 fd,
770 offset,
771 length);
John Grossmanc795b642012-02-22 15:38:35 -0800772 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
773 if (p == NULL) {
774 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800775 }
776
777 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800778 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800779 return mStatus;
780}
781
Andreas Hubere2b10282010-11-23 11:41:34 -0800782status_t MediaPlayerService::Client::setDataSource(
783 const sp<IStreamSource> &source) {
784 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700785 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800786 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800787 if (p == NULL) {
788 return NO_INIT;
789 }
790
Andreas Hubere2b10282010-11-23 11:41:34 -0800791 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800792 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800793 return mStatus;
794}
795
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700796void MediaPlayerService::Client::disconnectNativeWindow() {
797 if (mConnectedWindow != NULL) {
798 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
799 NATIVE_WINDOW_API_MEDIA);
800
801 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000802 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700803 strerror(-err), err);
804 }
805 }
806 mConnectedWindow.clear();
807}
808
Glenn Kasten11731182011-02-08 17:26:17 -0800809status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800810 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800811{
Andy McFadden484566c2012-12-18 09:46:54 -0800812 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800813 sp<MediaPlayerBase> p = getPlayer();
814 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700815
Andy McFadden484566c2012-12-18 09:46:54 -0800816 sp<IBinder> binder(bufferProducer == NULL ? NULL :
817 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700818 if (mConnectedWindowBinder == binder) {
819 return OK;
820 }
821
822 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800823 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700824 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700825 status_t err = native_window_api_connect(anw.get(),
826 NATIVE_WINDOW_API_MEDIA);
827
828 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000829 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700830 // Note that we must do the reset before disconnecting from the ANW.
831 // Otherwise queue/dequeue calls could be made on the disconnected
832 // ANW, which may result in errors.
833 reset();
834
835 disconnectNativeWindow();
836
837 return err;
838 }
839 }
840
Andy McFadden484566c2012-12-18 09:46:54 -0800841 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700842 // disconnecting the old one. Otherwise queue/dequeue calls could be made
843 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800844 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700845
846 disconnectNativeWindow();
847
848 mConnectedWindow = anw;
849
850 if (err == OK) {
851 mConnectedWindowBinder = binder;
852 } else {
853 disconnectNativeWindow();
854 }
855
856 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800857}
858
Nicolas Catania1d187f12009-05-12 23:25:55 -0700859status_t MediaPlayerService::Client::invoke(const Parcel& request,
860 Parcel *reply)
861{
862 sp<MediaPlayerBase> p = getPlayer();
863 if (p == NULL) return UNKNOWN_ERROR;
864 return p->invoke(request, reply);
865}
866
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700867// This call doesn't need to access the native player.
868status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
869{
870 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700871 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700872
Nicolas Catania48290382009-07-10 13:53:06 -0700873 if (unmarshallFilter(filter, &allow, &status) &&
874 unmarshallFilter(filter, &drop, &status)) {
875 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700876
877 mMetadataAllow = allow;
878 mMetadataDrop = drop;
879 }
880 return status;
881}
882
Nicolas Catania48290382009-07-10 13:53:06 -0700883status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700884 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700885{
nikoa64c8c72009-07-20 15:07:26 -0700886 sp<MediaPlayerBase> player = getPlayer();
887 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700888
nikod608a812009-07-16 16:39:53 -0700889 status_t status;
890 // Placeholder for the return code, updated by the caller.
891 reply->writeInt32(-1);
892
nikoa64c8c72009-07-20 15:07:26 -0700893 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700894
895 // We don't block notifications while we fetch the data. We clear
896 // mMetadataUpdated first so we don't lose notifications happening
897 // during the rest of this call.
898 {
899 Mutex::Autolock lock(mLock);
900 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700901 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700902 }
903 mMetadataUpdated.clear();
904 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700905
nikoa64c8c72009-07-20 15:07:26 -0700906 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700907
nikoa64c8c72009-07-20 15:07:26 -0700908 metadata.appendHeader();
909 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700910
911 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700912 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000913 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700914 return status;
915 }
916
917 // FIXME: Implement filtering on the result. Not critical since
918 // filtering takes place on the update notifications already. This
919 // would be when all the metadata are fetch and a filter is set.
920
nikod608a812009-07-16 16:39:53 -0700921 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700922 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700923 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700924}
925
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800926status_t MediaPlayerService::Client::prepareAsync()
927{
Steve Block3856b092011-10-20 11:56:00 +0100928 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929 sp<MediaPlayerBase> p = getPlayer();
930 if (p == 0) return UNKNOWN_ERROR;
931 status_t ret = p->prepareAsync();
932#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000933 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800934 if (ret == NO_ERROR) mAntagonizer->start();
935#endif
936 return ret;
937}
938
939status_t MediaPlayerService::Client::start()
940{
Steve Block3856b092011-10-20 11:56:00 +0100941 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800942 sp<MediaPlayerBase> p = getPlayer();
943 if (p == 0) return UNKNOWN_ERROR;
944 p->setLooping(mLoop);
945 return p->start();
946}
947
948status_t MediaPlayerService::Client::stop()
949{
Steve Block3856b092011-10-20 11:56:00 +0100950 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800951 sp<MediaPlayerBase> p = getPlayer();
952 if (p == 0) return UNKNOWN_ERROR;
953 return p->stop();
954}
955
956status_t MediaPlayerService::Client::pause()
957{
Steve Block3856b092011-10-20 11:56:00 +0100958 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800959 sp<MediaPlayerBase> p = getPlayer();
960 if (p == 0) return UNKNOWN_ERROR;
961 return p->pause();
962}
963
964status_t MediaPlayerService::Client::isPlaying(bool* state)
965{
966 *state = false;
967 sp<MediaPlayerBase> p = getPlayer();
968 if (p == 0) return UNKNOWN_ERROR;
969 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100970 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800971 return NO_ERROR;
972}
973
974status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
975{
Steve Block3856b092011-10-20 11:56:00 +0100976 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977 sp<MediaPlayerBase> p = getPlayer();
978 if (p == 0) return UNKNOWN_ERROR;
979 status_t ret = p->getCurrentPosition(msec);
980 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100981 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800982 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000983 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800984 }
985 return ret;
986}
987
988status_t MediaPlayerService::Client::getDuration(int *msec)
989{
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800991 sp<MediaPlayerBase> p = getPlayer();
992 if (p == 0) return UNKNOWN_ERROR;
993 status_t ret = p->getDuration(msec);
994 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100995 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800996 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000997 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800998 }
999 return ret;
1000}
1001
Marco Nelissen6b74d672012-02-28 16:07:44 -08001002status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1003 ALOGV("setNextPlayer");
1004 Mutex::Autolock l(mLock);
1005 sp<Client> c = static_cast<Client*>(player.get());
1006 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001007
1008 if (c != NULL) {
1009 if (mAudioOutput != NULL) {
1010 mAudioOutput->setNextOutput(c->mAudioOutput);
1011 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1012 ALOGE("no current audio output");
1013 }
1014
1015 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1016 mPlayer->setNextPlayer(mNextClient->getPlayer());
1017 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001018 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001019
Marco Nelissen6b74d672012-02-28 16:07:44 -08001020 return OK;
1021}
1022
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001023status_t MediaPlayerService::Client::seekTo(int msec)
1024{
Steve Block3856b092011-10-20 11:56:00 +01001025 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001026 sp<MediaPlayerBase> p = getPlayer();
1027 if (p == 0) return UNKNOWN_ERROR;
1028 return p->seekTo(msec);
1029}
1030
1031status_t MediaPlayerService::Client::reset()
1032{
Steve Block3856b092011-10-20 11:56:00 +01001033 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001034 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001035 sp<MediaPlayerBase> p = getPlayer();
1036 if (p == 0) return UNKNOWN_ERROR;
1037 return p->reset();
1038}
1039
Glenn Kastenfff6d712012-01-12 16:38:12 -08001040status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001041{
Steve Block3856b092011-10-20 11:56:00 +01001042 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001043 // TODO: for hardware output, call player instead
1044 Mutex::Autolock l(mLock);
1045 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1046 return NO_ERROR;
1047}
1048
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001049status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1050{
1051 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1052 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1053 unmarshallAudioAttributes(parcel, mAudioAttributes);
1054
1055 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1056 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1057 mAudioAttributes->tags);
1058
1059 if (mAudioOutput != 0) {
1060 mAudioOutput->setAudioAttributes(mAudioAttributes);
1061 }
1062 return NO_ERROR;
1063}
1064
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001065status_t MediaPlayerService::Client::setLooping(int loop)
1066{
Steve Block3856b092011-10-20 11:56:00 +01001067 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001068 mLoop = loop;
1069 sp<MediaPlayerBase> p = getPlayer();
1070 if (p != 0) return p->setLooping(loop);
1071 return NO_ERROR;
1072}
1073
1074status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1075{
Steve Block3856b092011-10-20 11:56:00 +01001076 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001077
1078 // for hardware output, call player instead
1079 sp<MediaPlayerBase> p = getPlayer();
1080 {
1081 Mutex::Autolock l(mLock);
1082 if (p != 0 && p->hardwareOutput()) {
1083 MediaPlayerHWInterface* hwp =
1084 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1085 return hwp->setVolume(leftVolume, rightVolume);
1086 } else {
1087 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1088 return NO_ERROR;
1089 }
1090 }
1091
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001092 return NO_ERROR;
1093}
1094
Eric Laurent2beeb502010-07-16 07:43:46 -07001095status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1096{
Steve Block3856b092011-10-20 11:56:00 +01001097 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001098 Mutex::Autolock l(mLock);
1099 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1100 return NO_ERROR;
1101}
1102
1103status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1104{
Steve Block3856b092011-10-20 11:56:00 +01001105 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001106 Mutex::Autolock l(mLock);
1107 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1108 return NO_ERROR;
1109}
Nicolas Catania48290382009-07-10 13:53:06 -07001110
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001111status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001112 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001113 switch (key) {
1114 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1115 {
1116 Mutex::Autolock l(mLock);
1117 return setAudioAttributes_l(request);
1118 }
1119 default:
1120 sp<MediaPlayerBase> p = getPlayer();
1121 if (p == 0) { return UNKNOWN_ERROR; }
1122 return p->setParameter(key, request);
1123 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001124}
1125
1126status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001127 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001128 sp<MediaPlayerBase> p = getPlayer();
1129 if (p == 0) return UNKNOWN_ERROR;
1130 return p->getParameter(key, reply);
1131}
1132
John Grossmanc795b642012-02-22 15:38:35 -08001133status_t MediaPlayerService::Client::setRetransmitEndpoint(
1134 const struct sockaddr_in* endpoint) {
1135
1136 if (NULL != endpoint) {
1137 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1138 uint16_t p = ntohs(endpoint->sin_port);
1139 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1140 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1141 } else {
1142 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1143 }
1144
1145 sp<MediaPlayerBase> p = getPlayer();
1146
1147 // Right now, the only valid time to set a retransmit endpoint is before
1148 // player selection has been made (since the presence or absence of a
1149 // retransmit endpoint is going to determine which player is selected during
1150 // setDataSource).
1151 if (p != 0) return INVALID_OPERATION;
1152
1153 if (NULL != endpoint) {
1154 mRetransmitEndpoint = *endpoint;
1155 mRetransmitEndpointValid = true;
1156 } else {
1157 mRetransmitEndpointValid = false;
1158 }
1159
1160 return NO_ERROR;
1161}
1162
John Grossman44a7e422012-06-21 17:29:24 -07001163status_t MediaPlayerService::Client::getRetransmitEndpoint(
1164 struct sockaddr_in* endpoint)
1165{
1166 if (NULL == endpoint)
1167 return BAD_VALUE;
1168
1169 sp<MediaPlayerBase> p = getPlayer();
1170
1171 if (p != NULL)
1172 return p->getRetransmitEndpoint(endpoint);
1173
1174 if (!mRetransmitEndpointValid)
1175 return NO_INIT;
1176
1177 *endpoint = mRetransmitEndpoint;
1178
1179 return NO_ERROR;
1180}
1181
Gloria Wangb483c472011-04-11 17:23:27 -07001182void MediaPlayerService::Client::notify(
1183 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001184{
1185 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001186 if (client == NULL) {
1187 return;
1188 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001189
James Dongb8a98252012-08-26 16:13:03 -07001190 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001191 {
1192 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001193 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001194 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001195 if (client->mAudioOutput != NULL)
1196 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001197 client->mNextClient->start();
1198 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1199 }
1200 }
1201
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001202 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001203 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001204 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001205
1206 if(client->shouldDropMetadata(metadata_type)) {
1207 return;
1208 }
1209
1210 // Update the list of metadata that have changed. getMetadata
1211 // also access mMetadataUpdated and clears it.
1212 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001213 }
James Dongb8a98252012-08-26 16:13:03 -07001214
1215 if (c != NULL) {
1216 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1217 c->notify(msg, ext1, ext2, obj);
1218 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001219}
1220
Nicolas Catania48290382009-07-10 13:53:06 -07001221
nikoa64c8c72009-07-20 15:07:26 -07001222bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001223{
Nicolas Catania48290382009-07-10 13:53:06 -07001224 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001225
Nicolas Catania48290382009-07-10 13:53:06 -07001226 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001227 return true;
1228 }
1229
Nicolas Catania48290382009-07-10 13:53:06 -07001230 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001231 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001232 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001233 return true;
1234 }
1235}
1236
Nicolas Catania48290382009-07-10 13:53:06 -07001237
nikoa64c8c72009-07-20 15:07:26 -07001238void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001239 Mutex::Autolock lock(mLock);
1240 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1241 mMetadataUpdated.add(metadata_type);
1242 }
1243}
1244
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001245#if CALLBACK_ANTAGONIZER
1246const int Antagonizer::interval = 10000; // 10 msecs
1247
1248Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1249 mExit(false), mActive(false), mClient(client), mCb(cb)
1250{
1251 createThread(callbackThread, this);
1252}
1253
1254void Antagonizer::kill()
1255{
1256 Mutex::Autolock _l(mLock);
1257 mActive = false;
1258 mExit = true;
1259 mCondition.wait(mLock);
1260}
1261
1262int Antagonizer::callbackThread(void* user)
1263{
Steve Blockb8a80522011-12-20 16:23:08 +00001264 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001265 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1266 while (!p->mExit) {
1267 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001268 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001269 p->mCb(p->mClient, 0, 0, 0);
1270 }
1271 usleep(interval);
1272 }
1273 Mutex::Autolock _l(p->mLock);
1274 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001275 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001276 return 0;
1277}
1278#endif
1279
Andreas Huber1b86fe02014-01-29 11:13:26 -08001280status_t MediaPlayerService::decode(
1281 const sp<IMediaHTTPService> &httpService,
1282 const char* url,
1283 uint32_t *pSampleRate,
1284 int* pNumChannels,
1285 audio_format_t* pFormat,
1286 const sp<IMemoryHeap>& heap,
1287 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001288{
Steve Block3856b092011-10-20 11:56:00 +01001289 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001290 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001291 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292
1293 // Protect our precious, precious DRMd ringtones by only allowing
1294 // decoding of http, but not filesystem paths or content Uris.
1295 // If the application wants to decode those, it should open a
1296 // filedescriptor for them and use that.
1297 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001298 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001299 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300 }
1301
John Grossman44a7e422012-06-21 17:29:24 -07001302 player_type playerType =
1303 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001304 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305
1306 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001307 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001308 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001309 if (player == NULL) goto Exit;
1310 if (player->hardwareOutput()) goto Exit;
1311
1312 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1313
1314 // set data source
Andreas Huber1b86fe02014-01-29 11:13:26 -08001315 if (player->setDataSource(httpService, url) != NO_ERROR) goto Exit;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316
Steve Block3856b092011-10-20 11:56:00 +01001317 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001318 player->prepareAsync();
1319
Steve Block3856b092011-10-20 11:56:00 +01001320 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001321 if (cache->wait() != NO_ERROR) goto Exit;
1322
Steve Block3856b092011-10-20 11:56:00 +01001323 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001324 player->start();
1325
Steve Block3856b092011-10-20 11:56:00 +01001326 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001327 cache->wait();
1328 // in case of error, return what was successfully decoded.
1329 if (cache->size() == 0) {
1330 goto Exit;
1331 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001332
Eric Laurent3d00aa62013-09-24 09:53:27 -07001333 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001334 *pSampleRate = cache->sampleRate();
1335 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001336 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001337 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1338 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1339 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001340
1341Exit:
1342 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001343 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001344}
1345
Eric Laurent3d00aa62013-09-24 09:53:27 -07001346status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1347 uint32_t *pSampleRate, int* pNumChannels,
1348 audio_format_t* pFormat,
1349 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001350{
Steve Block3856b092011-10-20 11:56:00 +01001351 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001352 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001353 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001354
John Grossman44a7e422012-06-21 17:29:24 -07001355 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1356 fd,
1357 offset,
1358 length);
Steve Block3856b092011-10-20 11:56:00 +01001359 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001360
1361 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001362 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001363 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001364 if (player == NULL) goto Exit;
1365 if (player->hardwareOutput()) goto Exit;
1366
1367 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1368
1369 // set data source
1370 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1371
Steve Block3856b092011-10-20 11:56:00 +01001372 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001373 player->prepareAsync();
1374
Steve Block3856b092011-10-20 11:56:00 +01001375 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001376 if (cache->wait() != NO_ERROR) goto Exit;
1377
Steve Block3856b092011-10-20 11:56:00 +01001378 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001379 player->start();
1380
Steve Block3856b092011-10-20 11:56:00 +01001381 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001382 cache->wait();
1383 // in case of error, return what was successfully decoded.
1384 if (cache->size() == 0) {
1385 goto Exit;
1386 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001387
Eric Laurent3d00aa62013-09-24 09:53:27 -07001388 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001389 *pSampleRate = cache->sampleRate();
1390 *pNumChannels = cache->channelCount();
1391 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001392 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1393 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1394 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001395
1396Exit:
1397 if (player != 0) player->reset();
1398 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001399 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001400}
1401
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001402
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001403#undef LOG_TAG
1404#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001405MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1406 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001407 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001408 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001409 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001410 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001411 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001412 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001413 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001414 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001415 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001416 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001417 mLeftVolume = 1.0;
1418 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001419 mPlaybackRatePermille = 1000;
1420 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001421 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001422 mAuxEffectId = 0;
1423 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001424 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001425 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001426}
1427
1428MediaPlayerService::AudioOutput::~AudioOutput()
1429{
1430 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001431 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001432}
1433
1434void MediaPlayerService::AudioOutput::setMinBufferCount()
1435{
1436 char value[PROPERTY_VALUE_MAX];
1437 if (property_get("ro.kernel.qemu", value, 0)) {
1438 mIsOnEmulator = true;
1439 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1440 }
1441}
1442
1443bool MediaPlayerService::AudioOutput::isOnEmulator()
1444{
1445 setMinBufferCount();
1446 return mIsOnEmulator;
1447}
1448
1449int MediaPlayerService::AudioOutput::getMinBufferCount()
1450{
1451 setMinBufferCount();
1452 return mMinBufferCount;
1453}
1454
1455ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1456{
1457 if (mTrack == 0) return NO_INIT;
1458 return mTrack->frameCount() * frameSize();
1459}
1460
1461ssize_t MediaPlayerService::AudioOutput::frameCount() const
1462{
1463 if (mTrack == 0) return NO_INIT;
1464 return mTrack->frameCount();
1465}
1466
1467ssize_t MediaPlayerService::AudioOutput::channelCount() const
1468{
1469 if (mTrack == 0) return NO_INIT;
1470 return mTrack->channelCount();
1471}
1472
1473ssize_t MediaPlayerService::AudioOutput::frameSize() const
1474{
1475 if (mTrack == 0) return NO_INIT;
1476 return mTrack->frameSize();
1477}
1478
1479uint32_t MediaPlayerService::AudioOutput::latency () const
1480{
Eric Laurentdb354e52012-03-05 17:27:11 -08001481 if (mTrack == 0) return 0;
1482 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001483}
1484
1485float MediaPlayerService::AudioOutput::msecsPerFrame() const
1486{
1487 return mMsecsPerFrame;
1488}
1489
Marco Nelissen4110c102012-03-29 09:31:28 -07001490status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001491{
1492 if (mTrack == 0) return NO_INIT;
1493 return mTrack->getPosition(position);
1494}
1495
Marco Nelissen4110c102012-03-29 09:31:28 -07001496status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1497{
1498 if (mTrack == 0) return NO_INIT;
1499 *frameswritten = mBytesWritten / frameSize();
1500 return OK;
1501}
1502
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001503status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1504{
1505 if (mTrack == 0) return NO_INIT;
1506 return mTrack->setParameters(keyValuePairs);
1507}
1508
1509String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1510{
1511 if (mTrack == 0) return String8::empty();
1512 return mTrack->getParameters(keys);
1513}
1514
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001515void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1516 mAttributes = attributes;
1517}
1518
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001519void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1520{
1521 ALOGV("deleteRecycledTrack");
1522
1523 if (mRecycledTrack != 0) {
1524
1525 if (mCallbackData != NULL) {
1526 mCallbackData->setOutput(NULL);
1527 mCallbackData->endTrackSwitch();
1528 }
1529
1530 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1531 mRecycledTrack->flush();
1532 }
1533 // An offloaded track isn't flushed because the STREAM_END is reported
1534 // slightly prematurely to allow time for the gapless track switch
1535 // but this means that if we decide not to recycle the track there
1536 // could be a small amount of residual data still playing. We leave
1537 // AudioFlinger to drain the track.
1538
1539 mRecycledTrack.clear();
1540 delete mCallbackData;
1541 mCallbackData = NULL;
1542 close();
1543 }
1544}
1545
Andreas Huber20111aa2009-07-14 16:56:47 -07001546status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001547 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1548 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001549 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001550 audio_output_flags_t flags,
1551 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001552{
Andreas Huber20111aa2009-07-14 16:56:47 -07001553 mCallback = cb;
1554 mCallbackCookie = cookie;
1555
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001556 // Check argument "bufferCount" against the mininum buffer count
1557 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001558 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001559 bufferCount = mMinBufferCount;
1560
1561 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001562 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1563 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001564 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001565 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001566 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001567
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001568 // offloading is only supported in callback mode for now.
1569 // offloadInfo must be present if offload flag is set
1570 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1571 ((cb == NULL) || (offloadInfo == NULL))) {
1572 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001573 }
1574
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001575 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1576 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1577 } else {
1578 uint32_t afSampleRate;
1579 size_t afFrameCount;
1580
1581 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1582 return NO_INIT;
1583 }
1584 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1585 return NO_INIT;
1586 }
1587
1588 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1589 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001590
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001591 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001592 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001593 if (0 == channelMask) {
1594 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1595 return NO_INIT;
1596 }
1597 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001598
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001599 // Check whether we can recycle the track
1600 bool reuse = false;
1601 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001602
Glenn Kasten2799d742013-05-30 14:33:29 -07001603 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001604 // check whether we are switching between two offloaded tracks
1605 bothOffloaded = (flags & mRecycledTrack->getFlags()
1606 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001607
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001608 // check if the existing track can be reused as-is, or if a new track needs to be created.
1609 reuse = true;
1610
Marco Nelissen67295b52012-06-11 14:52:53 -07001611 if ((mCallbackData == NULL && mCallback != NULL) ||
1612 (mCallbackData != NULL && mCallback == NULL)) {
1613 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1614 ALOGV("can't chain callback and write");
1615 reuse = false;
1616 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001617 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1618 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001619 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001620 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001621 reuse = false;
1622 } else if (flags != mFlags) {
1623 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1624 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001625 } else if (mRecycledTrack->format() != format) {
1626 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001627 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001628 } else {
1629 ALOGV("no track available to recycle");
1630 }
1631
1632 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1633
1634 // If we can't recycle and both tracks are offloaded
1635 // we must close the previous output before opening a new one
1636 if (bothOffloaded && !reuse) {
1637 ALOGV("both offloaded and not recycling");
1638 deleteRecycledTrack();
1639 }
1640
1641 sp<AudioTrack> t;
1642 CallbackData *newcbd = NULL;
1643
1644 // We don't attempt to create a new track if we are recycling an
1645 // offloaded track. But, if we are recycling a non-offloaded or we
1646 // are switching where one is offloaded and one isn't then we create
1647 // the new track in advance so that we can read additional stream info
1648
1649 if (!(reuse && bothOffloaded)) {
1650 ALOGV("creating new AudioTrack");
1651
1652 if (mCallback != NULL) {
1653 newcbd = new CallbackData(this);
1654 t = new AudioTrack(
1655 mStreamType,
1656 sampleRate,
1657 format,
1658 channelMask,
1659 frameCount,
1660 flags,
1661 CallbackWrapper,
1662 newcbd,
1663 0, // notification frames
1664 mSessionId,
1665 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001666 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001667 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001668 mPid,
1669 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001670 } else {
1671 t = new AudioTrack(
1672 mStreamType,
1673 sampleRate,
1674 format,
1675 channelMask,
1676 frameCount,
1677 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001678 NULL, // callback
1679 NULL, // user data
1680 0, // notification frames
1681 mSessionId,
1682 AudioTrack::TRANSFER_DEFAULT,
1683 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001684 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001685 mPid,
1686 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001687 }
1688
1689 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1690 ALOGE("Unable to create audio track");
1691 delete newcbd;
1692 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001693 } else {
1694 // successful AudioTrack initialization implies a legacy stream type was generated
1695 // from the audio attributes
1696 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001697 }
1698 }
1699
1700 if (reuse) {
1701 CHECK(mRecycledTrack != NULL);
1702
1703 if (!bothOffloaded) {
1704 if (mRecycledTrack->frameCount() != t->frameCount()) {
1705 ALOGV("framecount differs: %u/%u frames",
1706 mRecycledTrack->frameCount(), t->frameCount());
1707 reuse = false;
1708 }
1709 }
1710
Marco Nelissen67295b52012-06-11 14:52:53 -07001711 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001712 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001713 close();
1714 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001715 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001716 if (mCallbackData != NULL) {
1717 mCallbackData->setOutput(this);
1718 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001719 delete newcbd;
1720 return OK;
1721 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001722 }
1723
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001724 // we're not going to reuse the track, unblock and flush it
1725 // this was done earlier if both tracks are offloaded
1726 if (!bothOffloaded) {
1727 deleteRecycledTrack();
1728 }
1729
1730 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1731
Marco Nelissen67295b52012-06-11 14:52:53 -07001732 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001733 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001734 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001735
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001736 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001737 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001738 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001739 uint32_t pos;
1740 if (t->getPosition(&pos) == OK) {
1741 mBytesWritten = uint64_t(pos) * t->frameSize();
1742 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001743 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001744
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001745 status_t res = NO_ERROR;
1746 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1747 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1748 if (res == NO_ERROR) {
1749 t->setAuxEffectSendLevel(mSendLevel);
1750 res = t->attachAuxEffect(mAuxEffectId);
1751 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001752 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001753 ALOGV("open() DONE status %d", res);
1754 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001755}
1756
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001757status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001758{
Steve Block3856b092011-10-20 11:56:00 +01001759 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001760 if (mCallbackData != NULL) {
1761 mCallbackData->endTrackSwitch();
1762 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001763 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001764 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001765 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001766 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001767 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001768 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001769}
1770
Marco Nelissen6b74d672012-02-28 16:07:44 -08001771void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1772 mNextOutput = nextOutput;
1773}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001774
1775
Marco Nelissen6b74d672012-02-28 16:07:44 -08001776void MediaPlayerService::AudioOutput::switchToNextOutput() {
1777 ALOGV("switchToNextOutput");
1778 if (mNextOutput != NULL) {
1779 if (mCallbackData != NULL) {
1780 mCallbackData->beginTrackSwitch();
1781 }
1782 delete mNextOutput->mCallbackData;
1783 mNextOutput->mCallbackData = mCallbackData;
1784 mCallbackData = NULL;
1785 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001786 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001787 mNextOutput->mSampleRateHz = mSampleRateHz;
1788 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001789 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001790 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001791 }
1792}
1793
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001794ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1795{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001796 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001797
Steve Block3856b092011-10-20 11:56:00 +01001798 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001799 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001800 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001801 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001802 return ret;
1803 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001804 return NO_INIT;
1805}
1806
1807void MediaPlayerService::AudioOutput::stop()
1808{
Steve Block3856b092011-10-20 11:56:00 +01001809 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001810 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001811}
1812
1813void MediaPlayerService::AudioOutput::flush()
1814{
Steve Block3856b092011-10-20 11:56:00 +01001815 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001816 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001817}
1818
1819void MediaPlayerService::AudioOutput::pause()
1820{
Steve Block3856b092011-10-20 11:56:00 +01001821 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001822 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001823}
1824
1825void MediaPlayerService::AudioOutput::close()
1826{
Steve Block3856b092011-10-20 11:56:00 +01001827 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001828 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829}
1830
1831void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1832{
Steve Block3856b092011-10-20 11:56:00 +01001833 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001834 mLeftVolume = left;
1835 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001836 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001837 mTrack->setVolume(left, right);
1838 }
1839}
1840
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001841status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1842{
1843 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1844 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001845 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001846 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1847 } else {
1848 res = NO_INIT;
1849 }
1850 mPlaybackRatePermille = ratePermille;
1851 if (mSampleRateHz != 0) {
1852 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1853 }
1854 return res;
1855}
1856
Eric Laurent2beeb502010-07-16 07:43:46 -07001857status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1858{
Steve Block3856b092011-10-20 11:56:00 +01001859 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001860 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001861 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001862 return mTrack->setAuxEffectSendLevel(level);
1863 }
1864 return NO_ERROR;
1865}
1866
1867status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1868{
Steve Block3856b092011-10-20 11:56:00 +01001869 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001870 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001871 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001872 return mTrack->attachAuxEffect(effectId);
1873 }
1874 return NO_ERROR;
1875}
1876
Andreas Huber20111aa2009-07-14 16:56:47 -07001877// static
1878void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001879 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001880 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001881 CallbackData *data = (CallbackData*)cookie;
1882 data->lock();
1883 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001884 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001885 if (me == NULL) {
1886 // no output set, likely because the track was scheduled to be reused
1887 // by another player, but the format turned out to be incompatible.
1888 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001889 if (buffer != NULL) {
1890 buffer->size = 0;
1891 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001892 return;
1893 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001894
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001895 switch(event) {
1896 case AudioTrack::EVENT_MORE_DATA: {
1897 size_t actualSize = (*me->mCallback)(
1898 me, buffer->raw, buffer->size, me->mCallbackCookie,
1899 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001900
aarti jadhav-gaikwadf2575572014-08-13 15:04:39 +05301901 if ((me->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0 &&
1902 actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001903 // We've reached EOS but the audio track is not stopped yet,
1904 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001905
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001906 memset(buffer->raw, 0, buffer->size);
1907 actualSize = buffer->size;
1908 }
1909
1910 buffer->size = actualSize;
1911 } break;
1912
1913
1914 case AudioTrack::EVENT_STREAM_END:
1915 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1916 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1917 me->mCallbackCookie, CB_EVENT_STREAM_END);
1918 break;
1919
1920 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1921 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1922 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1923 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1924 break;
1925
1926 default:
1927 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001928 }
1929
Marco Nelissen6b74d672012-02-28 16:07:44 -08001930 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001931}
1932
Marco Nelissen4110c102012-03-29 09:31:28 -07001933int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001934{
1935 return mSessionId;
1936}
1937
Eric Laurent6f59db12013-07-26 17:16:50 -07001938uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1939{
1940 if (mTrack == 0) return 0;
1941 return mTrack->getSampleRate();
1942}
1943
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001944#undef LOG_TAG
1945#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001946MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1947 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1948 mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001949{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001950}
1951
1952uint32_t MediaPlayerService::AudioCache::latency () const
1953{
1954 return 0;
1955}
1956
1957float MediaPlayerService::AudioCache::msecsPerFrame() const
1958{
1959 return mMsecsPerFrame;
1960}
1961
Marco Nelissen4110c102012-03-29 09:31:28 -07001962status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001963{
1964 if (position == 0) return BAD_VALUE;
1965 *position = mSize;
1966 return NO_ERROR;
1967}
1968
Marco Nelissen4110c102012-03-29 09:31:28 -07001969status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1970{
1971 if (written == 0) return BAD_VALUE;
1972 *written = mSize;
1973 return NO_ERROR;
1974}
1975
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001976////////////////////////////////////////////////////////////////////////////////
1977
1978struct CallbackThread : public Thread {
1979 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1980 MediaPlayerBase::AudioSink::AudioCallback cb,
1981 void *cookie);
1982
1983protected:
1984 virtual ~CallbackThread();
1985
1986 virtual bool threadLoop();
1987
1988private:
1989 wp<MediaPlayerBase::AudioSink> mSink;
1990 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1991 void *mCookie;
1992 void *mBuffer;
1993 size_t mBufferSize;
1994
1995 CallbackThread(const CallbackThread &);
1996 CallbackThread &operator=(const CallbackThread &);
1997};
1998
1999CallbackThread::CallbackThread(
2000 const wp<MediaPlayerBase::AudioSink> &sink,
2001 MediaPlayerBase::AudioSink::AudioCallback cb,
2002 void *cookie)
2003 : mSink(sink),
2004 mCallback(cb),
2005 mCookie(cookie),
2006 mBuffer(NULL),
2007 mBufferSize(0) {
2008}
2009
2010CallbackThread::~CallbackThread() {
2011 if (mBuffer) {
2012 free(mBuffer);
2013 mBuffer = NULL;
2014 }
2015}
2016
2017bool CallbackThread::threadLoop() {
2018 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2019 if (sink == NULL) {
2020 return false;
2021 }
2022
2023 if (mBuffer == NULL) {
2024 mBufferSize = sink->bufferSize();
2025 mBuffer = malloc(mBufferSize);
2026 }
2027
2028 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002029 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2030 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002031
2032 if (actualSize > 0) {
2033 sink->write(mBuffer, actualSize);
2034 }
2035
2036 return true;
2037}
2038
2039////////////////////////////////////////////////////////////////////////////////
2040
Andreas Huber20111aa2009-07-14 16:56:47 -07002041status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002042 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2043 audio_format_t format, int bufferCount,
Mark Salyzyn77342f72014-06-18 16:31:32 -07002044 AudioCallback cb, void *cookie, audio_output_flags_t /*flags*/,
2045 const audio_offload_info_t* /*offloadInfo*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002046{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002047 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08002048 if (mHeap->getHeapID() < 0) {
2049 return NO_INIT;
2050 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002051
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002052 mSampleRate = sampleRate;
2053 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08002054 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002055 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002056
2057 if (cb != NULL) {
2058 mCallbackThread = new CallbackThread(this, cb, cookie);
2059 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002060 return NO_ERROR;
2061}
2062
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002063status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002064 if (mCallbackThread != NULL) {
2065 mCallbackThread->run("AudioCache callback");
2066 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002067 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002068}
2069
2070void MediaPlayerService::AudioCache::stop() {
2071 if (mCallbackThread != NULL) {
2072 mCallbackThread->requestExitAndWait();
2073 }
2074}
2075
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002076ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
2077{
Steve Block3856b092011-10-20 11:56:00 +01002078 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002079 if ((buffer == 0) || (size == 0)) return size;
2080
2081 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
2082 if (p == NULL) return NO_INIT;
2083 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01002084 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002085 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00002086 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002087 size = mHeap->getSize() - mSize;
2088 }
2089 memcpy(p, buffer, size);
2090 mSize += size;
2091 return size;
2092}
2093
2094// call with lock held
2095status_t MediaPlayerService::AudioCache::wait()
2096{
2097 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08002098 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002099 mSignal.wait(mLock);
2100 }
2101 mCommandComplete = false;
2102
2103 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01002104 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002105 } else {
Steve Block3856b092011-10-20 11:56:00 +01002106 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002107 }
2108 return mError;
2109}
2110
Gloria Wangb483c472011-04-11 17:23:27 -07002111void MediaPlayerService::AudioCache::notify(
Mark Salyzyn77342f72014-06-18 16:31:32 -07002112 void* cookie, int msg, int ext1, int ext2, const Parcel* /*obj*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002113{
Steve Block3856b092011-10-20 11:56:00 +01002114 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002115 AudioCache* p = static_cast<AudioCache*>(cookie);
2116
2117 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002118 switch (msg)
2119 {
2120 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002121 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002122 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08002123 break;
2124 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002125 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002126 break;
2127 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002128 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002129 break;
2130 default:
Steve Block3856b092011-10-20 11:56:00 +01002131 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002132 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002133 }
2134
2135 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002136 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002137 p->mCommandComplete = true;
2138 p->mSignal.signal();
2139}
2140
Marco Nelissen4110c102012-03-29 09:31:28 -07002141int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002142{
2143 return 0;
2144}
2145
Eric Laurent6f59db12013-07-26 17:16:50 -07002146uint32_t MediaPlayerService::AudioCache::getSampleRate() const
2147{
2148 if (mMsecsPerFrame == 0) {
2149 return 0;
2150 }
2151 return (uint32_t)(1.e3 / mMsecsPerFrame);
2152}
2153
Gloria Wang7cf180c2011-02-19 18:37:57 -08002154void MediaPlayerService::addBatteryData(uint32_t params)
2155{
2156 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002157
2158 int32_t time = systemTime() / 1000000L;
2159
2160 // change audio output devices. This notification comes from AudioFlinger
2161 if ((params & kBatteryDataSpeakerOn)
2162 || (params & kBatteryDataOtherAudioDeviceOn)) {
2163
2164 int deviceOn[NUM_AUDIO_DEVICES];
2165 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2166 deviceOn[i] = 0;
2167 }
2168
2169 if ((params & kBatteryDataSpeakerOn)
2170 && (params & kBatteryDataOtherAudioDeviceOn)) {
2171 deviceOn[SPEAKER_AND_OTHER] = 1;
2172 } else if (params & kBatteryDataSpeakerOn) {
2173 deviceOn[SPEAKER] = 1;
2174 } else {
2175 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2176 }
2177
2178 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2179 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2180
2181 if (mBatteryAudio.refCount > 0) { // if playing audio
2182 if (!deviceOn[i]) {
2183 mBatteryAudio.lastTime[i] += time;
2184 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2185 mBatteryAudio.lastTime[i] = 0;
2186 } else {
2187 mBatteryAudio.lastTime[i] = 0 - time;
2188 }
2189 }
2190
2191 mBatteryAudio.deviceOn[i] = deviceOn[i];
2192 }
2193 }
2194 return;
2195 }
2196
2197 // an sudio stream is started
2198 if (params & kBatteryDataAudioFlingerStart) {
2199 // record the start time only if currently no other audio
2200 // is being played
2201 if (mBatteryAudio.refCount == 0) {
2202 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2203 if (mBatteryAudio.deviceOn[i]) {
2204 mBatteryAudio.lastTime[i] -= time;
2205 }
2206 }
2207 }
2208
2209 mBatteryAudio.refCount ++;
2210 return;
2211
2212 } else if (params & kBatteryDataAudioFlingerStop) {
2213 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002214 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002215 return;
2216 }
2217
2218 // record the stop time only if currently this is the only
2219 // audio being played
2220 if (mBatteryAudio.refCount == 1) {
2221 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2222 if (mBatteryAudio.deviceOn[i]) {
2223 mBatteryAudio.lastTime[i] += time;
2224 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2225 mBatteryAudio.lastTime[i] = 0;
2226 }
2227 }
2228 }
2229
2230 mBatteryAudio.refCount --;
2231 return;
2232 }
2233
Gloria Wang7cf180c2011-02-19 18:37:57 -08002234 int uid = IPCThreadState::self()->getCallingUid();
2235 if (uid == AID_MEDIA) {
2236 return;
2237 }
2238 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002239
2240 if (index < 0) { // create a new entry for this UID
2241 BatteryUsageInfo info;
2242 info.audioTotalTime = 0;
2243 info.videoTotalTime = 0;
2244 info.audioLastTime = 0;
2245 info.videoLastTime = 0;
2246 info.refCount = 0;
2247
Gloria Wang9ee159b2011-02-24 14:51:45 -08002248 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002249 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002250 return;
2251 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002252 }
2253
2254 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2255
2256 if (params & kBatteryDataCodecStarted) {
2257 if (params & kBatteryDataTrackAudio) {
2258 info.audioLastTime -= time;
2259 info.refCount ++;
2260 }
2261 if (params & kBatteryDataTrackVideo) {
2262 info.videoLastTime -= time;
2263 info.refCount ++;
2264 }
2265 } else {
2266 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002267 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002268 return;
2269 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002270 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002271 mBatteryData.removeItem(uid);
2272 return;
2273 }
2274
2275 if (params & kBatteryDataTrackAudio) {
2276 info.audioLastTime += time;
2277 info.refCount --;
2278 }
2279 if (params & kBatteryDataTrackVideo) {
2280 info.videoLastTime += time;
2281 info.refCount --;
2282 }
2283
2284 // no stream is being played by this UID
2285 if (info.refCount == 0) {
2286 info.audioTotalTime += info.audioLastTime;
2287 info.audioLastTime = 0;
2288 info.videoTotalTime += info.videoLastTime;
2289 info.videoLastTime = 0;
2290 }
2291 }
2292}
2293
2294status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2295 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002296
2297 // audio output devices usage
2298 int32_t time = systemTime() / 1000000L; //in ms
2299 int32_t totalTime;
2300
2301 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2302 totalTime = mBatteryAudio.totalTime[i];
2303
2304 if (mBatteryAudio.deviceOn[i]
2305 && (mBatteryAudio.lastTime[i] != 0)) {
2306 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2307 totalTime += tmpTime;
2308 }
2309
2310 reply->writeInt32(totalTime);
2311 // reset the total time
2312 mBatteryAudio.totalTime[i] = 0;
2313 }
2314
2315 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002316 BatteryUsageInfo info;
2317 int size = mBatteryData.size();
2318
2319 reply->writeInt32(size);
2320 int i = 0;
2321
2322 while (i < size) {
2323 info = mBatteryData.valueAt(i);
2324
2325 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2326 reply->writeInt32(info.audioTotalTime);
2327 reply->writeInt32(info.videoTotalTime);
2328
2329 info.audioTotalTime = 0;
2330 info.videoTotalTime = 0;
2331
2332 // remove the UID entry where no stream is being played
2333 if (info.refCount <= 0) {
2334 mBatteryData.removeItemsAt(i);
2335 size --;
2336 i --;
2337 }
2338 i++;
2339 }
2340 return NO_ERROR;
2341}
nikoa64c8c72009-07-20 15:07:26 -07002342} // namespace android