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