NuPlayer.cpp revision eac68baf095aeef54865c28b6888924dc6295cbd
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 "NuPlayer"
19#include <utils/Log.h>
20
21#include "NuPlayer.h"
22
23#include "HTTPLiveSource.h"
24#include "NuPlayerDecoder.h"
25#include "NuPlayerDriver.h"
26#include "NuPlayerRenderer.h"
27#include "NuPlayerSource.h"
28#include "StreamingSource.h"
29
30#include "ATSParser.h"
31
32#include <media/stagefright/foundation/hexdump.h>
33#include <media/stagefright/foundation/ABuffer.h>
34#include <media/stagefright/foundation/ADebug.h>
35#include <media/stagefright/foundation/AMessage.h>
36#include <media/stagefright/ACodec.h>
37#include <media/stagefright/MediaDefs.h>
38#include <media/stagefright/MediaErrors.h>
39#include <media/stagefright/MetaData.h>
40#include <surfaceflinger/Surface.h>
41#include <gui/ISurfaceTexture.h>
42
43#include "avc_utils.h"
44
45namespace android {
46
47////////////////////////////////////////////////////////////////////////////////
48
49NuPlayer::NuPlayer()
50    : mUIDValid(false),
51      mVideoIsAVC(false),
52      mAudioEOS(false),
53      mVideoEOS(false),
54      mScanSourcesPending(false),
55      mScanSourcesGeneration(0),
56      mFlushingAudio(NONE),
57      mFlushingVideo(NONE),
58      mResetInProgress(false),
59      mResetPostponed(false),
60      mSkipRenderingAudioUntilMediaTimeUs(-1ll),
61      mSkipRenderingVideoUntilMediaTimeUs(-1ll),
62      mVideoLateByUs(0ll),
63      mNumFramesTotal(0ll),
64      mNumFramesDropped(0ll) {
65}
66
67NuPlayer::~NuPlayer() {
68}
69
70void NuPlayer::setUID(uid_t uid) {
71    mUIDValid = true;
72    mUID = uid;
73}
74
75void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
76    mDriver = driver;
77}
78
79void NuPlayer::setDataSource(const sp<IStreamSource> &source) {
80    sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
81
82    msg->setObject("source", new StreamingSource(source));
83    msg->post();
84}
85
86void NuPlayer::setDataSource(
87        const char *url, const KeyedVector<String8, String8> *headers) {
88    sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
89
90    msg->setObject("source", new HTTPLiveSource(url, headers, mUIDValid, mUID));
91    msg->post();
92}
93
94void NuPlayer::setVideoSurface(const sp<Surface> &surface) {
95    sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
96    msg->setObject("native-window", new NativeWindowWrapper(surface));
97    msg->post();
98}
99
100void NuPlayer::setVideoSurfaceTexture(const sp<ISurfaceTexture> &surfaceTexture) {
101    sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
102    sp<SurfaceTextureClient> surfaceTextureClient(surfaceTexture != NULL ?
103                new SurfaceTextureClient(surfaceTexture) : NULL);
104    msg->setObject("native-window", new NativeWindowWrapper(surfaceTextureClient));
105    msg->post();
106}
107
108void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
109    sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
110    msg->setObject("sink", sink);
111    msg->post();
112}
113
114void NuPlayer::start() {
115    (new AMessage(kWhatStart, id()))->post();
116}
117
118void NuPlayer::pause() {
119    (new AMessage(kWhatPause, id()))->post();
120}
121
122void NuPlayer::resume() {
123    (new AMessage(kWhatResume, id()))->post();
124}
125
126void NuPlayer::resetAsync() {
127    (new AMessage(kWhatReset, id()))->post();
128}
129
130void NuPlayer::seekToAsync(int64_t seekTimeUs) {
131    sp<AMessage> msg = new AMessage(kWhatSeek, id());
132    msg->setInt64("seekTimeUs", seekTimeUs);
133    msg->post();
134}
135
136// static
137bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
138    switch (state) {
139        case FLUSHING_DECODER:
140            if (needShutdown != NULL) {
141                *needShutdown = false;
142            }
143            return true;
144
145        case FLUSHING_DECODER_SHUTDOWN:
146            if (needShutdown != NULL) {
147                *needShutdown = true;
148            }
149            return true;
150
151        default:
152            return false;
153    }
154}
155
156void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
157    switch (msg->what()) {
158        case kWhatSetDataSource:
159        {
160            LOGV("kWhatSetDataSource");
161
162            CHECK(mSource == NULL);
163
164            sp<RefBase> obj;
165            CHECK(msg->findObject("source", &obj));
166
167            mSource = static_cast<Source *>(obj.get());
168            break;
169        }
170
171        case kWhatSetVideoNativeWindow:
172        {
173            LOGV("kWhatSetVideoNativeWindow");
174
175            sp<RefBase> obj;
176            CHECK(msg->findObject("native-window", &obj));
177
178            mNativeWindow = static_cast<NativeWindowWrapper *>(obj.get());
179            break;
180        }
181
182        case kWhatSetAudioSink:
183        {
184            LOGV("kWhatSetAudioSink");
185
186            sp<RefBase> obj;
187            CHECK(msg->findObject("sink", &obj));
188
189            mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
190            break;
191        }
192
193        case kWhatStart:
194        {
195            LOGV("kWhatStart");
196
197            mVideoIsAVC = false;
198            mAudioEOS = false;
199            mVideoEOS = false;
200            mSkipRenderingAudioUntilMediaTimeUs = -1;
201            mSkipRenderingVideoUntilMediaTimeUs = -1;
202            mVideoLateByUs = 0;
203            mNumFramesTotal = 0;
204            mNumFramesDropped = 0;
205
206            mSource->start();
207
208            mRenderer = new Renderer(
209                    mAudioSink,
210                    new AMessage(kWhatRendererNotify, id()));
211
212            looper()->registerHandler(mRenderer);
213
214            postScanSources();
215            break;
216        }
217
218        case kWhatScanSources:
219        {
220            int32_t generation;
221            CHECK(msg->findInt32("generation", &generation));
222            if (generation != mScanSourcesGeneration) {
223                // Drop obsolete msg.
224                break;
225            }
226
227            mScanSourcesPending = false;
228
229            LOGV("scanning sources haveAudio=%d, haveVideo=%d",
230                 mAudioDecoder != NULL, mVideoDecoder != NULL);
231
232            instantiateDecoder(false, &mVideoDecoder);
233
234            if (mAudioSink != NULL) {
235                instantiateDecoder(true, &mAudioDecoder);
236            }
237
238            status_t err;
239            if ((err = mSource->feedMoreTSData()) != OK) {
240                if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
241                    // We're not currently decoding anything (no audio or
242                    // video tracks found) and we just ran out of input data.
243
244                    if (err == ERROR_END_OF_STREAM) {
245                        notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
246                    } else {
247                        notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
248                    }
249                }
250                break;
251            }
252
253            if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
254                msg->post(100000ll);
255                mScanSourcesPending = true;
256            }
257            break;
258        }
259
260        case kWhatVideoNotify:
261        case kWhatAudioNotify:
262        {
263            bool audio = msg->what() == kWhatAudioNotify;
264
265            sp<AMessage> codecRequest;
266            CHECK(msg->findMessage("codec-request", &codecRequest));
267
268            int32_t what;
269            CHECK(codecRequest->findInt32("what", &what));
270
271            if (what == ACodec::kWhatFillThisBuffer) {
272                status_t err = feedDecoderInputData(
273                        audio, codecRequest);
274
275                if (err == -EWOULDBLOCK) {
276                    if (mSource->feedMoreTSData() == OK) {
277                        msg->post();
278                    }
279                }
280            } else if (what == ACodec::kWhatEOS) {
281                int32_t err;
282                CHECK(codecRequest->findInt32("err", &err));
283
284                if (err == ERROR_END_OF_STREAM) {
285                    LOGV("got %s decoder EOS", audio ? "audio" : "video");
286                } else {
287                    LOGV("got %s decoder EOS w/ error %d",
288                         audio ? "audio" : "video",
289                         err);
290                }
291
292                mRenderer->queueEOS(audio, err);
293            } else if (what == ACodec::kWhatFlushCompleted) {
294                bool needShutdown;
295
296                if (audio) {
297                    CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
298                    mFlushingAudio = FLUSHED;
299                } else {
300                    CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
301                    mFlushingVideo = FLUSHED;
302
303                    mVideoLateByUs = 0;
304                }
305
306                LOGV("decoder %s flush completed", audio ? "audio" : "video");
307
308                if (needShutdown) {
309                    LOGV("initiating %s decoder shutdown",
310                         audio ? "audio" : "video");
311
312                    (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
313
314                    if (audio) {
315                        mFlushingAudio = SHUTTING_DOWN_DECODER;
316                    } else {
317                        mFlushingVideo = SHUTTING_DOWN_DECODER;
318                    }
319                }
320
321                finishFlushIfPossible();
322            } else if (what == ACodec::kWhatOutputFormatChanged) {
323                if (audio) {
324                    int32_t numChannels;
325                    CHECK(codecRequest->findInt32("channel-count", &numChannels));
326
327                    int32_t sampleRate;
328                    CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
329
330                    LOGV("Audio output format changed to %d Hz, %d channels",
331                         sampleRate, numChannels);
332
333                    mAudioSink->close();
334                    CHECK_EQ(mAudioSink->open(
335                                sampleRate,
336                                numChannels,
337                                AUDIO_FORMAT_PCM_16_BIT,
338                                8 /* bufferCount */),
339                             (status_t)OK);
340                    mAudioSink->start();
341
342                    mRenderer->signalAudioSinkChanged();
343                } else {
344                    // video
345
346                    int32_t width, height;
347                    CHECK(codecRequest->findInt32("width", &width));
348                    CHECK(codecRequest->findInt32("height", &height));
349
350                    int32_t cropLeft, cropTop, cropRight, cropBottom;
351                    CHECK(codecRequest->findRect(
352                                "crop",
353                                &cropLeft, &cropTop, &cropRight, &cropBottom));
354
355                    LOGV("Video output format changed to %d x %d "
356                         "(crop: %d x %d @ (%d, %d))",
357                         width, height,
358                         (cropRight - cropLeft + 1),
359                         (cropBottom - cropTop + 1),
360                         cropLeft, cropTop);
361
362                    notifyListener(
363                            MEDIA_SET_VIDEO_SIZE,
364                            cropRight - cropLeft + 1,
365                            cropBottom - cropTop + 1);
366                }
367            } else if (what == ACodec::kWhatShutdownCompleted) {
368                LOGV("%s shutdown completed", audio ? "audio" : "video");
369                if (audio) {
370                    mAudioDecoder.clear();
371
372                    CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
373                    mFlushingAudio = SHUT_DOWN;
374                } else {
375                    mVideoDecoder.clear();
376
377                    CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
378                    mFlushingVideo = SHUT_DOWN;
379                }
380
381                finishFlushIfPossible();
382            } else if (what == ACodec::kWhatError) {
383                LOGE("Received error from %s decoder, aborting playback.",
384                     audio ? "audio" : "video");
385
386                mRenderer->queueEOS(audio, UNKNOWN_ERROR);
387            } else {
388                CHECK_EQ((int)what, (int)ACodec::kWhatDrainThisBuffer);
389
390                renderBuffer(audio, codecRequest);
391            }
392
393            break;
394        }
395
396        case kWhatRendererNotify:
397        {
398            int32_t what;
399            CHECK(msg->findInt32("what", &what));
400
401            if (what == Renderer::kWhatEOS) {
402                int32_t audio;
403                CHECK(msg->findInt32("audio", &audio));
404
405                int32_t finalResult;
406                CHECK(msg->findInt32("finalResult", &finalResult));
407
408                if (audio) {
409                    mAudioEOS = true;
410                } else {
411                    mVideoEOS = true;
412                }
413
414                if (finalResult == ERROR_END_OF_STREAM) {
415                    LOGV("reached %s EOS", audio ? "audio" : "video");
416                } else {
417                    LOGE("%s track encountered an error (%d)",
418                         audio ? "audio" : "video", finalResult);
419
420                    notifyListener(
421                            MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
422                }
423
424                if ((mAudioEOS || mAudioDecoder == NULL)
425                        && (mVideoEOS || mVideoDecoder == NULL)) {
426                    notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
427                }
428            } else if (what == Renderer::kWhatPosition) {
429                int64_t positionUs;
430                CHECK(msg->findInt64("positionUs", &positionUs));
431
432                CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
433
434                if (mDriver != NULL) {
435                    sp<NuPlayerDriver> driver = mDriver.promote();
436                    if (driver != NULL) {
437                        driver->notifyPosition(positionUs);
438
439                        driver->notifyFrameStats(
440                                mNumFramesTotal, mNumFramesDropped);
441                    }
442                }
443            } else if (what == Renderer::kWhatFlushComplete) {
444                CHECK_EQ(what, (int32_t)Renderer::kWhatFlushComplete);
445
446                int32_t audio;
447                CHECK(msg->findInt32("audio", &audio));
448
449                LOGV("renderer %s flush completed.", audio ? "audio" : "video");
450            }
451            break;
452        }
453
454        case kWhatMoreDataQueued:
455        {
456            break;
457        }
458
459        case kWhatReset:
460        {
461            LOGV("kWhatReset");
462
463            if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
464                // We're currently flushing, postpone the reset until that's
465                // completed.
466
467                LOGV("postponing reset");
468
469                mResetPostponed = true;
470                break;
471            }
472
473            if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
474                finishReset();
475                break;
476            }
477
478            if (mAudioDecoder != NULL) {
479                flushDecoder(true /* audio */, true /* needShutdown */);
480            }
481
482            if (mVideoDecoder != NULL) {
483                flushDecoder(false /* audio */, true /* needShutdown */);
484            }
485
486            mResetInProgress = true;
487            break;
488        }
489
490        case kWhatSeek:
491        {
492            int64_t seekTimeUs;
493            CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
494
495            LOGV("kWhatSeek seekTimeUs=%lld us (%.2f secs)",
496                 seekTimeUs, seekTimeUs / 1E6);
497
498            mSource->seekTo(seekTimeUs);
499
500            if (mDriver != NULL) {
501                sp<NuPlayerDriver> driver = mDriver.promote();
502                if (driver != NULL) {
503                    driver->notifySeekComplete();
504                }
505            }
506
507            break;
508        }
509
510        case kWhatPause:
511        {
512            CHECK(mRenderer != NULL);
513            mRenderer->pause();
514            break;
515        }
516
517        case kWhatResume:
518        {
519            CHECK(mRenderer != NULL);
520            mRenderer->resume();
521            break;
522        }
523
524        default:
525            TRESPASS();
526            break;
527    }
528}
529
530void NuPlayer::finishFlushIfPossible() {
531    if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
532        return;
533    }
534
535    if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
536        return;
537    }
538
539    LOGV("both audio and video are flushed now.");
540
541    mRenderer->signalTimeDiscontinuity();
542
543    if (mAudioDecoder != NULL) {
544        mAudioDecoder->signalResume();
545    }
546
547    if (mVideoDecoder != NULL) {
548        mVideoDecoder->signalResume();
549    }
550
551    mFlushingAudio = NONE;
552    mFlushingVideo = NONE;
553
554    if (mResetInProgress) {
555        LOGV("reset completed");
556
557        mResetInProgress = false;
558        finishReset();
559    } else if (mResetPostponed) {
560        (new AMessage(kWhatReset, id()))->post();
561        mResetPostponed = false;
562    } else if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
563        postScanSources();
564    }
565}
566
567void NuPlayer::finishReset() {
568    CHECK(mAudioDecoder == NULL);
569    CHECK(mVideoDecoder == NULL);
570
571    mRenderer.clear();
572    mSource.clear();
573
574    if (mDriver != NULL) {
575        sp<NuPlayerDriver> driver = mDriver.promote();
576        if (driver != NULL) {
577            driver->notifyResetComplete();
578        }
579    }
580}
581
582void NuPlayer::postScanSources() {
583    if (mScanSourcesPending) {
584        return;
585    }
586
587    sp<AMessage> msg = new AMessage(kWhatScanSources, id());
588    msg->setInt32("generation", mScanSourcesGeneration);
589    msg->post();
590
591    mScanSourcesPending = true;
592}
593
594status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
595    if (*decoder != NULL) {
596        return OK;
597    }
598
599    sp<MetaData> meta = mSource->getFormat(audio);
600
601    if (meta == NULL) {
602        return -EWOULDBLOCK;
603    }
604
605    if (!audio) {
606        const char *mime;
607        CHECK(meta->findCString(kKeyMIMEType, &mime));
608        mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime);
609    }
610
611    sp<AMessage> notify =
612        new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
613                     id());
614
615    *decoder = audio ? new Decoder(notify) :
616                       new Decoder(notify, mNativeWindow);
617    looper()->registerHandler(*decoder);
618
619    (*decoder)->configure(meta);
620
621    int64_t durationUs;
622    if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
623        sp<NuPlayerDriver> driver = mDriver.promote();
624        if (driver != NULL) {
625            driver->notifyDuration(durationUs);
626        }
627    }
628
629    return OK;
630}
631
632status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
633    sp<AMessage> reply;
634    CHECK(msg->findMessage("reply", &reply));
635
636    if ((audio && IsFlushingState(mFlushingAudio))
637            || (!audio && IsFlushingState(mFlushingVideo))) {
638        reply->setInt32("err", INFO_DISCONTINUITY);
639        reply->post();
640        return OK;
641    }
642
643    sp<ABuffer> accessUnit;
644
645    bool dropAccessUnit;
646    do {
647        status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
648
649        if (err == -EWOULDBLOCK) {
650            return err;
651        } else if (err != OK) {
652            if (err == INFO_DISCONTINUITY) {
653                int32_t type;
654                CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
655
656                bool formatChange =
657                    type == ATSParser::DISCONTINUITY_FORMATCHANGE;
658
659                LOGV("%s discontinuity (formatChange=%d)",
660                     audio ? "audio" : "video", formatChange);
661
662                if (audio) {
663                    mSkipRenderingAudioUntilMediaTimeUs = -1;
664                } else {
665                    mSkipRenderingVideoUntilMediaTimeUs = -1;
666                }
667
668                sp<AMessage> extra;
669                if (accessUnit->meta()->findMessage("extra", &extra)
670                        && extra != NULL) {
671                    int64_t resumeAtMediaTimeUs;
672                    if (extra->findInt64(
673                                "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
674                        LOGI("suppressing rendering of %s until %lld us",
675                                audio ? "audio" : "video", resumeAtMediaTimeUs);
676
677                        if (audio) {
678                            mSkipRenderingAudioUntilMediaTimeUs =
679                                resumeAtMediaTimeUs;
680                        } else {
681                            mSkipRenderingVideoUntilMediaTimeUs =
682                                resumeAtMediaTimeUs;
683                        }
684                    }
685                }
686
687                flushDecoder(audio, formatChange);
688            }
689
690            reply->setInt32("err", err);
691            reply->post();
692            return OK;
693        }
694
695        if (!audio) {
696            ++mNumFramesTotal;
697        }
698
699        dropAccessUnit = false;
700        if (!audio
701                && mVideoLateByUs > 100000ll
702                && mVideoIsAVC
703                && !IsAVCReferenceFrame(accessUnit)) {
704            dropAccessUnit = true;
705            ++mNumFramesDropped;
706        }
707    } while (dropAccessUnit);
708
709    // LOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
710
711#if 0
712    int64_t mediaTimeUs;
713    CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
714    LOGV("feeding %s input buffer at media time %.2f secs",
715         audio ? "audio" : "video",
716         mediaTimeUs / 1E6);
717#endif
718
719    reply->setObject("buffer", accessUnit);
720    reply->post();
721
722    return OK;
723}
724
725void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
726    // LOGV("renderBuffer %s", audio ? "audio" : "video");
727
728    sp<AMessage> reply;
729    CHECK(msg->findMessage("reply", &reply));
730
731    if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
732        // We're currently attempting to flush the decoder, in order
733        // to complete this, the decoder wants all its buffers back,
734        // so we don't want any output buffers it sent us (from before
735        // we initiated the flush) to be stuck in the renderer's queue.
736
737        LOGV("we're still flushing the %s decoder, sending its output buffer"
738             " right back.", audio ? "audio" : "video");
739
740        reply->post();
741        return;
742    }
743
744    sp<RefBase> obj;
745    CHECK(msg->findObject("buffer", &obj));
746
747    sp<ABuffer> buffer = static_cast<ABuffer *>(obj.get());
748
749    int64_t &skipUntilMediaTimeUs =
750        audio
751            ? mSkipRenderingAudioUntilMediaTimeUs
752            : mSkipRenderingVideoUntilMediaTimeUs;
753
754    if (skipUntilMediaTimeUs >= 0) {
755        int64_t mediaTimeUs;
756        CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
757
758        if (mediaTimeUs < skipUntilMediaTimeUs) {
759            LOGV("dropping %s buffer at time %lld as requested.",
760                 audio ? "audio" : "video",
761                 mediaTimeUs);
762
763            reply->post();
764            return;
765        }
766
767        skipUntilMediaTimeUs = -1;
768    }
769
770    mRenderer->queueBuffer(audio, buffer, reply);
771}
772
773void NuPlayer::notifyListener(int msg, int ext1, int ext2) {
774    if (mDriver == NULL) {
775        return;
776    }
777
778    sp<NuPlayerDriver> driver = mDriver.promote();
779
780    if (driver == NULL) {
781        return;
782    }
783
784    driver->sendEvent(msg, ext1, ext2);
785}
786
787void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
788    // Make sure we don't continue to scan sources until we finish flushing.
789    ++mScanSourcesGeneration;
790    mScanSourcesPending = false;
791
792    (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
793    mRenderer->flush(audio);
794
795    FlushStatus newStatus =
796        needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
797
798    if (audio) {
799        CHECK(mFlushingAudio == NONE
800                || mFlushingAudio == AWAITING_DISCONTINUITY);
801
802        mFlushingAudio = newStatus;
803
804        if (mFlushingVideo == NONE) {
805            mFlushingVideo = (mVideoDecoder != NULL)
806                ? AWAITING_DISCONTINUITY
807                : FLUSHED;
808        }
809    } else {
810        CHECK(mFlushingVideo == NONE
811                || mFlushingVideo == AWAITING_DISCONTINUITY);
812
813        mFlushingVideo = newStatus;
814
815        if (mFlushingAudio == NONE) {
816            mFlushingAudio = (mAudioDecoder != NULL)
817                ? AWAITING_DISCONTINUITY
818                : FLUSHED;
819        }
820    }
821}
822
823}  // namespace android
824