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 *****************************************************************************/
32 #include <vlc/input.h>
35 #include "vlc_httpd.h"
40 /*****************************************************************************
42 *****************************************************************************/
46 #define DST_TEXT N_("Destination")
47 #define DST_LONGTEXT N_( \
48 "Allows you to specify the output URL used for the streaming output." )
49 #define SDP_TEXT N_("SDP")
50 #define SDP_LONGTEXT N_( \
51 "Allows you to specify the SDP used for the streaming output. " \
52 "You must use an url: http://location to access the SDP via HTTP, " \
53 "rtsp://location for RTSP access, and sap:// for the SDP to be " \
54 "announced via SAP." )
55 #define MUX_TEXT N_("Muxer")
56 #define MUX_LONGTEXT N_( \
57 "Allows you to specify the muxer used for the streaming output." )
59 #define NAME_TEXT N_("Session name")
60 #define NAME_LONGTEXT N_( \
61 "Allows you to specify the session name used for the streaming output." )
62 #define DESC_TEXT N_("Session description")
63 #define DESC_LONGTEXT N_( \
64 "Allows you to give a broader description of the stream." )
65 #define URL_TEXT N_("Session URL")
66 #define URL_LONGTEXT N_( \
67 "Allows you to specify a URL with additional information on the stream." )
68 #define EMAIL_TEXT N_("Session email")
69 #define EMAIL_LONGTEXT N_( \
70 "Allows you to specify contact e-mail address for this session." )
72 #define PORT_TEXT N_("Port")
73 #define PORT_LONGTEXT N_( \
74 "Allows you to specify the base port used for the RTP streaming." )
75 #define PORT_AUDIO_TEXT N_("Audio port")
76 #define PORT_AUDIO_LONGTEXT N_( \
77 "Allows you to specify the default audio port used for the RTP streaming." )
78 #define PORT_VIDEO_TEXT N_("Video port")
79 #define PORT_VIDEO_LONGTEXT N_( \
80 "Allows you to specify the default video port used for the RTP streaming." )
82 #define TTL_TEXT N_("Time-To-Live (TTL)")
83 #define TTL_LONGTEXT N_( \
84 "Allows you to specify the Time-To-Live for the output stream." )
86 #define RFC3016_TEXT N_("RFC3016(LATM)")
87 #define RFC3016_LONGTEXT N_( \
88 "Allows you to specify using RFC3016 for MPEG4 audio streaming." )
90 static int Open ( vlc_object_t * );
91 static void Close( vlc_object_t * );
93 #define SOUT_CFG_PREFIX "sout-rtp-"
96 set_shortname( _("RTP"));
97 set_description( _("RTP stream output") );
98 set_capability( "sout stream", 0 );
99 add_shortcut( "rtp" );
100 set_category( CAT_SOUT );
101 set_subcategory( SUBCAT_SOUT_STREAM );
103 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
104 DST_LONGTEXT, VLC_TRUE );
105 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
106 SDP_LONGTEXT, VLC_TRUE );
107 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
108 MUX_LONGTEXT, VLC_TRUE );
110 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
111 NAME_LONGTEXT, VLC_TRUE );
112 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
113 DESC_LONGTEXT, VLC_TRUE );
114 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
115 URL_LONGTEXT, VLC_TRUE );
116 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
117 EMAIL_LONGTEXT, VLC_TRUE );
119 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
120 PORT_LONGTEXT, VLC_TRUE );
121 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
122 PORT_AUDIO_LONGTEXT, VLC_TRUE );
123 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
124 PORT_VIDEO_LONGTEXT, VLC_TRUE );
126 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
127 TTL_LONGTEXT, VLC_TRUE );
129 add_bool( SOUT_CFG_PREFIX "rfc3016", 0, NULL, RFC3016_TEXT,
130 RFC3016_LONGTEXT, VLC_FALSE );
132 set_callbacks( Open, Close );
135 /*****************************************************************************
136 * Exported prototypes
137 *****************************************************************************/
138 static const char *ppsz_sout_options[] = {
139 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
140 "description", "url","email", "rfc3016", NULL
143 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
144 static int Del ( sout_stream_t *, sout_stream_id_t * );
145 static int Send( sout_stream_t *, sout_stream_id_t *,
148 /* For unicast/interleaved streaming */
152 int64_t i_last; /* for timeout */
154 /* is it in "play" state */
155 vlc_bool_t b_playing;
157 /* output (id-access) */
159 sout_stream_id_t **id;
161 sout_access_out_t **access;
164 struct sout_stream_sys_t
170 vlc_mutex_t lock_sdp;
172 char *psz_session_name;
173 char *psz_session_description;
174 char *psz_session_url;
175 char *psz_session_email;
178 vlc_bool_t b_export_sdp_file;
181 vlc_bool_t b_export_sap;
182 session_descriptor_t *p_session;
184 httpd_host_t *p_httpd_host;
185 httpd_file_t *p_httpd_file;
187 httpd_host_t *p_rtsp_host;
188 httpd_url_t *p_rtsp_url;
189 char *psz_rtsp_control;
193 char *psz_destination;
198 vlc_bool_t b_rfc3016;
200 /* when need to use a private one or when using muxer */
203 /* in case we do TS/PS over rtp */
205 sout_access_out_t *p_access;
207 sout_access_out_t *p_grab;
209 uint32_t i_timestamp_start;
216 sout_stream_id_t **es;
220 rtsp_client_t **rtsp;
223 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
226 struct sout_stream_id_t
228 sout_stream_t *p_stream;
230 uint8_t i_payload_type;
232 uint32_t i_timestamp_start;
239 char *psz_destination;
243 /* Packetizer specific fields */
244 pf_rtp_packetizer_t pf_packetize;
247 /* for sending the packets */
248 sout_access_out_t *p_access;
250 vlc_mutex_t lock_rtsp;
252 sout_access_out_t **rtsp_access;
255 sout_input_t *p_input;
257 /* RTSP url control */
258 httpd_url_t *p_rtsp_url;
261 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
263 static void SDPHandleUrl( sout_stream_t *, char * );
265 static int SapSetup( sout_stream_t *p_stream );
266 static int FileSetup( sout_stream_t *p_stream );
267 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
268 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
270 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
271 httpd_message_t *, httpd_message_t * );
272 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
273 httpd_message_t *, httpd_message_t * );
276 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
277 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
278 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
280 /*****************************************************************************
282 *****************************************************************************/
283 static int Open( vlc_object_t *p_this )
285 sout_stream_t *p_stream = (sout_stream_t*)p_this;
286 sout_instance_t *p_sout = p_stream->p_sout;
287 sout_stream_sys_t *p_sys = NULL;
288 sout_cfg_t *p_cfg = NULL;
290 vlc_bool_t b_rtsp = VLC_FALSE;
292 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
294 p_sys = malloc( sizeof( sout_stream_sys_t ) );
296 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
297 if( *p_sys->psz_destination == '\0' )
299 free( p_sys->psz_destination );
300 p_sys->psz_destination = NULL;
303 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
304 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
305 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
306 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
308 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
309 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
310 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
312 p_sys->psz_sdp_file = NULL;
314 if( p_sys->i_port_audio == p_sys->i_port_video )
316 msg_Err( p_stream, "audio and video port cannot be the same" );
317 p_sys->i_port_audio = 0;
318 p_sys->i_port_video = 0;
321 if( !p_sys->psz_session_name )
323 if( p_sys->psz_destination )
324 p_sys->psz_session_name = strdup( p_sys->psz_destination );
326 p_sys->psz_session_name = strdup( "NONE" );
329 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
331 if( !strcmp( p_cfg->psz_name, "sdp" ) )
333 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
343 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
344 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
346 free( val2.psz_string );
349 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
353 msg_Err( p_stream, "missing destination and not in rtsp mode" );
357 p_sys->psz_destination = NULL;
359 else if( p_sys->i_port <= 0 )
361 msg_Err( p_stream, "invalid port" );
366 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
369 /* Normally, we should let the default hop limit up to the core,
370 * but we have to know it to build our SDP properly, which is why
371 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
373 val.i_int = config_GetInt( p_stream, "ttl" );
375 if( val.i_int > 255 ) val.i_int = 255;
376 /* must not exceed 999 once formatted */
380 msg_Err( p_stream, "illegal TTL %d", val.i_int );
384 p_sys->i_ttl = val.i_int;
387 var_Get( p_stream, SOUT_CFG_PREFIX "rfc3016", &val );
388 p_sys->b_rfc3016 = val.b_bool;
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 char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv;
429 msg_Err( p_stream, "muxing is not supported in RTSP mode" );
433 else if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
435 msg_Err( p_stream, "rtp needs a destination when muxing" );
440 /* Check muxer type */
441 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
443 psz_rtpmap = "MP2P/90000";
445 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
447 psz_rtpmap = "MP2T/90000";
448 p_sys->i_payload_type = 33;
452 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
457 /* create the access out */
458 if( p_sys->i_ttl > 0 )
460 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
464 sprintf( access, "udp{raw}" );
467 /* IPv6 needs brackets if not already present */
468 snprintf( url, sizeof( url ),
469 ( ( p_sys->psz_destination[0] != '[' )
470 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
471 ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
473 url[sizeof( url ) - 1] = '\0';
474 /* FIXME: we should check that url is a numerical address, otherwise
475 * the SDP will be quite broken (regardless of the IP protocol version)
476 * Also it might be IPv6 with no ':' if it is a DNS name.
478 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
480 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
482 msg_Err( p_stream, "cannot create the access out for %s://%s",
487 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
488 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
490 /* better than nothing */
493 p_sys->i_mtu -= MTU_REDUCE;
495 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
496 p_grab = p_sys->p_grab =
497 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
498 p_grab->p_module = NULL;
499 p_grab->p_sout = p_sout;
500 p_grab->psz_access = strdup( "grab" );
501 p_grab->p_cfg = NULL;
502 p_grab->psz_name = strdup( "" );
503 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
504 p_grab->pf_seek = NULL;
505 p_grab->pf_write = AccessOutGrabberWrite;
508 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
510 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
511 sout_AccessOutDelete( p_sys->p_grab );
512 sout_AccessOutDelete( p_sys->p_access );
517 /* create the SDP for a muxed stream (only once) */
518 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
519 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
520 o= - should be local username (no spaces allowed)
521 o= time should be hashed with some other value to garantue uniqueness
522 o= we need IP6 support?
523 o= don't use the localhost address. use fully qualified domain name or IP4 address
524 p= international phone number (pass via vars?)
526 a= recvonly (missing)
527 a= type:broadcast (missing)
528 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
529 a= x-plgroup: (missing)
530 RTP packets need to get the correct src IP address */
531 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
533 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
534 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
541 asprintf( &p_sys->psz_sdp,
543 /* FIXME: source address not known :( */
544 "o=- "I64Fd" %d IN IP%c %s\r\n"
549 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
550 "a=tool:"PACKAGE_STRING"\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 )
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 )
641 RtspClientDel( p_stream, p_sys->rtsp[0] );
644 vlc_mutex_destroy( &p_sys->lock_sdp );
646 if( p_sys->p_httpd_file )
648 httpd_FileDelete( p_sys->p_httpd_file );
650 if( p_sys->p_httpd_host )
652 httpd_HostDelete( p_sys->p_httpd_host );
654 if( p_sys->p_rtsp_url )
656 httpd_UrlDelete( p_sys->p_rtsp_url );
658 if( p_sys->p_rtsp_host )
660 httpd_HostDelete( p_sys->p_rtsp_host );
662 if( p_sys->psz_session_name )
664 free( p_sys->psz_session_name );
665 p_sys->psz_session_name = NULL;
667 if( p_sys->psz_session_description )
669 free( p_sys->psz_session_description );
670 p_sys->psz_session_description = NULL;
672 if( p_sys->psz_session_url )
674 free( p_sys->psz_session_url );
675 p_sys->psz_session_url = NULL;
677 if( p_sys->psz_session_email )
679 free( p_sys->psz_session_email );
680 p_sys->psz_session_email = NULL;
684 free( p_sys->psz_sdp );
685 p_sys->psz_sdp = NULL;
690 /*****************************************************************************
692 *****************************************************************************/
693 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
695 sout_stream_sys_t *p_sys = p_stream->p_sys;
698 vlc_UrlParse( &url, psz_url, 0 );
699 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
701 if( p_sys->p_httpd_file )
703 msg_Err( p_stream, "You can use sdp=http:// only once" );
707 if( HttpSetup( p_stream, &url ) )
709 msg_Err( p_stream, "cannot export sdp as http" );
712 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
714 if( p_sys->p_rtsp_url )
716 msg_Err( p_stream, "You can use sdp=rtsp:// only once" );
720 /* FIXME test if destination is multicast or no destination at all FIXME */
721 if( RtspSetup( p_stream, &url ) )
723 msg_Err( p_stream, "cannot export sdp as rtsp" );
726 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
727 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
729 p_sys->b_export_sap = VLC_TRUE;
730 SapSetup( p_stream );
732 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
734 if( p_sys->b_export_sdp_file )
736 msg_Err( p_stream, "You can use sdp=file:// only once" );
739 p_sys->b_export_sdp_file = VLC_TRUE;
740 psz_url = &psz_url[5];
741 if( psz_url[0] == '/' && psz_url[1] == '/' )
743 p_sys->psz_sdp_file = strdup( psz_url );
747 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
750 vlc_UrlClean( &url );
753 /*****************************************************************************
755 *****************************************************************************/
756 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
757 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
758 o= - should be local username (no spaces allowed)
759 o= time should be hashed with some other value to garantue uniqueness
760 o= we need IP6 support?
761 o= don't use the localhost address. use fully qualified domain name or IP4 address
762 p= international phone number (pass via vars?)
764 a= recvonly (missing)
765 a= type:broadcast (missing)
766 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
767 a= x-plgroup: (missing)
768 RTP packets need to get the correct src IP address */
769 static char *SDPGenerate( const sout_stream_t *p_stream,
770 const char *psz_destination, vlc_bool_t b_rtsp )
772 sout_stream_sys_t *p_sys = p_stream->p_sys;
773 sout_instance_t *p_sout = p_stream->p_sout;
775 char *psz_sdp, *p, ipv;
778 /* FIXME: breaks IP version check on unknown destination */
779 if( psz_destination == NULL )
780 psz_destination = "0.0.0.0";
782 i_size = sizeof( "v=0\r\n" ) +
783 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
784 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
785 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
786 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
787 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
788 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
789 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
790 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
791 strlen( psz_destination ) ;
792 for( i = 0; i < p_sys->i_es; i++ )
794 sout_stream_id_t *id = p_sys->es[i];
796 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
799 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
803 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
807 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
812 i_size += strlen( "m=video %d RTP/AVP %d\r\n" ) +10 +10;
815 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
817 p = psz_sdp = malloc( i_size );
818 p += sprintf( p, "v=0\r\n" );
819 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
820 p_sys->i_sdp_id, p_sys->i_sdp_version,
821 ipv, ipv == '6' ? "::" : "127.0.0.1" );
822 if( *p_sys->psz_session_name )
823 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
824 if( *p_sys->psz_session_description )
825 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
826 if( *p_sys->psz_session_url )
827 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
828 if( *p_sys->psz_session_email )
829 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
831 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
832 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
834 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
836 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
838 /* Add the ttl if it is a multicast address */
839 /* FIXME: 1 is not a correct default value in the case of IPv6 */
840 p += sprintf( p, "/%d\r\n", p_sys->i_ttl ?: 1 );
844 p += sprintf( p, "\r\n" );
847 for( i = 0; i < p_sys->i_es; i++ )
849 sout_stream_id_t *id = p_sys->es[i];
851 if( id->i_cat == AUDIO_ES )
853 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
854 id->i_port, id->i_payload_type );
856 else if( id->i_cat == VIDEO_ES )
858 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
859 id->i_port, id->i_payload_type );
867 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
872 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
877 p += sprintf( p, "a=control:trackID=%d\r\n", i );
882 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
883 p_sys->i_port, p_sys->i_payload_type );
889 /*****************************************************************************
891 *****************************************************************************/
892 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
893 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
894 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
895 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
896 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
897 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
898 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
899 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
900 static int rtp_packetize_h263 ( 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,ttl=%d}",
978 access[sizeof( access ) - 1] = '\0';
981 strcpy( access, "udp{raw}" );
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;
1067 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1069 char hexa[2*p_fmt->i_extra +1];
1071 id->i_payload_type = p_sys->i_payload_type++;
1072 id->i_clock_rate = 90000;
1073 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1074 id->pf_packetize = rtp_packetize_split;
1075 if( p_fmt->i_extra > 0 )
1077 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1078 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1079 sprintf( id->psz_fmtp,
1080 "profile-level-id=3; config=%s;", hexa );
1084 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1086 id->i_payload_type = p_sys->i_payload_type++;
1087 id->i_clock_rate = p_fmt->audio.i_rate;
1089 if(!p_sys->b_rfc3016)
1091 char hexa[2*p_fmt->i_extra +1];
1093 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1094 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1095 id->pf_packetize = rtp_packetize_mp4a;
1096 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1097 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1098 sprintf( id->psz_fmtp,
1099 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1100 "config=%s; SizeLength=13;IndexLength=3; "
1101 "IndexDeltaLength=3; Profile=1;", hexa );
1107 unsigned char config[6];
1108 unsigned int aacsrates[15] = {
1109 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1110 16000, 12000, 11025, 8000, 7350, 0, 0 };
1112 for( i = 0; i < 15; i++ )
1113 if( p_fmt->audio.i_rate == aacsrates[i] )
1119 config[3]=p_fmt->audio.i_channels<<4;
1123 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1124 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1125 id->pf_packetize = rtp_packetize_mp4a_latm;
1126 sprintf_hexa( hexa, config, 6 );
1127 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1128 "object=2; cpresent=0; config=%s", hexa );
1132 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1133 id->i_payload_type = p_sys->i_payload_type++;
1134 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1135 "AMR/8000/2" : "AMR/8000" );
1136 id->psz_fmtp = strdup( "octet-align=1" );
1137 id->i_clock_rate = p_fmt->audio.i_rate;
1138 id->pf_packetize = rtp_packetize_amr;
1140 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1141 id->i_payload_type = p_sys->i_payload_type++;
1142 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1143 "AMR-WB/16000/2" : "AMR-WB/16000" );
1144 id->psz_fmtp = strdup( "octet-align=1" );
1145 id->i_clock_rate = p_fmt->audio.i_rate;
1146 id->pf_packetize = rtp_packetize_amr;
1150 msg_Err( p_stream, "cannot add this stream (unsupported "
1151 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1154 sout_AccessOutDelete( p_access );
1159 id->i_cat = p_fmt->i_cat;
1161 id->ssrc[0] = rand()&0xff;
1162 id->ssrc[1] = rand()&0xff;
1163 id->ssrc[2] = rand()&0xff;
1164 id->ssrc[3] = rand()&0xff;
1165 id->i_sequence = rand()&0xffff;
1166 id->i_timestamp_start = rand()&0xffffffff;
1168 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1169 if( id->i_mtu <= 16 + MTU_REDUCE )
1171 /* better than nothing */
1174 id->i_mtu -= MTU_REDUCE;
1175 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1177 if( p_sys->p_rtsp_url )
1179 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1181 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1182 msg_Dbg( p_stream, "rtsp: adding %s\n", psz_urlc );
1183 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1185 if( id->p_rtsp_url )
1187 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1188 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1189 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1194 /* Update p_sys context */
1195 vlc_mutex_lock( &p_sys->lock_es );
1196 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1197 vlc_mutex_unlock( &p_sys->lock_es );
1199 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1201 vlc_mutex_lock( &p_sys->lock_sdp );
1202 free( p_sys->psz_sdp );
1203 p_sys->psz_sdp = psz_sdp;
1204 vlc_mutex_unlock( &p_sys->lock_sdp );
1206 p_sys->i_sdp_version++;
1208 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
1210 /* Update SDP (sap/file) */
1211 if( p_sys->b_export_sap ) SapSetup( p_stream );
1212 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1217 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1219 sout_stream_sys_t *p_sys = p_stream->p_sys;
1221 vlc_mutex_lock( &p_sys->lock_es );
1222 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1223 vlc_mutex_unlock( &p_sys->lock_es );
1226 if( id->i_port > 0 )
1228 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1229 p_sys->i_port_audio = id->i_port;
1230 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1231 p_sys->i_port_video = id->i_port;
1236 if( id->psz_rtpmap )
1238 free( id->psz_rtpmap );
1242 free( id->psz_fmtp );
1244 if( id->psz_destination )
1245 free( id->psz_destination );
1246 sout_AccessOutDelete( id->p_access );
1248 else if( id->p_input )
1250 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1252 if( id->p_rtsp_url )
1254 httpd_UrlDelete( id->p_rtsp_url );
1256 vlc_mutex_destroy( &id->lock_rtsp );
1257 if( id->rtsp_access ) free( id->rtsp_access );
1259 /* Update SDP (sap/file) */
1260 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1261 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1267 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1272 if( p_stream->p_sys->p_mux )
1274 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1280 p_next = p_buffer->p_next;
1281 if( id->pf_packetize( p_stream, id, p_buffer ) )
1285 block_Release( p_buffer );
1293 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1296 sout_stream_sys_t *p_sys = p_stream->p_sys;
1298 int64_t i_dts = p_buffer->i_dts;
1299 uint32_t i_timestamp = i_dts * 9 / 100;
1301 uint8_t *p_data = p_buffer->p_buffer;
1302 unsigned int i_data = p_buffer->i_buffer;
1303 unsigned int i_max = p_sys->i_mtu - 12;
1305 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1309 unsigned int i_size;
1311 /* output complete packet */
1312 if( p_sys->packet &&
1313 p_sys->packet->i_buffer + i_data > i_max )
1315 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1316 p_sys->packet = NULL;
1319 if( p_sys->packet == NULL )
1321 /* allocate a new packet */
1322 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1323 p_sys->packet->p_buffer[ 0] = 0x80;
1324 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1325 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1326 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1327 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1328 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1329 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1330 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1331 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1332 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1333 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1334 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1335 p_sys->packet->i_buffer = 12;
1337 p_sys->packet->i_dts = i_dts;
1338 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1339 i_dts += p_sys->packet->i_length;
1341 p_sys->i_sequence++;
1344 i_size = __MIN( i_data,
1345 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1347 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1350 p_sys->packet->i_buffer += i_size;
1358 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1361 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1363 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1369 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1371 p_next = p_buffer->p_next;
1372 block_Release( p_buffer );
1379 /****************************************************************************
1381 ****************************************************************************/
1382 static int SapSetup( sout_stream_t *p_stream )
1384 sout_stream_sys_t *p_sys = p_stream->p_sys;
1385 sout_instance_t *p_sout = p_stream->p_sout;
1386 announce_method_t *p_method = sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
1388 /* Remove the previous session */
1389 if( p_sys->p_session != NULL)
1391 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1392 sout_AnnounceSessionDestroy( p_sys->p_session );
1393 p_sys->p_session = NULL;
1396 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1398 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1399 p_sys->psz_destination,
1407 /****************************************************************************
1409 ****************************************************************************/
1410 static int FileSetup( sout_stream_t *p_stream )
1412 sout_stream_sys_t *p_sys = p_stream->p_sys;
1415 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1417 msg_Err( p_stream, "cannot open file '%s' (%s)",
1418 p_sys->psz_sdp_file, strerror(errno) );
1419 return VLC_EGENERIC;
1422 fprintf( f, "%s", p_sys->psz_sdp );
1428 /****************************************************************************
1430 ****************************************************************************/
1431 static int HttpCallback( httpd_file_sys_t *p_args,
1432 httpd_file_t *, uint8_t *p_request,
1433 uint8_t **pp_data, int *pi_data );
1435 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1437 sout_stream_sys_t *p_sys = p_stream->p_sys;
1439 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1440 if( p_sys->p_httpd_host )
1442 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1443 url->psz_path ? url->psz_path : "/",
1446 HttpCallback, (void*)p_sys );
1448 if( p_sys->p_httpd_file == NULL )
1450 return VLC_EGENERIC;
1455 static int HttpCallback( httpd_file_sys_t *p_args,
1456 httpd_file_t *f, uint8_t *p_request,
1457 uint8_t **pp_data, int *pi_data )
1459 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1461 vlc_mutex_lock( &p_sys->lock_sdp );
1462 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1464 *pi_data = strlen( p_sys->psz_sdp );
1465 *pp_data = malloc( *pi_data );
1466 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1473 vlc_mutex_unlock( &p_sys->lock_sdp );
1478 /****************************************************************************
1480 ****************************************************************************/
1481 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1483 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1485 rtsp->psz_session = psz_session;
1487 rtsp->b_playing = VLC_FALSE;
1491 rtsp->access = NULL;
1493 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1497 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1500 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1502 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1504 return p_stream->p_sys->rtsp[i];
1510 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1513 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1515 for( i = 0; i < rtsp->i_access; i++ )
1517 sout_AccessOutDelete( rtsp->access[i] );
1519 if( rtsp->id ) free( rtsp->id );
1520 if( rtsp->access ) free( rtsp->access );
1522 free( rtsp->psz_session );
1526 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1528 sout_stream_sys_t *p_sys = p_stream->p_sys;
1530 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1532 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1533 if( p_sys->p_rtsp_host == NULL )
1535 return VLC_EGENERIC;
1538 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1539 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1540 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1541 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1543 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1544 if( p_sys->p_rtsp_url == 0 )
1546 return VLC_EGENERIC;
1548 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1549 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1550 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1551 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1556 static int RtspCallback( httpd_callback_sys_t *p_args,
1558 httpd_message_t *answer, httpd_message_t *query )
1560 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1561 sout_stream_sys_t *p_sys = p_stream->p_sys;
1562 char *psz_destination = p_sys->psz_destination;
1563 char *psz_session = NULL;
1565 if( answer == NULL || query == NULL )
1569 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1571 answer->i_proto = HTTPD_PROTO_RTSP;
1572 answer->i_version= query->i_version;
1573 answer->i_type = HTTPD_MSG_ANSWER;
1575 switch( query->i_type )
1577 case HTTPD_MSG_DESCRIBE:
1579 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1581 answer->i_status = 200;
1582 answer->psz_status = strdup( "OK" );
1583 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1585 answer->p_body = (uint8_t *)psz_sdp;
1586 answer->i_body = strlen( psz_sdp );
1590 case HTTPD_MSG_PLAY:
1592 rtsp_client_t *rtsp;
1593 /* for now only multicast so easy */
1594 answer->i_status = 200;
1595 answer->psz_status = strdup( "OK" );
1597 answer->p_body = NULL;
1599 psz_session = httpd_MsgGet( query, "Session" );
1600 rtsp = RtspClientGet( p_stream, psz_session );
1601 if( rtsp && !rtsp->b_playing )
1605 rtsp->b_playing = VLC_TRUE;
1607 vlc_mutex_lock( &p_sys->lock_es );
1608 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1610 sout_stream_id_t *id = rtsp->id[i_id];
1613 for( i = 0; i < p_sys->i_es; i++ )
1615 if( id == p_sys->es[i] )
1618 if( i >= p_sys->i_es ) continue;
1620 vlc_mutex_lock( &id->lock_rtsp );
1621 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1622 vlc_mutex_unlock( &id->lock_rtsp );
1624 vlc_mutex_unlock( &p_sys->lock_es );
1628 case HTTPD_MSG_PAUSE:
1630 return VLC_EGENERIC;
1631 case HTTPD_MSG_TEARDOWN:
1633 rtsp_client_t *rtsp;
1635 /* for now only multicast so easy again */
1636 answer->i_status = 200;
1637 answer->psz_status = strdup( "OK" );
1639 answer->p_body = NULL;
1641 psz_session = httpd_MsgGet( query, "Session" );
1642 rtsp = RtspClientGet( p_stream, psz_session );
1647 vlc_mutex_lock( &p_sys->lock_es );
1648 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1650 sout_stream_id_t *id = rtsp->id[i_id];
1653 for( i = 0; i < p_sys->i_es; i++ )
1655 if( id == p_sys->es[i] )
1658 if( i >= p_sys->i_es ) continue;
1660 vlc_mutex_lock( &id->lock_rtsp );
1661 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1662 vlc_mutex_unlock( &id->lock_rtsp );
1664 vlc_mutex_unlock( &p_sys->lock_es );
1666 RtspClientDel( p_stream, rtsp );
1672 return VLC_EGENERIC;
1674 httpd_MsgAdd( answer, "Server", PACKAGE_STRING );
1675 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1676 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1677 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1681 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1686 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1688 httpd_message_t *answer, httpd_message_t *query )
1690 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1691 sout_stream_t *p_stream = id->p_stream;
1692 sout_instance_t *p_sout = p_stream->p_sout;
1693 sout_stream_sys_t *p_sys = p_stream->p_sys;
1694 char *psz_session = NULL;
1696 if( answer == NULL || query == NULL )
1700 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1702 answer->i_proto = HTTPD_PROTO_RTSP;
1703 answer->i_version= query->i_version;
1704 answer->i_type = HTTPD_MSG_ANSWER;
1706 switch( query->i_type )
1708 case HTTPD_MSG_SETUP:
1710 char *psz_transport = httpd_MsgGet( query, "Transport" );
1712 //fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1714 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1716 //fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1717 answer->i_status = 200;
1718 answer->psz_status = strdup( "OK" );
1720 answer->p_body = NULL;
1721 psz_session = httpd_MsgGet( query, "Session" );
1722 if( *psz_session == 0 )
1724 psz_session = malloc( 100 );
1725 sprintf( psz_session, "%d", rand() );
1727 httpd_MsgAdd( answer, "Transport",
1728 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1729 id->psz_destination, id->i_port,id->i_port+1,
1732 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1734 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1735 char ip[NI_MAXNUMERICHOST], psz_access[17], psz_url[NI_MAXNUMERICHOST + 8];
1737 sout_access_out_t *p_access;
1739 rtsp_client_t *rtsp = NULL;
1741 if( httpd_ClientIP( cl, ip ) == NULL )
1743 answer->i_status = 500;
1744 answer->psz_status = strdup( "Internal server error" );
1746 answer->p_body = NULL;
1750 //fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n", ip, i_port );
1752 psz_session = httpd_MsgGet( query, "Session" );
1753 if( *psz_session == 0 )
1755 psz_session = malloc( 100 );
1756 sprintf( psz_session, "%d", rand() );
1758 rtsp = RtspClientNew( p_stream, psz_session );
1762 rtsp = RtspClientGet( p_stream, psz_session );
1765 answer->i_status = 454;
1766 answer->psz_status = strdup( "Unknown session id" );
1768 answer->p_body = NULL;
1773 /* first try to create the access out */
1775 snprintf( psz_access, sizeof( psz_access ),
1776 "udp{raw,ttl=%d}", p_sys->i_ttl );
1778 strncpy( psz_access, "udp{raw}", sizeof( psz_access ) );
1779 psz_access[sizeof( psz_access ) - 1] = '\0';
1781 snprintf( psz_url, sizeof( psz_url ),
1782 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
1785 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1787 msg_Err( p_stream, "cannot create the access out for %s://%s",
1788 psz_access, psz_url );
1789 answer->i_status = 500;
1790 answer->psz_status = strdup( "Internal server error" );
1792 answer->p_body = NULL;
1796 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1797 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1799 answer->i_status = 200;
1800 answer->psz_status = strdup( "OK" );
1802 answer->p_body = NULL;
1804 httpd_MsgAdd( answer, "Transport",
1805 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1807 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1809 answer->i_status = 461;
1810 answer->psz_status = strdup( "Unsupported Transport" );
1812 answer->p_body = NULL;
1818 return VLC_EGENERIC;
1820 httpd_MsgAdd( answer, "Server", "VLC Server" );
1821 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1822 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1823 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1827 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1832 /****************************************************************************
1834 ****************************************************************************/
1835 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1836 int b_marker, int64_t i_pts )
1838 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1840 out->p_buffer[0] = 0x80;
1841 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1842 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1843 out->p_buffer[3] = ( id->i_sequence )&0xff;
1844 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1845 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1846 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1847 out->p_buffer[7] = ( i_timestamp )&0xff;
1849 out->p_buffer[ 8] = id->ssrc[0];
1850 out->p_buffer[ 9] = id->ssrc[1];
1851 out->p_buffer[10] = id->ssrc[2];
1852 out->p_buffer[11] = id->ssrc[3];
1858 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1861 vlc_mutex_lock( &id->lock_rtsp );
1862 for( i = 0; i < id->i_rtsp_access; i++ )
1864 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1866 vlc_mutex_unlock( &id->lock_rtsp );
1870 sout_AccessOutWrite( id->p_access, out );
1874 block_Release( out );
1878 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1881 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1882 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1884 uint8_t *p_data = in->p_buffer;
1885 int i_data = in->i_buffer;
1888 for( i = 0; i < i_count; i++ )
1890 int i_payload = __MIN( i_max, i_data );
1891 block_t *out = block_New( p_stream, 16 + i_payload );
1893 /* rtp common header */
1894 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1896 out->p_buffer[12] = 0;
1897 out->p_buffer[13] = 0;
1898 /* fragment offset in the current frame */
1899 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1900 out->p_buffer[15] = ( (i*i_max) )&0xff;
1901 memcpy( &out->p_buffer[16], p_data, i_payload );
1903 out->i_buffer = 16 + i_payload;
1904 out->i_dts = in->i_dts + i * in->i_length / i_count;
1905 out->i_length = in->i_length / i_count;
1907 rtp_packetize_send( id, out );
1909 p_data += i_payload;
1910 i_data -= i_payload;
1917 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1920 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1921 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1923 uint8_t *p_data = in->p_buffer;
1924 int i_data = in->i_buffer;
1926 int b_sequence_start = 0;
1927 int i_temporal_ref = 0;
1928 int i_picture_coding_type = 0;
1929 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1930 int b_start_slice = 0;
1932 /* preparse this packet to get some info */
1933 if( in->i_buffer > 4 )
1935 uint8_t *p = p_data;
1936 int i_rest = in->i_buffer;
1940 while( i_rest > 4 &&
1941 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1955 /* sequence start code */
1956 b_sequence_start = 1;
1958 else if( *p == 0x00 && i_rest >= 4 )
1961 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1962 i_picture_coding_type = (p[2] >> 3)&0x07;
1964 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1965 i_picture_coding_type == 3 ) )
1967 i_ffv = (p[3] >> 2)&0x01;
1968 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1969 if( i_rest > 4 && i_picture_coding_type == 3 )
1971 i_fbv = (p[4]>>6)&0x01;
1972 i_bfc = (p[4]>>3)&0x07;
1976 else if( *p <= 0xaf )
1983 for( i = 0; i < i_count; i++ )
1985 int i_payload = __MIN( i_max, i_data );
1986 block_t *out = block_New( p_stream,
1988 uint32_t h = ( i_temporal_ref << 16 )|
1989 ( b_sequence_start << 13 )|
1990 ( b_start_slice << 12 )|
1991 ( i == i_count - 1 ? 1 << 11 : 0 )|
1992 ( i_picture_coding_type << 8 )|
1993 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1995 /* rtp common header */
1996 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1997 in->i_pts > 0 ? in->i_pts : in->i_dts );
1999 /* 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 */
2000 out->p_buffer[12] = ( h >> 24 )&0xff;
2001 out->p_buffer[13] = ( h >> 16 )&0xff;
2002 out->p_buffer[14] = ( h >> 8 )&0xff;
2003 out->p_buffer[15] = ( h )&0xff;
2005 memcpy( &out->p_buffer[16], p_data, i_payload );
2007 out->i_buffer = 16 + i_payload;
2008 out->i_dts = in->i_dts + i * in->i_length / i_count;
2009 out->i_length = in->i_length / i_count;
2011 rtp_packetize_send( id, out );
2013 p_data += i_payload;
2014 i_data -= i_payload;
2020 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
2023 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
2024 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2026 uint8_t *p_data = in->p_buffer;
2027 int i_data = in->i_buffer;
2030 for( i = 0; i < i_count; i++ )
2032 int i_payload = __MIN( i_max, i_data );
2033 block_t *out = block_New( p_stream, 14 + i_payload );
2035 /* rtp common header */
2036 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
2038 out->p_buffer[12] = 1;
2040 out->p_buffer[13] = 0x00;
2042 memcpy( &out->p_buffer[14], p_data, i_payload );
2044 out->i_buffer = 14 + i_payload;
2045 out->i_dts = in->i_dts + i * in->i_length / i_count;
2046 out->i_length = in->i_length / i_count;
2048 rtp_packetize_send( id, out );
2050 p_data += i_payload;
2051 i_data -= i_payload;
2057 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
2060 int i_max = id->i_mtu - 12; /* payload max in one packet */
2061 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2063 uint8_t *p_data = in->p_buffer;
2064 int i_data = in->i_buffer;
2067 for( i = 0; i < i_count; i++ )
2069 int i_payload = __MIN( i_max, i_data );
2070 block_t *out = block_New( p_stream, 12 + i_payload );
2072 /* rtp common header */
2073 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2074 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2075 memcpy( &out->p_buffer[12], p_data, i_payload );
2077 out->i_buffer = 12 + i_payload;
2078 out->i_dts = in->i_dts + i * in->i_length / i_count;
2079 out->i_length = in->i_length / i_count;
2081 rtp_packetize_send( id, out );
2083 p_data += i_payload;
2084 i_data -= i_payload;
2090 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
2093 int i_max = id->i_mtu - 14; /* payload max in one packet */
2094 int latmhdrsize = in->i_buffer/0xff + 1;
2095 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2097 uint8_t *p_data = in->p_buffer, *pheader;
2098 int i_data = in->i_buffer;
2101 for( i = 0; i < i_count; i++ )
2103 int i_payload = __MIN( i_max, i_data );
2108 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
2110 /* rtp common header */
2111 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2112 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2116 int tmp = in->i_buffer;
2118 pheader=out->p_buffer+12;
2128 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
2130 out->i_buffer = 12 + latmhdrsize + i_payload;
2131 out->i_dts = in->i_dts + i * in->i_length / i_count;
2132 out->i_length = in->i_length / i_count;
2134 rtp_packetize_send( id, out );
2136 p_data += i_payload;
2137 i_data -= i_payload;
2143 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
2146 int i_max = id->i_mtu - 12; /* payload max in one packet */
2147 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2149 uint8_t *p_data = in->p_buffer;
2150 int i_data = in->i_buffer;
2155 int i_payload = (__MIN( i_max, i_data )/4)*4;
2156 block_t *out = block_New( p_stream, 12 + i_payload );
2158 /* rtp common header */
2159 rtp_packetize_common( id, out, 0,
2160 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2161 memcpy( &out->p_buffer[12], p_data, i_payload );
2163 out->i_buffer = 12 + i_payload;
2164 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2165 out->i_length = in->i_length / i_count;
2167 rtp_packetize_send( id, out );
2169 p_data += i_payload;
2170 i_data -= i_payload;
2177 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2180 int i_max = id->i_mtu - 12; /* payload max in one packet */
2181 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2183 uint8_t *p_data = in->p_buffer;
2184 int i_data = in->i_buffer;
2189 int i_payload = (__MIN( i_max, i_data )/2)*2;
2190 block_t *out = block_New( p_stream, 12 + i_payload );
2192 /* rtp common header */
2193 rtp_packetize_common( id, out, 0,
2194 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2195 memcpy( &out->p_buffer[12], p_data, i_payload );
2197 out->i_buffer = 12 + i_payload;
2198 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2199 out->i_length = in->i_length / i_count;
2201 rtp_packetize_send( id, out );
2203 p_data += i_payload;
2204 i_data -= i_payload;
2210 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2213 int i_max = id->i_mtu - 16; /* payload max in one packet */
2214 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2216 uint8_t *p_data = in->p_buffer;
2217 int i_data = in->i_buffer;
2220 for( i = 0; i < i_count; i++ )
2222 int i_payload = __MIN( i_max, i_data );
2223 block_t *out = block_New( p_stream, 16 + i_payload );
2225 /* rtp common header */
2226 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2227 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2229 /* AU headers length (bits) */
2230 out->p_buffer[12] = 0;
2231 out->p_buffer[13] = 2*8;
2232 /* for each AU length 13 bits + idx 3bits, */
2233 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2234 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2236 memcpy( &out->p_buffer[16], p_data, i_payload );
2238 out->i_buffer = 16 + i_payload;
2239 out->i_dts = in->i_dts + i * in->i_length / i_count;
2240 out->i_length = in->i_length / i_count;
2242 rtp_packetize_send( id, out );
2244 p_data += i_payload;
2245 i_data -= i_payload;
2253 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2254 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2255 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2258 uint8_t *p_data = in->p_buffer;
2259 int i_data = in->i_buffer;
2261 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2264 int b_v_bit = 0; // no pesky error resilience
2265 int i_plen = 0; // normally plen=0 for PSC packet
2266 int i_pebit = 0; // because plen=0
2271 return VLC_EGENERIC;
2273 if( p_data[0] || p_data[1] )
2275 return VLC_EGENERIC;
2277 /* remove 2 leading 0 bytes */
2280 i_count = ( i_data + i_max - 1 ) / i_max;
2282 for( i = 0; i < i_count; i++ )
2284 int i_payload = __MIN( i_max, i_data );
2285 block_t *out = block_New( p_stream,
2286 RTP_H263_PAYLOAD_START + i_payload );
2287 b_p_bit = (i == 0) ? 1 : 0;
2288 h = ( b_p_bit << 10 )|
2293 /* rtp common header */
2294 //b_m_bit = 1; // always contains end of frame
2295 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2296 in->i_pts > 0 ? in->i_pts : in->i_dts );
2299 out->p_buffer[12] = ( h >> 8 )&0xff;
2300 out->p_buffer[13] = ( h )&0xff;
2301 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2303 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2304 out->i_dts = in->i_dts + i * in->i_length / i_count;
2305 out->i_length = in->i_length / i_count;
2307 rtp_packetize_send( id, out );
2309 p_data += i_payload;
2310 i_data -= i_payload;
2316 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2319 int i_max = id->i_mtu - 14; /* payload max in one packet */
2320 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2322 uint8_t *p_data = in->p_buffer;
2323 int i_data = in->i_buffer;
2326 /* Only supports octet-aligned mode */
2327 for( i = 0; i < i_count; i++ )
2329 int i_payload = __MIN( i_max, i_data );
2330 block_t *out = block_New( p_stream, 14 + i_payload );
2332 /* rtp common header */
2333 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2334 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2335 /* Payload header */
2336 out->p_buffer[12] = 0xF0; /* CMR */
2337 out->p_buffer[13] = 0x00; /* ToC */ /* FIXME: frame type */
2339 /* FIXME: are we fed multiple frames ? */
2340 memcpy( &out->p_buffer[14], p_data, i_payload );
2342 out->i_buffer = 14 + i_payload;
2343 out->i_dts = in->i_dts + i * in->i_length / i_count;
2344 out->i_length = in->i_length / i_count;
2346 rtp_packetize_send( id, out );
2348 p_data += i_payload;
2349 i_data -= i_payload;