| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#ifndef INC_SRT_COMMON_TRANSMITMEDIA_HPP |
| 12 |
#define INC_SRT_COMMON_TRANSMITMEDIA_HPP |
| 13 |
|
| 14 |
#include <string> |
| 15 |
#include <map> |
| 16 |
#include <stdexcept> |
| 17 |
#include <deque> |
| 18 |
|
| 19 |
#include <sync.h> // use srt::sync::atomic instead of std::atomic for the sake of logging |
| 20 |
|
| 21 |
#include "apputil.hpp" |
| 22 |
#include "statswriter.hpp" |
| 23 |
#include "testmediabase.hpp" |
| 24 |
#include <udt.h> // Needs access to CUDTException |
| 25 |
#include <netinet_any.h> |
| 26 |
|
| 27 |
extern srt_listen_callback_fn* transmit_accept_hook_fn; |
| 28 |
extern void* transmit_accept_hook_op; |
| 29 |
extern srt::sync::atomic<bool> transmit_int_state; |
| 30 |
|
| 31 |
extern std::shared_ptr<SrtStatsWriter> transmit_stats_writer; |
| 32 |
|
| 33 |
const srt_logging::LogFA SRT_LOGFA_APP = 10; |
| 34 |
extern srt_logging::Logger applog; |
| 35 |
|
| 36 |
// Trial version of an exception. Try to implement later an official |
| 37 |
// interruption mechanism in SRT using this. |
| 38 |
|
| 39 |
struct TransmissionError: public std::runtime_error |
| 40 |
{ |
| 41 |
TransmissionError(const std::string& arg): |
| 42 |
std::runtime_error(arg) |
| 43 |
{ |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
class SrtCommon |
| 48 |
{ |
| 49 |
int srt_conn_epoll = -1; |
| 50 |
|
| 51 |
void SpinWaitAsync(); |
| 52 |
|
| 53 |
protected: |
| 54 |
|
| 55 |
friend void TransmitGroupSocketConnect(void* srtcommon, SRTSOCKET sock, int error, const sockaddr* peer, int token); |
| 56 |
|
| 57 |
struct ConnectionBase |
| 58 |
{ |
| 59 |
std::string host; |
| 60 |
int port; |
| 61 |
int weight = 0; |
| 62 |
SRTSOCKET socket = SRT_INVALID_SOCK; |
| 63 |
srt::sockaddr_any source; |
| 64 |
srt::sockaddr_any target; |
| 65 |
int token = -1; |
| 66 |
|
| 67 |
ConnectionBase(std::string h, int p): host(h), port(p), source(AF_INET) {} |
| 68 |
}; |
| 69 |
|
| 70 |
struct Connection: ConnectionBase |
| 71 |
{ |
| 72 |
#if ENABLE_BONDING |
| 73 |
SRT_SOCKOPT_CONFIG* options = nullptr; |
| 74 |
#endif |
| 75 |
int error = SRT_SUCCESS; |
| 76 |
int reason = SRT_REJ_UNKNOWN; |
| 77 |
|
| 78 |
Connection(std::string h, int p): ConnectionBase(h, p) {} |
| 79 |
Connection(Connection&& old): ConnectionBase(old) |
| 80 |
{ |
| 81 |
#if ENABLE_BONDING |
| 82 |
if (old.options) |
| 83 |
{ |
| 84 |
options = old.options; |
| 85 |
old.options = nullptr; |
| 86 |
} |
| 87 |
#endif |
| 88 |
} |
| 89 |
~Connection() |
| 90 |
{ |
| 91 |
#if ENABLE_BONDING |
| 92 |
srt_delete_config(options); |
| 93 |
#endif |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
int srt_epoll = -1; |
| 98 |
SRT_EPOLL_T m_direction = SRT_EPOLL_OPT_NONE; //< Defines which of SND or RCV option variant should be used, also to set SRT_SENDER for output |
| 99 |
bool m_blocking_mode = true; //< enforces using SRTO_SNDSYN or SRTO_RCVSYN, depending on @a m_direction |
| 100 |
int m_timeout = 0; //< enforces using SRTO_SNDTIMEO or SRTO_RCVTIMEO, depending on @a m_direction |
| 101 |
bool m_tsbpdmode = true; |
| 102 |
int m_outgoing_port = 0; |
| 103 |
std::string m_mode; |
| 104 |
std::string m_adapter; |
| 105 |
std::map<std::string, std::string> m_options; // All other options, as provided in the URI |
| 106 |
std::vector<Connection> m_group_nodes; |
| 107 |
std::string m_group_type; |
| 108 |
std::string m_group_config; |
| 109 |
#if ENABLE_BONDING |
| 110 |
std::vector<SRT_SOCKGROUPDATA> m_group_data; |
| 111 |
#ifdef SRT_OLD_APP_READER |
| 112 |
int32_t m_group_seqno = -1; |
| 113 |
|
| 114 |
struct ReadPos |
| 115 |
{ |
| 116 |
int32_t sequence; |
| 117 |
bytevector packet; |
| 118 |
}; |
| 119 |
std::map<SRTSOCKET, ReadPos> m_group_positions; |
| 120 |
SRTSOCKET m_group_active; // The link from which the last packet was delivered |
| 121 |
#endif |
| 122 |
#endif |
| 123 |
|
| 124 |
SRTSOCKET m_sock = SRT_INVALID_SOCK; |
| 125 |
SRTSOCKET m_bindsock = SRT_INVALID_SOCK; |
| 126 |
bool m_listener_group = false; |
| 127 |
bool IsUsable() { SRT_SOCKSTATUS st = srt_getsockstate(m_sock); return st > SRTS_INIT && st < SRTS_BROKEN; } |
| 128 |
bool IsBroken() { return srt_getsockstate(m_sock) > SRTS_CONNECTED; } |
| 129 |
|
| 130 |
void UpdateGroupStatus(const SRT_SOCKGROUPDATA* grpdata, size_t grpdata_size); |
| 131 |
|
| 132 |
public: |
| 133 |
void InitParameters(std::string host, std::string path, std::map<std::string,std::string> par); |
| 134 |
void PrepareListener(std::string host, int port, int backlog); |
| 135 |
void StealFrom(SrtCommon& src); |
| 136 |
void AcceptNewClient(); |
| 137 |
|
| 138 |
SRTSOCKET Socket() const { return m_sock; } |
| 139 |
SRTSOCKET Listener() const { return m_bindsock; } |
| 140 |
|
| 141 |
void Acquire(SRTSOCKET s) |
| 142 |
{ |
| 143 |
m_sock = s; |
| 144 |
if (s & SRTGROUP_MASK) |
| 145 |
m_listener_group = true; |
| 146 |
} |
| 147 |
|
| 148 |
virtual void Close(); |
| 149 |
|
| 150 |
protected: |
| 151 |
|
| 152 |
void Error(std::string src, int reason = SRT_REJ_UNKNOWN, int force_result = 0); |
| 153 |
void Init(std::string host, int port, std::string path, std::map<std::string,std::string> par, SRT_EPOLL_OPT dir); |
| 154 |
int AddPoller(SRTSOCKET socket, int modes); |
| 155 |
virtual int ConfigurePost(SRTSOCKET sock); |
| 156 |
virtual int ConfigurePre(SRTSOCKET sock); |
| 157 |
|
| 158 |
void OpenClient(std::string host, int port); |
| 159 |
#if ENABLE_BONDING |
| 160 |
void OpenGroupClient(); |
| 161 |
#endif |
| 162 |
void PrepareClient(); |
| 163 |
void SetupAdapter(const std::string& host, int port); |
| 164 |
void ConnectClient(std::string host, int port); |
| 165 |
void SetupRendezvous(std::string adapter, std::string host, int port); |
| 166 |
|
| 167 |
void OpenServer(std::string host, int port, int backlog = 1) |
| 168 |
{ |
| 169 |
PrepareListener(host, port, backlog); |
| 170 |
if (transmit_accept_hook_fn) |
| 171 |
{ |
| 172 |
srt_listen_callback(m_bindsock, transmit_accept_hook_fn, transmit_accept_hook_op); |
| 173 |
} |
| 174 |
AcceptNewClient(); |
| 175 |
} |
| 176 |
|
| 177 |
void OpenRendezvous(std::string adapter, std::string host, int port) |
| 178 |
{ |
| 179 |
PrepareClient(); |
| 180 |
SetupRendezvous(adapter, host, port); |
| 181 |
ConnectClient(host, port); |
| 182 |
} |
| 183 |
|
| 184 |
virtual ~SrtCommon(); |
| 185 |
}; |
| 186 |
|
| 187 |
|
| 188 |
class SrtSource: public virtual Source, public virtual SrtCommon |
| 189 |
{ |
| 190 |
std::string hostport_copy; |
| 191 |
public: |
| 192 |
|
| 193 |
SrtSource(std::string host, int port, std::string path, const std::map<std::string,std::string>& par); |
| 194 |
SrtSource() |
| 195 |
{ |
| 196 |
// Do nothing - create just to prepare for use |
| 197 |
} |
| 198 |
|
| 199 |
MediaPacket Read(size_t chunk) override; |
| 200 |
bytevector GroupRead(size_t chunk); |
| 201 |
bool GroupCheckPacketAhead(bytevector& output); |
| 202 |
|
| 203 |
|
| 204 |
/* |
| 205 |
In this form this isn't needed. |
| 206 |
Unblock if any extra settings have to be made. |
| 207 |
virtual int ConfigurePre(UDTSOCKET sock) override |
| 208 |
{ |
| 209 |
int result = SrtCommon::ConfigurePre(sock); |
| 210 |
if ( result == -1 ) |
| 211 |
return result; |
| 212 |
return 0; |
| 213 |
} |
| 214 |
*/ |
| 215 |
|
| 216 |
bool IsOpen() override { return IsUsable(); } |
| 217 |
bool End() override { return IsBroken(); } |
| 218 |
void Close() override { return SrtCommon::Close(); } |
| 219 |
}; |
| 220 |
|
| 221 |
class SrtTarget: public virtual Target, public virtual SrtCommon |
| 222 |
{ |
| 223 |
public: |
| 224 |
|
| 225 |
SrtTarget(std::string host, int port, std::string path, const std::map<std::string,std::string>& par); |
| 226 |
SrtTarget() {} |
| 227 |
|
| 228 |
int ConfigurePre(SRTSOCKET sock) override; |
| 229 |
void Write(const MediaPacket& data) override; |
| 230 |
bool IsOpen() override { return IsUsable(); } |
| 231 |
bool Broken() override { return IsBroken(); } |
| 232 |
void Close() override { return SrtCommon::Close(); } |
| 233 |
|
| 234 |
size_t Still() override |
| 235 |
{ |
| 236 |
size_t bytes; |
| 237 |
int st = srt_getsndbuffer(m_sock, nullptr, &bytes); |
| 238 |
if (st == -1) |
| 239 |
return 0; |
| 240 |
return bytes; |
| 241 |
} |
| 242 |
|
| 243 |
}; |
| 244 |
|
| 245 |
class SrtRelay: public Relay, public SrtSource, public SrtTarget |
| 246 |
{ |
| 247 |
public: |
| 248 |
SrtRelay(std::string host, int port, std::string path, const std::map<std::string,std::string>& par); |
| 249 |
SrtRelay() {} |
| 250 |
|
| 251 |
int ConfigurePre(SRTSOCKET sock) override |
| 252 |
{ |
| 253 |
// This overrides the change introduced in SrtTarget, |
| 254 |
// which sets the SRTO_SENDER flag. For a bidirectional transmission |
| 255 |
// this flag should not be set, as the connection should be checked |
| 256 |
// for being 1.3.0 clients only. |
| 257 |
return SrtCommon::ConfigurePre(sock); |
| 258 |
} |
| 259 |
|
| 260 |
// Override separately overridden methods by SrtSource and SrtTarget |
| 261 |
bool IsOpen() override { return IsUsable(); } |
| 262 |
void Close() override { return SrtCommon::Close(); } |
| 263 |
}; |
| 264 |
|
| 265 |
|
| 266 |
// This class is used when we don't know yet whether the given URI |
| 267 |
// designates an effective listener or caller. So we create it, initialize, |
| 268 |
// then we know what mode we'll be using. |
| 269 |
// |
| 270 |
// When caller, then we will do connect() using this object, then clone out |
| 271 |
// a new object - of a direction specific class - which will steal the socket |
| 272 |
// from this one and then roll the data. After this, this object is ready |
| 273 |
// to connect again, and will create its own socket for that occasion, and |
| 274 |
// the whole procedure repeats. |
| 275 |
// |
| 276 |
// When listener, then this object will be doing accept() and with every |
| 277 |
// successful acceptation it will clone out a new object - of a direction |
| 278 |
// specific class - which will steal just the connection socket from this |
| 279 |
// object. This object will still live on and accept new connections and |
| 280 |
// so on. |
| 281 |
class SrtModel: public SrtCommon |
| 282 |
{ |
| 283 |
public: |
| 284 |
bool is_caller = false; |
| 285 |
bool is_rend = false; |
| 286 |
std::string m_host; |
| 287 |
int m_port = 0; |
| 288 |
|
| 289 |
|
| 290 |
SrtModel(std::string host, int port, std::map<std::string,std::string> par); |
| 291 |
void Establish(std::string& w_name); |
| 292 |
|
| 293 |
void Close() |
| 294 |
{ |
| 295 |
if (m_sock != SRT_INVALID_SOCK) |
| 296 |
{ |
| 297 |
srt_close(m_sock); |
| 298 |
m_sock = SRT_INVALID_SOCK; |
| 299 |
} |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
class UdpCommon |
| 304 |
{ |
| 305 |
protected: |
| 306 |
int m_sock = -1; |
| 307 |
std::string adapter; |
| 308 |
srt::sockaddr_any interface_addr; |
| 309 |
srt::sockaddr_any target_addr; |
| 310 |
bool is_multicast = false; |
| 311 |
std::map<std::string, std::string> m_options; |
| 312 |
void Setup(std::string host, int port, std::map<std::string,std::string> attr); |
| 313 |
void Error(int err, std::string src); |
| 314 |
|
| 315 |
~UdpCommon(); |
| 316 |
}; |
| 317 |
|
| 318 |
|
| 319 |
class UdpSource: public virtual Source, public virtual UdpCommon |
| 320 |
{ |
| 321 |
bool eof = true; |
| 322 |
public: |
| 323 |
|
| 324 |
UdpSource(std::string host, int port, const std::map<std::string,std::string>& attr); |
| 325 |
|
| 326 |
MediaPacket Read(size_t chunk) override; |
| 327 |
|
| 328 |
bool IsOpen() override { return m_sock != -1; } |
| 329 |
bool End() override { return eof; } |
| 330 |
}; |
| 331 |
|
| 332 |
class UdpTarget: public virtual Target, public virtual UdpCommon |
| 333 |
{ |
| 334 |
public: |
| 335 |
UdpTarget(std::string host, int port, const std::map<std::string,std::string>& attr); |
| 336 |
|
| 337 |
void Write(const MediaPacket& data) override; |
| 338 |
bool IsOpen() override { return m_sock != -1; } |
| 339 |
bool Broken() override { return false; } |
| 340 |
}; |
| 341 |
|
| 342 |
class UdpRelay: public Relay, public UdpSource, public UdpTarget |
| 343 |
{ |
| 344 |
public: |
| 345 |
UdpRelay(std::string host, int port, const std::map<std::string,std::string>& attr): |
| 346 |
UdpSource(host, port, attr), |
| 347 |
UdpTarget(host, port, attr) |
| 348 |
{ |
| 349 |
} |
| 350 |
|
| 351 |
bool IsOpen() override { return m_sock != -1; } |
| 352 |
}; |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
#endif |