1 /*****************************************************************************
2 * rtp.c: rtp stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
38 #include <vlc_block.h>
40 #include <vlc_httpd.h>
42 #include <vlc_network.h>
43 #include <vlc_charset.h>
45 /*****************************************************************************
47 *****************************************************************************/
51 #define DST_TEXT N_("Destination")
52 #define DST_LONGTEXT N_( \
53 "This is the output URL that will be used." )
54 #define SDP_TEXT N_("SDP")
55 #define SDP_LONGTEXT N_( \
56 "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
57 "session will be made available. You must use an url: http://location to " \
58 "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
59 "for the SDP to be announced via SAP." )
60 #define MUX_TEXT N_("Muxer")
61 #define MUX_LONGTEXT N_( \
62 "This allows you to specify the muxer used for the streaming output. " \
63 "Default is to use no muxer (standard RTP stream)." )
65 #define NAME_TEXT N_("Session name")
66 #define NAME_LONGTEXT N_( \
67 "This is the name of the session that will be announced in the SDP " \
68 "(Session Descriptor)." )
69 #define DESC_TEXT N_("Session description")
70 #define DESC_LONGTEXT N_( \
71 "This allows you to give a broader description of the stream, that will " \
72 "be announced in the SDP (Session Descriptor)." )
73 #define URL_TEXT N_("Session URL")
74 #define URL_LONGTEXT N_( \
75 "This allows you to give an URL with more details about the stream " \
76 "(often the website of the streaming organization), that will " \
77 "be announced in the SDP (Session Descriptor)." )
78 #define EMAIL_TEXT N_("Session email")
79 #define EMAIL_LONGTEXT N_( \
80 "This allows you to give a contact mail address for the stream, that will " \
81 "be announced in the SDP (Session Descriptor)." )
82 #define PORT_TEXT N_("Port")
83 #define PORT_LONGTEXT N_( \
84 "This allows you to specify the base port for the RTP streaming." )
85 #define PORT_AUDIO_TEXT N_("Audio port")
86 #define PORT_AUDIO_LONGTEXT N_( \
87 "This allows you to specify the default audio port for the RTP streaming." )
88 #define PORT_VIDEO_TEXT N_("Video port")
89 #define PORT_VIDEO_LONGTEXT N_( \
90 "This allows you to specify the default video port for the RTP streaming." )
92 #define TTL_TEXT N_("Hop limit (TTL)")
93 #define TTL_LONGTEXT N_( \
94 "This is the hop limit (also known as \"Time-To-Live\" or TTL) of " \
95 "the multicast packets sent by the stream output (0 = use operating " \
96 "system built-in default).")
98 #define RFC3016_TEXT N_("MP4A LATM")
99 #define RFC3016_LONGTEXT N_( \
100 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
102 static int Open ( vlc_object_t * );
103 static void Close( vlc_object_t * );
105 #define SOUT_CFG_PREFIX "sout-rtp-"
108 set_shortname( _("RTP"));
109 set_description( _("RTP stream output") );
110 set_capability( "sout stream", 0 );
111 add_shortcut( "rtp" );
112 set_category( CAT_SOUT );
113 set_subcategory( SUBCAT_SOUT_STREAM );
115 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
116 DST_LONGTEXT, VLC_TRUE );
117 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
118 SDP_LONGTEXT, VLC_TRUE );
119 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
120 MUX_LONGTEXT, VLC_TRUE );
122 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
123 NAME_LONGTEXT, VLC_TRUE );
124 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
125 DESC_LONGTEXT, VLC_TRUE );
126 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
127 URL_LONGTEXT, VLC_TRUE );
128 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
129 EMAIL_LONGTEXT, VLC_TRUE );
131 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
132 PORT_LONGTEXT, VLC_TRUE );
133 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
134 PORT_AUDIO_LONGTEXT, VLC_TRUE );
135 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
136 PORT_VIDEO_LONGTEXT, VLC_TRUE );
138 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
139 TTL_LONGTEXT, VLC_TRUE );
141 add_bool( SOUT_CFG_PREFIX "mp4a-latm", 0, NULL, RFC3016_TEXT,
142 RFC3016_LONGTEXT, VLC_FALSE );
144 set_callbacks( Open, Close );
147 /*****************************************************************************
148 * Exported prototypes
149 *****************************************************************************/
150 static const char *ppsz_sout_options[] = {
151 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
152 "description", "url","email", "mp4a-latm", NULL
155 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
156 static int Del ( sout_stream_t *, sout_stream_id_t * );
157 static int Send( sout_stream_t *, sout_stream_id_t *,
160 /* For unicast/interleaved streaming */
164 int64_t i_last; /* for timeout */
166 /* is it in "play" state */
167 vlc_bool_t b_playing;
169 /* output (id-access) */
171 sout_stream_id_t **id;
173 sout_access_out_t **access;
176 struct sout_stream_sys_t
182 vlc_mutex_t lock_sdp;
184 char *psz_session_name;
185 char *psz_session_description;
186 char *psz_session_url;
187 char *psz_session_email;
190 vlc_bool_t b_export_sdp_file;
193 vlc_bool_t b_export_sap;
194 session_descriptor_t *p_session;
196 httpd_host_t *p_httpd_host;
197 httpd_file_t *p_httpd_file;
199 httpd_host_t *p_rtsp_host;
200 httpd_url_t *p_rtsp_url;
201 char *psz_rtsp_control;
205 char *psz_destination;
212 /* when need to use a private one or when using muxer */
215 /* in case we do TS/PS over rtp */
217 sout_access_out_t *p_access;
219 sout_access_out_t *p_grab;
221 uint32_t i_timestamp_start;
228 sout_stream_id_t **es;
232 rtsp_client_t **rtsp;
235 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
238 struct sout_stream_id_t
240 sout_stream_t *p_stream;
242 uint8_t i_payload_type;
244 uint32_t i_timestamp_start;
251 char *psz_destination;
256 /* Packetizer specific fields */
257 pf_rtp_packetizer_t pf_packetize;
260 /* for sending the packets */
261 sout_access_out_t *p_access;
263 vlc_mutex_t lock_rtsp;
265 sout_access_out_t **rtsp_access;
268 sout_input_t *p_input;
270 /* RTSP url control */
271 httpd_url_t *p_rtsp_url;
274 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
276 static void SDPHandleUrl( sout_stream_t *, char * );
278 static int SapSetup( sout_stream_t *p_stream );
279 static int FileSetup( sout_stream_t *p_stream );
280 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
281 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
283 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
284 httpd_message_t *, httpd_message_t * );
285 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
286 httpd_message_t *, httpd_message_t * );
289 static rtsp_client_t *RtspClientNew( sout_stream_t *, const char *psz_session );
290 static rtsp_client_t *RtspClientGet( sout_stream_t *, const char *psz_session );
291 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
293 /*****************************************************************************
295 *****************************************************************************/
296 static int Open( vlc_object_t *p_this )
298 sout_stream_t *p_stream = (sout_stream_t*)p_this;
299 sout_instance_t *p_sout = p_stream->p_sout;
300 sout_stream_sys_t *p_sys = NULL;
301 config_chain_t *p_cfg = NULL;
303 vlc_bool_t b_rtsp = VLC_FALSE;
305 config_ChainParse( p_stream, SOUT_CFG_PREFIX,
306 ppsz_sout_options, p_stream->p_cfg );
308 p_sys = malloc( sizeof( sout_stream_sys_t ) );
310 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
311 if( *p_sys->psz_destination == '\0' )
313 free( p_sys->psz_destination );
314 p_sys->psz_destination = NULL;
317 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
318 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
319 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
320 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
322 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
323 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
324 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
326 p_sys->psz_sdp_file = NULL;
328 if( p_sys->i_port_audio == p_sys->i_port_video )
330 msg_Err( p_stream, "audio and video port cannot be the same" );
331 p_sys->i_port_audio = 0;
332 p_sys->i_port_video = 0;
335 if( !p_sys->psz_session_name )
337 if( p_sys->psz_destination )
338 p_sys->psz_session_name = strdup( p_sys->psz_destination );
340 p_sys->psz_session_name = strdup( "NONE" );
343 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
345 if( !strcmp( p_cfg->psz_name, "sdp" ) )
347 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
357 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
358 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
360 free( val2.psz_string );
363 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
367 msg_Err( p_stream, "missing destination and not in rtsp mode" );
371 p_sys->psz_destination = NULL;
373 else if( p_sys->i_port <= 0 )
375 msg_Err( p_stream, "invalid port" );
380 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
383 /* Normally, we should let the default hop limit up to the core,
384 * but we have to know it to build our SDP properly, which is why
385 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
387 val.i_int = config_GetInt( p_stream, "ttl" );
389 if( val.i_int > 255 ) val.i_int = 255;
390 /* must not exceed 999 once formatted */
394 msg_Warn( p_stream, "illegal TTL %d, using 1", val.i_int );
397 p_sys->i_ttl = val.i_int;
400 var_Get( p_stream, SOUT_CFG_PREFIX "mp4a-latm", &val );
401 p_sys->b_latm = val.b_bool;
403 p_sys->i_payload_type = 96;
408 p_sys->psz_sdp = NULL;
410 p_sys->i_sdp_id = mdate();
411 p_sys->i_sdp_version = 1;
412 p_sys->psz_sdp = NULL;
414 p_sys->b_export_sap = VLC_FALSE;
415 p_sys->b_export_sdp_file = VLC_FALSE;
416 p_sys->p_session = NULL;
418 p_sys->p_httpd_host = NULL;
419 p_sys->p_httpd_file = NULL;
420 p_sys->p_rtsp_host = NULL;
421 p_sys->p_rtsp_url = NULL;
422 p_sys->psz_rtsp_control = NULL;
423 p_sys->psz_rtsp_path = NULL;
425 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
426 vlc_mutex_init( p_stream, &p_sys->lock_es );
428 p_stream->pf_add = Add;
429 p_stream->pf_del = Del;
430 p_stream->pf_send = Send;
432 p_stream->p_sys = p_sys;
434 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
435 if( *val.psz_string )
437 sout_access_out_t *p_grab;
438 const char *psz_rtpmap;
439 char url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv;
443 msg_Err( p_stream, "muxing is not supported in RTSP mode" );
447 else if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
449 msg_Err( p_stream, "rtp needs a destination when muxing" );
454 /* Check muxer type */
455 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
457 psz_rtpmap = "MP2P/90000";
459 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
461 psz_rtpmap = "MP2T/90000";
462 p_sys->i_payload_type = 33;
466 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
471 /* create the access out */
472 if( p_sys->i_ttl > 0 )
474 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
478 sprintf( access, "udp{raw}" );
481 /* IPv6 needs brackets if not already present */
482 snprintf( url, sizeof( url ),
483 ( ( p_sys->psz_destination[0] != '[' )
484 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
485 ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
487 url[sizeof( url ) - 1] = '\0';
488 /* FIXME: we should check that url is a numerical address, otherwise
489 * the SDP will be quite broken (regardless of the IP protocol version)
490 * Also it might be IPv6 with no ':' if it is a DNS name.
492 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
494 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
496 msg_Err( p_stream, "cannot create the access out for %s://%s",
501 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
502 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
504 /* better than nothing */
507 p_sys->i_mtu -= MTU_REDUCE;
509 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
510 p_grab = p_sys->p_grab =
511 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
512 p_grab->p_module = NULL;
513 p_grab->p_sout = p_sout;
514 p_grab->psz_access = strdup( "grab" );
515 p_grab->p_cfg = NULL;
516 p_grab->psz_path = strdup( "" );
517 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
518 p_grab->pf_seek = NULL;
519 p_grab->pf_write = AccessOutGrabberWrite;
522 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
524 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
525 sout_AccessOutDelete( p_sys->p_grab );
526 sout_AccessOutDelete( p_sys->p_access );
531 /* create the SDP for a muxed stream (only once) */
532 /* FIXME http://www.faqs.org/rfcs/rfc4566.html
533 o= - should be local username (no spaces allowed)
534 o= time should be hashed with some other value to garantee uniqueness
535 o= don't use the localhost address. use fully qualified domain name or IP4 address
536 a= source-filter: we need our source address
537 a= x-plgroup: (missing)
538 RTP packets need to get the correct src IP address */
539 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
541 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
542 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
549 asprintf( &p_sys->psz_sdp,
551 /* FIXME: source address not known :( */
552 "o=- "I64Fd" %d IN IP%c %s\r\n"
558 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
559 "a=tool:"PACKAGE_STRING"\r\n"
561 "a=type:broadcast\r\n"
562 "m=video %d RTP/AVP %d\r\n"
563 "a=rtpmap:%d %s\r\n",
564 p_sys->i_sdp_id, p_sys->i_sdp_version,
565 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
566 p_sys->psz_session_name,
567 p_sys->psz_session_description,
568 p_sys->psz_session_url,
569 p_sys->psz_session_email,
570 ipv, p_sys->psz_destination, psz_ttl,
571 p_sys->i_port, p_sys->i_payload_type,
572 p_sys->i_payload_type, psz_rtpmap );
573 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
575 /* create the rtp context */
576 p_sys->ssrc[0] = rand()&0xff;
577 p_sys->ssrc[1] = rand()&0xff;
578 p_sys->ssrc[2] = rand()&0xff;
579 p_sys->ssrc[3] = rand()&0xff;
580 p_sys->i_sequence = rand()&0xffff;
581 p_sys->i_timestamp_start = rand()&0xffffffff;
582 p_sys->packet = NULL;
587 p_sys->p_access = NULL;
588 p_sys->p_grab = NULL;
590 free( val.psz_string );
593 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
594 if( *val.psz_string )
596 config_chain_t *p_cfg;
598 SDPHandleUrl( p_stream, val.psz_string );
600 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
602 if( !strcmp( p_cfg->psz_name, "sdp" ) )
604 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
607 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
610 SDPHandleUrl( p_stream, p_cfg->psz_value );
614 free( val.psz_string );
616 /* update p_sout->i_out_pace_nocontrol */
617 p_stream->p_sout->i_out_pace_nocontrol++;
622 /*****************************************************************************
624 *****************************************************************************/
625 static void Close( vlc_object_t * p_this )
627 sout_stream_t *p_stream = (sout_stream_t*)p_this;
628 sout_stream_sys_t *p_sys = p_stream->p_sys;
630 /* update p_sout->i_out_pace_nocontrol */
631 p_stream->p_sout->i_out_pace_nocontrol--;
635 sout_MuxDelete( p_sys->p_mux );
636 sout_AccessOutDelete( p_sys->p_access );
637 sout_AccessOutDelete( p_sys->p_grab );
640 block_Release( p_sys->packet );
642 if( p_sys->b_export_sap )
645 SapSetup( p_stream );
649 while( p_sys->i_rtsp > 0 )
651 RtspClientDel( p_stream, p_sys->rtsp[0] );
654 vlc_mutex_destroy( &p_sys->lock_sdp );
656 if( p_sys->p_httpd_file )
658 httpd_FileDelete( p_sys->p_httpd_file );
660 if( p_sys->p_httpd_host )
662 httpd_HostDelete( p_sys->p_httpd_host );
664 if( p_sys->p_rtsp_url )
666 httpd_UrlDelete( p_sys->p_rtsp_url );
668 if( p_sys->p_rtsp_host )
670 httpd_HostDelete( p_sys->p_rtsp_host );
672 if( p_sys->psz_session_name )
674 free( p_sys->psz_session_name );
675 p_sys->psz_session_name = NULL;
677 if( p_sys->psz_session_description )
679 free( p_sys->psz_session_description );
680 p_sys->psz_session_description = NULL;
682 if( p_sys->psz_session_url )
684 free( p_sys->psz_session_url );
685 p_sys->psz_session_url = NULL;
687 if( p_sys->psz_session_email )
689 free( p_sys->psz_session_email );
690 p_sys->psz_session_email = NULL;
694 free( p_sys->psz_sdp );
695 p_sys->psz_sdp = NULL;
697 if( p_sys->b_export_sdp_file )
700 unlink( p_sys->psz_sdp_file );
702 free( p_sys->psz_sdp_file );
707 /*****************************************************************************
709 *****************************************************************************/
710 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
712 sout_stream_sys_t *p_sys = p_stream->p_sys;
715 vlc_UrlParse( &url, psz_url, 0 );
716 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
718 if( p_sys->p_httpd_file )
720 msg_Err( p_stream, "you can use sdp=http:// only once" );
724 if( HttpSetup( p_stream, &url ) )
726 msg_Err( p_stream, "cannot export sdp as http" );
729 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
731 if( p_sys->p_rtsp_url )
733 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
737 /* FIXME test if destination is multicast or no destination at all FIXME */
738 if( RtspSetup( p_stream, &url ) )
740 msg_Err( p_stream, "cannot export sdp as rtsp" );
743 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
744 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
746 p_sys->b_export_sap = VLC_TRUE;
747 SapSetup( p_stream );
749 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
751 if( p_sys->b_export_sdp_file )
753 msg_Err( p_stream, "you can use sdp=file:// only once" );
756 p_sys->b_export_sdp_file = VLC_TRUE;
757 psz_url = &psz_url[5];
758 if( psz_url[0] == '/' && psz_url[1] == '/' )
760 p_sys->psz_sdp_file = strdup( psz_url );
764 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
767 vlc_UrlClean( &url );
770 /*****************************************************************************
772 *****************************************************************************/
773 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
774 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
775 o= - should be local username (no spaces allowed)
776 o= time should be hashed with some other value to garantue uniqueness
777 o= we need IP6 support?
778 o= don't use the localhost address. use fully qualified domain name or IP4 address
779 p= international phone number (pass via vars?)
781 a= recvonly (missing)
782 a= type:broadcast (missing)
783 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
784 a= x-plgroup: (missing)
785 RTP packets need to get the correct src IP address */
786 static char *SDPGenerate( const sout_stream_t *p_stream,
787 const char *psz_destination, vlc_bool_t b_rtsp )
789 sout_stream_sys_t *p_sys = p_stream->p_sys;
791 char *psz_sdp, *p, ipv;
794 /* FIXME: breaks IP version check on unknown destination */
795 if( psz_destination == NULL )
796 psz_destination = "0.0.0.0";
798 i_size = sizeof( "v=0\r\n" ) +
799 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
800 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
801 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
802 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
803 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
804 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
805 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
806 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
807 strlen( psz_destination ) ;
808 for( i = 0; i < p_sys->i_es; i++ )
810 sout_stream_id_t *id = p_sys->es[i];
812 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
815 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
819 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
823 i_size += strlen( "b=AS: *\r\n") + 10;
827 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
832 i_size += strlen( "m=video %d RTP/AVP %d\r\n" ) +10 +10;
835 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
837 p = psz_sdp = malloc( i_size );
838 p += sprintf( p, "v=0\r\n" );
839 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
840 p_sys->i_sdp_id, p_sys->i_sdp_version,
841 ipv, ipv == '6' ? "::" : "127.0.0.1" );
842 if( *p_sys->psz_session_name )
843 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
844 if( *p_sys->psz_session_description )
845 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
846 if( *p_sys->psz_session_url )
847 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
848 if( *p_sys->psz_session_email )
849 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
851 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
852 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
854 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
856 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
858 /* Add the ttl if it is a multicast address */
859 /* FIXME: 1 is not a correct default value in the case of IPv6 */
860 p += sprintf( p, "/%d\r\n", p_sys->i_ttl ?: 1 );
864 p += sprintf( p, "\r\n" );
867 for( i = 0; i < p_sys->i_es; i++ )
869 sout_stream_id_t *id = p_sys->es[i];
871 if( id->i_cat == AUDIO_ES )
873 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
874 id->i_port, id->i_payload_type );
876 else if( id->i_cat == VIDEO_ES )
878 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
879 id->i_port, id->i_payload_type );
887 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
892 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
897 p += sprintf(p,"b=AS:%d\r\n",id->i_bitrate);
901 p += sprintf( p, "a=control:/trackID=%d\r\n", i );
906 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
907 p_sys->i_port, p_sys->i_payload_type );
913 /*****************************************************************************
915 *****************************************************************************/
916 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
917 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
918 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
919 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
920 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
921 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
922 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
923 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
924 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
925 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
926 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
928 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
930 static const char hex[16] = "0123456789abcdef";
933 for( i = 0; i < i_data; i++ )
935 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
936 s[2*i+1] = hex[(p_data[i] )&0xf];
941 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
943 sout_instance_t *p_sout = p_stream->p_sout;
944 sout_stream_sys_t *p_sys = p_stream->p_sys;
945 sout_stream_id_t *id;
946 sout_access_out_t *p_access = NULL;
950 if( p_sys->p_mux != NULL )
952 sout_input_t *p_input = NULL;
953 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
955 msg_Err( p_stream, "cannot add this stream to the muxer" );
959 id = malloc( sizeof( sout_stream_id_t ) );
960 memset( id, 0, sizeof( sout_stream_id_t ) );
962 id->p_input = p_input;
963 id->pf_packetize= NULL;
964 id->p_rtsp_url = NULL;
970 /* Choose the port */
972 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
974 i_port = p_sys->i_port_audio;
975 p_sys->i_port_audio = 0;
977 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
979 i_port = p_sys->i_port_video;
980 p_sys->i_port_video = 0;
984 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
986 i_port = p_sys->i_port;
993 if( p_sys->psz_destination )
996 char url[NI_MAXHOST + 8];
998 /* first try to create the access out */
1001 snprintf( access, sizeof( access ), "udp{raw,ttl=%d}",
1003 access[sizeof( access ) - 1] = '\0';
1006 strcpy( access, "udp{raw}" );
1008 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
1009 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
1010 p_sys->psz_destination, i_port );
1011 url[sizeof( url ) - 1] = '\0';
1013 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
1015 msg_Err( p_stream, "cannot create the access out for %s://%s",
1019 msg_Dbg( p_stream, "access out %s:%s", access, url );
1022 /* not create the rtp specific stuff */
1023 id = malloc( sizeof( sout_stream_id_t ) );
1024 memset( id, 0, sizeof( sout_stream_id_t ) );
1025 id->p_stream = p_stream;
1026 id->p_access = p_access;
1028 id->psz_rtpmap = NULL;
1029 id->psz_fmtp = NULL;
1030 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
1031 id->i_port = i_port;
1032 id->p_rtsp_url = NULL;
1033 vlc_mutex_init( p_stream, &id->lock_rtsp );
1034 id->i_rtsp_access = 0;
1035 id->rtsp_access = NULL;
1037 switch( p_fmt->i_codec )
1039 case VLC_FOURCC( 's', '1', '6', 'b' ):
1040 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
1042 id->i_payload_type = 11;
1044 else if( p_fmt->audio.i_channels == 2 &&
1045 p_fmt->audio.i_rate == 44100 )
1047 id->i_payload_type = 10;
1051 id->i_payload_type = p_sys->i_payload_type++;
1053 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
1054 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
1055 p_fmt->audio.i_channels );
1056 id->i_clock_rate = p_fmt->audio.i_rate;
1057 id->pf_packetize = rtp_packetize_l16;
1059 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
1060 id->i_payload_type = p_sys->i_payload_type++;
1061 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
1062 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
1063 p_fmt->audio.i_channels );
1064 id->i_clock_rate = p_fmt->audio.i_rate;
1065 id->pf_packetize = rtp_packetize_l8;
1067 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1068 id->i_payload_type = 14;
1069 id->i_clock_rate = 90000;
1070 id->psz_rtpmap = strdup( "MPA/90000" );
1071 id->pf_packetize = rtp_packetize_mpa;
1073 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1074 id->i_payload_type = 32;
1075 id->i_clock_rate = 90000;
1076 id->psz_rtpmap = strdup( "MPV/90000" );
1077 id->pf_packetize = rtp_packetize_mpv;
1079 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1080 id->i_payload_type = p_sys->i_payload_type++;
1081 id->i_clock_rate = 90000;
1082 id->psz_rtpmap = strdup( "ac3/90000" );
1083 id->pf_packetize = rtp_packetize_ac3;
1085 case VLC_FOURCC( 'H', '2', '6', '3' ):
1086 id->i_payload_type = p_sys->i_payload_type++;
1087 id->i_clock_rate = 90000;
1088 id->psz_rtpmap = strdup( "H263-1998/90000" );
1089 id->pf_packetize = rtp_packetize_h263;
1091 case VLC_FOURCC( 'h', '2', '6', '4' ):
1092 id->i_payload_type = p_sys->i_payload_type++;
1093 id->i_clock_rate = 90000;
1094 id->psz_rtpmap = strdup( "H264/90000" );
1095 id->pf_packetize = rtp_packetize_h264;
1096 id->psz_fmtp = NULL;
1098 if( p_fmt->i_extra > 0 )
1100 uint8_t *p_buffer = p_fmt->p_extra;
1101 int i_buffer = p_fmt->i_extra;
1102 char *p_64_sps = NULL;
1103 char *p_64_pps = NULL;
1106 while( i_buffer > 4 &&
1107 p_buffer[0] == 0 && p_buffer[1] == 0 &&
1108 p_buffer[2] == 0 && p_buffer[3] == 1 )
1110 const int i_nal_type = p_buffer[4]&0x1f;
1114 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
1117 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
1119 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
1121 /* we found another startcode */
1126 if( i_nal_type == 7 )
1128 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1129 sprintf_hexa( hexa, &p_buffer[5], 3 );
1131 else if( i_nal_type == 8 )
1133 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1139 if( p_64_sps && p_64_pps )
1140 asprintf( &id->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
1147 id->psz_fmtp = strdup( "packetization-mode=1" );
1150 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1152 char hexa[2*p_fmt->i_extra +1];
1154 id->i_payload_type = p_sys->i_payload_type++;
1155 id->i_clock_rate = 90000;
1156 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1157 id->pf_packetize = rtp_packetize_split;
1158 if( p_fmt->i_extra > 0 )
1160 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1161 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1162 sprintf( id->psz_fmtp,
1163 "profile-level-id=3; config=%s;", hexa );
1167 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1169 id->i_payload_type = p_sys->i_payload_type++;
1170 id->i_clock_rate = p_fmt->audio.i_rate;
1174 char hexa[2*p_fmt->i_extra +1];
1176 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1177 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1178 id->pf_packetize = rtp_packetize_mp4a;
1179 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1180 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1181 sprintf( id->psz_fmtp,
1182 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1183 "config=%s; SizeLength=13;IndexLength=3; "
1184 "IndexDeltaLength=3; Profile=1;", hexa );
1190 unsigned char config[6];
1191 unsigned int aacsrates[15] = {
1192 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1193 16000, 12000, 11025, 8000, 7350, 0, 0 };
1195 for( i = 0; i < 15; i++ )
1196 if( p_fmt->audio.i_rate == aacsrates[i] )
1202 config[3]=p_fmt->audio.i_channels<<4;
1206 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1207 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1208 id->pf_packetize = rtp_packetize_mp4a_latm;
1209 sprintf_hexa( hexa, config, 6 );
1210 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1211 "object=2; cpresent=0; config=%s", hexa );
1215 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1216 id->i_payload_type = p_sys->i_payload_type++;
1217 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1218 "AMR/8000/2" : "AMR/8000" );
1219 id->psz_fmtp = strdup( "octet-align=1" );
1220 id->i_clock_rate = p_fmt->audio.i_rate;
1221 id->pf_packetize = rtp_packetize_amr;
1223 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1224 id->i_payload_type = p_sys->i_payload_type++;
1225 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1226 "AMR-WB/16000/2" : "AMR-WB/16000" );
1227 id->psz_fmtp = strdup( "octet-align=1" );
1228 id->i_clock_rate = p_fmt->audio.i_rate;
1229 id->pf_packetize = rtp_packetize_amr;
1233 msg_Err( p_stream, "cannot add this stream (unsupported "
1234 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1237 sout_AccessOutDelete( p_access );
1242 id->i_cat = p_fmt->i_cat;
1244 id->ssrc[0] = rand()&0xff;
1245 id->ssrc[1] = rand()&0xff;
1246 id->ssrc[2] = rand()&0xff;
1247 id->ssrc[3] = rand()&0xff;
1248 id->i_sequence = rand()&0xffff;
1249 id->i_timestamp_start = rand()&0xffffffff;
1250 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
1252 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1253 if( id->i_mtu <= 16 + MTU_REDUCE )
1255 /* better than nothing */
1258 id->i_mtu -= MTU_REDUCE;
1259 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1261 if( p_sys->p_rtsp_url )
1263 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1265 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1266 msg_Dbg( p_stream, "rtsp: adding %s\n", psz_urlc );
1267 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1269 if( id->p_rtsp_url )
1271 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1272 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1273 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1278 /* Update p_sys context */
1279 vlc_mutex_lock( &p_sys->lock_es );
1280 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1281 vlc_mutex_unlock( &p_sys->lock_es );
1283 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1285 vlc_mutex_lock( &p_sys->lock_sdp );
1286 free( p_sys->psz_sdp );
1287 p_sys->psz_sdp = psz_sdp;
1288 vlc_mutex_unlock( &p_sys->lock_sdp );
1290 p_sys->i_sdp_version++;
1292 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
1294 /* Update SDP (sap/file) */
1295 if( p_sys->b_export_sap ) SapSetup( p_stream );
1296 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1301 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1303 sout_stream_sys_t *p_sys = p_stream->p_sys;
1305 vlc_mutex_lock( &p_sys->lock_es );
1306 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1307 vlc_mutex_unlock( &p_sys->lock_es );
1310 if( id->i_port > 0 )
1312 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1313 p_sys->i_port_audio = id->i_port;
1314 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1315 p_sys->i_port_video = id->i_port;
1320 if( id->psz_rtpmap )
1322 free( id->psz_rtpmap );
1326 free( id->psz_fmtp );
1328 if( id->psz_destination )
1329 free( id->psz_destination );
1330 sout_AccessOutDelete( id->p_access );
1332 else if( id->p_input )
1334 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1336 if( id->p_rtsp_url )
1338 httpd_UrlDelete( id->p_rtsp_url );
1340 vlc_mutex_destroy( &id->lock_rtsp );
1341 if( id->rtsp_access ) free( id->rtsp_access );
1343 /* Update SDP (sap/file) */
1344 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1345 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1351 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1356 if( p_stream->p_sys->p_mux )
1358 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1364 p_next = p_buffer->p_next;
1365 if( id->pf_packetize( p_stream, id, p_buffer ) )
1369 block_Release( p_buffer );
1377 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1380 sout_stream_sys_t *p_sys = p_stream->p_sys;
1382 int64_t i_dts = p_buffer->i_dts;
1383 uint32_t i_timestamp = i_dts * 9 / 100;
1385 uint8_t *p_data = p_buffer->p_buffer;
1386 unsigned int i_data = p_buffer->i_buffer;
1387 unsigned int i_max = p_sys->i_mtu - 12;
1389 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1393 unsigned int i_size;
1395 /* output complete packet */
1396 if( p_sys->packet &&
1397 p_sys->packet->i_buffer + i_data > i_max )
1399 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1400 p_sys->packet = NULL;
1403 if( p_sys->packet == NULL )
1405 /* allocate a new packet */
1406 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1407 p_sys->packet->p_buffer[ 0] = 0x80;
1408 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1409 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1410 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1411 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1412 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1413 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1414 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1415 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1416 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1417 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1418 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1419 p_sys->packet->i_buffer = 12;
1421 p_sys->packet->i_dts = i_dts;
1422 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1423 i_dts += p_sys->packet->i_length;
1425 p_sys->i_sequence++;
1428 i_size = __MIN( i_data,
1429 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1431 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1434 p_sys->packet->i_buffer += i_size;
1442 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1445 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1447 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1453 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1455 p_next = p_buffer->p_next;
1456 block_Release( p_buffer );
1463 /****************************************************************************
1465 ****************************************************************************/
1466 static int SapSetup( sout_stream_t *p_stream )
1468 sout_stream_sys_t *p_sys = p_stream->p_sys;
1469 sout_instance_t *p_sout = p_stream->p_sout;
1470 announce_method_t *p_method = sout_SAPMethod();
1472 /* Remove the previous session */
1473 if( p_sys->p_session != NULL)
1475 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1476 sout_AnnounceSessionDestroy( p_sys->p_session );
1477 p_sys->p_session = NULL;
1480 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1482 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
1484 p_sys->psz_destination,
1488 sout_MethodRelease( p_method );
1492 /****************************************************************************
1494 ****************************************************************************/
1495 static int FileSetup( sout_stream_t *p_stream )
1497 sout_stream_sys_t *p_sys = p_stream->p_sys;
1500 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1502 msg_Err( p_stream, "cannot open file '%s' (%s)",
1503 p_sys->psz_sdp_file, strerror(errno) );
1504 return VLC_EGENERIC;
1507 fprintf( f, "%s", p_sys->psz_sdp );
1513 /****************************************************************************
1515 ****************************************************************************/
1516 static int HttpCallback( httpd_file_sys_t *p_args,
1517 httpd_file_t *, uint8_t *p_request,
1518 uint8_t **pp_data, int *pi_data );
1520 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1522 sout_stream_sys_t *p_sys = p_stream->p_sys;
1524 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1525 if( p_sys->p_httpd_host )
1527 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1528 url->psz_path ? url->psz_path : "/",
1531 HttpCallback, (void*)p_sys );
1533 if( p_sys->p_httpd_file == NULL )
1535 return VLC_EGENERIC;
1540 static int HttpCallback( httpd_file_sys_t *p_args,
1541 httpd_file_t *f, uint8_t *p_request,
1542 uint8_t **pp_data, int *pi_data )
1544 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1546 vlc_mutex_lock( &p_sys->lock_sdp );
1547 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1549 *pi_data = strlen( p_sys->psz_sdp );
1550 *pp_data = malloc( *pi_data );
1551 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1558 vlc_mutex_unlock( &p_sys->lock_sdp );
1563 /****************************************************************************
1565 ****************************************************************************/
1566 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
1568 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1570 rtsp->psz_session = strdup( psz_session );
1572 rtsp->b_playing = VLC_FALSE;
1576 rtsp->access = NULL;
1578 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1582 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
1586 if( !psz_session ) return NULL;
1588 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1590 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1592 return p_stream->p_sys->rtsp[i];
1598 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1601 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1603 for( i = 0; i < rtsp->i_access; i++ )
1605 sout_AccessOutDelete( rtsp->access[i] );
1607 if( rtsp->id ) free( rtsp->id );
1608 if( rtsp->access ) free( rtsp->access );
1610 free( rtsp->psz_session );
1614 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1616 sout_stream_sys_t *p_sys = p_stream->p_sys;
1618 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1620 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1621 if( p_sys->p_rtsp_host == NULL )
1623 return VLC_EGENERIC;
1626 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1627 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1628 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1629 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1631 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1632 if( p_sys->p_rtsp_url == 0 )
1634 return VLC_EGENERIC;
1636 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1637 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1638 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1639 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1644 static int RtspCallback( httpd_callback_sys_t *p_args,
1646 httpd_message_t *answer, httpd_message_t *query )
1648 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1649 sout_stream_sys_t *p_sys = p_stream->p_sys;
1650 char *psz_destination = p_sys->psz_destination;
1651 const char *psz_session = NULL;
1652 const char *psz_cseq = NULL;
1655 if( answer == NULL || query == NULL )
1659 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1661 answer->i_proto = HTTPD_PROTO_RTSP;
1662 answer->i_version= query->i_version;
1663 answer->i_type = HTTPD_MSG_ANSWER;
1665 switch( query->i_type )
1667 case HTTPD_MSG_DESCRIBE:
1669 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1671 answer->i_status = 200;
1672 answer->psz_status = strdup( "OK" );
1673 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1674 httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
1675 answer->p_body = (uint8_t *)psz_sdp;
1676 answer->i_body = strlen( psz_sdp );
1680 case HTTPD_MSG_PLAY:
1682 rtsp_client_t *rtsp;
1683 /* for now only multicast so easy */
1684 answer->i_status = 200;
1685 answer->psz_status = strdup( "OK" );
1687 answer->p_body = NULL;
1689 psz_session = httpd_MsgGet( query, "Session" );
1690 rtsp = RtspClientGet( p_stream, psz_session );
1691 if( rtsp && !rtsp->b_playing )
1695 rtsp->b_playing = VLC_TRUE;
1697 vlc_mutex_lock( &p_sys->lock_es );
1698 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1700 sout_stream_id_t *id = rtsp->id[i_id];
1703 for( i = 0; i < p_sys->i_es; i++ )
1705 if( id == p_sys->es[i] )
1708 if( i >= p_sys->i_es ) continue;
1710 vlc_mutex_lock( &id->lock_rtsp );
1711 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1712 vlc_mutex_unlock( &id->lock_rtsp );
1714 vlc_mutex_unlock( &p_sys->lock_es );
1718 case HTTPD_MSG_PAUSE:
1720 return VLC_EGENERIC;
1721 case HTTPD_MSG_TEARDOWN:
1723 rtsp_client_t *rtsp;
1725 /* for now only multicast so easy again */
1726 answer->i_status = 200;
1727 answer->psz_status = strdup( "OK" );
1729 answer->p_body = NULL;
1731 psz_session = httpd_MsgGet( query, "Session" );
1732 rtsp = RtspClientGet( p_stream, psz_session );
1737 vlc_mutex_lock( &p_sys->lock_es );
1738 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1740 sout_stream_id_t *id = rtsp->id[i_id];
1743 for( i = 0; i < p_sys->i_es; i++ )
1745 if( id == p_sys->es[i] )
1748 if( i >= p_sys->i_es ) continue;
1750 vlc_mutex_lock( &id->lock_rtsp );
1751 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1752 vlc_mutex_unlock( &id->lock_rtsp );
1754 vlc_mutex_unlock( &p_sys->lock_es );
1756 RtspClientDel( p_stream, rtsp );
1762 return VLC_EGENERIC;
1764 httpd_MsgAdd( answer, "Server", PACKAGE_STRING );
1765 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1766 psz_cseq = httpd_MsgGet( query, "Cseq" );
1768 i_cseq = atoi( psz_cseq );
1771 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1772 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1775 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1779 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1781 httpd_message_t *answer, httpd_message_t *query )
1783 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1784 sout_stream_t *p_stream = id->p_stream;
1785 sout_stream_sys_t *p_sys = p_stream->p_sys;
1786 char psz_session_init[100];
1787 const char *psz_session = NULL;
1788 const char *psz_cseq = NULL;
1792 if( answer == NULL || query == NULL )
1794 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1797 snprintf( psz_session_init, sizeof(psz_session_init), "%d", rand() );
1800 answer->i_proto = HTTPD_PROTO_RTSP;
1801 answer->i_version= query->i_version;
1802 answer->i_type = HTTPD_MSG_ANSWER;
1804 switch( query->i_type )
1806 case HTTPD_MSG_SETUP:
1808 const char *psz_transport = httpd_MsgGet( query, "Transport" );
1810 //fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1812 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1814 //fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1815 answer->i_status = 200;
1816 answer->psz_status = strdup( "OK" );
1818 answer->p_body = NULL;
1820 psz_session = httpd_MsgGet( query, "Session" );
1822 psz_session = psz_session_init;
1824 httpd_MsgAdd( answer, "Transport",
1825 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1826 id->psz_destination, id->i_port,id->i_port+1,
1829 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1831 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1832 char ip[NI_MAXNUMERICHOST], psz_access[17], psz_url[NI_MAXNUMERICHOST + 8];
1834 sout_access_out_t *p_access;
1836 rtsp_client_t *rtsp = NULL;
1838 if( httpd_ClientIP( cl, ip ) == NULL )
1840 answer->i_status = 500;
1841 answer->psz_status = strdup( "Internal server error" );
1843 answer->p_body = NULL;
1847 //fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n", ip, i_port );
1849 psz_session = httpd_MsgGet( query, "Session" );
1852 psz_session = psz_session_init;
1853 rtsp = RtspClientNew( p_stream, psz_session_init );
1857 rtsp = RtspClientGet( p_stream, psz_session );
1860 answer->i_status = 454;
1861 answer->psz_status = strdup( "Unknown session id" );
1863 answer->p_body = NULL;
1868 /* first try to create the access out */
1870 snprintf( psz_access, sizeof( psz_access ),
1871 "udp{raw,ttl=%d}", p_sys->i_ttl );
1873 strlcpy( psz_access, "udp{raw}", sizeof( psz_access ) );
1875 snprintf( psz_url, sizeof( psz_url ),
1876 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
1879 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1881 msg_Err( p_stream, "cannot create the access out for %s://%s",
1882 psz_access, psz_url );
1883 answer->i_status = 500;
1884 answer->psz_status = strdup( "Internal server error" );
1886 answer->p_body = NULL;
1890 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1891 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1893 answer->i_status = 200;
1894 answer->psz_status = strdup( "OK" );
1896 answer->p_body = NULL;
1898 httpd_MsgAdd( answer, "Transport",
1899 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1901 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1903 answer->i_status = 461;
1904 answer->psz_status = strdup( "Unsupported Transport" );
1906 answer->p_body = NULL;
1912 return VLC_EGENERIC;
1914 httpd_MsgAdd( answer, "Server", "VLC Server" );
1915 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1916 psz_cseq = httpd_MsgGet( query, "Cseq" );
1918 i_cseq = atoi( psz_cseq );
1921 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1922 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1925 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1929 /****************************************************************************
1931 ****************************************************************************/
1932 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1933 int b_marker, int64_t i_pts )
1935 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1937 out->p_buffer[0] = 0x80;
1938 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1939 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1940 out->p_buffer[3] = ( id->i_sequence )&0xff;
1941 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1942 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1943 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1944 out->p_buffer[7] = ( i_timestamp )&0xff;
1946 out->p_buffer[ 8] = id->ssrc[0];
1947 out->p_buffer[ 9] = id->ssrc[1];
1948 out->p_buffer[10] = id->ssrc[2];
1949 out->p_buffer[11] = id->ssrc[3];
1955 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1958 vlc_mutex_lock( &id->lock_rtsp );
1959 for( i = 0; i < id->i_rtsp_access; i++ )
1961 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1963 vlc_mutex_unlock( &id->lock_rtsp );
1967 sout_AccessOutWrite( id->p_access, out );
1971 block_Release( out );
1975 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1978 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1979 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1981 uint8_t *p_data = in->p_buffer;
1982 int i_data = in->i_buffer;
1985 for( i = 0; i < i_count; i++ )
1987 int i_payload = __MIN( i_max, i_data );
1988 block_t *out = block_New( p_stream, 16 + i_payload );
1990 /* rtp common header */
1991 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1993 out->p_buffer[12] = 0;
1994 out->p_buffer[13] = 0;
1995 /* fragment offset in the current frame */
1996 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1997 out->p_buffer[15] = ( (i*i_max) )&0xff;
1998 memcpy( &out->p_buffer[16], p_data, i_payload );
2000 out->i_buffer = 16 + i_payload;
2001 out->i_dts = in->i_dts + i * in->i_length / i_count;
2002 out->i_length = in->i_length / i_count;
2004 rtp_packetize_send( id, out );
2006 p_data += i_payload;
2007 i_data -= i_payload;
2014 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
2017 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
2018 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2020 uint8_t *p_data = in->p_buffer;
2021 int i_data = in->i_buffer;
2023 int b_sequence_start = 0;
2024 int i_temporal_ref = 0;
2025 int i_picture_coding_type = 0;
2026 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
2027 int b_start_slice = 0;
2029 /* preparse this packet to get some info */
2030 if( in->i_buffer > 4 )
2032 uint8_t *p = p_data;
2033 int i_rest = in->i_buffer;
2037 while( i_rest > 4 &&
2038 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
2052 /* sequence start code */
2053 b_sequence_start = 1;
2055 else if( *p == 0x00 && i_rest >= 4 )
2058 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
2059 i_picture_coding_type = (p[2] >> 3)&0x07;
2061 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
2062 i_picture_coding_type == 3 ) )
2064 i_ffv = (p[3] >> 2)&0x01;
2065 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
2066 if( i_rest > 4 && i_picture_coding_type == 3 )
2068 i_fbv = (p[4]>>6)&0x01;
2069 i_bfc = (p[4]>>3)&0x07;
2073 else if( *p <= 0xaf )
2080 for( i = 0; i < i_count; i++ )
2082 int i_payload = __MIN( i_max, i_data );
2083 block_t *out = block_New( p_stream,
2085 uint32_t h = ( i_temporal_ref << 16 )|
2086 ( b_sequence_start << 13 )|
2087 ( b_start_slice << 12 )|
2088 ( i == i_count - 1 ? 1 << 11 : 0 )|
2089 ( i_picture_coding_type << 8 )|
2090 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
2092 /* rtp common header */
2093 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2094 in->i_pts > 0 ? in->i_pts : in->i_dts );
2096 /* 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 */
2097 out->p_buffer[12] = ( h >> 24 )&0xff;
2098 out->p_buffer[13] = ( h >> 16 )&0xff;
2099 out->p_buffer[14] = ( h >> 8 )&0xff;
2100 out->p_buffer[15] = ( h )&0xff;
2102 memcpy( &out->p_buffer[16], p_data, i_payload );
2104 out->i_buffer = 16 + i_payload;
2105 out->i_dts = in->i_dts + i * in->i_length / i_count;
2106 out->i_length = in->i_length / i_count;
2108 rtp_packetize_send( id, out );
2110 p_data += i_payload;
2111 i_data -= i_payload;
2117 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
2120 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
2121 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2123 uint8_t *p_data = in->p_buffer;
2124 int i_data = in->i_buffer;
2127 for( i = 0; i < i_count; i++ )
2129 int i_payload = __MIN( i_max, i_data );
2130 block_t *out = block_New( p_stream, 14 + i_payload );
2132 /* rtp common header */
2133 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
2135 out->p_buffer[12] = 1;
2137 out->p_buffer[13] = 0x00;
2139 memcpy( &out->p_buffer[14], p_data, i_payload );
2141 out->i_buffer = 14 + i_payload;
2142 out->i_dts = in->i_dts + i * in->i_length / i_count;
2143 out->i_length = in->i_length / i_count;
2145 rtp_packetize_send( id, out );
2147 p_data += i_payload;
2148 i_data -= i_payload;
2154 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
2157 int i_max = id->i_mtu - 12; /* payload max in one packet */
2158 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2160 uint8_t *p_data = in->p_buffer;
2161 int i_data = in->i_buffer;
2164 for( i = 0; i < i_count; i++ )
2166 int i_payload = __MIN( i_max, i_data );
2167 block_t *out = block_New( p_stream, 12 + i_payload );
2169 /* rtp common header */
2170 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2171 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2172 memcpy( &out->p_buffer[12], p_data, i_payload );
2174 out->i_buffer = 12 + i_payload;
2175 out->i_dts = in->i_dts + i * in->i_length / i_count;
2176 out->i_length = in->i_length / i_count;
2178 rtp_packetize_send( id, out );
2180 p_data += i_payload;
2181 i_data -= i_payload;
2188 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
2191 int i_max = id->i_mtu - 14; /* payload max in one packet */
2192 int latmhdrsize = in->i_buffer / 0xff + 1;
2193 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2195 uint8_t *p_data = in->p_buffer, *p_header = NULL;
2196 int i_data = in->i_buffer;
2199 for( i = 0; i < i_count; i++ )
2201 int i_payload = __MIN( i_max, i_data );
2206 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
2208 /* rtp common header */
2209 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
2210 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2214 int tmp = in->i_buffer;
2216 p_header=out->p_buffer+12;
2226 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
2228 out->i_buffer = 12 + latmhdrsize + i_payload;
2229 out->i_dts = in->i_dts + i * in->i_length / i_count;
2230 out->i_length = in->i_length / i_count;
2232 rtp_packetize_send( id, out );
2234 p_data += i_payload;
2235 i_data -= i_payload;
2241 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
2244 int i_max = id->i_mtu - 12; /* payload max in one packet */
2245 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2247 uint8_t *p_data = in->p_buffer;
2248 int i_data = in->i_buffer;
2253 int i_payload = (__MIN( i_max, i_data )/4)*4;
2254 block_t *out = block_New( p_stream, 12 + i_payload );
2256 /* rtp common header */
2257 rtp_packetize_common( id, out, 0,
2258 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2259 memcpy( &out->p_buffer[12], p_data, i_payload );
2261 out->i_buffer = 12 + i_payload;
2262 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2263 out->i_length = in->i_length / i_count;
2265 rtp_packetize_send( id, out );
2267 p_data += i_payload;
2268 i_data -= i_payload;
2275 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2278 int i_max = id->i_mtu - 12; /* payload max in one packet */
2279 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2281 uint8_t *p_data = in->p_buffer;
2282 int i_data = in->i_buffer;
2287 int i_payload = (__MIN( i_max, i_data )/2)*2;
2288 block_t *out = block_New( p_stream, 12 + i_payload );
2290 /* rtp common header */
2291 rtp_packetize_common( id, out, 0,
2292 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2293 memcpy( &out->p_buffer[12], p_data, i_payload );
2295 out->i_buffer = 12 + i_payload;
2296 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2297 out->i_length = in->i_length / i_count;
2299 rtp_packetize_send( id, out );
2301 p_data += i_payload;
2302 i_data -= i_payload;
2308 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2311 int i_max = id->i_mtu - 16; /* payload max in one packet */
2312 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2314 uint8_t *p_data = in->p_buffer;
2315 int i_data = in->i_buffer;
2318 for( i = 0; i < i_count; i++ )
2320 int i_payload = __MIN( i_max, i_data );
2321 block_t *out = block_New( p_stream, 16 + i_payload );
2323 /* rtp common header */
2324 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2325 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2327 /* AU headers length (bits) */
2328 out->p_buffer[12] = 0;
2329 out->p_buffer[13] = 2*8;
2330 /* for each AU length 13 bits + idx 3bits, */
2331 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2332 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2334 memcpy( &out->p_buffer[16], p_data, i_payload );
2336 out->i_buffer = 16 + i_payload;
2337 out->i_dts = in->i_dts + i * in->i_length / i_count;
2338 out->i_length = in->i_length / i_count;
2340 rtp_packetize_send( id, out );
2342 p_data += i_payload;
2343 i_data -= i_payload;
2351 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2352 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2353 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2356 uint8_t *p_data = in->p_buffer;
2357 int i_data = in->i_buffer;
2359 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2362 int b_v_bit = 0; // no pesky error resilience
2363 int i_plen = 0; // normally plen=0 for PSC packet
2364 int i_pebit = 0; // because plen=0
2369 return VLC_EGENERIC;
2371 if( p_data[0] || p_data[1] )
2373 return VLC_EGENERIC;
2375 /* remove 2 leading 0 bytes */
2378 i_count = ( i_data + i_max - 1 ) / i_max;
2380 for( i = 0; i < i_count; i++ )
2382 int i_payload = __MIN( i_max, i_data );
2383 block_t *out = block_New( p_stream,
2384 RTP_H263_PAYLOAD_START + i_payload );
2385 b_p_bit = (i == 0) ? 1 : 0;
2386 h = ( b_p_bit << 10 )|
2391 /* rtp common header */
2392 //b_m_bit = 1; // always contains end of frame
2393 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2394 in->i_pts > 0 ? in->i_pts : in->i_dts );
2397 out->p_buffer[12] = ( h >> 8 )&0xff;
2398 out->p_buffer[13] = ( h )&0xff;
2399 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2401 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2402 out->i_dts = in->i_dts + i * in->i_length / i_count;
2403 out->i_length = in->i_length / i_count;
2405 rtp_packetize_send( id, out );
2407 p_data += i_payload;
2408 i_data -= i_payload;
2415 static int rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
2416 const uint8_t *p_data, int i_data, int64_t i_pts, int64_t i_dts, vlc_bool_t b_last, int64_t i_length )
2418 const int i_max = id->i_mtu - 12; /* payload max in one packet */
2425 i_nal_hdr = p_data[3];
2426 i_nal_type = i_nal_hdr&0x1f;
2427 if( i_nal_type == 7 || i_nal_type == 8 )
2429 /* XXX Why do you want to remove them ? It will break streaming with
2430 * SPS/PPS change (broadcast) ? */
2434 /* Skip start code */
2439 if( i_data <= i_max )
2441 /* Single NAL unit packet */
2442 block_t *out = block_New( p_stream, 12 + i_data );
2444 out->i_length = i_length;
2447 rtp_packetize_common( id, out, b_last, i_pts );
2448 out->i_buffer = 12 + i_data;
2450 memcpy( &out->p_buffer[12], p_data, i_data );
2452 rtp_packetize_send( id, out );
2456 /* FU-A Fragmentation Unit without interleaving */
2457 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
2463 for( i = 0; i < i_count; i++ )
2465 const int i_payload = __MIN( i_data, i_max-2 );
2466 block_t *out = block_New( p_stream, 12 + 2 + i_payload );
2467 out->i_dts = i_dts + i * i_length / i_count;
2468 out->i_length = i_length / i_count;
2471 rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
2472 out->i_buffer = 14 + i_payload;
2475 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
2477 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
2478 memcpy( &out->p_buffer[14], p_data, i_payload );
2480 rtp_packetize_send( id, out );
2482 i_data -= i_payload;
2483 p_data += i_payload;
2489 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
2492 const uint8_t *p_buffer = in->p_buffer;
2493 int i_buffer = in->i_buffer;
2495 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
2501 /* Split nal units */
2502 while( i_buffer > 4 )
2505 int i_size = i_buffer;
2506 int i_skip = i_buffer;
2508 /* search nal end */
2509 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
2511 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
2513 /* we found another startcode */
2514 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
2519 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
2520 rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
2521 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
2522 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
2530 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2533 int i_max = id->i_mtu - 14; /* payload max in one packet */
2534 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2536 uint8_t *p_data = in->p_buffer;
2537 int i_data = in->i_buffer;
2540 /* Only supports octet-aligned mode */
2541 for( i = 0; i < i_count; i++ )
2543 int i_payload = __MIN( i_max, i_data );
2544 block_t *out = block_New( p_stream, 14 + i_payload );
2546 /* rtp common header */
2547 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2548 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2549 /* Payload header */
2550 out->p_buffer[12] = 0xF0; /* CMR */
2551 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
2553 /* FIXME: are we fed multiple frames ? */
2554 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
2556 out->i_buffer = 14 + i_payload-1;
2557 out->i_dts = in->i_dts + i * in->i_length / i_count;
2558 out->i_length = in->i_length / i_count;
2560 rtp_packetize_send( id, out );
2562 p_data += i_payload;
2563 i_data -= i_payload;