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_description( _("RTP stream output") );
88 set_capability( "sout stream", 0 );
89 add_shortcut( "rtp" );
91 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
92 DST_LONGTEXT, VLC_TRUE );
93 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
94 SDP_LONGTEXT, VLC_TRUE );
95 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
96 MUX_LONGTEXT, VLC_TRUE );
98 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
99 NAME_LONGTEXT, VLC_TRUE );
100 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
101 DESC_LONGTEXT, VLC_TRUE );
102 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
103 URL_LONGTEXT, VLC_TRUE );
104 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
105 EMAIL_LONGTEXT, VLC_TRUE );
107 add_integer( SOUT_CFG_PREFIX "port-audio", 1234, NULL, PORT_AUDIO_TEXT,
108 PORT_LONGTEXT, VLC_TRUE );
109 add_integer( SOUT_CFG_PREFIX "port-video", 1236, NULL, PORT_VIDEO_TEXT,
110 PORT_LONGTEXT, VLC_TRUE );
111 add_integer( SOUT_CFG_PREFIX "port", 1238, NULL, PORT_TEXT,
112 PORT_LONGTEXT, VLC_TRUE );
114 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
115 TTL_LONGTEXT, VLC_TRUE );
117 set_callbacks( Open, Close );
120 /*****************************************************************************
121 * Exported prototypes
122 *****************************************************************************/
123 static const char *ppsz_sout_options[] = {
124 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
125 "description", "url","email", NULL
128 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
129 static int Del ( sout_stream_t *, sout_stream_id_t * );
130 static int Send( sout_stream_t *, sout_stream_id_t *,
133 /* For unicast/interleaved streaming */
137 int64_t i_last; /* for timeout */
139 /* is it in "play" state */
140 vlc_bool_t b_playing;
142 /* output (id-access) */
144 sout_stream_id_t **id;
146 sout_access_out_t **access;
149 struct sout_stream_sys_t
155 vlc_mutex_t lock_sdp;
157 char *psz_session_name;
158 char *psz_session_description;
159 char *psz_session_url;
160 char *psz_session_email;
163 vlc_bool_t b_export_sdp_file;
166 vlc_bool_t b_export_sap;
167 session_descriptor_t *p_session;
169 httpd_host_t *p_httpd_host;
170 httpd_file_t *p_httpd_file;
172 httpd_host_t *p_rtsp_host;
173 httpd_url_t *p_rtsp_url;
174 char *psz_rtsp_control;
178 char *psz_destination;
184 /* when need to use a private one or when using muxer */
187 /* in case we do TS/PS over rtp */
189 sout_access_out_t *p_access;
191 sout_access_out_t *p_grab;
193 uint32_t i_timestamp_start;
200 sout_stream_id_t **es;
204 rtsp_client_t **rtsp;
207 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
210 struct sout_stream_id_t
212 sout_stream_t *p_stream;
214 uint8_t i_payload_type;
216 uint32_t i_timestamp_start;
223 char *psz_destination;
227 /* Packetizer specific fields */
228 pf_rtp_packetizer_t pf_packetize;
231 /* for sending the packets */
232 sout_access_out_t *p_access;
234 vlc_mutex_t lock_rtsp;
236 sout_access_out_t **rtsp_access;
239 sout_input_t *p_input;
241 /* RTSP url control */
242 httpd_url_t *p_rtsp_url;
245 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
247 static void SDPHandleUrl( sout_stream_t *, char * );
249 static int SapSetup( sout_stream_t *p_stream );
250 static int FileSetup( sout_stream_t *p_stream );
251 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
252 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
254 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
255 httpd_message_t *, httpd_message_t * );
256 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
257 httpd_message_t *, httpd_message_t * );
260 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
261 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
262 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
264 /*****************************************************************************
266 *****************************************************************************/
267 static int Open( vlc_object_t *p_this )
269 sout_stream_t *p_stream = (sout_stream_t*)p_this;
270 sout_instance_t *p_sout = p_stream->p_sout;
271 sout_stream_sys_t *p_sys;
274 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
276 p_sys = malloc( sizeof( sout_stream_sys_t ) );
278 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
279 if( *p_sys->psz_destination == '\0' )
281 free( p_sys->psz_destination );
282 p_sys->psz_destination = NULL;
285 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
286 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
287 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
288 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
290 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
291 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
292 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
294 p_sys->psz_sdp_file = NULL;
296 if( p_sys->i_port_audio == p_sys->i_port_video )
298 msg_Err( p_stream, "audio and video port cannot be the same" );
299 p_sys->i_port_audio = 0;
300 p_sys->i_port_video = 0;
303 if( !p_sys->psz_session_name )
305 if( p_sys->psz_destination )
306 p_sys->psz_session_name = strdup( p_sys->psz_destination );
308 p_sys->psz_session_name = strdup( "NONE" );
311 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
314 vlc_bool_t b_ok = VLC_FALSE;
316 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
318 if( !strcmp( p_cfg->psz_name, "sdp" ) )
320 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
330 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
331 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
333 free( val2.psz_string );
338 msg_Err( p_stream, "missing destination and not in rtsp mode" );
342 p_sys->psz_destination = NULL;
344 else if( p_sys->i_port <= 0 )
346 msg_Err( p_stream, "invalid port" );
351 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
352 p_sys->i_ttl = val.i_int;
354 p_sys->i_payload_type = 96;
359 p_sys->psz_sdp = NULL;
361 p_sys->i_sdp_id = mdate();
362 p_sys->i_sdp_version = 1;
363 p_sys->psz_sdp = NULL;
365 p_sys->b_export_sap = VLC_FALSE;
366 p_sys->b_export_sdp_file = VLC_FALSE;
367 p_sys->p_session = NULL;
369 p_sys->p_httpd_host = NULL;
370 p_sys->p_httpd_file = NULL;
371 p_sys->p_rtsp_host = NULL;
372 p_sys->p_rtsp_url = NULL;
373 p_sys->psz_rtsp_control = NULL;
374 p_sys->psz_rtsp_path = NULL;
376 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
377 vlc_mutex_init( p_stream, &p_sys->lock_es );
379 p_stream->pf_add = Add;
380 p_stream->pf_del = Del;
381 p_stream->pf_send = Send;
383 p_stream->p_sys = p_sys;
385 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
386 if( *val.psz_string )
388 sout_access_out_t *p_grab;
392 char url[p_sys->psz_destination ? strlen( p_sys->psz_destination ) + 1 + 12+1 : 14];
394 /* Check muxer type */
395 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
397 psz_rtpmap = "MP2P/90000";
399 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
401 psz_rtpmap = "MP2T/90000";
402 p_sys->i_payload_type = 33;
406 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
410 /* create the access out */
411 if( p_sys->i_ttl > 0 )
413 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
417 sprintf( access, "udp{raw}" );
419 sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
420 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
422 msg_Err( p_stream, "cannot create the access out for %s://%s",
427 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
428 if( p_sys->i_mtu <= 16 )
430 /* better than nothing */
434 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
435 p_grab = p_sys->p_grab =
436 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
437 p_grab->p_module = NULL;
438 p_grab->p_sout = p_sout;
439 p_grab->psz_access = strdup( "grab" );
440 p_grab->p_cfg = NULL;
441 p_grab->psz_name = strdup( "" );
442 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
443 p_grab->pf_seek = NULL;
444 p_grab->pf_write = AccessOutGrabberWrite;
447 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
449 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
450 sout_AccessOutDelete( p_sys->p_grab );
451 sout_AccessOutDelete( p_sys->p_access );
456 /* create the SDP for a muxed stream (only once) */
457 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
458 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
459 o= - should be local username (no spaces allowed)
460 o= time should be hashed with some other value to garantue uniqueness
461 o= we need IP6 support?
462 o= don't use the localhost address. use fully qualified domain name or IP4 address
463 p= international phone number (pass via vars?)
465 c= /ttl should only be added in case of multicast
466 a= recvonly (missing)
467 a= type:broadcast (missing)
468 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
469 a= x-plgroup: (missing)
470 RTP packets need to get the correct src IP address */
472 malloc( 10 + 30 + 10 + strlen( p_sys->psz_session_name ) +
473 10 + strlen( p_sys->psz_session_description ) + 10 + strlen( p_sys->psz_session_url ) +
474 10 + strlen( p_sys->psz_session_email ) + 10 + strlen( p_sys->psz_destination ) +
475 10 + 10 + strlen( PACKAGE_STRING ) +
476 20 + 10 + 20 + 10 + strlen( psz_rtpmap ) );
477 sprintf( p_sys->psz_sdp,
479 "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n"
484 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
485 "a=tool:"PACKAGE_STRING"\r\n"
487 "m=video %d RTP/AVP %d\r\n"
488 "a=rtpmap:%d %s\r\n",
489 p_sys->i_sdp_id, p_sys->i_sdp_version,
490 p_sys->psz_session_name,
491 p_sys->psz_session_description,
492 p_sys->psz_session_url,
493 p_sys->psz_session_email,
494 p_sys->psz_destination, p_sys->i_ttl,
495 p_sys->i_port, p_sys->i_payload_type,
496 p_sys->i_payload_type, psz_rtpmap );
497 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
499 /* create the rtp context */
500 p_sys->ssrc[0] = rand()&0xff;
501 p_sys->ssrc[1] = rand()&0xff;
502 p_sys->ssrc[2] = rand()&0xff;
503 p_sys->ssrc[3] = rand()&0xff;
504 p_sys->i_sequence = rand()&0xffff;
505 p_sys->i_timestamp_start = rand()&0xffffffff;
506 p_sys->packet = NULL;
511 p_sys->p_access = NULL;
512 p_sys->p_grab = NULL;
514 free( val.psz_string );
517 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
518 if( *val.psz_string )
522 SDPHandleUrl( p_stream, val.psz_string );
524 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
526 if( !strcmp( p_cfg->psz_name, "sdp" ) )
528 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
531 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
534 SDPHandleUrl( p_stream, p_cfg->psz_value );
538 free( val.psz_string );
540 /* update p_sout->i_out_pace_nocontrol */
541 p_stream->p_sout->i_out_pace_nocontrol++;
546 /*****************************************************************************
548 *****************************************************************************/
549 static void Close( vlc_object_t * p_this )
551 sout_stream_t *p_stream = (sout_stream_t*)p_this;
552 sout_stream_sys_t *p_sys = p_stream->p_sys;
554 /* update p_sout->i_out_pace_nocontrol */
555 p_stream->p_sout->i_out_pace_nocontrol--;
559 sout_MuxDelete( p_sys->p_mux );
560 sout_AccessOutDelete( p_sys->p_access );
561 sout_AccessOutDelete( p_sys->p_grab );
564 block_Release( p_sys->packet );
568 while( p_sys->i_rtsp > 0 )
570 RtspClientDel( p_stream, p_sys->rtsp[0] );
573 vlc_mutex_destroy( &p_sys->lock_sdp );
575 if( p_sys->p_httpd_file )
577 httpd_FileDelete( p_sys->p_httpd_file );
579 if( p_sys->p_httpd_host )
581 httpd_HostDelete( p_sys->p_httpd_host );
583 if( p_sys->p_rtsp_url )
585 httpd_UrlDelete( p_sys->p_rtsp_url );
587 if( p_sys->p_rtsp_host )
589 httpd_HostDelete( p_sys->p_rtsp_host );
592 /* why? is this disabled? */
593 if( p_sys->psz_session_name )
595 free( p_sys->psz_session_name );
596 p_sys->psz_session_name = NULL;
598 if( p_sys->psz_session_description )
600 free( p_sys->psz_session_description );
601 p_sys->psz_session_description = NULL;
603 if( p_sys->psz_session_url )
605 free( p_sys->psz_session_url );
606 p_sys->psz_session_url = NULL;
608 if( p_sys->psz_session_email )
610 free( p_sys->psz_session_email );
611 p_sys->psz_session_email = NULL;
616 free( p_sys->psz_sdp );
621 /*****************************************************************************
623 *****************************************************************************/
624 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
626 sout_stream_sys_t *p_sys = p_stream->p_sys;
629 vlc_UrlParse( &url, psz_url, 0 );
630 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
632 if( p_sys->p_httpd_file )
634 msg_Err( p_stream, "You can used sdp=http:// only once" );
638 if( HttpSetup( p_stream, &url ) )
640 msg_Err( p_stream, "cannot export sdp as http" );
643 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
645 if( p_sys->p_rtsp_url )
647 msg_Err( p_stream, "You can used sdp=rtsp:// only once" );
651 /* FIXME test if destination is multicast or no destination at all FIXME */
652 if( RtspSetup( p_stream, &url ) )
654 msg_Err( p_stream, "cannot export sdp as rtsp" );
657 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
658 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
660 p_sys->b_export_sap = VLC_TRUE;
661 SapSetup( p_stream );
663 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
665 if( p_sys->b_export_sdp_file )
667 msg_Err( p_stream, "You can used sdp=file:// only once" );
670 p_sys->b_export_sdp_file = VLC_TRUE;
671 psz_url = &psz_url[5];
672 if( psz_url[0] == '/' && psz_url[1] == '/' )
674 p_sys->psz_sdp_file = strdup( psz_url );
678 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
681 vlc_UrlClean( &url );
684 /*****************************************************************************
686 *****************************************************************************/
687 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
688 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
689 o= - should be local username (no spaces allowed)
690 o= time should be hashed with some other value to garantue uniqueness
691 o= we need IP6 support?
692 o= don't use the localhost address. use fully qualified domain name or IP4 address
693 p= international phone number (pass via vars?)
695 c= /ttl should only be added in case of multicast
696 a= recvonly (missing)
697 a= type:broadcast (missing)
698 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
699 a= x-plgroup: (missing)
700 RTP packets need to get the correct src IP address */
701 static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
703 sout_stream_sys_t *p_sys = p_stream->p_sys;
708 i_size = strlen( "v=0\r\n" ) +
709 strlen( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
710 strlen( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
711 strlen( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
712 strlen( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
713 strlen( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
714 strlen( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
715 strlen( "a=tool:"PACKAGE_STRING"\r\n" ) +
716 strlen( "c=IN IP4 */*\r\n" ) + 20 + 10 +
717 strlen( psz_destination ? psz_destination : "0.0.0.0") ;
718 for( i = 0; i < p_sys->i_es; i++ )
720 sout_stream_id_t *id = p_sys->es[i];
722 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
725 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
729 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
733 i_size += strlen( "a=control:*/trackid=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
737 p = psz_sdp = malloc( i_size );
738 p += sprintf( p, "v=0\r\n" );
739 p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\r\n",
740 p_sys->i_sdp_id, p_sys->i_sdp_version );
741 if( *p_sys->psz_session_name )
742 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
743 if( *p_sys->psz_session_description )
744 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
745 if( *p_sys->psz_session_url )
746 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
747 if( *p_sys->psz_session_email )
748 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
750 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
751 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
753 p += sprintf( p, "c=IN IP4 %s/%d\r\n", psz_destination ? psz_destination : "0.0.0.0",
756 for( i = 0; i < p_sys->i_es; i++ )
758 sout_stream_id_t *id = p_sys->es[i];
760 if( id->i_cat == AUDIO_ES )
762 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
763 id->i_port, id->i_payload_type );
765 else if( id->i_cat == VIDEO_ES )
767 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
768 id->i_port, id->i_payload_type );
776 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
781 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
786 p += sprintf( p, "a=control:%s/trackid=%d\r\n", p_sys->psz_rtsp_control, i );
793 /*****************************************************************************
795 *****************************************************************************/
796 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
797 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
798 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
799 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
800 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
801 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
802 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
803 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
805 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
807 static const char hex[16] = "0123456789abcdef";
810 for( i = 0; i < i_data; i++ )
812 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
813 s[2*i+1] = hex[(p_data[i] )&0xf];
818 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
820 sout_instance_t *p_sout = p_stream->p_sout;
821 sout_stream_sys_t *p_sys = p_stream->p_sys;
822 sout_stream_id_t *id;
823 sout_access_out_t *p_access = NULL;
827 if( p_sys->p_mux != NULL )
829 sout_input_t *p_input = NULL;
830 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
832 msg_Err( p_stream, "cannot add this stream to the muxer" );
836 id = malloc( sizeof( sout_stream_id_t ) );
837 memset( id, 0, sizeof( sout_stream_id_t ) );
839 id->p_input = p_input;
840 id->pf_packetize= NULL;
841 id->p_rtsp_url = NULL;
847 /* Choose the port */
849 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
851 i_port = p_sys->i_port_audio;
852 p_sys->i_port_audio = 0;
854 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
856 i_port = p_sys->i_port_video;
857 p_sys->i_port_video = 0;
861 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
863 i_port = p_sys->i_port;
870 if( p_sys->psz_destination )
873 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
875 /* first try to create the access out */
876 if( p_sys->i_ttl > 0 )
878 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
882 sprintf( access, "udp{raw}" );
884 sprintf( url, "%s:%d", p_sys->psz_destination, i_port );
885 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
887 msg_Err( p_stream, "cannot create the access out for %s://%s",
891 msg_Dbg( p_stream, "access out %s:%s", access, url );
894 /* not create the rtp specific stuff */
895 id = malloc( sizeof( sout_stream_id_t ) );
896 memset( id, 0, sizeof( sout_stream_id_t ) );
897 id->p_stream = p_stream;
898 id->p_access = p_access;
900 id->psz_rtpmap = NULL;
902 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
904 id->p_rtsp_url = NULL;
905 vlc_mutex_init( p_stream, &id->lock_rtsp );
906 id->i_rtsp_access = 0;
907 id->rtsp_access = NULL;
909 switch( p_fmt->i_codec )
911 case VLC_FOURCC( 's', '1', '6', 'b' ):
912 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
914 id->i_payload_type = 11;
916 else if( p_fmt->audio.i_channels == 2 &&
917 p_fmt->audio.i_rate == 44100 )
919 id->i_payload_type = 10;
923 id->i_payload_type = p_sys->i_payload_type++;
925 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
926 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
927 p_fmt->audio.i_channels );
928 id->i_clock_rate = p_fmt->audio.i_rate;
929 id->pf_packetize = rtp_packetize_l16;
931 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
932 id->i_payload_type = p_sys->i_payload_type++;
933 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
934 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
935 p_fmt->audio.i_channels );
936 id->i_clock_rate = p_fmt->audio.i_rate;
937 id->pf_packetize = rtp_packetize_l8;
939 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
940 id->i_payload_type = 14;
941 id->i_clock_rate = 90000;
942 id->psz_rtpmap = strdup( "MPA/90000" );
943 id->pf_packetize = rtp_packetize_mpa;
945 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
946 id->i_payload_type = 32;
947 id->i_clock_rate = 90000;
948 id->psz_rtpmap = strdup( "MPV/90000" );
949 id->pf_packetize = rtp_packetize_mpv;
951 case VLC_FOURCC( 'a', '5', '2', ' ' ):
952 id->i_payload_type = p_sys->i_payload_type++;
953 id->i_clock_rate = 90000;
954 id->psz_rtpmap = strdup( "ac3/90000" );
955 id->pf_packetize = rtp_packetize_ac3;
957 case VLC_FOURCC( 'H', '2', '6', '3' ):
958 id->i_payload_type = p_sys->i_payload_type++;
959 id->i_clock_rate = 90000;
960 id->psz_rtpmap = strdup( "H263-1998/90000" );
961 id->pf_packetize = rtp_packetize_h263;
964 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
966 char hexa[2*p_fmt->i_extra +1];
968 id->i_payload_type = p_sys->i_payload_type++;
969 id->i_clock_rate = 90000;
970 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
971 id->pf_packetize = rtp_packetize_split;
972 if( p_fmt->i_extra > 0 )
974 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
975 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
976 sprintf( id->psz_fmtp,
977 "profile-level-id=3; config=%s;", hexa );
981 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
983 char hexa[2*p_fmt->i_extra +1];
985 id->i_payload_type = p_sys->i_payload_type++;
986 id->i_clock_rate = p_fmt->audio.i_rate;
987 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
988 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
989 id->pf_packetize = rtp_packetize_mp4a;
990 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
991 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
992 sprintf( id->psz_fmtp,
993 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
994 "config=%s; SizeLength=13;IndexLength=3; "
995 "IndexDeltaLength=3; Profile=1;", hexa );
1000 msg_Err( p_stream, "cannot add this stream (unsupported "
1001 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1004 sout_AccessOutDelete( p_access );
1009 id->i_cat = p_fmt->i_cat;
1011 id->ssrc[0] = rand()&0xff;
1012 id->ssrc[1] = rand()&0xff;
1013 id->ssrc[2] = rand()&0xff;
1014 id->ssrc[3] = rand()&0xff;
1015 id->i_sequence = rand()&0xffff;
1016 id->i_timestamp_start = rand()&0xffffffff;
1018 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1019 if( id->i_mtu <= 16 )
1021 /* better than nothing */
1024 msg_Dbg( p_stream, "using mtu=%d", id->i_mtu );
1026 if( p_sys->p_rtsp_url )
1028 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1030 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1031 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
1032 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
1034 if( id->p_rtsp_url )
1036 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1037 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1038 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1043 /* Update p_sys context */
1044 vlc_mutex_lock( &p_sys->lock_es );
1045 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1046 vlc_mutex_unlock( &p_sys->lock_es );
1048 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1050 vlc_mutex_lock( &p_sys->lock_sdp );
1051 free( p_sys->psz_sdp );
1052 p_sys->psz_sdp = psz_sdp;
1053 vlc_mutex_unlock( &p_sys->lock_sdp );
1055 p_sys->i_sdp_version++;
1057 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
1059 /* Update SDP (sap/file) */
1060 if( p_sys->b_export_sap ) SapSetup( p_stream );
1061 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1066 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1068 sout_stream_sys_t *p_sys = p_stream->p_sys;
1070 vlc_mutex_lock( &p_sys->lock_es );
1071 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1072 vlc_mutex_unlock( &p_sys->lock_es );
1075 if( id->i_port > 0 )
1077 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1078 p_sys->i_port_audio = id->i_port;
1079 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1080 p_sys->i_port_video = id->i_port;
1085 if( id->psz_rtpmap )
1087 free( id->psz_rtpmap );
1091 free( id->psz_fmtp );
1093 if( id->psz_destination )
1094 free( id->psz_destination );
1095 sout_AccessOutDelete( id->p_access );
1097 else if( id->p_input )
1099 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1101 if( id->p_rtsp_url )
1103 httpd_UrlDelete( id->p_rtsp_url );
1105 vlc_mutex_destroy( &id->lock_rtsp );
1106 if( id->rtsp_access ) free( id->rtsp_access );
1108 /* Update SDP (sap/file) */
1109 if( p_sys->b_export_sap ) SapSetup( p_stream );
1110 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1116 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1121 if( p_stream->p_sys->p_mux )
1123 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1129 p_next = p_buffer->p_next;
1130 if( id->pf_packetize( p_stream, id, p_buffer ) )
1134 block_Release( p_buffer );
1142 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1145 sout_stream_sys_t *p_sys = p_stream->p_sys;
1147 int64_t i_dts = p_buffer->i_dts;
1148 uint32_t i_timestamp = i_dts * 9 / 100;
1150 uint8_t *p_data = p_buffer->p_buffer;
1151 unsigned int i_data = p_buffer->i_buffer;
1152 unsigned int i_max = p_sys->i_mtu - 12;
1154 int i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1158 unsigned int i_size;
1160 /* output complete packet */
1161 if( p_sys->packet &&
1162 p_sys->packet->i_buffer + i_data > i_max )
1164 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1165 p_sys->packet = NULL;
1168 if( p_sys->packet == NULL )
1170 /* allocate a new packet */
1171 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1172 p_sys->packet->p_buffer[ 0] = 0x80;
1173 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1174 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1175 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1176 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1177 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1178 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1179 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1180 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1181 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1182 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1183 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1184 p_sys->packet->i_buffer = 12;
1186 p_sys->packet->i_dts = i_dts;
1187 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1188 i_dts += p_sys->packet->i_length;
1190 p_sys->i_sequence++;
1193 i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_buffer );
1195 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1198 p_sys->packet->i_buffer += i_size;
1206 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1209 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1211 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1217 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1219 p_next = p_buffer->p_next;
1220 block_Release( p_buffer );
1227 /****************************************************************************
1229 ****************************************************************************/
1230 static int SapSetup( sout_stream_t *p_stream )
1232 sout_stream_sys_t *p_sys = p_stream->p_sys;
1233 sout_instance_t *p_sout = p_stream->p_sout;
1234 announce_method_t *p_method = (announce_method_t *)
1235 malloc(sizeof(announce_method_t));
1237 /* Remove the previous session */
1238 if( p_sys->p_session != NULL)
1240 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1241 sout_AnnounceSessionDestroy( p_sys->p_session );
1242 p_sys->p_session = NULL;
1244 p_method->i_type = METHOD_TYPE_SAP;
1245 p_method->psz_address = NULL; /* FIXME */
1246 p_method->i_ip_version = 4; /* FIXME ! */
1248 if( p_sys->i_es > 0 && p_sys->psz_sdp && *p_sys->psz_sdp )
1250 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1258 /****************************************************************************
1260 ****************************************************************************/
1261 static int FileSetup( sout_stream_t *p_stream )
1263 sout_stream_sys_t *p_sys = p_stream->p_sys;
1266 if( ( f = fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1268 msg_Err( p_stream, "cannot open file '%s' (%s)",
1269 p_sys->psz_sdp_file, strerror(errno) );
1270 return VLC_EGENERIC;
1273 fprintf( f, "%s", p_sys->psz_sdp );
1279 /****************************************************************************
1281 ****************************************************************************/
1282 static int HttpCallback( httpd_file_sys_t *p_args,
1283 httpd_file_t *, uint8_t *p_request,
1284 uint8_t **pp_data, int *pi_data );
1286 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1288 sout_stream_sys_t *p_sys = p_stream->p_sys;
1290 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
1291 if( p_sys->p_httpd_host )
1293 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1294 url->psz_path ? url->psz_path : "/",
1297 HttpCallback, (void*)p_sys );
1299 if( p_sys->p_httpd_file == NULL )
1301 return VLC_EGENERIC;
1306 static int HttpCallback( httpd_file_sys_t *p_args,
1307 httpd_file_t *f, uint8_t *p_request,
1308 uint8_t **pp_data, int *pi_data )
1310 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1312 vlc_mutex_lock( &p_sys->lock_sdp );
1313 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1315 *pi_data = strlen( p_sys->psz_sdp );
1316 *pp_data = malloc( *pi_data );
1317 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1324 vlc_mutex_unlock( &p_sys->lock_sdp );
1329 /****************************************************************************
1331 ****************************************************************************/
1332 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1334 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1336 rtsp->psz_session = psz_session;
1338 rtsp->b_playing = VLC_FALSE;
1342 rtsp->access = NULL;
1344 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1348 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1351 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1353 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1355 return p_stream->p_sys->rtsp[i];
1361 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1364 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1366 for( i = 0; i < rtsp->i_access; i++ )
1368 sout_AccessOutDelete( rtsp->access[i] );
1370 if( rtsp->id ) free( rtsp->id );
1371 if( rtsp->access ) free( rtsp->access );
1373 free( rtsp->psz_session );
1377 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1379 sout_stream_sys_t *p_sys = p_stream->p_sys;
1381 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1383 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1384 if( p_sys->p_rtsp_host == NULL )
1386 return VLC_EGENERIC;
1389 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1390 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1391 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1392 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1394 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
1395 if( p_sys->p_rtsp_url == 0 )
1397 return VLC_EGENERIC;
1399 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1400 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1401 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1402 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1407 static int RtspCallback( httpd_callback_sys_t *p_args,
1409 httpd_message_t *answer, httpd_message_t *query )
1411 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1412 sout_stream_sys_t *p_sys = p_stream->p_sys;
1413 char *psz_destination = p_sys->psz_destination;
1414 char *psz_session = NULL;
1416 if( answer == NULL || query == NULL )
1420 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1422 answer->i_proto = HTTPD_PROTO_RTSP;
1423 answer->i_version= query->i_version;
1424 answer->i_type = HTTPD_MSG_ANSWER;
1426 switch( query->i_type )
1428 case HTTPD_MSG_DESCRIBE:
1430 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1432 answer->i_status = 200;
1433 answer->psz_status = strdup( "OK" );
1434 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1436 answer->p_body = psz_sdp;
1437 answer->i_body = strlen( psz_sdp );
1441 case HTTPD_MSG_PLAY:
1443 rtsp_client_t *rtsp;
1444 /* for now only multicast so easy */
1445 answer->i_status = 200;
1446 answer->psz_status = strdup( "OK" );
1448 answer->p_body = NULL;
1450 psz_session = httpd_MsgGet( query, "Session" );
1451 rtsp = RtspClientGet( p_stream, psz_session );
1452 if( rtsp && !rtsp->b_playing )
1456 rtsp->b_playing = VLC_TRUE;
1458 vlc_mutex_lock( &p_sys->lock_es );
1459 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1461 sout_stream_id_t *id = rtsp->id[i_id];
1464 for( i = 0; i < p_sys->i_es; i++ )
1466 if( id == p_sys->es[i] )
1469 if( i >= p_sys->i_es ) continue;
1471 vlc_mutex_lock( &id->lock_rtsp );
1472 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1473 vlc_mutex_unlock( &id->lock_rtsp );
1475 vlc_mutex_unlock( &p_sys->lock_es );
1479 case HTTPD_MSG_PAUSE:
1481 return VLC_EGENERIC;
1482 case HTTPD_MSG_TEARDOWN:
1484 rtsp_client_t *rtsp;
1486 /* for now only multicast so easy again */
1487 answer->i_status = 200;
1488 answer->psz_status = strdup( "OK" );
1490 answer->p_body = NULL;
1492 psz_session = httpd_MsgGet( query, "Session" );
1493 rtsp = RtspClientGet( p_stream, psz_session );
1498 vlc_mutex_lock( &p_sys->lock_es );
1499 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1501 sout_stream_id_t *id = rtsp->id[i_id];
1504 for( i = 0; i < p_sys->i_es; i++ )
1506 if( id == p_sys->es[i] )
1509 if( i >= p_sys->i_es ) continue;
1511 vlc_mutex_lock( &id->lock_rtsp );
1512 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1513 vlc_mutex_unlock( &id->lock_rtsp );
1515 vlc_mutex_unlock( &p_sys->lock_es );
1517 RtspClientDel( p_stream, rtsp );
1523 return VLC_EGENERIC;
1525 httpd_MsgAdd( answer, "Server", "VLC Server" );
1526 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1527 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1528 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1532 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1537 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1539 httpd_message_t *answer, httpd_message_t *query )
1541 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1542 sout_stream_t *p_stream = id->p_stream;
1543 sout_stream_sys_t *p_sys = p_stream->p_sys;
1544 char *psz_session = NULL;
1546 if( answer == NULL || query == NULL )
1550 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1552 answer->i_proto = HTTPD_PROTO_RTSP;
1553 answer->i_version= query->i_version;
1554 answer->i_type = HTTPD_MSG_ANSWER;
1556 switch( query->i_type )
1558 case HTTPD_MSG_SETUP:
1560 char *psz_transport = httpd_MsgGet( query, "Transport" );
1562 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1564 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1566 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1567 answer->i_status = 200;
1568 answer->psz_status = strdup( "OK" );
1570 answer->p_body = NULL;
1571 psz_session = httpd_MsgGet( query, "Session" );
1572 if( *psz_session == 0 )
1574 psz_session = malloc( 100 );
1575 sprintf( psz_session, "%d", rand() );
1577 httpd_MsgAdd( answer, "Transport",
1578 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1579 id->psz_destination, id->i_port,id->i_port+1, p_sys->i_ttl );
1581 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1583 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1584 char *ip = httpd_ClientIP( cl );
1586 char psz_access[100];
1589 sout_access_out_t *p_access;
1591 rtsp_client_t *rtsp = NULL;
1595 answer->i_status = 400;
1596 answer->psz_status = strdup( "Internal server error" );
1598 answer->p_body = NULL;
1602 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1605 psz_session = httpd_MsgGet( query, "Session" );
1606 if( *psz_session == 0 )
1608 psz_session = malloc( 100 );
1609 sprintf( psz_session, "%d", rand() );
1611 rtsp = RtspClientNew( p_stream, psz_session );
1615 rtsp = RtspClientGet( p_stream, psz_session );
1618 /* FIXME right error code */
1619 answer->i_status = 400;
1620 answer->psz_status = strdup( "Unknown session id" );
1622 answer->p_body = NULL;
1628 /* first try to create the access out */
1629 if( p_sys->i_ttl > 0 )
1630 sprintf( psz_access, "udp{raw,ttl=%d}", p_sys->i_ttl );
1632 sprintf( psz_access, "udp{raw}" );
1633 sprintf( psz_url, "%s:%d", ip, i_port );
1636 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1638 msg_Err( p_stream, "cannot create the access out for %s://%s",
1639 psz_access, psz_url );
1640 answer->i_status = 400;
1641 answer->psz_status = strdup( "Server internal error" );
1643 answer->p_body = NULL;
1647 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1648 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1650 answer->i_status = 200;
1651 answer->psz_status = strdup( "OK" );
1653 answer->p_body = NULL;
1655 httpd_MsgAdd( answer, "Transport",
1656 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1658 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1660 answer->i_status = 400;
1661 answer->psz_status = strdup( "Bad Request" );
1663 answer->p_body = NULL;
1669 return VLC_EGENERIC;
1671 httpd_MsgAdd( answer, "Server", "VLC Server" );
1672 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1673 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1674 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1678 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1683 /****************************************************************************
1685 ****************************************************************************/
1686 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1687 int b_marker, int64_t i_pts )
1689 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1691 out->p_buffer[0] = 0x80;
1692 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1693 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1694 out->p_buffer[3] = ( id->i_sequence )&0xff;
1695 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1696 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1697 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1698 out->p_buffer[7] = ( i_timestamp )&0xff;
1700 out->p_buffer[ 8] = id->ssrc[0];
1701 out->p_buffer[ 9] = id->ssrc[1];
1702 out->p_buffer[10] = id->ssrc[2];
1703 out->p_buffer[11] = id->ssrc[3];
1709 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1712 vlc_mutex_lock( &id->lock_rtsp );
1713 for( i = 0; i < id->i_rtsp_access; i++ )
1715 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1717 vlc_mutex_unlock( &id->lock_rtsp );
1721 sout_AccessOutWrite( id->p_access, out );
1725 block_Release( out );
1729 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1732 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1733 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1735 uint8_t *p_data = in->p_buffer;
1736 int i_data = in->i_buffer;
1739 for( i = 0; i < i_count; i++ )
1741 int i_payload = __MIN( i_max, i_data );
1742 block_t *out = block_New( p_stream, 16 + i_payload );
1744 /* rtp common header */
1745 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1747 out->p_buffer[12] = 0;
1748 out->p_buffer[13] = 0;
1749 /* fragment offset in the current frame */
1750 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1751 out->p_buffer[15] = ( (i*i_max) )&0xff;
1752 memcpy( &out->p_buffer[16], p_data, i_payload );
1754 out->i_buffer = 16 + i_payload;
1755 out->i_dts = in->i_dts + i * in->i_length / i_count;
1756 out->i_length = in->i_length / i_count;
1758 rtp_packetize_send( id, out );
1760 p_data += i_payload;
1761 i_data -= i_payload;
1768 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1771 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1772 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1774 uint8_t *p_data = in->p_buffer;
1775 int i_data = in->i_buffer;
1777 int b_sequence_start = 0;
1778 int i_temporal_ref = 0;
1779 int i_picture_coding_type = 0;
1780 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1781 int b_start_slice = 0;
1783 /* preparse this packet to get some info */
1784 if( in->i_buffer > 4 )
1786 uint8_t *p = p_data;
1787 int i_rest = in->i_buffer;
1791 while( i_rest > 4 &&
1792 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1806 /* sequence start code */
1807 b_sequence_start = 1;
1809 else if( *p == 0x00 && i_rest >= 4 )
1812 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1813 i_picture_coding_type = (p[2] >> 3)&0x07;
1815 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1816 i_picture_coding_type == 3 ) )
1818 i_ffv = (p[3] >> 2)&0x01;
1819 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1820 if( i_rest > 4 && i_picture_coding_type == 3 )
1822 i_fbv = (p[4]>>6)&0x01;
1823 i_bfc = (p[4]>>3)&0x07;
1827 else if( *p <= 0xaf )
1834 for( i = 0; i < i_count; i++ )
1836 int i_payload = __MIN( i_max, i_data );
1837 block_t *out = block_New( p_stream,
1839 uint32_t h = ( i_temporal_ref << 16 )|
1840 ( b_sequence_start << 13 )|
1841 ( b_start_slice << 12 )|
1842 ( i == i_count - 1 ? 1 << 11 : 0 )|
1843 ( i_picture_coding_type << 8 )|
1844 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1846 /* rtp common header */
1847 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1848 in->i_pts > 0 ? in->i_pts : in->i_dts );
1850 /* 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 */
1851 out->p_buffer[12] = ( h >> 24 )&0xff;
1852 out->p_buffer[13] = ( h >> 16 )&0xff;
1853 out->p_buffer[14] = ( h >> 8 )&0xff;
1854 out->p_buffer[15] = ( h )&0xff;
1856 memcpy( &out->p_buffer[16], p_data, i_payload );
1858 out->i_buffer = 16 + i_payload;
1859 out->i_dts = in->i_dts + i * in->i_length / i_count;
1860 out->i_length = in->i_length / i_count;
1862 rtp_packetize_send( id, out );
1864 p_data += i_payload;
1865 i_data -= i_payload;
1870 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1873 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1874 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1876 uint8_t *p_data = in->p_buffer;
1877 int i_data = in->i_buffer;
1880 for( i = 0; i < i_count; i++ )
1882 int i_payload = __MIN( i_max, i_data );
1883 block_t *out = block_New( p_stream, 14 + i_payload );
1885 /* rtp common header */
1886 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1888 out->p_buffer[12] = 1;
1890 out->p_buffer[13] = 0x00;
1892 memcpy( &out->p_buffer[14], p_data, i_payload );
1894 out->i_buffer = 14 + i_payload;
1895 out->i_dts = in->i_dts + i * in->i_length / i_count;
1896 out->i_length = in->i_length / i_count;
1898 rtp_packetize_send( id, out );
1900 p_data += i_payload;
1901 i_data -= i_payload;
1907 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1910 int i_max = id->i_mtu - 12; /* payload max in one packet */
1911 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1913 uint8_t *p_data = in->p_buffer;
1914 int i_data = in->i_buffer;
1917 for( i = 0; i < i_count; i++ )
1919 int i_payload = __MIN( i_max, i_data );
1920 block_t *out = block_New( p_stream, 12 + i_payload );
1922 /* rtp common header */
1923 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1924 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1925 memcpy( &out->p_buffer[12], p_data, i_payload );
1927 out->i_buffer = 12 + i_payload;
1928 out->i_dts = in->i_dts + i * in->i_length / i_count;
1929 out->i_length = in->i_length / i_count;
1931 rtp_packetize_send( id, out );
1933 p_data += i_payload;
1934 i_data -= i_payload;
1940 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1943 int i_max = id->i_mtu - 12; /* payload max in one packet */
1944 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1946 uint8_t *p_data = in->p_buffer;
1947 int i_data = in->i_buffer;
1952 int i_payload = (__MIN( i_max, i_data )/4)*4;
1953 block_t *out = block_New( p_stream, 12 + i_payload );
1955 /* rtp common header */
1956 rtp_packetize_common( id, out, 0,
1957 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1958 memcpy( &out->p_buffer[12], p_data, i_payload );
1960 out->i_buffer = 12 + i_payload;
1961 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1962 out->i_length = in->i_length / i_count;
1964 rtp_packetize_send( id, out );
1966 p_data += i_payload;
1967 i_data -= i_payload;
1974 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1977 int i_max = id->i_mtu - 12; /* payload max in one packet */
1978 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1980 uint8_t *p_data = in->p_buffer;
1981 int i_data = in->i_buffer;
1986 int i_payload = (__MIN( i_max, i_data )/2)*2;
1987 block_t *out = block_New( p_stream, 12 + i_payload );
1989 /* rtp common header */
1990 rtp_packetize_common( id, out, 0,
1991 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1992 memcpy( &out->p_buffer[12], p_data, i_payload );
1994 out->i_buffer = 12 + i_payload;
1995 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1996 out->i_length = in->i_length / i_count;
1998 rtp_packetize_send( id, out );
2000 p_data += i_payload;
2001 i_data -= i_payload;
2007 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2010 int i_max = id->i_mtu - 16; /* payload max in one packet */
2011 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2013 uint8_t *p_data = in->p_buffer;
2014 int i_data = in->i_buffer;
2017 for( i = 0; i < i_count; i++ )
2019 int i_payload = __MIN( i_max, i_data );
2020 block_t *out = block_New( p_stream, 16 + i_payload );
2022 /* rtp common header */
2023 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2024 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2026 /* AU headers length (bits) */
2027 out->p_buffer[12] = 0;
2028 out->p_buffer[13] = 2*8;
2029 /* for each AU length 13 bits + idx 3bits, */
2030 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2031 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2033 memcpy( &out->p_buffer[16], p_data, i_payload );
2035 out->i_buffer = 16 + i_payload;
2036 out->i_dts = in->i_dts + i * in->i_length / i_count;
2037 out->i_length = in->i_length / i_count;
2039 rtp_packetize_send( id, out );
2041 p_data += i_payload;
2042 i_data -= i_payload;
2050 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2051 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2052 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2055 uint8_t *p_data = in->p_buffer;
2056 int i_data = in->i_buffer;
2058 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2061 int b_v_bit = 0; // no pesky error resilience
2062 int i_plen = 0; // normally plen=0 for PSC packet
2063 int i_pebit = 0; // because plen=0
2068 return VLC_EGENERIC;
2070 if( p_data[0] || p_data[1] )
2072 return VLC_EGENERIC;
2074 /* remove 2 leading 0 bytes */
2077 i_count = ( i_data + i_max - 1 ) / i_max;
2079 for( i = 0; i < i_count; i++ )
2081 int i_payload = __MIN( i_max, i_data );
2082 block_t *out = block_New( p_stream,
2083 RTP_H263_PAYLOAD_START + i_payload );
2084 b_p_bit = (i == 0) ? 1 : 0;
2085 h = ( b_p_bit << 10 )|
2090 /* rtp common header */
2091 //b_m_bit = 1; // always contains end of frame
2092 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2093 in->i_pts > 0 ? in->i_pts : in->i_dts );
2096 out->p_buffer[12] = ( h >> 8 )&0xff;
2097 out->p_buffer[13] = ( h )&0xff;
2098 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2100 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2101 out->i_dts = in->i_dts + i * in->i_length / i_count;
2102 out->i_length = in->i_length / i_count;
2104 rtp_packetize_send( id, out );
2106 p_data += i_payload;
2107 i_data -= i_payload;