• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,audio_port_handle_t deviceId)857 void MediaPlayerService::Client::AudioDeviceUpdatedNotifier::onAudioDeviceUpdate(
858         audio_io_handle_t audioIo,
859         audio_port_handle_t deviceId) {
860     sp<MediaPlayerBase> listener = mListener.promote();
861     if (listener != NULL) {
862         listener->sendEvent(MEDIA_AUDIO_ROUTING_CHANGED, audioIo, deviceId);
863     } else {
864         ALOGW("listener for process %d death is gone", MEDIA_AUDIO_ROUTING_CHANGED);
865     }
866 }
867 
setDataSource_pre(player_type playerType)868 sp<MediaPlayerBase> MediaPlayerService::Client::setDataSource_pre(
869         player_type playerType)
870 {
871     ALOGV("player type = %d", playerType);
872 
873     // create the right type of player
874     sp<MediaPlayerBase> p = createPlayer(playerType);
875     if (p == NULL) {
876         return p;
877     }
878 
879     std::vector<DeathNotifier> deathNotifiers;
880 
881     // Listen to death of media.extractor service
882     sp<IServiceManager> sm = defaultServiceManager();
883     sp<IBinder> binder = sm->getService(String16("media.extractor"));
884     if (binder == NULL) {
885         ALOGE("extractor service not available");
886         return NULL;
887     }
888     deathNotifiers.emplace_back(
889             binder, [l = wp<MediaPlayerBase>(p)]() {
890         sp<MediaPlayerBase> listener = l.promote();
891         if (listener) {
892             ALOGI("media.extractor died. Sending death notification.");
893             listener->sendEvent(MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
894                                 MEDIAEXTRACTOR_PROCESS_DEATH);
895         } else {
896             ALOGW("media.extractor died without a death handler.");
897         }
898     });
899 
900     {
901         using ::android::hidl::base::V1_0::IBase;
902 
903         // Listen to death of OMX service
904         {
905             sp<IBase> base = ::android::hardware::media::omx::V1_0::
906                     IOmx::getService();
907             if (base == nullptr) {
908                 ALOGD("OMX service is not available");
909             } else {
910                 deathNotifiers.emplace_back(
911                         base, [l = wp<MediaPlayerBase>(p)]() {
912                     sp<MediaPlayerBase> listener = l.promote();
913                     if (listener) {
914                         ALOGI("OMX service died. "
915                               "Sending death notification.");
916                         listener->sendEvent(
917                                 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
918                                 MEDIACODEC_PROCESS_DEATH);
919                     } else {
920                         ALOGW("OMX service died without a death handler.");
921                     }
922                 });
923             }
924         }
925 
926         // Listen to death of Codec2 services
927         {
928             for (std::shared_ptr<Codec2Client> const& client :
929                     Codec2Client::CreateFromAllServices()) {
930                 sp<IBase> hidlBase = client->getHidlBase();
931                 ::ndk::SpAIBinder aidlBase = client->getAidlBase();
932                 auto onBinderDied = [l = wp<MediaPlayerBase>(p),
933                                      name = std::string(client->getServiceName())]() {
934                     sp<MediaPlayerBase> listener = l.promote();
935                     if (listener) {
936                         ALOGI("Codec2 service \"%s\" died. "
937                               "Sending death notification.",
938                               name.c_str());
939                         listener->sendEvent(
940                                 MEDIA_ERROR, MEDIA_ERROR_SERVER_DIED,
941                                 MEDIACODEC_PROCESS_DEATH);
942                     } else {
943                         ALOGW("Codec2 service \"%s\" died "
944                               "without a death handler.",
945                               name.c_str());
946                     }
947                 };
948                 if (hidlBase) {
949                     deathNotifiers.emplace_back(hidlBase, onBinderDied);
950                 } else if (aidlBase.get() != nullptr) {
951                     deathNotifiers.emplace_back(aidlBase, onBinderDied);
952                 }
953             }
954         }
955     }
956 
957     Mutex::Autolock lock(mLock);
958 
959     mDeathNotifiers.clear();
960     mDeathNotifiers.swap(deathNotifiers);
961     mAudioDeviceUpdatedListener = new AudioDeviceUpdatedNotifier(p);
962 
963     if (!p->hardwareOutput()) {
964         mAudioOutput = new AudioOutput(mAudioSessionId, mAttributionSource,
965                 mAudioAttributes, mAudioDeviceUpdatedListener);
966         static_cast<MediaPlayerInterface*>(p.get())->setAudioSink(mAudioOutput);
967     }
968 
969     return p;
970 }
971 
setDataSource_post(const sp<MediaPlayerBase> & p,status_t status)972 status_t MediaPlayerService::Client::setDataSource_post(
973         const sp<MediaPlayerBase>& p,
974         status_t status)
975 {
976     ALOGV(" setDataSource");
977     if (status != OK) {
978         ALOGE("  error: %d", status);
979         return status;
980     }
981 
982     // Set the re-transmission endpoint if one was chosen.
983     if (mRetransmitEndpointValid) {
984         status = p->setRetransmitEndpoint(&mRetransmitEndpoint);
985         if (status != NO_ERROR) {
986             ALOGE("setRetransmitEndpoint error: %d", status);
987         }
988     }
989 
990     if (status == OK) {
991         Mutex::Autolock lock(mLock);
992         mPlayer = p;
993     }
994     return status;
995 }
996 
setDataSource(const sp<IMediaHTTPService> & httpService,const char * url,const KeyedVector<String8,String8> * headers)997 status_t MediaPlayerService::Client::setDataSource(
998         const sp<IMediaHTTPService> &httpService,
999         const char *url,
1000         const KeyedVector<String8, String8> *headers)
1001 {
1002     ALOGV("setDataSource(%s)", url);
1003     if (url == NULL)
1004         return UNKNOWN_ERROR;
1005 
1006     if ((strncmp(url, "http://", 7) == 0) ||
1007         (strncmp(url, "https://", 8) == 0) ||
1008         (strncmp(url, "rtsp://", 7) == 0)) {
1009         if (!checkPermission("android.permission.INTERNET")) {
1010             return PERMISSION_DENIED;
1011         }
1012     }
1013 
1014     if (strncmp(url, "content://", 10) == 0) {
1015         // get a filedescriptor for the content Uri and
1016         // pass it to the setDataSource(fd) method
1017 
1018         String16 url16(url);
1019         int fd = android::openContentProviderFile(url16);
1020         if (fd < 0)
1021         {
1022             ALOGE("Couldn't open fd for %s", url);
1023             return UNKNOWN_ERROR;
1024         }
1025         status_t status = setDataSource(fd, 0, 0x7fffffffffLL); // this sets mStatus
1026         close(fd);
1027         return mStatus = status;
1028     } else {
1029         player_type playerType = MediaPlayerFactory::getPlayerType(this, url);
1030         sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1031         if (p == NULL) {
1032             return NO_INIT;
1033         }
1034 
1035         return mStatus =
1036                 setDataSource_post(
1037                 p, p->setDataSource(httpService, url, headers));
1038     }
1039 }
1040 
setDataSource(int fd,int64_t offset,int64_t length)1041 status_t MediaPlayerService::Client::setDataSource(int fd, int64_t offset, int64_t length)
1042 {
1043     ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
1044             fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
1045     struct stat sb;
1046     int ret = fstat(fd, &sb);
1047     if (ret != 0) {
1048         ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
1049         return UNKNOWN_ERROR;
1050     }
1051 
1052     ALOGV("st_dev  = %llu", static_cast<unsigned long long>(sb.st_dev));
1053     ALOGV("st_mode = %u", sb.st_mode);
1054     ALOGV("st_uid  = %lu", static_cast<unsigned long>(sb.st_uid));
1055     ALOGV("st_gid  = %lu", static_cast<unsigned long>(sb.st_gid));
1056     ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
1057 
1058     if (offset >= sb.st_size) {
1059         ALOGE("offset error");
1060         return UNKNOWN_ERROR;
1061     }
1062     if (offset + length > sb.st_size) {
1063         length = sb.st_size - offset;
1064         ALOGV("calculated length = %lld", (long long)length);
1065     }
1066 
1067     player_type playerType = MediaPlayerFactory::getPlayerType(this,
1068                                                                fd,
1069                                                                offset,
1070                                                                length);
1071     sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1072     if (p == NULL) {
1073         return NO_INIT;
1074     }
1075 
1076     // now set data source
1077     return mStatus = setDataSource_post(p, p->setDataSource(fd, offset, length));
1078 }
1079 
setDataSource(const sp<IStreamSource> & source)1080 status_t MediaPlayerService::Client::setDataSource(
1081         const sp<IStreamSource> &source) {
1082     // create the right type of player
1083     player_type playerType = MediaPlayerFactory::getPlayerType(this, source);
1084     sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1085     if (p == NULL) {
1086         return NO_INIT;
1087     }
1088 
1089     // now set data source
1090     return mStatus = setDataSource_post(p, p->setDataSource(source));
1091 }
1092 
setDataSource(const sp<IDataSource> & source)1093 status_t MediaPlayerService::Client::setDataSource(
1094         const sp<IDataSource> &source) {
1095     sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source);
1096     player_type playerType = MediaPlayerFactory::getPlayerType(this, dataSource);
1097     sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1098     if (p == NULL) {
1099         return NO_INIT;
1100     }
1101     // now set data source
1102     return mStatus = setDataSource_post(p, p->setDataSource(dataSource));
1103 }
1104 
setDataSource(const String8 & rtpParams)1105 status_t MediaPlayerService::Client::setDataSource(
1106         const String8& rtpParams) {
1107     player_type playerType = NU_PLAYER;
1108     sp<MediaPlayerBase> p = setDataSource_pre(playerType);
1109     if (p == NULL) {
1110         return NO_INIT;
1111     }
1112     // now set data source
1113     return mStatus = setDataSource_post(p, p->setDataSource(rtpParams));
1114 }
1115 
disconnectNativeWindow_l()1116 void MediaPlayerService::Client::disconnectNativeWindow_l() {
1117     if (mConnectedWindow != NULL) {
1118         status_t err = nativeWindowDisconnect(
1119                 mConnectedWindow.get(), "disconnectNativeWindow");
1120 
1121         if (err != OK) {
1122             ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
1123                     strerror(-err), err);
1124         }
1125     }
1126     mConnectedWindow.clear();
1127 }
1128 
setVideoSurfaceTexture(const sp<IGraphicBufferProducer> & bufferProducer)1129 status_t MediaPlayerService::Client::setVideoSurfaceTexture(
1130         const sp<IGraphicBufferProducer>& bufferProducer)
1131 {
1132     ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
1133     sp<MediaPlayerBase> p = getPlayer();
1134     if (p == 0) return UNKNOWN_ERROR;
1135 
1136     sp<IBinder> binder(IInterface::asBinder(bufferProducer));
1137     if (mConnectedWindowBinder == binder) {
1138         return OK;
1139     }
1140 
1141     sp<ANativeWindow> anw;
1142     if (bufferProducer != NULL) {
1143         anw = new Surface(bufferProducer, true /* controlledByApp */);
1144         status_t err = nativeWindowConnect(anw.get(), "setVideoSurfaceTexture");
1145 
1146         if (err != OK) {
1147             ALOGE("setVideoSurfaceTexture failed: %d", err);
1148             // Note that we must do the reset before disconnecting from the ANW.
1149             // Otherwise queue/dequeue calls could be made on the disconnected
1150             // ANW, which may result in errors.
1151             reset();
1152 
1153             Mutex::Autolock lock(mLock);
1154             disconnectNativeWindow_l();
1155 
1156             return err;
1157         }
1158     }
1159 
1160     // Note that we must set the player's new GraphicBufferProducer before
1161     // disconnecting the old one.  Otherwise queue/dequeue calls could be made
1162     // on the disconnected ANW, which may result in errors.
1163     status_t err = p->setVideoSurfaceTexture(bufferProducer);
1164 
1165     mLock.lock();
1166     disconnectNativeWindow_l();
1167 
1168     if (err == OK) {
1169         mConnectedWindow = anw;
1170         mConnectedWindowBinder = binder;
1171         mLock.unlock();
1172     } else {
1173         mLock.unlock();
1174         status_t err = nativeWindowDisconnect(
1175                 anw.get(), "disconnectNativeWindow");
1176 
1177         if (err != OK) {
1178             ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
1179                     strerror(-err), err);
1180         }
1181     }
1182 
1183     return err;
1184 }
1185 
invoke(const Parcel & request,Parcel * reply)1186 status_t MediaPlayerService::Client::invoke(const Parcel& request,
1187                                             Parcel *reply)
1188 {
1189     sp<MediaPlayerBase> p = getPlayer();
1190     if (p == NULL) return UNKNOWN_ERROR;
1191     return p->invoke(request, reply);
1192 }
1193 
1194 // This call doesn't need to access the native player.
setMetadataFilter(const Parcel & filter)1195 status_t MediaPlayerService::Client::setMetadataFilter(const Parcel& filter)
1196 {
1197     status_t status;
1198     media::Metadata::Filter allow, drop;
1199 
1200     if (unmarshallFilter(filter, &allow, &status) &&
1201         unmarshallFilter(filter, &drop, &status)) {
1202         Mutex::Autolock lock(mLock);
1203 
1204         mMetadataAllow = allow;
1205         mMetadataDrop = drop;
1206     }
1207     return status;
1208 }
1209 
getMetadata(bool update_only,bool,Parcel * reply)1210 status_t MediaPlayerService::Client::getMetadata(
1211         bool update_only, bool /*apply_filter*/, Parcel *reply)
1212 {
1213     sp<MediaPlayerBase> player = getPlayer();
1214     if (player == 0) return UNKNOWN_ERROR;
1215 
1216     status_t status;
1217     // Placeholder for the return code, updated by the caller.
1218     reply->writeInt32(-1);
1219 
1220     media::Metadata::Filter ids;
1221 
1222     // We don't block notifications while we fetch the data. We clear
1223     // mMetadataUpdated first so we don't lose notifications happening
1224     // during the rest of this call.
1225     {
1226         Mutex::Autolock lock(mLock);
1227         if (update_only) {
1228             ids = mMetadataUpdated;
1229         }
1230         mMetadataUpdated.clear();
1231     }
1232 
1233     media::Metadata metadata(reply);
1234 
1235     metadata.appendHeader();
1236     status = player->getMetadata(ids, reply);
1237 
1238     if (status != OK) {
1239         metadata.resetParcel();
1240         ALOGE("getMetadata failed %d", status);
1241         return status;
1242     }
1243 
1244     // FIXME: Implement filtering on the result. Not critical since
1245     // filtering takes place on the update notifications already. This
1246     // would be when all the metadata are fetch and a filter is set.
1247 
1248     // Everything is fine, update the metadata length.
1249     metadata.updateLength();
1250     return OK;
1251 }
1252 
setBufferingSettings(const BufferingSettings & buffering)1253 status_t MediaPlayerService::Client::setBufferingSettings(
1254         const BufferingSettings& buffering)
1255 {
1256     ALOGV("[%d] setBufferingSettings{%s}",
1257             mConnId, buffering.toString().c_str());
1258     sp<MediaPlayerBase> p = getPlayer();
1259     if (p == 0) return UNKNOWN_ERROR;
1260     return p->setBufferingSettings(buffering);
1261 }
1262 
getBufferingSettings(BufferingSettings * buffering)1263 status_t MediaPlayerService::Client::getBufferingSettings(
1264         BufferingSettings* buffering /* nonnull */)
1265 {
1266     sp<MediaPlayerBase> p = getPlayer();
1267     // TODO: create mPlayer on demand.
1268     if (p == 0) return UNKNOWN_ERROR;
1269     status_t ret = p->getBufferingSettings(buffering);
1270     if (ret == NO_ERROR) {
1271         ALOGV("[%d] getBufferingSettings{%s}",
1272                 mConnId, buffering->toString().c_str());
1273     } else {
1274         ALOGE("[%d] getBufferingSettings returned %d", mConnId, ret);
1275     }
1276     return ret;
1277 }
1278 
prepareAsync()1279 status_t MediaPlayerService::Client::prepareAsync()
1280 {
1281     ALOGV("[%d] prepareAsync", mConnId);
1282     sp<MediaPlayerBase> p = getPlayer();
1283     if (p == 0) return UNKNOWN_ERROR;
1284     status_t ret = p->prepareAsync();
1285 #if CALLBACK_ANTAGONIZER
1286     ALOGD("start Antagonizer");
1287     if (ret == NO_ERROR) mAntagonizer->start();
1288 #endif
1289     return ret;
1290 }
1291 
start()1292 status_t MediaPlayerService::Client::start()
1293 {
1294     ALOGV("[%d] start", mConnId);
1295     sp<MediaPlayerBase> p = getPlayer();
1296     if (p == 0) return UNKNOWN_ERROR;
1297     p->setLooping(mLoop);
1298     return p->start();
1299 }
1300 
stop()1301 status_t MediaPlayerService::Client::stop()
1302 {
1303     ALOGV("[%d] stop", mConnId);
1304     sp<MediaPlayerBase> p = getPlayer();
1305     if (p == 0) return UNKNOWN_ERROR;
1306     return p->stop();
1307 }
1308 
pause()1309 status_t MediaPlayerService::Client::pause()
1310 {
1311     ALOGV("[%d] pause", mConnId);
1312     sp<MediaPlayerBase> p = getPlayer();
1313     if (p == 0) return UNKNOWN_ERROR;
1314     return p->pause();
1315 }
1316 
isPlaying(bool * state)1317 status_t MediaPlayerService::Client::isPlaying(bool* state)
1318 {
1319     *state = false;
1320     sp<MediaPlayerBase> p = getPlayer();
1321     if (p == 0) return UNKNOWN_ERROR;
1322     *state = p->isPlaying();
1323     ALOGV("[%d] isPlaying: %d", mConnId, *state);
1324     return NO_ERROR;
1325 }
1326 
setPlaybackSettings(const AudioPlaybackRate & rate)1327 status_t MediaPlayerService::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
1328 {
1329     ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
1330             mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
1331     sp<MediaPlayerBase> p = getPlayer();
1332     if (p == 0) return UNKNOWN_ERROR;
1333     return p->setPlaybackSettings(rate);
1334 }
1335 
getPlaybackSettings(AudioPlaybackRate * rate)1336 status_t MediaPlayerService::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
1337 {
1338     sp<MediaPlayerBase> p = getPlayer();
1339     if (p == 0) return UNKNOWN_ERROR;
1340     status_t ret = p->getPlaybackSettings(rate);
1341     if (ret == NO_ERROR) {
1342         ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
1343                 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
1344     } else {
1345         ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
1346     }
1347     return ret;
1348 }
1349 
setSyncSettings(const AVSyncSettings & sync,float videoFpsHint)1350 status_t MediaPlayerService::Client::setSyncSettings(
1351         const AVSyncSettings& sync, float videoFpsHint)
1352 {
1353     ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
1354             mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
1355     sp<MediaPlayerBase> p = getPlayer();
1356     if (p == 0) return UNKNOWN_ERROR;
1357     return p->setSyncSettings(sync, videoFpsHint);
1358 }
1359 
getSyncSettings(AVSyncSettings * sync,float * videoFps)1360 status_t MediaPlayerService::Client::getSyncSettings(
1361         AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
1362 {
1363     sp<MediaPlayerBase> p = getPlayer();
1364     if (p == 0) return UNKNOWN_ERROR;
1365     status_t ret = p->getSyncSettings(sync, videoFps);
1366     if (ret == NO_ERROR) {
1367         ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
1368                 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
1369     } else {
1370         ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
1371     }
1372     return ret;
1373 }
1374 
getCurrentPosition(int * msec)1375 status_t MediaPlayerService::Client::getCurrentPosition(int *msec)
1376 {
1377     ALOGV("getCurrentPosition");
1378     sp<MediaPlayerBase> p = getPlayer();
1379     if (p == 0) return UNKNOWN_ERROR;
1380     status_t ret = p->getCurrentPosition(msec);
1381     if (ret == NO_ERROR) {
1382         ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
1383     } else {
1384         ALOGE("getCurrentPosition returned %d", ret);
1385     }
1386     return ret;
1387 }
1388 
getDuration(int * msec)1389 status_t MediaPlayerService::Client::getDuration(int *msec)
1390 {
1391     ALOGV("getDuration");
1392     sp<MediaPlayerBase> p = getPlayer();
1393     if (p == 0) return UNKNOWN_ERROR;
1394     status_t ret = p->getDuration(msec);
1395     if (ret == NO_ERROR) {
1396         ALOGV("[%d] getDuration = %d", mConnId, *msec);
1397     } else {
1398         ALOGE("getDuration returned %d", ret);
1399     }
1400     return ret;
1401 }
1402 
setNextPlayer(const sp<IMediaPlayer> & player)1403 status_t MediaPlayerService::Client::setNextPlayer(const sp<IMediaPlayer>& player) {
1404     ALOGV("setNextPlayer");
1405     Mutex::Autolock l(mLock);
1406     sp<Client> c = static_cast<Client*>(player.get());
1407     if (c != NULL && !mService->hasClient(c)) {
1408       return BAD_VALUE;
1409     }
1410 
1411     mNextClient = c;
1412 
1413     if (c != NULL) {
1414         if (mAudioOutput != NULL) {
1415             mAudioOutput->setNextOutput(c->mAudioOutput);
1416         } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1417             ALOGE("no current audio output");
1418         }
1419 
1420         if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1421             mPlayer->setNextPlayer(mNextClient->getPlayer());
1422         }
1423     }
1424 
1425     return OK;
1426 }
1427 
applyVolumeShaper(const sp<VolumeShaper::Configuration> & configuration,const sp<VolumeShaper::Operation> & operation)1428 VolumeShaper::Status MediaPlayerService::Client::applyVolumeShaper(
1429         const sp<VolumeShaper::Configuration>& configuration,
1430         const sp<VolumeShaper::Operation>& operation) {
1431     // for hardware output, call player instead
1432     ALOGV("Client::applyVolumeShaper(%p)", this);
1433     sp<MediaPlayerBase> p = getPlayer();
1434     {
1435         Mutex::Autolock l(mLock);
1436         if (p != 0 && p->hardwareOutput()) {
1437             // TODO: investigate internal implementation
1438             return VolumeShaper::Status(INVALID_OPERATION);
1439         }
1440         if (mAudioOutput.get() != nullptr) {
1441             return mAudioOutput->applyVolumeShaper(configuration, operation);
1442         }
1443     }
1444     return VolumeShaper::Status(INVALID_OPERATION);
1445 }
1446 
getVolumeShaperState(int id)1447 sp<VolumeShaper::State> MediaPlayerService::Client::getVolumeShaperState(int id) {
1448     // for hardware output, call player instead
1449     ALOGV("Client::getVolumeShaperState(%p)", this);
1450     sp<MediaPlayerBase> p = getPlayer();
1451     {
1452         Mutex::Autolock l(mLock);
1453         if (p != 0 && p->hardwareOutput()) {
1454             // TODO: investigate internal implementation.
1455             return nullptr;
1456         }
1457         if (mAudioOutput.get() != nullptr) {
1458             return mAudioOutput->getVolumeShaperState(id);
1459         }
1460     }
1461     return nullptr;
1462 }
1463 
seekTo(int msec,MediaPlayerSeekMode mode)1464 status_t MediaPlayerService::Client::seekTo(int msec, MediaPlayerSeekMode mode)
1465 {
1466     ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode);
1467     sp<MediaPlayerBase> p = getPlayer();
1468     if (p == 0) return UNKNOWN_ERROR;
1469     return p->seekTo(msec, mode);
1470 }
1471 
reset()1472 status_t MediaPlayerService::Client::reset()
1473 {
1474     ALOGV("[%d] reset", mConnId);
1475     mRetransmitEndpointValid = false;
1476     sp<MediaPlayerBase> p = getPlayer();
1477     if (p == 0) return UNKNOWN_ERROR;
1478     return p->reset();
1479 }
1480 
notifyAt(int64_t mediaTimeUs)1481 status_t MediaPlayerService::Client::notifyAt(int64_t mediaTimeUs)
1482 {
1483     ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs);
1484     sp<MediaPlayerBase> p = getPlayer();
1485     if (p == 0) return UNKNOWN_ERROR;
1486     return p->notifyAt(mediaTimeUs);
1487 }
1488 
setAudioStreamType(audio_stream_type_t type)1489 status_t MediaPlayerService::Client::setAudioStreamType(audio_stream_type_t type)
1490 {
1491     ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1492     // TODO: for hardware output, call player instead
1493     Mutex::Autolock l(mLock);
1494     if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1495     return NO_ERROR;
1496 }
1497 
setAudioAttributes_l(const Parcel & parcel)1498 status_t MediaPlayerService::Client::setAudioAttributes_l(const Parcel &parcel)
1499 {
1500     if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1501     mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1502     if (mAudioAttributes == NULL) {
1503         return NO_MEMORY;
1504     }
1505     unmarshallAudioAttributes(parcel, mAudioAttributes);
1506 
1507     ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1508             mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1509             mAudioAttributes->tags);
1510 
1511     if (mAudioOutput != 0) {
1512         mAudioOutput->setAudioAttributes(mAudioAttributes);
1513     }
1514     return NO_ERROR;
1515 }
1516 
setLooping(int loop)1517 status_t MediaPlayerService::Client::setLooping(int loop)
1518 {
1519     ALOGV("[%d] setLooping(%d)", mConnId, loop);
1520     mLoop = loop;
1521     sp<MediaPlayerBase> p = getPlayer();
1522     if (p != 0) return p->setLooping(loop);
1523     return NO_ERROR;
1524 }
1525 
setVolume(float leftVolume,float rightVolume)1526 status_t MediaPlayerService::Client::setVolume(float leftVolume, float rightVolume)
1527 {
1528     ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1529 
1530     // for hardware output, call player instead
1531     sp<MediaPlayerBase> p = getPlayer();
1532     {
1533       Mutex::Autolock l(mLock);
1534       if (p != 0 && p->hardwareOutput()) {
1535           MediaPlayerHWInterface* hwp =
1536                   reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1537           return hwp->setVolume(leftVolume, rightVolume);
1538       } else {
1539           if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1540           return NO_ERROR;
1541       }
1542     }
1543 
1544     return NO_ERROR;
1545 }
1546 
setAuxEffectSendLevel(float level)1547 status_t MediaPlayerService::Client::setAuxEffectSendLevel(float level)
1548 {
1549     ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
1550     Mutex::Autolock l(mLock);
1551     if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1552     return NO_ERROR;
1553 }
1554 
attachAuxEffect(int effectId)1555 status_t MediaPlayerService::Client::attachAuxEffect(int effectId)
1556 {
1557     ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
1558     Mutex::Autolock l(mLock);
1559     if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1560     return NO_ERROR;
1561 }
1562 
setParameter(int key,const Parcel & request)1563 status_t MediaPlayerService::Client::setParameter(int key, const Parcel &request) {
1564     ALOGV("[%d] setParameter(%d)", mConnId, key);
1565     switch (key) {
1566     case KEY_PARAMETER_AUDIO_ATTRIBUTES:
1567     {
1568         Mutex::Autolock l(mLock);
1569         return setAudioAttributes_l(request);
1570     }
1571     default:
1572         sp<MediaPlayerBase> p = getPlayer();
1573         if (p == 0) { return UNKNOWN_ERROR; }
1574         return p->setParameter(key, request);
1575     }
1576 }
1577 
getParameter(int key,Parcel * reply)1578 status_t MediaPlayerService::Client::getParameter(int key, Parcel *reply) {
1579     ALOGV("[%d] getParameter(%d)", mConnId, key);
1580     sp<MediaPlayerBase> p = getPlayer();
1581     if (p == 0) return UNKNOWN_ERROR;
1582     return p->getParameter(key, reply);
1583 }
1584 
setRetransmitEndpoint(const struct sockaddr_in * endpoint)1585 status_t MediaPlayerService::Client::setRetransmitEndpoint(
1586         const struct sockaddr_in* endpoint) {
1587 
1588     if (NULL != endpoint) {
1589         uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1590         uint16_t p = ntohs(endpoint->sin_port);
1591         ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1592                 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1593     } else {
1594         ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1595     }
1596 
1597     sp<MediaPlayerBase> p = getPlayer();
1598 
1599     // Right now, the only valid time to set a retransmit endpoint is before
1600     // player selection has been made (since the presence or absence of a
1601     // retransmit endpoint is going to determine which player is selected during
1602     // setDataSource).
1603     if (p != 0) return INVALID_OPERATION;
1604 
1605     if (NULL != endpoint) {
1606         Mutex::Autolock lock(mLock);
1607         mRetransmitEndpoint = *endpoint;
1608         mRetransmitEndpointValid = true;
1609     } else {
1610         Mutex::Autolock lock(mLock);
1611         mRetransmitEndpointValid = false;
1612     }
1613 
1614     return NO_ERROR;
1615 }
1616 
getRetransmitEndpoint(struct sockaddr_in * endpoint)1617 status_t MediaPlayerService::Client::getRetransmitEndpoint(
1618         struct sockaddr_in* endpoint)
1619 {
1620     if (NULL == endpoint)
1621         return BAD_VALUE;
1622 
1623     sp<MediaPlayerBase> p = getPlayer();
1624 
1625     if (p != NULL)
1626         return p->getRetransmitEndpoint(endpoint);
1627 
1628     Mutex::Autolock lock(mLock);
1629     if (!mRetransmitEndpointValid)
1630         return NO_INIT;
1631 
1632     *endpoint = mRetransmitEndpoint;
1633 
1634     return NO_ERROR;
1635 }
1636 
notify(int msg,int ext1,int ext2,const Parcel * obj)1637 void MediaPlayerService::Client::notify(
1638         int msg, int ext1, int ext2, const Parcel *obj)
1639 {
1640     sp<IMediaPlayerClient> c;
1641     sp<Client> nextClient;
1642     status_t errStartNext = NO_ERROR;
1643     {
1644         Mutex::Autolock l(mLock);
1645         c = mClient;
1646         if (msg == MEDIA_PLAYBACK_COMPLETE && mNextClient != NULL) {
1647             nextClient = mNextClient;
1648 
1649             if (mAudioOutput != NULL)
1650                 mAudioOutput->switchToNextOutput();
1651 
1652             errStartNext = nextClient->start();
1653         }
1654     }
1655 
1656     if (nextClient != NULL) {
1657         sp<IMediaPlayerClient> nc;
1658         {
1659             Mutex::Autolock l(nextClient->mLock);
1660             nc = nextClient->mClient;
1661         }
1662         if (nc != NULL) {
1663             if (errStartNext == NO_ERROR) {
1664                 nc->notify(MEDIA_INFO, MEDIA_INFO_STARTED_AS_NEXT, 0, obj);
1665             } else {
1666                 nc->notify(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN , 0, obj);
1667                 ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext);
1668             }
1669         }
1670     }
1671 
1672     if (MEDIA_INFO == msg &&
1673         MEDIA_INFO_METADATA_UPDATE == ext1) {
1674         const media::Metadata::Type metadata_type = ext2;
1675 
1676         if(shouldDropMetadata(metadata_type)) {
1677             return;
1678         }
1679 
1680         // Update the list of metadata that have changed. getMetadata
1681         // also access mMetadataUpdated and clears it.
1682         addNewMetadataUpdate(metadata_type);
1683     }
1684 
1685     if (c != NULL) {
1686         ALOGV("[%d] notify (%d, %d, %d)", mConnId, msg, ext1, ext2);
1687         c->notify(msg, ext1, ext2, obj);
1688     }
1689 }
1690 
1691 
shouldDropMetadata(media::Metadata::Type code) const1692 bool MediaPlayerService::Client::shouldDropMetadata(media::Metadata::Type code) const
1693 {
1694     Mutex::Autolock lock(mLock);
1695 
1696     if (findMetadata(mMetadataDrop, code)) {
1697         return true;
1698     }
1699 
1700     if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
1701         return false;
1702     } else {
1703         return true;
1704     }
1705 }
1706 
1707 
addNewMetadataUpdate(media::Metadata::Type metadata_type)1708 void MediaPlayerService::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
1709     Mutex::Autolock lock(mLock);
1710     if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1711         mMetadataUpdated.add(metadata_type);
1712     }
1713 }
1714 
1715 // Modular DRM
prepareDrm(const uint8_t uuid[16],const Vector<uint8_t> & drmSessionId)1716 status_t MediaPlayerService::Client::prepareDrm(const uint8_t uuid[16],
1717         const Vector<uint8_t>& drmSessionId)
1718 {
1719     ALOGV("[%d] prepareDrm", mConnId);
1720     sp<MediaPlayerBase> p = getPlayer();
1721     if (p == 0) return UNKNOWN_ERROR;
1722 
1723     status_t ret = p->prepareDrm(uuid, drmSessionId);
1724     ALOGV("prepareDrm ret: %d", ret);
1725 
1726     return ret;
1727 }
1728 
releaseDrm()1729 status_t MediaPlayerService::Client::releaseDrm()
1730 {
1731     ALOGV("[%d] releaseDrm", mConnId);
1732     sp<MediaPlayerBase> p = getPlayer();
1733     if (p == 0) return UNKNOWN_ERROR;
1734 
1735     status_t ret = p->releaseDrm();
1736     ALOGV("releaseDrm ret: %d", ret);
1737 
1738     return ret;
1739 }
1740 
setOutputDevice(audio_port_handle_t deviceId)1741 status_t MediaPlayerService::Client::setOutputDevice(audio_port_handle_t deviceId)
1742 {
1743     ALOGV("[%d] setOutputDevice", mConnId);
1744     {
1745         Mutex::Autolock l(mLock);
1746         if (mAudioOutput.get() != nullptr) {
1747             return mAudioOutput->setOutputDevice(deviceId);
1748         }
1749     }
1750     return NO_INIT;
1751 }
1752 
getRoutedDeviceId(audio_port_handle_t * deviceId)1753 status_t MediaPlayerService::Client::getRoutedDeviceId(audio_port_handle_t* deviceId)
1754 {
1755     ALOGV("[%d] getRoutedDeviceId", mConnId);
1756     {
1757         Mutex::Autolock l(mLock);
1758         if (mAudioOutput.get() != nullptr) {
1759             return mAudioOutput->getRoutedDeviceId(deviceId);
1760         }
1761     }
1762     return NO_INIT;
1763 }
1764 
enableAudioDeviceCallback(bool enabled)1765 status_t MediaPlayerService::Client::enableAudioDeviceCallback(bool enabled)
1766 {
1767     ALOGV("[%d] enableAudioDeviceCallback, %d", mConnId, enabled);
1768     {
1769         Mutex::Autolock l(mLock);
1770         if (mAudioOutput.get() != nullptr) {
1771             return mAudioOutput->enableAudioDeviceCallback(enabled);
1772         }
1773     }
1774     return NO_INIT;
1775 }
1776 
1777 #if CALLBACK_ANTAGONIZER
1778 const int Antagonizer::interval = 10000; // 10 msecs
1779 
Antagonizer(const sp<MediaPlayerBase::Listener> & listener)1780 Antagonizer::Antagonizer(const sp<MediaPlayerBase::Listener> &listener) :
1781     mExit(false), mActive(false), mListener(listener)
1782 {
1783     createThread(callbackThread, this);
1784 }
1785 
kill()1786 void Antagonizer::kill()
1787 {
1788     Mutex::Autolock _l(mLock);
1789     mActive = false;
1790     mExit = true;
1791     mCondition.wait(mLock);
1792 }
1793 
callbackThread(void * user)1794 int Antagonizer::callbackThread(void* user)
1795 {
1796     ALOGD("Antagonizer started");
1797     Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1798     while (!p->mExit) {
1799         if (p->mActive) {
1800             ALOGV("send event");
1801             p->mListener->notify(0, 0, 0, 0);
1802         }
1803         usleep(interval);
1804     }
1805     Mutex::Autolock _l(p->mLock);
1806     p->mCondition.signal();
1807     ALOGD("Antagonizer stopped");
1808     return 0;
1809 }
1810 #endif
1811 
1812 #undef LOG_TAG
1813 #define LOG_TAG "AudioSink"
AudioOutput(audio_session_t sessionId,const AttributionSourceState & attributionSource,const audio_attributes_t * attr,const sp<AudioSystem::AudioDeviceCallback> & deviceCallback)1814 MediaPlayerService::AudioOutput::AudioOutput(audio_session_t sessionId,
1815         const AttributionSourceState& attributionSource, const audio_attributes_t* attr,
1816         const sp<AudioSystem::AudioDeviceCallback>& deviceCallback)
1817     : mCachedPlayerIId(PLAYER_PIID_INVALID),
1818       mCallback(NULL),
1819       mCallbackCookie(NULL),
1820       mCallbackData(NULL),
1821       mStreamType(AUDIO_STREAM_MUSIC),
1822       mLeftVolume(1.0),
1823       mRightVolume(1.0),
1824       mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
1825       mSampleRateHz(0),
1826       mMsecsPerFrame(0),
1827       mFrameSize(0),
1828       mSessionId(sessionId),
1829       mAttributionSource(attributionSource),
1830       mSendLevel(0.0),
1831       mAuxEffectId(0),
1832       mFlags(AUDIO_OUTPUT_FLAG_NONE),
1833       mVolumeHandler(new media::VolumeHandler()),
1834       mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
1835       mRoutedDeviceId(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,void * 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, void *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         close_l(); // clears mTrack
2518     }
2519     // destruction of the track occurs outside of mutex.
2520 }
2521 
setVolume(float left,float right)2522 void MediaPlayerService::AudioOutput::setVolume(float left, float right)
2523 {
2524     ALOGV("setVolume(%f, %f)", left, right);
2525     Mutex::Autolock lock(mLock);
2526     mLeftVolume = left;
2527     mRightVolume = right;
2528     if (mTrack != 0) {
2529         mTrack->setVolume(left, right);
2530     }
2531 }
2532 
setPlaybackRate(const AudioPlaybackRate & rate)2533 status_t MediaPlayerService::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
2534 {
2535     ALOGV("setPlaybackRate(%f %f %d %d)",
2536                 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
2537     Mutex::Autolock lock(mLock);
2538     if (mTrack == 0) {
2539         // remember rate so that we can set it when the track is opened
2540         mPlaybackRate = rate;
2541         return OK;
2542     }
2543     status_t res = mTrack->setPlaybackRate(rate);
2544     if (res != NO_ERROR) {
2545         return res;
2546     }
2547     // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
2548     CHECK_GT(rate.mSpeed, 0.f);
2549     mPlaybackRate = rate;
2550     if (mSampleRateHz != 0) {
2551         mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
2552     }
2553     return res;
2554 }
2555 
getPlaybackRate(AudioPlaybackRate * rate)2556 status_t MediaPlayerService::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
2557 {
2558     ALOGV("setPlaybackRate");
2559     Mutex::Autolock lock(mLock);
2560     if (mTrack == 0) {
2561         return NO_INIT;
2562     }
2563     *rate = mTrack->getPlaybackRate();
2564     return NO_ERROR;
2565 }
2566 
setAuxEffectSendLevel(float level)2567 status_t MediaPlayerService::AudioOutput::setAuxEffectSendLevel(float level)
2568 {
2569     ALOGV("setAuxEffectSendLevel(%f)", level);
2570     Mutex::Autolock lock(mLock);
2571     mSendLevel = level;
2572     if (mTrack != 0) {
2573         return mTrack->setAuxEffectSendLevel(level);
2574     }
2575     return NO_ERROR;
2576 }
2577 
attachAuxEffect(int effectId)2578 status_t MediaPlayerService::AudioOutput::attachAuxEffect(int effectId)
2579 {
2580     ALOGV("attachAuxEffect(%d)", effectId);
2581     Mutex::Autolock lock(mLock);
2582     mAuxEffectId = effectId;
2583     if (mTrack != 0) {
2584         return mTrack->attachAuxEffect(effectId);
2585     }
2586     return NO_ERROR;
2587 }
2588 
setOutputDevice(audio_port_handle_t deviceId)2589 status_t MediaPlayerService::AudioOutput::setOutputDevice(audio_port_handle_t deviceId)
2590 {
2591     ALOGV("setOutputDevice(%d)", deviceId);
2592     Mutex::Autolock lock(mLock);
2593     mSelectedDeviceId = deviceId;
2594     if (mTrack != 0) {
2595         return mTrack->setOutputDevice(deviceId);
2596     }
2597     return NO_ERROR;
2598 }
2599 
getRoutedDeviceId(audio_port_handle_t * deviceId)2600 status_t MediaPlayerService::AudioOutput::getRoutedDeviceId(audio_port_handle_t* deviceId)
2601 {
2602     ALOGV("getRoutedDeviceId");
2603     Mutex::Autolock lock(mLock);
2604     if (mTrack != 0) {
2605         mRoutedDeviceId = mTrack->getRoutedDeviceId();
2606     }
2607     *deviceId = mRoutedDeviceId;
2608     return NO_ERROR;
2609 }
2610 
enableAudioDeviceCallback(bool enabled)2611 status_t MediaPlayerService::AudioOutput::enableAudioDeviceCallback(bool enabled)
2612 {
2613     ALOGV("enableAudioDeviceCallback, %d", enabled);
2614     Mutex::Autolock lock(mLock);
2615     mDeviceCallbackEnabled = enabled;
2616     if (mTrack != 0) {
2617         status_t status;
2618         if (enabled) {
2619             status = mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
2620         } else {
2621             status = mTrack->removeAudioDeviceCallback(mDeviceCallback.promote());
2622         }
2623         return status;
2624     }
2625     return NO_ERROR;
2626 }
2627 
applyVolumeShaper(const sp<VolumeShaper::Configuration> & configuration,const sp<VolumeShaper::Operation> & operation)2628 VolumeShaper::Status MediaPlayerService::AudioOutput::applyVolumeShaper(
2629                 const sp<VolumeShaper::Configuration>& configuration,
2630                 const sp<VolumeShaper::Operation>& operation)
2631 {
2632     Mutex::Autolock lock(mLock);
2633     ALOGV("AudioOutput::applyVolumeShaper");
2634 
2635     if (configuration == nullptr) {
2636         ALOGE("AudioOutput::applyVolumeShaper Null configuration parameter");
2637         return VolumeShaper::Status(BAD_VALUE);
2638     }
2639 
2640     if (operation == nullptr) {
2641         ALOGE("AudioOutput::applyVolumeShaper Null operation parameter");
2642         return VolumeShaper::Status(BAD_VALUE);
2643     }
2644 
2645     mVolumeHandler->setIdIfNecessary(configuration);
2646 
2647     VolumeShaper::Status status;
2648     if (mTrack != 0) {
2649         status = mTrack->applyVolumeShaper(configuration, operation);
2650         if (status >= 0) {
2651             (void)mVolumeHandler->applyVolumeShaper(configuration, operation);
2652             if (mTrack->isPlaying()) { // match local AudioTrack to properly restore.
2653                 mVolumeHandler->setStarted();
2654             }
2655         }
2656     } else {
2657         // VolumeShapers are not affected when a track moves between players for
2658         // gapless playback (setNextMediaPlayer).
2659         // We forward VolumeShaper operations that do not change configuration
2660         // to the new player so that unducking may occur as expected.
2661         // Unducking is an idempotent operation, same if applied back-to-back.
2662         if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID
2663                 && mNextOutput != nullptr) {
2664             ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s",
2665                     configuration->toString().c_str(), operation->toString().c_str());
2666             Mutex::Autolock nextLock(mNextOutput->mLock);
2667 
2668             // recycled track should be forwarded from this AudioSink by switchToNextOutput
2669             sp<AudioTrack> track = mNextOutput->mRecycledTrack;
2670             if (track != nullptr) {
2671                 ALOGD("Forward VolumeShaper operation to recycled track %p", track.get());
2672                 (void)track->applyVolumeShaper(configuration, operation);
2673             } else {
2674                 // There is a small chance that the unduck occurs after the next
2675                 // player has already started, but before it is registered to receive
2676                 // the unduck command.
2677                 track = mNextOutput->mTrack;
2678                 if (track != nullptr) {
2679                     ALOGD("Forward VolumeShaper operation to track %p", track.get());
2680                     (void)track->applyVolumeShaper(configuration, operation);
2681                 }
2682             }
2683         }
2684         status = mVolumeHandler->applyVolumeShaper(configuration, operation);
2685     }
2686     return status;
2687 }
2688 
getVolumeShaperState(int id)2689 sp<VolumeShaper::State> MediaPlayerService::AudioOutput::getVolumeShaperState(int id)
2690 {
2691     Mutex::Autolock lock(mLock);
2692     if (mTrack != 0) {
2693         return mTrack->getVolumeShaperState(id);
2694     } else {
2695         return mVolumeHandler->getVolumeShaperState(id);
2696     }
2697 }
2698 
onMoreData(const AudioTrack::Buffer & buffer)2699 size_t MediaPlayerService::AudioOutput::CallbackData::onMoreData(const AudioTrack::Buffer& buffer) {
2700     ALOGD("data callback");
2701     lock();
2702     sp<AudioOutput> me = getOutput();
2703     if (me == nullptr) {
2704         unlock();
2705         return 0;
2706     }
2707     size_t actualSize = (*me->mCallback)(
2708             me.get(), buffer.data(), buffer.size(), me->mCallbackCookie,
2709             CB_EVENT_FILL_BUFFER);
2710 
2711     // Log when no data is returned from the callback.
2712     // (1) We may have no data (especially with network streaming sources).
2713     // (2) We may have reached the EOS and the audio track is not stopped yet.
2714     // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
2715     // NuPlayerRenderer will return zero when it doesn't have data (it doesn't block to fill).
2716     //
2717     // This is a benign busy-wait, with the next data request generated 10 ms or more later;
2718     // nevertheless for power reasons, we don't want to see too many of these.
2719 
2720     ALOGV_IF(actualSize == 0 && buffer.size() > 0, "callbackwrapper: empty buffer returned");
2721     unlock();
2722     return actualSize;
2723 }
2724 
onStreamEnd()2725 void MediaPlayerService::AudioOutput::CallbackData::onStreamEnd() {
2726     lock();
2727     sp<AudioOutput> me = getOutput();
2728     if (me == nullptr) {
2729         unlock();
2730         return;
2731     }
2732     ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
2733     (*me->mCallback)(me.get(), NULL /* buffer */, 0 /* size */,
2734             me->mCallbackCookie, CB_EVENT_STREAM_END);
2735     unlock();
2736 }
2737 
2738 
onNewIAudioTrack()2739 void MediaPlayerService::AudioOutput::CallbackData::onNewIAudioTrack() {
2740     lock();
2741     sp<AudioOutput> me = getOutput();
2742     if (me == nullptr) {
2743         unlock();
2744         return;
2745     }
2746     ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
2747     (*me->mCallback)(me.get(),  NULL /* buffer */, 0 /* size */,
2748             me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
2749     unlock();
2750 }
2751 
onUnderrun()2752 void MediaPlayerService::AudioOutput::CallbackData::onUnderrun() {
2753     // This occurs when there is no data available, typically
2754     // when there is a failure to supply data to the AudioTrack.  It can also
2755     // occur in non-offloaded mode when the audio device comes out of standby.
2756     //
2757     // If an AudioTrack underruns it outputs silence. Since this happens suddenly
2758     // it may sound like an audible pop or glitch.
2759     //
2760     // The underrun event is sent once per track underrun; the condition is reset
2761     // when more data is sent to the AudioTrack.
2762     ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)");
2763 
2764 }
2765 
getSessionId() const2766 audio_session_t MediaPlayerService::AudioOutput::getSessionId() const
2767 {
2768     Mutex::Autolock lock(mLock);
2769     return mSessionId;
2770 }
2771 
getSampleRate() const2772 uint32_t MediaPlayerService::AudioOutput::getSampleRate() const
2773 {
2774     Mutex::Autolock lock(mLock);
2775     if (mTrack == 0) return 0;
2776     return mTrack->getSampleRate();
2777 }
2778 
getBufferDurationInUs() const2779 int64_t MediaPlayerService::AudioOutput::getBufferDurationInUs() const
2780 {
2781     Mutex::Autolock lock(mLock);
2782     if (mTrack == 0) {
2783         return 0;
2784     }
2785     int64_t duration;
2786     if (mTrack->getBufferDurationInUs(&duration) != OK) {
2787         return 0;
2788     }
2789     return duration;
2790 }
2791 
2792 ////////////////////////////////////////////////////////////////////////////////
2793 
2794 struct CallbackThread : public Thread {
2795     CallbackThread(const wp<MediaPlayerBase::AudioSink> &sink,
2796                    MediaPlayerBase::AudioSink::AudioCallback cb,
2797                    void *cookie);
2798 
2799 protected:
2800     virtual ~CallbackThread();
2801 
2802     virtual bool threadLoop();
2803 
2804 private:
2805     wp<MediaPlayerBase::AudioSink> mSink;
2806     MediaPlayerBase::AudioSink::AudioCallback mCallback;
2807     void *mCookie;
2808     void *mBuffer;
2809     size_t mBufferSize;
2810 
2811     CallbackThread(const CallbackThread &);
2812     CallbackThread &operator=(const CallbackThread &);
2813 };
2814 
CallbackThread(const wp<MediaPlayerBase::AudioSink> & sink,MediaPlayerBase::AudioSink::AudioCallback cb,void * cookie)2815 CallbackThread::CallbackThread(
2816         const wp<MediaPlayerBase::AudioSink> &sink,
2817         MediaPlayerBase::AudioSink::AudioCallback cb,
2818         void *cookie)
2819     : mSink(sink),
2820       mCallback(cb),
2821       mCookie(cookie),
2822       mBuffer(NULL),
2823       mBufferSize(0) {
2824 }
2825 
~CallbackThread()2826 CallbackThread::~CallbackThread() {
2827     if (mBuffer) {
2828         free(mBuffer);
2829         mBuffer = NULL;
2830     }
2831 }
2832 
threadLoop()2833 bool CallbackThread::threadLoop() {
2834     sp<MediaPlayerBase::AudioSink> sink = mSink.promote();
2835     if (sink == NULL) {
2836         return false;
2837     }
2838 
2839     if (mBuffer == NULL) {
2840         mBufferSize = sink->bufferSize();
2841         mBuffer = malloc(mBufferSize);
2842     }
2843 
2844     size_t actualSize =
2845         (*mCallback)(sink.get(), mBuffer, mBufferSize, mCookie,
2846                 MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER);
2847 
2848     if (actualSize > 0) {
2849         sink->write(mBuffer, actualSize);
2850         // Could return false on sink->write() error or short count.
2851         // Not necessarily appropriate but would work for AudioCache behavior.
2852     }
2853 
2854     return true;
2855 }
2856 
2857 ////////////////////////////////////////////////////////////////////////////////
2858 
addBatteryData(uint32_t params)2859 void MediaPlayerService::addBatteryData(uint32_t params) {
2860     mBatteryTracker.addBatteryData(params);
2861 }
2862 
pullBatteryData(Parcel * reply)2863 status_t MediaPlayerService::pullBatteryData(Parcel* reply) {
2864     return mBatteryTracker.pullBatteryData(reply);
2865 }
2866 
BatteryTracker()2867 MediaPlayerService::BatteryTracker::BatteryTracker() {
2868     mBatteryAudio.refCount = 0;
2869     for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2870         mBatteryAudio.deviceOn[i] = 0;
2871         mBatteryAudio.lastTime[i] = 0;
2872         mBatteryAudio.totalTime[i] = 0;
2873     }
2874     // speaker is on by default
2875     mBatteryAudio.deviceOn[SPEAKER] = 1;
2876 
2877     // reset battery stats
2878     // if the mediaserver has crashed, battery stats could be left
2879     // in bad state, reset the state upon service start.
2880     BatteryNotifier::getInstance().noteResetVideo();
2881 }
2882 
addBatteryData(uint32_t params)2883 void MediaPlayerService::BatteryTracker::addBatteryData(uint32_t params)
2884 {
2885     Mutex::Autolock lock(mLock);
2886 
2887     int32_t time = systemTime() / 1000000L;
2888 
2889     // change audio output devices. This notification comes from AudioFlinger
2890     if ((params & kBatteryDataSpeakerOn)
2891             || (params & kBatteryDataOtherAudioDeviceOn)) {
2892 
2893         int deviceOn[NUM_AUDIO_DEVICES];
2894         for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2895             deviceOn[i] = 0;
2896         }
2897 
2898         if ((params & kBatteryDataSpeakerOn)
2899                 && (params & kBatteryDataOtherAudioDeviceOn)) {
2900             deviceOn[SPEAKER_AND_OTHER] = 1;
2901         } else if (params & kBatteryDataSpeakerOn) {
2902             deviceOn[SPEAKER] = 1;
2903         } else {
2904             deviceOn[OTHER_AUDIO_DEVICE] = 1;
2905         }
2906 
2907         for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2908             if (mBatteryAudio.deviceOn[i] != deviceOn[i]){
2909 
2910                 if (mBatteryAudio.refCount > 0) { // if playing audio
2911                     if (!deviceOn[i]) {
2912                         mBatteryAudio.lastTime[i] += time;
2913                         mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2914                         mBatteryAudio.lastTime[i] = 0;
2915                     } else {
2916                         mBatteryAudio.lastTime[i] = 0 - time;
2917                     }
2918                 }
2919 
2920                 mBatteryAudio.deviceOn[i] = deviceOn[i];
2921             }
2922         }
2923         return;
2924     }
2925 
2926     // an audio stream is started
2927     if (params & kBatteryDataAudioFlingerStart) {
2928         // record the start time only if currently no other audio
2929         // is being played
2930         if (mBatteryAudio.refCount == 0) {
2931             for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2932                 if (mBatteryAudio.deviceOn[i]) {
2933                     mBatteryAudio.lastTime[i] -= time;
2934                 }
2935             }
2936         }
2937 
2938         mBatteryAudio.refCount ++;
2939         return;
2940 
2941     } else if (params & kBatteryDataAudioFlingerStop) {
2942         if (mBatteryAudio.refCount <= 0) {
2943             ALOGW("Battery track warning: refCount is <= 0");
2944             return;
2945         }
2946 
2947         // record the stop time only if currently this is the only
2948         // audio being played
2949         if (mBatteryAudio.refCount == 1) {
2950             for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
2951                 if (mBatteryAudio.deviceOn[i]) {
2952                     mBatteryAudio.lastTime[i] += time;
2953                     mBatteryAudio.totalTime[i] += mBatteryAudio.lastTime[i];
2954                     mBatteryAudio.lastTime[i] = 0;
2955                 }
2956             }
2957         }
2958 
2959         mBatteryAudio.refCount --;
2960         return;
2961     }
2962 
2963     uid_t uid = IPCThreadState::self()->getCallingUid();
2964     if (uid == AID_MEDIA) {
2965         return;
2966     }
2967     int index = mBatteryData.indexOfKey(uid);
2968 
2969     if (index < 0) { // create a new entry for this UID
2970         BatteryUsageInfo info;
2971         info.audioTotalTime = 0;
2972         info.videoTotalTime = 0;
2973         info.audioLastTime = 0;
2974         info.videoLastTime = 0;
2975         info.refCount = 0;
2976 
2977         if (mBatteryData.add(uid, info) == NO_MEMORY) {
2978             ALOGE("Battery track error: no memory for new app");
2979             return;
2980         }
2981     }
2982 
2983     BatteryUsageInfo &info = mBatteryData.editValueFor(uid);
2984 
2985     if (params & kBatteryDataCodecStarted) {
2986         if (params & kBatteryDataTrackAudio) {
2987             info.audioLastTime -= time;
2988             info.refCount ++;
2989         }
2990         if (params & kBatteryDataTrackVideo) {
2991             info.videoLastTime -= time;
2992             info.refCount ++;
2993         }
2994     } else {
2995         if (info.refCount == 0) {
2996             ALOGW("Battery track warning: refCount is already 0");
2997             return;
2998         } else if (info.refCount < 0) {
2999             ALOGE("Battery track error: refCount < 0");
3000             mBatteryData.removeItem(uid);
3001             return;
3002         }
3003 
3004         if (params & kBatteryDataTrackAudio) {
3005             info.audioLastTime += time;
3006             info.refCount --;
3007         }
3008         if (params & kBatteryDataTrackVideo) {
3009             info.videoLastTime += time;
3010             info.refCount --;
3011         }
3012 
3013         // no stream is being played by this UID
3014         if (info.refCount == 0) {
3015             info.audioTotalTime += info.audioLastTime;
3016             info.audioLastTime = 0;
3017             info.videoTotalTime += info.videoLastTime;
3018             info.videoLastTime = 0;
3019         }
3020     }
3021 }
3022 
pullBatteryData(Parcel * reply)3023 status_t MediaPlayerService::BatteryTracker::pullBatteryData(Parcel* reply) {
3024     Mutex::Autolock lock(mLock);
3025 
3026     // audio output devices usage
3027     int32_t time = systemTime() / 1000000L; //in ms
3028     int32_t totalTime;
3029 
3030     for (int i = 0; i < NUM_AUDIO_DEVICES; i++) {
3031         totalTime = mBatteryAudio.totalTime[i];
3032 
3033         if (mBatteryAudio.deviceOn[i]
3034             && (mBatteryAudio.lastTime[i] != 0)) {
3035                 int32_t tmpTime = mBatteryAudio.lastTime[i] + time;
3036                 totalTime += tmpTime;
3037         }
3038 
3039         reply->writeInt32(totalTime);
3040         // reset the total time
3041         mBatteryAudio.totalTime[i] = 0;
3042    }
3043 
3044     // codec usage
3045     BatteryUsageInfo info;
3046     int size = mBatteryData.size();
3047 
3048     reply->writeInt32(size);
3049     int i = 0;
3050 
3051     while (i < size) {
3052         info = mBatteryData.valueAt(i);
3053 
3054         reply->writeInt32(mBatteryData.keyAt(i)); //UID
3055         reply->writeInt32(info.audioTotalTime);
3056         reply->writeInt32(info.videoTotalTime);
3057 
3058         info.audioTotalTime = 0;
3059         info.videoTotalTime = 0;
3060 
3061         // remove the UID entry where no stream is being played
3062         if (info.refCount <= 0) {
3063             mBatteryData.removeItemsAt(i);
3064             size --;
3065             i --;
3066         }
3067         i++;
3068     }
3069     return NO_ERROR;
3070 }
3071 
3072 #ifdef FUZZ_MODE_MEDIA_PLAYER_SERVICE
createForFuzzTesting()3073 sp<MediaPlayerService> MediaPlayerService::createForFuzzTesting() {
3074     return sp<MediaPlayerService>::make();
3075 }
3076 #endif // FUZZ_MODE_MEDIA_PLAYER_SERVICE
3077 
3078 } // namespace android
3079