Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
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
17#include "roc_core/array.h"
18#include "roc_core/iallocator.h"
20#include "roc_core/time.h"
21#include "roc_packet/units.h"
22
23namespace roc {
24namespace audio {
25
26//! Watchdog parameters.
28 //! Timeout for the lack of packets, nanoseconds.
29 //! @remarks
30 //! Maximum allowed period during which every frame is blank. After this period,
31 //! the session is terminated. This mechanism allows to detect dead, hanging, or
32 //! broken clients. Set to zero to disable.
34
35 //! Timeout for frequent breakages, nanoseconds.
36 //! @remarks
37 //! Maximum allowed period during which every drop detection window overlaps with
38 //! at least one frame which caused packet drops and with at least one frame which
39 //! is incomplete (it may be the same frame). After this period, the session is
40 //! terminated. This mechanism allows to detect the vicious circle when all client
41 //! packets are a bit late and we are constantly dropping them producing unpleasant
42 //! noise. Set to zero to disable.
44
45 //! Breakage detection window, nanoseconds.
46 //! @see broken_playback_timeout.
48
49 //! Frame status window size for logging, number of frames.
50 //! @remarks
51 //! Used for debug logging. Set to zero to disable.
53
54 //! Initialize config with default values.
56 : no_playback_timeout(2 * core::Second)
57 , broken_playback_timeout(2 * core::Second)
58 , breakage_detection_window(300 * core::Millisecond)
60 }
61};
62
63//! Watchdog.
64//! @remarks
65//! Terminates session if it is considered dead or corrupted.
66class Watchdog : public IFrameReader, public core::NonCopyable<> {
67public:
68 //! Initialize.
70 const audio::SampleSpec& sample_spec,
71 const WatchdogConfig& config,
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 bool 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
89private:
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 IFrameReader& reader_;
100
101 const audio::SampleSpec sample_spec_;
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_
Dynamic array.
Audio frame.
Definition frame.h:22
Frame reader interface.
Sample stream specification. Defines sample rate and channel layout.
Definition sample_spec.h:24
bool update()
Update stream.
virtual bool read(Frame &frame)
Read audio frame.
Watchdog(IFrameReader &reader, const audio::SampleSpec &sample_spec, const WatchdogConfig &config, core::IAllocator &allocator)
Initialize.
bool valid() const
Check if object is successfully constructed.
Dynamic array.
Definition array.h:38
Memory allocator interface.
Definition iallocator.h:23
Base class for non-copyable objects.
Definition noncopyable.h:23
Memory allocator interface.
Frame reader interface.
int64_t nanoseconds_t
Nanoseconds.
Definition time.h:58
uint32_t timestamp_t
Audio packet timestamp.
Definition units.h:25
Root namespace.
Non-copyable object.
Sample specifications.
Watchdog parameters.
Definition watchdog.h:27
WatchdogConfig()
Initialize config with default values.
Definition watchdog.h:55
core::nanoseconds_t breakage_detection_window
Breakage detection window, nanoseconds.
Definition watchdog.h:47
core::nanoseconds_t broken_playback_timeout
Timeout for frequent breakages, nanoseconds.
Definition watchdog.h:43
size_t frame_status_window
Frame status window size for logging, number of frames.
Definition watchdog.h:52
core::nanoseconds_t no_playback_timeout
Timeout for the lack of packets, nanoseconds.
Definition watchdog.h:33
Time definitions.
Various units used in packets.