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 *****************************************************************************/
36 #include <vlc_block.h>
38 #include <vlc_httpd.h>
40 #include <vlc_network.h>
41 #include <vlc_charset.h>
42 #include <vlc_strings.h>
44 /*****************************************************************************
46 *****************************************************************************/
50 #define DST_TEXT N_("Destination")
51 #define DST_LONGTEXT N_( \
52 "This is the output URL that will be used." )
53 #define SDP_TEXT N_("SDP")
54 #define SDP_LONGTEXT N_( \
55 "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
56 "session will be made available. You must use an url: http://location to " \
57 "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
58 "for the SDP to be announced via SAP." )
59 #define MUX_TEXT N_("Muxer")
60 #define MUX_LONGTEXT N_( \
61 "This allows you to specify the muxer used for the streaming output. " \
62 "Default is to use no muxer (standard RTP stream)." )
64 #define NAME_TEXT N_("Session name")
65 #define NAME_LONGTEXT N_( \
66 "This is the name of the session that will be announced in the SDP " \
67 "(Session Descriptor)." )
68 #define DESC_TEXT N_("Session description")
69 #define DESC_LONGTEXT N_( \
70 "This allows you to give a broader description of the stream, that will " \
71 "be announced in the SDP (Session Descriptor)." )
72 #define URL_TEXT N_("Session URL")
73 #define URL_LONGTEXT N_( \
74 "This allows you to give an URL with more details about the stream " \
75 "(often the website of the streaming organization), that will " \
76 "be announced in the SDP (Session Descriptor)." )
77 #define EMAIL_TEXT N_("Session email")
78 #define EMAIL_LONGTEXT N_( \
79 "This allows you to give a contact mail address for the stream, that will " \
80 "be announced in the SDP (Session Descriptor)." )
81 #define PORT_TEXT N_("Port")
82 #define PORT_LONGTEXT N_( \
83 "This allows you to specify the base port for the RTP streaming." )
84 #define PORT_AUDIO_TEXT N_("Audio port")
85 #define PORT_AUDIO_LONGTEXT N_( \
86 "This allows you to specify the default audio port for the RTP streaming." )
87 #define PORT_VIDEO_TEXT N_("Video port")
88 #define PORT_VIDEO_LONGTEXT N_( \
89 "This allows you to specify the default video port for the RTP streaming." )
91 #define TTL_TEXT N_("Hop limit (TTL)")
92 #define TTL_LONGTEXT N_( \
93 "This is the hop limit (also known as \"Time-To-Live\" or TTL) of " \
94 "the multicast packets sent by the stream output (0 = use operating " \
95 "system built-in default).")
97 #define RFC3016_TEXT N_("MP4A LATM")
98 #define RFC3016_LONGTEXT N_( \
99 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
101 static int Open ( vlc_object_t * );
102 static void Close( vlc_object_t * );
104 #define SOUT_CFG_PREFIX "sout-rtp-"
107 set_shortname( _("RTP"));
108 set_description( _("RTP stream output") );
109 set_capability( "sout stream", 0 );
110 add_shortcut( "rtp" );
111 set_category( CAT_SOUT );
112 set_subcategory( SUBCAT_SOUT_STREAM );
114 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
115 DST_LONGTEXT, VLC_TRUE );
116 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
117 SDP_LONGTEXT, VLC_TRUE );
118 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
119 MUX_LONGTEXT, VLC_TRUE );
121 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
122 NAME_LONGTEXT, VLC_TRUE );
123 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
124 DESC_LONGTEXT, VLC_TRUE );
125 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
126 URL_LONGTEXT, VLC_TRUE );
127 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
128 EMAIL_LONGTEXT, VLC_TRUE );
130 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
131 PORT_LONGTEXT, VLC_TRUE );
132 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
133 PORT_AUDIO_LONGTEXT, VLC_TRUE );
134 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
135 PORT_VIDEO_LONGTEXT, VLC_TRUE );
137 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
138 TTL_LONGTEXT, VLC_TRUE );
140 add_bool( SOUT_CFG_PREFIX "mp4a-latm", 0, NULL, RFC3016_TEXT,
141 RFC3016_LONGTEXT, VLC_FALSE );
143 set_callbacks( Open, Close );
146 /*****************************************************************************
147 * Exported prototypes
148 *****************************************************************************/
149 static const char *ppsz_sout_options[] = {
150 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
151 "description", "url","email", "mp4a-latm", NULL
154 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
155 static int Del ( sout_stream_t *, sout_stream_id_t * );
156 static int Send( sout_stream_t *, sout_stream_id_t *,
159 /* For unicast/interleaved streaming */
163 int64_t i_last; /* for timeout */
165 /* is it in "play" state */
166 vlc_bool_t b_playing;
168 /* output (id-access) */
170 sout_stream_id_t **id;
172 sout_access_out_t **access;
175 struct sout_stream_sys_t
181 vlc_mutex_t lock_sdp;
183 char *psz_session_name;
184 char *psz_session_description;
185 char *psz_session_url;
186 char *psz_session_email;
189 vlc_bool_t b_export_sdp_file;
192 vlc_bool_t b_export_sap;
193 session_descriptor_t *p_session;
195 httpd_host_t *p_httpd_host;
196 httpd_file_t *p_httpd_file;
198 httpd_host_t *p_rtsp_host;
199 httpd_url_t *p_rtsp_url;
200 char *psz_rtsp_control;
204 char *psz_destination;
211 /* when need to use a private one or when using muxer */
214 /* in case we do TS/PS over rtp */
216 sout_access_out_t *p_access;
218 sout_access_out_t *p_grab;
220 uint32_t i_timestamp_start;
227 sout_stream_id_t **es;
231 rtsp_client_t **rtsp;
234 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
237 struct sout_stream_id_t
239 sout_stream_t *p_stream;
241 uint8_t i_payload_type;
243 uint32_t i_timestamp_start;
250 char *psz_destination;
255 /* Packetizer specific fields */
256 pf_rtp_packetizer_t pf_packetize;
259 /* for sending the packets */
260 sout_access_out_t *p_access;
262 vlc_mutex_t lock_rtsp;
264 sout_access_out_t **rtsp_access;
267 sout_input_t *p_input;
269 /* RTSP url control */
270 httpd_url_t *p_rtsp_url;
273 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
275 static void SDPHandleUrl( sout_stream_t *, char * );
277 static int SapSetup( sout_stream_t *p_stream );
278 static int FileSetup( sout_stream_t *p_stream );
279 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
280 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
282 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
283 httpd_message_t *, httpd_message_t * );
284 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
285 httpd_message_t *, httpd_message_t * );
288 static rtsp_client_t *RtspClientNew( sout_stream_t *, const char *psz_session );
289 static rtsp_client_t *RtspClientGet( sout_stream_t *, const char *psz_session );
290 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
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" );
312 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
313 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
314 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
315 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
317 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
318 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
319 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
321 p_sys->psz_sdp_file = NULL;
323 if( p_sys->i_port_audio == p_sys->i_port_video )
325 msg_Err( p_stream, "audio and video port cannot be the same" );
326 p_sys->i_port_audio = 0;
327 p_sys->i_port_video = 0;
330 if( !p_sys->psz_session_name )
332 if( p_sys->psz_destination )
333 p_sys->psz_session_name = strdup( p_sys->psz_destination );
335 p_sys->psz_session_name = strdup( "NONE" );
338 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
340 if( !strcmp( p_cfg->psz_name, "sdp" ) )
342 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
352 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
353 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
355 free( val2.psz_string );
358 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
362 msg_Err( p_stream, "missing destination and not in rtsp mode" );
366 p_sys->psz_destination = NULL;
368 else if( p_sys->i_port <= 0 )
370 msg_Err( p_stream, "invalid port" );
375 p_sys->i_ttl = var_GetInteger( p_stream, SOUT_CFG_PREFIX "ttl" );
376 if( p_sys->i_ttl == 0 )
378 /* Normally, we should let the default hop limit up to the core,
379 * but we have to know it to build our SDP properly, which is why
380 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
382 p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
384 if( p_sys->i_ttl > 255 )
386 /* must not exceed 999 once formatted */
388 p_sys->b_latm = var_GetBool( p_stream, SOUT_CFG_PREFIX "mp4a-latm" );
390 p_sys->i_payload_type = 96;
395 p_sys->psz_sdp = NULL;
397 p_sys->i_sdp_id = mdate();
398 p_sys->i_sdp_version = 1;
399 p_sys->psz_sdp = NULL;
401 p_sys->b_export_sap = VLC_FALSE;
402 p_sys->b_export_sdp_file = VLC_FALSE;
403 p_sys->p_session = NULL;
405 p_sys->p_httpd_host = NULL;
406 p_sys->p_httpd_file = NULL;
407 p_sys->p_rtsp_host = NULL;
408 p_sys->p_rtsp_url = NULL;
409 p_sys->psz_rtsp_control = NULL;
410 p_sys->psz_rtsp_path = NULL;
412 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
413 vlc_mutex_init( p_stream, &p_sys->lock_es );
415 p_stream->pf_add = Add;
416 p_stream->pf_del = Del;
417 p_stream->pf_send = Send;
419 p_stream->p_sys = p_sys;
421 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
422 if( *val.psz_string )
424 sout_access_out_t *p_grab;
425 const char *psz_rtpmap;
426 char url[NI_MAXHOST + 8], access[22], psz_ttl[5], ipv;
430 msg_Err( p_stream, "muxing is not supported in RTSP mode" );
434 else if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
436 msg_Err( p_stream, "RTP needs a destination when muxing" );
441 /* Check muxer type */
442 if( !strncasecmp( val.psz_string, "ps", 2 )
443 || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
445 psz_rtpmap = "MP2P/90000";
447 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
449 psz_rtpmap = "MP2T/90000";
450 p_sys->i_payload_type = 33;
454 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
459 /* create the access out */
460 if( p_sys->i_ttl > 0 )
462 sprintf( access, "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
466 strcat( access, "udp{raw,rtcp}" );
469 /* FIXME: we should check that url is a numerical address, otherwise
470 * the SDP will be quite broken (regardless of the IP protocol version)
471 * Also it might be IPv6 with no ':' if it is a DNS name.
473 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
475 /* IPv6 needs brackets if not already present */
476 if ((ipv == '6') && (p_sys->psz_destination[0] != '['))
477 snprintf (url, sizeof (url), "[%s]:%d",p_sys->psz_destination,
480 snprintf (url, sizeof (url), "%s:%d",p_sys->psz_destination,
483 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
485 msg_Err( p_stream, "cannot create the access out for %s://%s",
490 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
491 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
493 /* better than nothing */
496 p_sys->i_mtu -= MTU_REDUCE;
498 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
499 p_grab = p_sys->p_grab =
500 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
501 p_grab->p_module = NULL;
502 p_grab->p_sout = p_sout;
503 p_grab->psz_access = strdup( "grab" );
504 p_grab->p_cfg = NULL;
505 p_grab->psz_path = strdup( "" );
506 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
507 p_grab->pf_seek = NULL;
508 p_grab->pf_write = AccessOutGrabberWrite;
511 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
513 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
514 sout_AccessOutDelete( p_sys->p_grab );
515 sout_AccessOutDelete( p_sys->p_access );
520 /* create the SDP for a muxed stream (only once) */
521 /* FIXME http://www.faqs.org/rfcs/rfc4566.html
522 o= - should be local username (no spaces allowed)
523 o= time should be hashed with some other value to garantee uniqueness
524 o= don't use the localhost address. use fully qualified domain name or IP4 address
525 a= source-filter: we need our source address
526 a= x-plgroup: (missing)
527 RTP packets need to get the correct src IP address */
529 && net_AddressIsMulticast( VLC_OBJECT(p_stream), p_sys->psz_destination ) )
531 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
532 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
539 asprintf( &p_sys->psz_sdp,
541 /* FIXME: source address not known :( */
542 "o=- "I64Fd" %d IN IP%c %s\r\n"
548 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
549 "a=tool:"PACKAGE_STRING"\r\n"
551 "a=type:broadcast\r\n"
552 "m=video %d RTP/AVP %d\r\n"
553 "a=rtpmap:%d %s\r\n",
554 p_sys->i_sdp_id, p_sys->i_sdp_version,
555 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
556 p_sys->psz_session_name,
557 p_sys->psz_session_description,
558 p_sys->psz_session_url,
559 p_sys->psz_session_email,
560 ipv, p_sys->psz_destination, psz_ttl,
561 p_sys->i_port, p_sys->i_payload_type,
562 p_sys->i_payload_type, psz_rtpmap );
563 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
565 /* create the rtp context */
566 p_sys->ssrc[0] = rand()&0xff;
567 p_sys->ssrc[1] = rand()&0xff;
568 p_sys->ssrc[2] = rand()&0xff;
569 p_sys->ssrc[3] = rand()&0xff;
570 p_sys->i_sequence = rand()&0xffff;
571 p_sys->i_timestamp_start = rand()&0xffffffff;
572 p_sys->packet = NULL;
577 p_sys->p_access = NULL;
578 p_sys->p_grab = NULL;
580 free( val.psz_string );
583 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
584 if( *val.psz_string )
586 config_chain_t *p_cfg;
588 SDPHandleUrl( p_stream, val.psz_string );
590 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
592 if( !strcmp( p_cfg->psz_name, "sdp" ) )
594 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
597 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
600 SDPHandleUrl( p_stream, p_cfg->psz_value );
604 free( val.psz_string );
606 /* update p_sout->i_out_pace_nocontrol */
607 p_stream->p_sout->i_out_pace_nocontrol++;
612 /*****************************************************************************
614 *****************************************************************************/
615 static void Close( vlc_object_t * p_this )
617 sout_stream_t *p_stream = (sout_stream_t*)p_this;
618 sout_stream_sys_t *p_sys = p_stream->p_sys;
620 /* update p_sout->i_out_pace_nocontrol */
621 p_stream->p_sout->i_out_pace_nocontrol--;
625 sout_MuxDelete( p_sys->p_mux );
626 sout_AccessOutDelete( p_sys->p_access );
627 sout_AccessOutDelete( p_sys->p_grab );
630 block_Release( p_sys->packet );
632 if( p_sys->b_export_sap )
635 SapSetup( p_stream );
639 while( p_sys->i_rtsp > 0 )
640 RtspClientDel( p_stream, p_sys->rtsp[0] );
642 vlc_mutex_destroy( &p_sys->lock_sdp );
644 if( p_sys->p_httpd_file )
645 httpd_FileDelete( p_sys->p_httpd_file );
647 if( p_sys->p_httpd_host )
648 httpd_HostDelete( p_sys->p_httpd_host );
650 if( p_sys->p_rtsp_url )
651 httpd_UrlDelete( p_sys->p_rtsp_url );
653 if( p_sys->p_rtsp_host )
654 httpd_HostDelete( p_sys->p_rtsp_host );
656 if( p_sys->psz_session_name )
657 free( p_sys->psz_session_name );
659 if( p_sys->psz_session_description )
660 free( p_sys->psz_session_description );
662 if( p_sys->psz_session_url )
663 free( p_sys->psz_session_url );
665 if( p_sys->psz_session_email )
666 free( p_sys->psz_session_email );
669 free( p_sys->psz_sdp );
671 if( p_sys->b_export_sdp_file )
674 unlink( p_sys->psz_sdp_file );
676 free( p_sys->psz_sdp_file );
678 if( p_sys->psz_destination )
679 free( p_sys->psz_destination );
683 /*****************************************************************************
685 *****************************************************************************/
686 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
688 sout_stream_sys_t *p_sys = p_stream->p_sys;
691 vlc_UrlParse( &url, psz_url, 0 );
692 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
694 if( p_sys->p_httpd_file )
696 msg_Err( p_stream, "you can use sdp=http:// only once" );
700 if( HttpSetup( p_stream, &url ) )
702 msg_Err( p_stream, "cannot export sdp as http" );
705 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
707 if( p_sys->p_rtsp_url )
709 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
713 /* FIXME test if destination is multicast or no destination at all FIXME */
714 if( RtspSetup( p_stream, &url ) )
716 msg_Err( p_stream, "cannot export sdp as rtsp" );
719 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
720 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
722 p_sys->b_export_sap = VLC_TRUE;
723 SapSetup( p_stream );
725 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
727 if( p_sys->b_export_sdp_file )
729 msg_Err( p_stream, "you can use sdp=file:// only once" );
732 p_sys->b_export_sdp_file = VLC_TRUE;
733 psz_url = &psz_url[5];
734 if( psz_url[0] == '/' && psz_url[1] == '/' )
736 p_sys->psz_sdp_file = strdup( psz_url );
740 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
743 vlc_UrlClean( &url );
746 /*****************************************************************************
748 *****************************************************************************/
749 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
750 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
751 o= - should be local username (no spaces allowed)
752 o= time should be hashed with some other value to garantue uniqueness
753 o= we need IP6 support?
754 o= don't use the localhost address. use fully qualified domain name or IP4 address
755 p= international phone number (pass via vars?)
757 a= recvonly (missing)
758 a= type:broadcast (missing)
759 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
760 a= x-plgroup: (missing)
761 RTP packets need to get the correct src IP address */
762 static char *SDPGenerate( const sout_stream_t *p_stream,
763 const char *psz_destination, vlc_bool_t b_rtsp )
765 sout_stream_sys_t *p_sys = p_stream->p_sys;
767 char *psz_sdp, *p, ipv;
770 /* FIXME: breaks IP version check on unknown destination */
771 if( psz_destination == NULL )
772 psz_destination = "0.0.0.0";
774 i_size = sizeof( "v=0\r\n" ) +
775 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
776 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
777 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
778 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
779 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
780 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
781 sizeof( "b=RR:0\r\n" ) +
782 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
783 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
784 strlen( psz_destination ) ;
785 for( i = 0; i < p_sys->i_es; i++ )
787 sout_stream_id_t *id = p_sys->es[i];
789 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
792 i_size += strlen( "b=AS: *\r\n") + 10;
796 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
800 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
804 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
809 i_size += strlen( "m=video %d RTP/AVP %d\r\n" ) +10 +10;
812 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
814 p = psz_sdp = malloc( i_size );
815 p += sprintf( p, "v=0\r\n" );
816 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
817 p_sys->i_sdp_id, p_sys->i_sdp_version,
818 ipv, ipv == '6' ? "::1" : "127.0.0.1" );
819 if( *p_sys->psz_session_name )
820 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
821 if( *p_sys->psz_session_description )
822 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
823 if( *p_sys->psz_session_url )
824 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
825 if( *p_sys->psz_session_email )
826 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
828 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
829 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
831 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
834 && net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
836 /* Add the deprecated TTL field if it is an IPv4 multicast address */
837 p += sprintf( p, "/%d", p_sys->i_ttl ?: 1 );
839 p += sprintf( p, "\r\n" );
841 for( i = 0; i < p_sys->i_es; i++ )
843 sout_stream_id_t *id = p_sys->es[i];
845 if( id->i_cat == AUDIO_ES )
847 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
848 id->i_port, id->i_payload_type );
850 else if( id->i_cat == VIDEO_ES )
852 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
853 id->i_port, id->i_payload_type );
861 p += sprintf(p,"b=AS:%d\r\n",id->i_bitrate);
863 p += sprintf( p, "b=RR:0\r\n" );
866 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
871 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
876 p += sprintf( p, "a=control:/trackID=%d\r\n", i );
881 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
882 p_sys->i_port, p_sys->i_payload_type );
888 /*****************************************************************************
890 *****************************************************************************/
891 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
892 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
893 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
894 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
895 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
896 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
897 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
898 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
899 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
900 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
901 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
903 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
905 static const char hex[16] = "0123456789abcdef";
908 for( i = 0; i < i_data; i++ )
910 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
911 s[2*i+1] = hex[(p_data[i] )&0xf];
916 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
918 sout_instance_t *p_sout = p_stream->p_sout;
919 sout_stream_sys_t *p_sys = p_stream->p_sys;
920 sout_stream_id_t *id;
921 sout_access_out_t *p_access = NULL;
925 if( p_sys->p_mux != NULL )
927 sout_input_t *p_input = NULL;
928 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
930 msg_Err( p_stream, "cannot add this stream to the muxer" );
934 id = malloc( sizeof( sout_stream_id_t ) );
935 memset( id, 0, sizeof( sout_stream_id_t ) );
937 id->p_input = p_input;
938 id->pf_packetize= NULL;
939 id->p_rtsp_url = NULL;
945 /* Choose the port */
947 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
949 i_port = p_sys->i_port_audio;
950 p_sys->i_port_audio = 0;
952 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
954 i_port = p_sys->i_port_video;
955 p_sys->i_port_video = 0;
959 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
961 i_port = p_sys->i_port;
968 if( p_sys->psz_destination )
971 char url[NI_MAXHOST + 8];
973 /* first try to create the access out */
976 snprintf( access, sizeof( access ), "udp{raw,rtcp,ttl=%d}",
978 access[sizeof( access ) - 1] = '\0';
981 strcpy( access, "udp{raw,rtcp}" );
983 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
984 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
985 p_sys->psz_destination, i_port );
986 url[sizeof( url ) - 1] = '\0';
988 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
990 msg_Err( p_stream, "cannot create the access out for %s://%s",
994 msg_Dbg( p_stream, "access out %s:%s", access, url );
997 /* not create the rtp specific stuff */
998 id = malloc( sizeof( sout_stream_id_t ) );
999 memset( id, 0, sizeof( sout_stream_id_t ) );
1000 id->p_stream = p_stream;
1001 id->p_access = p_access;
1003 id->psz_rtpmap = NULL;
1004 id->psz_fmtp = NULL;
1005 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
1006 id->i_port = i_port;
1007 id->p_rtsp_url = NULL;
1008 vlc_mutex_init( p_stream, &id->lock_rtsp );
1009 id->i_rtsp_access = 0;
1010 id->rtsp_access = NULL;
1012 switch( p_fmt->i_codec )
1014 case VLC_FOURCC( 's', '1', '6', 'b' ):
1015 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
1017 id->i_payload_type = 11;
1019 else if( p_fmt->audio.i_channels == 2 &&
1020 p_fmt->audio.i_rate == 44100 )
1022 id->i_payload_type = 10;
1026 id->i_payload_type = p_sys->i_payload_type++;
1028 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
1029 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
1030 p_fmt->audio.i_channels );
1031 id->i_clock_rate = p_fmt->audio.i_rate;
1032 id->pf_packetize = rtp_packetize_l16;
1034 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
1035 id->i_payload_type = p_sys->i_payload_type++;
1036 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
1037 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
1038 p_fmt->audio.i_channels );
1039 id->i_clock_rate = p_fmt->audio.i_rate;
1040 id->pf_packetize = rtp_packetize_l8;
1042 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1043 id->i_payload_type = 14;
1044 id->i_clock_rate = 90000;
1045 id->psz_rtpmap = strdup( "MPA/90000" );
1046 id->pf_packetize = rtp_packetize_mpa;
1048 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1049 id->i_payload_type = 32;
1050 id->i_clock_rate = 90000;
1051 id->psz_rtpmap = strdup( "MPV/90000" );
1052 id->pf_packetize = rtp_packetize_mpv;
1054 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1055 id->i_payload_type = p_sys->i_payload_type++;
1056 id->i_clock_rate = 90000;
1057 id->psz_rtpmap = strdup( "ac3/90000" );
1058 id->pf_packetize = rtp_packetize_ac3;
1060 case VLC_FOURCC( 'H', '2', '6', '3' ):
1061 id->i_payload_type = p_sys->i_payload_type++;
1062 id->i_clock_rate = 90000;
1063 id->psz_rtpmap = strdup( "H263-1998/90000" );
1064 id->pf_packetize = rtp_packetize_h263;
1066 case VLC_FOURCC( 'h', '2', '6', '4' ):
1067 id->i_payload_type = p_sys->i_payload_type++;
1068 id->i_clock_rate = 90000;
1069 id->psz_rtpmap = strdup( "H264/90000" );
1070 id->pf_packetize = rtp_packetize_h264;
1071 id->psz_fmtp = NULL;
1073 if( p_fmt->i_extra > 0 )
1075 uint8_t *p_buffer = p_fmt->p_extra;
1076 int i_buffer = p_fmt->i_extra;
1077 char *p_64_sps = NULL;
1078 char *p_64_pps = NULL;
1081 while( i_buffer > 4 &&
1082 p_buffer[0] == 0 && p_buffer[1] == 0 &&
1083 p_buffer[2] == 0 && p_buffer[3] == 1 )
1085 const int i_nal_type = p_buffer[4]&0x1f;
1089 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
1092 for( i_offset = 4; i_offset+3 < i_buffer ; i_offset++)
1094 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
1096 /* we found another startcode */
1101 if( i_nal_type == 7 )
1103 p_64_sps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1104 sprintf_hexa( hexa, &p_buffer[5], 3 );
1106 else if( i_nal_type == 8 )
1108 p_64_pps = vlc_b64_encode_binary( &p_buffer[4], i_size - 4 );
1114 if( p_64_sps && p_64_pps )
1115 asprintf( &id->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
1122 id->psz_fmtp = strdup( "packetization-mode=1" );
1125 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1127 char hexa[2*p_fmt->i_extra +1];
1129 id->i_payload_type = p_sys->i_payload_type++;
1130 id->i_clock_rate = 90000;
1131 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1132 id->pf_packetize = rtp_packetize_split;
1133 if( p_fmt->i_extra > 0 )
1135 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1136 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1137 sprintf( id->psz_fmtp,
1138 "profile-level-id=3; config=%s;", hexa );
1142 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1144 id->i_payload_type = p_sys->i_payload_type++;
1145 id->i_clock_rate = p_fmt->audio.i_rate;
1149 char hexa[2*p_fmt->i_extra +1];
1151 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1152 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1153 id->pf_packetize = rtp_packetize_mp4a;
1154 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1155 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1156 sprintf( id->psz_fmtp,
1157 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1158 "config=%s; SizeLength=13;IndexLength=3; "
1159 "IndexDeltaLength=3; Profile=1;", hexa );
1165 unsigned char config[6];
1166 unsigned int aacsrates[15] = {
1167 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1168 16000, 12000, 11025, 8000, 7350, 0, 0 };
1170 for( i = 0; i < 15; i++ )
1171 if( p_fmt->audio.i_rate == aacsrates[i] )
1177 config[3]=p_fmt->audio.i_channels<<4;
1181 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1182 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1183 id->pf_packetize = rtp_packetize_mp4a_latm;
1184 sprintf_hexa( hexa, config, 6 );
1185 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1186 "object=2; cpresent=0; config=%s", hexa );
1190 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1191 id->i_payload_type = p_sys->i_payload_type++;
1192 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1193 "AMR/8000/2" : "AMR/8000" );
1194 id->psz_fmtp = strdup( "octet-align=1" );
1195 id->i_clock_rate = p_fmt->audio.i_rate;
1196 id->pf_packetize = rtp_packetize_amr;
1198 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1199 id->i_payload_type = p_sys->i_payload_type++;
1200 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1201 "AMR-WB/16000/2" : "AMR-WB/16000" );
1202 id->psz_fmtp = strdup( "octet-align=1" );
1203 id->i_clock_rate = p_fmt->audio.i_rate;
1204 id->pf_packetize = rtp_packetize_amr;
1208 msg_Err( p_stream, "cannot add this stream (unsupported "
1209 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1212 sout_AccessOutDelete( p_access );
1217 id->i_cat = p_fmt->i_cat;
1219 id->ssrc[0] = rand()&0xff;
1220 id->ssrc[1] = rand()&0xff;
1221 id->ssrc[2] = rand()&0xff;
1222 id->ssrc[3] = rand()&0xff;
1223 id->i_sequence = rand()&0xffff;
1224 id->i_timestamp_start = rand()&0xffffffff;
1225 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
1227 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1228 if( id->i_mtu <= 16 + MTU_REDUCE )
1230 /* better than nothing */
1233 id->i_mtu -= MTU_REDUCE;
1234 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1236 if( p_sys->p_rtsp_url )
1238 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1240 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1241 msg_Dbg( p_stream, "rtsp: adding %s\n", psz_urlc );
1242 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1244 if( id->p_rtsp_url )
1246 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1247 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1248 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1253 /* Update p_sys context */
1254 vlc_mutex_lock( &p_sys->lock_es );
1255 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1256 vlc_mutex_unlock( &p_sys->lock_es );
1258 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1260 vlc_mutex_lock( &p_sys->lock_sdp );
1261 free( p_sys->psz_sdp );
1262 p_sys->psz_sdp = psz_sdp;
1263 vlc_mutex_unlock( &p_sys->lock_sdp );
1265 p_sys->i_sdp_version++;
1267 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
1269 /* Update SDP (sap/file) */
1270 if( p_sys->b_export_sap ) SapSetup( p_stream );
1271 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1276 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1278 sout_stream_sys_t *p_sys = p_stream->p_sys;
1280 vlc_mutex_lock( &p_sys->lock_es );
1281 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1282 vlc_mutex_unlock( &p_sys->lock_es );
1285 if( id->i_port > 0 )
1287 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1288 p_sys->i_port_audio = id->i_port;
1289 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1290 p_sys->i_port_video = id->i_port;
1295 if( id->psz_rtpmap )
1297 free( id->psz_rtpmap );
1301 free( id->psz_fmtp );
1303 if( id->psz_destination )
1304 free( id->psz_destination );
1305 sout_AccessOutDelete( id->p_access );
1307 else if( id->p_input )
1309 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1311 if( id->p_rtsp_url )
1313 httpd_UrlDelete( id->p_rtsp_url );
1315 vlc_mutex_destroy( &id->lock_rtsp );
1316 if( id->rtsp_access ) free( id->rtsp_access );
1318 /* Update SDP (sap/file) */
1319 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1320 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1326 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1331 if( p_stream->p_sys->p_mux )
1333 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1339 p_next = p_buffer->p_next;
1340 if( id->pf_packetize( p_stream, id, p_buffer ) )
1344 block_Release( p_buffer );
1352 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1355 sout_stream_sys_t *p_sys = p_stream->p_sys;
1357 int64_t i_dts = p_buffer->i_dts;
1358 uint32_t i_timestamp = i_dts * 9 / 100;
1360 uint8_t *p_data = p_buffer->p_buffer;
1361 unsigned int i_data = p_buffer->i_buffer;
1362 unsigned int i_max = p_sys->i_mtu - 12;
1364 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1368 unsigned int i_size;
1370 /* output complete packet */
1371 if( p_sys->packet &&
1372 p_sys->packet->i_buffer + i_data > i_max )
1374 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1375 p_sys->packet = NULL;
1378 if( p_sys->packet == NULL )
1380 /* allocate a new packet */
1381 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1382 p_sys->packet->p_buffer[ 0] = 0x80;
1383 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1384 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1385 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1386 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1387 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1388 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1389 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1390 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1391 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1392 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1393 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1394 p_sys->packet->i_buffer = 12;
1396 p_sys->packet->i_dts = i_dts;
1397 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1398 i_dts += p_sys->packet->i_length;
1400 p_sys->i_sequence++;
1403 i_size = __MIN( i_data,
1404 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1406 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1409 p_sys->packet->i_buffer += i_size;
1417 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1420 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1422 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1428 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1430 p_next = p_buffer->p_next;
1431 block_Release( p_buffer );
1438 /****************************************************************************
1440 ****************************************************************************/
1441 static int SapSetup( sout_stream_t *p_stream )
1443 sout_stream_sys_t *p_sys = p_stream->p_sys;
1444 sout_instance_t *p_sout = p_stream->p_sout;
1445 announce_method_t *p_method = sout_SAPMethod();
1447 /* Remove the previous session */
1448 if( p_sys->p_session != NULL)
1450 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1451 sout_AnnounceSessionDestroy( p_sys->p_session );
1452 p_sys->p_session = NULL;
1455 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1457 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, SOUT_CFG_PREFIX,
1459 p_sys->psz_destination,
1463 sout_MethodRelease( p_method );
1467 /****************************************************************************
1469 ****************************************************************************/
1470 static int FileSetup( sout_stream_t *p_stream )
1472 sout_stream_sys_t *p_sys = p_stream->p_sys;
1475 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1477 msg_Err( p_stream, "cannot open file '%s' (%s)",
1478 p_sys->psz_sdp_file, strerror(errno) );
1479 return VLC_EGENERIC;
1482 fprintf( f, "%s", p_sys->psz_sdp );
1488 /****************************************************************************
1490 ****************************************************************************/
1491 static int HttpCallback( httpd_file_sys_t *p_args,
1492 httpd_file_t *, uint8_t *p_request,
1493 uint8_t **pp_data, int *pi_data );
1495 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1497 sout_stream_sys_t *p_sys = p_stream->p_sys;
1499 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1500 if( p_sys->p_httpd_host )
1502 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1503 url->psz_path ? url->psz_path : "/",
1506 HttpCallback, (void*)p_sys );
1508 if( p_sys->p_httpd_file == NULL )
1510 return VLC_EGENERIC;
1515 static int HttpCallback( httpd_file_sys_t *p_args,
1516 httpd_file_t *f, uint8_t *p_request,
1517 uint8_t **pp_data, int *pi_data )
1519 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1521 vlc_mutex_lock( &p_sys->lock_sdp );
1522 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1524 *pi_data = strlen( p_sys->psz_sdp );
1525 *pp_data = malloc( *pi_data );
1526 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1533 vlc_mutex_unlock( &p_sys->lock_sdp );
1538 /****************************************************************************
1540 ****************************************************************************/
1541 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, const char *psz_session )
1543 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1545 rtsp->psz_session = strdup( psz_session );
1547 rtsp->b_playing = VLC_FALSE;
1551 rtsp->access = NULL;
1553 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1557 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, const char *psz_session )
1561 if( !psz_session ) return NULL;
1563 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1565 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1567 return p_stream->p_sys->rtsp[i];
1573 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1576 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1578 for( i = 0; i < rtsp->i_access; i++ )
1580 sout_AccessOutDelete( rtsp->access[i] );
1582 if( rtsp->id ) free( rtsp->id );
1583 if( rtsp->access ) free( rtsp->access );
1585 free( rtsp->psz_session );
1589 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1591 sout_stream_sys_t *p_sys = p_stream->p_sys;
1593 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1595 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1596 if( p_sys->p_rtsp_host == NULL )
1598 return VLC_EGENERIC;
1601 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1602 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1603 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1604 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1606 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1607 if( p_sys->p_rtsp_url == 0 )
1609 return VLC_EGENERIC;
1611 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1612 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1613 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1614 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1619 static int RtspCallback( httpd_callback_sys_t *p_args,
1621 httpd_message_t *answer, httpd_message_t *query )
1623 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1624 sout_stream_sys_t *p_sys = p_stream->p_sys;
1625 char *psz_destination = p_sys->psz_destination;
1626 const char *psz_session = NULL;
1627 const char *psz_cseq = NULL;
1630 if( answer == NULL || query == NULL )
1634 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1636 answer->i_proto = HTTPD_PROTO_RTSP;
1637 answer->i_version= query->i_version;
1638 answer->i_type = HTTPD_MSG_ANSWER;
1640 switch( query->i_type )
1642 case HTTPD_MSG_DESCRIBE:
1644 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1646 answer->i_status = 200;
1647 answer->psz_status = strdup( "OK" );
1648 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1649 httpd_MsgAdd( answer, "Content-Base", "%s", p_sys->psz_rtsp_control );
1650 answer->p_body = (uint8_t *)psz_sdp;
1651 answer->i_body = strlen( psz_sdp );
1655 case HTTPD_MSG_PLAY:
1657 rtsp_client_t *rtsp;
1658 /* for now only multicast so easy */
1659 answer->i_status = 200;
1660 answer->psz_status = strdup( "OK" );
1662 answer->p_body = NULL;
1664 psz_session = httpd_MsgGet( query, "Session" );
1665 rtsp = RtspClientGet( p_stream, psz_session );
1666 if( rtsp && !rtsp->b_playing )
1670 rtsp->b_playing = VLC_TRUE;
1672 vlc_mutex_lock( &p_sys->lock_es );
1673 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1675 sout_stream_id_t *id = rtsp->id[i_id];
1678 for( i = 0; i < p_sys->i_es; i++ )
1680 if( id == p_sys->es[i] )
1683 if( i >= p_sys->i_es ) continue;
1685 vlc_mutex_lock( &id->lock_rtsp );
1686 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1687 vlc_mutex_unlock( &id->lock_rtsp );
1689 vlc_mutex_unlock( &p_sys->lock_es );
1693 case HTTPD_MSG_PAUSE:
1695 return VLC_EGENERIC;
1696 case HTTPD_MSG_TEARDOWN:
1698 rtsp_client_t *rtsp;
1700 /* for now only multicast so easy again */
1701 answer->i_status = 200;
1702 answer->psz_status = strdup( "OK" );
1704 answer->p_body = NULL;
1706 psz_session = httpd_MsgGet( query, "Session" );
1707 rtsp = RtspClientGet( p_stream, psz_session );
1712 vlc_mutex_lock( &p_sys->lock_es );
1713 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1715 sout_stream_id_t *id = rtsp->id[i_id];
1718 for( i = 0; i < p_sys->i_es; i++ )
1720 if( id == p_sys->es[i] )
1723 if( i >= p_sys->i_es ) continue;
1725 vlc_mutex_lock( &id->lock_rtsp );
1726 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1727 vlc_mutex_unlock( &id->lock_rtsp );
1729 vlc_mutex_unlock( &p_sys->lock_es );
1731 RtspClientDel( p_stream, rtsp );
1737 return VLC_EGENERIC;
1739 httpd_MsgAdd( answer, "Server", PACKAGE_STRING );
1740 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1741 psz_cseq = httpd_MsgGet( query, "Cseq" );
1743 i_cseq = atoi( psz_cseq );
1746 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1747 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1750 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1754 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1756 httpd_message_t *answer, httpd_message_t *query )
1758 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1759 sout_stream_t *p_stream = id->p_stream;
1760 sout_stream_sys_t *p_sys = p_stream->p_sys;
1761 char psz_session_init[100];
1762 const char *psz_session = NULL;
1763 const char *psz_cseq = NULL;
1767 if( answer == NULL || query == NULL )
1769 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1772 snprintf( psz_session_init, sizeof(psz_session_init), "%d", rand() );
1775 answer->i_proto = HTTPD_PROTO_RTSP;
1776 answer->i_version= query->i_version;
1777 answer->i_type = HTTPD_MSG_ANSWER;
1779 switch( query->i_type )
1781 case HTTPD_MSG_SETUP:
1783 const char *psz_transport = httpd_MsgGet( query, "Transport" );
1785 //fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1787 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1789 //fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1790 answer->i_status = 200;
1791 answer->psz_status = strdup( "OK" );
1793 answer->p_body = NULL;
1795 psz_session = httpd_MsgGet( query, "Session" );
1797 psz_session = psz_session_init;
1799 httpd_MsgAdd( answer, "Transport",
1800 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1801 id->psz_destination, id->i_port,id->i_port+1,
1802 p_sys->i_ttl > 0 ? p_sys->i_ttl : 1);
1804 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1806 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1807 char ip[NI_MAXNUMERICHOST], psz_access[22], psz_url[NI_MAXNUMERICHOST + 8];
1809 sout_access_out_t *p_access;
1811 rtsp_client_t *rtsp = NULL;
1813 if( httpd_ClientIP( cl, ip ) == NULL )
1815 answer->i_status = 500;
1816 answer->psz_status = strdup( "Internal server error" );
1818 answer->p_body = NULL;
1822 //fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n", ip, i_port );
1824 psz_session = httpd_MsgGet( query, "Session" );
1827 psz_session = psz_session_init;
1828 rtsp = RtspClientNew( p_stream, psz_session_init );
1832 rtsp = RtspClientGet( p_stream, psz_session );
1835 answer->i_status = 454;
1836 answer->psz_status = strdup( "Unknown session id" );
1838 answer->p_body = NULL;
1843 /* first try to create the access out */
1845 snprintf( psz_access, sizeof( psz_access ),
1846 "udp{raw,rtcp,ttl=%d}", p_sys->i_ttl );
1848 strcpy( psz_access, "udp{raw,rtcp}" );
1850 snprintf( psz_url, sizeof( psz_url ),
1851 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
1854 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1856 msg_Err( p_stream, "cannot create the access out for %s://%s",
1857 psz_access, psz_url );
1858 answer->i_status = 500;
1859 answer->psz_status = strdup( "Internal server error" );
1861 answer->p_body = NULL;
1865 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1866 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1868 answer->i_status = 200;
1869 answer->psz_status = strdup( "OK" );
1871 answer->p_body = NULL;
1873 httpd_MsgAdd( answer, "Transport",
1874 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1876 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1878 answer->i_status = 461;
1879 answer->psz_status = strdup( "Unsupported Transport" );
1881 answer->p_body = NULL;
1887 return VLC_EGENERIC;
1889 httpd_MsgAdd( answer, "Server", "VLC Server" );
1890 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1891 psz_cseq = httpd_MsgGet( query, "Cseq" );
1893 i_cseq = atoi( psz_cseq );
1896 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1897 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1900 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1904 /****************************************************************************
1906 ****************************************************************************/
1907 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1908 int b_marker, int64_t i_pts )
1910 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1912 out->p_buffer[0] = 0x80;
1913 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1914 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1915 out->p_buffer[3] = ( id->i_sequence )&0xff;
1916 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1917 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1918 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1919 out->p_buffer[7] = ( i_timestamp )&0xff;
1921 out->p_buffer[ 8] = id->ssrc[0];
1922 out->p_buffer[ 9] = id->ssrc[1];
1923 out->p_buffer[10] = id->ssrc[2];
1924 out->p_buffer[11] = id->ssrc[3];
1930 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1933 vlc_mutex_lock( &id->lock_rtsp );
1934 for( i = 0; i < id->i_rtsp_access; i++ )
1936 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1938 vlc_mutex_unlock( &id->lock_rtsp );
1942 sout_AccessOutWrite( id->p_access, out );
1946 block_Release( out );
1950 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1953 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1954 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1956 uint8_t *p_data = in->p_buffer;
1957 int i_data = in->i_buffer;
1960 for( i = 0; i < i_count; i++ )
1962 int i_payload = __MIN( i_max, i_data );
1963 block_t *out = block_New( p_stream, 16 + i_payload );
1965 /* rtp common header */
1966 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1968 out->p_buffer[12] = 0;
1969 out->p_buffer[13] = 0;
1970 /* fragment offset in the current frame */
1971 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1972 out->p_buffer[15] = ( (i*i_max) )&0xff;
1973 memcpy( &out->p_buffer[16], p_data, i_payload );
1975 out->i_buffer = 16 + i_payload;
1976 out->i_dts = in->i_dts + i * in->i_length / i_count;
1977 out->i_length = in->i_length / i_count;
1979 rtp_packetize_send( id, out );
1981 p_data += i_payload;
1982 i_data -= i_payload;
1989 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1992 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1993 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1995 uint8_t *p_data = in->p_buffer;
1996 int i_data = in->i_buffer;
1998 int b_sequence_start = 0;
1999 int i_temporal_ref = 0;
2000 int i_picture_coding_type = 0;
2001 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
2002 int b_start_slice = 0;
2004 /* preparse this packet to get some info */
2005 if( in->i_buffer > 4 )
2007 uint8_t *p = p_data;
2008 int i_rest = in->i_buffer;
2012 while( i_rest > 4 &&
2013 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
2027 /* sequence start code */
2028 b_sequence_start = 1;
2030 else if( *p == 0x00 && i_rest >= 4 )
2033 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
2034 i_picture_coding_type = (p[2] >> 3)&0x07;
2036 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
2037 i_picture_coding_type == 3 ) )
2039 i_ffv = (p[3] >> 2)&0x01;
2040 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
2041 if( i_rest > 4 && i_picture_coding_type == 3 )
2043 i_fbv = (p[4]>>6)&0x01;
2044 i_bfc = (p[4]>>3)&0x07;
2048 else if( *p <= 0xaf )
2055 for( i = 0; i < i_count; i++ )
2057 int i_payload = __MIN( i_max, i_data );
2058 block_t *out = block_New( p_stream,
2060 uint32_t h = ( i_temporal_ref << 16 )|
2061 ( b_sequence_start << 13 )|
2062 ( b_start_slice << 12 )|
2063 ( i == i_count - 1 ? 1 << 11 : 0 )|
2064 ( i_picture_coding_type << 8 )|
2065 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
2067 /* rtp common header */
2068 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2069 in->i_pts > 0 ? in->i_pts : in->i_dts );
2071 /* 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 */
2072 out->p_buffer[12] = ( h >> 24 )&0xff;
2073 out->p_buffer[13] = ( h >> 16 )&0xff;
2074 out->p_buffer[14] = ( h >> 8 )&0xff;
2075 out->p_buffer[15] = ( h )&0xff;
2077 memcpy( &out->p_buffer[16], p_data, i_payload );
2079 out->i_buffer = 16 + i_payload;
2080 out->i_dts = in->i_dts + i * in->i_length / i_count;
2081 out->i_length = in->i_length / i_count;
2083 rtp_packetize_send( id, out );
2085 p_data += i_payload;
2086 i_data -= i_payload;
2092 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
2095 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
2096 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2098 uint8_t *p_data = in->p_buffer;
2099 int i_data = in->i_buffer;
2102 for( i = 0; i < i_count; i++ )
2104 int i_payload = __MIN( i_max, i_data );
2105 block_t *out = block_New( p_stream, 14 + i_payload );
2107 /* rtp common header */
2108 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
2110 out->p_buffer[12] = 1;
2112 out->p_buffer[13] = 0x00;
2114 memcpy( &out->p_buffer[14], p_data, i_payload );
2116 out->i_buffer = 14 + i_payload;
2117 out->i_dts = in->i_dts + i * in->i_length / i_count;
2118 out->i_length = in->i_length / i_count;
2120 rtp_packetize_send( id, out );
2122 p_data += i_payload;
2123 i_data -= i_payload;
2129 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
2132 int i_max = id->i_mtu - 12; /* payload max in one packet */
2133 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2135 uint8_t *p_data = in->p_buffer;
2136 int i_data = in->i_buffer;
2139 for( i = 0; i < i_count; i++ )
2141 int i_payload = __MIN( i_max, i_data );
2142 block_t *out = block_New( p_stream, 12 + i_payload );
2144 /* rtp common header */
2145 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2146 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2147 memcpy( &out->p_buffer[12], p_data, i_payload );
2149 out->i_buffer = 12 + i_payload;
2150 out->i_dts = in->i_dts + i * in->i_length / i_count;
2151 out->i_length = in->i_length / i_count;
2153 rtp_packetize_send( id, out );
2155 p_data += i_payload;
2156 i_data -= i_payload;
2163 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
2166 int i_max = id->i_mtu - 14; /* payload max in one packet */
2167 int latmhdrsize = in->i_buffer / 0xff + 1;
2168 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2170 uint8_t *p_data = in->p_buffer, *p_header = NULL;
2171 int i_data = in->i_buffer;
2174 for( i = 0; i < i_count; i++ )
2176 int i_payload = __MIN( i_max, i_data );
2181 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
2183 /* rtp common header */
2184 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
2185 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2189 int tmp = in->i_buffer;
2191 p_header=out->p_buffer+12;
2201 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
2203 out->i_buffer = 12 + latmhdrsize + i_payload;
2204 out->i_dts = in->i_dts + i * in->i_length / i_count;
2205 out->i_length = in->i_length / i_count;
2207 rtp_packetize_send( id, out );
2209 p_data += i_payload;
2210 i_data -= i_payload;
2216 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
2219 int i_max = id->i_mtu - 12; /* payload max in one packet */
2220 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2222 uint8_t *p_data = in->p_buffer;
2223 int i_data = in->i_buffer;
2228 int i_payload = (__MIN( i_max, i_data )/4)*4;
2229 block_t *out = block_New( p_stream, 12 + i_payload );
2231 /* rtp common header */
2232 rtp_packetize_common( id, out, 0,
2233 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2234 memcpy( &out->p_buffer[12], p_data, i_payload );
2236 out->i_buffer = 12 + i_payload;
2237 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2238 out->i_length = in->i_length / i_count;
2240 rtp_packetize_send( id, out );
2242 p_data += i_payload;
2243 i_data -= i_payload;
2250 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2253 int i_max = id->i_mtu - 12; /* payload max in one packet */
2254 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2256 uint8_t *p_data = in->p_buffer;
2257 int i_data = in->i_buffer;
2262 int i_payload = (__MIN( i_max, i_data )/2)*2;
2263 block_t *out = block_New( p_stream, 12 + i_payload );
2265 /* rtp common header */
2266 rtp_packetize_common( id, out, 0,
2267 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2268 memcpy( &out->p_buffer[12], p_data, i_payload );
2270 out->i_buffer = 12 + i_payload;
2271 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2272 out->i_length = in->i_length / i_count;
2274 rtp_packetize_send( id, out );
2276 p_data += i_payload;
2277 i_data -= i_payload;
2283 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2286 int i_max = id->i_mtu - 16; /* payload max in one packet */
2287 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2289 uint8_t *p_data = in->p_buffer;
2290 int i_data = in->i_buffer;
2293 for( i = 0; i < i_count; i++ )
2295 int i_payload = __MIN( i_max, i_data );
2296 block_t *out = block_New( p_stream, 16 + i_payload );
2298 /* rtp common header */
2299 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2300 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2302 /* AU headers length (bits) */
2303 out->p_buffer[12] = 0;
2304 out->p_buffer[13] = 2*8;
2305 /* for each AU length 13 bits + idx 3bits, */
2306 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2307 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2309 memcpy( &out->p_buffer[16], p_data, i_payload );
2311 out->i_buffer = 16 + i_payload;
2312 out->i_dts = in->i_dts + i * in->i_length / i_count;
2313 out->i_length = in->i_length / i_count;
2315 rtp_packetize_send( id, out );
2317 p_data += i_payload;
2318 i_data -= i_payload;
2326 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2327 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2328 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2331 uint8_t *p_data = in->p_buffer;
2332 int i_data = in->i_buffer;
2334 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2337 int b_v_bit = 0; // no pesky error resilience
2338 int i_plen = 0; // normally plen=0 for PSC packet
2339 int i_pebit = 0; // because plen=0
2344 return VLC_EGENERIC;
2346 if( p_data[0] || p_data[1] )
2348 return VLC_EGENERIC;
2350 /* remove 2 leading 0 bytes */
2353 i_count = ( i_data + i_max - 1 ) / i_max;
2355 for( i = 0; i < i_count; i++ )
2357 int i_payload = __MIN( i_max, i_data );
2358 block_t *out = block_New( p_stream,
2359 RTP_H263_PAYLOAD_START + i_payload );
2360 b_p_bit = (i == 0) ? 1 : 0;
2361 h = ( b_p_bit << 10 )|
2366 /* rtp common header */
2367 //b_m_bit = 1; // always contains end of frame
2368 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2369 in->i_pts > 0 ? in->i_pts : in->i_dts );
2372 out->p_buffer[12] = ( h >> 8 )&0xff;
2373 out->p_buffer[13] = ( h )&0xff;
2374 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2376 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2377 out->i_dts = in->i_dts + i * in->i_length / i_count;
2378 out->i_length = in->i_length / i_count;
2380 rtp_packetize_send( id, out );
2382 p_data += i_payload;
2383 i_data -= i_payload;
2390 static int rtp_packetize_h264_nal( sout_stream_t *p_stream, sout_stream_id_t *id,
2391 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 )
2393 const int i_max = id->i_mtu - 12; /* payload max in one packet */
2400 i_nal_hdr = p_data[3];
2401 i_nal_type = i_nal_hdr&0x1f;
2402 if( i_nal_type == 7 || i_nal_type == 8 )
2404 /* XXX Why do you want to remove them ? It will break streaming with
2405 * SPS/PPS change (broadcast) ? */
2409 /* Skip start code */
2414 if( i_data <= i_max )
2416 /* Single NAL unit packet */
2417 block_t *out = block_New( p_stream, 12 + i_data );
2419 out->i_length = i_length;
2422 rtp_packetize_common( id, out, b_last, i_pts );
2423 out->i_buffer = 12 + i_data;
2425 memcpy( &out->p_buffer[12], p_data, i_data );
2427 rtp_packetize_send( id, out );
2431 /* FU-A Fragmentation Unit without interleaving */
2432 const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
2438 for( i = 0; i < i_count; i++ )
2440 const int i_payload = __MIN( i_data, i_max-2 );
2441 block_t *out = block_New( p_stream, 12 + 2 + i_payload );
2442 out->i_dts = i_dts + i * i_length / i_count;
2443 out->i_length = i_length / i_count;
2446 rtp_packetize_common( id, out, (b_last && i_payload == i_data), i_pts );
2447 out->i_buffer = 14 + i_payload;
2450 out->p_buffer[12] = 0x00 | (i_nal_hdr & 0x60) | 28;
2452 out->p_buffer[13] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 ) | i_nal_type;
2453 memcpy( &out->p_buffer[14], p_data, i_payload );
2455 rtp_packetize_send( id, out );
2457 i_data -= i_payload;
2458 p_data += i_payload;
2464 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
2467 const uint8_t *p_buffer = in->p_buffer;
2468 int i_buffer = in->i_buffer;
2470 while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )
2476 /* Split nal units */
2477 while( i_buffer > 4 )
2480 int i_size = i_buffer;
2481 int i_skip = i_buffer;
2483 /* search nal end */
2484 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)
2486 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )
2488 /* we found another startcode */
2489 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);
2494 /* TODO add STAP-A to remove a lot of overhead with small slice/sei/... */
2495 rtp_packetize_h264_nal( p_stream, id, p_buffer, i_size,
2496 (in->i_pts > 0 ? in->i_pts : in->i_dts), in->i_dts,
2497 (i_size >= i_buffer), in->i_length * i_size / in->i_buffer );
2505 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2508 int i_max = id->i_mtu - 14; /* payload max in one packet */
2509 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2511 uint8_t *p_data = in->p_buffer;
2512 int i_data = in->i_buffer;
2515 /* Only supports octet-aligned mode */
2516 for( i = 0; i < i_count; i++ )
2518 int i_payload = __MIN( i_max, i_data );
2519 block_t *out = block_New( p_stream, 14 + i_payload );
2521 /* rtp common header */
2522 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2523 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2524 /* Payload header */
2525 out->p_buffer[12] = 0xF0; /* CMR */
2526 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
2528 /* FIXME: are we fed multiple frames ? */
2529 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
2531 out->i_buffer = 14 + i_payload-1;
2532 out->i_dts = in->i_dts + i * in->i_length / i_count;
2533 out->i_length = in->i_length / i_count;
2535 rtp_packetize_send( id, out );
2537 p_data += i_payload;
2538 i_data -= i_payload;