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
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-08-02 18:06:37 +08:00
|
|
|
|
*
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef ZLMEDIAKIT_STAMP_H
|
|
|
|
|
#define ZLMEDIAKIT_STAMP_H
|
|
|
|
|
|
2020-01-14 10:04:24 +08:00
|
|
|
|
#include <set>
|
2019-08-02 18:06:37 +08:00
|
|
|
|
#include <cstdint>
|
2020-01-14 10:04:24 +08:00
|
|
|
|
#include "Util/TimeTicker.h"
|
2019-08-06 22:54:37 +08:00
|
|
|
|
using namespace toolkit;
|
2019-08-02 18:06:37 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2019-10-08 12:55:19 +08:00
|
|
|
|
class DeltaStamp{
|
2019-08-02 18:06:37 +08:00
|
|
|
|
public:
|
2019-10-08 12:55:19 +08:00
|
|
|
|
DeltaStamp() = default;
|
|
|
|
|
~DeltaStamp() = default;
|
2019-09-03 17:20:56 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-10-08 12:55:19 +08:00
|
|
|
|
* 计算时间戳增量
|
|
|
|
|
* @param stamp 绝对时间戳
|
|
|
|
|
* @return 时间戳增量
|
|
|
|
|
*/
|
|
|
|
|
int64_t deltaStamp(int64_t stamp);
|
2020-08-30 09:15:39 +08:00
|
|
|
|
|
2019-10-08 12:55:19 +08:00
|
|
|
|
private:
|
|
|
|
|
int64_t _last_stamp = 0;
|
|
|
|
|
};
|
2020-05-06 18:54:28 +08:00
|
|
|
|
|
2019-10-08 12:55:19 +08:00
|
|
|
|
//该类解决时间戳回环、回退问题
|
|
|
|
|
//计算相对时间戳或者产生平滑时间戳
|
|
|
|
|
class Stamp : public DeltaStamp{
|
|
|
|
|
public:
|
|
|
|
|
Stamp() = default;
|
|
|
|
|
~Stamp() = default;
|
2019-09-03 17:20:56 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2020-08-30 09:15:39 +08:00
|
|
|
|
* 求取相对时间戳,同时实现了音视频同步、限制dts回退等功能
|
2019-09-03 17:20:56 +08:00
|
|
|
|
* @param dts 输入dts,如果为0则根据系统时间戳生成
|
|
|
|
|
* @param pts 输入pts,如果为0则等于dts
|
|
|
|
|
* @param dts_out 输出dts
|
|
|
|
|
* @param pts_out 输出pts
|
2019-10-12 22:44:16 +08:00
|
|
|
|
* @param modifyStamp 是否用系统时间戳覆盖
|
2019-09-03 17:20:56 +08:00
|
|
|
|
*/
|
2019-10-12 22:44:16 +08:00
|
|
|
|
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-06 18:54:28 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2020-05-15 18:08:54 +08:00
|
|
|
|
* 音视频同步用,音频应该同步于视频(只修改音频时间戳)
|
|
|
|
|
* 因为音频时间戳修改后不影响播放速度
|
2020-05-06 18:54:28 +08:00
|
|
|
|
*/
|
2020-05-15 18:08:54 +08:00
|
|
|
|
void syncTo(Stamp &other);
|
2020-05-06 18:54:28 +08:00
|
|
|
|
|
|
|
|
|
private:
|
2020-08-30 09:15:39 +08:00
|
|
|
|
//主要实现音视频时间戳同步功能
|
2020-05-06 18:54:28 +08:00
|
|
|
|
void revise_l(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp = false);
|
2020-08-30 09:15:39 +08:00
|
|
|
|
|
|
|
|
|
//主要实现获取相对时间戳功能
|
|
|
|
|
void revise_l2(int64_t dts, int64_t pts, int64_t &dts_out, int64_t &pts_out,bool modifyStamp = false);
|
|
|
|
|
|
2019-08-02 18:06:37 +08:00
|
|
|
|
private:
|
2020-08-30 09:15:39 +08:00
|
|
|
|
int64_t _relative_stamp = 0;
|
|
|
|
|
int64_t _last_dts_in = 0;
|
|
|
|
|
int64_t _last_dts_out = 0;
|
|
|
|
|
int64_t _last_pts_out = 0;
|
2019-08-22 16:54:30 +08:00
|
|
|
|
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;
|
2019-08-02 18:06:37 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-05-06 18:54:28 +08:00
|
|
|
|
//dts生成器,
|
|
|
|
|
//pts排序后就是dts
|
2020-01-14 10:04:24 +08:00
|
|
|
|
class DtsGenerator{
|
|
|
|
|
public:
|
|
|
|
|
DtsGenerator() = default;
|
|
|
|
|
~DtsGenerator() = default;
|
|
|
|
|
bool getDts(uint32_t pts, uint32_t &dts);
|
2020-08-30 09:15:39 +08:00
|
|
|
|
|
2020-01-14 10:04:24 +08:00
|
|
|
|
private:
|
|
|
|
|
bool getDts_l(uint32_t pts, uint32_t &dts);
|
2020-08-30 09:15:39 +08:00
|
|
|
|
|
2020-01-14 10:04:24 +08:00
|
|
|
|
private:
|
|
|
|
|
uint32_t _dts_pts_offset = 0;
|
|
|
|
|
uint32_t _last_dts = 0;
|
|
|
|
|
uint32_t _last_pts = 0;
|
|
|
|
|
uint32_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;
|
2020-01-14 10:04:24 +08:00
|
|
|
|
set<uint32_t> _pts_sorter;
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-12 21:18:22 +08:00
|
|
|
|
class NtpStamp {
|
|
|
|
|
public:
|
|
|
|
|
NtpStamp() = default;
|
|
|
|
|
~NtpStamp() = default;
|
|
|
|
|
|
|
|
|
|
void setNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate, uint64_t ntp_stamp_ms);
|
|
|
|
|
uint64_t getNtpStamp(uint32_t rtp_stamp, uint32_t sample_rate);
|
|
|
|
|
|
|
|
|
|
private:
|
2021-07-13 10:10:23 +08:00
|
|
|
|
uint32_t _last_rtp_stamp = 0;
|
2021-07-12 21:18:22 +08:00
|
|
|
|
uint64_t _rtp_stamp_ms = 0;
|
|
|
|
|
uint64_t _ntp_stamp_ms = 0;
|
|
|
|
|
uint64_t _last_ret = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-02 18:06:37 +08:00
|
|
|
|
}//namespace mediakit
|
|
|
|
|
|
|
|
|
|
#endif //ZLMEDIAKIT_STAMP_H
|