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