1 /*****************************************************************************
2 * rtp.c: rtp stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
5 * Copyright © 2007 Rémi Denis-Courmont
8 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
31 #include <vlc_block.h>
33 #include <vlc_httpd.h>
35 #include <vlc_network.h>
36 #include <vlc_charset.h>
37 #include <vlc_strings.h>
47 /*****************************************************************************
49 *****************************************************************************/
51 #define DST_TEXT N_("Destination")
52 #define DST_LONGTEXT N_( \
53 "This is the output URL that will be used." )
54 #define SDP_TEXT N_("SDP")
55 #define SDP_LONGTEXT N_( \
56 "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
57 "session will be made available. You must use an url: http://location to " \
58 "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
59 "for the SDP to be announced via SAP." )
60 #define MUX_TEXT N_("Muxer")
61 #define MUX_LONGTEXT N_( \
62 "This allows you to specify the muxer used for the streaming output. " \
63 "Default is to use no muxer (standard RTP stream)." )
65 #define NAME_TEXT N_("Session name")
66 #define NAME_LONGTEXT N_( \
67 "This is the name of the session that will be announced in the SDP " \
68 "(Session Descriptor)." )
69 #define DESC_TEXT N_("Session description")
70 #define DESC_LONGTEXT N_( \
71 "This allows you to give a broader description of the stream, that will " \
72 "be announced in the SDP (Session Descriptor)." )
73 #define URL_TEXT N_("Session URL")
74 #define URL_LONGTEXT N_( \
75 "This allows you to give an URL with more details about the stream " \
76 "(often the website of the streaming organization), that will " \
77 "be announced in the SDP (Session Descriptor)." )
78 #define EMAIL_TEXT N_("Session email")
79 #define EMAIL_LONGTEXT N_( \
80 "This allows you to give a contact mail address for the stream, that will " \
81 "be announced in the SDP (Session Descriptor)." )
82 #define PORT_TEXT N_("Port")
83 #define PORT_LONGTEXT N_( \
84 "This allows you to specify the base port for the RTP streaming." )
85 #define PORT_AUDIO_TEXT N_("Audio port")
86 #define PORT_AUDIO_LONGTEXT N_( \
87 "This allows you to specify the default audio port for the RTP streaming." )
88 #define PORT_VIDEO_TEXT N_("Video port")
89 #define PORT_VIDEO_LONGTEXT N_( \
90 "This allows you to specify the default video port for the RTP streaming." )
92 #define TTL_TEXT N_("Hop limit (TTL)")
93 #define TTL_LONGTEXT N_( \
94 "This is the hop limit (also known as \"Time-To-Live\" or TTL) of " \
95 "the multicast packets sent by the stream output (0 = use operating " \
96 "system built-in default).")
98 #define RFC3016_TEXT N_("MP4A LATM")
99 #define RFC3016_LONGTEXT N_( \
100 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
102 static int Open ( vlc_object_t * );
103 static void Close( vlc_object_t * );
105 #define SOUT_CFG_PREFIX "sout-rtp-"
108 set_shortname( _("RTP"));
109 set_description( _("RTP stream output") );
110 set_capability( "sout stream", 0 );
111 add_shortcut( "rtp" );
112 set_category( CAT_SOUT );
113 set_subcategory( SUBCAT_SOUT_STREAM );
115 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
116 DST_LONGTEXT, VLC_TRUE );
117 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
118 SDP_LONGTEXT, VLC_TRUE );
119 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
120 MUX_LONGTEXT, VLC_TRUE );
122 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
123 NAME_LONGTEXT, VLC_TRUE );
124 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
125 DESC_LONGTEXT, VLC_TRUE );
126 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
127 URL_LONGTEXT, VLC_TRUE );
128 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
129 EMAIL_LONGTEXT, VLC_TRUE );
131 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
132 PORT_LONGTEXT, VLC_TRUE );
133 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
134 PORT_AUDIO_LONGTEXT, VLC_TRUE );
135 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
136 PORT_VIDEO_LONGTEXT, VLC_TRUE );
138 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
139 TTL_LONGTEXT, VLC_TRUE );
141 add_bool( SOUT_CFG_PREFIX "mp4a-latm", 0, NULL, RFC3016_TEXT,
142 RFC3016_LONGTEXT, VLC_FALSE );
144 set_callbacks( Open, Close );
147 /*****************************************************************************
148 * Exported prototypes
149 *****************************************************************************/
150 static const char *ppsz_sout_options[] = {
151 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
152 "description", "url","email", "mp4a-latm", NULL
155 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
156 static int Del ( sout_stream_t *, sout_stream_id_t * );
157 static int Send( sout_stream_t *, sout_stream_id_t *,
159 static sout_stream_id_t *MuxAdd ( sout_stream_t *, es_format_t * );
160 static int MuxDel ( sout_stream_t *, sout_stream_id_t * );
161 static int MuxSend( sout_stream_t *, sout_stream_id_t *,
164 static sout_access_out_t *GrabberCreate( sout_stream_t *p_sout );
166 static void SDPHandleUrl( sout_stream_t *, char * );
168 static int SapSetup( sout_stream_t *p_stream );
169 static int FileSetup( sout_stream_t *p_stream );
170 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
172 struct sout_stream_sys_t
178 vlc_mutex_t lock_sdp;
180 char *psz_session_name;
181 char *psz_session_description;
182 char *psz_session_url;
183 char *psz_session_email;
186 vlc_bool_t b_export_sdp_file;
189 vlc_bool_t b_export_sap;
190 session_descriptor_t *p_session;
192 httpd_host_t *p_httpd_host;
193 httpd_file_t *p_httpd_file;
198 char *psz_destination;
205 /* when need to use a private one or when using muxer */
208 /* in case we do TS/PS over rtp */
210 sout_access_out_t *p_grab;
216 sout_stream_id_t **es;
219 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
222 struct sout_stream_id_t
224 sout_stream_t *p_stream;
226 uint32_t i_timestamp_start;
228 uint8_t i_payload_type;
239 /* Packetizer specific fields */
240 pf_rtp_packetizer_t pf_packetize;
244 vlc_mutex_t lock_sink;
247 rtsp_stream_id_t *rtsp_id;
251 /*****************************************************************************
253 *****************************************************************************/
254 static int Open( vlc_object_t *p_this )
256 sout_stream_t *p_stream = (sout_stream_t*)p_this;
257 sout_instance_t *p_sout = p_stream->p_sout;
258 sout_stream_sys_t *p_sys = NULL;
259 config_chain_t *p_cfg = NULL;
261 vlc_bool_t b_rtsp = VLC_FALSE;
263 config_ChainParse( p_stream, SOUT_CFG_PREFIX,
264 ppsz_sout_options, p_stream->p_cfg );
266 p_sys = malloc( sizeof( sout_stream_sys_t ) );
270 p_sys->psz_destination = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
271 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
272 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
273 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
274 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
276 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
277 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
278 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
280 p_sys->psz_sdp_file = NULL;
282 if( p_sys->i_port_audio == p_sys->i_port_video )
284 msg_Err( p_stream, "audio and video port cannot be the same" );
285 p_sys->i_port_audio = 0;
286 p_sys->i_port_video = 0;
289 if( !p_sys->psz_session_name )
291 if( p_sys->psz_destination )
292 p_sys->psz_session_name = strdup( p_sys->psz_destination );
294 p_sys->psz_session_name = strdup( "NONE" );
297 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
299 if( !strcmp( p_cfg->psz_name, "sdp" ) )
301 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
310 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
313 if( !strncasecmp( psz, "rtsp", 4 ) )
319 if( p_sys->psz_destination == NULL )
323 msg_Err( p_stream, "missing destination and not in RTSP mode" );
328 else if( p_sys->i_port <= 0 )
330 msg_Err( p_stream, "invalid port" );
335 p_sys->i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
336 if( p_sys->i_ttl == 0 )
338 /* Normally, we should let the default hop limit up to the core,
339 * but we have to know it to build our SDP properly, which is why
340 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
342 p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
344 if( p_sys->i_ttl > 255 )
346 /* must not exceed 999 once formatted */
348 p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
350 p_sys->i_payload_type = 96;
354 p_sys->psz_sdp = NULL;
356 p_sys->i_sdp_id = mdate();
357 p_sys->i_sdp_version = 1;
358 p_sys->psz_sdp = NULL;
360 p_sys->b_export_sap = VLC_FALSE;
361 p_sys->b_export_sdp_file = VLC_FALSE;
362 p_sys->p_session = NULL;
364 p_sys->p_httpd_host = NULL;
365 p_sys->p_httpd_file = NULL;
367 p_stream->p_sys = p_sys;
369 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
372 /* TODO: factorize code with Add() */
373 sout_stream_id_t *id;
375 p_stream->pf_add = MuxAdd;
376 p_stream->pf_del = MuxDel;
377 p_stream->pf_send = MuxSend;
379 id = malloc( sizeof( *id ) );
381 /* We have one and only one "ES" from RTP perspective */
382 TAB_APPEND( p_sys->i_es, p_sys->es, id );
384 //p_sys->pr_rtp_packetizer = abort;
385 id->p_stream = p_stream;
387 vlc_mutex_init( p_stream, &id->lock_sink );
392 /* Check muxer type */
393 if( !strncasecmp( psz, "ps", 2 )
394 || !strncasecmp( psz, "mpeg1", 5 ) )
396 id->psz_rtpmap = strdup( "MP2P/90000" );
397 id->i_payload_type = p_sys->i_payload_type++;
399 else if( !strncasecmp( psz, "ts", 2 ) )
401 id->psz_rtpmap = strdup( "MP2T/90000" );
402 id->i_payload_type = 33;
406 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
412 id->i_clock_rate = 90000;
414 id->i_port = p_sys->i_port;
415 id->i_cat = VIDEO_ES;
418 /* create the access out */
419 if( p_sys->psz_destination != NULL )
421 int ttl = (p_sys->i_ttl > 0) ? p_sys->i_ttl : -1;
422 int fd = net_ConnectDgram( p_stream, p_sys->psz_destination,
423 p_sys->i_port, ttl, IPPROTO_UDP );
427 msg_Err( p_stream, "cannot create RTP socket" );
428 vlc_mutex_destroy( &id->lock_sink );
434 rtp_add_sink( id, fd );
437 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
438 if( id->i_mtu <= 12 + 16 )
440 /* better than nothing */
441 id->i_mtu = 576 - 20 - 8;
444 p_sys->p_grab = GrabberCreate( p_stream );
447 if( !( p_sys->p_mux = sout_MuxNew( p_sout, psz, p_sys->p_grab ) ) )
449 msg_Err( p_stream, "cannot create the muxer (%s)", psz );
450 sout_AccessOutDelete( p_sys->p_grab );
452 rtp_del_sink( id, id->fdv[0] );
453 vlc_mutex_destroy( &id->lock_sink );
460 /* create the SDP for a muxed stream (only once) */
461 p_sys->psz_sdp = SDPGenerate( p_stream, NULL );
462 msg_Dbg( p_stream, "sdp=\n%s", p_sys->psz_sdp );
464 /* create the rtp context */
465 id->ssrc[0] = rand()&0xff;
466 id->ssrc[1] = rand()&0xff;
467 id->ssrc[2] = rand()&0xff;
468 id->ssrc[3] = rand()&0xff;
469 id->i_sequence = rand()&0xffff;
470 id->i_timestamp_start = rand()&0xffffffff;
471 p_sys->packet = NULL;
477 p_stream->pf_add = Add;
478 p_stream->pf_del = Del;
479 p_stream->pf_send = Send;
482 p_sys->p_grab = NULL;
485 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
486 vlc_mutex_init( p_stream, &p_sys->lock_es );
488 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
491 config_chain_t *p_cfg;
493 SDPHandleUrl( p_stream, psz );
495 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
497 if( !strcmp( p_cfg->psz_name, "sdp" ) )
499 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
502 /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
503 if( !strcmp( p_cfg->psz_value, psz ) )
506 SDPHandleUrl( p_stream, p_cfg->psz_value );
512 /* update p_sout->i_out_pace_nocontrol */
513 p_stream->p_sout->i_out_pace_nocontrol++;
518 /*****************************************************************************
520 *****************************************************************************/
521 static void Close( vlc_object_t * p_this )
523 sout_stream_t *p_stream = (sout_stream_t*)p_this;
524 sout_stream_sys_t *p_sys = p_stream->p_sys;
526 /* update p_sout->i_out_pace_nocontrol */
527 p_stream->p_sout->i_out_pace_nocontrol--;
531 assert( p_sys->i_es == 1 );
532 Del( p_stream, p_sys->es[0] );
535 sout_MuxDelete( p_sys->p_mux );
536 sout_AccessOutDelete( p_sys->p_grab );
539 block_Release( p_sys->packet );
541 if( p_sys->b_export_sap )
544 SapSetup( p_stream );
548 if( p_sys->rtsp != NULL )
549 RtspUnsetup( p_sys->rtsp );
551 vlc_mutex_destroy( &p_sys->lock_sdp );
552 vlc_mutex_destroy( &p_sys->lock_es );
554 if( p_sys->p_httpd_file )
555 httpd_FileDelete( p_sys->p_httpd_file );
557 if( p_sys->p_httpd_host )
558 httpd_HostDelete( p_sys->p_httpd_host );
560 free( p_sys->psz_session_name );
561 free( p_sys->psz_session_description );
562 free( p_sys->psz_session_url );
563 free( p_sys->psz_session_email );
564 free( p_sys->psz_sdp );
566 if( p_sys->b_export_sdp_file )
569 unlink( p_sys->psz_sdp_file );
571 free( p_sys->psz_sdp_file );
573 free( p_sys->psz_destination );
577 /*****************************************************************************
579 *****************************************************************************/
580 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
582 sout_stream_sys_t *p_sys = p_stream->p_sys;
585 vlc_UrlParse( &url, psz_url, 0 );
586 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
588 if( p_sys->p_httpd_file )
590 msg_Err( p_stream, "you can use sdp=http:// only once" );
594 if( HttpSetup( p_stream, &url ) )
596 msg_Err( p_stream, "cannot export SDP as HTTP" );
599 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
601 if( p_sys->rtsp != NULL )
603 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
607 /* FIXME test if destination is multicast or no destination at all */
608 p_sys->rtsp = RtspSetup( p_stream, &url );
609 if( p_sys->rtsp == NULL )
611 msg_Err( p_stream, "cannot export SDP as RTSP" );
614 if( p_sys->p_mux != NULL )
616 sout_stream_id_t *id = p_sys->es[0];
617 id->rtsp_id = RtspAddId( p_sys->rtsp, id, 0,
618 p_sys->psz_destination, p_sys->i_ttl,
619 id->i_port, id->i_port + 1 );
622 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
623 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
625 p_sys->b_export_sap = VLC_TRUE;
626 SapSetup( p_stream );
628 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
630 if( p_sys->b_export_sdp_file )
632 msg_Err( p_stream, "you can use sdp=file:// only once" );
635 p_sys->b_export_sdp_file = VLC_TRUE;
636 psz_url = &psz_url[5];
637 if( psz_url[0] == '/' && psz_url[1] == '/' )
639 p_sys->psz_sdp_file = strdup( psz_url );
643 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
648 vlc_UrlClean( &url );
651 /*****************************************************************************
653 *****************************************************************************/
654 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
655 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
656 o= - should be local username (no spaces allowed)
657 o= time should be hashed with some other value to garantue uniqueness
658 o= we need IP6 support?
659 o= don't use the localhost address. use fully qualified domain name or IP4 address
660 p= international phone number (pass via vars?)
662 a= recvonly (missing)
663 a= type:broadcast (missing)
664 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
665 a= x-plgroup: (missing)
666 RTP packets need to get the correct src IP address */
668 char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
670 const sout_stream_sys_t *p_sys = p_stream->p_sys;
672 const char *psz_destination = p_sys->psz_destination;
673 char *psz_sdp, *p, ipv;
676 * When we have a fixed destination (typically when we do multicast),
677 * we need to put the actual port numbers in the SDP.
678 * When there is no fixed destination, we only support RTSP unicast
679 * on-demand setup, so we should rather let the clients decide which ports
681 * When there is both a fixed destination and RTSP unicast, we need to
682 * put port numbers used by the fixed destination, otherwise the SDP would
683 * become totally incorrect for multicast use. It should be noted that
684 * port numbers from SDP with RTSP are only "recommendation" from the
685 * server to the clients (per RFC2326), so only broken clients will fail
686 * to handle this properly. There is no solution but to use two differents
687 * output chain with two different RTSP URLs if you need to handle this
690 int inclport = (psz_destination != NULL);
692 /* FIXME: breaks IP version check on unknown destination */
693 if( psz_destination == NULL )
694 psz_destination = "0.0.0.0";
696 i_size = sizeof( "v=0\r\n" ) +
697 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
698 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
699 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
700 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
701 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
702 sizeof( "t=0 0\r\n" ) +
703 sizeof( "b=RR:0\r\n" ) +
704 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
705 sizeof( "a=recvonly\r\n" ) +
706 sizeof( "a=type:broadcast\r\n" ) +
707 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
708 strlen( psz_destination ) ;
709 for( i = 0; i < p_sys->i_es; i++ )
711 sout_stream_id_t *id = p_sys->es[i];
713 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
716 i_size += strlen( "b=AS: *\r\n") + 10;
720 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
724 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
726 if( rtsp_url != NULL )
728 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( rtsp_url ) + 10;
732 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
734 p = psz_sdp = malloc( i_size );
735 p += sprintf( p, "v=0\r\n" );
736 p += sprintf( p, "o=- "I64Fu" %d IN IP%c %s\r\n",
737 p_sys->i_sdp_id, p_sys->i_sdp_version,
738 ipv, ipv == '6' ? "::1" : "127.0.0.1" );
739 if( *p_sys->psz_session_name )
740 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
741 if( *p_sys->psz_session_description )
742 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
743 if( *p_sys->psz_session_url )
744 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
745 if( *p_sys->psz_session_email )
746 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
748 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */
749 /* when scheduled from vlm, we should set this info correctly */
750 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
751 p += sprintf( p, "a=recvonly\r\n" );
752 p += sprintf( p, "a=type:broadcast\r\n" );
754 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
757 && net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
759 /* Add the deprecated TTL field if it is an IPv4 multicast address */
760 p += sprintf( p, "/%d", p_sys->i_ttl ?: 1 );
762 p += sprintf( p, "\r\n" );
763 p += sprintf( p, "b=RR:0\r\n" );
765 for( i = 0; i < p_sys->i_es; i++ )
767 sout_stream_id_t *id = p_sys->es[i];
768 const char *mime_major; /* major MIME type */
770 if( id->i_cat == AUDIO_ES )
771 mime_major = "audio";
773 if( id->i_cat == VIDEO_ES )
774 mime_major = "video";
778 p += sprintf( p, "m=%s %d RTP/AVP %d\r\n", mime_major,
779 inclport * id->i_port, id->i_payload_type );
783 p += sprintf(p,"b=AS:%d\r\n",id->i_bitrate);
787 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
792 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
795 if( rtsp_url != NULL )
797 p += sprintf( p, "a=control:/trackID=%d\r\n", i );
804 /*****************************************************************************
806 *****************************************************************************/
807 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
808 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
809 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
810 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
811 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
812 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
813 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
814 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
815 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
816 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
817 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
819 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
821 static const char hex[16] = "0123456789abcdef";
824 for( i = 0; i < i_data; i++ )
826 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
827 s[2*i+1] = hex[(p_data[i] )&0xf];
833 /** Add an ES as a new RTP stream */
834 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
836 sout_stream_sys_t *p_sys = p_stream->p_sys;
837 sout_stream_id_t *id;
841 assert( p_sys->p_mux == NULL );
843 /* Choose the port */
845 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
847 i_port = p_sys->i_port_audio;
848 p_sys->i_port_audio = 0;
850 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
852 i_port = p_sys->i_port_video;
853 p_sys->i_port_video = 0;
857 if( p_sys->i_port != p_sys->i_port_audio
858 && p_sys->i_port != p_sys->i_port_video )
860 i_port = p_sys->i_port;
867 /* not create the rtp specific stuff */
868 id = malloc( sizeof( sout_stream_id_t ) );
869 memset( id, 0, sizeof( sout_stream_id_t ) );
870 id->p_stream = p_stream;
871 id->psz_rtpmap = NULL;
876 vlc_mutex_init( p_stream, &id->lock_sink );
880 if( p_sys->psz_destination != NULL )
882 int ttl = (p_sys->i_ttl > 0) ? p_sys->i_ttl : -1;
883 int fd = net_ConnectDgram( p_stream, p_sys->psz_destination,
884 p_sys->i_port, ttl, IPPROTO_UDP );
888 msg_Err( p_stream, "cannot create RTP socket" );
889 vlc_mutex_destroy( &id->lock_sink );
893 rtp_add_sink( id, fd );
896 switch( p_fmt->i_codec )
898 case VLC_FOURCC( 's', '1', '6', 'b' ):
899 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
901 id->i_payload_type = 11;
903 else if( p_fmt->audio.i_channels == 2 &&
904 p_fmt->audio.i_rate == 44100 )
906 id->i_payload_type = 10;
910 id->i_payload_type = p_sys->i_payload_type++;
912 asprintf( &id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
913 p_fmt->audio.i_channels );
914 id->i_clock_rate = p_fmt->audio.i_rate;
915 id->pf_packetize = rtp_packetize_l16;
917 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
918 id->i_payload_type = p_sys->i_payload_type++;
919 asprintf( &id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
920 p_fmt->audio.i_channels );
921 id->i_clock_rate = p_fmt->audio.i_rate;
922 id->pf_packetize = rtp_packetize_l8;
924 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
925 id->i_payload_type = 14;
926 id->i_clock_rate = 90000;
927 id->psz_rtpmap = strdup( "MPA/90000" );
928 id->pf_packetize = rtp_packetize_mpa;
930 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
931 id->i_payload_type = 32;
932 id->i_clock_rate = 90000;
933 id->psz_rtpmap = strdup( "MPV/90000" );
934 id->pf_packetize = rtp_packetize_mpv;
936 case VLC_FOURCC( 'a', '5', '2', ' ' ):
937 id->i_payload_type = p_sys->i_payload_type++;
938 id->i_clock_rate = 90000;
939 id->psz_rtpmap = strdup( "ac3/90000" );
940 id->pf_packetize = rtp_packetize_ac3;
942 case VLC_FOURCC( 'H', '2', '6', '3' ):
943 id->i_payload_type = p_sys->i_payload_type++;
944 id->i_clock_rate = 90000;
945 id->psz_rtpmap = strdup( "H263-1998/90000" );
946 id->pf_packetize = rtp_packetize_h263;
948 case VLC_FOURCC( 'h', '2', '6', '4' ):
949 id->i_payload_type = p_sys->i_payload_type++;
950 id->i_clock_rate = 90000;
951 id->psz_rtpmap = strdup( "H264/90000" );
952 id->pf_packetize = rtp_packetize_h264;
955 if( p_fmt->i_extra > 0 )
957 uint8_t *p_buffer = p_fmt->p_extra;
958 int i_buffer = p_fmt->i_extra;
959 char *p_64_sps = NULL;
960 char *p_64_pps = NULL;
963 while( i_buffer > 4 &&
964 p_buffer[0] == 0 && p_buffer[1] == 0 &&
965 p_buffer[2] == 0 && p_buffer[3] == 1 )
967 const int i_nal_type = p_buffer[4]&0x1f;
971 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
974 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
976 if( !memcmp (p_buffer + i_offset, "\x00\x00\x00\x01", 4 ) )
978 /* we found another startcode */
983 if( i_nal_type == 7 )
985 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
986 sprintf_hexa( hexa, &p_buffer[5], 3 );
988 else if( i_nal_type == 8 )
990 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
996 if( p_64_sps && p_64_pps )
997 asprintf( &id->psz_fmtp,
998 "packetization-mode=1;profile-level-id=%s;"
999 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
1007 id->psz_fmtp = strdup( "packetization-mode=1" );
1010 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1012 char hexa[2*p_fmt->i_extra +1];
1014 id->i_payload_type = p_sys->i_payload_type++;
1015 id->i_clock_rate = 90000;
1016 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1017 id->pf_packetize = rtp_packetize_split;
1018 if( p_fmt->i_extra > 0 )
1020 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1021 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1022 sprintf( id->psz_fmtp,
1023 "profile-level-id=3; config=%s;", hexa );
1027 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1029 id->i_payload_type = p_sys->i_payload_type++;
1030 id->i_clock_rate = p_fmt->audio.i_rate;
1034 char hexa[2*p_fmt->i_extra +1];
1036 asprintf( &id->psz_rtpmap, "mpeg4-generic/%d",
1037 p_fmt->audio.i_rate );
1038 id->pf_packetize = rtp_packetize_mp4a;
1039 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1040 asprintf( &id->psz_fmtp,
1041 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1042 "config=%s; SizeLength=13;IndexLength=3; "
1043 "IndexDeltaLength=3; Profile=1;", hexa );
1049 unsigned char config[6];
1050 unsigned int aacsrates[15] = {
1051 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1052 16000, 12000, 11025, 8000, 7350, 0, 0 };
1054 for( i = 0; i < 15; i++ )
1055 if( p_fmt->audio.i_rate == aacsrates[i] )
1061 config[3]=p_fmt->audio.i_channels<<4;
1065 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1066 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1067 id->pf_packetize = rtp_packetize_mp4a_latm;
1068 sprintf_hexa( hexa, config, 6 );
1069 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1070 "object=2; cpresent=0; config=%s", hexa );
1074 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1075 id->i_payload_type = p_sys->i_payload_type++;
1076 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1077 "AMR/8000/2" : "AMR/8000" );
1078 id->psz_fmtp = strdup( "octet-align=1" );
1079 id->i_clock_rate = p_fmt->audio.i_rate;
1080 id->pf_packetize = rtp_packetize_amr;
1082 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1083 id->i_payload_type = p_sys->i_payload_type++;
1084 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1085 "AMR-WB/16000/2" : "AMR-WB/16000" );
1086 id->psz_fmtp = strdup( "octet-align=1" );
1087 id->i_clock_rate = p_fmt->audio.i_rate;
1088 id->pf_packetize = rtp_packetize_amr;
1092 msg_Err( p_stream, "cannot add this stream (unsupported "
1093 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1095 rtp_del_sink( id, id->fdv[0] );
1096 vlc_mutex_destroy( &id->lock_sink );
1100 id->i_cat = p_fmt->i_cat;
1102 id->ssrc[0] = rand()&0xff;
1103 id->ssrc[1] = rand()&0xff;
1104 id->ssrc[2] = rand()&0xff;
1105 id->ssrc[3] = rand()&0xff;
1106 id->i_sequence = rand()&0xffff;
1107 id->i_timestamp_start = rand()&0xffffffff;
1108 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
1110 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1111 if( id->i_mtu <= 12 + 16 )
1113 /* better than nothing */
1114 id->i_mtu = 576 - 20 - 8;
1116 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1118 if( p_sys->rtsp != NULL )
1119 id->rtsp_id = RtspAddId( p_sys->rtsp, id, p_sys->i_es,
1120 p_sys->psz_destination,
1121 p_sys->i_ttl, id->i_port, id->i_port + 1 );
1123 /* Update p_sys context */
1124 vlc_mutex_lock( &p_sys->lock_es );
1125 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1126 vlc_mutex_unlock( &p_sys->lock_es );
1128 psz_sdp = SDPGenerate( p_stream, NULL );
1130 vlc_mutex_lock( &p_sys->lock_sdp );
1131 free( p_sys->psz_sdp );
1132 p_sys->psz_sdp = psz_sdp;
1133 vlc_mutex_unlock( &p_sys->lock_sdp );
1135 p_sys->i_sdp_version++;
1137 msg_Dbg( p_stream, "sdp=\n%s", p_sys->psz_sdp );
1139 /* Update SDP (sap/file) */
1140 if( p_sys->b_export_sap ) SapSetup( p_stream );
1141 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1146 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1148 sout_stream_sys_t *p_sys = p_stream->p_sys;
1150 vlc_mutex_lock( &p_sys->lock_es );
1151 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1152 vlc_mutex_unlock( &p_sys->lock_es );
1155 if( id->i_port > 0 )
1157 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1158 p_sys->i_port_audio = id->i_port;
1159 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1160 p_sys->i_port_video = id->i_port;
1163 free( id->psz_rtpmap );
1164 free( id->psz_fmtp );
1167 RtspDelId( p_sys->rtsp, id->rtsp_id );
1169 rtp_del_sink( id, id->fdv[0] ); /* sink for explicit dst= */
1171 vlc_mutex_destroy( &id->lock_sink );
1173 /* Update SDP (sap/file) */
1174 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1175 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1181 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1186 assert( p_stream->p_sys->p_mux == NULL );
1188 while( p_buffer != NULL )
1190 p_next = p_buffer->p_next;
1191 if( id->pf_packetize( p_stream, id, p_buffer ) )
1194 block_Release( p_buffer );
1200 /****************************************************************************
1202 ****************************************************************************/
1203 static int SapSetup( sout_stream_t *p_stream )
1205 sout_stream_sys_t *p_sys = p_stream->p_sys;
1206 sout_instance_t *p_sout = p_stream->p_sout;
1207 announce_method_t *p_method = sout_SAPMethod();
1209 /* Remove the previous session */
1210 if( p_sys->p_session != NULL)
1212 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1213 sout_AnnounceSessionDestroy( p_sys->p_session );
1214 p_sys->p_session = NULL;
1217 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1219 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
1221 p_sys->psz_destination,
1225 sout_MethodRelease( p_method );
1229 /****************************************************************************
1231 ****************************************************************************/
1232 static int FileSetup( sout_stream_t *p_stream )
1234 sout_stream_sys_t *p_sys = p_stream->p_sys;
1237 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1239 msg_Err( p_stream, "cannot open file '%s' (%s)",
1240 p_sys->psz_sdp_file, strerror(errno) );
1241 return VLC_EGENERIC;
1244 fputs( p_sys->psz_sdp, f );
1250 /****************************************************************************
1252 ****************************************************************************/
1253 static int HttpCallback( httpd_file_sys_t *p_args,
1254 httpd_file_t *, uint8_t *p_request,
1255 uint8_t **pp_data, int *pi_data );
1257 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1259 sout_stream_sys_t *p_sys = p_stream->p_sys;
1261 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
1262 url->i_port > 0 ? url->i_port : 80 );
1263 if( p_sys->p_httpd_host )
1265 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1266 url->psz_path ? url->psz_path : "/",
1269 HttpCallback, (void*)p_sys );
1271 if( p_sys->p_httpd_file == NULL )
1273 return VLC_EGENERIC;
1278 static int HttpCallback( httpd_file_sys_t *p_args,
1279 httpd_file_t *f, uint8_t *p_request,
1280 uint8_t **pp_data, int *pi_data )
1282 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1284 vlc_mutex_lock( &p_sys->lock_sdp );
1285 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1287 *pi_data = strlen( p_sys->psz_sdp );
1288 *pp_data = malloc( *pi_data );
1289 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1296 vlc_mutex_unlock( &p_sys->lock_sdp );
1301 /****************************************************************************
1303 ****************************************************************************/
1304 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1305 int b_marker, int64_t i_pts )
1307 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1309 out->p_buffer[0] = 0x80;
1310 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1311 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1312 out->p_buffer[3] = ( id->i_sequence )&0xff;
1313 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1314 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1315 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1316 out->p_buffer[7] = ( i_timestamp )&0xff;
1318 memcpy( out->p_buffer + 8, id->ssrc, 4 );
1324 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1327 vlc_mutex_lock( &id->lock_sink );
1328 for( i = 0; i < id->fdc; i++ )
1330 send( id->fdv[i], out->p_buffer, out->i_buffer, 0 );
1332 vlc_mutex_unlock( &id->lock_sink );
1334 block_Release( out );
1337 int rtp_add_sink( sout_stream_id_t *id, int fd )
1339 vlc_mutex_lock( &id->lock_sink );
1340 TAB_APPEND( id->fdc, id->fdv, fd );
1341 vlc_mutex_unlock( &id->lock_sink );
1345 void rtp_del_sink( sout_stream_id_t *id, int fd )
1347 /* NOTE: must be safe to use if fd is not included */
1348 vlc_mutex_lock( &id->lock_sink );
1349 TAB_REMOVE( id->fdc, id->fdv, fd );
1350 vlc_mutex_unlock( &id->lock_sink );
1354 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1357 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1358 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1360 uint8_t *p_data = in->p_buffer;
1361 int i_data = in->i_buffer;
1364 for( i = 0; i < i_count; i++ )
1366 int i_payload = __MIN( i_max, i_data );
1367 block_t *out = block_New( p_stream, 16 + i_payload );
1369 /* rtp common header */
1370 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1372 out->p_buffer[12] = 0;
1373 out->p_buffer[13] = 0;
1374 /* fragment offset in the current frame */
1375 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1376 out->p_buffer[15] = ( (i*i_max) )&0xff;
1377 memcpy( &out->p_buffer[16], p_data, i_payload );
1379 out->i_buffer = 16 + i_payload;
1380 out->i_dts = in->i_dts + i * in->i_length / i_count;
1381 out->i_length = in->i_length / i_count;
1383 rtp_packetize_send( id, out );
1385 p_data += i_payload;
1386 i_data -= i_payload;
1393 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1396 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1397 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1399 uint8_t *p_data = in->p_buffer;
1400 int i_data = in->i_buffer;
1402 int b_sequence_start = 0;
1403 int i_temporal_ref = 0;
1404 int i_picture_coding_type = 0;
1405 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1406 int b_start_slice = 0;
1408 /* preparse this packet to get some info */
1409 if( in->i_buffer > 4 )
1411 uint8_t *p = p_data;
1412 int i_rest = in->i_buffer;
1416 while( i_rest > 4 &&
1417 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1431 /* sequence start code */
1432 b_sequence_start = 1;
1434 else if( *p == 0x00 && i_rest >= 4 )
1437 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1438 i_picture_coding_type = (p[2] >> 3)&0x07;
1440 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1441 i_picture_coding_type == 3 ) )
1443 i_ffv = (p[3] >> 2)&0x01;
1444 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1445 if( i_rest > 4 && i_picture_coding_type == 3 )
1447 i_fbv = (p[4]>>6)&0x01;
1448 i_bfc = (p[4]>>3)&0x07;
1452 else if( *p <= 0xaf )
1459 for( i = 0; i < i_count; i++ )
1461 int i_payload = __MIN( i_max, i_data );
1462 block_t *out = block_New( p_stream,
1464 uint32_t h = ( i_temporal_ref << 16 )|
1465 ( b_sequence_start << 13 )|
1466 ( b_start_slice << 12 )|
1467 ( i == i_count - 1 ? 1 << 11 : 0 )|
1468 ( i_picture_coding_type << 8 )|
1469 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1471 /* rtp common header */
1472 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1473 in->i_pts > 0 ? in->i_pts : in->i_dts );
1475 /* 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 */
1476 out->p_buffer[12] = ( h >> 24 )&0xff;
1477 out->p_buffer[13] = ( h >> 16 )&0xff;
1478 out->p_buffer[14] = ( h >> 8 )&0xff;
1479 out->p_buffer[15] = ( h )&0xff;
1481 memcpy( &out->p_buffer[16], p_data, i_payload );
1483 out->i_buffer = 16 + i_payload;
1484 out->i_dts = in->i_dts + i * in->i_length / i_count;
1485 out->i_length = in->i_length / i_count;
1487 rtp_packetize_send( id, out );
1489 p_data += i_payload;
1490 i_data -= i_payload;
1496 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1499 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1500 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1502 uint8_t *p_data = in->p_buffer;
1503 int i_data = in->i_buffer;
1506 for( i = 0; i < i_count; i++ )
1508 int i_payload = __MIN( i_max, i_data );
1509 block_t *out = block_New( p_stream, 14 + i_payload );
1511 /* rtp common header */
1512 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1514 out->p_buffer[12] = 1;
1516 out->p_buffer[13] = 0x00;
1518 memcpy( &out->p_buffer[14], p_data, i_payload );
1520 out->i_buffer = 14 + i_payload;
1521 out->i_dts = in->i_dts + i * in->i_length / i_count;
1522 out->i_length = in->i_length / i_count;
1524 rtp_packetize_send( id, out );
1526 p_data += i_payload;
1527 i_data -= i_payload;
1533 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1536 int i_max = id->i_mtu - 12; /* payload max in one packet */
1537 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1539 uint8_t *p_data = in->p_buffer;
1540 int i_data = in->i_buffer;
1543 for( i = 0; i < i_count; i++ )
1545 int i_payload = __MIN( i_max, i_data );
1546 block_t *out = block_New( p_stream, 12 + i_payload );
1548 /* rtp common header */
1549 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1550 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1551 memcpy( &out->p_buffer[12], p_data, i_payload );
1553 out->i_buffer = 12 + i_payload;
1554 out->i_dts = in->i_dts + i * in->i_length / i_count;
1555 out->i_length = in->i_length / i_count;
1557 rtp_packetize_send( id, out );
1559 p_data += i_payload;
1560 i_data -= i_payload;
1567 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
1570 int i_max = id->i_mtu - 14; /* payload max in one packet */
1571 int latmhdrsize = in->i_buffer / 0xff + 1;
1572 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1574 uint8_t *p_data = in->p_buffer, *p_header = NULL;
1575 int i_data = in->i_buffer;
1578 for( i = 0; i < i_count; i++ )
1580 int i_payload = __MIN( i_max, i_data );
1585 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
1587 /* rtp common header */
1588 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
1589 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1593 int tmp = in->i_buffer;
1595 p_header=out->p_buffer+12;
1605 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
1607 out->i_buffer = 12 + latmhdrsize + i_payload;
1608 out->i_dts = in->i_dts + i * in->i_length / i_count;
1609 out->i_length = in->i_length / i_count;
1611 rtp_packetize_send( id, out );
1613 p_data += i_payload;
1614 i_data -= i_payload;
1620 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1623 int i_max = id->i_mtu - 12; /* payload max in one packet */
1624 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1626 uint8_t *p_data = in->p_buffer;
1627 int i_data = in->i_buffer;
1632 int i_payload = (__MIN( i_max, i_data )/4)*4;
1633 block_t *out = block_New( p_stream, 12 + i_payload );
1635 /* rtp common header */
1636 rtp_packetize_common( id, out, 0,
1637 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1638 memcpy( &out->p_buffer[12], p_data, i_payload );
1640 out->i_buffer = 12 + i_payload;
1641 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1642 out->i_length = in->i_length / i_count;
1644 rtp_packetize_send( id, out );
1646 p_data += i_payload;
1647 i_data -= i_payload;
1654 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1657 int i_max = id->i_mtu - 12; /* payload max in one packet */
1658 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1660 uint8_t *p_data = in->p_buffer;
1661 int i_data = in->i_buffer;
1666 int i_payload = (__MIN( i_max, i_data )/2)*2;
1667 block_t *out = block_New( p_stream, 12 + i_payload );
1669 /* rtp common header */
1670 rtp_packetize_common( id, out, 0,
1671 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1672 memcpy( &out->p_buffer[12], p_data, i_payload );
1674 out->i_buffer = 12 + i_payload;
1675 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1676 out->i_length = in->i_length / i_count;
1678 rtp_packetize_send( id, out );
1680 p_data += i_payload;
1681 i_data -= i_payload;
1688 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1691 int i_max = id->i_mtu - 16; /* payload max in one packet */
1692 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1694 uint8_t *p_data = in->p_buffer;
1695 int i_data = in->i_buffer;
1698 for( i = 0; i < i_count; i++ )
1700 int i_payload = __MIN( i_max, i_data );
1701 block_t *out = block_New( p_stream, 16 + i_payload );
1703 /* rtp common header */
1704 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1705 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1707 /* AU headers length (bits) */
1708 out->p_buffer[12] = 0;
1709 out->p_buffer[13] = 2*8;
1710 /* for each AU length 13 bits + idx 3bits, */
1711 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1712 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1714 memcpy( &out->p_buffer[16], p_data, i_payload );
1716 out->i_buffer = 16 + i_payload;
1717 out->i_dts = in->i_dts + i * in->i_length / i_count;
1718 out->i_length = in->i_length / i_count;
1720 rtp_packetize_send( id, out );
1722 p_data += i_payload;
1723 i_data -= i_payload;
1731 #define RTP_H263_HEADER_SIZE (2) // plen = 0
1732 #define RTP_H263_PAYLOAD_START (14) // plen = 0
1733 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
1736 uint8_t *p_data = in->p_buffer;
1737 int i_data = in->i_buffer;
1739 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
1742 int b_v_bit = 0; // no pesky error resilience
1743 int i_plen = 0; // normally plen=0 for PSC packet
1744 int i_pebit = 0; // because plen=0
1749 return VLC_EGENERIC;
1751 if( p_data[0] || p_data[1] )
1753 return VLC_EGENERIC;
1755 /* remove 2 leading 0 bytes */
1758 i_count = ( i_data + i_max - 1 ) / i_max;
1760 for( i = 0; i < i_count; i++ )
1762 int i_payload = __MIN( i_max, i_data );
1763 block_t *out = block_New( p_stream,
1764 RTP_H263_PAYLOAD_START + i_payload );
1765 b_p_bit = (i == 0) ? 1 : 0;
1766 h = ( b_p_bit << 10 )|
1771 /* rtp common header */
1772 //b_m_bit = 1; // always contains end of frame
1773 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1774 in->i_pts > 0 ? in->i_pts : in->i_dts );
1777 out->p_buffer[12] = ( h >> 8 )&0xff;
1778 out->p_buffer[13] = ( h )&0xff;
1779 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1781 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1782 out->i_dts = in->i_dts + i * 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;
1796 rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
1797 const uint8_t *p_data, int i_data, int64_t i_pts,
1798 int64_t i_dts, vlc_bool_t b_last, int64_t i_length )
1800 const int i_max = id->i_mtu - 12; /* payload max in one packet */
1807 i_nal_hdr = p_data[3];
1808 i_nal_type = i_nal_hdr&0x1f;
1809 if( i_nal_type == 7 || i_nal_type == 8 )
1811 /* XXX Why do you want to remove them ? It will break streaming with
1812 * SPS/PPS change (broadcast) ? */
1816 /* Skip start code */
1821 if( i_data <= i_max )
1823 /* Single NAL unit packet */
1824 block_t *out = block_New( p_stream, 12 + i_data );
1826 out->i_length = i_length;
1829 rtp_packetize_common( id, out, b_last, i_pts );
1830 out->i_buffer = 12 + i_data;
1832 memcpy( &out->p_buffer[12], p_data, i_data );
1834 rtp_packetize_send( id, out );
1838 /* FU-A Fragmentation Unit without interleaving */
1839 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1845 for( i = 0; i < i_count; i++ )
1847 const int i_payload = __MIN( i_data, i_max-2 );
1848 block_t *out = block_New( p_stream, 12 + 2 + i_payload );
1849 out->i_dts = i_dts + i * i_length / i_count;
1850 out->i_length = i_length / i_count;
1853 rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
1854 out->i_buffer = 14 + i_payload;
1857 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1859 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
1860 memcpy( &out->p_buffer[14], p_data, i_payload );
1862 rtp_packetize_send( id, out );
1864 i_data -= i_payload;
1865 p_data += i_payload;
1871 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
1874 const uint8_t *p_buffer = in->p_buffer;
1875 int i_buffer = in->i_buffer;
1877 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1883 /* Split nal units */
1884 while( i_buffer > 4 )
1887 int i_size = i_buffer;
1888 int i_skip = i_buffer;
1890 /* search nal end */
1891 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1893 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1895 /* we found another startcode */
1896 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
1901 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
1902 rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
1903 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
1904 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
1912 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
1915 int i_max = id->i_mtu - 14; /* payload max in one packet */
1916 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1918 uint8_t *p_data = in->p_buffer;
1919 int i_data = in->i_buffer;
1922 /* Only supports octet-aligned mode */
1923 for( i = 0; i < i_count; i++ )
1925 int i_payload = __MIN( i_max, i_data );
1926 block_t *out = block_New( p_stream, 14 + i_payload );
1928 /* rtp common header */
1929 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1930 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1931 /* Payload header */
1932 out->p_buffer[12] = 0xF0; /* CMR */
1933 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
1935 /* FIXME: are we fed multiple frames ? */
1936 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
1938 out->i_buffer = 14 + i_payload-1;
1939 out->i_dts = in->i_dts + i * in->i_length / i_count;
1940 out->i_length = in->i_length / i_count;
1942 rtp_packetize_send( id, out );
1944 p_data += i_payload;
1945 i_data -= i_payload;
1951 /*****************************************************************************
1953 *****************************************************************************/
1955 /** Add an ES to a non-RTP muxed stream */
1956 static sout_stream_id_t *MuxAdd( sout_stream_t *p_stream, es_format_t *p_fmt )
1958 sout_input_t *p_input;
1959 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
1960 assert( p_mux != NULL );
1962 p_input = sout_MuxAddStream( p_mux, p_fmt );
1963 if( p_input == NULL )
1965 msg_Err( p_stream, "cannot add this stream to the muxer" );
1969 return (sout_stream_id_t *)p_input;
1973 static int MuxSend( sout_stream_t *p_stream, sout_stream_id_t *id,
1976 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
1977 assert( p_mux != NULL );
1979 sout_MuxSendBuffer( p_mux, (sout_input_t *)id, p_buffer );
1984 /** Remove an ES from a non-RTP muxed stream */
1985 static int MuxDel( sout_stream_t *p_stream, sout_stream_id_t *id )
1987 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
1988 assert( p_mux != NULL );
1990 sout_MuxDeleteStream( p_mux, (sout_input_t *)id );
1995 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1996 const block_t *p_buffer )
1998 sout_stream_sys_t *p_sys = p_stream->p_sys;
1999 sout_stream_id_t *id = p_sys->es[0];
2001 int64_t i_dts = p_buffer->i_dts;
2003 uint8_t *p_data = p_buffer->p_buffer;
2004 unsigned int i_data = p_buffer->i_buffer;
2005 unsigned int i_max = id->i_mtu - 12;
2007 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
2011 unsigned int i_size;
2013 /* output complete packet */
2014 if( p_sys->packet &&
2015 p_sys->packet->i_buffer + i_data > i_max )
2017 rtp_packetize_send( id, p_sys->packet );
2018 p_sys->packet = NULL;
2021 if( p_sys->packet == NULL )
2023 /* allocate a new packet */
2024 p_sys->packet = block_New( p_stream, id->i_mtu );
2025 rtp_packetize_common( id, p_sys->packet, 1, i_dts );
2026 p_sys->packet->i_dts = i_dts;
2027 p_sys->packet->i_length = p_buffer->i_length / i_packet;
2028 i_dts += p_sys->packet->i_length;
2031 i_size = __MIN( i_data,
2032 (unsigned)(id->i_mtu - p_sys->packet->i_buffer) );
2034 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
2037 p_sys->packet->i_buffer += i_size;
2046 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
2049 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
2055 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
2057 p_next = p_buffer->p_next;
2058 block_Release( p_buffer );
2066 static sout_access_out_t *GrabberCreate( sout_stream_t *p_stream )
2068 sout_access_out_t *p_grab;
2070 p_grab = vlc_object_create( p_stream->p_sout, sizeof( *p_grab ) );
2071 if( p_grab == NULL )
2074 p_grab->p_module = NULL;
2075 p_grab->p_sout = p_stream->p_sout;
2076 p_grab->psz_access = strdup( "grab" );
2077 p_grab->p_cfg = NULL;
2078 p_grab->psz_path = strdup( "" );
2079 p_grab->p_sys = (sout_access_out_sys_t *)p_stream;
2080 p_grab->pf_seek = NULL;
2081 p_grab->pf_write = AccessOutGrabberWrite;