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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
35 #include "vlc_httpd.h"
38 /*****************************************************************************
40 *****************************************************************************/
41 #define DST_TEXT N_("Destination")
42 #define DST_LONGTEXT N_( \
43 "Allows you to specify the output URL used for the streaming output." )
44 #define SDP_TEXT N_("SDP")
45 #define SDP_LONGTEXT N_( \
46 "Allows you to specify the SDP used for the streaming output. " \
47 "You must use an url: http://location to access the SDP via HTTP, " \
48 "rtsp://location for RTSP access, and sap:// for the SDP to be " \
49 "announced via SAP." )
50 #define MUX_TEXT N_("Muxer")
51 #define MUX_LONGTEXT N_( \
52 "Allows you to specify the muxer used for the streaming output." )
54 #define NAME_TEXT N_("Session name")
55 #define NAME_LONGTEXT N_( \
56 "Allows you to specify the session name used for the streaming output." )
57 #define DESC_TEXT N_("Session description")
58 #define DESC_LONGTEXT N_( \
59 "Allows you to give a broader description of the stream." )
60 #define URL_TEXT N_("Session URL")
61 #define URL_LONGTEXT N_( \
62 "Allows you to specify a URL with additional information on the stream." )
63 #define EMAIL_TEXT N_("Session email")
64 #define EMAIL_LONGTEXT N_( \
65 "Allows you to specify contact e-mail address for this session." )
67 #define PORT_TEXT N_("Port")
68 #define PORT_LONGTEXT N_( \
69 "Allows you to specify the base port used for the RTP streaming." )
70 #define PORT_AUDIO_TEXT N_("Audio port")
71 #define PORT_AUDIO_LONGTEXT N_( \
72 "Allows you to specify the default audio port used for the RTP streaming." )
73 #define PORT_VIDEO_TEXT N_("Video port")
74 #define PORT_VIDEO_LONGTEXT N_( \
75 "Allows you to specify the default video port used for the RTP streaming." )
77 #define TTL_TEXT N_("Time To Live")
78 #define TTL_LONGTEXT N_( \
79 "Allows you to specify the time to live for the output stream." )
81 static int Open ( vlc_object_t * );
82 static void Close( vlc_object_t * );
84 #define SOUT_CFG_PREFIX "sout-rtp-"
87 set_shortname( _("RTP"));
88 set_description( _("RTP stream output") );
89 set_capability( "sout stream", 0 );
90 add_shortcut( "rtp" );
91 set_category( CAT_SOUT );
92 set_subcategory( SUBCAT_SOUT_STREAM );
94 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
95 DST_LONGTEXT, VLC_TRUE );
96 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
97 SDP_LONGTEXT, VLC_TRUE );
98 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
99 MUX_LONGTEXT, VLC_TRUE );
101 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
102 NAME_LONGTEXT, VLC_TRUE );
103 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
104 DESC_LONGTEXT, VLC_TRUE );
105 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
106 URL_LONGTEXT, VLC_TRUE );
107 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
108 EMAIL_LONGTEXT, VLC_TRUE );
110 add_integer( SOUT_CFG_PREFIX "port-audio", 1234, NULL, PORT_AUDIO_TEXT,
111 PORT_LONGTEXT, VLC_TRUE );
112 add_integer( SOUT_CFG_PREFIX "port-video", 1236, NULL, PORT_VIDEO_TEXT,
113 PORT_LONGTEXT, VLC_TRUE );
114 add_integer( SOUT_CFG_PREFIX "port", 1238, NULL, PORT_TEXT,
115 PORT_LONGTEXT, VLC_TRUE );
117 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
118 TTL_LONGTEXT, VLC_TRUE );
120 set_callbacks( Open, Close );
123 /*****************************************************************************
124 * Exported prototypes
125 *****************************************************************************/
126 static const char *ppsz_sout_options[] = {
127 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
128 "description", "url","email", NULL
131 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
132 static int Del ( sout_stream_t *, sout_stream_id_t * );
133 static int Send( sout_stream_t *, sout_stream_id_t *,
136 /* For unicast/interleaved streaming */
140 int64_t i_last; /* for timeout */
142 /* is it in "play" state */
143 vlc_bool_t b_playing;
145 /* output (id-access) */
147 sout_stream_id_t **id;
149 sout_access_out_t **access;
152 struct sout_stream_sys_t
158 vlc_mutex_t lock_sdp;
160 char *psz_session_name;
161 char *psz_session_description;
162 char *psz_session_url;
163 char *psz_session_email;
166 vlc_bool_t b_export_sdp_file;
169 vlc_bool_t b_export_sap;
170 session_descriptor_t *p_session;
172 httpd_host_t *p_httpd_host;
173 httpd_file_t *p_httpd_file;
175 httpd_host_t *p_rtsp_host;
176 httpd_url_t *p_rtsp_url;
177 char *psz_rtsp_control;
181 char *psz_destination;
187 /* when need to use a private one or when using muxer */
190 /* in case we do TS/PS over rtp */
192 sout_access_out_t *p_access;
194 sout_access_out_t *p_grab;
196 uint32_t i_timestamp_start;
203 sout_stream_id_t **es;
207 rtsp_client_t **rtsp;
210 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
213 struct sout_stream_id_t
215 sout_stream_t *p_stream;
217 uint8_t i_payload_type;
219 uint32_t i_timestamp_start;
226 char *psz_destination;
230 /* Packetizer specific fields */
231 pf_rtp_packetizer_t pf_packetize;
234 /* for sending the packets */
235 sout_access_out_t *p_access;
237 vlc_mutex_t lock_rtsp;
239 sout_access_out_t **rtsp_access;
242 sout_input_t *p_input;
244 /* RTSP url control */
245 httpd_url_t *p_rtsp_url;
248 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
250 static void SDPHandleUrl( sout_stream_t *, char * );
252 static int SapSetup( sout_stream_t *p_stream );
253 static int FileSetup( sout_stream_t *p_stream );
254 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
255 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
257 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
258 httpd_message_t *, httpd_message_t * );
259 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
260 httpd_message_t *, httpd_message_t * );
263 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
264 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
265 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
267 /*****************************************************************************
269 *****************************************************************************/
270 static int Open( vlc_object_t *p_this )
272 sout_stream_t *p_stream = (sout_stream_t*)p_this;
273 sout_instance_t *p_sout = p_stream->p_sout;
274 sout_stream_sys_t *p_sys;
277 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
279 p_sys = malloc( sizeof( sout_stream_sys_t ) );
281 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
282 if( *p_sys->psz_destination == '\0' )
284 free( p_sys->psz_destination );
285 p_sys->psz_destination = NULL;
288 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
289 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
290 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
291 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
293 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
294 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
295 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
297 p_sys->psz_sdp_file = NULL;
299 if( p_sys->i_port_audio == p_sys->i_port_video )
301 msg_Err( p_stream, "audio and video port cannot be the same" );
302 p_sys->i_port_audio = 0;
303 p_sys->i_port_video = 0;
306 if( !p_sys->psz_session_name )
308 if( p_sys->psz_destination )
309 p_sys->psz_session_name = strdup( p_sys->psz_destination );
311 p_sys->psz_session_name = strdup( "NONE" );
314 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
317 vlc_bool_t b_ok = VLC_FALSE;
319 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
321 if( !strcmp( p_cfg->psz_name, "sdp" ) )
323 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
333 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
334 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
336 free( val2.psz_string );
341 msg_Err( p_stream, "missing destination and not in rtsp mode" );
345 p_sys->psz_destination = NULL;
347 else if( p_sys->i_port <= 0 )
349 msg_Err( p_stream, "invalid port" );
354 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
355 if( ( val.i_int > 255 ) || ( val.i_int < 0 ) )
357 msg_Err( p_stream, "illegal TTL %d", val.i_int );
361 p_sys->i_ttl = val.i_int;
363 p_sys->i_payload_type = 96;
368 p_sys->psz_sdp = NULL;
370 p_sys->i_sdp_id = mdate();
371 p_sys->i_sdp_version = 1;
372 p_sys->psz_sdp = NULL;
374 p_sys->b_export_sap = VLC_FALSE;
375 p_sys->b_export_sdp_file = VLC_FALSE;
376 p_sys->p_session = NULL;
378 p_sys->p_httpd_host = NULL;
379 p_sys->p_httpd_file = NULL;
380 p_sys->p_rtsp_host = NULL;
381 p_sys->p_rtsp_url = NULL;
382 p_sys->psz_rtsp_control = NULL;
383 p_sys->psz_rtsp_path = NULL;
385 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
386 vlc_mutex_init( p_stream, &p_sys->lock_es );
388 p_stream->pf_add = Add;
389 p_stream->pf_del = Del;
390 p_stream->pf_send = Send;
392 p_stream->p_sys = p_sys;
394 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
395 if( *val.psz_string )
397 sout_access_out_t *p_grab;
399 char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5];
401 if( p_sys->psz_destination )
403 msg_Err( p_stream, "rtp needs a destination when muxing" );
408 /* Check muxer type */
409 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
411 psz_rtpmap = "MP2P/90000";
413 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
415 psz_rtpmap = "MP2T/90000";
416 p_sys->i_payload_type = 33;
420 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
425 /* create the access out */
426 if( p_sys->i_ttl > 0 )
428 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
432 sprintf( access, "udp{raw}" );
435 /* IPv6 needs brackets if not already present */
436 snprintf( url, sizeof( url ),
437 ( ( p_sys->psz_destination[0] != '[' )
438 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
439 ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
441 url[sizeof( url ) - 1] = '\0';
443 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
445 msg_Err( p_stream, "cannot create the access out for %s://%s",
450 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
451 if( p_sys->i_mtu <= 16 )
453 /* better than nothing */
457 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
458 p_grab = p_sys->p_grab =
459 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
460 p_grab->p_module = NULL;
461 p_grab->p_sout = p_sout;
462 p_grab->psz_access = strdup( "grab" );
463 p_grab->p_cfg = NULL;
464 p_grab->psz_name = strdup( "" );
465 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
466 p_grab->pf_seek = NULL;
467 p_grab->pf_write = AccessOutGrabberWrite;
470 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
472 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
473 sout_AccessOutDelete( p_sys->p_grab );
474 sout_AccessOutDelete( p_sys->p_access );
479 /* create the SDP for a muxed stream (only once) */
480 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
481 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
482 o= - should be local username (no spaces allowed)
483 o= time should be hashed with some other value to garantue uniqueness
484 o= we need IP6 support?
485 o= don't use the localhost address. use fully qualified domain name or IP4 address
486 p= international phone number (pass via vars?)
488 a= recvonly (missing)
489 a= type:broadcast (missing)
490 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
491 a= x-plgroup: (missing)
492 RTP packets need to get the correct src IP address */
494 malloc( 10 + 30 + 10 + strlen( p_sys->psz_session_name ) +
495 10 + strlen( p_sys->psz_session_description ) + 10 + strlen( p_sys->psz_session_url ) +
496 10 + strlen( p_sys->psz_session_email ) + 10 + strlen( p_sys->psz_destination ) +
497 10 + 10 + strlen( PACKAGE_STRING ) +
498 20 + 10 + 20 + 10 + strlen( psz_rtpmap ) );
500 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
502 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
503 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
510 sprintf( p_sys->psz_sdp,
512 "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
517 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
518 "a=tool:"PACKAGE_STRING"\r\n"
520 "m=video %d RTP/AVP %d\r\n"
521 "a=rtpmap:%d %s\r\n",
522 p_sys->i_sdp_id, p_sys->i_sdp_version,
523 p_sys->psz_session_name,
524 p_sys->psz_session_description,
525 p_sys->psz_session_url,
526 p_sys->psz_session_email,
527 p_sys->psz_destination, psz_ttl,
528 p_sys->i_port, p_sys->i_payload_type,
529 p_sys->i_payload_type, psz_rtpmap );
530 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
532 /* create the rtp context */
533 p_sys->ssrc[0] = rand()&0xff;
534 p_sys->ssrc[1] = rand()&0xff;
535 p_sys->ssrc[2] = rand()&0xff;
536 p_sys->ssrc[3] = rand()&0xff;
537 p_sys->i_sequence = rand()&0xffff;
538 p_sys->i_timestamp_start = rand()&0xffffffff;
539 p_sys->packet = NULL;
544 p_sys->p_access = NULL;
545 p_sys->p_grab = NULL;
547 free( val.psz_string );
550 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
551 if( *val.psz_string )
555 SDPHandleUrl( p_stream, val.psz_string );
557 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
559 if( !strcmp( p_cfg->psz_name, "sdp" ) )
561 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
564 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
567 SDPHandleUrl( p_stream, p_cfg->psz_value );
571 free( val.psz_string );
573 /* update p_sout->i_out_pace_nocontrol */
574 p_stream->p_sout->i_out_pace_nocontrol++;
579 /*****************************************************************************
581 *****************************************************************************/
582 static void Close( vlc_object_t * p_this )
584 sout_stream_t *p_stream = (sout_stream_t*)p_this;
585 sout_stream_sys_t *p_sys = p_stream->p_sys;
587 /* update p_sout->i_out_pace_nocontrol */
588 p_stream->p_sout->i_out_pace_nocontrol--;
592 sout_MuxDelete( p_sys->p_mux );
593 sout_AccessOutDelete( p_sys->p_access );
594 sout_AccessOutDelete( p_sys->p_grab );
597 block_Release( p_sys->packet );
601 while( p_sys->i_rtsp > 0 )
603 RtspClientDel( p_stream, p_sys->rtsp[0] );
606 vlc_mutex_destroy( &p_sys->lock_sdp );
608 if( p_sys->p_httpd_file )
610 httpd_FileDelete( p_sys->p_httpd_file );
612 if( p_sys->p_httpd_host )
614 httpd_HostDelete( p_sys->p_httpd_host );
616 if( p_sys->p_rtsp_url )
618 httpd_UrlDelete( p_sys->p_rtsp_url );
620 if( p_sys->p_rtsp_host )
622 httpd_HostDelete( p_sys->p_rtsp_host );
625 /* why? is this disabled? */
626 if( p_sys->psz_session_name )
628 free( p_sys->psz_session_name );
629 p_sys->psz_session_name = NULL;
631 if( p_sys->psz_session_description )
633 free( p_sys->psz_session_description );
634 p_sys->psz_session_description = NULL;
636 if( p_sys->psz_session_url )
638 free( p_sys->psz_session_url );
639 p_sys->psz_session_url = NULL;
641 if( p_sys->psz_session_email )
643 free( p_sys->psz_session_email );
644 p_sys->psz_session_email = NULL;
649 free( p_sys->psz_sdp );
654 /*****************************************************************************
656 *****************************************************************************/
657 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
659 sout_stream_sys_t *p_sys = p_stream->p_sys;
662 vlc_UrlParse( &url, psz_url, 0 );
663 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
665 if( p_sys->p_httpd_file )
667 msg_Err( p_stream, "You can used sdp=http:// only once" );
671 if( HttpSetup( p_stream, &url ) )
673 msg_Err( p_stream, "cannot export sdp as http" );
676 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
678 if( p_sys->p_rtsp_url )
680 msg_Err( p_stream, "You can used sdp=rtsp:// only once" );
684 /* FIXME test if destination is multicast or no destination at all FIXME */
685 if( RtspSetup( p_stream, &url ) )
687 msg_Err( p_stream, "cannot export sdp as rtsp" );
690 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
691 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
693 p_sys->b_export_sap = VLC_TRUE;
694 SapSetup( p_stream );
696 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
698 if( p_sys->b_export_sdp_file )
700 msg_Err( p_stream, "You can used sdp=file:// only once" );
703 p_sys->b_export_sdp_file = VLC_TRUE;
704 psz_url = &psz_url[5];
705 if( psz_url[0] == '/' && psz_url[1] == '/' )
707 p_sys->psz_sdp_file = strdup( psz_url );
711 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
714 vlc_UrlClean( &url );
717 /*****************************************************************************
719 *****************************************************************************/
720 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
721 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
722 o= - should be local username (no spaces allowed)
723 o= time should be hashed with some other value to garantue uniqueness
724 o= we need IP6 support?
725 o= don't use the localhost address. use fully qualified domain name or IP4 address
726 p= international phone number (pass via vars?)
728 a= recvonly (missing)
729 a= type:broadcast (missing)
730 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
731 a= x-plgroup: (missing)
732 RTP packets need to get the correct src IP address */
733 static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
735 sout_stream_sys_t *p_sys = p_stream->p_sys;
740 i_size = strlen( "v=0\r\n" ) +
741 strlen( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
742 strlen( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
743 strlen( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
744 strlen( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
745 strlen( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
746 strlen( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
747 strlen( "a=tool:"PACKAGE_STRING"\r\n" ) +
748 strlen( "c=IN IP4 */*\r\n" ) + 20 + 10 +
749 strlen( psz_destination ? psz_destination : "0.0.0.0") ;
750 for( i = 0; i < p_sys->i_es; i++ )
752 sout_stream_id_t *id = p_sys->es[i];
754 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
757 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
761 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
765 i_size += strlen( "a=control:*/trackid=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
769 p = psz_sdp = malloc( i_size );
770 p += sprintf( p, "v=0\r\n" );
771 p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n",
772 p_sys->i_sdp_id, p_sys->i_sdp_version );
773 if( *p_sys->psz_session_name )
774 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
775 if( *p_sys->psz_session_description )
776 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
777 if( *p_sys->psz_session_url )
778 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
779 if( *p_sys->psz_session_email )
780 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
782 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
783 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
785 p += sprintf( p, "c=IN IP4 %s", psz_destination ? psz_destination : "0.0.0.0" );
787 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ? psz_destination : "0.0.0.0" ) )
789 /* Add the ttl if it is a multicast address */
790 p += sprintf( p, "/%d\r\n", p_sys->i_ttl );
794 p += sprintf( p, "\r\n" );
797 for( i = 0; i < p_sys->i_es; i++ )
799 sout_stream_id_t *id = p_sys->es[i];
801 if( id->i_cat == AUDIO_ES )
803 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
804 id->i_port, id->i_payload_type );
806 else if( id->i_cat == VIDEO_ES )
808 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
809 id->i_port, id->i_payload_type );
817 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
822 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
827 p += sprintf( p, "a=control:%s/trackid=%d\r\n", p_sys->psz_rtsp_control, i );
834 /*****************************************************************************
836 *****************************************************************************/
837 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
838 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
839 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
840 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
841 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
842 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
843 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
844 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
845 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
847 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
849 static const char hex[16] = "0123456789abcdef";
852 for( i = 0; i < i_data; i++ )
854 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
855 s[2*i+1] = hex[(p_data[i] )&0xf];
860 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
862 sout_instance_t *p_sout = p_stream->p_sout;
863 sout_stream_sys_t *p_sys = p_stream->p_sys;
864 sout_stream_id_t *id;
865 sout_access_out_t *p_access = NULL;
869 if( p_sys->p_mux != NULL )
871 sout_input_t *p_input = NULL;
872 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
874 msg_Err( p_stream, "cannot add this stream to the muxer" );
878 id = malloc( sizeof( sout_stream_id_t ) );
879 memset( id, 0, sizeof( sout_stream_id_t ) );
881 id->p_input = p_input;
882 id->pf_packetize= NULL;
883 id->p_rtsp_url = NULL;
889 /* Choose the port */
891 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
893 i_port = p_sys->i_port_audio;
894 p_sys->i_port_audio = 0;
896 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
898 i_port = p_sys->i_port_video;
899 p_sys->i_port_video = 0;
903 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
905 i_port = p_sys->i_port;
912 if( p_sys->psz_destination )
915 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
917 /* first try to create the access out */
918 if( p_sys->i_ttl > 0 )
920 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
924 sprintf( access, "udp{raw}" );
926 sprintf( url, "%s:%d", p_sys->psz_destination, i_port );
927 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
929 msg_Err( p_stream, "cannot create the access out for %s://%s",
933 msg_Dbg( p_stream, "access out %s:%s", access, url );
936 /* not create the rtp specific stuff */
937 id = malloc( sizeof( sout_stream_id_t ) );
938 memset( id, 0, sizeof( sout_stream_id_t ) );
939 id->p_stream = p_stream;
940 id->p_access = p_access;
942 id->psz_rtpmap = NULL;
944 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
946 id->p_rtsp_url = NULL;
947 vlc_mutex_init( p_stream, &id->lock_rtsp );
948 id->i_rtsp_access = 0;
949 id->rtsp_access = NULL;
951 switch( p_fmt->i_codec )
953 case VLC_FOURCC( 's', '1', '6', 'b' ):
954 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
956 id->i_payload_type = 11;
958 else if( p_fmt->audio.i_channels == 2 &&
959 p_fmt->audio.i_rate == 44100 )
961 id->i_payload_type = 10;
965 id->i_payload_type = p_sys->i_payload_type++;
967 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
968 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
969 p_fmt->audio.i_channels );
970 id->i_clock_rate = p_fmt->audio.i_rate;
971 id->pf_packetize = rtp_packetize_l16;
973 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
974 id->i_payload_type = p_sys->i_payload_type++;
975 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
976 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
977 p_fmt->audio.i_channels );
978 id->i_clock_rate = p_fmt->audio.i_rate;
979 id->pf_packetize = rtp_packetize_l8;
981 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
982 id->i_payload_type = 14;
983 id->i_clock_rate = 90000;
984 id->psz_rtpmap = strdup( "MPA/90000" );
985 id->pf_packetize = rtp_packetize_mpa;
987 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
988 id->i_payload_type = 32;
989 id->i_clock_rate = 90000;
990 id->psz_rtpmap = strdup( "MPV/90000" );
991 id->pf_packetize = rtp_packetize_mpv;
993 case VLC_FOURCC( 'a', '5', '2', ' ' ):
994 id->i_payload_type = p_sys->i_payload_type++;
995 id->i_clock_rate = 90000;
996 id->psz_rtpmap = strdup( "ac3/90000" );
997 id->pf_packetize = rtp_packetize_ac3;
999 case VLC_FOURCC( 'H', '2', '6', '3' ):
1000 id->i_payload_type = p_sys->i_payload_type++;
1001 id->i_clock_rate = 90000;
1002 id->psz_rtpmap = strdup( "H263-1998/90000" );
1003 id->pf_packetize = rtp_packetize_h263;
1006 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1008 char hexa[2*p_fmt->i_extra +1];
1010 id->i_payload_type = p_sys->i_payload_type++;
1011 id->i_clock_rate = 90000;
1012 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1013 id->pf_packetize = rtp_packetize_split;
1014 if( p_fmt->i_extra > 0 )
1016 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1017 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1018 sprintf( id->psz_fmtp,
1019 "profile-level-id=3; config=%s;", hexa );
1023 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1025 char hexa[2*p_fmt->i_extra +1];
1027 id->i_payload_type = p_sys->i_payload_type++;
1028 id->i_clock_rate = p_fmt->audio.i_rate;
1029 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1030 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1031 id->pf_packetize = rtp_packetize_mp4a;
1032 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1033 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1034 sprintf( id->psz_fmtp,
1035 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1036 "config=%s; SizeLength=13;IndexLength=3; "
1037 "IndexDeltaLength=3; Profile=1;", hexa );
1040 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1041 id->i_payload_type = p_sys->i_payload_type++;
1042 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1043 "AMR/8000/2" : "AMR/8000" );
1044 id->psz_fmtp = strdup( "octet-align=1" );
1045 id->i_clock_rate = p_fmt->audio.i_rate;
1046 id->pf_packetize = rtp_packetize_amr;
1048 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1049 id->i_payload_type = p_sys->i_payload_type++;
1050 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1051 "AMR-WB/16000/2" : "AMR-WB/16000" );
1052 id->psz_fmtp = strdup( "octet-align=1" );
1053 id->i_clock_rate = p_fmt->audio.i_rate;
1054 id->pf_packetize = rtp_packetize_amr;
1058 msg_Err( p_stream, "cannot add this stream (unsupported "
1059 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1062 sout_AccessOutDelete( p_access );
1067 id->i_cat = p_fmt->i_cat;
1069 id->ssrc[0] = rand()&0xff;
1070 id->ssrc[1] = rand()&0xff;
1071 id->ssrc[2] = rand()&0xff;
1072 id->ssrc[3] = rand()&0xff;
1073 id->i_sequence = rand()&0xffff;
1074 id->i_timestamp_start = rand()&0xffffffff;
1076 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1077 if( id->i_mtu <= 16 )
1079 /* better than nothing */
1082 msg_Dbg( p_stream, "using mtu=%d", id->i_mtu );
1084 if( p_sys->p_rtsp_url )
1086 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1088 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1089 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
1090 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1092 if( id->p_rtsp_url )
1094 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1095 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1096 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1101 /* Update p_sys context */
1102 vlc_mutex_lock( &p_sys->lock_es );
1103 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1104 vlc_mutex_unlock( &p_sys->lock_es );
1106 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1108 vlc_mutex_lock( &p_sys->lock_sdp );
1109 free( p_sys->psz_sdp );
1110 p_sys->psz_sdp = psz_sdp;
1111 vlc_mutex_unlock( &p_sys->lock_sdp );
1113 p_sys->i_sdp_version++;
1115 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
1117 /* Update SDP (sap/file) */
1118 if( p_sys->b_export_sap ) SapSetup( p_stream );
1119 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1124 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1126 sout_stream_sys_t *p_sys = p_stream->p_sys;
1128 vlc_mutex_lock( &p_sys->lock_es );
1129 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1130 vlc_mutex_unlock( &p_sys->lock_es );
1133 if( id->i_port > 0 )
1135 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1136 p_sys->i_port_audio = id->i_port;
1137 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1138 p_sys->i_port_video = id->i_port;
1143 if( id->psz_rtpmap )
1145 free( id->psz_rtpmap );
1149 free( id->psz_fmtp );
1151 if( id->psz_destination )
1152 free( id->psz_destination );
1153 sout_AccessOutDelete( id->p_access );
1155 else if( id->p_input )
1157 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1159 if( id->p_rtsp_url )
1161 httpd_UrlDelete( id->p_rtsp_url );
1163 vlc_mutex_destroy( &id->lock_rtsp );
1164 if( id->rtsp_access ) free( id->rtsp_access );
1166 /* Update SDP (sap/file) */
1167 if( p_sys->b_export_sap ) SapSetup( p_stream );
1168 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1174 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1179 if( p_stream->p_sys->p_mux )
1181 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1187 p_next = p_buffer->p_next;
1188 if( id->pf_packetize( p_stream, id, p_buffer ) )
1192 block_Release( p_buffer );
1200 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1203 sout_stream_sys_t *p_sys = p_stream->p_sys;
1205 int64_t i_dts = p_buffer->i_dts;
1206 uint32_t i_timestamp = i_dts * 9 / 100;
1208 uint8_t *p_data = p_buffer->p_buffer;
1209 unsigned int i_data = p_buffer->i_buffer;
1210 unsigned int i_max = p_sys->i_mtu - 12;
1212 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1216 unsigned int i_size;
1218 /* output complete packet */
1219 if( p_sys->packet &&
1220 p_sys->packet->i_buffer + i_data > i_max )
1222 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1223 p_sys->packet = NULL;
1226 if( p_sys->packet == NULL )
1228 /* allocate a new packet */
1229 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1230 p_sys->packet->p_buffer[ 0] = 0x80;
1231 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1232 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1233 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1234 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1235 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1236 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1237 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1238 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1239 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1240 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1241 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1242 p_sys->packet->i_buffer = 12;
1244 p_sys->packet->i_dts = i_dts;
1245 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1246 i_dts += p_sys->packet->i_length;
1248 p_sys->i_sequence++;
1251 i_size = __MIN( i_data,
1252 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1254 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1257 p_sys->packet->i_buffer += i_size;
1265 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1268 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1270 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1276 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1278 p_next = p_buffer->p_next;
1279 block_Release( p_buffer );
1286 /****************************************************************************
1288 ****************************************************************************/
1289 static int SapSetup( sout_stream_t *p_stream )
1291 sout_stream_sys_t *p_sys = p_stream->p_sys;
1292 sout_instance_t *p_sout = p_stream->p_sout;
1293 announce_method_t *p_method = sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
1295 /* Remove the previous session */
1296 if( p_sys->p_session != NULL)
1298 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1299 sout_AnnounceSessionDestroy( p_sys->p_session );
1300 p_sys->p_session = NULL;
1303 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1305 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1313 /****************************************************************************
1315 ****************************************************************************/
1316 static int FileSetup( sout_stream_t *p_stream )
1318 sout_stream_sys_t *p_sys = p_stream->p_sys;
1321 if( ( f = fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1323 msg_Err( p_stream, "cannot open file '%s' (%s)",
1324 p_sys->psz_sdp_file, strerror(errno) );
1325 return VLC_EGENERIC;
1328 fprintf( f, "%s", p_sys->psz_sdp );
1334 /****************************************************************************
1336 ****************************************************************************/
1337 static int HttpCallback( httpd_file_sys_t *p_args,
1338 httpd_file_t *, uint8_t *p_request,
1339 uint8_t **pp_data, int *pi_data );
1341 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1343 sout_stream_sys_t *p_sys = p_stream->p_sys;
1345 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
1346 if( p_sys->p_httpd_host )
1348 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1349 url->psz_path ? url->psz_path : "/",
1352 HttpCallback, (void*)p_sys );
1354 if( p_sys->p_httpd_file == NULL )
1356 return VLC_EGENERIC;
1361 static int HttpCallback( httpd_file_sys_t *p_args,
1362 httpd_file_t *f, uint8_t *p_request,
1363 uint8_t **pp_data, int *pi_data )
1365 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1367 vlc_mutex_lock( &p_sys->lock_sdp );
1368 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1370 *pi_data = strlen( p_sys->psz_sdp );
1371 *pp_data = malloc( *pi_data );
1372 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1379 vlc_mutex_unlock( &p_sys->lock_sdp );
1384 /****************************************************************************
1386 ****************************************************************************/
1387 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1389 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1391 rtsp->psz_session = psz_session;
1393 rtsp->b_playing = VLC_FALSE;
1397 rtsp->access = NULL;
1399 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1403 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1406 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1408 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1410 return p_stream->p_sys->rtsp[i];
1416 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1419 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1421 for( i = 0; i < rtsp->i_access; i++ )
1423 sout_AccessOutDelete( rtsp->access[i] );
1425 if( rtsp->id ) free( rtsp->id );
1426 if( rtsp->access ) free( rtsp->access );
1428 free( rtsp->psz_session );
1432 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1434 sout_stream_sys_t *p_sys = p_stream->p_sys;
1436 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1438 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1439 if( p_sys->p_rtsp_host == NULL )
1441 return VLC_EGENERIC;
1444 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1445 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1446 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1447 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1449 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1450 if( p_sys->p_rtsp_url == 0 )
1452 return VLC_EGENERIC;
1454 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1455 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1456 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1457 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1462 static int RtspCallback( httpd_callback_sys_t *p_args,
1464 httpd_message_t *answer, httpd_message_t *query )
1466 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1467 sout_stream_sys_t *p_sys = p_stream->p_sys;
1468 char *psz_destination = p_sys->psz_destination;
1469 char *psz_session = NULL;
1471 if( answer == NULL || query == NULL )
1475 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1477 answer->i_proto = HTTPD_PROTO_RTSP;
1478 answer->i_version= query->i_version;
1479 answer->i_type = HTTPD_MSG_ANSWER;
1481 switch( query->i_type )
1483 case HTTPD_MSG_DESCRIBE:
1485 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1487 answer->i_status = 200;
1488 answer->psz_status = strdup( "OK" );
1489 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1491 answer->p_body = (uint8_t *)psz_sdp;
1492 answer->i_body = strlen( psz_sdp );
1496 case HTTPD_MSG_PLAY:
1498 rtsp_client_t *rtsp;
1499 /* for now only multicast so easy */
1500 answer->i_status = 200;
1501 answer->psz_status = strdup( "OK" );
1503 answer->p_body = NULL;
1505 psz_session = httpd_MsgGet( query, "Session" );
1506 rtsp = RtspClientGet( p_stream, psz_session );
1507 if( rtsp && !rtsp->b_playing )
1511 rtsp->b_playing = VLC_TRUE;
1513 vlc_mutex_lock( &p_sys->lock_es );
1514 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1516 sout_stream_id_t *id = rtsp->id[i_id];
1519 for( i = 0; i < p_sys->i_es; i++ )
1521 if( id == p_sys->es[i] )
1524 if( i >= p_sys->i_es ) continue;
1526 vlc_mutex_lock( &id->lock_rtsp );
1527 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1528 vlc_mutex_unlock( &id->lock_rtsp );
1530 vlc_mutex_unlock( &p_sys->lock_es );
1534 case HTTPD_MSG_PAUSE:
1536 return VLC_EGENERIC;
1537 case HTTPD_MSG_TEARDOWN:
1539 rtsp_client_t *rtsp;
1541 /* for now only multicast so easy again */
1542 answer->i_status = 200;
1543 answer->psz_status = strdup( "OK" );
1545 answer->p_body = NULL;
1547 psz_session = httpd_MsgGet( query, "Session" );
1548 rtsp = RtspClientGet( p_stream, psz_session );
1553 vlc_mutex_lock( &p_sys->lock_es );
1554 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1556 sout_stream_id_t *id = rtsp->id[i_id];
1559 for( i = 0; i < p_sys->i_es; i++ )
1561 if( id == p_sys->es[i] )
1564 if( i >= p_sys->i_es ) continue;
1566 vlc_mutex_lock( &id->lock_rtsp );
1567 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1568 vlc_mutex_unlock( &id->lock_rtsp );
1570 vlc_mutex_unlock( &p_sys->lock_es );
1572 RtspClientDel( p_stream, rtsp );
1578 return VLC_EGENERIC;
1580 httpd_MsgAdd( answer, "Server", "VLC Server" );
1581 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1582 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1583 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1587 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1592 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1594 httpd_message_t *answer, httpd_message_t *query )
1596 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1597 sout_stream_t *p_stream = id->p_stream;
1598 sout_stream_sys_t *p_sys = p_stream->p_sys;
1599 char *psz_session = NULL;
1601 if( answer == NULL || query == NULL )
1605 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1607 answer->i_proto = HTTPD_PROTO_RTSP;
1608 answer->i_version= query->i_version;
1609 answer->i_type = HTTPD_MSG_ANSWER;
1611 switch( query->i_type )
1613 case HTTPD_MSG_SETUP:
1615 char *psz_transport = httpd_MsgGet( query, "Transport" );
1617 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1619 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1621 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1622 answer->i_status = 200;
1623 answer->psz_status = strdup( "OK" );
1625 answer->p_body = NULL;
1626 psz_session = httpd_MsgGet( query, "Session" );
1627 if( *psz_session == 0 )
1629 psz_session = malloc( 100 );
1630 sprintf( psz_session, "%d", rand() );
1632 httpd_MsgAdd( answer, "Transport",
1633 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1634 id->psz_destination, id->i_port,id->i_port+1, p_sys->i_ttl );
1636 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1638 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1639 char *ip = httpd_ClientIP( cl );
1641 char psz_access[100];
1644 sout_access_out_t *p_access;
1646 rtsp_client_t *rtsp = NULL;
1650 answer->i_status = 500;
1651 answer->psz_status = strdup( "Internal server error" );
1653 answer->p_body = NULL;
1657 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1660 psz_session = httpd_MsgGet( query, "Session" );
1661 if( *psz_session == 0 )
1663 psz_session = malloc( 100 );
1664 sprintf( psz_session, "%d", rand() );
1666 rtsp = RtspClientNew( p_stream, psz_session );
1670 rtsp = RtspClientGet( p_stream, psz_session );
1673 answer->i_status = 454;
1674 answer->psz_status = strdup( "Unknown session id" );
1676 answer->p_body = NULL;
1682 /* first try to create the access out */
1683 if( p_sys->i_ttl > 0 )
1684 sprintf( psz_access, "udp{raw,ttl=%d}", p_sys->i_ttl );
1686 sprintf( psz_access, "udp{raw}" );
1687 sprintf( psz_url, "%s:%d", ip, i_port );
1690 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1692 msg_Err( p_stream, "cannot create the access out for %s://%s",
1693 psz_access, psz_url );
1694 answer->i_status = 500;
1695 answer->psz_status = strdup( "Internal server error" );
1697 answer->p_body = NULL;
1701 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1702 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1704 answer->i_status = 200;
1705 answer->psz_status = strdup( "OK" );
1707 answer->p_body = NULL;
1709 httpd_MsgAdd( answer, "Transport",
1710 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1712 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1714 answer->i_status = 461;
1715 answer->psz_status = strdup( "Unsupported Transport" );
1717 answer->p_body = NULL;
1723 return VLC_EGENERIC;
1725 httpd_MsgAdd( answer, "Server", "VLC Server" );
1726 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1727 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1728 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1732 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1737 /****************************************************************************
1739 ****************************************************************************/
1740 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1741 int b_marker, int64_t i_pts )
1743 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1745 out->p_buffer[0] = 0x80;
1746 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1747 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1748 out->p_buffer[3] = ( id->i_sequence )&0xff;
1749 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1750 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1751 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1752 out->p_buffer[7] = ( i_timestamp )&0xff;
1754 out->p_buffer[ 8] = id->ssrc[0];
1755 out->p_buffer[ 9] = id->ssrc[1];
1756 out->p_buffer[10] = id->ssrc[2];
1757 out->p_buffer[11] = id->ssrc[3];
1763 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1766 vlc_mutex_lock( &id->lock_rtsp );
1767 for( i = 0; i < id->i_rtsp_access; i++ )
1769 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1771 vlc_mutex_unlock( &id->lock_rtsp );
1775 sout_AccessOutWrite( id->p_access, out );
1779 block_Release( out );
1783 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1786 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1787 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1789 uint8_t *p_data = in->p_buffer;
1790 int i_data = in->i_buffer;
1793 for( i = 0; i < i_count; i++ )
1795 int i_payload = __MIN( i_max, i_data );
1796 block_t *out = block_New( p_stream, 16 + i_payload );
1798 /* rtp common header */
1799 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1801 out->p_buffer[12] = 0;
1802 out->p_buffer[13] = 0;
1803 /* fragment offset in the current frame */
1804 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1805 out->p_buffer[15] = ( (i*i_max) )&0xff;
1806 memcpy( &out->p_buffer[16], p_data, i_payload );
1808 out->i_buffer = 16 + i_payload;
1809 out->i_dts = in->i_dts + i * in->i_length / i_count;
1810 out->i_length = in->i_length / i_count;
1812 rtp_packetize_send( id, out );
1814 p_data += i_payload;
1815 i_data -= i_payload;
1822 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1825 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1826 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1828 uint8_t *p_data = in->p_buffer;
1829 int i_data = in->i_buffer;
1831 int b_sequence_start = 0;
1832 int i_temporal_ref = 0;
1833 int i_picture_coding_type = 0;
1834 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1835 int b_start_slice = 0;
1837 /* preparse this packet to get some info */
1838 if( in->i_buffer > 4 )
1840 uint8_t *p = p_data;
1841 int i_rest = in->i_buffer;
1845 while( i_rest > 4 &&
1846 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1860 /* sequence start code */
1861 b_sequence_start = 1;
1863 else if( *p == 0x00 && i_rest >= 4 )
1866 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1867 i_picture_coding_type = (p[2] >> 3)&0x07;
1869 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1870 i_picture_coding_type == 3 ) )
1872 i_ffv = (p[3] >> 2)&0x01;
1873 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1874 if( i_rest > 4 && i_picture_coding_type == 3 )
1876 i_fbv = (p[4]>>6)&0x01;
1877 i_bfc = (p[4]>>3)&0x07;
1881 else if( *p <= 0xaf )
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,
1893 uint32_t h = ( i_temporal_ref << 16 )|
1894 ( b_sequence_start << 13 )|
1895 ( b_start_slice << 12 )|
1896 ( i == i_count - 1 ? 1 << 11 : 0 )|
1897 ( i_picture_coding_type << 8 )|
1898 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1900 /* rtp common header */
1901 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1902 in->i_pts > 0 ? in->i_pts : in->i_dts );
1904 /* 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 */
1905 out->p_buffer[12] = ( h >> 24 )&0xff;
1906 out->p_buffer[13] = ( h >> 16 )&0xff;
1907 out->p_buffer[14] = ( h >> 8 )&0xff;
1908 out->p_buffer[15] = ( h )&0xff;
1910 memcpy( &out->p_buffer[16], p_data, i_payload );
1912 out->i_buffer = 16 + i_payload;
1913 out->i_dts = in->i_dts + i * in->i_length / i_count;
1914 out->i_length = in->i_length / i_count;
1916 rtp_packetize_send( id, out );
1918 p_data += i_payload;
1919 i_data -= i_payload;
1924 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1927 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1928 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1930 uint8_t *p_data = in->p_buffer;
1931 int i_data = in->i_buffer;
1934 for( i = 0; i < i_count; i++ )
1936 int i_payload = __MIN( i_max, i_data );
1937 block_t *out = block_New( p_stream, 14 + i_payload );
1939 /* rtp common header */
1940 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1942 out->p_buffer[12] = 1;
1944 out->p_buffer[13] = 0x00;
1946 memcpy( &out->p_buffer[14], p_data, i_payload );
1948 out->i_buffer = 14 + i_payload;
1949 out->i_dts = in->i_dts + i * in->i_length / i_count;
1950 out->i_length = in->i_length / i_count;
1952 rtp_packetize_send( id, out );
1954 p_data += i_payload;
1955 i_data -= i_payload;
1961 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1964 int i_max = id->i_mtu - 12; /* payload max in one packet */
1965 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1967 uint8_t *p_data = in->p_buffer;
1968 int i_data = in->i_buffer;
1971 for( i = 0; i < i_count; i++ )
1973 int i_payload = __MIN( i_max, i_data );
1974 block_t *out = block_New( p_stream, 12 + i_payload );
1976 /* rtp common header */
1977 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1978 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1979 memcpy( &out->p_buffer[12], p_data, i_payload );
1981 out->i_buffer = 12 + i_payload;
1982 out->i_dts = in->i_dts + i * in->i_length / i_count;
1983 out->i_length = in->i_length / i_count;
1985 rtp_packetize_send( id, out );
1987 p_data += i_payload;
1988 i_data -= i_payload;
1994 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1997 int i_max = id->i_mtu - 12; /* payload max in one packet */
1998 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2000 uint8_t *p_data = in->p_buffer;
2001 int i_data = in->i_buffer;
2006 int i_payload = (__MIN( i_max, i_data )/4)*4;
2007 block_t *out = block_New( p_stream, 12 + i_payload );
2009 /* rtp common header */
2010 rtp_packetize_common( id, out, 0,
2011 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2012 memcpy( &out->p_buffer[12], p_data, i_payload );
2014 out->i_buffer = 12 + i_payload;
2015 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2016 out->i_length = in->i_length / i_count;
2018 rtp_packetize_send( id, out );
2020 p_data += i_payload;
2021 i_data -= i_payload;
2028 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2031 int i_max = id->i_mtu - 12; /* payload max in one packet */
2032 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2034 uint8_t *p_data = in->p_buffer;
2035 int i_data = in->i_buffer;
2040 int i_payload = (__MIN( i_max, i_data )/2)*2;
2041 block_t *out = block_New( p_stream, 12 + i_payload );
2043 /* rtp common header */
2044 rtp_packetize_common( id, out, 0,
2045 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2046 memcpy( &out->p_buffer[12], p_data, i_payload );
2048 out->i_buffer = 12 + i_payload;
2049 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2050 out->i_length = in->i_length / i_count;
2052 rtp_packetize_send( id, out );
2054 p_data += i_payload;
2055 i_data -= i_payload;
2061 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2064 int i_max = id->i_mtu - 16; /* payload max in one packet */
2065 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2067 uint8_t *p_data = in->p_buffer;
2068 int i_data = in->i_buffer;
2071 for( i = 0; i < i_count; i++ )
2073 int i_payload = __MIN( i_max, i_data );
2074 block_t *out = block_New( p_stream, 16 + i_payload );
2076 /* rtp common header */
2077 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2078 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2080 /* AU headers length (bits) */
2081 out->p_buffer[12] = 0;
2082 out->p_buffer[13] = 2*8;
2083 /* for each AU length 13 bits + idx 3bits, */
2084 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2085 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2087 memcpy( &out->p_buffer[16], p_data, i_payload );
2089 out->i_buffer = 16 + i_payload;
2090 out->i_dts = in->i_dts + i * in->i_length / i_count;
2091 out->i_length = in->i_length / i_count;
2093 rtp_packetize_send( id, out );
2095 p_data += i_payload;
2096 i_data -= i_payload;
2104 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2105 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2106 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2109 uint8_t *p_data = in->p_buffer;
2110 int i_data = in->i_buffer;
2112 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2115 int b_v_bit = 0; // no pesky error resilience
2116 int i_plen = 0; // normally plen=0 for PSC packet
2117 int i_pebit = 0; // because plen=0
2122 return VLC_EGENERIC;
2124 if( p_data[0] || p_data[1] )
2126 return VLC_EGENERIC;
2128 /* remove 2 leading 0 bytes */
2131 i_count = ( i_data + i_max - 1 ) / i_max;
2133 for( i = 0; i < i_count; i++ )
2135 int i_payload = __MIN( i_max, i_data );
2136 block_t *out = block_New( p_stream,
2137 RTP_H263_PAYLOAD_START + i_payload );
2138 b_p_bit = (i == 0) ? 1 : 0;
2139 h = ( b_p_bit << 10 )|
2144 /* rtp common header */
2145 //b_m_bit = 1; // always contains end of frame
2146 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2147 in->i_pts > 0 ? in->i_pts : in->i_dts );
2150 out->p_buffer[12] = ( h >> 8 )&0xff;
2151 out->p_buffer[13] = ( h )&0xff;
2152 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2154 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2155 out->i_dts = in->i_dts + i * in->i_length / i_count;
2156 out->i_length = in->i_length / i_count;
2158 rtp_packetize_send( id, out );
2160 p_data += i_payload;
2161 i_data -= i_payload;
2167 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2170 int i_max = id->i_mtu - 14; /* payload max in one packet */
2171 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2173 uint8_t *p_data = in->p_buffer;
2174 int i_data = in->i_buffer;
2177 /* Only supports octet-aligned mode */
2178 for( i = 0; i < i_count; i++ )
2180 int i_payload = __MIN( i_max, i_data );
2181 block_t *out = block_New( p_stream, 14 + 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) );
2186 /* Payload header */
2187 out->p_buffer[12] = 0xF0; /* CMR */
2188 out->p_buffer[13] = 0x00; /* ToC */ /* FIXME: frame type */
2190 /* FIXME: are we fed multiple frames ? */
2191 memcpy( &out->p_buffer[14], p_data, i_payload );
2193 out->i_buffer = 14 + i_payload;
2194 out->i_dts = in->i_dts + i * in->i_length / i_count;
2195 out->i_length = in->i_length / i_count;
2197 rtp_packetize_send( id, out );
2199 p_data += i_payload;
2200 i_data -= i_payload;