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 | */ |
| 11 | |
danilchap | 5eb4988 | 2015-12-09 03:32:40 -0800 | [diff] [blame] | 12 | #ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |
| 13 | #define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 14 | |
| 15 | #include <vector> |
| 16 | |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 17 | #include "webrtc/base/buffer.h" |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 18 | #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 19 | #include "webrtc/typedefs.h" |
| 20 | |
| 21 | namespace webrtc { |
| 22 | namespace rtcp { |
| 23 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 24 | static const int kCommonFbFmtLength = 12; |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 25 | |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 26 | // Class for building RTCP packets. |
| 27 | // |
| 28 | // Example: |
| 29 | // ReportBlock report_block; |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 30 | // report_block.To(234); |
| 31 | // report_block.WithFractionLost(10); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 32 | // |
| 33 | // ReceiverReport rr; |
| 34 | // rr.From(123); |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 35 | // rr.WithReportBlock(report_block); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 36 | // |
| 37 | // Fir fir; |
| 38 | // fir.From(123); |
Danil Chapovalov | 32e590e | 2016-01-22 11:04:56 +0100 | [diff] [blame] | 39 | // fir.WithRequestTo(234, 56); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 40 | // |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 41 | // size_t length = 0; // Builds an intra frame request |
Danil Chapovalov | 32e590e | 2016-01-22 11:04:56 +0100 | [diff] [blame] | 42 | // uint8_t packet[kPacketSize]; // with sequence number 56. |
asapersson@webrtc.org | 4b12d40 | 2014-06-16 14:09:28 +0000 | [diff] [blame] | 43 | // fir.Build(packet, &length, kPacketSize); |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 44 | // |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 45 | // rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 46 | // // the built rtcp packet. |
| 47 | // |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 48 | // rr.Append(&fir); // Builds a compound RTCP packet with |
| 49 | // rtc::Buffer packet = rr.Build(); // a receiver report, report block |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 50 | // // and fir message. |
| 51 | |
| 52 | class RtcpPacket { |
| 53 | public: |
| 54 | virtual ~RtcpPacket() {} |
| 55 | |
| 56 | void Append(RtcpPacket* packet); |
| 57 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 58 | // Callback used to signal that an RTCP packet is ready. Note that this may |
| 59 | // not contain all data in this RtcpPacket; if a packet cannot fit in |
| 60 | // max_length bytes, it will be fragmented and multiple calls to this |
| 61 | // callback will be made. |
| 62 | class PacketReadyCallback { |
| 63 | public: |
| 64 | PacketReadyCallback() {} |
| 65 | virtual ~PacketReadyCallback() {} |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 66 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 67 | virtual void OnPacketReady(uint8_t* data, size_t length) = 0; |
| 68 | }; |
| 69 | |
| 70 | // Convenience method mostly used for test. Max length of IP_PACKET_SIZE is |
| 71 | // used, will cause assertion error if fragmentation occurs. |
danilchap | 69e59e6 | 2016-02-17 03:11:42 -0800 | [diff] [blame] | 72 | rtc::Buffer Build() const; |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 73 | |
| 74 | // Returns true if all calls to Create succeeded. A buffer of size |
| 75 | // IP_PACKET_SIZE will be allocated and reused between calls to callback. |
| 76 | bool Build(PacketReadyCallback* callback) const; |
| 77 | |
| 78 | // Returns true if all calls to Create succeeded. Provided buffer reference |
| 79 | // will be used for all calls to callback. |
| 80 | bool BuildExternalBuffer(uint8_t* buffer, |
| 81 | size_t max_length, |
| 82 | PacketReadyCallback* callback) const; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 83 | |
Erik Språng | 6b8d355 | 2015-09-24 15:06:57 +0200 | [diff] [blame] | 84 | // Size of this packet in bytes (including headers, excluding nested packets). |
| 85 | virtual size_t BlockLength() const = 0; |
| 86 | |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 87 | protected: |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 88 | RtcpPacket() {} |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 89 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 90 | virtual bool Create(uint8_t* packet, |
| 91 | size_t* index, |
| 92 | size_t max_length, |
| 93 | PacketReadyCallback* callback) const = 0; |
| 94 | |
sprang | 73a93e8 | 2015-09-14 12:50:39 -0700 | [diff] [blame] | 95 | static void CreateHeader(uint8_t count_or_format, |
| 96 | uint8_t packet_type, |
| 97 | size_t block_length, // Size in 32bit words - 1. |
| 98 | uint8_t* buffer, |
| 99 | size_t* pos); |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 100 | |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 101 | bool OnBufferFull(uint8_t* packet, |
| 102 | size_t* index, |
| 103 | RtcpPacket::PacketReadyCallback* callback) const; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 104 | |
Erik Språng | a3b8769 | 2015-07-29 10:46:54 +0200 | [diff] [blame] | 105 | size_t HeaderLength() const; |
| 106 | |
| 107 | static const size_t kHeaderLength = 4; |
Erik Språng | f7c5776 | 2015-12-04 10:40:35 +0100 | [diff] [blame] | 108 | std::vector<RtcpPacket*> appended_packets_; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 109 | |
| 110 | private: |
Erik Språng | c1b9d4e | 2015-06-08 09:54:14 +0200 | [diff] [blame] | 111 | bool CreateAndAddAppended(uint8_t* packet, |
| 112 | size_t* index, |
| 113 | size_t max_length, |
| 114 | PacketReadyCallback* callback) const; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 115 | }; |
asapersson@webrtc.org | 0f2809a | 2014-02-21 08:14:45 +0000 | [diff] [blame] | 116 | } // namespace rtcp |
| 117 | } // namespace webrtc |
danilchap | 5eb4988 | 2015-12-09 03:32:40 -0800 | [diff] [blame] | 118 | #endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_ |