blob: fef7258b5a8874e3aaf5f03e382ecc737e70279c [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
15#include <vector>
16
danilchap69e59e62016-02-17 03:11:42 -080017#include "webrtc/base/buffer.h"
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000018#include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h"
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000019#include "webrtc/typedefs.h"
20
21namespace webrtc {
22namespace rtcp {
23
Erik Språngc1b9d4e2015-06-08 09:54:14 +020024static const int kCommonFbFmtLength = 12;
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000025
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000026// Class for building RTCP packets.
27//
28// Example:
29// ReportBlock report_block;
danilchap69e59e62016-02-17 03:11:42 -080030// report_block.To(234);
31// report_block.WithFractionLost(10);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000032//
33// ReceiverReport rr;
34// rr.From(123);
danilchap69e59e62016-02-17 03:11:42 -080035// rr.WithReportBlock(report_block);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000036//
37// Fir fir;
38// fir.From(123);
Danil Chapovalov32e590e2016-01-22 11:04:56 +010039// fir.WithRequestTo(234, 56);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000040//
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000041// size_t length = 0; // Builds an intra frame request
Danil Chapovalov32e590e2016-01-22 11:04:56 +010042// uint8_t packet[kPacketSize]; // with sequence number 56.
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000043// fir.Build(packet, &length, kPacketSize);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000044//
danilchap69e59e62016-02-17 03:11:42 -080045// rtc::Buffer packet = fir.Build(); // Returns a RawPacket holding
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000046// // the built rtcp packet.
47//
danilchap69e59e62016-02-17 03:11:42 -080048// rr.Append(&fir); // Builds a compound RTCP packet with
49// rtc::Buffer packet = rr.Build(); // a receiver report, report block
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000050// // and fir message.
51
52class RtcpPacket {
53 public:
54 virtual ~RtcpPacket() {}
55
56 void Append(RtcpPacket* packet);
57
Erik Språngc1b9d4e2015-06-08 09:54:14 +020058 // 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.org0f2809a2014-02-21 08:14:45 +000066
Erik Språngc1b9d4e2015-06-08 09:54:14 +020067 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.
danilchap69e59e62016-02-17 03:11:42 -080072 rtc::Buffer Build() const;
Erik Språngc1b9d4e2015-06-08 09:54:14 +020073
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.org0f2809a2014-02-21 08:14:45 +000083
Erik Språng6b8d3552015-09-24 15:06:57 +020084 // Size of this packet in bytes (including headers, excluding nested packets).
85 virtual size_t BlockLength() const = 0;
86
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000087 protected:
Erik Språnga3b87692015-07-29 10:46:54 +020088 RtcpPacket() {}
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000089
Erik Språngc1b9d4e2015-06-08 09:54:14 +020090 virtual bool Create(uint8_t* packet,
91 size_t* index,
92 size_t max_length,
93 PacketReadyCallback* callback) const = 0;
94
sprang73a93e82015-09-14 12:50:39 -070095 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ånga3b87692015-07-29 10:46:54 +0200100
Erik Språngc1b9d4e2015-06-08 09:54:14 +0200101 bool OnBufferFull(uint8_t* packet,
102 size_t* index,
103 RtcpPacket::PacketReadyCallback* callback) const;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000104
Erik Språnga3b87692015-07-29 10:46:54 +0200105 size_t HeaderLength() const;
106
107 static const size_t kHeaderLength = 4;
Erik Språngf7c57762015-12-04 10:40:35 +0100108 std::vector<RtcpPacket*> appended_packets_;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000109
110 private:
Erik Språngc1b9d4e2015-06-08 09:54:14 +0200111 bool CreateAndAddAppended(uint8_t* packet,
112 size_t* index,
113 size_t max_length,
114 PacketReadyCallback* callback) const;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000115};
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000116} // namespace rtcp
117} // namespace webrtc
danilchap5eb49882015-12-09 03:32:40 -0800118#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_