Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
feedback_monitor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 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_audio/feedback_monitor.h
10 //! @brief Feedback monitor.
11 
12 #ifndef ROC_AUDIO_FEEDBACK_MONITOR_H_
13 #define ROC_AUDIO_FEEDBACK_MONITOR_H_
14 
17 #include "roc_audio/packetizer.h"
19 #include "roc_audio/sample_spec.h"
20 #include "roc_core/noncopyable.h"
21 #include "roc_core/rate_limiter.h"
22 #include "roc_core/time.h"
23 #include "roc_packet/ilink_meter.h"
24 
25 namespace roc {
26 namespace audio {
27 
28 //! Feedback monitor configuration.
30  //! Timeout for source feedback.
31  //! If there is no new feedback during timeout, feedback monitor resets state.
33 
34  //! Cooldown period between source changes.
35  //! After source is change, another source change is now allowed during
36  //! this period and is ignored.
38 
40  : source_timeout(1500 * core::Millisecond)
41  , source_cooldown(50 * core::Millisecond) {
42  }
43 };
44 
45 //! Feedback monitor.
46 //!
47 //! @b Features
48 //!
49 //! - handles latency metrics from receiver (obtained via RTCP)
50 //! - asks LatencyTuner to calculate scaling factor based on the actual and
51 //! target latencies
52 //! - passes calculated scaling factor to resampler
53 //!
54 //! @b Flow
55 //!
56 //! - when pipeline obtains RTCP report, it calls write_metrics() method
57 //! - pipeline periodically calls write() method; it passes latest metrics
58 //! to LatencyTuner, and obtains scaling factor for resampler
59 //! - feedback monitor has a reference to resampler, and periodically passes
60 //! updated scaling factor to it
61 //! - pipeline also can query feedback monitor for latency metrics on behalf of
62 //! request from user
64 public:
65  //! Constructor.
67  Packetizer& packetizer,
68  ResamplerWriter* resampler,
69  const FeedbackConfig& feedback_config,
70  const LatencyConfig& latency_config,
71  const SampleSpec& sample_spec);
72 
73  //! Check if the object was initialized successfully.
74  bool is_valid() const;
75 
76  //! Check if feedback monitoring is started.
77  bool is_started() const;
78 
79  //! Enable feedback monitoring.
80  void start();
81 
82  //! Process feedback from receiver.
86 
87  //! Write audio frame.
88  //! Passes frame to underlying writer.
89  //! If feedback monitoring is started, also performs latency tuning.
90  virtual void write(Frame& frame);
91 
92  //! Get number of remote participants from which there is feedback.
93  size_t num_participants() const;
94 
95  //! Get latest latency metrics for session.
96  //! @p party_index should be in range [0; num_participants()-1].
97  const LatencyMetrics& latency_metrics(size_t party_index) const;
98 
99  //! Get latest link metrics for session.
100  //! @p party_index should be in range [0; num_participants()-1].
101  const packet::LinkMetrics& link_metrics(size_t party_index) const;
102 
103 private:
104  bool update_tuner_(packet::stream_timestamp_t duration);
105 
106  bool init_scaling_();
107  bool update_scaling_();
108 
109  LatencyTuner tuner_;
110 
111  LatencyMetrics latency_metrics_;
112  packet::LinkMetrics link_metrics_;
113  bool use_packetizer_;
114 
115  bool has_feedback_;
116  core::nanoseconds_t last_feedback_ts_;
117  const core::nanoseconds_t feedback_timeout_;
118 
119  Packetizer& packetizer_;
120  IFrameWriter& writer_;
121 
122  ResamplerWriter* resampler_;
123  const bool enable_scaling_;
124 
125  packet::stream_source_t source_;
126  core::RateLimiter source_change_limiter_;
127 
128  const SampleSpec sample_spec_;
129 
130  bool started_;
131  bool valid_;
132 };
133 
134 } // namespace audio
135 } // namespace roc
136 
137 #endif // ROC_AUDIO_FEEDBACK_MONITOR_H_
void process_feedback(packet::stream_source_t source_id, const LatencyMetrics &latency_metrics, const packet::LinkMetrics &link_metrics)
Process feedback from receiver.
bool is_started() const
Check if feedback monitoring is started.
void start()
Enable feedback monitoring.
bool is_valid() const
Check if the object was initialized successfully.
virtual void write(Frame &frame)
Write audio frame. Passes frame to underlying writer. If feedback monitoring is started,...
size_t num_participants() const
Get number of remote participants from which there is feedback.
FeedbackMonitor(IFrameWriter &writer, Packetizer &packetizer, ResamplerWriter *resampler, const FeedbackConfig &feedback_config, const LatencyConfig &latency_config, const SampleSpec &sample_spec)
Constructor.
const LatencyMetrics & latency_metrics(size_t party_index) const
Get latest latency metrics for session. party_index should be in range [0; num_participants()-1].
const packet::LinkMetrics & link_metrics(size_t party_index) const
Get latest link metrics for session. party_index should be in range [0; num_participants()-1].
Audio frame.
Definition: frame.h:25
Frame writer interface.
Definition: iframe_writer.h:22
Resampler element for writing pipeline.
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:30
Base class for non-copyable objects.
Definition: noncopyable.h:23
Frame writer interface.
Latency tuner.
const nanoseconds_t Millisecond
One millisecond represented in nanoseconds.
Definition: time.h:67
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
uint32_t stream_source_t
Packet stream identifier.
Definition: units.h:27
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition: units.h:36
Root namespace.
Non-copyable object.
Packetizer.
Rate limiter.
Sample specifications.
Feedback monitor configuration.
core::nanoseconds_t source_cooldown
Cooldown period between source changes. After source is change, another source change is now allowed ...
core::nanoseconds_t source_timeout
Timeout for source feedback. If there is no new feedback during timeout, feedback monitor resets stat...
Latency settings.
Definition: latency_tuner.h:64
Latency-related metrics.
Time definitions.