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