Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
watchdog.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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/watchdog.h
10 //! @brief Watchdog.
11 
12 #ifndef ROC_AUDIO_WATCHDOG_H_
13 #define ROC_AUDIO_WATCHDOG_H_
14 
16 #include "roc_audio/sample_spec.h"
17 #include "roc_core/array.h"
18 #include "roc_core/attributes.h"
19 #include "roc_core/iarena.h"
20 #include "roc_core/noncopyable.h"
21 #include "roc_core/time.h"
22 #include "roc_packet/units.h"
23 
24 namespace roc {
25 namespace audio {
26 
27 //! Watchdog parameters.
29  //! Timeout for the lack of packets, nanoseconds.
30  //! @remarks
31  //! Maximum allowed period during which every frame is blank. After this period,
32  //! the session is terminated. This mechanism allows to detect dead, hanging, or
33  //! broken clients.
34  //! @note
35  //! If zero, default value is used.
36  //! If negative, the check is disabled.
38 
39  //! Timeout for frequent stuttering, nanoseconds.
40  //! @remarks
41  //! Maximum allowed period during which every drop detection window overlaps with
42  //! at least one frame which caused packet drops and with at least one frame which
43  //! is incomplete (it may be the same frame). After this period, the session is
44  //! terminated. This mechanism allows to detect the vicious circle when all client
45  //! packets are a bit late and we are constantly dropping them producing unpleasant
46  //! noise.
47  //! @note
48  //! If zero, default value is used.
49  //! If negative, the check is disabled.
51 
52  //! Window size of detecting stuttering, nanoseconds.
53  //! @see choppy_playback_timeout
54  //! @note
55  //! If zero, default value is used.
57 
58  //! Duration of the warmup phase in the beginning, nanoseconds
59  //! @remarks
60  //! During the warmup phase blank_timeout is not triggered. After this period last
61  //! position before blank frames is set to the current position. Warmup can also
62  //! be terminated in case a non-blank frame occurs during it. This mechanism allows
63  //! watchdog to work with latency longer than no_playback_timeout. Usually is equal
64  //! to target_latency.
65  //! @note
66  //! If zero, default value is used.
67  //! If negative, warmup phase is disabled.
69 
70  //! Frame status window size for logging, number of frames.
71  //! @remarks
72  //! Used for debug logging. Set to zero to disable.
73  //! @note
74  //! If zero, default value is used.
76 
77  //! Initialize config with default values.
82  , warmup_duration(0)
83  , frame_status_window(0) {
84  }
85 
86  //! Automatically fill missing settings.
87  void deduce_defaults(const core::nanoseconds_t target_latency);
88 };
89 
90 //! Watchdog.
91 //! @remarks
92 //! Terminates session if it is considered dead or corrupted.
93 class Watchdog : public IFrameReader, public core::NonCopyable<> {
94 public:
95  //! Initialize.
97  const SampleSpec& sample_spec,
98  const WatchdogConfig& config,
99  core::IArena& arena);
100 
101  //! Check if object is successfully constructed.
102  bool is_valid() const;
103 
104  //! Check if stream is still alive.
105  //! @returns
106  //! false if during the session timeout each frame has an empty flag or the maximum
107  //! allowed number of consecutive windows that can contain frames that aren't fully
108  //! filled and contain dropped packets was exceeded.
109  bool is_alive() const;
110 
111  //! Read audio frame.
112  //! @remarks
113  //! Updates stream state and reads frame from the input reader.
114  virtual bool read(Frame& frame);
115 
116 private:
117  void update_blank_timeout_(const Frame& frame,
118  packet::stream_timestamp_t next_read_pos);
119  bool check_blank_timeout_() const;
120 
121  void update_drops_timeout_(const Frame& frame,
122  packet::stream_timestamp_t next_read_pos);
123  bool check_drops_timeout_();
124 
125  void update_warmup_();
126 
127  void update_status_(const Frame& frame);
128  void flush_status_();
129 
130  IFrameReader& reader_;
131 
132  const SampleSpec sample_spec_;
133 
134  packet::stream_timestamp_t max_blank_duration_;
135  packet::stream_timestamp_t max_drops_duration_;
136  packet::stream_timestamp_t drops_detection_window_;
137 
138  packet::stream_timestamp_t curr_read_pos_;
139  packet::stream_timestamp_t last_pos_before_blank_;
140  packet::stream_timestamp_t last_pos_before_drops_;
141 
142  packet::stream_timestamp_t warmup_duration_;
143  bool in_warmup_;
144 
145  unsigned curr_window_flags_;
146 
147  core::Array<char> status_;
148  size_t status_pos_;
149  bool show_status_;
150 
151  bool alive_;
152  bool valid_;
153 };
154 
155 } // namespace audio
156 } // namespace roc
157 
158 #endif // ROC_AUDIO_WATCHDOG_H_
Dynamic array.
Compiler attributes.
Audio frame.
Definition: frame.h:25
Frame reader interface.
Definition: iframe_reader.h:22
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:30
virtual bool read(Frame &frame)
Read audio frame.
bool is_valid() const
Check if object is successfully constructed.
bool is_alive() const
Check if stream is still alive.
Watchdog(IFrameReader &reader, const SampleSpec &sample_spec, const WatchdogConfig &config, core::IArena &arena)
Initialize.
Memory arena interface.
Definition: iarena.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Memory arena interface.
Frame reader interface.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
uint32_t stream_timestamp_t
Packet stream timestamp.
Definition: units.h:36
Root namespace.
Non-copyable object.
Sample specifications.
Watchdog parameters.
Definition: watchdog.h:28
core::nanoseconds_t choppy_playback_window
Window size of detecting stuttering, nanoseconds.
Definition: watchdog.h:56
WatchdogConfig()
Initialize config with default values.
Definition: watchdog.h:78
void deduce_defaults(const core::nanoseconds_t target_latency)
Automatically fill missing settings.
core::nanoseconds_t choppy_playback_timeout
Timeout for frequent stuttering, nanoseconds.
Definition: watchdog.h:50
size_t frame_status_window
Frame status window size for logging, number of frames.
Definition: watchdog.h:75
core::nanoseconds_t no_playback_timeout
Timeout for the lack of packets, nanoseconds.
Definition: watchdog.h:37
core::nanoseconds_t warmup_duration
Duration of the warmup phase in the beginning, nanoseconds.
Definition: watchdog.h:68
Time definitions.
Various units used in packets.