mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-22 19:00:01 +08:00
媒体时间戳默认改为相对时间戳,通过时间戳矫正器解决跳跃和回退问题 (#2523)
通过用户反馈发现,遇到较多直播源时间戳混乱问题,包括rtsp流之rtcp ntp时间戳混乱,国标流时间戳混乱问题。 之前默认采用源流绝对时间戳,但是由于时间戳混乱会导致hls切片异常或视频播放不了等问题。 故默认改成相对时间戳(modify_stamp=2),通过时间戳矫正器过滤了时间戳跳跃以及回退问题(强制加1ms), 同时保留了对之前时间戳覆盖(modify_stamp=1)的兼容。 该修改同时兼容点播流,在点播流开启时间戳覆盖时,不起实质作用(采用原始绝对时间戳)。
This commit is contained in:
parent
af57691bc8
commit
e4acc59bec
@ -32,7 +32,10 @@ restart_sec=0
|
|||||||
#转协议相关开关;如果addStreamProxy api和on_publish hook回复未指定转协议参数,则采用这些配置项
|
#转协议相关开关;如果addStreamProxy api和on_publish hook回复未指定转协议参数,则采用这些配置项
|
||||||
[protocol]
|
[protocol]
|
||||||
#转协议时,是否开启帧级时间戳覆盖
|
#转协议时,是否开启帧级时间戳覆盖
|
||||||
modify_stamp=0
|
# 0:采用源视频流绝对时间戳,不做任何改变
|
||||||
|
# 1:采用zlmediakit接收数据时的系统时间戳(有平滑处理)
|
||||||
|
# 2:采用源视频流时间戳相对时间戳(增长量),有做时间戳跳跃和回退矫正
|
||||||
|
modify_stamp=2
|
||||||
#转协议是否开启音频
|
#转协议是否开启音频
|
||||||
enable_audio=1
|
enable_audio=1
|
||||||
#添加acc静音音频,在关闭音频时,此开关无效
|
#添加acc静音音频,在关闭音频时,此开关无效
|
||||||
|
@ -582,7 +582,7 @@
|
|||||||
{
|
{
|
||||||
"key": "modify_stamp",
|
"key": "modify_stamp",
|
||||||
"value": null,
|
"value": null,
|
||||||
"description": "是否重新计算时间戳",
|
"description": "是否修改原始时间戳,默认值2;取值范围:0.采用源视频流绝对时间戳,不做任何改变;1.采用zlmediakit接收数据时的系统时间戳(有平滑处理);2.采用源视频流时间戳相对时间戳(增长量),有做时间戳跳跃和回退矫正",
|
||||||
"disabled": true
|
"disabled": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -53,7 +53,7 @@ string getOriginTypeString(MediaOriginType type){
|
|||||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
ProtocolOption::ProtocolOption() {
|
ProtocolOption::ProtocolOption() {
|
||||||
GET_CONFIG(bool, s_modify_stamp, Protocol::kModifyStamp);
|
GET_CONFIG(int, s_modify_stamp, Protocol::kModifyStamp);
|
||||||
GET_CONFIG(bool, s_enabel_audio, Protocol::kEnableAudio);
|
GET_CONFIG(bool, s_enabel_audio, Protocol::kEnableAudio);
|
||||||
GET_CONFIG(bool, s_add_mute_audio, Protocol::kAddMuteAudio);
|
GET_CONFIG(bool, s_add_mute_audio, Protocol::kAddMuteAudio);
|
||||||
GET_CONFIG(uint32_t, s_continue_push_ms, Protocol::kContinuePushMS);
|
GET_CONFIG(uint32_t, s_continue_push_ms, Protocol::kContinuePushMS);
|
||||||
|
@ -136,8 +136,14 @@ class ProtocolOption {
|
|||||||
public:
|
public:
|
||||||
ProtocolOption();
|
ProtocolOption();
|
||||||
|
|
||||||
//时间戳修复这一路流标志位
|
enum {
|
||||||
bool modify_stamp;
|
kModifyStampOff = 0, // 采用源视频流绝对时间戳,不做任何改变
|
||||||
|
kModifyStampSystem = 1, // 采用zlmediakit接收数据时的系统时间戳(有平滑处理)
|
||||||
|
kModifyStampRelative = 2 // 采用源视频流时间戳相对时间戳(增长量),有做时间戳跳跃和回退矫正
|
||||||
|
};
|
||||||
|
// 时间戳类型
|
||||||
|
int modify_stamp;
|
||||||
|
|
||||||
//转协议是否开启音频
|
//转协议是否开启音频
|
||||||
bool enable_audio;
|
bool enable_audio;
|
||||||
//添加静音音频,在关闭音频时,此开关无效
|
//添加静音音频,在关闭音频时,此开关无效
|
||||||
|
@ -94,6 +94,11 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const MediaTuple& tuple, float dur_
|
|||||||
_poller = EventPollerPool::Instance().getPoller();
|
_poller = EventPollerPool::Instance().getPoller();
|
||||||
_create_in_poller = _poller->isCurrentThread();
|
_create_in_poller = _poller->isCurrentThread();
|
||||||
_option = option;
|
_option = option;
|
||||||
|
if (dur_sec > 0.01) {
|
||||||
|
// 点播
|
||||||
|
_stamp[TrackVideo].setPlayBack();
|
||||||
|
_stamp[TrackAudio].setPlayBack();
|
||||||
|
}
|
||||||
|
|
||||||
if (option.enable_rtmp) {
|
if (option.enable_rtmp) {
|
||||||
_rtmp = std::make_shared<RtmpMediaSourceMuxer>(_tuple, option, std::make_shared<TitleMeta>(dur_sec));
|
_rtmp = std::make_shared<RtmpMediaSourceMuxer>(_tuple, option, std::make_shared<TitleMeta>(dur_sec));
|
||||||
@ -378,6 +383,11 @@ void MultiMediaSourceMuxer::onAllTrackReady() {
|
|||||||
createGopCacheIfNeed();
|
createGopCacheIfNeed();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
auto tracks = getTracks(false);
|
||||||
|
if (tracks.size() >= 2) {
|
||||||
|
// 音频时间戳同步于视频,因为音频时间戳被修改后不影响播放
|
||||||
|
_stamp[TrackAudio].syncTo(_stamp[TrackVideo]);
|
||||||
|
}
|
||||||
InfoL << "stream: " << shortUrl() << " , codec info: " << getTrackInfoStr(this);
|
InfoL << "stream: " << shortUrl() << " , codec info: " << getTrackInfoStr(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -429,9 +439,9 @@ void MultiMediaSourceMuxer::resetTracks() {
|
|||||||
|
|
||||||
bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) {
|
bool MultiMediaSourceMuxer::onTrackFrame(const Frame::Ptr &frame_in) {
|
||||||
auto frame = frame_in;
|
auto frame = frame_in;
|
||||||
if (_option.modify_stamp) {
|
if (_option.modify_stamp != ProtocolOption::kModifyStampOff) {
|
||||||
//开启了时间戳覆盖
|
// 时间戳不采用原始的绝对时间戳
|
||||||
frame = std::make_shared<FrameStamp>(frame, _stamp[frame->getTrackType()],true);
|
frame = std::make_shared<FrameStamp>(frame, _stamp[frame->getTrackType()], _option.modify_stamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
#include "Stamp.h"
|
#include "Stamp.h"
|
||||||
|
|
||||||
//时间戳最大允许跳变3秒,主要是防止网络抖动导致的跳变
|
// 时间戳最大允许跳变3秒,主要是防止网络抖动导致的跳变
|
||||||
#define MAX_DELTA_STAMP (3 * 1000)
|
#define MAX_DELTA_STAMP (3 * 1000)
|
||||||
#define STAMP_LOOP_DELTA (60 * 1000)
|
#define STAMP_LOOP_DELTA (60 * 1000)
|
||||||
#define MAX_CTS 500
|
#define MAX_CTS 500
|
||||||
@ -25,51 +25,51 @@ int64_t DeltaStamp::relativeStamp(int64_t stamp) {
|
|||||||
return _relative_stamp;
|
return _relative_stamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t DeltaStamp::relativeStamp(){
|
int64_t DeltaStamp::relativeStamp() {
|
||||||
return _relative_stamp;
|
return _relative_stamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t DeltaStamp::deltaStamp(int64_t stamp) {
|
int64_t DeltaStamp::deltaStamp(int64_t stamp) {
|
||||||
if(!_last_stamp){
|
if (!_last_stamp) {
|
||||||
//第一次计算时间戳增量,时间戳增量为0
|
// 第一次计算时间戳增量,时间戳增量为0
|
||||||
if(stamp){
|
if (stamp) {
|
||||||
_last_stamp = stamp;
|
_last_stamp = stamp;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t ret = stamp - _last_stamp;
|
int64_t ret = stamp - _last_stamp;
|
||||||
if(ret >= 0){
|
if (ret >= 0) {
|
||||||
//时间戳增量为正,返回之
|
// 时间戳增量为正,返回之
|
||||||
_last_stamp = stamp;
|
_last_stamp = stamp;
|
||||||
//在直播情况下,时间戳增量不得大于MAX_DELTA_STAMP
|
// 在直播情况下,时间戳增量不得大于MAX_DELTA_STAMP,否则强制相对时间戳加1
|
||||||
return ret < MAX_DELTA_STAMP ? ret : 0;
|
return ret < MAX_DELTA_STAMP ? ret : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//时间戳增量为负,说明时间戳回环了或回退了
|
// 时间戳增量为负,说明时间戳回环了或回退了
|
||||||
_last_stamp = stamp;
|
_last_stamp = stamp;
|
||||||
//如果时间戳回退不多,那么返回负值
|
// 时间戳回退了,强制相对时间戳加1
|
||||||
return -ret < MAX_CTS ? ret : 0;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Stamp::setPlayBack(bool playback) {
|
void Stamp::setPlayBack(bool playback) {
|
||||||
_playback = playback;
|
_playback = playback;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Stamp::syncTo(Stamp &other){
|
void Stamp::syncTo(Stamp &other) {
|
||||||
_sync_master = &other;
|
_sync_master = &other;
|
||||||
}
|
}
|
||||||
|
|
||||||
//限制dts回退
|
// 限制dts回退
|
||||||
void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) {
|
void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) {
|
||||||
revise_l(dts, pts, dts_out, pts_out, modifyStamp);
|
revise_l(dts, pts, dts_out, pts_out, modifyStamp);
|
||||||
if (_playback) {
|
if (_playback) {
|
||||||
//回放允许时间戳回退
|
// 回放允许时间戳回退
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dts_out < _last_dts_out) {
|
if (dts_out < _last_dts_out) {
|
||||||
// WarnL << "dts回退:" << dts_out << " < " << _last_dts_out;
|
// WarnL << "dts回退:" << dts_out << " < " << _last_dts_out;
|
||||||
dts_out = _last_dts_out;
|
dts_out = _last_dts_out;
|
||||||
pts_out = _last_pts_out;
|
pts_out = _last_pts_out;
|
||||||
return;
|
return;
|
||||||
@ -78,35 +78,35 @@ void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,
|
|||||||
_last_pts_out = pts_out;
|
_last_pts_out = pts_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
//音视频时间戳同步
|
// 音视频时间戳同步
|
||||||
void Stamp::revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) {
|
void Stamp::revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) {
|
||||||
revise_l2(dts, pts, dts_out, pts_out, modifyStamp);
|
revise_l2(dts, pts, dts_out, pts_out, modifyStamp);
|
||||||
if (!_sync_master || modifyStamp || _playback) {
|
if (!_sync_master || modifyStamp || _playback) {
|
||||||
//自动生成时间戳或回放或同步完毕
|
// 自动生成时间戳或回放或同步完毕
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_sync_master && _sync_master->_last_dts_in) {
|
if (_sync_master && _sync_master->_last_dts_in) {
|
||||||
//音视频dts当前时间差
|
// 音视频dts当前时间差
|
||||||
int64_t dts_diff = _last_dts_in - _sync_master->_last_dts_in;
|
int64_t dts_diff = _last_dts_in - _sync_master->_last_dts_in;
|
||||||
if (ABS(dts_diff) < 5000) {
|
if (ABS(dts_diff) < 5000) {
|
||||||
//如果绝对时间戳小于5秒,那么说明他们的起始时间戳是一致的,那么强制同步
|
// 如果绝对时间戳小于5秒,那么说明他们的起始时间戳是一致的,那么强制同步
|
||||||
_relative_stamp = _sync_master->_relative_stamp + dts_diff;
|
_relative_stamp = _sync_master->_relative_stamp + dts_diff;
|
||||||
}
|
}
|
||||||
//下次不用再强制同步
|
// 下次不用再强制同步
|
||||||
_sync_master = nullptr;
|
_sync_master = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//求取相对时间戳
|
// 求取相对时间戳
|
||||||
void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) {
|
void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out, bool modifyStamp) {
|
||||||
if (!pts) {
|
if (!pts) {
|
||||||
//没有播放时间戳,使其赋值为解码时间戳
|
// 没有播放时间戳,使其赋值为解码时间戳
|
||||||
pts = dts;
|
pts = dts;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_playback) {
|
if (_playback) {
|
||||||
//这是点播
|
// 这是点播
|
||||||
dts_out = dts;
|
dts_out = dts;
|
||||||
pts_out = pts;
|
pts_out = pts;
|
||||||
_relative_stamp = dts_out;
|
_relative_stamp = dts_out;
|
||||||
@ -114,13 +114,13 @@ void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_o
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//pts和dts的差值
|
// pts和dts的差值
|
||||||
int64_t pts_dts_diff = pts - dts;
|
int64_t pts_dts_diff = pts - dts;
|
||||||
|
|
||||||
if (_last_dts_in != dts) {
|
if (_last_dts_in != dts) {
|
||||||
//时间戳发生变更
|
// 时间戳发生变更
|
||||||
if (modifyStamp) {
|
if (modifyStamp) {
|
||||||
//内部自己生产时间戳
|
// 内部自己生产时间戳
|
||||||
_relative_stamp = _ticker.elapsedTime();
|
_relative_stamp = _ticker.elapsedTime();
|
||||||
} else {
|
} else {
|
||||||
_relative_stamp += deltaStamp(dts);
|
_relative_stamp += deltaStamp(dts);
|
||||||
@ -131,7 +131,7 @@ void Stamp::revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_o
|
|||||||
|
|
||||||
//////////////以下是播放时间戳的计算//////////////////
|
//////////////以下是播放时间戳的计算//////////////////
|
||||||
if (ABS(pts_dts_diff) > MAX_CTS) {
|
if (ABS(pts_dts_diff) > MAX_CTS) {
|
||||||
//如果差值太大,则认为由于回环导致时间戳错乱了
|
// 如果差值太大,则认为由于回环导致时间戳错乱了
|
||||||
pts_dts_diff = 0;
|
pts_dts_diff = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,86 +146,86 @@ int64_t Stamp::getRelativeStamp() const {
|
|||||||
return _relative_stamp;
|
return _relative_stamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DtsGenerator::getDts(uint64_t pts, uint64_t &dts){
|
bool DtsGenerator::getDts(uint64_t pts, uint64_t &dts) {
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
if (pts == _last_pts) {
|
if (pts == _last_pts) {
|
||||||
//pts未变,说明dts也不会变,返回上次dts
|
// pts未变,说明dts也不会变,返回上次dts
|
||||||
if (_last_dts) {
|
if (_last_dts) {
|
||||||
dts = _last_dts;
|
dts = _last_dts;
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//pts变了,尝试计算dts
|
// pts变了,尝试计算dts
|
||||||
ret = getDts_l(pts, dts);
|
ret = getDts_l(pts, dts);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
//获取到了dts,保存本次结果
|
// 获取到了dts,保存本次结果
|
||||||
_last_dts = dts;
|
_last_dts = dts;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
//pts排序列队长度还不知道,也就是不知道有没有B帧,
|
// pts排序列队长度还不知道,也就是不知道有没有B帧,
|
||||||
//那么先强制dts == pts,这样可能导致有B帧的情况下,起始画面有几帧回退
|
// 那么先强制dts == pts,这样可能导致有B帧的情况下,起始画面有几帧回退
|
||||||
dts = pts;
|
dts = pts;
|
||||||
}
|
}
|
||||||
|
|
||||||
//记录上次pts
|
// 记录上次pts
|
||||||
_last_pts = pts;
|
_last_pts = pts;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
//该算法核心思想是对pts进行排序,排序好的pts就是dts。
|
// 该算法核心思想是对pts进行排序,排序好的pts就是dts。
|
||||||
//排序有一定的滞后性,那么需要加上排序导致的时间戳偏移量
|
// 排序有一定的滞后性,那么需要加上排序导致的时间戳偏移量
|
||||||
bool DtsGenerator::getDts_l(uint64_t pts, uint64_t &dts){
|
bool DtsGenerator::getDts_l(uint64_t pts, uint64_t &dts) {
|
||||||
if(_sorter_max_size == 1){
|
if (_sorter_max_size == 1) {
|
||||||
//没有B帧,dts就等于pts
|
// 没有B帧,dts就等于pts
|
||||||
dts = pts;
|
dts = pts;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!_sorter_max_size){
|
if (!_sorter_max_size) {
|
||||||
//尚未计算出pts排序列队长度(也就是P帧间B帧个数)
|
// 尚未计算出pts排序列队长度(也就是P帧间B帧个数)
|
||||||
if(pts > _last_max_pts){
|
if (pts > _last_max_pts) {
|
||||||
//pts时间戳增加了,那么说明这帧画面不是B帧(说明是P帧或关键帧)
|
// pts时间戳增加了,那么说明这帧画面不是B帧(说明是P帧或关键帧)
|
||||||
if(_frames_since_last_max_pts && _count_sorter_max_size++ > 0){
|
if (_frames_since_last_max_pts && _count_sorter_max_size++ > 0) {
|
||||||
//已经出现多次非B帧的情况,那么我们就能知道P帧间B帧的个数
|
// 已经出现多次非B帧的情况,那么我们就能知道P帧间B帧的个数
|
||||||
_sorter_max_size = _frames_since_last_max_pts;
|
_sorter_max_size = _frames_since_last_max_pts;
|
||||||
//我们记录P帧间时间间隔(也就是多个B帧时间戳增量累计)
|
// 我们记录P帧间时间间隔(也就是多个B帧时间戳增量累计)
|
||||||
_dts_pts_offset = (pts - _last_max_pts);
|
_dts_pts_offset = (pts - _last_max_pts);
|
||||||
//除以2,防止dts大于pts
|
// 除以2,防止dts大于pts
|
||||||
_dts_pts_offset /= 2;
|
_dts_pts_offset /= 2;
|
||||||
}
|
}
|
||||||
//遇到P帧或关键帧,连续B帧计数清零
|
// 遇到P帧或关键帧,连续B帧计数清零
|
||||||
_frames_since_last_max_pts = 0;
|
_frames_since_last_max_pts = 0;
|
||||||
//记录上次非B帧的pts时间戳(同时也是dts),用于统计连续B帧时间戳增量
|
// 记录上次非B帧的pts时间戳(同时也是dts),用于统计连续B帧时间戳增量
|
||||||
_last_max_pts = pts;
|
_last_max_pts = pts;
|
||||||
}
|
}
|
||||||
//如果pts时间戳小于上一个P帧,那么断定这个是B帧,我们记录B帧连续个数
|
// 如果pts时间戳小于上一个P帧,那么断定这个是B帧,我们记录B帧连续个数
|
||||||
++_frames_since_last_max_pts;
|
++_frames_since_last_max_pts;
|
||||||
}
|
}
|
||||||
|
|
||||||
//pts放入排序缓存列队,缓存列队最大等于连续B帧个数
|
// pts放入排序缓存列队,缓存列队最大等于连续B帧个数
|
||||||
_pts_sorter.emplace(pts);
|
_pts_sorter.emplace(pts);
|
||||||
|
|
||||||
if(_sorter_max_size && _pts_sorter.size() > _sorter_max_size){
|
if (_sorter_max_size && _pts_sorter.size() > _sorter_max_size) {
|
||||||
//如果启用了pts排序(意味着存在B帧),并且pts排序缓存列队长度大于连续B帧个数,
|
// 如果启用了pts排序(意味着存在B帧),并且pts排序缓存列队长度大于连续B帧个数,
|
||||||
//意味着后续的pts都会比最早的pts大,那么说明可以取出最早的pts了,这个pts将当做该帧的dts基准
|
// 意味着后续的pts都会比最早的pts大,那么说明可以取出最早的pts了,这个pts将当做该帧的dts基准
|
||||||
auto it = _pts_sorter.begin();
|
auto it = _pts_sorter.begin();
|
||||||
|
|
||||||
//由于该pts是前面偏移了个_sorter_max_size帧的pts(也就是那帧画面的dts),
|
// 由于该pts是前面偏移了个_sorter_max_size帧的pts(也就是那帧画面的dts),
|
||||||
//那么我们加上时间戳偏移量,基本等于该帧的dts
|
// 那么我们加上时间戳偏移量,基本等于该帧的dts
|
||||||
dts = *it + _dts_pts_offset;
|
dts = *it + _dts_pts_offset;
|
||||||
if(dts > pts){
|
if (dts > pts) {
|
||||||
//dts不能大于pts(基本不可能到达这个逻辑)
|
// dts不能大于pts(基本不可能到达这个逻辑)
|
||||||
dts = pts;
|
dts = pts;
|
||||||
}
|
}
|
||||||
|
|
||||||
//pts排序缓存出列
|
// pts排序缓存出列
|
||||||
_pts_sorter.erase(it);
|
_pts_sorter.erase(it);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//排序缓存尚未满
|
// 排序缓存尚未满
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,7 +235,7 @@ void NtpStamp::setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
|
|||||||
|
|
||||||
void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
|
void NtpStamp::update(uint32_t rtp_stamp, uint64_t ntp_stamp_ms) {
|
||||||
if (ntp_stamp_ms == 0) {
|
if (ntp_stamp_ms == 0) {
|
||||||
//实测发现有些rtsp服务器发送的rtp时间戳和ntp时间戳一直为0
|
// 实测发现有些rtsp服务器发送的rtp时间戳和ntp时间戳一直为0
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_last_rtp_stamp = rtp_stamp;
|
_last_rtp_stamp = rtp_stamp;
|
||||||
@ -251,51 +251,51 @@ uint64_t NtpStamp::getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate) {
|
|||||||
|
|
||||||
uint64_t NtpStamp::getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate) {
|
uint64_t NtpStamp::getNtpStamp_l(uint32_t rtp_stamp, uint32_t sample_rate) {
|
||||||
if (!_last_ntp_stamp_ms) {
|
if (!_last_ntp_stamp_ms) {
|
||||||
//尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧
|
// 尚未收到sender report rtcp包,那么赋值为本地系统时间戳吧
|
||||||
update(rtp_stamp, getCurrentMillisecond(true));
|
update(rtp_stamp, getCurrentMillisecond(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
//rtp时间戳正增长
|
// rtp时间戳正增长
|
||||||
if (rtp_stamp >= _last_rtp_stamp) {
|
if (rtp_stamp >= _last_rtp_stamp) {
|
||||||
auto diff = static_cast<int>((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000.0f));
|
auto diff = static_cast<int>((rtp_stamp - _last_rtp_stamp) / (sample_rate / 1000.0f));
|
||||||
if (diff < MAX_DELTA_STAMP) {
|
if (diff < MAX_DELTA_STAMP) {
|
||||||
//时间戳正常增长
|
// 时间戳正常增长
|
||||||
update(rtp_stamp, _last_ntp_stamp_ms + diff);
|
update(rtp_stamp, _last_ntp_stamp_ms + diff);
|
||||||
return _last_ntp_stamp_ms;
|
return _last_ntp_stamp_ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
//时间戳大幅跳跃
|
// 时间戳大幅跳跃
|
||||||
uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000;
|
uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000;
|
||||||
if (_last_rtp_stamp < loop_delta && rtp_stamp > UINT32_MAX - loop_delta) {
|
if (_last_rtp_stamp < loop_delta && rtp_stamp > UINT32_MAX - loop_delta) {
|
||||||
//应该是rtp时间戳溢出+乱序
|
// 应该是rtp时间戳溢出+乱序
|
||||||
uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate;
|
uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate;
|
||||||
return _last_ntp_stamp_ms + diff - max_rtp_ms;
|
return _last_ntp_stamp_ms + diff - max_rtp_ms;
|
||||||
}
|
}
|
||||||
//不明原因的时间戳大幅跳跃,直接返回上次值
|
// 不明原因的时间戳大幅跳跃,直接返回上次值
|
||||||
WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp;
|
WarnL << "rtp stamp abnormal increased:" << _last_rtp_stamp << " -> " << rtp_stamp;
|
||||||
update(rtp_stamp, _last_ntp_stamp_ms);
|
update(rtp_stamp, _last_ntp_stamp_ms);
|
||||||
return _last_ntp_stamp_ms;
|
return _last_ntp_stamp_ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
//rtp时间戳负增长
|
// rtp时间戳负增长
|
||||||
auto diff = static_cast<int>((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000.0f));
|
auto diff = static_cast<int>((_last_rtp_stamp - rtp_stamp) / (sample_rate / 1000.0f));
|
||||||
if (diff < MAX_DELTA_STAMP) {
|
if (diff < MAX_DELTA_STAMP) {
|
||||||
//正常范围的时间戳回退,说明收到rtp乱序了
|
// 正常范围的时间戳回退,说明收到rtp乱序了
|
||||||
return _last_ntp_stamp_ms - diff;
|
return _last_ntp_stamp_ms - diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
//时间戳大幅度回退
|
// 时间戳大幅度回退
|
||||||
uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000;
|
uint64_t loop_delta = STAMP_LOOP_DELTA * sample_rate / 1000;
|
||||||
if (rtp_stamp < loop_delta && _last_rtp_stamp > UINT32_MAX - loop_delta) {
|
if (rtp_stamp < loop_delta && _last_rtp_stamp > UINT32_MAX - loop_delta) {
|
||||||
//确定是时间戳溢出
|
// 确定是时间戳溢出
|
||||||
uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate;
|
uint64_t max_rtp_ms = uint64_t(UINT32_MAX) * 1000 / sample_rate;
|
||||||
update(rtp_stamp, _last_ntp_stamp_ms + (max_rtp_ms - diff));
|
update(rtp_stamp, _last_ntp_stamp_ms + (max_rtp_ms - diff));
|
||||||
return _last_ntp_stamp_ms;
|
return _last_ntp_stamp_ms;
|
||||||
}
|
}
|
||||||
//不明原因的时间戳回退,直接返回上次值
|
// 不明原因的时间戳回退,直接返回上次值
|
||||||
WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp;
|
WarnL << "rtp stamp abnormal reduced:" << _last_rtp_stamp << " -> " << rtp_stamp;
|
||||||
update(rtp_stamp, _last_ntp_stamp_ms);
|
update(rtp_stamp, _last_ntp_stamp_ms);
|
||||||
return _last_ntp_stamp_ms;
|
return _last_ntp_stamp_ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
}//namespace mediakit
|
} // namespace mediakit
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Common/config.h"
|
#include "Common/config.h"
|
||||||
|
#include "MediaSource.h"
|
||||||
#include "Util/NoticeCenter.h"
|
#include "Util/NoticeCenter.h"
|
||||||
#include "Util/logger.h"
|
#include "Util/logger.h"
|
||||||
#include "Util/onceToken.h"
|
#include "Util/onceToken.h"
|
||||||
@ -120,7 +121,7 @@ const string kTSDemand = PROTOCOL_FIELD "ts_demand";
|
|||||||
const string kFMP4Demand = PROTOCOL_FIELD "fmp4_demand";
|
const string kFMP4Demand = PROTOCOL_FIELD "fmp4_demand";
|
||||||
|
|
||||||
static onceToken token([]() {
|
static onceToken token([]() {
|
||||||
mINI::Instance()[kModifyStamp] = 0;
|
mINI::Instance()[kModifyStamp] = (int)ProtocolOption::kModifyStampRelative;
|
||||||
mINI::Instance()[kEnableAudio] = 1;
|
mINI::Instance()[kEnableAudio] = 1;
|
||||||
mINI::Instance()[kAddMuteAudio] = 1;
|
mINI::Instance()[kAddMuteAudio] = 1;
|
||||||
mINI::Instance()[kContinuePushMS] = 15000;
|
mINI::Instance()[kContinuePushMS] = 15000;
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
#include "H265.h"
|
#include "H265.h"
|
||||||
#include "Common/Parser.h"
|
#include "Common/Parser.h"
|
||||||
#include "Common/Stamp.h"
|
#include "Common/Stamp.h"
|
||||||
|
#include "Common/MediaSource.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace toolkit;
|
using namespace toolkit;
|
||||||
@ -31,11 +32,11 @@ Frame::Ptr Frame::getCacheAbleFrame(const Frame::Ptr &frame){
|
|||||||
return std::make_shared<FrameCacheAble>(frame);
|
return std::make_shared<FrameCacheAble>(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
FrameStamp::FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp)
|
FrameStamp::FrameStamp(Frame::Ptr frame, Stamp &stamp, int modify_stamp)
|
||||||
{
|
{
|
||||||
_frame = std::move(frame);
|
_frame = std::move(frame);
|
||||||
//覆盖时间戳
|
// kModifyStampSystem时采用系统时间戳,kModifyStampRelative采用相对时间戳
|
||||||
stamp.revise(_frame->dts(), _frame->pts(), _dts, _pts, modify_stamp);
|
stamp.revise(_frame->dts(), _frame->pts(), _dts, _pts, modify_stamp == ProtocolOption::kModifyStampSystem);
|
||||||
}
|
}
|
||||||
|
|
||||||
TrackType getTrackType(CodecId codecId) {
|
TrackType getTrackType(CodecId codecId) {
|
||||||
|
@ -492,7 +492,7 @@ private:
|
|||||||
class FrameStamp : public Frame {
|
class FrameStamp : public Frame {
|
||||||
public:
|
public:
|
||||||
using Ptr = std::shared_ptr<FrameStamp>;
|
using Ptr = std::shared_ptr<FrameStamp>;
|
||||||
FrameStamp(Frame::Ptr frame, Stamp &stamp, bool modify_stamp);
|
FrameStamp(Frame::Ptr frame, Stamp &stamp, int modify_stamp);
|
||||||
~FrameStamp() override {}
|
~FrameStamp() override {}
|
||||||
|
|
||||||
uint64_t dts() const override { return (uint64_t)_dts; }
|
uint64_t dts() const override { return (uint64_t)_dts; }
|
||||||
|
Loading…
Reference in New Issue
Block a user