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