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.
|
2019-08-02 18:06:37 +08:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
2019-08-02 18:06:37 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Stamp.h"
|
|
|
|
|
|
2020-01-13 15:48:55 +08:00
|
|
|
|
#define MAX_DELTA_STAMP 1000
|
|
|
|
|
#define MAX_CTS 500
|
2020-01-13 11:51:29 +08:00
|
|
|
|
#define ABS(x) ((x) > 0 ? (x) : (-x))
|
2019-12-16 16:17:52 +08:00
|
|
|
|
|
2019-08-02 18:06:37 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2019-10-08 12:55:19 +08:00
|
|
|
|
int64_t DeltaStamp::deltaStamp(int64_t stamp) {
|
|
|
|
|
if(!_last_stamp){
|
|
|
|
|
//第一次计算时间戳增量,时间戳增量为0
|
2020-01-13 11:51:29 +08:00
|
|
|
|
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;
|
2020-01-14 10:04:24 +08:00
|
|
|
|
if(ret >= 0){
|
|
|
|
|
//时间戳增量为正,返回之
|
2019-10-08 12:55:19 +08:00
|
|
|
|
_last_stamp = stamp;
|
2020-01-14 10:04:24 +08:00
|
|
|
|
//在直播情况下,时间戳增量不得大于MAX_DELTA_STAMP
|
2020-04-04 15:37:37 +08:00
|
|
|
|
return ret < MAX_DELTA_STAMP ? ret : 0;
|
2019-08-02 18:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 10:04:24 +08:00
|
|
|
|
//时间戳增量为负,说明时间戳回环了或回退了
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-12 22:44:16 +08:00
|
|
|
|
void Stamp::revise(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;
|
2019-08-02 18:06:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-04 15:37:37 +08:00
|
|
|
|
if(_playback){
|
|
|
|
|
//这是点播
|
|
|
|
|
dts_out = dts;
|
|
|
|
|
pts_out = pts;
|
|
|
|
|
_relativeStamp = dts_out;
|
|
|
|
|
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){
|
|
|
|
|
_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;
|
2019-08-02 18:06:37 +08:00
|
|
|
|
|
|
|
|
|
//////////////以下是播放时间戳的计算//////////////////
|
2020-01-13 15:48:55 +08:00
|
|
|
|
if(ABS(pts_dts_diff) > MAX_CTS){
|
2019-12-16 16:17:52 +08:00
|
|
|
|
//如果差值太大,则认为由于回环导致时间戳错乱了
|
2019-08-02 18:06:37 +08:00
|
|
|
|
pts_dts_diff = 0;
|
|
|
|
|
}
|
2019-10-08 12:55:19 +08:00
|
|
|
|
|
2019-08-02 18:06:37 +08:00
|
|
|
|
pts_out = dts_out + pts_dts_diff;
|
|
|
|
|
if(pts_out < 0){
|
|
|
|
|
//时间戳不能小于0
|
|
|
|
|
pts_out = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 12:55:19 +08:00
|
|
|
|
void Stamp::setRelativeStamp(int64_t relativeStamp) {
|
|
|
|
|
_relativeStamp = relativeStamp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int64_t Stamp::getRelativeStamp() const {
|
|
|
|
|
return _relativeStamp;
|
2019-09-03 17:20:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-08 12:55:19 +08:00
|
|
|
|
|
2020-01-14 10:04:24 +08:00
|
|
|
|
bool DtsGenerator::getDts(uint32_t pts, uint32_t &dts){
|
|
|
|
|
bool ret = false;
|
|
|
|
|
if(pts == _last_pts){
|
|
|
|
|
//pts未变,返回上次结果
|
|
|
|
|
if(_last_dts){
|
|
|
|
|
dts = _last_dts;
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = getDts_l(pts,dts);
|
|
|
|
|
if(ret){
|
|
|
|
|
//保存本次结果
|
|
|
|
|
_last_dts = dts;
|
|
|
|
|
}
|
|
|
|
|
//记录上次pts
|
|
|
|
|
_last_pts = pts;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DtsGenerator::getDts_l(uint32_t pts, uint32_t &dts){
|
2020-01-14 10:06:35 +08:00
|
|
|
|
if(_sorter_max_size == 1){
|
|
|
|
|
//没有B帧
|
|
|
|
|
dts = pts;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-14 10:25:14 +08:00
|
|
|
|
if(!_sorter_max_size){
|
|
|
|
|
if(pts > _last_max_pts){
|
|
|
|
|
if(_frames_since_last_max_pts && _count_sorter_max_size++ > 0){
|
|
|
|
|
_sorter_max_size = _frames_since_last_max_pts;
|
|
|
|
|
_dts_pts_offset = (pts - _last_max_pts) / 2;
|
|
|
|
|
}
|
|
|
|
|
_frames_since_last_max_pts = 0;
|
|
|
|
|
_last_max_pts = pts;
|
2020-01-14 10:04:24 +08:00
|
|
|
|
}
|
2020-01-14 10:25:14 +08:00
|
|
|
|
++_frames_since_last_max_pts;
|
2020-01-14 10:04:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pts_sorter.emplace(pts);
|
|
|
|
|
if(_sorter_max_size && _pts_sorter.size() > _sorter_max_size){
|
|
|
|
|
auto it = _pts_sorter.begin();
|
|
|
|
|
dts = *it + _dts_pts_offset;
|
|
|
|
|
if(dts > pts){
|
|
|
|
|
//dts不能大于pts(基本不可能到达这个逻辑)
|
|
|
|
|
dts = pts;
|
|
|
|
|
}
|
|
|
|
|
_pts_sorter.erase(it);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-02 18:06:37 +08:00
|
|
|
|
}//namespace mediakit
|