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 *****************************************************************************/
37 #include <vlc/input.h>
40 #include "vlc_httpd.h"
45 /*****************************************************************************
47 *****************************************************************************/
51 #define DST_TEXT N_("Destination")
52 #define DST_LONGTEXT N_( \
53 "This is the output URL that will be used." )
54 #define SDP_TEXT N_("SDP")
55 #define SDP_LONGTEXT N_( \
56 "This allows you to specify how the SDP (Session Descriptor) for this RTP "\
57 "session will be made available. You must use an url: http://location to " \
58 "access the SDP via HTTP, rtsp://location for RTSP access, and sap:// " \
59 "for the SDP to be announced via SAP." )
60 #define MUX_TEXT N_("Muxer")
61 #define MUX_LONGTEXT N_( \
62 "This allows you to specify the muxer used for the streaming output. " \
63 "Default is to use no muxer (standard RTP stream)." )
65 #define NAME_TEXT N_("Session name")
66 #define NAME_LONGTEXT N_( \
67 "This is the name of the session that will be announced in the SDP " \
68 "(Session Descriptor)." )
69 #define DESC_TEXT N_("Session description")
70 #define DESC_LONGTEXT N_( \
71 "This allows you to give a broader description of the stream, that will " \
72 "be announced in the SDP (Session Descriptor)." )
73 #define URL_TEXT N_("Session URL")
74 #define URL_LONGTEXT N_( \
75 "This allows you to give an URL with more details about the stream " \
76 "(often the website of the streaming organization), that will " \
77 "be announced in the SDP (Session Descriptor)." )
78 #define EMAIL_TEXT N_("Session email")
79 #define EMAIL_LONGTEXT N_( \
80 "This allows you to give a contact mail address for the stream, that will " \
81 "be announced in the SDP (Session Descriptor)." )
82 #define PORT_TEXT N_("Port")
83 #define PORT_LONGTEXT N_( \
84 "This allows you to specify the base port for the RTP streaming." )
85 #define PORT_AUDIO_TEXT N_("Audio port")
86 #define PORT_AUDIO_LONGTEXT N_( \
87 "This allows you to specify the default audio port for the RTP streaming." )
88 #define PORT_VIDEO_TEXT N_("Video port")
89 #define PORT_VIDEO_LONGTEXT N_( \
90 "This allows you to specify the default video port for the RTP streaming." )
92 #define TTL_TEXT N_("Time-To-Live (TTL)")
93 #define TTL_LONGTEXT N_( \
94 "This allows you to specify the Time-To-Live for the output stream." )
96 #define RFC3016_TEXT N_("MP4A LATM")
97 #define RFC3016_LONGTEXT N_( \
98 "This allows you to stream MPEG4 LATM audio streams (see RFC3016)." )
100 static int Open ( vlc_object_t * );
101 static void Close( vlc_object_t * );
103 #define SOUT_CFG_PREFIX "sout-rtp-"
106 set_shortname( _("RTP"));
107 set_description( _("RTP stream output") );
108 set_capability( "sout stream", 0 );
109 add_shortcut( "rtp" );
110 set_category( CAT_SOUT );
111 set_subcategory( SUBCAT_SOUT_STREAM );
113 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
114 DST_LONGTEXT, VLC_TRUE );
115 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
116 SDP_LONGTEXT, VLC_TRUE );
117 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
118 MUX_LONGTEXT, VLC_TRUE );
120 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
121 NAME_LONGTEXT, VLC_TRUE );
122 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
123 DESC_LONGTEXT, VLC_TRUE );
124 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
125 URL_LONGTEXT, VLC_TRUE );
126 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
127 EMAIL_LONGTEXT, VLC_TRUE );
129 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
130 PORT_LONGTEXT, VLC_TRUE );
131 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
132 PORT_AUDIO_LONGTEXT, VLC_TRUE );
133 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
134 PORT_VIDEO_LONGTEXT, VLC_TRUE );
136 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
137 TTL_LONGTEXT, VLC_TRUE );
139 add_bool( SOUT_CFG_PREFIX "mp4a-latm", 0, NULL, RFC3016_TEXT,
140 RFC3016_LONGTEXT, VLC_FALSE );
142 set_callbacks( Open, Close );
145 /*****************************************************************************
146 * Exported prototypes
147 *****************************************************************************/
148 static const char *ppsz_sout_options[] = {
149 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
150 "description", "url","email", "mp4a-latm", NULL
153 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
154 static int Del ( sout_stream_t *, sout_stream_id_t * );
155 static int Send( sout_stream_t *, sout_stream_id_t *,
158 /* For unicast/interleaved streaming */
162 int64_t i_last; /* for timeout */
164 /* is it in "play" state */
165 vlc_bool_t b_playing;
167 /* output (id-access) */
169 sout_stream_id_t **id;
171 sout_access_out_t **access;
174 struct sout_stream_sys_t
180 vlc_mutex_t lock_sdp;
182 char *psz_session_name;
183 char *psz_session_description;
184 char *psz_session_url;
185 char *psz_session_email;
188 vlc_bool_t b_export_sdp_file;
191 vlc_bool_t b_export_sap;
192 session_descriptor_t *p_session;
194 httpd_host_t *p_httpd_host;
195 httpd_file_t *p_httpd_file;
197 httpd_host_t *p_rtsp_host;
198 httpd_url_t *p_rtsp_url;
199 char *psz_rtsp_control;
203 char *psz_destination;
210 /* when need to use a private one or when using muxer */
213 /* in case we do TS/PS over rtp */
215 sout_access_out_t *p_access;
217 sout_access_out_t *p_grab;
219 uint32_t i_timestamp_start;
226 sout_stream_id_t **es;
230 rtsp_client_t **rtsp;
233 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
236 struct sout_stream_id_t
238 sout_stream_t *p_stream;
240 uint8_t i_payload_type;
242 uint32_t i_timestamp_start;
249 char *psz_destination;
254 /* Packetizer specific fields */
255 pf_rtp_packetizer_t pf_packetize;
258 /* for sending the packets */
259 sout_access_out_t *p_access;
261 vlc_mutex_t lock_rtsp;
263 sout_access_out_t **rtsp_access;
266 sout_input_t *p_input;
268 /* RTSP url control */
269 httpd_url_t *p_rtsp_url;
272 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
274 static void SDPHandleUrl( sout_stream_t *, char * );
276 static int SapSetup( sout_stream_t *p_stream );
277 static int FileSetup( sout_stream_t *p_stream );
278 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
279 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
281 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
282 httpd_message_t *, httpd_message_t * );
283 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
284 httpd_message_t *, httpd_message_t * );
287 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
288 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
289 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
291 /*****************************************************************************
293 *****************************************************************************/
294 static int Open( vlc_object_t *p_this )
296 sout_stream_t *p_stream = (sout_stream_t*)p_this;
297 sout_instance_t *p_sout = p_stream->p_sout;
298 sout_stream_sys_t *p_sys = NULL;
299 config_chain_t *p_cfg = NULL;
301 vlc_bool_t b_rtsp = VLC_FALSE;
303 config_ChainParse( p_stream, SOUT_CFG_PREFIX,
304 ppsz_sout_options, p_stream->p_cfg );
306 p_sys = malloc( sizeof( sout_stream_sys_t ) );
308 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
309 if( *p_sys->psz_destination == '\0' )
311 free( p_sys->psz_destination );
312 p_sys->psz_destination = NULL;
315 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
316 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
317 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
318 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
320 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
321 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
322 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
324 p_sys->psz_sdp_file = NULL;
326 if( p_sys->i_port_audio == p_sys->i_port_video )
328 msg_Err( p_stream, "audio and video port cannot be the same" );
329 p_sys->i_port_audio = 0;
330 p_sys->i_port_video = 0;
333 if( !p_sys->psz_session_name )
335 if( p_sys->psz_destination )
336 p_sys->psz_session_name = strdup( p_sys->psz_destination );
338 p_sys->psz_session_name = strdup( "NONE" );
341 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
343 if( !strcmp( p_cfg->psz_name, "sdp" ) )
345 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
355 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
356 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
358 free( val2.psz_string );
361 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
365 msg_Err( p_stream, "missing destination and not in rtsp mode" );
369 p_sys->psz_destination = NULL;
371 else if( p_sys->i_port <= 0 )
373 msg_Err( p_stream, "invalid port" );
378 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
381 /* Normally, we should let the default hop limit up to the core,
382 * but we have to know it to build our SDP properly, which is why
383 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
385 val.i_int = config_GetInt( p_stream, "ttl" );
387 if( val.i_int > 255 ) val.i_int = 255;
388 /* must not exceed 999 once formatted */
392 msg_Err( p_stream, "illegal TTL %d", val.i_int );
396 p_sys->i_ttl = val.i_int;
399 var_Get( p_stream, SOUT_CFG_PREFIX "mp4a-latm", &val );
400 p_sys->b_latm = val.b_bool;
402 p_sys->i_payload_type = 96;
407 p_sys->psz_sdp = NULL;
409 p_sys->i_sdp_id = mdate();
410 p_sys->i_sdp_version = 1;
411 p_sys->psz_sdp = NULL;
413 p_sys->b_export_sap = VLC_FALSE;
414 p_sys->b_export_sdp_file = VLC_FALSE;
415 p_sys->p_session = NULL;
417 p_sys->p_httpd_host = NULL;
418 p_sys->p_httpd_file = NULL;
419 p_sys->p_rtsp_host = NULL;
420 p_sys->p_rtsp_url = NULL;
421 p_sys->psz_rtsp_control = NULL;
422 p_sys->psz_rtsp_path = NULL;
424 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
425 vlc_mutex_init( p_stream, &p_sys->lock_es );
427 p_stream->pf_add = Add;
428 p_stream->pf_del = Del;
429 p_stream->pf_send = Send;
431 p_stream->p_sys = p_sys;
433 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
434 if( *val.psz_string )
436 sout_access_out_t *p_grab;
437 char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv;
441 msg_Err( p_stream, "muxing is not supported in RTSP mode" );
445 else if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
447 msg_Err( p_stream, "rtp needs a destination when muxing" );
452 /* Check muxer type */
453 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
455 psz_rtpmap = "MP2P/90000";
457 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
459 psz_rtpmap = "MP2T/90000";
460 p_sys->i_payload_type = 33;
464 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
469 /* create the access out */
470 if( p_sys->i_ttl > 0 )
472 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
476 sprintf( access, "udp{raw}" );
479 /* IPv6 needs brackets if not already present */
480 snprintf( url, sizeof( url ),
481 ( ( p_sys->psz_destination[0] != '[' )
482 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
483 ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
485 url[sizeof( url ) - 1] = '\0';
486 /* FIXME: we should check that url is a numerical address, otherwise
487 * the SDP will be quite broken (regardless of the IP protocol version)
488 * Also it might be IPv6 with no ':' if it is a DNS name.
490 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
492 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
494 msg_Err( p_stream, "cannot create the access out for %s://%s",
499 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
500 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
502 /* better than nothing */
505 p_sys->i_mtu -= MTU_REDUCE;
507 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
508 p_grab = p_sys->p_grab =
509 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
510 p_grab->p_module = NULL;
511 p_grab->p_sout = p_sout;
512 p_grab->psz_access = strdup( "grab" );
513 p_grab->p_cfg = NULL;
514 p_grab->psz_name = strdup( "" );
515 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
516 p_grab->pf_seek = NULL;
517 p_grab->pf_write = AccessOutGrabberWrite;
520 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
522 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
523 sout_AccessOutDelete( p_sys->p_grab );
524 sout_AccessOutDelete( p_sys->p_access );
529 /* create the SDP for a muxed stream (only once) */
530 /* FIXME http://www.faqs.org/rfcs/rfc4566.html
531 o= - should be local username (no spaces allowed)
532 o= time should be hashed with some other value to garantee uniqueness
533 o= don't use the localhost address. use fully qualified domain name or IP4 address
534 a= source-filter: we need our source address
535 a= x-plgroup: (missing)
536 RTP packets need to get the correct src IP address */
537 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
539 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
540 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
547 asprintf( &p_sys->psz_sdp,
549 /* FIXME: source address not known :( */
550 "o=- "I64Fd" %d IN IP%c %s\r\n"
556 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
557 "a=tool:"PACKAGE_STRING"\r\n"
559 "a=type:broadcast\r\n"
560 "m=video %d RTP/AVP %d\r\n"
561 "a=rtpmap:%d %s\r\n",
562 p_sys->i_sdp_id, p_sys->i_sdp_version,
563 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
564 p_sys->psz_session_name,
565 p_sys->psz_session_description,
566 p_sys->psz_session_url,
567 p_sys->psz_session_email,
568 ipv, p_sys->psz_destination, psz_ttl,
569 p_sys->i_port, p_sys->i_payload_type,
570 p_sys->i_payload_type, psz_rtpmap );
571 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
573 /* create the rtp context */
574 p_sys->ssrc[0] = rand()&0xff;
575 p_sys->ssrc[1] = rand()&0xff;
576 p_sys->ssrc[2] = rand()&0xff;
577 p_sys->ssrc[3] = rand()&0xff;
578 p_sys->i_sequence = rand()&0xffff;
579 p_sys->i_timestamp_start = rand()&0xffffffff;
580 p_sys->packet = NULL;
585 p_sys->p_access = NULL;
586 p_sys->p_grab = NULL;
588 free( val.psz_string );
591 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
592 if( *val.psz_string )
594 config_chain_t *p_cfg;
596 SDPHandleUrl( p_stream, val.psz_string );
598 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
600 if( !strcmp( p_cfg->psz_name, "sdp" ) )
602 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
605 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
608 SDPHandleUrl( p_stream, p_cfg->psz_value );
612 free( val.psz_string );
614 /* update p_sout->i_out_pace_nocontrol */
615 p_stream->p_sout->i_out_pace_nocontrol++;
620 /*****************************************************************************
622 *****************************************************************************/
623 static void Close( vlc_object_t * p_this )
625 sout_stream_t *p_stream = (sout_stream_t*)p_this;
626 sout_stream_sys_t *p_sys = p_stream->p_sys;
628 /* update p_sout->i_out_pace_nocontrol */
629 p_stream->p_sout->i_out_pace_nocontrol--;
633 sout_MuxDelete( p_sys->p_mux );
634 sout_AccessOutDelete( p_sys->p_access );
635 sout_AccessOutDelete( p_sys->p_grab );
638 block_Release( p_sys->packet );
640 if( p_sys->b_export_sap )
643 SapSetup( p_stream );
647 while( p_sys->i_rtsp > 0 )
649 RtspClientDel( p_stream, p_sys->rtsp[0] );
652 vlc_mutex_destroy( &p_sys->lock_sdp );
654 if( p_sys->p_httpd_file )
656 httpd_FileDelete( p_sys->p_httpd_file );
658 if( p_sys->p_httpd_host )
660 httpd_HostDelete( p_sys->p_httpd_host );
662 if( p_sys->p_rtsp_url )
664 httpd_UrlDelete( p_sys->p_rtsp_url );
666 if( p_sys->p_rtsp_host )
668 httpd_HostDelete( p_sys->p_rtsp_host );
670 if( p_sys->psz_session_name )
672 free( p_sys->psz_session_name );
673 p_sys->psz_session_name = NULL;
675 if( p_sys->psz_session_description )
677 free( p_sys->psz_session_description );
678 p_sys->psz_session_description = NULL;
680 if( p_sys->psz_session_url )
682 free( p_sys->psz_session_url );
683 p_sys->psz_session_url = NULL;
685 if( p_sys->psz_session_email )
687 free( p_sys->psz_session_email );
688 p_sys->psz_session_email = NULL;
692 free( p_sys->psz_sdp );
693 p_sys->psz_sdp = NULL;
695 if( p_sys->b_export_sdp_file )
698 unlink( p_sys->psz_sdp_file );
700 free( p_sys->psz_sdp_file );
705 /*****************************************************************************
707 *****************************************************************************/
708 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
710 sout_stream_sys_t *p_sys = p_stream->p_sys;
713 vlc_UrlParse( &url, psz_url, 0 );
714 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
716 if( p_sys->p_httpd_file )
718 msg_Err( p_stream, "you can use sdp=http:// only once" );
722 if( HttpSetup( p_stream, &url ) )
724 msg_Err( p_stream, "cannot export sdp as http" );
727 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
729 if( p_sys->p_rtsp_url )
731 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
735 /* FIXME test if destination is multicast or no destination at all FIXME */
736 if( RtspSetup( p_stream, &url ) )
738 msg_Err( p_stream, "cannot export sdp as rtsp" );
741 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
742 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
744 p_sys->b_export_sap = VLC_TRUE;
745 SapSetup( p_stream );
747 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
749 if( p_sys->b_export_sdp_file )
751 msg_Err( p_stream, "you can use sdp=file:// only once" );
754 p_sys->b_export_sdp_file = VLC_TRUE;
755 psz_url = &psz_url[5];
756 if( psz_url[0] == '/' && psz_url[1] == '/' )
758 p_sys->psz_sdp_file = strdup( psz_url );
762 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
765 vlc_UrlClean( &url );
768 /*****************************************************************************
770 *****************************************************************************/
771 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
772 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
773 o= - should be local username (no spaces allowed)
774 o= time should be hashed with some other value to garantue uniqueness
775 o= we need IP6 support?
776 o= don't use the localhost address. use fully qualified domain name or IP4 address
777 p= international phone number (pass via vars?)
779 a= recvonly (missing)
780 a= type:broadcast (missing)
781 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
782 a= x-plgroup: (missing)
783 RTP packets need to get the correct src IP address */
784 static char *SDPGenerate( const sout_stream_t *p_stream,
785 const char *psz_destination, vlc_bool_t b_rtsp )
787 sout_stream_sys_t *p_sys = p_stream->p_sys;
789 char *psz_sdp, *p, ipv;
792 /* FIXME: breaks IP version check on unknown destination */
793 if( psz_destination == NULL )
794 psz_destination = "0.0.0.0";
796 i_size = sizeof( "v=0\r\n" ) +
797 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
798 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
799 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
800 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
801 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
802 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
803 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
804 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
805 strlen( psz_destination ) ;
806 for( i = 0; i < p_sys->i_es; i++ )
808 sout_stream_id_t *id = p_sys->es[i];
810 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
813 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
817 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
821 i_size += strlen( "b=AS: *\r\n") + 10;
825 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
830 i_size += strlen( "m=video %d RTP/AVP %d\r\n" ) +10 +10;
833 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
835 p = psz_sdp = malloc( i_size );
836 p += sprintf( p, "v=0\r\n" );
837 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
838 p_sys->i_sdp_id, p_sys->i_sdp_version,
839 ipv, ipv == '6' ? "::" : "127.0.0.1" );
840 if( *p_sys->psz_session_name )
841 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
842 if( *p_sys->psz_session_description )
843 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
844 if( *p_sys->psz_session_url )
845 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
846 if( *p_sys->psz_session_email )
847 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
849 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
850 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
852 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
854 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
856 /* Add the ttl if it is a multicast address */
857 /* FIXME: 1 is not a correct default value in the case of IPv6 */
858 p += sprintf( p, "/%d\r\n", p_sys->i_ttl ?: 1 );
862 p += sprintf( p, "\r\n" );
865 for( i = 0; i < p_sys->i_es; i++ )
867 sout_stream_id_t *id = p_sys->es[i];
869 if( id->i_cat == AUDIO_ES )
871 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
872 id->i_port, id->i_payload_type );
874 else if( id->i_cat == VIDEO_ES )
876 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
877 id->i_port, id->i_payload_type );
885 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
890 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
895 p += sprintf(p,"b=AS:%d\r\n",id->i_bitrate);
899 p += sprintf( p, "a=control:trackID=%d\r\n", i );
904 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
905 p_sys->i_port, p_sys->i_payload_type );
911 /*****************************************************************************
913 *****************************************************************************/
914 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
915 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
916 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
917 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
918 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
919 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
920 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
921 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
922 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
923 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
924 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
926 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
928 static const char hex[16] = "0123456789abcdef";
931 for( i = 0; i < i_data; i++ )
933 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
934 s[2*i+1] = hex[(p_data[i] )&0xf];
939 static const char basis_64[] =
940 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
942 int ap_base64encode_len(int len)
944 return ((len + 2) / 3 * 4) + 1;
947 int ap_base64encode_binary(char *encoded,
948 const unsigned char *string, int len)
954 for (i = 0; i < len - 2; i += 3) {
955 *p++ = basis_64[(string[i] >> 2) & 0x3F];
956 *p++ = basis_64[((string[i] & 0x3) << 4) |
957 ((int) (string[i + 1] & 0xF0) >> 4)];
958 *p++ = basis_64[((string[i + 1] & 0xF) << 2) |
959 ((int) (string[i + 2] & 0xC0) >> 6)];
960 *p++ = basis_64[string[i + 2] & 0x3F];
963 *p++ = basis_64[(string[i] >> 2) & 0x3F];
964 if (i == (len - 1)) {
965 *p++ = basis_64[((string[i] & 0x3) << 4)];
969 *p++ = basis_64[((string[i] & 0x3) << 4) |
970 ((int) (string[i + 1] & 0xF0) >> 4)];
971 *p++ = basis_64[((string[i + 1] & 0xF) << 2)];
980 int ap_base64encode(char *encoded, const char *string, int len)
982 return ap_base64encode_binary(encoded, (const unsigned char *) string, len);
985 char *b64_encode(char *buf, int len)
993 elen = ap_base64encode_len(len);
994 out = (char *) malloc(sizeof(char) * (elen + 1));
996 ap_base64encode(out, buf, len);
1001 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
1003 sout_instance_t *p_sout = p_stream->p_sout;
1004 sout_stream_sys_t *p_sys = p_stream->p_sys;
1005 sout_stream_id_t *id;
1006 sout_access_out_t *p_access = NULL;
1010 if( p_sys->p_mux != NULL )
1012 sout_input_t *p_input = NULL;
1013 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
1015 msg_Err( p_stream, "cannot add this stream to the muxer" );
1019 id = malloc( sizeof( sout_stream_id_t ) );
1020 memset( id, 0, sizeof( sout_stream_id_t ) );
1021 id->p_access = NULL;
1022 id->p_input = p_input;
1023 id->pf_packetize= NULL;
1024 id->p_rtsp_url = NULL;
1030 /* Choose the port */
1032 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
1034 i_port = p_sys->i_port_audio;
1035 p_sys->i_port_audio = 0;
1037 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
1039 i_port = p_sys->i_port_video;
1040 p_sys->i_port_video = 0;
1042 while( i_port == 0 )
1044 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
1046 i_port = p_sys->i_port;
1053 if( p_sys->psz_destination )
1056 char url[NI_MAXHOST + 8];
1058 /* first try to create the access out */
1061 snprintf( access, sizeof( access ), "udp{raw,ttl=%d}",
1063 access[sizeof( access ) - 1] = '\0';
1066 strcpy( access, "udp{raw}" );
1068 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
1069 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
1070 p_sys->psz_destination, i_port );
1071 url[sizeof( url ) - 1] = '\0';
1073 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
1075 msg_Err( p_stream, "cannot create the access out for %s://%s",
1079 msg_Dbg( p_stream, "access out %s:%s", access, url );
1082 /* not create the rtp specific stuff */
1083 id = malloc( sizeof( sout_stream_id_t ) );
1084 memset( id, 0, sizeof( sout_stream_id_t ) );
1085 id->p_stream = p_stream;
1086 id->p_access = p_access;
1088 id->psz_rtpmap = NULL;
1089 id->psz_fmtp = NULL;
1090 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
1091 id->i_port = i_port;
1092 id->p_rtsp_url = NULL;
1093 vlc_mutex_init( p_stream, &id->lock_rtsp );
1094 id->i_rtsp_access = 0;
1095 id->rtsp_access = NULL;
1097 switch( p_fmt->i_codec )
1099 case VLC_FOURCC( 's', '1', '6', 'b' ):
1100 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
1102 id->i_payload_type = 11;
1104 else if( p_fmt->audio.i_channels == 2 &&
1105 p_fmt->audio.i_rate == 44100 )
1107 id->i_payload_type = 10;
1111 id->i_payload_type = p_sys->i_payload_type++;
1113 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
1114 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
1115 p_fmt->audio.i_channels );
1116 id->i_clock_rate = p_fmt->audio.i_rate;
1117 id->pf_packetize = rtp_packetize_l16;
1119 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
1120 id->i_payload_type = p_sys->i_payload_type++;
1121 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
1122 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
1123 p_fmt->audio.i_channels );
1124 id->i_clock_rate = p_fmt->audio.i_rate;
1125 id->pf_packetize = rtp_packetize_l8;
1127 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1128 id->i_payload_type = 14;
1129 id->i_clock_rate = 90000;
1130 id->psz_rtpmap = strdup( "MPA/90000" );
1131 id->pf_packetize = rtp_packetize_mpa;
1133 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1134 id->i_payload_type = 32;
1135 id->i_clock_rate = 90000;
1136 id->psz_rtpmap = strdup( "MPV/90000" );
1137 id->pf_packetize = rtp_packetize_mpv;
1139 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1140 id->i_payload_type = p_sys->i_payload_type++;
1141 id->i_clock_rate = 90000;
1142 id->psz_rtpmap = strdup( "ac3/90000" );
1143 id->pf_packetize = rtp_packetize_ac3;
1145 case VLC_FOURCC( 'H', '2', '6', '3' ):
1146 id->i_payload_type = p_sys->i_payload_type++;
1147 id->i_clock_rate = 90000;
1148 id->psz_rtpmap = strdup( "H263-1998/90000" );
1149 id->pf_packetize = rtp_packetize_h263;
1151 case VLC_FOURCC( 'h', '2', '6', '4' ):
1152 id->i_payload_type = p_sys->i_payload_type++;
1153 id->i_clock_rate = 90000;
1154 id->psz_rtpmap = strdup( "H264/90000" );
1155 id->pf_packetize = rtp_packetize_h264;
1156 if( p_fmt->i_extra > 0 )
1158 uint8_t *p_buffer = p_fmt->p_extra;
1159 int i_buffer = p_fmt->i_extra;
1160 char *p_64_sps = NULL;
1161 char *p_64_pps = NULL;
1164 while( i_buffer > 4 &&
1165 p_buffer[0] == 0 && p_buffer[1] == 0 &&
1166 p_buffer[2] == 0 && p_buffer[3] == 1 )
1168 const int i_nal_type = p_buffer[4]&0x1f;
1171 int i_startcode = 0;
1174 msg_Dbg( p_stream, "we found a startcode for NAL with TYPE:%d", i_nal_type );
1176 for( i_offset = 1; i_offset+3 < i_buffer ; i_offset++)
1178 if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 0 && p_buffer[i_offset+3] == 1 )
1180 /* we found another startcode */
1181 i_startcode = i_offset;
1185 i_size = i_startcode ? i_startcode : i_buffer;
1186 if( i_nal_type == 7 )
1188 p_64_sps = (char *)malloc( ap_base64encode_len( i_size - 4) );
1189 i_encoded = ap_base64encode_binary( p_64_sps, &p_buffer[4], i_size - 4 );
1190 p_64_sps[i_encoded] = '\0';
1191 sprintf_hexa( hexa, &p_buffer[5], 3 );
1194 if( i_nal_type == 8 )
1196 p_64_pps = (char *)malloc( ap_base64encode_len( i_size - 4) );
1197 i_encoded = ap_base64encode_binary( p_64_pps, &p_buffer[4], i_size - 4 );
1198 p_64_pps[i_encoded] = '\0';
1203 /* FIXME: All this is a bit unsafe */
1204 asprintf( &id->psz_fmtp, "packetization-mode=1;profile-level-id=%s;sprop-parameter-sets=%s,%s;", hexa, p_64_sps, p_64_pps );
1209 id->psz_fmtp = strdup( "packetization-mode=1" );
1212 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1214 char hexa[2*p_fmt->i_extra +1];
1216 id->i_payload_type = p_sys->i_payload_type++;
1217 id->i_clock_rate = 90000;
1218 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1219 id->pf_packetize = rtp_packetize_split;
1220 if( p_fmt->i_extra > 0 )
1222 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1223 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1224 sprintf( id->psz_fmtp,
1225 "profile-level-id=3; config=%s;", hexa );
1229 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1231 id->i_payload_type = p_sys->i_payload_type++;
1232 id->i_clock_rate = p_fmt->audio.i_rate;
1236 char hexa[2*p_fmt->i_extra +1];
1238 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1239 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1240 id->pf_packetize = rtp_packetize_mp4a;
1241 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1242 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1243 sprintf( id->psz_fmtp,
1244 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1245 "config=%s; SizeLength=13;IndexLength=3; "
1246 "IndexDeltaLength=3; Profile=1;", hexa );
1252 unsigned char config[6];
1253 unsigned int aacsrates[15] = {
1254 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1255 16000, 12000, 11025, 8000, 7350, 0, 0 };
1257 for( i = 0; i < 15; i++ )
1258 if( p_fmt->audio.i_rate == aacsrates[i] )
1264 config[3]=p_fmt->audio.i_channels<<4;
1268 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1269 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1270 id->pf_packetize = rtp_packetize_mp4a_latm;
1271 sprintf_hexa( hexa, config, 6 );
1272 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1273 "object=2; cpresent=0; config=%s", hexa );
1277 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1278 id->i_payload_type = p_sys->i_payload_type++;
1279 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1280 "AMR/8000/2" : "AMR/8000" );
1281 id->psz_fmtp = strdup( "octet-align=1" );
1282 id->i_clock_rate = p_fmt->audio.i_rate;
1283 id->pf_packetize = rtp_packetize_amr;
1285 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1286 id->i_payload_type = p_sys->i_payload_type++;
1287 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1288 "AMR-WB/16000/2" : "AMR-WB/16000" );
1289 id->psz_fmtp = strdup( "octet-align=1" );
1290 id->i_clock_rate = p_fmt->audio.i_rate;
1291 id->pf_packetize = rtp_packetize_amr;
1295 msg_Err( p_stream, "cannot add this stream (unsupported "
1296 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1299 sout_AccessOutDelete( p_access );
1304 id->i_cat = p_fmt->i_cat;
1306 id->ssrc[0] = rand()&0xff;
1307 id->ssrc[1] = rand()&0xff;
1308 id->ssrc[2] = rand()&0xff;
1309 id->ssrc[3] = rand()&0xff;
1310 id->i_sequence = rand()&0xffff;
1311 id->i_timestamp_start = rand()&0xffffffff;
1312 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
1314 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1315 if( id->i_mtu <= 16 + MTU_REDUCE )
1317 /* better than nothing */
1320 id->i_mtu -= MTU_REDUCE;
1321 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1323 if( p_sys->p_rtsp_url )
1325 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1327 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1328 msg_Dbg( p_stream, "rtsp: adding %s\n", psz_urlc );
1329 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1331 if( id->p_rtsp_url )
1333 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1334 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1335 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1340 /* Update p_sys context */
1341 vlc_mutex_lock( &p_sys->lock_es );
1342 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1343 vlc_mutex_unlock( &p_sys->lock_es );
1345 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1347 vlc_mutex_lock( &p_sys->lock_sdp );
1348 free( p_sys->psz_sdp );
1349 p_sys->psz_sdp = psz_sdp;
1350 vlc_mutex_unlock( &p_sys->lock_sdp );
1352 p_sys->i_sdp_version++;
1354 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
1356 /* Update SDP (sap/file) */
1357 if( p_sys->b_export_sap ) SapSetup( p_stream );
1358 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1363 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1365 sout_stream_sys_t *p_sys = p_stream->p_sys;
1367 vlc_mutex_lock( &p_sys->lock_es );
1368 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1369 vlc_mutex_unlock( &p_sys->lock_es );
1372 if( id->i_port > 0 )
1374 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1375 p_sys->i_port_audio = id->i_port;
1376 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1377 p_sys->i_port_video = id->i_port;
1382 if( id->psz_rtpmap )
1384 free( id->psz_rtpmap );
1388 free( id->psz_fmtp );
1390 if( id->psz_destination )
1391 free( id->psz_destination );
1392 sout_AccessOutDelete( id->p_access );
1394 else if( id->p_input )
1396 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1398 if( id->p_rtsp_url )
1400 httpd_UrlDelete( id->p_rtsp_url );
1402 vlc_mutex_destroy( &id->lock_rtsp );
1403 if( id->rtsp_access ) free( id->rtsp_access );
1405 /* Update SDP (sap/file) */
1406 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1407 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1413 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1418 if( p_stream->p_sys->p_mux )
1420 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1426 p_next = p_buffer->p_next;
1427 if( id->pf_packetize( p_stream, id, p_buffer ) )
1431 block_Release( p_buffer );
1439 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1442 sout_stream_sys_t *p_sys = p_stream->p_sys;
1444 int64_t i_dts = p_buffer->i_dts;
1445 uint32_t i_timestamp = i_dts * 9 / 100;
1447 uint8_t *p_data = p_buffer->p_buffer;
1448 unsigned int i_data = p_buffer->i_buffer;
1449 unsigned int i_max = p_sys->i_mtu - 12;
1451 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1455 unsigned int i_size;
1457 /* output complete packet */
1458 if( p_sys->packet &&
1459 p_sys->packet->i_buffer + i_data > i_max )
1461 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1462 p_sys->packet = NULL;
1465 if( p_sys->packet == NULL )
1467 /* allocate a new packet */
1468 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1469 p_sys->packet->p_buffer[ 0] = 0x80;
1470 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1471 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1472 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1473 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1474 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1475 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1476 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1477 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1478 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1479 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1480 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1481 p_sys->packet->i_buffer = 12;
1483 p_sys->packet->i_dts = i_dts;
1484 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1485 i_dts += p_sys->packet->i_length;
1487 p_sys->i_sequence++;
1490 i_size = __MIN( i_data,
1491 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1493 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1496 p_sys->packet->i_buffer += i_size;
1504 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1507 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1509 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1515 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1517 p_next = p_buffer->p_next;
1518 block_Release( p_buffer );
1525 /****************************************************************************
1527 ****************************************************************************/
1528 static int SapSetup( sout_stream_t *p_stream )
1530 sout_stream_sys_t *p_sys = p_stream->p_sys;
1531 sout_instance_t *p_sout = p_stream->p_sout;
1532 announce_method_t *p_method = sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
1534 /* Remove the previous session */
1535 if( p_sys->p_session != NULL)
1537 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1538 sout_AnnounceSessionDestroy( p_sys->p_session );
1539 p_sys->p_session = NULL;
1542 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1544 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1545 p_sys->psz_destination,
1553 /****************************************************************************
1555 ****************************************************************************/
1556 static int FileSetup( sout_stream_t *p_stream )
1558 sout_stream_sys_t *p_sys = p_stream->p_sys;
1561 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1563 msg_Err( p_stream, "cannot open file '%s' (%s)",
1564 p_sys->psz_sdp_file, strerror(errno) );
1565 return VLC_EGENERIC;
1568 fprintf( f, "%s", p_sys->psz_sdp );
1574 /****************************************************************************
1576 ****************************************************************************/
1577 static int HttpCallback( httpd_file_sys_t *p_args,
1578 httpd_file_t *, uint8_t *p_request,
1579 uint8_t **pp_data, int *pi_data );
1581 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1583 sout_stream_sys_t *p_sys = p_stream->p_sys;
1585 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1586 if( p_sys->p_httpd_host )
1588 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1589 url->psz_path ? url->psz_path : "/",
1592 HttpCallback, (void*)p_sys );
1594 if( p_sys->p_httpd_file == NULL )
1596 return VLC_EGENERIC;
1601 static int HttpCallback( httpd_file_sys_t *p_args,
1602 httpd_file_t *f, uint8_t *p_request,
1603 uint8_t **pp_data, int *pi_data )
1605 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1607 vlc_mutex_lock( &p_sys->lock_sdp );
1608 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1610 *pi_data = strlen( p_sys->psz_sdp );
1611 *pp_data = malloc( *pi_data );
1612 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1619 vlc_mutex_unlock( &p_sys->lock_sdp );
1624 /****************************************************************************
1626 ****************************************************************************/
1627 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1629 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1631 rtsp->psz_session = psz_session;
1633 rtsp->b_playing = VLC_FALSE;
1637 rtsp->access = NULL;
1639 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1643 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1647 if( psz_session ) return NULL;
1649 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1651 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1653 return p_stream->p_sys->rtsp[i];
1659 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1662 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1664 for( i = 0; i < rtsp->i_access; i++ )
1666 sout_AccessOutDelete( rtsp->access[i] );
1668 if( rtsp->id ) free( rtsp->id );
1669 if( rtsp->access ) free( rtsp->access );
1671 free( rtsp->psz_session );
1675 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1677 sout_stream_sys_t *p_sys = p_stream->p_sys;
1679 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1681 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1682 if( p_sys->p_rtsp_host == NULL )
1684 return VLC_EGENERIC;
1687 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1688 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1689 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1690 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1692 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1693 if( p_sys->p_rtsp_url == 0 )
1695 return VLC_EGENERIC;
1697 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1698 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1699 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1700 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1705 static int RtspCallback( httpd_callback_sys_t *p_args,
1707 httpd_message_t *answer, httpd_message_t *query )
1709 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1710 sout_stream_sys_t *p_sys = p_stream->p_sys;
1711 char *psz_destination = p_sys->psz_destination;
1712 char *psz_session = NULL;
1713 char *psz_cseq = NULL;
1716 if( answer == NULL || query == NULL )
1720 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1722 answer->i_proto = HTTPD_PROTO_RTSP;
1723 answer->i_version= query->i_version;
1724 answer->i_type = HTTPD_MSG_ANSWER;
1726 switch( query->i_type )
1728 case HTTPD_MSG_DESCRIBE:
1730 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1732 answer->i_status = 200;
1733 answer->psz_status = strdup( "OK" );
1734 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1735 httpd_MsgAdd( answer, "Content-Base", "%s/", p_sys->psz_rtsp_control );
1736 answer->p_body = (uint8_t *)psz_sdp;
1737 answer->i_body = strlen( psz_sdp );
1741 case HTTPD_MSG_PLAY:
1743 rtsp_client_t *rtsp;
1744 /* for now only multicast so easy */
1745 answer->i_status = 200;
1746 answer->psz_status = strdup( "OK" );
1748 answer->p_body = NULL;
1750 psz_session = httpd_MsgGet( query, "Session" );
1751 rtsp = RtspClientGet( p_stream, psz_session );
1752 if( rtsp && !rtsp->b_playing )
1756 rtsp->b_playing = VLC_TRUE;
1758 vlc_mutex_lock( &p_sys->lock_es );
1759 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1761 sout_stream_id_t *id = rtsp->id[i_id];
1764 for( i = 0; i < p_sys->i_es; i++ )
1766 if( id == p_sys->es[i] )
1769 if( i >= p_sys->i_es ) continue;
1771 vlc_mutex_lock( &id->lock_rtsp );
1772 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1773 vlc_mutex_unlock( &id->lock_rtsp );
1775 vlc_mutex_unlock( &p_sys->lock_es );
1779 case HTTPD_MSG_PAUSE:
1781 return VLC_EGENERIC;
1782 case HTTPD_MSG_TEARDOWN:
1784 rtsp_client_t *rtsp;
1786 /* for now only multicast so easy again */
1787 answer->i_status = 200;
1788 answer->psz_status = strdup( "OK" );
1790 answer->p_body = NULL;
1792 psz_session = httpd_MsgGet( query, "Session" );
1793 rtsp = RtspClientGet( p_stream, psz_session );
1798 vlc_mutex_lock( &p_sys->lock_es );
1799 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1801 sout_stream_id_t *id = rtsp->id[i_id];
1804 for( i = 0; i < p_sys->i_es; i++ )
1806 if( id == p_sys->es[i] )
1809 if( i >= p_sys->i_es ) continue;
1811 vlc_mutex_lock( &id->lock_rtsp );
1812 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1813 vlc_mutex_unlock( &id->lock_rtsp );
1815 vlc_mutex_unlock( &p_sys->lock_es );
1817 RtspClientDel( p_stream, rtsp );
1823 return VLC_EGENERIC;
1825 httpd_MsgAdd( answer, "Server", PACKAGE_STRING );
1826 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1827 psz_cseq = httpd_MsgGet( query, "Cseq" );
1829 i_cseq = atoi( psz_cseq );
1832 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1833 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1837 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1842 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1844 httpd_message_t *answer, httpd_message_t *query )
1846 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1847 sout_stream_t *p_stream = id->p_stream;
1848 sout_stream_sys_t *p_sys = p_stream->p_sys;
1849 char *psz_session = NULL;
1850 char *psz_cseq = NULL;
1854 if( answer == NULL || query == NULL )
1858 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1860 answer->i_proto = HTTPD_PROTO_RTSP;
1861 answer->i_version= query->i_version;
1862 answer->i_type = HTTPD_MSG_ANSWER;
1864 switch( query->i_type )
1866 case HTTPD_MSG_SETUP:
1868 char *psz_transport = httpd_MsgGet( query, "Transport" );
1870 //fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1872 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1874 //fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1875 answer->i_status = 200;
1876 answer->psz_status = strdup( "OK" );
1878 answer->p_body = NULL;
1879 psz_session = httpd_MsgGet( query, "Session" );
1882 psz_session = malloc( 100 );
1883 sprintf( psz_session, "%d", rand() );
1885 httpd_MsgAdd( answer, "Transport",
1886 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1887 id->psz_destination, id->i_port,id->i_port+1,
1890 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1892 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1893 char ip[NI_MAXNUMERICHOST], psz_access[17], psz_url[NI_MAXNUMERICHOST + 8];
1895 sout_access_out_t *p_access;
1897 rtsp_client_t *rtsp = NULL;
1899 if( httpd_ClientIP( cl, ip ) == NULL )
1901 answer->i_status = 500;
1902 answer->psz_status = strdup( "Internal server error" );
1904 answer->p_body = NULL;
1908 //fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n", ip, i_port );
1910 psz_session = httpd_MsgGet( query, "Session" );
1913 psz_session = malloc( 100 );
1914 sprintf( psz_session, "%d", rand() );
1916 rtsp = RtspClientNew( p_stream, psz_session );
1920 rtsp = RtspClientGet( p_stream, psz_session );
1923 answer->i_status = 454;
1924 answer->psz_status = strdup( "Unknown session id" );
1926 answer->p_body = NULL;
1931 /* first try to create the access out */
1933 snprintf( psz_access, sizeof( psz_access ),
1934 "udp{raw,ttl=%d}", p_sys->i_ttl );
1936 strlcpy( psz_access, "udp{raw}", sizeof( psz_access ) );
1938 snprintf( psz_url, sizeof( psz_url ),
1939 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
1942 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1944 msg_Err( p_stream, "cannot create the access out for %s://%s",
1945 psz_access, psz_url );
1946 answer->i_status = 500;
1947 answer->psz_status = strdup( "Internal server error" );
1949 answer->p_body = NULL;
1953 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1954 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1956 answer->i_status = 200;
1957 answer->psz_status = strdup( "OK" );
1959 answer->p_body = NULL;
1961 httpd_MsgAdd( answer, "Transport",
1962 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1964 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1966 answer->i_status = 461;
1967 answer->psz_status = strdup( "Unsupported Transport" );
1969 answer->p_body = NULL;
1975 return VLC_EGENERIC;
1977 httpd_MsgAdd( answer, "Server", "VLC Server" );
1978 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1979 psz_cseq = httpd_MsgGet( query, "Cseq" );
1981 i_cseq = atoi( psz_cseq );
1984 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1985 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1989 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1994 /****************************************************************************
1996 ****************************************************************************/
1997 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1998 int b_marker, int64_t i_pts )
2000 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
2002 out->p_buffer[0] = 0x80;
2003 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
2004 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
2005 out->p_buffer[3] = ( id->i_sequence )&0xff;
2006 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
2007 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
2008 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
2009 out->p_buffer[7] = ( i_timestamp )&0xff;
2011 out->p_buffer[ 8] = id->ssrc[0];
2012 out->p_buffer[ 9] = id->ssrc[1];
2013 out->p_buffer[10] = id->ssrc[2];
2014 out->p_buffer[11] = id->ssrc[3];
2020 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
2023 vlc_mutex_lock( &id->lock_rtsp );
2024 for( i = 0; i < id->i_rtsp_access; i++ )
2026 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
2028 vlc_mutex_unlock( &id->lock_rtsp );
2032 sout_AccessOutWrite( id->p_access, out );
2036 block_Release( out );
2040 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
2043 int i_max = id->i_mtu - 12 - 4; /* 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, 16 + 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] = 0;
2059 out->p_buffer[13] = 0;
2060 /* fragment offset in the current frame */
2061 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
2062 out->p_buffer[15] = ( (i*i_max) )&0xff;
2063 memcpy( &out->p_buffer[16], p_data, i_payload );
2065 out->i_buffer = 16 + i_payload;
2066 out->i_dts = in->i_dts + i * in->i_length / i_count;
2067 out->i_length = in->i_length / i_count;
2069 rtp_packetize_send( id, out );
2071 p_data += i_payload;
2072 i_data -= i_payload;
2079 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
2082 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
2083 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2085 uint8_t *p_data = in->p_buffer;
2086 int i_data = in->i_buffer;
2088 int b_sequence_start = 0;
2089 int i_temporal_ref = 0;
2090 int i_picture_coding_type = 0;
2091 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
2092 int b_start_slice = 0;
2094 /* preparse this packet to get some info */
2095 if( in->i_buffer > 4 )
2097 uint8_t *p = p_data;
2098 int i_rest = in->i_buffer;
2102 while( i_rest > 4 &&
2103 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
2117 /* sequence start code */
2118 b_sequence_start = 1;
2120 else if( *p == 0x00 && i_rest >= 4 )
2123 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
2124 i_picture_coding_type = (p[2] >> 3)&0x07;
2126 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
2127 i_picture_coding_type == 3 ) )
2129 i_ffv = (p[3] >> 2)&0x01;
2130 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
2131 if( i_rest > 4 && i_picture_coding_type == 3 )
2133 i_fbv = (p[4]>>6)&0x01;
2134 i_bfc = (p[4]>>3)&0x07;
2138 else if( *p <= 0xaf )
2145 for( i = 0; i < i_count; i++ )
2147 int i_payload = __MIN( i_max, i_data );
2148 block_t *out = block_New( p_stream,
2150 uint32_t h = ( i_temporal_ref << 16 )|
2151 ( b_sequence_start << 13 )|
2152 ( b_start_slice << 12 )|
2153 ( i == i_count - 1 ? 1 << 11 : 0 )|
2154 ( i_picture_coding_type << 8 )|
2155 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
2157 /* rtp common header */
2158 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2159 in->i_pts > 0 ? in->i_pts : in->i_dts );
2161 /* 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 */
2162 out->p_buffer[12] = ( h >> 24 )&0xff;
2163 out->p_buffer[13] = ( h >> 16 )&0xff;
2164 out->p_buffer[14] = ( h >> 8 )&0xff;
2165 out->p_buffer[15] = ( h )&0xff;
2167 memcpy( &out->p_buffer[16], p_data, i_payload );
2169 out->i_buffer = 16 + i_payload;
2170 out->i_dts = in->i_dts + i * in->i_length / i_count;
2171 out->i_length = in->i_length / i_count;
2173 rtp_packetize_send( id, out );
2175 p_data += i_payload;
2176 i_data -= i_payload;
2182 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
2185 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
2186 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2188 uint8_t *p_data = in->p_buffer;
2189 int i_data = in->i_buffer;
2192 for( i = 0; i < i_count; i++ )
2194 int i_payload = __MIN( i_max, i_data );
2195 block_t *out = block_New( p_stream, 14 + i_payload );
2197 /* rtp common header */
2198 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
2200 out->p_buffer[12] = 1;
2202 out->p_buffer[13] = 0x00;
2204 memcpy( &out->p_buffer[14], p_data, i_payload );
2206 out->i_buffer = 14 + i_payload;
2207 out->i_dts = in->i_dts + i * in->i_length / i_count;
2208 out->i_length = in->i_length / i_count;
2210 rtp_packetize_send( id, out );
2212 p_data += i_payload;
2213 i_data -= i_payload;
2219 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
2222 int i_max = id->i_mtu - 12; /* payload max in one packet */
2223 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2225 uint8_t *p_data = in->p_buffer;
2226 int i_data = in->i_buffer;
2229 for( i = 0; i < i_count; i++ )
2231 int i_payload = __MIN( i_max, i_data );
2232 block_t *out = block_New( p_stream, 12 + i_payload );
2234 /* rtp common header */
2235 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2236 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2237 memcpy( &out->p_buffer[12], p_data, i_payload );
2239 out->i_buffer = 12 + i_payload;
2240 out->i_dts = in->i_dts + i * in->i_length / i_count;
2241 out->i_length = in->i_length / i_count;
2243 rtp_packetize_send( id, out );
2245 p_data += i_payload;
2246 i_data -= i_payload;
2253 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
2256 int i_max = id->i_mtu - 14; /* payload max in one packet */
2257 int latmhdrsize = in->i_buffer / 0xff + 1;
2258 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2260 uint8_t *p_data = in->p_buffer, *p_header = NULL;
2261 int i_data = in->i_buffer;
2264 for( i = 0; i < i_count; i++ )
2266 int i_payload = __MIN( i_max, i_data );
2271 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
2273 /* rtp common header */
2274 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
2275 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2279 int tmp = in->i_buffer;
2281 p_header=out->p_buffer+12;
2291 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
2293 out->i_buffer = 12 + latmhdrsize + i_payload;
2294 out->i_dts = in->i_dts + i * in->i_length / i_count;
2295 out->i_length = in->i_length / i_count;
2297 rtp_packetize_send( id, out );
2299 p_data += i_payload;
2300 i_data -= i_payload;
2306 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
2309 int i_max = id->i_mtu - 12; /* payload max in one packet */
2310 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2312 uint8_t *p_data = in->p_buffer;
2313 int i_data = in->i_buffer;
2318 int i_payload = (__MIN( i_max, i_data )/4)*4;
2319 block_t *out = block_New( p_stream, 12 + i_payload );
2321 /* rtp common header */
2322 rtp_packetize_common( id, out, 0,
2323 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2324 memcpy( &out->p_buffer[12], p_data, i_payload );
2326 out->i_buffer = 12 + i_payload;
2327 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2328 out->i_length = in->i_length / i_count;
2330 rtp_packetize_send( id, out );
2332 p_data += i_payload;
2333 i_data -= i_payload;
2340 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2343 int i_max = id->i_mtu - 12; /* payload max in one packet */
2344 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2346 uint8_t *p_data = in->p_buffer;
2347 int i_data = in->i_buffer;
2352 int i_payload = (__MIN( i_max, i_data )/2)*2;
2353 block_t *out = block_New( p_stream, 12 + i_payload );
2355 /* rtp common header */
2356 rtp_packetize_common( id, out, 0,
2357 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2358 memcpy( &out->p_buffer[12], p_data, i_payload );
2360 out->i_buffer = 12 + i_payload;
2361 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2362 out->i_length = in->i_length / i_count;
2364 rtp_packetize_send( id, out );
2366 p_data += i_payload;
2367 i_data -= i_payload;
2373 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2376 int i_max = id->i_mtu - 16; /* payload max in one packet */
2377 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2379 uint8_t *p_data = in->p_buffer;
2380 int i_data = in->i_buffer;
2383 for( i = 0; i < i_count; i++ )
2385 int i_payload = __MIN( i_max, i_data );
2386 block_t *out = block_New( p_stream, 16 + i_payload );
2388 /* rtp common header */
2389 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2390 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2392 /* AU headers length (bits) */
2393 out->p_buffer[12] = 0;
2394 out->p_buffer[13] = 2*8;
2395 /* for each AU length 13 bits + idx 3bits, */
2396 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2397 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2399 memcpy( &out->p_buffer[16], p_data, i_payload );
2401 out->i_buffer = 16 + i_payload;
2402 out->i_dts = in->i_dts + i * in->i_length / i_count;
2403 out->i_length = in->i_length / i_count;
2405 rtp_packetize_send( id, out );
2407 p_data += i_payload;
2408 i_data -= i_payload;
2416 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2417 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2418 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2421 uint8_t *p_data = in->p_buffer;
2422 int i_data = in->i_buffer;
2424 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2427 int b_v_bit = 0; // no pesky error resilience
2428 int i_plen = 0; // normally plen=0 for PSC packet
2429 int i_pebit = 0; // because plen=0
2434 return VLC_EGENERIC;
2436 if( p_data[0] || p_data[1] )
2438 return VLC_EGENERIC;
2440 /* remove 2 leading 0 bytes */
2443 i_count = ( i_data + i_max - 1 ) / i_max;
2445 for( i = 0; i < i_count; i++ )
2447 int i_payload = __MIN( i_max, i_data );
2448 block_t *out = block_New( p_stream,
2449 RTP_H263_PAYLOAD_START + i_payload );
2450 b_p_bit = (i == 0) ? 1 : 0;
2451 h = ( b_p_bit << 10 )|
2456 /* rtp common header */
2457 //b_m_bit = 1; // always contains end of frame
2458 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2459 in->i_pts > 0 ? in->i_pts : in->i_dts );
2462 out->p_buffer[12] = ( h >> 8 )&0xff;
2463 out->p_buffer[13] = ( h )&0xff;
2464 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2466 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2467 out->i_dts = in->i_dts + i * in->i_length / i_count;
2468 out->i_length = in->i_length / i_count;
2470 rtp_packetize_send( id, out );
2472 p_data += i_payload;
2473 i_data -= i_payload;
2480 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
2483 int i_max = id->i_mtu - 12; /* payload max in one packet */
2484 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2485 uint8_t *p_data = in->p_buffer;
2486 int i_data = in->i_buffer;
2491 while( i_data > 5 &&
2492 ( p_data[0] != 0x00 || p_data[1] != 0x00 || p_data[2] != 0x01 || /* startcode */
2493 (p_data[3]&0x1f) < 1 || (p_data[3]&0x1f) > 23 ) ) /* naltype should be between 1 and 23 */
2504 i_nal_type = p_data[0]&0x1f;
2506 if( i_data <= i_max ) /* The whole pack will fit in one rtp payload */
2509 i_payload = __MIN( i_max, i_data );
2510 out = block_New( p_stream, 12 + i_payload );
2512 /* rtp common header */
2513 rtp_packetize_common( id, out, 1,
2514 in->i_pts > 0 ? in->i_pts : in->i_dts );
2516 memcpy( &out->p_buffer[12], p_data, i_payload );
2518 out->i_buffer = 12 + i_payload;
2519 out->i_dts = in->i_dts;
2520 out->i_length = in->i_length;
2522 rtp_packetize_send( id, out );
2524 /*msg_Dbg( p_stream, "nal-out plain %d %02x", i_payload, out->p_buffer[16] );*/
2529 uint8_t nalh; /* The nalheader byte */
2530 int i=0, start=1, end=0, first=0;
2536 i_max = id->i_mtu - 14;
2537 i_count = ( i_data + i_max - 1 ) / i_max;
2539 /*msg_Dbg( p_stream, "nal-out fragmented %02x %d", nalh, i_rest);*/
2543 i_payload = __MIN( i_max, i_data );
2544 out = block_New( p_stream, 14 + i_payload );
2546 if( i_data == i_payload )
2549 /* rtp common header */
2550 rtp_packetize_common( id, out, (end)?1:0,
2551 in->i_pts > 0 ? in->i_pts : in->i_dts );
2554 out->p_buffer[12] = (nalh&0x60)|28;
2556 out->p_buffer[13] = (start<<7)|(end<<6)|(nalh&0x1f);
2558 memcpy( &out->p_buffer[14], p_data+first, i_payload );
2560 out->i_buffer = 14 + i_payload;
2562 // not sure what of these should be used and what it does :)
2563 out->i_pts = in->i_pts;
2564 out->i_dts = in->i_dts;
2565 //out->i_dts = in->i_dts + i * in->i_length / i_count;
2566 //out->i_length = in->i_length / i_count;
2568 rtp_packetize_send( id, out );
2570 /*msg_Dbg( p_stream, "nal-out fragmented: frag %d %d %02x %02x %d", start,end,
2571 out->p_buffer[12], out->p_buffer[13], i_payload );*/
2573 i_data -= i_payload;
2582 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2585 int i_max = id->i_mtu - 14; /* payload max in one packet */
2586 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2588 uint8_t *p_data = in->p_buffer;
2589 int i_data = in->i_buffer;
2592 /* Only supports octet-aligned mode */
2593 for( i = 0; i < i_count; i++ )
2595 int i_payload = __MIN( i_max, i_data );
2596 block_t *out = block_New( p_stream, 14 + i_payload );
2598 /* rtp common header */
2599 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2600 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2601 /* Payload header */
2602 out->p_buffer[12] = 0xF0; /* CMR */
2603 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
2605 /* FIXME: are we fed multiple frames ? */
2606 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
2608 out->i_buffer = 14 + i_payload-1;
2609 out->i_dts = in->i_dts + i * in->i_length / i_count;
2610 out->i_length = in->i_length / i_count;
2612 rtp_packetize_send( id, out );
2614 p_data += i_payload;
2615 i_data -= i_payload;