Stamp.cpp 6.87 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
9 10 11 12
 */

#include "Stamp.h"

13 14
#define MAX_DELTA_STAMP 1000
#define MAX_CTS 500
15
#define ABS(x) ((x) > 0 ? (x) : (-x))
16

17 18
namespace mediakit {

xiongziliang committed
19 20 21
int64_t DeltaStamp::deltaStamp(int64_t stamp) {
    if(!_last_stamp){
        //第一次计算时间戳增量,时间戳增量为0
22 23 24
        if(stamp){
            _last_stamp = stamp;
        }
xiongziliang committed
25
        return 0;
26 27
    }

xiongziliang committed
28
    int64_t ret = stamp - _last_stamp;
29 30
    if(ret >= 0){
        //时间戳增量为正,返回之
xiongziliang committed
31
        _last_stamp = stamp;
32
        //在直播情况下,时间戳增量不得大于MAX_DELTA_STAMP
xiongziliang committed
33
        return  ret < MAX_DELTA_STAMP ? ret : 0;
34 35
    }

36
    //时间戳增量为负,说明时间戳回环了或回退了
xiongziliang committed
37
    _last_stamp = stamp;
xiongziliang committed
38
    return 0;
xiongziliang committed
39 40
}

xiongziliang committed
41
void Stamp::setPlayBack(bool playback) {
xiongziliang committed
42 43 44
    _playback = playback;
}

xiongziliang committed
45 46
void Stamp::syncTo(Stamp &other){
    _sync_master = &other;
47
    _sync_finished = false;
48 49
}

50
void Stamp::revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) {
51
    revise_l(dts,pts,dts_out,pts_out,modifyStamp);
52 53 54 55
    if(_sync_finished || modifyStamp || _playback){
        //自动生成时间戳或回放或同步完毕
        if(dts_out < 0) { dts_out = 0; }
        if(pts_out < 0) { pts_out = 0; }
56 57 58
        return;
    }

xiongziliang committed
59
    if(_sync_master && _sync_master->_last_dts){
60
        //音视频dts当前时间差
xiongziliang committed
61
        int64_t dts_diff = _last_dts - _sync_master->_last_dts;
62 63 64
        if(ABS(dts_diff) < 5000){
            //如果绝对时间戳小于5秒,那么说明他们的起始时间戳是一致的,那么强制同步
            _last_relativeStamp = _relativeStamp;
xiongziliang committed
65
            _relativeStamp = _sync_master->_relativeStamp + dts_diff;
66 67
        }
        //下次不用再强制同步
xiongziliang committed
68
        _sync_master = nullptr;
69 70
    }

71
    if (dts_out < 0 || dts_out < _last_relativeStamp) {
xiongziliang committed
72 73
        //相对时间戳小于0,或者小于上次的时间戳,
        //那么说明是同步时间戳导致的,在这个过渡期内,我们一直返回上次的结果(目的是为了防止时间戳回退)
74 75
        pts_out = _last_relativeStamp + (pts_out - dts_out);
        dts_out = _last_relativeStamp;
76 77 78 79 80 81 82
    } else if(!_sync_master){
        //音视频同步过渡期完毕
        _sync_finished = true;
    }

    if(pts_out < 0){
        pts_out = dts_out;
83 84 85 86
    }
}

void Stamp::revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp) {
87 88
    if(!pts){
        //没有播放时间戳,使其赋值为解码时间戳
xiongziliang committed
89
        pts = dts;
90 91
    }

xiongziliang committed
92 93 94 95 96
    if(_playback){
        //这是点播
        dts_out = dts;
        pts_out = pts;
        _relativeStamp = dts_out;
97
        _last_dts = dts;
xiongziliang committed
98 99 100
        return;
    }

xiongziliang committed
101 102
    //pts和dts的差值
    int pts_dts_diff = pts - dts;
xiongziliang committed
103

104 105
    if(_last_dts != dts){
        //时间戳发生变更
106
        if(modifyStamp){
107
            //内部自己生产时间戳
108 109 110 111
            _relativeStamp = _ticker.elapsedTime();
        }else{
            _relativeStamp += deltaStamp(dts);
        }
112 113
        _last_dts = dts;
    }
xiongziliang committed
114
    dts_out = _relativeStamp;
115 116

    //////////////以下是播放时间戳的计算//////////////////
117
    if(ABS(pts_dts_diff) > MAX_CTS){
118
        //如果差值太大,则认为由于回环导致时间戳错乱了
119 120
        pts_dts_diff = 0;
    }
xiongziliang committed
121

122 123 124
    pts_out = dts_out + pts_dts_diff;
}

xiongziliang committed
125 126 127 128 129 130
void Stamp::setRelativeStamp(int64_t relativeStamp) {
    _relativeStamp = relativeStamp;
}

int64_t Stamp::getRelativeStamp() const {
    return _relativeStamp;
131 132
}

133 134
bool DtsGenerator::getDts(uint32_t pts, uint32_t &dts){
    bool ret = false;
xiongziliang committed
135
    if (pts == _last_pts) {
xiongziliang committed
136
        //pts未变,说明dts也不会变,返回上次dts
xiongziliang committed
137
        if (_last_dts) {
138 139 140
            dts = _last_dts;
            ret = true;
        }
xiongziliang committed
141 142 143 144 145 146 147
    } else {
        //pts变了,尝试计算dts
        ret = getDts_l(pts, dts);
        if (ret) {
            //获取到了dts,保存本次结果
            _last_dts = dts;
        }
148 149
    }

xiongziliang committed
150
    if (!ret) {
xiongziliang committed
151 152 153
        //pts排序列队长度还不知道,也就是不知道有没有B帧,
        //那么先强制dts == pts,这样可能导致有B帧的情况下,起始画面有几帧回退
        dts = pts;
154
    }
xiongziliang committed
155

156 157 158 159 160
    //记录上次pts
    _last_pts = pts;
    return ret;
}

xiongziliang committed
161 162
//该算法核心思想是对pts进行排序,排序好的pts就是dts。
//排序有一定的滞后性,那么需要加上排序导致的时间戳偏移量
163
bool DtsGenerator::getDts_l(uint32_t pts, uint32_t &dts){
164
    if(_sorter_max_size == 1){
xiongziliang committed
165
        //没有B帧,dts就等于pts
166 167 168 169
        dts = pts;
        return true;
    }

xiongziliang committed
170
    if(!_sorter_max_size){
xiongziliang committed
171
        //尚未计算出pts排序列队长度(也就是P帧间B帧个数)
xiongziliang committed
172
        if(pts > _last_max_pts){
xiongziliang committed
173
            //pts时间戳增加了,那么说明这帧画面不是B帧(说明是P帧或关键帧)
xiongziliang committed
174
            if(_frames_since_last_max_pts && _count_sorter_max_size++ > 0){
xiongziliang committed
175
                //已经出现多次非B帧的情况,那么我们就能知道P帧间B帧的个数
xiongziliang committed
176
                _sorter_max_size = _frames_since_last_max_pts;
xiongziliang committed
177
                //我们记录P帧间时间间隔(也就是多个B帧时间戳增量累计)
xiongziliang committed
178
                _dts_pts_offset = (pts - _last_max_pts);
179 180
                //除以2,防止dts大于pts
                _dts_pts_offset /= 2;
xiongziliang committed
181
            }
xiongziliang committed
182
            //遇到P帧或关键帧,连续B帧计数清零
xiongziliang committed
183
            _frames_since_last_max_pts = 0;
xiongziliang committed
184
            //记录上次非B帧的pts时间戳(同时也是dts),用于统计连续B帧时间戳增量
xiongziliang committed
185
            _last_max_pts = pts;
186
        }
xiongziliang committed
187
        //如果pts时间戳小于上一个P帧,那么断定这个是B帧,我们记录B帧连续个数
xiongziliang committed
188
        ++_frames_since_last_max_pts;
189 190
    }

xiongziliang committed
191
    //pts放入排序缓存列队,缓存列队最大等于连续B帧个数
192
    _pts_sorter.emplace(pts);
xiongziliang committed
193

194
    if(_sorter_max_size && _pts_sorter.size() >  _sorter_max_size){
xiongziliang committed
195 196
        //如果启用了pts排序(意味着存在B帧),并且pts排序缓存列队长度大于连续B帧个数,
        //意味着后续的pts都会比最早的pts大,那么说明可以取出最早的pts了,这个pts将当做该帧的dts基准
197
        auto it = _pts_sorter.begin();
xiongziliang committed
198 199 200

        //由于该pts是前面偏移了个_sorter_max_size帧的pts(也就是那帧画面的dts),
        //那么我们加上时间戳偏移量,基本等于该帧的dts
201 202 203 204 205
        dts = *it + _dts_pts_offset;
        if(dts > pts){
            //dts不能大于pts(基本不可能到达这个逻辑)
            dts = pts;
        }
xiongziliang committed
206 207

        //pts排序缓存出列
208 209 210
        _pts_sorter.erase(it);
        return true;
    }
xiongziliang committed
211 212

    //排序缓存尚未满
213 214 215
    return false;
}

216
}//namespace mediakit