From 0c1159959cc0da92fde32d457722a645c7bdd99a Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 17 Dec 2021 02:04:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=89=E5=8E=BBhls=E7=B4=A2=E5=BC=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=97=B6,=20=E4=B8=8D=E8=83=BD=E4=BB=85?= =?UTF-8?q?=E4=BB=85=E5=8F=AA=E6=98=AF=E6=8C=89=E7=85=A7m3u8=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=AD=E7=9A=84=E5=88=86=E6=AE=B5=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=9D=A5=E6=8B=89=E5=8F=96,=20=E8=BF=99=E6=A0=B7=E5=9C=A8?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E5=BB=B6=E8=BF=9F=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E5=BE=88=E5=AE=B9=E6=98=93=E5=87=BA=E7=8E=B0=E9=97=AE?= =?UTF-8?q?=E9=A2=98,=20=E6=A0=B9=E6=8D=AE=E8=A7=84=E8=8C=83=E4=B8=8Effmpe?= =?UTF-8?q?g=E4=B8=AD=E7=9A=84=E5=AE=9E=E7=8E=B0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=A6=82=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HlsParser.cpp | 5 ++++- src/Http/HlsParser.h | 7 ++++++- src/Http/HlsPlayer.cpp | 22 +++++++++++++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) 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