Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
builder.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_rtcp/builder.h
10 //! @brief RTCP packet builder.
11 
12 #ifndef ROC_RTCP_BUILDER_H_
13 #define ROC_RTCP_BUILDER_H_
14 
15 #include "roc_core/noncopyable.h"
16 #include "roc_core/stddefs.h"
17 #include "roc_packet/units.h"
18 #include "roc_rtcp/bye_traverser.h"
19 #include "roc_rtcp/config.h"
20 #include "roc_rtcp/headers.h"
21 #include "roc_rtcp/sdes.h"
22 
23 namespace roc {
24 namespace rtcp {
25 
26 //! RTCP compound packet builder.
27 //!
28 //! Builder will panic if any of the following rules is violated
29 //! (mandated by RFC 3550):
30 //! - At least one packet should be present.
31 //! - First packet should be SR or RR.
32 //! - SDES packet with CNAME item should be present.
33 //! - Each SDES chunk should have exactly one CNAME item.
34 //! - Padding can be added only to last packet.
35 //!
36 //! If the packet does not fit into resulting slice, builder will raise
37 //! error flags, and all its method will become no-op.
38 //!
39 //! Some of these rules may be disabled via config struct, which is used
40 //! in tests when we need to produce not strictly correct RTCP packets.
41 class Builder : public core::NonCopyable<> {
42 public:
43  //! Initialize builder.
44  //! It will write data to the given slice.
45  Builder(const Config& config, core::Slice<uint8_t>& result);
46  ~Builder();
47 
48  //! Check for errors.
49  //! @returns false if the packet did not fit into the slice.
50  bool is_ok() const;
51 
52  //! @name Sender Report (SR)
53  //! @{
54 
55  //! Start SR packet inside compound RTCP packet.
57 
58  //! Add reception report to current SR packet.
60 
61  //! Finish SR packet.
62  void end_sr();
63 
64  //! @}
65 
66  //! @name Receiver Report (RR)
67  //! @{
68 
69  //! Start RR packet inside compound RTCP packet.
71 
72  //! Add reception report to current RR packet.
74 
75  //! Finish RR packet.
76  void end_rr();
77 
78  //! @}
79 
80  //! @name Extended Report (XR)
81  //! @{
82 
83  //! Start XR packet inside compound RTCP packet.
84  void begin_xr(const header::XrPacket& xr);
85 
86  //! Add RRTR block to current XR packet.
87  void add_xr_rrtr(const header::XrRrtrBlock& rrtr);
88 
89  //! Start DLRR block inside current XR packet.
91 
92  //! Add DLRR report to current DLRR block.
94 
95  //! Add measurement info block.to current XR packet.
97 
98  //! Add delay metrics block.to current XR packet.
100 
101  //! Add queue metrics block.to current XR packet.
103 
104  //! Finish current DLRR block.
105  void end_xr_dlrr();
106 
107  //! Finish current XR packet.
108  void end_xr();
109 
110  //! @}
111 
112  //! @name Session Description (SDES)
113  //! @{
114 
115  //! Start SDES packet inside compound RTCP packet.
116  void begin_sdes();
117 
118  //! Start new SDES chunk in current SDES packet.
119  void begin_sdes_chunk(const SdesChunk& chunk);
120 
121  //! Add SDES item to current SDES chunk.
122  void add_sdes_item(const SdesItem& item);
123 
124  //! Finish current SDES chunk.
126 
127  //! Finish current SDES packet.
128  void end_sdes();
129 
130  //! @}
131 
132  //! @name Goodbye message (BYE)
133  //! @{
134 
135  //! Start BYE packet inside compound RTCP packet.
136  void begin_bye();
137 
138  //! Add SSRC to current BYE packet.
140 
141  //! Add REASON to current BYE packet.
142  void add_bye_reason(const char* reason);
143 
144  //! Finish current BYE packet.
145  void end_bye();
146 
147  //! @}
148 
149  //! @name Packet padding
150  //! @{
151 
152  //! Add given number of padding bytes to last packet.
153  //! Padding should be multiple of 4 in range [1; 255].
154  void add_padding(size_t padding_len);
155 
156  //! @}
157 
158 private:
159  header::PacketHeader* begin_packet_(size_t size);
160  void* add_block_(size_t size);
161  void end_packet_();
162 
163  enum State {
164  TOP,
165  SR_HEAD,
166  SR_REPORT,
167  RR_HEAD,
168  RR_REPORT,
169  XR_HEAD,
170  XR_DLRR_HEAD,
171  XR_DLRR_REPORT,
172  SDES_HEAD,
173  SDES_CHUNK,
174  BYE_HEAD,
175  BYE_SSRC,
176  BYE_REASON,
177  LAST
178  };
179 
180  State state_;
181 
182  core::Slice<uint8_t>& result_slice_;
183 
184  core::Slice<uint8_t> cur_pkt_slice_;
185  header::PacketHeader* cur_pkt_header_;
186  header::XrBlockHeader* cur_xr_block_header_;
187 
188  bool sr_written_;
189  bool rr_written_;
190  bool cname_written_;
191 
192  bool truncated_;
193 
194  const Config config_;
195 };
196 
197 } // namespace rtcp
198 } // namespace roc
199 
200 #endif // ROC_RTCP_BUILDER_H_
RTCP interface structures.
Base class for non-copyable objects.
Definition: noncopyable.h:23
RTCP compound packet builder.
Definition: builder.h:41
void end_sr()
Finish SR packet.
void begin_rr(const header::ReceiverReportPacket &rr)
Start RR packet inside compound RTCP packet.
void add_xr_queue_metrics(const header::XrQueueMetricsBlock &queue_metrics)
Add queue metrics block.to current XR packet.
void end_sdes()
Finish current SDES packet.
void add_xr_dlrr_report(const header::XrDlrrSubblock &report)
Add DLRR report to current DLRR block.
void end_rr()
Finish RR packet.
void end_sdes_chunk()
Finish current SDES chunk.
void end_xr_dlrr()
Finish current DLRR block.
void begin_bye()
Start BYE packet inside compound RTCP packet.
void end_bye()
Finish current BYE packet.
void add_padding(size_t padding_len)
Add given number of padding bytes to last packet. Padding should be multiple of 4 in range [1; 255].
void add_rr_report(const header::ReceptionReportBlock &report)
Add reception report to current RR packet.
void add_xr_measurement_info(const header::XrMeasurementInfoBlock &measurement_info)
Add measurement info block.to current XR packet.
void add_xr_delay_metrics(const header::XrDelayMetricsBlock &delay_metrics)
Add delay metrics block.to current XR packet.
void begin_xr_dlrr(const header::XrDlrrBlock &dlrr)
Start DLRR block inside current XR packet.
bool is_ok() const
Check for errors.
void add_bye_ssrc(const packet::stream_source_t ssrc)
Add SSRC to current BYE packet.
Builder(const Config &config, core::Slice< uint8_t > &result)
Initialize builder. It will write data to the given slice.
void begin_sr(const header::SenderReportPacket &sr)
Start SR packet inside compound RTCP packet.
void begin_sdes()
Start SDES packet inside compound RTCP packet.
void add_xr_rrtr(const header::XrRrtrBlock &rrtr)
Add RRTR block to current XR packet.
void add_sdes_item(const SdesItem &item)
Add SDES item to current SDES chunk.
void end_xr()
Finish current XR packet.
void begin_sdes_chunk(const SdesChunk &chunk)
Start new SDES chunk in current SDES packet.
void add_bye_reason(const char *reason)
Add REASON to current BYE packet.
void begin_xr(const header::XrPacket &xr)
Start XR packet inside compound RTCP packet.
void add_sr_report(const header::ReceptionReportBlock &report)
Add reception report to current SR packet.
RTCP packet header, common for all RTCP packet types.
Definition: headers.h:126
Receiver Report RTCP packet (RR).
Definition: headers.h:524
Reception report block.
Definition: headers.h:333
Sender Report RTCP packet (SR).
Definition: headers.h:621
XR Delay Metrics Block.
Definition: headers.h:1515
XR DLRR Report block.
Definition: headers.h:1303
XR DLRR Report sub-block.
Definition: headers.h:1228
XR Measurement Info Report Block.
Definition: headers.h:1373
RTCP Extended Report Packet.
Definition: headers.h:1035
XR Queue Metrics Block.
Definition: headers.h:1668
XR Receiver Reference Time Report block.
Definition: headers.h:1174
uint32_t stream_source_t
Packet stream identifier.
Definition: units.h:27
Root namespace.
Non-copyable object.
RTCP config.
RTCP headers.
SDES elements.
Commonly used types and functions.
RTCP config.
Definition: config.h:24
Parsed SDES chunk.
Definition: sdes.h:23
Parsed SDES item.
Definition: sdes.h:33
Various units used in packets.