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