srs-control
0.2.2
Toggle main menu visibility
Loading...
Searching...
No Matches
RawFrameReader.cpp
Go to the documentation of this file.
1
#include "
srs/readers/RawFrameReader.hpp
"
2
#include "
srs/utils/CommonDefinitions.hpp
"
3
#include <array>
4
#include <cstddef>
5
#include <cstdint>
6
#include <expected>
7
#include <fmt/format.h>
8
#include <fstream>
9
#include <ios>
10
#include <spdlog/spdlog.h>
11
#include <stdexcept>
12
#include <string>
13
#include <string_view>
14
#include <vector>
15
#include <zpp_bits.h>
16
17
namespace
srs::reader
18
{
19
20
RawFrame::RawFrame
(
const
std::string& filename)
21
:
input_filename_
{ filename }
22
,
input_file_
{ filename, std::ios::binary }
23
{
24
if
(
input_file_
.fail())
25
{
26
throw
std::runtime_error{ fmt::format(
"Cannot open the file {:?}"
,
input_filename_
) };
27
}
28
spdlog::debug(
"Open the binary file {:?}"
,
input_filename_
);
29
}
30
31
auto
RawFrame::read_one_frame
(std::vector<char>& binary_data, std::ifstream& input_file)
32
-> std::expected<std::size_t, std::string>
33
{
34
if
(not input_file.is_open())
35
{
36
return
std::unexpected(fmt::format(
"The input file {} is not open!"
,
input_filename_
));
37
}
38
binary_data.reserve(
common::LARGE_READ_MSG_BUFFER_SIZE
);
39
std::array<char,
sizeof
(
common::RawDelimSizeType
)> size_buffer{};
40
auto
size =
common::RawDelimSizeType
{};
41
if
(input_file.eof())
42
{
43
return
0;
44
// return std::unexpected("End of the binary file.");
45
}
46
47
input_file.read(size_buffer.data(),
static_cast<
int64_t
>
(size_buffer.size()));
48
auto
serialize_to = zpp::bits::in{ size_buffer, zpp::bits::endian::big{} };
49
serialize_to(size).or_throw();
50
binary_data.resize(size, 0);
51
auto
read_size =
static_cast<
std::size_t
>
(input_file.read(binary_data.data(), size).gcount());
52
53
if
(read_size != size)
54
{
55
spdlog::warn(
"Binary Data is not extracted correctly from the input file"
);
56
}
57
58
return
read_size;
59
}
60
61
void
RawFrame::reset
()
62
{
63
input_file_
.clear();
64
input_file_
.seekg(0, std::ios::beg);
65
}
66
67
auto
RawFrame::read_one_frame
() -> std::expected<std::string_view, std::string>
68
{
69
auto
res =
read_one_frame
(
input_buffer_
,
input_file_
);
70
if
(res.has_value())
71
{
72
return
std::string_view{
input_buffer_
.data(),
input_buffer_
.size() };
73
}
74
return
std::unexpected{ res.error() };
75
}
76
}
// namespace srs::reader
CommonDefinitions.hpp
RawFrameReader.hpp
srs::reader::RawFrame::read_one_frame
auto read_one_frame(std::vector< char > &binary_data, std::ifstream &input_file) -> std::expected< std::size_t, std::string >
Read one frame of the bianry data to a vector.
Definition
RawFrameReader.cpp:31
srs::reader::RawFrame::input_filename_
std::string input_filename_
Input binary file name.
Definition
RawFrameReader.hpp:61
srs::reader::RawFrame::input_buffer_
std::vector< char > input_buffer_
internal binary data buffer
Definition
RawFrameReader.hpp:63
srs::reader::RawFrame::RawFrame
RawFrame()=default
Default Constructor. No memory allocation.
srs::reader::RawFrame::reset
void reset()
Definition
RawFrameReader.cpp:61
srs::reader::RawFrame::input_file_
std::ifstream input_file_
Input binary file handler.
Definition
RawFrameReader.hpp:62
srs::common::RawDelimSizeType
uint32_t RawDelimSizeType
Definition
CommonDefinitions.hpp:96
srs::common::LARGE_READ_MSG_BUFFER_SIZE
constexpr auto LARGE_READ_MSG_BUFFER_SIZE
size of the data array for reading a UDP package
Definition
CommonDefinitions.hpp:21
srs::reader
Definition
ProtoMsgReader.cpp:9
backend
srs
readers
RawFrameReader.cpp
Impressum
Datenschutzerklärung
Generated by
1.17.0