Stamp.h 3.14 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 13
 */

#ifndef ZLMEDIAKIT_STAMP_H
#define ZLMEDIAKIT_STAMP_H

14
#include <set>
15
#include <cstdint>
16
#include "Util/TimeTicker.h"
xiongziliang committed
17
using namespace toolkit;
18 19 20

namespace mediakit {

xiongziliang committed
21
class DeltaStamp{
22
public:
xiongziliang committed
23 24
    DeltaStamp() = default;
    ~DeltaStamp() = default;
25 26

    /**
xiongziliang committed
27 28 29 30 31
     * 计算时间戳增量
     * @param stamp 绝对时间戳
     * @return 时间戳增量
     */
    int64_t deltaStamp(int64_t stamp);
32

xiongziliang committed
33 34 35
private:
    int64_t _last_stamp = 0;
};
36

xiongziliang committed
37 38 39 40 41 42
//该类解决时间戳回环、回退问题
//计算相对时间戳或者产生平滑时间戳
class Stamp : public DeltaStamp{
public:
    Stamp() = default;
    ~Stamp() = default;
43 44

    /**
45
     * 求取相对时间戳,同时实现了音视频同步、限制dts回退等功能
46 47 48 49
     * @param dts 输入dts,如果为0则根据系统时间戳生成
     * @param pts 输入pts,如果为0则等于dts
     * @param dts_out 输出dts
     * @param pts_out 输出pts
50
     * @param modifyStamp 是否用系统时间戳覆盖
51
     */
52
    void revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp = false);
xiongziliang committed
53 54 55 56 57 58 59 60 61 62 63 64

    /**
     * 再设置相对时间戳,用于seek用
     * @param relativeStamp 相对时间戳
     */
    void setRelativeStamp(int64_t relativeStamp);

    /**
     * 获取当前相对时间戳
     * @return
     */
    int64_t getRelativeStamp() const ;
xiongziliang committed
65 66 67 68 69 70

    /**
     * 设置是否为回放模式,回放模式运行时间戳回退
     * @param playback 是否为回放模式
     */
    void setPlayBack(bool playback = true);
71 72

    /**
xiongziliang committed
73 74
     * 音视频同步用,音频应该同步于视频(只修改音频时间戳)
     * 因为音频时间戳修改后不影响播放速度
75
     */
xiongziliang committed
76
    void syncTo(Stamp &other);
77 78

private:
79
    //主要实现音视频时间戳同步功能
80
    void revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp = false);
81 82 83 84

    //主要实现获取相对时间戳功能
    void revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp = false);

85
private:
86 87 88 89
    int64_t _relative_stamp = 0;
    int64_t _last_dts_in = 0;
    int64_t _last_dts_out = 0;
    int64_t _last_pts_out = 0;
xiongziliang committed
90
    SmoothTicker _ticker;
xiongziliang committed
91
    bool _playback = false;
xiongziliang committed
92
    Stamp *_sync_master = nullptr;
93 94
};

95 96
//dts生成器,
//pts排序后就是dts
97 98 99 100 101
class DtsGenerator{
public:
    DtsGenerator() = default;
    ~DtsGenerator() = default;
    bool getDts(uint32_t pts, uint32_t &dts);
102

103 104
private:
    bool getDts_l(uint32_t pts, uint32_t &dts);
105

106 107 108 109 110 111 112 113 114 115 116
private:
    uint32_t _dts_pts_offset = 0;
    uint32_t _last_dts = 0;
    uint32_t _last_pts = 0;
    uint32_t _last_max_pts = 0;
    int _frames_since_last_max_pts = 0;
    int _sorter_max_size = 0;
    int _count_sorter_max_size = 0;
    set<uint32_t> _pts_sorter;
};

117 118 119
}//namespace mediakit

#endif //ZLMEDIAKIT_STAMP_H