access_out: rist: improve/fix config help text
[vlc.git] / modules / access_output / rist.c
blob0097291bedf3f266f702c28d06a94931be024e9f
1 /*****************************************************************************
2 * * rist.c: RIST (Reliable Internet Stream Transport) output module
3 *****************************************************************************
4 * Copyright (C) 2018, DVEO, the Broadcast Division of Computer Modules, Inc.
5 * Copyright (C) 2018, SipRadius LLC
7 * Authors: Sergio Ammirata <sergio@ammirata.net>
8 * Daniele Lacamera <root@danielinux.net>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <vlc_common.h>
30 #include <vlc_interrupt.h>
31 #include <vlc_fs.h>
32 #include <vlc_plugin.h>
33 #include <vlc_sout.h>
34 #include <vlc_block.h>
35 #include <vlc_network.h>
36 #include <vlc_threads.h>
37 #include <vlc_rand.h>
38 #ifdef HAVE_POLL
39 #include <poll.h>
40 #endif
41 #include <sys/time.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
44 #endif
45 #include <bitstream/ietf/rtcp_rr.h>
46 #include <bitstream/ietf/rtcp_sr.h>
47 #include <bitstream/ietf/rtcp_fb.h>
48 #include <bitstream/ietf/rtcp_sdes.h>
49 #include <bitstream/ietf/rtp.h>
51 #include "../access/rist.h"
53 /* Uncomment the following to introduce induced packet loss for TESTING purposes only */
54 /*#define TEST_PACKET_LOSS*/
56 /* The default target packet size */
57 #define RIST_TARGET_PACKET_SIZE 1328
58 /* The default caching delay for output data */
59 #define DEFAULT_CACHING_DELAY 50
60 /* The default buffer size in ms */
61 #define DEFAULT_BUFFER_SIZE 0
62 /* Calculate and print stats once per second */
63 #define STATS_INTERVAL 1000 /*ms*/
65 #define MPEG_II_TRANSPORT_STREAM (0x21)
66 #define RIST_DEFAULT_PORT 1968
68 #define SOUT_CFG_PREFIX "sout-rist-"
70 static const char *const ppsz_sout_options[] = {
71 "packet-size",
72 "caching",
73 "buffer-size",
74 "ssrc",
75 NULL
78 typedef struct
80 struct rist_flow *flow;
81 uint16_t rtp_counter;
82 char receiver_name[MAX_CNAME];
83 uint64_t last_rtcp_tx;
84 vlc_thread_t ristthread;
85 vlc_thread_t senderthread;
86 size_t i_packet_size;
87 bool b_mtu_warning;
88 vlc_mutex_t lock;
89 vlc_mutex_t fd_lock;
90 block_t *p_pktbuffer;
91 uint64_t i_ticks_caching;
92 uint32_t ssrc;
93 block_fifo_t *p_fifo;
94 /* stats variables */
95 uint64_t i_last_stat;
96 uint32_t i_retransmit_packets;
97 uint32_t i_total_packets;
98 } sout_access_out_sys_t;
100 static struct rist_flow *rist_init_tx()
102 struct rist_flow *flow = calloc(1, sizeof(struct rist_flow));
103 if (!flow)
104 return NULL;
106 flow->reset = 1;
107 flow->buffer = calloc(RIST_QUEUE_SIZE, sizeof(struct rtp_pkt));
108 if ( unlikely( flow->buffer == NULL ) )
110 free(flow);
111 return NULL;
114 return flow;
117 static struct rist_flow *rist_udp_transmitter(sout_access_out_t *p_access, char *psz_dst_server,
118 int i_dst_port)
120 struct rist_flow *flow;
121 flow = rist_init_tx();
122 if (!flow)
123 return NULL;
125 flow->fd_out = net_ConnectDgram(p_access, psz_dst_server, i_dst_port, -1, IPPROTO_UDP );
126 if (flow->fd_out < 0)
128 msg_Err( p_access, "cannot open output socket" );
129 goto fail;
132 flow->fd_rtcp = net_ConnectDgram(p_access, psz_dst_server, i_dst_port + 1, -1, IPPROTO_UDP );
133 if (flow->fd_rtcp < 0)
135 msg_Err( p_access, "cannot open nack socket" );
136 goto fail;
139 populate_cname(flow->fd_rtcp, flow->cname);
140 msg_Info(p_access, "our cname is %s", flow->cname);
142 return flow;
144 fail:
145 free(flow->buffer);
146 free(flow);
147 return NULL;
150 static void rist_retransmit(sout_access_out_t *p_access, struct rist_flow *flow, uint16_t seq)
152 sout_access_out_sys_t *p_sys = p_access->p_sys;
153 struct rtp_pkt *pkt = &(flow->buffer[seq]);
154 if (pkt->buffer == NULL)
156 msg_Err(p_access, "RIST recovery: missing requested packet %d, buffer not yet full", seq);
157 return;
160 /* Mark SSID for retransmission (change the last bit of the ssrc to 1) */
161 pkt->buffer->p_buffer[11] |= (1 << 0);
162 #ifdef TEST_PACKET_LOSS
163 # warning COMPILED WITH SELF INFLICTED PACKET LOSS
164 if ((flow->packets_count % 14) == 0) {
165 return;
167 #endif
168 uint32_t rtp_age = flow->hi_timestamp - pkt->rtp_ts;
169 uint64_t age = ts_get_from_rtp(rtp_age)/1000;
170 if (flow->rtp_latency > 0 && rtp_age > flow->rtp_latency)
172 msg_Err(p_access, " Not Sending Nack #%d, too old (age %"PRId64" ms), current seq is:" \
173 " [%d]. Perhaps you should increase the buffer-size ...", seq, age, flow->wi);
175 else
177 msg_Dbg(p_access, " Sending Nack #%d (age %"PRId64" ms), current seq is: [%d]",
178 seq, age, flow->wi);
179 p_sys->i_retransmit_packets++;
180 vlc_mutex_lock( &p_sys->fd_lock );
181 if (rist_Write(flow->fd_out, pkt->buffer->p_buffer, pkt->buffer->i_buffer)
182 != (ssize_t)pkt->buffer->i_buffer) {
183 msg_Err(p_access, "Error sending retransmitted packet after 2 tries ...");
185 vlc_mutex_unlock( &p_sys->fd_lock );
189 static void process_nack(sout_access_out_t *p_access, uint8_t ptype, uint16_t nrecords,
190 struct rist_flow *flow, uint8_t *pkt)
192 sout_access_out_sys_t *p_sys = p_access->p_sys;
193 int i,j;
195 /*msg_Info(p_access, " Nack (BbRR), %d record(s), Window: [%d:%d-->%d]", nrecords,
196 flow->ri, flow->wi, flow->wi-flow->ri);*/
198 if (ptype == RTCP_PT_RTPFR)
200 uint8_t pi_ssrc[4];
201 rtcp_fb_get_ssrc_media_src(pkt, pi_ssrc);
202 if (memcmp(pi_ssrc, "RIST", 4) != 0)
204 msg_Info(p_access, " Ignoring Nack with name %s", pi_ssrc);
205 return; /* Ignore app-type not RIST */
208 for (i = 0; i < (nrecords-2); i++) {
209 uint16_t missing;
210 uint16_t additional;
211 uint8_t *rtp_nack_record = (pkt + 12 + i * 4);
212 missing = rtcp_fb_nack_get_range_start(rtp_nack_record);
213 additional = rtcp_fb_nack_get_range_extra(rtp_nack_record);
214 /*msg_Info(p_access, " Nack (Range), %d, current seq is: [%d]", missing, flow->wi);*/
215 vlc_mutex_lock( &p_sys->lock );
216 rist_retransmit(p_access, flow, missing);
217 for (j = 0; j < additional; j++) {
218 rist_retransmit(p_access, flow, missing + j + 1);
220 vlc_mutex_unlock( &p_sys->lock );
223 else if (ptype == RTCP_PT_RTPFB)
225 for (i = 0; i < (nrecords-2); i++) {
226 uint16_t missing;
227 uint16_t bitmask;
228 uint8_t *rtp_nack_record = (pkt + 12 + i * 4);
229 missing = rtcp_fb_nack_get_packet_id(rtp_nack_record);
230 bitmask = rtcp_fb_nack_get_bitmask_lost(rtp_nack_record);
231 /*msg_Info(p_access, " Nack (Bitmask), %d, current seq is: [%d]", missing, flow->wi);*/
232 vlc_mutex_lock( &p_sys->lock );
233 rist_retransmit(p_access, flow, missing);
234 for (j = 0; j < 16; j++) {
235 if ((bitmask & (1 << j)) == (1 << j)) {
236 rist_retransmit(p_access, flow, missing + j + 1);
239 vlc_mutex_unlock( &p_sys->lock );
242 else
244 msg_Err(p_access, " !!! Wrong feedback. Ptype is %02x!=%02x, FMT: %02x", ptype,
245 RTCP_PT_RTPFR, rtcp_fb_get_fmt(pkt));
249 static void rist_rtcp_recv(sout_access_out_t *p_access, struct rist_flow *flow, uint8_t *pkt_raw,
250 size_t len)
252 sout_access_out_sys_t *p_sys = p_access->p_sys;
253 uint8_t *pkt = pkt_raw;
254 uint8_t ptype;
255 uint16_t processed_bytes = 0;
256 uint16_t records;
258 while (processed_bytes < len) {
259 pkt = pkt_raw + processed_bytes;
260 /* safety checks */
261 uint16_t bytes_left = len - processed_bytes + 1;
262 if ( bytes_left < 4 )
264 /* we must have at least 4 bytes */
265 msg_Err(p_access, "Rist rtcp packet must have at least 4 bytes, we have %d",
266 bytes_left);
267 return;
269 else if (!rtp_check_hdr(pkt))
271 /* check for a valid rtp header */
272 msg_Err(p_access, "Malformed feedback packet starting with %02x, ignoring.", pkt[0]);
273 return;
276 ptype = rtcp_get_pt(pkt);
277 records = rtcp_get_length(pkt);
278 uint16_t bytes = (uint16_t)(4 * (1 + records));
279 if (bytes > bytes_left)
281 /* check for a sane number of bytes */
282 msg_Err(p_access, "Malformed feedback packet, wrong len %d, expecting %u bytes in the" \
283 " packet, got a buffer of %u bytes. ptype = %d", rtcp_get_length(pkt), bytes,
284 bytes_left, ptype);
285 return;
288 switch(ptype) {
289 case RTCP_PT_RTPFR:
290 case RTCP_PT_RTPFB:
291 process_nack(p_access, ptype, records, flow, pkt);
292 break;
294 case RTCP_PT_RR:
295 /*process_rr(f, pkt, len);*/
296 break;
298 case RTCP_PT_SDES:
300 int8_t name_length = rtcp_sdes_get_name_length(pkt);
301 if (name_length > bytes_left)
303 /* check for a sane number of bytes */
304 msg_Err(p_access, "Malformed SDES packet, wrong cname len %u, got a " \
305 "buffer of %u bytes.", name_length, bytes_left);
306 return;
308 if (memcmp(pkt + RTCP_SDES_SIZE, p_sys->receiver_name, name_length) != 0)
310 memcpy(p_sys->receiver_name, pkt + RTCP_SDES_SIZE, name_length);
311 msg_Info(p_access, "Receiver name: %s", p_sys->receiver_name);
314 break;
316 case RTCP_PT_SR:
317 break;
319 default:
320 msg_Err(p_access, " Unrecognized RTCP packet with PTYPE=%02x!!", ptype);
322 processed_bytes += bytes;
326 static void rist_rtcp_send(sout_access_out_t *p_access)
328 sout_access_out_sys_t *p_sys = p_access->p_sys;
329 struct rist_flow *flow = p_sys->flow;
330 uint8_t rtcp_buf[RTCP_SR_SIZE + RTCP_SDES_SIZE + MAX_CNAME] = { };
331 struct timeval tv;
332 int r;
333 uint64_t fractions;
334 uint16_t namelen = strlen(flow->cname) + 1;
335 gettimeofday(&tv, NULL);
337 /* Populate SR for sender report */
338 uint8_t *p_sr = rtcp_buf;
339 rtp_set_hdr(p_sr);
340 rtcp_sr_set_pt(p_sr);
341 rtcp_sr_set_length(p_sr, 6);
342 rtcp_fb_set_int_ssrc_pkt_sender(p_sr, p_sys->ssrc);
343 rtcp_sr_set_ntp_time_msw(p_sr, tv.tv_sec + SEVENTY_YEARS_OFFSET);
344 fractions = (uint64_t)tv.tv_usec;
345 fractions <<= 32ULL;
346 fractions /= 1000000ULL;
347 rtcp_sr_set_ntp_time_lsw(p_sr, (uint32_t)fractions);
348 rtcp_sr_set_rtp_time(p_sr, rtp_get_ts(vlc_tick_now()));
349 vlc_mutex_lock( &p_sys->lock );
350 rtcp_sr_set_packet_count(p_sr, flow->packets_count);
351 rtcp_sr_set_octet_count(p_sr, flow->bytes_count);
352 vlc_mutex_unlock( &p_sys->lock );
354 /* Populate SDES for sender description */
355 uint8_t *p_sdes = (rtcp_buf + RTCP_SR_SIZE);
356 /* we need to make sure it is a multiple of 4, pad if necessary */
357 if ((namelen - 2) & 0x3)
358 namelen = ((((namelen - 2) >> 2) + 1) << 2) + 2;
359 rtp_set_hdr(p_sdes);
360 rtp_set_cc(p_sdes, 1); /* Actually it is source count in this case */
361 rtcp_sdes_set_pt(p_sdes);
362 rtcp_set_length(p_sdes, (namelen >> 2) + 2);
363 rtcp_sdes_set_cname(p_sdes, 1);
364 rtcp_sdes_set_name_length(p_sdes, strlen(flow->cname));
365 p_sdes += RTCP_SDES_SIZE;
366 strlcpy((char *)p_sdes, flow->cname, namelen);
368 /* Send the rtcp message */
369 r = send(flow->fd_rtcp, rtcp_buf, RTCP_SR_SIZE + RTCP_SDES_SIZE + namelen, 0);
370 (void)r;
373 static void *rist_thread(void *data)
375 sout_access_out_t *p_access = data;
376 sout_access_out_sys_t *p_sys = p_access->p_sys;
377 uint64_t now;
378 uint8_t pkt[RTP_PKT_SIZE];
379 struct pollfd pfd[1];
380 int ret;
381 int r;
383 pfd[0].fd = p_sys->flow->fd_rtcp;
384 pfd[0].events = POLLIN;
386 for (;;) {
387 ret = poll(pfd, 1, RTCP_INTERVAL >> 1);
388 int canc = vlc_savecancel();
389 if (ret > 0)
391 if (pfd[0].revents & POLLIN)
393 r = rist_Read(p_sys->flow->fd_rtcp, pkt, RTP_PKT_SIZE);
394 if (r == RTP_PKT_SIZE) {
395 msg_Err(p_access, "Rist RTCP messsage is too big (%d bytes) and was probably " \
396 "cut, please keep it under %d bytes", r, RTP_PKT_SIZE);
398 if (unlikely(r == -1)) {
399 msg_Err(p_access, "socket %d error: %s\n", p_sys->flow->fd_rtcp,
400 gai_strerror(errno));
402 else {
403 rist_rtcp_recv(p_access, p_sys->flow, pkt, r);
408 /* And, in any case: */
409 now = vlc_tick_now();
410 if ((now - p_sys->last_rtcp_tx) > VLC_TICK_FROM_MS(RTCP_INTERVAL))
412 rist_rtcp_send(p_access);
413 p_sys->last_rtcp_tx = now;
415 vlc_restorecancel (canc);
418 return NULL;
421 /****************************************************************************
422 * RTP send
423 ****************************************************************************/
424 static void* ThreadSend( void *data )
426 sout_access_out_t *p_access = data;
427 sout_access_out_sys_t *p_sys = p_access->p_sys;
428 vlc_tick_t i_caching = p_sys->i_ticks_caching;
429 struct rist_flow *flow = p_sys->flow;
431 for (;;)
433 ssize_t len = 0;
434 uint16_t seq = 0;
435 uint32_t pkt_ts = 0;
436 block_t *out = block_FifoGet( p_sys->p_fifo );
438 block_cleanup_push( out );
439 vlc_tick_wait (out->i_dts + i_caching);
440 vlc_cleanup_pop();
442 len = out->i_buffer;
443 int canc = vlc_savecancel();
445 seq = rtp_get_seqnum(out->p_buffer);
446 pkt_ts = rtp_get_timestamp(out->p_buffer);
448 vlc_mutex_lock( &p_sys->fd_lock );
449 #ifdef TEST_PACKET_LOSS
450 # warning COMPILED WITH SELF INFLICTED PACKET LOSS
451 if ((seq % 14) == 0) {
452 /*msg_Err(p_access, "Dropped packet with seq number %d ...", seq);*/
454 else if (rist_Write(flow->fd_out, out->p_buffer, len) != len) {
455 msg_Err(p_access, "Error sending data packet after 2 tries ...");
457 #else
458 if (rist_Write(flow->fd_out, out->p_buffer, len) != len) {
459 msg_Err(p_access, "Error sending data packet after 2 tries ...");
461 #endif
462 vlc_mutex_unlock( &p_sys->fd_lock );
464 /* Insert Into Queue */
465 vlc_mutex_lock( &p_sys->lock );
466 /* Always replace the existing one with the new one */
467 struct rtp_pkt *pkt;
468 pkt = &(flow->buffer[seq]);
469 if (pkt->buffer)
471 block_Release(pkt->buffer);
472 pkt->buffer = NULL;
474 pkt->rtp_ts = pkt_ts;
475 pkt->buffer = out;
477 if (flow->reset == 1)
479 msg_Info(p_access, "Traffic detected");
480 /* First packet in the queue */
481 flow->reset = 0;
483 flow->wi = seq;
484 flow->hi_timestamp = pkt_ts;
485 /* Stats for RTCP feedback packets */
486 flow->packets_count++;
487 flow->bytes_count += len;
488 flow->last_out = seq;
489 vlc_mutex_unlock( &p_sys->lock );
491 /* We print out the stats once per second */
492 uint64_t now = vlc_tick_now();
493 uint64_t interval = (now - p_sys->i_last_stat);
494 if ( interval > VLC_TICK_FROM_MS(STATS_INTERVAL) )
496 if (p_sys->i_retransmit_packets > 0)
498 float quality = 100;
499 if (p_sys->i_total_packets > 0)
500 quality = (float)100 - (float)100*(float)(p_sys->i_retransmit_packets)
501 /(float)p_sys->i_total_packets;
502 msg_Info(p_access, "STATS: Total %u, Retransmitted %u, Link Quality %.2f%%",
503 p_sys->i_total_packets, p_sys->i_retransmit_packets, quality);
505 p_sys->i_last_stat = now;
506 p_sys->i_retransmit_packets = 0;
507 p_sys->i_total_packets = 0;
509 p_sys->i_total_packets++;
511 vlc_restorecancel (canc);
513 return NULL;
516 static void SendtoFIFO( sout_access_out_t *p_access, block_t *buffer )
518 sout_access_out_sys_t *p_sys = p_access->p_sys;
519 uint16_t seq = p_sys->rtp_counter++;
521 /* Set fresh rtp header data */
522 uint8_t *bufhdr = buffer->p_buffer;
523 rtp_set_hdr(bufhdr);
524 rtp_set_type(bufhdr, MPEG_II_TRANSPORT_STREAM);
525 rtp_set_seqnum(bufhdr, seq);
526 rtp_set_int_ssrc(bufhdr, p_sys->ssrc);
527 uint32_t pkt_ts = rtp_get_ts(buffer->i_dts);
528 rtp_set_timestamp(bufhdr, pkt_ts);
530 block_t *pkt = block_Duplicate(buffer);
531 block_FifoPut( p_sys->p_fifo, pkt );
534 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
536 sout_access_out_sys_t *p_sys = p_access->p_sys;
537 int i_len = 0;
539 while( p_buffer )
541 block_t *p_next;
542 int i_block_split = 0;
544 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_packet_size )
546 msg_Warn( p_access, "Buffer data size (%zu) > configured packet size (%zu), you " \
547 "should probably increase the configured packet size", p_buffer->i_buffer,
548 p_sys->i_packet_size );
549 p_sys->b_mtu_warning = true;
552 /* Temp buffer is already too large, flush */
553 if( p_sys->p_pktbuffer->i_buffer + p_buffer->i_buffer > p_sys->i_packet_size )
555 SendtoFIFO(p_access, p_sys->p_pktbuffer);
556 p_sys->p_pktbuffer->i_buffer = RTP_HEADER_SIZE;
559 i_len += p_buffer->i_buffer;
561 while( p_buffer->i_buffer )
564 size_t i_write = __MIN( p_buffer->i_buffer, p_sys->i_packet_size );
566 i_block_split++;
568 if( p_sys->p_pktbuffer->i_buffer == RTP_HEADER_SIZE )
570 p_sys->p_pktbuffer->i_dts = p_buffer->i_dts;
573 memcpy( p_sys->p_pktbuffer->p_buffer + p_sys->p_pktbuffer->i_buffer,
574 p_buffer->p_buffer, i_write );
576 p_sys->p_pktbuffer->i_buffer += i_write;
577 p_buffer->p_buffer += i_write;
578 p_buffer->i_buffer -= i_write;
580 /* Flush if we reached the target size for the case of block size < target packet size.
581 * Also flush when we are in block_split > 1 for the case when the block_size is
582 * larger than the packet-size because we need to continue the inner loop */
583 if( p_sys->p_pktbuffer->i_buffer == p_sys->i_packet_size || i_block_split > 1 )
585 SendtoFIFO(p_access, p_sys->p_pktbuffer);
586 p_sys->p_pktbuffer->i_buffer = RTP_HEADER_SIZE;
591 p_next = p_buffer->p_next;
592 block_Release( p_buffer );
593 p_buffer = p_next;
597 if ( i_len <= 0 ) {
598 block_ChainRelease( p_buffer );
600 return i_len;
603 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
605 VLC_UNUSED( p_access );
607 int i_ret = VLC_SUCCESS;
609 switch( i_query )
611 case ACCESS_OUT_CONTROLS_PACE:
612 *va_arg( args, bool * ) = false;
613 break;
615 default:
616 i_ret = VLC_EGENERIC;
617 break;
620 return i_ret;
623 static void Clean( sout_access_out_t *p_access )
625 sout_access_out_sys_t *p_sys = p_access->p_sys;
627 if( likely(p_sys->p_fifo != NULL) )
628 block_FifoRelease( p_sys->p_fifo );
630 if ( p_sys->flow )
632 if (p_sys->flow->fd_out >= 0) {
633 net_Close (p_sys->flow->fd_out);
635 if (p_sys->flow->fd_nack >= 0) {
636 net_Close (p_sys->flow->fd_rtcp);
638 for (int i=0; i<RIST_QUEUE_SIZE; i++) {
639 struct rtp_pkt *pkt = &(p_sys->flow->buffer[i]);
640 if (pkt->buffer)
642 block_Release(pkt->buffer);
643 pkt->buffer = NULL;
646 free(p_sys->flow->buffer);
647 free(p_sys->flow);
650 vlc_mutex_destroy( &p_sys->lock );
651 vlc_mutex_destroy( &p_sys->fd_lock );
652 if (p_sys->p_pktbuffer)
653 block_Release(p_sys->p_pktbuffer);
656 static void Close( vlc_object_t * p_this )
658 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
659 sout_access_out_sys_t *p_sys = p_access->p_sys;
661 vlc_cancel(p_sys->ristthread);
662 vlc_cancel(p_sys->senderthread);
664 vlc_join(p_sys->ristthread, NULL);
665 vlc_join(p_sys->senderthread, NULL);
667 Clean( p_access );
670 static int Open( vlc_object_t *p_this )
672 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
673 sout_access_out_sys_t *p_sys = NULL;
675 if (var_Create ( p_access, "dst-port", VLC_VAR_INTEGER )
676 || var_Create ( p_access, "src-port", VLC_VAR_INTEGER )
677 || var_Create ( p_access, "dst-addr", VLC_VAR_STRING )
678 || var_Create ( p_access, "src-addr", VLC_VAR_STRING ) )
680 msg_Err( p_access, "Valid network information is required." );
681 return VLC_ENOMEM;
684 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
686 p_sys = vlc_obj_calloc( p_this, 1, sizeof( *p_sys ) );
687 if( unlikely( p_sys == NULL ) )
688 return VLC_ENOMEM;
690 int i_dst_port = RIST_DEFAULT_PORT;
691 char *psz_dst_addr;
692 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
693 if( !psz_dst_addr )
694 return VLC_ENOMEM;
696 if ( psz_parser[0] == '[' )
697 psz_parser = strchr( psz_parser, ']' );
699 psz_parser = strchr( psz_parser ? psz_parser : psz_dst_addr, ':' );
700 if ( psz_parser != NULL )
702 *psz_parser++ = '\0';
703 i_dst_port = atoi( psz_parser );
706 vlc_mutex_init( &p_sys->lock );
707 vlc_mutex_init( &p_sys->fd_lock );
709 msg_Info(p_access, "Connecting RIST output to %s:%d and %s:%d", psz_dst_addr, i_dst_port,
710 psz_dst_addr, i_dst_port+1);
711 struct rist_flow *flow = rist_udp_transmitter(p_access, psz_dst_addr, i_dst_port);
712 free (psz_dst_addr);
713 if (!flow)
714 goto failed;
716 p_sys->flow = flow;
717 flow->latency = var_InheritInteger(p_access, SOUT_CFG_PREFIX "buffer-size");
718 flow->rtp_latency = rtp_get_ts(VLC_TICK_FROM_MS(flow->latency));
719 p_sys->ssrc = var_InheritInteger(p_access, SOUT_CFG_PREFIX "ssrc");
720 if (p_sys->ssrc == 0) {
721 vlc_rand_bytes(&p_sys->ssrc, 4);
723 /* Last bit of ssrc must be 0 for normal data and 1 for retries */
724 p_sys->ssrc &= ~(1 << 0);
726 msg_Info(p_access, "SSRC: 0x%08X", p_sys->ssrc);
727 p_sys->i_ticks_caching = VLC_TICK_FROM_MS(var_InheritInteger( p_access,
728 SOUT_CFG_PREFIX "caching"));
729 p_sys->i_packet_size = var_InheritInteger(p_access, SOUT_CFG_PREFIX "packet-size" );
730 p_sys->p_fifo = block_FifoNew();
731 if( unlikely(p_sys->p_fifo == NULL) )
732 goto failed;
733 p_sys->p_pktbuffer = block_Alloc( p_sys->i_packet_size );
734 if( unlikely(p_sys->p_pktbuffer == NULL) )
735 goto failed;
737 p_sys->p_pktbuffer->i_buffer = RTP_HEADER_SIZE;
739 p_access->p_sys = p_sys;
741 if( vlc_clone(&p_sys->senderthread, ThreadSend, p_access, VLC_THREAD_PRIORITY_HIGHEST ) )
743 msg_Err(p_access, "Failed to create sender thread.");
744 goto failed;
747 if (vlc_clone(&p_sys->ristthread, rist_thread, p_access, VLC_THREAD_PRIORITY_INPUT))
749 msg_Err(p_access, "Failed to create worker thread.");
750 vlc_cancel(p_sys->senderthread);
751 vlc_join(p_sys->senderthread, NULL);
752 goto failed;
755 p_access->pf_write = Write;
756 p_access->pf_control = Control;
758 return VLC_SUCCESS;
760 failed:
761 Clean( p_access );
762 return VLC_EGENERIC;
765 #define CACHING_TEXT N_("RIST data output caching size (ms)")
766 #define CACHING_LONGTEXT N_( \
767 "Having this cache will guarantee that the packets going out are " \
768 "delivered at a spacing determined by the chain timestamps thus ensuring " \
769 "a near jitter free output. Be aware that this setting will also add to " \
770 "the overall latency of the stream." )
772 #define BUFFER_TEXT N_("RIST retry-buffer queue size (ms)")
773 #define BUFFER_LONGTEXT N_( \
774 "This must match the buffer size (latency) configured on the server side. If you " \
775 "are not sure, leave the default of 0 which will set it the maximum " \
776 "value and will use about 100MB of RAM" )
778 #define SSRC_TEXT N_("SSRC used in RTP output (default is random, i.e. 0)")
779 #define SSRC_LONGTEXT N_( \
780 "Use this setting to specify a known SSRC for the RTP header. This is only useful " \
781 "if your receiver acts on it. When using VLC as receiver, it is not." )
783 /* Module descriptor */
784 vlc_module_begin()
786 set_shortname( N_("RIST") )
787 set_description( N_("RIST stream output") )
788 set_category( CAT_SOUT )
789 set_subcategory( SUBCAT_SOUT_ACO )
791 add_integer( SOUT_CFG_PREFIX "packet-size", RIST_TARGET_PACKET_SIZE,
792 N_("RIST target packet size (bytes)"), NULL, true )
793 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_CACHING_DELAY,
794 CACHING_TEXT, CACHING_LONGTEXT, true )
795 add_integer( SOUT_CFG_PREFIX "buffer-size", DEFAULT_BUFFER_SIZE,
796 BUFFER_TEXT, BUFFER_LONGTEXT, true )
797 add_integer( SOUT_CFG_PREFIX "ssrc", 0,
798 SSRC_TEXT, SSRC_LONGTEXT, true )
800 set_capability( "sout access", 0 )
801 add_shortcut( "rist", "tr06" )
803 set_callbacks( Open, Close )
805 vlc_module_end ()