blob: 07deb0f9bdf404d3c467ccbbe67e7f7b87b41b01 [file] [log] [blame]
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
12#define MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
15#include <stdint.h>
16
17#include "api/array_view.h"
Artem Titov741daaf2019-03-21 14:37:36 +010018#include "api/function_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/buffer.h"
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000020
21namespace webrtc {
22namespace rtcp {
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000023// Class for building RTCP packets.
24//
25// Example:
26// ReportBlock report_block;
danilchap822a16f2016-09-27 09:27:47 -070027// report_block.SetMediaSsrc(234);
28// report_block.SetFractionLost(10);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000029//
30// ReceiverReport rr;
danilchap822a16f2016-09-27 09:27:47 -070031// rr.SetSenderSsrc(123);
32// rr.AddReportBlock(report_block);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000033//
34// Fir fir;
danilchap822a16f2016-09-27 09:27:47 -070035// fir.SetSenderSsrc(123);
36// fir.AddRequestTo(234, 56);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000037//
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000038// size_t length = 0; // Builds an intra frame request
Danil Chapovalov32e590e2016-01-22 11:04:56 +010039// uint8_t packet[kPacketSize]; // with sequence number 56.
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000040// fir.Build(packet, &length, kPacketSize);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000041//
danilchap69e59e62016-02-17 03:11:42 -080042// rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000043// // the built rtcp packet.
44//
danilchap7a4116a2016-03-14 08:19:28 -070045// 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.org0f2809a2014-02-21 08:14:45 +000049
50class RtcpPacket {
51 public:
Erik Språngc1b9d4e2015-06-08 09:54:14 +020052 // 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 Chapovalov5c3cc412017-12-07 10:15:53 +010056 using PacketReadyCallback =
57 rtc::FunctionView<void(rtc::ArrayView<const uint8_t> packet)>;
Erik Språngc1b9d4e2015-06-08 09:54:14 +020058
Danil Chapovalovcbbfd082019-10-10 09:56:29 +020059 virtual ~RtcpPacket() = default;
60
61 void SetSenderSsrc(uint32_t ssrc) { sender_ssrc_ = ssrc; }
62 uint32_t sender_ssrc() const { return sender_ssrc_; }
Erik Språngc1b9d4e2015-06-08 09:54:14 +020063
danilchapc1f40b72016-10-17 01:44:44 -070064 // 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ångc1b9d4e2015-06-08 09:54:14 +020067
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010068 // Returns true if call to Create succeeded.
69 bool Build(size_t max_length, PacketReadyCallback callback) const;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000070
danilchap7a4116a2016-03-14 08:19:28 -070071 // Size of this packet in bytes (including headers).
Erik Språng6b8d3552015-09-24 15:06:57 +020072 virtual size_t BlockLength() const = 0;
73
danilchap7a4116a2016-03-14 08:19:28 -070074 // 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ångc1b9d4e2015-06-08 09:54:14 +020077 virtual bool Create(uint8_t* packet,
78 size_t* index,
79 size_t max_length,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +010080 PacketReadyCallback callback) const = 0;
Erik Språngc1b9d4e2015-06-08 09:54:14 +020081
danilchap7a4116a2016-03-14 08:19:28 -070082 protected:
danilchapc1f40b72016-10-17 01:44:44 -070083 // Size of the rtcp common header.
84 static constexpr size_t kHeaderLength = 4;
danilchap7a4116a2016-03-14 08:19:28 -070085 RtcpPacket() {}
86
Danil Chapovalov6c170572017-09-15 16:48:14 +020087 static void CreateHeader(size_t count_or_format,
sprang73a93e82015-09-14 12:50:39 -070088 uint8_t packet_type,
danilchapc1f40b72016-10-17 01:44:44 -070089 size_t block_length, // Payload size in 32bit words.
sprang73a93e82015-09-14 12:50:39 -070090 uint8_t* buffer,
91 size_t* pos);
Erik Språnga3b87692015-07-29 10:46:54 +020092
Johannes Kron99b91492019-02-12 10:51:18 +010093 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ångc1b9d4e2015-06-08 09:54:14 +0200100 bool OnBufferFull(uint8_t* packet,
101 size_t* index,
Danil Chapovalov5c3cc412017-12-07 10:15:53 +0100102 PacketReadyCallback callback) const;
danilchapc1f40b72016-10-17 01:44:44 -0700103 // Size of the rtcp packet as written in header.
Erik Språnga3b87692015-07-29 10:46:54 +0200104 size_t HeaderLength() const;
Danil Chapovalovcbbfd082019-10-10 09:56:29 +0200105
106 private:
107 uint32_t sender_ssrc_ = 0;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000108};
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000109} // namespace rtcp
110} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200111#endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_