blob: 8208f28d20aa99e1e86c43e3b840a739223bcc5f [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 Giro37d7af32016-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;
Pawin Vongmasac7fe1652017-12-12 01:56:40 -0800606 mListener = new Listener(this);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700607
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800608#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000609 ALOGD("create Antagonizer");
Pawin Vongmasac7fe1652017-12-12 01:56:40 -0800610 mAntagonizer = new Antagonizer(mListener);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800611#endif
612}
613
614MediaPlayerService::Client::~Client()
615{
Steve Block3856b092011-10-20 11:56:00 +0100616 ALOGV("Client(%d) destructor pid = %d", mConnId, mPid);
Wei Jia502c2f42017-07-13 17:47:56 -0700617 {
618 Mutex::Autolock l(mLock);
619 mAudioOutput.clear();
620 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800621 wp<Client> client(this);
622 disconnect();
623 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700624 if (mAudioAttributes != NULL) {
625 free(mAudioAttributes);
626 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800627}
628
629void MediaPlayerService::Client::disconnect()
630{
Steve Block3856b092011-10-20 11:56:00 +0100631 ALOGV("disconnect(%d) from pid %d", mConnId, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800632 // grab local reference and clear main reference to prevent future
633 // access to object
634 sp<MediaPlayerBase> p;
635 {
636 Mutex::Autolock l(mLock);
637 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800638 mClient.clear();
Wei Jia502c2f42017-07-13 17:47:56 -0700639 mPlayer.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800640 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700641
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800642 // clear the notification to prevent callbacks to dead client
643 // and reset the player. We assume the player will serialize
644 // access to itself if necessary.
645 if (p != 0) {
Pawin Vongmasac7fe1652017-12-12 01:56:40 -0800646 p->setNotifyCallback(0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800647#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000648 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800649 mAntagonizer->kill();
650#endif
651 p->reset();
652 }
653
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700654 {
655 Mutex::Autolock l(mLock);
656 disconnectNativeWindow_l();
657 }
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700658
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800659 IPCThreadState::self()->flushCommands();
660}
661
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800662sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
663{
664 // determine if we have the right player type
Wei Jia502c2f42017-07-13 17:47:56 -0700665 sp<MediaPlayerBase> p = getPlayer();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800666 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100667 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668 p.clear();
669 }
670 if (p == NULL) {
Pawin Vongmasac7fe1652017-12-12 01:56:40 -0800671 p = MediaPlayerFactory::createPlayer(playerType, mListener, mPid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800672 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700673
Jason Simmonsdb29e522011-08-12 13:46:55 -0700674 if (p != NULL) {
675 p->setUID(mUID);
676 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700677
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800678 return p;
679}
680
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700681MediaPlayerService::Client::ServiceDeathNotifier::ServiceDeathNotifier(
682 const sp<IBinder>& service,
683 const sp<MediaPlayerBase>& listener,
684 int which) {
685 mService = service;
686 mListener = listener;
687 mWhich = which;
688}
689
690MediaPlayerService::Client::ServiceDeathNotifier::~ServiceDeathNotifier() {
691 mService->unlinkToDeath(this);
692}
693
694void MediaPlayerService::Client::ServiceDeathNotifier::binderDied(const wp<IBinder>& /*who*/) {
695 sp<MediaPlayerBase> listener = mListener.promote();
696 if (listener != NULL) {
697 listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED, mWhich);
698 } else {
699 ALOGW("listener for process %d death is gone", mWhich);
700 }
701}
702
John Grossmanc795b642012-02-22 15:38:35 -0800703sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
704 player_type playerType)
705{
706 ALOGV("player type = %d", playerType);
707
708 // create the right type of player
709 sp<MediaPlayerBase> p = createPlayer(playerType);
710 if (p == NULL) {
711 return p;
712 }
713
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700714 sp<IServiceManager> sm = defaultServiceManager();
715 sp<IBinder> binder = sm->getService(String16("media.extractor"));
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700716 sp<ServiceDeathNotifier> extractorDeathListener =
717 new ServiceDeathNotifier(binder, p, MEDIAEXTRACTOR_PROCESS_DEATH);
718 binder->linkToDeath(extractorDeathListener);
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700719
720 binder = sm->getService(String16("media.codec"));
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700721 sp<ServiceDeathNotifier> codecDeathListener =
722 new ServiceDeathNotifier(binder, p, MEDIACODEC_PROCESS_DEATH);
723 binder->linkToDeath(codecDeathListener);
724
725 Mutex::Autolock lock(mLock);
726
727 mExtractorDeathListener = extractorDeathListener;
728 mCodecDeathListener = codecDeathListener;
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700729
John Grossmanc795b642012-02-22 15:38:35 -0800730 if (!p->hardwareOutput()) {
Marco Nelissend457c972014-02-11 08:47:07 -0800731 mAudioOutput = new AudioOutput(mAudioSessionId, IPCThreadState::self()->getCallingUid(),
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700732 mPid, mAudioAttributes);
John Grossmanc795b642012-02-22 15:38:35 -0800733 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
734 }
735
736 return p;
737}
738
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700739status_t MediaPlayerService::Client::setDataSource_post(
John Grossmanc795b642012-02-22 15:38:35 -0800740 const sp<MediaPlayerBase>& p,
741 status_t status)
742{
743 ALOGV(" setDataSource");
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700744 if (status != OK) {
745 ALOGE(" error: %d", status);
746 return status;
John Grossmanc795b642012-02-22 15:38:35 -0800747 }
748
749 // Set the re-transmission endpoint if one was chosen.
750 if (mRetransmitEndpointValid) {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700751 status = p->setRetransmitEndpoint(&mRetransmitEndpoint);
752 if (status != NO_ERROR) {
753 ALOGE("setRetransmitEndpoint error: %d", status);
John Grossmanc795b642012-02-22 15:38:35 -0800754 }
755 }
756
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700757 if (status == OK) {
758 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -0800759 mPlayer = p;
760 }
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700761 return status;
John Grossmanc795b642012-02-22 15:38:35 -0800762}
763
Andreas Huber2db84552010-01-28 11:19:57 -0800764status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -0800765 const sp<IMediaHTTPService> &httpService,
766 const char *url,
767 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800768{
Steve Block3856b092011-10-20 11:56:00 +0100769 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800770 if (url == NULL)
771 return UNKNOWN_ERROR;
772
Dave Burked681bbb2011-08-30 14:39:17 +0100773 if ((strncmp(url, "http://", 7) == 0) ||
774 (strncmp(url, "https://", 8) == 0) ||
775 (strncmp(url, "rtsp://", 7) == 0)) {
776 if (!checkPermission("android.permission.INTERNET")) {
777 return PERMISSION_DENIED;
778 }
779 }
780
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800781 if (strncmp(url, "content://", 10) == 0) {
782 // get a filedescriptor for the content Uri and
783 // pass it to the setDataSource(fd) method
784
785 String16 url16(url);
786 int fd = android::openContentProviderFile(url16);
787 if (fd < 0)
788 {
Steve Block29357bc2012-01-06 19:20:56 +0000789 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800790 return UNKNOWN_ERROR;
791 }
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700792 status_t status = setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800793 close(fd);
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700794 return mStatus = status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800795 } else {
John Grossman44a7e422012-06-21 17:29:24 -0700796 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -0800797 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
798 if (p == NULL) {
799 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800800 }
801
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700802 return mStatus =
803 setDataSource_post(
804 p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800805 }
806}
807
808status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
809{
Marco Nelissen83b0fd92015-09-16 13:48:07 -0700810 ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
811 fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800812 struct stat sb;
813 int ret = fstat(fd, &sb);
814 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +0000815 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800816 return UNKNOWN_ERROR;
817 }
818
Andy Hung833b4752016-04-04 17:15:48 -0700819 ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev));
Steve Block3856b092011-10-20 11:56:00 +0100820 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -0700821 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
822 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Andy Hung833b4752016-04-04 17:15:48 -0700823 ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800824
825 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +0000826 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800827 return UNKNOWN_ERROR;
828 }
829 if (offset + length > sb.st_size) {
830 length = sb.st_size - offset;
Andy Hung833b4752016-04-04 17:15:48 -0700831 ALOGV("calculated length = %lld", (long long)length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800832 }
833
John Grossman44a7e422012-06-21 17:29:24 -0700834 player_type playerType = MediaPlayerFactory::getPlayerType(this,
835 fd,
836 offset,
837 length);
John Grossmanc795b642012-02-22 15:38:35 -0800838 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
839 if (p == NULL) {
840 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800841 }
842
843 // now set data source
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700844 return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800845}
846
Andreas Hubere2b10282010-11-23 11:41:34 -0800847status_t MediaPlayerService::Client::setDataSource(
848 const sp<IStreamSource> &source) {
849 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -0700850 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -0800851 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -0800852 if (p == NULL) {
853 return NO_INIT;
854 }
855
Andreas Hubere2b10282010-11-23 11:41:34 -0800856 // now set data source
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700857 return mStatus = setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -0800858}
859
Chris Watkins99f31602015-03-20 13:06:33 -0700860status_t MediaPlayerService::Client::setDataSource(
861 const sp<IDataSource> &source) {
862 sp<DataSource> dataSource = DataSource::CreateFromIDataSource(source);
863 player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
864 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
865 if (p == NULL) {
866 return NO_INIT;
867 }
868 // now set data source
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700869 return mStatus = setDataSource_post(p, p->setDataSource(dataSource));
Chris Watkins99f31602015-03-20 13:06:33 -0700870}
871
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700872void MediaPlayerService::Client::disconnectNativeWindow_l() {
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700873 if (mConnectedWindow != NULL) {
874 status_t err = native_window_api_disconnect(mConnectedWindow.get(),
875 NATIVE_WINDOW_API_MEDIA);
876
877 if (err != OK) {
Steve Block5ff1dd52012-01-05 23:22:43 +0000878 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700879 strerror(-err), err);
880 }
881 }
882 mConnectedWindow.clear();
883}
884
Glenn Kasten11731182011-02-08 17:26:17 -0800885status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -0800886 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -0800887{
Andy McFadden484566c2012-12-18 09:46:54 -0800888 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -0800889 sp<MediaPlayerBase> p = getPlayer();
890 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700891
Marco Nelissenf8880202014-11-14 07:58:25 -0800892 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700893 if (mConnectedWindowBinder == binder) {
894 return OK;
895 }
896
897 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -0800898 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -0700899 anw = new Surface(bufferProducer, true /* controlledByApp */);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700900 status_t err = native_window_api_connect(anw.get(),
901 NATIVE_WINDOW_API_MEDIA);
902
903 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +0000904 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700905 // Note that we must do the reset before disconnecting from the ANW.
906 // Otherwise queue/dequeue calls could be made on the disconnected
907 // ANW, which may result in errors.
908 reset();
909
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700910 Mutex::Autolock lock(mLock);
911 disconnectNativeWindow_l();
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700912
913 return err;
914 }
915 }
916
Andy McFadden484566c2012-12-18 09:46:54 -0800917 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700918 // disconnecting the old one. Otherwise queue/dequeue calls could be made
919 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -0800920 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700921
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700922 mLock.lock();
923 disconnectNativeWindow_l();
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700924
925 if (err == OK) {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700926 mConnectedWindow = anw;
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700927 mConnectedWindowBinder = binder;
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700928 mLock.unlock();
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700929 } else {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -0700930 mLock.unlock();
931 status_t err = native_window_api_disconnect(
932 anw.get(), NATIVE_WINDOW_API_MEDIA);
933
934 if (err != OK) {
935 ALOGW("native_window_api_disconnect returned an error: %s (%d)",
936 strerror(-err), err);
937 }
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700938 }
939
940 return err;
Glenn Kasten11731182011-02-08 17:26:17 -0800941}
942
Nicolas Catania1d187f12009-05-12 23:25:55 -0700943status_t MediaPlayerService::Client::invoke(const Parcel& request,
944 Parcel *reply)
945{
946 sp<MediaPlayerBase> p = getPlayer();
947 if (p == NULL) return UNKNOWN_ERROR;
948 return p->invoke(request, reply);
949}
950
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700951// This call doesn't need to access the native player.
952status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
953{
954 status_t status;
nikoa64c8c72009-07-20 15:07:26 -0700955 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700956
Nicolas Catania48290382009-07-10 13:53:06 -0700957 if (unmarshallFilter(filter, &allow, &status) &&
958 unmarshallFilter(filter, &drop, &status)) {
959 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700960
961 mMetadataAllow = allow;
962 mMetadataDrop = drop;
963 }
964 return status;
965}
966
Nicolas Catania48290382009-07-10 13:53:06 -0700967status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -0700968 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700969{
nikoa64c8c72009-07-20 15:07:26 -0700970 sp<MediaPlayerBase> player = getPlayer();
971 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -0700972
nikod608a812009-07-16 16:39:53 -0700973 status_t status;
974 // Placeholder for the return code, updated by the caller.
975 reply->writeInt32(-1);
976
nikoa64c8c72009-07-20 15:07:26 -0700977 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -0700978
979 // We don't block notifications while we fetch the data. We clear
980 // mMetadataUpdated first so we don't lose notifications happening
981 // during the rest of this call.
982 {
983 Mutex::Autolock lock(mLock);
984 if (update_only) {
nikod608a812009-07-16 16:39:53 -0700985 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -0700986 }
987 mMetadataUpdated.clear();
988 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -0700989
nikoa64c8c72009-07-20 15:07:26 -0700990 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -0700991
nikoa64c8c72009-07-20 15:07:26 -0700992 metadata.appendHeader();
993 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -0700994
995 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -0700996 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +0000997 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -0700998 return status;
999 }
1000
1001 // FIXME: Implement filtering on the result. Not critical since
1002 // filtering takes place on the update notifications already. This
1003 // would be when all the metadata are fetch and a filter is set.
1004
nikod608a812009-07-16 16:39:53 -07001005 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -07001006 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -07001007 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -07001008}
1009
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001010status_t MediaPlayerService::Client::prepareAsync()
1011{
Steve Block3856b092011-10-20 11:56:00 +01001012 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013 sp<MediaPlayerBase> p = getPlayer();
1014 if (p == 0) return UNKNOWN_ERROR;
1015 status_t ret = p->prepareAsync();
1016#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +00001017 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001018 if (ret == NO_ERROR) mAntagonizer->start();
1019#endif
1020 return ret;
1021}
1022
1023status_t MediaPlayerService::Client::start()
1024{
Steve Block3856b092011-10-20 11:56:00 +01001025 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001026 sp<MediaPlayerBase> p = getPlayer();
1027 if (p == 0) return UNKNOWN_ERROR;
1028 p->setLooping(mLoop);
1029 return p->start();
1030}
1031
1032status_t MediaPlayerService::Client::stop()
1033{
Steve Block3856b092011-10-20 11:56:00 +01001034 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001035 sp<MediaPlayerBase> p = getPlayer();
1036 if (p == 0) return UNKNOWN_ERROR;
1037 return p->stop();
1038}
1039
1040status_t MediaPlayerService::Client::pause()
1041{
Steve Block3856b092011-10-20 11:56:00 +01001042 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001043 sp<MediaPlayerBase> p = getPlayer();
1044 if (p == 0) return UNKNOWN_ERROR;
1045 return p->pause();
1046}
1047
1048status_t MediaPlayerService::Client::isPlaying(bool* state)
1049{
1050 *state = false;
1051 sp<MediaPlayerBase> p = getPlayer();
1052 if (p == 0) return UNKNOWN_ERROR;
1053 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +01001054 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001055 return NO_ERROR;
1056}
1057
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001058status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -08001059{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001060 ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
1061 mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Wei Jia98160162015-02-04 17:01:11 -08001062 sp<MediaPlayerBase> p = getPlayer();
1063 if (p == 0) return UNKNOWN_ERROR;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001064 return p->setPlaybackSettings(rate);
1065}
1066
1067status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
1068{
1069 sp<MediaPlayerBase> p = getPlayer();
1070 if (p == 0) return UNKNOWN_ERROR;
1071 status_t ret = p->getPlaybackSettings(rate);
1072 if (ret == NO_ERROR) {
1073 ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
1074 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
1075 } else {
1076 ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
1077 }
1078 return ret;
1079}
1080
1081status_t MediaPlayerService::Client::setSyncSettings(
1082 const AVSyncSettings& sync, float videoFpsHint)
1083{
1084 ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
1085 mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
1086 sp<MediaPlayerBase> p = getPlayer();
1087 if (p == 0) return UNKNOWN_ERROR;
1088 return p->setSyncSettings(sync, videoFpsHint);
1089}
1090
1091status_t MediaPlayerService::Client::getSyncSettings(
1092 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
1093{
1094 sp<MediaPlayerBase> p = getPlayer();
1095 if (p == 0) return UNKNOWN_ERROR;
1096 status_t ret = p->getSyncSettings(sync, videoFps);
1097 if (ret == NO_ERROR) {
1098 ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
1099 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
1100 } else {
1101 ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
1102 }
1103 return ret;
Wei Jia98160162015-02-04 17:01:11 -08001104}
1105
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001106status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1107{
Steve Block3856b092011-10-20 11:56:00 +01001108 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001109 sp<MediaPlayerBase> p = getPlayer();
1110 if (p == 0) return UNKNOWN_ERROR;
1111 status_t ret = p->getCurrentPosition(msec);
1112 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001113 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001114 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001115 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001116 }
1117 return ret;
1118}
1119
1120status_t MediaPlayerService::Client::getDuration(int *msec)
1121{
Steve Block3856b092011-10-20 11:56:00 +01001122 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001123 sp<MediaPlayerBase> p = getPlayer();
1124 if (p == 0) return UNKNOWN_ERROR;
1125 status_t ret = p->getDuration(msec);
1126 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001127 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001128 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001129 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001130 }
1131 return ret;
1132}
1133
Marco Nelissen6b74d672012-02-28 16:07:44 -08001134status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1135 ALOGV("setNextPlayer");
1136 Mutex::Autolock l(mLock);
1137 sp<Client> c = static_cast<Client*>(player.get());
Wei Jia28284122016-08-30 13:49:06 -07001138 if (c != NULL && !mService->hasClient(c)) {
Robert Shihee0a0e32016-08-16 16:50:54 -07001139 return BAD_VALUE;
1140 }
1141
Marco Nelissen6b74d672012-02-28 16:07:44 -08001142 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001143
1144 if (c != NULL) {
1145 if (mAudioOutput != NULL) {
1146 mAudioOutput->setNextOutput(c->mAudioOutput);
1147 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1148 ALOGE("no current audio output");
1149 }
1150
1151 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1152 mPlayer->setNextPlayer(mNextClient->getPlayer());
1153 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001154 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001155
Marco Nelissen6b74d672012-02-28 16:07:44 -08001156 return OK;
1157}
1158
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001159status_t MediaPlayerService::Client::seekTo(int msec)
1160{
Steve Block3856b092011-10-20 11:56:00 +01001161 ALOGV("[%d] seekTo(%d)", mConnId, msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001162 sp<MediaPlayerBase> p = getPlayer();
1163 if (p == 0) return UNKNOWN_ERROR;
1164 return p->seekTo(msec);
1165}
1166
1167status_t MediaPlayerService::Client::reset()
1168{
Steve Block3856b092011-10-20 11:56:00 +01001169 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001170 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001171 sp<MediaPlayerBase> p = getPlayer();
1172 if (p == 0) return UNKNOWN_ERROR;
1173 return p->reset();
1174}
1175
Glenn Kastenfff6d712012-01-12 16:38:12 -08001176status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001177{
Steve Block3856b092011-10-20 11:56:00 +01001178 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001179 // TODO: for hardware output, call player instead
1180 Mutex::Autolock l(mLock);
1181 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1182 return NO_ERROR;
1183}
1184
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001185status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1186{
1187 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1188 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001189 if (mAudioAttributes == NULL) {
1190 return NO_MEMORY;
1191 }
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001192 unmarshallAudioAttributes(parcel, mAudioAttributes);
1193
1194 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1195 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1196 mAudioAttributes->tags);
1197
1198 if (mAudioOutput != 0) {
1199 mAudioOutput->setAudioAttributes(mAudioAttributes);
1200 }
1201 return NO_ERROR;
1202}
1203
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001204status_t MediaPlayerService::Client::setLooping(int loop)
1205{
Steve Block3856b092011-10-20 11:56:00 +01001206 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001207 mLoop = loop;
1208 sp<MediaPlayerBase> p = getPlayer();
1209 if (p != 0) return p->setLooping(loop);
1210 return NO_ERROR;
1211}
1212
1213status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1214{
Steve Block3856b092011-10-20 11:56:00 +01001215 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001216
1217 // for hardware output, call player instead
1218 sp<MediaPlayerBase> p = getPlayer();
1219 {
1220 Mutex::Autolock l(mLock);
1221 if (p != 0 && p->hardwareOutput()) {
1222 MediaPlayerHWInterface* hwp =
1223 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1224 return hwp->setVolume(leftVolume, rightVolume);
1225 } else {
1226 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1227 return NO_ERROR;
1228 }
1229 }
1230
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001231 return NO_ERROR;
1232}
1233
Eric Laurent2beeb502010-07-16 07:43:46 -07001234status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1235{
Steve Block3856b092011-10-20 11:56:00 +01001236 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001237 Mutex::Autolock l(mLock);
1238 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1239 return NO_ERROR;
1240}
1241
1242status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1243{
Steve Block3856b092011-10-20 11:56:00 +01001244 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001245 Mutex::Autolock l(mLock);
1246 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1247 return NO_ERROR;
1248}
Nicolas Catania48290382009-07-10 13:53:06 -07001249
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001250status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001251 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001252 switch (key) {
1253 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1254 {
1255 Mutex::Autolock l(mLock);
1256 return setAudioAttributes_l(request);
1257 }
1258 default:
1259 sp<MediaPlayerBase> p = getPlayer();
1260 if (p == 0) { return UNKNOWN_ERROR; }
1261 return p->setParameter(key, request);
1262 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001263}
1264
1265status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001266 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001267 sp<MediaPlayerBase> p = getPlayer();
1268 if (p == 0) return UNKNOWN_ERROR;
1269 return p->getParameter(key, reply);
1270}
1271
John Grossmanc795b642012-02-22 15:38:35 -08001272status_t MediaPlayerService::Client::setRetransmitEndpoint(
1273 const struct sockaddr_in* endpoint) {
1274
1275 if (NULL != endpoint) {
1276 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1277 uint16_t p = ntohs(endpoint->sin_port);
1278 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1279 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1280 } else {
1281 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1282 }
1283
1284 sp<MediaPlayerBase> p = getPlayer();
1285
1286 // Right now, the only valid time to set a retransmit endpoint is before
1287 // player selection has been made (since the presence or absence of a
1288 // retransmit endpoint is going to determine which player is selected during
1289 // setDataSource).
1290 if (p != 0) return INVALID_OPERATION;
1291
1292 if (NULL != endpoint) {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -07001293 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -08001294 mRetransmitEndpoint = *endpoint;
1295 mRetransmitEndpointValid = true;
1296 } else {
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -07001297 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -08001298 mRetransmitEndpointValid = false;
1299 }
1300
1301 return NO_ERROR;
1302}
1303
John Grossman44a7e422012-06-21 17:29:24 -07001304status_t MediaPlayerService::Client::getRetransmitEndpoint(
1305 struct sockaddr_in* endpoint)
1306{
1307 if (NULL == endpoint)
1308 return BAD_VALUE;
1309
1310 sp<MediaPlayerBase> p = getPlayer();
1311
1312 if (p != NULL)
1313 return p->getRetransmitEndpoint(endpoint);
1314
Pawin Vongmasa27a40ba2017-09-28 05:31:11 -07001315 Mutex::Autolock lock(mLock);
John Grossman44a7e422012-06-21 17:29:24 -07001316 if (!mRetransmitEndpointValid)
1317 return NO_INIT;
1318
1319 *endpoint = mRetransmitEndpoint;
1320
1321 return NO_ERROR;
1322}
1323
Gloria Wangb483c472011-04-11 17:23:27 -07001324void MediaPlayerService::Client::notify(
Pawin Vongmasac7fe1652017-12-12 01:56:40 -08001325 int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001326{
James Dongb8a98252012-08-26 16:13:03 -07001327 sp<IMediaPlayerClient> c;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001328 {
Pawin Vongmasac7fe1652017-12-12 01:56:40 -08001329 Mutex::Autolock l(mLock);
1330 c = mClient;
1331 if (msg == MEDIA_PLAYBACK_COMPLETE && mNextClient != NULL) {
1332 if (mAudioOutput != NULL)
1333 mAudioOutput->switchToNextOutput();
1334 mNextClient->start();
1335 if (mNextClient->mClient != NULL) {
1336 mNextClient->mClient->notify(
Wei Jia2afac0c2016-01-07 12:13:07 -08001337 MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1338 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001339 }
1340 }
1341
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001342 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001343 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001344 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001345
Pawin Vongmasac7fe1652017-12-12 01:56:40 -08001346 if(shouldDropMetadata(metadata_type)) {
Nicolas Catania48290382009-07-10 13:53:06 -07001347 return;
1348 }
1349
1350 // Update the list of metadata that have changed. getMetadata
1351 // also access mMetadataUpdated and clears it.
Pawin Vongmasac7fe1652017-12-12 01:56:40 -08001352 addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001353 }
James Dongb8a98252012-08-26 16:13:03 -07001354
1355 if (c != NULL) {
Pawin Vongmasac7fe1652017-12-12 01:56:40 -08001356 ALOGV("[%d] notify (%d, %d, %d)", mConnId, msg, ext1, ext2);
James Dongb8a98252012-08-26 16:13:03 -07001357 c->notify(msg, ext1, ext2, obj);
1358 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001359}
1360
Nicolas Catania48290382009-07-10 13:53:06 -07001361
nikoa64c8c72009-07-20 15:07:26 -07001362bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001363{
Nicolas Catania48290382009-07-10 13:53:06 -07001364 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001365
Nicolas Catania48290382009-07-10 13:53:06 -07001366 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001367 return true;
1368 }
1369
Nicolas Catania48290382009-07-10 13:53:06 -07001370 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001371 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001372 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001373 return true;
1374 }
1375}
1376
Nicolas Catania48290382009-07-10 13:53:06 -07001377
nikoa64c8c72009-07-20 15:07:26 -07001378void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001379 Mutex::Autolock lock(mLock);
1380 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1381 mMetadataUpdated.add(metadata_type);
1382 }
1383}
1384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001385#if CALLBACK_ANTAGONIZER
1386const int Antagonizer::interval = 10000; // 10 msecs
1387
Pawin Vongmasac7fe1652017-12-12 01:56:40 -08001388Antagonizer::Antagonizer(const sp<MediaPlayerBase::Listener> &listener) :
1389 mExit(false), mActive(false), mListener(listener)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001390{
1391 createThread(callbackThread, this);
1392}
1393
1394void Antagonizer::kill()
1395{
1396 Mutex::Autolock _l(mLock);
1397 mActive = false;
1398 mExit = true;
1399 mCondition.wait(mLock);
1400}
1401
1402int Antagonizer::callbackThread(void* user)
1403{
Steve Blockb8a80522011-12-20 16:23:08 +00001404 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001405 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1406 while (!p->mExit) {
1407 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001408 ALOGV("send event");
Pawin Vongmasac7fe1652017-12-12 01:56:40 -08001409 p->mListener->notify(0, 0, 0, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001410 }
1411 usleep(interval);
1412 }
1413 Mutex::Autolock _l(p->mLock);
1414 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001415 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001416 return 0;
1417}
1418#endif
1419
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001420#undef LOG_TAG
1421#define LOG_TAG "AudioSink"
Glenn Kastend848eb42016-03-08 13:42:11 -08001422MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId, int uid, int pid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001423 const audio_attributes_t* attr)
Andreas Huber20111aa2009-07-14 16:56:47 -07001424 : mCallback(NULL),
Eric Laurenta514bdb2010-06-21 09:27:30 -07001425 mCallbackCookie(NULL),
Marco Nelissen6b74d672012-02-28 16:07:44 -08001426 mCallbackData(NULL),
Andy Hungd1c74342015-07-07 16:54:23 -07001427 mStreamType(AUDIO_STREAM_MUSIC),
Andy Hungd1c74342015-07-07 16:54:23 -07001428 mLeftVolume(1.0),
1429 mRightVolume(1.0),
1430 mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
1431 mSampleRateHz(0),
1432 mMsecsPerFrame(0),
1433 mFrameSize(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001434 mSessionId(sessionId),
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001435 mUid(uid),
Marco Nelissend457c972014-02-11 08:47:07 -08001436 mPid(pid),
Andy Hungd1c74342015-07-07 16:54:23 -07001437 mSendLevel(0.0),
1438 mAuxEffectId(0),
1439 mFlags(AUDIO_OUTPUT_FLAG_NONE)
1440{
Steve Block3856b092011-10-20 11:56:00 +01001441 ALOGV("AudioOutput(%d)", sessionId);
Eric Laurent43562692015-07-15 16:49:07 -07001442 if (attr != NULL) {
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001443 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1444 if (mAttributes != NULL) {
1445 memcpy(mAttributes, attr, sizeof(audio_attributes_t));
1446 mStreamType = audio_attributes_to_stream_type(attr);
1447 }
1448 } else {
1449 mAttributes = NULL;
Eric Laurent43562692015-07-15 16:49:07 -07001450 }
1451
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001452 setMinBufferCount();
1453}
1454
1455MediaPlayerService::AudioOutput::~AudioOutput()
1456{
1457 close();
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001458 free(mAttributes);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001459 delete mCallbackData;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001460}
1461
Andy Hungd1c74342015-07-07 16:54:23 -07001462//static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001463void MediaPlayerService::AudioOutput::setMinBufferCount()
1464{
1465 char value[PROPERTY_VALUE_MAX];
1466 if (property_get("ro.kernel.qemu", value, 0)) {
1467 mIsOnEmulator = true;
1468 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1469 }
1470}
1471
Andy Hungd1c74342015-07-07 16:54:23 -07001472// static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001473bool MediaPlayerService::AudioOutput::isOnEmulator()
1474{
Andy Hungd1c74342015-07-07 16:54:23 -07001475 setMinBufferCount(); // benign race wrt other threads
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001476 return mIsOnEmulator;
1477}
1478
Andy Hungd1c74342015-07-07 16:54:23 -07001479// static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001480int MediaPlayerService::AudioOutput::getMinBufferCount()
1481{
Andy Hungd1c74342015-07-07 16:54:23 -07001482 setMinBufferCount(); // benign race wrt other threads
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001483 return mMinBufferCount;
1484}
1485
1486ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1487{
Andy Hungd1c74342015-07-07 16:54:23 -07001488 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001489 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001490 return mTrack->frameCount() * mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001491}
1492
1493ssize_t MediaPlayerService::AudioOutput::frameCount() const
1494{
Andy Hungd1c74342015-07-07 16:54:23 -07001495 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001496 if (mTrack == 0) return NO_INIT;
1497 return mTrack->frameCount();
1498}
1499
1500ssize_t MediaPlayerService::AudioOutput::channelCount() const
1501{
Andy Hungd1c74342015-07-07 16:54:23 -07001502 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001503 if (mTrack == 0) return NO_INIT;
1504 return mTrack->channelCount();
1505}
1506
1507ssize_t MediaPlayerService::AudioOutput::frameSize() const
1508{
Andy Hungd1c74342015-07-07 16:54:23 -07001509 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001510 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001511 return mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001512}
1513
1514uint32_t MediaPlayerService::AudioOutput::latency () const
1515{
Andy Hungd1c74342015-07-07 16:54:23 -07001516 Mutex::Autolock lock(mLock);
Eric Laurentdb354e52012-03-05 17:27:11 -08001517 if (mTrack == 0) return 0;
1518 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001519}
1520
1521float MediaPlayerService::AudioOutput::msecsPerFrame() const
1522{
Andy Hungd1c74342015-07-07 16:54:23 -07001523 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001524 return mMsecsPerFrame;
1525}
1526
Marco Nelissen4110c102012-03-29 09:31:28 -07001527status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001528{
Andy Hungd1c74342015-07-07 16:54:23 -07001529 Mutex::Autolock lock(mLock);
Eric Laurent342e9cf2010-01-19 17:37:09 -08001530 if (mTrack == 0) return NO_INIT;
1531 return mTrack->getPosition(position);
1532}
1533
Lajos Molnar06ad1522014-08-28 07:27:44 -07001534status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1535{
Andy Hungd1c74342015-07-07 16:54:23 -07001536 Mutex::Autolock lock(mLock);
Lajos Molnar06ad1522014-08-28 07:27:44 -07001537 if (mTrack == 0) return NO_INIT;
1538 return mTrack->getTimestamp(ts);
1539}
1540
Wei Jiac4ac8172015-10-21 10:35:48 -07001541// TODO: Remove unnecessary calls to getPlayedOutDurationUs()
1542// as it acquires locks and may query the audio driver.
1543//
1544// Some calls could conceivably retrieve extrapolated data instead of
1545// accessing getTimestamp() or getPosition() every time a data buffer with
1546// a media time is received.
1547//
1548// Calculate duration of played samples if played at normal rate (i.e., 1.0).
1549int64_t MediaPlayerService::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const
1550{
1551 Mutex::Autolock lock(mLock);
1552 if (mTrack == 0 || mSampleRateHz == 0) {
Wei Jia213f4902015-10-22 08:55:25 -07001553 return 0;
Wei Jiac4ac8172015-10-21 10:35:48 -07001554 }
1555
1556 uint32_t numFramesPlayed;
1557 int64_t numFramesPlayedAt;
1558 AudioTimestamp ts;
1559 static const int64_t kStaleTimestamp100ms = 100000;
1560
1561 status_t res = mTrack->getTimestamp(ts);
1562 if (res == OK) { // case 1: mixing audio tracks and offloaded tracks.
1563 numFramesPlayed = ts.mPosition;
1564 numFramesPlayedAt = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000;
1565 const int64_t timestampAge = nowUs - numFramesPlayedAt;
1566 if (timestampAge > kStaleTimestamp100ms) {
1567 // This is an audio FIXME.
1568 // getTimestamp returns a timestamp which may come from audio mixing threads.
1569 // After pausing, the MixerThread may go idle, thus the mTime estimate may
1570 // become stale. Assuming that the MixerThread runs 20ms, with FastMixer at 5ms,
1571 // the max latency should be about 25ms with an average around 12ms (to be verified).
1572 // For safety we use 100ms.
1573 ALOGV("getTimestamp: returned stale timestamp nowUs(%lld) numFramesPlayedAt(%lld)",
1574 (long long)nowUs, (long long)numFramesPlayedAt);
1575 numFramesPlayedAt = nowUs - kStaleTimestamp100ms;
1576 }
1577 //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAt);
1578 } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track
1579 numFramesPlayed = 0;
1580 numFramesPlayedAt = nowUs;
1581 //ALOGD("getTimestamp: WOULD_BLOCK %d %lld",
1582 // numFramesPlayed, (long long)numFramesPlayedAt);
1583 } else { // case 3: transitory at new track or audio fast tracks.
1584 res = mTrack->getPosition(&numFramesPlayed);
1585 CHECK_EQ(res, (status_t)OK);
1586 numFramesPlayedAt = nowUs;
1587 numFramesPlayedAt += 1000LL * mTrack->latency() / 2; /* XXX */
1588 //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAt);
1589 }
1590
1591 // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test
1592 // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours.
1593 int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz)
1594 + nowUs - numFramesPlayedAt;
1595 if (durationUs < 0) {
1596 // Occurs when numFramesPlayed position is very small and the following:
1597 // (1) In case 1, the time nowUs is computed before getTimestamp() is called and
1598 // numFramesPlayedAt is greater than nowUs by time more than numFramesPlayed.
1599 // (2) In case 3, using getPosition and adding mAudioSink->latency() to
1600 // numFramesPlayedAt, by a time amount greater than numFramesPlayed.
1601 //
1602 // Both of these are transitory conditions.
1603 ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs);
1604 durationUs = 0;
1605 }
1606 ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)",
1607 (long long)durationUs, (long long)nowUs, numFramesPlayed, (long long)numFramesPlayedAt);
1608 return durationUs;
1609}
1610
Marco Nelissen4110c102012-03-29 09:31:28 -07001611status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1612{
Andy Hungd1c74342015-07-07 16:54:23 -07001613 Mutex::Autolock lock(mLock);
Marco Nelissen4110c102012-03-29 09:31:28 -07001614 if (mTrack == 0) return NO_INIT;
Andy Hung2f6e73d2016-04-08 12:12:58 -07001615 ExtendedTimestamp ets;
1616 status_t status = mTrack->getTimestamp(&ets);
1617 if (status == OK || status == WOULD_BLOCK) {
1618 *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT];
1619 }
1620 return status;
Marco Nelissen4110c102012-03-29 09:31:28 -07001621}
1622
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001623status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
1624{
Andy Hungd1c74342015-07-07 16:54:23 -07001625 Mutex::Autolock lock(mLock);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001626 if (mTrack == 0) return NO_INIT;
1627 return mTrack->setParameters(keyValuePairs);
1628}
1629
1630String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
1631{
Andy Hungd1c74342015-07-07 16:54:23 -07001632 Mutex::Autolock lock(mLock);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001633 if (mTrack == 0) return String8::empty();
1634 return mTrack->getParameters(keys);
1635}
1636
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001637void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
Andy Hungd1c74342015-07-07 16:54:23 -07001638 Mutex::Autolock lock(mLock);
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001639 if (attributes == NULL) {
1640 free(mAttributes);
1641 mAttributes = NULL;
1642 } else {
1643 if (mAttributes == NULL) {
1644 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1645 }
1646 memcpy(mAttributes, attributes, sizeof(audio_attributes_t));
Eric Laurent43562692015-07-15 16:49:07 -07001647 mStreamType = audio_attributes_to_stream_type(attributes);
1648 }
1649}
1650
1651void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType)
1652{
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001653 Mutex::Autolock lock(mLock);
Eric Laurent43562692015-07-15 16:49:07 -07001654 // do not allow direct stream type modification if attributes have been set
1655 if (mAttributes == NULL) {
1656 mStreamType = streamType;
1657 }
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001658}
1659
Andy Hungd1c74342015-07-07 16:54:23 -07001660void MediaPlayerService::AudioOutput::deleteRecycledTrack_l()
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001661{
Andy Hungd1c74342015-07-07 16:54:23 -07001662 ALOGV("deleteRecycledTrack_l");
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001663 if (mRecycledTrack != 0) {
1664
1665 if (mCallbackData != NULL) {
1666 mCallbackData->setOutput(NULL);
1667 mCallbackData->endTrackSwitch();
1668 }
1669
1670 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
Andy Hunge13f8a62016-03-30 14:20:42 -07001671 int32_t msec = 0;
1672 if (!mRecycledTrack->stopped()) { // check if active
1673 (void)mRecycledTrack->pendingDuration(&msec);
1674 }
1675 mRecycledTrack->stop(); // ensure full data drain
1676 ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec);
1677 if (msec > 0) {
1678 static const int32_t WAIT_LIMIT_MS = 3000;
1679 if (msec > WAIT_LIMIT_MS) {
1680 msec = WAIT_LIMIT_MS;
1681 }
1682 usleep(msec * 1000LL);
1683 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001684 }
1685 // An offloaded track isn't flushed because the STREAM_END is reported
1686 // slightly prematurely to allow time for the gapless track switch
1687 // but this means that if we decide not to recycle the track there
1688 // could be a small amount of residual data still playing. We leave
1689 // AudioFlinger to drain the track.
1690
1691 mRecycledTrack.clear();
Andy Hungd1c74342015-07-07 16:54:23 -07001692 close_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001693 delete mCallbackData;
1694 mCallbackData = NULL;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001695 }
1696}
1697
Andy Hungd1c74342015-07-07 16:54:23 -07001698void MediaPlayerService::AudioOutput::close_l()
1699{
1700 mTrack.clear();
1701}
1702
Andreas Huber20111aa2009-07-14 16:56:47 -07001703status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001704 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1705 audio_format_t format, int bufferCount,
Eric Laurent1948eb32012-04-13 16:50:19 -07001706 AudioCallback cb, void *cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00001707 audio_output_flags_t flags,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001708 const audio_offload_info_t *offloadInfo,
Andy Hung179652e2015-05-31 22:49:46 -07001709 bool doNotReconnect,
1710 uint32_t suggestedFrameCount)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001711{
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001712 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1713 format, bufferCount, mSessionId, flags);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001714
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001715 // offloading is only supported in callback mode for now.
1716 // offloadInfo must be present if offload flag is set
1717 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1718 ((cb == NULL) || (offloadInfo == NULL))) {
1719 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001720 }
1721
Andy Hung179652e2015-05-31 22:49:46 -07001722 // compute frame count for the AudioTrack internal buffer
1723 size_t frameCount;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001724 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1725 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1726 } else {
Andy Hung179652e2015-05-31 22:49:46 -07001727 // try to estimate the buffer processing fetch size from AudioFlinger.
1728 // framesPerBuffer is approximate and generally correct, except when it's not :-).
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001729 uint32_t afSampleRate;
1730 size_t afFrameCount;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001731 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1732 return NO_INIT;
1733 }
1734 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1735 return NO_INIT;
1736 }
Andy Hung179652e2015-05-31 22:49:46 -07001737 const size_t framesPerBuffer =
1738 (unsigned long long)sampleRate * afFrameCount / afSampleRate;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001739
Andy Hung179652e2015-05-31 22:49:46 -07001740 if (bufferCount == 0) {
1741 // use suggestedFrameCount
1742 bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer;
1743 }
1744 // Check argument bufferCount against the mininum buffer count
1745 if (bufferCount != 0 && bufferCount < mMinBufferCount) {
1746 ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount);
1747 bufferCount = mMinBufferCount;
1748 }
1749 // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger
1750 // which will be the minimum size permitted.
1751 frameCount = bufferCount * framesPerBuffer;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001752 }
Andreas Huber20111aa2009-07-14 16:56:47 -07001753
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001754 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07001755 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08001756 if (0 == channelMask) {
1757 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1758 return NO_INIT;
1759 }
1760 }
Eric Laurent1948eb32012-04-13 16:50:19 -07001761
Andy Hungd1c74342015-07-07 16:54:23 -07001762 Mutex::Autolock lock(mLock);
Andy Hung179652e2015-05-31 22:49:46 -07001763 mCallback = cb;
1764 mCallbackCookie = cookie;
1765
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001766 // Check whether we can recycle the track
1767 bool reuse = false;
1768 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001769
Glenn Kasten2799d742013-05-30 14:33:29 -07001770 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001771 // check whether we are switching between two offloaded tracks
1772 bothOffloaded = (flags & mRecycledTrack->getFlags()
1773 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07001774
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001775 // check if the existing track can be reused as-is, or if a new track needs to be created.
1776 reuse = true;
1777
Marco Nelissen67295b52012-06-11 14:52:53 -07001778 if ((mCallbackData == NULL && mCallback != NULL) ||
1779 (mCallbackData != NULL && mCallback == NULL)) {
1780 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1781 ALOGV("can't chain callback and write");
1782 reuse = false;
1783 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001784 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1785 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07001786 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001787 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07001788 reuse = false;
1789 } else if (flags != mFlags) {
1790 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1791 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001792 } else if (mRecycledTrack->format() != format) {
1793 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07001794 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001795 } else {
1796 ALOGV("no track available to recycle");
1797 }
1798
1799 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1800
1801 // If we can't recycle and both tracks are offloaded
1802 // we must close the previous output before opening a new one
1803 if (bothOffloaded && !reuse) {
1804 ALOGV("both offloaded and not recycling");
Andy Hungd1c74342015-07-07 16:54:23 -07001805 deleteRecycledTrack_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001806 }
1807
1808 sp<AudioTrack> t;
1809 CallbackData *newcbd = NULL;
1810
1811 // We don't attempt to create a new track if we are recycling an
1812 // offloaded track. But, if we are recycling a non-offloaded or we
1813 // are switching where one is offloaded and one isn't then we create
1814 // the new track in advance so that we can read additional stream info
1815
1816 if (!(reuse && bothOffloaded)) {
1817 ALOGV("creating new AudioTrack");
1818
1819 if (mCallback != NULL) {
1820 newcbd = new CallbackData(this);
1821 t = new AudioTrack(
1822 mStreamType,
1823 sampleRate,
1824 format,
1825 channelMask,
1826 frameCount,
1827 flags,
1828 CallbackWrapper,
1829 newcbd,
1830 0, // notification frames
1831 mSessionId,
1832 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001833 offloadInfo,
Marco Nelissend457c972014-02-11 08:47:07 -08001834 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001835 mPid,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001836 mAttributes,
1837 doNotReconnect);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001838 } else {
Andy Hungff874dc2016-04-11 16:49:09 -07001839 // TODO: Due to buffer memory concerns, we use a max target playback speed
1840 // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed),
1841 // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed.
1842 const float targetSpeed =
1843 std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed);
1844 ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed,
1845 "track target speed:%f clamped from playback speed:%f",
1846 targetSpeed, mPlaybackRate.mSpeed);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001847 t = new AudioTrack(
1848 mStreamType,
1849 sampleRate,
1850 format,
1851 channelMask,
1852 frameCount,
1853 flags,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08001854 NULL, // callback
1855 NULL, // user data
1856 0, // notification frames
1857 mSessionId,
1858 AudioTrack::TRANSFER_DEFAULT,
1859 NULL, // offload info
Marco Nelissend457c972014-02-11 08:47:07 -08001860 mUid,
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001861 mPid,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07001862 mAttributes,
Andy Hungff874dc2016-04-11 16:49:09 -07001863 doNotReconnect,
1864 targetSpeed);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001865 }
1866
1867 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1868 ALOGE("Unable to create audio track");
1869 delete newcbd;
Glenn Kasten3e98ecd2015-05-18 13:13:24 -07001870 // t goes out of scope, so reference count drops to zero
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001871 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001872 } else {
1873 // successful AudioTrack initialization implies a legacy stream type was generated
1874 // from the audio attributes
1875 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001876 }
1877 }
1878
1879 if (reuse) {
1880 CHECK(mRecycledTrack != NULL);
1881
1882 if (!bothOffloaded) {
1883 if (mRecycledTrack->frameCount() != t->frameCount()) {
Andy Hung833b4752016-04-04 17:15:48 -07001884 ALOGV("framecount differs: %zu/%zu frames",
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001885 mRecycledTrack->frameCount(), t->frameCount());
1886 reuse = false;
1887 }
1888 }
1889
Marco Nelissen67295b52012-06-11 14:52:53 -07001890 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001891 ALOGV("chaining to next output and recycling track");
Andy Hungd1c74342015-07-07 16:54:23 -07001892 close_l();
Marco Nelissen67295b52012-06-11 14:52:53 -07001893 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07001894 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07001895 if (mCallbackData != NULL) {
1896 mCallbackData->setOutput(this);
1897 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001898 delete newcbd;
1899 return OK;
1900 }
Marco Nelissen67295b52012-06-11 14:52:53 -07001901 }
1902
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001903 // we're not going to reuse the track, unblock and flush it
1904 // this was done earlier if both tracks are offloaded
1905 if (!bothOffloaded) {
Andy Hungd1c74342015-07-07 16:54:23 -07001906 deleteRecycledTrack_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001907 }
1908
1909 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1910
Marco Nelissen67295b52012-06-11 14:52:53 -07001911 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01001912 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001913 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001914
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001915 mSampleRateHz = sampleRate;
Preetam Singh Ranawat2e17eef2015-08-12 12:11:46 -07001916 mFlags = flags;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001917 mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
Andy Hungd1c74342015-07-07 16:54:23 -07001918 mFrameSize = t->frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001919 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07001920
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001921 status_t res = NO_ERROR;
Wei Jia0162d002015-06-09 11:59:33 -07001922 // Note some output devices may give us a direct track even though we don't specify it.
1923 // Example: Line application b/17459982.
Preetam Singh Ranawat2e17eef2015-08-12 12:11:46 -07001924 if ((t->getFlags() & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001925 res = t->setPlaybackRate(mPlaybackRate);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001926 if (res == NO_ERROR) {
1927 t->setAuxEffectSendLevel(mSendLevel);
1928 res = t->attachAuxEffect(mAuxEffectId);
1929 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08001930 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001931 ALOGV("open() DONE status %d", res);
1932 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001933}
1934
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001935status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001936{
Steve Block3856b092011-10-20 11:56:00 +01001937 ALOGV("start");
Andy Hungd1c74342015-07-07 16:54:23 -07001938 Mutex::Autolock lock(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001939 if (mCallbackData != NULL) {
1940 mCallbackData->endTrackSwitch();
1941 }
Glenn Kasten2799d742013-05-30 14:33:29 -07001942 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001943 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07001944 mTrack->setAuxEffectSendLevel(mSendLevel);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001945 return mTrack->start();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001946 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01001947 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001948}
1949
Marco Nelissen6b74d672012-02-28 16:07:44 -08001950void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
Andy Hungd1c74342015-07-07 16:54:23 -07001951 Mutex::Autolock lock(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08001952 mNextOutput = nextOutput;
1953}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08001954
Marco Nelissen6b74d672012-02-28 16:07:44 -08001955void MediaPlayerService::AudioOutput::switchToNextOutput() {
1956 ALOGV("switchToNextOutput");
Andy Hungd1c74342015-07-07 16:54:23 -07001957
1958 // Try to acquire the callback lock before moving track (without incurring deadlock).
1959 const unsigned kMaxSwitchTries = 100;
1960 Mutex::Autolock lock(mLock);
1961 for (unsigned tries = 0;;) {
1962 if (mTrack == 0) {
1963 return;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001964 }
Andy Hungd1c74342015-07-07 16:54:23 -07001965 if (mNextOutput != NULL && mNextOutput != this) {
1966 if (mCallbackData != NULL) {
1967 // two alternative approaches
1968#if 1
1969 CallbackData *callbackData = mCallbackData;
1970 mLock.unlock();
1971 // proper acquisition sequence
1972 callbackData->lock();
1973 mLock.lock();
1974 // Caution: it is unlikely that someone deleted our callback or changed our target
1975 if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) {
1976 // fatal if we are starved out.
1977 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
1978 "switchToNextOutput() cannot obtain correct lock sequence");
1979 callbackData->unlock();
1980 continue;
1981 }
1982 callbackData->mSwitching = true; // begin track switch
1983#else
1984 // tryBeginTrackSwitch() returns false if the callback has the lock.
1985 if (!mCallbackData->tryBeginTrackSwitch()) {
1986 // fatal if we are starved out.
1987 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
1988 "switchToNextOutput() cannot obtain callback lock");
1989 mLock.unlock();
1990 usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput
1991 mLock.lock();
1992 continue;
1993 }
1994#endif
1995 }
1996
1997 Mutex::Autolock nextLock(mNextOutput->mLock);
1998
1999 // If the next output track is not NULL, then it has been
2000 // opened already for playback.
2001 // This is possible even without the next player being started,
2002 // for example, the next player could be prepared and seeked.
2003 //
2004 // Presuming it isn't advisable to force the track over.
2005 if (mNextOutput->mTrack == NULL) {
2006 ALOGD("Recycling track for gapless playback");
2007 delete mNextOutput->mCallbackData;
2008 mNextOutput->mCallbackData = mCallbackData;
2009 mNextOutput->mRecycledTrack = mTrack;
2010 mNextOutput->mSampleRateHz = mSampleRateHz;
2011 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Andy Hungd1c74342015-07-07 16:54:23 -07002012 mNextOutput->mFlags = mFlags;
2013 mNextOutput->mFrameSize = mFrameSize;
2014 close_l();
2015 mCallbackData = NULL; // destruction handled by mNextOutput
2016 } else {
2017 ALOGW("Ignoring gapless playback because next player has already started");
2018 // remove track in case resource needed for future players.
2019 if (mCallbackData != NULL) {
2020 mCallbackData->endTrackSwitch(); // release lock for callbacks before close.
2021 }
2022 close_l();
2023 }
2024 }
2025 break;
Marco Nelissen6b74d672012-02-28 16:07:44 -08002026 }
2027}
2028
Wei Jia7d3f4df2015-03-03 15:28:00 -08002029ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002030{
Andy Hungd1c74342015-07-07 16:54:23 -07002031 Mutex::Autolock lock(mLock);
Glenn Kastenadad3d72014-02-21 14:51:43 -08002032 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07002033
Steve Block3856b092011-10-20 11:56:00 +01002034 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07002035 if (mTrack != 0) {
Andy Hung2f6e73d2016-04-08 12:12:58 -07002036 return mTrack->write(buffer, size, blocking);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07002037 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002038 return NO_INIT;
2039}
2040
2041void MediaPlayerService::AudioOutput::stop()
2042{
Steve Block3856b092011-10-20 11:56:00 +01002043 ALOGV("stop");
Andy Hungd1c74342015-07-07 16:54:23 -07002044 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07002045 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002046}
2047
2048void MediaPlayerService::AudioOutput::flush()
2049{
Steve Block3856b092011-10-20 11:56:00 +01002050 ALOGV("flush");
Andy Hungd1c74342015-07-07 16:54:23 -07002051 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07002052 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002053}
2054
2055void MediaPlayerService::AudioOutput::pause()
2056{
Steve Block3856b092011-10-20 11:56:00 +01002057 ALOGV("pause");
Andy Hungd1c74342015-07-07 16:54:23 -07002058 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07002059 if (mTrack != 0) mTrack->pause();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002060}
2061
2062void MediaPlayerService::AudioOutput::close()
2063{
Steve Block3856b092011-10-20 11:56:00 +01002064 ALOGV("close");
Erik Wolsheimer7845a1f2015-10-30 12:07:52 -07002065 sp<AudioTrack> track;
2066 {
2067 Mutex::Autolock lock(mLock);
2068 track = mTrack;
2069 close_l(); // clears mTrack
2070 }
2071 // destruction of the track occurs outside of mutex.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002072}
2073
2074void MediaPlayerService::AudioOutput::setVolume(float left, float right)
2075{
Steve Block3856b092011-10-20 11:56:00 +01002076 ALOGV("setVolume(%f, %f)", left, right);
Andy Hungd1c74342015-07-07 16:54:23 -07002077 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002078 mLeftVolume = left;
2079 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07002080 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002081 mTrack->setVolume(left, right);
2082 }
2083}
2084
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002085status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002086{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002087 ALOGV("setPlaybackRate(%f %f %d %d)",
2088 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Andy Hungd1c74342015-07-07 16:54:23 -07002089 Mutex::Autolock lock(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002090 if (mTrack == 0) {
2091 // remember rate so that we can set it when the track is opened
2092 mPlaybackRate = rate;
2093 return OK;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002094 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002095 status_t res = mTrack->setPlaybackRate(rate);
2096 if (res != NO_ERROR) {
2097 return res;
2098 }
2099 // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
2100 CHECK_GT(rate.mSpeed, 0.f);
2101 mPlaybackRate = rate;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002102 if (mSampleRateHz != 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002103 mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002104 }
2105 return res;
2106}
2107
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002108status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
2109{
2110 ALOGV("setPlaybackRate");
Andy Hungd1c74342015-07-07 16:54:23 -07002111 Mutex::Autolock lock(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002112 if (mTrack == 0) {
2113 return NO_INIT;
2114 }
2115 *rate = mTrack->getPlaybackRate();
2116 return NO_ERROR;
2117}
2118
Eric Laurent2beeb502010-07-16 07:43:46 -07002119status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
2120{
Steve Block3856b092011-10-20 11:56:00 +01002121 ALOGV("setAuxEffectSendLevel(%f)", level);
Andy Hungd1c74342015-07-07 16:54:23 -07002122 Mutex::Autolock lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -07002123 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07002124 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07002125 return mTrack->setAuxEffectSendLevel(level);
2126 }
2127 return NO_ERROR;
2128}
2129
2130status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
2131{
Steve Block3856b092011-10-20 11:56:00 +01002132 ALOGV("attachAuxEffect(%d)", effectId);
Andy Hungd1c74342015-07-07 16:54:23 -07002133 Mutex::Autolock lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -07002134 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07002135 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07002136 return mTrack->attachAuxEffect(effectId);
2137 }
2138 return NO_ERROR;
2139}
2140
Andreas Huber20111aa2009-07-14 16:56:47 -07002141// static
2142void MediaPlayerService::AudioOutput::CallbackWrapper(
Glenn Kastend217a8c2011-06-01 15:20:35 -07002143 int event, void *cookie, void *info) {
Steve Block3856b092011-10-20 11:56:00 +01002144 //ALOGV("callbackwrapper");
Marco Nelissen6b74d672012-02-28 16:07:44 -08002145 CallbackData *data = (CallbackData*)cookie;
Andy Hungd1c74342015-07-07 16:54:23 -07002146 // lock to ensure we aren't caught in the middle of a track switch.
Marco Nelissen6b74d672012-02-28 16:07:44 -08002147 data->lock();
2148 AudioOutput *me = data->getOutput();
Andreas Huber20111aa2009-07-14 16:56:47 -07002149 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
Marco Nelissen6b74d672012-02-28 16:07:44 -08002150 if (me == NULL) {
2151 // no output set, likely because the track was scheduled to be reused
2152 // by another player, but the format turned out to be incompatible.
2153 data->unlock();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002154 if (buffer != NULL) {
2155 buffer->size = 0;
2156 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08002157 return;
2158 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002159
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002160 switch(event) {
2161 case AudioTrack::EVENT_MORE_DATA: {
2162 size_t actualSize = (*me->mCallback)(
2163 me, buffer->raw, buffer->size, me->mCallbackCookie,
2164 CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002165
Andy Hung719b46b2015-05-31 22:18:25 -07002166 // Log when no data is returned from the callback.
2167 // (1) We may have no data (especially with network streaming sources).
2168 // (2) We may have reached the EOS and the audio track is not stopped yet.
2169 // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
2170 // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill).
2171 //
2172 // This is a benign busy-wait, with the next data request generated 10 ms or more later;
2173 // nevertheless for power reasons, we don't want to see too many of these.
Andreas Huber2e8ffaf2010-02-18 16:45:13 -08002174
Andy Hung719b46b2015-05-31 22:18:25 -07002175 ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned");
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002176
2177 buffer->size = actualSize;
2178 } break;
2179
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002180 case AudioTrack::EVENT_STREAM_END:
Andy Hung719b46b2015-05-31 22:18:25 -07002181 // currently only occurs for offloaded callbacks
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002182 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
2183 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
2184 me->mCallbackCookie, CB_EVENT_STREAM_END);
2185 break;
2186
2187 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
2188 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
2189 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
2190 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
2191 break;
2192
Glenn Kasten421743b2015-06-01 08:18:08 -07002193 case AudioTrack::EVENT_UNDERRUN:
Andy Hung719b46b2015-05-31 22:18:25 -07002194 // This occurs when there is no data available, typically
Glenn Kasten421743b2015-06-01 08:18:08 -07002195 // when there is a failure to supply data to the AudioTrack. It can also
2196 // occur in non-offloaded mode when the audio device comes out of standby.
2197 //
Andy Hung719b46b2015-05-31 22:18:25 -07002198 // If an AudioTrack underruns it outputs silence. Since this happens suddenly
2199 // it may sound like an audible pop or glitch.
2200 //
2201 // The underrun event is sent once per track underrun; the condition is reset
2202 // when more data is sent to the AudioTrack.
Eric Laurente93cc032016-05-05 10:15:10 -07002203 ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)");
Glenn Kasten421743b2015-06-01 08:18:08 -07002204 break;
2205
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002206 default:
2207 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
Andreas Huber51c1e0e2011-04-04 11:43:40 -07002208 }
2209
Marco Nelissen6b74d672012-02-28 16:07:44 -08002210 data->unlock();
Andreas Huber20111aa2009-07-14 16:56:47 -07002211}
2212
Glenn Kastend848eb42016-03-08 13:42:11 -08002213audio_session_t MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002214{
Andy Hungd1c74342015-07-07 16:54:23 -07002215 Mutex::Autolock lock(mLock);
Eric Laurent8c563ed2010-10-07 18:23:03 -07002216 return mSessionId;
2217}
2218
Eric Laurent6f59db12013-07-26 17:16:50 -07002219uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
2220{
Andy Hungd1c74342015-07-07 16:54:23 -07002221 Mutex::Autolock lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -07002222 if (mTrack == 0) return 0;
2223 return mTrack->getSampleRate();
2224}
2225
Andy Hungf2c87b32016-04-07 19:49:29 -07002226int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const
2227{
2228 Mutex::Autolock lock(mLock);
2229 if (mTrack == 0) {
2230 return 0;
2231 }
2232 int64_t duration;
2233 if (mTrack->getBufferDurationInUs(&duration) != OK) {
2234 return 0;
2235 }
2236 return duration;
2237}
2238
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002239////////////////////////////////////////////////////////////////////////////////
2240
2241struct CallbackThread : public Thread {
2242 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2243 MediaPlayerBase::AudioSink::AudioCallback cb,
2244 void *cookie);
2245
2246protected:
2247 virtual ~CallbackThread();
2248
2249 virtual bool threadLoop();
2250
2251private:
2252 wp<MediaPlayerBase::AudioSink> mSink;
2253 MediaPlayerBase::AudioSink::AudioCallback mCallback;
2254 void *mCookie;
2255 void *mBuffer;
2256 size_t mBufferSize;
2257
2258 CallbackThread(const CallbackThread &);
2259 CallbackThread &operator=(const CallbackThread &);
2260};
2261
2262CallbackThread::CallbackThread(
2263 const wp<MediaPlayerBase::AudioSink> &sink,
2264 MediaPlayerBase::AudioSink::AudioCallback cb,
2265 void *cookie)
2266 : mSink(sink),
2267 mCallback(cb),
2268 mCookie(cookie),
2269 mBuffer(NULL),
2270 mBufferSize(0) {
2271}
2272
2273CallbackThread::~CallbackThread() {
2274 if (mBuffer) {
2275 free(mBuffer);
2276 mBuffer = NULL;
2277 }
2278}
2279
2280bool CallbackThread::threadLoop() {
2281 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2282 if (sink == NULL) {
2283 return false;
2284 }
2285
2286 if (mBuffer == NULL) {
2287 mBufferSize = sink->bufferSize();
2288 mBuffer = malloc(mBufferSize);
2289 }
2290
2291 size_t actualSize =
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002292 (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2293 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002294
2295 if (actualSize > 0) {
2296 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07002297 // Could return false on sink->write() error or short count.
2298 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002299 }
2300
2301 return true;
2302}
2303
2304////////////////////////////////////////////////////////////////////////////////
2305
Gloria Wang7cf180c2011-02-19 18:37:57 -08002306void MediaPlayerService::addBatteryData(uint32_t params)
2307{
2308 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002309
2310 int32_t time = systemTime() / 1000000L;
2311
2312 // change audio output devices. This notification comes from AudioFlinger
2313 if ((params & kBatteryDataSpeakerOn)
2314 || (params & kBatteryDataOtherAudioDeviceOn)) {
2315
2316 int deviceOn[NUM_AUDIO_DEVICES];
2317 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2318 deviceOn[i] = 0;
2319 }
2320
2321 if ((params & kBatteryDataSpeakerOn)
2322 && (params & kBatteryDataOtherAudioDeviceOn)) {
2323 deviceOn[SPEAKER_AND_OTHER] = 1;
2324 } else if (params & kBatteryDataSpeakerOn) {
2325 deviceOn[SPEAKER] = 1;
2326 } else {
2327 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2328 }
2329
2330 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2331 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2332
2333 if (mBatteryAudio.refCount > 0) { // if playing audio
2334 if (!deviceOn[i]) {
2335 mBatteryAudio.lastTime[i] += time;
2336 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2337 mBatteryAudio.lastTime[i] = 0;
2338 } else {
2339 mBatteryAudio.lastTime[i] = 0 - time;
2340 }
2341 }
2342
2343 mBatteryAudio.deviceOn[i] = deviceOn[i];
2344 }
2345 }
2346 return;
2347 }
2348
Marco Nelissenb7848f12014-12-04 08:57:56 -08002349 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08002350 if (params & kBatteryDataAudioFlingerStart) {
2351 // record the start time only if currently no other audio
2352 // is being played
2353 if (mBatteryAudio.refCount == 0) {
2354 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2355 if (mBatteryAudio.deviceOn[i]) {
2356 mBatteryAudio.lastTime[i] -= time;
2357 }
2358 }
2359 }
2360
2361 mBatteryAudio.refCount ++;
2362 return;
2363
2364 } else if (params & kBatteryDataAudioFlingerStop) {
2365 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002366 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002367 return;
2368 }
2369
2370 // record the stop time only if currently this is the only
2371 // audio being played
2372 if (mBatteryAudio.refCount == 1) {
2373 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2374 if (mBatteryAudio.deviceOn[i]) {
2375 mBatteryAudio.lastTime[i] += time;
2376 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2377 mBatteryAudio.lastTime[i] = 0;
2378 }
2379 }
2380 }
2381
2382 mBatteryAudio.refCount --;
2383 return;
2384 }
2385
Gloria Wang7cf180c2011-02-19 18:37:57 -08002386 int uid = IPCThreadState::self()->getCallingUid();
2387 if (uid == AID_MEDIA) {
2388 return;
2389 }
2390 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002391
2392 if (index < 0) { // create a new entry for this UID
2393 BatteryUsageInfo info;
2394 info.audioTotalTime = 0;
2395 info.videoTotalTime = 0;
2396 info.audioLastTime = 0;
2397 info.videoLastTime = 0;
2398 info.refCount = 0;
2399
Gloria Wang9ee159b2011-02-24 14:51:45 -08002400 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002401 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002402 return;
2403 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002404 }
2405
2406 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2407
2408 if (params & kBatteryDataCodecStarted) {
2409 if (params & kBatteryDataTrackAudio) {
2410 info.audioLastTime -= time;
2411 info.refCount ++;
2412 }
2413 if (params & kBatteryDataTrackVideo) {
2414 info.videoLastTime -= time;
2415 info.refCount ++;
2416 }
2417 } else {
2418 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002419 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002420 return;
2421 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00002422 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08002423 mBatteryData.removeItem(uid);
2424 return;
2425 }
2426
2427 if (params & kBatteryDataTrackAudio) {
2428 info.audioLastTime += time;
2429 info.refCount --;
2430 }
2431 if (params & kBatteryDataTrackVideo) {
2432 info.videoLastTime += time;
2433 info.refCount --;
2434 }
2435
2436 // no stream is being played by this UID
2437 if (info.refCount == 0) {
2438 info.audioTotalTime += info.audioLastTime;
2439 info.audioLastTime = 0;
2440 info.videoTotalTime += info.videoLastTime;
2441 info.videoLastTime = 0;
2442 }
2443 }
2444}
2445
2446status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2447 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002448
2449 // audio output devices usage
2450 int32_t time = systemTime() / 1000000L; //in ms
2451 int32_t totalTime;
2452
2453 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2454 totalTime = mBatteryAudio.totalTime[i];
2455
2456 if (mBatteryAudio.deviceOn[i]
2457 && (mBatteryAudio.lastTime[i] != 0)) {
2458 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
2459 totalTime += tmpTime;
2460 }
2461
2462 reply->writeInt32(totalTime);
2463 // reset the total time
2464 mBatteryAudio.totalTime[i] = 0;
2465 }
2466
2467 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08002468 BatteryUsageInfo info;
2469 int size = mBatteryData.size();
2470
2471 reply->writeInt32(size);
2472 int i = 0;
2473
2474 while (i < size) {
2475 info = mBatteryData.valueAt(i);
2476
2477 reply->writeInt32(mBatteryData.keyAt(i)); //UID
2478 reply->writeInt32(info.audioTotalTime);
2479 reply->writeInt32(info.videoTotalTime);
2480
2481 info.audioTotalTime = 0;
2482 info.videoTotalTime = 0;
2483
2484 // remove the UID entry where no stream is being played
2485 if (info.refCount <= 0) {
2486 mBatteryData.removeItemsAt(i);
2487 size --;
2488 i --;
2489 }
2490 i++;
2491 }
2492 return NO_ERROR;
2493}
nikoa64c8c72009-07-20 15:07:26 -07002494} // namespace android