blob: 33064aac1581019dcf965f067e86d66f8ddf7405 [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
Erik Språngc1b9d4e2015-06-08 09:54:14 +020017#include "webrtc/base/scoped_ptr.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 +000026class RawPacket;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000027
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 Chapovalov32e590e2016-01-22 11:04:56 +010041// fir.WithRequestTo(234, 56);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000042//
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000043// size_t length = 0; // Builds an intra frame request
Danil Chapovalov32e590e2016-01-22 11:04:56 +010044// uint8_t packet[kPacketSize]; // with sequence number 56.
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +000045// fir.Build(packet, &length, kPacketSize);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000046//
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
54class RtcpPacket {
55 public:
56 virtual ~RtcpPacket() {}
57
58 void Append(RtcpPacket* packet);
59
Erik Språngc1b9d4e2015-06-08 09:54:14 +020060 // 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.org0f2809a2014-02-21 08:14:45 +000068
Erik Språngc1b9d4e2015-06-08 09:54:14 +020069 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.org0f2809a2014-02-21 08:14:45 +000085
Erik Språng6b8d3552015-09-24 15:06:57 +020086 // Size of this packet in bytes (including headers, excluding nested packets).
87 virtual size_t BlockLength() const = 0;
88
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000089 protected:
Erik Språnga3b87692015-07-29 10:46:54 +020090 RtcpPacket() {}
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +000091
Erik Språngc1b9d4e2015-06-08 09:54:14 +020092 virtual bool Create(uint8_t* packet,
93 size_t* index,
94 size_t max_length,
95 PacketReadyCallback* callback) const = 0;
96
sprang73a93e82015-09-14 12:50:39 -070097 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ånga3b87692015-07-29 10:46:54 +0200102
Erik Språngc1b9d4e2015-06-08 09:54:14 +0200103 bool OnBufferFull(uint8_t* packet,
104 size_t* index,
105 RtcpPacket::PacketReadyCallback* callback) const;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000106
Erik Språnga3b87692015-07-29 10:46:54 +0200107 size_t HeaderLength() const;
108
109 static const size_t kHeaderLength = 4;
Erik Språngf7c57762015-12-04 10:40:35 +0100110 std::vector<RtcpPacket*> appended_packets_;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000111
112 private:
Erik Språngc1b9d4e2015-06-08 09:54:14 +0200113 bool CreateAndAddAppended(uint8_t* packet,
114 size_t* index,
115 size_t max_length,
116 PacketReadyCallback* callback) const;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000117};
118
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000119// Class holding a RTCP packet.
120//
121// Takes a built rtcp packet.
asapersson@webrtc.org4b12d402014-06-16 14:09:28 +0000122// RawPacket raw_packet(buffer, length);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000123//
124// To access the raw packet:
Erik Språngc1b9d4e2015-06-08 09:54:14 +0200125// raw_packet.Buffer(); - pointer to the raw packet
126// raw_packet.BufferLength(); - the length of the raw packet
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000127
128class RawPacket {
129 public:
Erik Språngc1b9d4e2015-06-08 09:54:14 +0200130 explicit RawPacket(size_t buffer_length);
131 RawPacket(const uint8_t* packet, size_t packet_length);
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000132
Erik Språngc1b9d4e2015-06-08 09:54:14 +0200133 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.org0f2809a2014-02-21 08:14:45 +0000138
139 private:
Erik Språngc1b9d4e2015-06-08 09:54:14 +0200140 const size_t buffer_length_;
141 size_t length_;
142 rtc::scoped_ptr<uint8_t[]> buffer_;
asapersson@webrtc.org0f2809a2014-02-21 08:14:45 +0000143};
144
145} // namespace rtcp
146} // namespace webrtc
danilchap5eb49882015-12-09 03:32:40 -0800147#endif // WEBRTC_MODULES_RTP_RTCP_SOURCE_RTCP_PACKET_H_