Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
communicator.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/communicator.h
10 //! @brief RTCP communicator.
11 
12 #ifndef ROC_RTCP_COMMUNICATOR_H_
13 #define ROC_RTCP_COMMUNICATOR_H_
14 
15 #include "roc_core/iarena.h"
16 #include "roc_core/noncopyable.h"
17 #include "roc_core/rate_limiter.h"
18 #include "roc_core/stddefs.h"
19 #include "roc_core/time.h"
20 #include "roc_packet/icomposer.h"
21 #include "roc_packet/iwriter.h"
22 #include "roc_packet/packet.h"
24 #include "roc_rtcp/builder.h"
25 #include "roc_rtcp/config.h"
26 #include "roc_rtcp/iparticipant.h"
27 #include "roc_rtcp/reporter.h"
28 #include "roc_rtcp/traverser.h"
29 
30 namespace roc {
31 namespace rtcp {
32 
33 //! RTCP communicator.
34 //!
35 //! Implements bidirectional exchange of RTCP packets with other participants
36 //! of a single RTP session.
37 //!
38 //! Holds a reference to IParticipant interface, which is implemented by
39 //! sender or receiver pipeline.
40 //!
41 //! Features:
42 //!
43 //! - processes received RTCP packets, extract reports from packets,
44 //! and notifies IParticipant with reports from remote side
45 //!
46 //! - queries IParticipant with up-to-date reports from local
47 //! side, and generates RTCP packets to be sent to remote side
48 //!
49 //! For more details about streams and reports, @see IParticipant.
50 //!
51 //! This is top-level class of roc_rtcp module, gluing together other components:
52 //! - rtcp::Traverser, to iterate through blocks of compound RTCP packets
53 //! - rtcp::Builder, to construct compound RTCP packets
54 //! - rtcp::Reporter, to maintain hash table of active streams, process and generate
55 //! individual blocks of compound packets, and interact with IParticipant
56 class Communicator : public core::NonCopyable<> {
57 public:
58  //! Initialize.
59  Communicator(const Config& config,
60  IParticipant& participant,
61  packet::IWriter& packet_writer,
62  packet::IComposer& packet_composer,
63  packet::PacketFactory& packet_factory,
64  core::IArena& arena);
65 
66  //! Check if initialization succeeded.
67  bool is_valid() const;
68 
69  //! Get number of tracked destination addresses, for testing.
70  size_t total_destinations() const;
71 
72  //! Get number of tracked streams, for testing.
73  size_t total_streams() const;
74 
75  //! Parse and process incoming packet.
76  //! Invokes IParticipant methods during processing.
79 
80  //! When we should generate packets next time.
81  //! Returns absolute time.
82  //! @p current_time is current time in nanoseconds since Unix epoch.
84 
85  //! Generate and send report packet(s).
86  //! Should be called according to generation_deadline().
87  //! @p current_time is current time in nanoseconds since Unix epoch.
88  //! Invokes IParticipant methods during generation.
91 
92  //! Generate and send goodbye packet(s).
93  //! Should be called before termination sender session.
94  //! @p current_time is current time in nanoseconds since Unix epoch.
95  //! Invokes IParticipant methods during generation.
98 
99 private:
100  enum PacketType { PacketType_Reports, PacketType_Goodbye };
101 
102  void process_all_descriptions_(const Traverser& traverser);
103  void process_all_reports_(const Traverser& traverser);
104  void process_all_goodbyes_(const Traverser& traverser);
105 
106  void process_description_(const SdesTraverser& sdes);
107  void process_goodbye_(const ByeTraverser& bye);
108  void process_sender_report_(const header::SenderReportPacket& sr);
109  void process_receiver_report_(const header::ReceiverReportPacket& rr);
110  void process_extended_report_(const XrTraverser& xr);
111 
112  status::StatusCode generate_packets_(core::nanoseconds_t current_time,
113  PacketType packet_type);
114 
115  status::StatusCode begin_packet_generation_(core::nanoseconds_t current_time);
116  status::StatusCode end_packet_generation_();
117  bool continue_packet_generation_();
118  status::StatusCode write_generated_packet_(const packet::PacketPtr& packet);
119 
120  bool next_send_stream_(size_t new_stream_index);
121  bool next_recv_stream_(size_t new_stream_index);
122 
123  status::StatusCode generate_packet_(PacketType packet_type,
124  packet::PacketPtr& packet);
125 
126  status::StatusCode generate_packet_payload_(PacketType packet_type,
127  core::Slice<uint8_t>& packet_payload);
128 
129  void generate_reports_payload_(Builder& bld);
130  void generate_goodbye_payload_(Builder& bld);
131 
132  void generate_standard_report_(Builder& bld);
133  void generate_extended_report_(Builder& bld);
134  void generate_empty_report_(Builder& bld);
135  void generate_description_(Builder& bld);
136  void generate_goodbye_(Builder& bld);
137 
138  void log_stats_();
139 
140  packet::PacketFactory& packet_factory_;
141 
142  packet::IWriter& packet_writer_;
143  packet::IComposer& packet_composer_;
144 
145  const Config config_;
146  Reporter reporter_;
147 
148  // When generation_deadline() should be called next time.
149  core::nanoseconds_t next_deadline_;
150 
151  size_t dest_addr_count_; // Total count of destination addresses.
152  size_t dest_addr_index_; // Index of current destination address.
153 
154  size_t send_stream_count_; // Total count of sending stream reports.
155  size_t send_stream_index_; // Index of current sending stream report.
156  size_t recv_stream_count_; // Total count of receiving stream reports.
157  size_t recv_stream_index_; // Index of current receiving stream report.
158 
159  // Maximum number of sending and receiving stream reports per single packet,
160  // and number of current sending and receiving stream report inside packet.
161  size_t max_pkt_streams_;
162  size_t cur_pkt_send_stream_;
163  size_t cur_pkt_recv_stream_;
164 
165  // Statistics.
166  size_t error_count_;
167  size_t processed_packet_count_;
168  size_t generated_packet_count_;
169  core::RateLimiter log_limiter_;
170 
171  bool valid_;
172 };
173 
174 } // namespace rtcp
175 } // namespace roc
176 
177 #endif // ROC_RTCP_COMMUNICATOR_H_
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
RTCP packet builder.
Memory arena interface.
Definition: iarena.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Packet composer interface.
Definition: icomposer.h:22
Packet writer interface.
Definition: iwriter.h:23
RTCP compound packet builder.
Definition: builder.h:41
BYE packet traverer.
Definition: bye_traverser.h:25
RTCP communicator.
Definition: communicator.h:56
core::nanoseconds_t generation_deadline(core::nanoseconds_t current_time)
When we should generate packets next time. Returns absolute time. current_time is current time in nan...
ROC_ATTR_NODISCARD status::StatusCode generate_goodbye(core::nanoseconds_t current_time)
Generate and send goodbye packet(s). Should be called before termination sender session....
Communicator(const Config &config, IParticipant &participant, packet::IWriter &packet_writer, packet::IComposer &packet_composer, packet::PacketFactory &packet_factory, core::IArena &arena)
Initialize.
bool is_valid() const
Check if initialization succeeded.
size_t total_streams() const
Get number of tracked streams, for testing.
size_t total_destinations() const
Get number of tracked destination addresses, for testing.
ROC_ATTR_NODISCARD status::StatusCode process_packet(const packet::PacketPtr &packet, core::nanoseconds_t current_time)
Parse and process incoming packet. Invokes IParticipant methods during processing.
ROC_ATTR_NODISCARD status::StatusCode generate_reports(core::nanoseconds_t current_time)
Generate and send report packet(s). Should be called according to generation_deadline()....
RTCP participant.
Definition: iparticipant.h:49
RTCP report processor and generator.
Definition: reporter.h:90
SDES packet traverer.
RTCP compound packet traverser.
Definition: traverser.h:24
XR packet traverser.
Definition: xr_traverser.h:23
Receiver Report RTCP packet (RR).
Definition: headers.h:524
Sender Report RTCP packet (SR).
Definition: headers.h:621
Memory arena interface.
Packet composer interface.
RTCP participant.
Packet writer interface.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Packet.
Packet factory.
Rate limiter.
RTCP reporter.
RTCP config.
StatusCode
Status code.
Definition: status_code.h:19
Commonly used types and functions.
RTCP config.
Definition: config.h:24
Time definitions.
RTCP packet traverser.