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 /*****************************************************************************
171 *****************************************************************************/
172 static int Open( vlc_object_t *p_this )
174 sout_stream_t *p_stream = (sout_stream_t*)p_this;
175 sout_instance_t *p_sout = p_stream->p_sout;
176 sout_stream_sys_t *p_sys = NULL;
177 config_chain_t *p_cfg = NULL;
179 vlc_bool_t b_rtsp = VLC_FALSE;
181 config_ChainParse( p_stream, SOUT_CFG_PREFIX,
182 ppsz_sout_options, p_stream->p_cfg );
184 p_sys = malloc( sizeof( sout_stream_sys_t ) );
188 p_sys->psz_destination = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
189 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
190 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
191 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
192 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
194 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
195 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
196 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
198 p_sys->psz_sdp_file = NULL;
200 if( p_sys->i_port_audio == p_sys->i_port_video )
202 msg_Err( p_stream, "audio and video port cannot be the same" );
203 p_sys->i_port_audio = 0;
204 p_sys->i_port_video = 0;
207 if( !p_sys->psz_session_name )
209 if( p_sys->psz_destination )
210 p_sys->psz_session_name = strdup( p_sys->psz_destination );
212 p_sys->psz_session_name = strdup( "NONE" );
215 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
217 if( !strcmp( p_cfg->psz_name, "sdp" ) )
219 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
229 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
230 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
232 free( val2.psz_string );
235 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
239 msg_Err( p_stream, "missing destination and not in rtsp mode" );
243 p_sys->psz_destination = NULL;
245 else if( p_sys->i_port <= 0 )
247 msg_Err( p_stream, "invalid port" );
252 p_sys->i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
253 if( p_sys->i_ttl == 0 )
255 /* Normally, we should let the default hop limit up to the core,
256 * but we have to know it to build our SDP properly, which is why
257 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
259 p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
261 if( p_sys->i_ttl > 255 )
263 /* must not exceed 999 once formatted */
265 p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
267 p_sys->i_payload_type = 96;
272 p_sys->psz_sdp = NULL;
274 p_sys->i_sdp_id = mdate();
275 p_sys->i_sdp_version = 1;
276 p_sys->psz_sdp = NULL;
278 p_sys->b_export_sap = VLC_FALSE;
279 p_sys->b_export_sdp_file = VLC_FALSE;
280 p_sys->p_session = NULL;
282 p_sys->p_httpd_host = NULL;
283 p_sys->p_httpd_file = NULL;
284 p_sys->p_rtsp_host = NULL;
285 p_sys->p_rtsp_url = NULL;
286 p_sys->psz_rtsp_control = NULL;
287 p_sys->psz_rtsp_path = NULL;
289 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
290 vlc_mutex_init( p_stream, &p_sys->lock_es );
292 p_stream->pf_add = Add;
293 p_stream->pf_del = Del;
294 p_stream->pf_send = Send;
296 p_stream->p_sys = p_sys;
298 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
299 if( *val.psz_string )
301 sout_access_out_t *p_grab;
302 const char *psz_rtpmap;
303 char url[NI_MAXHOST + 8], access[22], psz_ttl[5], ipv;
307 msg_Err( p_stream, "muxing is not supported in RTSP mode" );
311 else if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
313 msg_Err( p_stream, "RTP needs a destination when muxing" );
318 /* Check muxer type */
319 if( !strncasecmp( val.psz_string, "ps", 2 )
320 || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
322 psz_rtpmap = "MP2P/90000";
324 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
326 psz_rtpmap = "MP2T/90000";
327 p_sys->i_payload_type = 33;
331 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
336 /* create the access out */
337 if( p_sys->i_ttl > 0 )
339 sprintf( access, "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
343 strcat( access, "udp{raw,rtcp}" );
346 /* FIXME: we should check that url is a numerical address, otherwise
347 * the SDP will be quite broken (regardless of the IP protocol version)
348 * Also it might be IPv6 with no ':' if it is a DNS name.
350 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
352 /* IPv6 needs brackets if not already present */
353 if ((ipv == '6') && (p_sys->psz_destination[0] != '['))
354 snprintf (url, sizeof (url), "[%s]:%d",p_sys->psz_destination,
357 snprintf (url, sizeof (url), "%s:%d",p_sys->psz_destination,
360 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
362 msg_Err( p_stream, "cannot create the access out for %s://%s",
367 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
368 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
370 /* better than nothing */
373 p_sys->i_mtu -= MTU_REDUCE;
375 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
376 p_grab = p_sys->p_grab =
377 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
378 p_grab->p_module = NULL;
379 p_grab->p_sout = p_sout;
380 p_grab->psz_access = strdup( "grab" );
381 p_grab->p_cfg = NULL;
382 p_grab->psz_path = strdup( "" );
383 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
384 p_grab->pf_seek = NULL;
385 p_grab->pf_write = AccessOutGrabberWrite;
388 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
390 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
391 sout_AccessOutDelete( p_sys->p_grab );
392 sout_AccessOutDelete( p_sys->p_access );
397 /* create the SDP for a muxed stream (only once) */
398 /* FIXME http://www.faqs.org/rfcs/rfc4566.html
399 o= - should be local username (no spaces allowed)
400 o= time should be hashed with some other value to garantee uniqueness
401 o= don't use the localhost address. use fully qualified domain name or IP4 address
402 a= source-filter: we need our source address
403 a= x-plgroup: (missing)
404 RTP packets need to get the correct src IP address */
406 && net_AddressIsMulticast( VLC_OBJECT(p_stream), p_sys->psz_destination ) )
408 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
409 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
416 asprintf( &p_sys->psz_sdp,
418 /* FIXME: source address not known :( */
419 "o=- "I64Fd" %d IN IP%c %s\r\n"
425 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
426 "a=tool:"PACKAGE_STRING"\r\n"
428 "a=type:broadcast\r\n"
429 "m=video %d RTP/AVP %d\r\n"
430 "a=rtpmap:%d %s\r\n",
431 p_sys->i_sdp_id, p_sys->i_sdp_version,
432 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
433 p_sys->psz_session_name,
434 p_sys->psz_session_description,
435 p_sys->psz_session_url,
436 p_sys->psz_session_email,
437 ipv, p_sys->psz_destination, psz_ttl,
438 p_sys->i_port, p_sys->i_payload_type,
439 p_sys->i_payload_type, psz_rtpmap );
440 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
442 /* create the rtp context */
443 p_sys->ssrc[0] = rand()&0xff;
444 p_sys->ssrc[1] = rand()&0xff;
445 p_sys->ssrc[2] = rand()&0xff;
446 p_sys->ssrc[3] = rand()&0xff;
447 p_sys->i_sequence = rand()&0xffff;
448 p_sys->i_timestamp_start = rand()&0xffffffff;
449 p_sys->packet = NULL;
454 p_sys->p_access = NULL;
455 p_sys->p_grab = NULL;
457 free( val.psz_string );
460 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
461 if( *val.psz_string )
463 config_chain_t *p_cfg;
465 SDPHandleUrl( p_stream, val.psz_string );
467 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
469 if( !strcmp( p_cfg->psz_name, "sdp" ) )
471 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
474 /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
475 if( !strcmp( p_cfg->psz_value, val.psz_string ) )
478 SDPHandleUrl( p_stream, p_cfg->psz_value );
482 free( val.psz_string );
484 /* update p_sout->i_out_pace_nocontrol */
485 p_stream->p_sout->i_out_pace_nocontrol++;
490 /*****************************************************************************
492 *****************************************************************************/
493 static void Close( vlc_object_t * p_this )
495 sout_stream_t *p_stream = (sout_stream_t*)p_this;
496 sout_stream_sys_t *p_sys = p_stream->p_sys;
498 /* update p_sout->i_out_pace_nocontrol */
499 p_stream->p_sout->i_out_pace_nocontrol--;
503 sout_MuxDelete( p_sys->p_mux );
504 sout_AccessOutDelete( p_sys->p_access );
505 sout_AccessOutDelete( p_sys->p_grab );
508 block_Release( p_sys->packet );
510 if( p_sys->b_export_sap )
513 SapSetup( p_stream );
517 RtspUnsetup( p_stream );
519 vlc_mutex_destroy( &p_sys->lock_sdp );
521 if( p_sys->p_httpd_file )
522 httpd_FileDelete( p_sys->p_httpd_file );
524 if( p_sys->p_httpd_host )
525 httpd_HostDelete( p_sys->p_httpd_host );
527 if( p_sys->p_rtsp_url )
528 httpd_UrlDelete( p_sys->p_rtsp_url );
530 if( p_sys->p_rtsp_host )
531 httpd_HostDelete( p_sys->p_rtsp_host );
533 if( p_sys->psz_session_name )
534 free( p_sys->psz_session_name );
536 if( p_sys->psz_session_description )
537 free( p_sys->psz_session_description );
539 if( p_sys->psz_session_url )
540 free( p_sys->psz_session_url );
542 if( p_sys->psz_session_email )
543 free( p_sys->psz_session_email );
546 free( p_sys->psz_sdp );
548 if( p_sys->b_export_sdp_file )
551 unlink( p_sys->psz_sdp_file );
553 free( p_sys->psz_sdp_file );
555 if( p_sys->psz_destination )
556 free( p_sys->psz_destination );
560 /*****************************************************************************
562 *****************************************************************************/
563 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
565 sout_stream_sys_t *p_sys = p_stream->p_sys;
568 vlc_UrlParse( &url, psz_url, 0 );
569 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
571 if( p_sys->p_httpd_file )
573 msg_Err( p_stream, "you can use sdp=http:// only once" );
577 if( HttpSetup( p_stream, &url ) )
579 msg_Err( p_stream, "cannot export sdp as http" );
582 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
584 if( p_sys->p_rtsp_url )
586 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
590 /* FIXME test if destination is multicast or no destination at all FIXME */
591 if( RtspSetup( p_stream, &url ) )
593 msg_Err( p_stream, "cannot export sdp as rtsp" );
596 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
597 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
599 p_sys->b_export_sap = VLC_TRUE;
600 SapSetup( p_stream );
602 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
604 if( p_sys->b_export_sdp_file )
606 msg_Err( p_stream, "you can use sdp=file:// only once" );
609 p_sys->b_export_sdp_file = VLC_TRUE;
610 psz_url = &psz_url[5];
611 if( psz_url[0] == '/' && psz_url[1] == '/' )
613 p_sys->psz_sdp_file = strdup( psz_url );
617 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
620 vlc_UrlClean( &url );
623 /*****************************************************************************
625 *****************************************************************************/
626 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
627 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
628 o= - should be local username (no spaces allowed)
629 o= time should be hashed with some other value to garantue uniqueness
630 o= we need IP6 support?
631 o= don't use the localhost address. use fully qualified domain name or IP4 address
632 p= international phone number (pass via vars?)
634 a= recvonly (missing)
635 a= type:broadcast (missing)
636 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
637 a= x-plgroup: (missing)
638 RTP packets need to get the correct src IP address */
639 /*static*/ char *SDPGenerate( const sout_stream_t *p_stream,
640 const char *psz_destination, vlc_bool_t b_rtsp )
642 sout_stream_sys_t *p_sys = p_stream->p_sys;
644 char *psz_sdp, *p, ipv;
647 /* FIXME: breaks IP version check on unknown destination */
648 if( psz_destination == NULL )
649 psz_destination = "0.0.0.0";
651 i_size = sizeof( "v=0\r\n" ) +
652 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
653 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
654 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
655 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
656 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
657 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
658 sizeof( "b=RR:0\r\n" ) +
659 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
660 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
661 strlen( psz_destination ) ;
662 for( i = 0; i < p_sys->i_es; i++ )
664 sout_stream_id_t *id = p_sys->es[i];
666 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
669 i_size += strlen( "b=AS: *\r\n") + 10;
673 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
677 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
681 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
686 i_size += strlen( "m=video %d RTP/AVP %d\r\n" ) +10 +10;
689 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
691 p = psz_sdp = malloc( i_size );
692 p += sprintf( p, "v=0\r\n" );
693 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
694 p_sys->i_sdp_id, p_sys->i_sdp_version,
695 ipv, ipv == '6' ? "::1" : "127.0.0.1" );
696 if( *p_sys->psz_session_name )
697 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
698 if( *p_sys->psz_session_description )
699 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
700 if( *p_sys->psz_session_url )
701 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
702 if( *p_sys->psz_session_email )
703 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
705 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
706 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
708 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
711 && net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
713 /* Add the deprecated TTL field if it is an IPv4 multicast address */
714 p += sprintf( p, "/%d", p_sys->i_ttl ?: 1 );
716 p += sprintf( p, "\r\n" );
717 p += sprintf( p, "b=RR:0\r\n" );
719 for( i = 0; i < p_sys->i_es; i++ )
721 sout_stream_id_t *id = p_sys->es[i];
723 if( id->i_cat == AUDIO_ES )
725 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
726 id->i_port, id->i_payload_type );
728 else if( id->i_cat == VIDEO_ES )
730 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
731 id->i_port, id->i_payload_type );
739 p += sprintf(p,"b=AS:%d\r\n",id->i_bitrate);
743 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
748 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
753 p += sprintf( p, "a=control:/trackID=%d\r\n", i );
758 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
759 p_sys->i_port, p_sys->i_payload_type );
765 /*****************************************************************************
767 *****************************************************************************/
768 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
769 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
770 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
771 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
772 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
773 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
774 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
775 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
776 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
777 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
778 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
780 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
782 static const char hex[16] = "0123456789abcdef";
785 for( i = 0; i < i_data; i++ )
787 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
788 s[2*i+1] = hex[(p_data[i] )&0xf];
793 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
795 sout_instance_t *p_sout = p_stream->p_sout;
796 sout_stream_sys_t *p_sys = p_stream->p_sys;
797 sout_stream_id_t *id;
798 sout_access_out_t *p_access = NULL;
802 if( p_sys->p_mux != NULL )
804 sout_input_t *p_input = NULL;
805 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
807 msg_Err( p_stream, "cannot add this stream to the muxer" );
811 id = malloc( sizeof( sout_stream_id_t ) );
812 memset( id, 0, sizeof( sout_stream_id_t ) );
814 id->p_input = p_input;
815 id->pf_packetize= NULL;
816 id->p_rtsp_url = NULL;
822 /* Choose the port */
824 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
826 i_port = p_sys->i_port_audio;
827 p_sys->i_port_audio = 0;
829 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
831 i_port = p_sys->i_port_video;
832 p_sys->i_port_video = 0;
836 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
838 i_port = p_sys->i_port;
845 if( p_sys->psz_destination )
848 char url[NI_MAXHOST + 8];
850 /* first try to create the access out */
851 if( p_sys->i_ttl > 0 )
853 snprintf( access, sizeof( access ), "udp{raw,rtcp,ttl=%d}",
855 access[sizeof( access ) - 1] = '\0';
858 strcpy( access, "udp{raw,rtcp}" );
860 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
861 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
862 p_sys->psz_destination, i_port );
863 url[sizeof( url ) - 1] = '\0';
865 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
867 msg_Err( p_stream, "cannot create the access out for %s://%s",
871 msg_Dbg( p_stream, "access out %s:%s", access, url );
874 /* not create the rtp specific stuff */
875 id = malloc( sizeof( sout_stream_id_t ) );
876 memset( id, 0, sizeof( sout_stream_id_t ) );
877 id->p_stream = p_stream;
878 id->p_access = p_access;
880 id->psz_rtpmap = NULL;
882 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
884 id->p_rtsp_url = NULL;
885 vlc_mutex_init( p_stream, &id->lock_rtsp );
886 id->i_rtsp_access = 0;
887 id->rtsp_access = NULL;
889 switch( p_fmt->i_codec )
891 case VLC_FOURCC( 's', '1', '6', 'b' ):
892 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
894 id->i_payload_type = 11;
896 else if( p_fmt->audio.i_channels == 2 &&
897 p_fmt->audio.i_rate == 44100 )
899 id->i_payload_type = 10;
903 id->i_payload_type = p_sys->i_payload_type++;
905 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
906 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
907 p_fmt->audio.i_channels );
908 id->i_clock_rate = p_fmt->audio.i_rate;
909 id->pf_packetize = rtp_packetize_l16;
911 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
912 id->i_payload_type = p_sys->i_payload_type++;
913 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
914 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
915 p_fmt->audio.i_channels );
916 id->i_clock_rate = p_fmt->audio.i_rate;
917 id->pf_packetize = rtp_packetize_l8;
919 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
920 id->i_payload_type = 14;
921 id->i_clock_rate = 90000;
922 id->psz_rtpmap = strdup( "MPA/90000" );
923 id->pf_packetize = rtp_packetize_mpa;
925 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
926 id->i_payload_type = 32;
927 id->i_clock_rate = 90000;
928 id->psz_rtpmap = strdup( "MPV/90000" );
929 id->pf_packetize = rtp_packetize_mpv;
931 case VLC_FOURCC( 'a', '5', '2', ' ' ):
932 id->i_payload_type = p_sys->i_payload_type++;
933 id->i_clock_rate = 90000;
934 id->psz_rtpmap = strdup( "ac3/90000" );
935 id->pf_packetize = rtp_packetize_ac3;
937 case VLC_FOURCC( 'H', '2', '6', '3' ):
938 id->i_payload_type = p_sys->i_payload_type++;
939 id->i_clock_rate = 90000;
940 id->psz_rtpmap = strdup( "H263-1998/90000" );
941 id->pf_packetize = rtp_packetize_h263;
943 case VLC_FOURCC( 'h', '2', '6', '4' ):
944 id->i_payload_type = p_sys->i_payload_type++;
945 id->i_clock_rate = 90000;
946 id->psz_rtpmap = strdup( "H264/90000" );
947 id->pf_packetize = rtp_packetize_h264;
950 if( p_fmt->i_extra > 0 )
952 uint8_t *p_buffer = p_fmt->p_extra;
953 int i_buffer = p_fmt->i_extra;
954 char *p_64_sps = NULL;
955 char *p_64_pps = NULL;
958 while( i_buffer > 4 &&
959 p_buffer[0] == 0 && p_buffer[1] == 0 &&
960 p_buffer[2] == 0 && p_buffer[3] == 1 )
962 const int i_nal_type = p_buffer[4]&0x1f;
966 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
969 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
971 if( !memcmp (p_buffer + i_offset, "\x00\x00\x00\x01", 4 ) )
973 /* we found another startcode */
978 if( i_nal_type == 7 )
980 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
981 sprintf_hexa( hexa, &p_buffer[5], 3 );
983 else if( i_nal_type == 8 )
985 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
991 if( p_64_sps && p_64_pps )
992 asprintf( &id->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
999 id->psz_fmtp = strdup( "packetization-mode=1" );
1002 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1004 char hexa[2*p_fmt->i_extra +1];
1006 id->i_payload_type = p_sys->i_payload_type++;
1007 id->i_clock_rate = 90000;
1008 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1009 id->pf_packetize = rtp_packetize_split;
1010 if( p_fmt->i_extra > 0 )
1012 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1013 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1014 sprintf( id->psz_fmtp,
1015 "profile-level-id=3; config=%s;", hexa );
1019 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1021 id->i_payload_type = p_sys->i_payload_type++;
1022 id->i_clock_rate = p_fmt->audio.i_rate;
1026 char hexa[2*p_fmt->i_extra +1];
1028 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1029 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1030 id->pf_packetize = rtp_packetize_mp4a;
1031 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1032 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1033 sprintf( id->psz_fmtp,
1034 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1035 "config=%s; SizeLength=13;IndexLength=3; "
1036 "IndexDeltaLength=3; Profile=1;", hexa );
1042 unsigned char config[6];
1043 unsigned int aacsrates[15] = {
1044 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1045 16000, 12000, 11025, 8000, 7350, 0, 0 };
1047 for( i = 0; i < 15; i++ )
1048 if( p_fmt->audio.i_rate == aacsrates[i] )
1054 config[3]=p_fmt->audio.i_channels<<4;
1058 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1059 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1060 id->pf_packetize = rtp_packetize_mp4a_latm;
1061 sprintf_hexa( hexa, config, 6 );
1062 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1063 "object=2; cpresent=0; config=%s", hexa );
1067 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1068 id->i_payload_type = p_sys->i_payload_type++;
1069 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1070 "AMR/8000/2" : "AMR/8000" );
1071 id->psz_fmtp = strdup( "octet-align=1" );
1072 id->i_clock_rate = p_fmt->audio.i_rate;
1073 id->pf_packetize = rtp_packetize_amr;
1075 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1076 id->i_payload_type = p_sys->i_payload_type++;
1077 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1078 "AMR-WB/16000/2" : "AMR-WB/16000" );
1079 id->psz_fmtp = strdup( "octet-align=1" );
1080 id->i_clock_rate = p_fmt->audio.i_rate;
1081 id->pf_packetize = rtp_packetize_amr;
1085 msg_Err( p_stream, "cannot add this stream (unsupported "
1086 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1089 sout_AccessOutDelete( p_access );
1094 id->i_cat = p_fmt->i_cat;
1096 id->ssrc[0] = rand()&0xff;
1097 id->ssrc[1] = rand()&0xff;
1098 id->ssrc[2] = rand()&0xff;
1099 id->ssrc[3] = rand()&0xff;
1100 id->i_sequence = rand()&0xffff;
1101 id->i_timestamp_start = rand()&0xffffffff;
1102 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
1104 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1105 if( id->i_mtu <= 16 + MTU_REDUCE )
1107 /* better than nothing */
1110 id->i_mtu -= MTU_REDUCE;
1111 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1113 if( p_sys->p_rtsp_url )
1114 RtspSetupId( p_stream, id );
1116 /* Update p_sys context */
1117 vlc_mutex_lock( &p_sys->lock_es );
1118 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1119 vlc_mutex_unlock( &p_sys->lock_es );
1121 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1123 vlc_mutex_lock( &p_sys->lock_sdp );
1124 free( p_sys->psz_sdp );
1125 p_sys->psz_sdp = psz_sdp;
1126 vlc_mutex_unlock( &p_sys->lock_sdp );
1128 p_sys->i_sdp_version++;
1130 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
1132 /* Update SDP (sap/file) */
1133 if( p_sys->b_export_sap ) SapSetup( p_stream );
1134 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1139 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1141 sout_stream_sys_t *p_sys = p_stream->p_sys;
1143 vlc_mutex_lock( &p_sys->lock_es );
1144 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1145 vlc_mutex_unlock( &p_sys->lock_es );
1148 if( id->i_port > 0 )
1150 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1151 p_sys->i_port_audio = id->i_port;
1152 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1153 p_sys->i_port_video = id->i_port;
1158 if( id->psz_rtpmap )
1160 free( id->psz_rtpmap );
1164 free( id->psz_fmtp );
1166 if( id->psz_destination )
1167 free( id->psz_destination );
1168 sout_AccessOutDelete( id->p_access );
1170 else if( id->p_input )
1172 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1174 if( id->p_rtsp_url )
1176 httpd_UrlDelete( id->p_rtsp_url );
1178 vlc_mutex_destroy( &id->lock_rtsp );
1179 if( id->rtsp_access ) free( id->rtsp_access );
1181 /* Update SDP (sap/file) */
1182 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1183 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1189 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1194 if( p_stream->p_sys->p_mux )
1196 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1202 p_next = p_buffer->p_next;
1203 if( id->pf_packetize( p_stream, id, p_buffer ) )
1207 block_Release( p_buffer );
1215 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1218 sout_stream_sys_t *p_sys = p_stream->p_sys;
1220 int64_t i_dts = p_buffer->i_dts;
1221 uint32_t i_timestamp = i_dts * 9 / 100;
1223 uint8_t *p_data = p_buffer->p_buffer;
1224 unsigned int i_data = p_buffer->i_buffer;
1225 unsigned int i_max = p_sys->i_mtu - 12;
1227 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1231 unsigned int i_size;
1233 /* output complete packet */
1234 if( p_sys->packet &&
1235 p_sys->packet->i_buffer + i_data > i_max )
1237 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1238 p_sys->packet = NULL;
1241 if( p_sys->packet == NULL )
1243 /* allocate a new packet */
1244 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1245 p_sys->packet->p_buffer[ 0] = 0x80;
1246 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1247 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1248 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1249 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1250 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1251 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1252 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1253 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1254 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1255 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1256 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1257 p_sys->packet->i_buffer = 12;
1259 p_sys->packet->i_dts = i_dts;
1260 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1261 i_dts += p_sys->packet->i_length;
1263 p_sys->i_sequence++;
1266 i_size = __MIN( i_data,
1267 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1269 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1272 p_sys->packet->i_buffer += i_size;
1280 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1283 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1285 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1291 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1293 p_next = p_buffer->p_next;
1294 block_Release( p_buffer );
1301 /****************************************************************************
1303 ****************************************************************************/
1304 static int SapSetup( sout_stream_t *p_stream )
1306 sout_stream_sys_t *p_sys = p_stream->p_sys;
1307 sout_instance_t *p_sout = p_stream->p_sout;
1308 announce_method_t *p_method = sout_SAPMethod();
1310 /* Remove the previous session */
1311 if( p_sys->p_session != NULL)
1313 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1314 sout_AnnounceSessionDestroy( p_sys->p_session );
1315 p_sys->p_session = NULL;
1318 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1320 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
1322 p_sys->psz_destination,
1326 sout_MethodRelease( p_method );
1330 /****************************************************************************
1332 ****************************************************************************/
1333 static int FileSetup( sout_stream_t *p_stream )
1335 sout_stream_sys_t *p_sys = p_stream->p_sys;
1338 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1340 msg_Err( p_stream, "cannot open file '%s' (%s)",
1341 p_sys->psz_sdp_file, strerror(errno) );
1342 return VLC_EGENERIC;
1345 fprintf( f, "%s", p_sys->psz_sdp );
1351 /****************************************************************************
1353 ****************************************************************************/
1354 static int HttpCallback( httpd_file_sys_t *p_args,
1355 httpd_file_t *, uint8_t *p_request,
1356 uint8_t **pp_data, int *pi_data );
1358 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1360 sout_stream_sys_t *p_sys = p_stream->p_sys;
1362 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1363 if( p_sys->p_httpd_host )
1365 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1366 url->psz_path ? url->psz_path : "/",
1369 HttpCallback, (void*)p_sys );
1371 if( p_sys->p_httpd_file == NULL )
1373 return VLC_EGENERIC;
1378 static int HttpCallback( httpd_file_sys_t *p_args,
1379 httpd_file_t *f, uint8_t *p_request,
1380 uint8_t **pp_data, int *pi_data )
1382 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1384 vlc_mutex_lock( &p_sys->lock_sdp );
1385 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1387 *pi_data = strlen( p_sys->psz_sdp );
1388 *pp_data = malloc( *pi_data );
1389 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1396 vlc_mutex_unlock( &p_sys->lock_sdp );
1401 /****************************************************************************
1403 ****************************************************************************/
1404 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1405 int b_marker, int64_t i_pts )
1407 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1409 out->p_buffer[0] = 0x80;
1410 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1411 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1412 out->p_buffer[3] = ( id->i_sequence )&0xff;
1413 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1414 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1415 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1416 out->p_buffer[7] = ( i_timestamp )&0xff;
1418 out->p_buffer[ 8] = id->ssrc[0];
1419 out->p_buffer[ 9] = id->ssrc[1];
1420 out->p_buffer[10] = id->ssrc[2];
1421 out->p_buffer[11] = id->ssrc[3];
1427 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1430 vlc_mutex_lock( &id->lock_rtsp );
1431 for( i = 0; i < id->i_rtsp_access; i++ )
1433 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1435 vlc_mutex_unlock( &id->lock_rtsp );
1439 sout_AccessOutWrite( id->p_access, out );
1443 block_Release( out );
1447 int rtp_add_sink( sout_stream_id_t *id, sout_access_out_t *access )
1449 vlc_mutex_lock( &id->lock_rtsp );
1450 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, access );
1451 vlc_mutex_unlock( &id->lock_rtsp );
1454 void rtp_del_sink( sout_stream_id_t *id, sout_access_out_t *access )
1456 vlc_mutex_lock( &id->lock_rtsp );
1457 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, access );
1458 vlc_mutex_unlock( &id->lock_rtsp );
1461 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1464 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1465 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1467 uint8_t *p_data = in->p_buffer;
1468 int i_data = in->i_buffer;
1471 for( i = 0; i < i_count; i++ )
1473 int i_payload = __MIN( i_max, i_data );
1474 block_t *out = block_New( p_stream, 16 + i_payload );
1476 /* rtp common header */
1477 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1479 out->p_buffer[12] = 0;
1480 out->p_buffer[13] = 0;
1481 /* fragment offset in the current frame */
1482 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1483 out->p_buffer[15] = ( (i*i_max) )&0xff;
1484 memcpy( &out->p_buffer[16], p_data, i_payload );
1486 out->i_buffer = 16 + i_payload;
1487 out->i_dts = in->i_dts + i * in->i_length / i_count;
1488 out->i_length = in->i_length / i_count;
1490 rtp_packetize_send( id, out );
1492 p_data += i_payload;
1493 i_data -= i_payload;
1500 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1503 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1504 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1506 uint8_t *p_data = in->p_buffer;
1507 int i_data = in->i_buffer;
1509 int b_sequence_start = 0;
1510 int i_temporal_ref = 0;
1511 int i_picture_coding_type = 0;
1512 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1513 int b_start_slice = 0;
1515 /* preparse this packet to get some info */
1516 if( in->i_buffer > 4 )
1518 uint8_t *p = p_data;
1519 int i_rest = in->i_buffer;
1523 while( i_rest > 4 &&
1524 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1538 /* sequence start code */
1539 b_sequence_start = 1;
1541 else if( *p == 0x00 && i_rest >= 4 )
1544 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1545 i_picture_coding_type = (p[2] >> 3)&0x07;
1547 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1548 i_picture_coding_type == 3 ) )
1550 i_ffv = (p[3] >> 2)&0x01;
1551 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1552 if( i_rest > 4 && i_picture_coding_type == 3 )
1554 i_fbv = (p[4]>>6)&0x01;
1555 i_bfc = (p[4]>>3)&0x07;
1559 else if( *p <= 0xaf )
1566 for( i = 0; i < i_count; i++ )
1568 int i_payload = __MIN( i_max, i_data );
1569 block_t *out = block_New( p_stream,
1571 uint32_t h = ( i_temporal_ref << 16 )|
1572 ( b_sequence_start << 13 )|
1573 ( b_start_slice << 12 )|
1574 ( i == i_count - 1 ? 1 << 11 : 0 )|
1575 ( i_picture_coding_type << 8 )|
1576 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1578 /* rtp common header */
1579 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1580 in->i_pts > 0 ? in->i_pts : in->i_dts );
1582 /* 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 */
1583 out->p_buffer[12] = ( h >> 24 )&0xff;
1584 out->p_buffer[13] = ( h >> 16 )&0xff;
1585 out->p_buffer[14] = ( h >> 8 )&0xff;
1586 out->p_buffer[15] = ( h )&0xff;
1588 memcpy( &out->p_buffer[16], p_data, i_payload );
1590 out->i_buffer = 16 + i_payload;
1591 out->i_dts = in->i_dts + i * in->i_length / i_count;
1592 out->i_length = in->i_length / i_count;
1594 rtp_packetize_send( id, out );
1596 p_data += i_payload;
1597 i_data -= i_payload;
1603 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1606 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1607 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1609 uint8_t *p_data = in->p_buffer;
1610 int i_data = in->i_buffer;
1613 for( i = 0; i < i_count; i++ )
1615 int i_payload = __MIN( i_max, i_data );
1616 block_t *out = block_New( p_stream, 14 + i_payload );
1618 /* rtp common header */
1619 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1621 out->p_buffer[12] = 1;
1623 out->p_buffer[13] = 0x00;
1625 memcpy( &out->p_buffer[14], p_data, i_payload );
1627 out->i_buffer = 14 + i_payload;
1628 out->i_dts = in->i_dts + i * in->i_length / i_count;
1629 out->i_length = in->i_length / i_count;
1631 rtp_packetize_send( id, out );
1633 p_data += i_payload;
1634 i_data -= i_payload;
1640 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1643 int i_max = id->i_mtu - 12; /* payload max in one packet */
1644 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1646 uint8_t *p_data = in->p_buffer;
1647 int i_data = in->i_buffer;
1650 for( i = 0; i < i_count; i++ )
1652 int i_payload = __MIN( i_max, i_data );
1653 block_t *out = block_New( p_stream, 12 + i_payload );
1655 /* rtp common header */
1656 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1657 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1658 memcpy( &out->p_buffer[12], p_data, i_payload );
1660 out->i_buffer = 12 + i_payload;
1661 out->i_dts = in->i_dts + i * in->i_length / i_count;
1662 out->i_length = in->i_length / i_count;
1664 rtp_packetize_send( id, out );
1666 p_data += i_payload;
1667 i_data -= i_payload;
1674 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
1677 int i_max = id->i_mtu - 14; /* payload max in one packet */
1678 int latmhdrsize = in->i_buffer / 0xff + 1;
1679 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1681 uint8_t *p_data = in->p_buffer, *p_header = NULL;
1682 int i_data = in->i_buffer;
1685 for( i = 0; i < i_count; i++ )
1687 int i_payload = __MIN( i_max, i_data );
1692 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
1694 /* rtp common header */
1695 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
1696 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1700 int tmp = in->i_buffer;
1702 p_header=out->p_buffer+12;
1712 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
1714 out->i_buffer = 12 + latmhdrsize + i_payload;
1715 out->i_dts = in->i_dts + i * in->i_length / i_count;
1716 out->i_length = in->i_length / i_count;
1718 rtp_packetize_send( id, out );
1720 p_data += i_payload;
1721 i_data -= i_payload;
1727 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1730 int i_max = id->i_mtu - 12; /* payload max in one packet */
1731 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1733 uint8_t *p_data = in->p_buffer;
1734 int i_data = in->i_buffer;
1739 int i_payload = (__MIN( i_max, i_data )/4)*4;
1740 block_t *out = block_New( p_stream, 12 + i_payload );
1742 /* rtp common header */
1743 rtp_packetize_common( id, out, 0,
1744 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1745 memcpy( &out->p_buffer[12], p_data, i_payload );
1747 out->i_buffer = 12 + i_payload;
1748 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1749 out->i_length = in->i_length / i_count;
1751 rtp_packetize_send( id, out );
1753 p_data += i_payload;
1754 i_data -= i_payload;
1761 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1764 int i_max = id->i_mtu - 12; /* payload max in one packet */
1765 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1767 uint8_t *p_data = in->p_buffer;
1768 int i_data = in->i_buffer;
1773 int i_payload = (__MIN( i_max, i_data )/2)*2;
1774 block_t *out = block_New( p_stream, 12 + i_payload );
1776 /* rtp common header */
1777 rtp_packetize_common( id, out, 0,
1778 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1779 memcpy( &out->p_buffer[12], p_data, i_payload );
1781 out->i_buffer = 12 + i_payload;
1782 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1783 out->i_length = in->i_length / i_count;
1785 rtp_packetize_send( id, out );
1787 p_data += i_payload;
1788 i_data -= i_payload;
1794 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1797 int i_max = id->i_mtu - 16; /* payload max in one packet */
1798 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1800 uint8_t *p_data = in->p_buffer;
1801 int i_data = in->i_buffer;
1804 for( i = 0; i < i_count; i++ )
1806 int i_payload = __MIN( i_max, i_data );
1807 block_t *out = block_New( p_stream, 16 + i_payload );
1809 /* rtp common header */
1810 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1811 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1813 /* AU headers length (bits) */
1814 out->p_buffer[12] = 0;
1815 out->p_buffer[13] = 2*8;
1816 /* for each AU length 13 bits + idx 3bits, */
1817 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1818 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1820 memcpy( &out->p_buffer[16], p_data, i_payload );
1822 out->i_buffer = 16 + i_payload;
1823 out->i_dts = in->i_dts + i * in->i_length / i_count;
1824 out->i_length = in->i_length / i_count;
1826 rtp_packetize_send( id, out );
1828 p_data += i_payload;
1829 i_data -= i_payload;
1837 #define RTP_H263_HEADER_SIZE (2) // plen = 0
1838 #define RTP_H263_PAYLOAD_START (14) // plen = 0
1839 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
1842 uint8_t *p_data = in->p_buffer;
1843 int i_data = in->i_buffer;
1845 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
1848 int b_v_bit = 0; // no pesky error resilience
1849 int i_plen = 0; // normally plen=0 for PSC packet
1850 int i_pebit = 0; // because plen=0
1855 return VLC_EGENERIC;
1857 if( p_data[0] || p_data[1] )
1859 return VLC_EGENERIC;
1861 /* remove 2 leading 0 bytes */
1864 i_count = ( i_data + i_max - 1 ) / i_max;
1866 for( i = 0; i < i_count; i++ )
1868 int i_payload = __MIN( i_max, i_data );
1869 block_t *out = block_New( p_stream,
1870 RTP_H263_PAYLOAD_START + i_payload );
1871 b_p_bit = (i == 0) ? 1 : 0;
1872 h = ( b_p_bit << 10 )|
1877 /* rtp common header */
1878 //b_m_bit = 1; // always contains end of frame
1879 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1880 in->i_pts > 0 ? in->i_pts : in->i_dts );
1883 out->p_buffer[12] = ( h >> 8 )&0xff;
1884 out->p_buffer[13] = ( h )&0xff;
1885 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1887 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1888 out->i_dts = in->i_dts + i * in->i_length / i_count;
1889 out->i_length = in->i_length / i_count;
1891 rtp_packetize_send( id, out );
1893 p_data += i_payload;
1894 i_data -= i_payload;
1901 static int rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
1902 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 )
1904 const int i_max = id->i_mtu - 12; /* payload max in one packet */
1911 i_nal_hdr = p_data[3];
1912 i_nal_type = i_nal_hdr&0x1f;
1913 if( i_nal_type == 7 || i_nal_type == 8 )
1915 /* XXX Why do you want to remove them ? It will break streaming with
1916 * SPS/PPS change (broadcast) ? */
1920 /* Skip start code */
1925 if( i_data <= i_max )
1927 /* Single NAL unit packet */
1928 block_t *out = block_New( p_stream, 12 + i_data );
1930 out->i_length = i_length;
1933 rtp_packetize_common( id, out, b_last, i_pts );
1934 out->i_buffer = 12 + i_data;
1936 memcpy( &out->p_buffer[12], p_data, i_data );
1938 rtp_packetize_send( id, out );
1942 /* FU-A Fragmentation Unit without interleaving */
1943 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1949 for( i = 0; i < i_count; i++ )
1951 const int i_payload = __MIN( i_data, i_max-2 );
1952 block_t *out = block_New( p_stream, 12 + 2 + i_payload );
1953 out->i_dts = i_dts + i * i_length / i_count;
1954 out->i_length = i_length / i_count;
1957 rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
1958 out->i_buffer = 14 + i_payload;
1961 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1963 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
1964 memcpy( &out->p_buffer[14], p_data, i_payload );
1966 rtp_packetize_send( id, out );
1968 i_data -= i_payload;
1969 p_data += i_payload;
1975 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
1978 const uint8_t *p_buffer = in->p_buffer;
1979 int i_buffer = in->i_buffer;
1981 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1987 /* Split nal units */
1988 while( i_buffer > 4 )
1991 int i_size = i_buffer;
1992 int i_skip = i_buffer;
1994 /* search nal end */
1995 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1997 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1999 /* we found another startcode */
2000 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
2005 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
2006 rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
2007 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
2008 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
2016 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2019 int i_max = id->i_mtu - 14; /* payload max in one packet */
2020 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2022 uint8_t *p_data = in->p_buffer;
2023 int i_data = in->i_buffer;
2026 /* Only supports octet-aligned mode */
2027 for( i = 0; i < i_count; i++ )
2029 int i_payload = __MIN( i_max, i_data );
2030 block_t *out = block_New( p_stream, 14 + i_payload );
2032 /* rtp common header */
2033 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2034 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2035 /* Payload header */
2036 out->p_buffer[12] = 0xF0; /* CMR */
2037 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
2039 /* FIXME: are we fed multiple frames ? */
2040 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
2042 out->i_buffer = 14 + i_payload-1;
2043 out->i_dts = in->i_dts + i * in->i_length / i_count;
2044 out->i_length = in->i_length / i_count;
2046 rtp_packetize_send( id, out );
2048 p_data += i_payload;
2049 i_data -= i_payload;