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 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 
15 #include "roc_audio/ireader.h"
16 #include "roc_core/array.h"
17 #include "roc_core/iallocator.h"
18 #include "roc_core/noncopyable.h"
19 #include "roc_core/time.h"
20 #include "roc_packet/units.h"
21 
22 namespace roc {
23 namespace audio {
24 
25 //! Watchdog parameters.
27  //! Timeout for the lack of packets, nanoseconds.
28  //! @remarks
29  //! Maximum allowed period during which every frame is blank. After this period,
30  //! the session is terminated. This mechanism allows to detect dead, hanging, or
31  //! broken clients. Set to zero to disable.
33 
34  //! Timeout for frequent breakages, nanoseconds.
35  //! @remarks
36  //! Maximum allowed period during which every drop detection window overlaps with
37  //! at least one frame which caused packet drops and with at least one frame which
38  //! is incomplete (it may be the same frame). After this period, the session is
39  //! terminated. This mechanism allows to detect the vicious circle when all client
40  //! packets are a bit late and we are constantly dropping them producing unpleasant
41  //! noise. Set to zero to disable.
43 
44  //! Breakage detection window, nanoseconds.
45  //! @see broken_playback_timeout.
47 
48  //! Frame status window size for logging, number of frames.
49  //! @remarks
50  //! Used for debug logging. Set to zero to disable.
52 
53  //! Initialize config with default values.
55  : no_playback_timeout(2 * core::Second)
56  , broken_playback_timeout(2 * core::Second)
57  , breakage_detection_window(300 * core::Millisecond)
58  , frame_status_window(20) {
59  }
60 };
61 
62 //! Watchdog.
63 //! @remarks
64 //! Terminates session if it is considered dead or corrupted.
65 class Watchdog : public IReader, public core::NonCopyable<> {
66 public:
67  //! Initialize.
68  Watchdog(IReader& reader,
69  size_t num_channels,
70  const WatchdogConfig& config,
71  size_t sample_rate,
72  core::IAllocator& allocator);
73 
74  //! Check if object is successfully constructed.
75  bool valid() const;
76 
77  //! Read audio frame.
78  //! @remarks
79  //! Updates stream state and reads frame from the input reader.
80  virtual void read(Frame& frame);
81 
82  //! Update stream.
83  //! @returns
84  //! false if during the session timeout each frame has an empty flag or the maximum
85  //! allowed number of consecutive windows that can contain frames that aren't fully
86  //! filled and contain dropped packets was exceeded.
87  bool update();
88 
89 private:
90  void update_blank_timeout_(const Frame& frame, packet::timestamp_t next_read_pos);
91  bool check_blank_timeout_() const;
92 
93  void update_drops_timeout_(const Frame& frame, packet::timestamp_t next_read_pos);
94  bool check_drops_timeout_();
95 
96  void update_status_(const Frame& frame);
97  void flush_status_();
98 
99  IReader& reader_;
100 
101  const size_t num_channels_;
102 
103  const packet::timestamp_t max_blank_duration_;
104  const packet::timestamp_t max_drops_duration_;
105  const packet::timestamp_t drop_detection_window_;
106 
107  packet::timestamp_t curr_read_pos_;
108  packet::timestamp_t last_pos_before_blank_;
109  packet::timestamp_t last_pos_before_drops_;
110 
111  unsigned curr_window_flags_;
112 
113  core::Array<char> status_;
114  size_t status_pos_;
115  bool status_show_;
116 
117  bool alive_;
118  bool valid_;
119 };
120 
121 } // namespace audio
122 } // namespace roc
123 
124 #endif // ROC_AUDIO_WATCHDOG_H_
const nanoseconds_t Millisecond
One millisecond represented in nanoseconds.
Definition: time.h:30
Memory allocator interface.
Definition: iallocator.h:23
size_t frame_status_window
Frame status window size for logging, number of frames.
Definition: watchdog.h:51
Time definitions.
Root namespace.
Dynamic array.
Audio reader interface.
core::nanoseconds_t broken_playback_timeout
Timeout for frequent breakages, nanoseconds.
Definition: watchdog.h:42
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:21
Watchdog parameters.
Definition: watchdog.h:26
uint32_t timestamp_t
Audio packet timestamp.
Definition: units.h:46
core::nanoseconds_t no_playback_timeout
Timeout for the lack of packets, nanoseconds.
Definition: watchdog.h:32
Audio frame.
Definition: frame.h:22
Watchdog.
Definition: watchdog.h:65
WatchdogConfig()
Initialize config with default values.
Definition: watchdog.h:54
core::nanoseconds_t breakage_detection_window
Breakage detection window, nanoseconds.
Definition: watchdog.h:46
Base class for non-copyable objects.
Definition: noncopyable.h:23
Audio reader interface.
Definition: ireader.h:22
Various units used in packets.
const nanoseconds_t Second
One second represented in nanoseconds.
Definition: time.h:33
Non-copyable object.
Memory allocator interface.