ZLMediaKit/src/Common/Stamp.cpp

216 lines
6.9 KiB
C++
Raw Normal View History

2019-08-08 19:01:45 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include "Stamp.h"
2020-01-13 15:48:55 +08:00
#define MAX_DELTA_STAMP 1000
#define MAX_CTS 500
#define ABS(x) ((x) > 0 ? (x) : (-x))
namespace mediakit {
2019-10-08 12:55:19 +08:00
int64_t DeltaStamp::deltaStamp(int64_t stamp) {
if(!_last_stamp){
//第一次计算时间戳增量,时间戳增量为0
if(stamp){
_last_stamp = stamp;
}
2019-10-08 12:55:19 +08:00
return 0;
2019-08-22 14:56:58 +08:00
}
2019-10-08 12:55:19 +08:00
int64_t ret = stamp - _last_stamp;
if(ret >= 0){
//时间戳增量为正,返回之
2019-10-08 12:55:19 +08:00
_last_stamp = stamp;
//在直播情况下时间戳增量不得大于MAX_DELTA_STAMP
2020-04-04 15:37:37 +08:00
return ret < MAX_DELTA_STAMP ? ret : 0;
}
//时间戳增量为负,说明时间戳回环了或回退了
2019-10-08 12:55:19 +08:00
_last_stamp = stamp;
2020-04-04 15:37:37 +08:00
return 0;
2019-10-08 12:55:19 +08:00
}
2020-04-04 15:37:37 +08:00
void Stamp::setPlayBack(bool playback) {
2019-10-08 12:55:19 +08:00
_playback = playback;
}
2020-05-15 18:08:54 +08:00
void Stamp::syncTo(Stamp &other){
_sync_master = &other;
2020-05-26 10:30:39 +08:00
_sync_finished = false;
}
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);
2020-05-26 10:30:39 +08:00
if(_sync_finished || modifyStamp || _playback){
//自动生成时间戳或回放或同步完毕
if(dts_out < 0) { dts_out = 0; }
if(pts_out < 0) { pts_out = 0; }
return;
}
2020-05-15 18:08:54 +08:00
if(_sync_master && _sync_master->_last_dts){
//音视频dts当前时间差
2020-05-15 18:08:54 +08:00
int64_t dts_diff = _last_dts - _sync_master->_last_dts;
if(ABS(dts_diff) < 5000){
//如果绝对时间戳小于5秒那么说明他们的起始时间戳是一致的那么强制同步
_last_relativeStamp = _relativeStamp;
2020-05-15 18:08:54 +08:00
_relativeStamp = _sync_master->_relativeStamp + dts_diff;
}
//下次不用再强制同步
2020-05-15 18:08:54 +08:00
_sync_master = nullptr;
}
2020-05-26 10:30:39 +08:00
if (dts_out < 0 || dts_out < _last_relativeStamp) {
2020-05-15 18:08:54 +08:00
//相对时间戳小于0或者小于上次的时间戳
//那么说明是同步时间戳导致的,在这个过渡期内,我们一直返回上次的结果(目的是为了防止时间戳回退)
pts_out = _last_relativeStamp + (pts_out - dts_out);
dts_out = _last_relativeStamp;
2020-05-26 10:30:39 +08:00
} else if(!_sync_master){
//音视频同步过渡期完毕
_sync_finished = true;
}
if(pts_out < 0){
pts_out = dts_out;
}
}
void Stamp::revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) {
if(!pts){
//没有播放时间戳,使其赋值为解码时间戳
2019-10-08 12:55:19 +08:00
pts = dts;
}
2020-04-04 15:37:37 +08:00
if(_playback){
//这是点播
dts_out = dts;
pts_out = pts;
_relativeStamp = dts_out;
_last_dts = dts;
2020-04-04 15:37:37 +08:00
return;
}
2019-10-08 12:55:19 +08:00
//pts和dts的差值
int pts_dts_diff = pts - dts;
2019-09-30 16:51:17 +08:00
2019-11-29 09:54:48 +08:00
if(_last_dts != dts){
//时间戳发生变更
2019-12-16 15:49:52 +08:00
if(modifyStamp){
//内部自己生产时间戳
2019-12-16 15:49:52 +08:00
_relativeStamp = _ticker.elapsedTime();
}else{
_relativeStamp += deltaStamp(dts);
}
2019-11-29 09:54:48 +08:00
_last_dts = dts;
}
2019-10-08 12:55:19 +08:00
dts_out = _relativeStamp;
//////////////以下是播放时间戳的计算//////////////////
2020-01-13 15:48:55 +08:00
if(ABS(pts_dts_diff) > MAX_CTS){
//如果差值太大,则认为由于回环导致时间戳错乱了
pts_dts_diff = 0;
}
2019-10-08 12:55:19 +08:00
pts_out = dts_out + pts_dts_diff;
}
2019-10-08 12:55:19 +08:00
void Stamp::setRelativeStamp(int64_t relativeStamp) {
_relativeStamp = relativeStamp;
}
int64_t Stamp::getRelativeStamp() const {
return _relativeStamp;
}
bool DtsGenerator::getDts(uint32_t pts, uint32_t &dts){
bool ret = false;
2020-04-29 18:19:51 +08:00
if (pts == _last_pts) {
2020-04-29 18:05:29 +08:00
//pts未变说明dts也不会变返回上次dts
2020-04-29 18:19:51 +08:00
if (_last_dts) {
dts = _last_dts;
ret = true;
}
2020-04-29 18:19:51 +08:00
} else {
//pts变了尝试计算dts
ret = getDts_l(pts, dts);
if (ret) {
//获取到了dts保存本次结果
_last_dts = dts;
}
}
2020-04-29 18:19:51 +08:00
if (!ret) {
2020-04-29 18:05:29 +08:00
//pts排序列队长度还不知道也就是不知道有没有B帧
//那么先强制dts == pts这样可能导致有B帧的情况下起始画面有几帧回退
dts = pts;
}
2020-04-29 18:19:51 +08:00
//记录上次pts
_last_pts = pts;
return ret;
}
2020-04-29 18:05:29 +08:00
//该算法核心思想是对pts进行排序排序好的pts就是dts。
//排序有一定的滞后性,那么需要加上排序导致的时间戳偏移量
bool DtsGenerator::getDts_l(uint32_t pts, uint32_t &dts){
2020-01-14 10:06:35 +08:00
if(_sorter_max_size == 1){
2020-04-29 18:05:29 +08:00
//没有B帧dts就等于pts
2020-01-14 10:06:35 +08:00
dts = pts;
return true;
}
2020-01-14 10:25:14 +08:00
if(!_sorter_max_size){
2020-04-29 18:05:29 +08:00
//尚未计算出pts排序列队长度(也就是P帧间B帧个数)
2020-01-14 10:25:14 +08:00
if(pts > _last_max_pts){
2020-04-29 18:05:29 +08:00
//pts时间戳增加了那么说明这帧画面不是B帧(说明是P帧或关键帧)
2020-01-14 10:25:14 +08:00
if(_frames_since_last_max_pts && _count_sorter_max_size++ > 0){
2020-04-29 18:05:29 +08:00
//已经出现多次非B帧的情况那么我们就能知道P帧间B帧的个数
2020-01-14 10:25:14 +08:00
_sorter_max_size = _frames_since_last_max_pts;
2020-04-29 18:05:29 +08:00
//我们记录P帧间时间间隔(也就是多个B帧时间戳增量累计)
2020-06-11 23:06:01 +08:00
_dts_pts_offset = (pts - _last_max_pts);
//除以2防止dts大于pts
_dts_pts_offset /= 2;
2020-01-14 10:25:14 +08:00
}
2020-04-29 18:05:29 +08:00
//遇到P帧或关键帧连续B帧计数清零
2020-01-14 10:25:14 +08:00
_frames_since_last_max_pts = 0;
2020-04-29 18:05:29 +08:00
//记录上次非B帧的pts时间戳(同时也是dts)用于统计连续B帧时间戳增量
2020-01-14 10:25:14 +08:00
_last_max_pts = pts;
}
2020-04-29 18:05:29 +08:00
//如果pts时间戳小于上一个P帧那么断定这个是B帧,我们记录B帧连续个数
2020-01-14 10:25:14 +08:00
++_frames_since_last_max_pts;
}
2020-04-29 18:05:29 +08:00
//pts放入排序缓存列队缓存列队最大等于连续B帧个数
_pts_sorter.emplace(pts);
2020-04-29 18:05:29 +08:00
if(_sorter_max_size && _pts_sorter.size() > _sorter_max_size){
2020-04-29 18:05:29 +08:00
//如果启用了pts排序(意味着存在B帧)并且pts排序缓存列队长度大于连续B帧个数
//意味着后续的pts都会比最早的pts大那么说明可以取出最早的pts了这个pts将当做该帧的dts基准
auto it = _pts_sorter.begin();
2020-04-29 18:05:29 +08:00
//由于该pts是前面偏移了个_sorter_max_size帧的pts(也就是那帧画面的dts),
//那么我们加上时间戳偏移量基本等于该帧的dts
dts = *it + _dts_pts_offset;
if(dts > pts){
//dts不能大于pts(基本不可能到达这个逻辑)
dts = pts;
}
2020-04-29 18:05:29 +08:00
//pts排序缓存出列
_pts_sorter.erase(it);
return true;
}
2020-04-29 18:05:29 +08:00
//排序缓存尚未满
return false;
}
}//namespace mediakit