1 /*****************************************************************************
2 * rtp.c: rtp stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
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 *****************************************************************************/
36 #include <vlc_block.h>
38 #include <vlc_httpd.h>
40 #include <vlc_network.h>
41 #include <vlc_charset.h>
42 #include <vlc_strings.h>
46 /*****************************************************************************
48 *****************************************************************************/
52 #define DST_TEXT N_("Destination")
53 #define DST_LONGTEXT N_( \
54 "This is the output URL that will be used." )
55 #define SDP_TEXT N_("SDP")
56 #define SDP_LONGTEXT N_( \
57 "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
58 "session will be made available. You must use an url: http://location to " \
59 "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
60 "for the SDP to be announced via SAP." )
61 #define MUX_TEXT N_("Muxer")
62 #define MUX_LONGTEXT N_( \
63 "This allows you to specify the muxer used for the streaming output. " \
64 "Default is to use no muxer (standard RTP stream)." )
66 #define NAME_TEXT N_("Session name")
67 #define NAME_LONGTEXT N_( \
68 "This is the name of the session that will be announced in the SDP " \
69 "(Session Descriptor)." )
70 #define DESC_TEXT N_("Session description")
71 #define DESC_LONGTEXT N_( \
72 "This allows you to give a broader description of the stream, that will " \
73 "be announced in the SDP (Session Descriptor)." )
74 #define URL_TEXT N_("Session URL")
75 #define URL_LONGTEXT N_( \
76 "This allows you to give an URL with more details about the stream " \
77 "(often the website of the streaming organization), that will " \
78 "be announced in the SDP (Session Descriptor)." )
79 #define EMAIL_TEXT N_("Session email")
80 #define EMAIL_LONGTEXT N_( \
81 "This allows you to give a contact mail address for the stream, that will " \
82 "be announced in the SDP (Session Descriptor)." )
83 #define PORT_TEXT N_("Port")
84 #define PORT_LONGTEXT N_( \
85 "This allows you to specify the base port for the RTP streaming." )
86 #define PORT_AUDIO_TEXT N_("Audio port")
87 #define PORT_AUDIO_LONGTEXT N_( \
88 "This allows you to specify the default audio port for the RTP streaming." )
89 #define PORT_VIDEO_TEXT N_("Video port")
90 #define PORT_VIDEO_LONGTEXT N_( \
91 "This allows you to specify the default video port for the RTP streaming." )
93 #define TTL_TEXT N_("Hop limit (TTL)")
94 #define TTL_LONGTEXT N_( \
95 "This is the hop limit (also known as \"Time-To-Live\" or TTL) of " \
96 "the multicast packets sent by the stream output (0 = use operating " \
97 "system built-in default).")
99 #define RFC3016_TEXT N_("MP4A LATM")
100 #define RFC3016_LONGTEXT N_( \
101 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
103 static int Open ( vlc_object_t * );
104 static void Close( vlc_object_t * );
106 #define SOUT_CFG_PREFIX "sout-rtp-"
109 set_shortname( _("RTP"));
110 set_description( _("RTP stream output") );
111 set_capability( "sout stream", 0 );
112 add_shortcut( "rtp" );
113 set_category( CAT_SOUT );
114 set_subcategory( SUBCAT_SOUT_STREAM );
116 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
117 DST_LONGTEXT, VLC_TRUE );
118 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
119 SDP_LONGTEXT, VLC_TRUE );
120 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
121 MUX_LONGTEXT, VLC_TRUE );
123 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
124 NAME_LONGTEXT, VLC_TRUE );
125 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
126 DESC_LONGTEXT, VLC_TRUE );
127 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
128 URL_LONGTEXT, VLC_TRUE );
129 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
130 EMAIL_LONGTEXT, VLC_TRUE );
132 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
133 PORT_LONGTEXT, VLC_TRUE );
134 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
135 PORT_AUDIO_LONGTEXT, VLC_TRUE );
136 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
137 PORT_VIDEO_LONGTEXT, VLC_TRUE );
139 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
140 TTL_LONGTEXT, VLC_TRUE );
142 add_bool( SOUT_CFG_PREFIX "mp4a-latm", 0, NULL, RFC3016_TEXT,
143 RFC3016_LONGTEXT, VLC_FALSE );
145 set_callbacks( Open, Close );
148 /*****************************************************************************
149 * Exported prototypes
150 *****************************************************************************/
151 static const char *ppsz_sout_options[] = {
152 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
153 "description", "url","email", "mp4a-latm", NULL
156 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
157 static int Del ( sout_stream_t *, sout_stream_id_t * );
158 static int Send( sout_stream_t *, sout_stream_id_t *,
161 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
163 static void SDPHandleUrl( sout_stream_t *, char * );
165 static int SapSetup( sout_stream_t *p_stream );
166 static int FileSetup( sout_stream_t *p_stream );
167 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
169 struct sout_stream_sys_t
175 vlc_mutex_t lock_sdp;
177 char *psz_session_name;
178 char *psz_session_description;
179 char *psz_session_url;
180 char *psz_session_email;
183 vlc_bool_t b_export_sdp_file;
186 vlc_bool_t b_export_sap;
187 session_descriptor_t *p_session;
189 httpd_host_t *p_httpd_host;
190 httpd_file_t *p_httpd_file;
195 char *psz_destination;
202 /* when need to use a private one or when using muxer */
205 /* in case we do TS/PS over rtp */
207 sout_access_out_t *p_access;
209 sout_access_out_t *p_grab;
211 uint32_t i_timestamp_start;
218 sout_stream_id_t **es;
221 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
224 struct sout_stream_id_t
226 sout_stream_t *p_stream;
228 uint8_t i_payload_type;
230 uint32_t i_timestamp_start;
241 /* Packetizer specific fields */
242 pf_rtp_packetizer_t pf_packetize;
245 /* for sending the packets */
246 sout_access_out_t *p_access;
248 vlc_mutex_t lock_sink;
250 sout_access_out_t **sink;
251 rtsp_stream_id_t *rtsp_id;
254 sout_input_t *p_input;
258 /*****************************************************************************
260 *****************************************************************************/
261 static int Open( vlc_object_t *p_this )
263 sout_stream_t *p_stream = (sout_stream_t*)p_this;
264 sout_instance_t *p_sout = p_stream->p_sout;
265 sout_stream_sys_t *p_sys = NULL;
266 config_chain_t *p_cfg = NULL;
268 vlc_bool_t b_rtsp = VLC_FALSE;
270 config_ChainParse( p_stream, SOUT_CFG_PREFIX,
271 ppsz_sout_options, p_stream->p_cfg );
273 p_sys = malloc( sizeof( sout_stream_sys_t ) );
277 p_sys->psz_destination = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
278 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
279 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
280 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
281 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
283 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
284 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
285 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
287 p_sys->psz_sdp_file = NULL;
289 if( p_sys->i_port_audio == p_sys->i_port_video )
291 msg_Err( p_stream, "audio and video port cannot be the same" );
292 p_sys->i_port_audio = 0;
293 p_sys->i_port_video = 0;
296 if( !p_sys->psz_session_name )
298 if( p_sys->psz_destination )
299 p_sys->psz_session_name = strdup( p_sys->psz_destination );
301 p_sys->psz_session_name = strdup( "NONE" );
304 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
306 if( !strcmp( p_cfg->psz_name, "sdp" ) )
308 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
318 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
319 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
321 free( val2.psz_string );
324 if( p_sys->psz_destination == NULL )
328 msg_Err( p_stream, "missing destination and not in rtsp mode" );
333 else if( p_sys->i_port <= 0 )
335 msg_Err( p_stream, "invalid port" );
340 p_sys->i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
341 if( p_sys->i_ttl == 0 )
343 /* Normally, we should let the default hop limit up to the core,
344 * but we have to know it to build our SDP properly, which is why
345 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
347 p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
349 if( p_sys->i_ttl > 255 )
351 /* must not exceed 999 once formatted */
353 p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
355 p_sys->i_payload_type = 96;
359 p_sys->psz_sdp = NULL;
361 p_sys->i_sdp_id = mdate();
362 p_sys->i_sdp_version = 1;
363 p_sys->psz_sdp = NULL;
365 p_sys->b_export_sap = VLC_FALSE;
366 p_sys->b_export_sdp_file = VLC_FALSE;
367 p_sys->p_session = NULL;
369 p_sys->p_httpd_host = NULL;
370 p_sys->p_httpd_file = NULL;
372 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
373 vlc_mutex_init( p_stream, &p_sys->lock_es );
375 p_stream->pf_add = Add;
376 p_stream->pf_del = Del;
377 p_stream->pf_send = Send;
379 p_stream->p_sys = p_sys;
381 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
382 if( *val.psz_string )
384 sout_access_out_t *p_grab;
385 const char *psz_rtpmap;
386 char url[NI_MAXHOST + 8], access[22], psz_ttl[5], ipv;
390 msg_Err( p_stream, "muxing is not supported in RTSP mode" );
394 else if( p_sys->psz_destination == NULL )
396 msg_Err( p_stream, "RTP needs a destination when muxing" );
401 /* Check muxer type */
402 if( !strncasecmp( val.psz_string, "ps", 2 )
403 || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
405 psz_rtpmap = "MP2P/90000";
407 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
409 psz_rtpmap = "MP2T/90000";
410 p_sys->i_payload_type = 33;
414 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
419 /* create the access out */
420 if( p_sys->i_ttl > 0 )
422 sprintf( access, "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
426 strcat( access, "udp{raw,rtcp}" );
429 /* FIXME: we should check that url is a numerical address, otherwise
430 * the SDP will be quite broken (regardless of the IP protocol version)
431 * Also it might be IPv6 with no ':' if it is a DNS name.
433 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
435 /* IPv6 needs brackets if not already present */
436 if ((ipv == '6') && (p_sys->psz_destination[0] != '['))
437 snprintf (url, sizeof (url), "[%s]:%d",p_sys->psz_destination,
440 snprintf (url, sizeof (url), "%s:%d",p_sys->psz_destination,
443 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
445 msg_Err( p_stream, "cannot create the access out for %s://%s",
450 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
451 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
453 /* better than nothing */
456 p_sys->i_mtu -= MTU_REDUCE;
458 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
459 p_grab = p_sys->p_grab =
460 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
461 p_grab->p_module = NULL;
462 p_grab->p_sout = p_sout;
463 p_grab->psz_access = strdup( "grab" );
464 p_grab->p_cfg = NULL;
465 p_grab->psz_path = strdup( "" );
466 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
467 p_grab->pf_seek = NULL;
468 p_grab->pf_write = AccessOutGrabberWrite;
471 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
473 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
474 sout_AccessOutDelete( p_sys->p_grab );
475 sout_AccessOutDelete( p_sys->p_access );
480 /* create the SDP for a muxed stream (only once) */
481 /* FIXME http://www.faqs.org/rfcs/rfc4566.html
482 o= - should be local username (no spaces allowed)
483 o= time should be hashed with some other value to garantee uniqueness
484 o= don't use the localhost address. use fully qualified domain name or IP4 address
485 a= source-filter: we need our source address
486 a= x-plgroup: (missing)
487 RTP packets need to get the correct src IP address */
489 && net_AddressIsMulticast( VLC_OBJECT(p_stream), p_sys->psz_destination ) )
491 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
492 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
499 asprintf( &p_sys->psz_sdp,
501 /* FIXME: source address not known :( */
502 "o=- "I64Fu" %d IN IP%c %s\r\n"
508 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
509 "a=tool:"PACKAGE_STRING"\r\n"
511 "a=type:broadcast\r\n"
512 "m=video %d RTP/AVP %d\r\n"
513 "a=rtpmap:%d %s\r\n",
514 p_sys->i_sdp_id, p_sys->i_sdp_version,
515 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
516 p_sys->psz_session_name,
517 p_sys->psz_session_description,
518 p_sys->psz_session_url,
519 p_sys->psz_session_email,
520 ipv, p_sys->psz_destination, psz_ttl,
521 p_sys->i_port, p_sys->i_payload_type,
522 p_sys->i_payload_type, psz_rtpmap );
523 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
525 /* create the rtp context */
526 p_sys->ssrc[0] = rand()&0xff;
527 p_sys->ssrc[1] = rand()&0xff;
528 p_sys->ssrc[2] = rand()&0xff;
529 p_sys->ssrc[3] = rand()&0xff;
530 p_sys->i_sequence = rand()&0xffff;
531 p_sys->i_timestamp_start = rand()&0xffffffff;
532 p_sys->packet = NULL;
537 p_sys->p_access = NULL;
538 p_sys->p_grab = NULL;
540 free( val.psz_string );
543 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
544 if( *val.psz_string )
546 config_chain_t *p_cfg;
548 SDPHandleUrl( p_stream, val.psz_string );
550 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
552 if( !strcmp( p_cfg->psz_name, "sdp" ) )
554 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
557 /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
558 if( !strcmp( p_cfg->psz_value, val.psz_string ) )
561 SDPHandleUrl( p_stream, p_cfg->psz_value );
565 free( val.psz_string );
567 /* update p_sout->i_out_pace_nocontrol */
568 p_stream->p_sout->i_out_pace_nocontrol++;
573 /*****************************************************************************
575 *****************************************************************************/
576 static void Close( vlc_object_t * p_this )
578 sout_stream_t *p_stream = (sout_stream_t*)p_this;
579 sout_stream_sys_t *p_sys = p_stream->p_sys;
581 /* update p_sout->i_out_pace_nocontrol */
582 p_stream->p_sout->i_out_pace_nocontrol--;
586 sout_MuxDelete( p_sys->p_mux );
587 sout_AccessOutDelete( p_sys->p_access );
588 sout_AccessOutDelete( p_sys->p_grab );
591 block_Release( p_sys->packet );
593 if( p_sys->b_export_sap )
596 SapSetup( p_stream );
600 if( p_sys->rtsp != NULL )
601 RtspUnsetup( p_sys->rtsp );
603 vlc_mutex_destroy( &p_sys->lock_sdp );
605 if( p_sys->p_httpd_file )
606 httpd_FileDelete( p_sys->p_httpd_file );
608 if( p_sys->p_httpd_host )
609 httpd_HostDelete( p_sys->p_httpd_host );
611 if( p_sys->psz_session_name )
612 free( p_sys->psz_session_name );
614 if( p_sys->psz_session_description )
615 free( p_sys->psz_session_description );
617 if( p_sys->psz_session_url )
618 free( p_sys->psz_session_url );
620 if( p_sys->psz_session_email )
621 free( p_sys->psz_session_email );
624 free( p_sys->psz_sdp );
626 if( p_sys->b_export_sdp_file )
629 unlink( p_sys->psz_sdp_file );
631 free( p_sys->psz_sdp_file );
633 free( p_sys->psz_destination );
637 /*****************************************************************************
639 *****************************************************************************/
640 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
642 sout_stream_sys_t *p_sys = p_stream->p_sys;
645 vlc_UrlParse( &url, psz_url, 0 );
646 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
648 if( p_sys->p_httpd_file )
650 msg_Err( p_stream, "you can use sdp=http:// only once" );
654 if( HttpSetup( p_stream, &url ) )
656 msg_Err( p_stream, "cannot export SDP as HTTP" );
659 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
661 if( p_sys->rtsp != NULL )
663 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
667 /* FIXME test if destination is multicast or no destination at all FIXME */
668 p_sys->rtsp = RtspSetup( p_stream, &url );
669 if( p_sys->rtsp == NULL )
671 msg_Err( p_stream, "cannot export SDP as RTSP" );
674 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
675 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
677 p_sys->b_export_sap = VLC_TRUE;
678 SapSetup( p_stream );
680 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
682 if( p_sys->b_export_sdp_file )
684 msg_Err( p_stream, "you can use sdp=file:// only once" );
687 p_sys->b_export_sdp_file = VLC_TRUE;
688 psz_url = &psz_url[5];
689 if( psz_url[0] == '/' && psz_url[1] == '/' )
691 p_sys->psz_sdp_file = strdup( psz_url );
695 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
700 vlc_UrlClean( &url );
703 /*****************************************************************************
705 *****************************************************************************/
706 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
707 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
708 o= - should be local username (no spaces allowed)
709 o= time should be hashed with some other value to garantue uniqueness
710 o= we need IP6 support?
711 o= don't use the localhost address. use fully qualified domain name or IP4 address
712 p= international phone number (pass via vars?)
714 a= recvonly (missing)
715 a= type:broadcast (missing)
716 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
717 a= x-plgroup: (missing)
718 RTP packets need to get the correct src IP address */
720 char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
722 const sout_stream_sys_t *p_sys = p_stream->p_sys;
724 const char *psz_destination = p_sys->psz_destination;
725 char *psz_sdp, *p, ipv;
728 /* FIXME: breaks IP version check on unknown destination */
729 if( psz_destination == NULL )
730 psz_destination = "0.0.0.0";
732 i_size = sizeof( "v=0\r\n" ) +
733 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
734 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
735 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
736 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
737 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
738 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
739 sizeof( "b=RR:0\r\n" ) +
740 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
741 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
742 strlen( psz_destination ) ;
743 for( i = 0; i < p_sys->i_es; i++ )
745 sout_stream_id_t *id = p_sys->es[i];
747 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
750 i_size += strlen( "b=AS: *\r\n") + 10;
754 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
758 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
760 if( rtsp_url != NULL )
762 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( rtsp_url ) + 10;
767 i_size += strlen( "m=video %d RTP/AVP %d\r\n" ) +10 +10;
770 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
772 p = psz_sdp = malloc( i_size );
773 p += sprintf( p, "v=0\r\n" );
774 p += sprintf( p, "o=- "I64Fu" %d IN IP%c %s\r\n",
775 p_sys->i_sdp_id, p_sys->i_sdp_version,
776 ipv, ipv == '6' ? "::1" : "127.0.0.1" );
777 if( *p_sys->psz_session_name )
778 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
779 if( *p_sys->psz_session_description )
780 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
781 if( *p_sys->psz_session_url )
782 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
783 if( *p_sys->psz_session_email )
784 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
786 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
787 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
789 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
792 && net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
794 /* Add the deprecated TTL field if it is an IPv4 multicast address */
795 p += sprintf( p, "/%d", p_sys->i_ttl ?: 1 );
797 p += sprintf( p, "\r\n" );
798 p += sprintf( p, "b=RR:0\r\n" );
800 for( i = 0; i < p_sys->i_es; i++ )
802 sout_stream_id_t *id = p_sys->es[i];
804 if( id->i_cat == AUDIO_ES )
806 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
807 id->i_port, id->i_payload_type );
809 else if( id->i_cat == VIDEO_ES )
811 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
812 id->i_port, id->i_payload_type );
820 p += sprintf(p,"b=AS:%d\r\n",id->i_bitrate);
824 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
829 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
832 if( rtsp_url != NULL )
834 p += sprintf( p, "a=control:/trackID=%d\r\n", i );
839 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
840 p_sys->i_port, p_sys->i_payload_type );
846 /*****************************************************************************
848 *****************************************************************************/
849 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
850 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
851 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
852 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
853 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
854 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
855 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
856 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
857 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
858 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
859 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
861 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
863 static const char hex[16] = "0123456789abcdef";
866 for( i = 0; i < i_data; i++ )
868 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
869 s[2*i+1] = hex[(p_data[i] )&0xf];
874 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
876 sout_instance_t *p_sout = p_stream->p_sout;
877 sout_stream_sys_t *p_sys = p_stream->p_sys;
878 sout_stream_id_t *id;
879 sout_access_out_t *p_access = NULL;
883 if( p_sys->p_mux != NULL )
885 sout_input_t *p_input = NULL;
886 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
888 msg_Err( p_stream, "cannot add this stream to the muxer" );
892 id = malloc( sizeof( sout_stream_id_t ) );
893 memset( id, 0, sizeof( sout_stream_id_t ) );
895 id->p_input = p_input;
896 id->pf_packetize= NULL;
903 /* Choose the port */
905 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
907 i_port = p_sys->i_port_audio;
908 p_sys->i_port_audio = 0;
910 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
912 i_port = p_sys->i_port_video;
913 p_sys->i_port_video = 0;
917 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
919 i_port = p_sys->i_port;
926 if( p_sys->psz_destination )
929 char url[NI_MAXHOST + 8];
931 /* first try to create the access out */
932 if( p_sys->i_ttl > 0 )
934 snprintf( access, sizeof( access ), "udp{raw,rtcp,ttl=%d}",
936 access[sizeof( access ) - 1] = '\0';
939 strcpy( access, "udp{raw,rtcp}" );
941 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
942 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
943 p_sys->psz_destination, i_port );
944 url[sizeof( url ) - 1] = '\0';
946 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
948 msg_Err( p_stream, "cannot create the access out for %s://%s",
952 msg_Dbg( p_stream, "access out %s:%s", access, url );
955 /* not create the rtp specific stuff */
956 id = malloc( sizeof( sout_stream_id_t ) );
957 memset( id, 0, sizeof( sout_stream_id_t ) );
958 id->p_stream = p_stream;
959 id->p_access = p_access;
961 id->psz_rtpmap = NULL;
966 vlc_mutex_init( p_stream, &id->lock_sink );
970 switch( p_fmt->i_codec )
972 case VLC_FOURCC( 's', '1', '6', 'b' ):
973 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
975 id->i_payload_type = 11;
977 else if( p_fmt->audio.i_channels == 2 &&
978 p_fmt->audio.i_rate == 44100 )
980 id->i_payload_type = 10;
984 id->i_payload_type = p_sys->i_payload_type++;
986 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
987 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
988 p_fmt->audio.i_channels );
989 id->i_clock_rate = p_fmt->audio.i_rate;
990 id->pf_packetize = rtp_packetize_l16;
992 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
993 id->i_payload_type = p_sys->i_payload_type++;
994 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
995 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
996 p_fmt->audio.i_channels );
997 id->i_clock_rate = p_fmt->audio.i_rate;
998 id->pf_packetize = rtp_packetize_l8;
1000 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1001 id->i_payload_type = 14;
1002 id->i_clock_rate = 90000;
1003 id->psz_rtpmap = strdup( "MPA/90000" );
1004 id->pf_packetize = rtp_packetize_mpa;
1006 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1007 id->i_payload_type = 32;
1008 id->i_clock_rate = 90000;
1009 id->psz_rtpmap = strdup( "MPV/90000" );
1010 id->pf_packetize = rtp_packetize_mpv;
1012 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1013 id->i_payload_type = p_sys->i_payload_type++;
1014 id->i_clock_rate = 90000;
1015 id->psz_rtpmap = strdup( "ac3/90000" );
1016 id->pf_packetize = rtp_packetize_ac3;
1018 case VLC_FOURCC( 'H', '2', '6', '3' ):
1019 id->i_payload_type = p_sys->i_payload_type++;
1020 id->i_clock_rate = 90000;
1021 id->psz_rtpmap = strdup( "H263-1998/90000" );
1022 id->pf_packetize = rtp_packetize_h263;
1024 case VLC_FOURCC( 'h', '2', '6', '4' ):
1025 id->i_payload_type = p_sys->i_payload_type++;
1026 id->i_clock_rate = 90000;
1027 id->psz_rtpmap = strdup( "H264/90000" );
1028 id->pf_packetize = rtp_packetize_h264;
1029 id->psz_fmtp = NULL;
1031 if( p_fmt->i_extra > 0 )
1033 uint8_t *p_buffer = p_fmt->p_extra;
1034 int i_buffer = p_fmt->i_extra;
1035 char *p_64_sps = NULL;
1036 char *p_64_pps = NULL;
1039 while( i_buffer > 4 &&
1040 p_buffer[0] == 0 && p_buffer[1] == 0 &&
1041 p_buffer[2] == 0 && p_buffer[3] == 1 )
1043 const int i_nal_type = p_buffer[4]&0x1f;
1047 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
1050 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
1052 if( !memcmp (p_buffer + i_offset, "\x00\x00\x00\x01", 4 ) )
1054 /* we found another startcode */
1059 if( i_nal_type == 7 )
1061 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1062 sprintf_hexa( hexa, &p_buffer[5], 3 );
1064 else if( i_nal_type == 8 )
1066 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1072 if( p_64_sps && p_64_pps )
1073 asprintf( &id->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
1080 id->psz_fmtp = strdup( "packetization-mode=1" );
1083 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1085 char hexa[2*p_fmt->i_extra +1];
1087 id->i_payload_type = p_sys->i_payload_type++;
1088 id->i_clock_rate = 90000;
1089 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1090 id->pf_packetize = rtp_packetize_split;
1091 if( p_fmt->i_extra > 0 )
1093 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1094 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1095 sprintf( id->psz_fmtp,
1096 "profile-level-id=3; config=%s;", hexa );
1100 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1102 id->i_payload_type = p_sys->i_payload_type++;
1103 id->i_clock_rate = p_fmt->audio.i_rate;
1107 char hexa[2*p_fmt->i_extra +1];
1109 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1110 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1111 id->pf_packetize = rtp_packetize_mp4a;
1112 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1113 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1114 sprintf( id->psz_fmtp,
1115 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1116 "config=%s; SizeLength=13;IndexLength=3; "
1117 "IndexDeltaLength=3; Profile=1;", hexa );
1123 unsigned char config[6];
1124 unsigned int aacsrates[15] = {
1125 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1126 16000, 12000, 11025, 8000, 7350, 0, 0 };
1128 for( i = 0; i < 15; i++ )
1129 if( p_fmt->audio.i_rate == aacsrates[i] )
1135 config[3]=p_fmt->audio.i_channels<<4;
1139 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1140 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1141 id->pf_packetize = rtp_packetize_mp4a_latm;
1142 sprintf_hexa( hexa, config, 6 );
1143 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1144 "object=2; cpresent=0; config=%s", hexa );
1148 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1149 id->i_payload_type = p_sys->i_payload_type++;
1150 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1151 "AMR/8000/2" : "AMR/8000" );
1152 id->psz_fmtp = strdup( "octet-align=1" );
1153 id->i_clock_rate = p_fmt->audio.i_rate;
1154 id->pf_packetize = rtp_packetize_amr;
1156 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1157 id->i_payload_type = p_sys->i_payload_type++;
1158 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1159 "AMR-WB/16000/2" : "AMR-WB/16000" );
1160 id->psz_fmtp = strdup( "octet-align=1" );
1161 id->i_clock_rate = p_fmt->audio.i_rate;
1162 id->pf_packetize = rtp_packetize_amr;
1166 msg_Err( p_stream, "cannot add this stream (unsupported "
1167 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1170 sout_AccessOutDelete( p_access );
1175 id->i_cat = p_fmt->i_cat;
1177 id->ssrc[0] = rand()&0xff;
1178 id->ssrc[1] = rand()&0xff;
1179 id->ssrc[2] = rand()&0xff;
1180 id->ssrc[3] = rand()&0xff;
1181 id->i_sequence = rand()&0xffff;
1182 id->i_timestamp_start = rand()&0xffffffff;
1183 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
1185 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1186 if( id->i_mtu <= 16 + MTU_REDUCE )
1188 /* better than nothing */
1191 id->i_mtu -= MTU_REDUCE;
1192 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1194 if( p_sys->rtsp != NULL )
1195 id->rtsp_id = RtspAddId( p_sys->rtsp, id, p_sys->i_es,
1196 p_sys->psz_destination,
1197 p_sys->i_ttl, id->i_port, id->i_port + 1 );
1199 /* Update p_sys context */
1200 vlc_mutex_lock( &p_sys->lock_es );
1201 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1202 vlc_mutex_unlock( &p_sys->lock_es );
1204 psz_sdp = SDPGenerate( p_stream, NULL );
1206 vlc_mutex_lock( &p_sys->lock_sdp );
1207 free( p_sys->psz_sdp );
1208 p_sys->psz_sdp = psz_sdp;
1209 vlc_mutex_unlock( &p_sys->lock_sdp );
1211 p_sys->i_sdp_version++;
1213 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
1215 /* Update SDP (sap/file) */
1216 if( p_sys->b_export_sap ) SapSetup( p_stream );
1217 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1222 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1224 sout_stream_sys_t *p_sys = p_stream->p_sys;
1226 vlc_mutex_lock( &p_sys->lock_es );
1227 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1228 vlc_mutex_unlock( &p_sys->lock_es );
1231 if( id->i_port > 0 )
1233 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1234 p_sys->i_port_audio = id->i_port;
1235 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1236 p_sys->i_port_video = id->i_port;
1241 if( id->psz_rtpmap )
1243 free( id->psz_rtpmap );
1247 free( id->psz_fmtp );
1249 sout_AccessOutDelete( id->p_access );
1251 else if( id->p_input )
1253 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1256 RtspDelId( p_sys->rtsp, id->rtsp_id );
1258 vlc_mutex_destroy( &id->lock_sink );
1261 /* Update SDP (sap/file) */
1262 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1263 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1269 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1274 if( p_stream->p_sys->p_mux )
1276 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1282 p_next = p_buffer->p_next;
1283 if( id->pf_packetize( p_stream, id, p_buffer ) )
1287 block_Release( p_buffer );
1295 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1298 sout_stream_sys_t *p_sys = p_stream->p_sys;
1300 int64_t i_dts = p_buffer->i_dts;
1301 uint32_t i_timestamp = i_dts * 9 / 100;
1303 uint8_t *p_data = p_buffer->p_buffer;
1304 unsigned int i_data = p_buffer->i_buffer;
1305 unsigned int i_max = p_sys->i_mtu - 12;
1307 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1311 unsigned int i_size;
1313 /* output complete packet */
1314 if( p_sys->packet &&
1315 p_sys->packet->i_buffer + i_data > i_max )
1317 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1318 p_sys->packet = NULL;
1321 if( p_sys->packet == NULL )
1323 /* allocate a new packet */
1324 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1325 p_sys->packet->p_buffer[ 0] = 0x80;
1326 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1327 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1328 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1329 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1330 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1331 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1332 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1333 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1334 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1335 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1336 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1337 p_sys->packet->i_buffer = 12;
1339 p_sys->packet->i_dts = i_dts;
1340 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1341 i_dts += p_sys->packet->i_length;
1343 p_sys->i_sequence++;
1346 i_size = __MIN( i_data,
1347 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1349 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1352 p_sys->packet->i_buffer += i_size;
1360 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1363 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1365 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1371 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1373 p_next = p_buffer->p_next;
1374 block_Release( p_buffer );
1381 /****************************************************************************
1383 ****************************************************************************/
1384 static int SapSetup( sout_stream_t *p_stream )
1386 sout_stream_sys_t *p_sys = p_stream->p_sys;
1387 sout_instance_t *p_sout = p_stream->p_sout;
1388 announce_method_t *p_method = sout_SAPMethod();
1390 /* Remove the previous session */
1391 if( p_sys->p_session != NULL)
1393 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1394 sout_AnnounceSessionDestroy( p_sys->p_session );
1395 p_sys->p_session = NULL;
1398 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1400 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
1402 p_sys->psz_destination,
1406 sout_MethodRelease( p_method );
1410 /****************************************************************************
1412 ****************************************************************************/
1413 static int FileSetup( sout_stream_t *p_stream )
1415 sout_stream_sys_t *p_sys = p_stream->p_sys;
1418 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1420 msg_Err( p_stream, "cannot open file '%s' (%s)",
1421 p_sys->psz_sdp_file, strerror(errno) );
1422 return VLC_EGENERIC;
1425 fprintf( f, "%s", p_sys->psz_sdp );
1431 /****************************************************************************
1433 ****************************************************************************/
1434 static int HttpCallback( httpd_file_sys_t *p_args,
1435 httpd_file_t *, uint8_t *p_request,
1436 uint8_t **pp_data, int *pi_data );
1438 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1440 sout_stream_sys_t *p_sys = p_stream->p_sys;
1442 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1443 if( p_sys->p_httpd_host )
1445 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1446 url->psz_path ? url->psz_path : "/",
1449 HttpCallback, (void*)p_sys );
1451 if( p_sys->p_httpd_file == NULL )
1453 return VLC_EGENERIC;
1458 static int HttpCallback( httpd_file_sys_t *p_args,
1459 httpd_file_t *f, uint8_t *p_request,
1460 uint8_t **pp_data, int *pi_data )
1462 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1464 vlc_mutex_lock( &p_sys->lock_sdp );
1465 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1467 *pi_data = strlen( p_sys->psz_sdp );
1468 *pp_data = malloc( *pi_data );
1469 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1476 vlc_mutex_unlock( &p_sys->lock_sdp );
1481 /****************************************************************************
1483 ****************************************************************************/
1484 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1485 int b_marker, int64_t i_pts )
1487 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1489 out->p_buffer[0] = 0x80;
1490 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1491 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1492 out->p_buffer[3] = ( id->i_sequence )&0xff;
1493 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1494 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1495 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1496 out->p_buffer[7] = ( i_timestamp )&0xff;
1498 out->p_buffer[ 8] = id->ssrc[0];
1499 out->p_buffer[ 9] = id->ssrc[1];
1500 out->p_buffer[10] = id->ssrc[2];
1501 out->p_buffer[11] = id->ssrc[3];
1507 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1510 vlc_mutex_lock( &id->lock_sink );
1511 for( i = 0; i < id->i_sink; i++ )
1513 sout_AccessOutWrite( id->sink[i], block_Duplicate( out ) );
1515 vlc_mutex_unlock( &id->lock_sink );
1519 sout_AccessOutWrite( id->p_access, out );
1523 block_Release( out );
1527 int rtp_add_sink( sout_stream_id_t *id, sout_access_out_t *access )
1529 vlc_mutex_lock( &id->lock_sink );
1530 TAB_APPEND( id->i_sink, id->sink, access );
1531 vlc_mutex_unlock( &id->lock_sink );
1535 void rtp_del_sink( sout_stream_id_t *id, sout_access_out_t *access )
1537 /* NOTE: must be safe to use if access is not a sink to id */
1538 vlc_mutex_lock( &id->lock_sink );
1539 TAB_REMOVE( id->i_sink, id->sink, access );
1540 vlc_mutex_unlock( &id->lock_sink );
1543 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1546 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1547 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1549 uint8_t *p_data = in->p_buffer;
1550 int i_data = in->i_buffer;
1553 for( i = 0; i < i_count; i++ )
1555 int i_payload = __MIN( i_max, i_data );
1556 block_t *out = block_New( p_stream, 16 + i_payload );
1558 /* rtp common header */
1559 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1561 out->p_buffer[12] = 0;
1562 out->p_buffer[13] = 0;
1563 /* fragment offset in the current frame */
1564 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1565 out->p_buffer[15] = ( (i*i_max) )&0xff;
1566 memcpy( &out->p_buffer[16], p_data, i_payload );
1568 out->i_buffer = 16 + i_payload;
1569 out->i_dts = in->i_dts + i * in->i_length / i_count;
1570 out->i_length = in->i_length / i_count;
1572 rtp_packetize_send( id, out );
1574 p_data += i_payload;
1575 i_data -= i_payload;
1582 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1585 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1586 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1588 uint8_t *p_data = in->p_buffer;
1589 int i_data = in->i_buffer;
1591 int b_sequence_start = 0;
1592 int i_temporal_ref = 0;
1593 int i_picture_coding_type = 0;
1594 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1595 int b_start_slice = 0;
1597 /* preparse this packet to get some info */
1598 if( in->i_buffer > 4 )
1600 uint8_t *p = p_data;
1601 int i_rest = in->i_buffer;
1605 while( i_rest > 4 &&
1606 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1620 /* sequence start code */
1621 b_sequence_start = 1;
1623 else if( *p == 0x00 && i_rest >= 4 )
1626 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1627 i_picture_coding_type = (p[2] >> 3)&0x07;
1629 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1630 i_picture_coding_type == 3 ) )
1632 i_ffv = (p[3] >> 2)&0x01;
1633 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1634 if( i_rest > 4 && i_picture_coding_type == 3 )
1636 i_fbv = (p[4]>>6)&0x01;
1637 i_bfc = (p[4]>>3)&0x07;
1641 else if( *p <= 0xaf )
1648 for( i = 0; i < i_count; i++ )
1650 int i_payload = __MIN( i_max, i_data );
1651 block_t *out = block_New( p_stream,
1653 uint32_t h = ( i_temporal_ref << 16 )|
1654 ( b_sequence_start << 13 )|
1655 ( b_start_slice << 12 )|
1656 ( i == i_count - 1 ? 1 << 11 : 0 )|
1657 ( i_picture_coding_type << 8 )|
1658 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1660 /* rtp common header */
1661 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1662 in->i_pts > 0 ? in->i_pts : in->i_dts );
1664 /* MBZ:5 T:1 TR:10 AN:1 N:1 S:1 B:1 E:1 P:3 FBV:1 BFC:3 FFV:1 FFC:3 */
1665 out->p_buffer[12] = ( h >> 24 )&0xff;
1666 out->p_buffer[13] = ( h >> 16 )&0xff;
1667 out->p_buffer[14] = ( h >> 8 )&0xff;
1668 out->p_buffer[15] = ( h )&0xff;
1670 memcpy( &out->p_buffer[16], p_data, i_payload );
1672 out->i_buffer = 16 + i_payload;
1673 out->i_dts = in->i_dts + i * in->i_length / i_count;
1674 out->i_length = in->i_length / i_count;
1676 rtp_packetize_send( id, out );
1678 p_data += i_payload;
1679 i_data -= i_payload;
1685 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1688 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1689 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1691 uint8_t *p_data = in->p_buffer;
1692 int i_data = in->i_buffer;
1695 for( i = 0; i < i_count; i++ )
1697 int i_payload = __MIN( i_max, i_data );
1698 block_t *out = block_New( p_stream, 14 + i_payload );
1700 /* rtp common header */
1701 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1703 out->p_buffer[12] = 1;
1705 out->p_buffer[13] = 0x00;
1707 memcpy( &out->p_buffer[14], p_data, i_payload );
1709 out->i_buffer = 14 + i_payload;
1710 out->i_dts = in->i_dts + i * in->i_length / i_count;
1711 out->i_length = in->i_length / i_count;
1713 rtp_packetize_send( id, out );
1715 p_data += i_payload;
1716 i_data -= i_payload;
1722 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1725 int i_max = id->i_mtu - 12; /* payload max in one packet */
1726 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1728 uint8_t *p_data = in->p_buffer;
1729 int i_data = in->i_buffer;
1732 for( i = 0; i < i_count; i++ )
1734 int i_payload = __MIN( i_max, i_data );
1735 block_t *out = block_New( p_stream, 12 + i_payload );
1737 /* rtp common header */
1738 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1739 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1740 memcpy( &out->p_buffer[12], p_data, i_payload );
1742 out->i_buffer = 12 + i_payload;
1743 out->i_dts = in->i_dts + i * in->i_length / i_count;
1744 out->i_length = in->i_length / i_count;
1746 rtp_packetize_send( id, out );
1748 p_data += i_payload;
1749 i_data -= i_payload;
1756 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
1759 int i_max = id->i_mtu - 14; /* payload max in one packet */
1760 int latmhdrsize = in->i_buffer / 0xff + 1;
1761 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1763 uint8_t *p_data = in->p_buffer, *p_header = NULL;
1764 int i_data = in->i_buffer;
1767 for( i = 0; i < i_count; i++ )
1769 int i_payload = __MIN( i_max, i_data );
1774 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
1776 /* rtp common header */
1777 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
1778 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1782 int tmp = in->i_buffer;
1784 p_header=out->p_buffer+12;
1794 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
1796 out->i_buffer = 12 + latmhdrsize + i_payload;
1797 out->i_dts = in->i_dts + i * in->i_length / i_count;
1798 out->i_length = in->i_length / i_count;
1800 rtp_packetize_send( id, out );
1802 p_data += i_payload;
1803 i_data -= i_payload;
1809 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1812 int i_max = id->i_mtu - 12; /* payload max in one packet */
1813 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1815 uint8_t *p_data = in->p_buffer;
1816 int i_data = in->i_buffer;
1821 int i_payload = (__MIN( i_max, i_data )/4)*4;
1822 block_t *out = block_New( p_stream, 12 + i_payload );
1824 /* rtp common header */
1825 rtp_packetize_common( id, out, 0,
1826 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1827 memcpy( &out->p_buffer[12], p_data, i_payload );
1829 out->i_buffer = 12 + i_payload;
1830 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1831 out->i_length = in->i_length / i_count;
1833 rtp_packetize_send( id, out );
1835 p_data += i_payload;
1836 i_data -= i_payload;
1843 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1846 int i_max = id->i_mtu - 12; /* payload max in one packet */
1847 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1849 uint8_t *p_data = in->p_buffer;
1850 int i_data = in->i_buffer;
1855 int i_payload = (__MIN( i_max, i_data )/2)*2;
1856 block_t *out = block_New( p_stream, 12 + i_payload );
1858 /* rtp common header */
1859 rtp_packetize_common( id, out, 0,
1860 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1861 memcpy( &out->p_buffer[12], p_data, i_payload );
1863 out->i_buffer = 12 + i_payload;
1864 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1865 out->i_length = in->i_length / i_count;
1867 rtp_packetize_send( id, out );
1869 p_data += i_payload;
1870 i_data -= i_payload;
1876 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1879 int i_max = id->i_mtu - 16; /* payload max in one packet */
1880 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1882 uint8_t *p_data = in->p_buffer;
1883 int i_data = in->i_buffer;
1886 for( i = 0; i < i_count; i++ )
1888 int i_payload = __MIN( i_max, i_data );
1889 block_t *out = block_New( p_stream, 16 + i_payload );
1891 /* rtp common header */
1892 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1893 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1895 /* AU headers length (bits) */
1896 out->p_buffer[12] = 0;
1897 out->p_buffer[13] = 2*8;
1898 /* for each AU length 13 bits + idx 3bits, */
1899 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1900 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1902 memcpy( &out->p_buffer[16], p_data, i_payload );
1904 out->i_buffer = 16 + i_payload;
1905 out->i_dts = in->i_dts + i * in->i_length / i_count;
1906 out->i_length = in->i_length / i_count;
1908 rtp_packetize_send( id, out );
1910 p_data += i_payload;
1911 i_data -= i_payload;
1919 #define RTP_H263_HEADER_SIZE (2) // plen = 0
1920 #define RTP_H263_PAYLOAD_START (14) // plen = 0
1921 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
1924 uint8_t *p_data = in->p_buffer;
1925 int i_data = in->i_buffer;
1927 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
1930 int b_v_bit = 0; // no pesky error resilience
1931 int i_plen = 0; // normally plen=0 for PSC packet
1932 int i_pebit = 0; // because plen=0
1937 return VLC_EGENERIC;
1939 if( p_data[0] || p_data[1] )
1941 return VLC_EGENERIC;
1943 /* remove 2 leading 0 bytes */
1946 i_count = ( i_data + i_max - 1 ) / i_max;
1948 for( i = 0; i < i_count; i++ )
1950 int i_payload = __MIN( i_max, i_data );
1951 block_t *out = block_New( p_stream,
1952 RTP_H263_PAYLOAD_START + i_payload );
1953 b_p_bit = (i == 0) ? 1 : 0;
1954 h = ( b_p_bit << 10 )|
1959 /* rtp common header */
1960 //b_m_bit = 1; // always contains end of frame
1961 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1962 in->i_pts > 0 ? in->i_pts : in->i_dts );
1965 out->p_buffer[12] = ( h >> 8 )&0xff;
1966 out->p_buffer[13] = ( h )&0xff;
1967 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1969 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1970 out->i_dts = in->i_dts + i * in->i_length / i_count;
1971 out->i_length = in->i_length / i_count;
1973 rtp_packetize_send( id, out );
1975 p_data += i_payload;
1976 i_data -= i_payload;
1983 static int rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
1984 const uint8_t *p_data, int i_data, int64_t i_pts, int64_t i_dts, vlc_bool_t b_last, int64_t i_length )
1986 const int i_max = id->i_mtu - 12; /* payload max in one packet */
1993 i_nal_hdr = p_data[3];
1994 i_nal_type = i_nal_hdr&0x1f;
1995 if( i_nal_type == 7 || i_nal_type == 8 )
1997 /* XXX Why do you want to remove them ? It will break streaming with
1998 * SPS/PPS change (broadcast) ? */
2002 /* Skip start code */
2007 if( i_data <= i_max )
2009 /* Single NAL unit packet */
2010 block_t *out = block_New( p_stream, 12 + i_data );
2012 out->i_length = i_length;
2015 rtp_packetize_common( id, out, b_last, i_pts );
2016 out->i_buffer = 12 + i_data;
2018 memcpy( &out->p_buffer[12], p_data, i_data );
2020 rtp_packetize_send( id, out );
2024 /* FU-A Fragmentation Unit without interleaving */
2025 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
2031 for( i = 0; i < i_count; i++ )
2033 const int i_payload = __MIN( i_data, i_max-2 );
2034 block_t *out = block_New( p_stream, 12 + 2 + i_payload );
2035 out->i_dts = i_dts + i * i_length / i_count;
2036 out->i_length = i_length / i_count;
2039 rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
2040 out->i_buffer = 14 + i_payload;
2043 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
2045 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
2046 memcpy( &out->p_buffer[14], p_data, i_payload );
2048 rtp_packetize_send( id, out );
2050 i_data -= i_payload;
2051 p_data += i_payload;
2057 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
2060 const uint8_t *p_buffer = in->p_buffer;
2061 int i_buffer = in->i_buffer;
2063 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
2069 /* Split nal units */
2070 while( i_buffer > 4 )
2073 int i_size = i_buffer;
2074 int i_skip = i_buffer;
2076 /* search nal end */
2077 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
2079 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
2081 /* we found another startcode */
2082 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
2087 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
2088 rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
2089 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
2090 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
2098 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2101 int i_max = id->i_mtu - 14; /* payload max in one packet */
2102 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2104 uint8_t *p_data = in->p_buffer;
2105 int i_data = in->i_buffer;
2108 /* Only supports octet-aligned mode */
2109 for( i = 0; i < i_count; i++ )
2111 int i_payload = __MIN( i_max, i_data );
2112 block_t *out = block_New( p_stream, 14 + i_payload );
2114 /* rtp common header */
2115 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2116 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2117 /* Payload header */
2118 out->p_buffer[12] = 0xF0; /* CMR */
2119 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
2121 /* FIXME: are we fed multiple frames ? */
2122 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
2124 out->i_buffer = 14 + i_payload-1;
2125 out->i_dts = in->i_dts + i * in->i_length / i_count;
2126 out->i_length = in->i_length / i_count;
2128 rtp_packetize_send( id, out );
2130 p_data += i_payload;
2131 i_data -= i_payload;