blob: 5f7304e6c1834a12cc1bac59d0bfa1c33e05c6c9 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/basictypes.h"
15#include "rtc_base/buffer.h"
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000016
17namespace webrtc {
18namespace rtcp {
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000019// Class for building RTCP packets.
20//
21// Example:
22// ReportBlock report_block;
danilchap822a16f2016-09-27 09:27:47 -070023// report_block.SetMediaSsrc(234);
24// report_block.SetFractionLost(10);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000025//
26// ReceiverReport rr;
danilchap822a16f2016-09-27 09:27:47 -070027// rr.SetSenderSsrc(123);
28// rr.AddReportBlock(report_block);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000029//
30// Fir fir;
danilchap822a16f2016-09-27 09:27:47 -070031// fir.SetSenderSsrc(123);
32// fir.AddRequestTo(234, 56);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000033//
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000034// size_t length = 0; // Builds an intra frame request
Danil Chapovalov32e590e2016-01-22 11:04:56 +010035// uint8_t packet[kPacketSize]; // with sequence number 56.
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000036// fir.Build(packet, &length, kPacketSize);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000037//
danilchap69e59e62016-02-17 03:11:42 -080038// rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000039// // the built rtcp packet.
40//
danilchap7a4116a2016-03-14 08:19:28 -070041// 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.org0f2809a2014-02-21 08:14:45 +000045
46class RtcpPacket {
47 public:
Erik Språngc1b9d4e2015-06-08 09:54:14 +020048 // 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:
danilchapc1f40b72016-10-17 01:44:44 -070054 virtual void OnPacketReady(uint8_t* data, size_t length) = 0;
55
56 protected:
Erik Språngc1b9d4e2015-06-08 09:54:14 +020057 PacketReadyCallback() {}
58 virtual ~PacketReadyCallback() {}
Erik Språngc1b9d4e2015-06-08 09:54:14 +020059 };
60
danilchapc1f40b72016-10-17 01:44:44 -070061 virtual ~RtcpPacket() {}
Erik Språngc1b9d4e2015-06-08 09:54:14 +020062
danilchapc1f40b72016-10-17 01:44:44 -070063 // 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ångc1b9d4e2015-06-08 09:54:14 +020066
danilchap7a4116a2016-03-14 08:19:28 -070067 // Returns true if call to Create succeeded. Provided buffer reference
Erik Språngc1b9d4e2015-06-08 09:54:14 +020068 // 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.org0f2809a2014-02-21 08:14:45 +000072
danilchap7a4116a2016-03-14 08:19:28 -070073 // Size of this packet in bytes (including headers).
Erik Språng6b8d3552015-09-24 15:06:57 +020074 virtual size_t BlockLength() const = 0;
75
danilchap7a4116a2016-03-14 08:19:28 -070076 // 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ångc1b9d4e2015-06-08 09:54:14 +020079 virtual bool Create(uint8_t* packet,
80 size_t* index,
81 size_t max_length,
82 PacketReadyCallback* callback) const = 0;
83
danilchap7a4116a2016-03-14 08:19:28 -070084 protected:
danilchapc1f40b72016-10-17 01:44:44 -070085 // Size of the rtcp common header.
86 static constexpr size_t kHeaderLength = 4;
danilchap7a4116a2016-03-14 08:19:28 -070087 RtcpPacket() {}
88
sprang73a93e82015-09-14 12:50:39 -070089 static void CreateHeader(uint8_t count_or_format,
90 uint8_t packet_type,
danilchapc1f40b72016-10-17 01:44:44 -070091 size_t block_length, // Payload size in 32bit words.
sprang73a93e82015-09-14 12:50:39 -070092 uint8_t* buffer,
93 size_t* pos);
Erik Språnga3b87692015-07-29 10:46:54 +020094
Erik Språngc1b9d4e2015-06-08 09:54:14 +020095 bool OnBufferFull(uint8_t* packet,
96 size_t* index,
danilchapc1f40b72016-10-17 01:44:44 -070097 PacketReadyCallback* callback) const;
98 // Size of the rtcp packet as written in header.
Erik Språnga3b87692015-07-29 10:46:54 +020099 size_t HeaderLength() const;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000100};
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000101} // namespace rtcp
102} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200103#endif // MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_