srs-control 0.2.2
Loading...
Searching...
No Matches
FrameCountChecker.cpp
Go to the documentation of this file.
6#include <atomic>
7#include <cstddef>
8#include <cstdint>
9#include <expected>
10#include <fmt/base.h>
11#include <zpp_bits.h>
12
13namespace srs::sink
14{
16 : SinkTask{ "FrameCountChecker", process::DataConvertOptions::raw, n_lines }
17 {
18 }
19
21 {
22
23 auto deserialize_to = zpp::bits::in{ binary_data, zpp::bits::endian::network{}, zpp::bits::no_size{} };
24 auto read_bytes = binary_data.size() * sizeof(BufferElementType);
25 auto header = ReceiveDataHeader{};
26 constexpr auto header_bytes = sizeof(header);
27 if (read_bytes <= header_bytes)
28 {
29 return std::unexpected{ "Deserialization: The size of the binary data is too small!" };
30 }
31 auto err = deserialize_to(header);
32
33 if (zpp::bits::failure(err))
34 {
35 return std::unexpected{ "FrameCountChecker failed to deserialize the header" };
36 }
37 return header.frame_counter;
38 }
39
41 {
42 auto last_value = last_frame_counter_.load();
43 while (true)
44 {
45 auto diff = frame_counter - last_value;
46 auto insert_value = (diff > 0) ? frame_counter : last_value;
47 if (last_frame_counter_.compare_exchange_weak(last_value, insert_value))
48 {
49 if (diff > 1)
50 {
51 frame_drop_counter_ += (diff - 1);
52 }
53 else if (diff < 0)
54 {
56 }
57 break;
58 }
59 }
60 }
61
63 {
64 auto frame_counter = extract_frame_counter(binary_data);
65
66 if (frame_counter)
67 {
68 register_frame_counter(frame_counter.value());
69 }
70
71 return frame_counter;
72 }
73
75} // namespace srs::sink
DataConvertOptions
std::expected< OutputType, std::string_view > RunResult
Definition BaseTask.hpp:32
SinkTask(std::string_view name, DataConvertOptions prev_convert, std::size_t n_lines=1)
Definition BaseTask.hpp:109
auto extract_frame_counter(InputType binary_data) -> RunResult
auto analyze_frame_counter(InputType binary_data) -> RunResult
std::atomic< int64_t > frame_drop_counter_
std::atomic< int64_t > last_frame_counter_
void register_frame_counter(int64_t frame_counter)
FrameCountChecker(std::size_t n_lines)
char BufferElementType