1 /*
2 **
3 ** Copyright 2008, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 // Proxy for media player implementations
19
20 //#define LOG_NDEBUG 0
21 #define LOG_TAG "MediaPlayerService"
22 #include <utils/Log.h>
23
24 #include <chrono>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <dirent.h>
29 #include <unistd.h>
30
31 #include <string.h>
32
33 #include <cutils/atomic.h>
34 #include <cutils/properties.h> // for property_get
35
36 #include <utils/misc.h>
37
38 #include <android/hardware/media/omx/1.0/IOmx.h>
39 #include <android/hardware/media/c2/1.0/IComponentStore.h>
40 #include <binder/IPCThreadState.h>
41 #include <binder/IServiceManager.h>
42 #include <binder/MemoryHeapBase.h>
43 #include <binder/MemoryBase.h>
44 #include <gui/Surface.h>
45 #include <utils/Errors.h> // for status_t
46 #include <utils/String8.h>
47 #include <utils/SystemClock.h>
48 #include <utils/Timers.h>
49 #include <utils/Vector.h>
50
51 #include <codec2/hidl/client.h>
52 #include <datasource/HTTPBase.h>
53 #include <media/AidlConversion.h>
54 #include <media/IMediaHTTPService.h>
55 #include <media/IRemoteDisplay.h>
56 #include <media/IRemoteDisplayClient.h>
57 #include <media/MediaPlayerInterface.h>
58 #include <media/mediarecorder.h>
59 #include <media/MediaMetadataRetrieverInterface.h>
60 #include <media/Metadata.h>
61 #include <media/AudioTrack.h>
62 #include <media/stagefright/InterfaceUtils.h>
63 #include <media/stagefright/MediaCodecConstants.h>
64 #include <media/stagefright/MediaCodecList.h>
65 #include <media/stagefright/MediaErrors.h>
66 #include <media/stagefright/Utils.h>
67 #include <media/stagefright/FoundationUtils.h>
68 #include <media/stagefright/foundation/ADebug.h>
69 #include <media/stagefright/foundation/ALooperRoster.h>
70 #include <media/stagefright/SurfaceUtils.h>
71 #include <mediautils/BatteryNotifier.h>
72 #include <mediautils/MemoryLeakTrackUtil.h>
73 #include <memunreachable/memunreachable.h>
74 #include <system/audio.h>
75
76 #include <private/android_filesystem_config.h>
77
78 #include "ActivityManager.h"
79 #include "MediaRecorderClient.h"
80 #include "MediaPlayerService.h"
81 #include "MetadataRetrieverClient.h"
82 #include "MediaPlayerFactory.h"
83
84 #include "TestPlayerStub.h"
85 #include <nuplayer/NuPlayerDriver.h>
86
87
88 static const int kDumpLockRetries = 50;
89 static const int kDumpLockSleepUs = 20000;
90
91 namespace {
92 using android::media::Metadata;
93 using android::status_t;
94 using android::OK;
95 using android::BAD_VALUE;
96 using android::NOT_ENOUGH_DATA;
97 using android::Parcel;
98 using android::media::VolumeShaper;
99 using android::content::AttributionSourceState;
100
101 // Max number of entries in the filter.
102 const int kMaxFilterSize = 64; // I pulled that out of thin air.
103
104 const float kMaxRequiredSpeed = 8.0f; // for PCM tracks allow up to 8x speedup.
105
106 // FIXME: Move all the metadata related function in the Metadata.cpp
107
108
109 // Unmarshall a filter from a Parcel.
110 // Filter format in a parcel:
111 //
112 // 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
113 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 // | number of entries (n) |
115 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 // | metadata type 1 |
117 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 // | metadata type 2 |
119 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 // ....
121 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 // | metadata type n |
123 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124 //
125 // @param p Parcel that should start with a filter.
126 // @param[out] filter On exit contains the list of metadata type to be
127 // filtered.
128 // @param[out] status On exit contains the status code to be returned.
129 // @return true if the parcel starts with a valid filter.
unmarshallFilter(const Parcel & p,Metadata::Filter * filter,status_t * status)130 bool unmarshallFilter(const Parcel& p,
131 Metadata::Filter *filter,
132 status_t *status)
133 {
134 int32_t val;
135 if (p.readInt32(&val) != OK)
136 {
137 ALOGE("Failed to read filter's length");
138 *status = NOT_ENOUGH_DATA;
139 return false;
140 }
141
142 if( val > kMaxFilterSize || val < 0)
143 {
144 ALOGE("Invalid filter len %d", val);
145 *status = BAD_VALUE;
146 return false;
147 }
148
149 const size_t num = val;
150
151 filter->clear();
152 filter->setCapacity(num);
153
154 size_t size = num * sizeof(Metadata::Type);
155
156
157 if (p.dataAvail() < size)
158 {
159 ALOGE("Filter too short expected %zu but got %zu", size, p.dataAvail());
160 *status = NOT_ENOUGH_DATA;
161 return false;
162 }
163
164 const Metadata::Type *data =
165 static_cast<const Metadata::Type*>(p.readInplace(size));
166
167 if (NULL == data)
168 {
169 ALOGE("Filter had no data");
170 *status = BAD_VALUE;
171 return false;
172 }
173
174 // TODO: The stl impl of vector would be more efficient here
175 // because it degenerates into a memcpy on pod types. Try to
176 // replace later or use stl::set.
177 for (size_t i = 0; i < num; ++i)
178 {
179 filter->add(*data);
180 ++data;
181 }
182 *status = OK;
183 return true;
184 }
185
186 // @param filter Of metadata type.
187 // @param val To be searched.
188 // @return true if a match was found.
findMetadata(const Metadata::Filter & filter,const int32_t val)189 bool findMetadata(const Metadata::Filter& filter, const int32_t val)
190 {
191 // Deal with empty and ANY right away
192 if (filter.isEmpty()) return false;
193 if (filter[0] == Metadata::kAny) return true;
194
195 return filter.indexOf(val) >= 0;
196 }
197
198 } // anonymous namespace
199
200
201 namespace {
202 using android::Parcel;
203 using android::String16;
204
205 // marshalling tag indicating flattened utf16 tags
206 // keep in sync with frameworks/base/media/java/android/media/AudioAttributes.java
207 const int32_t kAudioAttributesMarshallTagFlattenTags = 1;
208
209 // Audio attributes format in a parcel:
210 //
211 // 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
212 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
213 // | usage |
214 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
215 // | content_type |
216 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
217 // | source |
218 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
219 // | flags |
220 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
221 // | kAudioAttributesMarshallTagFlattenTags | // ignore tags if not found
222 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
223 // | flattened tags in UTF16 |
224 // | ... |
225 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
226 //
227 // @param p Parcel that contains audio attributes.
228 // @param[out] attributes On exit points to an initialized audio_attributes_t structure
229 // @param[out] status On exit contains the status code to be returned.
unmarshallAudioAttributes(const Parcel & parcel,audio_attributes_t * attributes)230 void unmarshallAudioAttributes(const Parcel& parcel, audio_attributes_t *attributes)
231 {
232 attributes->usage = (audio_usage_t) parcel.readInt32();
233 attributes->content_type = (audio_content_type_t) parcel.readInt32();
234 attributes->source = (audio_source_t) parcel.readInt32();
235 attributes->flags = (audio_flags_mask_t) parcel.readInt32();
236 const bool hasFlattenedTag = (parcel.readInt32() == kAudioAttributesMarshallTagFlattenTags);
237 if (hasFlattenedTag) {
238 // the tags are UTF16, convert to UTF8
239 String16 tags = parcel.readString16();
240 ssize_t realTagSize = utf16_to_utf8_length(tags.c_str(), tags.size());
241 if (realTagSize <= 0) {
242 strcpy(attributes->tags, "");
243 } else {
244 // copy the flattened string into the attributes as the destination for the conversion:
245 // copying array size -1, array for tags was calloc'd, no need to NULL-terminate it
246 size_t tagSize = realTagSize > AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 ?
247 AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1 : realTagSize;
248 utf16_to_utf8(tags.c_str(), tagSize, attributes->tags,
249 sizeof(attributes->tags) / sizeof(attributes->tags[0]));
250 }
251 } else {
252 ALOGE("unmarshallAudioAttributes() received unflattened tags, ignoring tag values");
253 strcpy(attributes->tags, "");
254 }
255 }
256 } // anonymous namespace
257
258
259 namespace android {
260
261 extern ALooperRoster gLooperRoster;
262
263
checkPermission(const char * permissionString)264 static bool checkPermission(const char* permissionString) {
265 if (getpid() == IPCThreadState::self()->getCallingPid()) return true;
266 bool ok = checkCallingPermission(String16(permissionString));
267 if (!ok) ALOGE("Request requires %s", permissionString);
268 return ok;
269 }
270
dumpCodecDetails(int fd,const sp<IMediaCodecList> & codecList,bool queryDecoders)271 static void dumpCodecDetails(int fd, const sp<IMediaCodecList> &codecList, bool queryDecoders) {
272 const size_t SIZE = 256;
273 char buffer[SIZE];
274 String8 result;
275
276 const char *codecType = queryDecoders? "Decoder" : "Encoder";
277 snprintf(buffer, SIZE - 1, "\n%s infos by media types:\n"
278 "=============================\n", codecType);
279 result.append(buffer);
280
281 size_t numCodecs = codecList->countCodecs();
282
283 // gather all media types supported by codec class, and link to codecs that support them
284 KeyedVector<AString, Vector<sp<MediaCodecInfo>>> allMediaTypes;
285 for (size_t codec_ix = 0; codec_ix < numCodecs; ++codec_ix) {
286 sp<MediaCodecInfo> info = codecList->getCodecInfo(codec_ix);
287 if (info->isEncoder() == !queryDecoders) {
288 Vector<AString> supportedMediaTypes;
289 info->getSupportedMediaTypes(&supportedMediaTypes);
290 if (!supportedMediaTypes.size()) {
291 snprintf(buffer, SIZE - 1, "warning: %s does not support any media types\n",
292 info->getCodecName());
293 result.append(buffer);
294 } else {
295 for (const AString &mediaType : supportedMediaTypes) {
296 if (allMediaTypes.indexOfKey(mediaType) < 0) {
297 allMediaTypes.add(mediaType, Vector<sp<MediaCodecInfo>>());
298 }
299 allMediaTypes.editValueFor(mediaType).add(info);
300 }
301 }
302 }
303 }
304
305 KeyedVector<AString, bool> visitedCodecs;
306 for (size_t type_ix = 0; type_ix < allMediaTypes.size(); ++type_ix) {
307 const AString &mediaType = allMediaTypes.keyAt(type_ix);
308 snprintf(buffer, SIZE - 1, "\nMedia type '%s':\n", mediaType.c_str());
309 result.append(buffer);
310
311 for (const sp<MediaCodecInfo> &info : allMediaTypes.valueAt(type_ix)) {
312 sp<MediaCodecInfo::Capabilities> caps = info->getCapabilitiesFor(mediaType.c_str());
313 if (caps == NULL) {
314 snprintf(buffer, SIZE - 1, "warning: %s does not have capabilities for type %s\n",
315 info->getCodecName(), mediaType.c_str());
316 result.append(buffer);
317 continue;
318 }
319 snprintf(buffer, SIZE - 1, " %s \"%s\" supports\n",
320 codecType, info->getCodecName());
321 result.append(buffer);
322
323 auto printList = [&](const char *type, const Vector<AString> &values){
324 snprintf(buffer, SIZE - 1, " %s: [", type);
325 result.append(buffer);
326 for (size_t j = 0; j < values.size(); ++j) {
327 snprintf(buffer, SIZE - 1, "\n %s%s", values[j].c_str(),
328 j == values.size() - 1 ? " " : ",");
329 result.append(buffer);
330 }
331 result.append("]\n");
332 };
333
334 if (visitedCodecs.indexOfKey(info->getCodecName()) < 0) {
335 visitedCodecs.add(info->getCodecName(), true);
336 {
337 Vector<AString> aliases;
338 info->getAliases(&aliases);
339 // quote alias
340 for (AString &alias : aliases) {
341 alias.insert("\"", 1, 0);
342 alias.append('"');
343 }
344 printList("aliases", aliases);
345 }
346 {
347 uint32_t attrs = info->getAttributes();
348 Vector<AString> list;
349 list.add(AStringPrintf("encoder: %d",
350 !!(attrs & MediaCodecInfo::kFlagIsEncoder)));
351 list.add(AStringPrintf("vendor: %d",
352 !!(attrs & MediaCodecInfo::kFlagIsVendor)));
353 list.add(AStringPrintf("software-only: %d",
354 !!(attrs & MediaCodecInfo::kFlagIsSoftwareOnly)));
355 list.add(AStringPrintf("hw-accelerated: %d",
356 !!(attrs & MediaCodecInfo::kFlagIsHardwareAccelerated)));
357 printList(AStringPrintf("attributes: %#x", attrs).c_str(), list);
358 }
359
360 snprintf(buffer, SIZE - 1, " owner: \"%s\"\n", info->getOwnerName());
361 result.append(buffer);
362 snprintf(buffer, SIZE - 1, " hal name: \"%s\"\n", info->getHalName());
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, hal name, 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)
393 ? asString_AV1Profile(pl.mProfile) :
394 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_DOLBY_VISION)
395 ? asString_DolbyVisionProfile(pl.mProfile) :
396 mediaType.equalsIgnoreCase(MIMETYPE_AUDIO_AC4)
397 ? asString_AC4Profile(pl.mProfile) : "??";
398 const char *niceLevel =
399 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_MPEG2)
400 ? asString_MPEG2Level(pl.mLevel) :
401 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_H263)
402 ? asString_H263Level(pl.mLevel) :
403 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_MPEG4)
404 ? asString_MPEG4Level(pl.mLevel) :
405 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_AVC)
406 ? asString_AVCLevel(pl.mLevel) :
407 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_VP8)
408 ? asString_VP8Level(pl.mLevel) :
409 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_HEVC)
410 ? asString_HEVCTierLevel(pl.mLevel) :
411 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_VP9)
412 ? asString_VP9Level(pl.mLevel) :
413 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_AV1)
414 ? asString_AV1Level(pl.mLevel) :
415 mediaType.equalsIgnoreCase(MIMETYPE_VIDEO_DOLBY_VISION)
416 ? asString_DolbyVisionLevel(pl.mLevel) :
417 mediaType.equalsIgnoreCase(MIMETYPE_AUDIO_AC4)
418 ? asString_AC4Level(pl.mLevel) : "??";
419
420 list.add(AStringPrintf("% 5u/% 5u (%s/%s)",
421 pl.mProfile, pl.mLevel, niceProfile, niceLevel));
422 }
423 printList("profile/levels", list);
424 }
425
426 {
427 Vector<AString> list;
428 Vector<uint32_t> colors;
429 caps->getSupportedColorFormats(&colors);
430 for (uint32_t color : colors) {
431 list.add(AStringPrintf("%#x (%s)", color,
432 asString_ColorFormat((int32_t)color)));
433 }
434 printList("colors", list);
435 }
436
437 result.append(" details: ");
438 result.append(caps->getDetails()->debugString(6).c_str());
439 result.append("\n");
440 }
441 }
442 result.append("\n");
443 ::write(fd, result.c_str(), result.size());
444 }
445
446
447 // TODO: Find real cause of Audio/Video delay in PV framework and remove this workaround
448 /* static */ int MediaPlayerService::AudioOutput::mMinBufferCount = 4;
449 /* static */ bool MediaPlayerService::AudioOutput::mIsOnEmulator = false;
450
instantiate()451 void MediaPlayerService::instantiate() {
452 defaultServiceManager()->addService(
453 String16("media.player"), new MediaPlayerService());
454 }
455
MediaPlayerService()456 MediaPlayerService::MediaPlayerService()
457 {
458 ALOGV("MediaPlayerService created");
459 mNextConnId = 1;
460
461 MediaPlayerFactory::registerBuiltinFactories();
462 }
463
~MediaPlayerService()464 MediaPlayerService::~MediaPlayerService()
465 {
466 ALOGV("MediaPlayerService destroyed");
467 }
468
createMediaRecorder(const AttributionSourceState & attributionSource)469 sp<IMediaRecorder> MediaPlayerService::createMediaRecorder(
470 const AttributionSourceState& attributionSource)
471 {
472 // TODO b/182392769: use attribution source util
473 AttributionSourceState verifiedAttributionSource = attributionSource;
474 verifiedAttributionSource.uid = VALUE_OR_FATAL(
475 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
476 verifiedAttributionSource.pid = VALUE_OR_FATAL(
477 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
478 sp<MediaRecorderClient> recorder =
479 new MediaRecorderClient(this, verifiedAttributionSource);
480 wp<MediaRecorderClient> w = recorder;
481 Mutex::Autolock lock(mLock);
482 mMediaRecorderClients.add(w);
483 ALOGV("Create new media recorder client from pid %s",
484 verifiedAttributionSource.toString().c_str());
485 return recorder;
486 }
487
removeMediaRecorderClient(const wp<MediaRecorderClient> & client)488 void MediaPlayerService::removeMediaRecorderClient(const wp<MediaRecorderClient>& client)
489 {
490 Mutex::Autolock lock(mLock);
491 mMediaRecorderClients.remove(client);
492 ALOGV("Delete media recorder client");
493 }
494
createMetadataRetriever()495 sp<IMediaMetadataRetriever> MediaPlayerService::createMetadataRetriever()
496 {
497 pid_t pid = IPCThreadState::self()->getCallingPid();
498 sp<MetadataRetrieverClient> retriever = new MetadataRetrieverClient(pid);
499 ALOGV("Create new media retriever from pid %d", pid);
500 return retriever;
501 }
502
create(const sp<IMediaPlayerClient> & client,audio_session_t audioSessionId,const AttributionSourceState & attributionSource)503 sp<IMediaPlayer> MediaPlayerService::create(const sp<IMediaPlayerClient>& client,
504 audio_session_t audioSessionId, const AttributionSourceState& attributionSource)
505 {
506 int32_t connId = android_atomic_inc(&mNextConnId);
507 // TODO b/182392769: use attribution source util
508 AttributionSourceState verifiedAttributionSource = attributionSource;
509 verifiedAttributionSource.pid = VALUE_OR_FATAL(
510 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
511 verifiedAttributionSource.uid = VALUE_OR_FATAL(
512 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
513
514 sp<Client> c = new Client(
515 this, verifiedAttributionSource, connId, client, audioSessionId);
516
517 ALOGV("Create new client(%d) from %s, ", connId,
518 verifiedAttributionSource.toString().c_str());
519
520 wp<Client> w = c;
521 {
522 Mutex::Autolock lock(mLock);
523 mClients.add(w);
524 }
525 return c;
526 }
527
getCodecList() const528 sp<IMediaCodecList> MediaPlayerService::getCodecList() const {
529 return MediaCodecList::getLocalInstance();
530 }
531
listenForRemoteDisplay(const String16 &,const sp<IRemoteDisplayClient> &,const String8 &)532 sp<IRemoteDisplay> MediaPlayerService::listenForRemoteDisplay(
533 const String16 &/*opPackageName*/,
534 const sp<IRemoteDisplayClient>& /*client*/,
535 const String8& /*iface*/) {
536 ALOGE("listenForRemoteDisplay is no longer supported!");
537
538 return NULL;
539 }
540
dump(int fd,const Vector<String16> & args) const541 status_t MediaPlayerService::AudioOutput::dump(int fd, const Vector<String16>& args) const
542 {
543 const size_t SIZE = 256;
544 char buffer[SIZE];
545 String8 result;
546
547 result.append(" AudioOutput\n");
548 snprintf(buffer, 255, " stream type(%d), left - right volume(%f, %f)\n",
549 mStreamType, mLeftVolume, mRightVolume);
550 result.append(buffer);
551 snprintf(buffer, 255, " msec per frame(%f), latency (%d)\n",
552 mMsecsPerFrame, (mTrack != 0) ? mTrack->latency() : -1);
553 result.append(buffer);
554 snprintf(buffer, 255, " aux effect id(%d), send level (%f)\n",
555 mAuxEffectId, mSendLevel);
556 result.append(buffer);
557
558 ::write(fd, result.c_str(), result.size());
559 if (mTrack != 0) {
560 mTrack->dump(fd, args);
561 }
562 return NO_ERROR;
563 }
564
dump(int fd,const Vector<String16> & args)565 status_t MediaPlayerService::Client::dump(int fd, const Vector<String16>& args)
566 {
567 const size_t SIZE = 256;
568 char buffer[SIZE];
569 String8 result;
570 result.append(" Client\n");
571 snprintf(buffer, 255, " AttributionSource(%s), connId(%d), status(%d), looping(%s)\n",
572 mAttributionSource.toString().c_str(), mConnId, mStatus, mLoop?"true": "false");
573 result.append(buffer);
574
575 sp<MediaPlayerBase> p;
576 sp<AudioOutput> audioOutput;
577 bool locked = false;
578 for (int i = 0; i < kDumpLockRetries; ++i) {
579 if (mLock.tryLock() == NO_ERROR) {
580 locked = true;
581 break;
582 }
583 usleep(kDumpLockSleepUs);
584 }
585
586 if (locked) {
587 p = mPlayer;
588 audioOutput = mAudioOutput;
589 mLock.unlock();
590 } else {
591 result.append(" lock is taken, no dump from player and audio output\n");
592 }
593 write(fd, result.c_str(), result.size());
594
595 if (p != NULL) {
596 p->dump(fd, args);
597 }
598 if (audioOutput != 0) {
599 audioOutput->dump(fd, args);
600 }
601 write(fd, "\n", 1);
602 return NO_ERROR;
603 }
604
605 /**
606 * The only arguments this understands right now are -c, -von and -voff,
607 * which are parsed by ALooperRoster::dump()
608 */
dump(int fd,const Vector<String16> & args)609 status_t MediaPlayerService::dump(int fd, const Vector<String16>& args)
610 {
611 const size_t SIZE = 256;
612 char buffer[SIZE];
613 String8 result;
614 SortedVector< sp<Client> > clients; //to serialise the mutex unlock & client destruction.
615 SortedVector< sp<MediaRecorderClient> > mediaRecorderClients;
616
617 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
618 snprintf(buffer, SIZE - 1, "Permission Denial: "
619 "can't dump MediaPlayerService from pid=%d, uid=%d\n",
620 IPCThreadState::self()->getCallingPid(),
621 IPCThreadState::self()->getCallingUid());
622 result.append(buffer);
623 } else {
624 {
625 // capture clients under lock
626 Mutex::Autolock lock(mLock);
627 for (int i = 0, n = mClients.size(); i < n; ++i) {
628 sp<Client> c = mClients[i].promote();
629 if (c != nullptr) {
630 clients.add(c);
631 }
632 }
633
634 for (int i = 0, n = mMediaRecorderClients.size(); i < n; ++i) {
635 sp<MediaRecorderClient> c = mMediaRecorderClients[i].promote();
636 if (c != nullptr) {
637 mediaRecorderClients.add(c);
638 }
639 }
640 }
641
642 // dump clients outside of lock
643 for (const sp<Client> &c : clients) {
644 c->dump(fd, args);
645 }
646 if (mediaRecorderClients.size() == 0) {
647 result.append(" No media recorder client\n\n");
648 } else {
649 for (const sp<MediaRecorderClient> &c : mediaRecorderClients) {
650 snprintf(buffer, 255, " MediaRecorderClient pid(%d)\n",
651 c->mAttributionSource.pid);
652 result.append(buffer);
653 write(fd, result.c_str(), result.size());
654 result = "\n";
655 c->dump(fd, args);
656
657 }
658 }
659
660 result.append(" Files opened and/or mapped:\n");
661 snprintf(buffer, SIZE - 1, "/proc/%d/maps", getpid());
662 FILE *f = fopen(buffer, "r");
663 if (f) {
664 while (!feof(f)) {
665 fgets(buffer, SIZE - 1, f);
666 if (strstr(buffer, " /storage/") ||
667 strstr(buffer, " /system/sounds/") ||
668 strstr(buffer, " /data/") ||
669 strstr(buffer, " /system/media/")) {
670 result.append(" ");
671 result.append(buffer);
672 }
673 }
674 fclose(f);
675 } else {
676 result.append("couldn't open ");
677 result.append(buffer);
678 result.append("\n");
679 }
680
681 snprintf(buffer, SIZE - 1, "/proc/%d/fd", getpid());
682 DIR *d = opendir(buffer);
683 if (d) {
684 struct dirent *ent;
685 while((ent = readdir(d)) != NULL) {
686 if (strcmp(ent->d_name,".") && strcmp(ent->d_name,"..")) {
687 snprintf(buffer, SIZE - 1, "/proc/%d/fd/%s", getpid(), ent->d_name);
688 struct stat s;
689 if (lstat(buffer, &s) == 0) {
690 if ((s.st_mode & S_IFMT) == S_IFLNK) {
691 char linkto[256];
692 int len = readlink(buffer, linkto, sizeof(linkto));
693 if(len > 0) {
694 if(len > 255) {
695 linkto[252] = '.';
696 linkto[253] = '.';
697 linkto[254] = '.';
698 linkto[255] = 0;
699 } else {
700 linkto[len] = 0;
701 }
702 if (strstr(linkto, "/storage/") == linkto ||
703 strstr(linkto, "/system/sounds/") == linkto ||
704 strstr(linkto, "/data/") == linkto ||
705 strstr(linkto, "/system/media/") == linkto) {
706 result.append(" ");
707 result.append(buffer);
708 result.append(" -> ");
709 result.append(linkto);
710 result.append("\n");
711 }
712 }
713 } else {
714 result.append(" unexpected type for ");
715 result.append(buffer);
716 result.append("\n");
717 }
718 }
719 }
720 }
721 closedir(d);
722 } else {
723 result.append("couldn't open ");
724 result.append(buffer);
725 result.append("\n");
726 }
727
728 gLooperRoster.dump(fd, args);
729
730 sp<IMediaCodecList> codecList = getCodecList();
731 dumpCodecDetails(fd, codecList, true /* decoders */);
732 dumpCodecDetails(fd, codecList, false /* !decoders */);
733
734 bool dumpMem = false;
735 bool unreachableMemory = false;
736 for (size_t i = 0; i < args.size(); i++) {
737 if (args[i] == String16("-m")) {
738 dumpMem = true;
739 } else if (args[i] == String16("--unreachable")) {
740 unreachableMemory = true;
741 }
742 }
743 if (dumpMem) {
744 result.append("\nDumping memory:\n");
745 std::string s = dumpMemoryAddresses(100 /* limit */);
746 result.append(s.c_str(), s.size());
747 }
748 if (unreachableMemory) {
749 result.append("\nDumping unreachable memory:\n");
750 // TODO - should limit be an argument parameter?
751 std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */);
752 result.append(s.c_str(), s.size());
753 }
754 }
755 write(fd, result.c_str(), result.size());
756
757 return NO_ERROR;
758 }
759
removeClient(const wp<Client> & client)760 void MediaPlayerService::removeClient(const wp<Client>& client)
761 {
762 Mutex::Autolock lock(mLock);
763 mClients.remove(client);
764 }
765
hasClient(wp<Client> client)766 bool MediaPlayerService::hasClient(wp<Client> client)
767 {
768 Mutex::Autolock lock(mLock);
769 return mClients.indexOf(client) != NAME_NOT_FOUND;
770 }
771
Client(const sp<MediaPlayerService> & service,const AttributionSourceState & attributionSource,int32_t connId,const sp<IMediaPlayerClient> & client,audio_session_t audioSessionId)772 MediaPlayerService::Client::Client(
773 const sp<MediaPlayerService>& service, const AttributionSourceState& attributionSource,
774 int32_t connId, const sp<IMediaPlayerClient>& client,
775 audio_session_t audioSessionId)
776 : mAttributionSource(attributionSource)
777 {
778 ALOGV("Client(%d) constructor", connId);
779 mConnId = connId;
780 mService = service;
781 mClient = client;
782 mLoop = false;
783 mStatus = NO_INIT;
784 mAudioSessionId = audioSessionId;
785 mRetransmitEndpointValid = false;
786 mAudioAttributes = NULL;
787 mListener = new Listener(this);
788
789 #if CALLBACK_ANTAGONIZER
790 ALOGD("create Antagonizer");
791 mAntagonizer = new Antagonizer(mListener);
792 #endif
793 }
794
~Client()795 MediaPlayerService::Client::~Client()
796 {
797 ALOGV("Client(%d) destructor AttributionSource = %s", mConnId,
798 mAttributionSource.toString().c_str());
799 mAudioOutput.clear();
800 wp<Client> client(this);
801 disconnect();
802 mService->removeClient(client);
803 if (mAudioAttributes != NULL) {
804 free(mAudioAttributes);
805 }
806 mAudioDeviceUpdatedListener.clear();
807 }
808
disconnect()809 void MediaPlayerService::Client::disconnect()
810 {
811 ALOGV("disconnect(%d) from AttributionSource %s", mConnId,
812 mAttributionSource.toString().c_str());
813 // grab local reference and clear main reference to prevent future
814 // access to object
815 sp<MediaPlayerBase> p;
816 {
817 Mutex::Autolock l(mLock);
818 p = mPlayer;
819 mClient.clear();
820 mPlayer.clear();
821 }
822
823 // clear the notification to prevent callbacks to dead client
824 // and reset the player. We assume the player will serialize
825 // access to itself if necessary.
826 if (p != 0) {
827 p->setNotifyCallback(0);
828 #if CALLBACK_ANTAGONIZER
829 ALOGD("kill Antagonizer");
830 mAntagonizer->kill();
831 #endif
832 p->reset();
833 }
834
835 {
836 Mutex::Autolock l(mLock);
837 disconnectNativeWindow_l();
838 }
839
840 IPCThreadState::self()->flushCommands();
841 }
842
createPlayer(player_type playerType)843 sp<MediaPlayerBase> MediaPlayerService::Client::createPlayer(player_type playerType)
844 {
845 // determine if we have the right player type
846 sp<MediaPlayerBase> p = getPlayer();
847 if ((p != NULL) && (p->playerType() != playerType)) {
848 ALOGV("delete player");
849 p.clear();
850 }
851 if (p == NULL) {
852 p = MediaPlayerFactory::createPlayer(playerType, mListener,
853 VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mAttributionSource.pid)));
854 }
855
856 if (p != NULL) {
857 p->setUID(VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(mAttributionSource.uid)));
858 }
859
860 return p;
861 }
862
onAudioDeviceUpdate(audio_io_handle_t audioIo,const DeviceIdVector & deviceIds)863 void MediaPlayerService::Client::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate(
864 audio_io_handle_t audioIo,
865 const DeviceIdVector& deviceIds) {
866 ALOGD("onAudioDeviceUpdate deviceIds: %s", toString(deviceIds).c_str());
867 sp<MediaPlayerBase> listener = mListener.promote();
868 if (listener != NULL) {
869 // Java should query the new device ids once it gets the event.
870 // TODO(b/378505346): Pass the deviceIds to Java to avoid race conditions.
871 listener->sendEvent(MEDIA_AUDIO_ROUTING_CHANGED, audioIo);
872 } else {
873 ALOGW("listener for process %d death is gone", MEDIA_AUDIO_ROUTING_CHANGED);
874 }
875 }
876
setDataSource_pre(player_type playerType)877 sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
878 player_type playerType)
879 {
880 ALOGV("player type = %d", playerType);
881
882 // create the right type of player
883 sp<MediaPlayerBase> p = createPlayer(playerType);
884 if (p == NULL) {
885 return p;
886 }
887
888 std::vector<DeathNotifier> deathNotifiers;
889
890 // Listen to death of media.extractor service
891 sp<IServiceManager> sm = defaultServiceManager();
892 sp<IBinder> binder = sm->getService(String16("media.extractor"));
893 if (binder == NULL) {
894 ALOGE("extractor service not available");
895 return NULL;
896 }
897 deathNotifiers.emplace_back(
898 binder, [l = wp<MediaPlayerBase>(p)]() {
899 sp<MediaPlayerBase> listener = l.promote();
900 if (listener) {
901 ALOGI("media.extractor died. Sending death notification.");
902 listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
903 MEDIAEXTRACTOR_PROCESS_DEATH);
904 } else {
905 ALOGW("media.extractor died without a death handler.");
906 }
907 });
908
909 {
910 using ::android::hidl::base::V1_0::IBase;
911
912 // Listen to death of OMX service
913 {
914 sp<IBase> base = ::android::hardware::media::omx::V1_0::
915 IOmx::getService();
916 if (base == nullptr) {
917 ALOGD("OMX service is not available");
918 } else {
919 deathNotifiers.emplace_back(
920 base, [l = wp<MediaPlayerBase>(p)]() {
921 sp<MediaPlayerBase> listener = l.promote();
922 if (listener) {
923 ALOGI("OMX service died. "
924 "Sending death notification.");
925 listener->sendEvent(
926 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
927 MEDIACODEC_PROCESS_DEATH);
928 } else {
929 ALOGW("OMX service died without a death handler.");
930 }
931 });
932 }
933 }
934
935 // Listen to death of Codec2 services
936 {
937 for (std::shared_ptr<Codec2Client> const& client :
938 Codec2Client::CreateFromAllServices()) {
939 sp<IBase> hidlBase = client->getHidlBase();
940 ::ndk::SpAIBinder aidlBase = client->getAidlBase();
941 auto onBinderDied = [l = wp<MediaPlayerBase>(p),
942 name = std::string(client->getServiceName())]() {
943 sp<MediaPlayerBase> listener = l.promote();
944 if (listener) {
945 ALOGI("Codec2 service \"%s\" died. "
946 "Sending death notification.",
947 name.c_str());
948 listener->sendEvent(
949 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
950 MEDIACODEC_PROCESS_DEATH);
951 } else {
952 ALOGW("Codec2 service \"%s\" died "
953 "without a death handler.",
954 name.c_str());
955 }
956 };
957 if (hidlBase) {
958 deathNotifiers.emplace_back(hidlBase, onBinderDied);
959 } else if (aidlBase.get() != nullptr) {
960 deathNotifiers.emplace_back(aidlBase, onBinderDied);
961 }
962 }
963 }
964 }
965
966 Mutex::Autolock lock(mLock);
967
968 mDeathNotifiers.clear();
969 mDeathNotifiers.swap(deathNotifiers);
970 mAudioDeviceUpdatedListener = new AudioDeviceUpdatedNotifier(p);
971
972 if (!p->hardwareOutput()) {
973 mAudioOutput = new AudioOutput(mAudioSessionId, mAttributionSource,
974 mAudioAttributes, mAudioDeviceUpdatedListener);
975 static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
976 }
977
978 return p;
979 }
980
setDataSource_post(const sp<MediaPlayerBase> & p,status_t status)981 status_t MediaPlayerService::Client::setDataSource_post(
982 const sp<MediaPlayerBase>& p,
983 status_t status)
984 {
985 ALOGV(" setDataSource");
986 if (status != OK) {
987 ALOGE(" error: %d", status);
988 return status;
989 }
990
991 // Set the re-transmission endpoint if one was chosen.
992 if (mRetransmitEndpointValid) {
993 status = p->setRetransmitEndpoint(&mRetransmitEndpoint);
994 if (status != NO_ERROR) {
995 ALOGE("setRetransmitEndpoint error: %d", status);
996 }
997 }
998
999 if (status == OK) {
1000 Mutex::Autolock lock(mLock);
1001 mPlayer = p;
1002 }
1003 return status;
1004 }
1005
setDataSource(const sp<IMediaHTTPService> & httpService,const char * url,const KeyedVector<String8,String8> * headers)1006 status_t MediaPlayerService::Client::setDataSource(
1007 const sp<IMediaHTTPService> &httpService,
1008 const char *url,
1009 const KeyedVector<String8, String8> *headers)
1010 {
1011 ALOGV("setDataSource(%s)", url);
1012 if (url == NULL)
1013 return UNKNOWN_ERROR;
1014
1015 if ((strncmp(url, "http://", 7) == 0) ||
1016 (strncmp(url, "https://", 8) == 0) ||
1017 (strncmp(url, "rtsp://", 7) == 0)) {
1018 if (!checkPermission("android.permission.INTERNET")) {
1019 return PERMISSION_DENIED;
1020 }
1021 }
1022
1023 if (strncmp(url, "content://", 10) == 0) {
1024 // get a filedescriptor for the content Uri and
1025 // pass it to the setDataSource(fd) method
1026
1027 String16 url16(url);
1028 int fd = android::openContentProviderFile(url16);
1029 if (fd < 0)
1030 {
1031 ALOGE("Couldn't open fd for %s", url);
1032 return UNKNOWN_ERROR;
1033 }
1034 status_t status = setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
1035 close(fd);
1036 return mStatus = status;
1037 } else {
1038 player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
1039 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1040 if (p == NULL) {
1041 return NO_INIT;
1042 }
1043
1044 return mStatus =
1045 setDataSource_post(
1046 p, p->setDataSource(httpService, url, headers));
1047 }
1048 }
1049
setDataSource(int fd,int64_t offset,int64_t length)1050 status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
1051 {
1052 ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
1053 fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
1054 struct stat sb;
1055 int ret = fstat(fd, &sb);
1056 if (ret != 0) {
1057 ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
1058 return UNKNOWN_ERROR;
1059 }
1060
1061 ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev));
1062 ALOGV("st_mode = %u", sb.st_mode);
1063 ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
1064 ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
1065 ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
1066
1067 if (offset >= sb.st_size) {
1068 ALOGE("offset error");
1069 return UNKNOWN_ERROR;
1070 }
1071 if (offset + length > sb.st_size) {
1072 length = sb.st_size - offset;
1073 ALOGV("calculated length = %lld", (long long)length);
1074 }
1075
1076 player_type playerType = MediaPlayerFactory::getPlayerType(this,
1077 fd,
1078 offset,
1079 length);
1080 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1081 if (p == NULL) {
1082 return NO_INIT;
1083 }
1084
1085 // now set data source
1086 return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length));
1087 }
1088
setDataSource(const sp<IStreamSource> & source)1089 status_t MediaPlayerService::Client::setDataSource(
1090 const sp<IStreamSource> &source) {
1091 // create the right type of player
1092 player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
1093 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1094 if (p == NULL) {
1095 return NO_INIT;
1096 }
1097
1098 // now set data source
1099 return mStatus = setDataSource_post(p, p->setDataSource(source));
1100 }
1101
setDataSource(const sp<IDataSource> & source)1102 status_t MediaPlayerService::Client::setDataSource(
1103 const sp<IDataSource> &source) {
1104 sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source);
1105 player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
1106 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1107 if (p == NULL) {
1108 return NO_INIT;
1109 }
1110 // now set data source
1111 return mStatus = setDataSource_post(p, p->setDataSource(dataSource));
1112 }
1113
setDataSource(const String8 & rtpParams)1114 status_t MediaPlayerService::Client::setDataSource(
1115 const String8& rtpParams) {
1116 player_type playerType = NU_PLAYER;
1117 sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1118 if (p == NULL) {
1119 return NO_INIT;
1120 }
1121 // now set data source
1122 return mStatus = setDataSource_post(p, p->setDataSource(rtpParams));
1123 }
1124
disconnectNativeWindow_l()1125 void MediaPlayerService::Client::disconnectNativeWindow_l() {
1126 if (mConnectedWindow != NULL) {
1127 status_t err = nativeWindowDisconnect(
1128 mConnectedWindow.get(), "disconnectNativeWindow");
1129
1130 if (err != OK) {
1131 ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
1132 strerror(-err), err);
1133 }
1134 }
1135 mConnectedWindow.clear();
1136 }
1137
setVideoSurfaceTexture(const sp<IGraphicBufferProducer> & bufferProducer)1138 status_t MediaPlayerService::Client::setVideoSurfaceTexture(
1139 const sp<IGraphicBufferProducer>& bufferProducer)
1140 {
1141 ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
1142 sp<MediaPlayerBase> p = getPlayer();
1143 if (p == 0) return UNKNOWN_ERROR;
1144
1145 sp<IBinder> binder(IInterface::asBinder(bufferProducer));
1146 if (mConnectedWindowBinder == binder) {
1147 return OK;
1148 }
1149
1150 sp<ANativeWindow> anw;
1151 if (bufferProducer != NULL) {
1152 anw = new Surface(bufferProducer, true /* controlledByApp */);
1153 status_t err = nativeWindowConnect(anw.get(), "setVideoSurfaceTexture");
1154
1155 if (err != OK) {
1156 ALOGE("setVideoSurfaceTexture failed: %d", err);
1157 // Note that we must do the reset before disconnecting from the ANW.
1158 // Otherwise queue/dequeue calls could be made on the disconnected
1159 // ANW, which may result in errors.
1160 reset();
1161
1162 Mutex::Autolock lock(mLock);
1163 disconnectNativeWindow_l();
1164
1165 return err;
1166 }
1167 }
1168
1169 // Note that we must set the player's new GraphicBufferProducer before
1170 // disconnecting the old one. Otherwise queue/dequeue calls could be made
1171 // on the disconnected ANW, which may result in errors.
1172 status_t err = p->setVideoSurfaceTexture(bufferProducer);
1173
1174 mLock.lock();
1175 disconnectNativeWindow_l();
1176
1177 if (err == OK) {
1178 mConnectedWindow = anw;
1179 mConnectedWindowBinder = binder;
1180 mLock.unlock();
1181 } else {
1182 mLock.unlock();
1183 status_t err = nativeWindowDisconnect(
1184 anw.get(), "disconnectNativeWindow");
1185
1186 if (err != OK) {
1187 ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
1188 strerror(-err), err);
1189 }
1190 }
1191
1192 return err;
1193 }
1194
invoke(const Parcel & request,Parcel * reply)1195 status_t MediaPlayerService::Client::invoke(const Parcel& request,
1196 Parcel *reply)
1197 {
1198 sp<MediaPlayerBase> p = getPlayer();
1199 if (p == NULL) return UNKNOWN_ERROR;
1200 return p->invoke(request, reply);
1201 }
1202
1203 // This call doesn't need to access the native player.
setMetadataFilter(const Parcel & filter)1204 status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
1205 {
1206 status_t status;
1207 media::Metadata::Filter allow, drop;
1208
1209 if (unmarshallFilter(filter, &allow, &status) &&
1210 unmarshallFilter(filter, &drop, &status)) {
1211 Mutex::Autolock lock(mLock);
1212
1213 mMetadataAllow = allow;
1214 mMetadataDrop = drop;
1215 }
1216 return status;
1217 }
1218
getMetadata(bool update_only,bool,Parcel * reply)1219 status_t MediaPlayerService::Client::getMetadata(
1220 bool update_only, bool /*apply_filter*/, Parcel *reply)
1221 {
1222 sp<MediaPlayerBase> player = getPlayer();
1223 if (player == 0) return UNKNOWN_ERROR;
1224
1225 status_t status;
1226 // Placeholder for the return code, updated by the caller.
1227 reply->writeInt32(-1);
1228
1229 media::Metadata::Filter ids;
1230
1231 // We don't block notifications while we fetch the data. We clear
1232 // mMetadataUpdated first so we don't lose notifications happening
1233 // during the rest of this call.
1234 {
1235 Mutex::Autolock lock(mLock);
1236 if (update_only) {
1237 ids = mMetadataUpdated;
1238 }
1239 mMetadataUpdated.clear();
1240 }
1241
1242 media::Metadata metadata(reply);
1243
1244 metadata.appendHeader();
1245 status = player->getMetadata(ids, reply);
1246
1247 if (status != OK) {
1248 metadata.resetParcel();
1249 ALOGE("getMetadata failed %d", status);
1250 return status;
1251 }
1252
1253 // FIXME: Implement filtering on the result. Not critical since
1254 // filtering takes place on the update notifications already. This
1255 // would be when all the metadata are fetch and a filter is set.
1256
1257 // Everything is fine, update the metadata length.
1258 metadata.updateLength();
1259 return OK;
1260 }
1261
setBufferingSettings(const BufferingSettings & buffering)1262 status_t MediaPlayerService::Client::setBufferingSettings(
1263 const BufferingSettings& buffering)
1264 {
1265 ALOGV("[%d] setBufferingSettings{%s}",
1266 mConnId, buffering.toString().c_str());
1267 sp<MediaPlayerBase> p = getPlayer();
1268 if (p == 0) return UNKNOWN_ERROR;
1269 return p->setBufferingSettings(buffering);
1270 }
1271
getBufferingSettings(BufferingSettings * buffering)1272 status_t MediaPlayerService::Client::getBufferingSettings(
1273 BufferingSettings* buffering /* nonnull */)
1274 {
1275 sp<MediaPlayerBase> p = getPlayer();
1276 // TODO: create mPlayer on demand.
1277 if (p == 0) return UNKNOWN_ERROR;
1278 status_t ret = p->getBufferingSettings(buffering);
1279 if (ret == NO_ERROR) {
1280 ALOGV("[%d] getBufferingSettings{%s}",
1281 mConnId, buffering->toString().c_str());
1282 } else {
1283 ALOGE("[%d] getBufferingSettings returned %d", mConnId, ret);
1284 }
1285 return ret;
1286 }
1287
prepareAsync()1288 status_t MediaPlayerService::Client::prepareAsync()
1289 {
1290 ALOGV("[%d] prepareAsync", mConnId);
1291 sp<MediaPlayerBase> p = getPlayer();
1292 if (p == 0) return UNKNOWN_ERROR;
1293 status_t ret = p->prepareAsync();
1294 #if CALLBACK_ANTAGONIZER
1295 ALOGD("start Antagonizer");
1296 if (ret == NO_ERROR) mAntagonizer->start();
1297 #endif
1298 return ret;
1299 }
1300
start()1301 status_t MediaPlayerService::Client::start()
1302 {
1303 ALOGV("[%d] start", mConnId);
1304 sp<MediaPlayerBase> p = getPlayer();
1305 if (p == 0) return UNKNOWN_ERROR;
1306 p->setLooping(mLoop);
1307 return p->start();
1308 }
1309
stop()1310 status_t MediaPlayerService::Client::stop()
1311 {
1312 ALOGV("[%d] stop", mConnId);
1313 sp<MediaPlayerBase> p = getPlayer();
1314 if (p == 0) return UNKNOWN_ERROR;
1315 return p->stop();
1316 }
1317
pause()1318 status_t MediaPlayerService::Client::pause()
1319 {
1320 ALOGV("[%d] pause", mConnId);
1321 sp<MediaPlayerBase> p = getPlayer();
1322 if (p == 0) return UNKNOWN_ERROR;
1323 return p->pause();
1324 }
1325
isPlaying(bool * state)1326 status_t MediaPlayerService::Client::isPlaying(bool* state)
1327 {
1328 *state = false;
1329 sp<MediaPlayerBase> p = getPlayer();
1330 if (p == 0) return UNKNOWN_ERROR;
1331 *state = p->isPlaying();
1332 ALOGV("[%d] isPlaying: %d", mConnId, *state);
1333 return NO_ERROR;
1334 }
1335
setPlaybackSettings(const AudioPlaybackRate & rate)1336 status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
1337 {
1338 ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
1339 mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
1340 sp<MediaPlayerBase> p = getPlayer();
1341 if (p == 0) return UNKNOWN_ERROR;
1342 return p->setPlaybackSettings(rate);
1343 }
1344
getPlaybackSettings(AudioPlaybackRate * rate)1345 status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
1346 {
1347 sp<MediaPlayerBase> p = getPlayer();
1348 if (p == 0) return UNKNOWN_ERROR;
1349 status_t ret = p->getPlaybackSettings(rate);
1350 if (ret == NO_ERROR) {
1351 ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
1352 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
1353 } else {
1354 ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
1355 }
1356 return ret;
1357 }
1358
setSyncSettings(const AVSyncSettings & sync,float videoFpsHint)1359 status_t MediaPlayerService::Client::setSyncSettings(
1360 const AVSyncSettings& sync, float videoFpsHint)
1361 {
1362 ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
1363 mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
1364 sp<MediaPlayerBase> p = getPlayer();
1365 if (p == 0) return UNKNOWN_ERROR;
1366 return p->setSyncSettings(sync, videoFpsHint);
1367 }
1368
getSyncSettings(AVSyncSettings * sync,float * videoFps)1369 status_t MediaPlayerService::Client::getSyncSettings(
1370 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
1371 {
1372 sp<MediaPlayerBase> p = getPlayer();
1373 if (p == 0) return UNKNOWN_ERROR;
1374 status_t ret = p->getSyncSettings(sync, videoFps);
1375 if (ret == NO_ERROR) {
1376 ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
1377 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
1378 } else {
1379 ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
1380 }
1381 return ret;
1382 }
1383
getCurrentPosition(int * msec)1384 status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1385 {
1386 ALOGV("getCurrentPosition");
1387 sp<MediaPlayerBase> p = getPlayer();
1388 if (p == 0) return UNKNOWN_ERROR;
1389 status_t ret = p->getCurrentPosition(msec);
1390 if (ret == NO_ERROR) {
1391 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
1392 } else {
1393 ALOGE("getCurrentPosition returned %d", ret);
1394 }
1395 return ret;
1396 }
1397
getDuration(int * msec)1398 status_t MediaPlayerService::Client::getDuration(int *msec)
1399 {
1400 ALOGV("getDuration");
1401 sp<MediaPlayerBase> p = getPlayer();
1402 if (p == 0) return UNKNOWN_ERROR;
1403 status_t ret = p->getDuration(msec);
1404 if (ret == NO_ERROR) {
1405 ALOGV("[%d] getDuration = %d", mConnId, *msec);
1406 } else {
1407 ALOGE("getDuration returned %d", ret);
1408 }
1409 return ret;
1410 }
1411
setNextPlayer(const sp<IMediaPlayer> & player)1412 status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1413 ALOGV("setNextPlayer");
1414 Mutex::Autolock l(mLock);
1415 sp<Client> c = static_cast<Client*>(player.get());
1416 if (c != NULL && !mService->hasClient(c)) {
1417 return BAD_VALUE;
1418 }
1419
1420 mNextClient = c;
1421
1422 if (c != NULL) {
1423 if (mAudioOutput != NULL) {
1424 mAudioOutput->setNextOutput(c->mAudioOutput);
1425 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1426 ALOGE("no current audio output");
1427 }
1428
1429 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1430 mPlayer->setNextPlayer(mNextClient->getPlayer());
1431 }
1432 }
1433
1434 return OK;
1435 }
1436
applyVolumeShaper(const sp<VolumeShaper::Configuration> & configuration,const sp<VolumeShaper::Operation> & operation)1437 VolumeShaper::Status MediaPlayerService::Client::applyVolumeShaper(
1438 const sp<VolumeShaper::Configuration>& configuration,
1439 const sp<VolumeShaper::Operation>& operation) {
1440 // for hardware output, call player instead
1441 ALOGV("Client::applyVolumeShaper(%p)", this);
1442 sp<MediaPlayerBase> p = getPlayer();
1443 {
1444 Mutex::Autolock l(mLock);
1445 if (p != 0 && p->hardwareOutput()) {
1446 // TODO: investigate internal implementation
1447 return VolumeShaper::Status(INVALID_OPERATION);
1448 }
1449 if (mAudioOutput.get() != nullptr) {
1450 return mAudioOutput->applyVolumeShaper(configuration, operation);
1451 }
1452 }
1453 return VolumeShaper::Status(INVALID_OPERATION);
1454 }
1455
getVolumeShaperState(int id)1456 sp<VolumeShaper::State> MediaPlayerService::Client::getVolumeShaperState(int id) {
1457 // for hardware output, call player instead
1458 ALOGV("Client::getVolumeShaperState(%p)", this);
1459 sp<MediaPlayerBase> p = getPlayer();
1460 {
1461 Mutex::Autolock l(mLock);
1462 if (p != 0 && p->hardwareOutput()) {
1463 // TODO: investigate internal implementation.
1464 return nullptr;
1465 }
1466 if (mAudioOutput.get() != nullptr) {
1467 return mAudioOutput->getVolumeShaperState(id);
1468 }
1469 }
1470 return nullptr;
1471 }
1472
seekTo(int msec,MediaPlayerSeekMode mode)1473 status_t MediaPlayerService::Client::seekTo(int msec, MediaPlayerSeekMode mode)
1474 {
1475 ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode);
1476 sp<MediaPlayerBase> p = getPlayer();
1477 if (p == 0) return UNKNOWN_ERROR;
1478 return p->seekTo(msec, mode);
1479 }
1480
reset()1481 status_t MediaPlayerService::Client::reset()
1482 {
1483 ALOGV("[%d] reset", mConnId);
1484 mRetransmitEndpointValid = false;
1485 sp<MediaPlayerBase> p = getPlayer();
1486 if (p == 0) return UNKNOWN_ERROR;
1487 return p->reset();
1488 }
1489
notifyAt(int64_t mediaTimeUs)1490 status_t MediaPlayerService::Client::notifyAt(int64_t mediaTimeUs)
1491 {
1492 ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs);
1493 sp<MediaPlayerBase> p = getPlayer();
1494 if (p == 0) return UNKNOWN_ERROR;
1495 return p->notifyAt(mediaTimeUs);
1496 }
1497
setAudioStreamType(audio_stream_type_t type)1498 status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
1499 {
1500 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1501 // TODO: for hardware output, call player instead
1502 Mutex::Autolock l(mLock);
1503 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1504 return NO_ERROR;
1505 }
1506
setAudioAttributes_l(const Parcel & parcel)1507 status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1508 {
1509 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1510 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1511 if (mAudioAttributes == NULL) {
1512 return NO_MEMORY;
1513 }
1514 unmarshallAudioAttributes(parcel, mAudioAttributes);
1515
1516 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1517 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1518 mAudioAttributes->tags);
1519
1520 if (mAudioOutput != 0) {
1521 mAudioOutput->setAudioAttributes(mAudioAttributes);
1522 }
1523 return NO_ERROR;
1524 }
1525
setLooping(int loop)1526 status_t MediaPlayerService::Client::setLooping(int loop)
1527 {
1528 ALOGV("[%d] setLooping(%d)", mConnId, loop);
1529 mLoop = loop;
1530 sp<MediaPlayerBase> p = getPlayer();
1531 if (p != 0) return p->setLooping(loop);
1532 return NO_ERROR;
1533 }
1534
setVolume(float leftVolume,float rightVolume)1535 status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1536 {
1537 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1538
1539 // for hardware output, call player instead
1540 sp<MediaPlayerBase> p = getPlayer();
1541 {
1542 Mutex::Autolock l(mLock);
1543 if (p != 0 && p->hardwareOutput()) {
1544 MediaPlayerHWInterface* hwp =
1545 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1546 return hwp->setVolume(leftVolume, rightVolume);
1547 } else {
1548 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1549 return NO_ERROR;
1550 }
1551 }
1552
1553 return NO_ERROR;
1554 }
1555
setAuxEffectSendLevel(float level)1556 status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1557 {
1558 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
1559 Mutex::Autolock l(mLock);
1560 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1561 return NO_ERROR;
1562 }
1563
attachAuxEffect(int effectId)1564 status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1565 {
1566 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
1567 Mutex::Autolock l(mLock);
1568 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1569 return NO_ERROR;
1570 }
1571
setParameter(int key,const Parcel & request)1572 status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
1573 ALOGV("[%d] setParameter(%d)", mConnId, key);
1574 switch (key) {
1575 case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1576 {
1577 Mutex::Autolock l(mLock);
1578 return setAudioAttributes_l(request);
1579 }
1580 default:
1581 sp<MediaPlayerBase> p = getPlayer();
1582 if (p == 0) { return UNKNOWN_ERROR; }
1583 return p->setParameter(key, request);
1584 }
1585 }
1586
getParameter(int key,Parcel * reply)1587 status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
1588 ALOGV("[%d] getParameter(%d)", mConnId, key);
1589 sp<MediaPlayerBase> p = getPlayer();
1590 if (p == 0) return UNKNOWN_ERROR;
1591 return p->getParameter(key, reply);
1592 }
1593
setRetransmitEndpoint(const struct sockaddr_in * endpoint)1594 status_t MediaPlayerService::Client::setRetransmitEndpoint(
1595 const struct sockaddr_in* endpoint) {
1596
1597 if (NULL != endpoint) {
1598 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1599 uint16_t p = ntohs(endpoint->sin_port);
1600 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1601 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1602 } else {
1603 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1604 }
1605
1606 sp<MediaPlayerBase> p = getPlayer();
1607
1608 // Right now, the only valid time to set a retransmit endpoint is before
1609 // player selection has been made (since the presence or absence of a
1610 // retransmit endpoint is going to determine which player is selected during
1611 // setDataSource).
1612 if (p != 0) return INVALID_OPERATION;
1613
1614 if (NULL != endpoint) {
1615 Mutex::Autolock lock(mLock);
1616 mRetransmitEndpoint = *endpoint;
1617 mRetransmitEndpointValid = true;
1618 } else {
1619 Mutex::Autolock lock(mLock);
1620 mRetransmitEndpointValid = false;
1621 }
1622
1623 return NO_ERROR;
1624 }
1625
getRetransmitEndpoint(struct sockaddr_in * endpoint)1626 status_t MediaPlayerService::Client::getRetransmitEndpoint(
1627 struct sockaddr_in* endpoint)
1628 {
1629 if (NULL == endpoint)
1630 return BAD_VALUE;
1631
1632 sp<MediaPlayerBase> p = getPlayer();
1633
1634 if (p != NULL)
1635 return p->getRetransmitEndpoint(endpoint);
1636
1637 Mutex::Autolock lock(mLock);
1638 if (!mRetransmitEndpointValid)
1639 return NO_INIT;
1640
1641 *endpoint = mRetransmitEndpoint;
1642
1643 return NO_ERROR;
1644 }
1645
notify(int msg,int ext1,int ext2,const Parcel * obj)1646 void MediaPlayerService::Client::notify(
1647 int msg, int ext1, int ext2, const Parcel *obj)
1648 {
1649 sp<IMediaPlayerClient> c;
1650 sp<Client> nextClient;
1651 status_t errStartNext = NO_ERROR;
1652 {
1653 Mutex::Autolock l(mLock);
1654 c = mClient;
1655 if (msg == MEDIA_PLAYBACK_COMPLETE && mNextClient != NULL) {
1656 nextClient = mNextClient;
1657
1658 if (mAudioOutput != NULL)
1659 mAudioOutput->switchToNextOutput();
1660
1661 errStartNext = nextClient->start();
1662 }
1663 }
1664
1665 if (nextClient != NULL) {
1666 sp<IMediaPlayerClient> nc;
1667 {
1668 Mutex::Autolock l(nextClient->mLock);
1669 nc = nextClient->mClient;
1670 }
1671 if (nc != NULL) {
1672 if (errStartNext == NO_ERROR) {
1673 nc->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1674 } else {
1675 nc->notify(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN , 0, obj);
1676 ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext);
1677 }
1678 }
1679 }
1680
1681 if (MEDIA_INFO == msg &&
1682 MEDIA_INFO_METADATA_UPDATE == ext1) {
1683 const media::Metadata::Type metadata_type = ext2;
1684
1685 if(shouldDropMetadata(metadata_type)) {
1686 return;
1687 }
1688
1689 // Update the list of metadata that have changed. getMetadata
1690 // also access mMetadataUpdated and clears it.
1691 addNewMetadataUpdate(metadata_type);
1692 }
1693
1694 if (c != NULL) {
1695 ALOGV("[%d] notify (%d, %d, %d)", mConnId, msg, ext1, ext2);
1696 c->notify(msg, ext1, ext2, obj);
1697 }
1698 }
1699
1700
shouldDropMetadata(media::Metadata::Type code) const1701 bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
1702 {
1703 Mutex::Autolock lock(mLock);
1704
1705 if (findMetadata(mMetadataDrop, code)) {
1706 return true;
1707 }
1708
1709 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
1710 return false;
1711 } else {
1712 return true;
1713 }
1714 }
1715
1716
addNewMetadataUpdate(media::Metadata::Type metadata_type)1717 void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
1718 Mutex::Autolock lock(mLock);
1719 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1720 mMetadataUpdated.add(metadata_type);
1721 }
1722 }
1723
1724 // Modular DRM
prepareDrm(const uint8_t uuid[16],const Vector<uint8_t> & drmSessionId)1725 status_t MediaPlayerService::Client::prepareDrm(const uint8_t uuid[16],
1726 const Vector<uint8_t>& drmSessionId)
1727 {
1728 ALOGV("[%d] prepareDrm", mConnId);
1729 sp<MediaPlayerBase> p = getPlayer();
1730 if (p == 0) return UNKNOWN_ERROR;
1731
1732 status_t ret = p->prepareDrm(uuid, drmSessionId);
1733 ALOGV("prepareDrm ret: %d", ret);
1734
1735 return ret;
1736 }
1737
releaseDrm()1738 status_t MediaPlayerService::Client::releaseDrm()
1739 {
1740 ALOGV("[%d] releaseDrm", mConnId);
1741 sp<MediaPlayerBase> p = getPlayer();
1742 if (p == 0) return UNKNOWN_ERROR;
1743
1744 status_t ret = p->releaseDrm();
1745 ALOGV("releaseDrm ret: %d", ret);
1746
1747 return ret;
1748 }
1749
setOutputDevice(audio_port_handle_t deviceId)1750 status_t MediaPlayerService::Client::setOutputDevice(audio_port_handle_t deviceId)
1751 {
1752 ALOGV("[%d] setOutputDevice", mConnId);
1753 {
1754 Mutex::Autolock l(mLock);
1755 if (mAudioOutput.get() != nullptr) {
1756 return mAudioOutput->setOutputDevice(deviceId);
1757 }
1758 }
1759 return NO_INIT;
1760 }
1761
getRoutedDeviceIds(DeviceIdVector & deviceIds)1762 status_t MediaPlayerService::Client::getRoutedDeviceIds(DeviceIdVector& deviceIds)
1763 {
1764 ALOGV("[%d] getRoutedDeviceIds", mConnId);
1765 {
1766 Mutex::Autolock l(mLock);
1767 if (mAudioOutput.get() != nullptr) {
1768 return mAudioOutput->getRoutedDeviceIds(deviceIds);
1769 }
1770 }
1771 return NO_INIT;
1772 }
1773
enableAudioDeviceCallback(bool enabled)1774 status_t MediaPlayerService::Client::enableAudioDeviceCallback(bool enabled)
1775 {
1776 ALOGV("[%d] enableAudioDeviceCallback, %d", mConnId, enabled);
1777 {
1778 Mutex::Autolock l(mLock);
1779 if (mAudioOutput.get() != nullptr) {
1780 return mAudioOutput->enableAudioDeviceCallback(enabled);
1781 }
1782 }
1783 return NO_INIT;
1784 }
1785
1786 #if CALLBACK_ANTAGONIZER
1787 const int Antagonizer::interval = 10000; // 10 msecs
1788
Antagonizer(const sp<MediaPlayerBase::Listener> & listener)1789 Antagonizer::Antagonizer(const sp<MediaPlayerBase::Listener> &listener) :
1790 mExit(false), mActive(false), mListener(listener)
1791 {
1792 createThread(callbackThread, this);
1793 }
1794
kill()1795 void Antagonizer::kill()
1796 {
1797 Mutex::Autolock _l(mLock);
1798 mActive = false;
1799 mExit = true;
1800 mCondition.wait(mLock);
1801 }
1802
callbackThread(void * user)1803 int Antagonizer::callbackThread(void* user)
1804 {
1805 ALOGD("Antagonizer started");
1806 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1807 while (!p->mExit) {
1808 if (p->mActive) {
1809 ALOGV("send event");
1810 p->mListener->notify(0, 0, 0, 0);
1811 }
1812 usleep(interval);
1813 }
1814 Mutex::Autolock _l(p->mLock);
1815 p->mCondition.signal();
1816 ALOGD("Antagonizer stopped");
1817 return 0;
1818 }
1819 #endif
1820
1821 #undef LOG_TAG
1822 #define LOG_TAG "AudioSink"
AudioOutput(audio_session_t sessionId,const AttributionSourceState & attributionSource,const audio_attributes_t * attr,const sp<AudioSystem::AudioDeviceCallback> & deviceCallback)1823 MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId,
1824 const AttributionSourceState& attributionSource, const audio_attributes_t* attr,
1825 const sp<AudioSystem::AudioDeviceCallback>& deviceCallback)
1826 : mCachedPlayerIId(PLAYER_PIID_INVALID),
1827 mCallback(NULL),
1828 mStreamType(AUDIO_STREAM_MUSIC),
1829 mLeftVolume(1.0),
1830 mRightVolume(1.0),
1831 mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
1832 mSampleRateHz(0),
1833 mMsecsPerFrame(0),
1834 mFrameSize(0),
1835 mSessionId(sessionId),
1836 mAttributionSource(attributionSource),
1837 mSendLevel(0.0),
1838 mAuxEffectId(0),
1839 mFlags(AUDIO_OUTPUT_FLAG_NONE),
1840 mVolumeHandler(new media::VolumeHandler()),
1841 mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
1842 mDeviceCallbackEnabled(false),
1843 mDeviceCallback(deviceCallback)
1844 {
1845 ALOGV("AudioOutput(%d)", sessionId);
1846 if (attr != NULL) {
1847 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1848 if (mAttributes != NULL) {
1849 memcpy(mAttributes, attr, sizeof(audio_attributes_t));
1850 mStreamType = AudioSystem::attributesToStreamType(*attr);
1851 }
1852 } else {
1853 mAttributes = NULL;
1854 }
1855 setMinBufferCount();
1856 }
1857
~AudioOutput()1858 MediaPlayerService::AudioOutput::~AudioOutput()
1859 {
1860 close();
1861 free(mAttributes);
1862 }
1863
1864 //static
setMinBufferCount()1865 void MediaPlayerService::AudioOutput::setMinBufferCount()
1866 {
1867 if (property_get_bool("ro.boot.qemu", false)) {
1868 mIsOnEmulator = true;
1869 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1870 }
1871 }
1872
1873 // static
isOnEmulator()1874 bool MediaPlayerService::AudioOutput::isOnEmulator()
1875 {
1876 setMinBufferCount(); // benign race wrt other threads
1877 return mIsOnEmulator;
1878 }
1879
1880 // static
getMinBufferCount()1881 int MediaPlayerService::AudioOutput::getMinBufferCount()
1882 {
1883 setMinBufferCount(); // benign race wrt other threads
1884 return mMinBufferCount;
1885 }
1886
bufferSize() const1887 ssize_t MediaPlayerService::AudioOutput::bufferSize() const
1888 {
1889 Mutex::Autolock lock(mLock);
1890 if (mTrack == 0) return NO_INIT;
1891 return mTrack->frameCount() * mFrameSize;
1892 }
1893
frameCount() const1894 ssize_t MediaPlayerService::AudioOutput::frameCount() const
1895 {
1896 Mutex::Autolock lock(mLock);
1897 if (mTrack == 0) return NO_INIT;
1898 return mTrack->frameCount();
1899 }
1900
channelCount() const1901 ssize_t MediaPlayerService::AudioOutput::channelCount() const
1902 {
1903 Mutex::Autolock lock(mLock);
1904 if (mTrack == 0) return NO_INIT;
1905 return mTrack->channelCount();
1906 }
1907
frameSize() const1908 ssize_t MediaPlayerService::AudioOutput::frameSize() const
1909 {
1910 Mutex::Autolock lock(mLock);
1911 if (mTrack == 0) return NO_INIT;
1912 return mFrameSize;
1913 }
1914
latency() const1915 uint32_t MediaPlayerService::AudioOutput::latency () const
1916 {
1917 Mutex::Autolock lock(mLock);
1918 if (mTrack == 0) return 0;
1919 return mTrack->latency();
1920 }
1921
msecsPerFrame() const1922 float MediaPlayerService::AudioOutput::msecsPerFrame() const
1923 {
1924 Mutex::Autolock lock(mLock);
1925 return mMsecsPerFrame;
1926 }
1927
getPosition(uint32_t * position) const1928 status_t MediaPlayerService::AudioOutput::getPosition(uint32_t *position) const
1929 {
1930 Mutex::Autolock lock(mLock);
1931 if (mTrack == 0) return NO_INIT;
1932 return mTrack->getPosition(position);
1933 }
1934
getTimestamp(AudioTimestamp & ts) const1935 status_t MediaPlayerService::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1936 {
1937 Mutex::Autolock lock(mLock);
1938 if (mTrack == 0) return NO_INIT;
1939 return mTrack->getTimestamp(ts);
1940 }
1941
1942 // TODO: Remove unnecessary calls to getPlayedOutDurationUs()
1943 // as it acquires locks and may query the audio driver.
1944 //
1945 // Some calls could conceivably retrieve extrapolated data instead of
1946 // accessing getTimestamp() or getPosition() every time a data buffer with
1947 // a media time is received.
1948 //
1949 // Calculate duration of played samples if played at normal rate (i.e., 1.0).
getPlayedOutDurationUs(int64_t nowUs) const1950 int64_t MediaPlayerService::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const
1951 {
1952 Mutex::Autolock lock(mLock);
1953 if (mTrack == 0 || mSampleRateHz == 0) {
1954 return 0;
1955 }
1956
1957 uint32_t numFramesPlayed;
1958 int64_t numFramesPlayedAtUs;
1959 AudioTimestamp ts;
1960
1961 status_t res = mTrack->getTimestamp(ts);
1962 if (res == OK) { // case 1: mixing audio tracks and offloaded tracks.
1963 numFramesPlayed = ts.mPosition;
1964 numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000;
1965 //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
1966 } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track
1967 numFramesPlayed = 0;
1968 numFramesPlayedAtUs = nowUs;
1969 //ALOGD("getTimestamp: WOULD_BLOCK %d %lld",
1970 // numFramesPlayed, (long long)numFramesPlayedAtUs);
1971 } else { // case 3: transitory at new track or audio fast tracks.
1972 res = mTrack->getPosition(&numFramesPlayed);
1973 CHECK_EQ(res, (status_t)OK);
1974 numFramesPlayedAtUs = nowUs;
1975 numFramesPlayedAtUs += 1000LL * mTrack->latency() / 2; /* XXX */
1976 //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
1977 }
1978
1979 // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test
1980 // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours.
1981 int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz)
1982 + nowUs - numFramesPlayedAtUs;
1983 if (durationUs < 0) {
1984 // Occurs when numFramesPlayed position is very small and the following:
1985 // (1) In case 1, the time nowUs is computed before getTimestamp() is called and
1986 // numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed.
1987 // (2) In case 3, using getPosition and adding mAudioSink->latency() to
1988 // numFramesPlayedAtUs, by a time amount greater than numFramesPlayed.
1989 //
1990 // Both of these are transitory conditions.
1991 ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs);
1992 durationUs = 0;
1993 }
1994 ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)",
1995 (long long)durationUs, (long long)nowUs,
1996 numFramesPlayed, (long long)numFramesPlayedAtUs);
1997 return durationUs;
1998 }
1999
getFramesWritten(uint32_t * frameswritten) const2000 status_t MediaPlayerService::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
2001 {
2002 Mutex::Autolock lock(mLock);
2003 if (mTrack == 0) return NO_INIT;
2004 ExtendedTimestamp ets;
2005 status_t status = mTrack->getTimestamp(&ets);
2006 if (status == OK || status == WOULD_BLOCK) {
2007 *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT];
2008 }
2009 return status;
2010 }
2011
setParameters(const String8 & keyValuePairs)2012 status_t MediaPlayerService::AudioOutput::setParameters(const String8& keyValuePairs)
2013 {
2014 Mutex::Autolock lock(mLock);
2015 if (mTrack == 0) return NO_INIT;
2016 return mTrack->setParameters(keyValuePairs);
2017 }
2018
getParameters(const String8 & keys)2019 String8 MediaPlayerService::AudioOutput::getParameters(const String8& keys)
2020 {
2021 Mutex::Autolock lock(mLock);
2022 if (mTrack == 0) return String8();
2023 return mTrack->getParameters(keys);
2024 }
2025
setAudioAttributes(const audio_attributes_t * attributes)2026 void MediaPlayerService::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
2027 Mutex::Autolock lock(mLock);
2028 if (attributes == NULL) {
2029 free(mAttributes);
2030 mAttributes = NULL;
2031 } else {
2032 if (mAttributes == NULL) {
2033 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
2034 }
2035 memcpy(mAttributes, attributes, sizeof(audio_attributes_t));
2036 mStreamType = AudioSystem::attributesToStreamType(*attributes);
2037 }
2038 }
2039
setAudioStreamType(audio_stream_type_t streamType)2040 void MediaPlayerService::AudioOutput::setAudioStreamType(audio_stream_type_t streamType)
2041 {
2042 Mutex::Autolock lock(mLock);
2043 // do not allow direct stream type modification if attributes have been set
2044 if (mAttributes == NULL) {
2045 mStreamType = streamType;
2046 }
2047 }
2048
deleteRecycledTrack_l()2049 void MediaPlayerService::AudioOutput::deleteRecycledTrack_l()
2050 {
2051 ALOGV("deleteRecycledTrack_l");
2052 if (mRecycledTrack != 0) {
2053
2054 if (mCallbackData != NULL) {
2055 mCallbackData->setOutput(NULL);
2056 mCallbackData->endTrackSwitch();
2057 }
2058
2059 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
2060 int32_t msec = 0;
2061 if (!mRecycledTrack->stopped()) { // check if active
2062 (void)mRecycledTrack->pendingDuration(&msec);
2063 }
2064 mRecycledTrack->stop(); // ensure full data drain
2065 ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec);
2066 if (msec > 0) {
2067 static const int32_t WAIT_LIMIT_MS = 3000;
2068 if (msec > WAIT_LIMIT_MS) {
2069 msec = WAIT_LIMIT_MS;
2070 }
2071 usleep(msec * 1000LL);
2072 }
2073 }
2074 // An offloaded track isn't flushed because the STREAM_END is reported
2075 // slightly prematurely to allow time for the gapless track switch
2076 // but this means that if we decide not to recycle the track there
2077 // could be a small amount of residual data still playing. We leave
2078 // AudioFlinger to drain the track.
2079
2080 mRecycledTrack.clear();
2081 close_l();
2082 mCallbackData.clear();
2083 }
2084 }
2085
close_l()2086 void MediaPlayerService::AudioOutput::close_l()
2087 {
2088 mTrack.clear();
2089 }
2090
open(uint32_t sampleRate,int channelCount,audio_channel_mask_t channelMask,audio_format_t format,int bufferCount,AudioCallback cb,const wp<RefBase> & cookie,audio_output_flags_t flags,const audio_offload_info_t * offloadInfo,bool doNotReconnect,uint32_t suggestedFrameCount)2091 status_t MediaPlayerService::AudioOutput::open(
2092 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
2093 audio_format_t format, int bufferCount,
2094 AudioCallback cb, const wp<RefBase>& cookie,
2095 audio_output_flags_t flags,
2096 const audio_offload_info_t *offloadInfo,
2097 bool doNotReconnect,
2098 uint32_t suggestedFrameCount)
2099 {
2100 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
2101 format, bufferCount, mSessionId, flags);
2102
2103 // offloading is only supported in callback mode for now.
2104 // offloadInfo must be present if offload flag is set
2105 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
2106 ((cb == NULL) || (offloadInfo == NULL))) {
2107 return BAD_VALUE;
2108 }
2109
2110 // compute frame count for the AudioTrack internal buffer
2111 size_t frameCount;
2112 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
2113 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
2114 } else {
2115 // try to estimate the buffer processing fetch size from AudioFlinger.
2116 // framesPerBuffer is approximate and generally correct, except when it's not :-).
2117 uint32_t afSampleRate;
2118 size_t afFrameCount;
2119 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
2120 return NO_INIT;
2121 }
2122 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
2123 return NO_INIT;
2124 }
2125 if (afSampleRate == 0) {
2126 return NO_INIT;
2127 }
2128 const size_t framesPerBuffer =
2129 (unsigned long long)sampleRate * afFrameCount / afSampleRate;
2130
2131 if (bufferCount == 0) {
2132 if (framesPerBuffer == 0) {
2133 return NO_INIT;
2134 }
2135 // use suggestedFrameCount
2136 bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer;
2137 }
2138 // Check argument bufferCount against the mininum buffer count
2139 if (bufferCount != 0 && bufferCount < mMinBufferCount) {
2140 ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount);
2141 bufferCount = mMinBufferCount;
2142 }
2143 // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger
2144 // which will be the minimum size permitted.
2145 frameCount = bufferCount * framesPerBuffer;
2146 }
2147
2148 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
2149 channelMask = audio_channel_out_mask_from_count(channelCount);
2150 if (0 == channelMask) {
2151 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
2152 return NO_INIT;
2153 }
2154 }
2155
2156 Mutex::Autolock lock(mLock);
2157 mCallback = cb;
2158 mCallbackCookie = cookie;
2159
2160 // Check whether we can recycle the track
2161 bool reuse = false;
2162 bool bothOffloaded = false;
2163
2164 if (mRecycledTrack != 0) {
2165 // check whether we are switching between two offloaded tracks
2166 bothOffloaded = (flags & mRecycledTrack->getFlags()
2167 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
2168
2169 // check if the existing track can be reused as-is, or if a new track needs to be created.
2170 reuse = true;
2171
2172 if ((mCallbackData == NULL && mCallback != NULL) ||
2173 (mCallbackData != NULL && mCallback == NULL)) {
2174 // recycled track uses callbacks but the caller wants to use writes, or vice versa
2175 ALOGV("can't chain callback and write");
2176 reuse = false;
2177 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
2178 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
2179 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
2180 mRecycledTrack->getSampleRate(), sampleRate,
2181 mRecycledTrack->channelCount(), channelCount);
2182 reuse = false;
2183 } else if (flags != mFlags) {
2184 ALOGV("output flags differ %08x/%08x", flags, mFlags);
2185 reuse = false;
2186 } else if (mRecycledTrack->format() != format) {
2187 reuse = false;
2188 }
2189 } else {
2190 ALOGV("no track available to recycle");
2191 }
2192
2193 ALOGV_IF(bothOffloaded, "both tracks offloaded");
2194
2195 // If we can't recycle and both tracks are offloaded
2196 // we must close the previous output before opening a new one
2197 if (bothOffloaded && !reuse) {
2198 ALOGV("both offloaded and not recycling");
2199 deleteRecycledTrack_l();
2200 }
2201
2202 sp<AudioTrack> t;
2203 sp<CallbackData> newcbd;
2204
2205 // We don't attempt to create a new track if we are recycling an
2206 // offloaded track. But, if we are recycling a non-offloaded or we
2207 // are switching where one is offloaded and one isn't then we create
2208 // the new track in advance so that we can read additional stream info
2209
2210 if (!(reuse && bothOffloaded)) {
2211 ALOGV("creating new AudioTrack");
2212
2213 if (mCallback != nullptr) {
2214 newcbd = sp<CallbackData>::make(wp<AudioOutput>::fromExisting(this));
2215 t = new AudioTrack(
2216 mStreamType,
2217 sampleRate,
2218 format,
2219 channelMask,
2220 frameCount,
2221 flags,
2222 newcbd,
2223 0, // notification frames
2224 mSessionId,
2225 AudioTrack::TRANSFER_CALLBACK,
2226 offloadInfo,
2227 mAttributionSource,
2228 mAttributes,
2229 doNotReconnect,
2230 1.0f, // default value for maxRequiredSpeed
2231 mSelectedDeviceId);
2232 } else {
2233 // TODO: Due to buffer memory concerns, we use a max target playback speed
2234 // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed),
2235 // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed.
2236 const float targetSpeed =
2237 std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed);
2238 ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed,
2239 "track target speed:%f clamped from playback speed:%f",
2240 targetSpeed, mPlaybackRate.mSpeed);
2241 t = new AudioTrack(
2242 mStreamType,
2243 sampleRate,
2244 format,
2245 channelMask,
2246 frameCount,
2247 flags,
2248 nullptr, // callback
2249 0, // notification frames
2250 mSessionId,
2251 AudioTrack::TRANSFER_DEFAULT,
2252 NULL, // offload info
2253 mAttributionSource,
2254 mAttributes,
2255 doNotReconnect,
2256 targetSpeed,
2257 mSelectedDeviceId);
2258 }
2259 // Set caller name so it can be logged in destructor.
2260 // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_MEDIA
2261 t->setCallerName("media");
2262 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
2263 ALOGE("Unable to create audio track");
2264 // t, newcbd goes out of scope, so reference count drops to zero
2265 return NO_INIT;
2266 } else {
2267 // successful AudioTrack initialization implies a legacy stream type was generated
2268 // from the audio attributes
2269 mStreamType = t->streamType();
2270 }
2271 }
2272
2273 if (reuse) {
2274 CHECK(mRecycledTrack != NULL);
2275
2276 if (!bothOffloaded) {
2277 if (mRecycledTrack->frameCount() != t->frameCount()) {
2278 ALOGV("framecount differs: %zu/%zu frames",
2279 mRecycledTrack->frameCount(), t->frameCount());
2280 reuse = false;
2281 }
2282 // If recycled and new tracks are not on the same output,
2283 // don't reuse the recycled one.
2284 if (mRecycledTrack->getOutput() != t->getOutput()) {
2285 ALOGV("output has changed, don't reuse track");
2286 reuse = false;
2287 }
2288 }
2289
2290 if (reuse) {
2291 ALOGV("chaining to next output and recycling track");
2292 close_l();
2293 mTrack = mRecycledTrack;
2294 mRecycledTrack.clear();
2295 if (mCallbackData != NULL) {
2296 mCallbackData->setOutput(this);
2297 }
2298 return updateTrack();
2299 }
2300 }
2301
2302 // we're not going to reuse the track, unblock and flush it
2303 // this was done earlier if both tracks are offloaded
2304 if (!bothOffloaded) {
2305 deleteRecycledTrack_l();
2306 }
2307
2308 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
2309
2310 mCallbackData = newcbd;
2311 ALOGV("setVolume");
2312 t->setVolume(mLeftVolume, mRightVolume);
2313
2314 // Restore VolumeShapers for the MediaPlayer in case the track was recreated
2315 // due to an output sink error (e.g. offload to non-offload switch).
2316 mVolumeHandler->forall([&t](const VolumeShaper &shaper) -> VolumeShaper::Status {
2317 sp<VolumeShaper::Operation> operationToEnd =
2318 new VolumeShaper::Operation(shaper.mOperation);
2319 // TODO: Ideally we would restore to the exact xOffset position
2320 // as returned by getVolumeShaperState(), but we don't have that
2321 // information when restoring at the client unless we periodically poll
2322 // the server or create shared memory state.
2323 //
2324 // For now, we simply advance to the end of the VolumeShaper effect
2325 // if it has been started.
2326 if (shaper.isStarted()) {
2327 operationToEnd->setNormalizedTime(1.f);
2328 }
2329 return t->applyVolumeShaper(shaper.mConfiguration, operationToEnd);
2330 });
2331
2332 if (mCachedPlayerIId != PLAYER_PIID_INVALID) {
2333 t->setPlayerIId(mCachedPlayerIId);
2334 }
2335
2336 mSampleRateHz = sampleRate;
2337 mFlags = flags;
2338 mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
2339 mFrameSize = t->frameSize();
2340 mTrack = t;
2341
2342 return updateTrack();
2343 }
2344
updateTrack()2345 status_t MediaPlayerService::AudioOutput::updateTrack() {
2346 if (mTrack == NULL) {
2347 return NO_ERROR;
2348 }
2349
2350 status_t res = NO_ERROR;
2351 // Note some output devices may give us a direct track even though we don't specify it.
2352 // Example: Line application b/17459982.
2353 if ((mTrack->getFlags()
2354 & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
2355 res = mTrack->setPlaybackRate(mPlaybackRate);
2356 if (res == NO_ERROR) {
2357 mTrack->setAuxEffectSendLevel(mSendLevel);
2358 res = mTrack->attachAuxEffect(mAuxEffectId);
2359 }
2360 }
2361 mTrack->setOutputDevice(mSelectedDeviceId);
2362 if (mDeviceCallbackEnabled) {
2363 mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
2364 }
2365 ALOGV("updateTrack() DONE status %d", res);
2366 return res;
2367 }
2368
start()2369 status_t MediaPlayerService::AudioOutput::start()
2370 {
2371 ALOGV("start");
2372 Mutex::Autolock lock(mLock);
2373 if (mCallbackData != NULL) {
2374 mCallbackData->endTrackSwitch();
2375 }
2376 if (mTrack != 0) {
2377 mTrack->setVolume(mLeftVolume, mRightVolume);
2378 mTrack->setAuxEffectSendLevel(mSendLevel);
2379 status_t status = mTrack->start();
2380 if (status == NO_ERROR) {
2381 mVolumeHandler->setStarted();
2382 }
2383 return status;
2384 }
2385 return NO_INIT;
2386 }
2387
setPlayerIId(int32_t playerIId)2388 void MediaPlayerService::AudioOutput::setPlayerIId(int32_t playerIId)
2389 {
2390 ALOGV("setPlayerIId(%d)", playerIId);
2391 Mutex::Autolock lock(mLock);
2392 mCachedPlayerIId = playerIId;
2393
2394 if (mTrack != nullptr) {
2395 mTrack->setPlayerIId(mCachedPlayerIId);
2396 }
2397 }
2398
setNextOutput(const sp<AudioOutput> & nextOutput)2399 void MediaPlayerService::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
2400 Mutex::Autolock lock(mLock);
2401 mNextOutput = nextOutput;
2402 }
2403
switchToNextOutput()2404 void MediaPlayerService::AudioOutput::switchToNextOutput() {
2405 ALOGV("switchToNextOutput");
2406
2407 // Try to acquire the callback lock before moving track (without incurring deadlock).
2408 const unsigned kMaxSwitchTries = 100;
2409 Mutex::Autolock lock(mLock);
2410 for (unsigned tries = 0;;) {
2411 if (mTrack == 0) {
2412 return;
2413 }
2414 if (mNextOutput != NULL && mNextOutput != this) {
2415 if (mCallbackData != NULL) {
2416 // two alternative approaches
2417 #if 1
2418 sp<CallbackData> callbackData = mCallbackData;
2419 mLock.unlock();
2420 // proper acquisition sequence
2421 callbackData->lock();
2422 mLock.lock();
2423 // Caution: it is unlikely that someone deleted our callback or changed our target
2424 if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) {
2425 // fatal if we are starved out.
2426 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
2427 "switchToNextOutput() cannot obtain correct lock sequence");
2428 callbackData->unlock();
2429 continue;
2430 }
2431 callbackData->mSwitching = true; // begin track switch
2432 callbackData->setOutput(NULL);
2433 #else
2434 // tryBeginTrackSwitch() returns false if the callback has the lock.
2435 if (!mCallbackData->tryBeginTrackSwitch()) {
2436 // fatal if we are starved out.
2437 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
2438 "switchToNextOutput() cannot obtain callback lock");
2439 mLock.unlock();
2440 usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput
2441 mLock.lock();
2442 continue;
2443 }
2444 #endif
2445 }
2446
2447 Mutex::Autolock nextLock(mNextOutput->mLock);
2448
2449 // If the next output track is not NULL, then it has been
2450 // opened already for playback.
2451 // This is possible even without the next player being started,
2452 // for example, the next player could be prepared and seeked.
2453 //
2454 // Presuming it isn't advisable to force the track over.
2455 if (mNextOutput->mTrack == nullptr) {
2456 ALOGD("Recycling track for gapless playback");
2457 mNextOutput->mCallbackData = mCallbackData;
2458 mNextOutput->mRecycledTrack = mTrack;
2459 mNextOutput->mSampleRateHz = mSampleRateHz;
2460 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
2461 mNextOutput->mFlags = mFlags;
2462 mNextOutput->mFrameSize = mFrameSize;
2463 close_l();
2464 mCallbackData.clear();
2465 } else {
2466 ALOGW("Ignoring gapless playback because next player has already started");
2467 // remove track in case resource needed for future players.
2468 if (mCallbackData != nullptr) {
2469 mCallbackData->endTrackSwitch(); // release lock for callbacks before close.
2470 }
2471 close_l();
2472 }
2473 }
2474 break;
2475 }
2476 }
2477
write(const void * buffer,size_t size,bool blocking)2478 ssize_t MediaPlayerService::AudioOutput::write(const void* buffer, size_t size, bool blocking)
2479 {
2480 Mutex::Autolock lock(mLock);
2481 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
2482
2483 //ALOGV("write(%p, %u)", buffer, size);
2484 if (mTrack != 0) {
2485 return mTrack->write(buffer, size, blocking);
2486 }
2487 return NO_INIT;
2488 }
2489
stop()2490 void MediaPlayerService::AudioOutput::stop()
2491 {
2492 ALOGV("stop");
2493 Mutex::Autolock lock(mLock);
2494 if (mTrack != 0) mTrack->stop();
2495 }
2496
flush()2497 void MediaPlayerService::AudioOutput::flush()
2498 {
2499 ALOGV("flush");
2500 Mutex::Autolock lock(mLock);
2501 if (mTrack != 0) mTrack->flush();
2502 }
2503
pause()2504 void MediaPlayerService::AudioOutput::pause()
2505 {
2506 ALOGV("pause");
2507 // We use pauseAndWait() instead of pause() to ensure tracks ramp to silence before
2508 // any flush. We choose 40 ms timeout to allow 1 deep buffer mixer period
2509 // to occur. Often waiting is 0 - 20 ms.
2510 using namespace std::chrono_literals;
2511 constexpr auto TIMEOUT_MS = 40ms;
2512 Mutex::Autolock lock(mLock);
2513 if (mTrack != 0) mTrack->pauseAndWait(TIMEOUT_MS);
2514 }
2515
close()2516 void MediaPlayerService::AudioOutput::close()
2517 {
2518 ALOGV("close");
2519 sp<AudioTrack> track;
2520 {
2521 Mutex::Autolock lock(mLock);
2522 track = mTrack;
2523 }
2524
2525 // do not hold lock while joining.
2526 if (track) {
2527 track->stopAndJoinCallbacks();
2528 }
2529
2530 {
2531 Mutex::Autolock lock(mLock);
2532 close_l(); // clears mTrack
2533 }
2534 // destruction of the track occurs outside of mutex.
2535 }
2536
setVolume(float left,float right)2537 void MediaPlayerService::AudioOutput::setVolume(float left, float right)
2538 {
2539 ALOGV("setVolume(%f, %f)", left, right);
2540 Mutex::Autolock lock(mLock);
2541 mLeftVolume = left;
2542 mRightVolume = right;
2543 if (mTrack != 0) {
2544 mTrack->setVolume(left, right);
2545 }
2546 }
2547
setPlaybackRate(const AudioPlaybackRate & rate)2548 status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
2549 {
2550 ALOGV("setPlaybackRate(%f %f %d %d)",
2551 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
2552 Mutex::Autolock lock(mLock);
2553 if (mTrack == 0) {
2554 // remember rate so that we can set it when the track is opened
2555 mPlaybackRate = rate;
2556 return OK;
2557 }
2558 status_t res = mTrack->setPlaybackRate(rate);
2559 if (res != NO_ERROR) {
2560 return res;
2561 }
2562 // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
2563 CHECK_GT(rate.mSpeed, 0.f);
2564 mPlaybackRate = rate;
2565 if (mSampleRateHz != 0) {
2566 mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
2567 }
2568 return res;
2569 }
2570
getPlaybackRate(AudioPlaybackRate * rate)2571 status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
2572 {
2573 ALOGV("setPlaybackRate");
2574 Mutex::Autolock lock(mLock);
2575 if (mTrack == 0) {
2576 return NO_INIT;
2577 }
2578 *rate = mTrack->getPlaybackRate();
2579 return NO_ERROR;
2580 }
2581
setAuxEffectSendLevel(float level)2582 status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
2583 {
2584 ALOGV("setAuxEffectSendLevel(%f)", level);
2585 Mutex::Autolock lock(mLock);
2586 mSendLevel = level;
2587 if (mTrack != 0) {
2588 return mTrack->setAuxEffectSendLevel(level);
2589 }
2590 return NO_ERROR;
2591 }
2592
attachAuxEffect(int effectId)2593 status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
2594 {
2595 ALOGV("attachAuxEffect(%d)", effectId);
2596 Mutex::Autolock lock(mLock);
2597 mAuxEffectId = effectId;
2598 if (mTrack != 0) {
2599 return mTrack->attachAuxEffect(effectId);
2600 }
2601 return NO_ERROR;
2602 }
2603
setOutputDevice(audio_port_handle_t deviceId)2604 status_t MediaPlayerService::AudioOutput::setOutputDevice(audio_port_handle_t deviceId)
2605 {
2606 ALOGV("setOutputDevice(%d)", deviceId);
2607 Mutex::Autolock lock(mLock);
2608 mSelectedDeviceId = deviceId;
2609 if (mTrack != 0) {
2610 return mTrack->setOutputDevice(deviceId);
2611 }
2612 return NO_ERROR;
2613 }
2614
getRoutedDeviceIds(DeviceIdVector & deviceIds)2615 status_t MediaPlayerService::AudioOutput::getRoutedDeviceIds(DeviceIdVector& deviceIds)
2616 {
2617 ALOGV("getRoutedDeviceIds");
2618 Mutex::Autolock lock(mLock);
2619 if (mTrack != 0) {
2620 mRoutedDeviceIds = mTrack->getRoutedDeviceIds();
2621 }
2622 deviceIds = mRoutedDeviceIds;
2623 return NO_ERROR;
2624 }
2625
enableAudioDeviceCallback(bool enabled)2626 status_t MediaPlayerService::AudioOutput::enableAudioDeviceCallback(bool enabled)
2627 {
2628 ALOGV("enableAudioDeviceCallback, %d", enabled);
2629 Mutex::Autolock lock(mLock);
2630 mDeviceCallbackEnabled = enabled;
2631 if (mTrack != 0) {
2632 status_t status;
2633 if (enabled) {
2634 status = mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
2635 } else {
2636 status = mTrack->removeAudioDeviceCallback(mDeviceCallback.promote());
2637 }
2638 return status;
2639 }
2640 return NO_ERROR;
2641 }
2642
applyVolumeShaper(const sp<VolumeShaper::Configuration> & configuration,const sp<VolumeShaper::Operation> & operation)2643 VolumeShaper::Status MediaPlayerService::AudioOutput::applyVolumeShaper(
2644 const sp<VolumeShaper::Configuration>& configuration,
2645 const sp<VolumeShaper::Operation>& operation)
2646 {
2647 Mutex::Autolock lock(mLock);
2648 ALOGV("AudioOutput::applyVolumeShaper");
2649
2650 if (configuration == nullptr) {
2651 ALOGE("AudioOutput::applyVolumeShaper Null configuration parameter");
2652 return VolumeShaper::Status(BAD_VALUE);
2653 }
2654
2655 if (operation == nullptr) {
2656 ALOGE("AudioOutput::applyVolumeShaper Null operation parameter");
2657 return VolumeShaper::Status(BAD_VALUE);
2658 }
2659
2660 mVolumeHandler->setIdIfNecessary(configuration);
2661
2662 VolumeShaper::Status status;
2663 if (mTrack != 0) {
2664 status = mTrack->applyVolumeShaper(configuration, operation);
2665 if (status >= 0) {
2666 (void)mVolumeHandler->applyVolumeShaper(configuration, operation);
2667 if (mTrack->isPlaying()) { // match local AudioTrack to properly restore.
2668 mVolumeHandler->setStarted();
2669 }
2670 }
2671 } else {
2672 // VolumeShapers are not affected when a track moves between players for
2673 // gapless playback (setNextMediaPlayer).
2674 // We forward VolumeShaper operations that do not change configuration
2675 // to the new player so that unducking may occur as expected.
2676 // Unducking is an idempotent operation, same if applied back-to-back.
2677 if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID
2678 && mNextOutput != nullptr) {
2679 ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s",
2680 configuration->toString().c_str(), operation->toString().c_str());
2681 Mutex::Autolock nextLock(mNextOutput->mLock);
2682
2683 // recycled track should be forwarded from this AudioSink by switchToNextOutput
2684 sp<AudioTrack> track = mNextOutput->mRecycledTrack;
2685 if (track != nullptr) {
2686 ALOGD("Forward VolumeShaper operation to recycled track %p", track.get());
2687 (void)track->applyVolumeShaper(configuration, operation);
2688 } else {
2689 // There is a small chance that the unduck occurs after the next
2690 // player has already started, but before it is registered to receive
2691 // the unduck command.
2692 track = mNextOutput->mTrack;
2693 if (track != nullptr) {
2694 ALOGD("Forward VolumeShaper operation to track %p", track.get());
2695 (void)track->applyVolumeShaper(configuration, operation);
2696 }
2697 }
2698 }
2699 status = mVolumeHandler->applyVolumeShaper(configuration, operation);
2700 }
2701 return status;
2702 }
2703
getVolumeShaperState(int id)2704 sp<VolumeShaper::State> MediaPlayerService::AudioOutput::getVolumeShaperState(int id)
2705 {
2706 Mutex::Autolock lock(mLock);
2707 if (mTrack != 0) {
2708 return mTrack->getVolumeShaperState(id);
2709 } else {
2710 return mVolumeHandler->getVolumeShaperState(id);
2711 }
2712 }
2713
onMoreData(const AudioTrack::Buffer & buffer)2714 size_t MediaPlayerService::AudioOutput::CallbackData::onMoreData(const AudioTrack::Buffer& buffer) {
2715 ALOGD("data callback");
2716 lock();
2717 sp<AudioOutput> me = getOutput();
2718 if (me == nullptr) {
2719 unlock();
2720 return 0;
2721 }
2722 size_t actualSize = (*me->mCallback)(
2723 me, buffer.data(), buffer.size(), me->mCallbackCookie,
2724 CB_EVENT_FILL_BUFFER);
2725
2726 // Log when no data is returned from the callback.
2727 // (1) We may have no data (especially with network streaming sources).
2728 // (2) We may have reached the EOS and the audio track is not stopped yet.
2729 // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
2730 // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill).
2731 //
2732 // This is a benign busy-wait, with the next data request generated 10 ms or more later;
2733 // nevertheless for power reasons, we don't want to see too many of these.
2734
2735 ALOGV_IF(actualSize == 0 && buffer.size() > 0, "callbackwrapper: empty buffer returned");
2736 unlock();
2737 return actualSize;
2738 }
2739
onStreamEnd()2740 void MediaPlayerService::AudioOutput::CallbackData::onStreamEnd() {
2741 lock();
2742 sp<AudioOutput> me = getOutput();
2743 if (me == nullptr) {
2744 unlock();
2745 return;
2746 }
2747 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
2748 (*me->mCallback)(me, nullptr /* buffer */, 0 /* size */,
2749 me->mCallbackCookie, CB_EVENT_STREAM_END);
2750 unlock();
2751 }
2752
2753
onNewIAudioTrack()2754 void MediaPlayerService::AudioOutput::CallbackData::onNewIAudioTrack() {
2755 lock();
2756 sp<AudioOutput> me = getOutput();
2757 if (me == nullptr) {
2758 unlock();
2759 return;
2760 }
2761 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
2762 (*me->mCallback)(me, nullptr /* buffer */, 0 /* size */,
2763 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
2764 unlock();
2765 }
2766
onUnderrun()2767 void MediaPlayerService::AudioOutput::CallbackData::onUnderrun() {
2768 // This occurs when there is no data available, typically
2769 // when there is a failure to supply data to the AudioTrack. It can also
2770 // occur in non-offloaded mode when the audio device comes out of standby.
2771 //
2772 // If an AudioTrack underruns it outputs silence. Since this happens suddenly
2773 // it may sound like an audible pop or glitch.
2774 //
2775 // The underrun event is sent once per track underrun; the condition is reset
2776 // when more data is sent to the AudioTrack.
2777 ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)");
2778
2779 }
2780
getSessionId() const2781 audio_session_t MediaPlayerService::AudioOutput::getSessionId() const
2782 {
2783 Mutex::Autolock lock(mLock);
2784 return mSessionId;
2785 }
2786
getSampleRate() const2787 uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
2788 {
2789 Mutex::Autolock lock(mLock);
2790 if (mTrack == 0) return 0;
2791 return mTrack->getSampleRate();
2792 }
2793
getBufferDurationInUs() const2794 int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const
2795 {
2796 Mutex::Autolock lock(mLock);
2797 if (mTrack == 0) {
2798 return 0;
2799 }
2800 int64_t duration;
2801 if (mTrack->getBufferDurationInUs(&duration) != OK) {
2802 return 0;
2803 }
2804 return duration;
2805 }
2806
2807 ////////////////////////////////////////////////////////////////////////////////
2808
2809 struct CallbackThread : public Thread {
2810 CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2811 MediaPlayerBase::AudioSink::AudioCallback cb,
2812 const wp<RefBase>& cookie);
2813
2814 protected:
2815 virtual ~CallbackThread();
2816
2817 virtual bool threadLoop();
2818
2819 private:
2820 wp<MediaPlayerBase::AudioSink> mSink;
2821 MediaPlayerBase::AudioSink::AudioCallback mCallback;
2822 wp<RefBase> mCookie;
2823 void *mBuffer;
2824 size_t mBufferSize;
2825
2826 CallbackThread(const CallbackThread &);
2827 CallbackThread &operator=(const CallbackThread &);
2828 };
2829
CallbackThread(const wp<MediaPlayerBase::AudioSink> & sink,MediaPlayerBase::AudioSink::AudioCallback cb,const wp<RefBase> & cookie)2830 CallbackThread::CallbackThread(
2831 const wp<MediaPlayerBase::AudioSink> &sink,
2832 MediaPlayerBase::AudioSink::AudioCallback cb,
2833 const wp<RefBase>& cookie)
2834 : mSink(sink),
2835 mCallback(cb),
2836 mCookie(cookie),
2837 mBuffer(NULL),
2838 mBufferSize(0) {
2839 }
2840
~CallbackThread()2841 CallbackThread::~CallbackThread() {
2842 if (mBuffer) {
2843 free(mBuffer);
2844 mBuffer = NULL;
2845 }
2846 }
2847
threadLoop()2848 bool CallbackThread::threadLoop() {
2849 sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2850 if (sink == NULL) {
2851 return false;
2852 }
2853
2854 if (mBuffer == NULL) {
2855 mBufferSize = sink->bufferSize();
2856 mBuffer = malloc(mBufferSize);
2857 }
2858
2859 size_t actualSize =
2860 (*mCallback)(sink, mBuffer, mBufferSize, mCookie,
2861 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
2862
2863 if (actualSize > 0) {
2864 sink->write(mBuffer, actualSize);
2865 // Could return false on sink->write() error or short count.
2866 // Not necessarily appropriate but would work for AudioCache behavior.
2867 }
2868
2869 return true;
2870 }
2871
2872 ////////////////////////////////////////////////////////////////////////////////
2873
addBatteryData(uint32_t params)2874 void MediaPlayerService::addBatteryData(uint32_t params) {
2875 mBatteryTracker.addBatteryData(params);
2876 }
2877
pullBatteryData(Parcel * reply)2878 status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2879 return mBatteryTracker.pullBatteryData(reply);
2880 }
2881
BatteryTracker()2882 MediaPlayerService::BatteryTracker::BatteryTracker() {
2883 mBatteryAudio.refCount = 0;
2884 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2885 mBatteryAudio.deviceOn[i] = 0;
2886 mBatteryAudio.lastTime[i] = 0;
2887 mBatteryAudio.totalTime[i] = 0;
2888 }
2889 // speaker is on by default
2890 mBatteryAudio.deviceOn[SPEAKER] = 1;
2891
2892 // reset battery stats
2893 // if the mediaserver has crashed, battery stats could be left
2894 // in bad state, reset the state upon service start.
2895 BatteryNotifier::getInstance().noteResetVideo();
2896 }
2897
addBatteryData(uint32_t params)2898 void MediaPlayerService::BatteryTracker::addBatteryData(uint32_t params)
2899 {
2900 Mutex::Autolock lock(mLock);
2901
2902 int32_t time = systemTime() / 1000000L;
2903
2904 // change audio output devices. This notification comes from AudioFlinger
2905 if ((params & kBatteryDataSpeakerOn)
2906 || (params & kBatteryDataOtherAudioDeviceOn)) {
2907
2908 int deviceOn[NUM_AUDIO_DEVICES];
2909 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2910 deviceOn[i] = 0;
2911 }
2912
2913 if ((params & kBatteryDataSpeakerOn)
2914 && (params & kBatteryDataOtherAudioDeviceOn)) {
2915 deviceOn[SPEAKER_AND_OTHER] = 1;
2916 } else if (params & kBatteryDataSpeakerOn) {
2917 deviceOn[SPEAKER] = 1;
2918 } else {
2919 deviceOn[OTHER_AUDIO_DEVICE] = 1;
2920 }
2921
2922 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2923 if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2924
2925 if (mBatteryAudio.refCount > 0) { // if playing audio
2926 if (!deviceOn[i]) {
2927 mBatteryAudio.lastTime[i] += time;
2928 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2929 mBatteryAudio.lastTime[i] = 0;
2930 } else {
2931 mBatteryAudio.lastTime[i] = 0 - time;
2932 }
2933 }
2934
2935 mBatteryAudio.deviceOn[i] = deviceOn[i];
2936 }
2937 }
2938 return;
2939 }
2940
2941 // an audio stream is started
2942 if (params & kBatteryDataAudioFlingerStart) {
2943 // record the start time only if currently no other audio
2944 // is being played
2945 if (mBatteryAudio.refCount == 0) {
2946 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2947 if (mBatteryAudio.deviceOn[i]) {
2948 mBatteryAudio.lastTime[i] -= time;
2949 }
2950 }
2951 }
2952
2953 mBatteryAudio.refCount ++;
2954 return;
2955
2956 } else if (params & kBatteryDataAudioFlingerStop) {
2957 if (mBatteryAudio.refCount <= 0) {
2958 ALOGW("Battery track warning: refCount is <= 0");
2959 return;
2960 }
2961
2962 // record the stop time only if currently this is the only
2963 // audio being played
2964 if (mBatteryAudio.refCount == 1) {
2965 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2966 if (mBatteryAudio.deviceOn[i]) {
2967 mBatteryAudio.lastTime[i] += time;
2968 mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2969 mBatteryAudio.lastTime[i] = 0;
2970 }
2971 }
2972 }
2973
2974 mBatteryAudio.refCount --;
2975 return;
2976 }
2977
2978 uid_t uid = IPCThreadState::self()->getCallingUid();
2979 if (uid == AID_MEDIA) {
2980 return;
2981 }
2982 int index = mBatteryData.indexOfKey(uid);
2983
2984 if (index < 0) { // create a new entry for this UID
2985 BatteryUsageInfo info;
2986 info.audioTotalTime = 0;
2987 info.videoTotalTime = 0;
2988 info.audioLastTime = 0;
2989 info.videoLastTime = 0;
2990 info.refCount = 0;
2991
2992 if (mBatteryData.add(uid, info) == NO_MEMORY) {
2993 ALOGE("Battery track error: no memory for new app");
2994 return;
2995 }
2996 }
2997
2998 BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2999
3000 if (params & kBatteryDataCodecStarted) {
3001 if (params & kBatteryDataTrackAudio) {
3002 info.audioLastTime -= time;
3003 info.refCount ++;
3004 }
3005 if (params & kBatteryDataTrackVideo) {
3006 info.videoLastTime -= time;
3007 info.refCount ++;
3008 }
3009 } else {
3010 if (info.refCount == 0) {
3011 ALOGW("Battery track warning: refCount is already 0");
3012 return;
3013 } else if (info.refCount < 0) {
3014 ALOGE("Battery track error: refCount < 0");
3015 mBatteryData.removeItem(uid);
3016 return;
3017 }
3018
3019 if (params & kBatteryDataTrackAudio) {
3020 info.audioLastTime += time;
3021 info.refCount --;
3022 }
3023 if (params & kBatteryDataTrackVideo) {
3024 info.videoLastTime += time;
3025 info.refCount --;
3026 }
3027
3028 // no stream is being played by this UID
3029 if (info.refCount == 0) {
3030 info.audioTotalTime += info.audioLastTime;
3031 info.audioLastTime = 0;
3032 info.videoTotalTime += info.videoLastTime;
3033 info.videoLastTime = 0;
3034 }
3035 }
3036 }
3037
pullBatteryData(Parcel * reply)3038 status_t MediaPlayerService::BatteryTracker::pullBatteryData(Parcel* reply) {
3039 Mutex::Autolock lock(mLock);
3040
3041 // audio output devices usage
3042 int32_t time = systemTime() / 1000000L; //in ms
3043 int32_t totalTime;
3044
3045 for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
3046 totalTime = mBatteryAudio.totalTime[i];
3047
3048 if (mBatteryAudio.deviceOn[i]
3049 && (mBatteryAudio.lastTime[i] != 0)) {
3050 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
3051 totalTime += tmpTime;
3052 }
3053
3054 reply->writeInt32(totalTime);
3055 // reset the total time
3056 mBatteryAudio.totalTime[i] = 0;
3057 }
3058
3059 // codec usage
3060 BatteryUsageInfo info;
3061 int size = mBatteryData.size();
3062
3063 reply->writeInt32(size);
3064 int i = 0;
3065
3066 while (i < size) {
3067 info = mBatteryData.valueAt(i);
3068
3069 reply->writeInt32(mBatteryData.keyAt(i)); //UID
3070 reply->writeInt32(info.audioTotalTime);
3071 reply->writeInt32(info.videoTotalTime);
3072
3073 info.audioTotalTime = 0;
3074 info.videoTotalTime = 0;
3075
3076 // remove the UID entry where no stream is being played
3077 if (info.refCount <= 0) {
3078 mBatteryData.removeItemsAt(i);
3079 size --;
3080 i --;
3081 }
3082 i++;
3083 }
3084 return NO_ERROR;
3085 }
3086
3087 #ifdef FUZZ_MODE_MEDIA_PLAYER_SERVICE
createForFuzzTesting()3088 sp<MediaPlayerService> MediaPlayerService::createForFuzzTesting() {
3089 return sp<MediaPlayerService>::make();
3090 }
3091 #endif // FUZZ_MODE_MEDIA_PLAYER_SERVICE
3092
3093 } // namespace android
3094