NuPlayerRenderer.cpp revision 15ce5f35187659ad757ee2ea6f70dcef5010c2c2
1/*
2 * Copyright (C) 2010 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//#define LOG_NDEBUG 0
18#define LOG_TAG "NuPlayerRenderer"
19#include <utils/Log.h>
20
21#include "NuPlayerRenderer.h"
22
23#include <cutils/properties.h>
24
25#include <media/stagefright/foundation/ABuffer.h>
26#include <media/stagefright/foundation/ADebug.h>
27#include <media/stagefright/foundation/AMessage.h>
28#include <media/stagefright/foundation/AUtils.h>
29#include <media/stagefright/MediaErrors.h>
30#include <media/stagefright/MetaData.h>
31#include <media/stagefright/Utils.h>
32
33#include <VideoFrameScheduler.h>
34
35#include <inttypes.h>
36
37namespace android {
38
39// Maximum time in paused state when offloading audio decompression. When elapsed, the AudioSink
40// is closed to allow the audio DSP to power down.
41static const int64_t kOffloadPauseMaxUs = 60000000ll;
42
43// static
44const int64_t NuPlayer::Renderer::kMinPositionUpdateDelayUs = 100000ll;
45
46static bool sFrameAccurateAVsync = false;
47
48static void readProperties() {
49    char value[PROPERTY_VALUE_MAX];
50    if (property_get("persist.sys.media.avsync", value, NULL)) {
51        sFrameAccurateAVsync =
52            !strcmp("1", value) || !strcasecmp("true", value);
53    }
54}
55
56NuPlayer::Renderer::Renderer(
57        const sp<MediaPlayerBase::AudioSink> &sink,
58        const sp<AMessage> &notify,
59        uint32_t flags)
60    : mAudioSink(sink),
61      mNotify(notify),
62      mFlags(flags),
63      mNumFramesWritten(0),
64      mDrainAudioQueuePending(false),
65      mDrainVideoQueuePending(false),
66      mAudioQueueGeneration(0),
67      mVideoQueueGeneration(0),
68      mAudioFirstAnchorTimeMediaUs(-1),
69      mAnchorTimeMediaUs(-1),
70      mAnchorTimeRealUs(-1),
71      mAnchorNumFramesWritten(-1),
72      mAnchorMaxMediaUs(-1),
73      mVideoLateByUs(0ll),
74      mHasAudio(false),
75      mHasVideo(false),
76      mPauseStartedTimeRealUs(-1),
77      mFlushingAudio(false),
78      mFlushingVideo(false),
79      mSyncQueues(false),
80      mPaused(false),
81      mVideoSampleReceived(false),
82      mVideoRenderingStarted(false),
83      mVideoRenderingStartGeneration(0),
84      mAudioRenderingStartGeneration(0),
85      mAudioOffloadPauseTimeoutGeneration(0),
86      mAudioOffloadTornDown(false),
87      mCurrentOffloadInfo(AUDIO_INFO_INITIALIZER),
88      mTotalBuffersQueued(0),
89      mLastAudioBufferDrained(0) {
90    readProperties();
91}
92
93NuPlayer::Renderer::~Renderer() {
94    if (offloadingAudio()) {
95        mAudioSink->stop();
96        mAudioSink->flush();
97        mAudioSink->close();
98    }
99}
100
101void NuPlayer::Renderer::queueBuffer(
102        bool audio,
103        const sp<ABuffer> &buffer,
104        const sp<AMessage> &notifyConsumed) {
105    sp<AMessage> msg = new AMessage(kWhatQueueBuffer, id());
106    msg->setInt32("audio", static_cast<int32_t>(audio));
107    msg->setBuffer("buffer", buffer);
108    msg->setMessage("notifyConsumed", notifyConsumed);
109    msg->post();
110}
111
112void NuPlayer::Renderer::queueEOS(bool audio, status_t finalResult) {
113    CHECK_NE(finalResult, (status_t)OK);
114
115    sp<AMessage> msg = new AMessage(kWhatQueueEOS, id());
116    msg->setInt32("audio", static_cast<int32_t>(audio));
117    msg->setInt32("finalResult", finalResult);
118    msg->post();
119}
120
121void NuPlayer::Renderer::flush(bool audio) {
122    {
123        Mutex::Autolock autoLock(mFlushLock);
124        if (audio) {
125            if (mFlushingAudio) {
126                return;
127            }
128            mFlushingAudio = true;
129        } else {
130            if (mFlushingVideo) {
131                return;
132            }
133            mFlushingVideo = true;
134        }
135    }
136
137    sp<AMessage> msg = new AMessage(kWhatFlush, id());
138    msg->setInt32("audio", static_cast<int32_t>(audio));
139    msg->post();
140}
141
142void NuPlayer::Renderer::signalTimeDiscontinuity() {
143    Mutex::Autolock autoLock(mLock);
144    // CHECK(mAudioQueue.empty());
145    // CHECK(mVideoQueue.empty());
146    setAudioFirstAnchorTime(-1);
147    setAnchorTime(-1, -1);
148    setVideoLateByUs(0);
149    mSyncQueues = false;
150}
151
152void NuPlayer::Renderer::signalAudioSinkChanged() {
153    (new AMessage(kWhatAudioSinkChanged, id()))->post();
154}
155
156void NuPlayer::Renderer::signalDisableOffloadAudio() {
157    (new AMessage(kWhatDisableOffloadAudio, id()))->post();
158}
159
160void NuPlayer::Renderer::pause() {
161    (new AMessage(kWhatPause, id()))->post();
162}
163
164void NuPlayer::Renderer::resume() {
165    (new AMessage(kWhatResume, id()))->post();
166}
167
168void NuPlayer::Renderer::setVideoFrameRate(float fps) {
169    sp<AMessage> msg = new AMessage(kWhatSetVideoFrameRate, id());
170    msg->setFloat("frame-rate", fps);
171    msg->post();
172}
173
174status_t NuPlayer::Renderer::getCurrentPosition(int64_t *mediaUs) {
175    return getCurrentPosition(mediaUs, ALooper::GetNowUs());
176}
177
178status_t NuPlayer::Renderer::getCurrentPosition(
179        int64_t *mediaUs, int64_t nowUs, bool allowPastQueuedVideo) {
180    Mutex::Autolock autoLock(mTimeLock);
181    if (!mHasAudio && !mHasVideo) {
182        return NO_INIT;
183    }
184
185    if (mAnchorTimeMediaUs < 0) {
186        return NO_INIT;
187    }
188
189    int64_t positionUs = (nowUs - mAnchorTimeRealUs) + mAnchorTimeMediaUs;
190
191    if (mPauseStartedTimeRealUs != -1) {
192        positionUs -= (nowUs - mPauseStartedTimeRealUs);
193    }
194
195    // limit position to the last queued media time (for video only stream
196    // position will be discrete as we don't know how long each frame lasts)
197    if (mAnchorMaxMediaUs >= 0 && !allowPastQueuedVideo) {
198        if (positionUs > mAnchorMaxMediaUs) {
199            positionUs = mAnchorMaxMediaUs;
200        }
201    }
202
203    if (positionUs < mAudioFirstAnchorTimeMediaUs) {
204        positionUs = mAudioFirstAnchorTimeMediaUs;
205    }
206
207    *mediaUs = (positionUs <= 0) ? 0 : positionUs;
208    return OK;
209}
210
211void NuPlayer::Renderer::setHasMedia(bool audio) {
212    Mutex::Autolock autoLock(mTimeLock);
213    if (audio) {
214        mHasAudio = true;
215    } else {
216        mHasVideo = true;
217    }
218}
219
220void NuPlayer::Renderer::setAudioFirstAnchorTime(int64_t mediaUs) {
221    Mutex::Autolock autoLock(mTimeLock);
222    mAudioFirstAnchorTimeMediaUs = mediaUs;
223}
224
225void NuPlayer::Renderer::setAudioFirstAnchorTimeIfNeeded(int64_t mediaUs) {
226    Mutex::Autolock autoLock(mTimeLock);
227    if (mAudioFirstAnchorTimeMediaUs == -1) {
228        mAudioFirstAnchorTimeMediaUs = mediaUs;
229    }
230}
231
232void NuPlayer::Renderer::setAnchorTime(
233        int64_t mediaUs, int64_t realUs, int64_t numFramesWritten, bool resume) {
234    Mutex::Autolock autoLock(mTimeLock);
235    mAnchorTimeMediaUs = mediaUs;
236    mAnchorTimeRealUs = realUs;
237    mAnchorNumFramesWritten = numFramesWritten;
238    if (resume) {
239        mPauseStartedTimeRealUs = -1;
240    }
241}
242
243void NuPlayer::Renderer::setVideoLateByUs(int64_t lateUs) {
244    Mutex::Autolock autoLock(mTimeLock);
245    mVideoLateByUs = lateUs;
246}
247
248int64_t NuPlayer::Renderer::getVideoLateByUs() {
249    Mutex::Autolock autoLock(mTimeLock);
250    return mVideoLateByUs;
251}
252
253void NuPlayer::Renderer::setPauseStartedTimeRealUs(int64_t realUs) {
254    Mutex::Autolock autoLock(mTimeLock);
255    mPauseStartedTimeRealUs = realUs;
256}
257
258bool NuPlayer::Renderer::openAudioSink(
259        const sp<AMessage> &format,
260        bool offloadOnly,
261        bool hasVideo,
262        uint32_t flags) {
263    sp<AMessage> msg = new AMessage(kWhatOpenAudioSink, id());
264    msg->setMessage("format", format);
265    msg->setInt32("offload-only", offloadOnly);
266    msg->setInt32("has-video", hasVideo);
267    msg->setInt32("flags", flags);
268
269    sp<AMessage> response;
270    msg->postAndAwaitResponse(&response);
271
272    int32_t offload;
273    CHECK(response->findInt32("offload", &offload));
274    return (offload != 0);
275}
276
277void NuPlayer::Renderer::closeAudioSink() {
278    sp<AMessage> msg = new AMessage(kWhatCloseAudioSink, id());
279
280    sp<AMessage> response;
281    msg->postAndAwaitResponse(&response);
282}
283
284void NuPlayer::Renderer::onMessageReceived(const sp<AMessage> &msg) {
285    switch (msg->what()) {
286        case kWhatOpenAudioSink:
287        {
288            sp<AMessage> format;
289            CHECK(msg->findMessage("format", &format));
290
291            int32_t offloadOnly;
292            CHECK(msg->findInt32("offload-only", &offloadOnly));
293
294            int32_t hasVideo;
295            CHECK(msg->findInt32("has-video", &hasVideo));
296
297            uint32_t flags;
298            CHECK(msg->findInt32("flags", (int32_t *)&flags));
299
300            bool offload = onOpenAudioSink(format, offloadOnly, hasVideo, flags);
301
302            sp<AMessage> response = new AMessage;
303            response->setInt32("offload", offload);
304
305            uint32_t replyID;
306            CHECK(msg->senderAwaitsResponse(&replyID));
307            response->postReply(replyID);
308
309            break;
310        }
311
312        case kWhatCloseAudioSink:
313        {
314            uint32_t replyID;
315            CHECK(msg->senderAwaitsResponse(&replyID));
316
317            onCloseAudioSink();
318
319            sp<AMessage> response = new AMessage;
320            response->postReply(replyID);
321            break;
322        }
323
324        case kWhatStopAudioSink:
325        {
326            mAudioSink->stop();
327            break;
328        }
329
330        case kWhatDrainAudioQueue:
331        {
332            int32_t generation;
333            CHECK(msg->findInt32("generation", &generation));
334            if (generation != mAudioQueueGeneration) {
335                break;
336            }
337
338            mDrainAudioQueuePending = false;
339
340            if (onDrainAudioQueue()) {
341                uint32_t numFramesPlayed;
342                CHECK_EQ(mAudioSink->getPosition(&numFramesPlayed),
343                         (status_t)OK);
344
345                uint32_t numFramesPendingPlayout =
346                    mNumFramesWritten - numFramesPlayed;
347
348                // This is how long the audio sink will have data to
349                // play back.
350                int64_t delayUs =
351                    mAudioSink->msecsPerFrame()
352                        * numFramesPendingPlayout * 1000ll;
353
354                // Let's give it more data after about half that time
355                // has elapsed.
356                // kWhatDrainAudioQueue is used for non-offloading mode,
357                // and mLock is used only for offloading mode. Therefore,
358                // no need to acquire mLock here.
359                postDrainAudioQueue_l(delayUs / 2);
360            }
361            break;
362        }
363
364        case kWhatDrainVideoQueue:
365        {
366            int32_t generation;
367            CHECK(msg->findInt32("generation", &generation));
368            if (generation != mVideoQueueGeneration) {
369                break;
370            }
371
372            mDrainVideoQueuePending = false;
373
374            onDrainVideoQueue();
375
376            postDrainVideoQueue();
377            break;
378        }
379
380        case kWhatPostDrainVideoQueue:
381        {
382            int32_t generation;
383            CHECK(msg->findInt32("generation", &generation));
384            if (generation != mVideoQueueGeneration) {
385                break;
386            }
387
388            mDrainVideoQueuePending = false;
389            postDrainVideoQueue();
390            break;
391        }
392
393        case kWhatQueueBuffer:
394        {
395            onQueueBuffer(msg);
396            break;
397        }
398
399        case kWhatQueueEOS:
400        {
401            onQueueEOS(msg);
402            break;
403        }
404
405        case kWhatFlush:
406        {
407            onFlush(msg);
408            break;
409        }
410
411        case kWhatAudioSinkChanged:
412        {
413            onAudioSinkChanged();
414            break;
415        }
416
417        case kWhatDisableOffloadAudio:
418        {
419            onDisableOffloadAudio();
420            break;
421        }
422
423        case kWhatPause:
424        {
425            onPause();
426            break;
427        }
428
429        case kWhatResume:
430        {
431            onResume();
432            break;
433        }
434
435        case kWhatSetVideoFrameRate:
436        {
437            float fps;
438            CHECK(msg->findFloat("frame-rate", &fps));
439            onSetVideoFrameRate(fps);
440            break;
441        }
442
443        case kWhatAudioOffloadTearDown:
444        {
445            onAudioOffloadTearDown(kDueToError);
446            break;
447        }
448
449        case kWhatAudioOffloadPauseTimeout:
450        {
451            int32_t generation;
452            CHECK(msg->findInt32("generation", &generation));
453            if (generation != mAudioOffloadPauseTimeoutGeneration) {
454                break;
455            }
456            ALOGV("Audio Offload tear down due to pause timeout.");
457            onAudioOffloadTearDown(kDueToTimeout);
458            break;
459        }
460
461        default:
462            TRESPASS();
463            break;
464    }
465}
466
467void NuPlayer::Renderer::postDrainAudioQueue_l(int64_t delayUs) {
468    if (mDrainAudioQueuePending || mSyncQueues || mPaused
469            || offloadingAudio()) {
470        return;
471    }
472
473    if (mAudioQueue.empty()) {
474        return;
475    }
476
477    mDrainAudioQueuePending = true;
478    sp<AMessage> msg = new AMessage(kWhatDrainAudioQueue, id());
479    msg->setInt32("generation", mAudioQueueGeneration);
480    msg->post(delayUs);
481}
482
483void NuPlayer::Renderer::prepareForMediaRenderingStart() {
484    mAudioRenderingStartGeneration = mAudioQueueGeneration;
485    mVideoRenderingStartGeneration = mVideoQueueGeneration;
486}
487
488void NuPlayer::Renderer::notifyIfMediaRenderingStarted() {
489    if (mVideoRenderingStartGeneration == mVideoQueueGeneration &&
490        mAudioRenderingStartGeneration == mAudioQueueGeneration) {
491        mVideoRenderingStartGeneration = -1;
492        mAudioRenderingStartGeneration = -1;
493
494        sp<AMessage> notify = mNotify->dup();
495        notify->setInt32("what", kWhatMediaRenderingStart);
496        notify->post();
497    }
498}
499
500// static
501size_t NuPlayer::Renderer::AudioSinkCallback(
502        MediaPlayerBase::AudioSink * /* audioSink */,
503        void *buffer,
504        size_t size,
505        void *cookie,
506        MediaPlayerBase::AudioSink::cb_event_t event) {
507    NuPlayer::Renderer *me = (NuPlayer::Renderer *)cookie;
508
509    switch (event) {
510        case MediaPlayerBase::AudioSink::CB_EVENT_FILL_BUFFER:
511        {
512            return me->fillAudioBuffer(buffer, size);
513            break;
514        }
515
516        case MediaPlayerBase::AudioSink::CB_EVENT_STREAM_END:
517        {
518            me->notifyEOS(true /* audio */, ERROR_END_OF_STREAM);
519            break;
520        }
521
522        case MediaPlayerBase::AudioSink::CB_EVENT_TEAR_DOWN:
523        {
524            me->notifyAudioOffloadTearDown();
525            break;
526        }
527    }
528
529    return 0;
530}
531
532size_t NuPlayer::Renderer::fillAudioBuffer(void *buffer, size_t size) {
533    Mutex::Autolock autoLock(mLock);
534
535    if (!offloadingAudio() || mPaused) {
536        return 0;
537    }
538
539    bool hasEOS = false;
540
541    size_t sizeCopied = 0;
542    bool firstEntry = true;
543    while (sizeCopied < size && !mAudioQueue.empty()) {
544        QueueEntry *entry = &*mAudioQueue.begin();
545
546        if (entry->mBuffer == NULL) { // EOS
547            hasEOS = true;
548            mAudioQueue.erase(mAudioQueue.begin());
549            entry = NULL;
550            break;
551        }
552
553        if (firstEntry && entry->mOffset == 0) {
554            firstEntry = false;
555            int64_t mediaTimeUs;
556            CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
557            ALOGV("rendering audio at media time %.2f secs", mediaTimeUs / 1E6);
558            setAudioFirstAnchorTimeIfNeeded(mediaTimeUs);
559        }
560
561        size_t copy = entry->mBuffer->size() - entry->mOffset;
562        size_t sizeRemaining = size - sizeCopied;
563        if (copy > sizeRemaining) {
564            copy = sizeRemaining;
565        }
566
567        memcpy((char *)buffer + sizeCopied,
568               entry->mBuffer->data() + entry->mOffset,
569               copy);
570
571        entry->mOffset += copy;
572        if (entry->mOffset == entry->mBuffer->size()) {
573            entry->mNotifyConsumed->post();
574            mAudioQueue.erase(mAudioQueue.begin());
575            entry = NULL;
576        }
577        sizeCopied += copy;
578        notifyIfMediaRenderingStarted();
579    }
580
581    if (mAudioFirstAnchorTimeMediaUs >= 0) {
582        int64_t nowUs = ALooper::GetNowUs();
583        setAnchorTime(mAudioFirstAnchorTimeMediaUs, nowUs - getPlayedOutAudioDurationUs(nowUs));
584    }
585
586    // we don't know how much data we are queueing for offloaded tracks
587    mAnchorMaxMediaUs = -1;
588
589    if (hasEOS) {
590        (new AMessage(kWhatStopAudioSink, id()))->post();
591    }
592
593    return sizeCopied;
594}
595
596bool NuPlayer::Renderer::onDrainAudioQueue() {
597    uint32_t numFramesPlayed;
598    if (mAudioSink->getPosition(&numFramesPlayed) != OK) {
599        return false;
600    }
601
602    ssize_t numFramesAvailableToWrite =
603        mAudioSink->frameCount() - (mNumFramesWritten - numFramesPlayed);
604
605#if 0
606    if (numFramesAvailableToWrite == mAudioSink->frameCount()) {
607        ALOGI("audio sink underrun");
608    } else {
609        ALOGV("audio queue has %d frames left to play",
610             mAudioSink->frameCount() - numFramesAvailableToWrite);
611    }
612#endif
613
614    size_t numBytesAvailableToWrite =
615        numFramesAvailableToWrite * mAudioSink->frameSize();
616
617    while (numBytesAvailableToWrite > 0 && !mAudioQueue.empty()) {
618        QueueEntry *entry = &*mAudioQueue.begin();
619
620        mLastAudioBufferDrained = entry->mBufferOrdinal;
621
622        if (entry->mBuffer == NULL) {
623            // EOS
624            int64_t postEOSDelayUs = 0;
625            if (mAudioSink->needsTrailingPadding()) {
626                postEOSDelayUs = getPendingAudioPlayoutDurationUs(ALooper::GetNowUs());
627            }
628            notifyEOS(true /* audio */, entry->mFinalResult, postEOSDelayUs);
629
630            mAudioQueue.erase(mAudioQueue.begin());
631            entry = NULL;
632            // Need to stop the track here, because that will play out the last
633            // little bit at the end of the file. Otherwise short files won't play.
634            mAudioSink->stop();
635            mNumFramesWritten = 0;
636            return false;
637        }
638
639        if (entry->mOffset == 0) {
640            int64_t mediaTimeUs;
641            CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
642            ALOGV("rendering audio at media time %.2f secs", mediaTimeUs / 1E6);
643            onNewAudioMediaTime(mediaTimeUs);
644        }
645
646        size_t copy = entry->mBuffer->size() - entry->mOffset;
647        if (copy > numBytesAvailableToWrite) {
648            copy = numBytesAvailableToWrite;
649        }
650
651        ssize_t written = mAudioSink->write(entry->mBuffer->data() + entry->mOffset, copy);
652        if (written < 0) {
653            // An error in AudioSink write is fatal here.
654            LOG_ALWAYS_FATAL("AudioSink write error(%zd) when writing %zu bytes", written, copy);
655        }
656
657        entry->mOffset += written;
658        if (entry->mOffset == entry->mBuffer->size()) {
659            entry->mNotifyConsumed->post();
660            mAudioQueue.erase(mAudioQueue.begin());
661
662            entry = NULL;
663        }
664
665        numBytesAvailableToWrite -= written;
666        size_t copiedFrames = written / mAudioSink->frameSize();
667        mNumFramesWritten += copiedFrames;
668
669        notifyIfMediaRenderingStarted();
670
671        if (written != (ssize_t)copy) {
672            // A short count was received from AudioSink::write()
673            //
674            // AudioSink write should block until exactly the number of bytes are delivered.
675            // But it may return with a short count (without an error) when:
676            //
677            // 1) Size to be copied is not a multiple of the frame size. We consider this fatal.
678            // 2) AudioSink is an AudioCache for data retrieval, and the AudioCache is exceeded.
679
680            // (Case 1)
681            // Must be a multiple of the frame size.  If it is not a multiple of a frame size, it
682            // needs to fail, as we should not carry over fractional frames between calls.
683            CHECK_EQ(copy % mAudioSink->frameSize(), 0);
684
685            // (Case 2)
686            // Return early to the caller.
687            // Beware of calling immediately again as this may busy-loop if you are not careful.
688            ALOGW("AudioSink write short frame count %zd < %zu", written, copy);
689            break;
690        }
691    }
692    mAnchorMaxMediaUs =
693        mAnchorTimeMediaUs +
694                (int64_t)(max((long long)mNumFramesWritten - mAnchorNumFramesWritten, 0LL)
695                        * 1000LL * mAudioSink->msecsPerFrame());
696
697    return !mAudioQueue.empty();
698}
699
700int64_t NuPlayer::Renderer::getPendingAudioPlayoutDurationUs(int64_t nowUs) {
701    int64_t writtenAudioDurationUs =
702        mNumFramesWritten * 1000LL * mAudioSink->msecsPerFrame();
703    return writtenAudioDurationUs - getPlayedOutAudioDurationUs(nowUs);
704}
705
706int64_t NuPlayer::Renderer::getRealTimeUs(int64_t mediaTimeUs, int64_t nowUs) {
707    int64_t currentPositionUs;
708    if (getCurrentPosition(&currentPositionUs, nowUs, true /* allowPastQueuedVideo */) != OK) {
709        // If failed to get current position, e.g. due to audio clock is not ready, then just
710        // play out video immediately without delay.
711        return nowUs;
712    }
713    return (mediaTimeUs - currentPositionUs) + nowUs;
714}
715
716void NuPlayer::Renderer::onNewAudioMediaTime(int64_t mediaTimeUs) {
717    // TRICKY: vorbis decoder generates multiple frames with the same
718    // timestamp, so only update on the first frame with a given timestamp
719    if (mediaTimeUs == mAnchorTimeMediaUs) {
720        return;
721    }
722    setAudioFirstAnchorTimeIfNeeded(mediaTimeUs);
723    int64_t nowUs = ALooper::GetNowUs();
724    setAnchorTime(
725            mediaTimeUs, nowUs + getPendingAudioPlayoutDurationUs(nowUs), mNumFramesWritten);
726}
727
728void NuPlayer::Renderer::postDrainVideoQueue() {
729    if (mDrainVideoQueuePending
730            || mSyncQueues
731            || (mPaused && mVideoSampleReceived)) {
732        return;
733    }
734
735    if (mVideoQueue.empty()) {
736        return;
737    }
738
739    QueueEntry &entry = *mVideoQueue.begin();
740
741    sp<AMessage> msg = new AMessage(kWhatDrainVideoQueue, id());
742    msg->setInt32("generation", mVideoQueueGeneration);
743
744    if (entry.mBuffer == NULL) {
745        // EOS doesn't carry a timestamp.
746        msg->post();
747        mDrainVideoQueuePending = true;
748        return;
749    }
750
751    int64_t delayUs;
752    int64_t nowUs = ALooper::GetNowUs();
753    int64_t realTimeUs;
754    if (mFlags & FLAG_REAL_TIME) {
755        int64_t mediaTimeUs;
756        CHECK(entry.mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
757        realTimeUs = mediaTimeUs;
758    } else {
759        int64_t mediaTimeUs;
760        CHECK(entry.mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
761
762        if (mAnchorTimeMediaUs < 0) {
763            setAnchorTime(mediaTimeUs, nowUs);
764            realTimeUs = nowUs;
765        } else {
766            realTimeUs = getRealTimeUs(mediaTimeUs, nowUs);
767        }
768        if (!mHasAudio) {
769            mAnchorMaxMediaUs = mediaTimeUs + 100000; // smooth out videos >= 10fps
770        }
771
772        // Heuristics to handle situation when media time changed without a
773        // discontinuity. If we have not drained an audio buffer that was
774        // received after this buffer, repost in 10 msec. Otherwise repost
775        // in 500 msec.
776        delayUs = realTimeUs - nowUs;
777        if (delayUs > 500000) {
778            int64_t postDelayUs = 500000;
779            if (mHasAudio && (mLastAudioBufferDrained - entry.mBufferOrdinal) <= 0) {
780                postDelayUs = 10000;
781            }
782            msg->setWhat(kWhatPostDrainVideoQueue);
783            msg->post(postDelayUs);
784            mVideoScheduler->restart();
785            ALOGI("possible video time jump of %dms, retrying in %dms",
786                    (int)(delayUs / 1000), (int)(postDelayUs / 1000));
787            mDrainVideoQueuePending = true;
788            return;
789        }
790    }
791
792    realTimeUs = mVideoScheduler->schedule(realTimeUs * 1000) / 1000;
793    int64_t twoVsyncsUs = 2 * (mVideoScheduler->getVsyncPeriod() / 1000);
794
795    delayUs = realTimeUs - nowUs;
796
797    ALOGW_IF(delayUs > 500000, "unusually high delayUs: %" PRId64, delayUs);
798    // post 2 display refreshes before rendering is due
799    // FIXME currently this increases power consumption, so unless frame-accurate
800    // AV sync is requested, post closer to required render time (at 0.63 vsyncs)
801    if (!sFrameAccurateAVsync) {
802        twoVsyncsUs >>= 4;
803    }
804    msg->post(delayUs > twoVsyncsUs ? delayUs - twoVsyncsUs : 0);
805
806    mDrainVideoQueuePending = true;
807}
808
809void NuPlayer::Renderer::onDrainVideoQueue() {
810    if (mVideoQueue.empty()) {
811        return;
812    }
813
814    QueueEntry *entry = &*mVideoQueue.begin();
815
816    if (entry->mBuffer == NULL) {
817        // EOS
818
819        notifyEOS(false /* audio */, entry->mFinalResult);
820
821        mVideoQueue.erase(mVideoQueue.begin());
822        entry = NULL;
823
824        setVideoLateByUs(0);
825        return;
826    }
827
828    int64_t nowUs = -1;
829    int64_t realTimeUs;
830    if (mFlags & FLAG_REAL_TIME) {
831        CHECK(entry->mBuffer->meta()->findInt64("timeUs", &realTimeUs));
832    } else {
833        int64_t mediaTimeUs;
834        CHECK(entry->mBuffer->meta()->findInt64("timeUs", &mediaTimeUs));
835
836        nowUs = ALooper::GetNowUs();
837        realTimeUs = getRealTimeUs(mediaTimeUs, nowUs);
838    }
839
840    bool tooLate = false;
841
842    if (!mPaused) {
843        if (nowUs == -1) {
844            nowUs = ALooper::GetNowUs();
845        }
846        setVideoLateByUs(nowUs - realTimeUs);
847        tooLate = (mVideoLateByUs > 40000);
848
849        if (tooLate) {
850            ALOGV("video late by %lld us (%.2f secs)",
851                 mVideoLateByUs, mVideoLateByUs / 1E6);
852        } else {
853            ALOGV("rendering video at media time %.2f secs",
854                    (mFlags & FLAG_REAL_TIME ? realTimeUs :
855                    (realTimeUs + mAnchorTimeMediaUs - mAnchorTimeRealUs)) / 1E6);
856        }
857    } else {
858        setVideoLateByUs(0);
859        if (!mVideoSampleReceived && !mHasAudio) {
860            // This will ensure that the first frame after a flush won't be used as anchor
861            // when renderer is in paused state, because resume can happen any time after seek.
862            setAnchorTime(-1, -1);
863        }
864    }
865
866    entry->mNotifyConsumed->setInt64("timestampNs", realTimeUs * 1000ll);
867    entry->mNotifyConsumed->setInt32("render", !tooLate);
868    entry->mNotifyConsumed->post();
869    mVideoQueue.erase(mVideoQueue.begin());
870    entry = NULL;
871
872    mVideoSampleReceived = true;
873
874    if (!mPaused) {
875        if (!mVideoRenderingStarted) {
876            mVideoRenderingStarted = true;
877            notifyVideoRenderingStart();
878        }
879        notifyIfMediaRenderingStarted();
880    }
881}
882
883void NuPlayer::Renderer::notifyVideoRenderingStart() {
884    sp<AMessage> notify = mNotify->dup();
885    notify->setInt32("what", kWhatVideoRenderingStart);
886    notify->post();
887}
888
889void NuPlayer::Renderer::notifyEOS(bool audio, status_t finalResult, int64_t delayUs) {
890    sp<AMessage> notify = mNotify->dup();
891    notify->setInt32("what", kWhatEOS);
892    notify->setInt32("audio", static_cast<int32_t>(audio));
893    notify->setInt32("finalResult", finalResult);
894    notify->post(delayUs);
895}
896
897void NuPlayer::Renderer::notifyAudioOffloadTearDown() {
898    (new AMessage(kWhatAudioOffloadTearDown, id()))->post();
899}
900
901void NuPlayer::Renderer::onQueueBuffer(const sp<AMessage> &msg) {
902    int32_t audio;
903    CHECK(msg->findInt32("audio", &audio));
904
905    setHasMedia(audio);
906
907    if (mHasVideo) {
908        if (mVideoScheduler == NULL) {
909            mVideoScheduler = new VideoFrameScheduler();
910            mVideoScheduler->init();
911        }
912    }
913
914    if (dropBufferWhileFlushing(audio, msg)) {
915        return;
916    }
917
918    sp<ABuffer> buffer;
919    CHECK(msg->findBuffer("buffer", &buffer));
920
921    sp<AMessage> notifyConsumed;
922    CHECK(msg->findMessage("notifyConsumed", &notifyConsumed));
923
924    QueueEntry entry;
925    entry.mBuffer = buffer;
926    entry.mNotifyConsumed = notifyConsumed;
927    entry.mOffset = 0;
928    entry.mFinalResult = OK;
929    entry.mBufferOrdinal = ++mTotalBuffersQueued;
930
931    if (audio) {
932        Mutex::Autolock autoLock(mLock);
933        mAudioQueue.push_back(entry);
934        postDrainAudioQueue_l();
935    } else {
936        mVideoQueue.push_back(entry);
937        postDrainVideoQueue();
938    }
939
940    Mutex::Autolock autoLock(mLock);
941    if (!mSyncQueues || mAudioQueue.empty() || mVideoQueue.empty()) {
942        return;
943    }
944
945    sp<ABuffer> firstAudioBuffer = (*mAudioQueue.begin()).mBuffer;
946    sp<ABuffer> firstVideoBuffer = (*mVideoQueue.begin()).mBuffer;
947
948    if (firstAudioBuffer == NULL || firstVideoBuffer == NULL) {
949        // EOS signalled on either queue.
950        syncQueuesDone_l();
951        return;
952    }
953
954    int64_t firstAudioTimeUs;
955    int64_t firstVideoTimeUs;
956    CHECK(firstAudioBuffer->meta()
957            ->findInt64("timeUs", &firstAudioTimeUs));
958    CHECK(firstVideoBuffer->meta()
959            ->findInt64("timeUs", &firstVideoTimeUs));
960
961    int64_t diff = firstVideoTimeUs - firstAudioTimeUs;
962
963    ALOGV("queueDiff = %.2f secs", diff / 1E6);
964
965    if (diff > 100000ll) {
966        // Audio data starts More than 0.1 secs before video.
967        // Drop some audio.
968
969        (*mAudioQueue.begin()).mNotifyConsumed->post();
970        mAudioQueue.erase(mAudioQueue.begin());
971        return;
972    }
973
974    syncQueuesDone_l();
975}
976
977void NuPlayer::Renderer::syncQueuesDone_l() {
978    if (!mSyncQueues) {
979        return;
980    }
981
982    mSyncQueues = false;
983
984    if (!mAudioQueue.empty()) {
985        postDrainAudioQueue_l();
986    }
987
988    if (!mVideoQueue.empty()) {
989        postDrainVideoQueue();
990    }
991}
992
993void NuPlayer::Renderer::onQueueEOS(const sp<AMessage> &msg) {
994    int32_t audio;
995    CHECK(msg->findInt32("audio", &audio));
996
997    if (dropBufferWhileFlushing(audio, msg)) {
998        return;
999    }
1000
1001    int32_t finalResult;
1002    CHECK(msg->findInt32("finalResult", &finalResult));
1003
1004    QueueEntry entry;
1005    entry.mOffset = 0;
1006    entry.mFinalResult = finalResult;
1007
1008    if (audio) {
1009        Mutex::Autolock autoLock(mLock);
1010        if (mAudioQueue.empty() && mSyncQueues) {
1011            syncQueuesDone_l();
1012        }
1013        mAudioQueue.push_back(entry);
1014        postDrainAudioQueue_l();
1015    } else {
1016        if (mVideoQueue.empty() && mSyncQueues) {
1017            Mutex::Autolock autoLock(mLock);
1018            syncQueuesDone_l();
1019        }
1020        mVideoQueue.push_back(entry);
1021        postDrainVideoQueue();
1022    }
1023}
1024
1025void NuPlayer::Renderer::onFlush(const sp<AMessage> &msg) {
1026    int32_t audio;
1027    CHECK(msg->findInt32("audio", &audio));
1028
1029    {
1030        Mutex::Autolock autoLock(mFlushLock);
1031        if (audio) {
1032            mFlushingAudio = false;
1033        } else {
1034            mFlushingVideo = false;
1035        }
1036    }
1037
1038    // If we're currently syncing the queues, i.e. dropping audio while
1039    // aligning the first audio/video buffer times and only one of the
1040    // two queues has data, we may starve that queue by not requesting
1041    // more buffers from the decoder. If the other source then encounters
1042    // a discontinuity that leads to flushing, we'll never find the
1043    // corresponding discontinuity on the other queue.
1044    // Therefore we'll stop syncing the queues if at least one of them
1045    // is flushed.
1046    {
1047         Mutex::Autolock autoLock(mLock);
1048         syncQueuesDone_l();
1049         setPauseStartedTimeRealUs(-1);
1050    }
1051
1052    ALOGV("flushing %s", audio ? "audio" : "video");
1053    if (audio) {
1054        {
1055            Mutex::Autolock autoLock(mLock);
1056            flushQueue(&mAudioQueue);
1057
1058            ++mAudioQueueGeneration;
1059            prepareForMediaRenderingStart();
1060
1061            if (offloadingAudio()) {
1062                setAudioFirstAnchorTime(-1);
1063            }
1064        }
1065
1066        mDrainAudioQueuePending = false;
1067
1068        if (offloadingAudio()) {
1069            mAudioSink->pause();
1070            mAudioSink->flush();
1071            mAudioSink->start();
1072        }
1073    } else {
1074        flushQueue(&mVideoQueue);
1075
1076        mDrainVideoQueuePending = false;
1077        ++mVideoQueueGeneration;
1078
1079        if (mVideoScheduler != NULL) {
1080            mVideoScheduler->restart();
1081        }
1082
1083        prepareForMediaRenderingStart();
1084    }
1085
1086    mVideoSampleReceived = false;
1087    notifyFlushComplete(audio);
1088}
1089
1090void NuPlayer::Renderer::flushQueue(List<QueueEntry> *queue) {
1091    while (!queue->empty()) {
1092        QueueEntry *entry = &*queue->begin();
1093
1094        if (entry->mBuffer != NULL) {
1095            entry->mNotifyConsumed->post();
1096        }
1097
1098        queue->erase(queue->begin());
1099        entry = NULL;
1100    }
1101}
1102
1103void NuPlayer::Renderer::notifyFlushComplete(bool audio) {
1104    sp<AMessage> notify = mNotify->dup();
1105    notify->setInt32("what", kWhatFlushComplete);
1106    notify->setInt32("audio", static_cast<int32_t>(audio));
1107    notify->post();
1108}
1109
1110bool NuPlayer::Renderer::dropBufferWhileFlushing(
1111        bool audio, const sp<AMessage> &msg) {
1112    bool flushing = false;
1113
1114    {
1115        Mutex::Autolock autoLock(mFlushLock);
1116        if (audio) {
1117            flushing = mFlushingAudio;
1118        } else {
1119            flushing = mFlushingVideo;
1120        }
1121    }
1122
1123    if (!flushing) {
1124        return false;
1125    }
1126
1127    sp<AMessage> notifyConsumed;
1128    if (msg->findMessage("notifyConsumed", &notifyConsumed)) {
1129        notifyConsumed->post();
1130    }
1131
1132    return true;
1133}
1134
1135void NuPlayer::Renderer::onAudioSinkChanged() {
1136    if (offloadingAudio()) {
1137        return;
1138    }
1139    CHECK(!mDrainAudioQueuePending);
1140    mNumFramesWritten = 0;
1141    mAnchorNumFramesWritten = -1;
1142    uint32_t written;
1143    if (mAudioSink->getFramesWritten(&written) == OK) {
1144        mNumFramesWritten = written;
1145    }
1146}
1147
1148void NuPlayer::Renderer::onDisableOffloadAudio() {
1149    Mutex::Autolock autoLock(mLock);
1150    mFlags &= ~FLAG_OFFLOAD_AUDIO;
1151    ++mAudioQueueGeneration;
1152}
1153
1154void NuPlayer::Renderer::onPause() {
1155    if (mPaused) {
1156        ALOGW("Renderer::onPause() called while already paused!");
1157        return;
1158    }
1159    {
1160        Mutex::Autolock autoLock(mLock);
1161        ++mAudioQueueGeneration;
1162        ++mVideoQueueGeneration;
1163        prepareForMediaRenderingStart();
1164        mPaused = true;
1165        setPauseStartedTimeRealUs(ALooper::GetNowUs());
1166    }
1167
1168    mDrainAudioQueuePending = false;
1169    mDrainVideoQueuePending = false;
1170
1171    if (mHasAudio) {
1172        mAudioSink->pause();
1173        startAudioOffloadPauseTimeout();
1174    }
1175
1176    ALOGV("now paused audio queue has %d entries, video has %d entries",
1177          mAudioQueue.size(), mVideoQueue.size());
1178}
1179
1180void NuPlayer::Renderer::onResume() {
1181    readProperties();
1182
1183    if (!mPaused) {
1184        return;
1185    }
1186
1187    if (mHasAudio) {
1188        cancelAudioOffloadPauseTimeout();
1189        mAudioSink->start();
1190    }
1191
1192    Mutex::Autolock autoLock(mLock);
1193    mPaused = false;
1194    if (mPauseStartedTimeRealUs != -1) {
1195        int64_t newAnchorRealUs =
1196            mAnchorTimeRealUs + ALooper::GetNowUs() - mPauseStartedTimeRealUs;
1197        setAnchorTime(
1198                mAnchorTimeMediaUs, newAnchorRealUs, mAnchorNumFramesWritten, true /* resume */);
1199    }
1200
1201    if (!mAudioQueue.empty()) {
1202        postDrainAudioQueue_l();
1203    }
1204
1205    if (!mVideoQueue.empty()) {
1206        postDrainVideoQueue();
1207    }
1208}
1209
1210void NuPlayer::Renderer::onSetVideoFrameRate(float fps) {
1211    if (mVideoScheduler == NULL) {
1212        mVideoScheduler = new VideoFrameScheduler();
1213    }
1214    mVideoScheduler->init(fps);
1215}
1216
1217// TODO: Remove unnecessary calls to getPlayedOutAudioDurationUs()
1218// as it acquires locks and may query the audio driver.
1219//
1220// Some calls could conceivably retrieve extrapolated data instead of
1221// accessing getTimestamp() or getPosition() every time a data buffer with
1222// a media time is received.
1223//
1224int64_t NuPlayer::Renderer::getPlayedOutAudioDurationUs(int64_t nowUs) {
1225    uint32_t numFramesPlayed;
1226    int64_t numFramesPlayedAt;
1227    AudioTimestamp ts;
1228    static const int64_t kStaleTimestamp100ms = 100000;
1229
1230    status_t res = mAudioSink->getTimestamp(ts);
1231    if (res == OK) {                 // case 1: mixing audio tracks and offloaded tracks.
1232        numFramesPlayed = ts.mPosition;
1233        numFramesPlayedAt =
1234            ts.mTime.tv_sec * 1000000LL + ts.mTime.tv_nsec / 1000;
1235        const int64_t timestampAge = nowUs - numFramesPlayedAt;
1236        if (timestampAge > kStaleTimestamp100ms) {
1237            // This is an audio FIXME.
1238            // getTimestamp returns a timestamp which may come from audio mixing threads.
1239            // After pausing, the MixerThread may go idle, thus the mTime estimate may
1240            // become stale. Assuming that the MixerThread runs 20ms, with FastMixer at 5ms,
1241            // the max latency should be about 25ms with an average around 12ms (to be verified).
1242            // For safety we use 100ms.
1243            ALOGV("getTimestamp: returned stale timestamp nowUs(%lld) numFramesPlayedAt(%lld)",
1244                    (long long)nowUs, (long long)numFramesPlayedAt);
1245            numFramesPlayedAt = nowUs - kStaleTimestamp100ms;
1246        }
1247        //ALOGD("getTimestamp: OK %d %lld", numFramesPlayed, (long long)numFramesPlayedAt);
1248    } else if (res == WOULD_BLOCK) { // case 2: transitory state on start of a new track
1249        numFramesPlayed = 0;
1250        numFramesPlayedAt = nowUs;
1251        //ALOGD("getTimestamp: WOULD_BLOCK %d %lld",
1252        //        numFramesPlayed, (long long)numFramesPlayedAt);
1253    } else {                         // case 3: transitory at new track or audio fast tracks.
1254        res = mAudioSink->getPosition(&numFramesPlayed);
1255        CHECK_EQ(res, (status_t)OK);
1256        numFramesPlayedAt = nowUs;
1257        numFramesPlayedAt += 1000LL * mAudioSink->latency() / 2; /* XXX */
1258        //ALOGD("getPosition: %d %lld", numFramesPlayed, numFramesPlayedAt);
1259    }
1260
1261    // TODO: remove the (int32_t) casting below as it may overflow at 12.4 hours.
1262    //CHECK_EQ(numFramesPlayed & (1 << 31), 0);  // can't be negative until 12.4 hrs, test
1263    int64_t durationUs = (int64_t)((int32_t)numFramesPlayed * 1000LL * mAudioSink->msecsPerFrame())
1264            + nowUs - numFramesPlayedAt;
1265    if (durationUs < 0) {
1266        // Occurs when numFramesPlayed position is very small and the following:
1267        // (1) In case 1, the time nowUs is computed before getTimestamp() is called and
1268        //     numFramesPlayedAt is greater than nowUs by time more than numFramesPlayed.
1269        // (2) In case 3, using getPosition and adding mAudioSink->latency() to
1270        //     numFramesPlayedAt, by a time amount greater than numFramesPlayed.
1271        //
1272        // Both of these are transitory conditions.
1273        ALOGV("getPlayedOutAudioDurationUs: negative duration %lld set to zero", (long long)durationUs);
1274        durationUs = 0;
1275    }
1276    ALOGV("getPlayedOutAudioDurationUs(%lld) nowUs(%lld) frames(%u) framesAt(%lld)",
1277            (long long)durationUs, (long long)nowUs, numFramesPlayed, (long long)numFramesPlayedAt);
1278    return durationUs;
1279}
1280
1281void NuPlayer::Renderer::onAudioOffloadTearDown(AudioOffloadTearDownReason reason) {
1282    if (mAudioOffloadTornDown) {
1283        return;
1284    }
1285    mAudioOffloadTornDown = true;
1286
1287    int64_t currentPositionUs;
1288    if (getCurrentPosition(&currentPositionUs) != OK) {
1289        currentPositionUs = 0;
1290    }
1291
1292    mAudioSink->stop();
1293    mAudioSink->flush();
1294
1295    sp<AMessage> notify = mNotify->dup();
1296    notify->setInt32("what", kWhatAudioOffloadTearDown);
1297    notify->setInt64("positionUs", currentPositionUs);
1298    notify->setInt32("reason", reason);
1299    notify->post();
1300}
1301
1302void NuPlayer::Renderer::startAudioOffloadPauseTimeout() {
1303    if (offloadingAudio()) {
1304        sp<AMessage> msg = new AMessage(kWhatAudioOffloadPauseTimeout, id());
1305        msg->setInt32("generation", mAudioOffloadPauseTimeoutGeneration);
1306        msg->post(kOffloadPauseMaxUs);
1307    }
1308}
1309
1310void NuPlayer::Renderer::cancelAudioOffloadPauseTimeout() {
1311    if (offloadingAudio()) {
1312        ++mAudioOffloadPauseTimeoutGeneration;
1313    }
1314}
1315
1316bool NuPlayer::Renderer::onOpenAudioSink(
1317        const sp<AMessage> &format,
1318        bool offloadOnly,
1319        bool hasVideo,
1320        uint32_t flags) {
1321    ALOGV("openAudioSink: offloadOnly(%d) offloadingAudio(%d)",
1322            offloadOnly, offloadingAudio());
1323    bool audioSinkChanged = false;
1324
1325    int32_t numChannels;
1326    CHECK(format->findInt32("channel-count", &numChannels));
1327
1328    int32_t channelMask;
1329    if (!format->findInt32("channel-mask", &channelMask)) {
1330        // signal to the AudioSink to derive the mask from count.
1331        channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
1332    }
1333
1334    int32_t sampleRate;
1335    CHECK(format->findInt32("sample-rate", &sampleRate));
1336
1337    if (offloadingAudio()) {
1338        audio_format_t audioFormat = AUDIO_FORMAT_PCM_16_BIT;
1339        AString mime;
1340        CHECK(format->findString("mime", &mime));
1341        status_t err = mapMimeToAudioFormat(audioFormat, mime.c_str());
1342
1343        if (err != OK) {
1344            ALOGE("Couldn't map mime \"%s\" to a valid "
1345                    "audio_format", mime.c_str());
1346            onDisableOffloadAudio();
1347        } else {
1348            ALOGV("Mime \"%s\" mapped to audio_format 0x%x",
1349                    mime.c_str(), audioFormat);
1350
1351            int avgBitRate = -1;
1352            format->findInt32("bit-rate", &avgBitRate);
1353
1354            int32_t aacProfile = -1;
1355            if (audioFormat == AUDIO_FORMAT_AAC
1356                    && format->findInt32("aac-profile", &aacProfile)) {
1357                // Redefine AAC format as per aac profile
1358                mapAACProfileToAudioFormat(
1359                        audioFormat,
1360                        aacProfile);
1361            }
1362
1363            audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1364            offloadInfo.duration_us = -1;
1365            format->findInt64(
1366                    "durationUs", &offloadInfo.duration_us);
1367            offloadInfo.sample_rate = sampleRate;
1368            offloadInfo.channel_mask = channelMask;
1369            offloadInfo.format = audioFormat;
1370            offloadInfo.stream_type = AUDIO_STREAM_MUSIC;
1371            offloadInfo.bit_rate = avgBitRate;
1372            offloadInfo.has_video = hasVideo;
1373            offloadInfo.is_streaming = true;
1374
1375            if (memcmp(&mCurrentOffloadInfo, &offloadInfo, sizeof(offloadInfo)) == 0) {
1376                ALOGV("openAudioSink: no change in offload mode");
1377                // no change from previous configuration, everything ok.
1378                return offloadingAudio();
1379            }
1380            ALOGV("openAudioSink: try to open AudioSink in offload mode");
1381            flags |= AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1382            flags &= ~AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
1383            audioSinkChanged = true;
1384            mAudioSink->close();
1385            err = mAudioSink->open(
1386                    sampleRate,
1387                    numChannels,
1388                    (audio_channel_mask_t)channelMask,
1389                    audioFormat,
1390                    8 /* bufferCount */,
1391                    &NuPlayer::Renderer::AudioSinkCallback,
1392                    this,
1393                    (audio_output_flags_t)flags,
1394                    &offloadInfo);
1395
1396            if (err == OK) {
1397                // If the playback is offloaded to h/w, we pass
1398                // the HAL some metadata information.
1399                // We don't want to do this for PCM because it
1400                // will be going through the AudioFlinger mixer
1401                // before reaching the hardware.
1402                // TODO
1403                mCurrentOffloadInfo = offloadInfo;
1404                err = mAudioSink->start();
1405                ALOGV_IF(err == OK, "openAudioSink: offload succeeded");
1406            }
1407            if (err != OK) {
1408                // Clean up, fall back to non offload mode.
1409                mAudioSink->close();
1410                onDisableOffloadAudio();
1411                mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1412                ALOGV("openAudioSink: offload failed");
1413            }
1414        }
1415    }
1416    if (!offloadOnly && !offloadingAudio()) {
1417        flags &= ~AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD;
1418        ALOGV("openAudioSink: open AudioSink in NON-offload mode");
1419
1420        audioSinkChanged = true;
1421        mAudioSink->close();
1422        mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1423        CHECK_EQ(mAudioSink->open(
1424                    sampleRate,
1425                    numChannels,
1426                    (audio_channel_mask_t)channelMask,
1427                    AUDIO_FORMAT_PCM_16_BIT,
1428                    8 /* bufferCount */,
1429                    NULL,
1430                    NULL,
1431                    (audio_output_flags_t)flags),
1432                 (status_t)OK);
1433        mAudioSink->start();
1434    }
1435    if (audioSinkChanged) {
1436        onAudioSinkChanged();
1437    }
1438
1439    return offloadingAudio();
1440}
1441
1442void NuPlayer::Renderer::onCloseAudioSink() {
1443    mAudioSink->close();
1444    mCurrentOffloadInfo = AUDIO_INFO_INITIALIZER;
1445}
1446
1447}  // namespace android
1448
1449