Rtcp.h 28.9 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3 4 5 6 7 8 9 10 11 12 13
 * 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_RTCP_H
#define ZLMEDIAKIT_RTCP_H

14 15 16
#include "Common/macros.h"
#include "Network/Buffer.h"
#include "Util/util.h"
xiongziliang committed
17 18 19 20 21 22 23 24 25
#include <stdint.h>
#include <vector>

namespace mediakit {

#if defined(_WIN32)
#pragma pack(push, 1)
#endif // defined(_WIN32)

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
// http://www.networksorcery.com/enp/protocol/rtcp.htm
#define RTCP_PT_MAP(XX)                                                                                                \
    XX(RTCP_FIR, 192)                                                                                                  \
    XX(RTCP_NACK, 193)                                                                                                 \
    XX(RTCP_SMPTETC, 194)                                                                                              \
    XX(RTCP_IJ, 195)                                                                                                   \
    XX(RTCP_SR, 200)                                                                                                   \
    XX(RTCP_RR, 201)                                                                                                   \
    XX(RTCP_SDES, 202)                                                                                                 \
    XX(RTCP_BYE, 203)                                                                                                  \
    XX(RTCP_APP, 204)                                                                                                  \
    XX(RTCP_RTPFB, 205)                                                                                                \
    XX(RTCP_PSFB, 206)                                                                                                 \
    XX(RTCP_XR, 207)                                                                                                   \
    XX(RTCP_AVB, 208)                                                                                                  \
    XX(RTCP_RSI, 209)                                                                                                  \
    XX(RTCP_TOKEN, 210)

// https://tools.ietf.org/html/rfc3550#section-6.5
#define SDES_TYPE_MAP(XX)                                                                                              \
    XX(RTCP_SDES_END, 0)                                                                                               \
    XX(RTCP_SDES_CNAME, 1)                                                                                             \
    XX(RTCP_SDES_NAME, 2)                                                                                              \
    XX(RTCP_SDES_EMAIL, 3)                                                                                             \
    XX(RTCP_SDES_PHONE, 4)                                                                                             \
    XX(RTCP_SDES_LOC, 5)                                                                                               \
    XX(RTCP_SDES_TOOL, 6)                                                                                              \
    XX(RTCP_SDES_NOTE, 7)                                                                                              \
    XX(RTCP_SDES_PRIVATE, 8)

// https://datatracker.ietf.org/doc/rfc4585/?include_text=1
// 6.3.  Payload-Specific Feedback Messages
58
//
59 60
//    Payload-Specific FB messages are identified by the value PT=PSFB as
//    RTCP message type.
61
//
62 63 64
//    Three payload-specific FB messages are defined so far plus an
//    application layer FB message.  They are identified by means of the
//    FMT parameter as follows:
65
//
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
//       0:     unassigned
//       1:     Picture Loss Indication (PLI)
//       2:     Slice Loss Indication (SLI)
//       3:     Reference Picture Selection Indication (RPSI)
//       4:     FIR https://tools.ietf.org/html/rfc5104#section-4.3.1.1
//       5:     TSTR https://tools.ietf.org/html/rfc5104#section-4.3.2.1
//       6:     TSTN https://tools.ietf.org/html/rfc5104#section-4.3.2.1
//       7:     VBCM https://tools.ietf.org/html/rfc5104#section-4.3.4.1
//       8-14:  unassigned
//       15:    REMB / Application layer FB (AFB) message, https://tools.ietf.org/html/draft-alvestrand-rmcat-remb-03
//       16-30: unassigned
//       31:    reserved for future expansion of the sequence number space
#define PSFB_TYPE_MAP(XX)                                                                                              \
    XX(RTCP_PSFB_PLI, 1)                                                                                               \
    XX(RTCP_PSFB_SLI, 2)                                                                                               \
    XX(RTCP_PSFB_RPSI, 3)                                                                                              \
    XX(RTCP_PSFB_FIR, 4)                                                                                               \
    XX(RTCP_PSFB_TSTR, 5)                                                                                              \
    XX(RTCP_PSFB_TSTN, 6)                                                                                              \
    XX(RTCP_PSFB_VBCM, 7)                                                                                              \
    XX(RTCP_PSFB_REMB, 15)

// https://tools.ietf.org/html/rfc4585#section-6.2
// 6.2.   Transport Layer Feedback Messages
xiongziliang committed
90
//
91 92
//    Transport layer FB messages are identified by the value RTPFB as RTCP
//    message type.
xiongziliang committed
93
//
94 95 96
//    A single general purpose transport layer FB message is defined in
//    this document: Generic NACK.  It is identified by means of the FMT
//    parameter as follows:
xiongziliang committed
97
//
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
//    0:     unassigned
//    1:     Generic NACK
//    2:     reserved https://tools.ietf.org/html/rfc5104#section-4.2
//    3:     TMMBR https://tools.ietf.org/html/rfc5104#section-4.2.1.1
//    4:     TMMBN https://tools.ietf.org/html/rfc5104#section-4.2.2.1
//    5-14:  unassigned
//    15     transport-cc https://tools.ietf.org/html/draft-holmer-rmcat-transport-wide-cc-extensions-01
//    16-30: unassigned
//    31:    reserved for future expansion of the identifier number space
#define RTPFB_TYPE_MAP(XX)                                                                                             \
    XX(RTCP_RTPFB_NACK, 1)                                                                                             \
    XX(RTCP_RTPFB_TMMBR, 3)                                                                                            \
    XX(RTCP_RTPFB_TMMBN, 4)                                                                                            \
    XX(RTCP_RTPFB_TWCC, 15)

// rtcp类型枚举
xiongziliang committed
114 115 116 117 118 119
enum class RtcpType : uint8_t {
#define XX(key, value) key = value,
    RTCP_PT_MAP(XX)
#undef XX
};

120
// sdes类型枚举
xiongziliang committed
121 122 123 124 125 126
enum class SdesType : uint8_t {
#define XX(key, value) key = value,
    SDES_TYPE_MAP(XX)
#undef XX
};

127
// psfb类型枚举
128 129 130 131 132 133
enum class PSFBType : uint8_t {
#define XX(key, value) key = value,
    PSFB_TYPE_MAP(XX)
#undef XX
};

134
// rtpfb类型枚举
135 136 137 138 139 140
enum class RTPFBType : uint8_t {
#define XX(key, value) key = value,
    RTPFB_TYPE_MAP(XX)
#undef XX
};

xiongziliang committed
141 142 143 144 145 146 147 148 149 150
/**
 * RtcpType转描述字符串
 */
const char *rtcpTypeToStr(RtcpType type);

/**
 * SdesType枚举转描述字符串
 */
const char *sdesTypeToStr(SdesType type);

151 152 153 154 155
/**
 * psfb枚举转描述字符串
 */
const char *psfbTypeToStr(PSFBType type);

156 157 158 159 160
/**
 * rtpfb枚举转描述字符串
 */
const char *rtpfbTypeToStr(RTPFBType type);

xiongziliang committed
161 162 163
class RtcpHeader {
public:
#if __BYTE_ORDER == __BIG_ENDIAN
164 165 166 167 168 169
    // 版本号,固定为2
    uint32_t version : 2;
    // padding,固定为0
    uint32_t padding : 1;
    // reception report count
    uint32_t report_count : 5;
xiongziliang committed
170
#else
171 172 173 174 175 176
    // reception report count
    uint32_t report_count : 5;
    // padding,末尾是否有追加填充
    uint32_t padding : 1;
    // 版本号,固定为2
    uint32_t version : 2;
xiongziliang committed
177
#endif
178 179
    // rtcp类型,RtcpType
    uint32_t pt : 8;
180 181

private:
182 183
    // 长度
    uint32_t length : 16;
xiongziliang committed
184 185 186 187 188 189 190 191

public:
    /**
     * 解析rtcp并转换网络字节序为主机字节序,返回RtcpHeader派生类列表
     * @param data 数据指针
     * @param size 数据总长度
     * @return rtcp对象列表,无需free
     */
夏楚 committed
192
    static std::vector<RtcpHeader *> loadFromBytes(char *data, size_t size);
xiongziliang committed
193 194 195 196 197 198

    /**
     * rtcp包转Buffer对象
     * @param rtcp rtcp包对象智能指针
     * @return Buffer对象
     */
夏楚 committed
199
    static toolkit::Buffer::Ptr toBuffer(std::shared_ptr<RtcpHeader> rtcp);
xiongziliang committed
200 201 202 203 204 205

    /**
     * 打印rtcp相关字段详情(调用派生类的dumpString函数)
     * 内部会判断是什么类型的派生类
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
206
    std::string dumpString() const;
xiongziliang committed
207

xia-chu committed
208 209 210 211 212
    /**
     * 根据length字段获取rtcp总长度
     */
    size_t getSize() const;

xiongziliang committed
213
    /**
214 215 216 217 218
     * 后面追加padding数据长度
     */
    size_t getPaddingSize() const;

    /**
219 220
     * 设置rtcp length字段
     * @param size rtcp总长度,单位字节
xiongziliang committed
221
     */
222 223 224
    void setSize(size_t size);

protected:
xiongziliang committed
225 226 227 228
    /**
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
229
    std::string dumpHeader() const;
xiongziliang committed
230 231 232 233 234 235 236 237 238 239 240 241

private:
    /**
     * 调用派生类的net2Host函数
     * @param size rtcp字符长度
     */
    void net2Host(size_t size);

} PACKED;

/////////////////////////////////////////////////////////////////////////////

242
// ReportBlock
xiongziliang committed
243 244 245 246 247 248
class ReportItem {
public:
    friend class RtcpSR;
    friend class RtcpRR;

    uint32_t ssrc;
249 250 251 252 253
    // Fraction lost
    uint32_t fraction : 8;
    // Cumulative number of packets lost
    uint32_t cumulative : 24;
    // Sequence number cycles count
xiongziliang committed
254
    uint16_t seq_cycles;
255
    // Highest sequence number received
xiongziliang committed
256
    uint16_t seq_max;
257
    // Interarrival jitter
xiongziliang committed
258
    uint32_t jitter;
259
    // Last SR timestamp, NTP timestamp,(ntpmsw & 0xFFFF) << 16  | (ntplsw >> 16) & 0xFFFF)
xiongziliang committed
260
    uint32_t last_sr_stamp;
261
    // Delay since last SR timestamp,expressed in units of 1/65536 seconds
xiongziliang committed
262 263 264 265 266 267 268
    uint32_t delay_since_last_sr;

private:
    /**
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
269
    std::string dumpString() const;
xiongziliang committed
270 271 272 273 274

    /**
     * 网络字节序转换为主机字节序
     */
    void net2Host();
275
} PACKED;
xiongziliang committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

/*
 * 6.4.1 SR: Sender Report RTCP Packet

        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
header |V=2|P|    RC   |   PT=SR=200   |             length            |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         SSRC of sender                        |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
sender |              NTP timestamp, most significant word             |
info   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |             NTP timestamp, least significant word             |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         RTP timestamp                         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                     sender's packet count                     |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      sender's octet count                     |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_1 (SSRC of first source)                 |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1    | fraction lost |       cumulative number of packets lost       |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |           extended highest sequence number received           |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      interarrival jitter                      |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         last SR (LSR)                         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                   delay since last SR (DLSR)                  |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_2 (SSRC of second source)                |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2    :                               ...                             :
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
       |                  profile-specific extensions                  |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
// sender report
class RtcpSR : public RtcpHeader {
public:
    friend class RtcpHeader;
    uint32_t ssrc;
    // ntp timestamp MSW(in second)
    uint32_t ntpmsw;
    // ntp timestamp LSW(in picosecond)
    uint32_t ntplsw;
    // rtp timestamp
    uint32_t rtpts;
    // sender packet count
    uint32_t packet_count;
    // sender octet count
    uint32_t octet_count;
331
    // 可能有很多个
xiongziliang committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    ReportItem items;

public:
    /**
     * 创建SR包,只赋值了RtcpHeader部分(网络字节序)
     * @param item_count ReportItem对象个数
     * @return SR包
     */
    static std::shared_ptr<RtcpSR> create(size_t item_count);

    /**
     * 设置ntpmsw与ntplsw字段为网络字节序
     * @param tv 时间
     */
    void setNtpStamp(struct timeval tv);
347
    void setNtpStamp(uint64_t unix_stamp_ms);
xiongziliang committed
348 349 350 351 352

    /**
     * 返回ntp时间的字符串
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
353
    std::string getNtpStamp() const;
354
    uint64_t getNtpUnixStampMS() const;
xiongziliang committed
355 356 357 358 359

    /**
     * 获取ReportItem对象指针列表
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
360
    std::vector<ReportItem *> getItemList();
xiongziliang committed
361 362 363

private:
    /**
364 365 366
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
367
    std::string dumpString() const;
xiongziliang committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    /**
     * 网络字节序转换为主机字节序
     * @param size 字节长度,防止内存越界
     */
    void net2Host(size_t size);
} PACKED;

/////////////////////////////////////////////////////////////////////////////

/*
 * 6.4.2 RR: Receiver Report RTCP Packet

        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
header |V=2|P|    RC   |   PT=RR=201   |             length            |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                     SSRC of packet sender                     |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_1 (SSRC of first source)                 |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1    | fraction lost |       cumulative number of packets lost       |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |           extended highest sequence number received           |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      interarrival jitter                      |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         last SR (LSR)                         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                   delay since last SR (DLSR)                  |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_2 (SSRC of second source)                |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2    :                               ...                             :
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
       |                  profile-specific extensions                  |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

408
// Receiver Report
xiongziliang committed
409 410 411 412 413
class RtcpRR : public RtcpHeader {
public:
    friend class RtcpHeader;

    uint32_t ssrc;
414
    // 可能有很多个
xiongziliang committed
415 416 417 418 419 420 421
    ReportItem items;

public:
    /**
     * 创建RR包,只赋值了RtcpHeader部分
     * @param item_count ReportItem对象个数
     * @return RR包
422
     */
xiongziliang committed
423 424 425 426 427 428
    static std::shared_ptr<RtcpRR> create(size_t item_count);

    /**
     * 获取ReportItem对象指针列表
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
429
    std::vector<ReportItem *> getItemList();
xiongziliang committed
430 431 432 433 434 435 436 437 438 439 440 441

private:
    /**
     * 网络字节序转换为主机字节序
     * @param size 字节长度,防止内存越界
     */
    void net2Host(size_t size);

    /**
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
442
    std::string dumpString() const;
xiongziliang committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

} PACKED;

/////////////////////////////////////////////////////////////////////////////

/*
 *      6.5 SDES: Source Description RTCP Packet
        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
header |V=2|P|    SC   |  PT=SDES=202  |             length            |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
chunk  |                          SSRC/CSRC_1                          |
  1    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                           SDES items                          |
       |                              ...                              |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
chunk  |                          SSRC/CSRC_2                          |
  2    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                           SDES items                          |
       |                              ...                              |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 */

/*

SDES items 定义
0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|   SdesType  |     length    | user and domain name        ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

477
// Source description Chunk
ziyue committed
478
class SdesChunk {
xiongziliang committed
479 480 481 482
public:
    friend class RtcpSdes;

    uint32_t ssrc;
483
    // SdesType
xiongziliang committed
484
    uint8_t type;
485
    // text长度股,可以为0
486
    uint8_t txt_len;
487
    // 不定长
xiongziliang committed
488
    char text[1];
489 490
    // 最后以RTCP_SDES_END结尾
    // 只字段为占位字段,不代表真实位置
xiongziliang committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
    uint8_t end;

public:
    /**
     * 返回改对象字节长度
     */
    size_t totalBytes() const;

    /**
     * 本对象最少长度
     */
    static size_t minSize();

private:
    /**
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
509
    std::string dumpString() const;
xiongziliang committed
510 511 512

    /**
     * 网络字节序转换为主机字节序
513
     */
xiongziliang committed
514 515 516
    void net2Host();
} PACKED;

517
// Source description
xiongziliang committed
518 519 520 521
class RtcpSdes : public RtcpHeader {
public:
    friend class RtcpHeader;

522
    // 可能有很多个
ziyue committed
523
    SdesChunk chunks;
xiongziliang committed
524 525 526

public:
    /**
ziyue committed
527 528
     * 创建SDES包,只赋值了RtcpHeader以及SdesChunk对象的length和text部分
     * @param item_text SdesChunk列表,只赋值length和text部分
xiongziliang committed
529 530
     * @return SDES包
     */
夏楚 committed
531
    static std::shared_ptr<RtcpSdes> create(const std::vector<std::string> &item_text);
xiongziliang committed
532 533

    /**
ziyue committed
534
     * 获取SdesChunk对象指针列表
xiongziliang committed
535 536
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
537
    std::vector<SdesChunk *> getChunkList();
xiongziliang committed
538 539 540

private:
    /**
541 542 543
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
544
    std::string dumpString() const;
xiongziliang committed
545 546 547 548 549 550 551 552

    /**
     * 网络字节序转换为主机字节序
     * @param size 字节长度,防止内存越界
     */
    void net2Host(size_t size);
} PACKED;

xiongziliang committed
553 554
// https://tools.ietf.org/html/rfc4585#section-6.1
// 6.1.   Common Packet Format for Feedback Messages
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
//
//   All FB messages MUST use a common packet format that is depicted in
//   Figure 3:
//
//    0                   1                   2                   3
//    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//   |V=2|P|   FMT   |       PT      |          length               |
//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//   |                  SSRC of packet sender                        |
//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//   |                  SSRC of media source                         |
//   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//   :            Feedback Control Information (FCI)                 :
//   :                                                               :
570 571
// rtcpfb和psfb的数据结构一致
class RtcpFB : public RtcpHeader {
xiongziliang committed
572 573 574 575 576 577 578
public:
    friend class RtcpHeader;
    uint32_t ssrc;
    uint32_t ssrc_media;

public:
    /**
579
     * 创建psfb类型的反馈包
xiongziliang committed
580
     */
581 582 583 584 585 586
    static std::shared_ptr<RtcpFB> create(PSFBType fmt, const void *fci = nullptr, size_t fci_len = 0);

    /**
     * 创建rtpfb类型的反馈包
     */
    static std::shared_ptr<RtcpFB> create(RTPFBType fmt, const void *fci = nullptr, size_t fci_len = 0);
xiongziliang committed
587

xiongziliang committed
588 589 590 591 592
    /**
     * fci转换成某对象指针
     * @tparam Type 对象类型
     * @return 对象指针
     */
593 594
    template <typename Type>
    const Type &getFci() const {
xiongziliang committed
595 596
        auto fci_data = getFciPtr();
        auto fci_len = getFciSize();
597
        Type *fci = (Type *)fci_data;
xiongziliang committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611
        fci->check(fci_len);
        return *fci;
    }

    /**
     * 获取fci指针
     */
    const void *getFciPtr() const;

    /**
     * 获取fci数据长度
     */
    size_t getFciSize() const;

xiongziliang committed
612 613
private:
    /**
614 615 616
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
617
    std::string dumpString() const;
xiongziliang committed
618 619 620 621 622 623

    /**
     * 网络字节序转换为主机字节序
     * @param size 字节长度,防止内存越界
     */
    void net2Host(size_t size);
624 625 626

private:
    static std::shared_ptr<RtcpFB> create_l(RtcpType type, int fmt, const void *fci, size_t fci_len);
xiongziliang committed
627 628
} PACKED;

629
// BYE
xia-chu committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
/*
       0                   1                   2                   3
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |V=2|P|    SC   |   PT=BYE=203  |             length            |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                           SSRC/CSRC                           |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      :                              ...                              :
      +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
(opt) |     length    |               reason for leaving            ...
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/

class RtcpBye : public RtcpHeader {
public:
    friend class RtcpHeader;
    /* 变长,根据count决定有多少个ssrc */
648
    uint32_t ssrc[1];
xia-chu committed
649 650 651 652 653

    /** 中间可能有若干个 ssrc **/

    /* 可选 */
    uint8_t reason_len;
xiongziliang committed
654
    char reason[1];
xia-chu committed
655 656 657 658 659 660 661 662

public:
    /**
     * 创建bye包
     * @param ssrc ssrc列表
     * @param reason 原因
     * @return rtcp bye包
     */
夏楚 committed
663
    static std::shared_ptr<RtcpBye> create(const std::vector<uint32_t> &ssrc, const std::string &reason);
xia-chu committed
664 665 666 667

    /**
     * 获取ssrc列表
     */
夏楚 committed
668
    std::vector<uint32_t *> getSSRC();
xia-chu committed
669 670 671 672

    /**
     * 获取原因
     */
夏楚 committed
673
    std::string getReason() const;
xia-chu committed
674 675 676

private:
    /**
677 678 679
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
夏楚 committed
680
    std::string dumpString() const;
xia-chu committed
681 682 683 684 685 686 687 688

    /**
     * 网络字节序转换为主机字节序
     * @param size 字节长度,防止内存越界
     */
    void net2Host(size_t size);
} PACKED;

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
/*
0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|V=2|P|reserved |   PT=XR=207   |             length            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                              SSRC                             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
:                         report blocks                         :
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

*/
/*

    0                   1                   2                   3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |     BT=4      |   reserved    |       block length = 2        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |              NTP timestamp, most significant word             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |             NTP timestamp, least significant word             |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

*/
714
class RtcpXRRRTR : public RtcpHeader {
715 716 717 718 719 720 721 722
public:
    friend class RtcpHeader;
    uint32_t ssrc;
    // 4
    uint8_t bt;
    uint8_t reserved;
    // 2
    uint16_t block_length;
723
    // ntp timestamp MSW(in second)
724 725 726
    uint32_t ntpmsw;
    // ntp timestamp LSW(in picosecond)
    uint32_t ntplsw;
727

728
private:
729 730 731 732
    /**
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
733 734 735 736 737 738 739 740
    std::string dumpString() const;

    /**
     * 网络字节序转换为主机字节序
     * @param size 字节长度,防止内存越界
     */
    void net2Host(size_t size);

741
} PACKED;
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760

/*

  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 |     BT=5      |   reserved    |         block length          |
 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 |                 SSRC_1 (SSRC of first receiver)               | sub-
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
 |                         last RR (LRR)                         |   1
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 |                   delay since last RR (DLRR)                  |
 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
 |                 SSRC_2 (SSRC of second receiver)              | sub-
 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ block
 :                               ...                             :   2
 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
*/
761
class RtcpXRDLRRReportItem {
762 763 764 765 766
public:
    friend class RtcpXRDLRR;
    uint32_t ssrc;
    uint32_t lrr;
    uint32_t dlrr;
767

768
private:
769 770 771 772
    /**
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
773 774 775 776 777 778 779
    std::string dumpString() const;

    /**
     * 网络字节序转换为主机字节序
     * @param size 字节长度,防止内存越界
     */
    void net2Host();
780
} PACKED;
781

782
class RtcpXRDLRR : public RtcpHeader {
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
public:
    friend class RtcpHeader;
    uint32_t ssrc;
    uint8_t bt;
    uint8_t reserved;
    uint16_t block_length;
    RtcpXRDLRRReportItem items;

    /**
     * 创建RtcpXRDLRR包,只赋值了RtcpHeader部分(网络字节序)
     * @param item_count RtcpXRDLRRReportItem对象个数
     * @return RtcpXRDLRR包
     */
    static std::shared_ptr<RtcpXRDLRR> create(size_t item_count);

798
    /**
799 800 801
     * 获取RtcpXRDLRRReportItem对象指针列表
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
802
    std::vector<RtcpXRDLRRReportItem *> getItemList();
803 804

private:
805 806 807 808
    /**
     * 打印字段详情
     * 使用net2Host转换成主机字节序后才可使用此函数
     */
809 810 811 812 813 814 815 816
    std::string dumpString() const;

    /**
     * 网络字节序转换为主机字节序
     * @param size 字节长度,防止内存越界
     */
    void net2Host(size_t size);

817
} PACKED;
818

xiongziliang committed
819 820 821 822
#if defined(_WIN32)
#pragma pack(pop)
#endif // defined(_WIN32)

823 824
} // namespace mediakit
#endif // ZLMEDIAKIT_RTCP_H