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