1 /*****************************************************************************
2 * rtp.c: rtp stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
5 * Copyright © 2007-2008 Rémi Denis-Courmont
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_block.h>
37 #include <vlc_httpd.h>
39 #include <vlc_network.h>
40 #include <vlc_charset.h>
41 #include <vlc_strings.h>
50 # include <sys/types.h>
53 # include <sys/stat.h>
55 #ifdef HAVE_ARPA_INET_H
56 # include <arpa/inet.h>
58 #ifdef HAVE_LINUX_DCCP_H
59 # include <linux/dccp.h>
62 # define IPPROTO_DCCP 33
64 #ifndef IPPROTO_UDPLITE
65 # define IPPROTO_UDPLITE 136
72 /*****************************************************************************
74 *****************************************************************************/
76 #define DEST_TEXT N_("Destination")
77 #define DEST_LONGTEXT N_( \
78 "This is the output URL that will be used." )
79 #define SDP_TEXT N_("SDP")
80 #define SDP_LONGTEXT N_( \
81 "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
82 "session will be made available. You must use an url: http://location to " \
83 "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
84 "for the SDP to be announced via SAP." )
85 #define SAP_TEXT N_("SAP announcing")
86 #define SAP_LONGTEXT N_("Announce this session with SAP.")
87 #define MUX_TEXT N_("Muxer")
88 #define MUX_LONGTEXT N_( \
89 "This allows you to specify the muxer used for the streaming output. " \
90 "Default is to use no muxer (standard RTP stream)." )
92 #define NAME_TEXT N_("Session name")
93 #define NAME_LONGTEXT N_( \
94 "This is the name of the session that will be announced in the SDP " \
95 "(Session Descriptor)." )
96 #define DESC_TEXT N_("Session description")
97 #define DESC_LONGTEXT N_( \
98 "This allows you to give a short description with details about the stream, " \
99 "that will be announced in the SDP (Session Descriptor)." )
100 #define URL_TEXT N_("Session URL")
101 #define URL_LONGTEXT N_( \
102 "This allows you to give an URL with more details about the stream " \
103 "(often the website of the streaming organization), that will " \
104 "be announced in the SDP (Session Descriptor)." )
105 #define EMAIL_TEXT N_("Session email")
106 #define EMAIL_LONGTEXT N_( \
107 "This allows you to give a contact mail address for the stream, that will " \
108 "be announced in the SDP (Session Descriptor)." )
109 #define PHONE_TEXT N_("Session phone number")
110 #define PHONE_LONGTEXT N_( \
111 "This allows you to give a contact telephone number for the stream, that will " \
112 "be announced in the SDP (Session Descriptor)." )
114 #define PORT_TEXT N_("Port")
115 #define PORT_LONGTEXT N_( \
116 "This allows you to specify the base port for the RTP streaming." )
117 #define PORT_AUDIO_TEXT N_("Audio port")
118 #define PORT_AUDIO_LONGTEXT N_( \
119 "This allows you to specify the default audio port for the RTP streaming." )
120 #define PORT_VIDEO_TEXT N_("Video port")
121 #define PORT_VIDEO_LONGTEXT N_( \
122 "This allows you to specify the default video port for the RTP streaming." )
124 #define TTL_TEXT N_("Hop limit (TTL)")
125 #define TTL_LONGTEXT N_( \
126 "This is the hop limit (also known as \"Time-To-Live\" or TTL) of " \
127 "the multicast packets sent by the stream output (-1 = use operating " \
128 "system built-in default).")
130 #define RTCP_MUX_TEXT N_("RTP/RTCP multiplexing")
131 #define RTCP_MUX_LONGTEXT N_( \
132 "This sends and receives RTCP packet multiplexed over the same port " \
135 #define CACHING_TEXT N_("Caching value (ms)")
136 #define CACHING_LONGTEXT N_( \
137 "Default caching value for outbound RTP streams. This " \
138 "value should be set in milliseconds." )
140 #define PROTO_TEXT N_("Transport protocol")
141 #define PROTO_LONGTEXT N_( \
142 "This selects which transport protocol to use for RTP." )
144 #define SRTP_KEY_TEXT N_("SRTP key (hexadecimal)")
145 #define SRTP_KEY_LONGTEXT N_( \
146 "RTP packets will be integrity-protected and ciphered "\
147 "with this Secure RTP master shared secret key.")
149 #define SRTP_SALT_TEXT N_("SRTP salt (hexadecimal)")
150 #define SRTP_SALT_LONGTEXT N_( \
151 "Secure RTP requires a (non-secret) master salt value.")
153 static const char *const ppsz_protos[] = {
154 "dccp", "sctp", "tcp", "udp", "udplite",
157 static const char *const ppsz_protocols[] = {
158 "DCCP", "SCTP", "TCP", "UDP", "UDP-Lite",
161 #define RFC3016_TEXT N_("MP4A LATM")
162 #define RFC3016_LONGTEXT N_( \
163 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
165 static int Open ( vlc_object_t * );
166 static void Close( vlc_object_t * );
168 #define SOUT_CFG_PREFIX "sout-rtp-"
169 #define MAX_EMPTY_BLOCKS 200
172 set_shortname( N_("RTP"))
173 set_description( N_("RTP stream output") )
174 set_capability( "sout stream", 0 )
175 add_shortcut( "rtp" )
176 set_category( CAT_SOUT )
177 set_subcategory( SUBCAT_SOUT_STREAM )
179 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DEST_TEXT,
180 DEST_LONGTEXT, true )
181 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
183 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
185 add_bool( SOUT_CFG_PREFIX "sap", false, NULL, SAP_TEXT, SAP_LONGTEXT,
188 add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT,
189 NAME_LONGTEXT, true )
190 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
191 DESC_LONGTEXT, true )
192 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
194 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
195 EMAIL_LONGTEXT, true )
196 add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT,
197 PHONE_LONGTEXT, true )
199 add_string( SOUT_CFG_PREFIX "proto", "udp", NULL, PROTO_TEXT,
200 PROTO_LONGTEXT, false )
201 change_string_list( ppsz_protos, ppsz_protocols, NULL )
202 add_integer( SOUT_CFG_PREFIX "port", 5004, NULL, PORT_TEXT,
203 PORT_LONGTEXT, true )
204 add_integer( SOUT_CFG_PREFIX "port-audio", 0, NULL, PORT_AUDIO_TEXT,
205 PORT_AUDIO_LONGTEXT, true )
206 add_integer( SOUT_CFG_PREFIX "port-video", 0, NULL, PORT_VIDEO_TEXT,
207 PORT_VIDEO_LONGTEXT, true )
209 add_integer( SOUT_CFG_PREFIX "ttl", -1, NULL, TTL_TEXT,
211 add_bool( SOUT_CFG_PREFIX "rtcp-mux", false, NULL,
212 RTCP_MUX_TEXT, RTCP_MUX_LONGTEXT, false )
213 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
214 CACHING_TEXT, CACHING_LONGTEXT, true )
217 add_string( SOUT_CFG_PREFIX "key", "", NULL,
218 SRTP_KEY_TEXT, SRTP_KEY_LONGTEXT, false )
219 add_string( SOUT_CFG_PREFIX "salt", "", NULL,
220 SRTP_SALT_TEXT, SRTP_SALT_LONGTEXT, false )
223 add_bool( SOUT_CFG_PREFIX "mp4a-latm", false, NULL, RFC3016_TEXT,
224 RFC3016_LONGTEXT, false )
226 set_callbacks( Open, Close )
229 /*****************************************************************************
230 * Exported prototypes
231 *****************************************************************************/
232 static const char *const ppsz_sout_options[] = {
233 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
234 "sap", "description", "url", "email", "phone",
235 "proto", "rtcp-mux", "caching", "key", "salt",
239 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
240 static int Del ( sout_stream_t *, sout_stream_id_t * );
241 static int Send( sout_stream_t *, sout_stream_id_t *,
243 static sout_stream_id_t *MuxAdd ( sout_stream_t *, es_format_t * );
244 static int MuxDel ( sout_stream_t *, sout_stream_id_t * );
245 static int MuxSend( sout_stream_t *, sout_stream_id_t *,
248 static sout_access_out_t *GrabberCreate( sout_stream_t *p_sout );
249 static void* ThreadSend( vlc_object_t *p_this );
250 static void *rtp_listen_thread( void * );
252 static void SDPHandleUrl( sout_stream_t *, const char * );
254 static int SapSetup( sout_stream_t *p_stream );
255 static int FileSetup( sout_stream_t *p_stream );
256 static int HttpSetup( sout_stream_t *p_stream, const vlc_url_t * );
258 struct sout_stream_sys_t
262 vlc_mutex_t lock_sdp;
269 session_descriptor_t *p_session;
272 httpd_host_t *p_httpd_host;
273 httpd_file_t *p_httpd_file;
279 char *psz_destination;
280 uint32_t payload_bitmap;
282 uint16_t i_port_audio;
283 uint16_t i_port_video;
289 /* in case we do TS/PS over rtp */
291 sout_access_out_t *p_grab;
297 sout_stream_id_t **es;
300 typedef int (*pf_rtp_packetizer_t)( sout_stream_id_t *, block_t * );
302 typedef struct rtp_sink_t
308 struct sout_stream_id_t
312 sout_stream_t *p_stream;
315 uint8_t i_payload_type;
319 uint16_t i_seq_sent_next;
330 /* Packetizer specific fields */
333 srtp_session_t *srtp;
335 pf_rtp_packetizer_t pf_packetize;
338 vlc_mutex_t lock_sink;
341 rtsp_stream_id_t *rtsp_id;
347 block_fifo_t *p_fifo;
351 /*****************************************************************************
353 *****************************************************************************/
354 static int Open( vlc_object_t *p_this )
356 sout_stream_t *p_stream = (sout_stream_t*)p_this;
357 sout_instance_t *p_sout = p_stream->p_sout;
358 sout_stream_sys_t *p_sys = NULL;
359 config_chain_t *p_cfg = NULL;
363 config_ChainParse( p_stream, SOUT_CFG_PREFIX,
364 ppsz_sout_options, p_stream->p_cfg );
366 p_sys = malloc( sizeof( sout_stream_sys_t ) );
370 p_sys->psz_destination = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
372 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
373 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
374 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
375 p_sys->rtcp_mux = var_GetBool( p_stream, SOUT_CFG_PREFIX "rtcp-mux" );
377 if( p_sys->i_port_audio && p_sys->i_port_video == p_sys->i_port_audio )
379 msg_Err( p_stream, "audio and video RTP port must be distinct" );
380 free( p_sys->psz_destination );
385 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
387 if( !strcmp( p_cfg->psz_name, "sdp" )
388 && ( p_cfg->psz_value != NULL )
389 && !strncasecmp( p_cfg->psz_value, "rtsp:", 5 ) )
397 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
400 if( !strncasecmp( psz, "rtsp:", 5 ) )
406 /* Transport protocol */
407 p_sys->proto = IPPROTO_UDP;
408 psz = var_GetNonEmptyString (p_stream, SOUT_CFG_PREFIX"proto");
410 if ((psz == NULL) || !strcasecmp (psz, "udp"))
411 (void)0; /* default */
413 if (!strcasecmp (psz, "dccp"))
415 p_sys->proto = IPPROTO_DCCP;
416 p_sys->rtcp_mux = true; /* Force RTP/RTCP mux */
420 if (!strcasecmp (psz, "sctp"))
422 p_sys->proto = IPPROTO_TCP;
423 p_sys->rtcp_mux = true; /* Force RTP/RTCP mux */
428 if (!strcasecmp (psz, "tcp"))
430 p_sys->proto = IPPROTO_TCP;
431 p_sys->rtcp_mux = true; /* Force RTP/RTCP mux */
435 if (!strcasecmp (psz, "udplite") || !strcasecmp (psz, "udp-lite"))
436 p_sys->proto = IPPROTO_UDPLITE;
438 msg_Warn (p_this, "unknown or unsupported transport protocol \"%s\"",
441 var_Create (p_this, "dccp-service", VLC_VAR_STRING);
443 if( ( p_sys->psz_destination == NULL ) && !b_rtsp )
445 msg_Err( p_stream, "missing destination and not in RTSP mode" );
450 p_sys->i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
451 if( p_sys->i_ttl == -1 )
453 /* Normally, we should let the default hop limit up to the core,
454 * but we have to know it to build our SDP properly, which is why
455 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
457 p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
460 p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
462 p_sys->payload_bitmap = 0;
466 p_sys->psz_sdp = NULL;
468 p_sys->b_export_sap = false;
469 p_sys->p_session = NULL;
470 p_sys->psz_sdp_file = NULL;
472 p_sys->p_httpd_host = NULL;
473 p_sys->p_httpd_file = NULL;
475 p_stream->p_sys = p_sys;
477 vlc_mutex_init( &p_sys->lock_sdp );
478 vlc_mutex_init( &p_sys->lock_es );
480 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
483 sout_stream_id_t *id;
485 /* Check muxer type */
486 if( strncasecmp( psz, "ps", 2 )
487 && strncasecmp( psz, "mpeg1", 5 )
488 && strncasecmp( psz, "ts", 2 ) )
490 msg_Err( p_stream, "unsupported muxer type for RTP (only TS/PS)" );
492 vlc_mutex_destroy( &p_sys->lock_sdp );
493 vlc_mutex_destroy( &p_sys->lock_es );
494 free( p_sys->psz_destination );
499 p_sys->p_grab = GrabberCreate( p_stream );
500 p_sys->p_mux = sout_MuxNew( p_sout, psz, p_sys->p_grab );
503 if( p_sys->p_mux == NULL )
505 msg_Err( p_stream, "cannot create muxer" );
506 sout_AccessOutDelete( p_sys->p_grab );
507 vlc_mutex_destroy( &p_sys->lock_sdp );
508 vlc_mutex_destroy( &p_sys->lock_es );
509 free( p_sys->psz_destination );
514 id = Add( p_stream, NULL );
517 sout_MuxDelete( p_sys->p_mux );
518 sout_AccessOutDelete( p_sys->p_grab );
519 vlc_mutex_destroy( &p_sys->lock_sdp );
520 vlc_mutex_destroy( &p_sys->lock_es );
521 free( p_sys->psz_destination );
526 p_sys->packet = NULL;
528 p_stream->pf_add = MuxAdd;
529 p_stream->pf_del = MuxDel;
530 p_stream->pf_send = MuxSend;
535 p_sys->p_grab = NULL;
537 p_stream->pf_add = Add;
538 p_stream->pf_del = Del;
539 p_stream->pf_send = Send;
542 if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) )
543 SDPHandleUrl( p_stream, "sap" );
545 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
548 config_chain_t *p_cfg;
550 SDPHandleUrl( p_stream, psz );
552 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
554 if( !strcmp( p_cfg->psz_name, "sdp" ) )
556 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
559 /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
560 if( !strcmp( p_cfg->psz_value, psz ) )
563 SDPHandleUrl( p_stream, p_cfg->psz_value );
569 /* update p_sout->i_out_pace_nocontrol */
570 p_stream->p_sout->i_out_pace_nocontrol++;
575 /*****************************************************************************
577 *****************************************************************************/
578 static void Close( vlc_object_t * p_this )
580 sout_stream_t *p_stream = (sout_stream_t*)p_this;
581 sout_stream_sys_t *p_sys = p_stream->p_sys;
583 /* update p_sout->i_out_pace_nocontrol */
584 p_stream->p_sout->i_out_pace_nocontrol--;
588 assert( p_sys->i_es == 1 );
590 sout_MuxDelete( p_sys->p_mux );
591 Del( p_stream, p_sys->es[0] );
592 sout_AccessOutDelete( p_sys->p_grab );
596 block_Release( p_sys->packet );
598 if( p_sys->b_export_sap )
601 SapSetup( p_stream );
605 if( p_sys->rtsp != NULL )
606 RtspUnsetup( p_sys->rtsp );
608 vlc_mutex_destroy( &p_sys->lock_sdp );
609 vlc_mutex_destroy( &p_sys->lock_es );
611 if( p_sys->p_httpd_file )
612 httpd_FileDelete( p_sys->p_httpd_file );
614 if( p_sys->p_httpd_host )
615 httpd_HostDelete( p_sys->p_httpd_host );
617 free( p_sys->psz_sdp );
619 if( p_sys->psz_sdp_file != NULL )
622 unlink( p_sys->psz_sdp_file );
624 free( p_sys->psz_sdp_file );
626 free( p_sys->psz_destination );
630 /*****************************************************************************
632 *****************************************************************************/
633 static void SDPHandleUrl( sout_stream_t *p_stream, const char *psz_url )
635 sout_stream_sys_t *p_sys = p_stream->p_sys;
638 vlc_UrlParse( &url, psz_url, 0 );
639 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
641 if( p_sys->p_httpd_file )
643 msg_Err( p_stream, "you can use sdp=http:// only once" );
647 if( HttpSetup( p_stream, &url ) )
649 msg_Err( p_stream, "cannot export SDP as HTTP" );
652 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
654 if( p_sys->rtsp != NULL )
656 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
660 /* FIXME test if destination is multicast or no destination at all */
661 p_sys->rtsp = RtspSetup( p_stream, &url );
662 if( p_sys->rtsp == NULL )
663 msg_Err( p_stream, "cannot export SDP as RTSP" );
665 if( p_sys->p_mux != NULL )
667 sout_stream_id_t *id = p_sys->es[0];
668 id->rtsp_id = RtspAddId( p_sys->rtsp, id, 0, GetDWBE( id->ssrc ),
669 p_sys->psz_destination, p_sys->i_ttl,
670 id->i_port, id->i_port + 1 );
673 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
674 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
676 p_sys->b_export_sap = true;
677 SapSetup( p_stream );
679 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
681 if( p_sys->psz_sdp_file != NULL )
683 msg_Err( p_stream, "you can use sdp=file:// only once" );
686 psz_url = &psz_url[5];
687 if( psz_url[0] == '/' && psz_url[1] == '/' )
689 p_sys->psz_sdp_file = strdup( psz_url );
690 if( p_sys->psz_sdp_file == NULL )
692 decode_URI( p_sys->psz_sdp_file ); /* FIXME? */
693 FileSetup( p_stream );
697 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
702 vlc_UrlClean( &url );
705 /*****************************************************************************
707 *****************************************************************************/
709 char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
711 const sout_stream_sys_t *p_sys = p_stream->p_sys;
713 struct sockaddr_storage dst;
717 * When we have a fixed destination (typically when we do multicast),
718 * we need to put the actual port numbers in the SDP.
719 * When there is no fixed destination, we only support RTSP unicast
720 * on-demand setup, so we should rather let the clients decide which ports
722 * When there is both a fixed destination and RTSP unicast, we need to
723 * put port numbers used by the fixed destination, otherwise the SDP would
724 * become totally incorrect for multicast use. It should be noted that
725 * port numbers from SDP with RTSP are only "recommendation" from the
726 * server to the clients (per RFC2326), so only broken clients will fail
727 * to handle this properly. There is no solution but to use two differents
728 * output chain with two different RTSP URLs if you need to handle this
733 if( p_sys->psz_destination != NULL )
737 /* Oh boy, this is really ugly! (+ race condition on lock_es) */
738 dstlen = sizeof( dst );
739 if( p_sys->es[0]->listen.fd != NULL )
740 getsockname( p_sys->es[0]->listen.fd[0],
741 (struct sockaddr *)&dst, &dstlen );
743 getpeername( p_sys->es[0]->sinkv[0].rtp_fd,
744 (struct sockaddr *)&dst, &dstlen );
750 /* Dummy destination address for RTSP */
751 memset (&dst, 0, sizeof( struct sockaddr_in ) );
752 dst.ss_family = AF_INET;
756 dstlen = sizeof( struct sockaddr_in );
759 psz_sdp = vlc_sdp_Start( VLC_OBJECT( p_stream ), SOUT_CFG_PREFIX,
760 NULL, 0, (struct sockaddr *)&dst, dstlen );
761 if( psz_sdp == NULL )
764 /* TODO: a=source-filter */
765 if( p_sys->rtcp_mux )
766 sdp_AddAttribute( &psz_sdp, "rtcp-mux", NULL );
768 if( rtsp_url != NULL )
769 sdp_AddAttribute ( &psz_sdp, "control", "%s", rtsp_url );
771 /* FIXME: locking?! */
772 for( i = 0; i < p_sys->i_es; i++ )
774 sout_stream_id_t *id = p_sys->es[i];
775 const char *mime_major; /* major MIME type */
776 const char *proto = "RTP/AVP"; /* protocol */
781 mime_major = "video";
784 mime_major = "audio";
793 if( rtsp_url == NULL )
795 switch( p_sys->proto )
800 proto = "TCP/RTP/AVP";
803 proto = "DCCP/RTP/AVP";
805 case IPPROTO_UDPLITE:
810 sdp_AddMedia( &psz_sdp, mime_major, proto, inclport * id->i_port,
811 id->i_payload_type, false, id->i_bitrate,
812 id->psz_enc, id->i_clock_rate, id->i_channels,
815 if( !p_sys->rtcp_mux && (id->i_port & 1) ) /* cf RFC4566 §5.14 */
816 sdp_AddAttribute ( &psz_sdp, "rtcp", "%u", id->i_port + 1 );
818 if( rtsp_url != NULL )
820 assert( strlen( rtsp_url ) > 0 );
821 bool addslash = ( rtsp_url[strlen( rtsp_url ) - 1] != '/' );
822 sdp_AddAttribute ( &psz_sdp, "control",
823 addslash ? "%s/trackID=%u" : "%strackID=%u",
828 if( id->listen.fd != NULL )
829 sdp_AddAttribute( &psz_sdp, "setup", "passive" );
830 if( p_sys->proto == IPPROTO_DCCP )
831 sdp_AddAttribute( &psz_sdp, "dccp-service-code",
832 "SC:RTP%c", toupper( mime_major[0] ) );
839 /*****************************************************************************
841 *****************************************************************************/
843 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
845 static const char hex[16] = "0123456789abcdef";
848 for( i = 0; i < i_data; i++ )
850 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
851 s[2*i+1] = hex[(p_data[i] )&0xf];
857 * Shrink the MTU down to a fixed packetization time (for audio).
860 rtp_set_ptime (sout_stream_id_t *id, unsigned ptime_ms, size_t bytes)
862 /* Samples per second */
863 size_t spl = (id->i_clock_rate - 1) * ptime_ms / 1000 + 1;
864 bytes *= id->i_channels;
867 if (spl < rtp_mtu (id)) /* MTU is big enough for ptime */
868 id->i_mtu = 12 + spl;
869 else /* MTU is too small for ptime, align to a sample boundary */
870 id->i_mtu = 12 + (((id->i_mtu - 12) / bytes) * bytes);
873 /** Add an ES as a new RTP stream */
874 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
876 /* NOTE: As a special case, if we use a non-RTP
877 * mux (TS/PS), then p_fmt is NULL. */
878 sout_stream_sys_t *p_sys = p_stream->p_sys;
879 sout_stream_id_t *id;
882 if (0xffffffff == p_sys->payload_bitmap)
884 msg_Err (p_stream, "too many RTP elementary streams");
888 /* Choose the port */
893 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
894 i_port = p_sys->i_port_audio;
896 if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
897 i_port = p_sys->i_port_video;
899 /* We do not need the ES lock (p_sys->lock_es) here, because this is the
900 * only one thread that can *modify* the ES table. The ES lock protects
901 * the other threads from our modifications (TAB_APPEND, TAB_REMOVE). */
902 for (int i = 0; i_port && (i < p_sys->i_es); i++)
903 if (i_port == p_sys->es[i]->i_port)
904 i_port = 0; /* Port already in use! */
905 for (uint16_t p = p_sys->i_port; i_port == 0; p += 2)
909 msg_Err (p_stream, "too many RTP elementary streams");
913 for (int i = 0; i_port && (i < p_sys->i_es); i++)
914 if (p == p_sys->es[i]->i_port)
918 id = vlc_object_create( p_stream, sizeof( sout_stream_id_t ) );
921 vlc_object_attach( id, p_stream );
923 id->p_stream = p_stream;
925 /* Look for free dymanic payload type */
926 id->i_payload_type = 96;
927 while (p_sys->payload_bitmap & (1 << (id->i_payload_type - 96)))
928 id->i_payload_type++;
929 assert (id->i_payload_type < 128);
931 vlc_rand_bytes (&id->i_sequence, sizeof (id->i_sequence));
932 vlc_rand_bytes (id->ssrc, sizeof (id->ssrc));
934 id->i_seq_sent_next = id->i_sequence;
938 id->i_clock_rate = 90000; /* most common case for video */
943 id->i_cat = p_fmt->i_cat;
944 if( p_fmt->i_cat == AUDIO_ES )
946 id->i_clock_rate = p_fmt->audio.i_rate;
947 id->i_channels = p_fmt->audio.i_channels;
949 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
953 id->i_cat = VIDEO_ES;
957 id->i_mtu = config_GetInt( p_stream, "mtu" );
958 if( id->i_mtu <= 12 + 16 )
959 id->i_mtu = 576 - 20 - 8; /* pessimistic */
960 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
962 id->pf_packetize = NULL;
967 char *key = var_CreateGetNonEmptyString (p_stream, SOUT_CFG_PREFIX"key");
970 id->srtp = srtp_create (SRTP_ENCR_AES_CM, SRTP_AUTH_HMAC_SHA1, 10,
971 SRTP_PRF_AES_CM, SRTP_RCC_MODE1);
972 if (id->srtp == NULL)
978 char *salt = var_CreateGetNonEmptyString (p_stream, SOUT_CFG_PREFIX"salt");
979 errno = srtp_setkeystring (id->srtp, key, salt ? salt : "");
984 msg_Err (p_stream, "bad SRTP key/salt combination (%m)");
987 id->i_sequence = 0; /* FIXME: awful hack for libvlc_srtp */
991 vlc_mutex_init( &id->lock_sink );
996 id->listen.fd = NULL;
999 (int64_t)1000 * var_GetInteger( p_stream, SOUT_CFG_PREFIX "caching");
1001 if( p_sys->psz_destination != NULL )
1002 switch( p_sys->proto )
1009 case VIDEO_ES: code = "RTPV"; break;
1010 case AUDIO_ES: code = "RTPARTPV"; break;
1011 case SPU_ES: code = "RTPTRTPV"; break;
1012 default: code = "RTPORTPV"; break;
1014 var_SetString (p_stream, "dccp-service", code);
1015 } /* fall through */
1017 id->listen.fd = net_Listen( VLC_OBJECT(p_stream),
1018 p_sys->psz_destination, i_port,
1020 if( id->listen.fd == NULL )
1022 msg_Err( p_stream, "passive COMEDIA RTP socket failed" );
1025 if( vlc_clone( &id->listen.thread, rtp_listen_thread, id,
1026 VLC_THREAD_PRIORITY_LOW ) )
1028 net_ListenClose( id->listen.fd );
1029 id->listen.fd = NULL;
1036 int ttl = (p_sys->i_ttl >= 0) ? p_sys->i_ttl : -1;
1037 int fd = net_ConnectDgram( p_stream, p_sys->psz_destination,
1038 i_port, ttl, p_sys->proto );
1041 msg_Err( p_stream, "cannot create RTP socket" );
1044 /* Ignore any unexpected incoming packet (including RTCP-RR
1045 * packets in case of rtcp-mux) */
1046 setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &(int){ 0 },
1048 rtp_add_sink( id, fd, p_sys->rtcp_mux, NULL );
1054 char *psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
1056 if( psz == NULL ) /* Uho! */
1059 if( strncmp( psz, "ts", 2 ) == 0 )
1061 id->i_payload_type = 33;
1062 id->psz_enc = "MP2T";
1066 id->psz_enc = "MP2P";
1071 switch( p_fmt->i_codec )
1073 case VLC_CODEC_MULAW:
1074 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
1075 id->i_payload_type = 0;
1076 id->psz_enc = "PCMU";
1077 id->pf_packetize = rtp_packetize_split;
1078 rtp_set_ptime (id, 20, 1);
1080 case VLC_CODEC_ALAW:
1081 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 8000 )
1082 id->i_payload_type = 8;
1083 id->psz_enc = "PCMA";
1084 id->pf_packetize = rtp_packetize_split;
1085 rtp_set_ptime (id, 20, 1);
1087 case VLC_CODEC_S16B:
1088 case VLC_CODEC_S16L:
1089 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
1091 id->i_payload_type = 11;
1093 else if( p_fmt->audio.i_channels == 2 &&
1094 p_fmt->audio.i_rate == 44100 )
1096 id->i_payload_type = 10;
1098 id->psz_enc = "L16";
1099 if( p_fmt->i_codec == VLC_CODEC_S16B )
1100 id->pf_packetize = rtp_packetize_split;
1102 id->pf_packetize = rtp_packetize_swab;
1103 rtp_set_ptime (id, 20, 2);
1107 id->pf_packetize = rtp_packetize_split;
1108 rtp_set_ptime (id, 20, 1);
1110 case VLC_CODEC_MPGA:
1111 id->i_payload_type = 14;
1112 id->psz_enc = "MPA";
1113 id->i_clock_rate = 90000; /* not 44100 */
1114 id->pf_packetize = rtp_packetize_mpa;
1116 case VLC_CODEC_MPGV:
1117 id->i_payload_type = 32;
1118 id->psz_enc = "MPV";
1119 id->pf_packetize = rtp_packetize_mpv;
1121 case VLC_CODEC_ADPCM_G726:
1122 switch( p_fmt->i_bitrate / 1000 )
1125 id->psz_enc = "G726-16";
1126 id->pf_packetize = rtp_packetize_g726_16;
1129 id->psz_enc = "G726-24";
1130 id->pf_packetize = rtp_packetize_g726_24;
1133 id->psz_enc = "G726-32";
1134 id->pf_packetize = rtp_packetize_g726_32;
1137 id->psz_enc = "G726-40";
1138 id->pf_packetize = rtp_packetize_g726_40;
1141 msg_Err( p_stream, "cannot add this stream (unsupported "
1142 "G.726 bit rate: %u)", p_fmt->i_bitrate );
1147 id->psz_enc = "ac3";
1148 id->pf_packetize = rtp_packetize_ac3;
1150 case VLC_CODEC_H263:
1151 id->psz_enc = "H263-1998";
1152 id->pf_packetize = rtp_packetize_h263;
1154 case VLC_CODEC_H264:
1155 id->psz_enc = "H264";
1156 id->pf_packetize = rtp_packetize_h264;
1157 id->psz_fmtp = NULL;
1159 if( p_fmt->i_extra > 0 )
1161 uint8_t *p_buffer = p_fmt->p_extra;
1162 int i_buffer = p_fmt->i_extra;
1163 char *p_64_sps = NULL;
1164 char *p_64_pps = NULL;
1167 while( i_buffer > 4 &&
1168 p_buffer[0] == 0 && p_buffer[1] == 0 &&
1169 p_buffer[2] == 0 && p_buffer[3] == 1 )
1171 const int i_nal_type = p_buffer[4]&0x1f;
1175 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
1178 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
1180 if( !memcmp (p_buffer + i_offset, "\x00\x00\x00\x01", 4 ) )
1182 /* we found another startcode */
1187 if( i_nal_type == 7 )
1189 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1190 sprintf_hexa( hexa, &p_buffer[5], 3 );
1192 else if( i_nal_type == 8 )
1194 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1200 if( p_64_sps && p_64_pps &&
1201 ( asprintf( &id->psz_fmtp,
1202 "packetization-mode=1;profile-level-id=%s;"
1203 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
1204 p_64_pps ) == -1 ) )
1205 id->psz_fmtp = NULL;
1210 id->psz_fmtp = strdup( "packetization-mode=1" );
1213 case VLC_CODEC_MP4V:
1215 char hexa[2*p_fmt->i_extra +1];
1217 id->psz_enc = "MP4V-ES";
1218 id->pf_packetize = rtp_packetize_split;
1219 if( p_fmt->i_extra > 0 )
1221 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1222 if( asprintf( &id->psz_fmtp,
1223 "profile-level-id=3; config=%s;", hexa ) == -1 )
1224 id->psz_fmtp = NULL;
1228 case VLC_CODEC_MP4A:
1232 char hexa[2*p_fmt->i_extra +1];
1234 id->psz_enc = "mpeg4-generic";
1235 id->pf_packetize = rtp_packetize_mp4a;
1236 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1237 if( asprintf( &id->psz_fmtp,
1238 "streamtype=5; profile-level-id=15; "
1239 "mode=AAC-hbr; config=%s; SizeLength=13; "
1240 "IndexLength=3; IndexDeltaLength=3; Profile=1;",
1242 id->psz_fmtp = NULL;
1248 unsigned char config[6];
1249 unsigned int aacsrates[15] = {
1250 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1251 16000, 12000, 11025, 8000, 7350, 0, 0 };
1253 for( i = 0; i < 15; i++ )
1254 if( p_fmt->audio.i_rate == aacsrates[i] )
1260 config[3]=p_fmt->audio.i_channels<<4;
1264 id->psz_enc = "MP4A-LATM";
1265 id->pf_packetize = rtp_packetize_mp4a_latm;
1266 sprintf_hexa( hexa, config, 6 );
1267 if( asprintf( &id->psz_fmtp, "profile-level-id=15; "
1268 "object=2; cpresent=0; config=%s", hexa ) == -1 )
1269 id->psz_fmtp = NULL;
1273 case VLC_CODEC_AMR_NB:
1274 id->psz_enc = "AMR";
1275 id->psz_fmtp = strdup( "octet-align=1" );
1276 id->pf_packetize = rtp_packetize_amr;
1278 case VLC_CODEC_AMR_WB:
1279 id->psz_enc = "AMR-WB";
1280 id->psz_fmtp = strdup( "octet-align=1" );
1281 id->pf_packetize = rtp_packetize_amr;
1283 case VLC_CODEC_SPEEX:
1284 id->psz_enc = "SPEEX";
1285 id->pf_packetize = rtp_packetize_spx;
1287 case VLC_CODEC_ITU_T140:
1288 id->psz_enc = "t140" ;
1289 id->i_clock_rate = 1000;
1290 id->pf_packetize = rtp_packetize_t140;
1294 msg_Err( p_stream, "cannot add this stream (unsupported "
1295 "codec: %4.4s)", (char*)&p_fmt->i_codec );
1298 if (id->i_payload_type >= 96)
1299 /* Mark dynamic payload type in use */
1300 p_sys->payload_bitmap |= 1 << (id->i_payload_type - 96);
1302 #if 0 /* No payload formats sets this at the moment */
1305 cscov += 8 /* UDP */ + 12 /* RTP */;
1307 net_SetCSCov( id->sinkv[0].rtp_fd, cscov, -1 );
1310 if( p_sys->rtsp != NULL )
1311 id->rtsp_id = RtspAddId( p_sys->rtsp, id, p_sys->i_es,
1312 GetDWBE( id->ssrc ),
1313 p_sys->psz_destination,
1314 p_sys->i_ttl, id->i_port, id->i_port + 1 );
1316 id->p_fifo = block_FifoNew();
1317 if( vlc_thread_create( id, "RTP send thread", ThreadSend,
1318 VLC_THREAD_PRIORITY_HIGHEST ) )
1321 /* Update p_sys context */
1322 vlc_mutex_lock( &p_sys->lock_es );
1323 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1324 vlc_mutex_unlock( &p_sys->lock_es );
1326 psz_sdp = SDPGenerate( p_stream, NULL );
1328 vlc_mutex_lock( &p_sys->lock_sdp );
1329 free( p_sys->psz_sdp );
1330 p_sys->psz_sdp = psz_sdp;
1331 vlc_mutex_unlock( &p_sys->lock_sdp );
1333 msg_Dbg( p_stream, "sdp=\n%s", p_sys->psz_sdp );
1335 /* Update SDP (sap/file) */
1336 if( p_sys->b_export_sap ) SapSetup( p_stream );
1337 if( p_sys->psz_sdp_file != NULL ) FileSetup( p_stream );
1342 Del( p_stream, id );
1346 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1348 sout_stream_sys_t *p_sys = p_stream->p_sys;
1350 if( id->p_fifo != NULL )
1352 vlc_object_kill( id );
1353 vlc_thread_join( id );
1354 block_FifoRelease( id->p_fifo );
1357 vlc_mutex_lock( &p_sys->lock_es );
1358 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1359 vlc_mutex_unlock( &p_sys->lock_es );
1361 /* Release dynamic payload type */
1362 if (id->i_payload_type >= 96)
1363 p_sys->payload_bitmap &= ~(1 << (id->i_payload_type - 96));
1365 free( id->psz_fmtp );
1368 RtspDelId( p_sys->rtsp, id->rtsp_id );
1370 rtp_del_sink( id, id->sinkv[0].rtp_fd ); /* sink for explicit dst= */
1371 if( id->listen.fd != NULL )
1373 vlc_cancel( id->listen.thread );
1374 vlc_join( id->listen.thread, NULL );
1375 net_ListenClose( id->listen.fd );
1378 if( id->srtp != NULL )
1379 srtp_destroy( id->srtp );
1382 vlc_mutex_destroy( &id->lock_sink );
1384 /* Update SDP (sap/file) */
1385 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1386 if( p_sys->psz_sdp_file != NULL ) FileSetup( p_stream );
1388 vlc_object_detach( id );
1389 vlc_object_release( id );
1393 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1398 assert( p_stream->p_sys->p_mux == NULL );
1401 while( p_buffer != NULL )
1403 p_next = p_buffer->p_next;
1404 if( id->pf_packetize( id, p_buffer ) )
1407 block_Release( p_buffer );
1413 /****************************************************************************
1415 ****************************************************************************/
1416 static int SapSetup( sout_stream_t *p_stream )
1418 sout_stream_sys_t *p_sys = p_stream->p_sys;
1419 sout_instance_t *p_sout = p_stream->p_sout;
1421 /* Remove the previous session */
1422 if( p_sys->p_session != NULL)
1424 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1425 p_sys->p_session = NULL;
1428 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1430 announce_method_t *p_method = sout_SAPMethod();
1431 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout,
1433 p_sys->psz_destination,
1435 sout_MethodRelease( p_method );
1441 /****************************************************************************
1443 ****************************************************************************/
1444 static int FileSetup( sout_stream_t *p_stream )
1446 sout_stream_sys_t *p_sys = p_stream->p_sys;
1449 if( p_sys->psz_sdp == NULL )
1450 return VLC_EGENERIC; /* too early */
1452 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1454 msg_Err( p_stream, "cannot open file '%s' (%m)",
1455 p_sys->psz_sdp_file );
1456 return VLC_EGENERIC;
1459 fputs( p_sys->psz_sdp, f );
1465 /****************************************************************************
1467 ****************************************************************************/
1468 static int HttpCallback( httpd_file_sys_t *p_args,
1469 httpd_file_t *, uint8_t *p_request,
1470 uint8_t **pp_data, int *pi_data );
1472 static int HttpSetup( sout_stream_t *p_stream, const vlc_url_t *url)
1474 sout_stream_sys_t *p_sys = p_stream->p_sys;
1476 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
1477 url->i_port > 0 ? url->i_port : 80 );
1478 if( p_sys->p_httpd_host )
1480 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1481 url->psz_path ? url->psz_path : "/",
1484 HttpCallback, (void*)p_sys );
1486 if( p_sys->p_httpd_file == NULL )
1488 return VLC_EGENERIC;
1493 static int HttpCallback( httpd_file_sys_t *p_args,
1494 httpd_file_t *f, uint8_t *p_request,
1495 uint8_t **pp_data, int *pi_data )
1497 VLC_UNUSED(f); VLC_UNUSED(p_request);
1498 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1500 vlc_mutex_lock( &p_sys->lock_sdp );
1501 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1503 *pi_data = strlen( p_sys->psz_sdp );
1504 *pp_data = malloc( *pi_data );
1505 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1512 vlc_mutex_unlock( &p_sys->lock_sdp );
1517 /****************************************************************************
1519 ****************************************************************************/
1520 static void* ThreadSend( vlc_object_t *p_this )
1523 # define ECONNREFUSED WSAECONNREFUSED
1524 # define ENOPROTOOPT WSAENOPROTOOPT
1525 # define EHOSTUNREACH WSAEHOSTUNREACH
1526 # define ENETUNREACH WSAENETUNREACH
1527 # define ENETDOWN WSAENETDOWN
1528 # define ENOBUFS WSAENOBUFS
1529 # define EAGAIN WSAEWOULDBLOCK
1530 # define EWOULDBLOCK WSAEWOULDBLOCK
1532 sout_stream_id_t *id = (sout_stream_id_t *)p_this;
1533 unsigned i_caching = id->i_caching;
1537 block_t *out = block_FifoGet( id->p_fifo );
1538 block_cleanup_push (out);
1542 { /* FIXME: this is awfully inefficient */
1543 size_t len = out->i_buffer;
1544 out = block_Realloc( out, 0, len + 10 );
1545 out->i_buffer = len;
1547 int canc = vlc_savecancel ();
1548 int val = srtp_send( id->srtp, out->p_buffer, &len, len + 10 );
1549 vlc_restorecancel (canc);
1553 msg_Dbg( id, "SRTP sending error: %m" );
1554 block_Release( out );
1558 out->i_buffer = len;
1562 mwait (out->i_dts + i_caching);
1567 ssize_t len = out->i_buffer;
1568 int canc = vlc_savecancel ();
1570 vlc_mutex_lock( &id->lock_sink );
1571 unsigned deadc = 0; /* How many dead sockets? */
1572 int deadv[id->sinkc]; /* Dead sockets list */
1574 for( int i = 0; i < id->sinkc; i++ )
1577 if( !id->srtp ) /* FIXME: SRTCP support */
1579 SendRTCP( id->sinkv[i].rtcp, out );
1581 if( send( id->sinkv[i].rtp_fd, out->p_buffer, len, 0 ) >= 0 )
1585 /* Soft errors (e.g. ICMP): */
1586 case ECONNREFUSED: /* Port unreachable */
1589 case EPROTO: /* Protocol unreachable */
1591 case EHOSTUNREACH: /* Host unreachable */
1592 case ENETUNREACH: /* Network unreachable */
1593 case ENETDOWN: /* Entire network down */
1594 send( id->sinkv[i].rtp_fd, out->p_buffer, len, 0 );
1595 /* Transient congestion: */
1596 case ENOMEM: /* out of socket buffers */
1599 #if (EAGAIN != EWOULDBLOCK)
1605 deadv[deadc++] = id->sinkv[i].rtp_fd;
1607 id->i_seq_sent_next = ntohs(((uint16_t *) out->p_buffer)[1]) + 1;
1608 vlc_mutex_unlock( &id->lock_sink );
1609 block_Release( out );
1611 for( unsigned i = 0; i < deadc; i++ )
1613 msg_Dbg( id, "removing socket %d", deadv[i] );
1614 rtp_del_sink( id, deadv[i] );
1616 vlc_restorecancel (canc);
1622 /* This thread dequeues incoming connections (DCCP streaming) */
1623 static void *rtp_listen_thread( void *data )
1625 sout_stream_id_t *id = data;
1627 assert( id->listen.fd != NULL );
1631 int fd = net_Accept( id, id->listen.fd );
1634 int canc = vlc_savecancel( );
1635 rtp_add_sink( id, fd, true, NULL );
1636 vlc_restorecancel( canc );
1643 int rtp_add_sink( sout_stream_id_t *id, int fd, bool rtcp_mux, uint16_t *seq )
1645 rtp_sink_t sink = { fd, NULL };
1646 sink.rtcp = OpenRTCP( VLC_OBJECT( id->p_stream ), fd, IPPROTO_UDP,
1648 if( sink.rtcp == NULL )
1649 msg_Err( id, "RTCP failed!" );
1651 vlc_mutex_lock( &id->lock_sink );
1652 INSERT_ELEM( id->sinkv, id->sinkc, id->sinkc, sink );
1654 *seq = id->i_seq_sent_next;
1655 vlc_mutex_unlock( &id->lock_sink );
1659 void rtp_del_sink( sout_stream_id_t *id, int fd )
1661 rtp_sink_t sink = { fd, NULL };
1663 /* NOTE: must be safe to use if fd is not included */
1664 vlc_mutex_lock( &id->lock_sink );
1665 for( int i = 0; i < id->sinkc; i++ )
1667 if (id->sinkv[i].rtp_fd == fd)
1669 sink = id->sinkv[i];
1670 REMOVE_ELEM( id->sinkv, id->sinkc, i );
1674 vlc_mutex_unlock( &id->lock_sink );
1676 CloseRTCP( sink.rtcp );
1677 net_Close( sink.rtp_fd );
1680 uint16_t rtp_get_seq( sout_stream_id_t *id )
1682 /* This will return values for the next packet. */
1685 vlc_mutex_lock( &id->lock_sink );
1686 seq = id->i_seq_sent_next;
1687 vlc_mutex_unlock( &id->lock_sink );
1692 /* FIXME: this is pretty bad - if we remove and then insert an ES
1693 * the number will get unsynched from inside RTSP */
1694 unsigned rtp_get_num( const sout_stream_id_t *id )
1696 sout_stream_sys_t *p_sys = id->p_stream->p_sys;
1699 vlc_mutex_lock( &p_sys->lock_es );
1700 for( i = 0; i < p_sys->i_es; i++ )
1702 if( id == p_sys->es[i] )
1705 vlc_mutex_unlock( &p_sys->lock_es );
1711 void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1712 int b_marker, int64_t i_pts )
1714 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / CLOCK_FREQ;
1716 out->p_buffer[0] = 0x80;
1717 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1718 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1719 out->p_buffer[3] = ( id->i_sequence )&0xff;
1720 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1721 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1722 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1723 out->p_buffer[7] = ( i_timestamp )&0xff;
1725 memcpy( out->p_buffer + 8, id->ssrc, 4 );
1731 void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1733 block_FifoPut( id->p_fifo, out );
1737 * @return configured max RTP payload size (including payload type-specific
1738 * headers, excluding RTP and transport headers)
1740 size_t rtp_mtu (const sout_stream_id_t *id)
1742 return id->i_mtu - 12;
1745 /*****************************************************************************
1747 *****************************************************************************/
1749 /** Add an ES to a non-RTP muxed stream */
1750 static sout_stream_id_t *MuxAdd( sout_stream_t *p_stream, es_format_t *p_fmt )
1752 sout_input_t *p_input;
1753 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
1754 assert( p_mux != NULL );
1756 p_input = sout_MuxAddStream( p_mux, p_fmt );
1757 if( p_input == NULL )
1759 msg_Err( p_stream, "cannot add this stream to the muxer" );
1763 return (sout_stream_id_t *)p_input;
1767 static int MuxSend( sout_stream_t *p_stream, sout_stream_id_t *id,
1770 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
1771 assert( p_mux != NULL );
1773 sout_MuxSendBuffer( p_mux, (sout_input_t *)id, p_buffer );
1778 /** Remove an ES from a non-RTP muxed stream */
1779 static int MuxDel( sout_stream_t *p_stream, sout_stream_id_t *id )
1781 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
1782 assert( p_mux != NULL );
1784 sout_MuxDeleteStream( p_mux, (sout_input_t *)id );
1789 static ssize_t AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1790 const block_t *p_buffer )
1792 sout_stream_sys_t *p_sys = p_stream->p_sys;
1793 sout_stream_id_t *id = p_sys->es[0];
1795 int64_t i_dts = p_buffer->i_dts;
1797 uint8_t *p_data = p_buffer->p_buffer;
1798 size_t i_data = p_buffer->i_buffer;
1799 size_t i_max = id->i_mtu - 12;
1801 size_t i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1807 /* output complete packet */
1808 if( p_sys->packet &&
1809 p_sys->packet->i_buffer + i_data > i_max )
1811 rtp_packetize_send( id, p_sys->packet );
1812 p_sys->packet = NULL;
1815 if( p_sys->packet == NULL )
1817 /* allocate a new packet */
1818 p_sys->packet = block_New( p_stream, id->i_mtu );
1819 rtp_packetize_common( id, p_sys->packet, 1, i_dts );
1820 p_sys->packet->i_dts = i_dts;
1821 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1822 i_dts += p_sys->packet->i_length;
1825 i_size = __MIN( i_data,
1826 (unsigned)(id->i_mtu - p_sys->packet->i_buffer) );
1828 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1831 p_sys->packet->i_buffer += i_size;
1840 static ssize_t AccessOutGrabberWrite( sout_access_out_t *p_access,
1843 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1849 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1851 p_next = p_buffer->p_next;
1852 block_Release( p_buffer );
1860 static sout_access_out_t *GrabberCreate( sout_stream_t *p_stream )
1862 sout_access_out_t *p_grab;
1864 p_grab = vlc_object_create( p_stream->p_sout, sizeof( *p_grab ) );
1865 if( p_grab == NULL )
1868 p_grab->p_module = NULL;
1869 p_grab->psz_access = strdup( "grab" );
1870 p_grab->p_cfg = NULL;
1871 p_grab->psz_path = strdup( "" );
1872 p_grab->p_sys = (sout_access_out_sys_t *)p_stream;
1873 p_grab->pf_seek = NULL;
1874 p_grab->pf_write = AccessOutGrabberWrite;
1875 vlc_object_attach( p_grab, p_stream );