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