libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
srtsocket.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_SRT
13#include <promeki/iodevice.h>
14#include <promeki/error.h>
16#include <promeki/string.h>
17#include <promeki/uniqueptr.h>
18
19PROMEKI_NAMESPACE_BEGIN
20
76class SrtSocket : public IODevice {
77 PROMEKI_OBJECT(SrtSocket, IODevice)
78 public:
80 using UPtr = UniquePtr<SrtSocket>;
81
83 static constexpr int InvalidHandle = -1;
84
95 struct Stats {
97 int64_t msSinceStart = 0;
99 int64_t pktSent = 0;
101 int64_t pktRecv = 0;
103 uint64_t byteSent = 0;
105 uint64_t byteRecv = 0;
107 int pktSndLost = 0;
109 int pktRcvLost = 0;
111 int pktRetransmitted = 0;
113 int pktSndDrop = 0;
115 int pktRcvDrop = 0;
117 int pktRcvUndecrypt = 0;
119 double rttMs = 0.0;
121 double linkBandwidthMbps = 0.0;
123 double maxBandwidthMbps = 0.0;
125 int sendBufferBytes = 0;
127 int sendBufferMs = 0;
129 int recvBufferBytes = 0;
131 int recvBufferMs = 0;
132 };
133
135 enum TransportType {
136 Live,
137 File
138 };
139
141 enum SocketState {
142 Init,
143 Opened,
144 Listening,
145 Connecting,
146 Connected,
147 Broken,
148 Closing,
149 Closed,
150 NonExist
151 };
152
157 SrtSocket(ObjectBase *parent = nullptr);
158
171 SrtSocket(int handle, ObjectBase *parent = nullptr);
172
174 ~SrtSocket() override;
175
188 Error open(OpenMode mode) override;
189
194 Error close() override;
195
197 bool isOpen() const override;
198
211 int64_t read(void *data, int64_t maxSize) override;
212
224 int64_t write(const void *data, int64_t maxSize) override;
225
227 bool isSequential() const override { return true; }
228
240 Error bind(const SocketAddress &address);
241
255 Error connectToHost(const SocketAddress &address);
256
270 Error waitForConnected(unsigned int timeoutMs = 0);
271
273 SocketAddress localAddress() const { return _localAddress; }
274
276 SocketAddress peerAddress() const { return _peerAddress; }
277
279 SocketState state() const;
280
282 PROMEKI_SIGNAL(connected);
283
285 PROMEKI_SIGNAL(disconnected);
286
296 int handle() const { return _sock; }
297
314 int groupHandle() const;
315
316 // ---- SRT-specific configuration knobs ----------------
317
332 Error setLatency(int ms);
333
335 int latency() const { return _latencyMs; }
336
349 Error setPayloadSize(int bytes);
350
352 int payloadSize() const { return _payloadSize; }
353
359 Error setTransportType(TransportType type);
360
362 TransportType transportType() const { return _transportType; }
363
376 Error setPassphrase(const String &passphrase);
377
379 bool hasPassphrase() const { return !_passphrase.isEmpty(); }
380
387 Error setEncryptionKeyLength(int bytes);
388
390 int encryptionKeyLength() const { return _pbKeyLen; }
391
403 Error setStreamId(const String &id);
404
406 String streamId() const;
407
413 Error setMaxBandwidth(int64_t bytesPerSec);
414
420 Error setReceiveBufferSize(int bytes);
421
427 Error setSendBufferSize(int bytes);
428
434 Error setReceiveTimeout(int timeoutMs);
435
441 Error setSendTimeout(int timeoutMs);
442
448 Error setConnectTimeout(int timeoutMs);
449
464 Error setRendezvous(bool enable);
465
467 bool isRendezvous() const { return _rendezvous; }
468
478 Error setNonBlocking(bool enable);
479
484 String lastSrtError() const { return _lastError; }
485
502 Stats stats(bool resetWindowed = false) const;
503
504 protected:
512 void updateLocalAddress();
513
521 void updatePeerAddress();
522
530 void captureLastError();
531
532 private:
533 friend class SrtServer;
534
535 int _sock = InvalidHandle;
536 SocketAddress _localAddress;
537 SocketAddress _peerAddress;
538 String _passphrase;
539 String _streamId;
540 String _lastError;
541 int _latencyMs = 120;
542 int _payloadSize = 0;
543 int _pbKeyLen = 0;
544 bool _rendezvous = false;
545 TransportType _transportType = Live;
546
554 Error applyPreConnectOptions();
555};
556
557PROMEKI_NAMESPACE_END
558
559#endif // PROMEKI_ENABLE_SRT