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>
42 # include <sys/types.h>
45 # include <sys/stat.h>
50 /*****************************************************************************
52 *****************************************************************************/
54 #define DST_TEXT N_("Destination")
55 #define DST_LONGTEXT N_( \
56 "This is the output URL that will be used." )
57 #define SDP_TEXT N_("SDP")
58 #define SDP_LONGTEXT N_( \
59 "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
60 "session will be made available. You must use an url: http://location to " \
61 "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
62 "for the SDP to be announced via SAP." )
63 #define MUX_TEXT N_("Muxer")
64 #define MUX_LONGTEXT N_( \
65 "This allows you to specify the muxer used for the streaming output. " \
66 "Default is to use no muxer (standard RTP stream)." )
68 #define NAME_TEXT N_("Session name")
69 #define NAME_LONGTEXT N_( \
70 "This is the name of the session that will be announced in the SDP " \
71 "(Session Descriptor)." )
72 #define DESC_TEXT N_("Session description")
73 #define DESC_LONGTEXT N_( \
74 "This allows you to give a broader description of the stream, that will " \
75 "be announced in the SDP (Session Descriptor)." )
76 #define URL_TEXT N_("Session URL")
77 #define URL_LONGTEXT N_( \
78 "This allows you to give an URL with more details about the stream " \
79 "(often the website of the streaming organization), that will " \
80 "be announced in the SDP (Session Descriptor)." )
81 #define EMAIL_TEXT N_("Session email")
82 #define EMAIL_LONGTEXT N_( \
83 "This allows you to give a contact mail address for the stream, that will " \
84 "be announced in the SDP (Session Descriptor)." )
85 #define PORT_TEXT N_("Port")
86 #define PORT_LONGTEXT N_( \
87 "This allows you to specify the base port for the RTP streaming." )
88 #define PORT_AUDIO_TEXT N_("Audio port")
89 #define PORT_AUDIO_LONGTEXT N_( \
90 "This allows you to specify the default audio port for the RTP streaming." )
91 #define PORT_VIDEO_TEXT N_("Video port")
92 #define PORT_VIDEO_LONGTEXT N_( \
93 "This allows you to specify the default video port for the RTP streaming." )
95 #define TTL_TEXT N_("Hop limit (TTL)")
96 #define TTL_LONGTEXT N_( \
97 "This is the hop limit (also known as \"Time-To-Live\" or TTL) of " \
98 "the multicast packets sent by the stream output (0 = use operating " \
99 "system built-in default).")
101 #define DCCP_TEXT N_("DCCP transport")
102 #define DCCP_LONGTEXT N_( \
103 "This enables DCCP instead of UDP as a transport for RTP." )
104 #define TCP_TEXT N_("TCP transport")
105 #define TCP_LONGTEXT N_( \
106 "This enables TCP instead of UDP as a transport for RTP." )
107 #define UDP_LITE_TEXT N_("UDP-Lite transport")
108 #define UDP_LITE_LONGTEXT N_( \
109 "This enables UDP-Lite instead of UDP as a transport for RTP." )
111 #define RFC3016_TEXT N_("MP4A LATM")
112 #define RFC3016_LONGTEXT N_( \
113 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
115 static int Open ( vlc_object_t * );
116 static void Close( vlc_object_t * );
118 #define SOUT_CFG_PREFIX "sout-rtp-"
119 #define MAX_EMPTY_BLOCKS 200
122 set_shortname( _("RTP"));
123 set_description( _("RTP stream output") );
124 set_capability( "sout stream", 0 );
125 add_shortcut( "rtp" );
126 set_category( CAT_SOUT );
127 set_subcategory( SUBCAT_SOUT_STREAM );
129 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
130 DST_LONGTEXT, VLC_TRUE );
131 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
132 SDP_LONGTEXT, VLC_TRUE );
133 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
134 MUX_LONGTEXT, VLC_TRUE );
136 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
137 NAME_LONGTEXT, VLC_TRUE );
138 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
139 DESC_LONGTEXT, VLC_TRUE );
140 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
141 URL_LONGTEXT, VLC_TRUE );
142 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
143 EMAIL_LONGTEXT, VLC_TRUE );
145 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
146 PORT_LONGTEXT, VLC_TRUE );
147 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
148 PORT_AUDIO_LONGTEXT, VLC_TRUE );
149 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
150 PORT_VIDEO_LONGTEXT, VLC_TRUE );
152 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
153 TTL_LONGTEXT, VLC_TRUE );
155 add_bool( SOUT_CFG_PREFIX "dccp", 0, NULL,
156 DCCP_TEXT, DCCP_LONGTEXT, VLC_FALSE );
157 add_bool( SOUT_CFG_PREFIX "tcp", 0, NULL,
158 TCP_TEXT, TCP_LONGTEXT, VLC_FALSE );
159 add_bool( SOUT_CFG_PREFIX "udplite", 0, NULL,
160 UDP_LITE_TEXT, UDP_LITE_LONGTEXT, VLC_FALSE );
162 add_bool( SOUT_CFG_PREFIX "mp4a-latm", 0, NULL, RFC3016_TEXT,
163 RFC3016_LONGTEXT, VLC_FALSE );
165 set_callbacks( Open, Close );
168 /*****************************************************************************
169 * Exported prototypes
170 *****************************************************************************/
171 static const char *ppsz_sout_options[] = {
172 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
173 "description", "url", "email",
174 "dccp", "tcp", "udplite",
178 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
179 static int Del ( sout_stream_t *, sout_stream_id_t * );
180 static int Send( sout_stream_t *, sout_stream_id_t *,
182 static sout_stream_id_t *MuxAdd ( sout_stream_t *, es_format_t * );
183 static int MuxDel ( sout_stream_t *, sout_stream_id_t * );
184 static int MuxSend( sout_stream_t *, sout_stream_id_t *,
187 static sout_access_out_t *GrabberCreate( sout_stream_t *p_sout );
188 static void ThreadSend( vlc_object_t *p_this );
190 static void SDPHandleUrl( sout_stream_t *, char * );
192 static int SapSetup( sout_stream_t *p_stream );
193 static int FileSetup( sout_stream_t *p_stream );
194 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
196 struct sout_stream_sys_t
202 vlc_mutex_t lock_sdp;
204 char *psz_session_name;
205 char *psz_session_description;
206 char *psz_session_url;
207 char *psz_session_email;
210 vlc_bool_t b_export_sdp_file;
214 vlc_bool_t b_export_sap;
215 session_descriptor_t *p_session;
218 httpd_host_t *p_httpd_host;
219 httpd_file_t *p_httpd_file;
225 char *psz_destination;
229 uint16_t i_port_audio;
230 uint16_t i_port_video;
233 /* when need to use a private one or when using muxer */
236 /* in case we do TS/PS over rtp */
238 sout_access_out_t *p_grab;
244 sout_stream_id_t **es;
247 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
250 typedef struct rtp_sink_t
256 struct sout_stream_id_t
260 sout_stream_t *p_stream;
262 uint32_t i_timestamp_start;
264 uint8_t i_payload_type;
275 /* Packetizer specific fields */
276 pf_rtp_packetizer_t pf_packetize;
280 vlc_mutex_t lock_sink;
283 rtsp_stream_id_t *rtsp_id;
285 block_fifo_t *p_fifo;
290 /*****************************************************************************
292 *****************************************************************************/
293 static int Open( vlc_object_t *p_this )
295 sout_stream_t *p_stream = (sout_stream_t*)p_this;
296 sout_instance_t *p_sout = p_stream->p_sout;
297 sout_stream_sys_t *p_sys = NULL;
298 config_chain_t *p_cfg = NULL;
300 vlc_bool_t b_rtsp = VLC_FALSE;
302 config_ChainParse( p_stream, SOUT_CFG_PREFIX,
303 ppsz_sout_options, p_stream->p_cfg );
305 p_sys = malloc( sizeof( sout_stream_sys_t ) );
309 p_sys->psz_destination = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
310 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
311 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
312 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
313 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
315 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
316 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
317 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
319 p_sys->psz_sdp_file = NULL;
321 if( p_sys->i_port_audio == p_sys->i_port_video )
323 msg_Err( p_stream, "audio and video port cannot be the same" );
324 p_sys->i_port_audio = 0;
325 p_sys->i_port_video = 0;
328 if( !p_sys->psz_session_name )
330 if( p_sys->psz_destination )
331 p_sys->psz_session_name = strdup( p_sys->psz_destination );
333 p_sys->psz_session_name = strdup( "NONE" );
336 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
338 if( !strcmp( p_cfg->psz_name, "sdp" )
339 && ( p_cfg->psz_value != NULL )
340 && !strncasecmp( p_cfg->psz_value, "rtsp:", 5 ) )
348 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
351 if( !strncasecmp( psz, "rtsp:", 5 ) )
357 /* Transport protocol */
358 p_sys->proto = IPPROTO_UDP;
361 if( var_GetBool( p_stream, SOUT_CFG_PREFIX "dccp" ) )
363 p_sys->sotype = SOCK_DCCP;
364 p_sys->proto = 33 /*IPPROTO_DCCP*/;
367 if( var_GetBool( p_stream, SOUT_CFG_PREFIX "tcp" ) )
369 p_sys->sotype = SOCK_STREAM;
370 p_sys->proto = IPPROTO_TCP;
374 if( var_GetBool( p_stream, SOUT_CFG_PREFIX "udplite" ) )
375 p_sys->proto = 136 /*IPPROTO_UDPLITE*/;
377 if( ( p_sys->psz_destination == NULL ) && !b_rtsp )
379 msg_Err( p_stream, "missing destination and not in RTSP mode" );
384 p_sys->i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
385 if( p_sys->i_ttl == 0 )
387 /* Normally, we should let the default hop limit up to the core,
388 * but we have to know it to build our SDP properly, which is why
389 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
391 p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
394 p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
396 p_sys->i_payload_type = 96;
400 p_sys->psz_sdp = NULL;
402 p_sys->i_sdp_id = mdate();
403 p_sys->i_sdp_version = 1;
404 p_sys->psz_sdp = NULL;
406 p_sys->b_export_sap = VLC_FALSE;
407 p_sys->b_export_sdp_file = VLC_FALSE;
408 p_sys->p_session = NULL;
410 p_sys->p_httpd_host = NULL;
411 p_sys->p_httpd_file = NULL;
413 p_stream->p_sys = p_sys;
415 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
416 vlc_mutex_init( p_stream, &p_sys->lock_es );
418 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
421 sout_stream_id_t *id;
422 const char *psz_rtpmap;
425 /* Check muxer type */
426 if( !strncasecmp( psz, "ps", 2 )
427 || !strncasecmp( psz, "mpeg1", 5 ) )
429 psz_rtpmap = "MP2P/90000";
431 else if( !strncasecmp( psz, "ts", 2 ) )
433 psz_rtpmap = "MP2T/90000";
438 msg_Err( p_stream, "unsupported muxer type for RTP (only TS/PS)" );
440 vlc_mutex_destroy( &p_sys->lock_sdp );
441 vlc_mutex_destroy( &p_sys->lock_es );
446 p_sys->p_grab = GrabberCreate( p_stream );
447 p_sys->p_mux = sout_MuxNew( p_sout, psz, p_sys->p_grab );
450 if( p_sys->p_mux == NULL )
452 msg_Err( p_stream, "cannot create muxer" );
453 sout_AccessOutDelete( p_sys->p_grab );
454 vlc_mutex_destroy( &p_sys->lock_sdp );
455 vlc_mutex_destroy( &p_sys->lock_es );
460 id = Add( p_stream, NULL );
463 sout_MuxDelete( p_sys->p_mux );
464 sout_AccessOutDelete( p_sys->p_grab );
465 vlc_mutex_destroy( &p_sys->lock_sdp );
466 vlc_mutex_destroy( &p_sys->lock_es );
471 id->psz_rtpmap = strdup( psz_rtpmap );
472 id->i_payload_type = i_payload_type;
474 p_sys->packet = NULL;
476 p_stream->pf_add = MuxAdd;
477 p_stream->pf_del = MuxDel;
478 p_stream->pf_send = MuxSend;
483 p_sys->p_grab = NULL;
485 p_stream->pf_add = Add;
486 p_stream->pf_del = Del;
487 p_stream->pf_send = Send;
490 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
493 config_chain_t *p_cfg;
495 SDPHandleUrl( p_stream, psz );
497 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
499 if( !strcmp( p_cfg->psz_name, "sdp" ) )
501 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
504 /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
505 if( !strcmp( p_cfg->psz_value, psz ) )
508 SDPHandleUrl( p_stream, p_cfg->psz_value );
514 /* update p_sout->i_out_pace_nocontrol */
515 p_stream->p_sout->i_out_pace_nocontrol++;
520 /*****************************************************************************
522 *****************************************************************************/
523 static void Close( vlc_object_t * p_this )
525 sout_stream_t *p_stream = (sout_stream_t*)p_this;
526 sout_stream_sys_t *p_sys = p_stream->p_sys;
528 /* update p_sout->i_out_pace_nocontrol */
529 p_stream->p_sout->i_out_pace_nocontrol--;
533 assert( p_sys->i_es == 1 );
534 Del( p_stream, p_sys->es[0] );
536 sout_MuxDelete( p_sys->p_mux );
537 sout_AccessOutDelete( p_sys->p_grab );
540 block_Release( p_sys->packet );
542 if( p_sys->b_export_sap )
545 SapSetup( p_stream );
549 if( p_sys->rtsp != NULL )
550 RtspUnsetup( p_sys->rtsp );
552 vlc_mutex_destroy( &p_sys->lock_sdp );
553 vlc_mutex_destroy( &p_sys->lock_es );
555 if( p_sys->p_httpd_file )
556 httpd_FileDelete( p_sys->p_httpd_file );
558 if( p_sys->p_httpd_host )
559 httpd_HostDelete( p_sys->p_httpd_host );
561 free( p_sys->psz_session_name );
562 free( p_sys->psz_session_description );
563 free( p_sys->psz_session_url );
564 free( p_sys->psz_session_email );
565 free( p_sys->psz_sdp );
567 if( p_sys->b_export_sdp_file )
570 unlink( p_sys->psz_sdp_file );
572 free( p_sys->psz_sdp_file );
574 free( p_sys->psz_destination );
578 /*****************************************************************************
580 *****************************************************************************/
581 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
583 sout_stream_sys_t *p_sys = p_stream->p_sys;
586 vlc_UrlParse( &url, psz_url, 0 );
587 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
589 if( p_sys->p_httpd_file )
591 msg_Err( p_stream, "you can use sdp=http:// only once" );
595 if( HttpSetup( p_stream, &url ) )
597 msg_Err( p_stream, "cannot export SDP as HTTP" );
600 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
602 if( p_sys->rtsp != NULL )
604 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
608 /* FIXME test if destination is multicast or no destination at all */
609 p_sys->rtsp = RtspSetup( p_stream, &url );
610 if( p_sys->rtsp == NULL )
612 msg_Err( p_stream, "cannot export SDP as RTSP" );
615 if( p_sys->p_mux != NULL )
617 sout_stream_id_t *id = p_sys->es[0];
618 id->rtsp_id = RtspAddId( p_sys->rtsp, id, 0,
619 p_sys->psz_destination, p_sys->i_ttl,
620 id->i_port, id->i_port + 1 );
623 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
624 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
626 p_sys->b_export_sap = VLC_TRUE;
627 SapSetup( p_stream );
629 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
631 if( p_sys->b_export_sdp_file )
633 msg_Err( p_stream, "you can use sdp=file:// only once" );
636 p_sys->b_export_sdp_file = VLC_TRUE;
637 psz_url = &psz_url[5];
638 if( psz_url[0] == '/' && psz_url[1] == '/' )
640 p_sys->psz_sdp_file = strdup( psz_url );
644 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
649 vlc_UrlClean( &url );
652 /*****************************************************************************
654 *****************************************************************************/
655 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
656 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
657 o= - should be local username (no spaces allowed)
658 o= time should be hashed with some other value to garantue uniqueness
659 o= we need IP6 support?
660 o= don't use the localhost address. use fully qualified domain name or IP4 address
661 p= international phone number (pass via vars?)
663 a= recvonly (missing)
664 a= type:broadcast (missing)
665 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
666 a= x-plgroup: (missing)
667 RTP packets need to get the correct src IP address */
669 char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
671 const sout_stream_sys_t *p_sys = p_stream->p_sys;
673 const char *psz_destination = p_sys->psz_destination;
674 char *psz_sdp, *p, ipv;
677 * When we have a fixed destination (typically when we do multicast),
678 * we need to put the actual port numbers in the SDP.
679 * When there is no fixed destination, we only support RTSP unicast
680 * on-demand setup, so we should rather let the clients decide which ports
682 * When there is both a fixed destination and RTSP unicast, we need to
683 * put port numbers used by the fixed destination, otherwise the SDP would
684 * become totally incorrect for multicast use. It should be noted that
685 * port numbers from SDP with RTSP are only "recommendation" from the
686 * server to the clients (per RFC2326), so only broken clients will fail
687 * to handle this properly. There is no solution but to use two differents
688 * output chain with two different RTSP URLs if you need to handle this
691 int inclport = (psz_destination != NULL);
693 /* FIXME: breaks IP version check on unknown destination */
694 if( psz_destination == NULL )
695 psz_destination = "0.0.0.0";
697 i_size = sizeof( "v=0\r\n" ) +
698 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
699 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
700 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
701 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
702 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
703 sizeof( "t=0 0\r\n" ) +
704 sizeof( "b=RR:0\r\n" ) +
705 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
706 sizeof( "a=recvonly\r\n" ) +
707 sizeof( "a=type:broadcast\r\n" ) +
708 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
709 strlen( psz_destination ) ;
710 for( i = 0; i < p_sys->i_es; i++ )
712 sout_stream_id_t *id = p_sys->es[i];
714 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
717 i_size += strlen( "b=AS: *\r\n") + 10;
721 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
725 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
727 if( rtsp_url != NULL )
729 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( rtsp_url ) + 10;
733 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
735 p = psz_sdp = malloc( i_size );
736 p += sprintf( p, "v=0\r\n" );
737 p += sprintf( p, "o=- "I64Fu" %d IN IP%c %s\r\n",
738 p_sys->i_sdp_id, p_sys->i_sdp_version,
739 ipv, ipv == '6' ? "::1" : "127.0.0.1" );
740 if( *p_sys->psz_session_name )
741 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
742 if( *p_sys->psz_session_description )
743 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
744 if( *p_sys->psz_session_url )
745 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
746 if( *p_sys->psz_session_email )
747 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
749 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */
750 /* when scheduled from vlm, we should set this info correctly */
751 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
752 p += sprintf( p, "a=recvonly\r\n" );
753 p += sprintf( p, "a=type:broadcast\r\n" );
755 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
758 && net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
760 /* Add the deprecated TTL field if it is an IPv4 multicast address */
761 p += sprintf( p, "/%d", p_sys->i_ttl ?: 1 );
763 p += sprintf( p, "\r\n" );
764 p += sprintf( p, "b=RR:0\r\n" );
766 for( i = 0; i < p_sys->i_es; i++ )
768 sout_stream_id_t *id = p_sys->es[i];
769 const char *mime_major; /* major MIME type */
771 if( id->i_cat == AUDIO_ES )
772 mime_major = "audio";
774 if( id->i_cat == VIDEO_ES )
775 mime_major = "video";
779 p += sprintf( p, "m=%s %d RTP/AVP %d\r\n", mime_major,
780 inclport * id->i_port, id->i_payload_type );
784 p += sprintf(p,"b=AS:%d\r\n",id->i_bitrate);
788 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
793 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
796 if( rtsp_url != NULL )
798 p += sprintf( p, "a=control:/trackID=%d\r\n", i );
805 /*****************************************************************************
807 *****************************************************************************/
808 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
809 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
810 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
811 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
812 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
813 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
814 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
815 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
816 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
817 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
818 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
820 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
822 static const char hex[16] = "0123456789abcdef";
825 for( i = 0; i < i_data; i++ )
827 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
828 s[2*i+1] = hex[(p_data[i] )&0xf];
834 /** Add an ES as a new RTP stream */
835 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
837 /* NOTE: As a special case, if we use a non-RTP
838 * mux (TS/PS), then p_fmt is NULL. */
839 sout_stream_sys_t *p_sys = p_stream->p_sys;
840 sout_stream_id_t *id;
841 int i_port, cscov = -1;
844 id = vlc_object_create( p_stream, sizeof( sout_stream_id_t ) );
848 /* Choose the port */
853 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
855 i_port = p_sys->i_port_audio;
856 p_sys->i_port_audio = 0;
859 if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
861 i_port = p_sys->i_port_video;
862 p_sys->i_port_video = 0;
867 if( p_sys->i_port != p_sys->i_port_audio
868 && p_sys->i_port != p_sys->i_port_video )
870 i_port = p_sys->i_port;
877 id->p_stream = p_stream;
879 id->i_timestamp_start = rand()&0xffffffff;
880 id->i_sequence = rand()&0xffff;
881 id->i_payload_type = p_sys->i_payload_type;
882 id->ssrc[0] = rand()&0xff;
883 id->ssrc[1] = rand()&0xff;
884 id->ssrc[2] = rand()&0xff;
885 id->ssrc[3] = rand()&0xff;
887 id->psz_rtpmap = NULL;
889 id->i_clock_rate = 90000; /* most common case */
893 id->i_cat = p_fmt->i_cat;
894 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
898 id->i_cat = VIDEO_ES;
902 id->pf_packetize = NULL;
903 id->i_mtu = config_GetInt( p_stream, "mtu" );
904 if( id->i_mtu <= 12 + 16 )
905 id->i_mtu = 576 - 20 - 8; /* pessimistic */
907 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
909 vlc_mutex_init( p_stream, &id->lock_sink );
915 (int64_t)1000 * var_GetInteger( p_stream, SOUT_CFG_PREFIX "caching");
916 id->p_fifo = block_FifoNew( p_stream );
918 if( vlc_thread_create( id, "RTP send thread", ThreadSend,
919 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
921 vlc_mutex_destroy( &id->lock_sink );
922 vlc_object_destroy( id );
926 if( p_sys->psz_destination != NULL )
928 int ttl = (p_sys->i_ttl > 0) ? p_sys->i_ttl : -1;
929 int fd = net_ConnectDgram( p_stream, p_sys->psz_destination,
930 i_port, ttl, p_sys->proto );
934 msg_Err( p_stream, "cannot create RTP socket" );
935 vlc_thread_join( id );
936 vlc_mutex_destroy( &id->lock_sink );
937 vlc_object_destroy( id );
940 rtp_add_sink( id, fd );
946 switch( p_fmt->i_codec )
948 case VLC_FOURCC( 's', '1', '6', 'b' ):
949 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
951 id->i_payload_type = 11;
953 else if( p_fmt->audio.i_channels == 2 &&
954 p_fmt->audio.i_rate == 44100 )
956 id->i_payload_type = 10;
958 if( asprintf( &id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
959 p_fmt->audio.i_channels ) == -1 )
960 id->psz_rtpmap = NULL;
961 id->i_clock_rate = p_fmt->audio.i_rate;
962 id->pf_packetize = rtp_packetize_l16;
964 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
965 if( asprintf( &id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
966 p_fmt->audio.i_channels ) == -1 )
967 id->psz_rtpmap = NULL;
968 id->i_clock_rate = p_fmt->audio.i_rate;
969 id->pf_packetize = rtp_packetize_l8;
971 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
972 id->i_payload_type = 14;
973 id->psz_rtpmap = strdup( "MPA/90000" );
974 id->pf_packetize = rtp_packetize_mpa;
976 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
977 id->i_payload_type = 32;
978 id->psz_rtpmap = strdup( "MPV/90000" );
979 id->pf_packetize = rtp_packetize_mpv;
981 case VLC_FOURCC( 'a', '5', '2', ' ' ):
982 id->psz_rtpmap = strdup( "ac3/90000" );
983 id->pf_packetize = rtp_packetize_ac3;
985 case VLC_FOURCC( 'H', '2', '6', '3' ):
986 id->psz_rtpmap = strdup( "H263-1998/90000" );
987 id->pf_packetize = rtp_packetize_h263;
989 case VLC_FOURCC( 'h', '2', '6', '4' ):
990 id->psz_rtpmap = strdup( "H264/90000" );
991 id->pf_packetize = rtp_packetize_h264;
994 if( p_fmt->i_extra > 0 )
996 uint8_t *p_buffer = p_fmt->p_extra;
997 int i_buffer = p_fmt->i_extra;
998 char *p_64_sps = NULL;
999 char *p_64_pps = NULL;
1002 while( i_buffer > 4 &&
1003 p_buffer[0] == 0 && p_buffer[1] == 0 &&
1004 p_buffer[2] == 0 && p_buffer[3] == 1 )
1006 const int i_nal_type = p_buffer[4]&0x1f;
1010 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
1013 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
1015 if( !memcmp (p_buffer + i_offset, "\x00\x00\x00\x01", 4 ) )
1017 /* we found another startcode */
1022 if( i_nal_type == 7 )
1024 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1025 sprintf_hexa( hexa, &p_buffer[5], 3 );
1027 else if( i_nal_type == 8 )
1029 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1035 if( p_64_sps && p_64_pps &&
1036 ( asprintf( &id->psz_fmtp,
1037 "packetization-mode=1;profile-level-id=%s;"
1038 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
1039 p_64_pps ) == -1 ) )
1040 id->psz_fmtp = NULL;
1045 id->psz_fmtp = strdup( "packetization-mode=1" );
1048 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1050 char hexa[2*p_fmt->i_extra +1];
1052 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1053 id->pf_packetize = rtp_packetize_split;
1054 if( p_fmt->i_extra > 0 )
1056 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1057 if( asprintf( &id->psz_fmtp,
1058 "profile-level-id=3; config=%s;", hexa ) == -1 )
1059 id->psz_fmtp = NULL;
1063 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1065 id->i_clock_rate = p_fmt->audio.i_rate;
1069 char hexa[2*p_fmt->i_extra +1];
1071 if( asprintf( &id->psz_rtpmap, "mpeg4-generic/%d",
1072 p_fmt->audio.i_rate ) == -1 )
1073 id->psz_rtpmap = NULL;
1074 id->pf_packetize = rtp_packetize_mp4a;
1075 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1076 if( asprintf( &id->psz_fmtp,
1077 "streamtype=5; profile-level-id=15; "
1078 "mode=AAC-hbr; config=%s; SizeLength=13; "
1079 "IndexLength=3; IndexDeltaLength=3; Profile=1;",
1081 id->psz_fmtp = NULL;
1087 unsigned char config[6];
1088 unsigned int aacsrates[15] = {
1089 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1090 16000, 12000, 11025, 8000, 7350, 0, 0 };
1092 for( i = 0; i < 15; i++ )
1093 if( p_fmt->audio.i_rate == aacsrates[i] )
1099 config[3]=p_fmt->audio.i_channels<<4;
1103 if( asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1104 p_fmt->audio.i_rate,
1105 p_fmt->audio.i_channels ) == -1)
1106 id->psz_rtpmap = NULL;
1107 id->pf_packetize = rtp_packetize_mp4a_latm;
1108 sprintf_hexa( hexa, config, 6 );
1109 if( asprintf( &id->psz_fmtp, "profile-level-id=15; "
1110 "object=2; cpresent=0; config=%s", hexa ) == -1 )
1111 id->psz_fmtp = NULL;
1115 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1116 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1117 "AMR/8000/2" : "AMR/8000" );
1118 id->psz_fmtp = strdup( "octet-align=1" );
1119 id->i_clock_rate = p_fmt->audio.i_rate;
1120 id->pf_packetize = rtp_packetize_amr;
1122 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1123 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1124 "AMR-WB/16000/2" : "AMR-WB/16000" );
1125 id->psz_fmtp = strdup( "octet-align=1" );
1126 id->i_clock_rate = p_fmt->audio.i_rate;
1127 id->pf_packetize = rtp_packetize_amr;
1131 msg_Err( p_stream, "cannot add this stream (unsupported "
1132 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1134 rtp_del_sink( id, id->sinkv[0].rtp_fd );
1135 vlc_thread_join( id );
1136 vlc_mutex_destroy( &id->lock_sink );
1137 vlc_object_destroy( id );
1142 cscov += 8 /* UDP */ + 12 /* RTP */;
1144 net_SetCSCov( id->sinkv[0].rtp_fd, cscov, -1 );
1146 if( id->i_payload_type == p_sys->i_payload_type )
1147 p_sys->i_payload_type++;
1149 if( p_sys->rtsp != NULL )
1150 id->rtsp_id = RtspAddId( p_sys->rtsp, id, p_sys->i_es,
1151 p_sys->psz_destination,
1152 p_sys->i_ttl, id->i_port, id->i_port + 1 );
1154 /* Update p_sys context */
1155 vlc_mutex_lock( &p_sys->lock_es );
1156 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1157 vlc_mutex_unlock( &p_sys->lock_es );
1159 psz_sdp = SDPGenerate( p_stream, NULL );
1161 vlc_mutex_lock( &p_sys->lock_sdp );
1162 free( p_sys->psz_sdp );
1163 p_sys->psz_sdp = psz_sdp;
1164 vlc_mutex_unlock( &p_sys->lock_sdp );
1166 p_sys->i_sdp_version++;
1168 msg_Dbg( p_stream, "sdp=\n%s", p_sys->psz_sdp );
1170 /* Update SDP (sap/file) */
1171 if( p_sys->b_export_sap ) SapSetup( p_stream );
1172 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1174 vlc_object_attach( id, p_stream );
1178 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1180 sout_stream_sys_t *p_sys = p_stream->p_sys;
1182 vlc_object_kill( id );
1184 vlc_mutex_lock( &p_sys->lock_es );
1185 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1186 vlc_mutex_unlock( &p_sys->lock_es );
1189 if( id->i_port > 0 )
1191 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1192 p_sys->i_port_audio = id->i_port;
1193 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1194 p_sys->i_port_video = id->i_port;
1197 free( id->psz_rtpmap );
1198 free( id->psz_fmtp );
1201 RtspDelId( p_sys->rtsp, id->rtsp_id );
1203 rtp_del_sink( id, id->sinkv[0].rtp_fd ); /* sink for explicit dst= */
1205 vlc_thread_join( id );
1206 vlc_mutex_destroy( &id->lock_sink );
1207 block_FifoRelease( id->p_fifo );
1209 /* Update SDP (sap/file) */
1210 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1211 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1213 vlc_object_detach( id );
1214 vlc_object_destroy( id );
1218 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1223 assert( p_stream->p_sys->p_mux == NULL );
1225 while( p_buffer != NULL )
1227 p_next = p_buffer->p_next;
1228 if( id->pf_packetize( p_stream, id, p_buffer ) )
1231 block_Release( p_buffer );
1237 /****************************************************************************
1239 ****************************************************************************/
1240 static int SapSetup( sout_stream_t *p_stream )
1242 sout_stream_sys_t *p_sys = p_stream->p_sys;
1243 sout_instance_t *p_sout = p_stream->p_sout;
1244 announce_method_t *p_method = sout_SAPMethod();
1246 /* Remove the previous session */
1247 if( p_sys->p_session != NULL)
1249 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1250 sout_AnnounceSessionDestroy( p_sys->p_session );
1251 p_sys->p_session = NULL;
1254 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1256 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
1258 p_sys->psz_destination,
1262 sout_MethodRelease( p_method );
1266 /****************************************************************************
1268 ****************************************************************************/
1269 static int FileSetup( sout_stream_t *p_stream )
1271 sout_stream_sys_t *p_sys = p_stream->p_sys;
1274 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1276 msg_Err( p_stream, "cannot open file '%s' (%s)",
1277 p_sys->psz_sdp_file, strerror(errno) );
1278 return VLC_EGENERIC;
1281 fputs( p_sys->psz_sdp, f );
1287 /****************************************************************************
1289 ****************************************************************************/
1290 static int HttpCallback( httpd_file_sys_t *p_args,
1291 httpd_file_t *, uint8_t *p_request,
1292 uint8_t **pp_data, int *pi_data );
1294 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1296 sout_stream_sys_t *p_sys = p_stream->p_sys;
1298 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
1299 url->i_port > 0 ? url->i_port : 80 );
1300 if( p_sys->p_httpd_host )
1302 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1303 url->psz_path ? url->psz_path : "/",
1306 HttpCallback, (void*)p_sys );
1308 if( p_sys->p_httpd_file == NULL )
1310 return VLC_EGENERIC;
1315 static int HttpCallback( httpd_file_sys_t *p_args,
1316 httpd_file_t *f, uint8_t *p_request,
1317 uint8_t **pp_data, int *pi_data )
1319 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1321 vlc_mutex_lock( &p_sys->lock_sdp );
1322 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1324 *pi_data = strlen( p_sys->psz_sdp );
1325 *pp_data = malloc( *pi_data );
1326 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1333 vlc_mutex_unlock( &p_sys->lock_sdp );
1338 /****************************************************************************
1340 ****************************************************************************/
1341 static void ThreadSend( vlc_object_t *p_this )
1343 sout_stream_id_t *id = (sout_stream_id_t *)p_this;
1344 unsigned i_caching = id->i_caching;
1346 int fd[5] = { -1, -1, -1, -1, -1 };
1354 fd[4] = open( "/dev/null", O_WRONLY );
1359 block_t *out = block_FifoGet( id->p_fifo );
1360 mtime_t i_date = out->i_dts + i_caching;
1361 ssize_t len = out->i_buffer;
1365 len = write( fd[1], out->p_buffer, len);
1367 continue; /* Uho - should not happen */
1371 vlc_mutex_lock( &id->lock_sink );
1372 for( int i = 0; i < id->sinkc; i++ )
1374 SendRTCP( id->sinkv[i].rtcp, out );
1377 tee( fd[0], fd[3], len, 0 );
1378 if( splice( fd[2], NULL, id->sinkv[i].rtp_fd, NULL, len,
1379 SPLICE_F_NONBLOCK ) >= 0 )
1383 splice( fd[2], NULL, fd[4], NULL, len, 0 );
1385 send( id->sinkv[i].rtp_fd, out->p_buffer, len, 0 );
1387 vlc_mutex_unlock( &id->lock_sink );
1389 block_Release( out );
1391 splice( fd[0], NULL, fd[4], NULL, len, 0 );
1396 for( unsigned i = 0; i < 5; i++ )
1401 static inline void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1403 block_FifoPut( id->p_fifo, out );
1406 int rtp_add_sink( sout_stream_id_t *id, int fd )
1408 rtp_sink_t sink = { fd, NULL };
1409 sink.rtcp = OpenRTCP( VLC_OBJECT( id->p_stream ), fd, IPPROTO_UDP );
1410 if( sink.rtcp == NULL )
1411 msg_Err( id, "RTCP failed!" );
1413 vlc_mutex_lock( &id->lock_sink );
1414 INSERT_ELEM( id->sinkv, id->sinkc, id->sinkc, sink );
1415 vlc_mutex_unlock( &id->lock_sink );
1419 void rtp_del_sink( sout_stream_id_t *id, int fd )
1421 rtp_sink_t sink = { fd, NULL };
1423 /* NOTE: must be safe to use if fd is not included */
1424 vlc_mutex_lock( &id->lock_sink );
1425 for( int i = 0; i < id->sinkc; i++ )
1427 if (id->sinkv[i].rtp_fd == fd)
1429 sink = id->sinkv[i];
1430 REMOVE_ELEM( id->sinkv, id->sinkc, i );
1434 vlc_mutex_unlock( &id->lock_sink );
1436 CloseRTCP( sink.rtcp );
1437 net_Close( sink.rtp_fd );
1440 /****************************************************************************
1442 ****************************************************************************/
1443 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1444 int b_marker, int64_t i_pts )
1446 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1448 out->p_buffer[0] = 0x80;
1449 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1450 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1451 out->p_buffer[3] = ( id->i_sequence )&0xff;
1452 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1453 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1454 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1455 out->p_buffer[7] = ( i_timestamp )&0xff;
1457 memcpy( out->p_buffer + 8, id->ssrc, 4 );
1463 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1466 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1467 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1469 uint8_t *p_data = in->p_buffer;
1470 int i_data = in->i_buffer;
1473 for( i = 0; i < i_count; i++ )
1475 int i_payload = __MIN( i_max, i_data );
1476 block_t *out = block_New( p_stream, 16 + i_payload );
1478 /* rtp common header */
1479 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1481 out->p_buffer[12] = 0;
1482 out->p_buffer[13] = 0;
1483 /* fragment offset in the current frame */
1484 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1485 out->p_buffer[15] = ( (i*i_max) )&0xff;
1486 memcpy( &out->p_buffer[16], p_data, i_payload );
1488 out->i_buffer = 16 + i_payload;
1489 out->i_dts = in->i_dts + i * in->i_length / i_count;
1490 out->i_length = in->i_length / i_count;
1492 rtp_packetize_send( id, out );
1494 p_data += i_payload;
1495 i_data -= i_payload;
1502 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1505 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1506 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1508 uint8_t *p_data = in->p_buffer;
1509 int i_data = in->i_buffer;
1511 int b_sequence_start = 0;
1512 int i_temporal_ref = 0;
1513 int i_picture_coding_type = 0;
1514 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1515 int b_start_slice = 0;
1517 /* preparse this packet to get some info */
1518 if( in->i_buffer > 4 )
1520 uint8_t *p = p_data;
1521 int i_rest = in->i_buffer;
1525 while( i_rest > 4 &&
1526 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1540 /* sequence start code */
1541 b_sequence_start = 1;
1543 else if( *p == 0x00 && i_rest >= 4 )
1546 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1547 i_picture_coding_type = (p[2] >> 3)&0x07;
1549 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1550 i_picture_coding_type == 3 ) )
1552 i_ffv = (p[3] >> 2)&0x01;
1553 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1554 if( i_rest > 4 && i_picture_coding_type == 3 )
1556 i_fbv = (p[4]>>6)&0x01;
1557 i_bfc = (p[4]>>3)&0x07;
1561 else if( *p <= 0xaf )
1568 for( i = 0; i < i_count; i++ )
1570 int i_payload = __MIN( i_max, i_data );
1571 block_t *out = block_New( p_stream,
1573 uint32_t h = ( i_temporal_ref << 16 )|
1574 ( b_sequence_start << 13 )|
1575 ( b_start_slice << 12 )|
1576 ( i == i_count - 1 ? 1 << 11 : 0 )|
1577 ( i_picture_coding_type << 8 )|
1578 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1580 /* rtp common header */
1581 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1582 in->i_pts > 0 ? in->i_pts : in->i_dts );
1584 /* 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 */
1585 out->p_buffer[12] = ( h >> 24 )&0xff;
1586 out->p_buffer[13] = ( h >> 16 )&0xff;
1587 out->p_buffer[14] = ( h >> 8 )&0xff;
1588 out->p_buffer[15] = ( h )&0xff;
1590 memcpy( &out->p_buffer[16], p_data, i_payload );
1592 out->i_buffer = 16 + i_payload;
1593 out->i_dts = in->i_dts + i * in->i_length / i_count;
1594 out->i_length = in->i_length / i_count;
1596 rtp_packetize_send( id, out );
1598 p_data += i_payload;
1599 i_data -= i_payload;
1605 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1608 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1609 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1611 uint8_t *p_data = in->p_buffer;
1612 int i_data = in->i_buffer;
1615 for( i = 0; i < i_count; i++ )
1617 int i_payload = __MIN( i_max, i_data );
1618 block_t *out = block_New( p_stream, 14 + i_payload );
1620 /* rtp common header */
1621 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1623 out->p_buffer[12] = 1;
1625 out->p_buffer[13] = 0x00;
1627 memcpy( &out->p_buffer[14], p_data, i_payload );
1629 out->i_buffer = 14 + i_payload;
1630 out->i_dts = in->i_dts + i * in->i_length / i_count;
1631 out->i_length = in->i_length / i_count;
1633 rtp_packetize_send( id, out );
1635 p_data += i_payload;
1636 i_data -= i_payload;
1642 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1645 int i_max = id->i_mtu - 12; /* payload max in one packet */
1646 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1648 uint8_t *p_data = in->p_buffer;
1649 int i_data = in->i_buffer;
1652 for( i = 0; i < i_count; i++ )
1654 int i_payload = __MIN( i_max, i_data );
1655 block_t *out = block_New( p_stream, 12 + i_payload );
1657 /* rtp common header */
1658 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1659 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1660 memcpy( &out->p_buffer[12], p_data, i_payload );
1662 out->i_buffer = 12 + i_payload;
1663 out->i_dts = in->i_dts + i * in->i_length / i_count;
1664 out->i_length = in->i_length / i_count;
1666 rtp_packetize_send( id, out );
1668 p_data += i_payload;
1669 i_data -= i_payload;
1676 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
1679 int i_max = id->i_mtu - 14; /* payload max in one packet */
1680 int latmhdrsize = in->i_buffer / 0xff + 1;
1681 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1683 uint8_t *p_data = in->p_buffer, *p_header = NULL;
1684 int i_data = in->i_buffer;
1687 for( i = 0; i < i_count; i++ )
1689 int i_payload = __MIN( i_max, i_data );
1694 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
1696 /* rtp common header */
1697 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
1698 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1702 int tmp = in->i_buffer;
1704 p_header=out->p_buffer+12;
1714 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
1716 out->i_buffer = 12 + latmhdrsize + 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;
1729 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1732 int i_max = id->i_mtu - 12; /* payload max in one packet */
1733 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1735 uint8_t *p_data = in->p_buffer;
1736 int i_data = in->i_buffer;
1741 int i_payload = (__MIN( i_max, i_data )/4)*4;
1742 block_t *out = block_New( p_stream, 12 + i_payload );
1744 /* rtp common header */
1745 rtp_packetize_common( id, out, 0,
1746 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1747 memcpy( &out->p_buffer[12], p_data, i_payload );
1749 out->i_buffer = 12 + i_payload;
1750 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1751 out->i_length = in->i_length / i_count;
1753 rtp_packetize_send( id, out );
1755 p_data += i_payload;
1756 i_data -= i_payload;
1763 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1766 int i_max = id->i_mtu - 12; /* payload max in one packet */
1767 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1769 uint8_t *p_data = in->p_buffer;
1770 int i_data = in->i_buffer;
1775 int i_payload = (__MIN( i_max, i_data )/2)*2;
1776 block_t *out = block_New( p_stream, 12 + i_payload );
1778 /* rtp common header */
1779 rtp_packetize_common( id, out, 0,
1780 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1781 memcpy( &out->p_buffer[12], p_data, i_payload );
1783 out->i_buffer = 12 + i_payload;
1784 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1785 out->i_length = in->i_length / i_count;
1787 rtp_packetize_send( id, out );
1789 p_data += i_payload;
1790 i_data -= i_payload;
1797 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1800 int i_max = id->i_mtu - 16; /* payload max in one packet */
1801 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1803 uint8_t *p_data = in->p_buffer;
1804 int i_data = in->i_buffer;
1807 for( i = 0; i < i_count; i++ )
1809 int i_payload = __MIN( i_max, i_data );
1810 block_t *out = block_New( p_stream, 16 + i_payload );
1812 /* rtp common header */
1813 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1814 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1816 /* AU headers length (bits) */
1817 out->p_buffer[12] = 0;
1818 out->p_buffer[13] = 2*8;
1819 /* for each AU length 13 bits + idx 3bits, */
1820 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1821 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1823 memcpy( &out->p_buffer[16], p_data, i_payload );
1825 out->i_buffer = 16 + i_payload;
1826 out->i_dts = in->i_dts + i * in->i_length / i_count;
1827 out->i_length = in->i_length / i_count;
1829 rtp_packetize_send( id, out );
1831 p_data += i_payload;
1832 i_data -= i_payload;
1840 #define RTP_H263_HEADER_SIZE (2) // plen = 0
1841 #define RTP_H263_PAYLOAD_START (14) // plen = 0
1842 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
1845 uint8_t *p_data = in->p_buffer;
1846 int i_data = in->i_buffer;
1848 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
1851 int b_v_bit = 0; // no pesky error resilience
1852 int i_plen = 0; // normally plen=0 for PSC packet
1853 int i_pebit = 0; // because plen=0
1858 return VLC_EGENERIC;
1860 if( p_data[0] || p_data[1] )
1862 return VLC_EGENERIC;
1864 /* remove 2 leading 0 bytes */
1867 i_count = ( i_data + i_max - 1 ) / i_max;
1869 for( i = 0; i < i_count; i++ )
1871 int i_payload = __MIN( i_max, i_data );
1872 block_t *out = block_New( p_stream,
1873 RTP_H263_PAYLOAD_START + i_payload );
1874 b_p_bit = (i == 0) ? 1 : 0;
1875 h = ( b_p_bit << 10 )|
1880 /* rtp common header */
1881 //b_m_bit = 1; // always contains end of frame
1882 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1883 in->i_pts > 0 ? in->i_pts : in->i_dts );
1886 out->p_buffer[12] = ( h >> 8 )&0xff;
1887 out->p_buffer[13] = ( h )&0xff;
1888 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1890 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1891 out->i_dts = in->i_dts + i * in->i_length / i_count;
1892 out->i_length = in->i_length / i_count;
1894 rtp_packetize_send( id, out );
1896 p_data += i_payload;
1897 i_data -= i_payload;
1905 rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
1906 const uint8_t *p_data, int i_data, int64_t i_pts,
1907 int64_t i_dts, vlc_bool_t b_last, int64_t i_length )
1909 const int i_max = id->i_mtu - 12; /* payload max in one packet */
1916 i_nal_hdr = p_data[3];
1917 i_nal_type = i_nal_hdr&0x1f;
1919 /* Skip start code */
1924 if( i_data <= i_max )
1926 /* Single NAL unit packet */
1927 block_t *out = block_New( p_stream, 12 + i_data );
1929 out->i_length = i_length;
1932 rtp_packetize_common( id, out, b_last, i_pts );
1933 out->i_buffer = 12 + i_data;
1935 memcpy( &out->p_buffer[12], p_data, i_data );
1937 rtp_packetize_send( id, out );
1941 /* FU-A Fragmentation Unit without interleaving */
1942 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1948 for( i = 0; i < i_count; i++ )
1950 const int i_payload = __MIN( i_data, i_max-2 );
1951 block_t *out = block_New( p_stream, 12 + 2 + i_payload );
1952 out->i_dts = i_dts + i * i_length / i_count;
1953 out->i_length = i_length / i_count;
1956 rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
1957 out->i_buffer = 14 + i_payload;
1960 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1962 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
1963 memcpy( &out->p_buffer[14], p_data, i_payload );
1965 rtp_packetize_send( id, out );
1967 i_data -= i_payload;
1968 p_data += i_payload;
1974 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
1977 const uint8_t *p_buffer = in->p_buffer;
1978 int i_buffer = in->i_buffer;
1980 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1986 /* Split nal units */
1987 while( i_buffer > 4 )
1990 int i_size = i_buffer;
1991 int i_skip = i_buffer;
1993 /* search nal end */
1994 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1996 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1998 /* we found another startcode */
1999 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
2004 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
2005 rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
2006 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
2007 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
2015 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2018 int i_max = id->i_mtu - 14; /* payload max in one packet */
2019 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2021 uint8_t *p_data = in->p_buffer;
2022 int i_data = in->i_buffer;
2025 /* Only supports octet-aligned mode */
2026 for( i = 0; i < i_count; i++ )
2028 int i_payload = __MIN( i_max, i_data );
2029 block_t *out = block_New( p_stream, 14 + i_payload );
2031 /* rtp common header */
2032 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2033 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2034 /* Payload header */
2035 out->p_buffer[12] = 0xF0; /* CMR */
2036 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
2038 /* FIXME: are we fed multiple frames ? */
2039 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
2041 out->i_buffer = 14 + i_payload-1;
2042 out->i_dts = in->i_dts + i * in->i_length / i_count;
2043 out->i_length = in->i_length / i_count;
2045 rtp_packetize_send( id, out );
2047 p_data += i_payload;
2048 i_data -= i_payload;
2054 /*****************************************************************************
2056 *****************************************************************************/
2058 /** Add an ES to a non-RTP muxed stream */
2059 static sout_stream_id_t *MuxAdd( sout_stream_t *p_stream, es_format_t *p_fmt )
2061 sout_input_t *p_input;
2062 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2063 assert( p_mux != NULL );
2065 p_input = sout_MuxAddStream( p_mux, p_fmt );
2066 if( p_input == NULL )
2068 msg_Err( p_stream, "cannot add this stream to the muxer" );
2072 return (sout_stream_id_t *)p_input;
2076 static int MuxSend( sout_stream_t *p_stream, sout_stream_id_t *id,
2079 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2080 assert( p_mux != NULL );
2082 sout_MuxSendBuffer( p_mux, (sout_input_t *)id, p_buffer );
2087 /** Remove an ES from a non-RTP muxed stream */
2088 static int MuxDel( sout_stream_t *p_stream, sout_stream_id_t *id )
2090 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2091 assert( p_mux != NULL );
2093 sout_MuxDeleteStream( p_mux, (sout_input_t *)id );
2098 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
2099 const block_t *p_buffer )
2101 sout_stream_sys_t *p_sys = p_stream->p_sys;
2102 sout_stream_id_t *id = p_sys->es[0];
2104 int64_t i_dts = p_buffer->i_dts;
2106 uint8_t *p_data = p_buffer->p_buffer;
2107 unsigned int i_data = p_buffer->i_buffer;
2108 unsigned int i_max = id->i_mtu - 12;
2110 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
2114 unsigned int i_size;
2116 /* output complete packet */
2117 if( p_sys->packet &&
2118 p_sys->packet->i_buffer + i_data > i_max )
2120 rtp_packetize_send( id, p_sys->packet );
2121 p_sys->packet = NULL;
2124 if( p_sys->packet == NULL )
2126 /* allocate a new packet */
2127 p_sys->packet = block_New( p_stream, id->i_mtu );
2128 rtp_packetize_common( id, p_sys->packet, 1, i_dts );
2129 p_sys->packet->i_dts = i_dts;
2130 p_sys->packet->i_length = p_buffer->i_length / i_packet;
2131 i_dts += p_sys->packet->i_length;
2134 i_size = __MIN( i_data,
2135 (unsigned)(id->i_mtu - p_sys->packet->i_buffer) );
2137 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
2140 p_sys->packet->i_buffer += i_size;
2149 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
2152 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
2158 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
2160 p_next = p_buffer->p_next;
2161 block_Release( p_buffer );
2169 static sout_access_out_t *GrabberCreate( sout_stream_t *p_stream )
2171 sout_access_out_t *p_grab;
2173 p_grab = vlc_object_create( p_stream->p_sout, sizeof( *p_grab ) );
2174 if( p_grab == NULL )
2177 p_grab->p_module = NULL;
2178 p_grab->p_sout = p_stream->p_sout;
2179 p_grab->psz_access = strdup( "grab" );
2180 p_grab->p_cfg = NULL;
2181 p_grab->psz_path = strdup( "" );
2182 p_grab->p_sys = (sout_access_out_sys_t *)p_stream;
2183 p_grab->pf_seek = NULL;
2184 p_grab->pf_write = AccessOutGrabberWrite;