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