blob: 9443b6d3cb4dc4cab984d2964129b4f3c54f4c75 [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 */
11
danilchap5eb49882015-12-09 03:32:40 -080012#ifndef WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
13#define WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000014
danilchap69e59e62016-02-17 03:11:42 -080015#include "webrtc/base/buffer.h"
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000016#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000017
18namespace webrtc {
19namespace rtcp {
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000020// Class for building RTCP packets.
21//
22// Example:
23// ReportBlock report_block;
danilchap822a16f2016-09-27 09:27:47 -070024// report_block.SetMediaSsrc(234);
25// report_block.SetFractionLost(10);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000026//
27// ReceiverReport rr;
danilchap822a16f2016-09-27 09:27:47 -070028// rr.SetSenderSsrc(123);
29// rr.AddReportBlock(report_block);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000030//
31// Fir fir;
danilchap822a16f2016-09-27 09:27:47 -070032// fir.SetSenderSsrc(123);
33// fir.AddRequestTo(234, 56);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000034//
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000035// size_t length = 0; // Builds an intra frame request
Danil Chapovalov32e590e2016-01-22 11:04:56 +010036// uint8_t packet[kPacketSize]; // with sequence number 56.
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000037// fir.Build(packet, &length, kPacketSize);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000038//
danilchap69e59e62016-02-17 03:11:42 -080039// rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000040// // the built rtcp packet.
41//
danilchap7a4116a2016-03-14 08:19:28 -070042// CompoundPacket compound; // Builds a compound RTCP packet with
43// compound.Append(&rr); // a receiver report, report block
44// compound.Append(&fir); // and fir message.
45// rtc::Buffer packet = compound.Build();
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000046
47class RtcpPacket {
48 public:
49 virtual ~RtcpPacket() {}
50
Erik Språngc1b9d4e2015-06-08 09:54:14 +020051 // Callback used to signal that an RTCP packet is ready. Note that this may
52 // not contain all data in this RtcpPacket; if a packet cannot fit in
53 // max_length bytes, it will be fragmented and multiple calls to this
54 // callback will be made.
55 class PacketReadyCallback {
56 public:
57 PacketReadyCallback() {}
58 virtual ~PacketReadyCallback() {}
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000059
Erik Språngc1b9d4e2015-06-08 09:54:14 +020060 virtual void OnPacketReady(uint8_t* data, size_t length) = 0;
61 };
62
63 // Convenience method mostly used for test. Max length of IP_PACKET_SIZE is
64 // used, will cause assertion error if fragmentation occurs.
danilchap69e59e62016-02-17 03:11:42 -080065 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. A buffer of size
Erik Språngc1b9d4e2015-06-08 09:54:14 +020068 // IP_PACKET_SIZE will be allocated and reused between calls to callback.
69 bool Build(PacketReadyCallback* callback) const;
70
danilchap7a4116a2016-03-14 08:19:28 -070071 // Returns true if call to Create succeeded. Provided buffer reference
Erik Språngc1b9d4e2015-06-08 09:54:14 +020072 // will be used for all calls to callback.
73 bool BuildExternalBuffer(uint8_t* buffer,
74 size_t max_length,
75 PacketReadyCallback* callback) const;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000076
danilchap7a4116a2016-03-14 08:19:28 -070077 // Size of this packet in bytes (including headers).
Erik Språng6b8d3552015-09-24 15:06:57 +020078 virtual size_t BlockLength() const = 0;
79
danilchap7a4116a2016-03-14 08:19:28 -070080 // Creates packet in the given buffer at the given position.
81 // Calls PacketReadyCallback::OnPacketReady if remaining buffer is too small
82 // and assume buffer can be reused after OnPacketReady returns.
Erik Språngc1b9d4e2015-06-08 09:54:14 +020083 virtual bool Create(uint8_t* packet,
84 size_t* index,
85 size_t max_length,
86 PacketReadyCallback* callback) const = 0;
87
danilchap7a4116a2016-03-14 08:19:28 -070088 protected:
89 RtcpPacket() {}
90
sprang73a93e82015-09-14 12:50:39 -070091 static void CreateHeader(uint8_t count_or_format,
92 uint8_t packet_type,
93 size_t block_length, // Size in 32bit words - 1.
94 uint8_t* buffer,
95 size_t* pos);
Erik Språnga3b87692015-07-29 10:46:54 +020096
Erik Språngc1b9d4e2015-06-08 09:54:14 +020097 bool OnBufferFull(uint8_t* packet,
98 size_t* index,
99 RtcpPacket::PacketReadyCallback* callback) const;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000100
Erik Språnga3b87692015-07-29 10:46:54 +0200101 size_t HeaderLength() const;
102
103 static const size_t kHeaderLength = 4;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000104};
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000105} // namespace rtcp
106} // namespace webrtc
danilchap5eb49882015-12-09 03:32:40 -0800107#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_