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