Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
latency_monitor.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 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/latency_monitor.h
10//! @brief Latency monitor.
11
12#ifndef ROC_AUDIO_LATENCY_MONITOR_H_
13#define ROC_AUDIO_LATENCY_MONITOR_H_
14
21#include "roc_core/time.h"
23#include "roc_packet/units.h"
24
25namespace roc {
26namespace audio {
27
28//! Parameters for latency monitor.
30 //! FreqEstimator update interval, nanoseconds.
31 //! How often to run FreqEstimator and update Resampler scaling.
33
34 //! Minimum allowed latency, nanoseconds.
35 //! If the latency goes out of bounds, the session is terminated.
37
38 //! Maximum allowed latency, nanoseconds.
39 //! If the latency goes out of bounds, the session is terminated.
41
42 //! Maximum allowed freq_coeff delta around one.
43 //! If the scaling goes out of bounds, it is trimmed.
44 //! For example, 0.01 allows freq_coeff values in range [0.99; 1.01].
46
48 : fe_update_interval(5 * core::Millisecond)
49 , min_latency(0)
50 , max_latency(0)
51 , max_scaling_delta(0.005f) {
52 }
53};
54
55//! Session latency monitor.
56//! - calculates session latency
57//! - calculates session scaling factor
58//! - trims scaling factor to the allowed range
59//! - updates resampler scaling
60//! - shutdowns session if the latency goes out of bounds
62public:
63 //! Constructor.
64 //!
65 //! @b Parameters
66 //! - @p queue and @p depacketizer are used to calculate the latency
67 //! - @p resampler is used to set the scaling factor, may be null
68 //! - @p config defines various miscellaneous parameters
69 //! - @p target_latency defines FreqEstimator target latency, in samples
70 //! - @p input_sample_spec is the sample spec of the input packets
71 //! - @p output_sample_spec is the sample spec of the output frames
73 const Depacketizer& depacketizer,
74 ResamplerReader* resampler,
75 const LatencyMonitorConfig& config,
76 core::nanoseconds_t target_latency,
77 const audio::SampleSpec& input_sample_spec,
78 const audio::SampleSpec& output_sample_spec,
79 const FreqEstimatorConfig& fe_config);
80
81 //! Check if the object was initialized successfully.
82 bool valid() const;
83
84 //! Update latency.
85 //! @returns
86 //! false if the session should be terminated.
88
89private:
90 bool get_latency_(packet::timestamp_diff_t& latency) const;
91 bool check_latency_(packet::timestamp_diff_t latency) const;
92
93 float trim_scaling_(float scaling) const;
94
95 bool init_resampler_(size_t input_sample_rate, size_t output_sample_rate);
96 bool update_resampler_(packet::timestamp_t time, packet::timestamp_t latency);
97
98 void report_latency_(packet::timestamp_diff_t latency);
99
100 const packet::SortedQueue& queue_;
101 const Depacketizer& depacketizer_;
102 ResamplerReader* resampler_;
103 FreqEstimator fe_;
104
105 core::RateLimiter rate_limiter_;
106
107 const packet::timestamp_t update_interval_;
108 packet::timestamp_t update_pos_;
109 bool has_update_pos_;
110
111 const packet::timestamp_t target_latency_;
112 const packet::timestamp_diff_t min_latency_;
113 const packet::timestamp_diff_t max_latency_;
114
115 const float max_scaling_delta_;
116
117 const audio::SampleSpec input_sample_spec_;
118 const audio::SampleSpec output_sample_spec_;
119
120 bool valid_;
121};
122
123} // namespace audio
124} // namespace roc
125
126#endif // ROC_AUDIO_LATENCY_MONITOR_H_
Evaluates sender's frequency to receivers's frequency ratio.
Session latency monitor.
bool valid() const
Check if the object was initialized successfully.
bool update(packet::timestamp_t time)
Update latency.
LatencyMonitor(const packet::SortedQueue &queue, const Depacketizer &depacketizer, ResamplerReader *resampler, const LatencyMonitorConfig &config, core::nanoseconds_t target_latency, const audio::SampleSpec &input_sample_spec, const audio::SampleSpec &output_sample_spec, const FreqEstimatorConfig &fe_config)
Constructor.
Resampler element for reading pipeline.
Sample stream specification. Defines sample rate and channel layout.
Definition: sample_spec.h:24
Base class for non-copyable objects.
Definition: noncopyable.h:23
Sorted packet queue.
Definition: sorted_queue.h:27
Depacketizer.
Frequency estimator.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
uint32_t timestamp_t
Audio packet timestamp.
Definition: units.h:25
int32_t timestamp_diff_t
Audio packet timestamps difference.
Definition: units.h:28
Root namespace.
Non-copyable object.
Rate limiter.
Sample specifications.
Sorted packet queue.
FreqEstimator tunable parameters.
Parameters for latency monitor.
float max_scaling_delta
Maximum allowed freq_coeff delta around one. If the scaling goes out of bounds, it is trimmed....
core::nanoseconds_t max_latency
Maximum allowed latency, nanoseconds. If the latency goes out of bounds, the session is terminated.
core::nanoseconds_t min_latency
Minimum allowed latency, nanoseconds. If the latency goes out of bounds, the session is terminated.
core::nanoseconds_t fe_update_interval
FreqEstimator update interval, nanoseconds. How often to run FreqEstimator and update Resampler scali...
Time definitions.
Various units used in packets.