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:
34
36
40 static std::unique_ptr<StreamConfig> Create()
41 {
42 return Internal::DoResCallObjectOut<StreamConfig>([&](void*& handle)
43 {
44 return CVB_CALL_CAPI(CVRTPCreateStreamConfig(handle));
45 });
46 }
47
48 static std::unique_ptr<StreamConfig> FromHandle(HandleGuard<StreamConfig>&& guard)
49 {
50 if (!guard.Handle())
51 throw std::runtime_error("handle must not be null");
52
53 return std::make_unique<StreamConfig>(std::move(guard));
54 }
55
56 explicit StreamConfig(HandleGuard<StreamConfig>&& guard) noexcept
57 : handle_(std::move(guard))
58 {
59
60 }
61
62 StreamConfig(const StreamConfig& other) = delete;
63 StreamConfig& operator=(const StreamConfig& other) = delete;
64 StreamConfig(StreamConfig&& other) = delete;
65 StreamConfig& operator=(StreamConfig&& other) = delete;
66
67 ~StreamConfig() = default;
68
69 void* Handle() const noexcept
70 {
71 return handle_.Handle();
72 }
73
75
79 String DstAddr() const;
80
82
86 void SetDstAddr(const String& dstAddr);
87
89
93 uint16_t DstPort() const
94 {
95 return Property<uint16_t>(CExports::CVRTPSP_DST_PORT);
96 }
97
99
103 void SetDstPort(uint16_t port)
104 {
105 return SetProperty<uint16_t>(port, CExports::CVRTPSP_DST_PORT);
106 }
107
109
113 String URL() const;
114
116
123 void SetURL(const String& url);
124
126
130 void SetupCodecFromJSON(const String& codecJSON);
131
133
137 void SetupCodecWithName(const String& codecName);
138
140
144 String ReadCodecJSON() const;
145
147
151 String ReadSessionDescriptionProtocol() const;
152
153 private:
154
155 template<class T>
156 T Property(CExports::CVRTPStreamProperty property) const
157 {
158 T value = {};
159 size_t size = sizeof(T);
160 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigGetProperty(Handle(), property, &value, size));
161 return value;
162 }
163
164 template<class T>
165 void SetProperty(const T& value, CExports::CVRTPStreamProperty property)
166 {
167 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigSetProperty(Handle(), property, const_cast<T*>(&value), sizeof(T))); // NOLINT(cppcoreguidelines-pro-type-const-cast)
168 }
169
170 static std::string ToBytes(const std::wstring& text)
171 {
172 // we can be sure that properties are ASCII only
173 std::string asciiText;
174 asciiText.reserve(text.size());
175 for (const auto& c : text)
176 asciiText.push_back(static_cast<char>(c));
177 return asciiText;
178 }
179
180 static std::string ToBytes(const std::string& text)
181 {
182 return text;
183 }
184
185 HandleGuard<StreamConfig> handle_;
186
187 };
188
189 template<>
190 inline String StreamConfig::Property<String>(CExports::CVRTPStreamProperty property) const
191 {
192
193 size_t size = 0;
194 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigGetProperty(Handle(), property, nullptr, size));
195
196 std::vector<char> buffer(size);
197 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigGetProperty(Handle(), property, buffer.data(), size));
198
199 return { buffer.begin(), buffer.end() - 1 };
200 }
201
202 template<>
203 inline void StreamConfig::SetProperty<String>(const String& value, CExports::CVRTPStreamProperty property)
204 {
205 CVB_CALL_CAPI_CHECKED(CVRTPStreamConfigSetProperty(Handle(), property, const_cast<char*>(ToBytes(value).c_str()), value.size() + 1)); // NOLINT(cppcoreguidelines-pro-type-const-cast)
206 }
207
208
209 String StreamConfig::DstAddr() const
210 {
211 return Property<String>(CExports::CVRTPSP_DST_ADDR);
212 }
213
214 void StreamConfig::SetDstAddr(const String& dstAddr)
215 {
216 SetProperty<String>(dstAddr, CExports::CVRTPSP_DST_ADDR);
217 }
218
219 String StreamConfig::URL() const
220 {
221 return Property<String>(CExports::CVRTPSP_URL);
222 }
223
224 void StreamConfig::SetURL(const String& url)
225 {
226 SetProperty<String>(url, CExports::CVRTPSP_URL);
227 }
228
229 void StreamConfig::SetupCodecFromJSON(const String& codecJSON)
230 {
231 SetProperty<String>(codecJSON, CExports::CVRTPSP_CodecJSON);
232 }
233
234 void StreamConfig::SetupCodecWithName(const String& codecName)
235 {
236 SetProperty<String>(codecName, CExports::CVRTPSP_CodecName);
237 }
238
239 String StreamConfig::ReadCodecJSON() const
240 {
241 return Property<String>(CExports::CVRTPSP_CodecJSON);
242 }
243
244 String StreamConfig::ReadSessionDescriptionProtocol() const
245 {
246 return Property<String>(CExports::CVRTPSP_SDP);
247 }
248
249 }
250
251 CVB_END_INLINE_NS
252
253}
A configuration defining a RTP stream.
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24
std::string String
String for wide characters or unicode characters.
Definition: string.hpp:56