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", "", 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_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "name" );
311 p_sys->psz_session_description = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "description" );
312 p_sys->psz_session_url = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "url" );
313 p_sys->psz_session_email = var_GetNonEmptyString( 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;
423 /* Check muxer type */
424 if( strncasecmp( psz, "ps", 2 )
425 && strncasecmp( psz, "mpeg1", 5 )
426 && strncasecmp( psz, "ts", 2 ) )
428 msg_Err( p_stream, "unsupported muxer type for RTP (only TS/PS)" );
430 vlc_mutex_destroy( &p_sys->lock_sdp );
431 vlc_mutex_destroy( &p_sys->lock_es );
436 p_sys->p_grab = GrabberCreate( p_stream );
437 p_sys->p_mux = sout_MuxNew( p_sout, psz, p_sys->p_grab );
440 if( p_sys->p_mux == NULL )
442 msg_Err( p_stream, "cannot create muxer" );
443 sout_AccessOutDelete( p_sys->p_grab );
444 vlc_mutex_destroy( &p_sys->lock_sdp );
445 vlc_mutex_destroy( &p_sys->lock_es );
450 id = Add( p_stream, NULL );
453 sout_MuxDelete( p_sys->p_mux );
454 sout_AccessOutDelete( p_sys->p_grab );
455 vlc_mutex_destroy( &p_sys->lock_sdp );
456 vlc_mutex_destroy( &p_sys->lock_es );
461 p_sys->packet = NULL;
463 p_stream->pf_add = MuxAdd;
464 p_stream->pf_del = MuxDel;
465 p_stream->pf_send = MuxSend;
470 p_sys->p_grab = NULL;
472 p_stream->pf_add = Add;
473 p_stream->pf_del = Del;
474 p_stream->pf_send = Send;
477 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
480 config_chain_t *p_cfg;
482 SDPHandleUrl( p_stream, psz );
484 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
486 if( !strcmp( p_cfg->psz_name, "sdp" ) )
488 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
491 /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
492 if( !strcmp( p_cfg->psz_value, psz ) )
495 SDPHandleUrl( p_stream, p_cfg->psz_value );
501 /* update p_sout->i_out_pace_nocontrol */
502 p_stream->p_sout->i_out_pace_nocontrol++;
507 /*****************************************************************************
509 *****************************************************************************/
510 static void Close( vlc_object_t * p_this )
512 sout_stream_t *p_stream = (sout_stream_t*)p_this;
513 sout_stream_sys_t *p_sys = p_stream->p_sys;
515 /* update p_sout->i_out_pace_nocontrol */
516 p_stream->p_sout->i_out_pace_nocontrol--;
520 assert( p_sys->i_es == 1 );
521 Del( p_stream, p_sys->es[0] );
523 sout_MuxDelete( p_sys->p_mux );
524 sout_AccessOutDelete( p_sys->p_grab );
527 block_Release( p_sys->packet );
529 if( p_sys->b_export_sap )
532 SapSetup( p_stream );
536 if( p_sys->rtsp != NULL )
537 RtspUnsetup( p_sys->rtsp );
539 vlc_mutex_destroy( &p_sys->lock_sdp );
540 vlc_mutex_destroy( &p_sys->lock_es );
542 if( p_sys->p_httpd_file )
543 httpd_FileDelete( p_sys->p_httpd_file );
545 if( p_sys->p_httpd_host )
546 httpd_HostDelete( p_sys->p_httpd_host );
548 free( p_sys->psz_session_name );
549 free( p_sys->psz_session_description );
550 free( p_sys->psz_session_url );
551 free( p_sys->psz_session_email );
552 free( p_sys->psz_sdp );
554 if( p_sys->b_export_sdp_file )
557 unlink( p_sys->psz_sdp_file );
559 free( p_sys->psz_sdp_file );
561 free( p_sys->psz_destination );
565 /*****************************************************************************
567 *****************************************************************************/
568 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
570 sout_stream_sys_t *p_sys = p_stream->p_sys;
573 vlc_UrlParse( &url, psz_url, 0 );
574 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
576 if( p_sys->p_httpd_file )
578 msg_Err( p_stream, "you can use sdp=http:// only once" );
582 if( HttpSetup( p_stream, &url ) )
584 msg_Err( p_stream, "cannot export SDP as HTTP" );
587 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
589 if( p_sys->rtsp != NULL )
591 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
595 /* FIXME test if destination is multicast or no destination at all */
596 p_sys->rtsp = RtspSetup( p_stream, &url );
597 if( p_sys->rtsp == NULL )
599 msg_Err( p_stream, "cannot export SDP as RTSP" );
602 if( p_sys->p_mux != NULL )
604 sout_stream_id_t *id = p_sys->es[0];
605 id->rtsp_id = RtspAddId( p_sys->rtsp, id, 0,
606 p_sys->psz_destination, p_sys->i_ttl,
607 id->i_port, id->i_port + 1 );
610 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
611 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
613 p_sys->b_export_sap = VLC_TRUE;
614 SapSetup( p_stream );
616 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
618 if( p_sys->b_export_sdp_file )
620 msg_Err( p_stream, "you can use sdp=file:// only once" );
623 p_sys->b_export_sdp_file = VLC_TRUE;
624 psz_url = &psz_url[5];
625 if( psz_url[0] == '/' && psz_url[1] == '/' )
627 p_sys->psz_sdp_file = strdup( psz_url );
631 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
636 vlc_UrlClean( &url );
639 /*****************************************************************************
641 *****************************************************************************/
642 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
643 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
644 o= - should be local username (no spaces allowed)
645 o= time should be hashed with some other value to garantue uniqueness
646 o= we need IP6 support?
647 o= don't use the localhost address. use fully qualified domain name or IP4 address
648 p= international phone number (pass via vars?)
650 a= recvonly (missing)
651 a= type:broadcast (missing)
652 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
653 a= x-plgroup: (missing)
654 RTP packets need to get the correct src IP address */
656 char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
658 const sout_stream_sys_t *p_sys = p_stream->p_sys;
660 struct sockaddr_storage dst;
664 * When we have a fixed destination (typically when we do multicast),
665 * we need to put the actual port numbers in the SDP.
666 * When there is no fixed destination, we only support RTSP unicast
667 * on-demand setup, so we should rather let the clients decide which ports
669 * When there is both a fixed destination and RTSP unicast, we need to
670 * put port numbers used by the fixed destination, otherwise the SDP would
671 * become totally incorrect for multicast use. It should be noted that
672 * port numbers from SDP with RTSP are only "recommendation" from the
673 * server to the clients (per RFC2326), so only broken clients will fail
674 * to handle this properly. There is no solution but to use two differents
675 * output chain with two different RTSP URLs if you need to handle this
680 if( p_sys->psz_destination != NULL )
684 /* Oh boy, this is really ugly! (+ race condition on lock_es) */
685 dstlen = sizeof( dst );
686 getpeername( p_sys->es[0]->sinkv[0].rtp_fd, (struct sockaddr *)&dst,
693 /* Dummy destination address for RTSP */
694 memset (&dst, 0, sizeof( struct sockaddr_in ) );
695 dst.ss_family = AF_INET;
699 dstlen = sizeof( struct sockaddr_in );
702 psz_sdp = sdp_Start( p_sys->psz_session_name,
703 p_sys->psz_session_description,
704 p_sys->psz_session_url, p_sys->psz_session_email,
705 NULL, NULL, 0, (struct sockaddr *)&dst, dstlen );
706 if( psz_sdp == NULL )
709 /* TODO: a=source-filter */
711 /* FIXME: locking?! */
712 for( i = 0; i < p_sys->i_es; i++ )
714 sout_stream_id_t *id = p_sys->es[i];
715 const char *mime_major; /* major MIME type */
717 if( id->i_cat == AUDIO_ES )
718 mime_major = "audio";
720 if( id->i_cat == VIDEO_ES )
721 mime_major = "video";
725 sdp_AddMedia( &psz_sdp, mime_major, "RTP/AVP", inclport * id->i_port,
726 id->i_payload_type, VLC_FALSE, id->i_bitrate,
727 id->psz_rtpmap, id->psz_fmtp);
729 if( rtsp_url != NULL )
730 sdp_AddAttribute ( &psz_sdp, "control", "/trackID=%d", i );
736 /*****************************************************************************
738 *****************************************************************************/
739 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
740 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
741 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
742 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
743 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
744 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
745 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
746 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
747 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
748 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
749 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
751 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
753 static const char hex[16] = "0123456789abcdef";
756 for( i = 0; i < i_data; i++ )
758 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
759 s[2*i+1] = hex[(p_data[i] )&0xf];
765 /** Add an ES as a new RTP stream */
766 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
768 /* NOTE: As a special case, if we use a non-RTP
769 * mux (TS/PS), then p_fmt is NULL. */
770 sout_stream_sys_t *p_sys = p_stream->p_sys;
771 sout_stream_id_t *id;
772 int i_port, cscov = -1;
775 id = vlc_object_create( p_stream, sizeof( sout_stream_id_t ) );
779 /* Choose the port */
784 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
786 i_port = p_sys->i_port_audio;
787 p_sys->i_port_audio = 0;
790 if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
792 i_port = p_sys->i_port_video;
793 p_sys->i_port_video = 0;
798 if( p_sys->i_port != p_sys->i_port_audio
799 && p_sys->i_port != p_sys->i_port_video )
801 i_port = p_sys->i_port;
808 id->p_stream = p_stream;
810 id->i_timestamp_start = rand()&0xffffffff;
811 id->i_sequence = rand()&0xffff;
812 id->i_payload_type = p_sys->i_payload_type;
813 id->ssrc[0] = rand()&0xff;
814 id->ssrc[1] = rand()&0xff;
815 id->ssrc[2] = rand()&0xff;
816 id->ssrc[3] = rand()&0xff;
818 id->psz_rtpmap = NULL;
820 id->i_clock_rate = 90000; /* most common case */
824 id->i_cat = p_fmt->i_cat;
825 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
829 id->i_cat = VIDEO_ES;
833 id->pf_packetize = NULL;
834 id->i_mtu = config_GetInt( p_stream, "mtu" );
835 if( id->i_mtu <= 12 + 16 )
836 id->i_mtu = 576 - 20 - 8; /* pessimistic */
838 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
840 vlc_mutex_init( p_stream, &id->lock_sink );
846 (int64_t)1000 * var_GetInteger( p_stream, SOUT_CFG_PREFIX "caching");
847 id->p_fifo = block_FifoNew( p_stream );
849 if( vlc_thread_create( id, "RTP send thread", ThreadSend,
850 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
852 vlc_mutex_destroy( &id->lock_sink );
853 vlc_object_destroy( id );
857 if( p_sys->psz_destination != NULL )
859 int ttl = (p_sys->i_ttl > 0) ? p_sys->i_ttl : -1;
860 int fd = net_ConnectDgram( p_stream, p_sys->psz_destination,
861 i_port, ttl, p_sys->proto );
865 msg_Err( p_stream, "cannot create RTP socket" );
866 vlc_thread_join( id );
867 vlc_mutex_destroy( &id->lock_sink );
868 vlc_object_destroy( id );
871 rtp_add_sink( id, fd );
876 char *psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
878 if( psz == NULL ) /* Uho! */
881 if( strncmp( psz, "ts", 2 ) == 0 )
883 id->i_payload_type = 33;
884 id->psz_rtpmap = strdup( "MP2T/90000" );
888 id->psz_rtpmap = strdup( "MP2P/90000" );
892 switch( p_fmt->i_codec )
894 case VLC_FOURCC( 's', '1', '6', 'b' ):
895 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
897 id->i_payload_type = 11;
899 else if( p_fmt->audio.i_channels == 2 &&
900 p_fmt->audio.i_rate == 44100 )
902 id->i_payload_type = 10;
904 if( asprintf( &id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
905 p_fmt->audio.i_channels ) == -1 )
906 id->psz_rtpmap = NULL;
907 id->i_clock_rate = p_fmt->audio.i_rate;
908 id->pf_packetize = rtp_packetize_l16;
910 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
911 if( asprintf( &id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
912 p_fmt->audio.i_channels ) == -1 )
913 id->psz_rtpmap = NULL;
914 id->i_clock_rate = p_fmt->audio.i_rate;
915 id->pf_packetize = rtp_packetize_l8;
917 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
918 id->i_payload_type = 14;
919 id->psz_rtpmap = strdup( "MPA/90000" );
920 id->pf_packetize = rtp_packetize_mpa;
922 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
923 id->i_payload_type = 32;
924 id->psz_rtpmap = strdup( "MPV/90000" );
925 id->pf_packetize = rtp_packetize_mpv;
927 case VLC_FOURCC( 'a', '5', '2', ' ' ):
928 id->psz_rtpmap = strdup( "ac3/90000" );
929 id->pf_packetize = rtp_packetize_ac3;
931 case VLC_FOURCC( 'H', '2', '6', '3' ):
932 id->psz_rtpmap = strdup( "H263-1998/90000" );
933 id->pf_packetize = rtp_packetize_h263;
935 case VLC_FOURCC( 'h', '2', '6', '4' ):
936 id->psz_rtpmap = strdup( "H264/90000" );
937 id->pf_packetize = rtp_packetize_h264;
940 if( p_fmt->i_extra > 0 )
942 uint8_t *p_buffer = p_fmt->p_extra;
943 int i_buffer = p_fmt->i_extra;
944 char *p_64_sps = NULL;
945 char *p_64_pps = NULL;
948 while( i_buffer > 4 &&
949 p_buffer[0] == 0 && p_buffer[1] == 0 &&
950 p_buffer[2] == 0 && p_buffer[3] == 1 )
952 const int i_nal_type = p_buffer[4]&0x1f;
956 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
959 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
961 if( !memcmp (p_buffer + i_offset, "\x00\x00\x00\x01", 4 ) )
963 /* we found another startcode */
968 if( i_nal_type == 7 )
970 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
971 sprintf_hexa( hexa, &p_buffer[5], 3 );
973 else if( i_nal_type == 8 )
975 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
981 if( p_64_sps && p_64_pps &&
982 ( asprintf( &id->psz_fmtp,
983 "packetization-mode=1;profile-level-id=%s;"
984 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
991 id->psz_fmtp = strdup( "packetization-mode=1" );
994 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
996 char hexa[2*p_fmt->i_extra +1];
998 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
999 id->pf_packetize = rtp_packetize_split;
1000 if( p_fmt->i_extra > 0 )
1002 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1003 if( asprintf( &id->psz_fmtp,
1004 "profile-level-id=3; config=%s;", hexa ) == -1 )
1005 id->psz_fmtp = NULL;
1009 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1011 id->i_clock_rate = p_fmt->audio.i_rate;
1015 char hexa[2*p_fmt->i_extra +1];
1017 if( asprintf( &id->psz_rtpmap, "mpeg4-generic/%d",
1018 p_fmt->audio.i_rate ) == -1 )
1019 id->psz_rtpmap = NULL;
1020 id->pf_packetize = rtp_packetize_mp4a;
1021 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1022 if( asprintf( &id->psz_fmtp,
1023 "streamtype=5; profile-level-id=15; "
1024 "mode=AAC-hbr; config=%s; SizeLength=13; "
1025 "IndexLength=3; IndexDeltaLength=3; Profile=1;",
1027 id->psz_fmtp = NULL;
1033 unsigned char config[6];
1034 unsigned int aacsrates[15] = {
1035 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1036 16000, 12000, 11025, 8000, 7350, 0, 0 };
1038 for( i = 0; i < 15; i++ )
1039 if( p_fmt->audio.i_rate == aacsrates[i] )
1045 config[3]=p_fmt->audio.i_channels<<4;
1049 if( asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1050 p_fmt->audio.i_rate,
1051 p_fmt->audio.i_channels ) == -1)
1052 id->psz_rtpmap = NULL;
1053 id->pf_packetize = rtp_packetize_mp4a_latm;
1054 sprintf_hexa( hexa, config, 6 );
1055 if( asprintf( &id->psz_fmtp, "profile-level-id=15; "
1056 "object=2; cpresent=0; config=%s", hexa ) == -1 )
1057 id->psz_fmtp = NULL;
1061 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1062 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1063 "AMR/8000/2" : "AMR/8000" );
1064 id->psz_fmtp = strdup( "octet-align=1" );
1065 id->i_clock_rate = p_fmt->audio.i_rate;
1066 id->pf_packetize = rtp_packetize_amr;
1068 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1069 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1070 "AMR-WB/16000/2" : "AMR-WB/16000" );
1071 id->psz_fmtp = strdup( "octet-align=1" );
1072 id->i_clock_rate = p_fmt->audio.i_rate;
1073 id->pf_packetize = rtp_packetize_amr;
1077 msg_Err( p_stream, "cannot add this stream (unsupported "
1078 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1080 rtp_del_sink( id, id->sinkv[0].rtp_fd );
1081 vlc_thread_join( id );
1082 vlc_mutex_destroy( &id->lock_sink );
1083 vlc_object_destroy( id );
1088 cscov += 8 /* UDP */ + 12 /* RTP */;
1090 net_SetCSCov( id->sinkv[0].rtp_fd, cscov, -1 );
1092 if( id->i_payload_type == p_sys->i_payload_type )
1093 p_sys->i_payload_type++;
1095 if( p_sys->rtsp != NULL )
1096 id->rtsp_id = RtspAddId( p_sys->rtsp, id, p_sys->i_es,
1097 p_sys->psz_destination,
1098 p_sys->i_ttl, id->i_port, id->i_port + 1 );
1100 /* Update p_sys context */
1101 vlc_mutex_lock( &p_sys->lock_es );
1102 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1103 vlc_mutex_unlock( &p_sys->lock_es );
1105 psz_sdp = SDPGenerate( p_stream, NULL );
1107 vlc_mutex_lock( &p_sys->lock_sdp );
1108 free( p_sys->psz_sdp );
1109 p_sys->psz_sdp = psz_sdp;
1110 vlc_mutex_unlock( &p_sys->lock_sdp );
1112 p_sys->i_sdp_version++;
1114 msg_Dbg( p_stream, "sdp=\n%s", p_sys->psz_sdp );
1116 /* Update SDP (sap/file) */
1117 if( p_sys->b_export_sap ) SapSetup( p_stream );
1118 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1120 vlc_object_attach( id, p_stream );
1124 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1126 sout_stream_sys_t *p_sys = p_stream->p_sys;
1128 vlc_object_kill( id );
1130 vlc_mutex_lock( &p_sys->lock_es );
1131 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1132 vlc_mutex_unlock( &p_sys->lock_es );
1135 if( id->i_port > 0 )
1137 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1138 p_sys->i_port_audio = id->i_port;
1139 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1140 p_sys->i_port_video = id->i_port;
1143 free( id->psz_rtpmap );
1144 free( id->psz_fmtp );
1147 RtspDelId( p_sys->rtsp, id->rtsp_id );
1149 rtp_del_sink( id, id->sinkv[0].rtp_fd ); /* sink for explicit dst= */
1151 vlc_thread_join( id );
1152 vlc_mutex_destroy( &id->lock_sink );
1153 block_FifoRelease( id->p_fifo );
1155 /* Update SDP (sap/file) */
1156 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1157 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1159 vlc_object_detach( id );
1160 vlc_object_destroy( id );
1164 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1169 assert( p_stream->p_sys->p_mux == NULL );
1171 while( p_buffer != NULL )
1173 p_next = p_buffer->p_next;
1174 if( id->pf_packetize( p_stream, id, p_buffer ) )
1177 block_Release( p_buffer );
1183 /****************************************************************************
1185 ****************************************************************************/
1186 static int SapSetup( sout_stream_t *p_stream )
1188 sout_stream_sys_t *p_sys = p_stream->p_sys;
1189 sout_instance_t *p_sout = p_stream->p_sout;
1190 announce_method_t *p_method = sout_SAPMethod();
1192 /* Remove the previous session */
1193 if( p_sys->p_session != NULL)
1195 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1196 sout_AnnounceSessionDestroy( p_sys->p_session );
1197 p_sys->p_session = NULL;
1200 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1202 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
1204 p_sys->psz_destination,
1208 sout_MethodRelease( p_method );
1212 /****************************************************************************
1214 ****************************************************************************/
1215 static int FileSetup( sout_stream_t *p_stream )
1217 sout_stream_sys_t *p_sys = p_stream->p_sys;
1220 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1222 msg_Err( p_stream, "cannot open file '%s' (%s)",
1223 p_sys->psz_sdp_file, strerror(errno) );
1224 return VLC_EGENERIC;
1227 fputs( p_sys->psz_sdp, f );
1233 /****************************************************************************
1235 ****************************************************************************/
1236 static int HttpCallback( httpd_file_sys_t *p_args,
1237 httpd_file_t *, uint8_t *p_request,
1238 uint8_t **pp_data, int *pi_data );
1240 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1242 sout_stream_sys_t *p_sys = p_stream->p_sys;
1244 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
1245 url->i_port > 0 ? url->i_port : 80 );
1246 if( p_sys->p_httpd_host )
1248 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1249 url->psz_path ? url->psz_path : "/",
1252 HttpCallback, (void*)p_sys );
1254 if( p_sys->p_httpd_file == NULL )
1256 return VLC_EGENERIC;
1261 static int HttpCallback( httpd_file_sys_t *p_args,
1262 httpd_file_t *f, uint8_t *p_request,
1263 uint8_t **pp_data, int *pi_data )
1265 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1267 vlc_mutex_lock( &p_sys->lock_sdp );
1268 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1270 *pi_data = strlen( p_sys->psz_sdp );
1271 *pp_data = malloc( *pi_data );
1272 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1279 vlc_mutex_unlock( &p_sys->lock_sdp );
1284 /****************************************************************************
1286 ****************************************************************************/
1287 static void ThreadSend( vlc_object_t *p_this )
1289 sout_stream_id_t *id = (sout_stream_id_t *)p_this;
1290 unsigned i_caching = id->i_caching;
1292 int fd[5] = { -1, -1, -1, -1, -1 };
1300 fd[4] = open( "/dev/null", O_WRONLY );
1305 block_t *out = block_FifoGet( id->p_fifo );
1306 mtime_t i_date = out->i_dts + i_caching;
1307 ssize_t len = out->i_buffer;
1311 len = write( fd[1], out->p_buffer, len);
1313 continue; /* Uho - should not happen */
1317 vlc_mutex_lock( &id->lock_sink );
1318 for( int i = 0; i < id->sinkc; i++ )
1320 SendRTCP( id->sinkv[i].rtcp, out );
1323 tee( fd[0], fd[3], len, 0 );
1324 if( splice( fd[2], NULL, id->sinkv[i].rtp_fd, NULL, len,
1325 SPLICE_F_NONBLOCK ) >= 0 )
1329 splice( fd[2], NULL, fd[4], NULL, len, 0 );
1331 send( id->sinkv[i].rtp_fd, out->p_buffer, len, 0 );
1333 vlc_mutex_unlock( &id->lock_sink );
1335 block_Release( out );
1337 splice( fd[0], NULL, fd[4], NULL, len, 0 );
1342 for( unsigned i = 0; i < 5; i++ )
1347 static inline void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1349 block_FifoPut( id->p_fifo, out );
1352 int rtp_add_sink( sout_stream_id_t *id, int fd )
1354 rtp_sink_t sink = { fd, NULL };
1355 sink.rtcp = OpenRTCP( VLC_OBJECT( id->p_stream ), fd, IPPROTO_UDP );
1356 if( sink.rtcp == NULL )
1357 msg_Err( id, "RTCP failed!" );
1359 vlc_mutex_lock( &id->lock_sink );
1360 INSERT_ELEM( id->sinkv, id->sinkc, id->sinkc, sink );
1361 vlc_mutex_unlock( &id->lock_sink );
1365 void rtp_del_sink( sout_stream_id_t *id, int fd )
1367 rtp_sink_t sink = { fd, NULL };
1369 /* NOTE: must be safe to use if fd is not included */
1370 vlc_mutex_lock( &id->lock_sink );
1371 for( int i = 0; i < id->sinkc; i++ )
1373 if (id->sinkv[i].rtp_fd == fd)
1375 sink = id->sinkv[i];
1376 REMOVE_ELEM( id->sinkv, id->sinkc, i );
1380 vlc_mutex_unlock( &id->lock_sink );
1382 CloseRTCP( sink.rtcp );
1383 net_Close( sink.rtp_fd );
1386 /****************************************************************************
1388 ****************************************************************************/
1389 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1390 int b_marker, int64_t i_pts )
1392 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1394 out->p_buffer[0] = 0x80;
1395 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1396 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1397 out->p_buffer[3] = ( id->i_sequence )&0xff;
1398 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1399 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1400 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1401 out->p_buffer[7] = ( i_timestamp )&0xff;
1403 memcpy( out->p_buffer + 8, id->ssrc, 4 );
1409 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1412 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1413 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1415 uint8_t *p_data = in->p_buffer;
1416 int i_data = in->i_buffer;
1419 for( i = 0; i < i_count; i++ )
1421 int i_payload = __MIN( i_max, i_data );
1422 block_t *out = block_New( p_stream, 16 + i_payload );
1424 /* rtp common header */
1425 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1427 out->p_buffer[12] = 0;
1428 out->p_buffer[13] = 0;
1429 /* fragment offset in the current frame */
1430 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1431 out->p_buffer[15] = ( (i*i_max) )&0xff;
1432 memcpy( &out->p_buffer[16], p_data, i_payload );
1434 out->i_buffer = 16 + i_payload;
1435 out->i_dts = in->i_dts + i * in->i_length / i_count;
1436 out->i_length = in->i_length / i_count;
1438 rtp_packetize_send( id, out );
1440 p_data += i_payload;
1441 i_data -= i_payload;
1448 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1451 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1452 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1454 uint8_t *p_data = in->p_buffer;
1455 int i_data = in->i_buffer;
1457 int b_sequence_start = 0;
1458 int i_temporal_ref = 0;
1459 int i_picture_coding_type = 0;
1460 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1461 int b_start_slice = 0;
1463 /* preparse this packet to get some info */
1464 if( in->i_buffer > 4 )
1466 uint8_t *p = p_data;
1467 int i_rest = in->i_buffer;
1471 while( i_rest > 4 &&
1472 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1486 /* sequence start code */
1487 b_sequence_start = 1;
1489 else if( *p == 0x00 && i_rest >= 4 )
1492 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1493 i_picture_coding_type = (p[2] >> 3)&0x07;
1495 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1496 i_picture_coding_type == 3 ) )
1498 i_ffv = (p[3] >> 2)&0x01;
1499 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1500 if( i_rest > 4 && i_picture_coding_type == 3 )
1502 i_fbv = (p[4]>>6)&0x01;
1503 i_bfc = (p[4]>>3)&0x07;
1507 else if( *p <= 0xaf )
1514 for( i = 0; i < i_count; i++ )
1516 int i_payload = __MIN( i_max, i_data );
1517 block_t *out = block_New( p_stream,
1519 uint32_t h = ( i_temporal_ref << 16 )|
1520 ( b_sequence_start << 13 )|
1521 ( b_start_slice << 12 )|
1522 ( i == i_count - 1 ? 1 << 11 : 0 )|
1523 ( i_picture_coding_type << 8 )|
1524 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1526 /* rtp common header */
1527 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1528 in->i_pts > 0 ? in->i_pts : in->i_dts );
1530 /* 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 */
1531 out->p_buffer[12] = ( h >> 24 )&0xff;
1532 out->p_buffer[13] = ( h >> 16 )&0xff;
1533 out->p_buffer[14] = ( h >> 8 )&0xff;
1534 out->p_buffer[15] = ( h )&0xff;
1536 memcpy( &out->p_buffer[16], p_data, i_payload );
1538 out->i_buffer = 16 + i_payload;
1539 out->i_dts = in->i_dts + i * in->i_length / i_count;
1540 out->i_length = in->i_length / i_count;
1542 rtp_packetize_send( id, out );
1544 p_data += i_payload;
1545 i_data -= i_payload;
1551 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1554 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1555 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1557 uint8_t *p_data = in->p_buffer;
1558 int i_data = in->i_buffer;
1561 for( i = 0; i < i_count; i++ )
1563 int i_payload = __MIN( i_max, i_data );
1564 block_t *out = block_New( p_stream, 14 + i_payload );
1566 /* rtp common header */
1567 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1569 out->p_buffer[12] = 1;
1571 out->p_buffer[13] = 0x00;
1573 memcpy( &out->p_buffer[14], p_data, i_payload );
1575 out->i_buffer = 14 + i_payload;
1576 out->i_dts = in->i_dts + i * in->i_length / i_count;
1577 out->i_length = in->i_length / i_count;
1579 rtp_packetize_send( id, out );
1581 p_data += i_payload;
1582 i_data -= i_payload;
1588 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1591 int i_max = id->i_mtu - 12; /* payload max in one packet */
1592 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1594 uint8_t *p_data = in->p_buffer;
1595 int i_data = in->i_buffer;
1598 for( i = 0; i < i_count; i++ )
1600 int i_payload = __MIN( i_max, i_data );
1601 block_t *out = block_New( p_stream, 12 + i_payload );
1603 /* rtp common header */
1604 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1605 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1606 memcpy( &out->p_buffer[12], p_data, i_payload );
1608 out->i_buffer = 12 + i_payload;
1609 out->i_dts = in->i_dts + i * in->i_length / i_count;
1610 out->i_length = in->i_length / i_count;
1612 rtp_packetize_send( id, out );
1614 p_data += i_payload;
1615 i_data -= i_payload;
1622 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
1625 int i_max = id->i_mtu - 14; /* payload max in one packet */
1626 int latmhdrsize = in->i_buffer / 0xff + 1;
1627 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1629 uint8_t *p_data = in->p_buffer, *p_header = NULL;
1630 int i_data = in->i_buffer;
1633 for( i = 0; i < i_count; i++ )
1635 int i_payload = __MIN( i_max, i_data );
1640 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
1642 /* rtp common header */
1643 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
1644 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1648 int tmp = in->i_buffer;
1650 p_header=out->p_buffer+12;
1660 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
1662 out->i_buffer = 12 + latmhdrsize + 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;
1675 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1678 int i_max = id->i_mtu - 12; /* payload max in one packet */
1679 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1681 uint8_t *p_data = in->p_buffer;
1682 int i_data = in->i_buffer;
1687 int i_payload = (__MIN( i_max, i_data )/4)*4;
1688 block_t *out = block_New( p_stream, 12 + i_payload );
1690 /* rtp common header */
1691 rtp_packetize_common( id, out, 0,
1692 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1693 memcpy( &out->p_buffer[12], p_data, i_payload );
1695 out->i_buffer = 12 + i_payload;
1696 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1697 out->i_length = in->i_length / i_count;
1699 rtp_packetize_send( id, out );
1701 p_data += i_payload;
1702 i_data -= i_payload;
1709 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1712 int i_max = id->i_mtu - 12; /* payload max in one packet */
1713 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1715 uint8_t *p_data = in->p_buffer;
1716 int i_data = in->i_buffer;
1721 int i_payload = (__MIN( i_max, i_data )/2)*2;
1722 block_t *out = block_New( p_stream, 12 + i_payload );
1724 /* rtp common header */
1725 rtp_packetize_common( id, out, 0,
1726 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1727 memcpy( &out->p_buffer[12], p_data, i_payload );
1729 out->i_buffer = 12 + i_payload;
1730 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1731 out->i_length = in->i_length / i_count;
1733 rtp_packetize_send( id, out );
1735 p_data += i_payload;
1736 i_data -= i_payload;
1743 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1746 int i_max = id->i_mtu - 16; /* payload max in one packet */
1747 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1749 uint8_t *p_data = in->p_buffer;
1750 int i_data = in->i_buffer;
1753 for( i = 0; i < i_count; i++ )
1755 int i_payload = __MIN( i_max, i_data );
1756 block_t *out = block_New( p_stream, 16 + i_payload );
1758 /* rtp common header */
1759 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1760 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1762 /* AU headers length (bits) */
1763 out->p_buffer[12] = 0;
1764 out->p_buffer[13] = 2*8;
1765 /* for each AU length 13 bits + idx 3bits, */
1766 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1767 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1769 memcpy( &out->p_buffer[16], p_data, i_payload );
1771 out->i_buffer = 16 + i_payload;
1772 out->i_dts = in->i_dts + i * in->i_length / i_count;
1773 out->i_length = in->i_length / i_count;
1775 rtp_packetize_send( id, out );
1777 p_data += i_payload;
1778 i_data -= i_payload;
1786 #define RTP_H263_HEADER_SIZE (2) // plen = 0
1787 #define RTP_H263_PAYLOAD_START (14) // plen = 0
1788 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
1791 uint8_t *p_data = in->p_buffer;
1792 int i_data = in->i_buffer;
1794 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
1797 int b_v_bit = 0; // no pesky error resilience
1798 int i_plen = 0; // normally plen=0 for PSC packet
1799 int i_pebit = 0; // because plen=0
1804 return VLC_EGENERIC;
1806 if( p_data[0] || p_data[1] )
1808 return VLC_EGENERIC;
1810 /* remove 2 leading 0 bytes */
1813 i_count = ( i_data + i_max - 1 ) / i_max;
1815 for( i = 0; i < i_count; i++ )
1817 int i_payload = __MIN( i_max, i_data );
1818 block_t *out = block_New( p_stream,
1819 RTP_H263_PAYLOAD_START + i_payload );
1820 b_p_bit = (i == 0) ? 1 : 0;
1821 h = ( b_p_bit << 10 )|
1826 /* rtp common header */
1827 //b_m_bit = 1; // always contains end of frame
1828 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1829 in->i_pts > 0 ? in->i_pts : in->i_dts );
1832 out->p_buffer[12] = ( h >> 8 )&0xff;
1833 out->p_buffer[13] = ( h )&0xff;
1834 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1836 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1837 out->i_dts = in->i_dts + i * in->i_length / i_count;
1838 out->i_length = in->i_length / i_count;
1840 rtp_packetize_send( id, out );
1842 p_data += i_payload;
1843 i_data -= i_payload;
1851 rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
1852 const uint8_t *p_data, int i_data, int64_t i_pts,
1853 int64_t i_dts, vlc_bool_t b_last, int64_t i_length )
1855 const int i_max = id->i_mtu - 12; /* payload max in one packet */
1862 i_nal_hdr = p_data[3];
1863 i_nal_type = i_nal_hdr&0x1f;
1865 /* Skip start code */
1870 if( i_data <= i_max )
1872 /* Single NAL unit packet */
1873 block_t *out = block_New( p_stream, 12 + i_data );
1875 out->i_length = i_length;
1878 rtp_packetize_common( id, out, b_last, i_pts );
1879 out->i_buffer = 12 + i_data;
1881 memcpy( &out->p_buffer[12], p_data, i_data );
1883 rtp_packetize_send( id, out );
1887 /* FU-A Fragmentation Unit without interleaving */
1888 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1894 for( i = 0; i < i_count; i++ )
1896 const int i_payload = __MIN( i_data, i_max-2 );
1897 block_t *out = block_New( p_stream, 12 + 2 + i_payload );
1898 out->i_dts = i_dts + i * i_length / i_count;
1899 out->i_length = i_length / i_count;
1902 rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
1903 out->i_buffer = 14 + i_payload;
1906 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1908 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
1909 memcpy( &out->p_buffer[14], p_data, i_payload );
1911 rtp_packetize_send( id, out );
1913 i_data -= i_payload;
1914 p_data += i_payload;
1920 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
1923 const uint8_t *p_buffer = in->p_buffer;
1924 int i_buffer = in->i_buffer;
1926 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1932 /* Split nal units */
1933 while( i_buffer > 4 )
1936 int i_size = i_buffer;
1937 int i_skip = i_buffer;
1939 /* search nal end */
1940 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1942 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1944 /* we found another startcode */
1945 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
1950 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
1951 rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
1952 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
1953 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
1961 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
1964 int i_max = id->i_mtu - 14; /* payload max in one packet */
1965 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1967 uint8_t *p_data = in->p_buffer;
1968 int i_data = in->i_buffer;
1971 /* Only supports octet-aligned mode */
1972 for( i = 0; i < i_count; i++ )
1974 int i_payload = __MIN( i_max, i_data );
1975 block_t *out = block_New( p_stream, 14 + i_payload );
1977 /* rtp common header */
1978 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1979 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1980 /* Payload header */
1981 out->p_buffer[12] = 0xF0; /* CMR */
1982 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
1984 /* FIXME: are we fed multiple frames ? */
1985 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
1987 out->i_buffer = 14 + i_payload-1;
1988 out->i_dts = in->i_dts + i * in->i_length / i_count;
1989 out->i_length = in->i_length / i_count;
1991 rtp_packetize_send( id, out );
1993 p_data += i_payload;
1994 i_data -= i_payload;
2000 /*****************************************************************************
2002 *****************************************************************************/
2004 /** Add an ES to a non-RTP muxed stream */
2005 static sout_stream_id_t *MuxAdd( sout_stream_t *p_stream, es_format_t *p_fmt )
2007 sout_input_t *p_input;
2008 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2009 assert( p_mux != NULL );
2011 p_input = sout_MuxAddStream( p_mux, p_fmt );
2012 if( p_input == NULL )
2014 msg_Err( p_stream, "cannot add this stream to the muxer" );
2018 return (sout_stream_id_t *)p_input;
2022 static int MuxSend( sout_stream_t *p_stream, sout_stream_id_t *id,
2025 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2026 assert( p_mux != NULL );
2028 sout_MuxSendBuffer( p_mux, (sout_input_t *)id, p_buffer );
2033 /** Remove an ES from a non-RTP muxed stream */
2034 static int MuxDel( sout_stream_t *p_stream, sout_stream_id_t *id )
2036 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2037 assert( p_mux != NULL );
2039 sout_MuxDeleteStream( p_mux, (sout_input_t *)id );
2044 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
2045 const block_t *p_buffer )
2047 sout_stream_sys_t *p_sys = p_stream->p_sys;
2048 sout_stream_id_t *id = p_sys->es[0];
2050 int64_t i_dts = p_buffer->i_dts;
2052 uint8_t *p_data = p_buffer->p_buffer;
2053 unsigned int i_data = p_buffer->i_buffer;
2054 unsigned int i_max = id->i_mtu - 12;
2056 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
2060 unsigned int i_size;
2062 /* output complete packet */
2063 if( p_sys->packet &&
2064 p_sys->packet->i_buffer + i_data > i_max )
2066 rtp_packetize_send( id, p_sys->packet );
2067 p_sys->packet = NULL;
2070 if( p_sys->packet == NULL )
2072 /* allocate a new packet */
2073 p_sys->packet = block_New( p_stream, id->i_mtu );
2074 rtp_packetize_common( id, p_sys->packet, 1, i_dts );
2075 p_sys->packet->i_dts = i_dts;
2076 p_sys->packet->i_length = p_buffer->i_length / i_packet;
2077 i_dts += p_sys->packet->i_length;
2080 i_size = __MIN( i_data,
2081 (unsigned)(id->i_mtu - p_sys->packet->i_buffer) );
2083 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
2086 p_sys->packet->i_buffer += i_size;
2095 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
2098 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
2104 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
2106 p_next = p_buffer->p_next;
2107 block_Release( p_buffer );
2115 static sout_access_out_t *GrabberCreate( sout_stream_t *p_stream )
2117 sout_access_out_t *p_grab;
2119 p_grab = vlc_object_create( p_stream->p_sout, sizeof( *p_grab ) );
2120 if( p_grab == NULL )
2123 p_grab->p_module = NULL;
2124 p_grab->p_sout = p_stream->p_sout;
2125 p_grab->psz_access = strdup( "grab" );
2126 p_grab->p_cfg = NULL;
2127 p_grab->psz_path = strdup( "" );
2128 p_grab->p_sys = (sout_access_out_sys_t *)p_stream;
2129 p_grab->pf_seek = NULL;
2130 p_grab->pf_write = AccessOutGrabberWrite;