NuPlayer.cpp revision 240abcc4bf661a10ffca24859945796acc76ac6e
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 "RTSPSource.h"
29#include "StreamingSource.h"
30#include "GenericSource.h"
31
32#include "ATSParser.h"
33
34#include "SoftwareRenderer.h"
35
36#include <media/stagefright/foundation/hexdump.h>
37#include <media/stagefright/foundation/ABuffer.h>
38#include <media/stagefright/foundation/ADebug.h>
39#include <media/stagefright/foundation/AMessage.h>
40#include <media/stagefright/ACodec.h>
41#include <media/stagefright/MediaDefs.h>
42#include <media/stagefright/MediaErrors.h>
43#include <media/stagefright/MetaData.h>
44#include <gui/IGraphicBufferProducer.h>
45
46#include "avc_utils.h"
47
48#include "ESDS.h"
49#include <media/stagefright/Utils.h>
50
51namespace android {
52
53struct NuPlayer::Action : public RefBase {
54    Action() {}
55
56    virtual void execute(NuPlayer *player) = 0;
57
58private:
59    DISALLOW_EVIL_CONSTRUCTORS(Action);
60};
61
62struct NuPlayer::SeekAction : public Action {
63    SeekAction(int64_t seekTimeUs)
64        : mSeekTimeUs(seekTimeUs) {
65    }
66
67    virtual void execute(NuPlayer *player) {
68        player->performSeek(mSeekTimeUs);
69    }
70
71private:
72    int64_t mSeekTimeUs;
73
74    DISALLOW_EVIL_CONSTRUCTORS(SeekAction);
75};
76
77struct NuPlayer::SetSurfaceAction : public Action {
78    SetSurfaceAction(const sp<NativeWindowWrapper> &wrapper)
79        : mWrapper(wrapper) {
80    }
81
82    virtual void execute(NuPlayer *player) {
83        player->performSetSurface(mWrapper);
84    }
85
86private:
87    sp<NativeWindowWrapper> mWrapper;
88
89    DISALLOW_EVIL_CONSTRUCTORS(SetSurfaceAction);
90};
91
92struct NuPlayer::ShutdownDecoderAction : public Action {
93    ShutdownDecoderAction(bool audio, bool video)
94        : mAudio(audio),
95          mVideo(video) {
96    }
97
98    virtual void execute(NuPlayer *player) {
99        player->performDecoderShutdown(mAudio, mVideo);
100    }
101
102private:
103    bool mAudio;
104    bool mVideo;
105
106    DISALLOW_EVIL_CONSTRUCTORS(ShutdownDecoderAction);
107};
108
109struct NuPlayer::PostMessageAction : public Action {
110    PostMessageAction(const sp<AMessage> &msg)
111        : mMessage(msg) {
112    }
113
114    virtual void execute(NuPlayer *) {
115        mMessage->post();
116    }
117
118private:
119    sp<AMessage> mMessage;
120
121    DISALLOW_EVIL_CONSTRUCTORS(PostMessageAction);
122};
123
124// Use this if there's no state necessary to save in order to execute
125// the action.
126struct NuPlayer::SimpleAction : public Action {
127    typedef void (NuPlayer::*ActionFunc)();
128
129    SimpleAction(ActionFunc func)
130        : mFunc(func) {
131    }
132
133    virtual void execute(NuPlayer *player) {
134        (player->*mFunc)();
135    }
136
137private:
138    ActionFunc mFunc;
139
140    DISALLOW_EVIL_CONSTRUCTORS(SimpleAction);
141};
142
143////////////////////////////////////////////////////////////////////////////////
144
145NuPlayer::NuPlayer()
146    : mUIDValid(false),
147      mSourceFlags(0),
148      mVideoIsAVC(false),
149      mNeedsSwRenderer(false),
150      mAudioEOS(false),
151      mVideoEOS(false),
152      mScanSourcesPending(false),
153      mScanSourcesGeneration(0),
154      mPollDurationGeneration(0),
155      mTimeDiscontinuityPending(false),
156      mFlushingAudio(NONE),
157      mFlushingVideo(NONE),
158      mSkipRenderingAudioUntilMediaTimeUs(-1ll),
159      mSkipRenderingVideoUntilMediaTimeUs(-1ll),
160      mVideoLateByUs(0ll),
161      mNumFramesTotal(0ll),
162      mNumFramesDropped(0ll),
163      mVideoScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW),
164      mStarted(false) {
165}
166
167NuPlayer::~NuPlayer() {
168}
169
170void NuPlayer::setUID(uid_t uid) {
171    mUIDValid = true;
172    mUID = uid;
173}
174
175void NuPlayer::setDriver(const wp<NuPlayerDriver> &driver) {
176    mDriver = driver;
177}
178
179void NuPlayer::setDataSourceAsync(const sp<IStreamSource> &source) {
180    sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
181
182    sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
183
184    msg->setObject("source", new StreamingSource(notify, source));
185    msg->post();
186}
187
188static bool IsHTTPLiveURL(const char *url) {
189    if (!strncasecmp("http://", url, 7)
190            || !strncasecmp("https://", url, 8)
191            || !strncasecmp("file://", url, 7)) {
192        size_t len = strlen(url);
193        if (len >= 5 && !strcasecmp(".m3u8", &url[len - 5])) {
194            return true;
195        }
196
197        if (strstr(url,"m3u8")) {
198            return true;
199        }
200    }
201
202    return false;
203}
204
205void NuPlayer::setDataSourceAsync(
206        const sp<IMediaHTTPService> &httpService,
207        const char *url,
208        const KeyedVector<String8, String8> *headers) {
209    sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
210    size_t len = strlen(url);
211
212    sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
213
214    sp<Source> source;
215    if (IsHTTPLiveURL(url)) {
216        source = new HTTPLiveSource(notify, httpService, url, headers);
217    } else if (!strncasecmp(url, "rtsp://", 7)) {
218        source = new RTSPSource(
219                notify, httpService, url, headers, mUIDValid, mUID);
220    } else if ((!strncasecmp(url, "http://", 7)
221                || !strncasecmp(url, "https://", 8))
222                    && ((len >= 4 && !strcasecmp(".sdp", &url[len - 4]))
223                    || strstr(url, ".sdp?"))) {
224        source = new RTSPSource(
225                notify, httpService, url, headers, mUIDValid, mUID, true);
226    } else {
227        source = new GenericSource(notify, httpService, url, headers);
228    }
229
230    msg->setObject("source", source);
231    msg->post();
232}
233
234void NuPlayer::setDataSourceAsync(int fd, int64_t offset, int64_t length) {
235    sp<AMessage> msg = new AMessage(kWhatSetDataSource, id());
236
237    sp<AMessage> notify = new AMessage(kWhatSourceNotify, id());
238
239    sp<Source> source = new GenericSource(notify, fd, offset, length);
240    msg->setObject("source", source);
241    msg->post();
242}
243
244void NuPlayer::prepareAsync() {
245    (new AMessage(kWhatPrepare, id()))->post();
246}
247
248void NuPlayer::setVideoSurfaceTextureAsync(
249        const sp<IGraphicBufferProducer> &bufferProducer) {
250    sp<AMessage> msg = new AMessage(kWhatSetVideoNativeWindow, id());
251
252    if (bufferProducer == NULL) {
253        msg->setObject("native-window", NULL);
254    } else {
255        msg->setObject(
256                "native-window",
257                new NativeWindowWrapper(
258                    new Surface(bufferProducer)));
259    }
260
261    msg->post();
262}
263
264void NuPlayer::setAudioSink(const sp<MediaPlayerBase::AudioSink> &sink) {
265    sp<AMessage> msg = new AMessage(kWhatSetAudioSink, id());
266    msg->setObject("sink", sink);
267    msg->post();
268}
269
270void NuPlayer::start() {
271    (new AMessage(kWhatStart, id()))->post();
272}
273
274void NuPlayer::pause() {
275    (new AMessage(kWhatPause, id()))->post();
276}
277
278void NuPlayer::resume() {
279    (new AMessage(kWhatResume, id()))->post();
280}
281
282void NuPlayer::resetAsync() {
283    (new AMessage(kWhatReset, id()))->post();
284}
285
286void NuPlayer::seekToAsync(int64_t seekTimeUs) {
287    sp<AMessage> msg = new AMessage(kWhatSeek, id());
288    msg->setInt64("seekTimeUs", seekTimeUs);
289    msg->post();
290}
291
292// static
293bool NuPlayer::IsFlushingState(FlushStatus state, bool *needShutdown) {
294    switch (state) {
295        case FLUSHING_DECODER:
296            if (needShutdown != NULL) {
297                *needShutdown = false;
298            }
299            return true;
300
301        case FLUSHING_DECODER_SHUTDOWN:
302            if (needShutdown != NULL) {
303                *needShutdown = true;
304            }
305            return true;
306
307        default:
308            return false;
309    }
310}
311
312void NuPlayer::onMessageReceived(const sp<AMessage> &msg) {
313    switch (msg->what()) {
314        case kWhatSetDataSource:
315        {
316            ALOGV("kWhatSetDataSource");
317
318            CHECK(mSource == NULL);
319
320            sp<RefBase> obj;
321            CHECK(msg->findObject("source", &obj));
322
323            mSource = static_cast<Source *>(obj.get());
324
325            looper()->registerHandler(mSource);
326
327            CHECK(mDriver != NULL);
328            sp<NuPlayerDriver> driver = mDriver.promote();
329            if (driver != NULL) {
330                driver->notifySetDataSourceCompleted(OK);
331            }
332            break;
333        }
334
335        case kWhatPrepare:
336        {
337            mSource->prepareAsync();
338            break;
339        }
340
341        case kWhatGetTrackInfo:
342        {
343            uint32_t replyID;
344            CHECK(msg->senderAwaitsResponse(&replyID));
345
346            status_t err = INVALID_OPERATION;
347            if (mSource != NULL) {
348                Parcel* reply;
349                CHECK(msg->findPointer("reply", (void**)&reply));
350                err = mSource->getTrackInfo(reply);
351            }
352
353            sp<AMessage> response = new AMessage;
354            response->setInt32("err", err);
355
356            response->postReply(replyID);
357            break;
358        }
359
360        case kWhatSelectTrack:
361        {
362            uint32_t replyID;
363            CHECK(msg->senderAwaitsResponse(&replyID));
364
365            status_t err = INVALID_OPERATION;
366            if (mSource != NULL) {
367                size_t trackIndex;
368                int32_t select;
369                CHECK(msg->findSize("trackIndex", &trackIndex));
370                CHECK(msg->findInt32("select", &select));
371                err = mSource->selectTrack(trackIndex, select);
372            }
373
374            sp<AMessage> response = new AMessage;
375            response->setInt32("err", err);
376
377            response->postReply(replyID);
378            break;
379        }
380
381        case kWhatPollDuration:
382        {
383            int32_t generation;
384            CHECK(msg->findInt32("generation", &generation));
385
386            if (generation != mPollDurationGeneration) {
387                // stale
388                break;
389            }
390
391            int64_t durationUs;
392            if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
393                sp<NuPlayerDriver> driver = mDriver.promote();
394                if (driver != NULL) {
395                    driver->notifyDuration(durationUs);
396                }
397            }
398
399            msg->post(1000000ll);  // poll again in a second.
400            break;
401        }
402
403        case kWhatSetVideoNativeWindow:
404        {
405            ALOGV("kWhatSetVideoNativeWindow");
406
407            mDeferredActions.push_back(
408                    new ShutdownDecoderAction(
409                        false /* audio */, true /* video */));
410
411            sp<RefBase> obj;
412            CHECK(msg->findObject("native-window", &obj));
413
414            mDeferredActions.push_back(
415                    new SetSurfaceAction(
416                        static_cast<NativeWindowWrapper *>(obj.get())));
417
418            if (obj != NULL) {
419                // If there is a new surface texture, instantiate decoders
420                // again if possible.
421                mDeferredActions.push_back(
422                        new SimpleAction(&NuPlayer::performScanSources));
423            }
424
425            processDeferredActions();
426            break;
427        }
428
429        case kWhatSetAudioSink:
430        {
431            ALOGV("kWhatSetAudioSink");
432
433            sp<RefBase> obj;
434            CHECK(msg->findObject("sink", &obj));
435
436            mAudioSink = static_cast<MediaPlayerBase::AudioSink *>(obj.get());
437            break;
438        }
439
440        case kWhatStart:
441        {
442            ALOGV("kWhatStart");
443
444            mVideoIsAVC = false;
445            mNeedsSwRenderer = false;
446            mAudioEOS = false;
447            mVideoEOS = false;
448            mSkipRenderingAudioUntilMediaTimeUs = -1;
449            mSkipRenderingVideoUntilMediaTimeUs = -1;
450            mVideoLateByUs = 0;
451            mNumFramesTotal = 0;
452            mNumFramesDropped = 0;
453            mStarted = true;
454
455            mSource->start();
456
457            uint32_t flags = 0;
458
459            if (mSource->isRealTime()) {
460                flags |= Renderer::FLAG_REAL_TIME;
461            }
462
463            mRenderer = new Renderer(
464                    mAudioSink,
465                    new AMessage(kWhatRendererNotify, id()),
466                    flags);
467
468            looper()->registerHandler(mRenderer);
469
470            postScanSources();
471            break;
472        }
473
474        case kWhatScanSources:
475        {
476            int32_t generation;
477            CHECK(msg->findInt32("generation", &generation));
478            if (generation != mScanSourcesGeneration) {
479                // Drop obsolete msg.
480                break;
481            }
482
483            mScanSourcesPending = false;
484
485            ALOGV("scanning sources haveAudio=%d, haveVideo=%d",
486                 mAudioDecoder != NULL, mVideoDecoder != NULL);
487
488            bool mHadAnySourcesBefore =
489                (mAudioDecoder != NULL) || (mVideoDecoder != NULL);
490
491            if (mNativeWindow != NULL) {
492                instantiateDecoder(false, &mVideoDecoder);
493            }
494
495            if (mAudioSink != NULL) {
496                instantiateDecoder(true, &mAudioDecoder);
497            }
498
499            if (!mHadAnySourcesBefore
500                    && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
501                // This is the first time we've found anything playable.
502
503                if (mSourceFlags & Source::FLAG_DYNAMIC_DURATION) {
504                    schedulePollDuration();
505                }
506            }
507
508            status_t err;
509            if ((err = mSource->feedMoreTSData()) != OK) {
510                if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
511                    // We're not currently decoding anything (no audio or
512                    // video tracks found) and we just ran out of input data.
513
514                    if (err == ERROR_END_OF_STREAM) {
515                        notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
516                    } else {
517                        notifyListener(MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, err);
518                    }
519                }
520                break;
521            }
522
523            if ((mAudioDecoder == NULL && mAudioSink != NULL)
524                    || (mVideoDecoder == NULL && mNativeWindow != NULL)) {
525                msg->post(100000ll);
526                mScanSourcesPending = true;
527            }
528            break;
529        }
530
531        case kWhatVideoNotify:
532        case kWhatAudioNotify:
533        {
534            bool audio = msg->what() == kWhatAudioNotify;
535
536            sp<AMessage> codecRequest;
537            CHECK(msg->findMessage("codec-request", &codecRequest));
538
539            int32_t what;
540            CHECK(codecRequest->findInt32("what", &what));
541
542            if (what == ACodec::kWhatFillThisBuffer) {
543                status_t err = feedDecoderInputData(
544                        audio, codecRequest);
545
546                if (err == -EWOULDBLOCK) {
547                    if (mSource->feedMoreTSData() == OK) {
548                        msg->post(10000ll);
549                    }
550                }
551            } else if (what == ACodec::kWhatEOS) {
552                int32_t err;
553                CHECK(codecRequest->findInt32("err", &err));
554
555                if (err == ERROR_END_OF_STREAM) {
556                    ALOGV("got %s decoder EOS", audio ? "audio" : "video");
557                } else {
558                    ALOGV("got %s decoder EOS w/ error %d",
559                         audio ? "audio" : "video",
560                         err);
561                }
562
563                mRenderer->queueEOS(audio, err);
564            } else if (what == ACodec::kWhatFlushCompleted) {
565                bool needShutdown;
566
567                if (audio) {
568                    CHECK(IsFlushingState(mFlushingAudio, &needShutdown));
569                    mFlushingAudio = FLUSHED;
570                } else {
571                    CHECK(IsFlushingState(mFlushingVideo, &needShutdown));
572                    mFlushingVideo = FLUSHED;
573
574                    mVideoLateByUs = 0;
575                }
576
577                ALOGV("decoder %s flush completed", audio ? "audio" : "video");
578
579                if (needShutdown) {
580                    ALOGV("initiating %s decoder shutdown",
581                         audio ? "audio" : "video");
582
583                    (audio ? mAudioDecoder : mVideoDecoder)->initiateShutdown();
584
585                    if (audio) {
586                        mFlushingAudio = SHUTTING_DOWN_DECODER;
587                    } else {
588                        mFlushingVideo = SHUTTING_DOWN_DECODER;
589                    }
590                }
591
592                finishFlushIfPossible();
593            } else if (what == ACodec::kWhatOutputFormatChanged) {
594                if (audio) {
595                    int32_t numChannels;
596                    CHECK(codecRequest->findInt32(
597                                "channel-count", &numChannels));
598
599                    int32_t sampleRate;
600                    CHECK(codecRequest->findInt32("sample-rate", &sampleRate));
601
602                    ALOGV("Audio output format changed to %d Hz, %d channels",
603                         sampleRate, numChannels);
604
605                    mAudioSink->close();
606
607                    audio_output_flags_t flags;
608                    int64_t durationUs;
609                    // FIXME: we should handle the case where the video decoder
610                    // is created after we receive the format change indication.
611                    // Current code will just make that we select deep buffer
612                    // with video which should not be a problem as it should
613                    // not prevent from keeping A/V sync.
614                    if (mVideoDecoder == NULL &&
615                            mSource->getDuration(&durationUs) == OK &&
616                            durationUs
617                                > AUDIO_SINK_MIN_DEEP_BUFFER_DURATION_US) {
618                        flags = AUDIO_OUTPUT_FLAG_DEEP_BUFFER;
619                    } else {
620                        flags = AUDIO_OUTPUT_FLAG_NONE;
621                    }
622
623                    int32_t channelMask;
624                    if (!codecRequest->findInt32("channel-mask", &channelMask)) {
625                        channelMask = CHANNEL_MASK_USE_CHANNEL_ORDER;
626                    }
627
628                    CHECK_EQ(mAudioSink->open(
629                                sampleRate,
630                                numChannels,
631                                (audio_channel_mask_t)channelMask,
632                                AUDIO_FORMAT_PCM_16_BIT,
633                                8 /* bufferCount */,
634                                NULL,
635                                NULL,
636                                flags),
637                             (status_t)OK);
638                    mAudioSink->start();
639
640                    mRenderer->signalAudioSinkChanged();
641                } else {
642                    // video
643
644                    int32_t width, height;
645                    CHECK(codecRequest->findInt32("width", &width));
646                    CHECK(codecRequest->findInt32("height", &height));
647
648                    int32_t cropLeft, cropTop, cropRight, cropBottom;
649                    CHECK(codecRequest->findRect(
650                                "crop",
651                                &cropLeft, &cropTop, &cropRight, &cropBottom));
652
653                    int32_t displayWidth = cropRight - cropLeft + 1;
654                    int32_t displayHeight = cropBottom - cropTop + 1;
655
656                    ALOGV("Video output format changed to %d x %d "
657                         "(crop: %d x %d @ (%d, %d))",
658                         width, height,
659                         displayWidth,
660                         displayHeight,
661                         cropLeft, cropTop);
662
663                    sp<AMessage> videoInputFormat =
664                        mSource->getFormat(false /* audio */);
665
666                    // Take into account sample aspect ratio if necessary:
667                    int32_t sarWidth, sarHeight;
668                    if (videoInputFormat->findInt32("sar-width", &sarWidth)
669                            && videoInputFormat->findInt32(
670                                "sar-height", &sarHeight)) {
671                        ALOGV("Sample aspect ratio %d : %d",
672                              sarWidth, sarHeight);
673
674                        displayWidth = (displayWidth * sarWidth) / sarHeight;
675
676                        ALOGV("display dimensions %d x %d",
677                              displayWidth, displayHeight);
678                    }
679
680                    notifyListener(
681                            MEDIA_SET_VIDEO_SIZE, displayWidth, displayHeight);
682
683                    if (mNeedsSwRenderer && mNativeWindow != NULL) {
684                        int32_t colorFormat;
685                        CHECK(codecRequest->findInt32("color-format", &colorFormat));
686
687                        sp<MetaData> meta = new MetaData;
688                        meta->setInt32(kKeyWidth, width);
689                        meta->setInt32(kKeyHeight, height);
690                        meta->setRect(kKeyCropRect, cropLeft, cropTop, cropRight, cropBottom);
691                        meta->setInt32(kKeyColorFormat, colorFormat);
692
693                        mRenderer->setSoftRenderer(
694                                new SoftwareRenderer(mNativeWindow->getNativeWindow(), meta));
695                    }
696                }
697            } else if (what == ACodec::kWhatShutdownCompleted) {
698                ALOGV("%s shutdown completed", audio ? "audio" : "video");
699                if (audio) {
700                    mAudioDecoder.clear();
701
702                    CHECK_EQ((int)mFlushingAudio, (int)SHUTTING_DOWN_DECODER);
703                    mFlushingAudio = SHUT_DOWN;
704                } else {
705                    mVideoDecoder.clear();
706
707                    CHECK_EQ((int)mFlushingVideo, (int)SHUTTING_DOWN_DECODER);
708                    mFlushingVideo = SHUT_DOWN;
709                }
710
711                finishFlushIfPossible();
712            } else if (what == ACodec::kWhatError) {
713                ALOGE("Received error from %s decoder, aborting playback.",
714                     audio ? "audio" : "video");
715
716                mRenderer->queueEOS(audio, UNKNOWN_ERROR);
717            } else if (what == ACodec::kWhatDrainThisBuffer) {
718                renderBuffer(audio, codecRequest);
719            } else if (what == ACodec::kWhatComponentAllocated) {
720                if (!audio) {
721                    AString name;
722                    CHECK(codecRequest->findString("componentName", &name));
723                    mNeedsSwRenderer = name.startsWith("OMX.google.");
724                }
725            } else if (what != ACodec::kWhatComponentConfigured
726                    && what != ACodec::kWhatBuffersAllocated) {
727                ALOGV("Unhandled codec notification %d '%c%c%c%c'.",
728                      what,
729                      what >> 24,
730                      (what >> 16) & 0xff,
731                      (what >> 8) & 0xff,
732                      what & 0xff);
733            }
734
735            break;
736        }
737
738        case kWhatRendererNotify:
739        {
740            int32_t what;
741            CHECK(msg->findInt32("what", &what));
742
743            if (what == Renderer::kWhatEOS) {
744                int32_t audio;
745                CHECK(msg->findInt32("audio", &audio));
746
747                int32_t finalResult;
748                CHECK(msg->findInt32("finalResult", &finalResult));
749
750                if (audio) {
751                    mAudioEOS = true;
752                } else {
753                    mVideoEOS = true;
754                }
755
756                if (finalResult == ERROR_END_OF_STREAM) {
757                    ALOGV("reached %s EOS", audio ? "audio" : "video");
758                } else {
759                    ALOGE("%s track encountered an error (%d)",
760                         audio ? "audio" : "video", finalResult);
761
762                    notifyListener(
763                            MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, finalResult);
764                }
765
766                if ((mAudioEOS || mAudioDecoder == NULL)
767                        && (mVideoEOS || mVideoDecoder == NULL)) {
768                    notifyListener(MEDIA_PLAYBACK_COMPLETE, 0, 0);
769                }
770            } else if (what == Renderer::kWhatPosition) {
771                int64_t positionUs;
772                CHECK(msg->findInt64("positionUs", &positionUs));
773
774                CHECK(msg->findInt64("videoLateByUs", &mVideoLateByUs));
775
776                if (mDriver != NULL) {
777                    sp<NuPlayerDriver> driver = mDriver.promote();
778                    if (driver != NULL) {
779                        driver->notifyPosition(positionUs);
780
781                        driver->notifyFrameStats(
782                                mNumFramesTotal, mNumFramesDropped);
783                    }
784                }
785            } else if (what == Renderer::kWhatFlushComplete) {
786                int32_t audio;
787                CHECK(msg->findInt32("audio", &audio));
788
789                ALOGV("renderer %s flush completed.", audio ? "audio" : "video");
790            } else if (what == Renderer::kWhatVideoRenderingStart) {
791                notifyListener(MEDIA_INFO, MEDIA_INFO_RENDERING_START, 0);
792            } else if (what == Renderer::kWhatMediaRenderingStart) {
793                ALOGV("media rendering started");
794                notifyListener(MEDIA_STARTED, 0, 0);
795            }
796            break;
797        }
798
799        case kWhatMoreDataQueued:
800        {
801            break;
802        }
803
804        case kWhatReset:
805        {
806            ALOGV("kWhatReset");
807
808            mDeferredActions.push_back(
809                    new ShutdownDecoderAction(
810                        true /* audio */, true /* video */));
811
812            mDeferredActions.push_back(
813                    new SimpleAction(&NuPlayer::performReset));
814
815            processDeferredActions();
816            break;
817        }
818
819        case kWhatSeek:
820        {
821            int64_t seekTimeUs;
822            CHECK(msg->findInt64("seekTimeUs", &seekTimeUs));
823
824            ALOGV("kWhatSeek seekTimeUs=%lld us", seekTimeUs);
825
826            mDeferredActions.push_back(
827                    new SimpleAction(&NuPlayer::performDecoderFlush));
828
829            mDeferredActions.push_back(new SeekAction(seekTimeUs));
830
831            processDeferredActions();
832            break;
833        }
834
835        case kWhatPause:
836        {
837            CHECK(mRenderer != NULL);
838            mSource->pause();
839            mRenderer->pause();
840            break;
841        }
842
843        case kWhatResume:
844        {
845            CHECK(mRenderer != NULL);
846            mSource->resume();
847            mRenderer->resume();
848            break;
849        }
850
851        case kWhatSourceNotify:
852        {
853            onSourceNotify(msg);
854            break;
855        }
856
857        default:
858            TRESPASS();
859            break;
860    }
861}
862
863void NuPlayer::finishFlushIfPossible() {
864    if (mFlushingAudio != FLUSHED && mFlushingAudio != SHUT_DOWN) {
865        return;
866    }
867
868    if (mFlushingVideo != FLUSHED && mFlushingVideo != SHUT_DOWN) {
869        return;
870    }
871
872    ALOGV("both audio and video are flushed now.");
873
874    if (mTimeDiscontinuityPending) {
875        mRenderer->signalTimeDiscontinuity();
876        mTimeDiscontinuityPending = false;
877    }
878
879    if (mAudioDecoder != NULL) {
880        mAudioDecoder->signalResume();
881    }
882
883    if (mVideoDecoder != NULL) {
884        mVideoDecoder->signalResume();
885    }
886
887    mFlushingAudio = NONE;
888    mFlushingVideo = NONE;
889
890    processDeferredActions();
891}
892
893void NuPlayer::postScanSources() {
894    if (mScanSourcesPending) {
895        return;
896    }
897
898    sp<AMessage> msg = new AMessage(kWhatScanSources, id());
899    msg->setInt32("generation", mScanSourcesGeneration);
900    msg->post();
901
902    mScanSourcesPending = true;
903}
904
905status_t NuPlayer::instantiateDecoder(bool audio, sp<Decoder> *decoder) {
906    if (*decoder != NULL) {
907        return OK;
908    }
909
910    sp<AMessage> format = mSource->getFormat(audio);
911
912    if (format == NULL) {
913        return -EWOULDBLOCK;
914    }
915
916    if (!audio) {
917        AString mime;
918        CHECK(format->findString("mime", &mime));
919        mVideoIsAVC = !strcasecmp(MEDIA_MIMETYPE_VIDEO_AVC, mime.c_str());
920    }
921
922    sp<AMessage> notify =
923        new AMessage(audio ? kWhatAudioNotify : kWhatVideoNotify,
924                     id());
925
926    *decoder = audio ? new Decoder(notify) :
927                       new Decoder(notify, mNativeWindow);
928    looper()->registerHandler(*decoder);
929
930    (*decoder)->configure(format);
931
932    return OK;
933}
934
935status_t NuPlayer::feedDecoderInputData(bool audio, const sp<AMessage> &msg) {
936    sp<AMessage> reply;
937    CHECK(msg->findMessage("reply", &reply));
938
939    if ((audio && IsFlushingState(mFlushingAudio))
940            || (!audio && IsFlushingState(mFlushingVideo))) {
941        reply->setInt32("err", INFO_DISCONTINUITY);
942        reply->post();
943        return OK;
944    }
945
946    sp<ABuffer> accessUnit;
947
948    bool dropAccessUnit;
949    do {
950        status_t err = mSource->dequeueAccessUnit(audio, &accessUnit);
951
952        if (err == -EWOULDBLOCK) {
953            return err;
954        } else if (err != OK) {
955            if (err == INFO_DISCONTINUITY) {
956                int32_t type;
957                CHECK(accessUnit->meta()->findInt32("discontinuity", &type));
958
959                bool formatChange =
960                    (audio &&
961                     (type & ATSParser::DISCONTINUITY_AUDIO_FORMAT))
962                    || (!audio &&
963                            (type & ATSParser::DISCONTINUITY_VIDEO_FORMAT));
964
965                bool timeChange = (type & ATSParser::DISCONTINUITY_TIME) != 0;
966
967                ALOGI("%s discontinuity (formatChange=%d, time=%d)",
968                     audio ? "audio" : "video", formatChange, timeChange);
969
970                if (audio) {
971                    mSkipRenderingAudioUntilMediaTimeUs = -1;
972                } else {
973                    mSkipRenderingVideoUntilMediaTimeUs = -1;
974                }
975
976                if (timeChange) {
977                    sp<AMessage> extra;
978                    if (accessUnit->meta()->findMessage("extra", &extra)
979                            && extra != NULL) {
980                        int64_t resumeAtMediaTimeUs;
981                        if (extra->findInt64(
982                                    "resume-at-mediatimeUs", &resumeAtMediaTimeUs)) {
983                            ALOGI("suppressing rendering of %s until %lld us",
984                                    audio ? "audio" : "video", resumeAtMediaTimeUs);
985
986                            if (audio) {
987                                mSkipRenderingAudioUntilMediaTimeUs =
988                                    resumeAtMediaTimeUs;
989                            } else {
990                                mSkipRenderingVideoUntilMediaTimeUs =
991                                    resumeAtMediaTimeUs;
992                            }
993                        }
994                    }
995                }
996
997                mTimeDiscontinuityPending =
998                    mTimeDiscontinuityPending || timeChange;
999
1000                if (formatChange || timeChange) {
1001                    if (mFlushingAudio == NONE && mFlushingVideo == NONE) {
1002                        // And we'll resume scanning sources once we're done
1003                        // flushing.
1004                        mDeferredActions.push_front(
1005                                new SimpleAction(
1006                                    &NuPlayer::performScanSources));
1007                    }
1008
1009                    flushDecoder(audio, formatChange);
1010                } else {
1011                    // This stream is unaffected by the discontinuity
1012
1013                    if (audio) {
1014                        mFlushingAudio = FLUSHED;
1015                    } else {
1016                        mFlushingVideo = FLUSHED;
1017                    }
1018
1019                    finishFlushIfPossible();
1020
1021                    return -EWOULDBLOCK;
1022                }
1023            }
1024
1025            reply->setInt32("err", err);
1026            reply->post();
1027            return OK;
1028        }
1029
1030        if (!audio) {
1031            ++mNumFramesTotal;
1032        }
1033
1034        dropAccessUnit = false;
1035        if (!audio
1036                && mVideoLateByUs > 100000ll
1037                && mVideoIsAVC
1038                && !IsAVCReferenceFrame(accessUnit)) {
1039            dropAccessUnit = true;
1040            ++mNumFramesDropped;
1041        }
1042    } while (dropAccessUnit);
1043
1044    // ALOGV("returned a valid buffer of %s data", audio ? "audio" : "video");
1045
1046#if 0
1047    int64_t mediaTimeUs;
1048    CHECK(accessUnit->meta()->findInt64("timeUs", &mediaTimeUs));
1049    ALOGV("feeding %s input buffer at media time %.2f secs",
1050         audio ? "audio" : "video",
1051         mediaTimeUs / 1E6);
1052#endif
1053
1054    reply->setBuffer("buffer", accessUnit);
1055    reply->post();
1056
1057    return OK;
1058}
1059
1060void NuPlayer::renderBuffer(bool audio, const sp<AMessage> &msg) {
1061    // ALOGV("renderBuffer %s", audio ? "audio" : "video");
1062
1063    sp<AMessage> reply;
1064    CHECK(msg->findMessage("reply", &reply));
1065
1066    if (IsFlushingState(audio ? mFlushingAudio : mFlushingVideo)) {
1067        // We're currently attempting to flush the decoder, in order
1068        // to complete this, the decoder wants all its buffers back,
1069        // so we don't want any output buffers it sent us (from before
1070        // we initiated the flush) to be stuck in the renderer's queue.
1071
1072        ALOGV("we're still flushing the %s decoder, sending its output buffer"
1073             " right back.", audio ? "audio" : "video");
1074
1075        reply->post();
1076        return;
1077    }
1078
1079    sp<ABuffer> buffer;
1080    CHECK(msg->findBuffer("buffer", &buffer));
1081
1082    int64_t &skipUntilMediaTimeUs =
1083        audio
1084            ? mSkipRenderingAudioUntilMediaTimeUs
1085            : mSkipRenderingVideoUntilMediaTimeUs;
1086
1087    if (skipUntilMediaTimeUs >= 0) {
1088        int64_t mediaTimeUs;
1089        CHECK(buffer->meta()->findInt64("timeUs", &mediaTimeUs));
1090
1091        if (mediaTimeUs < skipUntilMediaTimeUs) {
1092            ALOGV("dropping %s buffer at time %lld as requested.",
1093                 audio ? "audio" : "video",
1094                 mediaTimeUs);
1095
1096            reply->post();
1097            return;
1098        }
1099
1100        skipUntilMediaTimeUs = -1;
1101    }
1102
1103    mRenderer->queueBuffer(audio, buffer, reply);
1104}
1105
1106void NuPlayer::notifyListener(int msg, int ext1, int ext2, const Parcel *in) {
1107    if (mDriver == NULL) {
1108        return;
1109    }
1110
1111    sp<NuPlayerDriver> driver = mDriver.promote();
1112
1113    if (driver == NULL) {
1114        return;
1115    }
1116
1117    driver->notifyListener(msg, ext1, ext2, in);
1118}
1119
1120void NuPlayer::flushDecoder(bool audio, bool needShutdown) {
1121    ALOGV("[%s] flushDecoder needShutdown=%d",
1122          audio ? "audio" : "video", needShutdown);
1123
1124    if ((audio && mAudioDecoder == NULL) || (!audio && mVideoDecoder == NULL)) {
1125        ALOGI("flushDecoder %s without decoder present",
1126             audio ? "audio" : "video");
1127    }
1128
1129    // Make sure we don't continue to scan sources until we finish flushing.
1130    ++mScanSourcesGeneration;
1131    mScanSourcesPending = false;
1132
1133    (audio ? mAudioDecoder : mVideoDecoder)->signalFlush();
1134    mRenderer->flush(audio);
1135
1136    FlushStatus newStatus =
1137        needShutdown ? FLUSHING_DECODER_SHUTDOWN : FLUSHING_DECODER;
1138
1139    if (audio) {
1140        CHECK(mFlushingAudio == NONE
1141                || mFlushingAudio == AWAITING_DISCONTINUITY);
1142
1143        mFlushingAudio = newStatus;
1144
1145        if (mFlushingVideo == NONE) {
1146            mFlushingVideo = (mVideoDecoder != NULL)
1147                ? AWAITING_DISCONTINUITY
1148                : FLUSHED;
1149        }
1150    } else {
1151        CHECK(mFlushingVideo == NONE
1152                || mFlushingVideo == AWAITING_DISCONTINUITY);
1153
1154        mFlushingVideo = newStatus;
1155
1156        if (mFlushingAudio == NONE) {
1157            mFlushingAudio = (mAudioDecoder != NULL)
1158                ? AWAITING_DISCONTINUITY
1159                : FLUSHED;
1160        }
1161    }
1162}
1163
1164sp<AMessage> NuPlayer::Source::getFormat(bool audio) {
1165    sp<MetaData> meta = getFormatMeta(audio);
1166
1167    if (meta == NULL) {
1168        return NULL;
1169    }
1170
1171    sp<AMessage> msg = new AMessage;
1172
1173    if(convertMetaDataToMessage(meta, &msg) == OK) {
1174        return msg;
1175    }
1176    return NULL;
1177}
1178
1179status_t NuPlayer::setVideoScalingMode(int32_t mode) {
1180    mVideoScalingMode = mode;
1181    if (mNativeWindow != NULL) {
1182        status_t ret = native_window_set_scaling_mode(
1183                mNativeWindow->getNativeWindow().get(), mVideoScalingMode);
1184        if (ret != OK) {
1185            ALOGE("Failed to set scaling mode (%d): %s",
1186                -ret, strerror(-ret));
1187            return ret;
1188        }
1189    }
1190    return OK;
1191}
1192
1193status_t NuPlayer::getTrackInfo(Parcel* reply) const {
1194    sp<AMessage> msg = new AMessage(kWhatGetTrackInfo, id());
1195    msg->setPointer("reply", reply);
1196
1197    sp<AMessage> response;
1198    status_t err = msg->postAndAwaitResponse(&response);
1199    return err;
1200}
1201
1202status_t NuPlayer::selectTrack(size_t trackIndex, bool select) {
1203    sp<AMessage> msg = new AMessage(kWhatSelectTrack, id());
1204    msg->setSize("trackIndex", trackIndex);
1205    msg->setInt32("select", select);
1206
1207    sp<AMessage> response;
1208    status_t err = msg->postAndAwaitResponse(&response);
1209
1210    return err;
1211}
1212
1213void NuPlayer::schedulePollDuration() {
1214    sp<AMessage> msg = new AMessage(kWhatPollDuration, id());
1215    msg->setInt32("generation", mPollDurationGeneration);
1216    msg->post();
1217}
1218
1219void NuPlayer::cancelPollDuration() {
1220    ++mPollDurationGeneration;
1221}
1222
1223void NuPlayer::processDeferredActions() {
1224    while (!mDeferredActions.empty()) {
1225        // We won't execute any deferred actions until we're no longer in
1226        // an intermediate state, i.e. one more more decoders are currently
1227        // flushing or shutting down.
1228
1229        if (mRenderer != NULL) {
1230            // There's an edge case where the renderer owns all output
1231            // buffers and is paused, therefore the decoder will not read
1232            // more input data and will never encounter the matching
1233            // discontinuity. To avoid this, we resume the renderer.
1234
1235            if (mFlushingAudio == AWAITING_DISCONTINUITY
1236                    || mFlushingVideo == AWAITING_DISCONTINUITY) {
1237                mRenderer->resume();
1238            }
1239        }
1240
1241        if (mFlushingAudio != NONE || mFlushingVideo != NONE) {
1242            // We're currently flushing, postpone the reset until that's
1243            // completed.
1244
1245            ALOGV("postponing action mFlushingAudio=%d, mFlushingVideo=%d",
1246                  mFlushingAudio, mFlushingVideo);
1247
1248            break;
1249        }
1250
1251        sp<Action> action = *mDeferredActions.begin();
1252        mDeferredActions.erase(mDeferredActions.begin());
1253
1254        action->execute(this);
1255    }
1256}
1257
1258void NuPlayer::performSeek(int64_t seekTimeUs) {
1259    ALOGV("performSeek seekTimeUs=%lld us (%.2f secs)",
1260          seekTimeUs,
1261          seekTimeUs / 1E6);
1262
1263    mSource->seekTo(seekTimeUs);
1264
1265    if (mDriver != NULL) {
1266        sp<NuPlayerDriver> driver = mDriver.promote();
1267        if (driver != NULL) {
1268            driver->notifyPosition(seekTimeUs);
1269            driver->notifySeekComplete();
1270        }
1271    }
1272
1273    // everything's flushed, continue playback.
1274}
1275
1276void NuPlayer::performDecoderFlush() {
1277    ALOGV("performDecoderFlush");
1278
1279    if (mAudioDecoder == NULL && mVideoDecoder == NULL) {
1280        return;
1281    }
1282
1283    mTimeDiscontinuityPending = true;
1284
1285    if (mAudioDecoder != NULL) {
1286        flushDecoder(true /* audio */, false /* needShutdown */);
1287    }
1288
1289    if (mVideoDecoder != NULL) {
1290        flushDecoder(false /* audio */, false /* needShutdown */);
1291    }
1292}
1293
1294void NuPlayer::performDecoderShutdown(bool audio, bool video) {
1295    ALOGV("performDecoderShutdown audio=%d, video=%d", audio, video);
1296
1297    if ((!audio || mAudioDecoder == NULL)
1298            && (!video || mVideoDecoder == NULL)) {
1299        return;
1300    }
1301
1302    mTimeDiscontinuityPending = true;
1303
1304    if (mFlushingAudio == NONE && (!audio || mAudioDecoder == NULL)) {
1305        mFlushingAudio = FLUSHED;
1306    }
1307
1308    if (mFlushingVideo == NONE && (!video || mVideoDecoder == NULL)) {
1309        mFlushingVideo = FLUSHED;
1310    }
1311
1312    if (audio && mAudioDecoder != NULL) {
1313        flushDecoder(true /* audio */, true /* needShutdown */);
1314    }
1315
1316    if (video && mVideoDecoder != NULL) {
1317        flushDecoder(false /* audio */, true /* needShutdown */);
1318    }
1319}
1320
1321void NuPlayer::performReset() {
1322    ALOGV("performReset");
1323
1324    CHECK(mAudioDecoder == NULL);
1325    CHECK(mVideoDecoder == NULL);
1326
1327    cancelPollDuration();
1328
1329    ++mScanSourcesGeneration;
1330    mScanSourcesPending = false;
1331
1332    mRenderer.clear();
1333
1334    if (mSource != NULL) {
1335        mSource->stop();
1336
1337        looper()->unregisterHandler(mSource->id());
1338
1339        mSource.clear();
1340    }
1341
1342    if (mDriver != NULL) {
1343        sp<NuPlayerDriver> driver = mDriver.promote();
1344        if (driver != NULL) {
1345            driver->notifyResetComplete();
1346        }
1347    }
1348
1349    mStarted = false;
1350}
1351
1352void NuPlayer::performScanSources() {
1353    ALOGV("performScanSources");
1354
1355    if (!mStarted) {
1356        return;
1357    }
1358
1359    if (mAudioDecoder == NULL || mVideoDecoder == NULL) {
1360        postScanSources();
1361    }
1362}
1363
1364void NuPlayer::performSetSurface(const sp<NativeWindowWrapper> &wrapper) {
1365    ALOGV("performSetSurface");
1366
1367    mNativeWindow = wrapper;
1368
1369    // XXX - ignore error from setVideoScalingMode for now
1370    setVideoScalingMode(mVideoScalingMode);
1371
1372    if (mDriver != NULL) {
1373        sp<NuPlayerDriver> driver = mDriver.promote();
1374        if (driver != NULL) {
1375            driver->notifySetSurfaceComplete();
1376        }
1377    }
1378}
1379
1380void NuPlayer::onSourceNotify(const sp<AMessage> &msg) {
1381    int32_t what;
1382    CHECK(msg->findInt32("what", &what));
1383
1384    switch (what) {
1385        case Source::kWhatPrepared:
1386        {
1387            if (mSource == NULL) {
1388                // This is a stale notification from a source that was
1389                // asynchronously preparing when the client called reset().
1390                // We handled the reset, the source is gone.
1391                break;
1392            }
1393
1394            int32_t err;
1395            CHECK(msg->findInt32("err", &err));
1396
1397            sp<NuPlayerDriver> driver = mDriver.promote();
1398            if (driver != NULL) {
1399                driver->notifyPrepareCompleted(err);
1400            }
1401
1402            int64_t durationUs;
1403            if (mDriver != NULL && mSource->getDuration(&durationUs) == OK) {
1404                sp<NuPlayerDriver> driver = mDriver.promote();
1405                if (driver != NULL) {
1406                    driver->notifyDuration(durationUs);
1407                }
1408            }
1409            break;
1410        }
1411
1412        case Source::kWhatFlagsChanged:
1413        {
1414            uint32_t flags;
1415            CHECK(msg->findInt32("flags", (int32_t *)&flags));
1416
1417            sp<NuPlayerDriver> driver = mDriver.promote();
1418            if (driver != NULL) {
1419                driver->notifyFlagsChanged(flags);
1420            }
1421
1422            if ((mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1423                    && (!(flags & Source::FLAG_DYNAMIC_DURATION))) {
1424                cancelPollDuration();
1425            } else if (!(mSourceFlags & Source::FLAG_DYNAMIC_DURATION)
1426                    && (flags & Source::FLAG_DYNAMIC_DURATION)
1427                    && (mAudioDecoder != NULL || mVideoDecoder != NULL)) {
1428                schedulePollDuration();
1429            }
1430
1431            mSourceFlags = flags;
1432            break;
1433        }
1434
1435        case Source::kWhatVideoSizeChanged:
1436        {
1437            int32_t width, height;
1438            CHECK(msg->findInt32("width", &width));
1439            CHECK(msg->findInt32("height", &height));
1440
1441            notifyListener(MEDIA_SET_VIDEO_SIZE, width, height);
1442            break;
1443        }
1444
1445        case Source::kWhatBufferingStart:
1446        {
1447            notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_START, 0);
1448            break;
1449        }
1450
1451        case Source::kWhatBufferingEnd:
1452        {
1453            notifyListener(MEDIA_INFO, MEDIA_INFO_BUFFERING_END, 0);
1454            break;
1455        }
1456
1457        case Source::kWhatSubtitleData:
1458        {
1459            sp<ABuffer> buffer;
1460            CHECK(msg->findBuffer("buffer", &buffer));
1461
1462            int32_t trackIndex;
1463            int64_t timeUs, durationUs;
1464            CHECK(buffer->meta()->findInt32("trackIndex", &trackIndex));
1465            CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1466            CHECK(buffer->meta()->findInt64("durationUs", &durationUs));
1467
1468            Parcel in;
1469            in.writeInt32(trackIndex);
1470            in.writeInt64(timeUs);
1471            in.writeInt64(durationUs);
1472            in.writeInt32(buffer->size());
1473            in.writeInt32(buffer->size());
1474            in.write(buffer->data(), buffer->size());
1475
1476            notifyListener(MEDIA_SUBTITLE_DATA, 0, 0, &in);
1477            break;
1478        }
1479
1480        case Source::kWhatQueueDecoderShutdown:
1481        {
1482            int32_t audio, video;
1483            CHECK(msg->findInt32("audio", &audio));
1484            CHECK(msg->findInt32("video", &video));
1485
1486            sp<AMessage> reply;
1487            CHECK(msg->findMessage("reply", &reply));
1488
1489            queueDecoderShutdown(audio, video, reply);
1490            break;
1491        }
1492
1493        default:
1494            TRESPASS();
1495    }
1496}
1497
1498////////////////////////////////////////////////////////////////////////////////
1499
1500void NuPlayer::Source::notifyFlagsChanged(uint32_t flags) {
1501    sp<AMessage> notify = dupNotify();
1502    notify->setInt32("what", kWhatFlagsChanged);
1503    notify->setInt32("flags", flags);
1504    notify->post();
1505}
1506
1507void NuPlayer::Source::notifyVideoSizeChanged(int32_t width, int32_t height) {
1508    sp<AMessage> notify = dupNotify();
1509    notify->setInt32("what", kWhatVideoSizeChanged);
1510    notify->setInt32("width", width);
1511    notify->setInt32("height", height);
1512    notify->post();
1513}
1514
1515void NuPlayer::Source::notifyPrepared(status_t err) {
1516    sp<AMessage> notify = dupNotify();
1517    notify->setInt32("what", kWhatPrepared);
1518    notify->setInt32("err", err);
1519    notify->post();
1520}
1521
1522void NuPlayer::Source::onMessageReceived(const sp<AMessage> & /* msg */) {
1523    TRESPASS();
1524}
1525
1526void NuPlayer::queueDecoderShutdown(
1527        bool audio, bool video, const sp<AMessage> &reply) {
1528    ALOGI("queueDecoderShutdown audio=%d, video=%d", audio, video);
1529
1530    mDeferredActions.push_back(
1531            new ShutdownDecoderAction(audio, video));
1532
1533    mDeferredActions.push_back(
1534            new SimpleAction(&NuPlayer::performScanSources));
1535
1536    mDeferredActions.push_back(new PostMessageAction(reply));
1537
1538    processDeferredActions();
1539}
1540
1541}  // namespace android
1542