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