ifm3d
rtsp_client.h
1 /*
2  * Copyright 2026-present ifm electronic, gmbh
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef IFM3D_RTSP_RTSP_CLIENT_H
7 #define IFM3D_RTSP_RTSP_CLIENT_H
8 
9 #include <cstdint>
10 #include <functional>
11 #include <future>
12 #include <memory>
13 #include <optional>
14 #include <string>
15 #include <vector>
16 
17 #include <ifm3d/common/err.h>
18 #include <ifm3d/device/device.h>
19 #include <ifm3d/device/o3r.h>
20 #include <ifm3d/fg/frame.h>
21 #include <ifm3d/rtsp/module_rtsp.h>
22 #include <ifm3d/rtsp/nal_unit.h>
23 
24 namespace ifm3d
25 {
42  class IFM3D_EXPORT RtspClient
43  {
44  public:
45  using Ptr = std::shared_ptr<RtspClient>;
46 
48  enum class Transport : std::uint8_t
49  {
51  INTERLEAVED = 0,
53  UDP = 1,
54  };
55 
57  enum class State : std::uint8_t
58  {
59  IDLE = 0,
60  CONNECTING = 1,
61  READY = 2,
62  PLAYING = 3,
63  STOPPED = 4,
64  FAILED = 5,
65  };
66 
68  struct Config
69  {
75  std::optional<std::string> url;
76 
78  std::uint16_t port = 8554;
79 
81  std::string stream_path = "port1";
82 
84  Transport transport = Transport::INTERLEAVED;
85 
90  std::optional<std::string> decoder;
91  };
92 
93  using BufferIdList = std::vector<ifm3d::buffer_id>;
94  using NewFrameCallback = std::function<void(ifm3d::Frame::Ptr)>;
95  using NalUnitCallback = std::function<void(const ifm3d::NalUnit&)>;
96  using ErrorCallback = std::function<void(const ifm3d::Error&)>;
97  using StateChangeCallback = std::function<void(State)>;
98 
105  explicit RtspClient(ifm3d::Device::Ptr device);
106 
114  RtspClient(ifm3d::Device::Ptr device, Config config);
115 
123  RtspClient(ifm3d::Device::Ptr device, const ifm3d::PortInfo& port);
124 
134  RtspClient(ifm3d::Device::Ptr device,
135  const ifm3d::PortInfo& port,
136  Config config);
137 
138  virtual ~RtspClient();
139 
140  // copy-disabled, move-enabled (like FrameGrabber's PImpl)
141  RtspClient(const RtspClient&) = delete;
142  RtspClient& operator=(const RtspClient&) = delete;
143  RtspClient(RtspClient&&) noexcept;
144  RtspClient& operator=(RtspClient&&) noexcept;
145 
155  std::shared_future<void> Start(const BufferIdList& buffers = {});
156 
162  std::shared_future<void> Stop();
163 
165  [[nodiscard]] bool IsRunning() const;
166 
168  [[nodiscard]] State GetState() const;
169 
173  void OnNewFrame(NewFrameCallback callback = nullptr);
174 
176  void OnNalUnit(NalUnitCallback callback = nullptr);
177 
179  void OnError(ErrorCallback callback = nullptr);
180 
182  void OnStateChange(StateChangeCallback callback = nullptr);
183 
184  private:
185  class Impl;
186  std::unique_ptr<Impl> _impl;
187  }; // end: class RtspClient
188 
189 } // namespace ifm3d
190 
191 #endif // IFM3D_RTSP_RTSP_CLIENT_H
Exception wrapper for library and system errors encountered by the library.
Definition: err.h:126
RTSP/1.0 client that streams H.264 video from an ifm device and, optionally, decodes it into ifm3d::F...
Definition: rtsp_client.h:43
void OnStateChange(StateChangeCallback callback=nullptr)
Registers a callback invoked on every session state transition.
void OnNalUnit(NalUnitCallback callback=nullptr)
Registers a callback invoked for each reassembled NAL unit.
State GetState() const
State
Lifecycle state of the RTSP session.
Definition: rtsp_client.h:58
RtspClient(ifm3d::Device::Ptr device, const ifm3d::PortInfo &port, Config config)
Constructs a client for a specific port, deriving the RTSP port and stream path from the port's adver...
Transport
RTP transport used for the media stream.
Definition: rtsp_client.h:49
RtspClient(ifm3d::Device::Ptr device)
Constructs a client that streams from the given device using the default configuration.
void OnError(ErrorCallback callback=nullptr)
Registers a callback invoked on terminal errors.
bool IsRunning() const
void OnNewFrame(NewFrameCallback callback=nullptr)
Registers a callback invoked for each received frame.
std::shared_future< void > Stop()
Sends TEARDOWN, stops the worker thread and closes the socket.
RtspClient(ifm3d::Device::Ptr device, const ifm3d::PortInfo &port)
Constructs a client for a specific port, deriving the RTSP port and stream path from the port's adver...
RtspClient(ifm3d::Device::Ptr device, Config config)
Constructs a client that streams from the given device.
A single, fully reassembled H.264 NAL unit.
Definition: nal_unit.h:31
Definition: o3r.h:65
Configuration for an RtspClient instance.
Definition: rtsp_client.h:69
std::optional< std::string > decoder
Override the decoder name used for decoding h264 video into buffers.
Definition: rtsp_client.h:90
std::optional< std::string > url
Full RTSP URL override (e.g.
Definition: rtsp_client.h:75