ZLMediaKit/src/Common/Stamp.h

139 lines
3.7 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/xia-chu/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.
*/
#ifndef ZLMEDIAKIT_STAMP_H
#define ZLMEDIAKIT_STAMP_H
#include <set>
#include <cstdint>
#include "Util/TimeTicker.h"
namespace mediakit {
2019-10-08 12:55:19 +08:00
class DeltaStamp{
public:
2019-10-08 12:55:19 +08:00
DeltaStamp() = default;
~DeltaStamp() = default;
/**
2019-10-08 12:55:19 +08:00
*
* @param stamp
* @return
*/
int64_t deltaStamp(int64_t stamp);
int64_t relativeStamp(int64_t stamp);
int64_t relativeStamp();
2019-10-08 12:55:19 +08:00
private:
int64_t _last_stamp = 0;
int64_t _relative_stamp = 0;
2019-10-08 12:55:19 +08:00
};
2019-10-08 12:55:19 +08:00
//该类解决时间戳回环、回退问题
//计算相对时间戳或者产生平滑时间戳
class Stamp : public DeltaStamp{
public:
Stamp() = default;
~Stamp() = default;
/**
* ,dts回退等功能
* @param dts dts0
* @param pts pts0dts
* @param dts_out dts
* @param pts_out pts
* @param modifyStamp
*/
void revise(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp = false);
2019-10-08 12:55:19 +08:00
/**
* seek用
* @param relativeStamp
*/
void setRelativeStamp(int64_t relativeStamp);
/**
*
* @return
*/
int64_t getRelativeStamp() const ;
2020-04-04 15:37:37 +08:00
/**
* 退
* @param playback
*/
void setPlayBack(bool playback = true);
/**
2020-05-15 18:08:54 +08:00
* ()
*
*/
2020-05-15 18:08:54 +08:00
void syncTo(Stamp &other);
private:
//主要实现音视频时间戳同步功能
void revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp = false);
//主要实现获取相对时间戳功能
void revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp = false);
private:
int64_t _relative_stamp = 0;
int64_t _last_dts_in = 0;
int64_t _last_dts_out = 0;
int64_t _last_pts_out = 0;
toolkit::SmoothTicker _ticker;
2020-04-04 15:37:37 +08:00
bool _playback = false;
2020-05-15 18:08:54 +08:00
Stamp *_sync_master = nullptr;
};
//dts生成器
//pts排序后就是dts
class DtsGenerator{
public:
DtsGenerator() = default;
~DtsGenerator() = default;
2022-08-08 17:13:39 +08:00
bool getDts(uint64_t pts, uint64_t &dts);
private:
2022-08-08 17:13:39 +08:00
bool getDts_l(uint64_t pts, uint64_t &dts);
private:
2022-08-08 17:13:39 +08:00
uint64_t _dts_pts_offset = 0;
uint64_t _last_dts = 0;
uint64_t _last_pts = 0;
uint64_t _last_max_pts = 0;
2021-01-19 16:05:38 +08:00
size_t _frames_since_last_max_pts = 0;
size_t _sorter_max_size = 0;
size_t _count_sorter_max_size = 0;
2022-08-08 17:13:39 +08:00
std::set<uint64_t> _pts_sorter;
};
class NtpStamp {
public:
NtpStamp() = default;
~NtpStamp() = default;
2021-09-02 21:17:59 +08:00
void setNtpStamp(uint32_t rtp_stamp, uint64_t ntp_stamp_ms);
uint64_t getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate);
private:
void update(uint32_t rtp_stamp, uint64_t ntp_stamp_us);
uint64_t getNtpStampUS(uint32_t rtp_stamp, uint32_t sample_rate);
private:
2021-07-13 10:10:23 +08:00
uint32_t _last_rtp_stamp = 0;
uint64_t _last_ntp_stamp_us = 0;
};
}//namespace mediakit
#endif //ZLMEDIAKIT_STAMP_H