CVB++ 15.0
stream_config.hpp
1#pragma once
2
3#include <map>
4
5#include "rtpstreaming.hpp"
6#include "../size_2d.hpp"
7#include "../string.hpp"
8#include "../shims/stdvariant.hpp"
9#include "../utilities/system_info.hpp"
10
11namespace Cvb
12{
13
14 CVB_BEGIN_INLINE_NS
15
16 template <>
17 inline HandleGuard<RTPStreaming::StreamConfig>::HandleGuard(void *handle) noexcept
18 : HandleGuard<RTPStreaming::StreamConfig>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
19 {
20 }
21
28 namespace RTPStreaming
29 {
30
31 class StreamConfig final
32 {
33 public:
35
39 static std::unique_ptr<StreamConfig> Create()
40 {
41 return Internal::DoResCallObjectOut<StreamConfig>(
42 [&](void *&handle) { return CVB_CALL_CAPI(CVRTPCreateStreamConfig(handle)); });
43 }
44
45 static std::unique_ptr<StreamConfig> FromHandle(HandleGuard<StreamConfig> &&guard)
46 {
47 if (!guard.Handle())
48 throw std::runtime_error("handle must not be null");
49
50 return std::make_unique<StreamConfig>(std::move(guard));
51 }
52
53 explicit StreamConfig(HandleGuard<StreamConfig> &&guard) noexcept
54 : handle_(std::move(guard))
55 {
56 }
57
58 StreamConfig(const StreamConfig &other) = delete;
59 StreamConfig &operator=(const StreamConfig &other) = delete;
60 StreamConfig(StreamConfig &&other) = delete;
61 StreamConfig &operator=(StreamConfig &&other) = delete;
62
63 ~StreamConfig() = default;
64
65 void *Handle() const noexcept
66 {
67 return handle_.Handle();
68 }
69
71
75 String DstAddr() const;
76
78
82 void SetDstAddr(const String &dstAddr);
83
85
89 uint16_t DstPort() const
90 {
91 return Property<uint16_t>(CExports::CVRTPSP_DST_PORT);
92 }
93
95
99 void SetDstPort(uint16_t port)
100 {
101 return SetProperty<uint16_t>(port, CExports::CVRTPSP_DST_PORT);
102 }
103
105
109 String URL() const;
110
112
119 void SetURL(const String &url);
120
122
126 void SetupCodecFromJSON(const String &codecJSON);
127
129
133 void SetupCodecWithName(const String &codecName);
134
136
140 String ReadCodecJSON() const;
141
143
147 String ReadSessionDescriptionProtocol() const;
148
149 private:
150 template <class T>
151 T Property(CExports::CVRTPStreamProperty property) const
152 {
153 T value = {};
154 size_t size = sizeof(T);
155 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigGetProperty(Handle(), property, &value, size));
156 return value;
157 }
158
159 template <class T>
160 void SetProperty(const T &value, CExports::CVRTPStreamProperty property)
161 {
162 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigSetProperty(
163 Handle(), property, const_cast<T *>(&value), sizeof(T))); // NOLINT(cppcoreguidelines-pro-type-const-cast)
164 }
165
166 static std::string ToBytes(const std::wstring &text)
167 {
168 // we can be sure that properties are ASCII only
169 std::string asciiText;
170 asciiText.reserve(text.size());
171 for (const auto &c : text)
172 asciiText.push_back(static_cast<char>(c));
173 return asciiText;
174 }
175
176 static std::string ToBytes(const std::string &text)
177 {
178 return text;
179 }
180
181 HandleGuard<StreamConfig> handle_;
182 };
183
184 template <>
185 inline String StreamConfig::Property<String>(CExports::CVRTPStreamProperty property) const
186 {
187
188 size_t size = 0;
189 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigGetProperty(Handle(), property, nullptr, size));
190
191 std::vector<char> buffer(size);
192 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigGetProperty(Handle(), property, buffer.data(), size));
193
194 return {buffer.begin(), buffer.end() - 1};
195 }
196
197 template <>
198 inline void StreamConfig::SetProperty<String>(const String &value, CExports::CVRTPStreamProperty property)
199 {
200 CVB_CALL_CAPI_CHECKED(
201 CVRTPStreamConfigSetProperty(Handle(), property, const_cast<char *>(ToBytes(value).c_str()),
202 value.size() + 1)); // NOLINT(cppcoreguidelines-pro-type-const-cast)
203 }
204
205 String StreamConfig::DstAddr() const
206 {
207 return Property<String>(CExports::CVRTPSP_DST_ADDR);
208 }
209
210 void StreamConfig::SetDstAddr(const String &dstAddr)
211 {
212 SetProperty<String>(dstAddr, CExports::CVRTPSP_DST_ADDR);
213 }
214
215 String StreamConfig::URL() const
216 {
217 return Property<String>(CExports::CVRTPSP_URL);
218 }
219
220 void StreamConfig::SetURL(const String &url)
221 {
222 SetProperty<String>(url, CExports::CVRTPSP_URL);
223 }
224
225 void StreamConfig::SetupCodecFromJSON(const String &codecJSON)
226 {
227 SetProperty<String>(codecJSON, CExports::CVRTPSP_CodecJSON);
228 }
229
230 void StreamConfig::SetupCodecWithName(const String &codecName)
231 {
232 SetProperty<String>(codecName, CExports::CVRTPSP_CodecName);
233 }
234
235 String StreamConfig::ReadCodecJSON() const
236 {
237 return Property<String>(CExports::CVRTPSP_CodecJSON);
238 }
239
240 String StreamConfig::ReadSessionDescriptionProtocol() const
241 {
242 return Property<String>(CExports::CVRTPSP_SDP);
243 }
244
245 } // namespace RTPStreaming
246
247 CVB_END_INLINE_NS
248
249} // namespace Cvb
A configuration defining a RTP stream.
cvbbool_t ReleaseObject(OBJ &Object)
T move(T... args)
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49