1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <sys/types.h>
39 #include <vlc_block.h>
46 # include <winsock2.h>
47 # include <ws2tcpip.h>
49 # include <sys/socket.h>
52 #include <vlc_network.h>
54 #if defined (HAVE_NETINET_UDPLITE_H)
55 # include <netinet/udplite.h>
56 #elif defined (__linux__)
57 # define UDPLITE_SEND_CSCOV 10
58 # define UDPLITE_RECV_CSCOV 11
61 #ifndef IPPROTO_UDPLITE
62 # define IPPROTO_UDPLITE 136 /* from IANA */
65 # define SOL_UDPLITE IPPROTO_UDPLITE
68 #define MAX_EMPTY_BLOCKS 200
70 #if defined(WIN32) || defined(UNDER_CE)
71 # define WINSOCK_STRERROR_SIZE 20
72 static const char *winsock_strerror( char *buf )
74 snprintf( buf, WINSOCK_STRERROR_SIZE, "Winsock error %d",
76 buf[WINSOCK_STRERROR_SIZE - 1] = '\0';
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
87 #define SOUT_CFG_PREFIX "sout-udp-"
89 #define CACHING_TEXT N_("Caching value (ms)")
90 #define CACHING_LONGTEXT N_( \
91 "Default caching value for outbound UDP streams. This " \
92 "value should be set in milliseconds." )
94 #define GROUP_TEXT N_("Group packets")
95 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
96 "or by groups. You can choose the number " \
97 "of packets that will be sent at a time. It " \
98 "helps reducing the scheduling load on " \
99 "heavily-loaded systems." )
100 #define RAW_TEXT N_("Raw write")
101 #define RAW_LONGTEXT N_("Packets will be sent " \
102 "directly, without trying to fill the MTU (ie, " \
103 "without trying to make the biggest possible packets " \
104 "in order to improve streaming)." )
105 #define RTCP_TEXT N_("RTCP Sender Report")
106 #define RTCP_LONGTEXT N_("Send RTCP Sender Report packets")
107 #define RTCP_PORT_TEXT N_("RTCP destination port number")
108 #define RTCP_PORT_LONGTEXT N_("Sends RTCP packets to this port (0 = auto)")
109 #define AUTO_MCAST_TEXT N_("Automatic multicast streaming")
110 #define AUTO_MCAST_LONGTEXT N_("Allocates an outbound multicast address " \
112 #define UDPLITE_TEXT N_("UDP-Lite")
113 #define UDPLITE_LONGTEXT N_("Use UDP-Lite/IP instead of normal UDP/IP")
114 #define CSCOV_TEXT N_("Checksum coverage")
115 #define CSCOV_LONGTEXT N_("Payload bytes covered by layer-4 checksum")
118 set_description( _("UDP stream output") );
119 set_shortname( "UDP" );
120 set_category( CAT_SOUT );
121 set_subcategory( SUBCAT_SOUT_ACO );
122 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
123 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
125 add_obsolete_integer( SOUT_CFG_PREFIX "late" );
126 add_bool( SOUT_CFG_PREFIX "raw", VLC_FALSE, NULL, RAW_TEXT, RAW_LONGTEXT,
128 add_bool( SOUT_CFG_PREFIX "rtcp", VLC_FALSE, NULL, RAW_TEXT, RAW_LONGTEXT,
130 add_integer( SOUT_CFG_PREFIX "rtcp-port", 0, NULL, RTCP_PORT_TEXT,
131 RTCP_PORT_LONGTEXT, VLC_TRUE );
132 add_bool( SOUT_CFG_PREFIX "auto-mcast", VLC_FALSE, NULL, AUTO_MCAST_TEXT,
133 AUTO_MCAST_LONGTEXT, VLC_TRUE );
134 add_bool( SOUT_CFG_PREFIX "udplite", VLC_FALSE, NULL, UDPLITE_TEXT, UDPLITE_LONGTEXT, VLC_TRUE );
135 add_integer( SOUT_CFG_PREFIX "cscov", 12, NULL, CSCOV_TEXT, CSCOV_LONGTEXT, VLC_TRUE );
137 set_capability( "sout access", 100 );
138 add_shortcut( "udp" );
139 add_shortcut( "rtp" ); // Will work only with ts muxer
140 set_callbacks( Open, Close );
143 /*****************************************************************************
144 * Exported prototypes
145 *****************************************************************************/
147 static const char *const ppsz_sout_options[] = {
159 /* Options handled by the libvlc network core */
160 static const char *const ppsz_core_options[] = {
168 static int Write ( sout_access_out_t *, block_t * );
169 static int WriteRaw( sout_access_out_t *, block_t * );
170 static int Seek ( sout_access_out_t *, off_t );
172 static void ThreadWrite( vlc_object_t * );
173 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
174 static const char *MakeRandMulticast (int family, char *buf, size_t buflen);
176 typedef struct sout_access_thread_t
180 sout_instance_t *p_sout;
182 block_fifo_t *p_fifo;
189 block_fifo_t *p_empty_blocks;
195 uint8_t rtcp_data[28 + 8 + (2 * 257)];
197 } sout_access_thread_t;
199 struct sout_access_out_sys_t
203 vlc_bool_t b_rtpts; // true for RTP/MP2 encapsulation
204 vlc_bool_t b_mtu_warning;
205 uint16_t i_sequence_number;
210 sout_access_thread_t *p_thread;
214 #define DEFAULT_PORT 1234
215 #define RTP_HEADER_LENGTH 12
217 static int OpenRTCP (sout_access_out_t *obj, int proto, uint16_t dport);
218 static void SendRTCP (sout_access_thread_t *obj, uint32_t timestamp);
219 static void CloseRTCP (sout_access_thread_t *obj);
221 /*****************************************************************************
222 * Open: open the file
223 *****************************************************************************/
224 static int Open( vlc_object_t *p_this )
226 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
227 sout_access_out_sys_t *p_sys;
229 char *psz_dst_addr = NULL;
230 int i_dst_port, i_rtcp_port = 0, proto = IPPROTO_UDP;
231 const char *protoname = "UDP";
235 config_ChainParse( p_access, SOUT_CFG_PREFIX,
236 ppsz_sout_options, p_access->p_cfg );
237 config_ChainParse( p_access, "",
238 ppsz_core_options, p_access->p_cfg );
240 if (var_Create (p_access, "dst-port", VLC_VAR_INTEGER)
241 || var_Create (p_access, "src-port", VLC_VAR_INTEGER)
242 || var_Create (p_access, "dst-addr", VLC_VAR_STRING)
243 || var_Create (p_access, "src-addr", VLC_VAR_STRING))
248 if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
250 msg_Err( p_access, "not enough memory" );
253 p_access->p_sys = p_sys;
255 if( p_access->psz_access != NULL )
257 if (strcmp (p_access->psz_access, "rtp") == 0)
258 p_sys->b_rtpts = VLC_TRUE;
261 if (var_GetBool (p_access, SOUT_CFG_PREFIX"lite"))
263 protoname = "UDP-Lite";
264 proto = IPPROTO_UDPLITE;
267 i_dst_port = DEFAULT_PORT;
268 if (var_GetBool (p_access, SOUT_CFG_PREFIX"auto-mcast"))
270 char buf[INET6_ADDRSTRLEN];
271 if (MakeRandMulticast (AF_INET, buf, sizeof (buf)) != NULL)
272 psz_dst_addr = strdup (buf);
276 char *psz_parser = psz_dst_addr = strdup( p_access->psz_path );
278 if (psz_parser[0] == '[')
279 psz_parser = strchr (psz_parser, ']');
281 psz_parser = strchr (psz_parser ?: psz_dst_addr, ':');
282 if (psz_parser != NULL)
284 *psz_parser++ = '\0';
285 i_dst_port = atoi (psz_parser);
289 if (var_GetBool (p_access, SOUT_CFG_PREFIX"rtcp"))
291 /* This is really only for the RTP sout plugin.
292 * Doing RTCP for non RTP packet is NOT a good idea. */
293 i_rtcp_port = var_GetInteger (p_access, SOUT_CFG_PREFIX"rtcp-port");
294 if (i_rtcp_port == 0)
295 i_rtcp_port = i_dst_port + 1;
299 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
300 if( !p_sys->p_thread )
302 msg_Err( p_access, "out of memory" );
308 vlc_object_attach( p_sys->p_thread, p_access );
309 p_sys->p_thread->p_sout = p_access->p_sout;
310 p_sys->p_thread->b_die = 0;
311 p_sys->p_thread->b_error= 0;
312 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
313 p_sys->p_thread->p_empty_blocks = block_FifoNew( p_access );
315 i_handle = net_ConnectDgram( p_this, psz_dst_addr, i_dst_port, -1, proto );
320 msg_Err( p_access, "failed to create %s socket", protoname );
321 vlc_object_destroy (p_sys->p_thread);
327 char addr[NI_MAXNUMERICHOST];
330 if (net_GetSockAddress (i_handle, addr, &port) == 0)
332 msg_Dbg (p_access, "source: %s port %d", addr, port);
333 var_SetString (p_access, "src-addr", addr);
334 var_SetInteger (p_access, "src-port", port);
337 if (net_GetPeerAddress (i_handle, addr, &port) == 0)
339 msg_Dbg (p_access, "destination: %s port %d", addr, port);
340 var_SetString (p_access, "dst-addr", addr);
341 var_SetInteger (p_access, "dst-port", port);
344 p_sys->p_thread->i_handle = i_handle;
345 shutdown( i_handle, SHUT_RD );
347 int cscov = var_GetInteger (p_access, SOUT_CFG_PREFIX"cscov");
352 #ifdef UDPLITE_SEND_CSCOV
353 case IPPROTO_UDPLITE:
355 setsockopt (i_handle, SOL_UDPLITE, UDPLITE_SEND_CSCOV,
356 &(int){ cscov }, sizeof (cscov));
359 #ifdef DCCP_SOCKOPT_RECV_CSCOV
360 /* FIXME: ^^is this the right name ? */
361 /* FIXME: is this inherited by accept() ? */
363 cscov = ((cscov + 3) >> 2) + 1;
365 break; /* out of DCCP cscov range */
366 setsockopt (i_handle, SOL_DCCP, DCCP_SOCKOPT_RECV_CSCOV,
367 &(int){ cscov }, sizeof (cscov));
373 p_sys->p_thread->i_caching =
374 (int64_t)1000 * var_GetInteger( p_access, SOUT_CFG_PREFIX "caching");
375 p_sys->p_thread->i_group =
376 var_GetInteger( p_access, SOUT_CFG_PREFIX "group" );
378 p_sys->i_mtu = var_GetInteger( p_this, "mtu" );
380 srand( (uint32_t)mdate());
381 p_sys->p_buffer = NULL;
382 p_sys->i_sequence_number = rand()&0xffff;
383 p_sys->i_ssrc = rand()&0xffffffff;
385 if (i_rtcp_port && OpenRTCP (p_access, proto, i_rtcp_port))
387 msg_Err (p_access, "cannot initialize RTCP sender");
388 net_Close (i_handle);
389 vlc_object_destroy (p_sys->p_thread);
394 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
395 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
397 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
398 net_Close (i_handle);
399 vlc_object_destroy( p_sys->p_thread );
404 if (var_GetBool (p_access, SOUT_CFG_PREFIX"raw"))
405 p_access->pf_write = WriteRaw;
407 p_access->pf_write = Write;
409 p_access->pf_seek = Seek;
411 /* update p_sout->i_out_pace_nocontrol */
412 p_access->p_sout->i_out_pace_nocontrol++;
417 /*****************************************************************************
418 * Close: close the target
419 *****************************************************************************/
420 static void Close( vlc_object_t * p_this )
422 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
423 sout_access_out_sys_t *p_sys = p_access->p_sys;
426 vlc_object_kill( p_sys->p_thread );
427 for( i = 0; i < 10; i++ )
429 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
432 p_dummy->i_length = 0;
433 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
434 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
436 vlc_thread_join( p_sys->p_thread );
438 block_FifoRelease( p_sys->p_thread->p_fifo );
439 block_FifoRelease( p_sys->p_thread->p_empty_blocks );
441 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
443 net_Close( p_sys->p_thread->i_handle );
444 CloseRTCP (p_sys->p_thread);
446 vlc_object_detach( p_sys->p_thread );
447 vlc_object_destroy( p_sys->p_thread );
448 /* update p_sout->i_out_pace_nocontrol */
449 p_access->p_sout->i_out_pace_nocontrol--;
451 msg_Dbg( p_access, "UDP access output closed" );
455 /*****************************************************************************
456 * Write: standard write on a file descriptor.
457 *****************************************************************************/
458 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
460 sout_access_out_sys_t *p_sys = p_access->p_sys;
467 mtime_t now = mdate();
469 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
471 msg_Warn( p_access, "packet size > MTU, you should probably "
472 "increase the MTU" );
473 p_sys->b_mtu_warning = VLC_TRUE;
476 /* Check if there is enough space in the buffer */
477 if( p_sys->p_buffer &&
478 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
480 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
482 msg_Dbg( p_access, "late packet for UDP input (" I64Fd ")",
483 now - p_sys->p_buffer->i_dts
484 - p_sys->p_thread->i_caching );
486 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
487 p_sys->p_buffer = NULL;
490 i_len += p_buffer->i_buffer;
491 while( p_buffer->i_buffer )
493 int i_payload_size = p_sys->i_mtu;
495 i_payload_size -= RTP_HEADER_LENGTH;
497 int i_write = __MIN( p_buffer->i_buffer, i_payload_size );
501 if( !p_sys->p_buffer )
503 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
504 if( !p_sys->p_buffer ) break;
507 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
508 p_buffer->p_buffer, i_write );
510 p_sys->p_buffer->i_buffer += i_write;
511 p_buffer->p_buffer += i_write;
512 p_buffer->i_buffer -= i_write;
513 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
515 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
516 msg_Warn( p_access, "putting two PCRs at once" );
517 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
520 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
523 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < now )
525 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
526 mdate() - p_sys->p_buffer->i_dts
527 - p_sys->p_thread->i_caching );
529 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
530 p_sys->p_buffer = NULL;
534 p_next = p_buffer->p_next;
535 block_Release( p_buffer );
539 return( p_sys->p_thread->b_error ? -1 : i_len );
542 /*****************************************************************************
543 * WriteRaw: write p_buffer without trying to fill mtu
544 *****************************************************************************/
545 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
547 sout_access_out_sys_t *p_sys = p_access->p_sys;
551 while ( p_sys->p_thread->p_empty_blocks->i_depth >= MAX_EMPTY_BLOCKS )
553 p_buf = block_FifoGet(p_sys->p_thread->p_empty_blocks);
554 block_Release( p_buf );
557 i_len = p_buffer->i_buffer;
558 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
560 return( p_sys->p_thread->b_error ? -1 : i_len );
563 /*****************************************************************************
564 * Seek: seek to a specific location in a file
565 *****************************************************************************/
566 static int Seek( sout_access_out_t *p_access, off_t i_pos )
568 msg_Err( p_access, "UDP sout access cannot seek" );
572 /*****************************************************************************
573 * NewUDPPacket: allocate a new UDP packet of size p_sys->i_mtu
574 *****************************************************************************/
575 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
577 sout_access_out_sys_t *p_sys = p_access->p_sys;
580 while ( p_sys->p_thread->p_empty_blocks->i_depth > MAX_EMPTY_BLOCKS )
582 p_buffer = block_FifoGet( p_sys->p_thread->p_empty_blocks );
583 block_Release( p_buffer );
586 if( p_sys->p_thread->p_empty_blocks->i_depth == 0 )
588 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
592 p_buffer = block_FifoGet(p_sys->p_thread->p_empty_blocks );
593 p_buffer->i_flags = 0;
594 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
597 p_buffer->i_dts = i_dts;
598 p_buffer->i_buffer = 0;
602 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
604 /* add rtp/ts header */
605 p_buffer->p_buffer[0] = 0x80;
606 p_buffer->p_buffer[1] = 33; // mpeg2-ts
608 SetWBE( p_buffer->p_buffer + 2, p_sys->i_sequence_number );
609 p_sys->i_sequence_number++;
610 SetDWBE( p_buffer->p_buffer + 4, i_timestamp );
611 SetDWBE( p_buffer->p_buffer + 8, p_sys->i_ssrc );
613 p_buffer->i_buffer = RTP_HEADER_LENGTH;
619 /*****************************************************************************
620 * ThreadWrite: Write a packet on the network at the good time.
621 *****************************************************************************/
622 static void ThreadWrite( vlc_object_t *p_this )
624 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
625 mtime_t i_date_last = -1;
626 mtime_t i_to_send = p_thread->i_group;
627 int i_dropped_packets = 0;
628 #if defined(WIN32) || defined(UNDER_CE)
629 char strerror_buf[WINSOCK_STRERROR_SIZE];
630 # define strerror( x ) winsock_strerror( strerror_buf )
632 size_t rtcp_counter = 0;
634 while( !p_thread->b_die )
637 mtime_t i_date, i_sent;
639 if( (i++ % 1000)==0 ) {
642 block_t *p_tmp = p_thread->p_empty_blocks->p_first;
643 while( p_tmp ) { p_tmp = p_tmp->p_next; i++;}
644 p_tmp = p_thread->p_fifo->p_first;
645 while( p_tmp ) { p_tmp = p_tmp->p_next; j++;}
646 msg_Dbg( p_thread, "fifo depth: %d/%d, empty blocks: %d/%d",
647 p_thread->p_fifo->i_depth, j,p_thread->p_empty_blocks->i_depth,i );
650 p_pk = block_FifoGet( p_thread->p_fifo );
652 i_date = p_thread->i_caching + p_pk->i_dts;
653 if( i_date_last > 0 )
655 if( i_date - i_date_last > 2000000 )
657 if( !i_dropped_packets )
658 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
659 i_date - i_date_last );
661 block_FifoPut( p_thread->p_empty_blocks, p_pk );
663 i_date_last = i_date;
667 else if( i_date - i_date_last < -1000 )
669 if( !i_dropped_packets )
670 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
671 i_date_last - i_date );
676 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
679 i_to_send = p_thread->i_group;
681 ssize_t val = send( p_thread->i_handle, p_pk->p_buffer,
685 msg_Warn( p_thread, "send error: %s", strerror(errno) );
689 p_thread->sent_pkts++;
690 p_thread->sent_bytes += val;
694 if( i_dropped_packets )
696 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
697 i_dropped_packets = 0;
702 if ( i_sent > i_date + 20000 )
704 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
709 if ((p_thread->rtcp_handle != -1) && (p_pk->i_buffer >= 8))
710 { // 1.25% rate limit:
711 if ((rtcp_counter / 80) >= p_thread->rtcp_size)
713 SendRTCP (p_thread, GetDWBE (p_pk->p_buffer + 4));
718 block_FifoPut( p_thread->p_empty_blocks, p_pk );
720 i_date_last = i_date;
725 static const char *MakeRandMulticast (int family, char *buf, size_t buflen)
727 uint32_t rand = (getpid() & 0xffff)
728 | (uint32_t)(((mdate () >> 10) & 0xffff) << 16);
735 struct in6_addr addr;
736 memcpy (&addr, "\xff\x38\x00\x00" "\x00\x00\x00\x00"
737 "\x00\x00\x00\x00", 12);
739 memcpy (addr.s6_addr + 12, &(uint32_t){ htonl (rand) }, 4);
740 return inet_ntop (family, &addr, buf, buflen);
747 addr.s_addr = htonl ((rand & 0xffffff) | 0xe8000000);
748 return inet_ntop (family, &addr, buf, buflen);
752 errno = EAFNOSUPPORT;
759 * NOTE on RTCP implementation:
760 * - there is a single sender (us), no conferencing here! => n = sender = 1,
761 * - as such we need not bother to include Receiver Reports,
762 * - in unicast case, there is a single receiver => members = 1 + 1 = 2,
763 * and obviously n > 25% of members,
764 * - in multicast case, we do not want to maintain the number of receivers
765 * and we assume it is big (i.e. than 3) because that's what broadcasting is
767 * - it is assumed we_sent = true (could be wrong), since we are THE sender,
768 * - we always send SR + SDES, while running,
769 * - FIXME: we do not implement separate rate limiting for SDES,
770 * - we do not implement any profile-specific extensions for the time being.
772 static int OpenRTCP (sout_access_out_t *obj, int proto, uint16_t dport)
774 sout_access_out_sys_t *p_sys = obj->p_sys;
778 char src[NI_MAXNUMERICHOST], dst[NI_MAXNUMERICHOST];
781 fd = obj->p_sys->p_thread->i_handle;
782 if (net_GetSockAddress (fd, src, &sport)
783 || net_GetPeerAddress (fd, dst, NULL))
787 fd = net_OpenDgram (obj, src, sport, dst, dport, AF_UNSPEC, proto);
791 obj->p_sys->p_thread->rtcp_handle = fd;
793 ptr = (uint8_t *)strchr (src, '%');
795 *ptr = '\0'; /* remove scope ID frop IPv6 addresses */
797 ptr = obj->p_sys->p_thread->rtcp_data;
800 ptr[0] = 2 << 6; /* V = 2, P = RC = 0 */
801 ptr[1] = 200; /* payload type: Sender Report */
802 SetWBE (ptr + 2, 6); /* length = 6 (7 double words) */
803 SetDWBE (ptr + 4, p_sys->i_ssrc);
804 SetQWBE (ptr + 8, NTPtime64 ());
806 /* timestamp and counter are handled later */
808 /* Source description */
810 ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
811 ptr[1] = 202; /* payload type: Source Description */
812 uint8_t *lenptr = ptr + 2;
813 SetDWBE (ptr + 4, p_sys->i_ssrc);
816 ptr[0] = 1; /* CNAME - mandatory */
817 assert (NI_MAXNUMERICHOST <= 256);
818 ptr[1] = strlen (src);
819 memcpy (ptr + 2, src, ptr[1]);
822 static const char tool[] = PACKAGE_STRING;
823 ptr[0] = 6; /* TOOL */
824 ptr[1] = (sizeof (tool) > 256) ? 255 : (sizeof (tool) - 1);
825 memcpy (ptr + 2, tool, ptr[1]);
828 while ((ptr - sdes) & 3) /* 32-bits padding */
830 SetWBE (lenptr, ptr - sdes);
832 obj->p_sys->p_thread->rtcp_size = ptr - obj->p_sys->p_thread->rtcp_data;
836 static void CloseRTCP (sout_access_thread_t *obj)
838 if (obj->rtcp_handle == -1)
841 uint8_t *ptr = obj->rtcp_data;
843 ptr[0] = (2 << 6) | 1; /* V = 2, P = 0, SC = 1 */
844 ptr[1] = 203; /* payload type: Bye */
846 /* SSRC is already there :) */
848 /* We are THE sender, so we are more important than anybody else, so
849 * we can afford not to check bandwidth constraints here. */
850 send (obj->rtcp_handle, obj->rtcp_data, 8, 0);
851 net_Close (obj->rtcp_handle);
854 static void SendRTCP (sout_access_thread_t *obj, uint32_t timestamp)
856 uint8_t *ptr = obj->rtcp_data;
858 uint32_t last = GetDWBE (ptr + 8); // last RTCP SR send time
859 uint64_t now64 = NTPtime64 ();
860 if ((now64 >> 32) < (last + 5))
861 return; // no more than one SR every 5 seconds
863 SetQWBE (ptr + 8, now64);
864 SetDWBE (ptr + 16, timestamp);
865 SetDWBE (ptr + 20, obj->sent_pkts);
866 SetDWBE (ptr + 24, obj->sent_bytes);
868 send (obj->rtcp_handle, ptr, obj->rtcp_size, 0);