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 descriptipn")
73 #define DESC_LONGTEXT N_( \
74 "This allows you to give a short description with details about the stream, " \
75 "that will 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 PHONE_TEXT N_("Session phone number")
86 #define PHONE_LONGTEXT N_( \
87 "This allows you to give a contact telephone number for the stream, that will " \
88 "be announced in the SDP (Session Descriptor)." )
90 #define PORT_TEXT N_("Port")
91 #define PORT_LONGTEXT N_( \
92 "This allows you to specify the base port for the RTP streaming." )
93 #define PORT_AUDIO_TEXT N_("Audio port")
94 #define PORT_AUDIO_LONGTEXT N_( \
95 "This allows you to specify the default audio port for the RTP streaming." )
96 #define PORT_VIDEO_TEXT N_("Video port")
97 #define PORT_VIDEO_LONGTEXT N_( \
98 "This allows you to specify the default video port for the RTP streaming." )
100 #define TTL_TEXT N_("Hop limit (TTL)")
101 #define TTL_LONGTEXT N_( \
102 "This is the hop limit (also known as \"Time-To-Live\" or TTL) of " \
103 "the multicast packets sent by the stream output (0 = use operating " \
104 "system built-in default).")
106 #define DCCP_TEXT N_("DCCP transport")
107 #define DCCP_LONGTEXT N_( \
108 "This enables DCCP instead of UDP as a transport for RTP." )
109 #define TCP_TEXT N_("TCP transport")
110 #define TCP_LONGTEXT N_( \
111 "This enables TCP instead of UDP as a transport for RTP." )
112 #define UDP_LITE_TEXT N_("UDP-Lite transport")
113 #define UDP_LITE_LONGTEXT N_( \
114 "This enables UDP-Lite instead of UDP as a transport for RTP." )
116 #define RFC3016_TEXT N_("MP4A LATM")
117 #define RFC3016_LONGTEXT N_( \
118 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
120 static int Open ( vlc_object_t * );
121 static void Close( vlc_object_t * );
123 #define SOUT_CFG_PREFIX "sout-rtp-"
124 #define MAX_EMPTY_BLOCKS 200
127 set_shortname( _("RTP"));
128 set_description( _("RTP stream output") );
129 set_capability( "sout stream", 0 );
130 add_shortcut( "rtp" );
131 set_category( CAT_SOUT );
132 set_subcategory( SUBCAT_SOUT_STREAM );
134 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
135 DST_LONGTEXT, VLC_TRUE );
136 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
137 SDP_LONGTEXT, VLC_TRUE );
138 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
139 MUX_LONGTEXT, VLC_TRUE );
141 add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT,
142 NAME_LONGTEXT, VLC_TRUE );
143 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
144 DESC_LONGTEXT, VLC_TRUE );
145 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
146 URL_LONGTEXT, VLC_TRUE );
147 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
148 EMAIL_LONGTEXT, VLC_TRUE );
149 add_string( SOUT_CFG_PREFIX "phone", "", NULL, PHONE_TEXT,
150 PHONE_LONGTEXT, VLC_TRUE );
152 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
153 PORT_LONGTEXT, VLC_TRUE );
154 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
155 PORT_AUDIO_LONGTEXT, VLC_TRUE );
156 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
157 PORT_VIDEO_LONGTEXT, VLC_TRUE );
159 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
160 TTL_LONGTEXT, VLC_TRUE );
162 add_bool( SOUT_CFG_PREFIX "dccp", 0, NULL,
163 DCCP_TEXT, DCCP_LONGTEXT, VLC_FALSE );
164 add_bool( SOUT_CFG_PREFIX "tcp", 0, NULL,
165 TCP_TEXT, TCP_LONGTEXT, VLC_FALSE );
166 add_bool( SOUT_CFG_PREFIX "udplite", 0, NULL,
167 UDP_LITE_TEXT, UDP_LITE_LONGTEXT, VLC_FALSE );
169 add_bool( SOUT_CFG_PREFIX "mp4a-latm", 0, NULL, RFC3016_TEXT,
170 RFC3016_LONGTEXT, VLC_FALSE );
172 set_callbacks( Open, Close );
175 /*****************************************************************************
176 * Exported prototypes
177 *****************************************************************************/
178 static const char *ppsz_sout_options[] = {
179 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
180 "description", "url", "email", "phone",
181 "dccp", "tcp", "udplite",
185 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
186 static int Del ( sout_stream_t *, sout_stream_id_t * );
187 static int Send( sout_stream_t *, sout_stream_id_t *,
189 static sout_stream_id_t *MuxAdd ( sout_stream_t *, es_format_t * );
190 static int MuxDel ( sout_stream_t *, sout_stream_id_t * );
191 static int MuxSend( sout_stream_t *, sout_stream_id_t *,
194 static sout_access_out_t *GrabberCreate( sout_stream_t *p_sout );
195 static void ThreadSend( vlc_object_t *p_this );
197 static void SDPHandleUrl( sout_stream_t *, char * );
199 static int SapSetup( sout_stream_t *p_stream );
200 static int FileSetup( sout_stream_t *p_stream );
201 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
203 struct sout_stream_sys_t
209 vlc_mutex_t lock_sdp;
212 vlc_bool_t b_export_sdp_file;
216 vlc_bool_t b_export_sap;
217 session_descriptor_t *p_session;
220 httpd_host_t *p_httpd_host;
221 httpd_file_t *p_httpd_file;
227 char *psz_destination;
231 uint16_t i_port_audio;
232 uint16_t i_port_video;
235 /* when need to use a private one or when using muxer */
238 /* in case we do TS/PS over rtp */
240 sout_access_out_t *p_grab;
246 sout_stream_id_t **es;
249 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
252 typedef struct rtp_sink_t
258 struct sout_stream_id_t
262 sout_stream_t *p_stream;
264 uint32_t i_timestamp_start;
266 uint8_t i_payload_type;
277 /* Packetizer specific fields */
278 pf_rtp_packetizer_t pf_packetize;
282 vlc_mutex_t lock_sink;
285 rtsp_stream_id_t *rtsp_id;
287 block_fifo_t *p_fifo;
292 /*****************************************************************************
294 *****************************************************************************/
295 static int Open( vlc_object_t *p_this )
297 sout_stream_t *p_stream = (sout_stream_t*)p_this;
298 sout_instance_t *p_sout = p_stream->p_sout;
299 sout_stream_sys_t *p_sys = NULL;
300 config_chain_t *p_cfg = NULL;
302 vlc_bool_t b_rtsp = VLC_FALSE;
304 config_ChainParse( p_stream, SOUT_CFG_PREFIX,
305 ppsz_sout_options, p_stream->p_cfg );
307 p_sys = malloc( sizeof( sout_stream_sys_t ) );
311 p_sys->psz_destination = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" );
313 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
314 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
315 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
317 p_sys->psz_sdp_file = NULL;
319 if( p_sys->i_port_audio == p_sys->i_port_video )
321 msg_Err( p_stream, "audio and video port cannot be the same" );
322 p_sys->i_port_audio = 0;
323 p_sys->i_port_video = 0;
326 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
328 if( !strcmp( p_cfg->psz_name, "sdp" )
329 && ( p_cfg->psz_value != NULL )
330 && !strncasecmp( p_cfg->psz_value, "rtsp:", 5 ) )
338 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
341 if( !strncasecmp( psz, "rtsp:", 5 ) )
347 /* Transport protocol */
348 p_sys->proto = IPPROTO_UDP;
351 if( var_GetBool( p_stream, SOUT_CFG_PREFIX "dccp" ) )
353 p_sys->sotype = SOCK_DCCP;
354 p_sys->proto = 33 /*IPPROTO_DCCP*/;
357 if( var_GetBool( p_stream, SOUT_CFG_PREFIX "tcp" ) )
359 p_sys->sotype = SOCK_STREAM;
360 p_sys->proto = IPPROTO_TCP;
364 if( var_GetBool( p_stream, SOUT_CFG_PREFIX "udplite" ) )
365 p_sys->proto = 136 /*IPPROTO_UDPLITE*/;
367 if( ( p_sys->psz_destination == NULL ) && !b_rtsp )
369 msg_Err( p_stream, "missing destination and not in RTSP mode" );
374 p_sys->i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
375 if( p_sys->i_ttl == 0 )
377 /* Normally, we should let the default hop limit up to the core,
378 * but we have to know it to build our SDP properly, which is why
379 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
381 p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
384 p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
386 p_sys->i_payload_type = 96;
390 p_sys->psz_sdp = NULL;
392 p_sys->i_sdp_id = mdate();
393 p_sys->i_sdp_version = 1;
394 p_sys->psz_sdp = NULL;
396 p_sys->b_export_sap = VLC_FALSE;
397 p_sys->b_export_sdp_file = VLC_FALSE;
398 p_sys->p_session = NULL;
400 p_sys->p_httpd_host = NULL;
401 p_sys->p_httpd_file = NULL;
403 p_stream->p_sys = p_sys;
405 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
406 vlc_mutex_init( p_stream, &p_sys->lock_es );
408 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
411 sout_stream_id_t *id;
413 /* Check muxer type */
414 if( strncasecmp( psz, "ps", 2 )
415 && strncasecmp( psz, "mpeg1", 5 )
416 && strncasecmp( psz, "ts", 2 ) )
418 msg_Err( p_stream, "unsupported muxer type for RTP (only TS/PS)" );
420 vlc_mutex_destroy( &p_sys->lock_sdp );
421 vlc_mutex_destroy( &p_sys->lock_es );
426 p_sys->p_grab = GrabberCreate( p_stream );
427 p_sys->p_mux = sout_MuxNew( p_sout, psz, p_sys->p_grab );
430 if( p_sys->p_mux == NULL )
432 msg_Err( p_stream, "cannot create muxer" );
433 sout_AccessOutDelete( p_sys->p_grab );
434 vlc_mutex_destroy( &p_sys->lock_sdp );
435 vlc_mutex_destroy( &p_sys->lock_es );
440 id = Add( p_stream, NULL );
443 sout_MuxDelete( p_sys->p_mux );
444 sout_AccessOutDelete( p_sys->p_grab );
445 vlc_mutex_destroy( &p_sys->lock_sdp );
446 vlc_mutex_destroy( &p_sys->lock_es );
451 p_sys->packet = NULL;
453 p_stream->pf_add = MuxAdd;
454 p_stream->pf_del = MuxDel;
455 p_stream->pf_send = MuxSend;
460 p_sys->p_grab = NULL;
462 p_stream->pf_add = Add;
463 p_stream->pf_del = Del;
464 p_stream->pf_send = Send;
467 psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "sdp" );
470 config_chain_t *p_cfg;
472 SDPHandleUrl( p_stream, psz );
474 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
476 if( !strcmp( p_cfg->psz_name, "sdp" ) )
478 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
481 /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
482 if( !strcmp( p_cfg->psz_value, psz ) )
485 SDPHandleUrl( p_stream, p_cfg->psz_value );
491 /* update p_sout->i_out_pace_nocontrol */
492 p_stream->p_sout->i_out_pace_nocontrol++;
497 /*****************************************************************************
499 *****************************************************************************/
500 static void Close( vlc_object_t * p_this )
502 sout_stream_t *p_stream = (sout_stream_t*)p_this;
503 sout_stream_sys_t *p_sys = p_stream->p_sys;
505 /* update p_sout->i_out_pace_nocontrol */
506 p_stream->p_sout->i_out_pace_nocontrol--;
510 assert( p_sys->i_es == 1 );
511 Del( p_stream, p_sys->es[0] );
513 sout_MuxDelete( p_sys->p_mux );
514 sout_AccessOutDelete( p_sys->p_grab );
517 block_Release( p_sys->packet );
519 if( p_sys->b_export_sap )
522 SapSetup( p_stream );
526 if( p_sys->rtsp != NULL )
527 RtspUnsetup( p_sys->rtsp );
529 vlc_mutex_destroy( &p_sys->lock_sdp );
530 vlc_mutex_destroy( &p_sys->lock_es );
532 if( p_sys->p_httpd_file )
533 httpd_FileDelete( p_sys->p_httpd_file );
535 if( p_sys->p_httpd_host )
536 httpd_HostDelete( p_sys->p_httpd_host );
538 free( p_sys->psz_sdp );
540 if( p_sys->b_export_sdp_file )
543 unlink( p_sys->psz_sdp_file );
545 free( p_sys->psz_sdp_file );
547 free( p_sys->psz_destination );
551 /*****************************************************************************
553 *****************************************************************************/
554 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
556 sout_stream_sys_t *p_sys = p_stream->p_sys;
559 vlc_UrlParse( &url, psz_url, 0 );
560 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
562 if( p_sys->p_httpd_file )
564 msg_Err( p_stream, "you can use sdp=http:// only once" );
568 if( HttpSetup( p_stream, &url ) )
570 msg_Err( p_stream, "cannot export SDP as HTTP" );
573 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
575 if( p_sys->rtsp != NULL )
577 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
581 /* FIXME test if destination is multicast or no destination at all */
582 p_sys->rtsp = RtspSetup( p_stream, &url );
583 if( p_sys->rtsp == NULL )
585 msg_Err( p_stream, "cannot export SDP as RTSP" );
588 if( p_sys->p_mux != NULL )
590 sout_stream_id_t *id = p_sys->es[0];
591 id->rtsp_id = RtspAddId( p_sys->rtsp, id, 0,
592 p_sys->psz_destination, p_sys->i_ttl,
593 id->i_port, id->i_port + 1 );
596 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
597 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
599 p_sys->b_export_sap = VLC_TRUE;
600 SapSetup( p_stream );
602 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
604 if( p_sys->b_export_sdp_file )
606 msg_Err( p_stream, "you can use sdp=file:// only once" );
609 p_sys->b_export_sdp_file = VLC_TRUE;
610 psz_url = &psz_url[5];
611 if( psz_url[0] == '/' && psz_url[1] == '/' )
613 p_sys->psz_sdp_file = strdup( psz_url );
617 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
622 vlc_UrlClean( &url );
625 /*****************************************************************************
627 *****************************************************************************/
629 char *SDPGenerate( const sout_stream_t *p_stream, const char *rtsp_url )
631 const sout_stream_sys_t *p_sys = p_stream->p_sys;
633 struct sockaddr_storage dst;
637 * When we have a fixed destination (typically when we do multicast),
638 * we need to put the actual port numbers in the SDP.
639 * When there is no fixed destination, we only support RTSP unicast
640 * on-demand setup, so we should rather let the clients decide which ports
642 * When there is both a fixed destination and RTSP unicast, we need to
643 * put port numbers used by the fixed destination, otherwise the SDP would
644 * become totally incorrect for multicast use. It should be noted that
645 * port numbers from SDP with RTSP are only "recommendation" from the
646 * server to the clients (per RFC2326), so only broken clients will fail
647 * to handle this properly. There is no solution but to use two differents
648 * output chain with two different RTSP URLs if you need to handle this
653 if( p_sys->psz_destination != NULL )
657 /* Oh boy, this is really ugly! (+ race condition on lock_es) */
658 dstlen = sizeof( dst );
659 getpeername( p_sys->es[0]->sinkv[0].rtp_fd, (struct sockaddr *)&dst,
666 /* Dummy destination address for RTSP */
667 memset (&dst, 0, sizeof( struct sockaddr_in ) );
668 dst.ss_family = AF_INET;
672 dstlen = sizeof( struct sockaddr_in );
675 psz_sdp = vlc_sdp_Start( VLC_OBJECT( p_stream ), SOUT_CFG_PREFIX,
676 NULL, 0, (struct sockaddr *)&dst, dstlen );
677 if( psz_sdp == NULL )
680 /* TODO: a=source-filter */
682 if( rtsp_url != NULL )
683 sdp_AddAttribute ( &psz_sdp, "control", "%s", rtsp_url );
685 /* FIXME: locking?! */
686 for( i = 0; i < p_sys->i_es; i++ )
688 sout_stream_id_t *id = p_sys->es[i];
689 const char *mime_major; /* major MIME type */
694 mime_major = "video";
697 mime_major = "audio";
706 sdp_AddMedia( &psz_sdp, mime_major, "RTP/AVP", inclport * id->i_port,
707 id->i_payload_type, VLC_FALSE, id->i_bitrate,
708 id->psz_rtpmap, id->psz_fmtp);
710 if( rtsp_url != NULL )
711 sdp_AddAttribute ( &psz_sdp, "control", "%s/trackID=%d",
718 /*****************************************************************************
720 *****************************************************************************/
721 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
722 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
723 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
724 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
725 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
726 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
727 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
728 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
729 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
730 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
731 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
732 static int rtp_packetize_t140 ( sout_stream_t *, sout_stream_id_t *, block_t * );
734 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
736 static const char hex[16] = "0123456789abcdef";
739 for( i = 0; i < i_data; i++ )
741 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
742 s[2*i+1] = hex[(p_data[i] )&0xf];
748 /** Add an ES as a new RTP stream */
749 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
751 /* NOTE: As a special case, if we use a non-RTP
752 * mux (TS/PS), then p_fmt is NULL. */
753 sout_stream_sys_t *p_sys = p_stream->p_sys;
754 sout_stream_id_t *id;
755 int i_port, cscov = -1;
758 id = vlc_object_create( p_stream, sizeof( sout_stream_id_t ) );
762 /* Choose the port */
767 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
769 i_port = p_sys->i_port_audio;
770 p_sys->i_port_audio = 0;
773 if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
775 i_port = p_sys->i_port_video;
776 p_sys->i_port_video = 0;
781 if( p_sys->i_port != p_sys->i_port_audio
782 && p_sys->i_port != p_sys->i_port_video )
784 i_port = p_sys->i_port;
791 id->p_stream = p_stream;
793 id->i_timestamp_start = rand()&0xffffffff;
794 id->i_sequence = rand()&0xffff;
795 id->i_payload_type = p_sys->i_payload_type;
796 id->ssrc[0] = rand()&0xff;
797 id->ssrc[1] = rand()&0xff;
798 id->ssrc[2] = rand()&0xff;
799 id->ssrc[3] = rand()&0xff;
801 id->psz_rtpmap = NULL;
803 id->i_clock_rate = 90000; /* most common case */
807 id->i_cat = p_fmt->i_cat;
808 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
812 id->i_cat = VIDEO_ES;
816 id->pf_packetize = NULL;
817 id->i_mtu = config_GetInt( p_stream, "mtu" );
818 if( id->i_mtu <= 12 + 16 )
819 id->i_mtu = 576 - 20 - 8; /* pessimistic */
821 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
823 vlc_mutex_init( p_stream, &id->lock_sink );
829 (int64_t)1000 * var_GetInteger( p_stream, SOUT_CFG_PREFIX "caching");
830 id->p_fifo = block_FifoNew( p_stream );
832 if( vlc_thread_create( id, "RTP send thread", ThreadSend,
833 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
835 vlc_mutex_destroy( &id->lock_sink );
836 vlc_object_destroy( id );
840 if( p_sys->psz_destination != NULL )
842 int ttl = (p_sys->i_ttl > 0) ? p_sys->i_ttl : -1;
843 int fd = net_ConnectDgram( p_stream, p_sys->psz_destination,
844 i_port, ttl, p_sys->proto );
848 msg_Err( p_stream, "cannot create RTP socket" );
849 vlc_thread_join( id );
850 vlc_mutex_destroy( &id->lock_sink );
851 vlc_object_destroy( id );
854 rtp_add_sink( id, fd );
859 char *psz = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" );
861 if( psz == NULL ) /* Uho! */
864 if( strncmp( psz, "ts", 2 ) == 0 )
866 id->i_payload_type = 33;
867 id->psz_rtpmap = strdup( "MP2T/90000" );
871 id->psz_rtpmap = strdup( "MP2P/90000" );
875 switch( p_fmt->i_codec )
877 case VLC_FOURCC( 's', '1', '6', 'b' ):
878 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
880 id->i_payload_type = 11;
882 else if( p_fmt->audio.i_channels == 2 &&
883 p_fmt->audio.i_rate == 44100 )
885 id->i_payload_type = 10;
887 if( asprintf( &id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
888 p_fmt->audio.i_channels ) == -1 )
889 id->psz_rtpmap = NULL;
890 id->i_clock_rate = p_fmt->audio.i_rate;
891 id->pf_packetize = rtp_packetize_l16;
893 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
894 if( asprintf( &id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
895 p_fmt->audio.i_channels ) == -1 )
896 id->psz_rtpmap = NULL;
897 id->i_clock_rate = p_fmt->audio.i_rate;
898 id->pf_packetize = rtp_packetize_l8;
900 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
901 id->i_payload_type = 14;
902 id->psz_rtpmap = strdup( "MPA/90000" );
903 id->pf_packetize = rtp_packetize_mpa;
905 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
906 id->i_payload_type = 32;
907 id->psz_rtpmap = strdup( "MPV/90000" );
908 id->pf_packetize = rtp_packetize_mpv;
910 case VLC_FOURCC( 'a', '5', '2', ' ' ):
911 id->psz_rtpmap = strdup( "ac3/90000" );
912 id->pf_packetize = rtp_packetize_ac3;
914 case VLC_FOURCC( 'H', '2', '6', '3' ):
915 id->psz_rtpmap = strdup( "H263-1998/90000" );
916 id->pf_packetize = rtp_packetize_h263;
918 case VLC_FOURCC( 'h', '2', '6', '4' ):
919 id->psz_rtpmap = strdup( "H264/90000" );
920 id->pf_packetize = rtp_packetize_h264;
923 if( p_fmt->i_extra > 0 )
925 uint8_t *p_buffer = p_fmt->p_extra;
926 int i_buffer = p_fmt->i_extra;
927 char *p_64_sps = NULL;
928 char *p_64_pps = NULL;
931 while( i_buffer > 4 &&
932 p_buffer[0] == 0 && p_buffer[1] == 0 &&
933 p_buffer[2] == 0 && p_buffer[3] == 1 )
935 const int i_nal_type = p_buffer[4]&0x1f;
939 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
942 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
944 if( !memcmp (p_buffer + i_offset, "\x00\x00\x00\x01", 4 ) )
946 /* we found another startcode */
951 if( i_nal_type == 7 )
953 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
954 sprintf_hexa( hexa, &p_buffer[5], 3 );
956 else if( i_nal_type == 8 )
958 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
964 if( p_64_sps && p_64_pps &&
965 ( asprintf( &id->psz_fmtp,
966 "packetization-mode=1;profile-level-id=%s;"
967 "sprop-parameter-sets=%s,%s;", hexa, p_64_sps,
974 id->psz_fmtp = strdup( "packetization-mode=1" );
977 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
979 char hexa[2*p_fmt->i_extra +1];
981 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
982 id->pf_packetize = rtp_packetize_split;
983 if( p_fmt->i_extra > 0 )
985 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
986 if( asprintf( &id->psz_fmtp,
987 "profile-level-id=3; config=%s;", hexa ) == -1 )
992 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
994 id->i_clock_rate = p_fmt->audio.i_rate;
998 char hexa[2*p_fmt->i_extra +1];
1000 if( asprintf( &id->psz_rtpmap, "mpeg4-generic/%d",
1001 p_fmt->audio.i_rate ) == -1 )
1002 id->psz_rtpmap = NULL;
1003 id->pf_packetize = rtp_packetize_mp4a;
1004 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1005 if( asprintf( &id->psz_fmtp,
1006 "streamtype=5; profile-level-id=15; "
1007 "mode=AAC-hbr; config=%s; SizeLength=13; "
1008 "IndexLength=3; IndexDeltaLength=3; Profile=1;",
1010 id->psz_fmtp = NULL;
1016 unsigned char config[6];
1017 unsigned int aacsrates[15] = {
1018 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1019 16000, 12000, 11025, 8000, 7350, 0, 0 };
1021 for( i = 0; i < 15; i++ )
1022 if( p_fmt->audio.i_rate == aacsrates[i] )
1028 config[3]=p_fmt->audio.i_channels<<4;
1032 if( asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1033 p_fmt->audio.i_rate,
1034 p_fmt->audio.i_channels ) == -1)
1035 id->psz_rtpmap = NULL;
1036 id->pf_packetize = rtp_packetize_mp4a_latm;
1037 sprintf_hexa( hexa, config, 6 );
1038 if( asprintf( &id->psz_fmtp, "profile-level-id=15; "
1039 "object=2; cpresent=0; config=%s", hexa ) == -1 )
1040 id->psz_fmtp = NULL;
1044 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1045 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1046 "AMR/8000/2" : "AMR/8000" );
1047 id->psz_fmtp = strdup( "octet-align=1" );
1048 id->i_clock_rate = p_fmt->audio.i_rate;
1049 id->pf_packetize = rtp_packetize_amr;
1051 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1052 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1053 "AMR-WB/16000/2" : "AMR-WB/16000" );
1054 id->psz_fmtp = strdup( "octet-align=1" );
1055 id->i_clock_rate = p_fmt->audio.i_rate;
1056 id->pf_packetize = rtp_packetize_amr;
1058 case VLC_FOURCC( 't', '1', '4', '0' ):
1059 id->psz_rtpmap = strdup( "t140/1000" );
1060 id->i_clock_rate = 1000;
1061 id->pf_packetize = rtp_packetize_t140;
1065 msg_Err( p_stream, "cannot add this stream (unsupported "
1066 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1068 rtp_del_sink( id, id->sinkv[0].rtp_fd );
1069 vlc_thread_join( id );
1070 vlc_mutex_destroy( &id->lock_sink );
1071 vlc_object_destroy( id );
1076 cscov += 8 /* UDP */ + 12 /* RTP */;
1078 net_SetCSCov( id->sinkv[0].rtp_fd, cscov, -1 );
1080 if( id->i_payload_type == p_sys->i_payload_type )
1081 p_sys->i_payload_type++;
1083 if( p_sys->rtsp != NULL )
1084 id->rtsp_id = RtspAddId( p_sys->rtsp, id, p_sys->i_es,
1085 p_sys->psz_destination,
1086 p_sys->i_ttl, id->i_port, id->i_port + 1 );
1088 /* Update p_sys context */
1089 vlc_mutex_lock( &p_sys->lock_es );
1090 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1091 vlc_mutex_unlock( &p_sys->lock_es );
1093 psz_sdp = SDPGenerate( p_stream, NULL );
1095 vlc_mutex_lock( &p_sys->lock_sdp );
1096 free( p_sys->psz_sdp );
1097 p_sys->psz_sdp = psz_sdp;
1098 vlc_mutex_unlock( &p_sys->lock_sdp );
1100 p_sys->i_sdp_version++;
1102 msg_Dbg( p_stream, "sdp=\n%s", p_sys->psz_sdp );
1104 /* Update SDP (sap/file) */
1105 if( p_sys->b_export_sap ) SapSetup( p_stream );
1106 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1108 vlc_object_attach( id, p_stream );
1112 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1114 sout_stream_sys_t *p_sys = p_stream->p_sys;
1116 vlc_object_kill( id );
1117 block_FifoWake( id->p_fifo );
1119 vlc_mutex_lock( &p_sys->lock_es );
1120 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1121 vlc_mutex_unlock( &p_sys->lock_es );
1124 if( id->i_port > 0 )
1126 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1127 p_sys->i_port_audio = id->i_port;
1128 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1129 p_sys->i_port_video = id->i_port;
1132 free( id->psz_rtpmap );
1133 free( id->psz_fmtp );
1136 RtspDelId( p_sys->rtsp, id->rtsp_id );
1138 rtp_del_sink( id, id->sinkv[0].rtp_fd ); /* sink for explicit dst= */
1140 vlc_thread_join( id );
1141 vlc_mutex_destroy( &id->lock_sink );
1142 block_FifoRelease( id->p_fifo );
1144 /* Update SDP (sap/file) */
1145 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1146 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1148 vlc_object_detach( id );
1149 vlc_object_destroy( id );
1153 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1158 assert( p_stream->p_sys->p_mux == NULL );
1160 while( p_buffer != NULL )
1162 p_next = p_buffer->p_next;
1163 if( id->pf_packetize( p_stream, id, p_buffer ) )
1166 block_Release( p_buffer );
1172 /****************************************************************************
1174 ****************************************************************************/
1175 static int SapSetup( sout_stream_t *p_stream )
1177 sout_stream_sys_t *p_sys = p_stream->p_sys;
1178 sout_instance_t *p_sout = p_stream->p_sout;
1180 /* Remove the previous session */
1181 if( p_sys->p_session != NULL)
1183 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1184 p_sys->p_session = NULL;
1187 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1189 announce_method_t *p_method = sout_SAPMethod();
1190 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
1192 p_sys->psz_destination,
1194 sout_MethodRelease( p_method );
1200 /****************************************************************************
1202 ****************************************************************************/
1203 static int FileSetup( sout_stream_t *p_stream )
1205 sout_stream_sys_t *p_sys = p_stream->p_sys;
1208 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1210 msg_Err( p_stream, "cannot open file '%s' (%s)",
1211 p_sys->psz_sdp_file, strerror(errno) );
1212 return VLC_EGENERIC;
1215 fputs( p_sys->psz_sdp, f );
1221 /****************************************************************************
1223 ****************************************************************************/
1224 static int HttpCallback( httpd_file_sys_t *p_args,
1225 httpd_file_t *, uint8_t *p_request,
1226 uint8_t **pp_data, int *pi_data );
1228 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1230 sout_stream_sys_t *p_sys = p_stream->p_sys;
1232 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host,
1233 url->i_port > 0 ? url->i_port : 80 );
1234 if( p_sys->p_httpd_host )
1236 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1237 url->psz_path ? url->psz_path : "/",
1240 HttpCallback, (void*)p_sys );
1242 if( p_sys->p_httpd_file == NULL )
1244 return VLC_EGENERIC;
1249 static int HttpCallback( httpd_file_sys_t *p_args,
1250 httpd_file_t *f, uint8_t *p_request,
1251 uint8_t **pp_data, int *pi_data )
1253 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1255 vlc_mutex_lock( &p_sys->lock_sdp );
1256 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1258 *pi_data = strlen( p_sys->psz_sdp );
1259 *pp_data = malloc( *pi_data );
1260 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1267 vlc_mutex_unlock( &p_sys->lock_sdp );
1272 /****************************************************************************
1274 ****************************************************************************/
1275 static void ThreadSend( vlc_object_t *p_this )
1277 sout_stream_id_t *id = (sout_stream_id_t *)p_this;
1278 unsigned i_caching = id->i_caching;
1280 int fd[5] = { -1, -1, -1, -1, -1 };
1288 fd[4] = open( "/dev/null", O_WRONLY );
1293 block_t *out = block_FifoGet( id->p_fifo );
1295 continue; /* Forced wakeup */
1297 mtime_t i_date = out->i_dts + i_caching;
1298 ssize_t len = out->i_buffer;
1302 len = write( fd[1], out->p_buffer, len);
1304 continue; /* Uho - should not happen */
1308 vlc_mutex_lock( &id->lock_sink );
1309 for( int i = 0; i < id->sinkc; i++ )
1311 SendRTCP( id->sinkv[i].rtcp, out );
1314 tee( fd[0], fd[3], len, 0 );
1315 if( splice( fd[2], NULL, id->sinkv[i].rtp_fd, NULL, len,
1316 SPLICE_F_NONBLOCK ) >= 0 )
1320 splice( fd[2], NULL, fd[4], NULL, len, 0 );
1322 send( id->sinkv[i].rtp_fd, out->p_buffer, len, 0 );
1324 vlc_mutex_unlock( &id->lock_sink );
1326 block_Release( out );
1328 splice( fd[0], NULL, fd[4], NULL, len, 0 );
1333 for( unsigned i = 0; i < 5; i++ )
1338 static inline void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1340 block_FifoPut( id->p_fifo, out );
1343 int rtp_add_sink( sout_stream_id_t *id, int fd )
1345 rtp_sink_t sink = { fd, NULL };
1346 sink.rtcp = OpenRTCP( VLC_OBJECT( id->p_stream ), fd, IPPROTO_UDP );
1347 if( sink.rtcp == NULL )
1348 msg_Err( id, "RTCP failed!" );
1350 vlc_mutex_lock( &id->lock_sink );
1351 INSERT_ELEM( id->sinkv, id->sinkc, id->sinkc, sink );
1352 vlc_mutex_unlock( &id->lock_sink );
1356 void rtp_del_sink( sout_stream_id_t *id, int fd )
1358 rtp_sink_t sink = { fd, NULL };
1360 /* NOTE: must be safe to use if fd is not included */
1361 vlc_mutex_lock( &id->lock_sink );
1362 for( int i = 0; i < id->sinkc; i++ )
1364 if (id->sinkv[i].rtp_fd == fd)
1366 sink = id->sinkv[i];
1367 REMOVE_ELEM( id->sinkv, id->sinkc, i );
1371 vlc_mutex_unlock( &id->lock_sink );
1373 CloseRTCP( sink.rtcp );
1374 net_Close( sink.rtp_fd );
1377 /****************************************************************************
1379 ****************************************************************************/
1380 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1381 int b_marker, int64_t i_pts )
1383 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1385 out->p_buffer[0] = 0x80;
1386 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1387 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1388 out->p_buffer[3] = ( id->i_sequence )&0xff;
1389 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1390 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1391 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1392 out->p_buffer[7] = ( i_timestamp )&0xff;
1394 memcpy( out->p_buffer + 8, id->ssrc, 4 );
1400 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1403 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1404 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1406 uint8_t *p_data = in->p_buffer;
1407 int i_data = in->i_buffer;
1410 for( i = 0; i < i_count; i++ )
1412 int i_payload = __MIN( i_max, i_data );
1413 block_t *out = block_New( p_stream, 16 + i_payload );
1415 /* rtp common header */
1416 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1418 out->p_buffer[12] = 0;
1419 out->p_buffer[13] = 0;
1420 /* fragment offset in the current frame */
1421 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1422 out->p_buffer[15] = ( (i*i_max) )&0xff;
1423 memcpy( &out->p_buffer[16], p_data, i_payload );
1425 out->i_buffer = 16 + i_payload;
1426 out->i_dts = in->i_dts + i * in->i_length / i_count;
1427 out->i_length = in->i_length / i_count;
1429 rtp_packetize_send( id, out );
1431 p_data += i_payload;
1432 i_data -= i_payload;
1439 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1442 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1443 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1445 uint8_t *p_data = in->p_buffer;
1446 int i_data = in->i_buffer;
1448 int b_sequence_start = 0;
1449 int i_temporal_ref = 0;
1450 int i_picture_coding_type = 0;
1451 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1452 int b_start_slice = 0;
1454 /* preparse this packet to get some info */
1455 if( in->i_buffer > 4 )
1457 uint8_t *p = p_data;
1458 int i_rest = in->i_buffer;
1462 while( i_rest > 4 &&
1463 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1477 /* sequence start code */
1478 b_sequence_start = 1;
1480 else if( *p == 0x00 && i_rest >= 4 )
1483 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1484 i_picture_coding_type = (p[2] >> 3)&0x07;
1486 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1487 i_picture_coding_type == 3 ) )
1489 i_ffv = (p[3] >> 2)&0x01;
1490 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1491 if( i_rest > 4 && i_picture_coding_type == 3 )
1493 i_fbv = (p[4]>>6)&0x01;
1494 i_bfc = (p[4]>>3)&0x07;
1498 else if( *p <= 0xaf )
1505 for( i = 0; i < i_count; i++ )
1507 int i_payload = __MIN( i_max, i_data );
1508 block_t *out = block_New( p_stream,
1510 uint32_t h = ( i_temporal_ref << 16 )|
1511 ( b_sequence_start << 13 )|
1512 ( b_start_slice << 12 )|
1513 ( i == i_count - 1 ? 1 << 11 : 0 )|
1514 ( i_picture_coding_type << 8 )|
1515 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1517 /* rtp common header */
1518 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1519 in->i_pts > 0 ? in->i_pts : in->i_dts );
1521 /* 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 */
1522 out->p_buffer[12] = ( h >> 24 )&0xff;
1523 out->p_buffer[13] = ( h >> 16 )&0xff;
1524 out->p_buffer[14] = ( h >> 8 )&0xff;
1525 out->p_buffer[15] = ( h )&0xff;
1527 memcpy( &out->p_buffer[16], p_data, i_payload );
1529 out->i_buffer = 16 + i_payload;
1530 out->i_dts = in->i_dts + i * in->i_length / i_count;
1531 out->i_length = in->i_length / i_count;
1533 rtp_packetize_send( id, out );
1535 p_data += i_payload;
1536 i_data -= i_payload;
1542 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1545 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1546 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1548 uint8_t *p_data = in->p_buffer;
1549 int i_data = in->i_buffer;
1552 for( i = 0; i < i_count; i++ )
1554 int i_payload = __MIN( i_max, i_data );
1555 block_t *out = block_New( p_stream, 14 + i_payload );
1557 /* rtp common header */
1558 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1560 out->p_buffer[12] = 1;
1562 out->p_buffer[13] = 0x00;
1564 memcpy( &out->p_buffer[14], p_data, i_payload );
1566 out->i_buffer = 14 + i_payload;
1567 out->i_dts = in->i_dts + i * in->i_length / i_count;
1568 out->i_length = in->i_length / i_count;
1570 rtp_packetize_send( id, out );
1572 p_data += i_payload;
1573 i_data -= i_payload;
1579 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1582 int i_max = id->i_mtu - 12; /* payload max in one packet */
1583 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1585 uint8_t *p_data = in->p_buffer;
1586 int i_data = in->i_buffer;
1589 for( i = 0; i < i_count; i++ )
1591 int i_payload = __MIN( i_max, i_data );
1592 block_t *out = block_New( p_stream, 12 + i_payload );
1594 /* rtp common header */
1595 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1596 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1597 memcpy( &out->p_buffer[12], p_data, i_payload );
1599 out->i_buffer = 12 + i_payload;
1600 out->i_dts = in->i_dts + i * in->i_length / i_count;
1601 out->i_length = in->i_length / i_count;
1603 rtp_packetize_send( id, out );
1605 p_data += i_payload;
1606 i_data -= i_payload;
1613 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
1616 int i_max = id->i_mtu - 14; /* payload max in one packet */
1617 int latmhdrsize = in->i_buffer / 0xff + 1;
1618 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1620 uint8_t *p_data = in->p_buffer, *p_header = NULL;
1621 int i_data = in->i_buffer;
1624 for( i = 0; i < i_count; i++ )
1626 int i_payload = __MIN( i_max, i_data );
1631 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
1633 /* rtp common header */
1634 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
1635 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1639 int tmp = in->i_buffer;
1641 p_header=out->p_buffer+12;
1651 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
1653 out->i_buffer = 12 + latmhdrsize + i_payload;
1654 out->i_dts = in->i_dts + i * in->i_length / i_count;
1655 out->i_length = in->i_length / i_count;
1657 rtp_packetize_send( id, out );
1659 p_data += i_payload;
1660 i_data -= i_payload;
1666 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1669 int i_max = id->i_mtu - 12; /* payload max in one packet */
1670 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1672 uint8_t *p_data = in->p_buffer;
1673 int i_data = in->i_buffer;
1678 int i_payload = (__MIN( i_max, i_data )/4)*4;
1679 block_t *out = block_New( p_stream, 12 + i_payload );
1681 /* rtp common header */
1682 rtp_packetize_common( id, out, 0,
1683 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1684 memcpy( &out->p_buffer[12], p_data, i_payload );
1686 out->i_buffer = 12 + i_payload;
1687 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1688 out->i_length = in->i_length / i_count;
1690 rtp_packetize_send( id, out );
1692 p_data += i_payload;
1693 i_data -= i_payload;
1700 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1703 int i_max = id->i_mtu - 12; /* payload max in one packet */
1704 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1706 uint8_t *p_data = in->p_buffer;
1707 int i_data = in->i_buffer;
1712 int i_payload = (__MIN( i_max, i_data )/2)*2;
1713 block_t *out = block_New( p_stream, 12 + i_payload );
1715 /* rtp common header */
1716 rtp_packetize_common( id, out, 0,
1717 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1718 memcpy( &out->p_buffer[12], p_data, i_payload );
1720 out->i_buffer = 12 + i_payload;
1721 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1722 out->i_length = in->i_length / i_count;
1724 rtp_packetize_send( id, out );
1726 p_data += i_payload;
1727 i_data -= i_payload;
1734 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1737 int i_max = id->i_mtu - 16; /* payload max in one packet */
1738 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1740 uint8_t *p_data = in->p_buffer;
1741 int i_data = in->i_buffer;
1744 for( i = 0; i < i_count; i++ )
1746 int i_payload = __MIN( i_max, i_data );
1747 block_t *out = block_New( p_stream, 16 + i_payload );
1749 /* rtp common header */
1750 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1751 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1753 /* AU headers length (bits) */
1754 out->p_buffer[12] = 0;
1755 out->p_buffer[13] = 2*8;
1756 /* for each AU length 13 bits + idx 3bits, */
1757 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1758 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1760 memcpy( &out->p_buffer[16], p_data, i_payload );
1762 out->i_buffer = 16 + i_payload;
1763 out->i_dts = in->i_dts + i * in->i_length / i_count;
1764 out->i_length = in->i_length / i_count;
1766 rtp_packetize_send( id, out );
1768 p_data += i_payload;
1769 i_data -= i_payload;
1777 #define RTP_H263_HEADER_SIZE (2) // plen = 0
1778 #define RTP_H263_PAYLOAD_START (14) // plen = 0
1779 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
1782 uint8_t *p_data = in->p_buffer;
1783 int i_data = in->i_buffer;
1785 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
1788 int b_v_bit = 0; // no pesky error resilience
1789 int i_plen = 0; // normally plen=0 for PSC packet
1790 int i_pebit = 0; // because plen=0
1795 return VLC_EGENERIC;
1797 if( p_data[0] || p_data[1] )
1799 return VLC_EGENERIC;
1801 /* remove 2 leading 0 bytes */
1804 i_count = ( i_data + i_max - 1 ) / i_max;
1806 for( i = 0; i < i_count; i++ )
1808 int i_payload = __MIN( i_max, i_data );
1809 block_t *out = block_New( p_stream,
1810 RTP_H263_PAYLOAD_START + i_payload );
1811 b_p_bit = (i == 0) ? 1 : 0;
1812 h = ( b_p_bit << 10 )|
1817 /* rtp common header */
1818 //b_m_bit = 1; // always contains end of frame
1819 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1820 in->i_pts > 0 ? in->i_pts : in->i_dts );
1823 out->p_buffer[12] = ( h >> 8 )&0xff;
1824 out->p_buffer[13] = ( h )&0xff;
1825 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1827 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1828 out->i_dts = in->i_dts + i * in->i_length / i_count;
1829 out->i_length = in->i_length / i_count;
1831 rtp_packetize_send( id, out );
1833 p_data += i_payload;
1834 i_data -= i_payload;
1842 rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
1843 const uint8_t *p_data, int i_data, int64_t i_pts,
1844 int64_t i_dts, vlc_bool_t b_last, int64_t i_length )
1846 const int i_max = id->i_mtu - 12; /* payload max in one packet */
1853 i_nal_hdr = p_data[3];
1854 i_nal_type = i_nal_hdr&0x1f;
1856 /* Skip start code */
1861 if( i_data <= i_max )
1863 /* Single NAL unit packet */
1864 block_t *out = block_New( p_stream, 12 + i_data );
1866 out->i_length = i_length;
1869 rtp_packetize_common( id, out, b_last, i_pts );
1870 out->i_buffer = 12 + i_data;
1872 memcpy( &out->p_buffer[12], p_data, i_data );
1874 rtp_packetize_send( id, out );
1878 /* FU-A Fragmentation Unit without interleaving */
1879 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
1885 for( i = 0; i < i_count; i++ )
1887 const int i_payload = __MIN( i_data, i_max-2 );
1888 block_t *out = block_New( p_stream, 12 + 2 + i_payload );
1889 out->i_dts = i_dts + i * i_length / i_count;
1890 out->i_length = i_length / i_count;
1893 rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
1894 out->i_buffer = 14 + i_payload;
1897 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
1899 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
1900 memcpy( &out->p_buffer[14], p_data, i_payload );
1902 rtp_packetize_send( id, out );
1904 i_data -= i_payload;
1905 p_data += i_payload;
1911 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
1914 const uint8_t *p_buffer = in->p_buffer;
1915 int i_buffer = in->i_buffer;
1917 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
1923 /* Split nal units */
1924 while( i_buffer > 4 )
1927 int i_size = i_buffer;
1928 int i_skip = i_buffer;
1930 /* search nal end */
1931 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
1933 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
1935 /* we found another startcode */
1936 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
1941 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
1942 rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
1943 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
1944 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
1952 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
1955 int i_max = id->i_mtu - 14; /* payload max in one packet */
1956 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1958 uint8_t *p_data = in->p_buffer;
1959 int i_data = in->i_buffer;
1962 /* Only supports octet-aligned mode */
1963 for( i = 0; i < i_count; i++ )
1965 int i_payload = __MIN( i_max, i_data );
1966 block_t *out = block_New( p_stream, 14 + i_payload );
1968 /* rtp common header */
1969 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1970 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1971 /* Payload header */
1972 out->p_buffer[12] = 0xF0; /* CMR */
1973 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
1975 /* FIXME: are we fed multiple frames ? */
1976 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
1978 out->i_buffer = 14 + i_payload-1;
1979 out->i_dts = in->i_dts + i * in->i_length / i_count;
1980 out->i_length = in->i_length / i_count;
1982 rtp_packetize_send( id, out );
1984 p_data += i_payload;
1985 i_data -= i_payload;
1991 static int rtp_packetize_t140( sout_stream_t *p_stream, sout_stream_id_t *id,
1994 const size_t i_max = id->i_mtu - 12;
1995 const uint8_t *p_data = in->p_buffer;
1996 size_t i_data = in->i_buffer;
1998 for( unsigned i_packet = 0; i_data > 0; i_packet++ )
2000 size_t i_payload = i_data;
2002 /* Make sure we stop on an UTF-8 character boundary
2003 * (assuming the input is valid UTF-8) */
2004 if( i_data > i_max )
2008 while( ( p_data[i_payload] & 0xC0 ) == 0x80 )
2010 if( i_payload == 0 )
2011 return VLC_SUCCESS; /* fishy input! */
2017 block_t *out = block_New( p_stream, 12 + i_payload );
2021 rtp_packetize_common( id, out, 0, in->i_pts + i_packet );
2022 memcpy( out->p_buffer + 12, p_data, i_payload );
2024 out->i_buffer = 12 + i_payload;
2025 out->i_dts = out->i_pts;
2028 rtp_packetize_send( id, out );
2030 p_data += i_payload;
2031 i_data -= i_payload;
2037 /*****************************************************************************
2039 *****************************************************************************/
2041 /** Add an ES to a non-RTP muxed stream */
2042 static sout_stream_id_t *MuxAdd( sout_stream_t *p_stream, es_format_t *p_fmt )
2044 sout_input_t *p_input;
2045 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2046 assert( p_mux != NULL );
2048 p_input = sout_MuxAddStream( p_mux, p_fmt );
2049 if( p_input == NULL )
2051 msg_Err( p_stream, "cannot add this stream to the muxer" );
2055 return (sout_stream_id_t *)p_input;
2059 static int MuxSend( sout_stream_t *p_stream, sout_stream_id_t *id,
2062 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2063 assert( p_mux != NULL );
2065 sout_MuxSendBuffer( p_mux, (sout_input_t *)id, p_buffer );
2070 /** Remove an ES from a non-RTP muxed stream */
2071 static int MuxDel( sout_stream_t *p_stream, sout_stream_id_t *id )
2073 sout_mux_t *p_mux = p_stream->p_sys->p_mux;
2074 assert( p_mux != NULL );
2076 sout_MuxDeleteStream( p_mux, (sout_input_t *)id );
2081 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
2082 const block_t *p_buffer )
2084 sout_stream_sys_t *p_sys = p_stream->p_sys;
2085 sout_stream_id_t *id = p_sys->es[0];
2087 int64_t i_dts = p_buffer->i_dts;
2089 uint8_t *p_data = p_buffer->p_buffer;
2090 unsigned int i_data = p_buffer->i_buffer;
2091 unsigned int i_max = id->i_mtu - 12;
2093 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
2097 unsigned int i_size;
2099 /* output complete packet */
2100 if( p_sys->packet &&
2101 p_sys->packet->i_buffer + i_data > i_max )
2103 rtp_packetize_send( id, p_sys->packet );
2104 p_sys->packet = NULL;
2107 if( p_sys->packet == NULL )
2109 /* allocate a new packet */
2110 p_sys->packet = block_New( p_stream, id->i_mtu );
2111 rtp_packetize_common( id, p_sys->packet, 1, i_dts );
2112 p_sys->packet->i_dts = i_dts;
2113 p_sys->packet->i_length = p_buffer->i_length / i_packet;
2114 i_dts += p_sys->packet->i_length;
2117 i_size = __MIN( i_data,
2118 (unsigned)(id->i_mtu - p_sys->packet->i_buffer) );
2120 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
2123 p_sys->packet->i_buffer += i_size;
2132 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
2135 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
2141 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
2143 p_next = p_buffer->p_next;
2144 block_Release( p_buffer );
2152 static sout_access_out_t *GrabberCreate( sout_stream_t *p_stream )
2154 sout_access_out_t *p_grab;
2156 p_grab = vlc_object_create( p_stream->p_sout, sizeof( *p_grab ) );
2157 if( p_grab == NULL )
2160 p_grab->p_module = NULL;
2161 p_grab->p_sout = p_stream->p_sout;
2162 p_grab->psz_access = strdup( "grab" );
2163 p_grab->p_cfg = NULL;
2164 p_grab->psz_path = strdup( "" );
2165 p_grab->p_sys = (sout_access_out_sys_t *)p_stream;
2166 p_grab->pf_seek = NULL;
2167 p_grab->pf_write = AccessOutGrabberWrite;