2021-01-31 20:46:55 +08:00
|
|
|
|
/*
|
2021-01-31 19:18:17 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* 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_RTCPCONTEXT_H
|
|
|
|
|
#define ZLMEDIAKIT_RTCPCONTEXT_H
|
|
|
|
|
|
|
|
|
|
#include "Rtcp.h"
|
2022-07-14 22:49:31 +08:00
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
class RtcpContext {
|
|
|
|
|
public:
|
|
|
|
|
using Ptr = std::shared_ptr<RtcpContext>;
|
2021-10-13 15:06:13 +08:00
|
|
|
|
virtual ~RtcpContext() = default;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输出或输入rtp时调用
|
|
|
|
|
* @param seq rtp的seq
|
2021-06-25 14:59:27 +08:00
|
|
|
|
* @param stamp rtp的时间戳,单位采样数(非毫秒)
|
2021-07-12 21:18:22 +08:00
|
|
|
|
* @param ntp_stamp_ms ntp时间戳
|
2021-07-06 21:48:51 +08:00
|
|
|
|
* @param rtp rtp时间戳采样率,视频一般为90000,音频一般为采样率
|
2021-01-31 19:18:17 +08:00
|
|
|
|
* @param bytes rtp数据长度
|
|
|
|
|
*/
|
2021-10-13 15:06:13 +08:00
|
|
|
|
virtual void onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 输入sr rtcp包
|
|
|
|
|
* @param rtcp 输入一个rtcp
|
|
|
|
|
*/
|
2021-10-13 20:28:10 +08:00
|
|
|
|
virtual void onRtcp(RtcpHeader *rtcp) = 0;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算总丢包数
|
|
|
|
|
*/
|
2021-10-13 15:06:13 +08:00
|
|
|
|
virtual size_t getLost();
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回理应收到的rtp数
|
|
|
|
|
*/
|
2021-10-13 15:06:13 +08:00
|
|
|
|
virtual size_t getExpectedPackets() const;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建SR rtcp包
|
|
|
|
|
* @param rtcp_ssrc rtcp的ssrc
|
|
|
|
|
* @return rtcp包
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual toolkit::Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
2022-07-12 19:26:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* @brief 创建xr的dlrr包,用于接收者估算rtt
|
2022-07-14 22:49:31 +08:00
|
|
|
|
*
|
|
|
|
|
* @return toolkit::Buffer::Ptr
|
2022-07-12 19:26:17 +08:00
|
|
|
|
*/
|
|
|
|
|
virtual toolkit::Buffer::Ptr createRtcpXRDLRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc);
|
|
|
|
|
|
2021-01-31 19:18:17 +08:00
|
|
|
|
/**
|
|
|
|
|
* 创建RR rtcp包
|
|
|
|
|
* @param rtcp_ssrc rtcp的ssrc
|
|
|
|
|
* @param rtp_ssrc rtp的ssrc
|
|
|
|
|
* @return rtcp包
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
virtual toolkit::Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc);
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 上次结果与本次结果间应收包数
|
|
|
|
|
*/
|
2021-10-13 17:57:52 +08:00
|
|
|
|
virtual size_t getExpectedPacketsInterval();
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 上次结果与本次结果间丢包个数
|
|
|
|
|
*/
|
2021-10-13 17:57:52 +08:00
|
|
|
|
virtual size_t geLostInterval();
|
2021-01-31 19:18:17 +08:00
|
|
|
|
|
2021-10-13 15:06:13 +08:00
|
|
|
|
protected:
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 收到或发送的rtp的字节数
|
2021-01-31 19:18:17 +08:00
|
|
|
|
size_t _bytes = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 收到或发送的rtp的个数
|
2021-01-31 19:18:17 +08:00
|
|
|
|
size_t _packets = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 上次的rtp时间戳,毫秒
|
2021-01-31 19:18:17 +08:00
|
|
|
|
uint32_t _last_rtp_stamp = 0;
|
2021-07-12 21:18:22 +08:00
|
|
|
|
uint64_t _last_ntp_stamp_ms = 0;
|
2021-01-31 19:18:17 +08:00
|
|
|
|
};
|
|
|
|
|
|
2021-10-13 15:06:13 +08:00
|
|
|
|
class RtcpContextForSend : public RtcpContext {
|
|
|
|
|
public:
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc) override;
|
2022-07-12 19:26:17 +08:00
|
|
|
|
|
2021-10-13 20:28:10 +08:00
|
|
|
|
void onRtcp(RtcpHeader *rtcp) override;
|
|
|
|
|
|
2022-07-12 19:26:17 +08:00
|
|
|
|
toolkit::Buffer::Ptr createRtcpXRDLRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) override;
|
|
|
|
|
|
2021-10-13 20:28:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取rtt
|
|
|
|
|
* @param ssrc rtp ssrc
|
|
|
|
|
* @return rtt,单位毫秒
|
|
|
|
|
*/
|
|
|
|
|
uint32_t getRtt(uint32_t ssrc) const;
|
|
|
|
|
|
|
|
|
|
private:
|
2022-07-14 22:49:31 +08:00
|
|
|
|
std::map<uint32_t /*ssrc*/, uint32_t /*rtt*/> _rtt;
|
|
|
|
|
std::map<uint32_t /*last_sr_lsr*/, uint64_t /*ntp stamp*/> _sender_report_ntp;
|
2022-07-12 19:26:17 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
std::map<uint32_t /*ssrc*/, uint64_t /*xr rrtr sys stamp*/> _xr_rrtr_recv_sys_stamp;
|
|
|
|
|
std::map<uint32_t /*ssrc*/, uint32_t /*last rr */> _xr_xrrtr_recv_last_rr;
|
2021-10-13 15:06:13 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class RtcpContextForRecv : public RtcpContext {
|
2021-10-13 20:28:10 +08:00
|
|
|
|
public:
|
|
|
|
|
void onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes) override;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) override;
|
2021-10-13 20:28:10 +08:00
|
|
|
|
size_t getExpectedPackets() const override;
|
|
|
|
|
size_t getExpectedPacketsInterval() override;
|
|
|
|
|
size_t getLost() override;
|
|
|
|
|
size_t geLostInterval() override;
|
|
|
|
|
void onRtcp(RtcpHeader *rtcp) override;
|
|
|
|
|
|
|
|
|
|
private:
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 时间戳抖动值
|
2021-10-13 15:20:03 +08:00
|
|
|
|
double _jitter = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 第一个seq的值
|
2021-10-13 15:20:03 +08:00
|
|
|
|
uint16_t _seq_base = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtp最大seq
|
2021-10-13 15:20:03 +08:00
|
|
|
|
uint16_t _seq_max = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// rtp回环次数
|
2021-10-13 15:20:03 +08:00
|
|
|
|
uint16_t _seq_cycles = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 上次回环发生时,记录的rtp包数
|
2021-10-13 15:20:03 +08:00
|
|
|
|
size_t _last_cycle_packets = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 上次的seq
|
2021-10-13 15:20:03 +08:00
|
|
|
|
uint16_t _last_rtp_seq = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 上次的rtp的系统时间戳(毫秒)用于统计抖动
|
2021-10-13 15:20:03 +08:00
|
|
|
|
uint64_t _last_rtp_sys_stamp = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 上次统计的丢包总数
|
2021-10-13 20:28:10 +08:00
|
|
|
|
size_t _last_lost = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 上次统计应收rtp包总数
|
2021-10-13 20:28:10 +08:00
|
|
|
|
size_t _last_expected = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 上次收到sr包时计算出的Last SR timestamp
|
2021-10-13 20:28:10 +08:00
|
|
|
|
uint32_t _last_sr_lsr = 0;
|
2022-07-14 22:49:31 +08:00
|
|
|
|
// 上次收到sr时的系统时间戳,单位毫秒
|
2021-10-13 20:28:10 +08:00
|
|
|
|
uint64_t _last_sr_ntp_sys = 0;
|
2021-10-13 15:06:13 +08:00
|
|
|
|
};
|
2021-10-13 20:28:10 +08:00
|
|
|
|
|
2022-07-14 22:49:31 +08:00
|
|
|
|
} // namespace mediakit
|
|
|
|
|
#endif // ZLMEDIAKIT_RTCPCONTEXT_H
|