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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/buffer.h" |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 15 | #include "rtc_base/function_view.h" |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace rtcp { |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 19 | // Class for building RTCP packets. |
| 20 | // |
| 21 | // Example: |
| 22 | // ReportBlock report_block; |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 23 | // report_block.SetMediaSsrc(234); |
| 24 | // report_block.SetFractionLost(10); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 25 | // |
| 26 | // ReceiverReport rr; |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 27 | // rr.SetSenderSsrc(123); |
| 28 | // rr.AddReportBlock(report_block); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 29 | // |
| 30 | // Fir fir; |
danilchap | 822a16f | 2016-09-27 09:27:47 -0700 | [diff] [blame] | 31 | // fir.SetSenderSsrc(123); |
| 32 | // fir.AddRequestTo(234, 56); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 33 | // |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 34 | // size_t length = 0; // Builds an intra frame request |
Danil Chapovalov | 32e590e | 2016-01-22 11:04:56 +0100 | [diff] [blame] | 35 | // uint8_t packet[kPacketSize]; // with sequence number 56. |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 36 | // fir.Build(packet, &length, kPacketSize); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 37 | // |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 38 | // rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 39 | // // the built rtcp packet. |
| 40 | // |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 41 | // CompoundPacket compound; // Builds a compound RTCP packet with |
| 42 | // compound.Append(&rr); // a receiver report, report block |
| 43 | // compound.Append(&fir); // and fir message. |
| 44 | // rtc::Buffer packet = compound.Build(); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 45 | |
| 46 | class RtcpPacket { |
| 47 | public: |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 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. |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 52 | using PacketReadyCallback = |
| 53 | rtc::FunctionView<void(rtc::ArrayView<const uint8_t> packet)>; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 54 | |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 55 | virtual ~RtcpPacket() {} |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 56 | |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 57 | // Convenience method mostly used for test. Creates packet without |
| 58 | // fragmentation using BlockLength() to allocate big enough buffer. |
| 59 | rtc::Buffer Build() const; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 60 | |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 61 | // Returns true if call to Create succeeded. |
| 62 | bool Build(size_t max_length, PacketReadyCallback callback) const; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 63 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 64 | // Size of this packet in bytes (including headers). |
Erik Språng | 6b8d355 | 2015-09-24 15:06:57 +0200 | [diff] [blame] | 65 | virtual size_t BlockLength() const = 0; |
| 66 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 67 | // Creates packet in the given buffer at the given position. |
| 68 | // Calls PacketReadyCallback::OnPacketReady if remaining buffer is too small |
| 69 | // and assume buffer can be reused after OnPacketReady returns. |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 70 | virtual bool Create(uint8_t* packet, |
| 71 | size_t* index, |
| 72 | size_t max_length, |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 73 | PacketReadyCallback callback) const = 0; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 74 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 75 | protected: |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 76 | // Size of the rtcp common header. |
| 77 | static constexpr size_t kHeaderLength = 4; |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 78 | RtcpPacket() {} |
| 79 | |
Danil Chapovalov | 6c17057 | 2017-09-15 16:48:14 +0200 | [diff] [blame] | 80 | static void CreateHeader(size_t count_or_format, |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 81 | uint8_t packet_type, |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 82 | size_t block_length, // Payload size in 32bit words. |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 83 | uint8_t* buffer, |
| 84 | size_t* pos); |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 85 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 86 | bool OnBufferFull(uint8_t* packet, |
| 87 | size_t* index, |
Danil Chapovalov | 5c3cc41 | 2017-12-07 10:15:53 +0100 | [diff] [blame] | 88 | PacketReadyCallback callback) const; |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 89 | // Size of the rtcp packet as written in header. |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 90 | size_t HeaderLength() const; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 91 | }; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 92 | } // namespace rtcp |
| 93 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 94 | #endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |