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
313 String sessionAttribute(const String &name) const {
314 for (size_t i = 0; i < _sessionAttributes.size(); i++) {
315 if (_sessionAttributes[i].first() == name) return _sessionAttributes[i].second();
316 }
317 return String();
318 }
319
330 void setSessionAttribute(const String &name, const String &value) {
331 for (size_t i = 0; i < _sessionAttributes.size(); i++) {
332 if (_sessionAttributes[i].first() == name) {
333 _sessionAttributes[i].setSecond(value);
334 return;
335 }
336 }
337 _sessionAttributes.pushToBack(SdpMediaDescription::Attribute(name, value));
338 }
339
341 const SdpMediaDescription::AttributeList &sessionAttributes() const { return _sessionAttributes; }
342
347 String toString() const;
348
360 Error toFile(const String &path) const;
361
363 bool operator==(const SdpSession &other) const {
364 return _sessionName == other._sessionName && _originUsername == other._originUsername &&
365 _sessionId == other._sessionId && _sessionVersion == other._sessionVersion &&
366 _originNetType == other._originNetType && _originAddrType == other._originAddrType &&
367 _originAddress == other._originAddress &&
368 _connectionAddress == other._connectionAddress &&
369 _sessionAttributes == other._sessionAttributes &&
370 _mediaDescriptions == other._mediaDescriptions;
371 }
372
374 bool operator!=(const SdpSession &other) const { return !(*this == other); }
375
376 private:
377 String _sessionName;
378 String _originUsername = "-";
379 uint64_t _sessionId = 0;
380 uint64_t _sessionVersion = 0;
381 String _originNetType = "IN";
382 String _originAddrType = "IP4";
383 String _originAddress = "0.0.0.0";
384 String _connectionAddress;
385 SdpMediaDescription::AttributeList _sessionAttributes;
386 MediaDescriptionList _mediaDescriptions;
387};
388
389PROMEKI_NAMESPACE_END
390
391PROMEKI_FORMAT_VIA_TOSTRING(promeki::SdpSession);
392
393#endif // PROMEKI_ENABLE_CORE