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,
253                 wp<IAudioTrackCallback>::fromExisting(this),
254                 0 /*notificationFrames*/);
255 
256         if ((err = mAudioTrack->initCheck()) != OK) {
257             mAudioTrack.clear();
258 
259             if (mFirstBuffer != NULL) {
260                 mFirstBuffer->release();
261                 mFirstBuffer = NULL;
262             }
263 
264             if (!sourceAlreadyStarted) {
265                 mSource->stop();
266             }
267 
268             return err;
269         }
270 
271         mLatencyUs = (int64_t)mAudioTrack->latency() * 1000;
272         mFrameSize = mAudioTrack->frameSize();
273 
274         mAudioTrack->start();
275     }
276 
277     mStarted = true;
278     mPlaying = true;
279 
280     return OK;
281 }
282 
pause(bool playPendingSamples)283 void AudioPlayer::pause(bool playPendingSamples) {
284     CHECK(mStarted);
285 
286     if (playPendingSamples) {
287         if (mAudioSink.get() != NULL) {
288             mAudioSink->stop();
289         } else {
290             mAudioTrack->stop();
291         }
292 
293         mNumFramesPlayed = 0;
294         mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
295     } else {
296         if (mAudioSink.get() != NULL) {
297             mAudioSink->pause();
298         } else {
299             mAudioTrack->pause();
300         }
301     }
302 
303     mPlaying = false;
304 }
305 
resume()306 status_t AudioPlayer::resume() {
307     CHECK(mStarted);
308     status_t err;
309 
310     if (mAudioSink.get() != NULL) {
311         err = mAudioSink->start();
312     } else {
313         err = mAudioTrack->start();
314     }
315 
316     if (err == OK) {
317         mPlaying = true;
318     }
319 
320     return err;
321 }
322 
reset()323 void AudioPlayer::reset() {
324     CHECK(mStarted);
325 
326     ALOGV("reset: mPlaying=%d mReachedEOS=%d useOffload=%d",
327                                 mPlaying, mReachedEOS, useOffload() );
328 
329     if (mAudioSink.get() != NULL) {
330         mAudioSink->stop();
331         // If we're closing and have reached EOS, we don't want to flush
332         // the track because if it is offloaded there could be a small
333         // amount of residual data in the hardware buffer which we must
334         // play to give gapless playback.
335         // But if we're resetting when paused or before we've reached EOS
336         // we can't be doing a gapless playback and there could be a large
337         // amount of data queued in the hardware if the track is offloaded,
338         // so we must flush to prevent a track switch being delayed playing
339         // the buffered data that we don't want now
340         if (!mPlaying || !mReachedEOS) {
341             mAudioSink->flush();
342         }
343 
344         mAudioSink->close();
345     } else {
346         mAudioTrack->stop();
347 
348         if (!mPlaying || !mReachedEOS) {
349             mAudioTrack->flush();
350         }
351 
352         mAudioTrack.clear();
353     }
354 
355     // Make sure to release any buffer we hold onto so that the
356     // source is able to stop().
357 
358     if (mFirstBuffer != NULL) {
359         mFirstBuffer->release();
360         mFirstBuffer = NULL;
361     }
362 
363     if (mInputBuffer != NULL) {
364         ALOGV("AudioPlayer releasing input buffer.");
365 
366         mInputBuffer->release();
367         mInputBuffer = NULL;
368     }
369 
370     mSource->stop();
371 
372     // The following hack is necessary to ensure that the OMX
373     // component is completely released by the time we may try
374     // to instantiate it again.
375     // When offloading, the OMX component is not used so this hack
376     // is not needed
377     if (!useOffload()) {
378         wp<MediaSource> tmp = mSource;
379         mSource.clear();
380         while (tmp.promote() != NULL) {
381             usleep(1000);
382         }
383     } else {
384         mSource.clear();
385     }
386     IPCThreadState::self()->flushCommands();
387 
388     mNumFramesPlayed = 0;
389     mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
390     mPositionTimeMediaUs = -1;
391     mPositionTimeRealUs = -1;
392     mSeeking = false;
393     mSeekTimeUs = 0;
394     mReachedEOS = false;
395     mFinalStatus = OK;
396     mStarted = false;
397     mPlaying = false;
398     mStartPosUs = 0;
399 }
400 
401 
reachedEOS(status_t * finalStatus)402 bool AudioPlayer::reachedEOS(status_t *finalStatus) {
403     *finalStatus = OK;
404 
405     Mutex::Autolock autoLock(mLock);
406     *finalStatus = mFinalStatus;
407     return mReachedEOS;
408 }
409 
setPlaybackRate(const AudioPlaybackRate & rate)410 status_t AudioPlayer::setPlaybackRate(const AudioPlaybackRate &rate) {
411     if (mAudioSink.get() != NULL) {
412         return mAudioSink->setPlaybackRate(rate);
413     } else if (mAudioTrack != 0){
414         return mAudioTrack->setPlaybackRate(rate);
415     } else {
416         return NO_INIT;
417     }
418 }
419 
getPlaybackRate(AudioPlaybackRate * rate)420 status_t AudioPlayer::getPlaybackRate(AudioPlaybackRate *rate /* nonnull */) {
421     if (mAudioSink.get() != NULL) {
422         return mAudioSink->getPlaybackRate(rate);
423     } else if (mAudioTrack != 0) {
424         *rate = mAudioTrack->getPlaybackRate();
425         return OK;
426     } else {
427         return NO_INIT;
428     }
429 }
430 
431 // static
AudioSinkCallback(MediaPlayerBase::AudioSink *,void * buffer,size_t size,void * cookie,MediaPlayerBase::AudioSink::cb_event_t event)432 size_t AudioPlayer::AudioSinkCallback(
433         MediaPlayerBase::AudioSink * /* audioSink */,
434         void *buffer, size_t size, void *cookie,
435         MediaPlayerBase::AudioSink::cb_event_t event) {
436     AudioPlayer *me = (AudioPlayer *)cookie;
437 
438     switch(event) {
439     case MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER:
440         return me->fillBuffer(buffer, size);
441 
442     case MediaPlayerBase::AudioSink::CB_EVENT_STREAM_END:
443         ALOGV("AudioSinkCallback: stream end");
444         me->mReachedEOS = true;
445         break;
446 
447     case MediaPlayerBase::AudioSink::CB_EVENT_TEAR_DOWN:
448         ALOGV("AudioSinkCallback: Tear down event");
449         break;
450     }
451 
452     return 0;
453 }
454 
onMoreData(const AudioTrack::Buffer & buffer)455 size_t AudioPlayer::onMoreData(const AudioTrack::Buffer& buffer) {
456     return fillBuffer(buffer.data(), buffer.size());
457 }
458 
onStreamEnd()459 void AudioPlayer::onStreamEnd() {
460     mReachedEOS = true;
461 }
462 
fillBuffer(void * data,size_t size)463 size_t AudioPlayer::fillBuffer(void *data, size_t size) {
464     if (mNumFramesPlayed == 0) {
465         ALOGV("AudioCallback");
466     }
467 
468     if (mReachedEOS) {
469         return 0;
470     }
471 
472     size_t size_done = 0;
473     size_t size_remaining = size;
474     while (size_remaining > 0) {
475         MediaSource::ReadOptions options;
476         bool refreshSeekTime = false;
477 
478         {
479             Mutex::Autolock autoLock(mLock);
480 
481             if (mSeeking) {
482                 if (mIsFirstBuffer) {
483                     if (mFirstBuffer != NULL) {
484                         mFirstBuffer->release();
485                         mFirstBuffer = NULL;
486                     }
487                     mIsFirstBuffer = false;
488                 }
489 
490                 options.setSeekTo(mSeekTimeUs);
491                 refreshSeekTime = true;
492 
493                 if (mInputBuffer != NULL) {
494                     mInputBuffer->release();
495                     mInputBuffer = NULL;
496                 }
497 
498                 mSeeking = false;
499             }
500         }
501 
502         if (mInputBuffer == NULL) {
503             status_t err;
504 
505             if (mIsFirstBuffer) {
506                 mInputBuffer = mFirstBuffer;
507                 mFirstBuffer = NULL;
508                 err = mFirstBufferResult;
509 
510                 mIsFirstBuffer = false;
511             } else {
512                 err = mSource->read(&mInputBuffer, &options);
513             }
514 
515             CHECK((err == OK && mInputBuffer != NULL)
516                    || (err != OK && mInputBuffer == NULL));
517 
518             Mutex::Autolock autoLock(mLock);
519 
520             if (err != OK) {
521                 if (!mReachedEOS) {
522                     if (useOffload()) {
523                         // no more buffers to push - stop() and wait for STREAM_END
524                         // don't set mReachedEOS until stream end received
525                         if (mAudioSink != NULL) {
526                             mAudioSink->stop();
527                         } else {
528                             mAudioTrack->stop();
529                         }
530                     } else {
531                         mReachedEOS = true;
532                     }
533                 }
534 
535                 mFinalStatus = err;
536                 break;
537             }
538 
539             if (mAudioSink != NULL) {
540                 mLatencyUs = (int64_t)mAudioSink->latency() * 1000;
541             } else {
542                 mLatencyUs = (int64_t)mAudioTrack->latency() * 1000;
543             }
544 
545             if(mInputBuffer->range_length() != 0) {
546                 CHECK(mInputBuffer->meta_data().findInt64(
547                         kKeyTime, &mPositionTimeMediaUs));
548             }
549 
550             // need to adjust the mStartPosUs for offload decoding since parser
551             // might not be able to get the exact seek time requested.
552             if (refreshSeekTime) {
553                 if (useOffload()) {
554                     mStartPosUs = mPositionTimeMediaUs;
555                     ALOGV("adjust seek time to: %.2f", mStartPosUs/ 1E6);
556                 }
557                 // clear seek time with mLock locked and once we have valid mPositionTimeMediaUs
558                 // and mPositionTimeRealUs
559                 // before clearing mSeekTimeUs check if a new seek request has been received while
560                 // we were reading from the source with mLock released.
561                 if (!mSeeking) {
562                     mSeekTimeUs = 0;
563                 }
564             }
565 
566             if (!useOffload()) {
567                 mPositionTimeRealUs =
568                     ((mNumFramesPlayed + size_done / mFrameSize) * 1000000)
569                         / mSampleRate;
570                 ALOGV("buffer->size() = %zu, "
571                      "mPositionTimeMediaUs=%.2f mPositionTimeRealUs=%.2f",
572                      mInputBuffer->range_length(),
573                      mPositionTimeMediaUs / 1E6, mPositionTimeRealUs / 1E6);
574             }
575 
576         }
577 
578         if (mInputBuffer->range_length() == 0) {
579             mInputBuffer->release();
580             mInputBuffer = NULL;
581 
582             continue;
583         }
584 
585         size_t copy = size_remaining;
586         if (copy > mInputBuffer->range_length()) {
587             copy = mInputBuffer->range_length();
588         }
589 
590         memcpy((char *)data + size_done,
591                (const char *)mInputBuffer->data() + mInputBuffer->range_offset(),
592                copy);
593 
594         mInputBuffer->set_range(mInputBuffer->range_offset() + copy,
595                                 mInputBuffer->range_length() - copy);
596 
597         size_done += copy;
598         size_remaining -= copy;
599     }
600 
601     if (useOffload()) {
602         // We must ask the hardware what it has played
603         mPositionTimeRealUs = getOutputPlayPositionUs_l();
604         ALOGV("mPositionTimeMediaUs=%.2f mPositionTimeRealUs=%.2f",
605              mPositionTimeMediaUs / 1E6, mPositionTimeRealUs / 1E6);
606     }
607 
608     {
609         Mutex::Autolock autoLock(mLock);
610         mNumFramesPlayed += size_done / mFrameSize;
611         mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
612     }
613 
614     return size_done;
615 }
616 
getOutputPlayPositionUs_l()617 int64_t AudioPlayer::getOutputPlayPositionUs_l()
618 {
619     uint32_t playedSamples = 0;
620     uint32_t sampleRate;
621     if (mAudioSink != NULL) {
622         mAudioSink->getPosition(&playedSamples);
623         sampleRate = mAudioSink->getSampleRate();
624     } else {
625         mAudioTrack->getPosition(&playedSamples);
626         sampleRate = mAudioTrack->getSampleRate();
627     }
628     if (sampleRate != 0) {
629         mSampleRate = sampleRate;
630     }
631 
632     int64_t playedUs;
633     if (mSampleRate != 0) {
634         playedUs = (static_cast<int64_t>(playedSamples) * 1000000 ) / mSampleRate;
635     } else {
636         playedUs = 0;
637     }
638 
639     // HAL position is relative to the first buffer we sent at mStartPosUs
640     const int64_t renderedDuration = mStartPosUs + playedUs;
641     ALOGV("getOutputPlayPositionUs_l %" PRId64, renderedDuration);
642     return renderedDuration;
643 }
644 
seekTo(int64_t time_us)645 status_t AudioPlayer::seekTo(int64_t time_us) {
646     Mutex::Autolock autoLock(mLock);
647 
648     ALOGV("seekTo( %" PRId64 " )", time_us);
649 
650     mSeeking = true;
651     mPositionTimeRealUs = mPositionTimeMediaUs = -1;
652     mReachedEOS = false;
653     mSeekTimeUs = time_us;
654     mStartPosUs = time_us;
655 
656     // Flush resets the number of played frames
657     mNumFramesPlayed = 0;
658     mNumFramesPlayedSysTimeUs = ALooper::GetNowUs();
659 
660     if (mAudioSink != NULL) {
661         if (mPlaying) {
662             mAudioSink->pause();
663         }
664         mAudioSink->flush();
665         if (mPlaying) {
666             mAudioSink->start();
667         }
668     } else {
669         if (mPlaying) {
670             mAudioTrack->pause();
671         }
672         mAudioTrack->flush();
673         if (mPlaying) {
674             mAudioTrack->start();
675         }
676     }
677 
678     return OK;
679 }
680 
681 }
682