1 /*****************************************************************************
2 * rtp.c: rtp stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
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 p_sys->i_ttl = val.i_int;
357 p_sys->i_payload_type = 96;
362 p_sys->psz_sdp = NULL;
364 p_sys->i_sdp_id = mdate();
365 p_sys->i_sdp_version = 1;
366 p_sys->psz_sdp = NULL;
368 p_sys->b_export_sap = VLC_FALSE;
369 p_sys->b_export_sdp_file = VLC_FALSE;
370 p_sys->p_session = NULL;
372 p_sys->p_httpd_host = NULL;
373 p_sys->p_httpd_file = NULL;
374 p_sys->p_rtsp_host = NULL;
375 p_sys->p_rtsp_url = NULL;
376 p_sys->psz_rtsp_control = NULL;
377 p_sys->psz_rtsp_path = NULL;
379 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
380 vlc_mutex_init( p_stream, &p_sys->lock_es );
382 p_stream->pf_add = Add;
383 p_stream->pf_del = Del;
384 p_stream->pf_send = Send;
386 p_stream->p_sys = p_sys;
388 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
389 if( *val.psz_string )
391 sout_access_out_t *p_grab;
395 char url[p_sys->psz_destination ? strlen( p_sys->psz_destination ) + 1 + 12+1 : 14];
397 /* Check muxer type */
398 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
400 psz_rtpmap = "MP2P/90000";
402 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
404 psz_rtpmap = "MP2T/90000";
405 p_sys->i_payload_type = 33;
409 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
413 /* create the access out */
414 if( p_sys->i_ttl > 0 )
416 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
420 sprintf( access, "udp{raw}" );
422 sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
423 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
425 msg_Err( p_stream, "cannot create the access out for %s://%s",
430 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
431 if( p_sys->i_mtu <= 16 )
433 /* better than nothing */
437 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
438 p_grab = p_sys->p_grab =
439 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
440 p_grab->p_module = NULL;
441 p_grab->p_sout = p_sout;
442 p_grab->psz_access = strdup( "grab" );
443 p_grab->p_cfg = NULL;
444 p_grab->psz_name = strdup( "" );
445 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
446 p_grab->pf_seek = NULL;
447 p_grab->pf_write = AccessOutGrabberWrite;
450 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
452 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
453 sout_AccessOutDelete( p_sys->p_grab );
454 sout_AccessOutDelete( p_sys->p_access );
459 /* create the SDP for a muxed stream (only once) */
460 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
461 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
462 o= - should be local username (no spaces allowed)
463 o= time should be hashed with some other value to garantue uniqueness
464 o= we need IP6 support?
465 o= don't use the localhost address. use fully qualified domain name or IP4 address
466 p= international phone number (pass via vars?)
468 c= /ttl should only be added in case of multicast
469 a= recvonly (missing)
470 a= type:broadcast (missing)
471 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
472 a= x-plgroup: (missing)
473 RTP packets need to get the correct src IP address */
475 malloc( 10 + 30 + 10 + strlen( p_sys->psz_session_name ) +
476 10 + strlen( p_sys->psz_session_description ) + 10 + strlen( p_sys->psz_session_url ) +
477 10 + strlen( p_sys->psz_session_email ) + 10 + strlen( p_sys->psz_destination ) +
478 10 + 10 + strlen( PACKAGE_STRING ) +
479 20 + 10 + 20 + 10 + strlen( psz_rtpmap ) );
480 sprintf( p_sys->psz_sdp,
482 "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
487 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
488 "a=tool:"PACKAGE_STRING"\r\n"
490 "m=video %d RTP/AVP %d\r\n"
491 "a=rtpmap:%d %s\r\n",
492 p_sys->i_sdp_id, p_sys->i_sdp_version,
493 p_sys->psz_session_name,
494 p_sys->psz_session_description,
495 p_sys->psz_session_url,
496 p_sys->psz_session_email,
497 p_sys->psz_destination, p_sys->i_ttl,
498 p_sys->i_port, p_sys->i_payload_type,
499 p_sys->i_payload_type, psz_rtpmap );
500 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
502 /* create the rtp context */
503 p_sys->ssrc[0] = rand()&0xff;
504 p_sys->ssrc[1] = rand()&0xff;
505 p_sys->ssrc[2] = rand()&0xff;
506 p_sys->ssrc[3] = rand()&0xff;
507 p_sys->i_sequence = rand()&0xffff;
508 p_sys->i_timestamp_start = rand()&0xffffffff;
509 p_sys->packet = NULL;
514 p_sys->p_access = NULL;
515 p_sys->p_grab = NULL;
517 free( val.psz_string );
520 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
521 if( *val.psz_string )
525 SDPHandleUrl( p_stream, val.psz_string );
527 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
529 if( !strcmp( p_cfg->psz_name, "sdp" ) )
531 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
534 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
537 SDPHandleUrl( p_stream, p_cfg->psz_value );
541 free( val.psz_string );
543 /* update p_sout->i_out_pace_nocontrol */
544 p_stream->p_sout->i_out_pace_nocontrol++;
549 /*****************************************************************************
551 *****************************************************************************/
552 static void Close( vlc_object_t * p_this )
554 sout_stream_t *p_stream = (sout_stream_t*)p_this;
555 sout_stream_sys_t *p_sys = p_stream->p_sys;
557 /* update p_sout->i_out_pace_nocontrol */
558 p_stream->p_sout->i_out_pace_nocontrol--;
562 sout_MuxDelete( p_sys->p_mux );
563 sout_AccessOutDelete( p_sys->p_access );
564 sout_AccessOutDelete( p_sys->p_grab );
567 block_Release( p_sys->packet );
571 while( p_sys->i_rtsp > 0 )
573 RtspClientDel( p_stream, p_sys->rtsp[0] );
576 vlc_mutex_destroy( &p_sys->lock_sdp );
578 if( p_sys->p_httpd_file )
580 httpd_FileDelete( p_sys->p_httpd_file );
582 if( p_sys->p_httpd_host )
584 httpd_HostDelete( p_sys->p_httpd_host );
586 if( p_sys->p_rtsp_url )
588 httpd_UrlDelete( p_sys->p_rtsp_url );
590 if( p_sys->p_rtsp_host )
592 httpd_HostDelete( p_sys->p_rtsp_host );
595 /* why? is this disabled? */
596 if( p_sys->psz_session_name )
598 free( p_sys->psz_session_name );
599 p_sys->psz_session_name = NULL;
601 if( p_sys->psz_session_description )
603 free( p_sys->psz_session_description );
604 p_sys->psz_session_description = NULL;
606 if( p_sys->psz_session_url )
608 free( p_sys->psz_session_url );
609 p_sys->psz_session_url = NULL;
611 if( p_sys->psz_session_email )
613 free( p_sys->psz_session_email );
614 p_sys->psz_session_email = NULL;
619 free( p_sys->psz_sdp );
624 /*****************************************************************************
626 *****************************************************************************/
627 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
629 sout_stream_sys_t *p_sys = p_stream->p_sys;
632 vlc_UrlParse( &url, psz_url, 0 );
633 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
635 if( p_sys->p_httpd_file )
637 msg_Err( p_stream, "You can used sdp=http:// only once" );
641 if( HttpSetup( p_stream, &url ) )
643 msg_Err( p_stream, "cannot export sdp as http" );
646 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
648 if( p_sys->p_rtsp_url )
650 msg_Err( p_stream, "You can used sdp=rtsp:// only once" );
654 /* FIXME test if destination is multicast or no destination at all FIXME */
655 if( RtspSetup( p_stream, &url ) )
657 msg_Err( p_stream, "cannot export sdp as rtsp" );
660 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
661 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
663 p_sys->b_export_sap = VLC_TRUE;
664 SapSetup( p_stream );
666 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
668 if( p_sys->b_export_sdp_file )
670 msg_Err( p_stream, "You can used sdp=file:// only once" );
673 p_sys->b_export_sdp_file = VLC_TRUE;
674 psz_url = &psz_url[5];
675 if( psz_url[0] == '/' && psz_url[1] == '/' )
677 p_sys->psz_sdp_file = strdup( psz_url );
681 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
684 vlc_UrlClean( &url );
687 /*****************************************************************************
689 *****************************************************************************/
690 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
691 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
692 o= - should be local username (no spaces allowed)
693 o= time should be hashed with some other value to garantue uniqueness
694 o= we need IP6 support?
695 o= don't use the localhost address. use fully qualified domain name or IP4 address
696 p= international phone number (pass via vars?)
698 c= /ttl should only be added in case of multicast
699 a= recvonly (missing)
700 a= type:broadcast (missing)
701 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
702 a= x-plgroup: (missing)
703 RTP packets need to get the correct src IP address */
704 static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
706 sout_stream_sys_t *p_sys = p_stream->p_sys;
711 i_size = strlen( "v=0\r\n" ) +
712 strlen( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
713 strlen( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
714 strlen( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
715 strlen( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
716 strlen( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
717 strlen( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
718 strlen( "a=tool:"PACKAGE_STRING"\r\n" ) +
719 strlen( "c=IN IP4 */*\r\n" ) + 20 + 10 +
720 strlen( psz_destination ? psz_destination : "0.0.0.0") ;
721 for( i = 0; i < p_sys->i_es; i++ )
723 sout_stream_id_t *id = p_sys->es[i];
725 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
728 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
732 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
736 i_size += strlen( "a=control:*/trackid=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
740 p = psz_sdp = malloc( i_size );
741 p += sprintf( p, "v=0\r\n" );
742 p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n",
743 p_sys->i_sdp_id, p_sys->i_sdp_version );
744 if( *p_sys->psz_session_name )
745 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
746 if( *p_sys->psz_session_description )
747 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
748 if( *p_sys->psz_session_url )
749 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
750 if( *p_sys->psz_session_email )
751 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
753 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
754 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
756 p += sprintf( p, "c=IN IP4 %s/%d\r\n", psz_destination ? psz_destination : "0.0.0.0",
759 for( i = 0; i < p_sys->i_es; i++ )
761 sout_stream_id_t *id = p_sys->es[i];
763 if( id->i_cat == AUDIO_ES )
765 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
766 id->i_port, id->i_payload_type );
768 else if( id->i_cat == VIDEO_ES )
770 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
771 id->i_port, id->i_payload_type );
779 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
784 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
789 p += sprintf( p, "a=control:%s/trackid=%d\r\n", p_sys->psz_rtsp_control, i );
796 /*****************************************************************************
798 *****************************************************************************/
799 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
800 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
801 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
802 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
803 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
804 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
805 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
806 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
808 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
810 static const char hex[16] = "0123456789abcdef";
813 for( i = 0; i < i_data; i++ )
815 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
816 s[2*i+1] = hex[(p_data[i] )&0xf];
821 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
823 sout_instance_t *p_sout = p_stream->p_sout;
824 sout_stream_sys_t *p_sys = p_stream->p_sys;
825 sout_stream_id_t *id;
826 sout_access_out_t *p_access = NULL;
830 if( p_sys->p_mux != NULL )
832 sout_input_t *p_input = NULL;
833 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
835 msg_Err( p_stream, "cannot add this stream to the muxer" );
839 id = malloc( sizeof( sout_stream_id_t ) );
840 memset( id, 0, sizeof( sout_stream_id_t ) );
842 id->p_input = p_input;
843 id->pf_packetize= NULL;
844 id->p_rtsp_url = NULL;
850 /* Choose the port */
852 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
854 i_port = p_sys->i_port_audio;
855 p_sys->i_port_audio = 0;
857 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
859 i_port = p_sys->i_port_video;
860 p_sys->i_port_video = 0;
864 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
866 i_port = p_sys->i_port;
873 if( p_sys->psz_destination )
876 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
878 /* first try to create the access out */
879 if( p_sys->i_ttl > 0 )
881 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
885 sprintf( access, "udp{raw}" );
887 sprintf( url, "%s:%d", p_sys->psz_destination, i_port );
888 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
890 msg_Err( p_stream, "cannot create the access out for %s://%s",
894 msg_Dbg( p_stream, "access out %s:%s", access, url );
897 /* not create the rtp specific stuff */
898 id = malloc( sizeof( sout_stream_id_t ) );
899 memset( id, 0, sizeof( sout_stream_id_t ) );
900 id->p_stream = p_stream;
901 id->p_access = p_access;
903 id->psz_rtpmap = NULL;
905 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
907 id->p_rtsp_url = NULL;
908 vlc_mutex_init( p_stream, &id->lock_rtsp );
909 id->i_rtsp_access = 0;
910 id->rtsp_access = NULL;
912 switch( p_fmt->i_codec )
914 case VLC_FOURCC( 's', '1', '6', 'b' ):
915 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
917 id->i_payload_type = 11;
919 else if( p_fmt->audio.i_channels == 2 &&
920 p_fmt->audio.i_rate == 44100 )
922 id->i_payload_type = 10;
926 id->i_payload_type = p_sys->i_payload_type++;
928 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
929 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
930 p_fmt->audio.i_channels );
931 id->i_clock_rate = p_fmt->audio.i_rate;
932 id->pf_packetize = rtp_packetize_l16;
934 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
935 id->i_payload_type = p_sys->i_payload_type++;
936 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
937 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
938 p_fmt->audio.i_channels );
939 id->i_clock_rate = p_fmt->audio.i_rate;
940 id->pf_packetize = rtp_packetize_l8;
942 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
943 id->i_payload_type = 14;
944 id->i_clock_rate = 90000;
945 id->psz_rtpmap = strdup( "MPA/90000" );
946 id->pf_packetize = rtp_packetize_mpa;
948 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
949 id->i_payload_type = 32;
950 id->i_clock_rate = 90000;
951 id->psz_rtpmap = strdup( "MPV/90000" );
952 id->pf_packetize = rtp_packetize_mpv;
954 case VLC_FOURCC( 'a', '5', '2', ' ' ):
955 id->i_payload_type = p_sys->i_payload_type++;
956 id->i_clock_rate = 90000;
957 id->psz_rtpmap = strdup( "ac3/90000" );
958 id->pf_packetize = rtp_packetize_ac3;
960 case VLC_FOURCC( 'H', '2', '6', '3' ):
961 id->i_payload_type = p_sys->i_payload_type++;
962 id->i_clock_rate = 90000;
963 id->psz_rtpmap = strdup( "H263-1998/90000" );
964 id->pf_packetize = rtp_packetize_h263;
967 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
969 char hexa[2*p_fmt->i_extra +1];
971 id->i_payload_type = p_sys->i_payload_type++;
972 id->i_clock_rate = 90000;
973 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
974 id->pf_packetize = rtp_packetize_split;
975 if( p_fmt->i_extra > 0 )
977 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
978 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
979 sprintf( id->psz_fmtp,
980 "profile-level-id=3; config=%s;", hexa );
984 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
986 char hexa[2*p_fmt->i_extra +1];
988 id->i_payload_type = p_sys->i_payload_type++;
989 id->i_clock_rate = p_fmt->audio.i_rate;
990 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
991 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
992 id->pf_packetize = rtp_packetize_mp4a;
993 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
994 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
995 sprintf( id->psz_fmtp,
996 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
997 "config=%s; SizeLength=13;IndexLength=3; "
998 "IndexDeltaLength=3; Profile=1;", hexa );
1003 msg_Err( p_stream, "cannot add this stream (unsupported "
1004 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1007 sout_AccessOutDelete( p_access );
1012 id->i_cat = p_fmt->i_cat;
1014 id->ssrc[0] = rand()&0xff;
1015 id->ssrc[1] = rand()&0xff;
1016 id->ssrc[2] = rand()&0xff;
1017 id->ssrc[3] = rand()&0xff;
1018 id->i_sequence = rand()&0xffff;
1019 id->i_timestamp_start = rand()&0xffffffff;
1021 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1022 if( id->i_mtu <= 16 )
1024 /* better than nothing */
1027 msg_Dbg( p_stream, "using mtu=%d", id->i_mtu );
1029 if( p_sys->p_rtsp_url )
1031 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1033 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1034 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
1035 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
1037 if( id->p_rtsp_url )
1039 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1040 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1041 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1046 /* Update p_sys context */
1047 vlc_mutex_lock( &p_sys->lock_es );
1048 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1049 vlc_mutex_unlock( &p_sys->lock_es );
1051 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1053 vlc_mutex_lock( &p_sys->lock_sdp );
1054 free( p_sys->psz_sdp );
1055 p_sys->psz_sdp = psz_sdp;
1056 vlc_mutex_unlock( &p_sys->lock_sdp );
1058 p_sys->i_sdp_version++;
1060 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
1062 /* Update SDP (sap/file) */
1063 if( p_sys->b_export_sap ) SapSetup( p_stream );
1064 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1069 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1071 sout_stream_sys_t *p_sys = p_stream->p_sys;
1073 vlc_mutex_lock( &p_sys->lock_es );
1074 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1075 vlc_mutex_unlock( &p_sys->lock_es );
1078 if( id->i_port > 0 )
1080 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1081 p_sys->i_port_audio = id->i_port;
1082 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1083 p_sys->i_port_video = id->i_port;
1088 if( id->psz_rtpmap )
1090 free( id->psz_rtpmap );
1094 free( id->psz_fmtp );
1096 if( id->psz_destination )
1097 free( id->psz_destination );
1098 sout_AccessOutDelete( id->p_access );
1100 else if( id->p_input )
1102 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1104 if( id->p_rtsp_url )
1106 httpd_UrlDelete( id->p_rtsp_url );
1108 vlc_mutex_destroy( &id->lock_rtsp );
1109 if( id->rtsp_access ) free( id->rtsp_access );
1111 /* Update SDP (sap/file) */
1112 if( p_sys->b_export_sap ) SapSetup( p_stream );
1113 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1119 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1124 if( p_stream->p_sys->p_mux )
1126 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1132 p_next = p_buffer->p_next;
1133 if( id->pf_packetize( p_stream, id, p_buffer ) )
1137 block_Release( p_buffer );
1145 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1148 sout_stream_sys_t *p_sys = p_stream->p_sys;
1150 int64_t i_dts = p_buffer->i_dts;
1151 uint32_t i_timestamp = i_dts * 9 / 100;
1153 uint8_t *p_data = p_buffer->p_buffer;
1154 unsigned int i_data = p_buffer->i_buffer;
1155 unsigned int i_max = p_sys->i_mtu - 12;
1157 int i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1161 unsigned int i_size;
1163 /* output complete packet */
1164 if( p_sys->packet &&
1165 p_sys->packet->i_buffer + i_data > i_max )
1167 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1168 p_sys->packet = NULL;
1171 if( p_sys->packet == NULL )
1173 /* allocate a new packet */
1174 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1175 p_sys->packet->p_buffer[ 0] = 0x80;
1176 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1177 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1178 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1179 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1180 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1181 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1182 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1183 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1184 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1185 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1186 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1187 p_sys->packet->i_buffer = 12;
1189 p_sys->packet->i_dts = i_dts;
1190 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1191 i_dts += p_sys->packet->i_length;
1193 p_sys->i_sequence++;
1196 i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_buffer );
1198 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1201 p_sys->packet->i_buffer += i_size;
1209 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1212 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1214 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1220 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1222 p_next = p_buffer->p_next;
1223 block_Release( p_buffer );
1230 /****************************************************************************
1232 ****************************************************************************/
1233 static int SapSetup( sout_stream_t *p_stream )
1235 sout_stream_sys_t *p_sys = p_stream->p_sys;
1236 sout_instance_t *p_sout = p_stream->p_sout;
1237 /* FIXME: use sout_AnnounceMethodCreate */
1238 announce_method_t *p_method = (announce_method_t *)
1239 malloc(sizeof(announce_method_t));
1241 /* Remove the previous session */
1242 if( p_sys->p_session != NULL)
1244 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1245 sout_AnnounceSessionDestroy( p_sys->p_session );
1246 p_sys->p_session = NULL;
1248 p_method->i_type = METHOD_TYPE_SAP;
1249 p_method->psz_address = NULL; /* FIXME */
1251 if( p_sys->i_es > 0 && p_sys->psz_sdp && *p_sys->psz_sdp )
1253 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1261 /****************************************************************************
1263 ****************************************************************************/
1264 static int FileSetup( sout_stream_t *p_stream )
1266 sout_stream_sys_t *p_sys = p_stream->p_sys;
1269 if( ( f = fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1271 msg_Err( p_stream, "cannot open file '%s' (%s)",
1272 p_sys->psz_sdp_file, strerror(errno) );
1273 return VLC_EGENERIC;
1276 fprintf( f, "%s", p_sys->psz_sdp );
1282 /****************************************************************************
1284 ****************************************************************************/
1285 static int HttpCallback( httpd_file_sys_t *p_args,
1286 httpd_file_t *, uint8_t *p_request,
1287 uint8_t **pp_data, int *pi_data );
1289 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1291 sout_stream_sys_t *p_sys = p_stream->p_sys;
1293 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
1294 if( p_sys->p_httpd_host )
1296 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1297 url->psz_path ? url->psz_path : "/",
1300 HttpCallback, (void*)p_sys );
1302 if( p_sys->p_httpd_file == NULL )
1304 return VLC_EGENERIC;
1309 static int HttpCallback( httpd_file_sys_t *p_args,
1310 httpd_file_t *f, uint8_t *p_request,
1311 uint8_t **pp_data, int *pi_data )
1313 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1315 vlc_mutex_lock( &p_sys->lock_sdp );
1316 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1318 *pi_data = strlen( p_sys->psz_sdp );
1319 *pp_data = malloc( *pi_data );
1320 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1327 vlc_mutex_unlock( &p_sys->lock_sdp );
1332 /****************************************************************************
1334 ****************************************************************************/
1335 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1337 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1339 rtsp->psz_session = psz_session;
1341 rtsp->b_playing = VLC_FALSE;
1345 rtsp->access = NULL;
1347 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1351 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1354 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1356 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1358 return p_stream->p_sys->rtsp[i];
1364 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1367 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1369 for( i = 0; i < rtsp->i_access; i++ )
1371 sout_AccessOutDelete( rtsp->access[i] );
1373 if( rtsp->id ) free( rtsp->id );
1374 if( rtsp->access ) free( rtsp->access );
1376 free( rtsp->psz_session );
1380 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1382 sout_stream_sys_t *p_sys = p_stream->p_sys;
1384 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1386 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1387 if( p_sys->p_rtsp_host == NULL )
1389 return VLC_EGENERIC;
1392 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1393 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1394 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1395 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1397 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
1398 if( p_sys->p_rtsp_url == 0 )
1400 return VLC_EGENERIC;
1402 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1403 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1404 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1405 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1410 static int RtspCallback( httpd_callback_sys_t *p_args,
1412 httpd_message_t *answer, httpd_message_t *query )
1414 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1415 sout_stream_sys_t *p_sys = p_stream->p_sys;
1416 char *psz_destination = p_sys->psz_destination;
1417 char *psz_session = NULL;
1419 if( answer == NULL || query == NULL )
1423 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1425 answer->i_proto = HTTPD_PROTO_RTSP;
1426 answer->i_version= query->i_version;
1427 answer->i_type = HTTPD_MSG_ANSWER;
1429 switch( query->i_type )
1431 case HTTPD_MSG_DESCRIBE:
1433 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1435 answer->i_status = 200;
1436 answer->psz_status = strdup( "OK" );
1437 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1439 answer->p_body = psz_sdp;
1440 answer->i_body = strlen( psz_sdp );
1444 case HTTPD_MSG_PLAY:
1446 rtsp_client_t *rtsp;
1447 /* for now only multicast so easy */
1448 answer->i_status = 200;
1449 answer->psz_status = strdup( "OK" );
1451 answer->p_body = NULL;
1453 psz_session = httpd_MsgGet( query, "Session" );
1454 rtsp = RtspClientGet( p_stream, psz_session );
1455 if( rtsp && !rtsp->b_playing )
1459 rtsp->b_playing = VLC_TRUE;
1461 vlc_mutex_lock( &p_sys->lock_es );
1462 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1464 sout_stream_id_t *id = rtsp->id[i_id];
1467 for( i = 0; i < p_sys->i_es; i++ )
1469 if( id == p_sys->es[i] )
1472 if( i >= p_sys->i_es ) continue;
1474 vlc_mutex_lock( &id->lock_rtsp );
1475 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1476 vlc_mutex_unlock( &id->lock_rtsp );
1478 vlc_mutex_unlock( &p_sys->lock_es );
1482 case HTTPD_MSG_PAUSE:
1484 return VLC_EGENERIC;
1485 case HTTPD_MSG_TEARDOWN:
1487 rtsp_client_t *rtsp;
1489 /* for now only multicast so easy again */
1490 answer->i_status = 200;
1491 answer->psz_status = strdup( "OK" );
1493 answer->p_body = NULL;
1495 psz_session = httpd_MsgGet( query, "Session" );
1496 rtsp = RtspClientGet( p_stream, psz_session );
1501 vlc_mutex_lock( &p_sys->lock_es );
1502 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1504 sout_stream_id_t *id = rtsp->id[i_id];
1507 for( i = 0; i < p_sys->i_es; i++ )
1509 if( id == p_sys->es[i] )
1512 if( i >= p_sys->i_es ) continue;
1514 vlc_mutex_lock( &id->lock_rtsp );
1515 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1516 vlc_mutex_unlock( &id->lock_rtsp );
1518 vlc_mutex_unlock( &p_sys->lock_es );
1520 RtspClientDel( p_stream, rtsp );
1526 return VLC_EGENERIC;
1528 httpd_MsgAdd( answer, "Server", "VLC Server" );
1529 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1530 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1531 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1535 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1540 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1542 httpd_message_t *answer, httpd_message_t *query )
1544 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1545 sout_stream_t *p_stream = id->p_stream;
1546 sout_stream_sys_t *p_sys = p_stream->p_sys;
1547 char *psz_session = NULL;
1549 if( answer == NULL || query == NULL )
1553 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1555 answer->i_proto = HTTPD_PROTO_RTSP;
1556 answer->i_version= query->i_version;
1557 answer->i_type = HTTPD_MSG_ANSWER;
1559 switch( query->i_type )
1561 case HTTPD_MSG_SETUP:
1563 char *psz_transport = httpd_MsgGet( query, "Transport" );
1565 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1567 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1569 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1570 answer->i_status = 200;
1571 answer->psz_status = strdup( "OK" );
1573 answer->p_body = NULL;
1574 psz_session = httpd_MsgGet( query, "Session" );
1575 if( *psz_session == 0 )
1577 psz_session = malloc( 100 );
1578 sprintf( psz_session, "%d", rand() );
1580 httpd_MsgAdd( answer, "Transport",
1581 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1582 id->psz_destination, id->i_port,id->i_port+1, p_sys->i_ttl );
1584 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1586 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1587 char *ip = httpd_ClientIP( cl );
1589 char psz_access[100];
1592 sout_access_out_t *p_access;
1594 rtsp_client_t *rtsp = NULL;
1598 answer->i_status = 400;
1599 answer->psz_status = strdup( "Internal server error" );
1601 answer->p_body = NULL;
1605 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1608 psz_session = httpd_MsgGet( query, "Session" );
1609 if( *psz_session == 0 )
1611 psz_session = malloc( 100 );
1612 sprintf( psz_session, "%d", rand() );
1614 rtsp = RtspClientNew( p_stream, psz_session );
1618 rtsp = RtspClientGet( p_stream, psz_session );
1621 /* FIXME right error code */
1622 answer->i_status = 400;
1623 answer->psz_status = strdup( "Unknown session id" );
1625 answer->p_body = NULL;
1631 /* first try to create the access out */
1632 if( p_sys->i_ttl > 0 )
1633 sprintf( psz_access, "udp{raw,ttl=%d}", p_sys->i_ttl );
1635 sprintf( psz_access, "udp{raw}" );
1636 sprintf( psz_url, "%s:%d", ip, i_port );
1639 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1641 msg_Err( p_stream, "cannot create the access out for %s://%s",
1642 psz_access, psz_url );
1643 answer->i_status = 400;
1644 answer->psz_status = strdup( "Server internal error" );
1646 answer->p_body = NULL;
1650 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1651 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1653 answer->i_status = 200;
1654 answer->psz_status = strdup( "OK" );
1656 answer->p_body = NULL;
1658 httpd_MsgAdd( answer, "Transport",
1659 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1661 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1663 answer->i_status = 400;
1664 answer->psz_status = strdup( "Bad Request" );
1666 answer->p_body = NULL;
1672 return VLC_EGENERIC;
1674 httpd_MsgAdd( answer, "Server", "VLC Server" );
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 /****************************************************************************
1688 ****************************************************************************/
1689 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1690 int b_marker, int64_t i_pts )
1692 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1694 out->p_buffer[0] = 0x80;
1695 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1696 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1697 out->p_buffer[3] = ( id->i_sequence )&0xff;
1698 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1699 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1700 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1701 out->p_buffer[7] = ( i_timestamp )&0xff;
1703 out->p_buffer[ 8] = id->ssrc[0];
1704 out->p_buffer[ 9] = id->ssrc[1];
1705 out->p_buffer[10] = id->ssrc[2];
1706 out->p_buffer[11] = id->ssrc[3];
1712 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1715 vlc_mutex_lock( &id->lock_rtsp );
1716 for( i = 0; i < id->i_rtsp_access; i++ )
1718 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1720 vlc_mutex_unlock( &id->lock_rtsp );
1724 sout_AccessOutWrite( id->p_access, out );
1728 block_Release( out );
1732 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1735 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1736 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1738 uint8_t *p_data = in->p_buffer;
1739 int i_data = in->i_buffer;
1742 for( i = 0; i < i_count; i++ )
1744 int i_payload = __MIN( i_max, i_data );
1745 block_t *out = block_New( p_stream, 16 + i_payload );
1747 /* rtp common header */
1748 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1750 out->p_buffer[12] = 0;
1751 out->p_buffer[13] = 0;
1752 /* fragment offset in the current frame */
1753 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1754 out->p_buffer[15] = ( (i*i_max) )&0xff;
1755 memcpy( &out->p_buffer[16], p_data, i_payload );
1757 out->i_buffer = 16 + i_payload;
1758 out->i_dts = in->i_dts + i * in->i_length / i_count;
1759 out->i_length = in->i_length / i_count;
1761 rtp_packetize_send( id, out );
1763 p_data += i_payload;
1764 i_data -= i_payload;
1771 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1774 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1775 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1777 uint8_t *p_data = in->p_buffer;
1778 int i_data = in->i_buffer;
1780 int b_sequence_start = 0;
1781 int i_temporal_ref = 0;
1782 int i_picture_coding_type = 0;
1783 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1784 int b_start_slice = 0;
1786 /* preparse this packet to get some info */
1787 if( in->i_buffer > 4 )
1789 uint8_t *p = p_data;
1790 int i_rest = in->i_buffer;
1794 while( i_rest > 4 &&
1795 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1809 /* sequence start code */
1810 b_sequence_start = 1;
1812 else if( *p == 0x00 && i_rest >= 4 )
1815 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1816 i_picture_coding_type = (p[2] >> 3)&0x07;
1818 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1819 i_picture_coding_type == 3 ) )
1821 i_ffv = (p[3] >> 2)&0x01;
1822 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1823 if( i_rest > 4 && i_picture_coding_type == 3 )
1825 i_fbv = (p[4]>>6)&0x01;
1826 i_bfc = (p[4]>>3)&0x07;
1830 else if( *p <= 0xaf )
1837 for( i = 0; i < i_count; i++ )
1839 int i_payload = __MIN( i_max, i_data );
1840 block_t *out = block_New( p_stream,
1842 uint32_t h = ( i_temporal_ref << 16 )|
1843 ( b_sequence_start << 13 )|
1844 ( b_start_slice << 12 )|
1845 ( i == i_count - 1 ? 1 << 11 : 0 )|
1846 ( i_picture_coding_type << 8 )|
1847 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1849 /* rtp common header */
1850 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1851 in->i_pts > 0 ? in->i_pts : in->i_dts );
1853 /* 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 */
1854 out->p_buffer[12] = ( h >> 24 )&0xff;
1855 out->p_buffer[13] = ( h >> 16 )&0xff;
1856 out->p_buffer[14] = ( h >> 8 )&0xff;
1857 out->p_buffer[15] = ( h )&0xff;
1859 memcpy( &out->p_buffer[16], p_data, i_payload );
1861 out->i_buffer = 16 + i_payload;
1862 out->i_dts = in->i_dts + i * in->i_length / i_count;
1863 out->i_length = in->i_length / i_count;
1865 rtp_packetize_send( id, out );
1867 p_data += i_payload;
1868 i_data -= i_payload;
1873 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1876 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1877 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1879 uint8_t *p_data = in->p_buffer;
1880 int i_data = in->i_buffer;
1883 for( i = 0; i < i_count; i++ )
1885 int i_payload = __MIN( i_max, i_data );
1886 block_t *out = block_New( p_stream, 14 + i_payload );
1888 /* rtp common header */
1889 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1891 out->p_buffer[12] = 1;
1893 out->p_buffer[13] = 0x00;
1895 memcpy( &out->p_buffer[14], p_data, i_payload );
1897 out->i_buffer = 14 + i_payload;
1898 out->i_dts = in->i_dts + i * in->i_length / i_count;
1899 out->i_length = in->i_length / i_count;
1901 rtp_packetize_send( id, out );
1903 p_data += i_payload;
1904 i_data -= i_payload;
1910 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1913 int i_max = id->i_mtu - 12; /* payload max in one packet */
1914 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1916 uint8_t *p_data = in->p_buffer;
1917 int i_data = in->i_buffer;
1920 for( i = 0; i < i_count; i++ )
1922 int i_payload = __MIN( i_max, i_data );
1923 block_t *out = block_New( p_stream, 12 + i_payload );
1925 /* rtp common header */
1926 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1927 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1928 memcpy( &out->p_buffer[12], p_data, i_payload );
1930 out->i_buffer = 12 + i_payload;
1931 out->i_dts = in->i_dts + i * in->i_length / i_count;
1932 out->i_length = in->i_length / i_count;
1934 rtp_packetize_send( id, out );
1936 p_data += i_payload;
1937 i_data -= i_payload;
1943 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1946 int i_max = id->i_mtu - 12; /* payload max in one packet */
1947 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1949 uint8_t *p_data = in->p_buffer;
1950 int i_data = in->i_buffer;
1955 int i_payload = (__MIN( i_max, i_data )/4)*4;
1956 block_t *out = block_New( p_stream, 12 + i_payload );
1958 /* rtp common header */
1959 rtp_packetize_common( id, out, 0,
1960 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1961 memcpy( &out->p_buffer[12], p_data, i_payload );
1963 out->i_buffer = 12 + i_payload;
1964 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1965 out->i_length = in->i_length / i_count;
1967 rtp_packetize_send( id, out );
1969 p_data += i_payload;
1970 i_data -= i_payload;
1977 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1980 int i_max = id->i_mtu - 12; /* payload max in one packet */
1981 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1983 uint8_t *p_data = in->p_buffer;
1984 int i_data = in->i_buffer;
1989 int i_payload = (__MIN( i_max, i_data )/2)*2;
1990 block_t *out = block_New( p_stream, 12 + i_payload );
1992 /* rtp common header */
1993 rtp_packetize_common( id, out, 0,
1994 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1995 memcpy( &out->p_buffer[12], p_data, i_payload );
1997 out->i_buffer = 12 + i_payload;
1998 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1999 out->i_length = in->i_length / i_count;
2001 rtp_packetize_send( id, out );
2003 p_data += i_payload;
2004 i_data -= i_payload;
2010 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2013 int i_max = id->i_mtu - 16; /* payload max in one packet */
2014 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2016 uint8_t *p_data = in->p_buffer;
2017 int i_data = in->i_buffer;
2020 for( i = 0; i < i_count; i++ )
2022 int i_payload = __MIN( i_max, i_data );
2023 block_t *out = block_New( p_stream, 16 + i_payload );
2025 /* rtp common header */
2026 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2027 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2029 /* AU headers length (bits) */
2030 out->p_buffer[12] = 0;
2031 out->p_buffer[13] = 2*8;
2032 /* for each AU length 13 bits + idx 3bits, */
2033 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2034 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2036 memcpy( &out->p_buffer[16], p_data, i_payload );
2038 out->i_buffer = 16 + i_payload;
2039 out->i_dts = in->i_dts + i * in->i_length / i_count;
2040 out->i_length = in->i_length / i_count;
2042 rtp_packetize_send( id, out );
2044 p_data += i_payload;
2045 i_data -= i_payload;
2053 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2054 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2055 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2058 uint8_t *p_data = in->p_buffer;
2059 int i_data = in->i_buffer;
2061 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2064 int b_v_bit = 0; // no pesky error resilience
2065 int i_plen = 0; // normally plen=0 for PSC packet
2066 int i_pebit = 0; // because plen=0
2071 return VLC_EGENERIC;
2073 if( p_data[0] || p_data[1] )
2075 return VLC_EGENERIC;
2077 /* remove 2 leading 0 bytes */
2080 i_count = ( i_data + i_max - 1 ) / i_max;
2082 for( i = 0; i < i_count; i++ )
2084 int i_payload = __MIN( i_max, i_data );
2085 block_t *out = block_New( p_stream,
2086 RTP_H263_PAYLOAD_START + i_payload );
2087 b_p_bit = (i == 0) ? 1 : 0;
2088 h = ( b_p_bit << 10 )|
2093 /* rtp common header */
2094 //b_m_bit = 1; // always contains end of frame
2095 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2096 in->i_pts > 0 ? in->i_pts : in->i_dts );
2099 out->p_buffer[12] = ( h >> 8 )&0xff;
2100 out->p_buffer[13] = ( h )&0xff;
2101 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2103 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2104 out->i_dts = in->i_dts + i * in->i_length / i_count;
2105 out->i_length = in->i_length / i_count;
2107 rtp_packetize_send( id, out );
2109 p_data += i_payload;
2110 i_data -= i_payload;