blob: 735344c9c5110abfc023c8a401f060f084c44ef8 [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>
Eric Laurent9cb839a2011-09-27 09:48:56 -070057#include <media/stagefright/MediaErrors.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010058#include <media/stagefright/AudioPlayer.h>
59#include <media/stagefright/foundation/ADebug.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080060
Dima Zavin64760242011-05-11 14:15:23 -070061#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070062
Gloria Wang7cf180c2011-02-19 18:37:57 -080063#include <private/android_filesystem_config.h>
64
James Dong559bf282012-03-28 10:29:14 -070065#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080066#include "MediaRecorderClient.h"
67#include "MediaPlayerService.h"
68#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070069#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080070
71#include "MidiFile.h"
Nicolas Catania14d27472009-07-13 14:37:49 -070072#include "TestPlayerStub.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070073#include "StagefrightPlayer.h"
Andreas Huberf9334412010-12-15 15:17:42 -080074#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070075
Andreas Huber20111aa2009-07-14 16:56:47 -070076#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070077
Andreas Hubered3e3e02012-03-26 11:13:27 -070078#include "Crypto.h"
Jeff Tinkercc82dc62013-02-08 10:18:35 -080079#include "Drm.h"
Andreas Huber59451f82012-09-18 10:36:32 -070080#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070081#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070082#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070083
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070084namespace {
nikoa64c8c72009-07-20 15:07:26 -070085using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070086using android::status_t;
87using android::OK;
88using android::BAD_VALUE;
89using android::NOT_ENOUGH_DATA;
90using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070091
92// Max number of entries in the filter.
93const int kMaxFilterSize = 64; // I pulled that out of thin air.
94
nikoa64c8c72009-07-20 15:07:26 -070095// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -070096
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070097
98// Unmarshall a filter from a Parcel.
99// Filter format in a parcel:
100//
101// 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
102// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103// | number of entries (n) |
104// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105// | metadata type 1 |
106// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107// | metadata type 2 |
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109// ....
110// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111// | metadata type n |
112// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113//
114// @param p Parcel that should start with a filter.
115// @param[out] filter On exit contains the list of metadata type to be
116// filtered.
117// @param[out] status On exit contains the status code to be returned.
118// @return true if the parcel starts with a valid filter.
119bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700120 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700121 status_t *status)
122{
Nicolas Catania48290382009-07-10 13:53:06 -0700123 int32_t val;
124 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700125 {
Steve Block29357bc2012-01-06 19:20:56 +0000126 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 *status = NOT_ENOUGH_DATA;
128 return false;
129 }
130
Nicolas Catania48290382009-07-10 13:53:06 -0700131 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700132 {
Steve Block29357bc2012-01-06 19:20:56 +0000133 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134 *status = BAD_VALUE;
135 return false;
136 }
137
Nicolas Catania48290382009-07-10 13:53:06 -0700138 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700139
140 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700141 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700142
nikoa64c8c72009-07-20 15:07:26 -0700143 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700144
Nicolas Catania48290382009-07-10 13:53:06 -0700145
146 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700147 {
Steve Block29357bc2012-01-06 19:20:56 +0000148 ALOGE("Filter too short expected %d but got %d", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700149 *status = NOT_ENOUGH_DATA;
150 return false;
151 }
152
nikoa64c8c72009-07-20 15:07:26 -0700153 const Metadata::Type *data =
154 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700155
Nicolas Catania48290382009-07-10 13:53:06 -0700156 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157 {
Steve Block29357bc2012-01-06 19:20:56 +0000158 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700159 *status = BAD_VALUE;
160 return false;
161 }
162
163 // TODO: The stl impl of vector would be more efficient here
164 // because it degenerates into a memcpy on pod types. Try to
165 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700166 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700167 {
Nicolas Catania48290382009-07-10 13:53:06 -0700168 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700169 ++data;
170 }
171 *status = OK;
172 return true;
173}
174
Nicolas Catania48290382009-07-10 13:53:06 -0700175// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700176// @param val To be searched.
177// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700178bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700179{
180 // Deal with empty and ANY right away
181 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700182 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700183
Nicolas Catania48290382009-07-10 13:53:06 -0700184 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700185}
186
187} // anonymous namespace
188
189
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700190namespace {
191using android::Parcel;
192using android::String16;
193
194// marshalling tag indicating flattened utf16 tags
195// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
196const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
197
198// Audio attributes format in a parcel:
199//
200// 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
201// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
202// | usage |
203// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
204// | content_type |
205// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
206// | flags |
207// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
208// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
209// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
210// | flattened tags in UTF16 |
211// | ... |
212// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213//
214// @param p Parcel that contains audio attributes.
215// @param[out] attributes On exit points to an initialized audio_attributes_t structure
216// @param[out] status On exit contains the status code to be returned.
217void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
218{
219 attributes->usage = (audio_usage_t) parcel.readInt32();
220 attributes->content_type = (audio_content_type_t) parcel.readInt32();
221 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
222 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
223 if (hasFlattenedTag) {
224 // the tags are UTF16, convert to UTF8
225 String16 tags = parcel.readString16();
226 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
227 if (realTagSize <= 0) {
228 strcpy(attributes->tags, "");
229 } else {
230 // copy the flattened string into the attributes as the destination for the conversion:
231 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
232 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
233 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
234 utf16_to_utf8(tags.string(), tagSize, attributes->tags);
235 }
236 } else {
237 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
238 strcpy(attributes->tags, "");
239 }
240}
241} // anonymous namespace
242
243
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800244namespace android {
245
Dave Burked681bbb2011-08-30 14:39:17 +0100246static bool checkPermission(const char* permissionString) {
247#ifndef HAVE_ANDROID_OS
248 return true;
249#endif
250 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
251 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000252 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100253 return ok;
254}
255
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800256// 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 -0800257/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
258/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
259
260void MediaPlayerService::instantiate() {
261 defaultServiceManager()->addService(
262 String16("media.player"), new MediaPlayerService());
263}
264
265MediaPlayerService::MediaPlayerService()
266{
Steve Block3856b092011-10-20 11:56:00 +0100267 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800268 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800269
270 mBatteryAudio.refCount = 0;
271 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
272 mBatteryAudio.deviceOn[i] = 0;
273 mBatteryAudio.lastTime[i] = 0;
274 mBatteryAudio.totalTime[i] = 0;
275 }
276 // speaker is on by default
277 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700278
Chong Zhang0b30fd42014-07-23 14:46:05 -0700279 // reset battery stats
280 // if the mediaserver has crashed, battery stats could be left
281 // in bad state, reset the state upon service start.
282 const sp<IServiceManager> sm(defaultServiceManager());
283 if (sm != NULL) {
284 const String16 name("batterystats");
285 sp<IBatteryStats> batteryStats =
286 interface_cast<IBatteryStats>(sm->getService(name));
287 if (batteryStats != NULL) {
288 batteryStats->noteResetVideo();
289 batteryStats->noteResetAudio();
290 }
291 }
292
John Grossman44a7e422012-06-21 17:29:24 -0700293 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800294}
295
296MediaPlayerService::~MediaPlayerService()
297{
Steve Block3856b092011-10-20 11:56:00 +0100298 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800299}
300
Glenn Kastenf37971f2012-02-03 11:06:53 -0800301sp<IMediaRecorder> MediaPlayerService::createMediaRecorder()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800302{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800303 pid_t pid = IPCThreadState::self()->getCallingPid();
Gloria Wangdac6a312009-10-29 15:46:37 -0700304 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid);
305 wp<MediaRecorderClient> w = recorder;
306 Mutex::Autolock lock(mLock);
307 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100308 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800309 return recorder;
310}
311
Gloria Wangdac6a312009-10-29 15:46:37 -0700312void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
313{
314 Mutex::Autolock lock(mLock);
315 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100316 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700317}
318
Glenn Kastenf37971f2012-02-03 11:06:53 -0800319sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800320{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800321 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800322 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100323 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800324 return retriever;
325}
326
Glenn Kastenf37971f2012-02-03 11:06:53 -0800327sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Dave Burked681bbb2011-08-30 14:39:17 +0100328 int audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800329{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800330 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800331 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700332
333 sp<Client> c = new Client(
334 this, pid, connId, client, audioSessionId,
335 IPCThreadState::self()->getCallingUid());
336
Steve Block3856b092011-10-20 11:56:00 +0100337 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100338 IPCThreadState::self()->getCallingUid());
339
340 wp<Client> w = c;
341 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800342 Mutex::Autolock lock(mLock);
343 mClients.add(w);
344 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800345 return c;
346}
347
Andreas Huber318ad9c2009-10-15 13:46:54 -0700348sp<IOMX> MediaPlayerService::getOMX() {
349 Mutex::Autolock autoLock(mLock);
350
351 if (mOMX.get() == NULL) {
352 mOMX = new OMX;
353 }
354
355 return mOMX;
Andreas Huber20111aa2009-07-14 16:56:47 -0700356}
357
Andreas Hubered3e3e02012-03-26 11:13:27 -0700358sp<ICrypto> MediaPlayerService::makeCrypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700359 return new Crypto;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700360}
361
Jeff Tinkercc82dc62013-02-08 10:18:35 -0800362sp<IDrm> MediaPlayerService::makeDrm() {
363 return new Drm;
364}
365
Andreas Huber279dcd82013-01-30 10:41:25 -0800366sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
367 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700368}
369
Jeff Brown2013a542012-09-04 21:38:42 -0700370sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
371 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700372 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
373 return NULL;
374 }
375
Jeff Brownced24b32012-09-05 17:48:03 -0700376 return new RemoteDisplay(client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700377}
378
Mark Salyzyn77342f72014-06-18 16:31:32 -0700379status_t MediaPlayerService::AudioCache::dump(int fd, const Vector<String16>& /*args*/) const
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800380{
381 const size_t SIZE = 256;
382 char buffer[SIZE];
383 String8 result;
384
385 result.append(" AudioCache\n");
386 if (mHeap != 0) {
Kévin PETIT377b2ec2014-02-03 12:35:36 +0000387 snprintf(buffer, 255, " heap base(%p), size(%zu), flags(%d)\n",
Eric Laurent3d00aa62013-09-24 09:53:27 -0700388 mHeap->getBase(), mHeap->getSize(), mHeap->getFlags());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800389 result.append(buffer);
390 }
Scott Fan7d409692013-04-28 10:13:54 +0800391 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 -0800392 mMsecsPerFrame, mChannelCount, mFormat, mFrameCount);
393 result.append(buffer);
394 snprintf(buffer, 255, " sample rate(%d), size(%d), error(%d), command complete(%s)\n",
395 mSampleRate, mSize, mError, mCommandComplete?"true":"false");
396 result.append(buffer);
397 ::write(fd, result.string(), result.size());
398 return NO_ERROR;
399}
400
401status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
402{
403 const size_t SIZE = 256;
404 char buffer[SIZE];
405 String8 result;
406
407 result.append(" AudioOutput\n");
408 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
409 mStreamType, mLeftVolume, mRightVolume);
410 result.append(buffer);
411 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800412 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800413 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700414 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
415 mAuxEffectId, mSendLevel);
416 result.append(buffer);
417
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800418 ::write(fd, result.string(), result.size());
419 if (mTrack != 0) {
420 mTrack->dump(fd, args);
421 }
422 return NO_ERROR;
423}
424
425status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args) const
426{
427 const size_t SIZE = 256;
428 char buffer[SIZE];
429 String8 result;
430 result.append(" Client\n");
431 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
432 mPid, mConnId, mStatus, mLoop?"true": "false");
433 result.append(buffer);
434 write(fd, result.string(), result.size());
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700435 if (mPlayer != NULL) {
436 mPlayer->dump(fd, args);
437 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800438 if (mAudioOutput != 0) {
439 mAudioOutput->dump(fd, args);
440 }
441 write(fd, "\n", 1);
442 return NO_ERROR;
443}
444
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800445status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
446{
447 const size_t SIZE = 256;
448 char buffer[SIZE];
449 String8 result;
450 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
451 snprintf(buffer, SIZE, "Permission Denial: "
452 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
453 IPCThreadState::self()->getCallingPid(),
454 IPCThreadState::self()->getCallingUid());
455 result.append(buffer);
456 } else {
457 Mutex::Autolock lock(mLock);
458 for (int i = 0, n = mClients.size(); i < n; ++i) {
459 sp<Client> c = mClients[i].promote();
460 if (c != 0) c->dump(fd, args);
461 }
James Dongb9141222010-07-08 11:16:11 -0700462 if (mMediaRecorderClients.size() == 0) {
463 result.append(" No media recorder client\n\n");
464 } else {
465 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
466 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700467 if (c != 0) {
468 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
469 result.append(buffer);
470 write(fd, result.string(), result.size());
471 result = "\n";
472 c->dump(fd, args);
473 }
James Dongb9141222010-07-08 11:16:11 -0700474 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700475 }
476
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800477 result.append(" Files opened and/or mapped:\n");
Glenn Kasten0512ab52011-05-04 17:58:57 -0700478 snprintf(buffer, SIZE, "/proc/%d/maps", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800479 FILE *f = fopen(buffer, "r");
480 if (f) {
481 while (!feof(f)) {
482 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700483 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800484 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700485 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800486 strstr(buffer, " /system/media/")) {
487 result.append(" ");
488 result.append(buffer);
489 }
490 }
491 fclose(f);
492 } else {
493 result.append("couldn't open ");
494 result.append(buffer);
495 result.append("\n");
496 }
497
Glenn Kasten0512ab52011-05-04 17:58:57 -0700498 snprintf(buffer, SIZE, "/proc/%d/fd", gettid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800499 DIR *d = opendir(buffer);
500 if (d) {
501 struct dirent *ent;
502 while((ent = readdir(d)) != NULL) {
503 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Glenn Kasten0512ab52011-05-04 17:58:57 -0700504 snprintf(buffer, SIZE, "/proc/%d/fd/%s", gettid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505 struct stat s;
506 if (lstat(buffer, &s) == 0) {
507 if ((s.st_mode & S_IFMT) == S_IFLNK) {
508 char linkto[256];
509 int len = readlink(buffer, linkto, sizeof(linkto));
510 if(len > 0) {
511 if(len > 255) {
512 linkto[252] = '.';
513 linkto[253] = '.';
514 linkto[254] = '.';
515 linkto[255] = 0;
516 } else {
517 linkto[len] = 0;
518 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700519 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800520 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700521 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800522 strstr(linkto, "/system/media/") == linkto) {
523 result.append(" ");
524 result.append(buffer);
525 result.append(" -> ");
526 result.append(linkto);
527 result.append("\n");
528 }
529 }
530 } else {
531 result.append(" unexpected type for ");
532 result.append(buffer);
533 result.append("\n");
534 }
535 }
536 }
537 }
538 closedir(d);
539 } else {
540 result.append("couldn't open ");
541 result.append(buffer);
542 result.append("\n");
543 }
544
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800545 bool dumpMem = false;
546 for (size_t i = 0; i < args.size(); i++) {
547 if (args[i] == String16("-m")) {
548 dumpMem = true;
549 }
550 }
551 if (dumpMem) {
James Dong8635b7b2011-03-14 17:01:38 -0700552 dumpMemoryAddresses(fd);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 }
555 write(fd, result.string(), result.size());
556 return NO_ERROR;
557}
558
559void MediaPlayerService::removeClient(wp<Client> client)
560{
561 Mutex::Autolock lock(mLock);
562 mClients.remove(client);
563}
564
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700565MediaPlayerService::Client::Client(
566 const sp<MediaPlayerService>& service, pid_t pid,
567 int32_t connId, const sp<IMediaPlayerClient>& client,
568 int audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800569{
Steve Block3856b092011-10-20 11:56:00 +0100570 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800571 mPid = pid;
572 mConnId = connId;
573 mService = service;
574 mClient = client;
575 mLoop = false;
576 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700577 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700578 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800579 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700580 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700581
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800582#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000583 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800584 mAntagonizer = new Antagonizer(notify, this);
585#endif
586}
587
588MediaPlayerService::Client::~Client()
589{
Steve Block3856b092011-10-20 11:56:00 +0100590 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800591 mAudioOutput.clear();
592 wp<Client> client(this);
593 disconnect();
594 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700595 if (mAudioAttributes != NULL) {
596 free(mAudioAttributes);
597 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800598}
599
600void MediaPlayerService::Client::disconnect()
601{
Steve Block3856b092011-10-20 11:56:00 +0100602 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800603 // grab local reference and clear main reference to prevent future
604 // access to object
605 sp<MediaPlayerBase> p;
606 {
607 Mutex::Autolock l(mLock);
608 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800609 mClient.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700611
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800612 mPlayer.clear();
613
614 // clear the notification to prevent callbacks to dead client
615 // and reset the player. We assume the player will serialize
616 // access to itself if necessary.
617 if (p != 0) {
618 p->setNotifyCallback(0, 0);
619#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000620 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621 mAntagonizer->kill();
622#endif
623 p->reset();
624 }
625
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700626 disconnectNativeWindow();
627
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800628 IPCThreadState::self()->flushCommands();
629}
630
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
632{
633 // determine if we have the right player type
634 sp<MediaPlayerBase> p = mPlayer;
635 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100636 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800637 p.clear();
638 }
639 if (p == NULL) {
John Grossman44a7e422012-06-21 17:29:24 -0700640 p = MediaPlayerFactory::createPlayer(playerType, this, notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700642
Jason Simmonsdb29e522011-08-12 13:46:55 -0700643 if (p != NULL) {
644 p->setUID(mUID);
645 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700646
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647 return p;
648}
649
John Grossmanc795b642012-02-22 15:38:35 -0800650sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
651 player_type playerType)
652{
653 ALOGV("player type = %d", playerType);
654
655 // create the right type of player
656 sp<MediaPlayerBase> p = createPlayer(playerType);
657 if (p == NULL) {
658 return p;
659 }
660
661 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800662 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700663 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800664 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
665 }
666
667 return p;
668}
669
670void MediaPlayerService::Client::setDataSource_post(
671 const sp<MediaPlayerBase>& p,
672 status_t status)
673{
674 ALOGV(" setDataSource");
675 mStatus = status;
676 if (mStatus != OK) {
677 ALOGE(" error: %d", mStatus);
678 return;
679 }
680
681 // Set the re-transmission endpoint if one was chosen.
682 if (mRetransmitEndpointValid) {
683 mStatus = p->setRetransmitEndpoint(&mRetransmitEndpoint);
684 if (mStatus != NO_ERROR) {
685 ALOGE("setRetransmitEndpoint error: %d", mStatus);
686 }
687 }
688
689 if (mStatus == OK) {
690 mPlayer = p;
691 }
692}
693
Andreas Huber2db84552010-01-28 11:19:57 -0800694status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800695 const sp<IMediaHTTPService> &httpService,
696 const char *url,
697 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800698{
Steve Block3856b092011-10-20 11:56:00 +0100699 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800700 if (url == NULL)
701 return UNKNOWN_ERROR;
702
Dave Burked681bbb2011-08-30 14:39:17 +0100703 if ((strncmp(url, "http://", 7) == 0) ||
704 (strncmp(url, "https://", 8) == 0) ||
705 (strncmp(url, "rtsp://", 7) == 0)) {
706 if (!checkPermission("android.permission.INTERNET")) {
707 return PERMISSION_DENIED;
708 }
709 }
710
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800711 if (strncmp(url, "content://", 10) == 0) {
712 // get a filedescriptor for the content Uri and
713 // pass it to the setDataSource(fd) method
714
715 String16 url16(url);
716 int fd = android::openContentProviderFile(url16);
717 if (fd < 0)
718 {
Steve Block29357bc2012-01-06 19:20:56 +0000719 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800720 return UNKNOWN_ERROR;
721 }
722 setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
723 close(fd);
724 return mStatus;
725 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700726 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800727 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
728 if (p == NULL) {
729 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800730 }
731
Andreas Huber1b86fe02014-01-29 11:13:26 -0800732 setDataSource_post(p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800733 return mStatus;
734 }
735}
736
737status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
738{
Steve Block3856b092011-10-20 11:56:00 +0100739 ALOGV("setDataSource fd=%d, offset=%lld, length=%lld", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800740 struct stat sb;
741 int ret = fstat(fd, &sb);
742 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000743 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800744 return UNKNOWN_ERROR;
745 }
746
Steve Block3856b092011-10-20 11:56:00 +0100747 ALOGV("st_dev = %llu", sb.st_dev);
748 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700749 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
750 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Steve Block3856b092011-10-20 11:56:00 +0100751 ALOGV("st_size = %llu", sb.st_size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800752
753 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000754 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800755 ::close(fd);
756 return UNKNOWN_ERROR;
757 }
758 if (offset + length > sb.st_size) {
759 length = sb.st_size - offset;
Steve Block3856b092011-10-20 11:56:00 +0100760 ALOGV("calculated length = %lld", length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800761 }
762
John Grossman44a7e422012-06-21 17:29:24 -0700763 player_type playerType = MediaPlayerFactory::getPlayerType(this,
764 fd,
765 offset,
766 length);
John Grossmanc795b642012-02-22 15:38:35 -0800767 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
768 if (p == NULL) {
769 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800770 }
771
772 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800773 setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800774 return mStatus;
775}
776
Andreas Hubere2b10282010-11-23 11:41:34 -0800777status_t MediaPlayerService::Client::setDataSource(
778 const sp<IStreamSource> &source) {
779 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700780 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800781 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800782 if (p == NULL) {
783 return NO_INIT;
784 }
785
Andreas Hubere2b10282010-11-23 11:41:34 -0800786 // now set data source
John Grossmanc795b642012-02-22 15:38:35 -0800787 setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800788 return mStatus;
789}
790
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700791void MediaPlayerService::Client::disconnectNativeWindow() {
792 if (mConnectedWindow != NULL) {
793 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
794 NATIVE_WINDOW_API_MEDIA);
795
796 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000797 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700798 strerror(-err), err);
799 }
800 }
801 mConnectedWindow.clear();
802}
803
Glenn Kasten11731182011-02-08 17:26:17 -0800804status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800805 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800806{
Andy McFadden484566c2012-12-18 09:46:54 -0800807 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800808 sp<MediaPlayerBase> p = getPlayer();
809 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700810
Andy McFadden484566c2012-12-18 09:46:54 -0800811 sp<IBinder> binder(bufferProducer == NULL ? NULL :
812 bufferProducer->asBinder());
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700813 if (mConnectedWindowBinder == binder) {
814 return OK;
815 }
816
817 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800818 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700819 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700820 status_t err = native_window_api_connect(anw.get(),
821 NATIVE_WINDOW_API_MEDIA);
822
823 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000824 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700825 // Note that we must do the reset before disconnecting from the ANW.
826 // Otherwise queue/dequeue calls could be made on the disconnected
827 // ANW, which may result in errors.
828 reset();
829
830 disconnectNativeWindow();
831
832 return err;
833 }
834 }
835
Andy McFadden484566c2012-12-18 09:46:54 -0800836 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700837 // disconnecting the old one. Otherwise queue/dequeue calls could be made
838 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800839 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700840
841 disconnectNativeWindow();
842
843 mConnectedWindow = anw;
844
845 if (err == OK) {
846 mConnectedWindowBinder = binder;
847 } else {
848 disconnectNativeWindow();
849 }
850
851 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800852}
853
Nicolas Catania1d187f12009-05-12 23:25:55 -0700854status_t MediaPlayerService::Client::invoke(const Parcel& request,
855 Parcel *reply)
856{
857 sp<MediaPlayerBase> p = getPlayer();
858 if (p == NULL) return UNKNOWN_ERROR;
859 return p->invoke(request, reply);
860}
861
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700862// This call doesn't need to access the native player.
863status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
864{
865 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700866 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700867
Nicolas Catania48290382009-07-10 13:53:06 -0700868 if (unmarshallFilter(filter, &allow, &status) &&
869 unmarshallFilter(filter, &drop, &status)) {
870 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700871
872 mMetadataAllow = allow;
873 mMetadataDrop = drop;
874 }
875 return status;
876}
877
Nicolas Catania48290382009-07-10 13:53:06 -0700878status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700879 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700880{
nikoa64c8c72009-07-20 15:07:26 -0700881 sp<MediaPlayerBase> player = getPlayer();
882 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700883
nikod608a812009-07-16 16:39:53 -0700884 status_t status;
885 // Placeholder for the return code, updated by the caller.
886 reply->writeInt32(-1);
887
nikoa64c8c72009-07-20 15:07:26 -0700888 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700889
890 // We don't block notifications while we fetch the data. We clear
891 // mMetadataUpdated first so we don't lose notifications happening
892 // during the rest of this call.
893 {
894 Mutex::Autolock lock(mLock);
895 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700896 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700897 }
898 mMetadataUpdated.clear();
899 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700900
nikoa64c8c72009-07-20 15:07:26 -0700901 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700902
nikoa64c8c72009-07-20 15:07:26 -0700903 metadata.appendHeader();
904 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700905
906 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700907 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000908 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700909 return status;
910 }
911
912 // FIXME: Implement filtering on the result. Not critical since
913 // filtering takes place on the update notifications already. This
914 // would be when all the metadata are fetch and a filter is set.
915
nikod608a812009-07-16 16:39:53 -0700916 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -0700917 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -0700918 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700919}
920
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800921status_t MediaPlayerService::Client::prepareAsync()
922{
Steve Block3856b092011-10-20 11:56:00 +0100923 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800924 sp<MediaPlayerBase> p = getPlayer();
925 if (p == 0) return UNKNOWN_ERROR;
926 status_t ret = p->prepareAsync();
927#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000928 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800929 if (ret == NO_ERROR) mAntagonizer->start();
930#endif
931 return ret;
932}
933
934status_t MediaPlayerService::Client::start()
935{
Steve Block3856b092011-10-20 11:56:00 +0100936 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800937 sp<MediaPlayerBase> p = getPlayer();
938 if (p == 0) return UNKNOWN_ERROR;
939 p->setLooping(mLoop);
940 return p->start();
941}
942
943status_t MediaPlayerService::Client::stop()
944{
Steve Block3856b092011-10-20 11:56:00 +0100945 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800946 sp<MediaPlayerBase> p = getPlayer();
947 if (p == 0) return UNKNOWN_ERROR;
948 return p->stop();
949}
950
951status_t MediaPlayerService::Client::pause()
952{
Steve Block3856b092011-10-20 11:56:00 +0100953 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800954 sp<MediaPlayerBase> p = getPlayer();
955 if (p == 0) return UNKNOWN_ERROR;
956 return p->pause();
957}
958
959status_t MediaPlayerService::Client::isPlaying(bool* state)
960{
961 *state = false;
962 sp<MediaPlayerBase> p = getPlayer();
963 if (p == 0) return UNKNOWN_ERROR;
964 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +0100965 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800966 return NO_ERROR;
967}
968
969status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
970{
Steve Block3856b092011-10-20 11:56:00 +0100971 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800972 sp<MediaPlayerBase> p = getPlayer();
973 if (p == 0) return UNKNOWN_ERROR;
974 status_t ret = p->getCurrentPosition(msec);
975 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100976 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800977 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000978 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800979 }
980 return ret;
981}
982
983status_t MediaPlayerService::Client::getDuration(int *msec)
984{
Steve Block3856b092011-10-20 11:56:00 +0100985 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800986 sp<MediaPlayerBase> p = getPlayer();
987 if (p == 0) return UNKNOWN_ERROR;
988 status_t ret = p->getDuration(msec);
989 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +0100990 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800991 } else {
Steve Block29357bc2012-01-06 19:20:56 +0000992 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800993 }
994 return ret;
995}
996
Marco Nelissen6b74d672012-02-28 16:07:44 -0800997status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
998 ALOGV("setNextPlayer");
999 Mutex::Autolock l(mLock);
1000 sp<Client> c = static_cast<Client*>(player.get());
1001 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001002
1003 if (c != NULL) {
1004 if (mAudioOutput != NULL) {
1005 mAudioOutput->setNextOutput(c->mAudioOutput);
1006 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1007 ALOGE("no current audio output");
1008 }
1009
1010 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1011 mPlayer->setNextPlayer(mNextClient->getPlayer());
1012 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001013 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001014
Marco Nelissen6b74d672012-02-28 16:07:44 -08001015 return OK;
1016}
1017
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001018status_t MediaPlayerService::Client::seekTo(int msec)
1019{
Steve Block3856b092011-10-20 11:56:00 +01001020 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001021 sp<MediaPlayerBase> p = getPlayer();
1022 if (p == 0) return UNKNOWN_ERROR;
1023 return p->seekTo(msec);
1024}
1025
1026status_t MediaPlayerService::Client::reset()
1027{
Steve Block3856b092011-10-20 11:56:00 +01001028 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001029 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001030 sp<MediaPlayerBase> p = getPlayer();
1031 if (p == 0) return UNKNOWN_ERROR;
1032 return p->reset();
1033}
1034
Glenn Kastenfff6d712012-01-12 16:38:12 -08001035status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001036{
Steve Block3856b092011-10-20 11:56:00 +01001037 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001038 // TODO: for hardware output, call player instead
1039 Mutex::Autolock l(mLock);
1040 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1041 return NO_ERROR;
1042}
1043
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001044status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1045{
1046 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1047 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1048 unmarshallAudioAttributes(parcel, mAudioAttributes);
1049
1050 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1051 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1052 mAudioAttributes->tags);
1053
1054 if (mAudioOutput != 0) {
1055 mAudioOutput->setAudioAttributes(mAudioAttributes);
1056 }
1057 return NO_ERROR;
1058}
1059
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001060status_t MediaPlayerService::Client::setLooping(int loop)
1061{
Steve Block3856b092011-10-20 11:56:00 +01001062 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001063 mLoop = loop;
1064 sp<MediaPlayerBase> p = getPlayer();
1065 if (p != 0) return p->setLooping(loop);
1066 return NO_ERROR;
1067}
1068
1069status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1070{
Steve Block3856b092011-10-20 11:56:00 +01001071 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001072
1073 // for hardware output, call player instead
1074 sp<MediaPlayerBase> p = getPlayer();
1075 {
1076 Mutex::Autolock l(mLock);
1077 if (p != 0 && p->hardwareOutput()) {
1078 MediaPlayerHWInterface* hwp =
1079 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1080 return hwp->setVolume(leftVolume, rightVolume);
1081 } else {
1082 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1083 return NO_ERROR;
1084 }
1085 }
1086
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001087 return NO_ERROR;
1088}
1089
Eric Laurent2beeb502010-07-16 07:43:46 -07001090status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1091{
Steve Block3856b092011-10-20 11:56:00 +01001092 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001093 Mutex::Autolock l(mLock);
1094 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1095 return NO_ERROR;
1096}
1097
1098status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1099{
Steve Block3856b092011-10-20 11:56:00 +01001100 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001101 Mutex::Autolock l(mLock);
1102 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1103 return NO_ERROR;
1104}
Nicolas Catania48290382009-07-10 13:53:06 -07001105
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001106status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001108 switch (key) {
1109 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1110 {
1111 Mutex::Autolock l(mLock);
1112 return setAudioAttributes_l(request);
1113 }
1114 default:
1115 sp<MediaPlayerBase> p = getPlayer();
1116 if (p == 0) { return UNKNOWN_ERROR; }
1117 return p->setParameter(key, request);
1118 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001119}
1120
1121status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001122 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001123 sp<MediaPlayerBase> p = getPlayer();
1124 if (p == 0) return UNKNOWN_ERROR;
1125 return p->getParameter(key, reply);
1126}
1127
John Grossmanc795b642012-02-22 15:38:35 -08001128status_t MediaPlayerService::Client::setRetransmitEndpoint(
1129 const struct sockaddr_in* endpoint) {
1130
1131 if (NULL != endpoint) {
1132 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1133 uint16_t p = ntohs(endpoint->sin_port);
1134 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1135 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1136 } else {
1137 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1138 }
1139
1140 sp<MediaPlayerBase> p = getPlayer();
1141
1142 // Right now, the only valid time to set a retransmit endpoint is before
1143 // player selection has been made (since the presence or absence of a
1144 // retransmit endpoint is going to determine which player is selected during
1145 // setDataSource).
1146 if (p != 0) return INVALID_OPERATION;
1147
1148 if (NULL != endpoint) {
1149 mRetransmitEndpoint = *endpoint;
1150 mRetransmitEndpointValid = true;
1151 } else {
1152 mRetransmitEndpointValid = false;
1153 }
1154
1155 return NO_ERROR;
1156}
1157
John Grossman44a7e422012-06-21 17:29:24 -07001158status_t MediaPlayerService::Client::getRetransmitEndpoint(
1159 struct sockaddr_in* endpoint)
1160{
1161 if (NULL == endpoint)
1162 return BAD_VALUE;
1163
1164 sp<MediaPlayerBase> p = getPlayer();
1165
1166 if (p != NULL)
1167 return p->getRetransmitEndpoint(endpoint);
1168
1169 if (!mRetransmitEndpointValid)
1170 return NO_INIT;
1171
1172 *endpoint = mRetransmitEndpoint;
1173
1174 return NO_ERROR;
1175}
1176
Gloria Wangb483c472011-04-11 17:23:27 -07001177void MediaPlayerService::Client::notify(
1178 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001179{
1180 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001181 if (client == NULL) {
1182 return;
1183 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001184
James Dongb8a98252012-08-26 16:13:03 -07001185 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001186 {
1187 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001188 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001189 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001190 if (client->mAudioOutput != NULL)
1191 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001192 client->mNextClient->start();
1193 client->mNextClient->mClient->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1194 }
1195 }
1196
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001197 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001198 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001199 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001200
1201 if(client->shouldDropMetadata(metadata_type)) {
1202 return;
1203 }
1204
1205 // Update the list of metadata that have changed. getMetadata
1206 // also access mMetadataUpdated and clears it.
1207 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001208 }
James Dongb8a98252012-08-26 16:13:03 -07001209
1210 if (c != NULL) {
1211 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1212 c->notify(msg, ext1, ext2, obj);
1213 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001214}
1215
Nicolas Catania48290382009-07-10 13:53:06 -07001216
nikoa64c8c72009-07-20 15:07:26 -07001217bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001218{
Nicolas Catania48290382009-07-10 13:53:06 -07001219 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001220
Nicolas Catania48290382009-07-10 13:53:06 -07001221 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001222 return true;
1223 }
1224
Nicolas Catania48290382009-07-10 13:53:06 -07001225 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001226 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001227 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001228 return true;
1229 }
1230}
1231
Nicolas Catania48290382009-07-10 13:53:06 -07001232
nikoa64c8c72009-07-20 15:07:26 -07001233void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001234 Mutex::Autolock lock(mLock);
1235 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1236 mMetadataUpdated.add(metadata_type);
1237 }
1238}
1239
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001240#if CALLBACK_ANTAGONIZER
1241const int Antagonizer::interval = 10000; // 10 msecs
1242
1243Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1244 mExit(false), mActive(false), mClient(client), mCb(cb)
1245{
1246 createThread(callbackThread, this);
1247}
1248
1249void Antagonizer::kill()
1250{
1251 Mutex::Autolock _l(mLock);
1252 mActive = false;
1253 mExit = true;
1254 mCondition.wait(mLock);
1255}
1256
1257int Antagonizer::callbackThread(void* user)
1258{
Steve Blockb8a80522011-12-20 16:23:08 +00001259 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001260 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1261 while (!p->mExit) {
1262 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001263 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001264 p->mCb(p->mClient, 0, 0, 0);
1265 }
1266 usleep(interval);
1267 }
1268 Mutex::Autolock _l(p->mLock);
1269 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001270 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001271 return 0;
1272}
1273#endif
1274
Andreas Huber1b86fe02014-01-29 11:13:26 -08001275status_t MediaPlayerService::decode(
1276 const sp<IMediaHTTPService> &httpService,
1277 const char* url,
1278 uint32_t *pSampleRate,
1279 int* pNumChannels,
1280 audio_format_t* pFormat,
1281 const sp<IMemoryHeap>& heap,
1282 size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001283{
Steve Block3856b092011-10-20 11:56:00 +01001284 ALOGV("decode(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001285 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001286 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001287
1288 // Protect our precious, precious DRMd ringtones by only allowing
1289 // decoding of http, but not filesystem paths or content Uris.
1290 // If the application wants to decode those, it should open a
1291 // filedescriptor for them and use that.
1292 if (url != NULL && strncmp(url, "http://", 7) != 0) {
Steve Blockb8a80522011-12-20 16:23:08 +00001293 ALOGD("Can't decode %s by path, use filedescriptor instead", url);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001294 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001295 }
1296
John Grossman44a7e422012-06-21 17:29:24 -07001297 player_type playerType =
1298 MediaPlayerFactory::getPlayerType(NULL /* client */, url);
Steve Block3856b092011-10-20 11:56:00 +01001299 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001300
1301 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001302 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001303 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001304 if (player == NULL) goto Exit;
1305 if (player->hardwareOutput()) goto Exit;
1306
1307 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1308
1309 // set data source
Andreas Huber1b86fe02014-01-29 11:13:26 -08001310 if (player->setDataSource(httpService, url) != NO_ERROR) goto Exit;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001311
Steve Block3856b092011-10-20 11:56:00 +01001312 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001313 player->prepareAsync();
1314
Steve Block3856b092011-10-20 11:56:00 +01001315 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001316 if (cache->wait() != NO_ERROR) goto Exit;
1317
Steve Block3856b092011-10-20 11:56:00 +01001318 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001319 player->start();
1320
Steve Block3856b092011-10-20 11:56:00 +01001321 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001322 cache->wait();
1323 // in case of error, return what was successfully decoded.
1324 if (cache->size() == 0) {
1325 goto Exit;
1326 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001327
Eric Laurent3d00aa62013-09-24 09:53:27 -07001328 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001329 *pSampleRate = cache->sampleRate();
1330 *pNumChannels = cache->channelCount();
Glenn Kastene1c39622012-01-04 09:36:37 -08001331 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001332 ALOGV("return size %d sampleRate=%u, channelCount = %d, format = %d",
1333 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1334 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001335
1336Exit:
1337 if (player != 0) player->reset();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001338 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001339}
1340
Eric Laurent3d00aa62013-09-24 09:53:27 -07001341status_t MediaPlayerService::decode(int fd, int64_t offset, int64_t length,
1342 uint32_t *pSampleRate, int* pNumChannels,
1343 audio_format_t* pFormat,
1344 const sp<IMemoryHeap>& heap, size_t *pSize)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001345{
Steve Block3856b092011-10-20 11:56:00 +01001346 ALOGV("decode(%d, %lld, %lld)", fd, offset, length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001347 sp<MediaPlayerBase> player;
Eric Laurent3d00aa62013-09-24 09:53:27 -07001348 status_t status = BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001349
John Grossman44a7e422012-06-21 17:29:24 -07001350 player_type playerType = MediaPlayerFactory::getPlayerType(NULL /* client */,
1351 fd,
1352 offset,
1353 length);
Steve Block3856b092011-10-20 11:56:00 +01001354 ALOGV("player type = %d", playerType);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001355
1356 // create the right type of player
Eric Laurent3d00aa62013-09-24 09:53:27 -07001357 sp<AudioCache> cache = new AudioCache(heap);
John Grossman44a7e422012-06-21 17:29:24 -07001358 player = MediaPlayerFactory::createPlayer(playerType, cache.get(), cache->notify);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001359 if (player == NULL) goto Exit;
1360 if (player->hardwareOutput()) goto Exit;
1361
1362 static_cast<MediaPlayerInterface*>(player.get())->setAudioSink(cache);
1363
1364 // set data source
1365 if (player->setDataSource(fd, offset, length) != NO_ERROR) goto Exit;
1366
Steve Block3856b092011-10-20 11:56:00 +01001367 ALOGV("prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001368 player->prepareAsync();
1369
Steve Block3856b092011-10-20 11:56:00 +01001370 ALOGV("wait for prepare");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001371 if (cache->wait() != NO_ERROR) goto Exit;
1372
Steve Block3856b092011-10-20 11:56:00 +01001373 ALOGV("start");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001374 player->start();
1375
Steve Block3856b092011-10-20 11:56:00 +01001376 ALOGV("wait for playback complete");
Eric Laurent9cb839a2011-09-27 09:48:56 -07001377 cache->wait();
1378 // in case of error, return what was successfully decoded.
1379 if (cache->size() == 0) {
1380 goto Exit;
1381 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001382
Eric Laurent3d00aa62013-09-24 09:53:27 -07001383 *pSize = cache->size();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001384 *pSampleRate = cache->sampleRate();
1385 *pNumChannels = cache->channelCount();
1386 *pFormat = cache->format();
Eric Laurent3d00aa62013-09-24 09:53:27 -07001387 ALOGV("return size %d, sampleRate=%u, channelCount = %d, format = %d",
1388 *pSize, *pSampleRate, *pNumChannels, *pFormat);
1389 status = NO_ERROR;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390
1391Exit:
1392 if (player != 0) player->reset();
1393 ::close(fd);
Eric Laurent3d00aa62013-09-24 09:53:27 -07001394 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001395}
1396
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001397
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001398#undef LOG_TAG
1399#define LOG_TAG "AudioSink"
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001400MediaPlayerService::AudioOutput::AudioOutput(int sessionId, int uid, int pid,
1401 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001402 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001403 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001404 mCallbackData(NULL),
Marco Nelissen4110c102012-03-29 09:31:28 -07001405 mBytesWritten(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001406 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001407 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001408 mPid(pid),
Eric Laurent1948eb32012-04-13 16:50:19 -07001409 mFlags(AUDIO_OUTPUT_FLAG_NONE) {
Steve Block3856b092011-10-20 11:56:00 +01001410 ALOGV("AudioOutput(%d)", sessionId);
Dima Zavinfce7a472011-04-19 22:30:36 -07001411 mStreamType = AUDIO_STREAM_MUSIC;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001412 mLeftVolume = 1.0;
1413 mRightVolume = 1.0;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001414 mPlaybackRatePermille = 1000;
1415 mSampleRateHz = 0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001416 mMsecsPerFrame = 0;
Eric Laurent2beeb502010-07-16 07:43:46 -07001417 mAuxEffectId = 0;
1418 mSendLevel = 0.0;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001419 setMinBufferCount();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001420 mAttributes = attr;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001421}
1422
1423MediaPlayerService::AudioOutput::~AudioOutput()
1424{
1425 close();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001426 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001427}
1428
1429void MediaPlayerService::AudioOutput::setMinBufferCount()
1430{
1431 char value[PROPERTY_VALUE_MAX];
1432 if (property_get("ro.kernel.qemu", value, 0)) {
1433 mIsOnEmulator = true;
1434 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1435 }
1436}
1437
1438bool MediaPlayerService::AudioOutput::isOnEmulator()
1439{
1440 setMinBufferCount();
1441 return mIsOnEmulator;
1442}
1443
1444int MediaPlayerService::AudioOutput::getMinBufferCount()
1445{
1446 setMinBufferCount();
1447 return mMinBufferCount;
1448}
1449
1450ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1451{
1452 if (mTrack == 0) return NO_INIT;
1453 return mTrack->frameCount() * frameSize();
1454}
1455
1456ssize_t MediaPlayerService::AudioOutput::frameCount() const
1457{
1458 if (mTrack == 0) return NO_INIT;
1459 return mTrack->frameCount();
1460}
1461
1462ssize_t MediaPlayerService::AudioOutput::channelCount() const
1463{
1464 if (mTrack == 0) return NO_INIT;
1465 return mTrack->channelCount();
1466}
1467
1468ssize_t MediaPlayerService::AudioOutput::frameSize() const
1469{
1470 if (mTrack == 0) return NO_INIT;
1471 return mTrack->frameSize();
1472}
1473
1474uint32_t MediaPlayerService::AudioOutput::latency () const
1475{
Eric Laurentdb354e52012-03-05 17:27:11 -08001476 if (mTrack == 0) return 0;
1477 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001478}
1479
1480float MediaPlayerService::AudioOutput::msecsPerFrame() const
1481{
1482 return mMsecsPerFrame;
1483}
1484
Marco Nelissen4110c102012-03-29 09:31:28 -07001485status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001486{
1487 if (mTrack == 0) return NO_INIT;
1488 return mTrack->getPosition(position);
1489}
1490
Marco Nelissen4110c102012-03-29 09:31:28 -07001491status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1492{
1493 if (mTrack == 0) return NO_INIT;
1494 *frameswritten = mBytesWritten / frameSize();
1495 return OK;
1496}
1497
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001498status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1499{
1500 if (mTrack == 0) return NO_INIT;
1501 return mTrack->setParameters(keyValuePairs);
1502}
1503
1504String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1505{
1506 if (mTrack == 0) return String8::empty();
1507 return mTrack->getParameters(keys);
1508}
1509
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001510void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1511 mAttributes = attributes;
1512}
1513
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001514void MediaPlayerService::AudioOutput::deleteRecycledTrack()
1515{
1516 ALOGV("deleteRecycledTrack");
1517
1518 if (mRecycledTrack != 0) {
1519
1520 if (mCallbackData != NULL) {
1521 mCallbackData->setOutput(NULL);
1522 mCallbackData->endTrackSwitch();
1523 }
1524
1525 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1526 mRecycledTrack->flush();
1527 }
1528 // An offloaded track isn't flushed because the STREAM_END is reported
1529 // slightly prematurely to allow time for the gapless track switch
1530 // but this means that if we decide not to recycle the track there
1531 // could be a small amount of residual data still playing. We leave
1532 // AudioFlinger to drain the track.
1533
1534 mRecycledTrack.clear();
1535 delete mCallbackData;
1536 mCallbackData = NULL;
1537 close();
1538 }
1539}
1540
Andreas Huber20111aa2009-07-14 16:56:47 -07001541status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001542 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1543 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001544 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001545 audio_output_flags_t flags,
1546 const audio_offload_info_t *offloadInfo)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001547{
Andreas Huber20111aa2009-07-14 16:56:47 -07001548 mCallback = cb;
1549 mCallbackCookie = cookie;
1550
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001551 // Check argument "bufferCount" against the mininum buffer count
1552 if (bufferCount < mMinBufferCount) {
Steve Blockb8a80522011-12-20 16:23:08 +00001553 ALOGD("bufferCount (%d) is too small and increased to %d", bufferCount, mMinBufferCount);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001554 bufferCount = mMinBufferCount;
1555
1556 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001557 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1558 format, bufferCount, mSessionId, flags);
Glenn Kasten1127d652012-11-14 08:44:39 -08001559 uint32_t afSampleRate;
Glenn Kasten7da35f22012-11-14 12:54:39 -08001560 size_t afFrameCount;
Glenn Kastenbce50bf2014-02-27 15:29:51 -08001561 size_t frameCount;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001562
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001563 // offloading is only supported in callback mode for now.
1564 // offloadInfo must be present if offload flag is set
1565 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1566 ((cb == NULL) || (offloadInfo == NULL))) {
1567 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001568 }
1569
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001570 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1571 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1572 } else {
1573 uint32_t afSampleRate;
1574 size_t afFrameCount;
1575
1576 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1577 return NO_INIT;
1578 }
1579 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1580 return NO_INIT;
1581 }
1582
1583 frameCount = (sampleRate*afFrameCount*bufferCount)/afSampleRate;
1584 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001585
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001586 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001587 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001588 if (0 == channelMask) {
1589 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1590 return NO_INIT;
1591 }
1592 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001593
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001594 // Check whether we can recycle the track
1595 bool reuse = false;
1596 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001597
Glenn Kasten2799d742013-05-30 14:33:29 -07001598 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001599 // check whether we are switching between two offloaded tracks
1600 bothOffloaded = (flags & mRecycledTrack->getFlags()
1601 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001602
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001603 // check if the existing track can be reused as-is, or if a new track needs to be created.
1604 reuse = true;
1605
Marco Nelissen67295b52012-06-11 14:52:53 -07001606 if ((mCallbackData == NULL && mCallback != NULL) ||
1607 (mCallbackData != NULL && mCallback == NULL)) {
1608 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1609 ALOGV("can't chain callback and write");
1610 reuse = false;
1611 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001612 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1613 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001614 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001615 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001616 reuse = false;
1617 } else if (flags != mFlags) {
1618 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1619 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001620 } else if (mRecycledTrack->format() != format) {
1621 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001622 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001623 } else {
1624 ALOGV("no track available to recycle");
1625 }
1626
1627 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1628
1629 // If we can't recycle and both tracks are offloaded
1630 // we must close the previous output before opening a new one
1631 if (bothOffloaded && !reuse) {
1632 ALOGV("both offloaded and not recycling");
1633 deleteRecycledTrack();
1634 }
1635
1636 sp<AudioTrack> t;
1637 CallbackData *newcbd = NULL;
1638
1639 // We don't attempt to create a new track if we are recycling an
1640 // offloaded track. But, if we are recycling a non-offloaded or we
1641 // are switching where one is offloaded and one isn't then we create
1642 // the new track in advance so that we can read additional stream info
1643
1644 if (!(reuse && bothOffloaded)) {
1645 ALOGV("creating new AudioTrack");
1646
1647 if (mCallback != NULL) {
1648 newcbd = new CallbackData(this);
1649 t = new AudioTrack(
1650 mStreamType,
1651 sampleRate,
1652 format,
1653 channelMask,
1654 frameCount,
1655 flags,
1656 CallbackWrapper,
1657 newcbd,
1658 0, // notification frames
1659 mSessionId,
1660 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001661 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001662 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001663 mPid,
1664 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001665 } else {
1666 t = new AudioTrack(
1667 mStreamType,
1668 sampleRate,
1669 format,
1670 channelMask,
1671 frameCount,
1672 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001673 NULL, // callback
1674 NULL, // user data
1675 0, // notification frames
1676 mSessionId,
1677 AudioTrack::TRANSFER_DEFAULT,
1678 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001679 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001680 mPid,
1681 mAttributes);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001682 }
1683
1684 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1685 ALOGE("Unable to create audio track");
1686 delete newcbd;
1687 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001688 } else {
1689 // successful AudioTrack initialization implies a legacy stream type was generated
1690 // from the audio attributes
1691 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001692 }
1693 }
1694
1695 if (reuse) {
1696 CHECK(mRecycledTrack != NULL);
1697
1698 if (!bothOffloaded) {
1699 if (mRecycledTrack->frameCount() != t->frameCount()) {
1700 ALOGV("framecount differs: %u/%u frames",
1701 mRecycledTrack->frameCount(), t->frameCount());
1702 reuse = false;
1703 }
1704 }
1705
Marco Nelissen67295b52012-06-11 14:52:53 -07001706 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001707 ALOGV("chaining to next output and recycling track");
Marco Nelissen67295b52012-06-11 14:52:53 -07001708 close();
1709 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001710 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001711 if (mCallbackData != NULL) {
1712 mCallbackData->setOutput(this);
1713 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001714 delete newcbd;
1715 return OK;
1716 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001717 }
1718
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001719 // we're not going to reuse the track, unblock and flush it
1720 // this was done earlier if both tracks are offloaded
1721 if (!bothOffloaded) {
1722 deleteRecycledTrack();
1723 }
1724
1725 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1726
Marco Nelissen67295b52012-06-11 14:52:53 -07001727 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001728 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001729 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001730
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001731 mSampleRateHz = sampleRate;
Eric Laurent1948eb32012-04-13 16:50:19 -07001732 mFlags = flags;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001733 mMsecsPerFrame = mPlaybackRatePermille / (float) sampleRate;
Marco Nelissen99448602012-04-02 12:16:49 -07001734 uint32_t pos;
1735 if (t->getPosition(&pos) == OK) {
1736 mBytesWritten = uint64_t(pos) * t->frameSize();
1737 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001738 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001739
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001740 status_t res = NO_ERROR;
1741 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1742 res = t->setSampleRate(mPlaybackRatePermille * mSampleRateHz / 1000);
1743 if (res == NO_ERROR) {
1744 t->setAuxEffectSendLevel(mSendLevel);
1745 res = t->attachAuxEffect(mAuxEffectId);
1746 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001747 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001748 ALOGV("open() DONE status %d", res);
1749 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001750}
1751
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001752status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001753{
Steve Block3856b092011-10-20 11:56:00 +01001754 ALOGV("start");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001755 if (mCallbackData != NULL) {
1756 mCallbackData->endTrackSwitch();
1757 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001758 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001759 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001760 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001761 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001762 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001763 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001764}
1765
Marco Nelissen6b74d672012-02-28 16:07:44 -08001766void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
1767 mNextOutput = nextOutput;
1768}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001769
1770
Marco Nelissen6b74d672012-02-28 16:07:44 -08001771void MediaPlayerService::AudioOutput::switchToNextOutput() {
1772 ALOGV("switchToNextOutput");
1773 if (mNextOutput != NULL) {
1774 if (mCallbackData != NULL) {
1775 mCallbackData->beginTrackSwitch();
1776 }
1777 delete mNextOutput->mCallbackData;
1778 mNextOutput->mCallbackData = mCallbackData;
1779 mCallbackData = NULL;
1780 mNextOutput->mRecycledTrack = mTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001781 mTrack.clear();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001782 mNextOutput->mSampleRateHz = mSampleRateHz;
1783 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Marco Nelissen4110c102012-03-29 09:31:28 -07001784 mNextOutput->mBytesWritten = mBytesWritten;
Marco Nelissend791e092012-06-11 17:00:59 -07001785 mNextOutput->mFlags = mFlags;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001786 }
1787}
1788
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001789ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size)
1790{
Glenn Kastenadad3d72014-02-21 14:51:43 -08001791 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07001792
Steve Block3856b092011-10-20 11:56:00 +01001793 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07001794 if (mTrack != 0) {
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001795 ssize_t ret = mTrack->write(buffer, size);
Marco Nelissen4110c102012-03-29 09:31:28 -07001796 mBytesWritten += ret;
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07001797 return ret;
1798 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001799 return NO_INIT;
1800}
1801
1802void MediaPlayerService::AudioOutput::stop()
1803{
Steve Block3856b092011-10-20 11:56:00 +01001804 ALOGV("stop");
Glenn Kasten2799d742013-05-30 14:33:29 -07001805 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001806}
1807
1808void MediaPlayerService::AudioOutput::flush()
1809{
Steve Block3856b092011-10-20 11:56:00 +01001810 ALOGV("flush");
Glenn Kasten2799d742013-05-30 14:33:29 -07001811 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812}
1813
1814void MediaPlayerService::AudioOutput::pause()
1815{
Steve Block3856b092011-10-20 11:56:00 +01001816 ALOGV("pause");
Glenn Kasten2799d742013-05-30 14:33:29 -07001817 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001818}
1819
1820void MediaPlayerService::AudioOutput::close()
1821{
Steve Block3856b092011-10-20 11:56:00 +01001822 ALOGV("close");
Glenn Kasten2799d742013-05-30 14:33:29 -07001823 mTrack.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001824}
1825
1826void MediaPlayerService::AudioOutput::setVolume(float left, float right)
1827{
Steve Block3856b092011-10-20 11:56:00 +01001828 ALOGV("setVolume(%f, %f)", left, right);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001829 mLeftVolume = left;
1830 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07001831 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001832 mTrack->setVolume(left, right);
1833 }
1834}
1835
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001836status_t MediaPlayerService::AudioOutput::setPlaybackRatePermille(int32_t ratePermille)
1837{
1838 ALOGV("setPlaybackRatePermille(%d)", ratePermille);
1839 status_t res = NO_ERROR;
Glenn Kasten2799d742013-05-30 14:33:29 -07001840 if (mTrack != 0) {
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001841 res = mTrack->setSampleRate(ratePermille * mSampleRateHz / 1000);
1842 } else {
1843 res = NO_INIT;
1844 }
1845 mPlaybackRatePermille = ratePermille;
1846 if (mSampleRateHz != 0) {
1847 mMsecsPerFrame = mPlaybackRatePermille / (float) mSampleRateHz;
1848 }
1849 return res;
1850}
1851
Eric Laurent2beeb502010-07-16 07:43:46 -07001852status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
1853{
Steve Block3856b092011-10-20 11:56:00 +01001854 ALOGV("setAuxEffectSendLevel(%f)", level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001855 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07001856 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001857 return mTrack->setAuxEffectSendLevel(level);
1858 }
1859 return NO_ERROR;
1860}
1861
1862status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
1863{
Steve Block3856b092011-10-20 11:56:00 +01001864 ALOGV("attachAuxEffect(%d)", effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001865 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07001866 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07001867 return mTrack->attachAuxEffect(effectId);
1868 }
1869 return NO_ERROR;
1870}
1871
Andreas Huber20111aa2009-07-14 16:56:47 -07001872// static
1873void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07001874 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01001875 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08001876 CallbackData *data = (CallbackData*)cookie;
1877 data->lock();
1878 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07001879 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001880 if (me == NULL) {
1881 // no output set, likely because the track was scheduled to be reused
1882 // by another player, but the format turned out to be incompatible.
1883 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001884 if (buffer != NULL) {
1885 buffer->size = 0;
1886 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001887 return;
1888 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001889
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001890 switch(event) {
1891 case AudioTrack::EVENT_MORE_DATA: {
1892 size_t actualSize = (*me->mCallback)(
1893 me, buffer->raw, buffer->size, me->mCallbackCookie,
1894 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001895
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001896 if (actualSize == 0 && buffer->size > 0 && me->mNextOutput == NULL) {
1897 // We've reached EOS but the audio track is not stopped yet,
1898 // keep playing silence.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08001899
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001900 memset(buffer->raw, 0, buffer->size);
1901 actualSize = buffer->size;
1902 }
1903
1904 buffer->size = actualSize;
1905 } break;
1906
1907
1908 case AudioTrack::EVENT_STREAM_END:
1909 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
1910 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1911 me->mCallbackCookie, CB_EVENT_STREAM_END);
1912 break;
1913
1914 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
1915 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
1916 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
1917 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
1918 break;
1919
1920 default:
1921 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07001922 }
1923
Marco Nelissen6b74d672012-02-28 16:07:44 -08001924 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07001925}
1926
Marco Nelissen4110c102012-03-29 09:31:28 -07001927int MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07001928{
1929 return mSessionId;
1930}
1931
Eric Laurent6f59db12013-07-26 17:16:50 -07001932uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
1933{
1934 if (mTrack == 0) return 0;
1935 return mTrack->getSampleRate();
1936}
1937
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001938#undef LOG_TAG
1939#define LOG_TAG "AudioCache"
Eric Laurent3d00aa62013-09-24 09:53:27 -07001940MediaPlayerService::AudioCache::AudioCache(const sp<IMemoryHeap>& heap) :
1941 mHeap(heap), mChannelCount(0), mFrameCount(1024), mSampleRate(0), mSize(0),
1942 mError(NO_ERROR), mCommandComplete(false)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001943{
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001944}
1945
1946uint32_t MediaPlayerService::AudioCache::latency () const
1947{
1948 return 0;
1949}
1950
1951float MediaPlayerService::AudioCache::msecsPerFrame() const
1952{
1953 return mMsecsPerFrame;
1954}
1955
Marco Nelissen4110c102012-03-29 09:31:28 -07001956status_t MediaPlayerService::AudioCache::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001957{
1958 if (position == 0) return BAD_VALUE;
1959 *position = mSize;
1960 return NO_ERROR;
1961}
1962
Marco Nelissen4110c102012-03-29 09:31:28 -07001963status_t MediaPlayerService::AudioCache::getFramesWritten(uint32_t *written) const
1964{
1965 if (written == 0) return BAD_VALUE;
1966 *written = mSize;
1967 return NO_ERROR;
1968}
1969
Andreas Huber7d5b8a72010-02-09 16:59:18 -08001970////////////////////////////////////////////////////////////////////////////////
1971
1972struct CallbackThread : public Thread {
1973 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
1974 MediaPlayerBase::AudioSink::AudioCallback cb,
1975 void *cookie);
1976
1977protected:
1978 virtual ~CallbackThread();
1979
1980 virtual bool threadLoop();
1981
1982private:
1983 wp<MediaPlayerBase::AudioSink> mSink;
1984 MediaPlayerBase::AudioSink::AudioCallback mCallback;
1985 void *mCookie;
1986 void *mBuffer;
1987 size_t mBufferSize;
1988
1989 CallbackThread(const CallbackThread &);
1990 CallbackThread &operator=(const CallbackThread &);
1991};
1992
1993CallbackThread::CallbackThread(
1994 const wp<MediaPlayerBase::AudioSink> &sink,
1995 MediaPlayerBase::AudioSink::AudioCallback cb,
1996 void *cookie)
1997 : mSink(sink),
1998 mCallback(cb),
1999 mCookie(cookie),
2000 mBuffer(NULL),
2001 mBufferSize(0) {
2002}
2003
2004CallbackThread::~CallbackThread() {
2005 if (mBuffer) {
2006 free(mBuffer);
2007 mBuffer = NULL;
2008 }
2009}
2010
2011bool CallbackThread::threadLoop() {
2012 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2013 if (sink == NULL) {
2014 return false;
2015 }
2016
2017 if (mBuffer == NULL) {
2018 mBufferSize = sink->bufferSize();
2019 mBuffer = malloc(mBufferSize);
2020 }
2021
2022 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002023 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2024 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002025
2026 if (actualSize > 0) {
2027 sink->write(mBuffer, actualSize);
2028 }
2029
2030 return true;
2031}
2032
2033////////////////////////////////////////////////////////////////////////////////
2034
Andreas Huber20111aa2009-07-14 16:56:47 -07002035status_t MediaPlayerService::AudioCache::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002036 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2037 audio_format_t format, int bufferCount,
Mark Salyzyn77342f72014-06-18 16:31:32 -07002038 AudioCallback cb, void *cookie, audio_output_flags_t /*flags*/,
2039 const audio_offload_info_t* /*offloadInfo*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002040{
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002041 ALOGV("open(%u, %d, 0x%x, %d, %d)", sampleRate, channelCount, channelMask, format, bufferCount);
Dave Sparks8eb80112009-12-09 20:20:26 -08002042 if (mHeap->getHeapID() < 0) {
2043 return NO_INIT;
2044 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002045
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002046 mSampleRate = sampleRate;
2047 mChannelCount = (uint16_t)channelCount;
Glenn Kastene1c39622012-01-04 09:36:37 -08002048 mFormat = format;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002049 mMsecsPerFrame = 1.e3 / (float) sampleRate;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002050
2051 if (cb != NULL) {
2052 mCallbackThread = new CallbackThread(this, cb, cookie);
2053 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002054 return NO_ERROR;
2055}
2056
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002057status_t MediaPlayerService::AudioCache::start() {
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002058 if (mCallbackThread != NULL) {
2059 mCallbackThread->run("AudioCache callback");
2060 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002061 return NO_ERROR;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002062}
2063
2064void MediaPlayerService::AudioCache::stop() {
2065 if (mCallbackThread != NULL) {
2066 mCallbackThread->requestExitAndWait();
2067 }
2068}
2069
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002070ssize_t MediaPlayerService::AudioCache::write(const void* buffer, size_t size)
2071{
Steve Block3856b092011-10-20 11:56:00 +01002072 ALOGV("write(%p, %u)", buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002073 if ((buffer == 0) || (size == 0)) return size;
2074
2075 uint8_t* p = static_cast<uint8_t*>(mHeap->getBase());
2076 if (p == NULL) return NO_INIT;
2077 p += mSize;
Steve Block3856b092011-10-20 11:56:00 +01002078 ALOGV("memcpy(%p, %p, %u)", p, buffer, size);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002079 if (mSize + size > mHeap->getSize()) {
Steve Block29357bc2012-01-06 19:20:56 +00002080 ALOGE("Heap size overflow! req size: %d, max size: %d", (mSize + size), mHeap->getSize());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002081 size = mHeap->getSize() - mSize;
2082 }
2083 memcpy(p, buffer, size);
2084 mSize += size;
2085 return size;
2086}
2087
2088// call with lock held
2089status_t MediaPlayerService::AudioCache::wait()
2090{
2091 Mutex::Autolock lock(mLock);
Dave Sparks4bbc0ba2010-03-01 19:29:58 -08002092 while (!mCommandComplete) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002093 mSignal.wait(mLock);
2094 }
2095 mCommandComplete = false;
2096
2097 if (mError == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01002098 ALOGV("wait - success");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002099 } else {
Steve Block3856b092011-10-20 11:56:00 +01002100 ALOGV("wait - error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002101 }
2102 return mError;
2103}
2104
Gloria Wangb483c472011-04-11 17:23:27 -07002105void MediaPlayerService::AudioCache::notify(
Mark Salyzyn77342f72014-06-18 16:31:32 -07002106 void* cookie, int msg, int ext1, int ext2, const Parcel* /*obj*/)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002107{
Steve Block3856b092011-10-20 11:56:00 +01002108 ALOGV("notify(%p, %d, %d, %d)", cookie, msg, ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002109 AudioCache* p = static_cast<AudioCache*>(cookie);
2110
2111 // ignore buffering messages
Dave Sparks8eb80112009-12-09 20:20:26 -08002112 switch (msg)
2113 {
2114 case MEDIA_ERROR:
Steve Block29357bc2012-01-06 19:20:56 +00002115 ALOGE("Error %d, %d occurred", ext1, ext2);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002116 p->mError = ext1;
Dave Sparks8eb80112009-12-09 20:20:26 -08002117 break;
2118 case MEDIA_PREPARED:
Steve Block3856b092011-10-20 11:56:00 +01002119 ALOGV("prepared");
Dave Sparks8eb80112009-12-09 20:20:26 -08002120 break;
2121 case MEDIA_PLAYBACK_COMPLETE:
Steve Block3856b092011-10-20 11:56:00 +01002122 ALOGV("playback complete");
Dave Sparks8eb80112009-12-09 20:20:26 -08002123 break;
2124 default:
Steve Block3856b092011-10-20 11:56:00 +01002125 ALOGV("ignored");
Dave Sparks8eb80112009-12-09 20:20:26 -08002126 return;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002127 }
2128
2129 // wake up thread
Dave Sparksfe4c6f02010-03-02 12:56:37 -08002130 Mutex::Autolock lock(p->mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002131 p->mCommandComplete = true;
2132 p->mSignal.signal();
2133}
2134
Marco Nelissen4110c102012-03-29 09:31:28 -07002135int MediaPlayerService::AudioCache::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002136{
2137 return 0;
2138}
2139
Eric Laurent6f59db12013-07-26 17:16:50 -07002140uint32_t MediaPlayerService::AudioCache::getSampleRate() const
2141{
2142 if (mMsecsPerFrame == 0) {
2143 return 0;
2144 }
2145 return (uint32_t)(1.e3 / mMsecsPerFrame);
2146}
2147
Gloria Wang7cf180c2011-02-19 18:37:57 -08002148void MediaPlayerService::addBatteryData(uint32_t params)
2149{
2150 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002151
2152 int32_t time = systemTime() / 1000000L;
2153
2154 // change audio output devices. This notification comes from AudioFlinger
2155 if ((params & kBatteryDataSpeakerOn)
2156 || (params & kBatteryDataOtherAudioDeviceOn)) {
2157
2158 int deviceOn[NUM_AUDIO_DEVICES];
2159 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2160 deviceOn[i] = 0;
2161 }
2162
2163 if ((params & kBatteryDataSpeakerOn)
2164 && (params & kBatteryDataOtherAudioDeviceOn)) {
2165 deviceOn[SPEAKER_AND_OTHER] = 1;
2166 } else if (params & kBatteryDataSpeakerOn) {
2167 deviceOn[SPEAKER] = 1;
2168 } else {
2169 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2170 }
2171
2172 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2173 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2174
2175 if (mBatteryAudio.refCount > 0) { // if playing audio
2176 if (!deviceOn[i]) {
2177 mBatteryAudio.lastTime[i] += time;
2178 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2179 mBatteryAudio.lastTime[i] = 0;
2180 } else {
2181 mBatteryAudio.lastTime[i] = 0 - time;
2182 }
2183 }
2184
2185 mBatteryAudio.deviceOn[i] = deviceOn[i];
2186 }
2187 }
2188 return;
2189 }
2190
2191 // an sudio stream is started
2192 if (params & kBatteryDataAudioFlingerStart) {
2193 // record the start time only if currently no other audio
2194 // is being played
2195 if (mBatteryAudio.refCount == 0) {
2196 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2197 if (mBatteryAudio.deviceOn[i]) {
2198 mBatteryAudio.lastTime[i] -= time;
2199 }
2200 }
2201 }
2202
2203 mBatteryAudio.refCount ++;
2204 return;
2205
2206 } else if (params & kBatteryDataAudioFlingerStop) {
2207 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002208 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002209 return;
2210 }
2211
2212 // record the stop time only if currently this is the only
2213 // audio being played
2214 if (mBatteryAudio.refCount == 1) {
2215 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2216 if (mBatteryAudio.deviceOn[i]) {
2217 mBatteryAudio.lastTime[i] += time;
2218 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2219 mBatteryAudio.lastTime[i] = 0;
2220 }
2221 }
2222 }
2223
2224 mBatteryAudio.refCount --;
2225 return;
2226 }
2227
Gloria Wang7cf180c2011-02-19 18:37:57 -08002228 int uid = IPCThreadState::self()->getCallingUid();
2229 if (uid == AID_MEDIA) {
2230 return;
2231 }
2232 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002233
2234 if (index < 0) { // create a new entry for this UID
2235 BatteryUsageInfo info;
2236 info.audioTotalTime = 0;
2237 info.videoTotalTime = 0;
2238 info.audioLastTime = 0;
2239 info.videoLastTime = 0;
2240 info.refCount = 0;
2241
Gloria Wang9ee159b2011-02-24 14:51:45 -08002242 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002243 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002244 return;
2245 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002246 }
2247
2248 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2249
2250 if (params & kBatteryDataCodecStarted) {
2251 if (params & kBatteryDataTrackAudio) {
2252 info.audioLastTime -= time;
2253 info.refCount ++;
2254 }
2255 if (params & kBatteryDataTrackVideo) {
2256 info.videoLastTime -= time;
2257 info.refCount ++;
2258 }
2259 } else {
2260 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002261 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002262 return;
2263 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002264 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002265 mBatteryData.removeItem(uid);
2266 return;
2267 }
2268
2269 if (params & kBatteryDataTrackAudio) {
2270 info.audioLastTime += time;
2271 info.refCount --;
2272 }
2273 if (params & kBatteryDataTrackVideo) {
2274 info.videoLastTime += time;
2275 info.refCount --;
2276 }
2277
2278 // no stream is being played by this UID
2279 if (info.refCount == 0) {
2280 info.audioTotalTime += info.audioLastTime;
2281 info.audioLastTime = 0;
2282 info.videoTotalTime += info.videoLastTime;
2283 info.videoLastTime = 0;
2284 }
2285 }
2286}
2287
2288status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2289 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002290
2291 // audio output devices usage
2292 int32_t time = systemTime() / 1000000L; //in ms
2293 int32_t totalTime;
2294
2295 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2296 totalTime = mBatteryAudio.totalTime[i];
2297
2298 if (mBatteryAudio.deviceOn[i]
2299 && (mBatteryAudio.lastTime[i] != 0)) {
2300 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2301 totalTime += tmpTime;
2302 }
2303
2304 reply->writeInt32(totalTime);
2305 // reset the total time
2306 mBatteryAudio.totalTime[i] = 0;
2307 }
2308
2309 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002310 BatteryUsageInfo info;
2311 int size = mBatteryData.size();
2312
2313 reply->writeInt32(size);
2314 int i = 0;
2315
2316 while (i < size) {
2317 info = mBatteryData.valueAt(i);
2318
2319 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2320 reply->writeInt32(info.audioTotalTime);
2321 reply->writeInt32(info.videoTotalTime);
2322
2323 info.audioTotalTime = 0;
2324 info.videoTotalTime = 0;
2325
2326 // remove the UID entry where no stream is being played
2327 if (info.refCount <= 0) {
2328 mBatteryData.removeItemsAt(i);
2329 size --;
2330 i --;
2331 }
2332 i++;
2333 }
2334 return NO_ERROR;
2335}
nikoa64c8c72009-07-20 15:07:26 -07002336} // namespace android