Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9bb23ff6 authored by Chong Zhang's avatar Chong Zhang Committed by Android (Google) Code Review
Browse files

Merge "HLS: faster switching and pause/resume on low buffer"

parents 27ce49ff 7c870804
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -281,6 +281,34 @@ void NuPlayer::HTTPLiveSource::onSessionNotify(const sp<AMessage> &msg) {
            break;
        }

        case LiveSession::kWhatBufferingStart:
        {
            sp<AMessage> notify = dupNotify();
            notify->setInt32("what", kWhatPauseOnBufferingStart);
            notify->post();
            break;
        }

        case LiveSession::kWhatBufferingEnd:
        {
            sp<AMessage> notify = dupNotify();
            notify->setInt32("what", kWhatResumeOnBufferingEnd);
            notify->post();
            break;
        }


        case LiveSession::kWhatBufferingUpdate:
        {
            sp<AMessage> notify = dupNotify();
            int32_t percentage;
            CHECK(msg->findInt32("percentage", &percentage));
            notify->setInt32("what", kWhatBufferingUpdate);
            notify->setInt32("percentage", percentage);
            notify->post();
            break;
        }

        case LiveSession::kWhatError:
        {
            break;
+505 −161

File changed.

Preview size limit exceeded, changes collapsed.

+39 −11
Original line number Diff line number Diff line
@@ -89,11 +89,16 @@ struct LiveSession : public AHandler {
    bool isSeekable() const;
    bool hasDynamicDuration() const;

    static const char *getKeyForStream(StreamType type);

    enum {
        kWhatStreamsChanged,
        kWhatError,
        kWhatPrepared,
        kWhatPreparationFailed,
        kWhatBufferingStart,
        kWhatBufferingEnd,
        kWhatBufferingUpdate,
    };

protected:
@@ -116,9 +121,15 @@ private:
        kWhatPollBuffering              = 'poll',
    };

    static const int64_t kHighWaterMark;
    static const int64_t kMidWaterMark;
    static const int64_t kLowWaterMark;
    // Bandwidth Switch Mark Defaults
    static const int64_t kUpSwitchMark = 25000000ll;
    static const int64_t kDownSwitchMark = 18000000ll;
    static const int64_t kUpSwitchMargin = 5000000ll;

    // Buffer Prepare/Ready/Underflow Marks
    static const int64_t kReadyMark = 5000000ll;
    static const int64_t kPrepareMark = 1500000ll;
    static const int64_t kUnderflowMark = 1000000ll;

    struct BandwidthEstimator;
    struct BandwidthItem {
@@ -160,8 +171,10 @@ private:
    uint32_t mFlags;
    sp<IMediaHTTPService> mHTTPService;

    bool mBuffering;
    bool mInPreparationPhase;
    bool mBuffering[kMaxStreams];
    int32_t mPollBufferingGeneration;
    int32_t mPrevBufferPercentage;

    sp<HTTPBase> mHTTPDataSource;
    KeyedVector<String8, String8> mExtraHeaders;
@@ -170,6 +183,7 @@ private:

    Vector<BandwidthItem> mBandwidthItems;
    ssize_t mCurBandwidthIndex;
    ssize_t mOrigBandwidthIndex;
    int32_t mLastBandwidthBps;
    sp<BandwidthEstimator> mBandwidthEstimator;

@@ -204,6 +218,10 @@ private:

    bool mReconfigurationInProgress;
    bool mSwitchInProgress;
    int64_t mUpSwitchMark;
    int64_t mDownSwitchMark;
    int64_t mUpSwitchMargin;

    sp<AReplyToken> mDisconnectReplyID;
    sp<AReplyToken> mSeekReplyID;

@@ -213,8 +231,6 @@ private:
    KeyedVector<size_t, int64_t> mDiscontinuityAbsStartTimesUs;
    KeyedVector<size_t, int64_t> mDiscontinuityOffsetTimesUs;

    int32_t mPollBufferingGeneration;

    sp<PlaylistFetcher> addFetcher(const char *uri);

    void onConnect(const sp<AMessage> &msg);
@@ -247,6 +263,10 @@ private:
    sp<M3UParser> fetchPlaylist(
            const char *url, uint8_t *curPlaylistHash, bool *unchanged);

    bool resumeFetcher(
            const AString &uri, uint32_t streamMask,
            int64_t timeUs = -1ll, bool newUri = false);

    float getAbortThreshold(
            ssize_t currentBWIndex, ssize_t targetBWIndex) const;
    void addBandwidthMeasurement(size_t numBytes, int64_t delayUs);
@@ -258,24 +278,32 @@ private:
    static ssize_t typeToIndex(int32_t type);

    void changeConfiguration(
            int64_t timeUs, size_t bandwidthIndex, bool pickTrack = false);
            int64_t timeUs, ssize_t bwIndex = -1, bool pickTrack = false);
    void onChangeConfiguration(const sp<AMessage> &msg);
    void onChangeConfiguration2(const sp<AMessage> &msg);
    void onChangeConfiguration3(const sp<AMessage> &msg);

    void swapPacketSource(StreamType stream);
    void tryToFinishBandwidthSwitch(const AString &uri);
    void tryToFinishBandwidthSwitch(const AString &oldUri);
    void cancelBandwidthSwitch(bool resume = false);
    bool checkSwitchProgress(
            sp<AMessage> &msg, int64_t delayUs, bool *needResumeUntil);

    void cancelBandwidthSwitch();
    void switchBandwidthIfNeeded(bool bufferHigh, bool bufferLow);

    void schedulePollBuffering();
    void cancelPollBuffering();
    void restartPollBuffering();
    void onPollBuffering();
    bool checkBuffering(bool &low, bool &mid, bool &high);
    void switchBandwidthIfNeeded(bool bufferHigh, bool bufferLow);
    bool checkBuffering(bool &underflow, bool &ready, bool &down, bool &up);
    void startBufferingIfNecessary();
    void stopBufferingIfNecessary();
    void notifyBufferingUpdate(int32_t percentage);

    void finishDisconnect();

    void postPrepared(status_t err);
    void postError(status_t err);

    DISALLOW_EVIL_CONSTRUCTORS(LiveSession);
};
+200 −140

File changed.

Preview size limit exceeded, changes collapsed.

+8 −3
Original line number Diff line number Diff line
@@ -44,9 +44,11 @@ struct PlaylistFetcher : public AHandler {
        kWhatStopped,
        kWhatError,
        kWhatDurationUpdate,
        kWhatTargetDurationUpdate,
        kWhatPrepared,
        kWhatPreparationFailed,
        kWhatStartedAt,
        kWhatStopReached,
    };

    PlaylistFetcher(
@@ -64,7 +66,7 @@ struct PlaylistFetcher : public AHandler {
            int64_t startTimeUs = -1ll,         // starting timestamps
            int64_t segmentStartTimeUs = -1ll, // starting position within playlist
            // startTimeUs!=segmentStartTimeUs only when playlist is live
            int32_t startDiscontinuitySeq = 0,
            int32_t startDiscontinuitySeq = -1,
            LiveSession::SeekMode seekMode = LiveSession::kSeekModeExactPosition);

    void pauseAsync(float thresholdRatio);
@@ -135,7 +137,6 @@ private:
    bool mStartup;
    bool mIDRFound;
    int32_t mSeekMode;
    bool mPrepared;
    bool mTimeChangeSignaled;
    int64_t mNextPTSTimeUs;

@@ -187,7 +188,7 @@ private:
    void postMonitorQueue(int64_t delayUs = 0, int64_t minDelayUs = 0);
    void cancelMonitorQueue();
    void setStoppingThreshold(float thresholdRatio);
    bool shouldPauseDownload(bool startFound);
    bool shouldPauseDownload();

    int64_t delayUsToRefreshPlaylist() const;
    status_t refreshPlaylist();
@@ -195,6 +196,8 @@ private:
    // Returns the media time in us of the segment specified by seqNumber.
    // This is computed by summing the durations of all segments before it.
    int64_t getSegmentStartTimeUs(int32_t seqNumber) const;
    // Returns the duration time in us of the segment specified.
    int64_t getSegmentDurationUs(int32_t seqNumber) const;

    status_t onStart(const sp<AMessage> &msg);
    void onPause();
@@ -219,6 +222,7 @@ private:
    status_t extractAndQueueAccessUnits(
            const sp<ABuffer> &buffer, const sp<AMessage> &itemMeta);

    void notifyStopReached();
    void notifyError(status_t err);

    void queueDiscontinuity(
@@ -230,6 +234,7 @@ private:
    int32_t getSeqNumberForTime(int64_t timeUs) const;

    void updateDuration();
    void updateTargetDuration();

    DISALLOW_EVIL_CONSTRUCTORS(PlaylistFetcher);
};
Loading