blob: 720c1e373ce6a4b16166b242eab66c67e43f023c [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(
Wei Jiac5c79da2017-12-21 18:03:05 -0800729 const sp<DataSource> &source) {
730 player2_type playerType = MediaPlayer2Factory::getPlayerType(this, source);
Wei Jia53692fa2017-12-11 10:33:46 -0800731 sp<MediaPlayer2Base> p = setDataSource_pre(playerType);
732 if (p == NULL) {
733 return NO_INIT;
734 }
735 // now set data source
Wei Jiac5c79da2017-12-21 18:03:05 -0800736 return mStatus = setDataSource_post(p, p->setDataSource(source));
Wei Jia53692fa2017-12-11 10:33:46 -0800737}
738
739void MediaPlayer2Manager::Client::disconnectNativeWindow_l() {
Wei Jia28288fb2017-12-15 13:45:29 -0800740 if (mConnectedWindow != NULL && mConnectedWindow->getANativeWindow() != NULL) {
Wei Jia53692fa2017-12-11 10:33:46 -0800741 status_t err = nativeWindowDisconnect(
Wei Jia28288fb2017-12-15 13:45:29 -0800742 mConnectedWindow->getANativeWindow(), "disconnectNativeWindow");
Wei Jia53692fa2017-12-11 10:33:46 -0800743
744 if (err != OK) {
745 ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
746 strerror(-err), err);
747 }
748 }
749 mConnectedWindow.clear();
750}
751
752status_t MediaPlayer2Manager::Client::setVideoSurfaceTexture(
Wei Jia28288fb2017-12-15 13:45:29 -0800753 const sp<ANativeWindowWrapper>& nww)
Wei Jia53692fa2017-12-11 10:33:46 -0800754{
Wei Jia28288fb2017-12-15 13:45:29 -0800755 ALOGV("[%d] setVideoSurfaceTexture(%p)",
756 mConnId,
757 (nww == NULL ? NULL : nww->getANativeWindow()));
Wei Jia53692fa2017-12-11 10:33:46 -0800758 sp<MediaPlayer2Base> p = getPlayer();
759 if (p == 0) return UNKNOWN_ERROR;
760
Wei Jia28288fb2017-12-15 13:45:29 -0800761 if (nww != NULL && nww->getANativeWindow() != NULL) {
762 if (mConnectedWindow != NULL
763 && mConnectedWindow->getANativeWindow() == nww->getANativeWindow()) {
764 return OK;
765 }
766 status_t err = nativeWindowConnect(nww->getANativeWindow(), "setVideoSurfaceTexture");
Wei Jia53692fa2017-12-11 10:33:46 -0800767
768 if (err != OK) {
769 ALOGE("setVideoSurfaceTexture failed: %d", err);
770 // Note that we must do the reset before disconnecting from the ANW.
771 // Otherwise queue/dequeue calls could be made on the disconnected
772 // ANW, which may result in errors.
773 reset();
774
775 Mutex::Autolock lock(mLock);
776 disconnectNativeWindow_l();
777
778 return err;
779 }
780 }
781
782 // Note that we must set the player's new GraphicBufferProducer before
783 // disconnecting the old one. Otherwise queue/dequeue calls could be made
784 // on the disconnected ANW, which may result in errors.
Wei Jia28288fb2017-12-15 13:45:29 -0800785 status_t err = p->setVideoSurfaceTexture(nww);
Wei Jia53692fa2017-12-11 10:33:46 -0800786
787 mLock.lock();
788 disconnectNativeWindow_l();
789
790 if (err == OK) {
Wei Jia28288fb2017-12-15 13:45:29 -0800791 mConnectedWindow = nww;
Wei Jia53692fa2017-12-11 10:33:46 -0800792 mLock.unlock();
Wei Jia28288fb2017-12-15 13:45:29 -0800793 } else if (nww != NULL) {
Wei Jia53692fa2017-12-11 10:33:46 -0800794 mLock.unlock();
795 status_t err = nativeWindowDisconnect(
Wei Jia28288fb2017-12-15 13:45:29 -0800796 nww->getANativeWindow(), "disconnectNativeWindow");
Wei Jia53692fa2017-12-11 10:33:46 -0800797
798 if (err != OK) {
799 ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
800 strerror(-err), err);
801 }
802 }
803
804 return err;
805}
806
807status_t MediaPlayer2Manager::Client::invoke(const Parcel& request,
808 Parcel *reply)
809{
810 sp<MediaPlayer2Base> p = getPlayer();
811 if (p == NULL) return UNKNOWN_ERROR;
812 return p->invoke(request, reply);
813}
814
815// This call doesn't need to access the native player.
816status_t MediaPlayer2Manager::Client::setMetadataFilter(const Parcel& filter)
817{
818 status_t status;
819 media::Metadata::Filter allow, drop;
820
821 if (unmarshallFilter(filter, &allow, &status) &&
822 unmarshallFilter(filter, &drop, &status)) {
823 Mutex::Autolock lock(mLock);
824
825 mMetadataAllow = allow;
826 mMetadataDrop = drop;
827 }
828 return status;
829}
830
831status_t MediaPlayer2Manager::Client::getMetadata(
832 bool update_only, bool /*apply_filter*/, Parcel *reply)
833{
834 sp<MediaPlayer2Base> player = getPlayer();
835 if (player == 0) return UNKNOWN_ERROR;
836
837 status_t status;
838 // Placeholder for the return code, updated by the caller.
839 reply->writeInt32(-1);
840
841 media::Metadata::Filter ids;
842
843 // We don't block notifications while we fetch the data. We clear
844 // mMetadataUpdated first so we don't lose notifications happening
845 // during the rest of this call.
846 {
847 Mutex::Autolock lock(mLock);
848 if (update_only) {
849 ids = mMetadataUpdated;
850 }
851 mMetadataUpdated.clear();
852 }
853
854 media::Metadata metadata(reply);
855
856 metadata.appendHeader();
857 status = player->getMetadata(ids, reply);
858
859 if (status != OK) {
860 metadata.resetParcel();
861 ALOGE("getMetadata failed %d", status);
862 return status;
863 }
864
865 // FIXME: ement filtering on the result. Not critical since
866 // filtering takes place on the update notifications already. This
867 // would be when all the metadata are fetch and a filter is set.
868
869 // Everything is fine, update the metadata length.
870 metadata.updateLength();
871 return OK;
872}
873
874status_t MediaPlayer2Manager::Client::setBufferingSettings(
875 const BufferingSettings& buffering)
876{
877 ALOGV("[%d] setBufferingSettings{%s}",
878 mConnId, buffering.toString().string());
879 sp<MediaPlayer2Base> p = getPlayer();
880 if (p == 0) return UNKNOWN_ERROR;
881 return p->setBufferingSettings(buffering);
882}
883
884status_t MediaPlayer2Manager::Client::getBufferingSettings(
885 BufferingSettings* buffering /* nonnull */)
886{
887 sp<MediaPlayer2Base> p = getPlayer();
888 // TODO: create mPlayer on demand.
889 if (p == 0) return UNKNOWN_ERROR;
890 status_t ret = p->getBufferingSettings(buffering);
891 if (ret == NO_ERROR) {
892 ALOGV("[%d] getBufferingSettings{%s}",
893 mConnId, buffering->toString().string());
894 } else {
895 ALOGE("[%d] getBufferingSettings returned %d", mConnId, ret);
896 }
897 return ret;
898}
899
900status_t MediaPlayer2Manager::Client::prepareAsync()
901{
902 ALOGV("[%d] prepareAsync", mConnId);
903 sp<MediaPlayer2Base> p = getPlayer();
904 if (p == 0) return UNKNOWN_ERROR;
905 status_t ret = p->prepareAsync();
906#if CALLBACK_ANTAGONIZER
907 ALOGD("start Antagonizer");
908 if (ret == NO_ERROR) mAntagonizer->start();
909#endif
910 return ret;
911}
912
913status_t MediaPlayer2Manager::Client::start()
914{
915 ALOGV("[%d] start", mConnId);
916 sp<MediaPlayer2Base> p = getPlayer();
917 if (p == 0) return UNKNOWN_ERROR;
918 p->setLooping(mLoop);
919 return p->start();
920}
921
922status_t MediaPlayer2Manager::Client::stop()
923{
924 ALOGV("[%d] stop", mConnId);
925 sp<MediaPlayer2Base> p = getPlayer();
926 if (p == 0) return UNKNOWN_ERROR;
927 return p->stop();
928}
929
930status_t MediaPlayer2Manager::Client::pause()
931{
932 ALOGV("[%d] pause", mConnId);
933 sp<MediaPlayer2Base> p = getPlayer();
934 if (p == 0) return UNKNOWN_ERROR;
935 return p->pause();
936}
937
938status_t MediaPlayer2Manager::Client::isPlaying(bool* state)
939{
940 *state = false;
941 sp<MediaPlayer2Base> p = getPlayer();
942 if (p == 0) return UNKNOWN_ERROR;
943 *state = p->isPlaying();
944 ALOGV("[%d] isPlaying: %d", mConnId, *state);
945 return NO_ERROR;
946}
947
948status_t MediaPlayer2Manager::Client::setPlaybackSettings(const AudioPlaybackRate& rate)
949{
950 ALOGV("[%d] setPlaybackSettings(%f, %f, %d, %d)",
951 mConnId, rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
952 sp<MediaPlayer2Base> p = getPlayer();
953 if (p == 0) return UNKNOWN_ERROR;
954 return p->setPlaybackSettings(rate);
955}
956
957status_t MediaPlayer2Manager::Client::getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */)
958{
959 sp<MediaPlayer2Base> p = getPlayer();
960 if (p == 0) return UNKNOWN_ERROR;
961 status_t ret = p->getPlaybackSettings(rate);
962 if (ret == NO_ERROR) {
963 ALOGV("[%d] getPlaybackSettings(%f, %f, %d, %d)",
964 mConnId, rate->mSpeed, rate->mPitch, rate->mFallbackMode, rate->mStretchMode);
965 } else {
966 ALOGV("[%d] getPlaybackSettings returned %d", mConnId, ret);
967 }
968 return ret;
969}
970
971status_t MediaPlayer2Manager::Client::setSyncSettings(
972 const AVSyncSettings& sync, float videoFpsHint)
973{
974 ALOGV("[%d] setSyncSettings(%u, %u, %f, %f)",
975 mConnId, sync.mSource, sync.mAudioAdjustMode, sync.mTolerance, videoFpsHint);
976 sp<MediaPlayer2Base> p = getPlayer();
977 if (p == 0) return UNKNOWN_ERROR;
978 return p->setSyncSettings(sync, videoFpsHint);
979}
980
981status_t MediaPlayer2Manager::Client::getSyncSettings(
982 AVSyncSettings* sync /* nonnull */, float* videoFps /* nonnull */)
983{
984 sp<MediaPlayer2Base> p = getPlayer();
985 if (p == 0) return UNKNOWN_ERROR;
986 status_t ret = p->getSyncSettings(sync, videoFps);
987 if (ret == NO_ERROR) {
988 ALOGV("[%d] getSyncSettings(%u, %u, %f, %f)",
989 mConnId, sync->mSource, sync->mAudioAdjustMode, sync->mTolerance, *videoFps);
990 } else {
991 ALOGV("[%d] getSyncSettings returned %d", mConnId, ret);
992 }
993 return ret;
994}
995
996status_t MediaPlayer2Manager::Client::getCurrentPosition(int *msec)
997{
998 ALOGV("getCurrentPosition");
999 sp<MediaPlayer2Base> p = getPlayer();
1000 if (p == 0) return UNKNOWN_ERROR;
1001 status_t ret = p->getCurrentPosition(msec);
1002 if (ret == NO_ERROR) {
1003 ALOGV("[%d] getCurrentPosition = %d", mConnId, *msec);
1004 } else {
1005 ALOGE("getCurrentPosition returned %d", ret);
1006 }
1007 return ret;
1008}
1009
1010status_t MediaPlayer2Manager::Client::getDuration(int *msec)
1011{
1012 ALOGV("getDuration");
1013 sp<MediaPlayer2Base> p = getPlayer();
1014 if (p == 0) return UNKNOWN_ERROR;
1015 status_t ret = p->getDuration(msec);
1016 if (ret == NO_ERROR) {
1017 ALOGV("[%d] getDuration = %d", mConnId, *msec);
1018 } else {
1019 ALOGE("getDuration returned %d", ret);
1020 }
1021 return ret;
1022}
1023
1024status_t MediaPlayer2Manager::Client::setNextPlayer(const sp<MediaPlayer2Engine>& player) {
1025 ALOGV("setNextPlayer");
1026 Mutex::Autolock l(mLock);
1027 sp<Client> c = static_cast<Client*>(player.get());
1028 if (c != NULL && !gMediaPlayer2Manager.hasClient(c)) {
1029 return BAD_VALUE;
1030 }
1031
1032 mNextClient = c;
1033
1034 if (c != NULL) {
1035 if (mAudioOutput != NULL) {
1036 mAudioOutput->setNextOutput(c->mAudioOutput);
1037 } else if ((mPlayer != NULL) && !mPlayer->hardwareOutput()) {
1038 ALOGE("no current audio output");
1039 }
1040
1041 if ((mPlayer != NULL) && (mNextClient->getPlayer() != NULL)) {
1042 mPlayer->setNextPlayer(mNextClient->getPlayer());
1043 }
1044 }
1045
1046 return OK;
1047}
1048
1049VolumeShaper::Status MediaPlayer2Manager::Client::applyVolumeShaper(
1050 const sp<VolumeShaper::Configuration>& configuration,
1051 const sp<VolumeShaper::Operation>& operation) {
1052 // for hardware output, call player instead
1053 ALOGV("Client::applyVolumeShaper(%p)", this);
1054 sp<MediaPlayer2Base> p = getPlayer();
1055 {
1056 Mutex::Autolock l(mLock);
1057 if (p != 0 && p->hardwareOutput()) {
1058 // TODO: investigate internal implementation
1059 return VolumeShaper::Status(INVALID_OPERATION);
1060 }
1061 if (mAudioOutput.get() != nullptr) {
1062 return mAudioOutput->applyVolumeShaper(configuration, operation);
1063 }
1064 }
1065 return VolumeShaper::Status(INVALID_OPERATION);
1066}
1067
1068sp<VolumeShaper::State> MediaPlayer2Manager::Client::getVolumeShaperState(int id) {
1069 // for hardware output, call player instead
1070 ALOGV("Client::getVolumeShaperState(%p)", this);
1071 sp<MediaPlayer2Base> p = getPlayer();
1072 {
1073 Mutex::Autolock l(mLock);
1074 if (p != 0 && p->hardwareOutput()) {
1075 // TODO: investigate internal implementation.
1076 return nullptr;
1077 }
1078 if (mAudioOutput.get() != nullptr) {
1079 return mAudioOutput->getVolumeShaperState(id);
1080 }
1081 }
1082 return nullptr;
1083}
1084
1085status_t MediaPlayer2Manager::Client::seekTo(int msec, MediaPlayer2SeekMode mode)
1086{
1087 ALOGV("[%d] seekTo(%d, %d)", mConnId, msec, mode);
1088 sp<MediaPlayer2Base> p = getPlayer();
1089 if (p == 0) return UNKNOWN_ERROR;
1090 return p->seekTo(msec, mode);
1091}
1092
1093status_t MediaPlayer2Manager::Client::reset()
1094{
1095 ALOGV("[%d] reset", mConnId);
1096 mRetransmitEndpointValid = false;
1097 sp<MediaPlayer2Base> p = getPlayer();
1098 if (p == 0) return UNKNOWN_ERROR;
1099 return p->reset();
1100}
1101
1102status_t MediaPlayer2Manager::Client::notifyAt(int64_t mediaTimeUs)
1103{
1104 ALOGV("[%d] notifyAt(%lld)", mConnId, (long long)mediaTimeUs);
1105 sp<MediaPlayer2Base> p = getPlayer();
1106 if (p == 0) return UNKNOWN_ERROR;
1107 return p->notifyAt(mediaTimeUs);
1108}
1109
1110status_t MediaPlayer2Manager::Client::setAudioStreamType(audio_stream_type_t type)
1111{
1112 ALOGV("[%d] setAudioStreamType(%d)", mConnId, type);
1113 // TODO: for hardware output, call player instead
1114 Mutex::Autolock l(mLock);
1115 if (mAudioOutput != 0) mAudioOutput->setAudioStreamType(type);
1116 return NO_ERROR;
1117}
1118
1119status_t MediaPlayer2Manager::Client::setAudioAttributes_l(const Parcel &parcel)
1120{
1121 if (mAudioAttributes != NULL) { free(mAudioAttributes); }
1122 mAudioAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1123 if (mAudioAttributes == NULL) {
1124 return NO_MEMORY;
1125 }
1126 unmarshallAudioAttributes(parcel, mAudioAttributes);
1127
1128 ALOGV("setAudioAttributes_l() usage=%d content=%d flags=0x%x tags=%s",
1129 mAudioAttributes->usage, mAudioAttributes->content_type, mAudioAttributes->flags,
1130 mAudioAttributes->tags);
1131
1132 if (mAudioOutput != 0) {
1133 mAudioOutput->setAudioAttributes(mAudioAttributes);
1134 }
1135 return NO_ERROR;
1136}
1137
1138status_t MediaPlayer2Manager::Client::setLooping(int loop)
1139{
1140 ALOGV("[%d] setLooping(%d)", mConnId, loop);
1141 mLoop = loop;
1142 sp<MediaPlayer2Base> p = getPlayer();
1143 if (p != 0) return p->setLooping(loop);
1144 return NO_ERROR;
1145}
1146
1147status_t MediaPlayer2Manager::Client::setVolume(float leftVolume, float rightVolume)
1148{
1149 ALOGV("[%d] setVolume(%f, %f)", mConnId, leftVolume, rightVolume);
1150
1151 // for hardware output, call player instead
1152 sp<MediaPlayer2Base> p = getPlayer();
1153 {
1154 Mutex::Autolock l(mLock);
1155 if (p != 0 && p->hardwareOutput()) {
1156 MediaPlayerHWInterface* hwp =
1157 reinterpret_cast<MediaPlayerHWInterface*>(p.get());
1158 return hwp->setVolume(leftVolume, rightVolume);
1159 } else {
1160 if (mAudioOutput != 0) mAudioOutput->setVolume(leftVolume, rightVolume);
1161 return NO_ERROR;
1162 }
1163 }
1164
1165 return NO_ERROR;
1166}
1167
1168status_t MediaPlayer2Manager::Client::setAuxEffectSendLevel(float level)
1169{
1170 ALOGV("[%d] setAuxEffectSendLevel(%f)", mConnId, level);
1171 Mutex::Autolock l(mLock);
1172 if (mAudioOutput != 0) return mAudioOutput->setAuxEffectSendLevel(level);
1173 return NO_ERROR;
1174}
1175
1176status_t MediaPlayer2Manager::Client::attachAuxEffect(int effectId)
1177{
1178 ALOGV("[%d] attachAuxEffect(%d)", mConnId, effectId);
1179 Mutex::Autolock l(mLock);
1180 if (mAudioOutput != 0) return mAudioOutput->attachAuxEffect(effectId);
1181 return NO_ERROR;
1182}
1183
1184status_t MediaPlayer2Manager::Client::setParameter(int key, const Parcel &request) {
1185 ALOGV("[%d] setParameter(%d)", mConnId, key);
1186 switch (key) {
1187 case MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES:
1188 {
1189 Mutex::Autolock l(mLock);
1190 return setAudioAttributes_l(request);
1191 }
1192 default:
1193 sp<MediaPlayer2Base> p = getPlayer();
1194 if (p == 0) { return UNKNOWN_ERROR; }
1195 return p->setParameter(key, request);
1196 }
1197}
1198
1199status_t MediaPlayer2Manager::Client::getParameter(int key, Parcel *reply) {
1200 ALOGV("[%d] getParameter(%d)", mConnId, key);
1201 sp<MediaPlayer2Base> p = getPlayer();
1202 if (p == 0) return UNKNOWN_ERROR;
1203 return p->getParameter(key, reply);
1204}
1205
1206status_t MediaPlayer2Manager::Client::setRetransmitEndpoint(
1207 const struct sockaddr_in* endpoint) {
1208
1209 if (NULL != endpoint) {
1210 uint32_t a = ntohl(endpoint->sin_addr.s_addr);
1211 uint16_t p = ntohs(endpoint->sin_port);
1212 ALOGV("[%d] setRetransmitEndpoint(%u.%u.%u.%u:%hu)", mConnId,
1213 (a >> 24), (a >> 16) & 0xFF, (a >> 8) & 0xFF, (a & 0xFF), p);
1214 } else {
1215 ALOGV("[%d] setRetransmitEndpoint = <none>", mConnId);
1216 }
1217
1218 sp<MediaPlayer2Base> p = getPlayer();
1219
1220 // Right now, the only valid time to set a retransmit endpoint is before
1221 // player selection has been made (since the presence or absence of a
1222 // retransmit endpoint is going to determine which player is selected during
1223 // setDataSource).
1224 if (p != 0) return INVALID_OPERATION;
1225
1226 if (NULL != endpoint) {
1227 Mutex::Autolock lock(mLock);
1228 mRetransmitEndpoint = *endpoint;
1229 mRetransmitEndpointValid = true;
1230 } else {
1231 Mutex::Autolock lock(mLock);
1232 mRetransmitEndpointValid = false;
1233 }
1234
1235 return NO_ERROR;
1236}
1237
1238status_t MediaPlayer2Manager::Client::getRetransmitEndpoint(
1239 struct sockaddr_in* endpoint)
1240{
1241 if (NULL == endpoint)
1242 return BAD_VALUE;
1243
1244 sp<MediaPlayer2Base> p = getPlayer();
1245
1246 if (p != NULL)
1247 return p->getRetransmitEndpoint(endpoint);
1248
1249 Mutex::Autolock lock(mLock);
1250 if (!mRetransmitEndpointValid)
1251 return NO_INIT;
1252
1253 *endpoint = mRetransmitEndpoint;
1254
1255 return NO_ERROR;
1256}
1257
1258void MediaPlayer2Manager::Client::notify(
Pawin Vongmasa50963852017-12-12 06:24:42 -08001259 const wp<MediaPlayer2Engine> &listener, int msg, int ext1, int ext2, const Parcel *obj)
Wei Jia53692fa2017-12-11 10:33:46 -08001260{
Pawin Vongmasa50963852017-12-12 06:24:42 -08001261 sp<MediaPlayer2Engine> spListener = listener.promote();
1262 if (spListener == NULL) {
Wei Jia53692fa2017-12-11 10:33:46 -08001263 return;
1264 }
Pawin Vongmasa50963852017-12-12 06:24:42 -08001265 Client* client = static_cast<Client*>(spListener.get());
Wei Jia53692fa2017-12-11 10:33:46 -08001266
1267 sp<MediaPlayer2EngineClient> c;
1268 sp<Client> nextClient;
1269 status_t errStartNext = NO_ERROR;
1270 {
1271 Mutex::Autolock l(client->mLock);
1272 c = client->mClient;
1273 if (msg == MEDIA2_PLAYBACK_COMPLETE && client->mNextClient != NULL) {
1274 nextClient = client->mNextClient;
1275
1276 if (client->mAudioOutput != NULL)
1277 client->mAudioOutput->switchToNextOutput();
1278
1279 errStartNext = nextClient->start();
1280 }
1281 }
1282
1283 if (nextClient != NULL) {
1284 sp<MediaPlayer2EngineClient> nc;
1285 {
1286 Mutex::Autolock l(nextClient->mLock);
1287 nc = nextClient->mClient;
1288 }
1289 if (nc != NULL) {
1290 if (errStartNext == NO_ERROR) {
1291 nc->notify(MEDIA2_INFO, MEDIA2_INFO_STARTED_AS_NEXT, 0, obj);
1292 } else {
1293 nc->notify(MEDIA2_ERROR, MEDIA2_ERROR_UNKNOWN , 0, obj);
1294 ALOGE("gapless:start playback for next track failed, err(%d)", errStartNext);
1295 }
1296 }
1297 }
1298
1299 if (MEDIA2_INFO == msg &&
1300 MEDIA2_INFO_METADATA_UPDATE == ext1) {
1301 const media::Metadata::Type metadata_type = ext2;
1302
1303 if(client->shouldDropMetadata(metadata_type)) {
1304 return;
1305 }
1306
1307 // Update the list of metadata that have changed. getMetadata
1308 // also access mMetadataUpdated and clears it.
1309 client->addNewMetadataUpdate(metadata_type);
1310 }
1311
1312 if (c != NULL) {
Pawin Vongmasa50963852017-12-12 06:24:42 -08001313 ALOGV("[%d] notify (%p, %d, %d, %d)", client->mConnId, spListener.get(), msg, ext1, ext2);
Wei Jia53692fa2017-12-11 10:33:46 -08001314 c->notify(msg, ext1, ext2, obj);
1315 }
1316}
1317
1318
1319bool MediaPlayer2Manager::Client::shouldDropMetadata(media::Metadata::Type code) const
1320{
1321 Mutex::Autolock lock(mLock);
1322
1323 if (findMetadata(mMetadataDrop, code)) {
1324 return true;
1325 }
1326
1327 if (mMetadataAllow.isEmpty() || findMetadata(mMetadataAllow, code)) {
1328 return false;
1329 } else {
1330 return true;
1331 }
1332}
1333
1334
1335void MediaPlayer2Manager::Client::addNewMetadataUpdate(media::Metadata::Type metadata_type) {
1336 Mutex::Autolock lock(mLock);
1337 if (mMetadataUpdated.indexOf(metadata_type) < 0) {
1338 mMetadataUpdated.add(metadata_type);
1339 }
1340}
1341
1342// Modular DRM
1343status_t MediaPlayer2Manager::Client::prepareDrm(const uint8_t uuid[16],
1344 const Vector<uint8_t>& drmSessionId)
1345{
1346 ALOGV("[%d] prepareDrm", mConnId);
1347 sp<MediaPlayer2Base> p = getPlayer();
1348 if (p == 0) return UNKNOWN_ERROR;
1349
1350 status_t ret = p->prepareDrm(uuid, drmSessionId);
1351 ALOGV("prepareDrm ret: %d", ret);
1352
1353 return ret;
1354}
1355
1356status_t MediaPlayer2Manager::Client::releaseDrm()
1357{
1358 ALOGV("[%d] releaseDrm", mConnId);
1359 sp<MediaPlayer2Base> p = getPlayer();
1360 if (p == 0) return UNKNOWN_ERROR;
1361
1362 status_t ret = p->releaseDrm();
1363 ALOGV("releaseDrm ret: %d", ret);
1364
1365 return ret;
1366}
1367
1368status_t MediaPlayer2Manager::Client::setOutputDevice(audio_port_handle_t deviceId)
1369{
1370 ALOGV("[%d] setOutputDevice", mConnId);
1371 {
1372 Mutex::Autolock l(mLock);
1373 if (mAudioOutput.get() != nullptr) {
1374 return mAudioOutput->setOutputDevice(deviceId);
1375 }
1376 }
1377 return NO_INIT;
1378}
1379
1380status_t MediaPlayer2Manager::Client::getRoutedDeviceId(audio_port_handle_t* deviceId)
1381{
1382 ALOGV("[%d] getRoutedDeviceId", mConnId);
1383 {
1384 Mutex::Autolock l(mLock);
1385 if (mAudioOutput.get() != nullptr) {
1386 return mAudioOutput->getRoutedDeviceId(deviceId);
1387 }
1388 }
1389 return NO_INIT;
1390}
1391
1392status_t MediaPlayer2Manager::Client::enableAudioDeviceCallback(bool enabled)
1393{
1394 ALOGV("[%d] enableAudioDeviceCallback, %d", mConnId, enabled);
1395 {
1396 Mutex::Autolock l(mLock);
1397 if (mAudioOutput.get() != nullptr) {
1398 return mAudioOutput->enableAudioDeviceCallback(enabled);
1399 }
1400 }
1401 return NO_INIT;
1402}
1403
1404#if CALLBACK_ANTAGONIZER
1405const int Antagonizer::interval = 10000; // 10 msecs
1406
Pawin Vongmasa50963852017-12-12 06:24:42 -08001407Antagonizer::Antagonizer(
1408 MediaPlayer2Manager::NotifyCallback cb,
1409 const wp<MediaPlayer2Engine> &client) :
Wei Jia53692fa2017-12-11 10:33:46 -08001410 mExit(false), mActive(false), mClient(client), mCb(cb)
1411{
1412 createThread(callbackThread, this);
1413}
1414
1415void Antagonizer::kill()
1416{
1417 Mutex::Autolock _l(mLock);
1418 mActive = false;
1419 mExit = true;
1420 mCondition.wait(mLock);
1421}
1422
1423int Antagonizer::callbackThread(void* user)
1424{
1425 ALOGD("Antagonizer started");
1426 Antagonizer* p = reinterpret_cast<Antagonizer*>(user);
1427 while (!p->mExit) {
1428 if (p->mActive) {
1429 ALOGV("send event");
1430 p->mCb(p->mClient, 0, 0, 0);
1431 }
1432 usleep(interval);
1433 }
1434 Mutex::Autolock _l(p->mLock);
1435 p->mCondition.signal();
1436 ALOGD("Antagonizer stopped");
1437 return 0;
1438}
1439#endif
1440
1441#undef LOG_TAG
1442#define LOG_TAG "AudioSink"
1443MediaPlayer2Manager::AudioOutput::AudioOutput(audio_session_t sessionId, uid_t uid, int pid,
1444 const audio_attributes_t* attr, const sp<AudioSystem::AudioDeviceCallback>& deviceCallback)
1445 : mCallback(NULL),
1446 mCallbackCookie(NULL),
1447 mCallbackData(NULL),
1448 mStreamType(AUDIO_STREAM_MUSIC),
1449 mLeftVolume(1.0),
1450 mRightVolume(1.0),
1451 mPlaybackRate(AUDIO_PLAYBACK_RATE_DEFAULT),
1452 mSampleRateHz(0),
1453 mMsecsPerFrame(0),
1454 mFrameSize(0),
1455 mSessionId(sessionId),
1456 mUid(uid),
1457 mPid(pid),
1458 mSendLevel(0.0),
1459 mAuxEffectId(0),
1460 mFlags(AUDIO_OUTPUT_FLAG_NONE),
1461 mVolumeHandler(new media::VolumeHandler()),
1462 mSelectedDeviceId(AUDIO_PORT_HANDLE_NONE),
1463 mRoutedDeviceId(AUDIO_PORT_HANDLE_NONE),
1464 mDeviceCallbackEnabled(false),
1465 mDeviceCallback(deviceCallback)
1466{
1467 ALOGV("AudioOutput(%d)", sessionId);
1468 if (attr != NULL) {
1469 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1470 if (mAttributes != NULL) {
1471 memcpy(mAttributes, attr, sizeof(audio_attributes_t));
1472 mStreamType = audio_attributes_to_stream_type(attr);
1473 }
1474 } else {
1475 mAttributes = NULL;
1476 }
1477
1478 setMinBufferCount();
1479}
1480
1481MediaPlayer2Manager::AudioOutput::~AudioOutput()
1482{
1483 close();
1484 free(mAttributes);
1485 delete mCallbackData;
1486}
1487
1488//static
1489void MediaPlayer2Manager::AudioOutput::setMinBufferCount()
1490{
1491 char value[PROPERTY_VALUE_MAX];
1492 if (property_get("ro.kernel.qemu", value, 0)) {
1493 mIsOnEmulator = true;
1494 mMinBufferCount = 12; // to prevent systematic buffer underrun for emulator
1495 }
1496}
1497
1498// static
1499bool MediaPlayer2Manager::AudioOutput::isOnEmulator()
1500{
1501 setMinBufferCount(); // benign race wrt other threads
1502 return mIsOnEmulator;
1503}
1504
1505// static
1506int MediaPlayer2Manager::AudioOutput::getMinBufferCount()
1507{
1508 setMinBufferCount(); // benign race wrt other threads
1509 return mMinBufferCount;
1510}
1511
1512ssize_t MediaPlayer2Manager::AudioOutput::bufferSize() const
1513{
1514 Mutex::Autolock lock(mLock);
1515 if (mTrack == 0) return NO_INIT;
1516 return mTrack->frameCount() * mFrameSize;
1517}
1518
1519ssize_t MediaPlayer2Manager::AudioOutput::frameCount() const
1520{
1521 Mutex::Autolock lock(mLock);
1522 if (mTrack == 0) return NO_INIT;
1523 return mTrack->frameCount();
1524}
1525
1526ssize_t MediaPlayer2Manager::AudioOutput::channelCount() const
1527{
1528 Mutex::Autolock lock(mLock);
1529 if (mTrack == 0) return NO_INIT;
1530 return mTrack->channelCount();
1531}
1532
1533ssize_t MediaPlayer2Manager::AudioOutput::frameSize() const
1534{
1535 Mutex::Autolock lock(mLock);
1536 if (mTrack == 0) return NO_INIT;
1537 return mFrameSize;
1538}
1539
1540uint32_t MediaPlayer2Manager::AudioOutput::latency () const
1541{
1542 Mutex::Autolock lock(mLock);
1543 if (mTrack == 0) return 0;
1544 return mTrack->latency();
1545}
1546
1547float MediaPlayer2Manager::AudioOutput::msecsPerFrame() const
1548{
1549 Mutex::Autolock lock(mLock);
1550 return mMsecsPerFrame;
1551}
1552
1553status_t MediaPlayer2Manager::AudioOutput::getPosition(uint32_t *position) const
1554{
1555 Mutex::Autolock lock(mLock);
1556 if (mTrack == 0) return NO_INIT;
1557 return mTrack->getPosition(position);
1558}
1559
1560status_t MediaPlayer2Manager::AudioOutput::getTimestamp(AudioTimestamp &ts) const
1561{
1562 Mutex::Autolock lock(mLock);
1563 if (mTrack == 0) return NO_INIT;
1564 return mTrack->getTimestamp(ts);
1565}
1566
1567// TODO: Remove unnecessary calls to getPlayedOutDurationUs()
1568// as it acquires locks and may query the audio driver.
1569//
1570// Some calls could conceivably retrieve extrapolated data instead of
1571// accessing getTimestamp() or getPosition() every time a data buffer with
1572// a media time is received.
1573//
1574// Calculate duration of played samples if played at normal rate (i.e., 1.0).
1575int64_t MediaPlayer2Manager::AudioOutput::getPlayedOutDurationUs(int64_t nowUs) const
1576{
1577 Mutex::Autolock lock(mLock);
1578 if (mTrack == 0 || mSampleRateHz == 0) {
1579 return 0;
1580 }
1581
1582 uint32_t numFramesPlayed;
1583 int64_t numFramesPlayedAtUs;
1584 AudioTimestamp ts;
1585
1586 status_t res = mTrack->getTimestamp(ts);
1587 if (res == OK) { // case 1: mixing audio tracks and offloaded tracks.
1588 numFramesPlayed = ts.mPosition;
1589 numFramesPlayedAtUs = ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000;
1590 //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
1591 } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track
1592 numFramesPlayed = 0;
1593 numFramesPlayedAtUs = nowUs;
1594 //ALOGD("getTimestamp: WOULD_BLOCK %d %lld",
1595 // numFramesPlayed, (long long)numFramesPlayedAtUs);
1596 } else { // case 3: transitory at new track or audio fast tracks.
1597 res = mTrack->getPosition(&numFramesPlayed);
1598 CHECK_EQ(res, (status_t)OK);
1599 numFramesPlayedAtUs = nowUs;
1600 numFramesPlayedAtUs += 1000LL * mTrack->latency() / 2; /* XXX */
1601 //ALOGD("getPosition: %u %lld", numFramesPlayed, (long long)numFramesPlayedAtUs);
1602 }
1603
1604 // CHECK_EQ(numFramesPlayed & (1 << 31), 0); // can't be negative until 12.4 hrs, test
1605 // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours.
1606 int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000000LL / mSampleRateHz)
1607 + nowUs - numFramesPlayedAtUs;
1608 if (durationUs < 0) {
1609 // Occurs when numFramesPlayed position is very small and the following:
1610 // (1) In case 1, the time nowUs is computed before getTimestamp() is called and
1611 // numFramesPlayedAtUs is greater than nowUs by time more than numFramesPlayed.
1612 // (2) In case 3, using getPosition and adding mAudioSink->latency() to
1613 // numFramesPlayedAtUs, by a time amount greater than numFramesPlayed.
1614 //
1615 // Both of these are transitory conditions.
1616 ALOGV("getPlayedOutDurationUs: negative duration %lld set to zero", (long long)durationUs);
1617 durationUs = 0;
1618 }
1619 ALOGV("getPlayedOutDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)",
1620 (long long)durationUs, (long long)nowUs,
1621 numFramesPlayed, (long long)numFramesPlayedAtUs);
1622 return durationUs;
1623}
1624
1625status_t MediaPlayer2Manager::AudioOutput::getFramesWritten(uint32_t *frameswritten) const
1626{
1627 Mutex::Autolock lock(mLock);
1628 if (mTrack == 0) return NO_INIT;
1629 ExtendedTimestamp ets;
1630 status_t status = mTrack->getTimestamp(&ets);
1631 if (status == OK || status == WOULD_BLOCK) {
1632 *frameswritten = (uint32_t)ets.mPosition[ExtendedTimestamp::LOCATION_CLIENT];
1633 }
1634 return status;
1635}
1636
1637status_t MediaPlayer2Manager::AudioOutput::setParameters(const String8& keyValuePairs)
1638{
1639 Mutex::Autolock lock(mLock);
1640 if (mTrack == 0) return NO_INIT;
1641 return mTrack->setParameters(keyValuePairs);
1642}
1643
1644String8 MediaPlayer2Manager::AudioOutput::getParameters(const String8& keys)
1645{
1646 Mutex::Autolock lock(mLock);
1647 if (mTrack == 0) return String8::empty();
1648 return mTrack->getParameters(keys);
1649}
1650
1651void MediaPlayer2Manager::AudioOutput::setAudioAttributes(const audio_attributes_t * attributes) {
1652 Mutex::Autolock lock(mLock);
1653 if (attributes == NULL) {
1654 free(mAttributes);
1655 mAttributes = NULL;
1656 } else {
1657 if (mAttributes == NULL) {
1658 mAttributes = (audio_attributes_t *) calloc(1, sizeof(audio_attributes_t));
1659 }
1660 memcpy(mAttributes, attributes, sizeof(audio_attributes_t));
1661 mStreamType = audio_attributes_to_stream_type(attributes);
1662 }
1663}
1664
1665void MediaPlayer2Manager::AudioOutput::setAudioStreamType(audio_stream_type_t streamType)
1666{
1667 Mutex::Autolock lock(mLock);
1668 // do not allow direct stream type modification if attributes have been set
1669 if (mAttributes == NULL) {
1670 mStreamType = streamType;
1671 }
1672}
1673
1674void MediaPlayer2Manager::AudioOutput::deleteRecycledTrack_l()
1675{
1676 ALOGV("deleteRecycledTrack_l");
1677 if (mRecycledTrack != 0) {
1678
1679 if (mCallbackData != NULL) {
1680 mCallbackData->setOutput(NULL);
1681 mCallbackData->endTrackSwitch();
1682 }
1683
1684 if ((mRecycledTrack->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) {
1685 int32_t msec = 0;
1686 if (!mRecycledTrack->stopped()) { // check if active
1687 (void)mRecycledTrack->pendingDuration(&msec);
1688 }
1689 mRecycledTrack->stop(); // ensure full data drain
1690 ALOGD("deleting recycled track, waiting for data drain (%d msec)", msec);
1691 if (msec > 0) {
1692 static const int32_t WAIT_LIMIT_MS = 3000;
1693 if (msec > WAIT_LIMIT_MS) {
1694 msec = WAIT_LIMIT_MS;
1695 }
1696 usleep(msec * 1000LL);
1697 }
1698 }
1699 // An offloaded track isn't flushed because the STREAM_END is reported
1700 // slightly prematurely to allow time for the gapless track switch
1701 // but this means that if we decide not to recycle the track there
1702 // could be a small amount of residual data still playing. We leave
1703 // AudioFlinger to drain the track.
1704
1705 mRecycledTrack.clear();
1706 close_l();
1707 delete mCallbackData;
1708 mCallbackData = NULL;
1709 }
1710}
1711
1712void MediaPlayer2Manager::AudioOutput::close_l()
1713{
1714 mTrack.clear();
1715}
1716
1717status_t MediaPlayer2Manager::AudioOutput::open(
1718 uint32_t sampleRate, int channelCount, audio_channel_mask_t channelMask,
1719 audio_format_t format, int bufferCount,
1720 AudioCallback cb, void *cookie,
1721 audio_output_flags_t flags,
1722 const audio_offload_info_t *offloadInfo,
1723 bool doNotReconnect,
1724 uint32_t suggestedFrameCount)
1725{
1726 ALOGV("open(%u, %d, 0x%x, 0x%x, %d, %d 0x%x)", sampleRate, channelCount, channelMask,
1727 format, bufferCount, mSessionId, flags);
1728
1729 // offloading is only supported in callback mode for now.
1730 // offloadInfo must be present if offload flag is set
1731 if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) &&
1732 ((cb == NULL) || (offloadInfo == NULL))) {
1733 return BAD_VALUE;
1734 }
1735
1736 // compute frame count for the AudioTrack internal buffer
1737 size_t frameCount;
1738 if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
1739 frameCount = 0; // AudioTrack will get frame count from AudioFlinger
1740 } else {
1741 // try to estimate the buffer processing fetch size from AudioFlinger.
1742 // framesPerBuffer is approximate and generally correct, except when it's not :-).
1743 uint32_t afSampleRate;
1744 size_t afFrameCount;
1745 if (AudioSystem::getOutputFrameCount(&afFrameCount, mStreamType) != NO_ERROR) {
1746 return NO_INIT;
1747 }
1748 if (AudioSystem::getOutputSamplingRate(&afSampleRate, mStreamType) != NO_ERROR) {
1749 return NO_INIT;
1750 }
1751 const size_t framesPerBuffer =
1752 (unsigned long long)sampleRate * afFrameCount / afSampleRate;
1753
1754 if (bufferCount == 0) {
1755 // use suggestedFrameCount
1756 bufferCount = (suggestedFrameCount + framesPerBuffer - 1) / framesPerBuffer;
1757 }
1758 // Check argument bufferCount against the mininum buffer count
1759 if (bufferCount != 0 && bufferCount < mMinBufferCount) {
1760 ALOGV("bufferCount (%d) increased to %d", bufferCount, mMinBufferCount);
1761 bufferCount = mMinBufferCount;
1762 }
1763 // if frameCount is 0, then AudioTrack will get frame count from AudioFlinger
1764 // which will be the minimum size permitted.
1765 frameCount = bufferCount * framesPerBuffer;
1766 }
1767
1768 if (channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER) {
1769 channelMask = audio_channel_out_mask_from_count(channelCount);
1770 if (0 == channelMask) {
1771 ALOGE("open() error, can\'t derive mask for %d audio channels", channelCount);
1772 return NO_INIT;
1773 }
1774 }
1775
1776 Mutex::Autolock lock(mLock);
1777 mCallback = cb;
1778 mCallbackCookie = cookie;
1779
1780 // Check whether we can recycle the track
1781 bool reuse = false;
1782 bool bothOffloaded = false;
1783
1784 if (mRecycledTrack != 0) {
1785 // check whether we are switching between two offloaded tracks
1786 bothOffloaded = (flags & mRecycledTrack->getFlags()
1787 & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0;
1788
1789 // check if the existing track can be reused as-is, or if a new track needs to be created.
1790 reuse = true;
1791
1792 if ((mCallbackData == NULL && mCallback != NULL) ||
1793 (mCallbackData != NULL && mCallback == NULL)) {
1794 // recycled track uses callbacks but the caller wants to use writes, or vice versa
1795 ALOGV("can't chain callback and write");
1796 reuse = false;
1797 } else if ((mRecycledTrack->getSampleRate() != sampleRate) ||
1798 (mRecycledTrack->channelCount() != (uint32_t)channelCount) ) {
1799 ALOGV("samplerate, channelcount differ: %u/%u Hz, %u/%d ch",
1800 mRecycledTrack->getSampleRate(), sampleRate,
1801 mRecycledTrack->channelCount(), channelCount);
1802 reuse = false;
1803 } else if (flags != mFlags) {
1804 ALOGV("output flags differ %08x/%08x", flags, mFlags);
1805 reuse = false;
1806 } else if (mRecycledTrack->format() != format) {
1807 reuse = false;
1808 }
1809 } else {
1810 ALOGV("no track available to recycle");
1811 }
1812
1813 ALOGV_IF(bothOffloaded, "both tracks offloaded");
1814
1815 // If we can't recycle and both tracks are offloaded
1816 // we must close the previous output before opening a new one
1817 if (bothOffloaded && !reuse) {
1818 ALOGV("both offloaded and not recycling");
1819 deleteRecycledTrack_l();
1820 }
1821
1822 sp<AudioTrack> t;
1823 CallbackData *newcbd = NULL;
1824
1825 // We don't attempt to create a new track if we are recycling an
1826 // offloaded track. But, if we are recycling a non-offloaded or we
1827 // are switching where one is offloaded and one isn't then we create
1828 // the new track in advance so that we can read additional stream info
1829
1830 if (!(reuse && bothOffloaded)) {
1831 ALOGV("creating new AudioTrack");
1832
1833 if (mCallback != NULL) {
1834 newcbd = new CallbackData(this);
1835 t = new AudioTrack(
1836 mStreamType,
1837 sampleRate,
1838 format,
1839 channelMask,
1840 frameCount,
1841 flags,
1842 CallbackWrapper,
1843 newcbd,
1844 0, // notification frames
1845 mSessionId,
1846 AudioTrack::TRANSFER_CALLBACK,
1847 offloadInfo,
1848 mUid,
1849 mPid,
1850 mAttributes,
1851 doNotReconnect,
1852 1.0f, // default value for maxRequiredSpeed
1853 mSelectedDeviceId);
1854 } else {
1855 // TODO: Due to buffer memory concerns, we use a max target playback speed
1856 // based on mPlaybackRate at the time of open (instead of kMaxRequiredSpeed),
1857 // also clamping the target speed to 1.0 <= targetSpeed <= kMaxRequiredSpeed.
1858 const float targetSpeed =
1859 std::min(std::max(mPlaybackRate.mSpeed, 1.0f), kMaxRequiredSpeed);
1860 ALOGW_IF(targetSpeed != mPlaybackRate.mSpeed,
1861 "track target speed:%f clamped from playback speed:%f",
1862 targetSpeed, mPlaybackRate.mSpeed);
1863 t = new AudioTrack(
1864 mStreamType,
1865 sampleRate,
1866 format,
1867 channelMask,
1868 frameCount,
1869 flags,
1870 NULL, // callback
1871 NULL, // user data
1872 0, // notification frames
1873 mSessionId,
1874 AudioTrack::TRANSFER_DEFAULT,
1875 NULL, // offload info
1876 mUid,
1877 mPid,
1878 mAttributes,
1879 doNotReconnect,
1880 targetSpeed,
1881 mSelectedDeviceId);
1882 }
1883
1884 if ((t == 0) || (t->initCheck() != NO_ERROR)) {
1885 ALOGE("Unable to create audio track");
1886 delete newcbd;
1887 // t goes out of scope, so reference count drops to zero
1888 return NO_INIT;
1889 } else {
1890 // successful AudioTrack initialization implies a legacy stream type was generated
1891 // from the audio attributes
1892 mStreamType = t->streamType();
1893 }
1894 }
1895
1896 if (reuse) {
1897 CHECK(mRecycledTrack != NULL);
1898
1899 if (!bothOffloaded) {
1900 if (mRecycledTrack->frameCount() != t->frameCount()) {
1901 ALOGV("framecount differs: %zu/%zu frames",
1902 mRecycledTrack->frameCount(), t->frameCount());
1903 reuse = false;
1904 }
1905 }
1906
1907 if (reuse) {
1908 ALOGV("chaining to next output and recycling track");
1909 close_l();
1910 mTrack = mRecycledTrack;
1911 mRecycledTrack.clear();
1912 if (mCallbackData != NULL) {
1913 mCallbackData->setOutput(this);
1914 }
1915 delete newcbd;
1916 return updateTrack();
1917 }
1918 }
1919
1920 // we're not going to reuse the track, unblock and flush it
1921 // this was done earlier if both tracks are offloaded
1922 if (!bothOffloaded) {
1923 deleteRecycledTrack_l();
1924 }
1925
1926 CHECK((t != NULL) && ((mCallback == NULL) || (newcbd != NULL)));
1927
1928 mCallbackData = newcbd;
1929 ALOGV("setVolume");
1930 t->setVolume(mLeftVolume, mRightVolume);
1931
1932 // Restore VolumeShapers for the MediaPlayer2 in case the track was recreated
1933 // due to an output sink error (e.g. offload to non-offload switch).
1934 mVolumeHandler->forall([&t](const VolumeShaper &shaper) -> VolumeShaper::Status {
1935 sp<VolumeShaper::Operation> operationToEnd =
1936 new VolumeShaper::Operation(shaper.mOperation);
1937 // TODO: Ideally we would restore to the exact xOffset position
1938 // as returned by getVolumeShaperState(), but we don't have that
1939 // information when restoring at the client unless we periodically poll
1940 // the server or create shared memory state.
1941 //
1942 // For now, we simply advance to the end of the VolumeShaper effect
1943 // if it has been started.
1944 if (shaper.isStarted()) {
1945 operationToEnd->setNormalizedTime(1.f);
1946 }
1947 return t->applyVolumeShaper(shaper.mConfiguration, operationToEnd);
1948 });
1949
1950 mSampleRateHz = sampleRate;
1951 mFlags = flags;
1952 mMsecsPerFrame = 1E3f / (mPlaybackRate.mSpeed * sampleRate);
1953 mFrameSize = t->frameSize();
1954 mTrack = t;
1955
1956 return updateTrack();
1957}
1958
1959status_t MediaPlayer2Manager::AudioOutput::updateTrack() {
1960 if (mTrack == NULL) {
1961 return NO_ERROR;
1962 }
1963
1964 status_t res = NO_ERROR;
1965 // Note some output devices may give us a direct track even though we don't specify it.
1966 // Example: Line application b/17459982.
1967 if ((mTrack->getFlags()
1968 & (AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | AUDIO_OUTPUT_FLAG_DIRECT)) == 0) {
1969 res = mTrack->setPlaybackRate(mPlaybackRate);
1970 if (res == NO_ERROR) {
1971 mTrack->setAuxEffectSendLevel(mSendLevel);
1972 res = mTrack->attachAuxEffect(mAuxEffectId);
1973 }
1974 }
1975 mTrack->setOutputDevice(mSelectedDeviceId);
1976 if (mDeviceCallbackEnabled) {
1977 mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
1978 }
1979 ALOGV("updateTrack() DONE status %d", res);
1980 return res;
1981}
1982
1983status_t MediaPlayer2Manager::AudioOutput::start()
1984{
1985 ALOGV("start");
1986 Mutex::Autolock lock(mLock);
1987 if (mCallbackData != NULL) {
1988 mCallbackData->endTrackSwitch();
1989 }
1990 if (mTrack != 0) {
1991 mTrack->setVolume(mLeftVolume, mRightVolume);
1992 mTrack->setAuxEffectSendLevel(mSendLevel);
1993 status_t status = mTrack->start();
1994 if (status == NO_ERROR) {
1995 mVolumeHandler->setStarted();
1996 }
1997 return status;
1998 }
1999 return NO_INIT;
2000}
2001
2002void MediaPlayer2Manager::AudioOutput::setNextOutput(const sp<AudioOutput>& nextOutput) {
2003 Mutex::Autolock lock(mLock);
2004 mNextOutput = nextOutput;
2005}
2006
2007void MediaPlayer2Manager::AudioOutput::switchToNextOutput() {
2008 ALOGV("switchToNextOutput");
2009
2010 // Try to acquire the callback lock before moving track (without incurring deadlock).
2011 const unsigned kMaxSwitchTries = 100;
2012 Mutex::Autolock lock(mLock);
2013 for (unsigned tries = 0;;) {
2014 if (mTrack == 0) {
2015 return;
2016 }
2017 if (mNextOutput != NULL && mNextOutput != this) {
2018 if (mCallbackData != NULL) {
2019 // two alternative approaches
2020#if 1
2021 CallbackData *callbackData = mCallbackData;
2022 mLock.unlock();
2023 // proper acquisition sequence
2024 callbackData->lock();
2025 mLock.lock();
2026 // Caution: it is unlikely that someone deleted our callback or changed our target
2027 if (callbackData != mCallbackData || mNextOutput == NULL || mNextOutput == this) {
2028 // fatal if we are starved out.
2029 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
2030 "switchToNextOutput() cannot obtain correct lock sequence");
2031 callbackData->unlock();
2032 continue;
2033 }
2034 callbackData->mSwitching = true; // begin track switch
2035 callbackData->setOutput(NULL);
2036#else
2037 // tryBeginTrackSwitch() returns false if the callback has the lock.
2038 if (!mCallbackData->tryBeginTrackSwitch()) {
2039 // fatal if we are starved out.
2040 LOG_ALWAYS_FATAL_IF(++tries > kMaxSwitchTries,
2041 "switchToNextOutput() cannot obtain callback lock");
2042 mLock.unlock();
2043 usleep(5 * 1000 /* usec */); // allow callback to use AudioOutput
2044 mLock.lock();
2045 continue;
2046 }
2047#endif
2048 }
2049
2050 Mutex::Autolock nextLock(mNextOutput->mLock);
2051
2052 // If the next output track is not NULL, then it has been
2053 // opened already for playback.
2054 // This is possible even without the next player being started,
2055 // for example, the next player could be prepared and seeked.
2056 //
2057 // Presuming it isn't advisable to force the track over.
2058 if (mNextOutput->mTrack == NULL) {
2059 ALOGD("Recycling track for gapless playback");
2060 delete mNextOutput->mCallbackData;
2061 mNextOutput->mCallbackData = mCallbackData;
2062 mNextOutput->mRecycledTrack = mTrack;
2063 mNextOutput->mSampleRateHz = mSampleRateHz;
2064 mNextOutput->mMsecsPerFrame = mMsecsPerFrame;
2065 mNextOutput->mFlags = mFlags;
2066 mNextOutput->mFrameSize = mFrameSize;
2067 close_l();
2068 mCallbackData = NULL; // destruction handled by mNextOutput
2069 } else {
2070 ALOGW("Ignoring gapless playback because next player has already started");
2071 // remove track in case resource needed for future players.
2072 if (mCallbackData != NULL) {
2073 mCallbackData->endTrackSwitch(); // release lock for callbacks before close.
2074 }
2075 close_l();
2076 }
2077 }
2078 break;
2079 }
2080}
2081
2082ssize_t MediaPlayer2Manager::AudioOutput::write(const void* buffer, size_t size, bool blocking)
2083{
2084 Mutex::Autolock lock(mLock);
2085 LOG_ALWAYS_FATAL_IF(mCallback != NULL, "Don't call write if supplying a callback.");
2086
2087 //ALOGV("write(%p, %u)", buffer, size);
2088 if (mTrack != 0) {
2089 return mTrack->write(buffer, size, blocking);
2090 }
2091 return NO_INIT;
2092}
2093
2094void MediaPlayer2Manager::AudioOutput::stop()
2095{
2096 ALOGV("stop");
2097 Mutex::Autolock lock(mLock);
2098 if (mTrack != 0) mTrack->stop();
2099}
2100
2101void MediaPlayer2Manager::AudioOutput::flush()
2102{
2103 ALOGV("flush");
2104 Mutex::Autolock lock(mLock);
2105 if (mTrack != 0) mTrack->flush();
2106}
2107
2108void MediaPlayer2Manager::AudioOutput::pause()
2109{
2110 ALOGV("pause");
2111 Mutex::Autolock lock(mLock);
2112 if (mTrack != 0) mTrack->pause();
2113}
2114
2115void MediaPlayer2Manager::AudioOutput::close()
2116{
2117 ALOGV("close");
2118 sp<AudioTrack> track;
2119 {
2120 Mutex::Autolock lock(mLock);
2121 track = mTrack;
2122 close_l(); // clears mTrack
2123 }
2124 // destruction of the track occurs outside of mutex.
2125}
2126
2127void MediaPlayer2Manager::AudioOutput::setVolume(float left, float right)
2128{
2129 ALOGV("setVolume(%f, %f)", left, right);
2130 Mutex::Autolock lock(mLock);
2131 mLeftVolume = left;
2132 mRightVolume = right;
2133 if (mTrack != 0) {
2134 mTrack->setVolume(left, right);
2135 }
2136}
2137
2138status_t MediaPlayer2Manager::AudioOutput::setPlaybackRate(const AudioPlaybackRate &rate)
2139{
2140 ALOGV("setPlaybackRate(%f %f %d %d)",
2141 rate.mSpeed, rate.mPitch, rate.mFallbackMode, rate.mStretchMode);
2142 Mutex::Autolock lock(mLock);
2143 if (mTrack == 0) {
2144 // remember rate so that we can set it when the track is opened
2145 mPlaybackRate = rate;
2146 return OK;
2147 }
2148 status_t res = mTrack->setPlaybackRate(rate);
2149 if (res != NO_ERROR) {
2150 return res;
2151 }
2152 // rate.mSpeed is always greater than 0 if setPlaybackRate succeeded
2153 CHECK_GT(rate.mSpeed, 0.f);
2154 mPlaybackRate = rate;
2155 if (mSampleRateHz != 0) {
2156 mMsecsPerFrame = 1E3f / (rate.mSpeed * mSampleRateHz);
2157 }
2158 return res;
2159}
2160
2161status_t MediaPlayer2Manager::AudioOutput::getPlaybackRate(AudioPlaybackRate *rate)
2162{
2163 ALOGV("setPlaybackRate");
2164 Mutex::Autolock lock(mLock);
2165 if (mTrack == 0) {
2166 return NO_INIT;
2167 }
2168 *rate = mTrack->getPlaybackRate();
2169 return NO_ERROR;
2170}
2171
2172status_t MediaPlayer2Manager::AudioOutput::setAuxEffectSendLevel(float level)
2173{
2174 ALOGV("setAuxEffectSendLevel(%f)", level);
2175 Mutex::Autolock lock(mLock);
2176 mSendLevel = level;
2177 if (mTrack != 0) {
2178 return mTrack->setAuxEffectSendLevel(level);
2179 }
2180 return NO_ERROR;
2181}
2182
2183status_t MediaPlayer2Manager::AudioOutput::attachAuxEffect(int effectId)
2184{
2185 ALOGV("attachAuxEffect(%d)", effectId);
2186 Mutex::Autolock lock(mLock);
2187 mAuxEffectId = effectId;
2188 if (mTrack != 0) {
2189 return mTrack->attachAuxEffect(effectId);
2190 }
2191 return NO_ERROR;
2192}
2193
2194status_t MediaPlayer2Manager::AudioOutput::setOutputDevice(audio_port_handle_t deviceId)
2195{
2196 ALOGV("setOutputDevice(%d)", deviceId);
2197 Mutex::Autolock lock(mLock);
2198 mSelectedDeviceId = deviceId;
2199 if (mTrack != 0) {
2200 return mTrack->setOutputDevice(deviceId);
2201 }
2202 return NO_ERROR;
2203}
2204
2205status_t MediaPlayer2Manager::AudioOutput::getRoutedDeviceId(audio_port_handle_t* deviceId)
2206{
2207 ALOGV("getRoutedDeviceId");
2208 Mutex::Autolock lock(mLock);
2209 if (mTrack != 0) {
2210 mRoutedDeviceId = mTrack->getRoutedDeviceId();
2211 }
2212 *deviceId = mRoutedDeviceId;
2213 return NO_ERROR;
2214}
2215
2216status_t MediaPlayer2Manager::AudioOutput::enableAudioDeviceCallback(bool enabled)
2217{
2218 ALOGV("enableAudioDeviceCallback, %d", enabled);
2219 Mutex::Autolock lock(mLock);
2220 mDeviceCallbackEnabled = enabled;
2221 if (mTrack != 0) {
2222 status_t status;
2223 if (enabled) {
2224 status = mTrack->addAudioDeviceCallback(mDeviceCallback.promote());
2225 } else {
2226 status = mTrack->removeAudioDeviceCallback(mDeviceCallback.promote());
2227 }
2228 return status;
2229 }
2230 return NO_ERROR;
2231}
2232
2233VolumeShaper::Status MediaPlayer2Manager::AudioOutput::applyVolumeShaper(
2234 const sp<VolumeShaper::Configuration>& configuration,
2235 const sp<VolumeShaper::Operation>& operation)
2236{
2237 Mutex::Autolock lock(mLock);
2238 ALOGV("AudioOutput::applyVolumeShaper");
2239
2240 mVolumeHandler->setIdIfNecessary(configuration);
2241
2242 VolumeShaper::Status status;
2243 if (mTrack != 0) {
2244 status = mTrack->applyVolumeShaper(configuration, operation);
2245 if (status >= 0) {
2246 (void)mVolumeHandler->applyVolumeShaper(configuration, operation);
2247 if (mTrack->isPlaying()) { // match local AudioTrack to properly restore.
2248 mVolumeHandler->setStarted();
2249 }
2250 }
2251 } else {
2252 // VolumeShapers are not affected when a track moves between players for
2253 // gapless playback (setNextMediaPlayer).
2254 // We forward VolumeShaper operations that do not change configuration
2255 // to the new player so that unducking may occur as expected.
2256 // Unducking is an idempotent operation, same if applied back-to-back.
2257 if (configuration->getType() == VolumeShaper::Configuration::TYPE_ID
2258 && mNextOutput != nullptr) {
2259 ALOGV("applyVolumeShaper: Attempting to forward missed operation: %s %s",
2260 configuration->toString().c_str(), operation->toString().c_str());
2261 Mutex::Autolock nextLock(mNextOutput->mLock);
2262
2263 // recycled track should be forwarded from this AudioSink by switchToNextOutput
2264 sp<AudioTrack> track = mNextOutput->mRecycledTrack;
2265 if (track != nullptr) {
2266 ALOGD("Forward VolumeShaper operation to recycled track %p", track.get());
2267 (void)track->applyVolumeShaper(configuration, operation);
2268 } else {
2269 // There is a small chance that the unduck occurs after the next
2270 // player has already started, but before it is registered to receive
2271 // the unduck command.
2272 track = mNextOutput->mTrack;
2273 if (track != nullptr) {
2274 ALOGD("Forward VolumeShaper operation to track %p", track.get());
2275 (void)track->applyVolumeShaper(configuration, operation);
2276 }
2277 }
2278 }
2279 status = mVolumeHandler->applyVolumeShaper(configuration, operation);
2280 }
2281 return status;
2282}
2283
2284sp<VolumeShaper::State> MediaPlayer2Manager::AudioOutput::getVolumeShaperState(int id)
2285{
2286 Mutex::Autolock lock(mLock);
2287 if (mTrack != 0) {
2288 return mTrack->getVolumeShaperState(id);
2289 } else {
2290 return mVolumeHandler->getVolumeShaperState(id);
2291 }
2292}
2293
2294// static
2295void MediaPlayer2Manager::AudioOutput::CallbackWrapper(
2296 int event, void *cookie, void *info) {
2297 //ALOGV("callbackwrapper");
2298 CallbackData *data = (CallbackData*)cookie;
2299 // lock to ensure we aren't caught in the middle of a track switch.
2300 data->lock();
2301 AudioOutput *me = data->getOutput();
2302 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
2303 if (me == NULL) {
2304 // no output set, likely because the track was scheduled to be reused
2305 // by another player, but the format turned out to be incompatible.
2306 data->unlock();
2307 if (buffer != NULL) {
2308 buffer->size = 0;
2309 }
2310 return;
2311 }
2312
2313 switch(event) {
2314 case AudioTrack::EVENT_MORE_DATA: {
2315 size_t actualSize = (*me->mCallback)(
2316 me, buffer->raw, buffer->size, me->mCallbackCookie,
2317 CB_EVENT_FILL_BUFFER);
2318
2319 // Log when no data is returned from the callback.
2320 // (1) We may have no data (especially with network streaming sources).
2321 // (2) We may have reached the EOS and the audio track is not stopped yet.
2322 // Note that AwesomePlayer/AudioPlayer will only return zero size when it reaches the EOS.
2323 // NuPlayer2Renderer will return zero when it doesn't have data (it doesn't block to fill).
2324 //
2325 // This is a benign busy-wait, with the next data request generated 10 ms or more later;
2326 // nevertheless for power reasons, we don't want to see too many of these.
2327
2328 ALOGV_IF(actualSize == 0 && buffer->size > 0, "callbackwrapper: empty buffer returned");
2329
2330 buffer->size = actualSize;
2331 } break;
2332
2333 case AudioTrack::EVENT_STREAM_END:
2334 // currently only occurs for offloaded callbacks
2335 ALOGV("callbackwrapper: deliver EVENT_STREAM_END");
2336 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
2337 me->mCallbackCookie, CB_EVENT_STREAM_END);
2338 break;
2339
2340 case AudioTrack::EVENT_NEW_IAUDIOTRACK :
2341 ALOGV("callbackwrapper: deliver EVENT_TEAR_DOWN");
2342 (*me->mCallback)(me, NULL /* buffer */, 0 /* size */,
2343 me->mCallbackCookie, CB_EVENT_TEAR_DOWN);
2344 break;
2345
2346 case AudioTrack::EVENT_UNDERRUN:
2347 // This occurs when there is no data available, typically
2348 // when there is a failure to supply data to the AudioTrack. It can also
2349 // occur in non-offloaded mode when the audio device comes out of standby.
2350 //
2351 // If an AudioTrack underruns it outputs silence. Since this happens suddenly
2352 // it may sound like an audible pop or glitch.
2353 //
2354 // The underrun event is sent once per track underrun; the condition is reset
2355 // when more data is sent to the AudioTrack.
2356 ALOGD("callbackwrapper: EVENT_UNDERRUN (discarded)");
2357 break;
2358
2359 default:
2360 ALOGE("received unknown event type: %d inside CallbackWrapper !", event);
2361 }
2362
2363 data->unlock();
2364}
2365
2366audio_session_t MediaPlayer2Manager::AudioOutput::getSessionId() const
2367{
2368 Mutex::Autolock lock(mLock);
2369 return mSessionId;
2370}
2371
2372uint32_t MediaPlayer2Manager::AudioOutput::getSampleRate() const
2373{
2374 Mutex::Autolock lock(mLock);
2375 if (mTrack == 0) return 0;
2376 return mTrack->getSampleRate();
2377}
2378
2379int64_t MediaPlayer2Manager::AudioOutput::getBufferDurationInUs() const
2380{
2381 Mutex::Autolock lock(mLock);
2382 if (mTrack == 0) {
2383 return 0;
2384 }
2385 int64_t duration;
2386 if (mTrack->getBufferDurationInUs(&duration) != OK) {
2387 return 0;
2388 }
2389 return duration;
2390}
2391
2392} // namespace android