rtcp_packet.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. *
  10. */
  11. #ifndef MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
  12. #define MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #include "api/array_view.h"
  16. #include "api/function_view.h"
  17. #include "rtc_base/buffer.h"
  18. namespace webrtc {
  19. namespace rtcp {
  20. // Class for building RTCP packets.
  21. //
  22. // Example:
  23. // ReportBlock report_block;
  24. // report_block.SetMediaSsrc(234);
  25. // report_block.SetFractionLost(10);
  26. //
  27. // ReceiverReport rr;
  28. // rr.SetSenderSsrc(123);
  29. // rr.AddReportBlock(report_block);
  30. //
  31. // Fir fir;
  32. // fir.SetSenderSsrc(123);
  33. // fir.AddRequestTo(234, 56);
  34. //
  35. // size_t length = 0; // Builds an intra frame request
  36. // uint8_t packet[kPacketSize]; // with sequence number 56.
  37. // fir.Build(packet, &length, kPacketSize);
  38. //
  39. // rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding
  40. // // the built rtcp packet.
  41. //
  42. // CompoundPacket compound; // Builds a compound RTCP packet with
  43. // compound.Append(&rr); // a receiver report, report block
  44. // compound.Append(&fir); // and fir message.
  45. // rtc::Buffer packet = compound.Build();
  46. class RtcpPacket {
  47. public:
  48. // Callback used to signal that an RTCP packet is ready. Note that this may
  49. // not contain all data in this RtcpPacket; if a packet cannot fit in
  50. // max_length bytes, it will be fragmented and multiple calls to this
  51. // callback will be made.
  52. using PacketReadyCallback =
  53. rtc::FunctionView<void(rtc::ArrayView<const uint8_t> packet)>;
  54. virtual ~RtcpPacket() = default;
  55. void SetSenderSsrc(uint32_t ssrc) { sender_ssrc_ = ssrc; }
  56. uint32_t sender_ssrc() const { return sender_ssrc_; }
  57. // Convenience method mostly used for test. Creates packet without
  58. // fragmentation using BlockLength() to allocate big enough buffer.
  59. rtc::Buffer Build() const;
  60. // Returns true if call to Create succeeded.
  61. bool Build(size_t max_length, PacketReadyCallback callback) const;
  62. // Size of this packet in bytes (including headers).
  63. virtual size_t BlockLength() const = 0;
  64. // Creates packet in the given buffer at the given position.
  65. // Calls PacketReadyCallback::OnPacketReady if remaining buffer is too small
  66. // and assume buffer can be reused after OnPacketReady returns.
  67. virtual bool Create(uint8_t* packet,
  68. size_t* index,
  69. size_t max_length,
  70. PacketReadyCallback callback) const = 0;
  71. protected:
  72. // Size of the rtcp common header.
  73. static constexpr size_t kHeaderLength = 4;
  74. RtcpPacket() {}
  75. static void CreateHeader(size_t count_or_format,
  76. uint8_t packet_type,
  77. size_t block_length, // Payload size in 32bit words.
  78. uint8_t* buffer,
  79. size_t* pos);
  80. static void CreateHeader(size_t count_or_format,
  81. uint8_t packet_type,
  82. size_t block_length, // Payload size in 32bit words.
  83. bool padding, // True if there are padding bytes.
  84. uint8_t* buffer,
  85. size_t* pos);
  86. bool OnBufferFull(uint8_t* packet,
  87. size_t* index,
  88. PacketReadyCallback callback) const;
  89. // Size of the rtcp packet as written in header.
  90. size_t HeaderLength() const;
  91. private:
  92. uint32_t sender_ssrc_ = 0;
  93. };
  94. } // namespace rtcp
  95. } // namespace webrtc
  96. #endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_