diff --git a/src/Http/HlsParser.cpp b/src/Http/HlsParser.cpp index ae876c4c..2ab64ddf 100644 --- a/src/Http/HlsParser.cpp +++ b/src/Http/HlsParser.cpp @@ -137,4 +137,7 @@ bool HlsParser::isM3u8Inner() const { return _is_m3u8_inner; } -}//namespace mediakit \ No newline at end of file +float HlsParser::getTotalDuration() const { + return _total_dur; +} +}//namespace mediakit diff --git a/src/Http/HlsParser.h b/src/Http/HlsParser.h index 9ced4158..d4c253a0 100644 --- a/src/Http/HlsParser.h +++ b/src/Http/HlsParser.h @@ -74,7 +74,12 @@ public: * 内部是否含有子m3u8 */ bool isM3u8Inner() const; - + /** + * 得到总时间 + * @return + */ + float getTotalDuration() const; + protected: //解析出ts文件地址回调 virtual void onParsed(bool is_m3u8_inner,int64_t sequence,const map &ts_list) {}; diff --git a/src/Http/HlsPlayer.cpp b/src/Http/HlsPlayer.cpp index b88c8243..e2cd530d 100644 --- a/src/Http/HlsPlayer.cpp +++ b/src/Http/HlsPlayer.cpp @@ -184,9 +184,25 @@ void HlsPlayer::onResponseCompleted() { } } -float HlsPlayer::delaySecond(){ +float HlsPlayer::delaySecond() { if (HlsParser::isM3u8() && HlsParser::getTargetDur() > 0) { - return (float)HlsParser::getTargetDur(); + float targetOffset; + if (HlsParser::isLive()) { + // see RFC 8216, Section 4.4.3.8. + // 根据rfc刷新index列表的周期应该是分段时间x3, 因为根据规范播放器只处理最后3个Segment + targetOffset = (float) (3 * HlsParser::getTargetDur()); + } else { + // 点播则一般m3u8文件不会在改变了, 没必要频繁的刷新, 所以按照总时间来进行刷新 + targetOffset = HlsParser::getTotalDuration(); + } + // 取最小值, 避免因为分段时长不规则而导致的问题 + if (targetOffset > HlsParser::getTotalDuration()) { + targetOffset = HlsParser::getTotalDuration(); + } + // 根据规范为一半的时间 + if(targetOffset / 2 > 1.0f) { + return targetOffset / 2; + } } return 1.0f; } @@ -391,4 +407,4 @@ vector HlsPlayerImp::getTracks(bool ready) const { return static_pointer_cast(_demuxer)->getTracks(ready); } -}//namespace mediakit \ No newline at end of file +}//namespace mediakit