1 /*****************************************************************************
2 * rtp.c: rtp stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
35 #include "vlc_httpd.h"
40 /*****************************************************************************
42 *****************************************************************************/
46 #define DST_TEXT N_("Destination")
47 #define DST_LONGTEXT N_( \
48 "This is the output URL that will be used." )
49 #define SDP_TEXT N_("SDP")
50 #define SDP_LONGTEXT N_( \
51 "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
52 "session will be made available. You must use an url: http://location to " \
53 "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
54 "for the SDP to be announced via SAP." )
55 #define MUX_TEXT N_("Muxer")
56 #define MUX_LONGTEXT N_( \
57 "This allows you to specify the muxer used for the streaming output. " \
58 "Default is to use no muxer (standard RTP stream)." )
60 #define NAME_TEXT N_("Session name")
61 #define NAME_LONGTEXT N_( \
62 "This is the name of the session that will be announced in the SDP " \
63 "(Session Descriptor)." )
64 #define DESC_TEXT N_("Session description")
65 #define DESC_LONGTEXT N_( \
66 "This allows you to give a broader description of the stream, that will " \
67 "be announced in the SDP (Session Descriptor)." )
68 #define URL_TEXT N_("Session URL")
69 #define URL_LONGTEXT N_( \
70 "This allows you to give an URL with more details about the stream " \
71 "(often the website of the streaming organization), that will " \
72 "be announced in the SDP (Session Descriptor)." )
73 #define EMAIL_TEXT N_("Session email")
74 #define EMAIL_LONGTEXT N_( \
75 "This allows you to give a contact mail address for the stream, that will " \
76 "be announced in the SDP (Session Descriptor)." )
77 #define PORT_TEXT N_("Port")
78 #define PORT_LONGTEXT N_( \
79 "This allows you to specify the base port for the RTP streaming." )
80 #define PORT_AUDIO_TEXT N_("Audio port")
81 #define PORT_AUDIO_LONGTEXT N_( \
82 "This allows you to specify the default audio port for the RTP streaming." )
83 #define PORT_VIDEO_TEXT N_("Video port")
84 #define PORT_VIDEO_LONGTEXT N_( \
85 "This allows you to specify the default video port for the RTP streaming." )
87 #define TTL_TEXT N_("Time-To-Live (TTL)")
88 #define TTL_LONGTEXT N_( \
89 "This allows you to specify the Time-To-Live for the output stream." )
91 #define RFC3016_TEXT N_("MP4A LATM")
92 #define RFC3016_LONGTEXT N_( \
93 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
95 static int Open ( vlc_object_t * );
96 static void Close( vlc_object_t * );
98 #define SOUT_CFG_PREFIX "sout-rtp-"
101 set_shortname( _("RTP"));
102 set_description( _("RTP stream output") );
103 set_capability( "sout stream", 0 );
104 add_shortcut( "rtp" );
105 set_category( CAT_SOUT );
106 set_subcategory( SUBCAT_SOUT_STREAM );
108 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
109 DST_LONGTEXT, VLC_TRUE );
110 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
111 SDP_LONGTEXT, VLC_TRUE );
112 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
113 MUX_LONGTEXT, VLC_TRUE );
115 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
116 NAME_LONGTEXT, VLC_TRUE );
117 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
118 DESC_LONGTEXT, VLC_TRUE );
119 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
120 URL_LONGTEXT, VLC_TRUE );
121 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
122 EMAIL_LONGTEXT, VLC_TRUE );
124 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
125 PORT_LONGTEXT, VLC_TRUE );
126 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
127 PORT_AUDIO_LONGTEXT, VLC_TRUE );
128 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
129 PORT_VIDEO_LONGTEXT, VLC_TRUE );
131 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
132 TTL_LONGTEXT, VLC_TRUE );
134 add_bool( SOUT_CFG_PREFIX "mp4a-latm", 0, NULL, RFC3016_TEXT,
135 RFC3016_LONGTEXT, VLC_FALSE );
137 set_callbacks( Open, Close );
140 /*****************************************************************************
141 * Exported prototypes
142 *****************************************************************************/
143 static const char *ppsz_sout_options[] = {
144 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
145 "description", "url","email", "mp4a-latm", NULL
148 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
149 static int Del ( sout_stream_t *, sout_stream_id_t * );
150 static int Send( sout_stream_t *, sout_stream_id_t *,
153 /* For unicast/interleaved streaming */
157 int64_t i_last; /* for timeout */
159 /* is it in "play" state */
160 vlc_bool_t b_playing;
162 /* output (id-access) */
164 sout_stream_id_t **id;
166 sout_access_out_t **access;
169 struct sout_stream_sys_t
175 vlc_mutex_t lock_sdp;
177 char *psz_session_name;
178 char *psz_session_description;
179 char *psz_session_url;
180 char *psz_session_email;
183 vlc_bool_t b_export_sdp_file;
186 vlc_bool_t b_export_sap;
187 session_descriptor_t *p_session;
189 httpd_host_t *p_httpd_host;
190 httpd_file_t *p_httpd_file;
192 httpd_host_t *p_rtsp_host;
193 httpd_url_t *p_rtsp_url;
194 char *psz_rtsp_control;
198 char *psz_destination;
205 /* when need to use a private one or when using muxer */
208 /* in case we do TS/PS over rtp */
210 sout_access_out_t *p_access;
212 sout_access_out_t *p_grab;
214 uint32_t i_timestamp_start;
221 sout_stream_id_t **es;
225 rtsp_client_t **rtsp;
228 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
231 struct sout_stream_id_t
233 sout_stream_t *p_stream;
235 uint8_t i_payload_type;
237 uint32_t i_timestamp_start;
244 char *psz_destination;
248 /* Packetizer specific fields */
249 pf_rtp_packetizer_t pf_packetize;
252 /* for sending the packets */
253 sout_access_out_t *p_access;
255 vlc_mutex_t lock_rtsp;
257 sout_access_out_t **rtsp_access;
260 sout_input_t *p_input;
262 /* RTSP url control */
263 httpd_url_t *p_rtsp_url;
266 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
268 static void SDPHandleUrl( sout_stream_t *, char * );
270 static int SapSetup( sout_stream_t *p_stream );
271 static int FileSetup( sout_stream_t *p_stream );
272 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
273 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
275 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
276 httpd_message_t *, httpd_message_t * );
277 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
278 httpd_message_t *, httpd_message_t * );
281 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
282 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
283 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
285 /*****************************************************************************
287 *****************************************************************************/
288 static int Open( vlc_object_t *p_this )
290 sout_stream_t *p_stream = (sout_stream_t*)p_this;
291 sout_instance_t *p_sout = p_stream->p_sout;
292 sout_stream_sys_t *p_sys = NULL;
293 sout_cfg_t *p_cfg = NULL;
295 vlc_bool_t b_rtsp = VLC_FALSE;
297 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
299 p_sys = malloc( sizeof( sout_stream_sys_t ) );
301 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
302 if( *p_sys->psz_destination == '\0' )
304 free( p_sys->psz_destination );
305 p_sys->psz_destination = NULL;
308 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
309 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
310 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
311 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
313 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
314 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
315 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
317 p_sys->psz_sdp_file = NULL;
319 if( p_sys->i_port_audio == p_sys->i_port_video )
321 msg_Err( p_stream, "audio and video port cannot be the same" );
322 p_sys->i_port_audio = 0;
323 p_sys->i_port_video = 0;
326 if( !p_sys->psz_session_name )
328 if( p_sys->psz_destination )
329 p_sys->psz_session_name = strdup( p_sys->psz_destination );
331 p_sys->psz_session_name = strdup( "NONE" );
334 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
336 if( !strcmp( p_cfg->psz_name, "sdp" ) )
338 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
348 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
349 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
351 free( val2.psz_string );
354 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
358 msg_Err( p_stream, "missing destination and not in rtsp mode" );
362 p_sys->psz_destination = NULL;
364 else if( p_sys->i_port <= 0 )
366 msg_Err( p_stream, "invalid port" );
371 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
374 /* Normally, we should let the default hop limit up to the core,
375 * but we have to know it to build our SDP properly, which is why
376 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
378 val.i_int = config_GetInt( p_stream, "ttl" );
380 if( val.i_int > 255 ) val.i_int = 255;
381 /* must not exceed 999 once formatted */
385 msg_Err( p_stream, "illegal TTL %d", val.i_int );
389 p_sys->i_ttl = val.i_int;
392 var_Get( p_stream, SOUT_CFG_PREFIX "mp4a-latm", &val );
393 p_sys->b_latm = val.b_bool;
395 p_sys->i_payload_type = 96;
400 p_sys->psz_sdp = NULL;
402 p_sys->i_sdp_id = mdate();
403 p_sys->i_sdp_version = 1;
404 p_sys->psz_sdp = NULL;
406 p_sys->b_export_sap = VLC_FALSE;
407 p_sys->b_export_sdp_file = VLC_FALSE;
408 p_sys->p_session = NULL;
410 p_sys->p_httpd_host = NULL;
411 p_sys->p_httpd_file = NULL;
412 p_sys->p_rtsp_host = NULL;
413 p_sys->p_rtsp_url = NULL;
414 p_sys->psz_rtsp_control = NULL;
415 p_sys->psz_rtsp_path = NULL;
417 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
418 vlc_mutex_init( p_stream, &p_sys->lock_es );
420 p_stream->pf_add = Add;
421 p_stream->pf_del = Del;
422 p_stream->pf_send = Send;
424 p_stream->p_sys = p_sys;
426 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
427 if( *val.psz_string )
429 sout_access_out_t *p_grab;
430 char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv;
434 msg_Err( p_stream, "muxing is not supported in RTSP mode" );
438 else if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
440 msg_Err( p_stream, "rtp needs a destination when muxing" );
445 /* Check muxer type */
446 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
448 psz_rtpmap = "MP2P/90000";
450 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
452 psz_rtpmap = "MP2T/90000";
453 p_sys->i_payload_type = 33;
457 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
462 /* create the access out */
463 if( p_sys->i_ttl > 0 )
465 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
469 sprintf( access, "udp{raw}" );
472 /* IPv6 needs brackets if not already present */
473 snprintf( url, sizeof( url ),
474 ( ( p_sys->psz_destination[0] != '[' )
475 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
476 ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
478 url[sizeof( url ) - 1] = '\0';
479 /* FIXME: we should check that url is a numerical address, otherwise
480 * the SDP will be quite broken (regardless of the IP protocol version)
481 * Also it might be IPv6 with no ':' if it is a DNS name.
483 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
485 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
487 msg_Err( p_stream, "cannot create the access out for %s://%s",
492 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
493 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
495 /* better than nothing */
498 p_sys->i_mtu -= MTU_REDUCE;
500 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
501 p_grab = p_sys->p_grab =
502 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
503 p_grab->p_module = NULL;
504 p_grab->p_sout = p_sout;
505 p_grab->psz_access = strdup( "grab" );
506 p_grab->p_cfg = NULL;
507 p_grab->psz_name = strdup( "" );
508 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
509 p_grab->pf_seek = NULL;
510 p_grab->pf_write = AccessOutGrabberWrite;
513 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
515 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
516 sout_AccessOutDelete( p_sys->p_grab );
517 sout_AccessOutDelete( p_sys->p_access );
522 /* create the SDP for a muxed stream (only once) */
523 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
524 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
525 o= - should be local username (no spaces allowed)
526 o= time should be hashed with some other value to garantue uniqueness
527 o= we need IP6 support?
528 o= don't use the localhost address. use fully qualified domain name or IP4 address
529 p= international phone number (pass via vars?)
531 a= recvonly (missing)
532 a= type:broadcast (missing)
533 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
534 a= x-plgroup: (missing)
535 RTP packets need to get the correct src IP address */
536 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
538 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
539 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
546 asprintf( &p_sys->psz_sdp,
548 /* FIXME: source address not known :( */
549 "o=- "I64Fd" %d IN IP%c %s\r\n"
554 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
555 "a=tool:"PACKAGE_STRING"\r\n"
557 "m=video %d RTP/AVP %d\r\n"
558 "a=rtpmap:%d %s\r\n",
559 p_sys->i_sdp_id, p_sys->i_sdp_version,
560 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
561 p_sys->psz_session_name,
562 p_sys->psz_session_description,
563 p_sys->psz_session_url,
564 p_sys->psz_session_email,
565 ipv, p_sys->psz_destination, psz_ttl,
566 p_sys->i_port, p_sys->i_payload_type,
567 p_sys->i_payload_type, psz_rtpmap );
568 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
570 /* create the rtp context */
571 p_sys->ssrc[0] = rand()&0xff;
572 p_sys->ssrc[1] = rand()&0xff;
573 p_sys->ssrc[2] = rand()&0xff;
574 p_sys->ssrc[3] = rand()&0xff;
575 p_sys->i_sequence = rand()&0xffff;
576 p_sys->i_timestamp_start = rand()&0xffffffff;
577 p_sys->packet = NULL;
582 p_sys->p_access = NULL;
583 p_sys->p_grab = NULL;
585 free( val.psz_string );
588 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
589 if( *val.psz_string )
593 SDPHandleUrl( p_stream, val.psz_string );
595 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
597 if( !strcmp( p_cfg->psz_name, "sdp" ) )
599 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
602 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
605 SDPHandleUrl( p_stream, p_cfg->psz_value );
609 free( val.psz_string );
611 /* update p_sout->i_out_pace_nocontrol */
612 p_stream->p_sout->i_out_pace_nocontrol++;
617 /*****************************************************************************
619 *****************************************************************************/
620 static void Close( vlc_object_t * p_this )
622 sout_stream_t *p_stream = (sout_stream_t*)p_this;
623 sout_stream_sys_t *p_sys = p_stream->p_sys;
625 /* update p_sout->i_out_pace_nocontrol */
626 p_stream->p_sout->i_out_pace_nocontrol--;
630 sout_MuxDelete( p_sys->p_mux );
631 sout_AccessOutDelete( p_sys->p_access );
632 sout_AccessOutDelete( p_sys->p_grab );
635 block_Release( p_sys->packet );
637 if( p_sys->b_export_sap )
640 SapSetup( p_stream );
644 while( p_sys->i_rtsp > 0 )
646 RtspClientDel( p_stream, p_sys->rtsp[0] );
649 vlc_mutex_destroy( &p_sys->lock_sdp );
651 if( p_sys->p_httpd_file )
653 httpd_FileDelete( p_sys->p_httpd_file );
655 if( p_sys->p_httpd_host )
657 httpd_HostDelete( p_sys->p_httpd_host );
659 if( p_sys->p_rtsp_url )
661 httpd_UrlDelete( p_sys->p_rtsp_url );
663 if( p_sys->p_rtsp_host )
665 httpd_HostDelete( p_sys->p_rtsp_host );
667 if( p_sys->psz_session_name )
669 free( p_sys->psz_session_name );
670 p_sys->psz_session_name = NULL;
672 if( p_sys->psz_session_description )
674 free( p_sys->psz_session_description );
675 p_sys->psz_session_description = NULL;
677 if( p_sys->psz_session_url )
679 free( p_sys->psz_session_url );
680 p_sys->psz_session_url = NULL;
682 if( p_sys->psz_session_email )
684 free( p_sys->psz_session_email );
685 p_sys->psz_session_email = NULL;
689 free( p_sys->psz_sdp );
690 p_sys->psz_sdp = NULL;
695 /*****************************************************************************
697 *****************************************************************************/
698 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
700 sout_stream_sys_t *p_sys = p_stream->p_sys;
703 vlc_UrlParse( &url, psz_url, 0 );
704 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
706 if( p_sys->p_httpd_file )
708 msg_Err( p_stream, "you can use sdp=http:// only once" );
712 if( HttpSetup( p_stream, &url ) )
714 msg_Err( p_stream, "cannot export sdp as http" );
717 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
719 if( p_sys->p_rtsp_url )
721 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
725 /* FIXME test if destination is multicast or no destination at all FIXME */
726 if( RtspSetup( p_stream, &url ) )
728 msg_Err( p_stream, "cannot export sdp as rtsp" );
731 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
732 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
734 p_sys->b_export_sap = VLC_TRUE;
735 SapSetup( p_stream );
737 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
739 if( p_sys->b_export_sdp_file )
741 msg_Err( p_stream, "you can use sdp=file:// only once" );
744 p_sys->b_export_sdp_file = VLC_TRUE;
745 psz_url = &psz_url[5];
746 if( psz_url[0] == '/' && psz_url[1] == '/' )
748 p_sys->psz_sdp_file = strdup( psz_url );
752 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
755 vlc_UrlClean( &url );
758 /*****************************************************************************
760 *****************************************************************************/
761 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
762 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
763 o= - should be local username (no spaces allowed)
764 o= time should be hashed with some other value to garantue uniqueness
765 o= we need IP6 support?
766 o= don't use the localhost address. use fully qualified domain name or IP4 address
767 p= international phone number (pass via vars?)
769 a= recvonly (missing)
770 a= type:broadcast (missing)
771 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
772 a= x-plgroup: (missing)
773 RTP packets need to get the correct src IP address */
774 static char *SDPGenerate( const sout_stream_t *p_stream,
775 const char *psz_destination, vlc_bool_t b_rtsp )
777 sout_stream_sys_t *p_sys = p_stream->p_sys;
779 char *psz_sdp, *p, ipv;
782 /* FIXME: breaks IP version check on unknown destination */
783 if( psz_destination == NULL )
784 psz_destination = "0.0.0.0";
786 i_size = sizeof( "v=0\r\n" ) +
787 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
788 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
789 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
790 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
791 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
792 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
793 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
794 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
795 strlen( psz_destination ) ;
796 for( i = 0; i < p_sys->i_es; i++ )
798 sout_stream_id_t *id = p_sys->es[i];
800 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
803 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
807 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
811 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
816 i_size += strlen( "m=video %d RTP/AVP %d\r\n" ) +10 +10;
819 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
821 p = psz_sdp = malloc( i_size );
822 p += sprintf( p, "v=0\r\n" );
823 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
824 p_sys->i_sdp_id, p_sys->i_sdp_version,
825 ipv, ipv == '6' ? "::" : "127.0.0.1" );
826 if( *p_sys->psz_session_name )
827 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
828 if( *p_sys->psz_session_description )
829 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
830 if( *p_sys->psz_session_url )
831 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
832 if( *p_sys->psz_session_email )
833 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
835 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
836 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
838 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
840 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
842 /* Add the ttl if it is a multicast address */
843 /* FIXME: 1 is not a correct default value in the case of IPv6 */
844 p += sprintf( p, "/%d\r\n", p_sys->i_ttl ?: 1 );
848 p += sprintf( p, "\r\n" );
851 for( i = 0; i < p_sys->i_es; i++ )
853 sout_stream_id_t *id = p_sys->es[i];
855 if( id->i_cat == AUDIO_ES )
857 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
858 id->i_port, id->i_payload_type );
860 else if( id->i_cat == VIDEO_ES )
862 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
863 id->i_port, id->i_payload_type );
871 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
876 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
881 p += sprintf( p, "a=control:trackID=%d\r\n", i );
886 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
887 p_sys->i_port, p_sys->i_payload_type );
893 /*****************************************************************************
895 *****************************************************************************/
896 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
897 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
898 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
899 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
900 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
901 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
902 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
903 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
904 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
905 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
907 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
909 static const char hex[16] = "0123456789abcdef";
912 for( i = 0; i < i_data; i++ )
914 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
915 s[2*i+1] = hex[(p_data[i] )&0xf];
920 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
922 sout_instance_t *p_sout = p_stream->p_sout;
923 sout_stream_sys_t *p_sys = p_stream->p_sys;
924 sout_stream_id_t *id;
925 sout_access_out_t *p_access = NULL;
929 if( p_sys->p_mux != NULL )
931 sout_input_t *p_input = NULL;
932 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
934 msg_Err( p_stream, "cannot add this stream to the muxer" );
938 id = malloc( sizeof( sout_stream_id_t ) );
939 memset( id, 0, sizeof( sout_stream_id_t ) );
941 id->p_input = p_input;
942 id->pf_packetize= NULL;
943 id->p_rtsp_url = NULL;
949 /* Choose the port */
951 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
953 i_port = p_sys->i_port_audio;
954 p_sys->i_port_audio = 0;
956 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
958 i_port = p_sys->i_port_video;
959 p_sys->i_port_video = 0;
963 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
965 i_port = p_sys->i_port;
972 if( p_sys->psz_destination )
975 char url[NI_MAXHOST + 8];
977 /* first try to create the access out */
980 snprintf( access, sizeof( access ), "udp{raw,ttl=%d}",
982 access[sizeof( access ) - 1] = '\0';
985 strcpy( access, "udp{raw}" );
987 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
988 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
989 p_sys->psz_destination, i_port );
990 url[sizeof( url ) - 1] = '\0';
992 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
994 msg_Err( p_stream, "cannot create the access out for %s://%s",
998 msg_Dbg( p_stream, "access out %s:%s", access, url );
1001 /* not create the rtp specific stuff */
1002 id = malloc( sizeof( sout_stream_id_t ) );
1003 memset( id, 0, sizeof( sout_stream_id_t ) );
1004 id->p_stream = p_stream;
1005 id->p_access = p_access;
1007 id->psz_rtpmap = NULL;
1008 id->psz_fmtp = NULL;
1009 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
1010 id->i_port = i_port;
1011 id->p_rtsp_url = NULL;
1012 vlc_mutex_init( p_stream, &id->lock_rtsp );
1013 id->i_rtsp_access = 0;
1014 id->rtsp_access = NULL;
1016 switch( p_fmt->i_codec )
1018 case VLC_FOURCC( 's', '1', '6', 'b' ):
1019 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
1021 id->i_payload_type = 11;
1023 else if( p_fmt->audio.i_channels == 2 &&
1024 p_fmt->audio.i_rate == 44100 )
1026 id->i_payload_type = 10;
1030 id->i_payload_type = p_sys->i_payload_type++;
1032 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
1033 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
1034 p_fmt->audio.i_channels );
1035 id->i_clock_rate = p_fmt->audio.i_rate;
1036 id->pf_packetize = rtp_packetize_l16;
1038 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
1039 id->i_payload_type = p_sys->i_payload_type++;
1040 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
1041 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
1042 p_fmt->audio.i_channels );
1043 id->i_clock_rate = p_fmt->audio.i_rate;
1044 id->pf_packetize = rtp_packetize_l8;
1046 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1047 id->i_payload_type = 14;
1048 id->i_clock_rate = 90000;
1049 id->psz_rtpmap = strdup( "MPA/90000" );
1050 id->pf_packetize = rtp_packetize_mpa;
1052 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1053 id->i_payload_type = 32;
1054 id->i_clock_rate = 90000;
1055 id->psz_rtpmap = strdup( "MPV/90000" );
1056 id->pf_packetize = rtp_packetize_mpv;
1058 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1059 id->i_payload_type = p_sys->i_payload_type++;
1060 id->i_clock_rate = 90000;
1061 id->psz_rtpmap = strdup( "ac3/90000" );
1062 id->pf_packetize = rtp_packetize_ac3;
1064 case VLC_FOURCC( 'H', '2', '6', '3' ):
1065 id->i_payload_type = p_sys->i_payload_type++;
1066 id->i_clock_rate = 90000;
1067 id->psz_rtpmap = strdup( "H263-1998/90000" );
1068 id->pf_packetize = rtp_packetize_h263;
1071 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1073 char hexa[2*p_fmt->i_extra +1];
1075 id->i_payload_type = p_sys->i_payload_type++;
1076 id->i_clock_rate = 90000;
1077 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1078 id->pf_packetize = rtp_packetize_split;
1079 if( p_fmt->i_extra > 0 )
1081 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1082 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1083 sprintf( id->psz_fmtp,
1084 "profile-level-id=3; config=%s;", hexa );
1088 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1090 id->i_payload_type = p_sys->i_payload_type++;
1091 id->i_clock_rate = p_fmt->audio.i_rate;
1095 char hexa[2*p_fmt->i_extra +1];
1097 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1098 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1099 id->pf_packetize = rtp_packetize_mp4a;
1100 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1101 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1102 sprintf( id->psz_fmtp,
1103 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1104 "config=%s; SizeLength=13;IndexLength=3; "
1105 "IndexDeltaLength=3; Profile=1;", hexa );
1111 unsigned char config[6];
1112 unsigned int aacsrates[15] = {
1113 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1114 16000, 12000, 11025, 8000, 7350, 0, 0 };
1116 for( i = 0; i < 15; i++ )
1117 if( p_fmt->audio.i_rate == aacsrates[i] )
1123 config[3]=p_fmt->audio.i_channels<<4;
1127 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1128 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1129 id->pf_packetize = rtp_packetize_mp4a_latm;
1130 sprintf_hexa( hexa, config, 6 );
1131 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1132 "object=2; cpresent=0; config=%s", hexa );
1136 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1137 id->i_payload_type = p_sys->i_payload_type++;
1138 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1139 "AMR/8000/2" : "AMR/8000" );
1140 id->psz_fmtp = strdup( "octet-align=1" );
1141 id->i_clock_rate = p_fmt->audio.i_rate;
1142 id->pf_packetize = rtp_packetize_amr;
1144 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1145 id->i_payload_type = p_sys->i_payload_type++;
1146 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1147 "AMR-WB/16000/2" : "AMR-WB/16000" );
1148 id->psz_fmtp = strdup( "octet-align=1" );
1149 id->i_clock_rate = p_fmt->audio.i_rate;
1150 id->pf_packetize = rtp_packetize_amr;
1154 msg_Err( p_stream, "cannot add this stream (unsupported "
1155 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1158 sout_AccessOutDelete( p_access );
1163 id->i_cat = p_fmt->i_cat;
1165 id->ssrc[0] = rand()&0xff;
1166 id->ssrc[1] = rand()&0xff;
1167 id->ssrc[2] = rand()&0xff;
1168 id->ssrc[3] = rand()&0xff;
1169 id->i_sequence = rand()&0xffff;
1170 id->i_timestamp_start = rand()&0xffffffff;
1172 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1173 if( id->i_mtu <= 16 + MTU_REDUCE )
1175 /* better than nothing */
1178 id->i_mtu -= MTU_REDUCE;
1179 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1181 if( p_sys->p_rtsp_url )
1183 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1185 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1186 msg_Dbg( p_stream, "rtsp: adding %s\n", psz_urlc );
1187 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1189 if( id->p_rtsp_url )
1191 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1192 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1193 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1198 /* Update p_sys context */
1199 vlc_mutex_lock( &p_sys->lock_es );
1200 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1201 vlc_mutex_unlock( &p_sys->lock_es );
1203 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1205 vlc_mutex_lock( &p_sys->lock_sdp );
1206 free( p_sys->psz_sdp );
1207 p_sys->psz_sdp = psz_sdp;
1208 vlc_mutex_unlock( &p_sys->lock_sdp );
1210 p_sys->i_sdp_version++;
1212 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
1214 /* Update SDP (sap/file) */
1215 if( p_sys->b_export_sap ) SapSetup( p_stream );
1216 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1221 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1223 sout_stream_sys_t *p_sys = p_stream->p_sys;
1225 vlc_mutex_lock( &p_sys->lock_es );
1226 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1227 vlc_mutex_unlock( &p_sys->lock_es );
1230 if( id->i_port > 0 )
1232 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1233 p_sys->i_port_audio = id->i_port;
1234 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1235 p_sys->i_port_video = id->i_port;
1240 if( id->psz_rtpmap )
1242 free( id->psz_rtpmap );
1246 free( id->psz_fmtp );
1248 if( id->psz_destination )
1249 free( id->psz_destination );
1250 sout_AccessOutDelete( id->p_access );
1252 else if( id->p_input )
1254 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1256 if( id->p_rtsp_url )
1258 httpd_UrlDelete( id->p_rtsp_url );
1260 vlc_mutex_destroy( &id->lock_rtsp );
1261 if( id->rtsp_access ) free( id->rtsp_access );
1263 /* Update SDP (sap/file) */
1264 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1265 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1271 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1276 if( p_stream->p_sys->p_mux )
1278 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1284 p_next = p_buffer->p_next;
1285 if( id->pf_packetize( p_stream, id, p_buffer ) )
1289 block_Release( p_buffer );
1297 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1300 sout_stream_sys_t *p_sys = p_stream->p_sys;
1302 int64_t i_dts = p_buffer->i_dts;
1303 uint32_t i_timestamp = i_dts * 9 / 100;
1305 uint8_t *p_data = p_buffer->p_buffer;
1306 unsigned int i_data = p_buffer->i_buffer;
1307 unsigned int i_max = p_sys->i_mtu - 12;
1309 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1313 unsigned int i_size;
1315 /* output complete packet */
1316 if( p_sys->packet &&
1317 p_sys->packet->i_buffer + i_data > i_max )
1319 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1320 p_sys->packet = NULL;
1323 if( p_sys->packet == NULL )
1325 /* allocate a new packet */
1326 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1327 p_sys->packet->p_buffer[ 0] = 0x80;
1328 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1329 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1330 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1331 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1332 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1333 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1334 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1335 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1336 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1337 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1338 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1339 p_sys->packet->i_buffer = 12;
1341 p_sys->packet->i_dts = i_dts;
1342 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1343 i_dts += p_sys->packet->i_length;
1345 p_sys->i_sequence++;
1348 i_size = __MIN( i_data,
1349 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1351 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1354 p_sys->packet->i_buffer += i_size;
1362 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1365 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1367 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1373 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1375 p_next = p_buffer->p_next;
1376 block_Release( p_buffer );
1383 /****************************************************************************
1385 ****************************************************************************/
1386 static int SapSetup( sout_stream_t *p_stream )
1388 sout_stream_sys_t *p_sys = p_stream->p_sys;
1389 sout_instance_t *p_sout = p_stream->p_sout;
1390 announce_method_t *p_method = sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
1392 /* Remove the previous session */
1393 if( p_sys->p_session != NULL)
1395 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1396 sout_AnnounceSessionDestroy( p_sys->p_session );
1397 p_sys->p_session = NULL;
1400 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1402 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1403 p_sys->psz_destination,
1411 /****************************************************************************
1413 ****************************************************************************/
1414 static int FileSetup( sout_stream_t *p_stream )
1416 sout_stream_sys_t *p_sys = p_stream->p_sys;
1419 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1421 msg_Err( p_stream, "cannot open file '%s' (%s)",
1422 p_sys->psz_sdp_file, strerror(errno) );
1423 return VLC_EGENERIC;
1426 fprintf( f, "%s", p_sys->psz_sdp );
1432 /****************************************************************************
1434 ****************************************************************************/
1435 static int HttpCallback( httpd_file_sys_t *p_args,
1436 httpd_file_t *, uint8_t *p_request,
1437 uint8_t **pp_data, int *pi_data );
1439 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1441 sout_stream_sys_t *p_sys = p_stream->p_sys;
1443 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1444 if( p_sys->p_httpd_host )
1446 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1447 url->psz_path ? url->psz_path : "/",
1450 HttpCallback, (void*)p_sys );
1452 if( p_sys->p_httpd_file == NULL )
1454 return VLC_EGENERIC;
1459 static int HttpCallback( httpd_file_sys_t *p_args,
1460 httpd_file_t *f, uint8_t *p_request,
1461 uint8_t **pp_data, int *pi_data )
1463 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1465 vlc_mutex_lock( &p_sys->lock_sdp );
1466 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1468 *pi_data = strlen( p_sys->psz_sdp );
1469 *pp_data = malloc( *pi_data );
1470 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1477 vlc_mutex_unlock( &p_sys->lock_sdp );
1482 /****************************************************************************
1484 ****************************************************************************/
1485 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1487 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1489 rtsp->psz_session = psz_session;
1491 rtsp->b_playing = VLC_FALSE;
1495 rtsp->access = NULL;
1497 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1501 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1505 if( psz_session ) return NULL;
1507 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1509 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1511 return p_stream->p_sys->rtsp[i];
1517 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1520 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1522 for( i = 0; i < rtsp->i_access; i++ )
1524 sout_AccessOutDelete( rtsp->access[i] );
1526 if( rtsp->id ) free( rtsp->id );
1527 if( rtsp->access ) free( rtsp->access );
1529 free( rtsp->psz_session );
1533 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1535 sout_stream_sys_t *p_sys = p_stream->p_sys;
1537 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1539 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1540 if( p_sys->p_rtsp_host == NULL )
1542 return VLC_EGENERIC;
1545 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1546 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1547 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1548 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1550 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1551 if( p_sys->p_rtsp_url == 0 )
1553 return VLC_EGENERIC;
1555 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1556 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1557 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1558 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1563 static int RtspCallback( httpd_callback_sys_t *p_args,
1565 httpd_message_t *answer, httpd_message_t *query )
1567 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1568 sout_stream_sys_t *p_sys = p_stream->p_sys;
1569 char *psz_destination = p_sys->psz_destination;
1570 char *psz_session = NULL;
1571 char *psz_cseq = NULL;
1574 if( answer == NULL || query == NULL )
1578 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1580 answer->i_proto = HTTPD_PROTO_RTSP;
1581 answer->i_version= query->i_version;
1582 answer->i_type = HTTPD_MSG_ANSWER;
1584 switch( query->i_type )
1586 case HTTPD_MSG_DESCRIBE:
1588 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1590 answer->i_status = 200;
1591 answer->psz_status = strdup( "OK" );
1592 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1594 answer->p_body = (uint8_t *)psz_sdp;
1595 answer->i_body = strlen( psz_sdp );
1599 case HTTPD_MSG_PLAY:
1601 rtsp_client_t *rtsp;
1602 /* for now only multicast so easy */
1603 answer->i_status = 200;
1604 answer->psz_status = strdup( "OK" );
1606 answer->p_body = NULL;
1608 psz_session = httpd_MsgGet( query, "Session" );
1609 rtsp = RtspClientGet( p_stream, psz_session );
1610 if( rtsp && !rtsp->b_playing )
1614 rtsp->b_playing = VLC_TRUE;
1616 vlc_mutex_lock( &p_sys->lock_es );
1617 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1619 sout_stream_id_t *id = rtsp->id[i_id];
1622 for( i = 0; i < p_sys->i_es; i++ )
1624 if( id == p_sys->es[i] )
1627 if( i >= p_sys->i_es ) continue;
1629 vlc_mutex_lock( &id->lock_rtsp );
1630 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1631 vlc_mutex_unlock( &id->lock_rtsp );
1633 vlc_mutex_unlock( &p_sys->lock_es );
1637 case HTTPD_MSG_PAUSE:
1639 return VLC_EGENERIC;
1640 case HTTPD_MSG_TEARDOWN:
1642 rtsp_client_t *rtsp;
1644 /* for now only multicast so easy again */
1645 answer->i_status = 200;
1646 answer->psz_status = strdup( "OK" );
1648 answer->p_body = NULL;
1650 psz_session = httpd_MsgGet( query, "Session" );
1651 rtsp = RtspClientGet( p_stream, psz_session );
1656 vlc_mutex_lock( &p_sys->lock_es );
1657 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1659 sout_stream_id_t *id = rtsp->id[i_id];
1662 for( i = 0; i < p_sys->i_es; i++ )
1664 if( id == p_sys->es[i] )
1667 if( i >= p_sys->i_es ) continue;
1669 vlc_mutex_lock( &id->lock_rtsp );
1670 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1671 vlc_mutex_unlock( &id->lock_rtsp );
1673 vlc_mutex_unlock( &p_sys->lock_es );
1675 RtspClientDel( p_stream, rtsp );
1681 return VLC_EGENERIC;
1683 httpd_MsgAdd( answer, "Server", PACKAGE_STRING );
1684 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1685 psz_cseq = httpd_MsgGet( query, "Cseq" );
1687 i_cseq = atoi( psz_cseq );
1690 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1691 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1695 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1700 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1702 httpd_message_t *answer, httpd_message_t *query )
1704 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1705 sout_stream_t *p_stream = id->p_stream;
1706 sout_stream_sys_t *p_sys = p_stream->p_sys;
1707 char *psz_session = NULL;
1708 char *psz_cseq = NULL;
1712 if( answer == NULL || query == NULL )
1716 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1718 answer->i_proto = HTTPD_PROTO_RTSP;
1719 answer->i_version= query->i_version;
1720 answer->i_type = HTTPD_MSG_ANSWER;
1722 switch( query->i_type )
1724 case HTTPD_MSG_SETUP:
1726 char *psz_transport = httpd_MsgGet( query, "Transport" );
1728 //fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1730 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1732 //fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1733 answer->i_status = 200;
1734 answer->psz_status = strdup( "OK" );
1736 answer->p_body = NULL;
1737 psz_session = httpd_MsgGet( query, "Session" );
1740 psz_session = malloc( 100 );
1741 sprintf( psz_session, "%d", rand() );
1743 httpd_MsgAdd( answer, "Transport",
1744 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1745 id->psz_destination, id->i_port,id->i_port+1,
1748 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1750 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1751 char ip[NI_MAXNUMERICHOST], psz_access[17], psz_url[NI_MAXNUMERICHOST + 8];
1753 sout_access_out_t *p_access;
1755 rtsp_client_t *rtsp = NULL;
1757 if( httpd_ClientIP( cl, ip ) == NULL )
1759 answer->i_status = 500;
1760 answer->psz_status = strdup( "Internal server error" );
1762 answer->p_body = NULL;
1766 //fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n", ip, i_port );
1768 psz_session = httpd_MsgGet( query, "Session" );
1771 psz_session = malloc( 100 );
1772 sprintf( psz_session, "%d", rand() );
1774 rtsp = RtspClientNew( p_stream, psz_session );
1778 rtsp = RtspClientGet( p_stream, psz_session );
1781 answer->i_status = 454;
1782 answer->psz_status = strdup( "Unknown session id" );
1784 answer->p_body = NULL;
1789 /* first try to create the access out */
1791 snprintf( psz_access, sizeof( psz_access ),
1792 "udp{raw,ttl=%d}", p_sys->i_ttl );
1794 strlcpy( psz_access, "udp{raw}", sizeof( psz_access ) );
1796 snprintf( psz_url, sizeof( psz_url ),
1797 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
1800 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1802 msg_Err( p_stream, "cannot create the access out for %s://%s",
1803 psz_access, psz_url );
1804 answer->i_status = 500;
1805 answer->psz_status = strdup( "Internal server error" );
1807 answer->p_body = NULL;
1811 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1812 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1814 answer->i_status = 200;
1815 answer->psz_status = strdup( "OK" );
1817 answer->p_body = NULL;
1819 httpd_MsgAdd( answer, "Transport",
1820 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1822 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1824 answer->i_status = 461;
1825 answer->psz_status = strdup( "Unsupported Transport" );
1827 answer->p_body = NULL;
1833 return VLC_EGENERIC;
1835 httpd_MsgAdd( answer, "Server", "VLC Server" );
1836 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1837 psz_cseq = httpd_MsgGet( query, "Cseq" );
1839 i_cseq = atoi( psz_cseq );
1842 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1843 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1847 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1852 /****************************************************************************
1854 ****************************************************************************/
1855 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1856 int b_marker, int64_t i_pts )
1858 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1860 out->p_buffer[0] = 0x80;
1861 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1862 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1863 out->p_buffer[3] = ( id->i_sequence )&0xff;
1864 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1865 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1866 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1867 out->p_buffer[7] = ( i_timestamp )&0xff;
1869 out->p_buffer[ 8] = id->ssrc[0];
1870 out->p_buffer[ 9] = id->ssrc[1];
1871 out->p_buffer[10] = id->ssrc[2];
1872 out->p_buffer[11] = id->ssrc[3];
1878 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1881 vlc_mutex_lock( &id->lock_rtsp );
1882 for( i = 0; i < id->i_rtsp_access; i++ )
1884 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1886 vlc_mutex_unlock( &id->lock_rtsp );
1890 sout_AccessOutWrite( id->p_access, out );
1894 block_Release( out );
1898 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1901 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1902 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1904 uint8_t *p_data = in->p_buffer;
1905 int i_data = in->i_buffer;
1908 for( i = 0; i < i_count; i++ )
1910 int i_payload = __MIN( i_max, i_data );
1911 block_t *out = block_New( p_stream, 16 + i_payload );
1913 /* rtp common header */
1914 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1916 out->p_buffer[12] = 0;
1917 out->p_buffer[13] = 0;
1918 /* fragment offset in the current frame */
1919 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1920 out->p_buffer[15] = ( (i*i_max) )&0xff;
1921 memcpy( &out->p_buffer[16], p_data, i_payload );
1923 out->i_buffer = 16 + i_payload;
1924 out->i_dts = in->i_dts + i * in->i_length / i_count;
1925 out->i_length = in->i_length / i_count;
1927 rtp_packetize_send( id, out );
1929 p_data += i_payload;
1930 i_data -= i_payload;
1937 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1940 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1941 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1943 uint8_t *p_data = in->p_buffer;
1944 int i_data = in->i_buffer;
1946 int b_sequence_start = 0;
1947 int i_temporal_ref = 0;
1948 int i_picture_coding_type = 0;
1949 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1950 int b_start_slice = 0;
1952 /* preparse this packet to get some info */
1953 if( in->i_buffer > 4 )
1955 uint8_t *p = p_data;
1956 int i_rest = in->i_buffer;
1960 while( i_rest > 4 &&
1961 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1975 /* sequence start code */
1976 b_sequence_start = 1;
1978 else if( *p == 0x00 && i_rest >= 4 )
1981 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1982 i_picture_coding_type = (p[2] >> 3)&0x07;
1984 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1985 i_picture_coding_type == 3 ) )
1987 i_ffv = (p[3] >> 2)&0x01;
1988 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1989 if( i_rest > 4 && i_picture_coding_type == 3 )
1991 i_fbv = (p[4]>>6)&0x01;
1992 i_bfc = (p[4]>>3)&0x07;
1996 else if( *p <= 0xaf )
2003 for( i = 0; i < i_count; i++ )
2005 int i_payload = __MIN( i_max, i_data );
2006 block_t *out = block_New( p_stream,
2008 uint32_t h = ( i_temporal_ref << 16 )|
2009 ( b_sequence_start << 13 )|
2010 ( b_start_slice << 12 )|
2011 ( i == i_count - 1 ? 1 << 11 : 0 )|
2012 ( i_picture_coding_type << 8 )|
2013 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
2015 /* rtp common header */
2016 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2017 in->i_pts > 0 ? in->i_pts : in->i_dts );
2019 /* 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 */
2020 out->p_buffer[12] = ( h >> 24 )&0xff;
2021 out->p_buffer[13] = ( h >> 16 )&0xff;
2022 out->p_buffer[14] = ( h >> 8 )&0xff;
2023 out->p_buffer[15] = ( h )&0xff;
2025 memcpy( &out->p_buffer[16], p_data, i_payload );
2027 out->i_buffer = 16 + i_payload;
2028 out->i_dts = in->i_dts + i * in->i_length / i_count;
2029 out->i_length = in->i_length / i_count;
2031 rtp_packetize_send( id, out );
2033 p_data += i_payload;
2034 i_data -= i_payload;
2040 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
2043 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
2044 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2046 uint8_t *p_data = in->p_buffer;
2047 int i_data = in->i_buffer;
2050 for( i = 0; i < i_count; i++ )
2052 int i_payload = __MIN( i_max, i_data );
2053 block_t *out = block_New( p_stream, 14 + i_payload );
2055 /* rtp common header */
2056 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
2058 out->p_buffer[12] = 1;
2060 out->p_buffer[13] = 0x00;
2062 memcpy( &out->p_buffer[14], p_data, i_payload );
2064 out->i_buffer = 14 + i_payload;
2065 out->i_dts = in->i_dts + i * in->i_length / i_count;
2066 out->i_length = in->i_length / i_count;
2068 rtp_packetize_send( id, out );
2070 p_data += i_payload;
2071 i_data -= i_payload;
2077 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
2080 int i_max = id->i_mtu - 12; /* payload max in one packet */
2081 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2083 uint8_t *p_data = in->p_buffer;
2084 int i_data = in->i_buffer;
2087 for( i = 0; i < i_count; i++ )
2089 int i_payload = __MIN( i_max, i_data );
2090 block_t *out = block_New( p_stream, 12 + i_payload );
2092 /* rtp common header */
2093 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2094 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2095 memcpy( &out->p_buffer[12], p_data, i_payload );
2097 out->i_buffer = 12 + i_payload;
2098 out->i_dts = in->i_dts + i * in->i_length / i_count;
2099 out->i_length = in->i_length / i_count;
2101 rtp_packetize_send( id, out );
2103 p_data += i_payload;
2104 i_data -= i_payload;
2111 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
2114 int i_max = id->i_mtu - 14; /* payload max in one packet */
2115 int latmhdrsize = in->i_buffer / 0xff + 1;
2116 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2118 uint8_t *p_data = in->p_buffer, *p_header = NULL;
2119 int i_data = in->i_buffer;
2122 for( i = 0; i < i_count; i++ )
2124 int i_payload = __MIN( i_max, i_data );
2129 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
2131 /* rtp common header */
2132 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
2133 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2137 int tmp = in->i_buffer;
2139 p_header=out->p_buffer+12;
2149 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
2151 out->i_buffer = 12 + latmhdrsize + i_payload;
2152 out->i_dts = in->i_dts + i * in->i_length / i_count;
2153 out->i_length = in->i_length / i_count;
2155 rtp_packetize_send( id, out );
2157 p_data += i_payload;
2158 i_data -= i_payload;
2164 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
2167 int i_max = id->i_mtu - 12; /* payload max in one packet */
2168 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2170 uint8_t *p_data = in->p_buffer;
2171 int i_data = in->i_buffer;
2176 int i_payload = (__MIN( i_max, i_data )/4)*4;
2177 block_t *out = block_New( p_stream, 12 + i_payload );
2179 /* rtp common header */
2180 rtp_packetize_common( id, out, 0,
2181 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2182 memcpy( &out->p_buffer[12], p_data, i_payload );
2184 out->i_buffer = 12 + i_payload;
2185 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2186 out->i_length = in->i_length / i_count;
2188 rtp_packetize_send( id, out );
2190 p_data += i_payload;
2191 i_data -= i_payload;
2198 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2201 int i_max = id->i_mtu - 12; /* payload max in one packet */
2202 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2204 uint8_t *p_data = in->p_buffer;
2205 int i_data = in->i_buffer;
2210 int i_payload = (__MIN( i_max, i_data )/2)*2;
2211 block_t *out = block_New( p_stream, 12 + i_payload );
2213 /* rtp common header */
2214 rtp_packetize_common( id, out, 0,
2215 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2216 memcpy( &out->p_buffer[12], p_data, i_payload );
2218 out->i_buffer = 12 + i_payload;
2219 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2220 out->i_length = in->i_length / i_count;
2222 rtp_packetize_send( id, out );
2224 p_data += i_payload;
2225 i_data -= i_payload;
2231 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2234 int i_max = id->i_mtu - 16; /* payload max in one packet */
2235 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2237 uint8_t *p_data = in->p_buffer;
2238 int i_data = in->i_buffer;
2241 for( i = 0; i < i_count; i++ )
2243 int i_payload = __MIN( i_max, i_data );
2244 block_t *out = block_New( p_stream, 16 + i_payload );
2246 /* rtp common header */
2247 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2248 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2250 /* AU headers length (bits) */
2251 out->p_buffer[12] = 0;
2252 out->p_buffer[13] = 2*8;
2253 /* for each AU length 13 bits + idx 3bits, */
2254 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2255 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2257 memcpy( &out->p_buffer[16], p_data, i_payload );
2259 out->i_buffer = 16 + i_payload;
2260 out->i_dts = in->i_dts + i * in->i_length / i_count;
2261 out->i_length = in->i_length / i_count;
2263 rtp_packetize_send( id, out );
2265 p_data += i_payload;
2266 i_data -= i_payload;
2274 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2275 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2276 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2279 uint8_t *p_data = in->p_buffer;
2280 int i_data = in->i_buffer;
2282 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2285 int b_v_bit = 0; // no pesky error resilience
2286 int i_plen = 0; // normally plen=0 for PSC packet
2287 int i_pebit = 0; // because plen=0
2292 return VLC_EGENERIC;
2294 if( p_data[0] || p_data[1] )
2296 return VLC_EGENERIC;
2298 /* remove 2 leading 0 bytes */
2301 i_count = ( i_data + i_max - 1 ) / i_max;
2303 for( i = 0; i < i_count; i++ )
2305 int i_payload = __MIN( i_max, i_data );
2306 block_t *out = block_New( p_stream,
2307 RTP_H263_PAYLOAD_START + i_payload );
2308 b_p_bit = (i == 0) ? 1 : 0;
2309 h = ( b_p_bit << 10 )|
2314 /* rtp common header */
2315 //b_m_bit = 1; // always contains end of frame
2316 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2317 in->i_pts > 0 ? in->i_pts : in->i_dts );
2320 out->p_buffer[12] = ( h >> 8 )&0xff;
2321 out->p_buffer[13] = ( h )&0xff;
2322 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2324 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2325 out->i_dts = in->i_dts + i * in->i_length / i_count;
2326 out->i_length = in->i_length / i_count;
2328 rtp_packetize_send( id, out );
2330 p_data += i_payload;
2331 i_data -= i_payload;
2337 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2340 int i_max = id->i_mtu - 14; /* payload max in one packet */
2341 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2343 uint8_t *p_data = in->p_buffer;
2344 int i_data = in->i_buffer;
2347 /* Only supports octet-aligned mode */
2348 for( i = 0; i < i_count; i++ )
2350 int i_payload = __MIN( i_max, i_data );
2351 block_t *out = block_New( p_stream, 14 + i_payload );
2353 /* rtp common header */
2354 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2355 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2356 /* Payload header */
2357 out->p_buffer[12] = 0xF0; /* CMR */
2358 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
2360 /* FIXME: are we fed multiple frames ? */
2361 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
2363 out->i_buffer = 14 + i_payload-1;
2364 out->i_dts = in->i_dts + i * in->i_length / i_count;
2365 out->i_length = in->i_length / i_count;
2367 rtp_packetize_send( id, out );
2369 p_data += i_payload;
2370 i_data -= i_payload;