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 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_interrupt.h>
32 #include <vlc_plugin.h>
34 #include <vlc_block.h>
35 #include <vlc_network.h>
36 #include <vlc_threads.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 #include <sys/socket.h>
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
[] = {
80 struct rist_flow
*flow
;
82 char receiver_name
[MAX_CNAME
];
83 uint64_t last_rtcp_tx
;
84 vlc_thread_t ristthread
;
85 vlc_thread_t senderthread
;
91 uint64_t i_ticks_caching
;
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
));
107 flow
->buffer
= calloc(RIST_QUEUE_SIZE
, sizeof(struct rtp_pkt
));
108 if ( unlikely( flow
->buffer
== NULL
) )
117 static struct rist_flow
*rist_udp_transmitter(sout_access_out_t
*p_access
, char *psz_dst_server
,
120 struct rist_flow
*flow
;
121 flow
= rist_init_tx();
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" );
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" );
139 populate_cname(flow
->fd_rtcp
, flow
->cname
);
140 msg_Info(p_access
, "our cname is %s", flow
->cname
);
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
);
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) {
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
);
177 msg_Dbg(p_access
, " Sending Nack #%d (age %"PRId64
" ms), current seq is: [%d]",
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
;
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
)
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
++) {
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
++) {
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
);
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
,
249 sout_access_out_sys_t
*p_sys
= p_access
->p_sys
;
250 uint8_t *pkt
= pkt_raw
;
252 uint16_t processed_bytes
= 0;
255 while (processed_bytes
< len
) {
256 pkt
= pkt_raw
+ processed_bytes
;
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",
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]);
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
,
288 process_nack(p_access
, ptype
, records
, flow
, pkt
);
292 /*process_rr(f, pkt, len);*/
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
);
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
);
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
] = { };
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
;
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;
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);
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
;
373 uint8_t pkt
[RTP_PKT_SIZE
];
374 struct pollfd pfd
[1];
378 pfd
[0].fd
= p_sys
->flow
->fd_rtcp
;
379 pfd
[0].events
= POLLIN
;
382 ret
= poll(pfd
, 1, RTCP_INTERVAL
>> 1);
383 int canc
= vlc_savecancel();
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
));
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
);
416 /****************************************************************************
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
;
431 block_t
*out
= block_FifoGet( p_sys
->p_fifo
);
433 block_cleanup_push( out
);
434 vlc_tick_wait (out
->i_dts
+ i_caching
);
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 ...");
453 if (rist_Write(flow
->fd_out
, out
->p_buffer
, len
) != len
) {
454 msg_Err(p_access
, "Error sending data packet after 2 tries ...");
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 */
463 pkt
= &(flow
->buffer
[seq
]);
466 block_Release(pkt
->buffer
);
469 pkt
->rtp_ts
= pkt_ts
;
472 if (flow
->reset
== 1)
474 msg_Info(p_access
, "Traffic detected");
475 /* First packet in the queue */
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)
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
);
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
;
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
;
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
);
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
);
593 block_ChainRelease( p_buffer
);
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
;
606 case ACCESS_OUT_CONTROLS_PACE
:
607 *va_arg( args
, bool * ) = false;
611 i_ret
= VLC_EGENERIC
;
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
);
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
]);
637 block_Release(pkt
->buffer
);
641 free(p_sys
->flow
->buffer
);
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
);
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." );
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
) )
685 int i_dst_port
= RIST_DEFAULT_PORT
;
687 char *psz_parser
= psz_dst_addr
= strdup( p_access
->psz_path
);
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
);
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
) )
728 p_sys
->p_pktbuffer
= block_Alloc( p_sys
->i_packet_size
);
729 if( unlikely(p_sys
->p_pktbuffer
== NULL
) )
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.");
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
);
750 p_access
->pf_write
= Write
;
751 p_access
->pf_control
= Control
;
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 */
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
)