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