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