libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
sdpsession.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <cstdint>
14#include <promeki/error.h>
15#include <promeki/namespace.h>
16#include <promeki/string.h>
17#include <promeki/list.h>
18#include <promeki/map.h>
19#include <promeki/pair.h>
20#include <promeki/result.h>
21#include <promeki/sharedptr.h>
22#include <promeki/datatype.h>
23
24PROMEKI_NAMESPACE_BEGIN
25
26class DataStream;
27
46class SdpMediaDescription {
47 public:
49 SdpMediaDescription() = default;
50
52 const String &mediaType() const { return _mediaType; }
53
55 void setMediaType(const String &type) { _mediaType = type; }
56
58 uint16_t port() const { return _port; }
59
61 void setPort(uint16_t port) { _port = port; }
62
64 const String &protocol() const { return _protocol; }
65
67 void setProtocol(const String &proto) { _protocol = proto; }
68
70 const List<uint8_t> &payloadTypes() const { return _payloadTypes; }
71
73 void addPayloadType(uint8_t pt) { _payloadTypes.pushToBack(pt); }
74
76 using Attribute = Pair<String, String>;
77
79 using AttributeList = ::promeki::List<Attribute>;
80
82 String attribute(const String &name) const {
83 for (size_t i = 0; i < _attributes.size(); i++) {
84 if (_attributes[i].first() == name) return _attributes[i].second();
85 }
86 return String();
87 }
88
96 void setAttribute(const String &name, const String &value) {
97 for (size_t i = 0; i < _attributes.size(); i++) {
98 if (_attributes[i].first() == name) {
99 _attributes[i].setSecond(value);
100 return;
101 }
102 }
103 _attributes.pushToBack(Attribute(name, value));
104 }
105
107 const AttributeList &attributes() const { return _attributes; }
108
114 const String &connectionAddress() const { return _connectionAddress; }
115
117 void setConnectionAddress(const String &addr) { _connectionAddress = addr; }
118
127 struct RtpMap {
128 uint8_t payloadType = 0;
129 String encoding;
130 uint32_t clockRate = 0;
131 unsigned int channels = 1;
132 bool valid = false;
133 };
134
142 RtpMap rtpMap() const;
143
145 using FmtpParameters = ::promeki::Map<String, String>;
146
156 FmtpParameters fmtpParameters() const;
157
159 bool operator==(const SdpMediaDescription &other) const {
160 return _mediaType == other._mediaType && _port == other._port && _protocol == other._protocol &&
161 _payloadTypes == other._payloadTypes && _attributes == other._attributes &&
162 _connectionAddress == other._connectionAddress;
163 }
164
166 bool operator!=(const SdpMediaDescription &other) const { return !(*this == other); }
167
168 private:
169 String _mediaType;
170 uint16_t _port = 0;
171 String _protocol;
172 List<uint8_t> _payloadTypes;
173 AttributeList _attributes;
174 String _connectionAddress;
175};
176
203class SdpSession {
204 PROMEKI_SHARED_FINAL(SdpSession)
205 public:
206 PROMEKI_DATATYPE(SdpSession, DataTypeSdpSession, 1)
207
208
209 Error writeToStream(DataStream &s) const;
211 template <uint32_t V> static Result<SdpSession> readFromStream(DataStream &s);
212
214 using Ptr = SharedPtr<SdpSession>;
215
217 using List = ::promeki::List<SdpSession>;
218
220 using PtrList = ::promeki::List<SdpSession::Ptr>;
221
223 using MediaDescriptionList = ::promeki::List<SdpMediaDescription>;
224
231 static Result<SdpSession> fromString(const String &sdp);
232
246 static Result<SdpSession> fromFile(const String &path);
247
249 SdpSession() = default;
250
252 const String &sessionName() const { return _sessionName; }
253
255 void setSessionName(const String &name) { _sessionName = name; }
256
258 const String &originUsername() const { return _originUsername; }
259
261 uint64_t sessionId() const { return _sessionId; }
262
264 uint64_t sessionVersion() const { return _sessionVersion; }
265
275 void setOrigin(const String &username, uint64_t sessionId, uint64_t sessionVersion,
276 const String &netType = "IN", const String &addrType = "IP4",
277 const String &address = "0.0.0.0");
278
280 const String &originNetType() const { return _originNetType; }
281
283 const String &originAddrType() const { return _originAddrType; }
284
286 const String &originAddress() const { return _originAddress; }
287
289 const String &connectionAddress() const { return _connectionAddress; }
290
292 void setConnectionAddress(const String &address) { _connectionAddress = address; }
293
295 const MediaDescriptionList &mediaDescriptions() const { return _mediaDescriptions; }
296
298 void addMediaDescription(const SdpMediaDescription &md) { _mediaDescriptions.pushToBack(md); }
299
304 String toString() const;
305
317 Error toFile(const String &path) const;
318
320 bool operator==(const SdpSession &other) const {
321 return _sessionName == other._sessionName && _originUsername == other._originUsername &&
322 _sessionId == other._sessionId && _sessionVersion == other._sessionVersion &&
323 _originNetType == other._originNetType && _originAddrType == other._originAddrType &&
324 _originAddress == other._originAddress &&
325 _connectionAddress == other._connectionAddress &&
326 _mediaDescriptions == other._mediaDescriptions;
327 }
328
330 bool operator!=(const SdpSession &other) const { return !(*this == other); }
331
332 private:
333 String _sessionName;
334 String _originUsername = "-";
335 uint64_t _sessionId = 0;
336 uint64_t _sessionVersion = 0;
337 String _originNetType = "IN";
338 String _originAddrType = "IP4";
339 String _originAddress = "0.0.0.0";
340 String _connectionAddress;
341 MediaDescriptionList _mediaDescriptions;
342};
343
344PROMEKI_NAMESPACE_END
345
346PROMEKI_FORMAT_VIA_TOSTRING(promeki::SdpSession);
347
348#endif // PROMEKI_ENABLE_CORE