UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
rtp.cpp
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2013-2024 CESNET z.s.p.o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <cinttypes>
39#include <map>
40#include <mutex>
41#include <sstream>
42#include <string>
43
44#include "debug.h"
45#include "host.h"
46#include "messaging.h"
47#include "module.h"
48#include "pdb.h"
49#include "rtp/fec.h"
50#include "rtp/pbuf.h"
51#include "rtp/rtp.h"
52#include "rtp/rtp_callback.h"
53#include "rtp/video_decoders.h"
54#include "transmit.h"
55#include "ug_runtime_error.hpp"
56#include "utils/net.h" // IN6_BLACKHOLE_STR
57#include "video.h"
58#include "video_compress.h"
59#include "video_decompress.h"
60#include "video_display.h"
61#include "video_rxtx.hpp"
62#include "video_rxtx/rtp.hpp"
63
64#define MOD_NAME "[video_rxtx/rtp] "
65
66using std::lock_guard;
67using std::map;
68using std::mutex;
69using std::ostringstream;
70using std::string;
71
72struct response *
74{
75 switch (msg->type) {
77 assert(m_rxtx_mode == MODE_SENDER); // sender only
78 lock_guard<mutex> lock(m_network_devices_lock);
79 auto *old_device = m_network_device;
80 auto old_receiver = m_requested_receiver;
81 m_requested_receiver = msg->receiver;
87 if (m_network_device == nullptr) {
88 m_network_device = old_device;
89 m_requested_receiver = std::move(old_receiver);
90 MSG(ERROR, "Failed receiver to %s.\n", msg->receiver);
92 "Changing receiver failed!");
93 }
94 MSG(NOTICE, "Changed receiver to %s.\n", msg->receiver);
95 destroy_rtp_device(old_device);
96 } break;
98 assert(m_rxtx_mode == MODE_SENDER); // sender only
99 lock_guard<mutex> lock(m_network_devices_lock);
100 auto *old_device = m_network_device;
101 auto old_port = m_send_port_number;
102
103 m_send_port_number = msg->tx_port;
104 if (msg->rx_port != 0) {
105 m_recv_port_number = msg->rx_port;
106 }
112
113 if (m_network_device == nullptr) {
114 m_network_device = old_device;
115 m_send_port_number = old_port;
116 MSG(ERROR, "Failed to Change TX port to %d.\n",
117 msg->tx_port);
119 "Changing TX port failed!");
120 }
121 MSG(NOTICE, "Changed TX port to %d.\n", msg->tx_port);
122 destroy_rtp_device(old_device);
123 } break;
125 lock_guard<mutex> lock(m_network_devices_lock);
126 auto *old_fec_state = m_fec_state;
127 m_fec_state = nullptr;
128 if (strcmp(msg->fec_cfg, "flush") == 0) {
129 delete old_fec_state;
130 break;
131 }
132 m_fec_state = fec::create_from_config(msg->fec_cfg, false);
133 if (m_fec_state == nullptr) {
134 int rc = 0;
135 if (strstr(msg->fec_cfg, "help") == nullptr) {
136 MSG(ERROR, "Unable to initalize FEC!\n");
137 rc = 1;
138 }
139
140 // Exit only if we failed because of command line
141 // params, not control port msg
142 if (m_frames_sent == 0ULL) {
143 exit_uv(rc);
144 }
145
146 m_fec_state = old_fec_state;
147 return new_response(RESPONSE_INT_SERV_ERR, nullptr);
148 }
149 delete old_fec_state;
150 MSG(NOTICE, "Fec changed successfully\n");
151 } break;
153 if (!m_video_desc) {
154 return new_response(RESPONSE_NO_CONTENT, nullptr);
155 }
156 ostringstream oss;
157 oss << m_video_desc;
158 return new_response(RESPONSE_OK, oss.str().c_str());
159 }
161 lock_guard<mutex> lock(m_network_devices_lock);
162 const uint32_t old_ssrc = rtp_my_ssrc(m_network_device);
163 auto *old_device = m_network_device;
169 if (m_network_device == nullptr) {
170 m_network_device = old_device;
171 MSG(ERROR, "Unable to change SSRC!\n");
172 return new_response(RESPONSE_INT_SERV_ERR, nullptr);
173 }
174 destroy_rtp_device(old_device);
175 MSG(NOTICE,
176 "Changed SSRC from 0x%08" PRIx32 " to "
177 "0x%08" PRIx32 ".\n",
178 old_ssrc, rtp_my_ssrc(m_network_device));
179 } break;
181 case SENDER_MSG_MUTE:
184 MSG(ERROR, "Unexpected audio message ID %d!\n", msg->type);
185 return new_response(RESPONSE_INT_SERV_ERR, nullptr);
186 default:
187 MSG(ERROR, "Unknown message ID %d!\n", msg->type);
188 return new_response(RESPONSE_INT_SERV_ERR, nullptr);
189 }
190
191 return new_response(RESPONSE_OK, nullptr);
192}
193
194rtp_video_rxtx::rtp_video_rxtx(map<string, param_u> const &params) :
195 video_rxtx(params), m_fec_state(NULL), m_video_desc{}
196{
198 m_requested_receiver = params.at("receiver").str;
199 m_recv_port_number = params.at("rx_port").i;
200 m_send_port_number = params.at("tx_port").i;
201
206 if (m_network_device == nullptr) {
207 throw ug_runtime_error("Unable to open network",
209 }
210
213 params.at("fec").str,
215 params.at("bitrate").ll)) == NULL) {
216 throw ug_runtime_error("Unable to initialize transmitter", EXIT_FAIL_TRANSMIT);
217 }
218
219 // The idea of doing that is to display help on '-f ldgm:help' even if UG would exit
220 // immediatelly. The encoder is actually created by a message.
222}
223
225{
226 if (m_tx) {
228 }
229
232 m_network_devices_lock.unlock();
233
234 if (m_participants != NULL) {
236 }
237
238 delete m_fec_state;
239}
240
242{
243 log_msg(LOG_LEVEL_INFO, "\n***\n"
244 "Unable to set buffer size to %d B.\n"
245#if defined _WIN32
246 "See https://github.com/CESNET/UltraGrid/wiki/Extending-Network-Buffers-%%28Windows%%29 for details.\n",
247#else
248 "Please set net.core.rmem_max value to %d or greater (see also\n"
249 "https://github.com/CESNET/UltraGrid/wiki/OS-Setup-UltraGrid):\n"
250#ifdef __APPLE__
251 "\tsysctl -w kern.ipc.maxsockbuf=%d\n"
252 "\tsysctl -w net.inet.udp.recvspace=%d\n"
253#else
254 "\tsysctl -w net.core.rmem_max=%d\n"
255#endif
256 "To make this persistent, add these options (key=value) to /etc/sysctl.conf\n"
257 "\n***\n\n",
258 size, size,
259#ifdef __APPLE__
260 size * 4,
261#endif /* __APPLE__ */
262#endif /* ! defined _WIN32 */
263 size);
264
265}
266
267struct rtp *rtp_video_rxtx::initialize_network(const char *addr, int recv_port,
268 int send_port, struct pdb *participants, int force_ip_version,
269 const char *mcast_if, int ttl)
270{
271 double rtcp_bw = 5 * 1024 * 1024; /* FIXME */
272
273#if !defined _WIN32
274 const bool multithreaded = true;
275#else
276 const bool multithreaded = false;
277#endif
278
279 if (strlen(mcast_if) == 0) {
280 mcast_if = nullptr;
281 }
282 struct rtp *device =
283 rtp_init_if(addr, mcast_if, recv_port, send_port, ttl, rtcp_bw,
284 FALSE, rtp_recv_callback, (uint8_t *) participants,
285 force_ip_version, multithreaded);
286 if (device == nullptr) {
287 return nullptr;
288 }
291 rtp_set_sdes(device, rtp_my_ssrc(device),
292 RTCP_SDES_TOOL, PACKAGE_STRING, strlen(PACKAGE_STRING));
293 if (strcmp(addr, IN6_BLACKHOLE_SERVER_MODE_STR) == 0) {
295 }
296
299
300 pdb_add(participants, rtp_my_ssrc(device));
301
302 return device;
303}
304
305void rtp_video_rxtx::destroy_rtp_device(struct rtp *network_device)
306{
307 rtp_done(network_device);
308}
309
fec * m_fec_state
Definition rtp.hpp:78
std::mutex m_network_devices_lock
Definition rtp.hpp:72
struct response * process_sender_message(struct msg_sender *msg) override
Definition rtp.cpp:73
static struct rtp * initialize_network(const char *addrs, int recv_port_base, int send_port_base, struct pdb *participants, int force_ip_version, const char *mcast_if, int ttl)
Definition rtp.cpp:267
int m_recv_port_number
Definition rtp.hpp:76
struct rtp * m_network_device
Definition rtp.hpp:71
static void destroy_rtp_device(struct rtp *network_devices)
Definition rtp.cpp:305
struct pdb * m_participants
Definition rtp.hpp:74
rtp_video_rxtx(std::map< std::string, param_u > const &params)
Definition rtp.cpp:194
std::string m_requested_receiver
Definition rtp.hpp:75
virtual ~rtp_video_rxtx()
Definition rtp.cpp:224
int m_send_port_number
Definition rtp.hpp:77
video_desc m_video_desc
Definition rtp.hpp:79
static void display_buf_increase_warning(int size)
Definition rtp.cpp:241
struct tx * m_tx
Definition rtp.hpp:73
#define TRUE
#define FALSE
void log_msg(int level, const char *format,...)
Definition debug.cpp:130
#define LOG_LEVEL_INFO
normal reporting
Definition debug.h:62
#define MSG(l, fmt,...)
Definition debug.h:96
void module_done(struct module *module_data)
Definition module.c:93
#define CAST_MODULE(x)
Definition module.h:197
void exit_uv(int status)
Definition host.cpp:625
volatile int video_offset
added video delay in ms (non-negative), can be used to tune AV sync
Definition host.cpp:179
#define MODE_SENDER
Definition host.h:102
#define EXIT_FAIL_TRANSMIT
Definition host.h:66
#define EXIT_FAIL_NETWORK
Definition host.h:69
struct response * new_response(int status, const char *text)
@ SENDER_MSG_MUTE_TOGGLE
Definition messaging.h:89
@ SENDER_MSG_RESET_SSRC
Definition messaging.h:92
@ SENDER_MSG_QUERY_VIDEO_MODE
Definition messaging.h:91
@ SENDER_MSG_GET_STATUS
Definition messaging.h:86
@ SENDER_MSG_MUTE
Definition messaging.h:87
@ SENDER_MSG_CHANGE_FEC
Definition messaging.h:90
@ SENDER_MSG_CHANGE_RECEIVER
Definition messaging.h:84
@ SENDER_MSG_UNMUTE
Definition messaging.h:88
@ SENDER_MSG_CHANGE_PORT
Definition messaging.h:85
#define RESPONSE_NO_CONTENT
Definition messaging.h:59
#define RESPONSE_OK
Definition messaging.h:57
#define RESPONSE_INT_SERV_ERR
Definition messaging.h:63
#define IN6_BLACKHOLE_SERVER_MODE_STR
Definition net.h:52
struct pdb * pdb_init(volatile int *delay_ms)
Definition pdb.c:244
int pdb_add(struct pdb *db, uint32_t ssrc)
Definition pdb.c:299
void pdb_destroy(struct pdb **db_p)
Definition pdb.c:256
bool rtp_set_send_buf(struct rtp *session, int bufsize)
Definition rtp.c:3954
bool rtp_set_recv_buf(struct rtp *session, int bufsize)
Definition rtp.c:3938
bool rtp_set_option(struct rtp *session, rtp_option optname, int optval)
Definition rtp.c:1308
struct rtp * rtp_init_if(const char *addr, const char *iface, uint16_t rx_port, uint16_t tx_port, int ttl, double rtcp_bw, int tfrc_on, rtp_callback callback, uint8_t *userdata, int force_ip_version, bool multithreaded)
Definition rtp.c:1034
uint32_t rtp_my_ssrc(struct rtp *session)
Definition rtp.c:1401
bool rtp_set_sdes(struct rtp *session, uint32_t ssrc, rtcp_sdes_type type, const char *value, int length)
Definition rtp.c:2546
void rtp_done(struct rtp *session)
Definition rtp.c:3676
@ RTCP_SDES_TOOL
Definition rtp.h:215
@ RTP_OPT_PROMISC
Definition rtp.h:228
@ RTP_OPT_WEAK_VALIDATION
Definition rtp.h:229
@ RTP_OPT_SEND_BACK
Definition rtp.h:236
#define INITIAL_VIDEO_SEND_BUFFER_SIZE
Definition rtp.hpp:53
#define INITIAL_VIDEO_RECV_BUFFER_SIZE
Definition rtp.hpp:50
void rtp_recv_callback(struct rtp *session, rtp_event *e)
char encryption[STR_LEN]
Definition host.h:172
int mtu
Definition host.h:174
int ttl
Definition host.h:175
char mcast_if[STR_LEN]
Definition host.h:173
int force_ip_version
Definition host.h:176
static fec * create_from_config(const char *str, bool is_audio) noexcept
Definition fec.cpp:62
Definition pdb.c:84
Definition rtp.c:274
unsigned long long int m_frames_sent
struct module m_sender_mod
void check_sender_messages()
struct common_opts m_common
struct tx * tx_init(struct module *parent, unsigned mtu, enum tx_media_type media_type, const char *fec, const char *encryption, long long int bitrate)
Definition transmit.cpp:201
@ TX_MEDIA_VIDEO
Definition types.h:365
This is an umbrella header for video functions.
API for video compress drivers.
Video RTP decoder.
API for video decompress drivers.