1 /*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <inttypes.h>
18
19 //#define LOG_NDEBUG 0
20 #define LOG_TAG "AudioPlayer"
21 #include <utils/Log.h>
22 #include <cutils/compiler.h>
23
24 #include <binder/IPCThreadState.h>
25 #include <media/AudioTrack.h>
26 #include <media/stagefright/MediaSource.h>
27 #include <media/openmax/OMX_Audio.h>
28 #include <media/stagefright/foundation/ADebug.h>
29 #include <media/stagefright/foundation/ALookup.h>
30 #include <media/stagefright/foundation/ALooper.h>
31 #include <media/stagefright/MediaDefs.h>
32 #include <media/stagefright/MediaErrors.h>
33 #include <media/stagefright/MetaData.h>
34 #include <media/stagefright/Utils.h>
35
36 #include "AudioPlayer.h"
37
38 namespace android {
39
AudioPlayer(const sp<MediaPlayerBase::AudioSink> & audioSink,uint32_t flags)40 AudioPlayer::AudioPlayer(
41 const sp<MediaPlayerBase::AudioSink> &audioSink,
42 uint32_t flags)
43 : mInputBuffer(NULL),
44 mSampleRate(0),
45 mLatencyUs(0),
46 mFrameSize(0),
47 mNumFramesPlayed(0),
48 mNumFramesPlayedSysTimeUs(ALooper::GetNowUs()),
49 mPositionTimeMediaUs(-1),
50 mPositionTimeRealUs(-1),
51 mSeeking(false),
52 mReachedEOS(false),
53 mFinalStatus(OK),
54 mSeekTimeUs(0),
55 mStarted(false),
56 mIsFirstBuffer(false),
57 mFirstBufferResult(OK),
58 mFirstBuffer(NULL),
59 mAudioSink(audioSink),
60 mPlaying(false),
61 mStartPosUs(0),
62 mCreateFlags(flags) {
63 }
64
~AudioPlayer()65 AudioPlayer::~AudioPlayer() {
66 if (mStarted) {
67 reset();
68 }
69 }
70
setSource(const sp<MediaSource> & source)71 void AudioPlayer::setSource(const sp<MediaSource> &source) {
72 CHECK(mSource == NULL);
73 mSource = source;
74 }
75
76 ALookup<audio_format_t, int32_t> sAudioFormatToPcmEncoding {
77 {
78 { AUDIO_FORMAT_PCM_16_BIT, kAudioEncodingPcm16bit },
79 { AUDIO_FORMAT_PCM_8_BIT, kAudioEncodingPcm8bit },
80 { AUDIO_FORMAT_PCM_FLOAT, kAudioEncodingPcmFloat },
81 }
82 };
83
start(bool sourceAlreadyStarted)84 status_t AudioPlayer::start(bool sourceAlreadyStarted) {
85 CHECK(!mStarted);
86 CHECK(mSource != NULL);
87
88 status_t err;
89 if (!sourceAlreadyStarted) {
90 err = mSource->start();
91
92 if (err != OK) {
93 return err;
94 }
95 }
96
97 // We allow an optional INFO_FORMAT_CHANGED at the very beginning
98 // of playback, if there is one, getFormat below will retrieve the
99 // updated format, if there isn't, we'll stash away the valid buffer
100 // of data to be used on the first audio callback.
101
102 CHECK(mFirstBuffer == NULL);
103
104 MediaSource::ReadOptions options;
105 if (mSeeking) {
106 options.setSeekTo(mSeekTimeUs);
107 mSeeking = false;
108 }
109
110 mFirstBufferResult = mSource->read(&mFirstBuffer, &options);
111 if (mFirstBufferResult == INFO_FORMAT_CHANGED) {
112 ALOGV("INFO_FORMAT_CHANGED!!!");
113
114 CHECK(mFirstBuffer == NULL);
115 mFirstBufferResult = OK;
116 mIsFirstBuffer = false;
117 } else {
118 mIsFirstBuffer = true;
119 }
120
121 sp<MetaData> format = mSource->getFormat();
122
123 if (format == NULL) {
124 ALOGE("No metadata b/118620871");
125 android_errorWriteLog(0x534e4554, "118620871");
126 return BAD_VALUE;
127 }
128
129 const char *mime;
130 bool success = format->findCString(kKeyMIMEType, &mime);
131 CHECK(success);
132 CHECK(useOffload() || !strcasecmp(mime, MEDIA_MIMETYPE_AUDIO_RAW));
133
134 success = format->findInt32(kKeySampleRate, &mSampleRate);
135 CHECK(success);
136
137 int32_t numChannels;
138 success = format->findInt32(kKeyChannelCount, &numChannels);
139 CHECK(success);
140
141 audio_channel_mask_t channelMask;
142 if (int32_t rawChannelMask; !format->findInt32(kKeyChannelMask, &rawChannelMask)) {
143 // log only when there's a risk of ambiguity of channel mask selection
144 ALOGI_IF(numChannels > 2,
145 "source format didn't specify channel mask, using (%d) channel order", numChannels);
146 channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
147 } else {
148 channelMask = static_cast<audio_channel_mask_t>(rawChannelMask);
149 }
150
151 audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
152 int32_t pcmEncoding;
153 if (format->findInt32(kKeyPcmEncoding, &pcmEncoding)) {
154 sAudioFormatToPcmEncoding.map(pcmEncoding, &audioFormat);
155 }
156
157 if (useOffload()) {
158 if (mapMimeToAudioFormat(audioFormat, mime) != OK) {
159 ALOGE("Couldn't map mime type \"%s\" to a valid AudioSystem::audio_format", mime);
160 audioFormat = AUDIO_FORMAT_INVALID;
161 } else {
162 ALOGV("Mime type \"%s\" mapped to audio_format 0x%x", mime, audioFormat);
163 }
164
165 int32_t aacaot = -1;
166 if ((audioFormat == AUDIO_FORMAT_AAC) && format->findInt32(kKeyAACAOT, &aacaot)) {
167 // Redefine AAC format corrosponding to aac profile
168 mapAACProfileToAudioFormat(audioFormat,(OMX_AUDIO_AACPROFILETYPE) aacaot);
169 }
170 }
171
172 int avgBitRate = -1;
173 format->findInt32(kKeyBitRate, &avgBitRate);
174
175 if (mAudioSink.get() != NULL) {
176
177 uint32_t flags = AUDIO_OUTPUT_FLAG_NONE;
178 audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
179
180 if (allowDeepBuffering()) {
181 flags |= AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
182 }
183 if (useOffload()) {
184 flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
185
186 int64_t durationUs;
187 if (format->findInt64(kKeyDuration, &durationUs)) {
188 offloadInfo.duration_us = durationUs;
189 } else {
190 offloadInfo.duration_us = -1;
191 }
192
193 offloadInfo.sample_rate = mSampleRate;
194 offloadInfo.channel_mask = channelMask;
195 offloadInfo.format = audioFormat;
196 offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
197 offloadInfo.bit_rate = avgBitRate;
198 offloadInfo.has_video = ((mCreateFlags & HAS_VIDEO) != 0);
199 offloadInfo.is_streaming = ((mCreateFlags & IS_STREAMING) != 0);
200 }
201
202 status_t err = mAudioSink->open(
203 mSampleRate, numChannels, channelMask, audioFormat,
204 DEFAULT_AUDIOSINK_BUFFERCOUNT,
205 &AudioPlayer::AudioSinkCallback,
206 this,
207 (audio_output_flags_t)flags,
208 useOffload() ? &offloadInfo : NULL);
209
210 if (err == OK) {
211 mLatencyUs = (int64_t)mAudioSink->latency() * 1000;
212 mFrameSize = mAudioSink->frameSize();
213
214 if (useOffload()) {
215 // If the playback is offloaded to h/w we pass the
216 // HAL some metadata information
217 // We don't want to do this for PCM because it will be going
218 // through the AudioFlinger mixer before reaching the hardware
219 sendMetaDataToHal(mAudioSink, format);
220 }
221
222 err = mAudioSink->start();
223 // do not alter behavior for non offloaded tracks: ignore start status.
224 if (!useOffload()) {
225 err = OK;
226 }
227 }
228
229 if (err != OK) {
230 if (mFirstBuffer != NULL) {
231 mFirstBuffer->release();
232 mFirstBuffer = NULL;
233 }
234
235 if (!sourceAlreadyStarted) {
236 mSource->stop();
237 }
238
239 return err;
240 }
241
242 } else {
243 // playing to an AudioTrack, set up mask if necessary
244 audio_channel_mask_t audioMask = channelMask == CHANNEL_MASK_USE_CHANNEL_ORDER ?
245 audio_channel_out_mask_from_count(numChannels) : channelMask;
246 if (0 == audioMask) {
247 return BAD_VALUE;
248 }
249
250 mAudioTrack = new AudioTrack(
251 AUDIO_STREAM_MUSIC, mSampleRate, AUDIO_FORMAT_PCM_16_BIT, audioMask,
252 0 /*frameCount*/, AUDIO_OUTPUT_FLAG_NONE, &AudioCallback, this,
253 0 /*notificationFrames*/);
254
255 if ((err = mAudioTrack->initCheck()) != OK) {
256 mAudioTrack.clear();
257
258 if (mFirstBuffer != NULL) {
259 mFirstBuffer->release();
260 mFirstBuffer = NULL;
261 }
262
263 if (!sourceAlreadyStarted) {
264 mSource->stop();
265 }
266
267 return err;
268 }
269
270 mLatencyUs = (int64_t)mAudioTrack->latency() * 1000;
271 mFrameSize = mAudioTrack->frameSize();
272
273 mAudioTrack->start();
274 }
275
276 mStarted = true;
277 mPlaying = true;
278
279 return OK;
280 }
281
pause(bool playPendingSamples)282 void AudioPlayer::pause(bool playPendingSamples) {
283 CHECK(mStarted);
284
285 if (playPendingSamples) {
286 if (mAudioSink.get() != NULL) {
287 mAudioSink->stop();
288 } else {
289 mAudioTrack->stop();
290 }
291
292 mNumFramesPlayed = 0;
293 mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
294 } else {
295 if (mAudioSink.get() != NULL) {
296 mAudioSink->pause();
297 } else {
298 mAudioTrack->pause();
299 }
300 }
301
302 mPlaying = false;
303 }
304
resume()305 status_t AudioPlayer::resume() {
306 CHECK(mStarted);
307 status_t err;
308
309 if (mAudioSink.get() != NULL) {
310 err = mAudioSink->start();
311 } else {
312 err = mAudioTrack->start();
313 }
314
315 if (err == OK) {
316 mPlaying = true;
317 }
318
319 return err;
320 }
321
reset()322 void AudioPlayer::reset() {
323 CHECK(mStarted);
324
325 ALOGV("reset: mPlaying=%d mReachedEOS=%d useOffload=%d",
326 mPlaying, mReachedEOS, useOffload() );
327
328 if (mAudioSink.get() != NULL) {
329 mAudioSink->stop();
330 // If we're closing and have reached EOS, we don't want to flush
331 // the track because if it is offloaded there could be a small
332 // amount of residual data in the hardware buffer which we must
333 // play to give gapless playback.
334 // But if we're resetting when paused or before we've reached EOS
335 // we can't be doing a gapless playback and there could be a large
336 // amount of data queued in the hardware if the track is offloaded,
337 // so we must flush to prevent a track switch being delayed playing
338 // the buffered data that we don't want now
339 if (!mPlaying || !mReachedEOS) {
340 mAudioSink->flush();
341 }
342
343 mAudioSink->close();
344 } else {
345 mAudioTrack->stop();
346
347 if (!mPlaying || !mReachedEOS) {
348 mAudioTrack->flush();
349 }
350
351 mAudioTrack.clear();
352 }
353
354 // Make sure to release any buffer we hold onto so that the
355 // source is able to stop().
356
357 if (mFirstBuffer != NULL) {
358 mFirstBuffer->release();
359 mFirstBuffer = NULL;
360 }
361
362 if (mInputBuffer != NULL) {
363 ALOGV("AudioPlayer releasing input buffer.");
364
365 mInputBuffer->release();
366 mInputBuffer = NULL;
367 }
368
369 mSource->stop();
370
371 // The following hack is necessary to ensure that the OMX
372 // component is completely released by the time we may try
373 // to instantiate it again.
374 // When offloading, the OMX component is not used so this hack
375 // is not needed
376 if (!useOffload()) {
377 wp<MediaSource> tmp = mSource;
378 mSource.clear();
379 while (tmp.promote() != NULL) {
380 usleep(1000);
381 }
382 } else {
383 mSource.clear();
384 }
385 IPCThreadState::self()->flushCommands();
386
387 mNumFramesPlayed = 0;
388 mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
389 mPositionTimeMediaUs = -1;
390 mPositionTimeRealUs = -1;
391 mSeeking = false;
392 mSeekTimeUs = 0;
393 mReachedEOS = false;
394 mFinalStatus = OK;
395 mStarted = false;
396 mPlaying = false;
397 mStartPosUs = 0;
398 }
399
400 // static
AudioCallback(int event,void * user,void * info)401 void AudioPlayer::AudioCallback(int event, void *user, void *info) {
402 static_cast<AudioPlayer *>(user)->AudioCallback(event, info);
403 }
404
reachedEOS(status_t * finalStatus)405 bool AudioPlayer::reachedEOS(status_t *finalStatus) {
406 *finalStatus = OK;
407
408 Mutex::Autolock autoLock(mLock);
409 *finalStatus = mFinalStatus;
410 return mReachedEOS;
411 }
412
setPlaybackRate(const AudioPlaybackRate & rate)413 status_t AudioPlayer::setPlaybackRate(const AudioPlaybackRate &rate) {
414 if (mAudioSink.get() != NULL) {
415 return mAudioSink->setPlaybackRate(rate);
416 } else if (mAudioTrack != 0){
417 return mAudioTrack->setPlaybackRate(rate);
418 } else {
419 return NO_INIT;
420 }
421 }
422
getPlaybackRate(AudioPlaybackRate * rate)423 status_t AudioPlayer::getPlaybackRate(AudioPlaybackRate *rate /* nonnull */) {
424 if (mAudioSink.get() != NULL) {
425 return mAudioSink->getPlaybackRate(rate);
426 } else if (mAudioTrack != 0) {
427 *rate = mAudioTrack->getPlaybackRate();
428 return OK;
429 } else {
430 return NO_INIT;
431 }
432 }
433
434 // static
AudioSinkCallback(MediaPlayerBase::AudioSink *,void * buffer,size_t size,void * cookie,MediaPlayerBase::AudioSink::cb_event_t event)435 size_t AudioPlayer::AudioSinkCallback(
436 MediaPlayerBase::AudioSink * /* audioSink */,
437 void *buffer, size_t size, void *cookie,
438 MediaPlayerBase::AudioSink::cb_event_t event) {
439 AudioPlayer *me = (AudioPlayer *)cookie;
440
441 switch(event) {
442 case MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER:
443 return me->fillBuffer(buffer, size);
444
445 case MediaPlayerBase::AudioSink::CB_EVENT_STREAM_END:
446 ALOGV("AudioSinkCallback: stream end");
447 me->mReachedEOS = true;
448 break;
449
450 case MediaPlayerBase::AudioSink::CB_EVENT_TEAR_DOWN:
451 ALOGV("AudioSinkCallback: Tear down event");
452 break;
453 }
454
455 return 0;
456 }
457
AudioCallback(int event,void * info)458 void AudioPlayer::AudioCallback(int event, void *info) {
459 switch (event) {
460 case AudioTrack::EVENT_MORE_DATA:
461 {
462 AudioTrack::Buffer *buffer = (AudioTrack::Buffer *)info;
463 size_t numBytesWritten = fillBuffer(buffer->raw, buffer->size);
464 buffer->size = numBytesWritten;
465 }
466 break;
467
468 case AudioTrack::EVENT_STREAM_END:
469 mReachedEOS = true;
470 break;
471 }
472 }
473
fillBuffer(void * data,size_t size)474 size_t AudioPlayer::fillBuffer(void *data, size_t size) {
475 if (mNumFramesPlayed == 0) {
476 ALOGV("AudioCallback");
477 }
478
479 if (mReachedEOS) {
480 return 0;
481 }
482
483 size_t size_done = 0;
484 size_t size_remaining = size;
485 while (size_remaining > 0) {
486 MediaSource::ReadOptions options;
487 bool refreshSeekTime = false;
488
489 {
490 Mutex::Autolock autoLock(mLock);
491
492 if (mSeeking) {
493 if (mIsFirstBuffer) {
494 if (mFirstBuffer != NULL) {
495 mFirstBuffer->release();
496 mFirstBuffer = NULL;
497 }
498 mIsFirstBuffer = false;
499 }
500
501 options.setSeekTo(mSeekTimeUs);
502 refreshSeekTime = true;
503
504 if (mInputBuffer != NULL) {
505 mInputBuffer->release();
506 mInputBuffer = NULL;
507 }
508
509 mSeeking = false;
510 }
511 }
512
513 if (mInputBuffer == NULL) {
514 status_t err;
515
516 if (mIsFirstBuffer) {
517 mInputBuffer = mFirstBuffer;
518 mFirstBuffer = NULL;
519 err = mFirstBufferResult;
520
521 mIsFirstBuffer = false;
522 } else {
523 err = mSource->read(&mInputBuffer, &options);
524 }
525
526 CHECK((err == OK && mInputBuffer != NULL)
527 || (err != OK && mInputBuffer == NULL));
528
529 Mutex::Autolock autoLock(mLock);
530
531 if (err != OK) {
532 if (!mReachedEOS) {
533 if (useOffload()) {
534 // no more buffers to push - stop() and wait for STREAM_END
535 // don't set mReachedEOS until stream end received
536 if (mAudioSink != NULL) {
537 mAudioSink->stop();
538 } else {
539 mAudioTrack->stop();
540 }
541 } else {
542 mReachedEOS = true;
543 }
544 }
545
546 mFinalStatus = err;
547 break;
548 }
549
550 if (mAudioSink != NULL) {
551 mLatencyUs = (int64_t)mAudioSink->latency() * 1000;
552 } else {
553 mLatencyUs = (int64_t)mAudioTrack->latency() * 1000;
554 }
555
556 if(mInputBuffer->range_length() != 0) {
557 CHECK(mInputBuffer->meta_data().findInt64(
558 kKeyTime, &mPositionTimeMediaUs));
559 }
560
561 // need to adjust the mStartPosUs for offload decoding since parser
562 // might not be able to get the exact seek time requested.
563 if (refreshSeekTime) {
564 if (useOffload()) {
565 mStartPosUs = mPositionTimeMediaUs;
566 ALOGV("adjust seek time to: %.2f", mStartPosUs/ 1E6);
567 }
568 // clear seek time with mLock locked and once we have valid mPositionTimeMediaUs
569 // and mPositionTimeRealUs
570 // before clearing mSeekTimeUs check if a new seek request has been received while
571 // we were reading from the source with mLock released.
572 if (!mSeeking) {
573 mSeekTimeUs = 0;
574 }
575 }
576
577 if (!useOffload()) {
578 mPositionTimeRealUs =
579 ((mNumFramesPlayed + size_done / mFrameSize) * 1000000)
580 / mSampleRate;
581 ALOGV("buffer->size() = %zu, "
582 "mPositionTimeMediaUs=%.2f mPositionTimeRealUs=%.2f",
583 mInputBuffer->range_length(),
584 mPositionTimeMediaUs / 1E6, mPositionTimeRealUs / 1E6);
585 }
586
587 }
588
589 if (mInputBuffer->range_length() == 0) {
590 mInputBuffer->release();
591 mInputBuffer = NULL;
592
593 continue;
594 }
595
596 size_t copy = size_remaining;
597 if (copy > mInputBuffer->range_length()) {
598 copy = mInputBuffer->range_length();
599 }
600
601 memcpy((char *)data + size_done,
602 (const char *)mInputBuffer->data() + mInputBuffer->range_offset(),
603 copy);
604
605 mInputBuffer->set_range(mInputBuffer->range_offset() + copy,
606 mInputBuffer->range_length() - copy);
607
608 size_done += copy;
609 size_remaining -= copy;
610 }
611
612 if (useOffload()) {
613 // We must ask the hardware what it has played
614 mPositionTimeRealUs = getOutputPlayPositionUs_l();
615 ALOGV("mPositionTimeMediaUs=%.2f mPositionTimeRealUs=%.2f",
616 mPositionTimeMediaUs / 1E6, mPositionTimeRealUs / 1E6);
617 }
618
619 {
620 Mutex::Autolock autoLock(mLock);
621 mNumFramesPlayed += size_done / mFrameSize;
622 mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
623 }
624
625 return size_done;
626 }
627
getOutputPlayPositionUs_l()628 int64_t AudioPlayer::getOutputPlayPositionUs_l()
629 {
630 uint32_t playedSamples = 0;
631 uint32_t sampleRate;
632 if (mAudioSink != NULL) {
633 mAudioSink->getPosition(&playedSamples);
634 sampleRate = mAudioSink->getSampleRate();
635 } else {
636 mAudioTrack->getPosition(&playedSamples);
637 sampleRate = mAudioTrack->getSampleRate();
638 }
639 if (sampleRate != 0) {
640 mSampleRate = sampleRate;
641 }
642
643 int64_t playedUs;
644 if (mSampleRate != 0) {
645 playedUs = (static_cast<int64_t>(playedSamples) * 1000000 ) / mSampleRate;
646 } else {
647 playedUs = 0;
648 }
649
650 // HAL position is relative to the first buffer we sent at mStartPosUs
651 const int64_t renderedDuration = mStartPosUs + playedUs;
652 ALOGV("getOutputPlayPositionUs_l %" PRId64, renderedDuration);
653 return renderedDuration;
654 }
655
seekTo(int64_t time_us)656 status_t AudioPlayer::seekTo(int64_t time_us) {
657 Mutex::Autolock autoLock(mLock);
658
659 ALOGV("seekTo( %" PRId64 " )", time_us);
660
661 mSeeking = true;
662 mPositionTimeRealUs = mPositionTimeMediaUs = -1;
663 mReachedEOS = false;
664 mSeekTimeUs = time_us;
665 mStartPosUs = time_us;
666
667 // Flush resets the number of played frames
668 mNumFramesPlayed = 0;
669 mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
670
671 if (mAudioSink != NULL) {
672 if (mPlaying) {
673 mAudioSink->pause();
674 }
675 mAudioSink->flush();
676 if (mPlaying) {
677 mAudioSink->start();
678 }
679 } else {
680 if (mPlaying) {
681 mAudioTrack->pause();
682 }
683 mAudioTrack->flush();
684 if (mPlaying) {
685 mAudioTrack->start();
686 }
687 }
688
689 return OK;
690 }
691
692 }
693