asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |
| 12 | #define MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | #include "api/array_view.h" |
Artem Titov | 741daaf | 2019-03-21 14:37:36 +0100 | [diff] [blame] | 18 | #include "api/function_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "rtc_base/buffer.h" |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace rtcp { |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 23 | // Class for building RTCP packets. |
| 24 | // |
| 25 | // Example: |
| 26 | // ReportBlock report_block; |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 27 | // report_block.SetMediaSsrc(234); |
| 28 | // report_block.SetFractionLost(10); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 29 | // |
| 30 | // ReceiverReport rr; |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 31 | // rr.SetSenderSsrc(123); |
| 32 | // rr.AddReportBlock(report_block); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 33 | // |
| 34 | // Fir fir; |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 35 | // fir.SetSenderSsrc(123); |
| 36 | // fir.AddRequestTo(234, 56); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 37 | // |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 38 | // size_t length = 0; // Builds an intra frame request |
Danil Chapovalov | 32e590e | 2016-01-22 11:04:56 +0100 | [diff] [blame] | 39 | // uint8_t packet[kPacketSize]; // with sequence number 56. |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 40 | // fir.Build(packet, &length, kPacketSize); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 41 | // |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 42 | // rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 43 | // // the built rtcp packet. |
| 44 | // |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 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(); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 49 | |
| 50 | class RtcpPacket { |
| 51 | public: |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 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. |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 56 | using PacketReadyCallback = |
| 57 | rtc::FunctionView<void(rtc::ArrayView<const uint8_t> packet)>; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 58 | |
Danil Chapovalov | cbbfd08 | 2019-10-10 09:56:29 +0200 | [diff] [blame] | 59 | virtual ~RtcpPacket() = default; |
| 60 | |
| 61 | void SetSenderSsrc(uint32_t ssrc) { sender_ssrc_ = ssrc; } |
| 62 | uint32_t sender_ssrc() const { return sender_ssrc_; } |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 63 | |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 64 | // Convenience method mostly used for test. Creates packet without |
| 65 | // fragmentation using BlockLength() to allocate big enough buffer. |
| 66 | rtc::Buffer Build() const; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 67 | |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 68 | // Returns true if call to Create succeeded. |
| 69 | bool Build(size_t max_length, PacketReadyCallback callback) const; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 70 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 71 | // Size of this packet in bytes (including headers). |
Erik Språng | 6b8d355 | 2015-09-24 15:06:57 +0200 | [diff] [blame] | 72 | virtual size_t BlockLength() const = 0; |
| 73 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 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. |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 77 | virtual bool Create(uint8_t* packet, |
| 78 | size_t* index, |
| 79 | size_t max_length, |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 80 | PacketReadyCallback callback) const = 0; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 81 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 82 | protected: |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 83 | // Size of the rtcp common header. |
| 84 | static constexpr size_t kHeaderLength = 4; |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 85 | RtcpPacket() {} |
| 86 | |
Danil Chapovalov | 6c17057 | 2017-09-15 16:48:14 +0200 | [diff] [blame] | 87 | static void CreateHeader(size_t count_or_format, |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 88 | uint8_t packet_type, |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 89 | size_t block_length, // Payload size in 32bit words. |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 90 | uint8_t* buffer, |
| 91 | size_t* pos); |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 92 | |
Johannes Kron | 99b9149 | 2019-02-12 10:51:18 +0100 | [diff] [blame] | 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 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 100 | bool OnBufferFull(uint8_t* packet, |
| 101 | size_t* index, |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 102 | PacketReadyCallback callback) const; |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 103 | // Size of the rtcp packet as written in header. |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 104 | size_t HeaderLength() const; |
Danil Chapovalov | cbbfd08 | 2019-10-10 09:56:29 +0200 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | uint32_t sender_ssrc_ = 0; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 108 | }; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 109 | } // namespace rtcp |
| 110 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 111 | #endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |