srs-control 0.2.2
Loading...
Searching...
No Matches
FrameMissMonitor.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <algorithm>
6#include <asio/any_io_executor.hpp>
7#include <asio/awaitable.hpp>
8#include <asio/co_spawn.hpp>
9#include <asio/detached.hpp>
10#include <asio/impl/co_spawn.hpp>
11#include <asio/steady_timer.hpp>
12#include <asio/this_coro.hpp>
13#include <atomic>
14#include <chrono>
15#include <cstddef>
16#include <cstdint>
17#include <functional>
18#include <gsl/gsl-lite.hpp>
19#include <spdlog/spdlog.h>
20#include <vector>
21
22namespace srs::workflow
23{
25 {
26 public:
27 struct Config
28 {
29 std::size_t sample_size{};
31 };
32 explicit FrameMissMonitor(const Config& config, const sink::FrameCountChecker& analysis_handle)
33 : config_{ config }
34 , frame_count_checker{ &analysis_handle }
35 {
36 samples_.reserve(config_.sample_size);
37 }
38
39 void launch(asio::any_io_executor executor) { asio::co_spawn(executor, main_loop(), asio::detached); }
40
41 void stop() { is_stopped_.store(true); }
42
43 private:
44 std::atomic<bool> is_stopped_{ false };
46 std::vector<int64_t> samples_;
48 gsl::not_null<const sink::FrameCountChecker*> frame_count_checker;
49
50 void report()
51 {
52 const auto average = static_cast<double>(std::ranges::fold_left(samples_, int64_t{ 0 }, std::plus{})) /
53 static_cast<double>(samples_.size());
54
55 if (auto diff = average - last_average_drop_count; diff > 1.)
56 {
57 spdlog::warn(
58 "{:.0f} frames miss detected from the frame counter in the last {:.1f} seconds",
59 diff,
60 static_cast<double>(config_.sample_size) * static_cast<double>(config_.sample_time_ms) / 1000.);
61 }
63 samples_.clear();
64 }
65
66 auto main_loop() -> asio::awaitable<void>
67 {
68 const auto _ = ExitLogger{};
69 auto executor = co_await asio::this_coro::executor;
70 auto sample_clock = asio::steady_timer{ executor };
71 while (not is_stopped_.load())
72 {
73 sample_clock.expires_after(std::chrono::milliseconds(config_.sample_time_ms));
74 samples_.push_back(frame_count_checker->get_frame_drop_count());
75
76 if (samples_.size() == config_.sample_size)
77 {
78 report();
79 }
80 }
81 }
82 };
83}; // namespace srs::workflow
gsl::not_null< const sink::FrameCountChecker * > frame_count_checker
FrameMissMonitor(const Config &config, const sink::FrameCountChecker &analysis_handle)
auto main_loop() -> asio::awaitable< void >
void launch(asio::any_io_executor executor)