blob: f8dc0dcb49d98d1482539712d4eb7a2be5c75992 [file] [log] [blame]
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18// Proxy for media player implementations
19
20//#define LOG_NDEBUG 0
21#define LOG_TAG "MediaPlayerService"
22#include <utils/Log.h>
23
24#include <sys/types.h>
25#include <sys/stat.h>
Gloria Wang7cf180c2011-02-19 18:37:57 -080026#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080027#include <dirent.h>
28#include <unistd.h>
29
30#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070031
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080032#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070033#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070034
35#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080036
Mathias Agopian75624082009-05-19 19:08:10 -070037#include <binder/IPCThreadState.h>
38#include <binder/IServiceManager.h>
39#include <binder/MemoryHeapBase.h>
40#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080041#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070042#include <utils/Errors.h> // for status_t
43#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070044#include <utils/SystemClock.h>
Lajos Molnar06ad1522014-08-28 07:27:44 -070045#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070046#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080047
Eric Laurent43562692015-07-15 16:49:07 -070048#include <media/AudioPolicyHelper.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080049#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070050#include <media/IRemoteDisplay.h>
51#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080052#include <media/MediaPlayerInterface.h>
53#include <media/mediarecorder.h>
54#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070055#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080056#include <media/AudioTrack.h>
James Dong8635b7b2011-03-14 17:01:38 -070057#include <media/MemoryLeakTrackUtil.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070058#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070059#include <media/stagefright/MediaErrors.h>
Marco Nelissen83b0fd92015-09-16 13:48:07 -070060#include <media/stagefright/Utils.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010061#include <media/stagefright/foundation/ADebug.h>
Marco Nelissenf09611f2015-02-13 14:12:42 -080062#include <media/stagefright/foundation/ALooperRoster.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070063#include <mediautils/BatteryNotifier.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080064
Andy Hung53541292016-04-13 16:48:51 -070065#include <memunreachable/memunreachable.h>
Dima Zavin64760242011-05-11 14:15:23 -070066#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070067
Gloria Wang7cf180c2011-02-19 18:37:57 -080068#include <private/android_filesystem_config.h>
69
James Dong559bf282012-03-28 10:29:14 -070070#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080071#include "MediaRecorderClient.h"
72#include "MediaPlayerService.h"
73#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070074#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080075
Nicolas Catania14d27472009-07-13 14:37:49 -070076#include "TestPlayerStub.h"
Andreas Huberf9334412010-12-15 15:17:42 -080077#include "nuplayer/NuPlayerDriver.h"
Andreas Huber20111aa2009-07-14 16:56:47 -070078
Andreas Huber20111aa2009-07-14 16:56:47 -070079#include <OMX.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070080
Andreas Huber59451f82012-09-18 10:36:32 -070081#include "HDCP.h"
Andreas Huberb7319a72013-05-29 14:20:52 -070082#include "HTTPBase.h"
Andreas Huber35213f12012-08-29 11:41:50 -070083#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070084
Wei Jia502c2f42017-07-13 17:47:56 -070085static const int kDumpLockRetries = 50;
86static const int kDumpLockSleepUs = 20000;
87
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070088namespace {
nikoa64c8c72009-07-20 15:07:26 -070089using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070090using android::status_t;
91using android::OK;
92using android::BAD_VALUE;
93using android::NOT_ENOUGH_DATA;
94using android::Parcel;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070095
96// Max number of entries in the filter.
97const int kMaxFilterSize = 64; // I pulled that out of thin air.
98
Andy Hungff874dc2016-04-11 16:49:09 -070099const float kMaxRequiredSpeed = 8.0f; // for PCM tracks allow up to 8x speedup.
100
nikoa64c8c72009-07-20 15:07:26 -0700101// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -0700102
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700103
104// Unmarshall a filter from a Parcel.
105// Filter format in a parcel:
106//
107// 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
108// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109// | number of entries (n) |
110// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111// | metadata type 1 |
112// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113// | metadata type 2 |
114// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
115// ....
116// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117// | metadata type n |
118// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
119//
120// @param p Parcel that should start with a filter.
121// @param[out] filter On exit contains the list of metadata type to be
122// filtered.
123// @param[out] status On exit contains the status code to be returned.
124// @return true if the parcel starts with a valid filter.
125bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700126 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700127 status_t *status)
128{
Nicolas Catania48290382009-07-10 13:53:06 -0700129 int32_t val;
130 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700131 {
Steve Block29357bc2012-01-06 19:20:56 +0000132 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700133 *status = NOT_ENOUGH_DATA;
134 return false;
135 }
136
Nicolas Catania48290382009-07-10 13:53:06 -0700137 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138 {
Steve Block29357bc2012-01-06 19:20:56 +0000139 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140 *status = BAD_VALUE;
141 return false;
142 }
143
Nicolas Catania48290382009-07-10 13:53:06 -0700144 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145
146 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700147 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700148
nikoa64c8c72009-07-20 15:07:26 -0700149 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700150
Nicolas Catania48290382009-07-10 13:53:06 -0700151
152 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700153 {
Andy Hung833b4752016-04-04 17:15:48 -0700154 ALOGE("Filter too short expected %zu but got %zu", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700155 *status = NOT_ENOUGH_DATA;
156 return false;
157 }
158
nikoa64c8c72009-07-20 15:07:26 -0700159 const Metadata::Type *data =
160 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700161
Nicolas Catania48290382009-07-10 13:53:06 -0700162 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700163 {
Steve Block29357bc2012-01-06 19:20:56 +0000164 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700165 *status = BAD_VALUE;
166 return false;
167 }
168
169 // TODO: The stl impl of vector would be more efficient here
170 // because it degenerates into a memcpy on pod types. Try to
171 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700172 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700173 {
Nicolas Catania48290382009-07-10 13:53:06 -0700174 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700175 ++data;
176 }
177 *status = OK;
178 return true;
179}
180
Nicolas Catania48290382009-07-10 13:53:06 -0700181// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700182// @param val To be searched.
183// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700184bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700185{
186 // Deal with empty and ANY right away
187 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700188 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700189
Nicolas Catania48290382009-07-10 13:53:06 -0700190 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700191}
192
193} // anonymous namespace
194
195
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700196namespace {
197using android::Parcel;
198using android::String16;
199
200// marshalling tag indicating flattened utf16 tags
201// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
202const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
203
204// Audio attributes format in a parcel:
205//
206// 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
207// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
208// | usage |
209// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
210// | content_type |
211// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900212// | source |
213// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700214// | flags |
215// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
216// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
217// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
218// | flattened tags in UTF16 |
219// | ... |
220// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221//
222// @param p Parcel that contains audio attributes.
223// @param[out] attributes On exit points to an initialized audio_attributes_t structure
224// @param[out] status On exit contains the status code to be returned.
225void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
226{
227 attributes->usage = (audio_usage_t) parcel.readInt32();
228 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900229 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700230 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
231 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
232 if (hasFlattenedTag) {
233 // the tags are UTF16, convert to UTF8
234 String16 tags = parcel.readString16();
235 ssize_t realTagSize = utf16_to_utf8_length(tags.string(), tags.size());
236 if (realTagSize <= 0) {
237 strcpy(attributes->tags, "");
238 } else {
239 // copy the flattened string into the attributes as the destination for the conversion:
240 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
241 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
242 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
Sergio Giro1d3f4272016-06-28 18:24:52 +0100243 utf16_to_utf8(tags.string(), tagSize, attributes->tags,
244 sizeof(attributes->tags) / sizeof(attributes->tags[0]));
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700245 }
246 } else {
247 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
248 strcpy(attributes->tags, "");
249 }
250}
251} // anonymous namespace
252
253
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800254namespace android {
255
Marco Nelissenf09611f2015-02-13 14:12:42 -0800256extern ALooperRoster gLooperRoster;
257
258
Dave Burked681bbb2011-08-30 14:39:17 +0100259static bool checkPermission(const char* permissionString) {
Dave Burked681bbb2011-08-30 14:39:17 +0100260 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
261 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000262 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100263 return ok;
264}
265
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800266// 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 -0800267/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
268/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
269
270void MediaPlayerService::instantiate() {
271 defaultServiceManager()->addService(
272 String16("media.player"), new MediaPlayerService());
273}
274
275MediaPlayerService::MediaPlayerService()
276{
Steve Block3856b092011-10-20 11:56:00 +0100277 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800278 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800279
280 mBatteryAudio.refCount = 0;
281 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
282 mBatteryAudio.deviceOn[i] = 0;
283 mBatteryAudio.lastTime[i] = 0;
284 mBatteryAudio.totalTime[i] = 0;
285 }
286 // speaker is on by default
287 mBatteryAudio.deviceOn[SPEAKER] = 1;
John Grossman44a7e422012-06-21 17:29:24 -0700288
Chong Zhang0b30fd42014-07-23 14:46:05 -0700289 // reset battery stats
290 // if the mediaserver has crashed, battery stats could be left
291 // in bad state, reset the state upon service start.
Wei Jia3f273d12015-11-24 09:06:49 -0800292 BatteryNotifier::getInstance().noteResetVideo();
Chong Zhang0b30fd42014-07-23 14:46:05 -0700293
John Grossman44a7e422012-06-21 17:29:24 -0700294 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800295}
296
297MediaPlayerService::~MediaPlayerService()
298{
Steve Block3856b092011-10-20 11:56:00 +0100299 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800300}
301
Svet Ganovbe71aa22015-04-28 12:06:02 -0700302sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(const String16 &opPackageName)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800303{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800304 pid_t pid = IPCThreadState::self()->getCallingPid();
Svet Ganovbe71aa22015-04-28 12:06:02 -0700305 sp<MediaRecorderClient> recorder = new MediaRecorderClient(this, pid, opPackageName);
Gloria Wangdac6a312009-10-29 15:46:37 -0700306 wp<MediaRecorderClient> w = recorder;
307 Mutex::Autolock lock(mLock);
308 mMediaRecorderClients.add(w);
Steve Block3856b092011-10-20 11:56:00 +0100309 ALOGV("Create new media recorder client from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800310 return recorder;
311}
312
Gloria Wangdac6a312009-10-29 15:46:37 -0700313void MediaPlayerService::removeMediaRecorderClient(wp<MediaRecorderClient> client)
314{
315 Mutex::Autolock lock(mLock);
316 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100317 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700318}
319
Glenn Kastenf37971f2012-02-03 11:06:53 -0800320sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800321{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800322 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800323 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100324 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800325 return retriever;
326}
327
Glenn Kastenf37971f2012-02-03 11:06:53 -0800328sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Glenn Kastend848eb42016-03-08 13:42:11 -0800329 audio_session_t audioSessionId)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800330{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800331 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800332 int32_t connId = android_atomic_inc(&mNextConnId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700333
334 sp<Client> c = new Client(
335 this, pid, connId, client, audioSessionId,
336 IPCThreadState::self()->getCallingUid());
337
Steve Block3856b092011-10-20 11:56:00 +0100338 ALOGV("Create new client(%d) from pid %d, uid %d, ", connId, pid,
Dave Burked681bbb2011-08-30 14:39:17 +0100339 IPCThreadState::self()->getCallingUid());
340
341 wp<Client> w = c;
342 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800343 Mutex::Autolock lock(mLock);
344 mClients.add(w);
345 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800346 return c;
347}
348
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700349sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
350 return MediaCodecList::getLocalInstance();
351}
352
Marco Nelissen260e56c2016-01-28 21:41:25 +0000353sp<IOMX> MediaPlayerService::getOMX() {
Marco Nelissen1900e772016-02-02 16:12:16 -0800354 ALOGI("MediaPlayerService::getOMX");
Marco Nelissen260e56c2016-01-28 21:41:25 +0000355 Mutex::Autolock autoLock(mLock);
356
357 if (mOMX.get() == NULL) {
358 mOMX = new OMX;
359 }
360
361 return mOMX;
362}
363
Andreas Huber279dcd82013-01-30 10:41:25 -0800364sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
365 return new HDCP(createEncryptionModule);
Andreas Huber59451f82012-09-18 10:36:32 -0700366}
367
Jeff Brown2013a542012-09-04 21:38:42 -0700368sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
Svet Ganovbe71aa22015-04-28 12:06:02 -0700369 const String16 &opPackageName,
Jeff Brown2013a542012-09-04 21:38:42 -0700370 const sp<IRemoteDisplayClient>& client, const String8& iface) {
Jeff Brownaba33d52012-09-07 17:38:58 -0700371 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
372 return NULL;
373 }
374
Svet Ganovbe71aa22015-04-28 12:06:02 -0700375 return new RemoteDisplay(opPackageName, client, iface.string());
Jeff Brown2013a542012-09-04 21:38:42 -0700376}
377
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800378status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
379{
380 const size_t SIZE = 256;
381 char buffer[SIZE];
382 String8 result;
383
384 result.append(" AudioOutput\n");
385 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
386 mStreamType, mLeftVolume, mRightVolume);
387 result.append(buffer);
388 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800389 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800390 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700391 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
392 mAuxEffectId, mSendLevel);
393 result.append(buffer);
394
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800395 ::write(fd, result.string(), result.size());
396 if (mTrack != 0) {
397 mTrack->dump(fd, args);
398 }
399 return NO_ERROR;
400}
401
Lajos Molnar6d339f12015-04-17 16:15:53 -0700402status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800403{
404 const size_t SIZE = 256;
405 char buffer[SIZE];
406 String8 result;
407 result.append(" Client\n");
408 snprintf(buffer, 255, " pid(%d), connId(%d), status(%d), looping(%s)\n",
409 mPid, mConnId, mStatus, mLoop?"true": "false");
410 result.append(buffer);
Wei Jia502c2f42017-07-13 17:47:56 -0700411
412 sp<MediaPlayerBase> p;
413 sp<AudioOutput> audioOutput;
414 bool locked = false;
415 for (int i = 0; i < kDumpLockRetries; ++i) {
416 if (mLock.tryLock() == NO_ERROR) {
417 locked = true;
418 break;
419 }
420 usleep(kDumpLockSleepUs);
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700421 }
Wei Jia502c2f42017-07-13 17:47:56 -0700422
423 if (locked) {
424 p = mPlayer;
425 audioOutput = mAudioOutput;
426 mLock.unlock();
427 } else {
428 result.append(" lock is taken, no dump from player and audio output\n");
429 }
430 write(fd, result.string(), result.size());
431
432 if (p != NULL) {
433 p->dump(fd, args);
434 }
435 if (audioOutput != 0) {
436 audioOutput->dump(fd, args);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800437 }
438 write(fd, "\n", 1);
439 return NO_ERROR;
440}
441
Marco Nelissenf09611f2015-02-13 14:12:42 -0800442/**
443 * The only arguments this understands right now are -c, -von and -voff,
444 * which are parsed by ALooperRoster::dump()
445 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800446status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
447{
448 const size_t SIZE = 256;
449 char buffer[SIZE];
450 String8 result;
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530451 SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
452 SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
453
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800454 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
455 snprintf(buffer, SIZE, "Permission Denial: "
456 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
457 IPCThreadState::self()->getCallingPid(),
458 IPCThreadState::self()->getCallingUid());
459 result.append(buffer);
460 } else {
461 Mutex::Autolock lock(mLock);
462 for (int i = 0, n = mClients.size(); i < n; ++i) {
463 sp<Client> c = mClients[i].promote();
464 if (c != 0) c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530465 clients.add(c);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800466 }
James Dongb9141222010-07-08 11:16:11 -0700467 if (mMediaRecorderClients.size() == 0) {
468 result.append(" No media recorder client\n\n");
469 } else {
470 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
471 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
James Donge579e282011-10-18 22:29:20 -0700472 if (c != 0) {
473 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n", c->mPid);
474 result.append(buffer);
475 write(fd, result.string(), result.size());
476 result = "\n";
477 c->dump(fd, args);
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530478 mediaRecorderClients.add(c);
James Donge579e282011-10-18 22:29:20 -0700479 }
James Dongb9141222010-07-08 11:16:11 -0700480 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700481 }
482
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800483 result.append(" Files opened and/or mapped:\n");
Marco Nelissenf09611f2015-02-13 14:12:42 -0800484 snprintf(buffer, SIZE, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800485 FILE *f = fopen(buffer, "r");
486 if (f) {
487 while (!feof(f)) {
488 fgets(buffer, SIZE, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700489 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800490 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700491 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492 strstr(buffer, " /system/media/")) {
493 result.append(" ");
494 result.append(buffer);
495 }
496 }
497 fclose(f);
498 } else {
499 result.append("couldn't open ");
500 result.append(buffer);
501 result.append("\n");
502 }
503
Marco Nelissenf09611f2015-02-13 14:12:42 -0800504 snprintf(buffer, SIZE, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800505 DIR *d = opendir(buffer);
506 if (d) {
507 struct dirent *ent;
508 while((ent = readdir(d)) != NULL) {
509 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Marco Nelissenf09611f2015-02-13 14:12:42 -0800510 snprintf(buffer, SIZE, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800511 struct stat s;
512 if (lstat(buffer, &s) == 0) {
513 if ((s.st_mode & S_IFMT) == S_IFLNK) {
514 char linkto[256];
515 int len = readlink(buffer, linkto, sizeof(linkto));
516 if(len > 0) {
517 if(len > 255) {
518 linkto[252] = '.';
519 linkto[253] = '.';
520 linkto[254] = '.';
521 linkto[255] = 0;
522 } else {
523 linkto[len] = 0;
524 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700525 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800526 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700527 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800528 strstr(linkto, "/system/media/") == linkto) {
529 result.append(" ");
530 result.append(buffer);
531 result.append(" -> ");
532 result.append(linkto);
533 result.append("\n");
534 }
535 }
536 } else {
537 result.append(" unexpected type for ");
538 result.append(buffer);
539 result.append("\n");
540 }
541 }
542 }
543 }
544 closedir(d);
545 } else {
546 result.append("couldn't open ");
547 result.append(buffer);
548 result.append("\n");
549 }
550
Marco Nelissenf09611f2015-02-13 14:12:42 -0800551 gLooperRoster.dump(fd, args);
552
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800553 bool dumpMem = false;
Andy Hung53541292016-04-13 16:48:51 -0700554 bool unreachableMemory = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800555 for (size_t i = 0; i < args.size(); i++) {
556 if (args[i] == String16("-m")) {
557 dumpMem = true;
Andy Hung53541292016-04-13 16:48:51 -0700558 } else if (args[i] == String16("--unreachable")) {
559 unreachableMemory = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560 }
561 }
562 if (dumpMem) {
Andy Hung07b745e2016-05-23 16:21:07 -0700563 result.append("\nDumping memory:\n");
564 std::string s = dumpMemoryAddresses(100 /* limit */);
565 result.append(s.c_str(), s.size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800566 }
Andy Hung53541292016-04-13 16:48:51 -0700567 if (unreachableMemory) {
568 result.append("\nDumping unreachable memory:\n");
569 // TODO - should limit be an argument parameter?
570 std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */);
571 result.append(s.c_str(), s.size());
572 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800573 }
574 write(fd, result.string(), result.size());
575 return NO_ERROR;
576}
577
578void MediaPlayerService::removeClient(wp<Client> client)
579{
580 Mutex::Autolock lock(mLock);
581 mClients.remove(client);
582}
583
Robert Shihee0a0e32016-08-16 16:50:54 -0700584bool MediaPlayerService::hasClient(wp<Client> client)
585{
586 Mutex::Autolock lock(mLock);
587 return mClients.indexOf(client) != NAME_NOT_FOUND;
588}
589
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700590MediaPlayerService::Client::Client(
591 const sp<MediaPlayerService>& service, pid_t pid,
592 int32_t connId, const sp<IMediaPlayerClient>& client,
Glenn Kastend848eb42016-03-08 13:42:11 -0800593 audio_session_t audioSessionId, uid_t uid)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800594{
Steve Block3856b092011-10-20 11:56:00 +0100595 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800596 mPid = pid;
597 mConnId = connId;
598 mService = service;
599 mClient = client;
600 mLoop = false;
601 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700602 mAudioSessionId = audioSessionId;
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700603 mUID = uid;
John Grossmanc795b642012-02-22 15:38:35 -0800604 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700605 mAudioAttributes = NULL;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700606
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800607#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000608 ALOGD("create Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800609 mAntagonizer = new Antagonizer(notify, this);
610#endif
611}
612
613MediaPlayerService::Client::~Client()
614{
Steve Block3856b092011-10-20 11:56:00 +0100615 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
Wei Jia502c2f42017-07-13 17:47:56 -0700616 {
617 Mutex::Autolock l(mLock);
618 mAudioOutput.clear();
619 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620 wp<Client> client(this);
621 disconnect();
622 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700623 if (mAudioAttributes != NULL) {
624 free(mAudioAttributes);
625 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800626}
627
628void MediaPlayerService::Client::disconnect()
629{
Steve Block3856b092011-10-20 11:56:00 +0100630 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800631 // grab local reference and clear main reference to prevent future
632 // access to object
633 sp<MediaPlayerBase> p;
634 {
635 Mutex::Autolock l(mLock);
636 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800637 mClient.clear();
Wei Jia502c2f42017-07-13 17:47:56 -0700638 mPlayer.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800639 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700640
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800641 // clear the notification to prevent callbacks to dead client
642 // and reset the player. We assume the player will serialize
643 // access to itself if necessary.
644 if (p != 0) {
645 p->setNotifyCallback(0, 0);
646#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000647 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800648 mAntagonizer->kill();
649#endif
650 p->reset();
651 }
652
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700653 {
654 Mutex::Autolock l(mLock);
655 disconnectNativeWindow_l();
656 }
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700657
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800658 IPCThreadState::self()->flushCommands();
659}
660
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
662{
663 // determine if we have the right player type
Wei Jia502c2f42017-07-13 17:47:56 -0700664 sp<MediaPlayerBase> p = getPlayer();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800665 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100666 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800667 p.clear();
668 }
669 if (p == NULL) {
Ronghua Wu68845c12015-07-21 09:50:48 -0700670 p = MediaPlayerFactory::createPlayer(playerType, this, notify, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800671 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700672
Jason Simmonsdb29e522011-08-12 13:46:55 -0700673 if (p != NULL) {
674 p->setUID(mUID);
675 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700676
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800677 return p;
678}
679
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700680MediaPlayerService::Client::ServiceDeathNotifier::ServiceDeathNotifier(
681 const sp<IBinder>& service,
682 const sp<MediaPlayerBase>& listener,
683 int which) {
684 mService = service;
685 mListener = listener;
686 mWhich = which;
687}
688
689MediaPlayerService::Client::ServiceDeathNotifier::~ServiceDeathNotifier() {
690 mService->unlinkToDeath(this);
691}
692
693void MediaPlayerService::Client::ServiceDeathNotifier::binderDied(const wp<IBinder>& /*who*/) {
694 sp<MediaPlayerBase> listener = mListener.promote();
695 if (listener != NULL) {
696 listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, mWhich);
697 } else {
698 ALOGW("listener for process %d death is gone", mWhich);
699 }
700}
701
John Grossmanc795b642012-02-22 15:38:35 -0800702sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
703 player_type playerType)
704{
705 ALOGV("player type = %d", playerType);
706
707 // create the right type of player
708 sp<MediaPlayerBase> p = createPlayer(playerType);
709 if (p == NULL) {
710 return p;
711 }
712
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700713 sp<IServiceManager> sm = defaultServiceManager();
714 sp<IBinder> binder = sm->getService(String16("media.extractor"));
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700715 sp<ServiceDeathNotifier> extractorDeathListener =
716 new ServiceDeathNotifier(binder, p, MEDIAEXTRACTOR_PROCESS_DEATH);
717 binder->linkToDeath(extractorDeathListener);
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700718
719 binder = sm->getService(String16("media.codec"));
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700720 sp<ServiceDeathNotifier> codecDeathListener =
721 new ServiceDeathNotifier(binder, p, MEDIACODEC_PROCESS_DEATH);
722 binder->linkToDeath(codecDeathListener);
723
724 Mutex::Autolock lock(mLock);
725
726 mExtractorDeathListener = extractorDeathListener;
727 mCodecDeathListener = codecDeathListener;
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700728
John Grossmanc795b642012-02-22 15:38:35 -0800729 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800730 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700731 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800732 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
733 }
734
735 return p;
736}
737
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700738status_t MediaPlayerService::Client::setDataSource_post(
John Grossmanc795b642012-02-22 15:38:35 -0800739 const sp<MediaPlayerBase>& p,
740 status_t status)
741{
742 ALOGV(" setDataSource");
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700743 if (status != OK) {
744 ALOGE(" error: %d", status);
745 return status;
John Grossmanc795b642012-02-22 15:38:35 -0800746 }
747
748 // Set the re-transmission endpoint if one was chosen.
749 if (mRetransmitEndpointValid) {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700750 status = p->setRetransmitEndpoint(&mRetransmitEndpoint);
751 if (status != NO_ERROR) {
752 ALOGE("setRetransmitEndpoint error: %d", status);
John Grossmanc795b642012-02-22 15:38:35 -0800753 }
754 }
755
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700756 if (status == OK) {
757 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -0800758 mPlayer = p;
759 }
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700760 return status;
John Grossmanc795b642012-02-22 15:38:35 -0800761}
762
Andreas Huber2db84552010-01-28 11:19:57 -0800763status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800764 const sp<IMediaHTTPService> &httpService,
765 const char *url,
766 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800767{
Steve Block3856b092011-10-20 11:56:00 +0100768 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800769 if (url == NULL)
770 return UNKNOWN_ERROR;
771
Dave Burked681bbb2011-08-30 14:39:17 +0100772 if ((strncmp(url, "http://", 7) == 0) ||
773 (strncmp(url, "https://", 8) == 0) ||
774 (strncmp(url, "rtsp://", 7) == 0)) {
775 if (!checkPermission("android.permission.INTERNET")) {
776 return PERMISSION_DENIED;
777 }
778 }
779
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800780 if (strncmp(url, "content://", 10) == 0) {
781 // get a filedescriptor for the content Uri and
782 // pass it to the setDataSource(fd) method
783
784 String16 url16(url);
785 int fd = android::openContentProviderFile(url16);
786 if (fd < 0)
787 {
Steve Block29357bc2012-01-06 19:20:56 +0000788 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800789 return UNKNOWN_ERROR;
790 }
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700791 status_t status = setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800792 close(fd);
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700793 return mStatus = status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800794 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700795 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800796 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
797 if (p == NULL) {
798 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800799 }
800
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700801 return mStatus =
802 setDataSource_post(
803 p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800804 }
805}
806
807status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
808{
Marco Nelissen83b0fd92015-09-16 13:48:07 -0700809 ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
810 fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800811 struct stat sb;
812 int ret = fstat(fd, &sb);
813 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000814 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800815 return UNKNOWN_ERROR;
816 }
817
Andy Hung833b4752016-04-04 17:15:48 -0700818 ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev));
Steve Block3856b092011-10-20 11:56:00 +0100819 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700820 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
821 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Andy Hung833b4752016-04-04 17:15:48 -0700822 ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800823
824 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000825 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800826 return UNKNOWN_ERROR;
827 }
828 if (offset + length > sb.st_size) {
829 length = sb.st_size - offset;
Andy Hung833b4752016-04-04 17:15:48 -0700830 ALOGV("calculated length = %lld", (long long)length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831 }
832
John Grossman44a7e422012-06-21 17:29:24 -0700833 player_type playerType = MediaPlayerFactory::getPlayerType(this,
834 fd,
835 offset,
836 length);
John Grossmanc795b642012-02-22 15:38:35 -0800837 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
838 if (p == NULL) {
839 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800840 }
841
842 // now set data source
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700843 return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844}
845
Andreas Hubere2b10282010-11-23 11:41:34 -0800846status_t MediaPlayerService::Client::setDataSource(
847 const sp<IStreamSource> &source) {
848 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700849 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800850 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800851 if (p == NULL) {
852 return NO_INIT;
853 }
854
Andreas Hubere2b10282010-11-23 11:41:34 -0800855 // now set data source
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700856 return mStatus = setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800857}
858
Chris Watkins99f31602015-03-20 13:06:33 -0700859status_t MediaPlayerService::Client::setDataSource(
860 const sp<IDataSource> &source) {
861 sp<DataSource> dataSource = DataSource::CreateFromIDataSource(source);
862 player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
863 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
864 if (p == NULL) {
865 return NO_INIT;
866 }
867 // now set data source
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700868 return mStatus = setDataSource_post(p, p->setDataSource(dataSource));
Chris Watkins99f31602015-03-20 13:06:33 -0700869}
870
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700871void MediaPlayerService::Client::disconnectNativeWindow_l() {
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700872 if (mConnectedWindow != NULL) {
873 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
874 NATIVE_WINDOW_API_MEDIA);
875
876 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000877 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700878 strerror(-err), err);
879 }
880 }
881 mConnectedWindow.clear();
882}
883
Glenn Kasten11731182011-02-08 17:26:17 -0800884status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800885 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800886{
Andy McFadden484566c2012-12-18 09:46:54 -0800887 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800888 sp<MediaPlayerBase> p = getPlayer();
889 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700890
Marco Nelissenf8880202014-11-14 07:58:25 -0800891 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700892 if (mConnectedWindowBinder == binder) {
893 return OK;
894 }
895
896 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800897 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700898 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700899 status_t err = native_window_api_connect(anw.get(),
900 NATIVE_WINDOW_API_MEDIA);
901
902 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000903 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700904 // Note that we must do the reset before disconnecting from the ANW.
905 // Otherwise queue/dequeue calls could be made on the disconnected
906 // ANW, which may result in errors.
907 reset();
908
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700909 Mutex::Autolock lock(mLock);
910 disconnectNativeWindow_l();
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700911
912 return err;
913 }
914 }
915
Andy McFadden484566c2012-12-18 09:46:54 -0800916 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700917 // disconnecting the old one. Otherwise queue/dequeue calls could be made
918 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800919 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700920
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700921 mLock.lock();
922 disconnectNativeWindow_l();
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700923
924 if (err == OK) {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700925 mConnectedWindow = anw;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700926 mConnectedWindowBinder = binder;
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700927 mLock.unlock();
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700928 } else {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700929 mLock.unlock();
930 status_t err = native_window_api_disconnect(
931 anw.get(), NATIVE_WINDOW_API_MEDIA);
932
933 if (err != OK) {
934 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
935 strerror(-err), err);
936 }
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700937 }
938
939 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800940}
941
Nicolas Catania1d187f12009-05-12 23:25:55 -0700942status_t MediaPlayerService::Client::invoke(const Parcel& request,
943 Parcel *reply)
944{
945 sp<MediaPlayerBase> p = getPlayer();
946 if (p == NULL) return UNKNOWN_ERROR;
947 return p->invoke(request, reply);
948}
949
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700950// This call doesn't need to access the native player.
951status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
952{
953 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700954 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700955
Nicolas Catania48290382009-07-10 13:53:06 -0700956 if (unmarshallFilter(filter, &allow, &status) &&
957 unmarshallFilter(filter, &drop, &status)) {
958 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700959
960 mMetadataAllow = allow;
961 mMetadataDrop = drop;
962 }
963 return status;
964}
965
Nicolas Catania48290382009-07-10 13:53:06 -0700966status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700967 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700968{
nikoa64c8c72009-07-20 15:07:26 -0700969 sp<MediaPlayerBase> player = getPlayer();
970 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700971
nikod608a812009-07-16 16:39:53 -0700972 status_t status;
973 // Placeholder for the return code, updated by the caller.
974 reply->writeInt32(-1);
975
nikoa64c8c72009-07-20 15:07:26 -0700976 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700977
978 // We don't block notifications while we fetch the data. We clear
979 // mMetadataUpdated first so we don't lose notifications happening
980 // during the rest of this call.
981 {
982 Mutex::Autolock lock(mLock);
983 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700984 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700985 }
986 mMetadataUpdated.clear();
987 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700988
nikoa64c8c72009-07-20 15:07:26 -0700989 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700990
nikoa64c8c72009-07-20 15:07:26 -0700991 metadata.appendHeader();
992 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700993
994 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700995 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000996 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700997 return status;
998 }
999
1000 // FIXME: Implement filtering on the result. Not critical since
1001 // filtering takes place on the update notifications already. This
1002 // would be when all the metadata are fetch and a filter is set.
1003
nikod608a812009-07-16 16:39:53 -07001004 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -07001005 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -07001006 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -07001007}
1008
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001009status_t MediaPlayerService::Client::prepareAsync()
1010{
Steve Block3856b092011-10-20 11:56:00 +01001011 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001012 sp<MediaPlayerBase> p = getPlayer();
1013 if (p == 0) return UNKNOWN_ERROR;
1014 status_t ret = p->prepareAsync();
1015#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +00001016 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001017 if (ret == NO_ERROR) mAntagonizer->start();
1018#endif
1019 return ret;
1020}
1021
1022status_t MediaPlayerService::Client::start()
1023{
Steve Block3856b092011-10-20 11:56:00 +01001024 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001025 sp<MediaPlayerBase> p = getPlayer();
1026 if (p == 0) return UNKNOWN_ERROR;
1027 p->setLooping(mLoop);
1028 return p->start();
1029}
1030
1031status_t MediaPlayerService::Client::stop()
1032{
Steve Block3856b092011-10-20 11:56:00 +01001033 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001034 sp<MediaPlayerBase> p = getPlayer();
1035 if (p == 0) return UNKNOWN_ERROR;
1036 return p->stop();
1037}
1038
1039status_t MediaPlayerService::Client::pause()
1040{
Steve Block3856b092011-10-20 11:56:00 +01001041 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001042 sp<MediaPlayerBase> p = getPlayer();
1043 if (p == 0) return UNKNOWN_ERROR;
1044 return p->pause();
1045}
1046
1047status_t MediaPlayerService::Client::isPlaying(bool* state)
1048{
1049 *state = false;
1050 sp<MediaPlayerBase> p = getPlayer();
1051 if (p == 0) return UNKNOWN_ERROR;
1052 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +01001053 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001054 return NO_ERROR;
1055}
1056
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001057status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -08001058{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001059 ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
1060 mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Wei Jia98160162015-02-04 17:01:11 -08001061 sp<MediaPlayerBase> p = getPlayer();
1062 if (p == 0) return UNKNOWN_ERROR;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001063 return p->setPlaybackSettings(rate);
1064}
1065
1066status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
1067{
1068 sp<MediaPlayerBase> p = getPlayer();
1069 if (p == 0) return UNKNOWN_ERROR;
1070 status_t ret = p->getPlaybackSettings(rate);
1071 if (ret == NO_ERROR) {
1072 ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
1073 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
1074 } else {
1075 ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
1076 }
1077 return ret;
1078}
1079
1080status_t MediaPlayerService::Client::setSyncSettings(
1081 const AVSyncSettings& sync, float videoFpsHint)
1082{
1083 ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
1084 mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
1085 sp<MediaPlayerBase> p = getPlayer();
1086 if (p == 0) return UNKNOWN_ERROR;
1087 return p->setSyncSettings(sync, videoFpsHint);
1088}
1089
1090status_t MediaPlayerService::Client::getSyncSettings(
1091 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
1092{
1093 sp<MediaPlayerBase> p = getPlayer();
1094 if (p == 0) return UNKNOWN_ERROR;
1095 status_t ret = p->getSyncSettings(sync, videoFps);
1096 if (ret == NO_ERROR) {
1097 ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
1098 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
1099 } else {
1100 ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
1101 }
1102 return ret;
Wei Jia98160162015-02-04 17:01:11 -08001103}
1104
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001105status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1106{
Steve Block3856b092011-10-20 11:56:00 +01001107 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001108 sp<MediaPlayerBase> p = getPlayer();
1109 if (p == 0) return UNKNOWN_ERROR;
1110 status_t ret = p->getCurrentPosition(msec);
1111 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001112 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001113 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001114 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001115 }
1116 return ret;
1117}
1118
1119status_t MediaPlayerService::Client::getDuration(int *msec)
1120{
Steve Block3856b092011-10-20 11:56:00 +01001121 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001122 sp<MediaPlayerBase> p = getPlayer();
1123 if (p == 0) return UNKNOWN_ERROR;
1124 status_t ret = p->getDuration(msec);
1125 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001126 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001127 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001128 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001129 }
1130 return ret;
1131}
1132
Marco Nelissen6b74d672012-02-28 16:07:44 -08001133status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1134 ALOGV("setNextPlayer");
1135 Mutex::Autolock l(mLock);
1136 sp<Client> c = static_cast<Client*>(player.get());
Wei Jia28284122016-08-30 13:49:06 -07001137 if (c != NULL && !mService->hasClient(c)) {
Robert Shihee0a0e32016-08-16 16:50:54 -07001138 return BAD_VALUE;
1139 }
1140
Marco Nelissen6b74d672012-02-28 16:07:44 -08001141 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001142
1143 if (c != NULL) {
1144 if (mAudioOutput != NULL) {
1145 mAudioOutput->setNextOutput(c->mAudioOutput);
1146 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1147 ALOGE("no current audio output");
1148 }
1149
1150 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1151 mPlayer->setNextPlayer(mNextClient->getPlayer());
1152 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001153 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001154
Marco Nelissen6b74d672012-02-28 16:07:44 -08001155 return OK;
1156}
1157
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001158status_t MediaPlayerService::Client::seekTo(int msec)
1159{
Steve Block3856b092011-10-20 11:56:00 +01001160 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001161 sp<MediaPlayerBase> p = getPlayer();
1162 if (p == 0) return UNKNOWN_ERROR;
1163 return p->seekTo(msec);
1164}
1165
1166status_t MediaPlayerService::Client::reset()
1167{
Steve Block3856b092011-10-20 11:56:00 +01001168 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001169 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001170 sp<MediaPlayerBase> p = getPlayer();
1171 if (p == 0) return UNKNOWN_ERROR;
1172 return p->reset();
1173}
1174
Glenn Kastenfff6d712012-01-12 16:38:12 -08001175status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001176{
Steve Block3856b092011-10-20 11:56:00 +01001177 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001178 // TODO: for hardware output, call player instead
1179 Mutex::Autolock l(mLock);
1180 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1181 return NO_ERROR;
1182}
1183
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001184status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1185{
1186 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1187 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001188 if (mAudioAttributes == NULL) {
1189 return NO_MEMORY;
1190 }
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001191 unmarshallAudioAttributes(parcel, mAudioAttributes);
1192
1193 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1194 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1195 mAudioAttributes->tags);
1196
1197 if (mAudioOutput != 0) {
1198 mAudioOutput->setAudioAttributes(mAudioAttributes);
1199 }
1200 return NO_ERROR;
1201}
1202
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001203status_t MediaPlayerService::Client::setLooping(int loop)
1204{
Steve Block3856b092011-10-20 11:56:00 +01001205 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001206 mLoop = loop;
1207 sp<MediaPlayerBase> p = getPlayer();
1208 if (p != 0) return p->setLooping(loop);
1209 return NO_ERROR;
1210}
1211
1212status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1213{
Steve Block3856b092011-10-20 11:56:00 +01001214 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001215
1216 // for hardware output, call player instead
1217 sp<MediaPlayerBase> p = getPlayer();
1218 {
1219 Mutex::Autolock l(mLock);
1220 if (p != 0 && p->hardwareOutput()) {
1221 MediaPlayerHWInterface* hwp =
1222 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1223 return hwp->setVolume(leftVolume, rightVolume);
1224 } else {
1225 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1226 return NO_ERROR;
1227 }
1228 }
1229
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001230 return NO_ERROR;
1231}
1232
Eric Laurent2beeb502010-07-16 07:43:46 -07001233status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1234{
Steve Block3856b092011-10-20 11:56:00 +01001235 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001236 Mutex::Autolock l(mLock);
1237 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1238 return NO_ERROR;
1239}
1240
1241status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1242{
Steve Block3856b092011-10-20 11:56:00 +01001243 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001244 Mutex::Autolock l(mLock);
1245 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1246 return NO_ERROR;
1247}
Nicolas Catania48290382009-07-10 13:53:06 -07001248
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001249status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001250 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001251 switch (key) {
1252 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1253 {
1254 Mutex::Autolock l(mLock);
1255 return setAudioAttributes_l(request);
1256 }
1257 default:
1258 sp<MediaPlayerBase> p = getPlayer();
1259 if (p == 0) { return UNKNOWN_ERROR; }
1260 return p->setParameter(key, request);
1261 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001262}
1263
1264status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001265 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001266 sp<MediaPlayerBase> p = getPlayer();
1267 if (p == 0) return UNKNOWN_ERROR;
1268 return p->getParameter(key, reply);
1269}
1270
John Grossmanc795b642012-02-22 15:38:35 -08001271status_t MediaPlayerService::Client::setRetransmitEndpoint(
1272 const struct sockaddr_in* endpoint) {
1273
1274 if (NULL != endpoint) {
1275 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1276 uint16_t p = ntohs(endpoint->sin_port);
1277 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1278 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1279 } else {
1280 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1281 }
1282
1283 sp<MediaPlayerBase> p = getPlayer();
1284
1285 // Right now, the only valid time to set a retransmit endpoint is before
1286 // player selection has been made (since the presence or absence of a
1287 // retransmit endpoint is going to determine which player is selected during
1288 // setDataSource).
1289 if (p != 0) return INVALID_OPERATION;
1290
1291 if (NULL != endpoint) {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -07001292 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -08001293 mRetransmitEndpoint = *endpoint;
1294 mRetransmitEndpointValid = true;
1295 } else {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -07001296 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -08001297 mRetransmitEndpointValid = false;
1298 }
1299
1300 return NO_ERROR;
1301}
1302
John Grossman44a7e422012-06-21 17:29:24 -07001303status_t MediaPlayerService::Client::getRetransmitEndpoint(
1304 struct sockaddr_in* endpoint)
1305{
1306 if (NULL == endpoint)
1307 return BAD_VALUE;
1308
1309 sp<MediaPlayerBase> p = getPlayer();
1310
1311 if (p != NULL)
1312 return p->getRetransmitEndpoint(endpoint);
1313
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -07001314 Mutex::Autolock lock(mLock);
John Grossman44a7e422012-06-21 17:29:24 -07001315 if (!mRetransmitEndpointValid)
1316 return NO_INIT;
1317
1318 *endpoint = mRetransmitEndpoint;
1319
1320 return NO_ERROR;
1321}
1322
Gloria Wangb483c472011-04-11 17:23:27 -07001323void MediaPlayerService::Client::notify(
1324 void* cookie, int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001325{
1326 Client* client = static_cast<Client*>(cookie);
James Dongb8a98252012-08-26 16:13:03 -07001327 if (client == NULL) {
1328 return;
1329 }
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001330
James Dongb8a98252012-08-26 16:13:03 -07001331 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001332 {
1333 Mutex::Autolock l(client->mLock);
James Dongb8a98252012-08-26 16:13:03 -07001334 c = client->mClient;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001335 if (msg == MEDIA_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
John Grossmancb0b7552012-08-23 17:47:31 -07001336 if (client->mAudioOutput != NULL)
1337 client->mAudioOutput->switchToNextOutput();
Marco Nelissen6b74d672012-02-28 16:07:44 -08001338 client->mNextClient->start();
Wei Jia2afac0c2016-01-07 12:13:07 -08001339 if (client->mNextClient->mClient != NULL) {
1340 client->mNextClient->mClient->notify(
1341 MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1342 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001343 }
1344 }
1345
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001346 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001347 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001348 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001349
1350 if(client->shouldDropMetadata(metadata_type)) {
1351 return;
1352 }
1353
1354 // Update the list of metadata that have changed. getMetadata
1355 // also access mMetadataUpdated and clears it.
1356 client->addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001357 }
James Dongb8a98252012-08-26 16:13:03 -07001358
1359 if (c != NULL) {
1360 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, cookie, msg, ext1, ext2);
1361 c->notify(msg, ext1, ext2, obj);
1362 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001363}
1364
Nicolas Catania48290382009-07-10 13:53:06 -07001365
nikoa64c8c72009-07-20 15:07:26 -07001366bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001367{
Nicolas Catania48290382009-07-10 13:53:06 -07001368 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001369
Nicolas Catania48290382009-07-10 13:53:06 -07001370 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001371 return true;
1372 }
1373
Nicolas Catania48290382009-07-10 13:53:06 -07001374 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001375 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001376 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001377 return true;
1378 }
1379}
1380
Nicolas Catania48290382009-07-10 13:53:06 -07001381
nikoa64c8c72009-07-20 15:07:26 -07001382void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001383 Mutex::Autolock lock(mLock);
1384 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1385 mMetadataUpdated.add(metadata_type);
1386 }
1387}
1388
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001389#if CALLBACK_ANTAGONIZER
1390const int Antagonizer::interval = 10000; // 10 msecs
1391
1392Antagonizer::Antagonizer(notify_callback_f cb, void* client) :
1393 mExit(false), mActive(false), mClient(client), mCb(cb)
1394{
1395 createThread(callbackThread, this);
1396}
1397
1398void Antagonizer::kill()
1399{
1400 Mutex::Autolock _l(mLock);
1401 mActive = false;
1402 mExit = true;
1403 mCondition.wait(mLock);
1404}
1405
1406int Antagonizer::callbackThread(void* user)
1407{
Steve Blockb8a80522011-12-20 16:23:08 +00001408 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001409 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1410 while (!p->mExit) {
1411 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001412 ALOGV("send event");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001413 p->mCb(p->mClient, 0, 0, 0);
1414 }
1415 usleep(interval);
1416 }
1417 Mutex::Autolock _l(p->mLock);
1418 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001419 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420 return 0;
1421}
1422#endif
1423
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001424#undef LOG_TAG
1425#define LOG_TAG "AudioSink"
Glenn Kastend848eb42016-03-08 13:42:11 -08001426MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId, int uid, int pid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001427 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001428 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001429 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001430 mCallbackData(NULL),
Andy Hungd1c74342015-07-07 16:54:23 -07001431 mStreamType(AUDIO_STREAM_MUSIC),
Andy Hungd1c74342015-07-07 16:54:23 -07001432 mLeftVolume(1.0),
1433 mRightVolume(1.0),
1434 mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
1435 mSampleRateHz(0),
1436 mMsecsPerFrame(0),
1437 mFrameSize(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001438 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001439 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001440 mPid(pid),
Andy Hungd1c74342015-07-07 16:54:23 -07001441 mSendLevel(0.0),
1442 mAuxEffectId(0),
1443 mFlags(AUDIO_OUTPUT_FLAG_NONE)
1444{
Steve Block3856b092011-10-20 11:56:00 +01001445 ALOGV("AudioOutput(%d)", sessionId);
Eric Laurent43562692015-07-15 16:49:07 -07001446 if (attr != NULL) {
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001447 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1448 if (mAttributes != NULL) {
1449 memcpy(mAttributes, attr, sizeof(audio_attributes_t));
1450 mStreamType = audio_attributes_to_stream_type(attr);
1451 }
1452 } else {
1453 mAttributes = NULL;
Eric Laurent43562692015-07-15 16:49:07 -07001454 }
1455
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001456 setMinBufferCount();
1457}
1458
1459MediaPlayerService::AudioOutput::~AudioOutput()
1460{
1461 close();
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001462 free(mAttributes);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001463 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001464}
1465
Andy Hungd1c74342015-07-07 16:54:23 -07001466//static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001467void MediaPlayerService::AudioOutput::setMinBufferCount()
1468{
1469 char value[PROPERTY_VALUE_MAX];
1470 if (property_get("ro.kernel.qemu", value, 0)) {
1471 mIsOnEmulator = true;
1472 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1473 }
1474}
1475
Andy Hungd1c74342015-07-07 16:54:23 -07001476// static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001477bool MediaPlayerService::AudioOutput::isOnEmulator()
1478{
Andy Hungd1c74342015-07-07 16:54:23 -07001479 setMinBufferCount(); // benign race wrt other threads
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001480 return mIsOnEmulator;
1481}
1482
Andy Hungd1c74342015-07-07 16:54:23 -07001483// static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001484int MediaPlayerService::AudioOutput::getMinBufferCount()
1485{
Andy Hungd1c74342015-07-07 16:54:23 -07001486 setMinBufferCount(); // benign race wrt other threads
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001487 return mMinBufferCount;
1488}
1489
1490ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1491{
Andy Hungd1c74342015-07-07 16:54:23 -07001492 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001493 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001494 return mTrack->frameCount() * mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001495}
1496
1497ssize_t MediaPlayerService::AudioOutput::frameCount() const
1498{
Andy Hungd1c74342015-07-07 16:54:23 -07001499 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001500 if (mTrack == 0) return NO_INIT;
1501 return mTrack->frameCount();
1502}
1503
1504ssize_t MediaPlayerService::AudioOutput::channelCount() const
1505{
Andy Hungd1c74342015-07-07 16:54:23 -07001506 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001507 if (mTrack == 0) return NO_INIT;
1508 return mTrack->channelCount();
1509}
1510
1511ssize_t MediaPlayerService::AudioOutput::frameSize() const
1512{
Andy Hungd1c74342015-07-07 16:54:23 -07001513 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001514 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001515 return mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001516}
1517
1518uint32_t MediaPlayerService::AudioOutput::latency () const
1519{
Andy Hungd1c74342015-07-07 16:54:23 -07001520 Mutex::Autolock lock(mLock);
Eric Laurentdb354e52012-03-05 17:27:11 -08001521 if (mTrack == 0) return 0;
1522 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001523}
1524
1525float MediaPlayerService::AudioOutput::msecsPerFrame() const
1526{
Andy Hungd1c74342015-07-07 16:54:23 -07001527 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001528 return mMsecsPerFrame;
1529}
1530
Marco Nelissen4110c102012-03-29 09:31:28 -07001531status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001532{
Andy Hungd1c74342015-07-07 16:54:23 -07001533 Mutex::Autolock lock(mLock);
Eric Laurent342e9cf2010-01-19 17:37:09 -08001534 if (mTrack == 0) return NO_INIT;
1535 return mTrack->getPosition(position);
1536}
1537
Lajos Molnar06ad1522014-08-28 07:27:44 -07001538status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1539{
Andy Hungd1c74342015-07-07 16:54:23 -07001540 Mutex::Autolock lock(mLock);
Lajos Molnar06ad1522014-08-28 07:27:44 -07001541 if (mTrack == 0) return NO_INIT;
1542 return mTrack->getTimestamp(ts);
1543}
1544
Wei Jiac4ac8172015-10-21 10:35:48 -07001545// TODO: Remove unnecessary calls to getPlayedOutDurationUs()
1546// as it acquires locks and may query the audio driver.
1547//
1548// Some calls could conceivably retrieve extrapolated data instead of
1549// accessing getTimestamp() or getPosition() every time a data buffer with
1550// a media time is received.
1551//
1552// Calculate duration of played samples if played at normal rate (i.e., 1.0).
1553int64_t MediaPlayerService::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const
1554{
1555 Mutex::Autolock lock(mLock);
1556 if (mTrack == 0 || mSampleRateHz == 0) {
Wei Jia213f4902015-10-22 08:55:25 -07001557 return 0;
Wei Jiac4ac8172015-10-21 10:35:48 -07001558 }
1559
1560 uint32_t numFramesPlayed;
1561 int64_t numFramesPlayedAt;
1562 AudioTimestamp ts;
1563 static const int64_t kStaleTimestamp100ms = 100000;
1564
1565 status_t res = mTrack->getTimestamp(ts);
1566 if (res == OK) { // case 1: mixing audio tracks and offloaded tracks.
1567 numFramesPlayed = ts.mPosition;
1568 numFramesPlayedAt = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000;
1569 const int64_t timestampAge = nowUs - numFramesPlayedAt;
1570 if (timestampAge > kStaleTimestamp100ms) {
1571 // This is an audio FIXME.
1572 // getTimestamp returns a timestamp which may come from audio mixing threads.
1573 // After pausing, the MixerThread may go idle, thus the mTime estimate may
1574 // become stale. Assuming that the MixerThread runs 20ms, with FastMixer at 5ms,
1575 // the max latency should be about 25ms with an average around 12ms (to be verified).
1576 // For safety we use 100ms.
1577 ALOGV("getTimestamp: returned stale timestamp nowUs(%lld) numFramesPlayedAt(%lld)",
1578 (long long)nowUs, (long long)numFramesPlayedAt);
1579 numFramesPlayedAt = nowUs - kStaleTimestamp100ms;
1580 }
1581 //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAt);
1582 } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track
1583 numFramesPlayed = 0;
1584 numFramesPlayedAt = nowUs;
1585 //ALOGD("getTimestamp: WOULD_BLOCK %d %lld",
1586 // numFramesPlayed, (long long)numFramesPlayedAt);
1587 } else { // case 3: transitory at new track or audio fast tracks.
1588 res = mTrack->getPosition(&numFramesPlayed);
1589 CHECK_EQ(res, (status_t)OK);
1590 numFramesPlayedAt = nowUs;
1591 numFramesPlayedAt += 1000LL * mTrack->latency() / 2; /* XXX */
1592 //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAt);
1593 }
1594
1595 // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test
1596 // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours.
1597 int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz)
1598 + nowUs - numFramesPlayedAt;
1599 if (durationUs < 0) {
1600 // Occurs when numFramesPlayed position is very small and the following:
1601 // (1) In case 1, the time nowUs is computed before getTimestamp() is called and
1602 // numFramesPlayedAt is greater than nowUs by time more than numFramesPlayed.
1603 // (2) In case 3, using getPosition and adding mAudioSink->latency() to
1604 // numFramesPlayedAt, by a time amount greater than numFramesPlayed.
1605 //
1606 // Both of these are transitory conditions.
1607 ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs);
1608 durationUs = 0;
1609 }
1610 ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)",
1611 (long long)durationUs, (long long)nowUs, numFramesPlayed, (long long)numFramesPlayedAt);
1612 return durationUs;
1613}
1614
Marco Nelissen4110c102012-03-29 09:31:28 -07001615status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1616{
Andy Hungd1c74342015-07-07 16:54:23 -07001617 Mutex::Autolock lock(mLock);
Marco Nelissen4110c102012-03-29 09:31:28 -07001618 if (mTrack == 0) return NO_INIT;
Andy Hung2f6e73d2016-04-08 12:12:58 -07001619 ExtendedTimestamp ets;
1620 status_t status = mTrack->getTimestamp(&ets);
1621 if (status == OK || status == WOULD_BLOCK) {
1622 *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT];
1623 }
1624 return status;
Marco Nelissen4110c102012-03-29 09:31:28 -07001625}
1626
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001627status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1628{
Andy Hungd1c74342015-07-07 16:54:23 -07001629 Mutex::Autolock lock(mLock);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001630 if (mTrack == 0) return NO_INIT;
1631 return mTrack->setParameters(keyValuePairs);
1632}
1633
1634String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1635{
Andy Hungd1c74342015-07-07 16:54:23 -07001636 Mutex::Autolock lock(mLock);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001637 if (mTrack == 0) return String8::empty();
1638 return mTrack->getParameters(keys);
1639}
1640
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001641void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
Andy Hungd1c74342015-07-07 16:54:23 -07001642 Mutex::Autolock lock(mLock);
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001643 if (attributes == NULL) {
1644 free(mAttributes);
1645 mAttributes = NULL;
1646 } else {
1647 if (mAttributes == NULL) {
1648 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1649 }
1650 memcpy(mAttributes, attributes, sizeof(audio_attributes_t));
Eric Laurent43562692015-07-15 16:49:07 -07001651 mStreamType = audio_attributes_to_stream_type(attributes);
1652 }
1653}
1654
1655void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType)
1656{
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001657 Mutex::Autolock lock(mLock);
Eric Laurent43562692015-07-15 16:49:07 -07001658 // do not allow direct stream type modification if attributes have been set
1659 if (mAttributes == NULL) {
1660 mStreamType = streamType;
1661 }
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001662}
1663
Andy Hungd1c74342015-07-07 16:54:23 -07001664void MediaPlayerService::AudioOutput::deleteRecycledTrack_l()
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001665{
Andy Hungd1c74342015-07-07 16:54:23 -07001666 ALOGV("deleteRecycledTrack_l");
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001667 if (mRecycledTrack != 0) {
1668
1669 if (mCallbackData != NULL) {
1670 mCallbackData->setOutput(NULL);
1671 mCallbackData->endTrackSwitch();
1672 }
1673
1674 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
Andy Hunge13f8a62016-03-30 14:20:42 -07001675 int32_t msec = 0;
1676 if (!mRecycledTrack->stopped()) { // check if active
1677 (void)mRecycledTrack->pendingDuration(&msec);
1678 }
1679 mRecycledTrack->stop(); // ensure full data drain
1680 ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec);
1681 if (msec > 0) {
1682 static const int32_t WAIT_LIMIT_MS = 3000;
1683 if (msec > WAIT_LIMIT_MS) {
1684 msec = WAIT_LIMIT_MS;
1685 }
1686 usleep(msec * 1000LL);
1687 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001688 }
1689 // An offloaded track isn't flushed because the STREAM_END is reported
1690 // slightly prematurely to allow time for the gapless track switch
1691 // but this means that if we decide not to recycle the track there
1692 // could be a small amount of residual data still playing. We leave
1693 // AudioFlinger to drain the track.
1694
1695 mRecycledTrack.clear();
Andy Hungd1c74342015-07-07 16:54:23 -07001696 close_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001697 delete mCallbackData;
1698 mCallbackData = NULL;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001699 }
1700}
1701
Andy Hungd1c74342015-07-07 16:54:23 -07001702void MediaPlayerService::AudioOutput::close_l()
1703{
1704 mTrack.clear();
1705}
1706
Andreas Huber20111aa2009-07-14 16:56:47 -07001707status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001708 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1709 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001710 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001711 audio_output_flags_t flags,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001712 const audio_offload_info_t *offloadInfo,
Andy Hung179652e2015-05-31 22:49:46 -07001713 bool doNotReconnect,
1714 uint32_t suggestedFrameCount)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001715{
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001716 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1717 format, bufferCount, mSessionId, flags);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001718
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001719 // offloading is only supported in callback mode for now.
1720 // offloadInfo must be present if offload flag is set
1721 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1722 ((cb == NULL) || (offloadInfo == NULL))) {
1723 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001724 }
1725
Andy Hung179652e2015-05-31 22:49:46 -07001726 // compute frame count for the AudioTrack internal buffer
1727 size_t frameCount;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001728 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1729 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1730 } else {
Andy Hung179652e2015-05-31 22:49:46 -07001731 // try to estimate the buffer processing fetch size from AudioFlinger.
1732 // framesPerBuffer is approximate and generally correct, except when it's not :-).
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001733 uint32_t afSampleRate;
1734 size_t afFrameCount;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001735 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1736 return NO_INIT;
1737 }
1738 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1739 return NO_INIT;
1740 }
Andy Hung179652e2015-05-31 22:49:46 -07001741 const size_t framesPerBuffer =
1742 (unsigned long long)sampleRate * afFrameCount / afSampleRate;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001743
Andy Hung179652e2015-05-31 22:49:46 -07001744 if (bufferCount == 0) {
1745 // use suggestedFrameCount
1746 bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer;
1747 }
1748 // Check argument bufferCount against the mininum buffer count
1749 if (bufferCount != 0 && bufferCount < mMinBufferCount) {
1750 ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount);
1751 bufferCount = mMinBufferCount;
1752 }
1753 // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger
1754 // which will be the minimum size permitted.
1755 frameCount = bufferCount * framesPerBuffer;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001756 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001757
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001758 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001759 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001760 if (0 == channelMask) {
1761 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1762 return NO_INIT;
1763 }
1764 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001765
Andy Hungd1c74342015-07-07 16:54:23 -07001766 Mutex::Autolock lock(mLock);
Andy Hung179652e2015-05-31 22:49:46 -07001767 mCallback = cb;
1768 mCallbackCookie = cookie;
1769
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001770 // Check whether we can recycle the track
1771 bool reuse = false;
1772 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001773
Glenn Kasten2799d742013-05-30 14:33:29 -07001774 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001775 // check whether we are switching between two offloaded tracks
1776 bothOffloaded = (flags & mRecycledTrack->getFlags()
1777 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001778
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001779 // check if the existing track can be reused as-is, or if a new track needs to be created.
1780 reuse = true;
1781
Marco Nelissen67295b52012-06-11 14:52:53 -07001782 if ((mCallbackData == NULL && mCallback != NULL) ||
1783 (mCallbackData != NULL && mCallback == NULL)) {
1784 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1785 ALOGV("can't chain callback and write");
1786 reuse = false;
1787 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001788 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1789 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001790 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001791 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001792 reuse = false;
1793 } else if (flags != mFlags) {
1794 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1795 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001796 } else if (mRecycledTrack->format() != format) {
1797 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001798 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001799 } else {
1800 ALOGV("no track available to recycle");
1801 }
1802
1803 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1804
1805 // If we can't recycle and both tracks are offloaded
1806 // we must close the previous output before opening a new one
1807 if (bothOffloaded && !reuse) {
1808 ALOGV("both offloaded and not recycling");
Andy Hungd1c74342015-07-07 16:54:23 -07001809 deleteRecycledTrack_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001810 }
1811
1812 sp<AudioTrack> t;
1813 CallbackData *newcbd = NULL;
1814
1815 // We don't attempt to create a new track if we are recycling an
1816 // offloaded track. But, if we are recycling a non-offloaded or we
1817 // are switching where one is offloaded and one isn't then we create
1818 // the new track in advance so that we can read additional stream info
1819
1820 if (!(reuse && bothOffloaded)) {
1821 ALOGV("creating new AudioTrack");
1822
1823 if (mCallback != NULL) {
1824 newcbd = new CallbackData(this);
1825 t = new AudioTrack(
1826 mStreamType,
1827 sampleRate,
1828 format,
1829 channelMask,
1830 frameCount,
1831 flags,
1832 CallbackWrapper,
1833 newcbd,
1834 0, // notification frames
1835 mSessionId,
1836 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001837 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001838 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001839 mPid,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001840 mAttributes,
1841 doNotReconnect);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001842 } else {
Andy Hungff874dc2016-04-11 16:49:09 -07001843 // TODO: Due to buffer memory concerns, we use a max target playback speed
1844 // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed),
1845 // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed.
1846 const float targetSpeed =
1847 std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed);
1848 ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed,
1849 "track target speed:%f clamped from playback speed:%f",
1850 targetSpeed, mPlaybackRate.mSpeed);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001851 t = new AudioTrack(
1852 mStreamType,
1853 sampleRate,
1854 format,
1855 channelMask,
1856 frameCount,
1857 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001858 NULL, // callback
1859 NULL, // user data
1860 0, // notification frames
1861 mSessionId,
1862 AudioTrack::TRANSFER_DEFAULT,
1863 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001864 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001865 mPid,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001866 mAttributes,
Andy Hungff874dc2016-04-11 16:49:09 -07001867 doNotReconnect,
1868 targetSpeed);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001869 }
1870
1871 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1872 ALOGE("Unable to create audio track");
1873 delete newcbd;
Glenn Kasten3e98ecd2015-05-18 13:13:24 -07001874 // t goes out of scope, so reference count drops to zero
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001875 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001876 } else {
1877 // successful AudioTrack initialization implies a legacy stream type was generated
1878 // from the audio attributes
1879 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001880 }
1881 }
1882
1883 if (reuse) {
1884 CHECK(mRecycledTrack != NULL);
1885
1886 if (!bothOffloaded) {
1887 if (mRecycledTrack->frameCount() != t->frameCount()) {
Andy Hung833b4752016-04-04 17:15:48 -07001888 ALOGV("framecount differs: %zu/%zu frames",
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001889 mRecycledTrack->frameCount(), t->frameCount());
1890 reuse = false;
1891 }
1892 }
1893
Marco Nelissen67295b52012-06-11 14:52:53 -07001894 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001895 ALOGV("chaining to next output and recycling track");
Andy Hungd1c74342015-07-07 16:54:23 -07001896 close_l();
Marco Nelissen67295b52012-06-11 14:52:53 -07001897 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001898 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001899 if (mCallbackData != NULL) {
1900 mCallbackData->setOutput(this);
1901 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001902 delete newcbd;
Wei Jiae0bbac92016-07-18 16:04:53 -07001903 return updateTrack();
Marco Nelissen67295b52012-06-11 14:52:53 -07001904 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001905 }
1906
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001907 // we're not going to reuse the track, unblock and flush it
1908 // this was done earlier if both tracks are offloaded
1909 if (!bothOffloaded) {
Andy Hungd1c74342015-07-07 16:54:23 -07001910 deleteRecycledTrack_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001911 }
1912
1913 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1914
Marco Nelissen67295b52012-06-11 14:52:53 -07001915 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001916 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001917 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001918
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001919 mSampleRateHz = sampleRate;
Preetam Singh Ranawat2e17eef2015-08-12 12:11:46 -07001920 mFlags = flags;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001921 mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
Andy Hungd1c74342015-07-07 16:54:23 -07001922 mFrameSize = t->frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001923 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001924
Wei Jiae0bbac92016-07-18 16:04:53 -07001925 return updateTrack();
1926}
1927
1928status_t MediaPlayerService::AudioOutput::updateTrack() {
1929 if (mTrack == NULL) {
1930 return NO_ERROR;
1931 }
1932
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001933 status_t res = NO_ERROR;
Wei Jia0162d002015-06-09 11:59:33 -07001934 // Note some output devices may give us a direct track even though we don't specify it.
1935 // Example: Line application b/17459982.
Wei Jiae0bbac92016-07-18 16:04:53 -07001936 if ((mTrack->getFlags()
1937 & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
1938 res = mTrack->setPlaybackRate(mPlaybackRate);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001939 if (res == NO_ERROR) {
Wei Jiae0bbac92016-07-18 16:04:53 -07001940 mTrack->setAuxEffectSendLevel(mSendLevel);
1941 res = mTrack->attachAuxEffect(mAuxEffectId);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001942 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001943 }
Wei Jiae0bbac92016-07-18 16:04:53 -07001944 ALOGV("updateTrack() DONE status %d", res);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001945 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001946}
1947
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001948status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001949{
Steve Block3856b092011-10-20 11:56:00 +01001950 ALOGV("start");
Andy Hungd1c74342015-07-07 16:54:23 -07001951 Mutex::Autolock lock(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001952 if (mCallbackData != NULL) {
1953 mCallbackData->endTrackSwitch();
1954 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001955 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001956 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001957 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001958 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001959 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001960 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001961}
1962
Marco Nelissen6b74d672012-02-28 16:07:44 -08001963void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
Andy Hungd1c74342015-07-07 16:54:23 -07001964 Mutex::Autolock lock(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001965 mNextOutput = nextOutput;
1966}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001967
Marco Nelissen6b74d672012-02-28 16:07:44 -08001968void MediaPlayerService::AudioOutput::switchToNextOutput() {
1969 ALOGV("switchToNextOutput");
Andy Hungd1c74342015-07-07 16:54:23 -07001970
1971 // Try to acquire the callback lock before moving track (without incurring deadlock).
1972 const unsigned kMaxSwitchTries = 100;
1973 Mutex::Autolock lock(mLock);
1974 for (unsigned tries = 0;;) {
1975 if (mTrack == 0) {
1976 return;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001977 }
Andy Hungd1c74342015-07-07 16:54:23 -07001978 if (mNextOutput != NULL && mNextOutput != this) {
1979 if (mCallbackData != NULL) {
1980 // two alternative approaches
1981#if 1
1982 CallbackData *callbackData = mCallbackData;
1983 mLock.unlock();
1984 // proper acquisition sequence
1985 callbackData->lock();
1986 mLock.lock();
1987 // Caution: it is unlikely that someone deleted our callback or changed our target
1988 if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) {
1989 // fatal if we are starved out.
1990 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
1991 "switchToNextOutput() cannot obtain correct lock sequence");
1992 callbackData->unlock();
1993 continue;
1994 }
1995 callbackData->mSwitching = true; // begin track switch
Wei Jiaadee56a2016-08-05 15:40:34 -07001996 callbackData->setOutput(NULL);
Andy Hungd1c74342015-07-07 16:54:23 -07001997#else
1998 // tryBeginTrackSwitch() returns false if the callback has the lock.
1999 if (!mCallbackData->tryBeginTrackSwitch()) {
2000 // fatal if we are starved out.
2001 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
2002 "switchToNextOutput() cannot obtain callback lock");
2003 mLock.unlock();
2004 usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput
2005 mLock.lock();
2006 continue;
2007 }
2008#endif
2009 }
2010
2011 Mutex::Autolock nextLock(mNextOutput->mLock);
2012
2013 // If the next output track is not NULL, then it has been
2014 // opened already for playback.
2015 // This is possible even without the next player being started,
2016 // for example, the next player could be prepared and seeked.
2017 //
2018 // Presuming it isn't advisable to force the track over.
2019 if (mNextOutput->mTrack == NULL) {
2020 ALOGD("Recycling track for gapless playback");
2021 delete mNextOutput->mCallbackData;
2022 mNextOutput->mCallbackData = mCallbackData;
2023 mNextOutput->mRecycledTrack = mTrack;
2024 mNextOutput->mSampleRateHz = mSampleRateHz;
2025 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Andy Hungd1c74342015-07-07 16:54:23 -07002026 mNextOutput->mFlags = mFlags;
2027 mNextOutput->mFrameSize = mFrameSize;
2028 close_l();
2029 mCallbackData = NULL; // destruction handled by mNextOutput
2030 } else {
2031 ALOGW("Ignoring gapless playback because next player has already started");
2032 // remove track in case resource needed for future players.
2033 if (mCallbackData != NULL) {
2034 mCallbackData->endTrackSwitch(); // release lock for callbacks before close.
2035 }
2036 close_l();
2037 }
2038 }
2039 break;
Marco Nelissen6b74d672012-02-28 16:07:44 -08002040 }
2041}
2042
Wei Jia7d3f4df2015-03-03 15:28:00 -08002043ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002044{
Andy Hungd1c74342015-07-07 16:54:23 -07002045 Mutex::Autolock lock(mLock);
Glenn Kastenadad3d72014-02-21 14:51:43 -08002046 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07002047
Steve Block3856b092011-10-20 11:56:00 +01002048 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07002049 if (mTrack != 0) {
Andy Hung2f6e73d2016-04-08 12:12:58 -07002050 return mTrack->write(buffer, size, blocking);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07002051 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002052 return NO_INIT;
2053}
2054
2055void MediaPlayerService::AudioOutput::stop()
2056{
Steve Block3856b092011-10-20 11:56:00 +01002057 ALOGV("stop");
Andy Hungd1c74342015-07-07 16:54:23 -07002058 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07002059 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002060}
2061
2062void MediaPlayerService::AudioOutput::flush()
2063{
Steve Block3856b092011-10-20 11:56:00 +01002064 ALOGV("flush");
Andy Hungd1c74342015-07-07 16:54:23 -07002065 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07002066 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002067}
2068
2069void MediaPlayerService::AudioOutput::pause()
2070{
Steve Block3856b092011-10-20 11:56:00 +01002071 ALOGV("pause");
Andy Hungd1c74342015-07-07 16:54:23 -07002072 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07002073 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002074}
2075
2076void MediaPlayerService::AudioOutput::close()
2077{
Steve Block3856b092011-10-20 11:56:00 +01002078 ALOGV("close");
Erik Wolsheimer7845a1f2015-10-30 12:07:52 -07002079 sp<AudioTrack> track;
2080 {
2081 Mutex::Autolock lock(mLock);
2082 track = mTrack;
2083 close_l(); // clears mTrack
2084 }
2085 // destruction of the track occurs outside of mutex.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002086}
2087
2088void MediaPlayerService::AudioOutput::setVolume(float left, float right)
2089{
Steve Block3856b092011-10-20 11:56:00 +01002090 ALOGV("setVolume(%f, %f)", left, right);
Andy Hungd1c74342015-07-07 16:54:23 -07002091 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002092 mLeftVolume = left;
2093 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07002094 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002095 mTrack->setVolume(left, right);
2096 }
2097}
2098
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002099status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002100{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002101 ALOGV("setPlaybackRate(%f %f %d %d)",
2102 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Andy Hungd1c74342015-07-07 16:54:23 -07002103 Mutex::Autolock lock(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002104 if (mTrack == 0) {
2105 // remember rate so that we can set it when the track is opened
2106 mPlaybackRate = rate;
2107 return OK;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002108 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002109 status_t res = mTrack->setPlaybackRate(rate);
2110 if (res != NO_ERROR) {
2111 return res;
2112 }
2113 // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
2114 CHECK_GT(rate.mSpeed, 0.f);
2115 mPlaybackRate = rate;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002116 if (mSampleRateHz != 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002117 mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002118 }
2119 return res;
2120}
2121
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002122status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
2123{
2124 ALOGV("setPlaybackRate");
Andy Hungd1c74342015-07-07 16:54:23 -07002125 Mutex::Autolock lock(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002126 if (mTrack == 0) {
2127 return NO_INIT;
2128 }
2129 *rate = mTrack->getPlaybackRate();
2130 return NO_ERROR;
2131}
2132
Eric Laurent2beeb502010-07-16 07:43:46 -07002133status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
2134{
Steve Block3856b092011-10-20 11:56:00 +01002135 ALOGV("setAuxEffectSendLevel(%f)", level);
Andy Hungd1c74342015-07-07 16:54:23 -07002136 Mutex::Autolock lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -07002137 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07002138 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07002139 return mTrack->setAuxEffectSendLevel(level);
2140 }
2141 return NO_ERROR;
2142}
2143
2144status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
2145{
Steve Block3856b092011-10-20 11:56:00 +01002146 ALOGV("attachAuxEffect(%d)", effectId);
Andy Hungd1c74342015-07-07 16:54:23 -07002147 Mutex::Autolock lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -07002148 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07002149 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07002150 return mTrack->attachAuxEffect(effectId);
2151 }
2152 return NO_ERROR;
2153}
2154
Andreas Huber20111aa2009-07-14 16:56:47 -07002155// static
2156void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07002157 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01002158 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08002159 CallbackData *data = (CallbackData*)cookie;
Andy Hungd1c74342015-07-07 16:54:23 -07002160 // lock to ensure we aren't caught in the middle of a track switch.
Marco Nelissen6b74d672012-02-28 16:07:44 -08002161 data->lock();
2162 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07002163 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08002164 if (me == NULL) {
2165 // no output set, likely because the track was scheduled to be reused
2166 // by another player, but the format turned out to be incompatible.
2167 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002168 if (buffer != NULL) {
2169 buffer->size = 0;
2170 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08002171 return;
2172 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002173
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002174 switch(event) {
2175 case AudioTrack::EVENT_MORE_DATA: {
2176 size_t actualSize = (*me->mCallback)(
2177 me, buffer->raw, buffer->size, me->mCallbackCookie,
2178 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002179
Andy Hung719b46b2015-05-31 22:18:25 -07002180 // Log when no data is returned from the callback.
2181 // (1) We may have no data (especially with network streaming sources).
2182 // (2) We may have reached the EOS and the audio track is not stopped yet.
2183 // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
2184 // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill).
2185 //
2186 // This is a benign busy-wait, with the next data request generated 10 ms or more later;
2187 // nevertheless for power reasons, we don't want to see too many of these.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08002188
Andy Hung719b46b2015-05-31 22:18:25 -07002189 ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned");
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002190
2191 buffer->size = actualSize;
2192 } break;
2193
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002194 case AudioTrack::EVENT_STREAM_END:
Andy Hung719b46b2015-05-31 22:18:25 -07002195 // currently only occurs for offloaded callbacks
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002196 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
2197 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
2198 me->mCallbackCookie, CB_EVENT_STREAM_END);
2199 break;
2200
2201 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
2202 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
2203 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
2204 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
2205 break;
2206
Glenn Kasten421743b2015-06-01 08:18:08 -07002207 case AudioTrack::EVENT_UNDERRUN:
Andy Hung719b46b2015-05-31 22:18:25 -07002208 // This occurs when there is no data available, typically
Glenn Kasten421743b2015-06-01 08:18:08 -07002209 // when there is a failure to supply data to the AudioTrack. It can also
2210 // occur in non-offloaded mode when the audio device comes out of standby.
2211 //
Andy Hung719b46b2015-05-31 22:18:25 -07002212 // If an AudioTrack underruns it outputs silence. Since this happens suddenly
2213 // it may sound like an audible pop or glitch.
2214 //
2215 // The underrun event is sent once per track underrun; the condition is reset
2216 // when more data is sent to the AudioTrack.
Eric Laurente93cc032016-05-05 10:15:10 -07002217 ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)");
Glenn Kasten421743b2015-06-01 08:18:08 -07002218 break;
2219
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002220 default:
2221 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07002222 }
2223
Marco Nelissen6b74d672012-02-28 16:07:44 -08002224 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07002225}
2226
Glenn Kastend848eb42016-03-08 13:42:11 -08002227audio_session_t MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002228{
Andy Hungd1c74342015-07-07 16:54:23 -07002229 Mutex::Autolock lock(mLock);
Eric Laurent8c563ed2010-10-07 18:23:03 -07002230 return mSessionId;
2231}
2232
Eric Laurent6f59db12013-07-26 17:16:50 -07002233uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
2234{
Andy Hungd1c74342015-07-07 16:54:23 -07002235 Mutex::Autolock lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -07002236 if (mTrack == 0) return 0;
2237 return mTrack->getSampleRate();
2238}
2239
Andy Hungf2c87b32016-04-07 19:49:29 -07002240int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const
2241{
2242 Mutex::Autolock lock(mLock);
2243 if (mTrack == 0) {
2244 return 0;
2245 }
2246 int64_t duration;
2247 if (mTrack->getBufferDurationInUs(&duration) != OK) {
2248 return 0;
2249 }
2250 return duration;
2251}
2252
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002253////////////////////////////////////////////////////////////////////////////////
2254
2255struct CallbackThread : public Thread {
2256 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2257 MediaPlayerBase::AudioSink::AudioCallback cb,
2258 void *cookie);
2259
2260protected:
2261 virtual ~CallbackThread();
2262
2263 virtual bool threadLoop();
2264
2265private:
2266 wp<MediaPlayerBase::AudioSink> mSink;
2267 MediaPlayerBase::AudioSink::AudioCallback mCallback;
2268 void *mCookie;
2269 void *mBuffer;
2270 size_t mBufferSize;
2271
2272 CallbackThread(const CallbackThread &);
2273 CallbackThread &operator=(const CallbackThread &);
2274};
2275
2276CallbackThread::CallbackThread(
2277 const wp<MediaPlayerBase::AudioSink> &sink,
2278 MediaPlayerBase::AudioSink::AudioCallback cb,
2279 void *cookie)
2280 : mSink(sink),
2281 mCallback(cb),
2282 mCookie(cookie),
2283 mBuffer(NULL),
2284 mBufferSize(0) {
2285}
2286
2287CallbackThread::~CallbackThread() {
2288 if (mBuffer) {
2289 free(mBuffer);
2290 mBuffer = NULL;
2291 }
2292}
2293
2294bool CallbackThread::threadLoop() {
2295 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2296 if (sink == NULL) {
2297 return false;
2298 }
2299
2300 if (mBuffer == NULL) {
2301 mBufferSize = sink->bufferSize();
2302 mBuffer = malloc(mBufferSize);
2303 }
2304
2305 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002306 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2307 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002308
2309 if (actualSize > 0) {
2310 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07002311 // Could return false on sink->write() error or short count.
2312 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002313 }
2314
2315 return true;
2316}
2317
2318////////////////////////////////////////////////////////////////////////////////
2319
Gloria Wang7cf180c2011-02-19 18:37:57 -08002320void MediaPlayerService::addBatteryData(uint32_t params)
2321{
2322 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002323
2324 int32_t time = systemTime() / 1000000L;
2325
2326 // change audio output devices. This notification comes from AudioFlinger
2327 if ((params & kBatteryDataSpeakerOn)
2328 || (params & kBatteryDataOtherAudioDeviceOn)) {
2329
2330 int deviceOn[NUM_AUDIO_DEVICES];
2331 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2332 deviceOn[i] = 0;
2333 }
2334
2335 if ((params & kBatteryDataSpeakerOn)
2336 && (params & kBatteryDataOtherAudioDeviceOn)) {
2337 deviceOn[SPEAKER_AND_OTHER] = 1;
2338 } else if (params & kBatteryDataSpeakerOn) {
2339 deviceOn[SPEAKER] = 1;
2340 } else {
2341 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2342 }
2343
2344 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2345 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2346
2347 if (mBatteryAudio.refCount > 0) { // if playing audio
2348 if (!deviceOn[i]) {
2349 mBatteryAudio.lastTime[i] += time;
2350 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2351 mBatteryAudio.lastTime[i] = 0;
2352 } else {
2353 mBatteryAudio.lastTime[i] = 0 - time;
2354 }
2355 }
2356
2357 mBatteryAudio.deviceOn[i] = deviceOn[i];
2358 }
2359 }
2360 return;
2361 }
2362
Marco Nelissenb7848f12014-12-04 08:57:56 -08002363 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08002364 if (params & kBatteryDataAudioFlingerStart) {
2365 // record the start time only if currently no other audio
2366 // is being played
2367 if (mBatteryAudio.refCount == 0) {
2368 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2369 if (mBatteryAudio.deviceOn[i]) {
2370 mBatteryAudio.lastTime[i] -= time;
2371 }
2372 }
2373 }
2374
2375 mBatteryAudio.refCount ++;
2376 return;
2377
2378 } else if (params & kBatteryDataAudioFlingerStop) {
2379 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002380 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002381 return;
2382 }
2383
2384 // record the stop time only if currently this is the only
2385 // audio being played
2386 if (mBatteryAudio.refCount == 1) {
2387 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2388 if (mBatteryAudio.deviceOn[i]) {
2389 mBatteryAudio.lastTime[i] += time;
2390 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2391 mBatteryAudio.lastTime[i] = 0;
2392 }
2393 }
2394 }
2395
2396 mBatteryAudio.refCount --;
2397 return;
2398 }
2399
Gloria Wang7cf180c2011-02-19 18:37:57 -08002400 int uid = IPCThreadState::self()->getCallingUid();
2401 if (uid == AID_MEDIA) {
2402 return;
2403 }
2404 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002405
2406 if (index < 0) { // create a new entry for this UID
2407 BatteryUsageInfo info;
2408 info.audioTotalTime = 0;
2409 info.videoTotalTime = 0;
2410 info.audioLastTime = 0;
2411 info.videoLastTime = 0;
2412 info.refCount = 0;
2413
Gloria Wang9ee159b2011-02-24 14:51:45 -08002414 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002415 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002416 return;
2417 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002418 }
2419
2420 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2421
2422 if (params & kBatteryDataCodecStarted) {
2423 if (params & kBatteryDataTrackAudio) {
2424 info.audioLastTime -= time;
2425 info.refCount ++;
2426 }
2427 if (params & kBatteryDataTrackVideo) {
2428 info.videoLastTime -= time;
2429 info.refCount ++;
2430 }
2431 } else {
2432 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002433 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002434 return;
2435 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002436 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002437 mBatteryData.removeItem(uid);
2438 return;
2439 }
2440
2441 if (params & kBatteryDataTrackAudio) {
2442 info.audioLastTime += time;
2443 info.refCount --;
2444 }
2445 if (params & kBatteryDataTrackVideo) {
2446 info.videoLastTime += time;
2447 info.refCount --;
2448 }
2449
2450 // no stream is being played by this UID
2451 if (info.refCount == 0) {
2452 info.audioTotalTime += info.audioLastTime;
2453 info.audioLastTime = 0;
2454 info.videoTotalTime += info.videoLastTime;
2455 info.videoLastTime = 0;
2456 }
2457 }
2458}
2459
2460status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2461 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002462
2463 // audio output devices usage
2464 int32_t time = systemTime() / 1000000L; //in ms
2465 int32_t totalTime;
2466
2467 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2468 totalTime = mBatteryAudio.totalTime[i];
2469
2470 if (mBatteryAudio.deviceOn[i]
2471 && (mBatteryAudio.lastTime[i] != 0)) {
2472 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2473 totalTime += tmpTime;
2474 }
2475
2476 reply->writeInt32(totalTime);
2477 // reset the total time
2478 mBatteryAudio.totalTime[i] = 0;
2479 }
2480
2481 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002482 BatteryUsageInfo info;
2483 int size = mBatteryData.size();
2484
2485 reply->writeInt32(size);
2486 int i = 0;
2487
2488 while (i < size) {
2489 info = mBatteryData.valueAt(i);
2490
2491 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2492 reply->writeInt32(info.audioTotalTime);
2493 reply->writeInt32(info.videoTotalTime);
2494
2495 info.audioTotalTime = 0;
2496 info.videoTotalTime = 0;
2497
2498 // remove the UID entry where no stream is being played
2499 if (info.refCount <= 0) {
2500 mBatteryData.removeItemsAt(i);
2501 size --;
2502 i --;
2503 }
2504 i++;
2505 }
2506 return NO_ERROR;
2507}
nikoa64c8c72009-07-20 15:07:26 -07002508} // namespace android