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/buffer.h>
21 #include <ifm3d/rtsp/frame_metadata.h>
22 #include <ifm3d/rtsp/module_rtsp.h>
23 #include <ifm3d/rtsp/nal_unit.h>
24 
25 namespace ifm3d
26 {
44  class IFM3D_EXPORT RtspClient
45  {
46  public:
47  using Ptr = std::shared_ptr<RtspClient>;
48 
50  enum class Transport : std::uint8_t
51  {
53  INTERLEAVED = 0,
55  UDP = 1,
56  };
57 
59  enum class State : std::uint8_t
60  {
61  IDLE = 0,
62  CONNECTING = 1,
63  READY = 2,
64  PLAYING = 3,
65  STOPPED = 4,
66  FAILED = 5,
67  };
68 
70  enum class OutputFormat : std::uint8_t
71  {
76  RGB = 0,
82  YUV420 = 1,
83  };
84 
86  struct Config
87  {
93  std::optional<std::string> url;
94 
96  std::uint16_t port = 8554;
97 
99  std::string stream_path = "port1";
100 
102  Transport transport = Transport::INTERLEAVED;
103 
110  std::optional<std::string> decoder;
111 
116  OutputFormat output_format = OutputFormat::RGB;
117  };
118 
119  using NewFrameCallback = std::function<void(ifm3d::Buffer)>;
120  using NalUnitCallback = std::function<void(const ifm3d::NalUnit&)>;
121  using ErrorCallback = std::function<void(const ifm3d::Error&)>;
122  using StateChangeCallback = std::function<void(State)>;
123 
130  explicit RtspClient(ifm3d::Device::Ptr device);
131 
139  RtspClient(ifm3d::Device::Ptr device, Config config);
140 
148  RtspClient(ifm3d::Device::Ptr device, const ifm3d::PortInfo& port);
149 
159  RtspClient(ifm3d::Device::Ptr device,
160  const ifm3d::PortInfo& port,
161  Config config);
162 
163  virtual ~RtspClient();
164 
165  // copy-disabled, move-enabled (like FrameGrabber's PImpl)
166  RtspClient(const RtspClient&) = delete;
167  RtspClient& operator=(const RtspClient&) = delete;
168  RtspClient(RtspClient&&) noexcept;
169  RtspClient& operator=(RtspClient&&) noexcept;
170 
177  std::shared_future<void> Start();
178 
184  std::shared_future<void> Stop();
185 
187  [[nodiscard]] bool IsRunning() const;
188 
190  [[nodiscard]] State GetState() const;
191 
196  void OnNewFrame(NewFrameCallback callback = nullptr);
197 
199  void OnNalUnit(NalUnitCallback callback = nullptr);
200 
202  void OnError(ErrorCallback callback = nullptr);
203 
205  void OnStateChange(StateChangeCallback callback = nullptr);
206 
207  private:
208  class Impl;
209  std::unique_ptr<Impl> _impl;
210  }; // end: class RtspClient
211 
212 } // namespace ifm3d
213 
214 #endif // IFM3D_RTSP_RTSP_CLIENT_H
The class Buffer represent a STL container to store data from the ifm devices in 2 dimension and supp...
Definition: buffer.h:99
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::B...
Definition: rtsp_client.h:45
OutputFormat
Pixel layout of decoded frames delivered to OnNewFrame.
Definition: rtsp_client.h:71
State
Lifecycle state of the RTSP session.
Definition: rtsp_client.h:60
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:51
RtspClient(ifm3d::Device::Ptr device)
Constructs a client that streams from the given device using the default configuration.
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:87
std::optional< std::string > decoder
Optional decoder selection.
Definition: rtsp_client.h:110
std::optional< std::string > url
Full RTSP URL override (e.g.
Definition: rtsp_client.h:93