blob: ee37b718dad7089c48b5fa7069ab848709d7bb44 [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
Andy Hung959b5b82021-09-24 10:46:20 -070024#include <chrono>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <sys/stat.h>
Gloria Wang7cf180c2011-02-19 18:37:57 -080027#include <sys/time.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080028#include <dirent.h>
29#include <unistd.h>
30
31#include <string.h>
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070032
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080033#include <cutils/atomic.h>
Nicolas Catania14d27472009-07-13 14:37:49 -070034#include <cutils/properties.h> // for property_get
Mathias Agopian6f74b0c2009-06-03 17:32:49 -070035
36#include <utils/misc.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080037
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -070038#include <android/hardware/media/omx/1.0/IOmx.h>
Marco Nelissena0c98412019-03-29 12:10:19 -070039#include <android/hardware/media/c2/1.0/IComponentStore.h>
Mathias Agopian75624082009-05-19 19:08:10 -070040#include <binder/IPCThreadState.h>
41#include <binder/IServiceManager.h>
42#include <binder/MemoryHeapBase.h>
43#include <binder/MemoryBase.h>
Mathias Agopianb1e7cd12013-02-14 17:11:27 -080044#include <gui/Surface.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070045#include <utils/Errors.h> // for status_t
46#include <utils/String8.h>
Marco Nelissen10dbb8e2009-09-20 10:42:13 -070047#include <utils/SystemClock.h>
Lajos Molnar06ad1522014-08-28 07:27:44 -070048#include <utils/Timers.h>
Nicolas Catania1d187f12009-05-12 23:25:55 -070049#include <utils/Vector.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080050
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -070051#include <codec2/hidl/client.h>
Marco Nelissen42057ce2019-09-23 12:15:57 -070052#include <datasource/HTTPBase.h>
Philip P. Moltmannbda45752020-07-17 16:41:18 -070053#include <media/AidlConversion.h>
Andreas Huber1b86fe02014-01-29 11:13:26 -080054#include <media/IMediaHTTPService.h>
Jeff Brown2013a542012-09-04 21:38:42 -070055#include <media/IRemoteDisplay.h>
56#include <media/IRemoteDisplayClient.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080057#include <media/MediaPlayerInterface.h>
58#include <media/mediarecorder.h>
59#include <media/MediaMetadataRetrieverInterface.h>
nikoa64c8c72009-07-20 15:07:26 -070060#include <media/Metadata.h>
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080061#include <media/AudioTrack.h>
Dongwon Kangd91dc5a2017-10-10 00:07:09 -070062#include <media/stagefright/InterfaceUtils.h>
Wonsik Kim6f675772019-08-20 17:06:53 -070063#include <media/stagefright/MediaCodecConstants.h>
Lajos Molnar1381d4b2014-08-07 15:18:35 -070064#include <media/stagefright/MediaCodecList.h>
Eric Laurent9cb839a2011-09-27 09:48:56 -070065#include <media/stagefright/MediaErrors.h>
Marco Nelissen83b0fd92015-09-16 13:48:07 -070066#include <media/stagefright/Utils.h>
Marco Nelissen42057ce2019-09-23 12:15:57 -070067#include <media/stagefright/FoundationUtils.h>
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +010068#include <media/stagefright/foundation/ADebug.h>
Marco Nelissenf09611f2015-02-13 14:12:42 -080069#include <media/stagefright/foundation/ALooperRoster.h>
Chong Zhang181fd9b2017-02-16 15:53:03 -080070#include <media/stagefright/SurfaceUtils.h>
Ruben Brunk99e69712015-05-26 17:25:07 -070071#include <mediautils/BatteryNotifier.h>
Andy Hunge9eb03e2020-04-04 14:08:23 -070072#include <mediautils/MemoryLeakTrackUtil.h>
Andy Hung53541292016-04-13 16:48:51 -070073#include <memunreachable/memunreachable.h>
Dima Zavin64760242011-05-11 14:15:23 -070074#include <system/audio.h>
Dima Zavinfce7a472011-04-19 22:30:36 -070075
Gloria Wang7cf180c2011-02-19 18:37:57 -080076#include <private/android_filesystem_config.h>
77
James Dong559bf282012-03-28 10:29:14 -070078#include "ActivityManager.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080079#include "MediaRecorderClient.h"
80#include "MediaPlayerService.h"
81#include "MetadataRetrieverClient.h"
John Grossman44a7e422012-06-21 17:29:24 -070082#include "MediaPlayerFactory.h"
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -080083
Nicolas Catania14d27472009-07-13 14:37:49 -070084#include "TestPlayerStub.h"
Ray Essickafca1562022-01-25 09:51:42 -080085#include <nuplayer/NuPlayerDriver.h>
Andreas Huber20111aa2009-07-14 16:56:47 -070086
Vladimir Olteand0aa6472019-01-09 01:13:10 +020087#include "HDCP.h"
88#include "RemoteDisplay.h"
Andreas Hubered3e3e02012-03-26 11:13:27 -070089
Wei Jia502c2f42017-07-13 17:47:56 -070090static const int kDumpLockRetries = 50;
91static const int kDumpLockSleepUs = 20000;
92
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070093namespace {
nikoa64c8c72009-07-20 15:07:26 -070094using android::media::Metadata;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -070095using android::status_t;
96using android::OK;
97using android::BAD_VALUE;
98using android::NOT_ENOUGH_DATA;
99using android::Parcel;
Ivan Lozano8cf3a072017-08-09 09:01:33 -0700100using android::media::VolumeShaper;
Svet Ganov33761132021-05-13 22:51:08 +0000101using android::content::AttributionSourceState;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700102
103// Max number of entries in the filter.
104const int kMaxFilterSize = 64; // I pulled that out of thin air.
105
Andy Hungff874dc2016-04-11 16:49:09 -0700106const float kMaxRequiredSpeed = 8.0f; // for PCM tracks allow up to 8x speedup.
107
nikoa64c8c72009-07-20 15:07:26 -0700108// FIXME: Move all the metadata related function in the Metadata.cpp
nikod608a812009-07-16 16:39:53 -0700109
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700110
111// Unmarshall a filter from a Parcel.
112// Filter format in a parcel:
113//
114// 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
115// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116// | number of entries (n) |
117// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118// | metadata type 1 |
119// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120// | metadata type 2 |
121// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122// ....
123// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124// | metadata type n |
125// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126//
127// @param p Parcel that should start with a filter.
128// @param[out] filter On exit contains the list of metadata type to be
129// filtered.
130// @param[out] status On exit contains the status code to be returned.
131// @return true if the parcel starts with a valid filter.
132bool unmarshallFilter(const Parcel& p,
nikoa64c8c72009-07-20 15:07:26 -0700133 Metadata::Filter *filter,
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700134 status_t *status)
135{
Nicolas Catania48290382009-07-10 13:53:06 -0700136 int32_t val;
137 if (p.readInt32(&val) != OK)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700138 {
Steve Block29357bc2012-01-06 19:20:56 +0000139 ALOGE("Failed to read filter's length");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700140 *status = NOT_ENOUGH_DATA;
141 return false;
142 }
143
Nicolas Catania48290382009-07-10 13:53:06 -0700144 if( val > kMaxFilterSize || val < 0)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700145 {
Steve Block29357bc2012-01-06 19:20:56 +0000146 ALOGE("Invalid filter len %d", val);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700147 *status = BAD_VALUE;
148 return false;
149 }
150
Nicolas Catania48290382009-07-10 13:53:06 -0700151 const size_t num = val;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700152
153 filter->clear();
Nicolas Catania48290382009-07-10 13:53:06 -0700154 filter->setCapacity(num);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700155
nikoa64c8c72009-07-20 15:07:26 -0700156 size_t size = num * sizeof(Metadata::Type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700157
Nicolas Catania48290382009-07-10 13:53:06 -0700158
159 if (p.dataAvail() < size)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700160 {
Andy Hung833b4752016-04-04 17:15:48 -0700161 ALOGE("Filter too short expected %zu but got %zu", size, p.dataAvail());
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700162 *status = NOT_ENOUGH_DATA;
163 return false;
164 }
165
nikoa64c8c72009-07-20 15:07:26 -0700166 const Metadata::Type *data =
167 static_cast<const Metadata::Type*>(p.readInplace(size));
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700168
Nicolas Catania48290382009-07-10 13:53:06 -0700169 if (NULL == data)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700170 {
Steve Block29357bc2012-01-06 19:20:56 +0000171 ALOGE("Filter had no data");
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700172 *status = BAD_VALUE;
173 return false;
174 }
175
176 // TODO: The stl impl of vector would be more efficient here
177 // because it degenerates into a memcpy on pod types. Try to
178 // replace later or use stl::set.
Nicolas Catania48290382009-07-10 13:53:06 -0700179 for (size_t i = 0; i < num; ++i)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700180 {
Nicolas Catania48290382009-07-10 13:53:06 -0700181 filter->add(*data);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700182 ++data;
183 }
184 *status = OK;
185 return true;
186}
187
Nicolas Catania48290382009-07-10 13:53:06 -0700188// @param filter Of metadata type.
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700189// @param val To be searched.
190// @return true if a match was found.
nikoa64c8c72009-07-20 15:07:26 -0700191bool findMetadata(const Metadata::Filter& filter, const int32_t val)
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700192{
193 // Deal with empty and ANY right away
194 if (filter.isEmpty()) return false;
nikoa64c8c72009-07-20 15:07:26 -0700195 if (filter[0] == Metadata::kAny) return true;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700196
Nicolas Catania48290382009-07-10 13:53:06 -0700197 return filter.indexOf(val) >= 0;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -0700198}
199
200} // anonymous namespace
201
202
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700203namespace {
204using android::Parcel;
205using android::String16;
206
207// marshalling tag indicating flattened utf16 tags
208// keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
209const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
210
211// Audio attributes format in a parcel:
212//
213// 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
214// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215// | usage |
216// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217// | content_type |
218// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Hyejin Kim4f418f92014-09-05 15:50:03 +0900219// | source |
220// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700221// | flags |
222// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223// | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
224// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
225// | flattened tags in UTF16 |
226// | ... |
227// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
228//
229// @param p Parcel that contains audio attributes.
230// @param[out] attributes On exit points to an initialized audio_attributes_t structure
231// @param[out] status On exit contains the status code to be returned.
232void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
233{
234 attributes->usage = (audio_usage_t) parcel.readInt32();
235 attributes->content_type = (audio_content_type_t) parcel.readInt32();
Hyejin Kim4f418f92014-09-05 15:50:03 +0900236 attributes->source = (audio_source_t) parcel.readInt32();
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700237 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
238 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
239 if (hasFlattenedTag) {
240 // the tags are UTF16, convert to UTF8
241 String16 tags = parcel.readString16();
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000242 ssize_t realTagSize = utf16_to_utf8_length(tags.c_str(), tags.size());
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700243 if (realTagSize <= 0) {
244 strcpy(attributes->tags, "");
245 } else {
246 // copy the flattened string into the attributes as the destination for the conversion:
247 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
248 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
249 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000250 utf16_to_utf8(tags.c_str(), tagSize, attributes->tags,
Sergio Giro1d3f4272016-06-28 18:24:52 +0100251 sizeof(attributes->tags) / sizeof(attributes->tags[0]));
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700252 }
253 } else {
254 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
255 strcpy(attributes->tags, "");
256 }
257}
258} // anonymous namespace
259
260
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800261namespace android {
262
Marco Nelissenf09611f2015-02-13 14:12:42 -0800263extern ALooperRoster gLooperRoster;
264
265
Dave Burked681bbb2011-08-30 14:39:17 +0100266static bool checkPermission(const char* permissionString) {
Dave Burked681bbb2011-08-30 14:39:17 +0100267 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
268 bool ok = checkCallingPermission(String16(permissionString));
Steve Block29357bc2012-01-06 19:20:56 +0000269 if (!ok) ALOGE("Request requires %s", permissionString);
Dave Burked681bbb2011-08-30 14:39:17 +0100270 return ok;
271}
272
Wonsik Kim6f675772019-08-20 17:06:53 -0700273static void dumpCodecDetails(int fd, const sp<IMediaCodecList> &codecList, bool queryDecoders) {
274 const size_t SIZE = 256;
275 char buffer[SIZE];
276 String8 result;
277
278 const char *codecType = queryDecoders? "Decoder" : "Encoder";
279 snprintf(buffer, SIZE - 1, "\n%s infos by media types:\n"
280 "=============================\n", codecType);
281 result.append(buffer);
282
283 size_t numCodecs = codecList->countCodecs();
284
285 // gather all media types supported by codec class, and link to codecs that support them
286 KeyedVector<AString, Vector<sp<MediaCodecInfo>>> allMediaTypes;
287 for (size_t codec_ix = 0; codec_ix < numCodecs; ++codec_ix) {
288 sp<MediaCodecInfo> info = codecList->getCodecInfo(codec_ix);
289 if (info->isEncoder() == !queryDecoders) {
290 Vector<AString> supportedMediaTypes;
291 info->getSupportedMediaTypes(&supportedMediaTypes);
292 if (!supportedMediaTypes.size()) {
293 snprintf(buffer, SIZE - 1, "warning: %s does not support any media types\n",
294 info->getCodecName());
295 result.append(buffer);
296 } else {
297 for (const AString &mediaType : supportedMediaTypes) {
298 if (allMediaTypes.indexOfKey(mediaType) < 0) {
299 allMediaTypes.add(mediaType, Vector<sp<MediaCodecInfo>>());
300 }
301 allMediaTypes.editValueFor(mediaType).add(info);
302 }
303 }
304 }
305 }
306
307 KeyedVector<AString, bool> visitedCodecs;
308 for (size_t type_ix = 0; type_ix < allMediaTypes.size(); ++type_ix) {
309 const AString &mediaType = allMediaTypes.keyAt(type_ix);
310 snprintf(buffer, SIZE - 1, "\nMedia type '%s':\n", mediaType.c_str());
311 result.append(buffer);
312
313 for (const sp<MediaCodecInfo> &info : allMediaTypes.valueAt(type_ix)) {
314 sp<MediaCodecInfo::Capabilities> caps = info->getCapabilitiesFor(mediaType.c_str());
315 if (caps == NULL) {
316 snprintf(buffer, SIZE - 1, "warning: %s does not have capabilities for type %s\n",
317 info->getCodecName(), mediaType.c_str());
318 result.append(buffer);
319 continue;
320 }
321 snprintf(buffer, SIZE - 1, " %s \"%s\" supports\n",
322 codecType, info->getCodecName());
323 result.append(buffer);
324
325 auto printList = [&](const char *type, const Vector<AString> &values){
326 snprintf(buffer, SIZE - 1, " %s: [", type);
327 result.append(buffer);
328 for (size_t j = 0; j < values.size(); ++j) {
329 snprintf(buffer, SIZE - 1, "\n %s%s", values[j].c_str(),
330 j == values.size() - 1 ? " " : ",");
331 result.append(buffer);
332 }
333 result.append("]\n");
334 };
335
336 if (visitedCodecs.indexOfKey(info->getCodecName()) < 0) {
337 visitedCodecs.add(info->getCodecName(), true);
338 {
339 Vector<AString> aliases;
340 info->getAliases(&aliases);
341 // quote alias
342 for (AString &alias : aliases) {
343 alias.insert("\"", 1, 0);
344 alias.append('"');
345 }
346 printList("aliases", aliases);
347 }
348 {
349 uint32_t attrs = info->getAttributes();
350 Vector<AString> list;
351 list.add(AStringPrintf("encoder: %d",
352 !!(attrs & MediaCodecInfo::kFlagIsEncoder)));
353 list.add(AStringPrintf("vendor: %d",
354 !!(attrs & MediaCodecInfo::kFlagIsVendor)));
355 list.add(AStringPrintf("software-only: %d",
356 !!(attrs & MediaCodecInfo::kFlagIsSoftwareOnly)));
357 list.add(AStringPrintf("hw-accelerated: %d",
358 !!(attrs & MediaCodecInfo::kFlagIsHardwareAccelerated)));
359 printList(AStringPrintf("attributes: %#x", attrs).c_str(), list);
360 }
361
362 snprintf(buffer, SIZE - 1, " owner: \"%s\"\n", info->getOwnerName());
363 result.append(buffer);
364 snprintf(buffer, SIZE - 1, " rank: %u\n", info->getRank());
365 result.append(buffer);
366 } else {
367 result.append(" aliases, attributes, owner, rank: see above\n");
368 }
369
370 {
371 Vector<AString> list;
372 Vector<MediaCodecInfo::ProfileLevel> profileLevels;
373 caps->getSupportedProfileLevels(&profileLevels);
374 for (const MediaCodecInfo::ProfileLevel &pl : profileLevels) {
375 const char *niceProfile =
376 mediaType.equalsIgnoreCase(MIMETYPE_AUDIO_AAC)
377 ? asString_AACObject(pl.mProfile) :
378 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_MPEG2)
379 ? asString_MPEG2Profile(pl.mProfile) :
380 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_H263)
381 ? asString_H263Profile(pl.mProfile) :
382 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_MPEG4)
383 ? asString_MPEG4Profile(pl.mProfile) :
384 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_AVC)
385 ? asString_AVCProfile(pl.mProfile) :
386 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_VP8)
387 ? asString_VP8Profile(pl.mProfile) :
388 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_HEVC)
389 ? asString_HEVCProfile(pl.mProfile) :
390 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_VP9)
391 ? asString_VP9Profile(pl.mProfile) :
392 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_AV1)
ybaia7de57f2023-06-07 14:06:14 +0800393 ? asString_AV1Profile(pl.mProfile) :
394 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_DOLBY_VISION)
395 ? asString_DolbyVisionProfile(pl.mProfile) : "??";
Wonsik Kim6f675772019-08-20 17:06:53 -0700396 const char *niceLevel =
397 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_MPEG2)
398 ? asString_MPEG2Level(pl.mLevel) :
399 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_H263)
400 ? asString_H263Level(pl.mLevel) :
401 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_MPEG4)
402 ? asString_MPEG4Level(pl.mLevel) :
403 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_AVC)
404 ? asString_AVCLevel(pl.mLevel) :
405 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_VP8)
406 ? asString_VP8Level(pl.mLevel) :
407 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_HEVC)
408 ? asString_HEVCTierLevel(pl.mLevel) :
409 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_VP9)
410 ? asString_VP9Level(pl.mLevel) :
411 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_AV1)
ybaia7de57f2023-06-07 14:06:14 +0800412 ? asString_AV1Level(pl.mLevel) :
413 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_DOLBY_VISION)
414 ? asString_DolbyVisionLevel(pl.mLevel) : "??";
Wonsik Kim6f675772019-08-20 17:06:53 -0700415
416 list.add(AStringPrintf("% 5u/% 5u (%s/%s)",
417 pl.mProfile, pl.mLevel, niceProfile, niceLevel));
418 }
419 printList("profile/levels", list);
420 }
421
422 {
423 Vector<AString> list;
424 Vector<uint32_t> colors;
425 caps->getSupportedColorFormats(&colors);
426 for (uint32_t color : colors) {
427 list.add(AStringPrintf("%#x (%s)", color,
428 asString_ColorFormat((int32_t)color)));
429 }
430 printList("colors", list);
431 }
432
Wonsik Kimd579f3e2019-10-07 14:14:09 -0700433 result.append(" details: ");
434 result.append(caps->getDetails()->debugString(6).c_str());
435 result.append("\n");
Wonsik Kim6f675772019-08-20 17:06:53 -0700436 }
437 }
438 result.append("\n");
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000439 ::write(fd, result.c_str(), result.size());
Wonsik Kim6f675772019-08-20 17:06:53 -0700440}
441
442
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800443// 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 -0800444/* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
445/* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
446
447void MediaPlayerService::instantiate() {
448 defaultServiceManager()->addService(
449 String16("media.player"), new MediaPlayerService());
450}
451
452MediaPlayerService::MediaPlayerService()
453{
Steve Block3856b092011-10-20 11:56:00 +0100454 ALOGV("MediaPlayerService created");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800455 mNextConnId = 1;
Gloria Wang9ee159b2011-02-24 14:51:45 -0800456
John Grossman44a7e422012-06-21 17:29:24 -0700457 MediaPlayerFactory::registerBuiltinFactories();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800458}
459
460MediaPlayerService::~MediaPlayerService()
461{
Steve Block3856b092011-10-20 11:56:00 +0100462 ALOGV("MediaPlayerService destroyed");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800463}
464
Svet Ganov33761132021-05-13 22:51:08 +0000465sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(
466 const AttributionSourceState& attributionSource)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800467{
Svet Ganov33761132021-05-13 22:51:08 +0000468 // TODO b/182392769: use attribution source util
469 AttributionSourceState verifiedAttributionSource = attributionSource;
470 verifiedAttributionSource.uid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700471 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
Svet Ganov33761132021-05-13 22:51:08 +0000472 verifiedAttributionSource.pid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700473 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
474 sp<MediaRecorderClient> recorder =
Svet Ganov33761132021-05-13 22:51:08 +0000475 new MediaRecorderClient(this, verifiedAttributionSource);
Gloria Wangdac6a312009-10-29 15:46:37 -0700476 wp<MediaRecorderClient> w = recorder;
477 Mutex::Autolock lock(mLock);
478 mMediaRecorderClients.add(w);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700479 ALOGV("Create new media recorder client from pid %s",
Svet Ganov33761132021-05-13 22:51:08 +0000480 verifiedAttributionSource.toString().c_str());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800481 return recorder;
482}
483
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -0700484void MediaPlayerService::removeMediaRecorderClient(const wp<MediaRecorderClient>& client)
Gloria Wangdac6a312009-10-29 15:46:37 -0700485{
486 Mutex::Autolock lock(mLock);
487 mMediaRecorderClients.remove(client);
Steve Block3856b092011-10-20 11:56:00 +0100488 ALOGV("Delete media recorder client");
Gloria Wangdac6a312009-10-29 15:46:37 -0700489}
490
Glenn Kastenf37971f2012-02-03 11:06:53 -0800491sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800492{
Glenn Kastenf37971f2012-02-03 11:06:53 -0800493 pid_t pid = IPCThreadState::self()->getCallingPid();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800494 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
Steve Block3856b092011-10-20 11:56:00 +0100495 ALOGV("Create new media retriever from pid %d", pid);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800496 return retriever;
497}
498
Glenn Kastenf37971f2012-02-03 11:06:53 -0800499sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
Svet Ganov33761132021-05-13 22:51:08 +0000500 audio_session_t audioSessionId, const AttributionSourceState& attributionSource)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800501{
502 int32_t connId = android_atomic_inc(&mNextConnId);
Svet Ganov33761132021-05-13 22:51:08 +0000503 // TODO b/182392769: use attribution source util
504 AttributionSourceState verifiedAttributionSource = attributionSource;
505 verifiedAttributionSource.pid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700506 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
Svet Ganov33761132021-05-13 22:51:08 +0000507 verifiedAttributionSource.uid = VALUE_OR_FATAL(
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700508 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700509
510 sp<Client> c = new Client(
Svet Ganov33761132021-05-13 22:51:08 +0000511 this, verifiedAttributionSource, connId, client, audioSessionId);
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700512
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700513 ALOGV("Create new client(%d) from %s, ", connId,
Svet Ganov33761132021-05-13 22:51:08 +0000514 verifiedAttributionSource.toString().c_str());
Dave Burked681bbb2011-08-30 14:39:17 +0100515
516 wp<Client> w = c;
517 {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800518 Mutex::Autolock lock(mLock);
519 mClients.add(w);
520 }
Andreas Hubere2b10282010-11-23 11:41:34 -0800521 return c;
522}
523
Lajos Molnar1381d4b2014-08-07 15:18:35 -0700524sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
525 return MediaCodecList::getLocalInstance();
526}
527
Vladimir Olteand0aa6472019-01-09 01:13:10 +0200528sp<IHDCP> MediaPlayerService::makeHDCP(bool createEncryptionModule) {
529 return new HDCP(createEncryptionModule);
530}
Jeff Brownaba33d52012-09-07 17:38:58 -0700531
Vladimir Olteand0aa6472019-01-09 01:13:10 +0200532sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
533 const String16 &opPackageName,
534 const sp<IRemoteDisplayClient>& client, const String8& iface) {
535 if (!checkPermission("android.permission.CONTROL_WIFI_DISPLAY")) {
536 return NULL;
537 }
538
539 return new RemoteDisplay(opPackageName, client, iface.c_str());
Jeff Brown2013a542012-09-04 21:38:42 -0700540}
541
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800542status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
543{
544 const size_t SIZE = 256;
545 char buffer[SIZE];
546 String8 result;
547
548 result.append(" AudioOutput\n");
549 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
550 mStreamType, mLeftVolume, mRightVolume);
551 result.append(buffer);
552 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
Eric Laurentdb354e52012-03-05 17:27:11 -0800553 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800554 result.append(buffer);
Eric Laurent2beeb502010-07-16 07:43:46 -0700555 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
556 mAuxEffectId, mSendLevel);
557 result.append(buffer);
558
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000559 ::write(fd, result.c_str(), result.size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800560 if (mTrack != 0) {
561 mTrack->dump(fd, args);
562 }
563 return NO_ERROR;
564}
565
Lajos Molnar6d339f12015-04-17 16:15:53 -0700566status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800567{
568 const size_t SIZE = 256;
569 char buffer[SIZE];
570 String8 result;
571 result.append(" Client\n");
Svet Ganov33761132021-05-13 22:51:08 +0000572 snprintf(buffer, 255, " AttributionSource(%s), connId(%d), status(%d), looping(%s)\n",
573 mAttributionSource.toString().c_str(), mConnId, mStatus, mLoop?"true": "false");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800574 result.append(buffer);
Wei Jia502c2f42017-07-13 17:47:56 -0700575
576 sp<MediaPlayerBase> p;
577 sp<AudioOutput> audioOutput;
578 bool locked = false;
579 for (int i = 0; i < kDumpLockRetries; ++i) {
580 if (mLock.tryLock() == NO_ERROR) {
581 locked = true;
582 break;
583 }
584 usleep(kDumpLockSleepUs);
Andreas Hubera0b1d4b2011-06-07 15:52:25 -0700585 }
Wei Jia502c2f42017-07-13 17:47:56 -0700586
587 if (locked) {
588 p = mPlayer;
589 audioOutput = mAudioOutput;
590 mLock.unlock();
591 } else {
592 result.append(" lock is taken, no dump from player and audio output\n");
593 }
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000594 write(fd, result.c_str(), result.size());
Wei Jia502c2f42017-07-13 17:47:56 -0700595
596 if (p != NULL) {
597 p->dump(fd, args);
598 }
599 if (audioOutput != 0) {
600 audioOutput->dump(fd, args);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800601 }
602 write(fd, "\n", 1);
603 return NO_ERROR;
604}
605
Marco Nelissenf09611f2015-02-13 14:12:42 -0800606/**
607 * The only arguments this understands right now are -c, -von and -voff,
608 * which are parsed by ALooperRoster::dump()
609 */
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800610status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
611{
612 const size_t SIZE = 256;
613 char buffer[SIZE];
614 String8 result;
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530615 SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
616 SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
617
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800618 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
Wonsik Kim6f675772019-08-20 17:06:53 -0700619 snprintf(buffer, SIZE - 1, "Permission Denial: "
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800620 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
621 IPCThreadState::self()->getCallingPid(),
622 IPCThreadState::self()->getCallingUid());
623 result.append(buffer);
624 } else {
Lajos Molnara42e6cc2022-05-17 15:13:31 -0700625 {
626 // capture clients under lock
627 Mutex::Autolock lock(mLock);
628 for (int i = 0, n = mClients.size(); i < n; ++i) {
629 sp<Client> c = mClients[i].promote();
630 if (c != nullptr) {
631 clients.add(c);
632 }
633 }
634
James Dongb9141222010-07-08 11:16:11 -0700635 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
636 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
Lajos Molnara42e6cc2022-05-17 15:13:31 -0700637 if (c != nullptr) {
Ravi Kumar Alamanda330c8e32014-12-22 10:05:29 +0530638 mediaRecorderClients.add(c);
James Donge579e282011-10-18 22:29:20 -0700639 }
James Dongb9141222010-07-08 11:16:11 -0700640 }
Gloria Wangdac6a312009-10-29 15:46:37 -0700641 }
642
Lajos Molnara42e6cc2022-05-17 15:13:31 -0700643 // dump clients outside of lock
644 for (const sp<Client> &c : clients) {
645 c->dump(fd, args);
646 }
647 if (mediaRecorderClients.size() == 0) {
648 result.append(" No media recorder client\n\n");
649 } else {
650 for (const sp<MediaRecorderClient> &c : mediaRecorderClients) {
651 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n",
652 c->mAttributionSource.pid);
653 result.append(buffer);
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000654 write(fd, result.c_str(), result.size());
Lajos Molnara42e6cc2022-05-17 15:13:31 -0700655 result = "\n";
656 c->dump(fd, args);
657
658 }
659 }
660
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800661 result.append(" Files opened and/or mapped:\n");
Wonsik Kim6f675772019-08-20 17:06:53 -0700662 snprintf(buffer, SIZE - 1, "/proc/%d/maps", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800663 FILE *f = fopen(buffer, "r");
664 if (f) {
665 while (!feof(f)) {
Wonsik Kim6f675772019-08-20 17:06:53 -0700666 fgets(buffer, SIZE - 1, f);
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700667 if (strstr(buffer, " /storage/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800668 strstr(buffer, " /system/sounds/") ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700669 strstr(buffer, " /data/") ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800670 strstr(buffer, " /system/media/")) {
671 result.append(" ");
672 result.append(buffer);
673 }
674 }
675 fclose(f);
676 } else {
677 result.append("couldn't open ");
678 result.append(buffer);
679 result.append("\n");
680 }
681
Wonsik Kim6f675772019-08-20 17:06:53 -0700682 snprintf(buffer, SIZE - 1, "/proc/%d/fd", getpid());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800683 DIR *d = opendir(buffer);
684 if (d) {
685 struct dirent *ent;
686 while((ent = readdir(d)) != NULL) {
687 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
Wonsik Kim6f675772019-08-20 17:06:53 -0700688 snprintf(buffer, SIZE - 1, "/proc/%d/fd/%s", getpid(), ent->d_name);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800689 struct stat s;
690 if (lstat(buffer, &s) == 0) {
691 if ((s.st_mode & S_IFMT) == S_IFLNK) {
692 char linkto[256];
693 int len = readlink(buffer, linkto, sizeof(linkto));
694 if(len > 0) {
695 if(len > 255) {
696 linkto[252] = '.';
697 linkto[253] = '.';
698 linkto[254] = '.';
699 linkto[255] = 0;
700 } else {
701 linkto[len] = 0;
702 }
Marco Nelissen73ac1ee2012-05-07 15:36:32 -0700703 if (strstr(linkto, "/storage/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800704 strstr(linkto, "/system/sounds/") == linkto ||
Dave Sparks02fa8342010-09-27 16:55:18 -0700705 strstr(linkto, "/data/") == linkto ||
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800706 strstr(linkto, "/system/media/") == linkto) {
707 result.append(" ");
708 result.append(buffer);
709 result.append(" -> ");
710 result.append(linkto);
711 result.append("\n");
712 }
713 }
714 } else {
715 result.append(" unexpected type for ");
716 result.append(buffer);
717 result.append("\n");
718 }
719 }
720 }
721 }
722 closedir(d);
723 } else {
724 result.append("couldn't open ");
725 result.append(buffer);
726 result.append("\n");
727 }
728
Marco Nelissenf09611f2015-02-13 14:12:42 -0800729 gLooperRoster.dump(fd, args);
730
Wonsik Kimd579f3e2019-10-07 14:14:09 -0700731 sp<IMediaCodecList> codecList = getCodecList();
732 dumpCodecDetails(fd, codecList, true /* decoders */);
733 dumpCodecDetails(fd, codecList, false /* !decoders */);
734
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800735 bool dumpMem = false;
Andy Hung53541292016-04-13 16:48:51 -0700736 bool unreachableMemory = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800737 for (size_t i = 0; i < args.size(); i++) {
738 if (args[i] == String16("-m")) {
739 dumpMem = true;
Andy Hung53541292016-04-13 16:48:51 -0700740 } else if (args[i] == String16("--unreachable")) {
741 unreachableMemory = true;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800742 }
743 }
744 if (dumpMem) {
Andy Hung07b745e2016-05-23 16:21:07 -0700745 result.append("\nDumping memory:\n");
746 std::string s = dumpMemoryAddresses(100 /* limit */);
747 result.append(s.c_str(), s.size());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800748 }
Andy Hung53541292016-04-13 16:48:51 -0700749 if (unreachableMemory) {
750 result.append("\nDumping unreachable memory:\n");
751 // TODO - should limit be an argument parameter?
752 std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */);
753 result.append(s.c_str(), s.size());
754 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800755 }
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +0000756 write(fd, result.c_str(), result.size());
Wonsik Kim6f675772019-08-20 17:06:53 -0700757
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800758 return NO_ERROR;
759}
760
Chih-Hung Hsieh36d0ca12016-08-09 14:31:32 -0700761void MediaPlayerService::removeClient(const wp<Client>& client)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800762{
763 Mutex::Autolock lock(mLock);
764 mClients.remove(client);
765}
766
Robert Shihee0a0e32016-08-16 16:50:54 -0700767bool MediaPlayerService::hasClient(wp<Client> client)
768{
769 Mutex::Autolock lock(mLock);
770 return mClients.indexOf(client) != NAME_NOT_FOUND;
771}
772
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700773MediaPlayerService::Client::Client(
Svet Ganov33761132021-05-13 22:51:08 +0000774 const sp<MediaPlayerService>& service, const AttributionSourceState& attributionSource,
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700775 int32_t connId, const sp<IMediaPlayerClient>& client,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700776 audio_session_t audioSessionId)
Svet Ganov33761132021-05-13 22:51:08 +0000777 : mAttributionSource(attributionSource)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800778{
Steve Block3856b092011-10-20 11:56:00 +0100779 ALOGV("Client(%d) constructor", connId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800780 mConnId = connId;
781 mService = service;
782 mClient = client;
783 mLoop = false;
784 mStatus = NO_INIT;
Eric Laurenta514bdb2010-06-21 09:27:30 -0700785 mAudioSessionId = audioSessionId;
John Grossmanc795b642012-02-22 15:38:35 -0800786 mRetransmitEndpointValid = false;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700787 mAudioAttributes = NULL;
Pawin Vongmasa082e4f72017-12-17 02:31:18 -0800788 mListener = new Listener(this);
Eric Laurenta514bdb2010-06-21 09:27:30 -0700789
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800790#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000791 ALOGD("create Antagonizer");
Pawin Vongmasa082e4f72017-12-17 02:31:18 -0800792 mAntagonizer = new Antagonizer(mListener);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800793#endif
794}
795
796MediaPlayerService::Client::~Client()
797{
Svet Ganov33761132021-05-13 22:51:08 +0000798 ALOGV("Client(%d) destructor AttributionSource = %s", mConnId,
799 mAttributionSource.toString().c_str());
Pawin Vongmasaf9c81462017-09-28 05:31:11 -0700800 mAudioOutput.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800801 wp<Client> client(this);
802 disconnect();
803 mService->removeClient(client);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -0700804 if (mAudioAttributes != NULL) {
805 free(mAudioAttributes);
806 }
jiabin156c6872017-10-06 09:47:15 -0700807 mAudioDeviceUpdatedListener.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800808}
809
810void MediaPlayerService::Client::disconnect()
811{
Svet Ganov33761132021-05-13 22:51:08 +0000812 ALOGV("disconnect(%d) from AttributionSource %s", mConnId,
813 mAttributionSource.toString().c_str());
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800814 // grab local reference and clear main reference to prevent future
815 // access to object
816 sp<MediaPlayerBase> p;
817 {
818 Mutex::Autolock l(mLock);
819 p = mPlayer;
beanzdcfefde2012-11-05 09:51:43 +0800820 mClient.clear();
Wei Jia502c2f42017-07-13 17:47:56 -0700821 mPlayer.clear();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800822 }
Andreas Huber20111aa2009-07-14 16:56:47 -0700823
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800824 // clear the notification to prevent callbacks to dead client
825 // and reset the player. We assume the player will serialize
826 // access to itself if necessary.
827 if (p != 0) {
Pawin Vongmasa082e4f72017-12-17 02:31:18 -0800828 p->setNotifyCallback(0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800829#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +0000830 ALOGD("kill Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800831 mAntagonizer->kill();
832#endif
833 p->reset();
834 }
835
Pawin Vongmasaf9c81462017-09-28 05:31:11 -0700836 {
837 Mutex::Autolock l(mLock);
838 disconnectNativeWindow_l();
839 }
Jamie Gennis7dae00b2011-10-26 18:36:31 -0700840
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800841 IPCThreadState::self()->flushCommands();
842}
843
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800844sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
845{
846 // determine if we have the right player type
Wei Jia502c2f42017-07-13 17:47:56 -0700847 sp<MediaPlayerBase> p = getPlayer();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800848 if ((p != NULL) && (p->playerType() != playerType)) {
Steve Block3856b092011-10-20 11:56:00 +0100849 ALOGV("delete player");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800850 p.clear();
851 }
852 if (p == NULL) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700853 p = MediaPlayerFactory::createPlayer(playerType, mListener,
Svet Ganov33761132021-05-13 22:51:08 +0000854 VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mAttributionSource.pid)));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800855 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700856
Jason Simmonsdb29e522011-08-12 13:46:55 -0700857 if (p != NULL) {
Svet Ganov33761132021-05-13 22:51:08 +0000858 p->setUID(VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(mAttributionSource.uid)));
Jason Simmonsdb29e522011-08-12 13:46:55 -0700859 }
Andreas Huber9b80c2b2011-06-30 15:47:02 -0700860
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -0800861 return p;
862}
863
jiabin156c6872017-10-06 09:47:15 -0700864void MediaPlayerService::Client::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate(
865 audio_io_handle_t audioIo,
Robert Wub7f8edc2024-11-04 19:54:38 +0000866 const DeviceIdVector& deviceIds) {
867 ALOGD("onAudioDeviceUpdate deviceIds: %s", toString(deviceIds).c_str());
jiabin156c6872017-10-06 09:47:15 -0700868 sp<MediaPlayerBase> listener = mListener.promote();
869 if (listener != NULL) {
Robert Wub7f8edc2024-11-04 19:54:38 +0000870 // Java should query the new device ids once it gets the event.
871 // TODO(b/378505346): Pass the deviceIds to Java to avoid race conditions.
872 listener->sendEvent(MEDIA_AUDIO_ROUTING_CHANGED, audioIo);
jiabin156c6872017-10-06 09:47:15 -0700873 } else {
874 ALOGW("listener for process %d death is gone", MEDIA_AUDIO_ROUTING_CHANGED);
875 }
876}
877
John Grossmanc795b642012-02-22 15:38:35 -0800878sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
879 player_type playerType)
880{
881 ALOGV("player type = %d", playerType);
882
883 // create the right type of player
884 sp<MediaPlayerBase> p = createPlayer(playerType);
885 if (p == NULL) {
886 return p;
887 }
888
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700889 std::vector<DeathNotifier> deathNotifiers;
890
891 // Listen to death of media.extractor service
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700892 sp<IServiceManager> sm = defaultServiceManager();
893 sp<IBinder> binder = sm->getService(String16("media.extractor"));
Marco Nelissen1b5a7ee2016-09-30 13:54:30 -0700894 if (binder == NULL) {
895 ALOGE("extractor service not available");
896 return NULL;
897 }
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700898 deathNotifiers.emplace_back(
899 binder, [l = wp<MediaPlayerBase>(p)]() {
900 sp<MediaPlayerBase> listener = l.promote();
901 if (listener) {
902 ALOGI("media.extractor died. Sending death notification.");
903 listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
904 MEDIAEXTRACTOR_PROCESS_DEATH);
905 } else {
906 ALOGW("media.extractor died without a death handler.");
907 }
908 });
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700909
Marco Nelissena0c98412019-03-29 12:10:19 -0700910 {
911 using ::android::hidl::base::V1_0::IBase;
912
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700913 // Listen to death of OMX service
Marco Nelissena0c98412019-03-29 12:10:19 -0700914 {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700915 sp<IBase> base = ::android::hardware::media::omx::V1_0::
916 IOmx::getService();
917 if (base == nullptr) {
Marco Nelissena0c98412019-03-29 12:10:19 -0700918 ALOGD("OMX service is not available");
919 } else {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700920 deathNotifiers.emplace_back(
921 base, [l = wp<MediaPlayerBase>(p)]() {
922 sp<MediaPlayerBase> listener = l.promote();
923 if (listener) {
924 ALOGI("OMX service died. "
925 "Sending death notification.");
926 listener->sendEvent(
927 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
928 MEDIACODEC_PROCESS_DEATH);
929 } else {
930 ALOGW("OMX service died without a death handler.");
931 }
932 });
Marco Nelissena0c98412019-03-29 12:10:19 -0700933 }
934 }
935
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700936 // Listen to death of Codec2 services
Marco Nelissena0c98412019-03-29 12:10:19 -0700937 {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700938 for (std::shared_ptr<Codec2Client> const& client :
939 Codec2Client::CreateFromAllServices()) {
Wonsik Kime8e98152022-12-16 16:04:17 -0800940 sp<IBase> hidlBase = client->getHidlBase();
941 ::ndk::SpAIBinder aidlBase = client->getAidlBase();
942 auto onBinderDied = [l = wp<MediaPlayerBase>(p),
943 name = std::string(client->getServiceName())]() {
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700944 sp<MediaPlayerBase> listener = l.promote();
945 if (listener) {
946 ALOGI("Codec2 service \"%s\" died. "
947 "Sending death notification.",
948 name.c_str());
949 listener->sendEvent(
950 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
951 MEDIACODEC_PROCESS_DEATH);
952 } else {
953 ALOGW("Codec2 service \"%s\" died "
954 "without a death handler.",
955 name.c_str());
956 }
Wonsik Kime8e98152022-12-16 16:04:17 -0800957 };
958 if (hidlBase) {
959 deathNotifiers.emplace_back(hidlBase, onBinderDied);
960 } else if (aidlBase.get() != nullptr) {
961 deathNotifiers.emplace_back(aidlBase, onBinderDied);
962 }
Marco Nelissene4da6c62019-04-01 15:01:41 -0700963 }
Marco Nelissena0c98412019-03-29 12:10:19 -0700964 }
Marco Nelissen1b5a7ee2016-09-30 13:54:30 -0700965 }
Marco Nelissen6dc3a3e2016-04-29 15:42:06 -0700966
Pawin Vongmasaf9c81462017-09-28 05:31:11 -0700967 Mutex::Autolock lock(mLock);
968
Pawin Vongmasa270dd6a2019-04-06 04:41:15 -0700969 mDeathNotifiers.clear();
970 mDeathNotifiers.swap(deathNotifiers);
jiabin156c6872017-10-06 09:47:15 -0700971 mAudioDeviceUpdatedListener = new AudioDeviceUpdatedNotifier(p);
Pawin Vongmasaf9c81462017-09-28 05:31:11 -0700972
John Grossmanc795b642012-02-22 15:38:35 -0800973 if (!p->hardwareOutput()) {
Svet Ganov33761132021-05-13 22:51:08 +0000974 mAudioOutput = new AudioOutput(mAudioSessionId, mAttributionSource,
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700975 mAudioAttributes, mAudioDeviceUpdatedListener);
John Grossmanc795b642012-02-22 15:38:35 -0800976 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
977 }
978
979 return p;
980}
981
Pawin Vongmasaf9c81462017-09-28 05:31:11 -0700982status_t MediaPlayerService::Client::setDataSource_post(
John Grossmanc795b642012-02-22 15:38:35 -0800983 const sp<MediaPlayerBase>& p,
984 status_t status)
985{
986 ALOGV(" setDataSource");
Pawin Vongmasaf9c81462017-09-28 05:31:11 -0700987 if (status != OK) {
988 ALOGE(" error: %d", status);
989 return status;
John Grossmanc795b642012-02-22 15:38:35 -0800990 }
991
992 // Set the re-transmission endpoint if one was chosen.
993 if (mRetransmitEndpointValid) {
Pawin Vongmasaf9c81462017-09-28 05:31:11 -0700994 status = p->setRetransmitEndpoint(&mRetransmitEndpoint);
995 if (status != NO_ERROR) {
996 ALOGE("setRetransmitEndpoint error: %d", status);
John Grossmanc795b642012-02-22 15:38:35 -0800997 }
998 }
999
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001000 if (status == OK) {
1001 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -08001002 mPlayer = p;
1003 }
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001004 return status;
John Grossmanc795b642012-02-22 15:38:35 -08001005}
1006
Andreas Huber2db84552010-01-28 11:19:57 -08001007status_t MediaPlayerService::Client::setDataSource(
Andreas Huber1b86fe02014-01-29 11:13:26 -08001008 const sp<IMediaHTTPService> &httpService,
1009 const char *url,
1010 const KeyedVector<String8, String8> *headers)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001011{
Steve Block3856b092011-10-20 11:56:00 +01001012 ALOGV("setDataSource(%s)", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001013 if (url == NULL)
1014 return UNKNOWN_ERROR;
1015
Dave Burked681bbb2011-08-30 14:39:17 +01001016 if ((strncmp(url, "http://", 7) == 0) ||
1017 (strncmp(url, "https://", 8) == 0) ||
1018 (strncmp(url, "rtsp://", 7) == 0)) {
1019 if (!checkPermission("android.permission.INTERNET")) {
1020 return PERMISSION_DENIED;
1021 }
1022 }
1023
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001024 if (strncmp(url, "content://", 10) == 0) {
1025 // get a filedescriptor for the content Uri and
1026 // pass it to the setDataSource(fd) method
1027
1028 String16 url16(url);
1029 int fd = android::openContentProviderFile(url16);
1030 if (fd < 0)
1031 {
Steve Block29357bc2012-01-06 19:20:56 +00001032 ALOGE("Couldn't open fd for %s", url);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001033 return UNKNOWN_ERROR;
1034 }
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001035 status_t status = setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001036 close(fd);
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001037 return mStatus = status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001038 } else {
John Grossman44a7e422012-06-21 17:29:24 -07001039 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
John Grossmanc795b642012-02-22 15:38:35 -08001040 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1041 if (p == NULL) {
1042 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001043 }
1044
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001045 return mStatus =
1046 setDataSource_post(
1047 p, p->setDataSource(httpService, url, headers));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001048 }
1049}
1050
1051status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
1052{
Marco Nelissen83b0fd92015-09-16 13:48:07 -07001053 ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
1054 fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001055 struct stat sb;
1056 int ret = fstat(fd, &sb);
1057 if (ret != 0) {
Steve Block29357bc2012-01-06 19:20:56 +00001058 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001059 return UNKNOWN_ERROR;
1060 }
1061
Andy Hung833b4752016-04-04 17:15:48 -07001062 ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev));
Steve Block3856b092011-10-20 11:56:00 +01001063 ALOGV("st_mode = %u", sb.st_mode);
Mark Salyzyn77342f72014-06-18 16:31:32 -07001064 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
1065 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
Andy Hung833b4752016-04-04 17:15:48 -07001066 ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001067
1068 if (offset >= sb.st_size) {
Steve Block29357bc2012-01-06 19:20:56 +00001069 ALOGE("offset error");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001070 return UNKNOWN_ERROR;
1071 }
1072 if (offset + length > sb.st_size) {
1073 length = sb.st_size - offset;
Andy Hung833b4752016-04-04 17:15:48 -07001074 ALOGV("calculated length = %lld", (long long)length);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001075 }
1076
John Grossman44a7e422012-06-21 17:29:24 -07001077 player_type playerType = MediaPlayerFactory::getPlayerType(this,
1078 fd,
1079 offset,
1080 length);
John Grossmanc795b642012-02-22 15:38:35 -08001081 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1082 if (p == NULL) {
1083 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001084 }
1085
1086 // now set data source
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001087 return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length));
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001088}
1089
Andreas Hubere2b10282010-11-23 11:41:34 -08001090status_t MediaPlayerService::Client::setDataSource(
1091 const sp<IStreamSource> &source) {
1092 // create the right type of player
John Grossman44a7e422012-06-21 17:29:24 -07001093 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
John Grossmanc795b642012-02-22 15:38:35 -08001094 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
Andreas Hubere2b10282010-11-23 11:41:34 -08001095 if (p == NULL) {
1096 return NO_INIT;
1097 }
1098
Andreas Hubere2b10282010-11-23 11:41:34 -08001099 // now set data source
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001100 return mStatus = setDataSource_post(p, p->setDataSource(source));
Andreas Hubere2b10282010-11-23 11:41:34 -08001101}
1102
Chris Watkins99f31602015-03-20 13:06:33 -07001103status_t MediaPlayerService::Client::setDataSource(
1104 const sp<IDataSource> &source) {
Dongwon Kangd91dc5a2017-10-10 00:07:09 -07001105 sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source);
Chris Watkins99f31602015-03-20 13:06:33 -07001106 player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
1107 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1108 if (p == NULL) {
1109 return NO_INIT;
1110 }
1111 // now set data source
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001112 return mStatus = setDataSource_post(p, p->setDataSource(dataSource));
Chris Watkins99f31602015-03-20 13:06:33 -07001113}
1114
Byeongjo Park28225ab2019-01-24 20:31:19 +09001115status_t MediaPlayerService::Client::setDataSource(
1116 const String8& rtpParams) {
1117 player_type playerType = NU_PLAYER;
1118 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1119 if (p == NULL) {
1120 return NO_INIT;
1121 }
1122 // now set data source
1123 return mStatus = setDataSource_post(p, p->setDataSource(rtpParams));
1124}
1125
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001126void MediaPlayerService::Client::disconnectNativeWindow_l() {
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001127 if (mConnectedWindow != NULL) {
Chong Zhang181fd9b2017-02-16 15:53:03 -08001128 status_t err = nativeWindowDisconnect(
1129 mConnectedWindow.get(), "disconnectNativeWindow");
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001130
1131 if (err != OK) {
Chong Zhang181fd9b2017-02-16 15:53:03 -08001132 ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001133 strerror(-err), err);
1134 }
1135 }
1136 mConnectedWindow.clear();
1137}
1138
Glenn Kasten11731182011-02-08 17:26:17 -08001139status_t MediaPlayerService::Client::setVideoSurfaceTexture(
Andy McFadden484566c2012-12-18 09:46:54 -08001140 const sp<IGraphicBufferProducer>& bufferProducer)
Glenn Kasten11731182011-02-08 17:26:17 -08001141{
Andy McFadden484566c2012-12-18 09:46:54 -08001142 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
Glenn Kasten11731182011-02-08 17:26:17 -08001143 sp<MediaPlayerBase> p = getPlayer();
1144 if (p == 0) return UNKNOWN_ERROR;
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001145
Marco Nelissenf8880202014-11-14 07:58:25 -08001146 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001147 if (mConnectedWindowBinder == binder) {
1148 return OK;
1149 }
1150
1151 sp<ANativeWindow> anw;
Andy McFadden484566c2012-12-18 09:46:54 -08001152 if (bufferProducer != NULL) {
Marco Nelissenee08f7e2013-09-16 13:30:01 -07001153 anw = new Surface(bufferProducer, true /* controlledByApp */);
Chong Zhang181fd9b2017-02-16 15:53:03 -08001154 status_t err = nativeWindowConnect(anw.get(), "setVideoSurfaceTexture");
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001155
1156 if (err != OK) {
Steve Block29357bc2012-01-06 19:20:56 +00001157 ALOGE("setVideoSurfaceTexture failed: %d", err);
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001158 // Note that we must do the reset before disconnecting from the ANW.
1159 // Otherwise queue/dequeue calls could be made on the disconnected
1160 // ANW, which may result in errors.
1161 reset();
1162
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001163 Mutex::Autolock lock(mLock);
1164 disconnectNativeWindow_l();
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001165
1166 return err;
1167 }
1168 }
1169
Andy McFadden484566c2012-12-18 09:46:54 -08001170 // Note that we must set the player's new GraphicBufferProducer before
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001171 // disconnecting the old one. Otherwise queue/dequeue calls could be made
1172 // on the disconnected ANW, which may result in errors.
Andy McFadden484566c2012-12-18 09:46:54 -08001173 status_t err = p->setVideoSurfaceTexture(bufferProducer);
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001174
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001175 mLock.lock();
1176 disconnectNativeWindow_l();
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001177
1178 if (err == OK) {
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001179 mConnectedWindow = anw;
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001180 mConnectedWindowBinder = binder;
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001181 mLock.unlock();
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001182 } else {
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001183 mLock.unlock();
1184 status_t err = nativeWindowDisconnect(
1185 anw.get(), "disconnectNativeWindow");
1186
1187 if (err != OK) {
1188 ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
1189 strerror(-err), err);
1190 }
Jamie Gennis7dae00b2011-10-26 18:36:31 -07001191 }
1192
1193 return err;
Glenn Kasten11731182011-02-08 17:26:17 -08001194}
1195
Nicolas Catania1d187f12009-05-12 23:25:55 -07001196status_t MediaPlayerService::Client::invoke(const Parcel& request,
1197 Parcel *reply)
1198{
1199 sp<MediaPlayerBase> p = getPlayer();
1200 if (p == NULL) return UNKNOWN_ERROR;
1201 return p->invoke(request, reply);
1202}
1203
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001204// This call doesn't need to access the native player.
1205status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
1206{
1207 status_t status;
nikoa64c8c72009-07-20 15:07:26 -07001208 media::Metadata::Filter allow, drop;
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001209
Nicolas Catania48290382009-07-10 13:53:06 -07001210 if (unmarshallFilter(filter, &allow, &status) &&
1211 unmarshallFilter(filter, &drop, &status)) {
1212 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001213
1214 mMetadataAllow = allow;
1215 mMetadataDrop = drop;
1216 }
1217 return status;
1218}
1219
Nicolas Catania48290382009-07-10 13:53:06 -07001220status_t MediaPlayerService::Client::getMetadata(
Mark Salyzyn77342f72014-06-18 16:31:32 -07001221 bool update_only, bool /*apply_filter*/, Parcel *reply)
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -07001222{
nikoa64c8c72009-07-20 15:07:26 -07001223 sp<MediaPlayerBase> player = getPlayer();
1224 if (player == 0) return UNKNOWN_ERROR;
Nicolas Catania48290382009-07-10 13:53:06 -07001225
nikod608a812009-07-16 16:39:53 -07001226 status_t status;
1227 // Placeholder for the return code, updated by the caller.
1228 reply->writeInt32(-1);
1229
nikoa64c8c72009-07-20 15:07:26 -07001230 media::Metadata::Filter ids;
Nicolas Catania48290382009-07-10 13:53:06 -07001231
1232 // We don't block notifications while we fetch the data. We clear
1233 // mMetadataUpdated first so we don't lose notifications happening
1234 // during the rest of this call.
1235 {
1236 Mutex::Autolock lock(mLock);
1237 if (update_only) {
nikod608a812009-07-16 16:39:53 -07001238 ids = mMetadataUpdated;
Nicolas Catania48290382009-07-10 13:53:06 -07001239 }
1240 mMetadataUpdated.clear();
1241 }
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -07001242
nikoa64c8c72009-07-20 15:07:26 -07001243 media::Metadata metadata(reply);
Nicolas Catania48290382009-07-10 13:53:06 -07001244
nikoa64c8c72009-07-20 15:07:26 -07001245 metadata.appendHeader();
1246 status = player->getMetadata(ids, reply);
nikod608a812009-07-16 16:39:53 -07001247
1248 if (status != OK) {
nikoa64c8c72009-07-20 15:07:26 -07001249 metadata.resetParcel();
Steve Block29357bc2012-01-06 19:20:56 +00001250 ALOGE("getMetadata failed %d", status);
nikod608a812009-07-16 16:39:53 -07001251 return status;
1252 }
1253
1254 // FIXME: Implement filtering on the result. Not critical since
1255 // filtering takes place on the update notifications already. This
1256 // would be when all the metadata are fetch and a filter is set.
1257
nikod608a812009-07-16 16:39:53 -07001258 // Everything is fine, update the metadata length.
nikoa64c8c72009-07-20 15:07:26 -07001259 metadata.updateLength();
nikod608a812009-07-16 16:39:53 -07001260 return OK;
Nicolas Catania8e1b6cc2009-07-09 09:21:33 -07001261}
1262
Wei Jiad399e7e2016-10-26 15:49:11 -07001263status_t MediaPlayerService::Client::setBufferingSettings(
1264 const BufferingSettings& buffering)
1265{
Wei Jiadc6f3402017-01-09 15:04:18 -08001266 ALOGV("[%d] setBufferingSettings{%s}",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +00001267 mConnId, buffering.toString().c_str());
Wei Jiad399e7e2016-10-26 15:49:11 -07001268 sp<MediaPlayerBase> p = getPlayer();
1269 if (p == 0) return UNKNOWN_ERROR;
1270 return p->setBufferingSettings(buffering);
1271}
1272
Wei Jia9bb38032017-03-23 18:00:38 -07001273status_t MediaPlayerService::Client::getBufferingSettings(
Wei Jiad399e7e2016-10-26 15:49:11 -07001274 BufferingSettings* buffering /* nonnull */)
1275{
1276 sp<MediaPlayerBase> p = getPlayer();
1277 // TODO: create mPlayer on demand.
1278 if (p == 0) return UNKNOWN_ERROR;
Wei Jia9bb38032017-03-23 18:00:38 -07001279 status_t ret = p->getBufferingSettings(buffering);
Wei Jiad399e7e2016-10-26 15:49:11 -07001280 if (ret == NO_ERROR) {
Wei Jia9bb38032017-03-23 18:00:38 -07001281 ALOGV("[%d] getBufferingSettings{%s}",
Tomasz Wasilczyk09977ff2023-08-11 15:52:22 +00001282 mConnId, buffering->toString().c_str());
Wei Jiad399e7e2016-10-26 15:49:11 -07001283 } else {
Wei Jia9bb38032017-03-23 18:00:38 -07001284 ALOGE("[%d] getBufferingSettings returned %d", mConnId, ret);
Wei Jiad399e7e2016-10-26 15:49:11 -07001285 }
1286 return ret;
1287}
1288
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001289status_t MediaPlayerService::Client::prepareAsync()
1290{
Steve Block3856b092011-10-20 11:56:00 +01001291 ALOGV("[%d] prepareAsync", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001292 sp<MediaPlayerBase> p = getPlayer();
1293 if (p == 0) return UNKNOWN_ERROR;
1294 status_t ret = p->prepareAsync();
1295#if CALLBACK_ANTAGONIZER
Steve Blockb8a80522011-12-20 16:23:08 +00001296 ALOGD("start Antagonizer");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001297 if (ret == NO_ERROR) mAntagonizer->start();
1298#endif
1299 return ret;
1300}
1301
1302status_t MediaPlayerService::Client::start()
1303{
Steve Block3856b092011-10-20 11:56:00 +01001304 ALOGV("[%d] start", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001305 sp<MediaPlayerBase> p = getPlayer();
1306 if (p == 0) return UNKNOWN_ERROR;
1307 p->setLooping(mLoop);
1308 return p->start();
1309}
1310
1311status_t MediaPlayerService::Client::stop()
1312{
Steve Block3856b092011-10-20 11:56:00 +01001313 ALOGV("[%d] stop", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001314 sp<MediaPlayerBase> p = getPlayer();
1315 if (p == 0) return UNKNOWN_ERROR;
1316 return p->stop();
1317}
1318
1319status_t MediaPlayerService::Client::pause()
1320{
Steve Block3856b092011-10-20 11:56:00 +01001321 ALOGV("[%d] pause", mConnId);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001322 sp<MediaPlayerBase> p = getPlayer();
1323 if (p == 0) return UNKNOWN_ERROR;
1324 return p->pause();
1325}
1326
1327status_t MediaPlayerService::Client::isPlaying(bool* state)
1328{
1329 *state = false;
1330 sp<MediaPlayerBase> p = getPlayer();
1331 if (p == 0) return UNKNOWN_ERROR;
1332 *state = p->isPlaying();
Steve Block3856b092011-10-20 11:56:00 +01001333 ALOGV("[%d] isPlaying: %d", mConnId, *state);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001334 return NO_ERROR;
1335}
1336
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001337status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
Wei Jia98160162015-02-04 17:01:11 -08001338{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001339 ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
1340 mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Wei Jia98160162015-02-04 17:01:11 -08001341 sp<MediaPlayerBase> p = getPlayer();
1342 if (p == 0) return UNKNOWN_ERROR;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07001343 return p->setPlaybackSettings(rate);
1344}
1345
1346status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
1347{
1348 sp<MediaPlayerBase> p = getPlayer();
1349 if (p == 0) return UNKNOWN_ERROR;
1350 status_t ret = p->getPlaybackSettings(rate);
1351 if (ret == NO_ERROR) {
1352 ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
1353 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
1354 } else {
1355 ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
1356 }
1357 return ret;
1358}
1359
1360status_t MediaPlayerService::Client::setSyncSettings(
1361 const AVSyncSettings& sync, float videoFpsHint)
1362{
1363 ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
1364 mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
1365 sp<MediaPlayerBase> p = getPlayer();
1366 if (p == 0) return UNKNOWN_ERROR;
1367 return p->setSyncSettings(sync, videoFpsHint);
1368}
1369
1370status_t MediaPlayerService::Client::getSyncSettings(
1371 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
1372{
1373 sp<MediaPlayerBase> p = getPlayer();
1374 if (p == 0) return UNKNOWN_ERROR;
1375 status_t ret = p->getSyncSettings(sync, videoFps);
1376 if (ret == NO_ERROR) {
1377 ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
1378 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
1379 } else {
1380 ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
1381 }
1382 return ret;
Wei Jia98160162015-02-04 17:01:11 -08001383}
1384
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001385status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1386{
Steve Block3856b092011-10-20 11:56:00 +01001387 ALOGV("getCurrentPosition");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001388 sp<MediaPlayerBase> p = getPlayer();
1389 if (p == 0) return UNKNOWN_ERROR;
1390 status_t ret = p->getCurrentPosition(msec);
1391 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001392 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001393 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001394 ALOGE("getCurrentPosition returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001395 }
1396 return ret;
1397}
1398
1399status_t MediaPlayerService::Client::getDuration(int *msec)
1400{
Steve Block3856b092011-10-20 11:56:00 +01001401 ALOGV("getDuration");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001402 sp<MediaPlayerBase> p = getPlayer();
1403 if (p == 0) return UNKNOWN_ERROR;
1404 status_t ret = p->getDuration(msec);
1405 if (ret == NO_ERROR) {
Steve Block3856b092011-10-20 11:56:00 +01001406 ALOGV("[%d] getDuration = %d", mConnId, *msec);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001407 } else {
Steve Block29357bc2012-01-06 19:20:56 +00001408 ALOGE("getDuration returned %d", ret);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001409 }
1410 return ret;
1411}
1412
Marco Nelissen6b74d672012-02-28 16:07:44 -08001413status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1414 ALOGV("setNextPlayer");
1415 Mutex::Autolock l(mLock);
1416 sp<Client> c = static_cast<Client*>(player.get());
Wei Jia28284122016-08-30 13:49:06 -07001417 if (c != NULL && !mService->hasClient(c)) {
Robert Shihee0a0e32016-08-16 16:50:54 -07001418 return BAD_VALUE;
1419 }
1420
Marco Nelissen6b74d672012-02-28 16:07:44 -08001421 mNextClient = c;
John Grossman5f7e55e2012-08-24 14:47:25 -07001422
1423 if (c != NULL) {
1424 if (mAudioOutput != NULL) {
1425 mAudioOutput->setNextOutput(c->mAudioOutput);
1426 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1427 ALOGE("no current audio output");
1428 }
1429
1430 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1431 mPlayer->setNextPlayer(mNextClient->getPlayer());
1432 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001433 }
John Grossman5f7e55e2012-08-24 14:47:25 -07001434
Marco Nelissen6b74d672012-02-28 16:07:44 -08001435 return OK;
1436}
1437
Andy Hung9fc8b5c2017-01-24 13:36:48 -08001438VolumeShaper::Status MediaPlayerService::Client::applyVolumeShaper(
1439 const sp<VolumeShaper::Configuration>& configuration,
1440 const sp<VolumeShaper::Operation>& operation) {
1441 // for hardware output, call player instead
1442 ALOGV("Client::applyVolumeShaper(%p)", this);
1443 sp<MediaPlayerBase> p = getPlayer();
1444 {
1445 Mutex::Autolock l(mLock);
1446 if (p != 0 && p->hardwareOutput()) {
1447 // TODO: investigate internal implementation
1448 return VolumeShaper::Status(INVALID_OPERATION);
1449 }
1450 if (mAudioOutput.get() != nullptr) {
1451 return mAudioOutput->applyVolumeShaper(configuration, operation);
1452 }
1453 }
1454 return VolumeShaper::Status(INVALID_OPERATION);
1455}
1456
1457sp<VolumeShaper::State> MediaPlayerService::Client::getVolumeShaperState(int id) {
1458 // for hardware output, call player instead
1459 ALOGV("Client::getVolumeShaperState(%p)", this);
1460 sp<MediaPlayerBase> p = getPlayer();
1461 {
1462 Mutex::Autolock l(mLock);
1463 if (p != 0 && p->hardwareOutput()) {
1464 // TODO: investigate internal implementation.
1465 return nullptr;
1466 }
1467 if (mAudioOutput.get() != nullptr) {
1468 return mAudioOutput->getVolumeShaperState(id);
1469 }
1470 }
1471 return nullptr;
1472}
1473
Wei Jiac5de0912016-11-18 10:22:14 -08001474status_t MediaPlayerService::Client::seekTo(int msec, MediaPlayerSeekMode mode)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001475{
Wei Jiac5de0912016-11-18 10:22:14 -08001476 ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001477 sp<MediaPlayerBase> p = getPlayer();
1478 if (p == 0) return UNKNOWN_ERROR;
Wei Jiac5de0912016-11-18 10:22:14 -08001479 return p->seekTo(msec, mode);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001480}
1481
1482status_t MediaPlayerService::Client::reset()
1483{
Steve Block3856b092011-10-20 11:56:00 +01001484 ALOGV("[%d] reset", mConnId);
John Grossmanc795b642012-02-22 15:38:35 -08001485 mRetransmitEndpointValid = false;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001486 sp<MediaPlayerBase> p = getPlayer();
1487 if (p == 0) return UNKNOWN_ERROR;
1488 return p->reset();
1489}
1490
Wei Jia52c28512017-09-13 18:17:51 -07001491status_t MediaPlayerService::Client::notifyAt(int64_t mediaTimeUs)
1492{
1493 ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs);
1494 sp<MediaPlayerBase> p = getPlayer();
1495 if (p == 0) return UNKNOWN_ERROR;
1496 return p->notifyAt(mediaTimeUs);
1497}
1498
Glenn Kastenfff6d712012-01-12 16:38:12 -08001499status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001500{
Steve Block3856b092011-10-20 11:56:00 +01001501 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001502 // TODO: for hardware output, call player instead
1503 Mutex::Autolock l(mLock);
1504 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1505 return NO_ERROR;
1506}
1507
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001508status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1509{
1510 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1511 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001512 if (mAudioAttributes == NULL) {
1513 return NO_MEMORY;
1514 }
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001515 unmarshallAudioAttributes(parcel, mAudioAttributes);
1516
1517 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1518 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1519 mAudioAttributes->tags);
1520
1521 if (mAudioOutput != 0) {
1522 mAudioOutput->setAudioAttributes(mAudioAttributes);
1523 }
1524 return NO_ERROR;
1525}
1526
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001527status_t MediaPlayerService::Client::setLooping(int loop)
1528{
Steve Block3856b092011-10-20 11:56:00 +01001529 ALOGV("[%d] setLooping(%d)", mConnId, loop);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001530 mLoop = loop;
1531 sp<MediaPlayerBase> p = getPlayer();
1532 if (p != 0) return p->setLooping(loop);
1533 return NO_ERROR;
1534}
1535
1536status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1537{
Steve Block3856b092011-10-20 11:56:00 +01001538 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
John Grossman761defc2012-02-09 15:09:05 -08001539
1540 // for hardware output, call player instead
1541 sp<MediaPlayerBase> p = getPlayer();
1542 {
1543 Mutex::Autolock l(mLock);
1544 if (p != 0 && p->hardwareOutput()) {
1545 MediaPlayerHWInterface* hwp =
1546 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1547 return hwp->setVolume(leftVolume, rightVolume);
1548 } else {
1549 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1550 return NO_ERROR;
1551 }
1552 }
1553
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001554 return NO_ERROR;
1555}
1556
Eric Laurent2beeb502010-07-16 07:43:46 -07001557status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1558{
Steve Block3856b092011-10-20 11:56:00 +01001559 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
Eric Laurent2beeb502010-07-16 07:43:46 -07001560 Mutex::Autolock l(mLock);
1561 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1562 return NO_ERROR;
1563}
1564
1565status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1566{
Steve Block3856b092011-10-20 11:56:00 +01001567 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
Eric Laurent2beeb502010-07-16 07:43:46 -07001568 Mutex::Autolock l(mLock);
1569 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1570 return NO_ERROR;
1571}
Nicolas Catania48290382009-07-10 13:53:06 -07001572
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001573status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
Steve Block3856b092011-10-20 11:56:00 +01001574 ALOGV("[%d] setParameter(%d)", mConnId, key);
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07001575 switch (key) {
1576 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1577 {
1578 Mutex::Autolock l(mLock);
1579 return setAudioAttributes_l(request);
1580 }
1581 default:
1582 sp<MediaPlayerBase> p = getPlayer();
1583 if (p == 0) { return UNKNOWN_ERROR; }
1584 return p->setParameter(key, request);
1585 }
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001586}
1587
1588status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
Steve Block3856b092011-10-20 11:56:00 +01001589 ALOGV("[%d] getParameter(%d)", mConnId, key);
Gloria Wang4f9e47f2011-04-25 17:28:22 -07001590 sp<MediaPlayerBase> p = getPlayer();
1591 if (p == 0) return UNKNOWN_ERROR;
1592 return p->getParameter(key, reply);
1593}
1594
John Grossmanc795b642012-02-22 15:38:35 -08001595status_t MediaPlayerService::Client::setRetransmitEndpoint(
1596 const struct sockaddr_in* endpoint) {
1597
1598 if (NULL != endpoint) {
1599 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1600 uint16_t p = ntohs(endpoint->sin_port);
1601 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1602 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1603 } else {
1604 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1605 }
1606
1607 sp<MediaPlayerBase> p = getPlayer();
1608
1609 // Right now, the only valid time to set a retransmit endpoint is before
1610 // player selection has been made (since the presence or absence of a
1611 // retransmit endpoint is going to determine which player is selected during
1612 // setDataSource).
1613 if (p != 0) return INVALID_OPERATION;
1614
1615 if (NULL != endpoint) {
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001616 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -08001617 mRetransmitEndpoint = *endpoint;
1618 mRetransmitEndpointValid = true;
1619 } else {
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001620 Mutex::Autolock lock(mLock);
John Grossmanc795b642012-02-22 15:38:35 -08001621 mRetransmitEndpointValid = false;
1622 }
1623
1624 return NO_ERROR;
1625}
1626
John Grossman44a7e422012-06-21 17:29:24 -07001627status_t MediaPlayerService::Client::getRetransmitEndpoint(
1628 struct sockaddr_in* endpoint)
1629{
1630 if (NULL == endpoint)
1631 return BAD_VALUE;
1632
1633 sp<MediaPlayerBase> p = getPlayer();
1634
1635 if (p != NULL)
1636 return p->getRetransmitEndpoint(endpoint);
1637
Pawin Vongmasaf9c81462017-09-28 05:31:11 -07001638 Mutex::Autolock lock(mLock);
John Grossman44a7e422012-06-21 17:29:24 -07001639 if (!mRetransmitEndpointValid)
1640 return NO_INIT;
1641
1642 *endpoint = mRetransmitEndpoint;
1643
1644 return NO_ERROR;
1645}
1646
Gloria Wangb483c472011-04-11 17:23:27 -07001647void MediaPlayerService::Client::notify(
Pawin Vongmasa082e4f72017-12-17 02:31:18 -08001648 int msg, int ext1, int ext2, const Parcel *obj)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001649{
James Dongb8a98252012-08-26 16:13:03 -07001650 sp<IMediaPlayerClient> c;
Haynes Mathew George6cb33752015-06-22 14:16:42 -07001651 sp<Client> nextClient;
1652 status_t errStartNext = NO_ERROR;
Marco Nelissen6b74d672012-02-28 16:07:44 -08001653 {
Pawin Vongmasa082e4f72017-12-17 02:31:18 -08001654 Mutex::Autolock l(mLock);
1655 c = mClient;
1656 if (msg == MEDIA_PLAYBACK_COMPLETE && mNextClient != NULL) {
1657 nextClient = mNextClient;
Haynes Mathew George6cb33752015-06-22 14:16:42 -07001658
Pawin Vongmasa082e4f72017-12-17 02:31:18 -08001659 if (mAudioOutput != NULL)
1660 mAudioOutput->switchToNextOutput();
Haynes Mathew George6cb33752015-06-22 14:16:42 -07001661
1662 errStartNext = nextClient->start();
1663 }
1664 }
1665
1666 if (nextClient != NULL) {
1667 sp<IMediaPlayerClient> nc;
1668 {
1669 Mutex::Autolock l(nextClient->mLock);
1670 nc = nextClient->mClient;
1671 }
1672 if (nc != NULL) {
1673 if (errStartNext == NO_ERROR) {
1674 nc->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1675 } else {
1676 nc->notify(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN , 0, obj);
1677 ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext);
Wei Jia2afac0c2016-01-07 12:13:07 -08001678 }
Marco Nelissen6b74d672012-02-28 16:07:44 -08001679 }
1680 }
1681
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001682 if (MEDIA_INFO == msg &&
Nicolas Catania48290382009-07-10 13:53:06 -07001683 MEDIA_INFO_METADATA_UPDATE == ext1) {
nikoa64c8c72009-07-20 15:07:26 -07001684 const media::Metadata::Type metadata_type = ext2;
Nicolas Catania48290382009-07-10 13:53:06 -07001685
Pawin Vongmasa082e4f72017-12-17 02:31:18 -08001686 if(shouldDropMetadata(metadata_type)) {
Nicolas Catania48290382009-07-10 13:53:06 -07001687 return;
1688 }
1689
1690 // Update the list of metadata that have changed. getMetadata
1691 // also access mMetadataUpdated and clears it.
Pawin Vongmasa082e4f72017-12-17 02:31:18 -08001692 addNewMetadataUpdate(metadata_type);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001693 }
James Dongb8a98252012-08-26 16:13:03 -07001694
1695 if (c != NULL) {
Pawin Vongmasa082e4f72017-12-17 02:31:18 -08001696 ALOGV("[%d] notify (%d, %d, %d)", mConnId, msg, ext1, ext2);
James Dongb8a98252012-08-26 16:13:03 -07001697 c->notify(msg, ext1, ext2, obj);
1698 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001699}
1700
Nicolas Catania48290382009-07-10 13:53:06 -07001701
nikoa64c8c72009-07-20 15:07:26 -07001702bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001703{
Nicolas Catania48290382009-07-10 13:53:06 -07001704 Mutex::Autolock lock(mLock);
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001705
Nicolas Catania48290382009-07-10 13:53:06 -07001706 if (findMetadata(mMetadataDrop, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001707 return true;
1708 }
1709
Nicolas Catania48290382009-07-10 13:53:06 -07001710 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001711 return false;
Nicolas Catania48290382009-07-10 13:53:06 -07001712 } else {
Nicolas Cataniaa7e0e8b2009-07-08 08:57:42 -07001713 return true;
1714 }
1715}
1716
Nicolas Catania48290382009-07-10 13:53:06 -07001717
nikoa64c8c72009-07-20 15:07:26 -07001718void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
Nicolas Catania48290382009-07-10 13:53:06 -07001719 Mutex::Autolock lock(mLock);
1720 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1721 mMetadataUpdated.add(metadata_type);
1722 }
1723}
1724
Hassan Shojaniacefac142017-02-06 21:02:02 -08001725// Modular DRM
1726status_t MediaPlayerService::Client::prepareDrm(const uint8_t uuid[16],
1727 const Vector<uint8_t>& drmSessionId)
1728{
1729 ALOGV("[%d] prepareDrm", mConnId);
1730 sp<MediaPlayerBase> p = getPlayer();
1731 if (p == 0) return UNKNOWN_ERROR;
1732
1733 status_t ret = p->prepareDrm(uuid, drmSessionId);
1734 ALOGV("prepareDrm ret: %d", ret);
1735
1736 return ret;
1737}
1738
1739status_t MediaPlayerService::Client::releaseDrm()
1740{
1741 ALOGV("[%d] releaseDrm", mConnId);
1742 sp<MediaPlayerBase> p = getPlayer();
1743 if (p == 0) return UNKNOWN_ERROR;
1744
1745 status_t ret = p->releaseDrm();
1746 ALOGV("releaseDrm ret: %d", ret);
1747
1748 return ret;
1749}
1750
jiabin156c6872017-10-06 09:47:15 -07001751status_t MediaPlayerService::Client::setOutputDevice(audio_port_handle_t deviceId)
1752{
1753 ALOGV("[%d] setOutputDevice", mConnId);
1754 {
1755 Mutex::Autolock l(mLock);
1756 if (mAudioOutput.get() != nullptr) {
1757 return mAudioOutput->setOutputDevice(deviceId);
1758 }
1759 }
1760 return NO_INIT;
1761}
1762
Robert Wub7f8edc2024-11-04 19:54:38 +00001763status_t MediaPlayerService::Client::getRoutedDeviceIds(DeviceIdVector& deviceIds)
jiabin156c6872017-10-06 09:47:15 -07001764{
Robert Wub7f8edc2024-11-04 19:54:38 +00001765 ALOGV("[%d] getRoutedDeviceIds", mConnId);
jiabin156c6872017-10-06 09:47:15 -07001766 {
1767 Mutex::Autolock l(mLock);
1768 if (mAudioOutput.get() != nullptr) {
Robert Wub7f8edc2024-11-04 19:54:38 +00001769 return mAudioOutput->getRoutedDeviceIds(deviceIds);
jiabin156c6872017-10-06 09:47:15 -07001770 }
1771 }
1772 return NO_INIT;
1773}
1774
1775status_t MediaPlayerService::Client::enableAudioDeviceCallback(bool enabled)
1776{
1777 ALOGV("[%d] enableAudioDeviceCallback, %d", mConnId, enabled);
1778 {
1779 Mutex::Autolock l(mLock);
1780 if (mAudioOutput.get() != nullptr) {
1781 return mAudioOutput->enableAudioDeviceCallback(enabled);
1782 }
1783 }
1784 return NO_INIT;
1785}
1786
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001787#if CALLBACK_ANTAGONIZER
1788const int Antagonizer::interval = 10000; // 10 msecs
1789
Pawin Vongmasa082e4f72017-12-17 02:31:18 -08001790Antagonizer::Antagonizer(const sp<MediaPlayerBase::Listener> &listener) :
1791 mExit(false), mActive(false), mListener(listener)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001792{
1793 createThread(callbackThread, this);
1794}
1795
1796void Antagonizer::kill()
1797{
1798 Mutex::Autolock _l(mLock);
1799 mActive = false;
1800 mExit = true;
1801 mCondition.wait(mLock);
1802}
1803
1804int Antagonizer::callbackThread(void* user)
1805{
Steve Blockb8a80522011-12-20 16:23:08 +00001806 ALOGD("Antagonizer started");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001807 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1808 while (!p->mExit) {
1809 if (p->mActive) {
Steve Block3856b092011-10-20 11:56:00 +01001810 ALOGV("send event");
Pawin Vongmasa082e4f72017-12-17 02:31:18 -08001811 p->mListener->notify(0, 0, 0, 0);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001812 }
1813 usleep(interval);
1814 }
1815 Mutex::Autolock _l(p->mLock);
1816 p->mCondition.signal();
Steve Blockb8a80522011-12-20 16:23:08 +00001817 ALOGD("Antagonizer stopped");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001818 return 0;
1819}
1820#endif
1821
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001822#undef LOG_TAG
1823#define LOG_TAG "AudioSink"
Svet Ganov33761132021-05-13 22:51:08 +00001824MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId,
1825 const AttributionSourceState& attributionSource, const audio_attributes_t* attr,
1826 const sp<AudioSystem::AudioDeviceCallback>& deviceCallback)
Vlad Popab4f5b012022-08-12 15:53:09 +02001827 : mCachedPlayerIId(PLAYER_PIID_INVALID),
1828 mCallback(NULL),
Andy Hungd1c74342015-07-07 16:54:23 -07001829 mStreamType(AUDIO_STREAM_MUSIC),
Andy Hungd1c74342015-07-07 16:54:23 -07001830 mLeftVolume(1.0),
1831 mRightVolume(1.0),
1832 mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
1833 mSampleRateHz(0),
1834 mMsecsPerFrame(0),
1835 mFrameSize(0),
Eric Laurent1948eb32012-04-13 16:50:19 -07001836 mSessionId(sessionId),
Svet Ganov33761132021-05-13 22:51:08 +00001837 mAttributionSource(attributionSource),
Andy Hungd1c74342015-07-07 16:54:23 -07001838 mSendLevel(0.0),
1839 mAuxEffectId(0),
Andy Hung4ef88d72017-02-21 19:47:53 -08001840 mFlags(AUDIO_OUTPUT_FLAG_NONE),
jiabin156c6872017-10-06 09:47:15 -07001841 mVolumeHandler(new media::VolumeHandler()),
1842 mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
1843 mDeviceCallbackEnabled(false),
Philip P. Moltmannbda45752020-07-17 16:41:18 -07001844 mDeviceCallback(deviceCallback)
Andy Hungd1c74342015-07-07 16:54:23 -07001845{
Steve Block3856b092011-10-20 11:56:00 +01001846 ALOGV("AudioOutput(%d)", sessionId);
Eric Laurent43562692015-07-15 16:49:07 -07001847 if (attr != NULL) {
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001848 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1849 if (mAttributes != NULL) {
1850 memcpy(mAttributes, attr, sizeof(audio_attributes_t));
François Gaffie58d4be52018-11-06 15:30:12 +01001851 mStreamType = AudioSystem::attributesToStreamType(*attr);
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001852 }
1853 } else {
1854 mAttributes = NULL;
Eric Laurent43562692015-07-15 16:49:07 -07001855 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001856 setMinBufferCount();
1857}
1858
1859MediaPlayerService::AudioOutput::~AudioOutput()
1860{
1861 close();
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07001862 free(mAttributes);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001863}
1864
Andy Hungd1c74342015-07-07 16:54:23 -07001865//static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001866void MediaPlayerService::AudioOutput::setMinBufferCount()
1867{
Roman Kiryanova3f34462021-03-10 16:17:24 -08001868 if (property_get_bool("ro.boot.qemu", false)) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001869 mIsOnEmulator = true;
1870 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1871 }
1872}
1873
Andy Hungd1c74342015-07-07 16:54:23 -07001874// static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001875bool MediaPlayerService::AudioOutput::isOnEmulator()
1876{
Andy Hungd1c74342015-07-07 16:54:23 -07001877 setMinBufferCount(); // benign race wrt other threads
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001878 return mIsOnEmulator;
1879}
1880
Andy Hungd1c74342015-07-07 16:54:23 -07001881// static
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001882int MediaPlayerService::AudioOutput::getMinBufferCount()
1883{
Andy Hungd1c74342015-07-07 16:54:23 -07001884 setMinBufferCount(); // benign race wrt other threads
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001885 return mMinBufferCount;
1886}
1887
1888ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1889{
Andy Hungd1c74342015-07-07 16:54:23 -07001890 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001891 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001892 return mTrack->frameCount() * mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001893}
1894
1895ssize_t MediaPlayerService::AudioOutput::frameCount() const
1896{
Andy Hungd1c74342015-07-07 16:54:23 -07001897 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001898 if (mTrack == 0) return NO_INIT;
1899 return mTrack->frameCount();
1900}
1901
1902ssize_t MediaPlayerService::AudioOutput::channelCount() const
1903{
Andy Hungd1c74342015-07-07 16:54:23 -07001904 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001905 if (mTrack == 0) return NO_INIT;
1906 return mTrack->channelCount();
1907}
1908
1909ssize_t MediaPlayerService::AudioOutput::frameSize() const
1910{
Andy Hungd1c74342015-07-07 16:54:23 -07001911 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001912 if (mTrack == 0) return NO_INIT;
Andy Hungd1c74342015-07-07 16:54:23 -07001913 return mFrameSize;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001914}
1915
1916uint32_t MediaPlayerService::AudioOutput::latency () const
1917{
Andy Hungd1c74342015-07-07 16:54:23 -07001918 Mutex::Autolock lock(mLock);
Eric Laurentdb354e52012-03-05 17:27:11 -08001919 if (mTrack == 0) return 0;
1920 return mTrack->latency();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001921}
1922
1923float MediaPlayerService::AudioOutput::msecsPerFrame() const
1924{
Andy Hungd1c74342015-07-07 16:54:23 -07001925 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08001926 return mMsecsPerFrame;
1927}
1928
Marco Nelissen4110c102012-03-29 09:31:28 -07001929status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
Eric Laurent342e9cf2010-01-19 17:37:09 -08001930{
Andy Hungd1c74342015-07-07 16:54:23 -07001931 Mutex::Autolock lock(mLock);
Eric Laurent342e9cf2010-01-19 17:37:09 -08001932 if (mTrack == 0) return NO_INIT;
1933 return mTrack->getPosition(position);
1934}
1935
Lajos Molnar06ad1522014-08-28 07:27:44 -07001936status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1937{
Andy Hungd1c74342015-07-07 16:54:23 -07001938 Mutex::Autolock lock(mLock);
Lajos Molnar06ad1522014-08-28 07:27:44 -07001939 if (mTrack == 0) return NO_INIT;
1940 return mTrack->getTimestamp(ts);
1941}
1942
Wei Jiac4ac8172015-10-21 10:35:48 -07001943// TODO: Remove unnecessary calls to getPlayedOutDurationUs()
1944// as it acquires locks and may query the audio driver.
1945//
1946// Some calls could conceivably retrieve extrapolated data instead of
1947// accessing getTimestamp() or getPosition() every time a data buffer with
1948// a media time is received.
1949//
1950// Calculate duration of played samples if played at normal rate (i.e., 1.0).
1951int64_t MediaPlayerService::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const
1952{
1953 Mutex::Autolock lock(mLock);
1954 if (mTrack == 0 || mSampleRateHz == 0) {
Wei Jia213f4902015-10-22 08:55:25 -07001955 return 0;
Wei Jiac4ac8172015-10-21 10:35:48 -07001956 }
1957
1958 uint32_t numFramesPlayed;
Andy Hung5d313802016-10-10 15:09:39 -07001959 int64_t numFramesPlayedAtUs;
Wei Jiac4ac8172015-10-21 10:35:48 -07001960 AudioTimestamp ts;
Wei Jiac4ac8172015-10-21 10:35:48 -07001961
1962 status_t res = mTrack->getTimestamp(ts);
1963 if (res == OK) { // case 1: mixing audio tracks and offloaded tracks.
1964 numFramesPlayed = ts.mPosition;
Andy Hung5d313802016-10-10 15:09:39 -07001965 numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000;
1966 //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
Wei Jiac4ac8172015-10-21 10:35:48 -07001967 } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track
1968 numFramesPlayed = 0;
Andy Hung5d313802016-10-10 15:09:39 -07001969 numFramesPlayedAtUs = nowUs;
Wei Jiac4ac8172015-10-21 10:35:48 -07001970 //ALOGD("getTimestamp: WOULD_BLOCK %d %lld",
Andy Hung5d313802016-10-10 15:09:39 -07001971 // numFramesPlayed, (long long)numFramesPlayedAtUs);
Wei Jiac4ac8172015-10-21 10:35:48 -07001972 } else { // case 3: transitory at new track or audio fast tracks.
1973 res = mTrack->getPosition(&numFramesPlayed);
1974 CHECK_EQ(res, (status_t)OK);
Andy Hung5d313802016-10-10 15:09:39 -07001975 numFramesPlayedAtUs = nowUs;
1976 numFramesPlayedAtUs += 1000LL * mTrack->latency() / 2; /* XXX */
1977 //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
Wei Jiac4ac8172015-10-21 10:35:48 -07001978 }
1979
1980 // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test
1981 // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours.
1982 int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz)
Andy Hung5d313802016-10-10 15:09:39 -07001983 + nowUs - numFramesPlayedAtUs;
Wei Jiac4ac8172015-10-21 10:35:48 -07001984 if (durationUs < 0) {
1985 // Occurs when numFramesPlayed position is very small and the following:
1986 // (1) In case 1, the time nowUs is computed before getTimestamp() is called and
Andy Hung5d313802016-10-10 15:09:39 -07001987 // numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed.
Wei Jiac4ac8172015-10-21 10:35:48 -07001988 // (2) In case 3, using getPosition and adding mAudioSink->latency() to
Andy Hung5d313802016-10-10 15:09:39 -07001989 // numFramesPlayedAtUs, by a time amount greater than numFramesPlayed.
Wei Jiac4ac8172015-10-21 10:35:48 -07001990 //
1991 // Both of these are transitory conditions.
1992 ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs);
1993 durationUs = 0;
1994 }
1995 ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)",
Andy Hung5d313802016-10-10 15:09:39 -07001996 (long long)durationUs, (long long)nowUs,
1997 numFramesPlayed, (long long)numFramesPlayedAtUs);
Wei Jiac4ac8172015-10-21 10:35:48 -07001998 return durationUs;
1999}
2000
Marco Nelissen4110c102012-03-29 09:31:28 -07002001status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
2002{
Andy Hungd1c74342015-07-07 16:54:23 -07002003 Mutex::Autolock lock(mLock);
Marco Nelissen4110c102012-03-29 09:31:28 -07002004 if (mTrack == 0) return NO_INIT;
Andy Hung2f6e73d2016-04-08 12:12:58 -07002005 ExtendedTimestamp ets;
2006 status_t status = mTrack->getTimestamp(&ets);
2007 if (status == OK || status == WOULD_BLOCK) {
2008 *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT];
2009 }
2010 return status;
Marco Nelissen4110c102012-03-29 09:31:28 -07002011}
2012
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002013status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
2014{
Andy Hungd1c74342015-07-07 16:54:23 -07002015 Mutex::Autolock lock(mLock);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002016 if (mTrack == 0) return NO_INIT;
2017 return mTrack->setParameters(keyValuePairs);
2018}
2019
2020String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
2021{
Andy Hungd1c74342015-07-07 16:54:23 -07002022 Mutex::Autolock lock(mLock);
Tomasz Wasilczyk53ce3af2023-08-14 16:16:55 +00002023 if (mTrack == 0) return String8();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002024 return mTrack->getParameters(keys);
2025}
2026
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07002027void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
Andy Hungd1c74342015-07-07 16:54:23 -07002028 Mutex::Autolock lock(mLock);
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07002029 if (attributes == NULL) {
2030 free(mAttributes);
2031 mAttributes = NULL;
2032 } else {
2033 if (mAttributes == NULL) {
2034 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
2035 }
2036 memcpy(mAttributes, attributes, sizeof(audio_attributes_t));
François Gaffie58d4be52018-11-06 15:30:12 +01002037 mStreamType = AudioSystem::attributesToStreamType(*attributes);
Eric Laurent43562692015-07-15 16:49:07 -07002038 }
2039}
2040
2041void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType)
2042{
Jean-Michel Trivi2650e962015-07-22 18:14:02 -07002043 Mutex::Autolock lock(mLock);
Eric Laurent43562692015-07-15 16:49:07 -07002044 // do not allow direct stream type modification if attributes have been set
2045 if (mAttributes == NULL) {
2046 mStreamType = streamType;
2047 }
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07002048}
2049
Andy Hungd1c74342015-07-07 16:54:23 -07002050void MediaPlayerService::AudioOutput::deleteRecycledTrack_l()
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002051{
Andy Hungd1c74342015-07-07 16:54:23 -07002052 ALOGV("deleteRecycledTrack_l");
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002053 if (mRecycledTrack != 0) {
2054
2055 if (mCallbackData != NULL) {
2056 mCallbackData->setOutput(NULL);
2057 mCallbackData->endTrackSwitch();
2058 }
2059
2060 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
Andy Hunge13f8a62016-03-30 14:20:42 -07002061 int32_t msec = 0;
2062 if (!mRecycledTrack->stopped()) { // check if active
2063 (void)mRecycledTrack->pendingDuration(&msec);
2064 }
2065 mRecycledTrack->stop(); // ensure full data drain
2066 ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec);
2067 if (msec > 0) {
2068 static const int32_t WAIT_LIMIT_MS = 3000;
2069 if (msec > WAIT_LIMIT_MS) {
2070 msec = WAIT_LIMIT_MS;
2071 }
2072 usleep(msec * 1000LL);
2073 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002074 }
2075 // An offloaded track isn't flushed because the STREAM_END is reported
2076 // slightly prematurely to allow time for the gapless track switch
2077 // but this means that if we decide not to recycle the track there
2078 // could be a small amount of residual data still playing. We leave
2079 // AudioFlinger to drain the track.
2080
2081 mRecycledTrack.clear();
Andy Hungd1c74342015-07-07 16:54:23 -07002082 close_l();
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002083 mCallbackData.clear();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002084 }
2085}
2086
Andy Hungd1c74342015-07-07 16:54:23 -07002087void MediaPlayerService::AudioOutput::close_l()
2088{
2089 mTrack.clear();
2090}
2091
Andreas Huber20111aa2009-07-14 16:56:47 -07002092status_t MediaPlayerService::AudioOutput::open(
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002093 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2094 audio_format_t format, int bufferCount,
Andy Hunge42b6482024-10-16 19:16:50 -07002095 AudioCallback cb, const wp<RefBase>& cookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002096 audio_output_flags_t flags,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07002097 const audio_offload_info_t *offloadInfo,
Andy Hung179652e2015-05-31 22:49:46 -07002098 bool doNotReconnect,
2099 uint32_t suggestedFrameCount)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002100{
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002101 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
2102 format, bufferCount, mSessionId, flags);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002103
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002104 // offloading is only supported in callback mode for now.
2105 // offloadInfo must be present if offload flag is set
2106 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
2107 ((cb == NULL) || (offloadInfo == NULL))) {
2108 return BAD_VALUE;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002109 }
2110
Andy Hung179652e2015-05-31 22:49:46 -07002111 // compute frame count for the AudioTrack internal buffer
2112 size_t frameCount;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002113 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2114 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
2115 } else {
Andy Hung179652e2015-05-31 22:49:46 -07002116 // try to estimate the buffer processing fetch size from AudioFlinger.
2117 // framesPerBuffer is approximate and generally correct, except when it's not :-).
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002118 uint32_t afSampleRate;
2119 size_t afFrameCount;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002120 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
2121 return NO_INIT;
2122 }
2123 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
2124 return NO_INIT;
2125 }
Ben Romberger259fb462018-08-07 17:58:53 -07002126 if (afSampleRate == 0) {
2127 return NO_INIT;
2128 }
Andy Hung179652e2015-05-31 22:49:46 -07002129 const size_t framesPerBuffer =
2130 (unsigned long long)sampleRate * afFrameCount / afSampleRate;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002131
Andy Hung179652e2015-05-31 22:49:46 -07002132 if (bufferCount == 0) {
Ben Romberger259fb462018-08-07 17:58:53 -07002133 if (framesPerBuffer == 0) {
2134 return NO_INIT;
2135 }
Andy Hung179652e2015-05-31 22:49:46 -07002136 // use suggestedFrameCount
2137 bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer;
2138 }
2139 // Check argument bufferCount against the mininum buffer count
2140 if (bufferCount != 0 && bufferCount < mMinBufferCount) {
2141 ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount);
2142 bufferCount = mMinBufferCount;
2143 }
2144 // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger
2145 // which will be the minimum size permitted.
2146 frameCount = bufferCount * framesPerBuffer;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002147 }
Andreas Huber20111aa2009-07-14 16:56:47 -07002148
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002149 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
Glenn Kastenab334fd2012-03-14 12:56:06 -07002150 channelMask = audio_channel_out_mask_from_count(channelCount);
Jean-Michel Trivi786618f2012-03-02 14:54:07 -08002151 if (0 == channelMask) {
2152 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
2153 return NO_INIT;
2154 }
2155 }
Eric Laurent1948eb32012-04-13 16:50:19 -07002156
Andy Hungd1c74342015-07-07 16:54:23 -07002157 Mutex::Autolock lock(mLock);
Andy Hung179652e2015-05-31 22:49:46 -07002158 mCallback = cb;
2159 mCallbackCookie = cookie;
2160
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002161 // Check whether we can recycle the track
2162 bool reuse = false;
2163 bool bothOffloaded = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07002164
Glenn Kasten2799d742013-05-30 14:33:29 -07002165 if (mRecycledTrack != 0) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002166 // check whether we are switching between two offloaded tracks
2167 bothOffloaded = (flags & mRecycledTrack->getFlags()
2168 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
Marco Nelissen67295b52012-06-11 14:52:53 -07002169
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002170 // check if the existing track can be reused as-is, or if a new track needs to be created.
2171 reuse = true;
2172
Marco Nelissen67295b52012-06-11 14:52:53 -07002173 if ((mCallbackData == NULL && mCallback != NULL) ||
2174 (mCallbackData != NULL && mCallback == NULL)) {
2175 // recycled track uses callbacks but the caller wants to use writes, or vice versa
2176 ALOGV("can't chain callback and write");
2177 reuse = false;
2178 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002179 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
2180 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
Marco Nelissen67295b52012-06-11 14:52:53 -07002181 mRecycledTrack->getSampleRate(), sampleRate,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002182 mRecycledTrack->channelCount(), channelCount);
Marco Nelissen67295b52012-06-11 14:52:53 -07002183 reuse = false;
2184 } else if (flags != mFlags) {
2185 ALOGV("output flags differ %08x/%08x", flags, mFlags);
2186 reuse = false;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002187 } else if (mRecycledTrack->format() != format) {
2188 reuse = false;
Marco Nelissen67295b52012-06-11 14:52:53 -07002189 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002190 } else {
2191 ALOGV("no track available to recycle");
2192 }
2193
2194 ALOGV_IF(bothOffloaded, "both tracks offloaded");
2195
2196 // If we can't recycle and both tracks are offloaded
2197 // we must close the previous output before opening a new one
2198 if (bothOffloaded && !reuse) {
2199 ALOGV("both offloaded and not recycling");
Andy Hungd1c74342015-07-07 16:54:23 -07002200 deleteRecycledTrack_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002201 }
2202
2203 sp<AudioTrack> t;
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002204 sp<CallbackData> newcbd;
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002205
2206 // We don't attempt to create a new track if we are recycling an
2207 // offloaded track. But, if we are recycling a non-offloaded or we
2208 // are switching where one is offloaded and one isn't then we create
2209 // the new track in advance so that we can read additional stream info
2210
2211 if (!(reuse && bothOffloaded)) {
2212 ALOGV("creating new AudioTrack");
2213
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002214 if (mCallback != nullptr) {
2215 newcbd = sp<CallbackData>::make(wp<AudioOutput>::fromExisting(this));
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002216 t = new AudioTrack(
2217 mStreamType,
2218 sampleRate,
2219 format,
2220 channelMask,
2221 frameCount,
2222 flags,
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002223 newcbd,
2224 0, // notification frames
2225 mSessionId,
2226 AudioTrack::TRANSFER_CALLBACK,
Marco Nelissen462fd2f2013-01-14 14:12:05 -08002227 offloadInfo,
Svet Ganov33761132021-05-13 22:51:08 +00002228 mAttributionSource,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07002229 mAttributes,
jiabin156c6872017-10-06 09:47:15 -07002230 doNotReconnect,
2231 1.0f, // default value for maxRequiredSpeed
Philip P. Moltmannbda45752020-07-17 16:41:18 -07002232 mSelectedDeviceId);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002233 } else {
Andy Hungff874dc2016-04-11 16:49:09 -07002234 // TODO: Due to buffer memory concerns, we use a max target playback speed
2235 // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed),
2236 // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed.
2237 const float targetSpeed =
2238 std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed);
2239 ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed,
2240 "track target speed:%f clamped from playback speed:%f",
2241 targetSpeed, mPlaybackRate.mSpeed);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002242 t = new AudioTrack(
2243 mStreamType,
2244 sampleRate,
2245 format,
2246 channelMask,
2247 frameCount,
2248 flags,
Atneya Nairc1bf42b2021-11-29 19:00:54 -05002249 nullptr, // callback
Marco Nelissen462fd2f2013-01-14 14:12:05 -08002250 0, // notification frames
2251 mSessionId,
2252 AudioTrack::TRANSFER_DEFAULT,
2253 NULL, // offload info
Svet Ganov33761132021-05-13 22:51:08 +00002254 mAttributionSource,
Ronghua Wufaeb0f22015-05-21 12:20:21 -07002255 mAttributes,
Andy Hungff874dc2016-04-11 16:49:09 -07002256 doNotReconnect,
jiabin156c6872017-10-06 09:47:15 -07002257 targetSpeed,
Philip P. Moltmannbda45752020-07-17 16:41:18 -07002258 mSelectedDeviceId);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002259 }
Andy Hunga6b27032020-04-27 10:34:24 -07002260 // Set caller name so it can be logged in destructor.
2261 // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_MEDIA
2262 t->setCallerName("media");
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002263 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
2264 ALOGE("Unable to create audio track");
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002265 // t, newcbd goes out of scope, so reference count drops to zero
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002266 return NO_INIT;
Jean-Michel Trivid9d7fa02014-06-24 08:01:46 -07002267 } else {
2268 // successful AudioTrack initialization implies a legacy stream type was generated
2269 // from the audio attributes
2270 mStreamType = t->streamType();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002271 }
2272 }
2273
2274 if (reuse) {
2275 CHECK(mRecycledTrack != NULL);
2276
2277 if (!bothOffloaded) {
2278 if (mRecycledTrack->frameCount() != t->frameCount()) {
Andy Hung833b4752016-04-04 17:15:48 -07002279 ALOGV("framecount differs: %zu/%zu frames",
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002280 mRecycledTrack->frameCount(), t->frameCount());
2281 reuse = false;
2282 }
Jaideep Sharmab3ce4482020-11-16 11:52:35 +05302283 // If recycled and new tracks are not on the same output,
2284 // don't reuse the recycled one.
2285 if (mRecycledTrack->getOutput() != t->getOutput()) {
2286 ALOGV("output has changed, don't reuse track");
2287 reuse = false;
2288 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002289 }
2290
Marco Nelissen67295b52012-06-11 14:52:53 -07002291 if (reuse) {
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002292 ALOGV("chaining to next output and recycling track");
Andy Hungd1c74342015-07-07 16:54:23 -07002293 close_l();
Marco Nelissen67295b52012-06-11 14:52:53 -07002294 mTrack = mRecycledTrack;
Glenn Kasten2799d742013-05-30 14:33:29 -07002295 mRecycledTrack.clear();
Marco Nelissen67295b52012-06-11 14:52:53 -07002296 if (mCallbackData != NULL) {
2297 mCallbackData->setOutput(this);
2298 }
Wei Jiae0bbac92016-07-18 16:04:53 -07002299 return updateTrack();
Marco Nelissen67295b52012-06-11 14:52:53 -07002300 }
Marco Nelissen67295b52012-06-11 14:52:53 -07002301 }
2302
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002303 // we're not going to reuse the track, unblock and flush it
2304 // this was done earlier if both tracks are offloaded
2305 if (!bothOffloaded) {
Andy Hungd1c74342015-07-07 16:54:23 -07002306 deleteRecycledTrack_l();
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002307 }
2308
2309 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
2310
Marco Nelissen67295b52012-06-11 14:52:53 -07002311 mCallbackData = newcbd;
Steve Block3856b092011-10-20 11:56:00 +01002312 ALOGV("setVolume");
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002313 t->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07002314
Andy Hung39399b62017-04-21 15:07:45 -07002315 // Restore VolumeShapers for the MediaPlayer in case the track was recreated
2316 // due to an output sink error (e.g. offload to non-offload switch).
2317 mVolumeHandler->forall([&t](const VolumeShaper &shaper) -> VolumeShaper::Status {
2318 sp<VolumeShaper::Operation> operationToEnd =
2319 new VolumeShaper::Operation(shaper.mOperation);
2320 // TODO: Ideally we would restore to the exact xOffset position
2321 // as returned by getVolumeShaperState(), but we don't have that
2322 // information when restoring at the client unless we periodically poll
2323 // the server or create shared memory state.
2324 //
2325 // For now, we simply advance to the end of the VolumeShaper effect
2326 // if it has been started.
2327 if (shaper.isStarted()) {
Andy Hungf3702642017-05-05 17:33:32 -07002328 operationToEnd->setNormalizedTime(1.f);
Andy Hung39399b62017-04-21 15:07:45 -07002329 }
2330 return t->applyVolumeShaper(shaper.mConfiguration, operationToEnd);
Andy Hung4ef88d72017-02-21 19:47:53 -08002331 });
Andy Hung4ef88d72017-02-21 19:47:53 -08002332
Vlad Popab4f5b012022-08-12 15:53:09 +02002333 if (mCachedPlayerIId != PLAYER_PIID_INVALID) {
2334 t->setPlayerIId(mCachedPlayerIId);
2335 }
2336
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002337 mSampleRateHz = sampleRate;
Preetam Singh Ranawat2e17eef2015-08-12 12:11:46 -07002338 mFlags = flags;
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002339 mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
Andy Hungd1c74342015-07-07 16:54:23 -07002340 mFrameSize = t->frameSize();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002341 mTrack = t;
Eric Laurent2beeb502010-07-16 07:43:46 -07002342
Wei Jiae0bbac92016-07-18 16:04:53 -07002343 return updateTrack();
2344}
2345
2346status_t MediaPlayerService::AudioOutput::updateTrack() {
2347 if (mTrack == NULL) {
2348 return NO_ERROR;
2349 }
2350
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002351 status_t res = NO_ERROR;
Wei Jia0162d002015-06-09 11:59:33 -07002352 // Note some output devices may give us a direct track even though we don't specify it.
2353 // Example: Line application b/17459982.
Wei Jiae0bbac92016-07-18 16:04:53 -07002354 if ((mTrack->getFlags()
2355 & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
2356 res = mTrack->setPlaybackRate(mPlaybackRate);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002357 if (res == NO_ERROR) {
Wei Jiae0bbac92016-07-18 16:04:53 -07002358 mTrack->setAuxEffectSendLevel(mSendLevel);
2359 res = mTrack->attachAuxEffect(mAuxEffectId);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002360 }
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002361 }
jiabin156c6872017-10-06 09:47:15 -07002362 mTrack->setOutputDevice(mSelectedDeviceId);
2363 if (mDeviceCallbackEnabled) {
2364 mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
2365 }
Wei Jiae0bbac92016-07-18 16:04:53 -07002366 ALOGV("updateTrack() DONE status %d", res);
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002367 return res;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002368}
2369
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002370status_t MediaPlayerService::AudioOutput::start()
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002371{
Steve Block3856b092011-10-20 11:56:00 +01002372 ALOGV("start");
Andy Hungd1c74342015-07-07 16:54:23 -07002373 Mutex::Autolock lock(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08002374 if (mCallbackData != NULL) {
2375 mCallbackData->endTrackSwitch();
2376 }
Glenn Kasten2799d742013-05-30 14:33:29 -07002377 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002378 mTrack->setVolume(mLeftVolume, mRightVolume);
Eric Laurent2beeb502010-07-16 07:43:46 -07002379 mTrack->setAuxEffectSendLevel(mSendLevel);
Andy Hung39399b62017-04-21 15:07:45 -07002380 status_t status = mTrack->start();
2381 if (status == NO_ERROR) {
2382 mVolumeHandler->setStarted();
2383 }
2384 return status;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002385 }
Richard Fitzgeraldd89532e2013-05-14 13:18:21 +01002386 return NO_INIT;
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002387}
2388
Vlad Popab4f5b012022-08-12 15:53:09 +02002389void MediaPlayerService::AudioOutput::setPlayerIId(int32_t playerIId)
2390{
2391 ALOGV("setPlayerIId(%d)", playerIId);
2392 Mutex::Autolock lock(mLock);
2393 mCachedPlayerIId = playerIId;
2394
2395 if (mTrack != nullptr) {
2396 mTrack->setPlayerIId(mCachedPlayerIId);
2397 }
2398}
2399
Marco Nelissen6b74d672012-02-28 16:07:44 -08002400void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
Andy Hungd1c74342015-07-07 16:54:23 -07002401 Mutex::Autolock lock(mLock);
Marco Nelissen6b74d672012-02-28 16:07:44 -08002402 mNextOutput = nextOutput;
2403}
Marco Nelissen7ee8ac92010-01-12 09:23:54 -08002404
Marco Nelissen6b74d672012-02-28 16:07:44 -08002405void MediaPlayerService::AudioOutput::switchToNextOutput() {
2406 ALOGV("switchToNextOutput");
Andy Hungd1c74342015-07-07 16:54:23 -07002407
2408 // Try to acquire the callback lock before moving track (without incurring deadlock).
2409 const unsigned kMaxSwitchTries = 100;
2410 Mutex::Autolock lock(mLock);
2411 for (unsigned tries = 0;;) {
2412 if (mTrack == 0) {
2413 return;
Marco Nelissen6b74d672012-02-28 16:07:44 -08002414 }
Andy Hungd1c74342015-07-07 16:54:23 -07002415 if (mNextOutput != NULL && mNextOutput != this) {
2416 if (mCallbackData != NULL) {
2417 // two alternative approaches
2418#if 1
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002419 sp<CallbackData> callbackData = mCallbackData;
Andy Hungd1c74342015-07-07 16:54:23 -07002420 mLock.unlock();
2421 // proper acquisition sequence
2422 callbackData->lock();
2423 mLock.lock();
2424 // Caution: it is unlikely that someone deleted our callback or changed our target
2425 if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) {
2426 // fatal if we are starved out.
2427 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
2428 "switchToNextOutput() cannot obtain correct lock sequence");
2429 callbackData->unlock();
2430 continue;
2431 }
2432 callbackData->mSwitching = true; // begin track switch
Wei Jiaadee56a2016-08-05 15:40:34 -07002433 callbackData->setOutput(NULL);
Andy Hungd1c74342015-07-07 16:54:23 -07002434#else
2435 // tryBeginTrackSwitch() returns false if the callback has the lock.
2436 if (!mCallbackData->tryBeginTrackSwitch()) {
2437 // fatal if we are starved out.
2438 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
2439 "switchToNextOutput() cannot obtain callback lock");
2440 mLock.unlock();
2441 usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput
2442 mLock.lock();
2443 continue;
2444 }
2445#endif
2446 }
2447
2448 Mutex::Autolock nextLock(mNextOutput->mLock);
2449
2450 // If the next output track is not NULL, then it has been
2451 // opened already for playback.
2452 // This is possible even without the next player being started,
2453 // for example, the next player could be prepared and seeked.
2454 //
2455 // Presuming it isn't advisable to force the track over.
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002456 if (mNextOutput->mTrack == nullptr) {
Andy Hungd1c74342015-07-07 16:54:23 -07002457 ALOGD("Recycling track for gapless playback");
Andy Hungd1c74342015-07-07 16:54:23 -07002458 mNextOutput->mCallbackData = mCallbackData;
2459 mNextOutput->mRecycledTrack = mTrack;
2460 mNextOutput->mSampleRateHz = mSampleRateHz;
2461 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
Andy Hungd1c74342015-07-07 16:54:23 -07002462 mNextOutput->mFlags = mFlags;
2463 mNextOutput->mFrameSize = mFrameSize;
2464 close_l();
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002465 mCallbackData.clear();
Andy Hungd1c74342015-07-07 16:54:23 -07002466 } else {
2467 ALOGW("Ignoring gapless playback because next player has already started");
2468 // remove track in case resource needed for future players.
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002469 if (mCallbackData != nullptr) {
Andy Hungd1c74342015-07-07 16:54:23 -07002470 mCallbackData->endTrackSwitch(); // release lock for callbacks before close.
2471 }
2472 close_l();
2473 }
2474 }
2475 break;
Marco Nelissen6b74d672012-02-28 16:07:44 -08002476 }
2477}
2478
Wei Jia7d3f4df2015-03-03 15:28:00 -08002479ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002480{
Andy Hungd1c74342015-07-07 16:54:23 -07002481 Mutex::Autolock lock(mLock);
Glenn Kastenadad3d72014-02-21 14:51:43 -08002482 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
Andreas Huber20111aa2009-07-14 16:56:47 -07002483
Steve Block3856b092011-10-20 11:56:00 +01002484 //ALOGV("write(%p, %u)", buffer, size);
Glenn Kasten2799d742013-05-30 14:33:29 -07002485 if (mTrack != 0) {
Andy Hung2f6e73d2016-04-08 12:12:58 -07002486 return mTrack->write(buffer, size, blocking);
Marco Nelissen10dbb8e2009-09-20 10:42:13 -07002487 }
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002488 return NO_INIT;
2489}
2490
2491void MediaPlayerService::AudioOutput::stop()
2492{
Steve Block3856b092011-10-20 11:56:00 +01002493 ALOGV("stop");
Andy Hungd1c74342015-07-07 16:54:23 -07002494 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07002495 if (mTrack != 0) mTrack->stop();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002496}
2497
2498void MediaPlayerService::AudioOutput::flush()
2499{
Steve Block3856b092011-10-20 11:56:00 +01002500 ALOGV("flush");
Andy Hungd1c74342015-07-07 16:54:23 -07002501 Mutex::Autolock lock(mLock);
Glenn Kasten2799d742013-05-30 14:33:29 -07002502 if (mTrack != 0) mTrack->flush();
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002503}
2504
2505void MediaPlayerService::AudioOutput::pause()
2506{
Steve Block3856b092011-10-20 11:56:00 +01002507 ALOGV("pause");
Andy Hung959b5b82021-09-24 10:46:20 -07002508 // We use pauseAndWait() instead of pause() to ensure tracks ramp to silence before
2509 // any flush. We choose 40 ms timeout to allow 1 deep buffer mixer period
2510 // to occur. Often waiting is 0 - 20 ms.
2511 using namespace std::chrono_literals;
2512 constexpr auto TIMEOUT_MS = 40ms;
Andy Hungd1c74342015-07-07 16:54:23 -07002513 Mutex::Autolock lock(mLock);
Andy Hung959b5b82021-09-24 10:46:20 -07002514 if (mTrack != 0) mTrack->pauseAndWait(TIMEOUT_MS);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002515}
2516
2517void MediaPlayerService::AudioOutput::close()
2518{
Steve Block3856b092011-10-20 11:56:00 +01002519 ALOGV("close");
Erik Wolsheimer7845a1f2015-10-30 12:07:52 -07002520 sp<AudioTrack> track;
2521 {
2522 Mutex::Autolock lock(mLock);
2523 track = mTrack;
Jaideep Sharma511599f2022-01-06 13:38:57 +05302524 }
2525
2526 // do not hold lock while joining.
2527 if (track) {
2528 track->stopAndJoinCallbacks();
2529 }
2530
2531 {
2532 Mutex::Autolock lock(mLock);
Erik Wolsheimer7845a1f2015-10-30 12:07:52 -07002533 close_l(); // clears mTrack
2534 }
2535 // destruction of the track occurs outside of mutex.
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002536}
2537
2538void MediaPlayerService::AudioOutput::setVolume(float left, float right)
2539{
Steve Block3856b092011-10-20 11:56:00 +01002540 ALOGV("setVolume(%f, %f)", left, right);
Andy Hungd1c74342015-07-07 16:54:23 -07002541 Mutex::Autolock lock(mLock);
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002542 mLeftVolume = left;
2543 mRightVolume = right;
Glenn Kasten2799d742013-05-30 14:33:29 -07002544 if (mTrack != 0) {
The Android Open Source Project89fa4ad2009-03-03 19:31:44 -08002545 mTrack->setVolume(left, right);
2546 }
2547}
2548
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002549status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002550{
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002551 ALOGV("setPlaybackRate(%f %f %d %d)",
2552 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
Andy Hungd1c74342015-07-07 16:54:23 -07002553 Mutex::Autolock lock(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002554 if (mTrack == 0) {
2555 // remember rate so that we can set it when the track is opened
2556 mPlaybackRate = rate;
2557 return OK;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002558 }
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002559 status_t res = mTrack->setPlaybackRate(rate);
2560 if (res != NO_ERROR) {
2561 return res;
2562 }
2563 // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
2564 CHECK_GT(rate.mSpeed, 0.f);
2565 mPlaybackRate = rate;
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002566 if (mSampleRateHz != 0) {
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002567 mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
Jean-Michel Trivi7a8b0ed2012-02-02 09:06:31 -08002568 }
2569 return res;
2570}
2571
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002572status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
2573{
2574 ALOGV("setPlaybackRate");
Andy Hungd1c74342015-07-07 16:54:23 -07002575 Mutex::Autolock lock(mLock);
Lajos Molnar3a474aa2015-04-24 17:10:07 -07002576 if (mTrack == 0) {
2577 return NO_INIT;
2578 }
2579 *rate = mTrack->getPlaybackRate();
2580 return NO_ERROR;
2581}
2582
Eric Laurent2beeb502010-07-16 07:43:46 -07002583status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
2584{
Steve Block3856b092011-10-20 11:56:00 +01002585 ALOGV("setAuxEffectSendLevel(%f)", level);
Andy Hungd1c74342015-07-07 16:54:23 -07002586 Mutex::Autolock lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -07002587 mSendLevel = level;
Glenn Kasten2799d742013-05-30 14:33:29 -07002588 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07002589 return mTrack->setAuxEffectSendLevel(level);
2590 }
2591 return NO_ERROR;
2592}
2593
2594status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
2595{
Steve Block3856b092011-10-20 11:56:00 +01002596 ALOGV("attachAuxEffect(%d)", effectId);
Andy Hungd1c74342015-07-07 16:54:23 -07002597 Mutex::Autolock lock(mLock);
Eric Laurent2beeb502010-07-16 07:43:46 -07002598 mAuxEffectId = effectId;
Glenn Kasten2799d742013-05-30 14:33:29 -07002599 if (mTrack != 0) {
Eric Laurent2beeb502010-07-16 07:43:46 -07002600 return mTrack->attachAuxEffect(effectId);
2601 }
2602 return NO_ERROR;
2603}
2604
jiabin156c6872017-10-06 09:47:15 -07002605status_t MediaPlayerService::AudioOutput::setOutputDevice(audio_port_handle_t deviceId)
2606{
2607 ALOGV("setOutputDevice(%d)", deviceId);
2608 Mutex::Autolock lock(mLock);
2609 mSelectedDeviceId = deviceId;
2610 if (mTrack != 0) {
2611 return mTrack->setOutputDevice(deviceId);
2612 }
2613 return NO_ERROR;
2614}
2615
Robert Wub7f8edc2024-11-04 19:54:38 +00002616status_t MediaPlayerService::AudioOutput::getRoutedDeviceIds(DeviceIdVector& deviceIds)
jiabin156c6872017-10-06 09:47:15 -07002617{
Robert Wub7f8edc2024-11-04 19:54:38 +00002618 ALOGV("getRoutedDeviceIds");
jiabin156c6872017-10-06 09:47:15 -07002619 Mutex::Autolock lock(mLock);
2620 if (mTrack != 0) {
Robert Wub7f8edc2024-11-04 19:54:38 +00002621 mRoutedDeviceIds = mTrack->getRoutedDeviceIds();
jiabin156c6872017-10-06 09:47:15 -07002622 }
Robert Wub7f8edc2024-11-04 19:54:38 +00002623 deviceIds = mRoutedDeviceIds;
jiabin161b64f2017-11-17 09:31:48 -08002624 return NO_ERROR;
jiabin156c6872017-10-06 09:47:15 -07002625}
2626
2627status_t MediaPlayerService::AudioOutput::enableAudioDeviceCallback(bool enabled)
2628{
2629 ALOGV("enableAudioDeviceCallback, %d", enabled);
2630 Mutex::Autolock lock(mLock);
2631 mDeviceCallbackEnabled = enabled;
2632 if (mTrack != 0) {
2633 status_t status;
2634 if (enabled) {
2635 status = mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
2636 } else {
2637 status = mTrack->removeAudioDeviceCallback(mDeviceCallback.promote());
2638 }
2639 return status;
2640 }
2641 return NO_ERROR;
2642}
2643
Andy Hung9fc8b5c2017-01-24 13:36:48 -08002644VolumeShaper::Status MediaPlayerService::AudioOutput::applyVolumeShaper(
2645 const sp<VolumeShaper::Configuration>& configuration,
2646 const sp<VolumeShaper::Operation>& operation)
2647{
2648 Mutex::Autolock lock(mLock);
2649 ALOGV("AudioOutput::applyVolumeShaper");
Andy Hung4ef88d72017-02-21 19:47:53 -08002650
Pawan Wagh8282bb42023-07-05 23:01:17 +00002651 if (configuration == nullptr) {
2652 ALOGE("AudioOutput::applyVolumeShaper Null configuration parameter");
2653 return VolumeShaper::Status(BAD_VALUE);
2654 }
2655
2656 if (operation == nullptr) {
2657 ALOGE("AudioOutput::applyVolumeShaper Null operation parameter");
2658 return VolumeShaper::Status(BAD_VALUE);
2659 }
2660
Andy Hung4ef88d72017-02-21 19:47:53 -08002661 mVolumeHandler->setIdIfNecessary(configuration);
Andy Hung39399b62017-04-21 15:07:45 -07002662
2663 VolumeShaper::Status status;
Andy Hung9fc8b5c2017-01-24 13:36:48 -08002664 if (mTrack != 0) {
Andy Hung39399b62017-04-21 15:07:45 -07002665 status = mTrack->applyVolumeShaper(configuration, operation);
2666 if (status >= 0) {
2667 (void)mVolumeHandler->applyVolumeShaper(configuration, operation);
Andy Hungf3702642017-05-05 17:33:32 -07002668 if (mTrack->isPlaying()) { // match local AudioTrack to properly restore.
2669 mVolumeHandler->setStarted();
2670 }
Andy Hung39399b62017-04-21 15:07:45 -07002671 }
Andy Hung4ef88d72017-02-21 19:47:53 -08002672 } else {
Andy Hungc01eddb2017-07-21 16:42:41 -07002673 // VolumeShapers are not affected when a track moves between players for
2674 // gapless playback (setNextMediaPlayer).
2675 // We forward VolumeShaper operations that do not change configuration
2676 // to the new player so that unducking may occur as expected.
2677 // Unducking is an idempotent operation, same if applied back-to-back.
2678 if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID
2679 && mNextOutput != nullptr) {
2680 ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s",
2681 configuration->toString().c_str(), operation->toString().c_str());
2682 Mutex::Autolock nextLock(mNextOutput->mLock);
2683
2684 // recycled track should be forwarded from this AudioSink by switchToNextOutput
2685 sp<AudioTrack> track = mNextOutput->mRecycledTrack;
2686 if (track != nullptr) {
2687 ALOGD("Forward VolumeShaper operation to recycled track %p", track.get());
2688 (void)track->applyVolumeShaper(configuration, operation);
2689 } else {
2690 // There is a small chance that the unduck occurs after the next
2691 // player has already started, but before it is registered to receive
2692 // the unduck command.
2693 track = mNextOutput->mTrack;
2694 if (track != nullptr) {
2695 ALOGD("Forward VolumeShaper operation to track %p", track.get());
2696 (void)track->applyVolumeShaper(configuration, operation);
2697 }
2698 }
2699 }
Andy Hung39399b62017-04-21 15:07:45 -07002700 status = mVolumeHandler->applyVolumeShaper(configuration, operation);
Andy Hung9fc8b5c2017-01-24 13:36:48 -08002701 }
Andy Hung39399b62017-04-21 15:07:45 -07002702 return status;
Andy Hung9fc8b5c2017-01-24 13:36:48 -08002703}
2704
2705sp<VolumeShaper::State> MediaPlayerService::AudioOutput::getVolumeShaperState(int id)
2706{
2707 Mutex::Autolock lock(mLock);
2708 if (mTrack != 0) {
2709 return mTrack->getVolumeShaperState(id);
Andy Hung4ef88d72017-02-21 19:47:53 -08002710 } else {
2711 return mVolumeHandler->getVolumeShaperState(id);
Andy Hung9fc8b5c2017-01-24 13:36:48 -08002712 }
Andy Hung9fc8b5c2017-01-24 13:36:48 -08002713}
2714
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002715size_t MediaPlayerService::AudioOutput::CallbackData::onMoreData(const AudioTrack::Buffer& buffer) {
2716 ALOGD("data callback");
2717 lock();
2718 sp<AudioOutput> me = getOutput();
2719 if (me == nullptr) {
2720 unlock();
2721 return 0;
2722 }
2723 size_t actualSize = (*me->mCallback)(
Andy Hunge42b6482024-10-16 19:16:50 -07002724 me, buffer.data(), buffer.size(), me->mCallbackCookie,
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002725 CB_EVENT_FILL_BUFFER);
2726
2727 // Log when no data is returned from the callback.
2728 // (1) We may have no data (especially with network streaming sources).
2729 // (2) We may have reached the EOS and the audio track is not stopped yet.
2730 // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
2731 // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill).
2732 //
2733 // This is a benign busy-wait, with the next data request generated 10 ms or more later;
2734 // nevertheless for power reasons, we don't want to see too many of these.
2735
Santiago Seifert578aeb52022-10-24 07:59:12 +00002736 ALOGV_IF(actualSize == 0 && buffer.size() > 0, "callbackwrapper: empty buffer returned");
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002737 unlock();
2738 return actualSize;
2739}
2740
2741void MediaPlayerService::AudioOutput::CallbackData::onStreamEnd() {
2742 lock();
2743 sp<AudioOutput> me = getOutput();
2744 if (me == nullptr) {
2745 unlock();
Marco Nelissen6b74d672012-02-28 16:07:44 -08002746 return;
2747 }
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002748 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
Andy Hunge42b6482024-10-16 19:16:50 -07002749 (*me->mCallback)(me, nullptr /* buffer */, 0 /* size */,
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002750 me->mCallbackCookie, CB_EVENT_STREAM_END);
2751 unlock();
2752}
Andreas Huber20111aa2009-07-14 16:56:47 -07002753
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002754
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002755void MediaPlayerService::AudioOutput::CallbackData::onNewIAudioTrack() {
2756 lock();
2757 sp<AudioOutput> me = getOutput();
2758 if (me == nullptr) {
2759 unlock();
2760 return;
Andreas Huber51c1e0e2011-04-04 11:43:40 -07002761 }
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002762 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
Andy Hunge42b6482024-10-16 19:16:50 -07002763 (*me->mCallback)(me, nullptr /* buffer */, 0 /* size */,
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002764 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
2765 unlock();
2766}
Andreas Huber51c1e0e2011-04-04 11:43:40 -07002767
Atneya Nair4dc66dc2021-11-02 12:59:19 -04002768void MediaPlayerService::AudioOutput::CallbackData::onUnderrun() {
2769 // This occurs when there is no data available, typically
2770 // when there is a failure to supply data to the AudioTrack. It can also
2771 // occur in non-offloaded mode when the audio device comes out of standby.
2772 //
2773 // If an AudioTrack underruns it outputs silence. Since this happens suddenly
2774 // it may sound like an audible pop or glitch.
2775 //
2776 // The underrun event is sent once per track underrun; the condition is reset
2777 // when more data is sent to the AudioTrack.
2778 ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)");
2779
Andreas Huber20111aa2009-07-14 16:56:47 -07002780}
2781
Glenn Kastend848eb42016-03-08 13:42:11 -08002782audio_session_t MediaPlayerService::AudioOutput::getSessionId() const
Eric Laurent8c563ed2010-10-07 18:23:03 -07002783{
Andy Hungd1c74342015-07-07 16:54:23 -07002784 Mutex::Autolock lock(mLock);
Eric Laurent8c563ed2010-10-07 18:23:03 -07002785 return mSessionId;
2786}
2787
Eric Laurent6f59db12013-07-26 17:16:50 -07002788uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
2789{
Andy Hungd1c74342015-07-07 16:54:23 -07002790 Mutex::Autolock lock(mLock);
Eric Laurent6f59db12013-07-26 17:16:50 -07002791 if (mTrack == 0) return 0;
2792 return mTrack->getSampleRate();
2793}
2794
Andy Hungf2c87b32016-04-07 19:49:29 -07002795int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const
2796{
2797 Mutex::Autolock lock(mLock);
2798 if (mTrack == 0) {
2799 return 0;
2800 }
2801 int64_t duration;
2802 if (mTrack->getBufferDurationInUs(&duration) != OK) {
2803 return 0;
2804 }
2805 return duration;
2806}
2807
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002808////////////////////////////////////////////////////////////////////////////////
2809
2810struct CallbackThread : public Thread {
2811 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2812 MediaPlayerBase::AudioSink::AudioCallback cb,
Andy Hunge42b6482024-10-16 19:16:50 -07002813 const wp<RefBase>& cookie);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002814
2815protected:
2816 virtual ~CallbackThread();
2817
2818 virtual bool threadLoop();
2819
2820private:
2821 wp<MediaPlayerBase::AudioSink> mSink;
2822 MediaPlayerBase::AudioSink::AudioCallback mCallback;
Andy Hunge42b6482024-10-16 19:16:50 -07002823 wp<RefBase> mCookie;
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002824 void *mBuffer;
2825 size_t mBufferSize;
2826
2827 CallbackThread(const CallbackThread &);
2828 CallbackThread &operator=(const CallbackThread &);
2829};
2830
2831CallbackThread::CallbackThread(
2832 const wp<MediaPlayerBase::AudioSink> &sink,
2833 MediaPlayerBase::AudioSink::AudioCallback cb,
Andy Hunge42b6482024-10-16 19:16:50 -07002834 const wp<RefBase>& cookie)
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002835 : mSink(sink),
2836 mCallback(cb),
2837 mCookie(cookie),
2838 mBuffer(NULL),
2839 mBufferSize(0) {
2840}
2841
2842CallbackThread::~CallbackThread() {
2843 if (mBuffer) {
2844 free(mBuffer);
2845 mBuffer = NULL;
2846 }
2847}
2848
2849bool CallbackThread::threadLoop() {
2850 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2851 if (sink == NULL) {
2852 return false;
2853 }
2854
2855 if (mBuffer == NULL) {
2856 mBufferSize = sink->bufferSize();
2857 mBuffer = malloc(mBufferSize);
2858 }
2859
2860 size_t actualSize =
Andy Hunge42b6482024-10-16 19:16:50 -07002861 (*mCallback)(sink, mBuffer, mBufferSize, mCookie,
Richard Fitzgeraldad3af332013-03-25 16:54:37 +00002862 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002863
2864 if (actualSize > 0) {
2865 sink->write(mBuffer, actualSize);
Andy Hunga31335a2014-08-20 17:37:59 -07002866 // Could return false on sink->write() error or short count.
2867 // Not necessarily appropriate but would work for AudioCache behavior.
Andreas Huber7d5b8a72010-02-09 16:59:18 -08002868 }
2869
2870 return true;
2871}
2872
2873////////////////////////////////////////////////////////////////////////////////
2874
Chong Zhange5b9b692017-05-11 11:44:56 -07002875void MediaPlayerService::addBatteryData(uint32_t params) {
2876 mBatteryTracker.addBatteryData(params);
2877}
2878
2879status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2880 return mBatteryTracker.pullBatteryData(reply);
2881}
2882
2883MediaPlayerService::BatteryTracker::BatteryTracker() {
2884 mBatteryAudio.refCount = 0;
2885 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2886 mBatteryAudio.deviceOn[i] = 0;
2887 mBatteryAudio.lastTime[i] = 0;
2888 mBatteryAudio.totalTime[i] = 0;
2889 }
2890 // speaker is on by default
2891 mBatteryAudio.deviceOn[SPEAKER] = 1;
2892
2893 // reset battery stats
2894 // if the mediaserver has crashed, battery stats could be left
2895 // in bad state, reset the state upon service start.
2896 BatteryNotifier::getInstance().noteResetVideo();
2897}
2898
2899void MediaPlayerService::BatteryTracker::addBatteryData(uint32_t params)
Gloria Wang7cf180c2011-02-19 18:37:57 -08002900{
2901 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08002902
2903 int32_t time = systemTime() / 1000000L;
2904
2905 // change audio output devices. This notification comes from AudioFlinger
2906 if ((params & kBatteryDataSpeakerOn)
2907 || (params & kBatteryDataOtherAudioDeviceOn)) {
2908
2909 int deviceOn[NUM_AUDIO_DEVICES];
2910 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2911 deviceOn[i] = 0;
2912 }
2913
2914 if ((params & kBatteryDataSpeakerOn)
2915 && (params & kBatteryDataOtherAudioDeviceOn)) {
2916 deviceOn[SPEAKER_AND_OTHER] = 1;
2917 } else if (params & kBatteryDataSpeakerOn) {
2918 deviceOn[SPEAKER] = 1;
2919 } else {
2920 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2921 }
2922
2923 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2924 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2925
2926 if (mBatteryAudio.refCount > 0) { // if playing audio
2927 if (!deviceOn[i]) {
2928 mBatteryAudio.lastTime[i] += time;
2929 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2930 mBatteryAudio.lastTime[i] = 0;
2931 } else {
2932 mBatteryAudio.lastTime[i] = 0 - time;
2933 }
2934 }
2935
2936 mBatteryAudio.deviceOn[i] = deviceOn[i];
2937 }
2938 }
2939 return;
2940 }
2941
Marco Nelissenb7848f12014-12-04 08:57:56 -08002942 // an audio stream is started
Gloria Wang9ee159b2011-02-24 14:51:45 -08002943 if (params & kBatteryDataAudioFlingerStart) {
2944 // record the start time only if currently no other audio
2945 // is being played
2946 if (mBatteryAudio.refCount == 0) {
2947 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2948 if (mBatteryAudio.deviceOn[i]) {
2949 mBatteryAudio.lastTime[i] -= time;
2950 }
2951 }
2952 }
2953
2954 mBatteryAudio.refCount ++;
2955 return;
2956
2957 } else if (params & kBatteryDataAudioFlingerStop) {
2958 if (mBatteryAudio.refCount <= 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00002959 ALOGW("Battery track warning: refCount is <= 0");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002960 return;
2961 }
2962
2963 // record the stop time only if currently this is the only
2964 // audio being played
2965 if (mBatteryAudio.refCount == 1) {
2966 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2967 if (mBatteryAudio.deviceOn[i]) {
2968 mBatteryAudio.lastTime[i] += time;
2969 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2970 mBatteryAudio.lastTime[i] = 0;
2971 }
2972 }
2973 }
2974
2975 mBatteryAudio.refCount --;
2976 return;
2977 }
2978
Andy Hung1afd0982016-11-08 16:13:44 -08002979 uid_t uid = IPCThreadState::self()->getCallingUid();
Gloria Wang7cf180c2011-02-19 18:37:57 -08002980 if (uid == AID_MEDIA) {
2981 return;
2982 }
2983 int index = mBatteryData.indexOfKey(uid);
Gloria Wang7cf180c2011-02-19 18:37:57 -08002984
2985 if (index < 0) { // create a new entry for this UID
2986 BatteryUsageInfo info;
2987 info.audioTotalTime = 0;
2988 info.videoTotalTime = 0;
2989 info.audioLastTime = 0;
2990 info.videoLastTime = 0;
2991 info.refCount = 0;
2992
Gloria Wang9ee159b2011-02-24 14:51:45 -08002993 if (mBatteryData.add(uid, info) == NO_MEMORY) {
Steve Block29357bc2012-01-06 19:20:56 +00002994 ALOGE("Battery track error: no memory for new app");
Gloria Wang9ee159b2011-02-24 14:51:45 -08002995 return;
2996 }
Gloria Wang7cf180c2011-02-19 18:37:57 -08002997 }
2998
2999 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
3000
3001 if (params & kBatteryDataCodecStarted) {
3002 if (params & kBatteryDataTrackAudio) {
3003 info.audioLastTime -= time;
3004 info.refCount ++;
3005 }
3006 if (params & kBatteryDataTrackVideo) {
3007 info.videoLastTime -= time;
3008 info.refCount ++;
3009 }
3010 } else {
3011 if (info.refCount == 0) {
Steve Block5ff1dd52012-01-05 23:22:43 +00003012 ALOGW("Battery track warning: refCount is already 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08003013 return;
3014 } else if (info.refCount < 0) {
Steve Block29357bc2012-01-06 19:20:56 +00003015 ALOGE("Battery track error: refCount < 0");
Gloria Wang7cf180c2011-02-19 18:37:57 -08003016 mBatteryData.removeItem(uid);
3017 return;
3018 }
3019
3020 if (params & kBatteryDataTrackAudio) {
3021 info.audioLastTime += time;
3022 info.refCount --;
3023 }
3024 if (params & kBatteryDataTrackVideo) {
3025 info.videoLastTime += time;
3026 info.refCount --;
3027 }
3028
3029 // no stream is being played by this UID
3030 if (info.refCount == 0) {
3031 info.audioTotalTime += info.audioLastTime;
3032 info.audioLastTime = 0;
3033 info.videoTotalTime += info.videoLastTime;
3034 info.videoLastTime = 0;
3035 }
3036 }
3037}
3038
Chong Zhange5b9b692017-05-11 11:44:56 -07003039status_t MediaPlayerService::BatteryTracker::pullBatteryData(Parcel* reply) {
Gloria Wang7cf180c2011-02-19 18:37:57 -08003040 Mutex::Autolock lock(mLock);
Gloria Wang9ee159b2011-02-24 14:51:45 -08003041
3042 // audio output devices usage
3043 int32_t time = systemTime() / 1000000L; //in ms
3044 int32_t totalTime;
3045
3046 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
3047 totalTime = mBatteryAudio.totalTime[i];
3048
3049 if (mBatteryAudio.deviceOn[i]
3050 && (mBatteryAudio.lastTime[i] != 0)) {
3051 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
3052 totalTime += tmpTime;
3053 }
3054
3055 reply->writeInt32(totalTime);
3056 // reset the total time
3057 mBatteryAudio.totalTime[i] = 0;
3058 }
3059
3060 // codec usage
Gloria Wang7cf180c2011-02-19 18:37:57 -08003061 BatteryUsageInfo info;
3062 int size = mBatteryData.size();
3063
3064 reply->writeInt32(size);
3065 int i = 0;
3066
3067 while (i < size) {
3068 info = mBatteryData.valueAt(i);
3069
3070 reply->writeInt32(mBatteryData.keyAt(i)); //UID
3071 reply->writeInt32(info.audioTotalTime);
3072 reply->writeInt32(info.videoTotalTime);
3073
3074 info.audioTotalTime = 0;
3075 info.videoTotalTime = 0;
3076
3077 // remove the UID entry where no stream is being played
3078 if (info.refCount <= 0) {
3079 mBatteryData.removeItemsAt(i);
3080 size --;
3081 i --;
3082 }
3083 i++;
3084 }
3085 return NO_ERROR;
3086}
Pawan Wagh074e53c2023-06-20 22:06:43 +00003087
3088#ifdef FUZZ_MODE_MEDIA_PLAYER_SERVICE
3089sp<MediaPlayerService> MediaPlayerService::createForFuzzTesting() {
3090 return sp<MediaPlayerService>::make();
3091}
3092#endif // FUZZ_MODE_MEDIA_PLAYER_SERVICE
3093
nikoa64c8c72009-07-20 15:07:26 -07003094} // namespace android