bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
mjpeg_http_camera_backend.h
Go to the documentation of this file.
1#ifndef BITSIM_SENSORS_BACKENDS_MJPEG_HTTP_CAMERA_BACKEND_H
2#define BITSIM_SENSORS_BACKENDS_MJPEG_HTTP_CAMERA_BACKEND_H
3
4#include "bitsim/bitsim_config.h"
5
6#ifdef BITSIM_OPENCV
7
10
11#include <atomic>
12#include <mutex>
13#include <optional>
14#include <string>
15#include <thread>
16
17namespace bitsim
18{
19namespace sensors::backends
20{
21
32{
33 public:
37 static const std::string BACKEND_TYPE;
38
42 struct Config
43 {
47 std::string url;
48
54
59
64 };
65
66 explicit MJPEG_HTTPCameraBackend(const Config &config);
67 ~MJPEG_HTTPCameraBackend() override;
68
71
72 void connect() override;
73 void disconnect() override;
74 std::optional<CameraFrame> latest_frame() const override;
75 bool is_alive() const override;
76
82 bool wait_for_frame(real_t timeout) const;
83
88 real_t frame_rate() const;
89
95 static bool can_read_streams();
96
100 uint_t n_reconnects() const noexcept { return n_reconnects_; }
101
102 const Config &config() const noexcept { return config_; }
103
104 private:
105 void run_();
106
107 Config config_;
108 std::thread thread_;
109 std::atomic<bool> running_{false};
110 std::atomic<uint_t> n_reconnects_{0};
111
112 mutable std::mutex mutex_;
113 std::optional<CameraFrame> frame_;
114 real_t mean_interval_{0.0};
115};
116
117} // namespace sensors::backends
118} // namespace bitsim
119
120#endif
121#endif // BITSIM_SENSORS_BACKENDS_MJPEG_HTTP_CAMERA_BACKEND_H
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
CameraBackendBase(const std::string &backend_type)
Definition camera_backend_base.h:76
void disconnect() override
Stop receiving frames.
Definition mjpeg_http_camera_backend.cpp:60
static const std::string BACKEND_TYPE
The backend type.
Definition mjpeg_http_camera_backend.h:37
MJPEG_HTTPCameraBackend(const MJPEG_HTTPCameraBackend &)=delete
std::optional< CameraFrame > latest_frame() const override
The latest frame, if one has been received.
Definition mjpeg_http_camera_backend.cpp:69
uint_t n_reconnects() const noexcept
How many times the stream had to be opened again.
Definition mjpeg_http_camera_backend.h:100
MJPEG_HTTPCameraBackend(const Config &config)
Definition mjpeg_http_camera_backend.cpp:39
MJPEG_HTTPCameraBackend & operator=(const MJPEG_HTTPCameraBackend &)=delete
bool wait_for_frame(real_t timeout) const
Wait until the first frame arrives.
Definition mjpeg_http_camera_backend.cpp:81
void connect() override
Start receiving frames.
Definition mjpeg_http_camera_backend.cpp:50
real_t frame_rate() const
The frame rate of the stream, averaged over the last frames [frames/s]. Zero before two frames have a...
Definition mjpeg_http_camera_backend.cpp:95
~MJPEG_HTTPCameraBackend() override
Definition mjpeg_http_camera_backend.cpp:48
bool is_alive() const override
Whether frames are arriving.
Definition mjpeg_http_camera_backend.cpp:75
const Config & config() const noexcept
Definition mjpeg_http_camera_backend.h:102
static bool can_read_streams()
Whether this OpenCV can read HTTP streams at all. The backend reads them with OpenCV's FFmpeg backend...
Definition mjpeg_http_camera_backend.cpp:32
Definition camera_backend_base.h:19
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
Settings of the backend.
Definition mjpeg_http_camera_backend.h:43
real_t io_timeout
Give up opening, or reading a frame, after this long [s].
Definition mjpeg_http_camera_backend.h:63
std::string url
The URL of the stream, e.g. http://raspberrypi.local:8000/stream.
Definition mjpeg_http_camera_backend.h:47
real_t timeout
The stream is alive while frames arrive at most this far apart [s].
Definition mjpeg_http_camera_backend.h:53
real_t reconnect_delay
How long to wait before connecting again after a failure [s].
Definition mjpeg_http_camera_backend.h:58