access_out: rist: fix mem leak
[vlc.git] / modules / access_output / rist.c
blob1e36b963c5cf44fdb6cf8cf7b476c40212838599
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 rist_Write(flow->fd_out, pkt->buffer->p_buffer, pkt->buffer->i_buffer);
182 vlc_mutex_unlock( &p_sys->fd_lock );
186 static void process_nack(sout_access_out_t *p_access, uint8_t ptype, uint16_t nrecords,
187 struct rist_flow *flow, uint8_t *pkt)
189 sout_access_out_sys_t *p_sys = p_access->p_sys;
190 int i,j;
192 /*msg_Info(p_access, " Nack (BbRR), %d record(s), Window: [%d:%d-->%d]", nrecords,
193 flow->ri, flow->wi, flow->wi-flow->ri);*/
195 if (ptype == RTCP_PT_RTPFR)
197 uint8_t pi_ssrc[4];
198 rtcp_fb_get_ssrc_media_src(pkt, pi_ssrc);
199 if (memcmp(pi_ssrc, "RIST", 4) != 0)
201 msg_Info(p_access, " Ignoring Nack with name %s", pi_ssrc);
202 return; /* Ignore app-type not RIST */
205 for (i = 0; i < (nrecords-2); i++) {
206 uint16_t missing;
207 uint16_t additional;
208 uint8_t *rtp_nack_record = (pkt + 12 + i * 4);
209 missing = rtcp_fb_nack_get_range_start(rtp_nack_record);
210 additional = rtcp_fb_nack_get_range_extra(rtp_nack_record);
211 /*msg_Info(p_access, " Nack (Range), %d, current seq is: [%d]", missing, flow->wi);*/
212 vlc_mutex_lock( &p_sys->lock );
213 rist_retransmit(p_access, flow, missing);
214 for (j = 0; j < additional; j++) {
215 rist_retransmit(p_access, flow, missing + j + 1);
217 vlc_mutex_unlock( &p_sys->lock );
220 else if (ptype == RTCP_PT_RTPFB)
222 for (i = 0; i < (nrecords-2); i++) {
223 uint16_t missing;
224 uint16_t bitmask;
225 uint8_t *rtp_nack_record = (pkt + 12 + i * 4);
226 missing = rtcp_fb_nack_get_packet_id(rtp_nack_record);
227 bitmask = rtcp_fb_nack_get_bitmask_lost(rtp_nack_record);
228 /*msg_Info(p_access, " Nack (Bitmask), %d, current seq is: [%d]", missing, flow->wi);*/
229 vlc_mutex_lock( &p_sys->lock );
230 rist_retransmit(p_access, flow, missing);
231 for (j = 0; j < 16; j++) {
232 if ((bitmask & (1 << j)) == (1 << j)) {
233 rist_retransmit(p_access, flow, missing + j + 1);
236 vlc_mutex_unlock( &p_sys->lock );
239 else
241 msg_Err(p_access, " !!! Wrong feedback. Ptype is %02x!=%02x, FMT: %02x", ptype,
242 RTCP_PT_RTPFR, rtcp_fb_get_fmt(pkt));
246 static void rist_rtcp_recv(sout_access_out_t *p_access, struct rist_flow *flow, uint8_t *pkt_raw,
247 size_t len)
249 sout_access_out_sys_t *p_sys = p_access->p_sys;
250 uint8_t *pkt = pkt_raw;
251 uint8_t ptype;
252 uint16_t processed_bytes = 0;
253 uint16_t records;
255 while (processed_bytes < len) {
256 pkt = pkt_raw + processed_bytes;
257 /* safety checks */
258 uint16_t bytes_left = len - processed_bytes + 1;
259 if ( bytes_left < 4 )
261 /* we must have at least 4 bytes */
262 msg_Err(p_access, "Rist rtcp packet must have at least 4 bytes, we have %d",
263 bytes_left);
264 return;
266 else if (!rtp_check_hdr(pkt))
268 /* check for a valid rtp header */
269 msg_Err(p_access, "Malformed feedback packet starting with %02x, ignoring.", pkt[0]);
270 return;
273 ptype = rtcp_get_pt(pkt);
274 records = rtcp_get_length(pkt);
275 uint16_t bytes = (uint16_t)(4 * (1 + records));
276 if (bytes > bytes_left)
278 /* check for a sane number of bytes */
279 msg_Err(p_access, "Malformed feedback packet, wrong len %d, expecting %u bytes in the" \
280 " packet, got a buffer of %u bytes. ptype = %d", rtcp_get_length(pkt), bytes,
281 bytes_left, ptype);
282 return;
285 switch(ptype) {
286 case RTCP_PT_RTPFR:
287 case RTCP_PT_RTPFB:
288 process_nack(p_access, ptype, records, flow, pkt);
289 break;
291 case RTCP_PT_RR:
292 /*process_rr(f, pkt, len);*/
293 break;
295 case RTCP_PT_SDES:
297 int8_t name_length = rtcp_sdes_get_name_length(pkt);
298 if (name_length > bytes_left)
300 /* check for a sane number of bytes */
301 msg_Err(p_access, "Malformed SDES packet, wrong cname len %u, got a " \
302 "buffer of %u bytes.", name_length, bytes_left);
303 return;
305 if (memcmp(pkt + RTCP_SDES_SIZE, p_sys->receiver_name, name_length) != 0)
307 memcpy(p_sys->receiver_name, pkt + RTCP_SDES_SIZE, name_length);
308 msg_Info(p_access, "Receiver name: %s", p_sys->receiver_name);
311 break;
313 case RTCP_PT_SR:
314 break;
316 default:
317 msg_Err(p_access, " Unrecognized RTCP packet with PTYPE=%02x!!", ptype);
319 processed_bytes += bytes;
323 static void rist_rtcp_send(sout_access_out_t *p_access)
325 sout_access_out_sys_t *p_sys = p_access->p_sys;
326 struct rist_flow *flow = p_sys->flow;
327 uint8_t rtcp_buf[RTCP_SR_SIZE + RTCP_SDES_SIZE + MAX_CNAME] = { };
328 struct timeval tv;
329 int r;
330 uint64_t fractions;
331 uint16_t namelen = strlen(flow->cname) + 1;
332 gettimeofday(&tv, NULL);
334 /* Populate SR for sender report */
335 uint8_t *p_sr = rtcp_buf;
336 rtp_set_hdr(p_sr);
337 rtcp_sr_set_pt(p_sr);
338 rtcp_sr_set_length(p_sr, 6);
339 rtcp_fb_set_int_ssrc_pkt_sender(p_sr, p_sys->ssrc);
340 rtcp_sr_set_ntp_time_msw(p_sr, tv.tv_sec + SEVENTY_YEARS_OFFSET);
341 fractions = (tv.tv_usec << 32ULL) / 1000000ULL;
342 rtcp_sr_set_ntp_time_lsw(p_sr, (uint32_t)fractions);
343 rtcp_sr_set_rtp_time(p_sr, rtp_get_ts(vlc_tick_now()));
344 vlc_mutex_lock( &p_sys->lock );
345 rtcp_sr_set_packet_count(p_sr, flow->packets_count);
346 rtcp_sr_set_octet_count(p_sr, flow->bytes_count);
347 vlc_mutex_unlock( &p_sys->lock );
349 /* Populate SDES for sender description */
350 uint8_t *p_sdes = (rtcp_buf + RTCP_SR_SIZE);
351 /* we need to make sure it is a multiple of 4, pad if necessary */
352 if ((namelen - 2) & 0x3)
353 namelen = ((((namelen - 2) >> 2) + 1) << 2) + 2;
354 rtp_set_hdr(p_sdes);
355 rtp_set_cc(p_sdes, 1); /* Actually it is source count in this case */
356 rtcp_sdes_set_pt(p_sdes);
357 rtcp_set_length(p_sdes, (namelen >> 2) + 2);
358 rtcp_sdes_set_cname(p_sdes, 1);
359 rtcp_sdes_set_name_length(p_sdes, strlen(flow->cname));
360 p_sdes += RTCP_SDES_SIZE;
361 strlcpy((char *)p_sdes, flow->cname, namelen);
363 /* Send the rtcp message */
364 r = send(flow->fd_rtcp, rtcp_buf, RTCP_SR_SIZE + RTCP_SDES_SIZE + namelen, 0);
365 (void)r;
368 static void *rist_thread(void *data)
370 sout_access_out_t *p_access = data;
371 sout_access_out_sys_t *p_sys = p_access->p_sys;
372 uint64_t now;
373 uint8_t pkt[RTP_PKT_SIZE];
374 struct pollfd pfd[1];
375 int ret;
376 int r;
378 pfd[0].fd = p_sys->flow->fd_rtcp;
379 pfd[0].events = POLLIN;
381 for (;;) {
382 ret = poll(pfd, 1, RTCP_INTERVAL >> 1);
383 int canc = vlc_savecancel();
384 if (ret > 0)
386 if (pfd[0].revents & POLLIN)
388 r = rist_Read(p_sys->flow->fd_rtcp, pkt, RTP_PKT_SIZE);
389 if (r == RTP_PKT_SIZE) {
390 msg_Err(p_access, "Rist RTCP messsage is too big (%d bytes) and was probably " \
391 "cut, please keep it under %d bytes", r, RTP_PKT_SIZE);
393 if (unlikely(r == -1)) {
394 msg_Err(p_access, "socket %d error: %s\n", p_sys->flow->fd_rtcp,
395 gai_strerror(errno));
397 else {
398 rist_rtcp_recv(p_access, p_sys->flow, pkt, r);
403 /* And, in any case: */
404 now = vlc_tick_now();
405 if ((now - p_sys->last_rtcp_tx) > VLC_TICK_FROM_MS(RTCP_INTERVAL))
407 rist_rtcp_send(p_access);
408 p_sys->last_rtcp_tx = now;
410 vlc_restorecancel (canc);
413 return NULL;
416 /****************************************************************************
417 * RTP send
418 ****************************************************************************/
419 static void* ThreadSend( void *data )
421 sout_access_out_t *p_access = data;
422 sout_access_out_sys_t *p_sys = p_access->p_sys;
423 vlc_tick_t i_caching = p_sys->i_ticks_caching;
424 struct rist_flow *flow = p_sys->flow;
426 for (;;)
428 ssize_t len = 0;
429 uint16_t seq = 0;
430 uint32_t pkt_ts = 0;
431 block_t *out = block_FifoGet( p_sys->p_fifo );
433 block_cleanup_push( out );
434 vlc_tick_wait (out->i_dts + i_caching);
435 vlc_cleanup_pop();
437 len = out->i_buffer;
438 int canc = vlc_savecancel();
440 seq = rtp_get_seqnum(out->p_buffer);
441 pkt_ts = rtp_get_timestamp(out->p_buffer);
443 vlc_mutex_lock( &p_sys->fd_lock );
444 #ifdef TEST_PACKET_LOSS
445 # warning COMPILED WITH SELF INFLICTED PACKET LOSS
446 if ((seq % 14) == 0) {
447 /*msg_Err(p_access, "Dropped packet with seq number %d ...", seq);*/
449 else if (rist_Write(flow->fd_out, out->p_buffer, len) != len) {
450 msg_Err(p_access, "Error sending data packet after 2 tries ...");
452 #else
453 if (rist_Write(flow->fd_out, out->p_buffer, len) != len) {
454 msg_Err(p_access, "Error sending data packet after 2 tries ...");
456 #endif
457 vlc_mutex_unlock( &p_sys->fd_lock );
459 /* Insert Into Queue */
460 vlc_mutex_lock( &p_sys->lock );
461 /* Always replace the existing one with the new one */
462 struct rtp_pkt *pkt;
463 pkt = &(flow->buffer[seq]);
464 if (pkt->buffer)
466 block_Release(pkt->buffer);
467 pkt->buffer = NULL;
469 pkt->rtp_ts = pkt_ts;
470 pkt->buffer = out;
472 if (flow->reset == 1)
474 msg_Info(p_access, "Traffic detected");
475 /* First packet in the queue */
476 flow->reset = 0;
478 flow->wi = seq;
479 flow->hi_timestamp = pkt_ts;
480 /* Stats for RTCP feedback packets */
481 flow->packets_count++;
482 flow->bytes_count += len;
483 flow->last_out = seq;
484 vlc_mutex_unlock( &p_sys->lock );
486 /* We print out the stats once per second */
487 uint64_t now = vlc_tick_now();
488 uint64_t interval = (now - p_sys->i_last_stat);
489 if ( interval > VLC_TICK_FROM_MS(STATS_INTERVAL) )
491 if (p_sys->i_retransmit_packets > 0)
493 float quality = 100;
494 if (p_sys->i_total_packets > 0)
495 quality = (float)100 - (float)100*(float)(p_sys->i_retransmit_packets)
496 /(float)p_sys->i_total_packets;
497 msg_Info(p_access, "STATS: Total %u, Retransmitted %u, Link Quality %.2f%%",
498 p_sys->i_total_packets, p_sys->i_retransmit_packets, quality);
500 p_sys->i_last_stat = now;
501 p_sys->i_retransmit_packets = 0;
502 p_sys->i_total_packets = 0;
504 p_sys->i_total_packets++;
506 vlc_restorecancel (canc);
508 return NULL;
511 static void SendtoFIFO( sout_access_out_t *p_access, block_t *buffer )
513 sout_access_out_sys_t *p_sys = p_access->p_sys;
514 uint16_t seq = p_sys->rtp_counter++;
516 /* Set fresh rtp header data */
517 uint8_t *bufhdr = buffer->p_buffer;
518 rtp_set_hdr(bufhdr);
519 rtp_set_type(bufhdr, MPEG_II_TRANSPORT_STREAM);
520 rtp_set_seqnum(bufhdr, seq);
521 rtp_set_int_ssrc(bufhdr, p_sys->ssrc);
522 uint32_t pkt_ts = rtp_get_ts(buffer->i_dts);
523 rtp_set_timestamp(bufhdr, pkt_ts);
525 block_t *pkt = block_Duplicate(buffer);
526 block_FifoPut( p_sys->p_fifo, pkt );
529 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
531 sout_access_out_sys_t *p_sys = p_access->p_sys;
532 int i_len = 0;
534 while( p_buffer )
536 block_t *p_next;
537 int i_block_split = 0;
539 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_packet_size )
541 msg_Warn( p_access, "Buffer data size (%zu) > configured packet size (%zu), you " \
542 "should probably increase the configured packet size", p_buffer->i_buffer,
543 p_sys->i_packet_size );
544 p_sys->b_mtu_warning = true;
547 /* Temp buffer is already too large, flush */
548 if( p_sys->p_pktbuffer->i_buffer + p_buffer->i_buffer > p_sys->i_packet_size )
550 SendtoFIFO(p_access, p_sys->p_pktbuffer);
551 p_sys->p_pktbuffer->i_buffer = RTP_HEADER_SIZE;
554 i_len += p_buffer->i_buffer;
556 while( p_buffer->i_buffer )
559 size_t i_write = __MIN( p_buffer->i_buffer, p_sys->i_packet_size );
561 i_block_split++;
563 if( p_sys->p_pktbuffer->i_buffer == RTP_HEADER_SIZE )
565 p_sys->p_pktbuffer->i_dts = p_buffer->i_dts;
568 memcpy( p_sys->p_pktbuffer->p_buffer + p_sys->p_pktbuffer->i_buffer,
569 p_buffer->p_buffer, i_write );
571 p_sys->p_pktbuffer->i_buffer += i_write;
572 p_buffer->p_buffer += i_write;
573 p_buffer->i_buffer -= i_write;
575 /* Flush if we reached the target size for the case of block size < target packet size.
576 * Also flush when we are in block_split > 1 for the case when the block_size is
577 * larger than the packet-size because we need to continue the inner loop */
578 if( p_sys->p_pktbuffer->i_buffer == p_sys->i_packet_size || i_block_split > 1 )
580 SendtoFIFO(p_access, p_sys->p_pktbuffer);
581 p_sys->p_pktbuffer->i_buffer = RTP_HEADER_SIZE;
586 p_next = p_buffer->p_next;
587 block_Release( p_buffer );
588 p_buffer = p_next;
592 if ( i_len <= 0 ) {
593 block_ChainRelease( p_buffer );
595 return i_len;
598 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
600 VLC_UNUSED( p_access );
602 int i_ret = VLC_SUCCESS;
604 switch( i_query )
606 case ACCESS_OUT_CONTROLS_PACE:
607 *va_arg( args, bool * ) = false;
608 break;
610 default:
611 i_ret = VLC_EGENERIC;
612 break;
615 return i_ret;
618 static void Clean( sout_access_out_t *p_access )
620 sout_access_out_sys_t *p_sys = p_access->p_sys;
622 if( likely(p_sys->p_fifo != NULL) )
623 block_FifoRelease( p_sys->p_fifo );
625 if ( p_sys->flow )
627 if (p_sys->flow->fd_out >= 0) {
628 net_Close (p_sys->flow->fd_out);
630 if (p_sys->flow->fd_nack >= 0) {
631 net_Close (p_sys->flow->fd_rtcp);
633 for (int i=0; i<RIST_QUEUE_SIZE; i++) {
634 struct rtp_pkt *pkt = &(p_sys->flow->buffer[i]);
635 if (pkt->buffer)
637 block_Release(pkt->buffer);
638 pkt->buffer = NULL;
641 free(p_sys->flow->buffer);
642 free(p_sys->flow);
645 vlc_mutex_destroy( &p_sys->lock );
646 vlc_mutex_destroy( &p_sys->fd_lock );
647 if (p_sys->p_pktbuffer)
648 block_Release(p_sys->p_pktbuffer);
651 static void Close( vlc_object_t * p_this )
653 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
654 sout_access_out_sys_t *p_sys = p_access->p_sys;
656 vlc_cancel(p_sys->ristthread);
657 vlc_cancel(p_sys->senderthread);
659 vlc_join(p_sys->ristthread, NULL);
660 vlc_join(p_sys->senderthread, NULL);
662 Clean( p_access );
665 static int Open( vlc_object_t *p_this )
667 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
668 sout_access_out_sys_t *p_sys = NULL;
670 if (var_Create ( p_access, "dst-port", VLC_VAR_INTEGER )
671 || var_Create ( p_access, "src-port", VLC_VAR_INTEGER )
672 || var_Create ( p_access, "dst-addr", VLC_VAR_STRING )
673 || var_Create ( p_access, "src-addr", VLC_VAR_STRING ) )
675 msg_Err( p_access, "Valid network information is required." );
676 return VLC_ENOMEM;
679 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
681 p_sys = vlc_obj_calloc( p_this, 1, sizeof( *p_sys ) );
682 if( unlikely( p_sys == NULL ) )
683 return VLC_ENOMEM;
685 int i_dst_port = RIST_DEFAULT_PORT;
686 char *psz_dst_addr;
687 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
688 if( !psz_dst_addr )
689 return VLC_ENOMEM;
691 if ( psz_parser[0] == '[' )
692 psz_parser = strchr( psz_parser, ']' );
694 psz_parser = strchr( psz_parser ? psz_parser : psz_dst_addr, ':' );
695 if ( psz_parser != NULL )
697 *psz_parser++ = '\0';
698 i_dst_port = atoi( psz_parser );
701 vlc_mutex_init( &p_sys->lock );
702 vlc_mutex_init( &p_sys->fd_lock );
704 msg_Info(p_access, "Connecting RIST output to %s:%d and %s:%d", psz_dst_addr, i_dst_port,
705 psz_dst_addr, i_dst_port+1);
706 struct rist_flow *flow = rist_udp_transmitter(p_access, psz_dst_addr, i_dst_port);
707 free (psz_dst_addr);
708 if (!flow)
709 goto failed;
711 p_sys->flow = flow;
712 flow->latency = var_InheritInteger(p_access, SOUT_CFG_PREFIX "buffer-size");
713 flow->rtp_latency = rtp_get_ts(VLC_TICK_FROM_MS(flow->latency));
714 p_sys->ssrc = var_InheritInteger(p_access, SOUT_CFG_PREFIX "ssrc");
715 if (p_sys->ssrc == 0) {
716 vlc_rand_bytes(&p_sys->ssrc, 4);
718 /* Last bit of ssrc must be 0 for normal data and 1 for retries */
719 p_sys->ssrc &= ~(1 << 0);
721 msg_Info(p_access, "SSRC: 0x%08X", p_sys->ssrc);
722 p_sys->i_ticks_caching = VLC_TICK_FROM_MS(var_InheritInteger( p_access,
723 SOUT_CFG_PREFIX "caching"));
724 p_sys->i_packet_size = var_InheritInteger(p_access, SOUT_CFG_PREFIX "packet-size" );
725 p_sys->p_fifo = block_FifoNew();
726 if( unlikely(p_sys->p_fifo == NULL) )
727 goto failed;
728 p_sys->p_pktbuffer = block_Alloc( p_sys->i_packet_size );
729 if( unlikely(p_sys->p_pktbuffer == NULL) )
730 goto failed;
732 p_sys->p_pktbuffer->i_buffer = RTP_HEADER_SIZE;
734 p_access->p_sys = p_sys;
736 if( vlc_clone(&p_sys->senderthread, ThreadSend, p_access, VLC_THREAD_PRIORITY_HIGHEST ) )
738 msg_Err(p_access, "Failed to create sender thread.");
739 goto failed;
742 if (vlc_clone(&p_sys->ristthread, rist_thread, p_access, VLC_THREAD_PRIORITY_INPUT))
744 msg_Err(p_access, "Failed to create worker thread.");
745 vlc_cancel(p_sys->senderthread);
746 vlc_join(p_sys->senderthread, NULL);
747 goto failed;
750 p_access->pf_write = Write;
751 p_access->pf_control = Control;
753 return VLC_SUCCESS;
755 failed:
756 Clean( p_access );
757 return VLC_EGENERIC;
760 #define CACHING_TEXT N_("RIST data output caching size (ms)")
761 #define CACHING_LONGTEXT N_( \
762 "Having this cache will guarantee that the packets going out are " \
763 "delivered at a spacing determined by the chain timestamps thus ensuring " \
764 "a near jitter free output. Be aware that this setting will also add to " \
765 "the overall latency of the stream." )
767 #define BUFFER_TEXT N_("RIST client side buffer size (ms)")
768 #define BUFFER_LONGTEXT N_( \
769 "This must match the buffer size (latency) configured on the server side. If you " \
770 "are not sure, leave the default of -1 which will set it the maximum " \
771 "value and will use about 100MB of RAM" )
773 #define SSRC_TEXT N_("SSRC used in RTP output (default is random, i.e. 0)")
774 #define SSRC_LONGTEXT N_( \
775 "Use this setting to specify a known SSRC for the RTP header. This is only useful " \
776 "if your receiver acts on it. When using VLC as receiver, it is not." )
778 /* Module descriptor */
779 vlc_module_begin()
781 set_shortname( N_("RIST") )
782 set_description( N_("RIST stream output") )
783 set_category( CAT_SOUT )
784 set_subcategory( SUBCAT_SOUT_ACO )
786 add_integer( SOUT_CFG_PREFIX "packet-size", RIST_TARGET_PACKET_SIZE,
787 N_("RIST target packet size (bytes)"), NULL, true )
788 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_CACHING_DELAY,
789 CACHING_TEXT, CACHING_LONGTEXT, true )
790 add_integer( SOUT_CFG_PREFIX "buffer-size", DEFAULT_BUFFER_SIZE,
791 BUFFER_TEXT, BUFFER_LONGTEXT, true )
792 add_integer( SOUT_CFG_PREFIX "ssrc", 0,
793 SSRC_TEXT, SSRC_LONGTEXT, true )
795 set_capability( "sout access", 0 )
796 add_shortcut( "rist", "tr06" )
798 set_callbacks( Open, Close )
800 vlc_module_end ()