| 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 |
|
| 18 |
#include "transmitbase.hpp" |
| 19 |
#include <udt.h> // Needs access to CUDTException |
| 20 |
|
| 21 |
using namespace std; |
| 22 |
|
| 23 |
// Trial version of an exception. Try to implement later an official |
| 24 |
// interruption mechanism in SRT using this. |
| 25 |
|
| 26 |
struct TransmissionError: public std::runtime_error |
| 27 |
{ |
| 28 |
TransmissionError(const std::string& arg): |
| 29 |
std::runtime_error(arg) |
| 30 |
{ |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
class SrtCommon |
| 35 |
{ |
| 36 |
protected: |
| 37 |
|
| 38 |
bool m_output_direction = false; //< Defines which of SND or RCV option variant should be used, also to set SRT_SENDER for output |
| 39 |
int m_timeout = 0; //< enforces using SRTO_SNDTIMEO or SRTO_RCVTIMEO, depending on @a m_output_direction |
| 40 |
bool m_tsbpdmode = true; |
| 41 |
int m_outgoing_port = 0; |
| 42 |
string m_mode; |
| 43 |
string m_adapter; |
| 44 |
map<string, string> m_options; // All other options, as provided in the URI |
| 45 |
SRTSOCKET m_sock = SRT_INVALID_SOCK; |
| 46 |
SRTSOCKET m_bindsock = SRT_INVALID_SOCK; |
| 47 |
bool IsUsable() { SRT_SOCKSTATUS st = srt_getsockstate(m_sock); return st > SRTS_INIT && st < SRTS_BROKEN; } |
| 48 |
bool IsBroken() { return srt_getsockstate(m_sock) > SRTS_CONNECTED; } |
| 49 |
|
| 50 |
public: |
| 51 |
void InitParameters(string host, map<string,string> par); |
| 52 |
void PrepareListener(string host, int port, int backlog); |
| 53 |
void StealFrom(SrtCommon& src); |
| 54 |
bool AcceptNewClient(); |
| 55 |
|
| 56 |
SRTSOCKET Socket() const { return m_sock; } |
| 57 |
SRTSOCKET Listener() const { return m_bindsock; } |
| 58 |
|
| 59 |
void Close(); |
| 60 |
|
| 61 |
protected: |
| 62 |
|
| 63 |
void Error(string src); |
| 64 |
void Init(string host, int port, map<string,string> par, bool dir_output); |
| 65 |
|
| 66 |
virtual int ConfigurePost(SRTSOCKET sock); |
| 67 |
virtual int ConfigurePre(SRTSOCKET sock); |
| 68 |
|
| 69 |
void OpenClient(string host, int port); |
| 70 |
void PrepareClient(); |
| 71 |
void SetupAdapter(const std::string& host, int port); |
| 72 |
void ConnectClient(string host, int port); |
| 73 |
|
| 74 |
void OpenServer(string host, int port) |
| 75 |
{ |
| 76 |
PrepareListener(host, port, 1); |
| 77 |
} |
| 78 |
|
| 79 |
void OpenRendezvous(string adapter, string host, int port); |
| 80 |
|
| 81 |
virtual ~SrtCommon(); |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
class SrtSource: public Source, public SrtCommon |
| 86 |
{ |
| 87 |
std::string hostport_copy; |
| 88 |
public: |
| 89 |
|
| 90 |
SrtSource(std::string host, int port, const std::map<std::string,std::string>& par); |
| 91 |
SrtSource() |
| 92 |
{ |
| 93 |
// Do nothing - create just to prepare for use |
| 94 |
} |
| 95 |
|
| 96 |
int Read(size_t chunk, MediaPacket& pkt, ostream& out_stats = cout) override; |
| 97 |
|
| 98 |
/* |
| 99 |
In this form this isn't needed. |
| 100 |
Unblock if any extra settings have to be made. |
| 101 |
virtual int ConfigurePre(UDTSOCKET sock) override |
| 102 |
{ |
| 103 |
int result = SrtCommon::ConfigurePre(sock); |
| 104 |
if ( result == -1 ) |
| 105 |
return result; |
| 106 |
return 0; |
| 107 |
} |
| 108 |
*/ |
| 109 |
|
| 110 |
bool IsOpen() override { return IsUsable(); } |
| 111 |
bool End() override { return IsBroken(); } |
| 112 |
|
| 113 |
SRTSOCKET GetSRTSocket() const override |
| 114 |
{ |
| 115 |
SRTSOCKET socket = SrtCommon::Socket(); |
| 116 |
if (socket == SRT_INVALID_SOCK) |
| 117 |
socket = SrtCommon::Listener(); |
| 118 |
return socket; |
| 119 |
} |
| 120 |
|
| 121 |
bool AcceptNewClient() override { return SrtCommon::AcceptNewClient(); } |
| 122 |
}; |
| 123 |
|
| 124 |
class SrtTarget: public Target, public SrtCommon |
| 125 |
{ |
| 126 |
public: |
| 127 |
|
| 128 |
SrtTarget(std::string host, int port, const std::map<std::string,std::string>& par) |
| 129 |
{ |
| 130 |
Init(host, port, par, true); |
| 131 |
} |
| 132 |
|
| 133 |
SrtTarget() {} |
| 134 |
|
| 135 |
int ConfigurePre(SRTSOCKET sock) override; |
| 136 |
int Write(const char* data, size_t size, int64_t src_time, ostream &out_stats = cout) override; |
| 137 |
bool IsOpen() override { return IsUsable(); } |
| 138 |
bool Broken() override { return IsBroken(); } |
| 139 |
|
| 140 |
size_t Still() override |
| 141 |
{ |
| 142 |
size_t bytes; |
| 143 |
int st = srt_getsndbuffer(m_sock, nullptr, &bytes); |
| 144 |
if (st == -1) |
| 145 |
return 0; |
| 146 |
return bytes; |
| 147 |
} |
| 148 |
|
| 149 |
SRTSOCKET GetSRTSocket() const override |
| 150 |
{ |
| 151 |
SRTSOCKET socket = SrtCommon::Socket(); |
| 152 |
if (socket == SRT_INVALID_SOCK) |
| 153 |
socket = SrtCommon::Listener(); |
| 154 |
return socket; |
| 155 |
} |
| 156 |
bool AcceptNewClient() override { return SrtCommon::AcceptNewClient(); } |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
// This class is used when we don't know yet whether the given URI |
| 161 |
// designates an effective listener or caller. So we create it, initialize, |
| 162 |
// then we know what mode we'll be using. |
| 163 |
// |
| 164 |
// When caller, then we will do connect() using this object, then clone out |
| 165 |
// a new object - of a direction specific class - which will steal the socket |
| 166 |
// from this one and then roll the data. After this, this object is ready |
| 167 |
// to connect again, and will create its own socket for that occasion, and |
| 168 |
// the whole procedure repeats. |
| 169 |
// |
| 170 |
// When listener, then this object will be doing accept() and with every |
| 171 |
// successful acceptation it will clone out a new object - of a direction |
| 172 |
// specific class - which will steal just the connection socket from this |
| 173 |
// object. This object will still live on and accept new connections and |
| 174 |
// so on. |
| 175 |
class SrtModel: public SrtCommon |
| 176 |
{ |
| 177 |
public: |
| 178 |
bool is_caller = false; |
| 179 |
string m_host; |
| 180 |
int m_port = 0; |
| 181 |
|
| 182 |
SrtModel(string host, int port, map<string,string> par); |
| 183 |
void Establish(std::string& name); |
| 184 |
}; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
#endif |