From 51042524744b7a23172ae6d9d239d19c8ceea6f0 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sun, 20 Sep 2020 10:56:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHLS=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E7=BB=93=E6=9D=9F=E5=90=8E=EF=BC=8C=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=88=87=E7=89=87=E6=97=A0=E6=B3=95=E5=86=99=E5=85=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/HlsMaker.cpp | 11 ++++++----- src/Record/HlsMaker.h | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Record/HlsMaker.cpp b/src/Record/HlsMaker.cpp index ff8ead52..291db725 100644 --- a/src/Record/HlsMaker.cpp +++ b/src/Record/HlsMaker.cpp @@ -68,10 +68,11 @@ void HlsMaker::inputData(void *data, uint32_t len, uint32_t timestamp, bool is_i if (!_last_file_name.empty()) { //存在切片才写入ts数据 onWriteSegment((char *) data, len); + _last_timestamp = timestamp; } } else { //resetTracks时触发此逻辑 - flushLastSegment(timestamp, true); + flushLastSegment(true); } } @@ -99,24 +100,24 @@ void HlsMaker::addNewSegment(uint32_t stamp) { } //关闭并保存上一个切片,如果_seg_number==0,那么是点播。 - flushLastSegment(stamp, _seg_number == 0); + flushLastSegment(_seg_number == 0); //新增切片 _last_file_name = onOpenSegment(_file_index++); //记录本次切片的起始时间戳 _last_seg_timestamp = stamp; } -void HlsMaker::flushLastSegment(uint32_t timestamp, bool eof){ +void HlsMaker::flushLastSegment(bool eof){ if (_last_file_name.empty()) { //不存在上个切片 return; } //文件创建到最后一次数据写入的时间即为切片长度 - auto seg_dur = timestamp - _last_seg_timestamp; + auto seg_dur = _last_timestamp - _last_seg_timestamp; if (seg_dur <= 0) { seg_dur = 100; } - _seg_dur_list.push_back(std::make_tuple(seg_dur, _last_file_name)); + _seg_dur_list.push_back(std::make_tuple(seg_dur, std::move(_last_file_name))); delOldSegment(); makeIndexFile(eof); _last_file_name.clear(); diff --git a/src/Record/HlsMaker.h b/src/Record/HlsMaker.h index 37666b93..607572c7 100644 --- a/src/Record/HlsMaker.h +++ b/src/Record/HlsMaker.h @@ -80,10 +80,9 @@ protected: /** * 关闭上个ts切片并且写入m3u8索引 - * @param timestamp 毫秒时间戳 - * @param eof + * @param eof HLS直播是否已结束 */ - void flushLastSegment(uint32_t timestamp, bool eof = false); + void flushLastSegment(bool eof); private: /** @@ -106,6 +105,7 @@ private: private: float _seg_duration = 0; uint32_t _seg_number = 0; + uint32_t _last_timestamp = 0; uint32_t _last_seg_timestamp = 0; uint64_t _file_index = 0; string _last_file_name;