RtcpContext.h 4.35 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3 4 5 6 7 8 9 10 11 12 13 14
 * 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"
15 16
#include <stddef.h>
#include <stdint.h>
xiongziliang committed
17 18 19 20 21 22

namespace mediakit {

class RtcpContext {
public:
    using Ptr = std::shared_ptr<RtcpContext>;
23
    virtual ~RtcpContext() = default;
xiongziliang committed
24 25 26 27

    /**
     * 输出或输入rtp时调用
     * @param seq rtp的seq
28
     * @param stamp rtp的时间戳,单位采样数(非毫秒)
29
     * @param ntp_stamp_ms ntp时间戳
30
     * @param rtp rtp时间戳采样率,视频一般为90000,音频一般为采样率
xiongziliang committed
31 32
     * @param bytes rtp数据长度
     */
33
    virtual void onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes);
xiongziliang committed
34 35 36 37 38

    /**
     * 输入sr rtcp包
     * @param rtcp 输入一个rtcp
     */
ziyue committed
39
    virtual void onRtcp(RtcpHeader *rtcp) = 0;
xiongziliang committed
40 41 42 43

    /**
     * 计算总丢包数
     */
44
    virtual size_t getLost();
xiongziliang committed
45 46 47 48

    /**
     * 返回理应收到的rtp数
     */
49
    virtual size_t getExpectedPackets() const;
xiongziliang committed
50 51 52 53 54 55

    /**
     * 创建SR rtcp包
     * @param rtcp_ssrc rtcp的ssrc
     * @return rtcp包
     */
夏楚 committed
56
    virtual toolkit::Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc);
xiongziliang committed
57

58 59
    /**
     * @brief 创建xr的dlrr包,用于接收者估算rtt
60 61
     *
     * @return toolkit::Buffer::Ptr
62 63 64
     */
    virtual toolkit::Buffer::Ptr createRtcpXRDLRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc);

xiongziliang committed
65 66 67 68 69 70
    /**
     * 创建RR rtcp包
     * @param rtcp_ssrc rtcp的ssrc
     * @param rtp_ssrc rtp的ssrc
     * @return rtcp包
     */
夏楚 committed
71
    virtual toolkit::Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc);
xiongziliang committed
72 73 74 75

    /**
     * 上次结果与本次结果间应收包数
     */
76
    virtual size_t getExpectedPacketsInterval();
xiongziliang committed
77 78 79 80

    /**
     * 上次结果与本次结果间丢包个数
     */
81
    virtual size_t geLostInterval();
xiongziliang committed
82

83
protected:
84
    // 收到或发送的rtp的字节数
xiongziliang committed
85
    size_t _bytes = 0;
86
    // 收到或发送的rtp的个数
xiongziliang committed
87
    size_t _packets = 0;
88
    // 上次的rtp时间戳,毫秒
xiongziliang committed
89
    uint32_t _last_rtp_stamp = 0;
90
    uint64_t _last_ntp_stamp_ms = 0;
xiongziliang committed
91 92
};

93 94
class RtcpContextForSend : public RtcpContext {
public:
夏楚 committed
95
    toolkit::Buffer::Ptr createRtcpSR(uint32_t rtcp_ssrc) override;
96

ziyue committed
97 98
    void onRtcp(RtcpHeader *rtcp) override;

99 100
    toolkit::Buffer::Ptr createRtcpXRDLRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) override;

ziyue committed
101 102 103 104 105 106 107 108
    /**
     * 获取rtt
     * @param ssrc rtp ssrc
     * @return rtt,单位毫秒
     */
    uint32_t getRtt(uint32_t ssrc) const;

private:
109 110
    std::map<uint32_t /*ssrc*/, uint32_t /*rtt*/> _rtt;
    std::map<uint32_t /*last_sr_lsr*/, uint64_t /*ntp stamp*/> _sender_report_ntp;
111

112 113
    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;
114 115 116
};

class RtcpContextForRecv : public RtcpContext {
ziyue committed
117 118
public:
    void onRtp(uint16_t seq, uint32_t stamp, uint64_t ntp_stamp_ms, uint32_t sample_rate, size_t bytes) override;
夏楚 committed
119
    toolkit::Buffer::Ptr createRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) override;
ziyue committed
120 121 122 123 124 125 126
    size_t getExpectedPackets() const override;
    size_t getExpectedPacketsInterval() override;
    size_t getLost() override;
    size_t geLostInterval() override;
    void onRtcp(RtcpHeader *rtcp) override;

private:
127
    // 时间戳抖动值
128
    double _jitter = 0;
129
    // 第一个seq的值
130
    uint16_t _seq_base = 0;
131
    // rtp最大seq
132
    uint16_t _seq_max = 0;
133
    // rtp回环次数
134
    uint16_t _seq_cycles = 0;
135
    // 上次回环发生时,记录的rtp包数
136
    size_t _last_cycle_packets = 0;
137
    // 上次的seq
138
    uint16_t _last_rtp_seq = 0;
139
    // 上次的rtp的系统时间戳(毫秒)用于统计抖动
140
    uint64_t _last_rtp_sys_stamp = 0;
141
    // 上次统计的丢包总数
ziyue committed
142
    size_t _last_lost = 0;
143
    // 上次统计应收rtp包总数
ziyue committed
144
    size_t _last_expected = 0;
145
    // 上次收到sr包时计算出的Last SR timestamp
ziyue committed
146
    uint32_t _last_sr_lsr = 0;
147
    // 上次收到sr时的系统时间戳,单位毫秒
ziyue committed
148
    uint64_t _last_sr_ntp_sys = 0;
149
};
ziyue committed
150

151 152
} // namespace mediakit
#endif // ZLMEDIAKIT_RTCPCONTEXT_H