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 | */ |
danilchap | 5eb4988 | 2015-12-09 03:32:40 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |
| 12 | #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 13 | |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 14 | #include "webrtc/rtc_base/basictypes.h" |
| 15 | #include "webrtc/rtc_base/buffer.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. |
| 52 | class PacketReadyCallback { |
| 53 | public: |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 54 | virtual void OnPacketReady(uint8_t* data, size_t length) = 0; |
| 55 | |
| 56 | protected: |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 57 | PacketReadyCallback() {} |
| 58 | virtual ~PacketReadyCallback() {} |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 61 | virtual ~RtcpPacket() {} |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 62 | |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 63 | // Convenience method mostly used for test. Creates packet without |
| 64 | // fragmentation using BlockLength() to allocate big enough buffer. |
| 65 | rtc::Buffer Build() const; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 66 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 67 | // Returns true if call to Create succeeded. Provided buffer reference |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 68 | // will be used for all calls to callback. |
| 69 | bool BuildExternalBuffer(uint8_t* buffer, |
| 70 | size_t max_length, |
| 71 | PacketReadyCallback* callback) const; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 72 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 73 | // Size of this packet in bytes (including headers). |
Erik Språng | 6b8d355 | 2015-09-24 15:06:57 +0200 | [diff] [blame] | 74 | virtual size_t BlockLength() const = 0; |
| 75 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 76 | // Creates packet in the given buffer at the given position. |
| 77 | // Calls PacketReadyCallback::OnPacketReady if remaining buffer is too small |
| 78 | // and assume buffer can be reused after OnPacketReady returns. |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 79 | virtual bool Create(uint8_t* packet, |
| 80 | size_t* index, |
| 81 | size_t max_length, |
| 82 | PacketReadyCallback* callback) const = 0; |
| 83 | |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 84 | protected: |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 85 | // Size of the rtcp common header. |
| 86 | static constexpr size_t kHeaderLength = 4; |
danilchap | 7a4116a | 2016-03-14 08:19:28 -0700 | [diff] [blame] | 87 | RtcpPacket() {} |
| 88 | |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 89 | static void CreateHeader(uint8_t count_or_format, |
| 90 | uint8_t packet_type, |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 91 | size_t block_length, // Payload size in 32bit words. |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 92 | uint8_t* buffer, |
| 93 | size_t* pos); |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 94 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 95 | bool OnBufferFull(uint8_t* packet, |
| 96 | size_t* index, |
danilchap | c1f40b7 | 2016-10-17 01:44:44 -0700 | [diff] [blame] | 97 | PacketReadyCallback* callback) const; |
| 98 | // Size of the rtcp packet as written in header. |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 99 | size_t HeaderLength() const; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 100 | }; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 101 | } // namespace rtcp |
| 102 | } // namespace webrtc |
danilchap | 5eb4988 | 2015-12-09 03:32:40 -0800 | [diff] [blame] | 103 | #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |