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;
249 /* Packetizer specific fields */
250 pf_rtp_packetizer_t pf_packetize;
253 /* for sending the packets */
254 sout_access_out_t *p_access;
256 vlc_mutex_t lock_rtsp;
258 sout_access_out_t **rtsp_access;
261 sout_input_t *p_input;
263 /* RTSP url control */
264 httpd_url_t *p_rtsp_url;
267 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
269 static void SDPHandleUrl( sout_stream_t *, char * );
271 static int SapSetup( sout_stream_t *p_stream );
272 static int FileSetup( sout_stream_t *p_stream );
273 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
274 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
276 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
277 httpd_message_t *, httpd_message_t * );
278 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
279 httpd_message_t *, httpd_message_t * );
282 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
283 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
284 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
286 /*****************************************************************************
288 *****************************************************************************/
289 static int Open( vlc_object_t *p_this )
291 sout_stream_t *p_stream = (sout_stream_t*)p_this;
292 sout_instance_t *p_sout = p_stream->p_sout;
293 sout_stream_sys_t *p_sys = NULL;
294 sout_cfg_t *p_cfg = NULL;
296 vlc_bool_t b_rtsp = VLC_FALSE;
298 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
300 p_sys = malloc( sizeof( sout_stream_sys_t ) );
302 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
303 if( *p_sys->psz_destination == '\0' )
305 free( p_sys->psz_destination );
306 p_sys->psz_destination = NULL;
309 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
310 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
311 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
312 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
314 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
315 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
316 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
318 p_sys->psz_sdp_file = NULL;
320 if( p_sys->i_port_audio == p_sys->i_port_video )
322 msg_Err( p_stream, "audio and video port cannot be the same" );
323 p_sys->i_port_audio = 0;
324 p_sys->i_port_video = 0;
327 if( !p_sys->psz_session_name )
329 if( p_sys->psz_destination )
330 p_sys->psz_session_name = strdup( p_sys->psz_destination );
332 p_sys->psz_session_name = strdup( "NONE" );
335 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
337 if( !strcmp( p_cfg->psz_name, "sdp" ) )
339 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
349 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
350 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
352 free( val2.psz_string );
355 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
359 msg_Err( p_stream, "missing destination and not in rtsp mode" );
363 p_sys->psz_destination = NULL;
365 else if( p_sys->i_port <= 0 )
367 msg_Err( p_stream, "invalid port" );
372 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
375 /* Normally, we should let the default hop limit up to the core,
376 * but we have to know it to build our SDP properly, which is why
377 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
379 val.i_int = config_GetInt( p_stream, "ttl" );
381 if( val.i_int > 255 ) val.i_int = 255;
382 /* must not exceed 999 once formatted */
386 msg_Err( p_stream, "illegal TTL %d", val.i_int );
390 p_sys->i_ttl = val.i_int;
393 var_Get( p_stream, SOUT_CFG_PREFIX "mp4a-latm", &val );
394 p_sys->b_latm = val.b_bool;
396 p_sys->i_payload_type = 96;
401 p_sys->psz_sdp = NULL;
403 p_sys->i_sdp_id = mdate();
404 p_sys->i_sdp_version = 1;
405 p_sys->psz_sdp = NULL;
407 p_sys->b_export_sap = VLC_FALSE;
408 p_sys->b_export_sdp_file = VLC_FALSE;
409 p_sys->p_session = NULL;
411 p_sys->p_httpd_host = NULL;
412 p_sys->p_httpd_file = NULL;
413 p_sys->p_rtsp_host = NULL;
414 p_sys->p_rtsp_url = NULL;
415 p_sys->psz_rtsp_control = NULL;
416 p_sys->psz_rtsp_path = NULL;
418 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
419 vlc_mutex_init( p_stream, &p_sys->lock_es );
421 p_stream->pf_add = Add;
422 p_stream->pf_del = Del;
423 p_stream->pf_send = Send;
425 p_stream->p_sys = p_sys;
427 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
428 if( *val.psz_string )
430 sout_access_out_t *p_grab;
431 char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv;
435 msg_Err( p_stream, "muxing is not supported in RTSP mode" );
439 else if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
441 msg_Err( p_stream, "rtp needs a destination when muxing" );
446 /* Check muxer type */
447 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
449 psz_rtpmap = "MP2P/90000";
451 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
453 psz_rtpmap = "MP2T/90000";
454 p_sys->i_payload_type = 33;
458 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
463 /* create the access out */
464 if( p_sys->i_ttl > 0 )
466 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
470 sprintf( access, "udp{raw}" );
473 /* IPv6 needs brackets if not already present */
474 snprintf( url, sizeof( url ),
475 ( ( p_sys->psz_destination[0] != '[' )
476 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
477 ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
479 url[sizeof( url ) - 1] = '\0';
480 /* FIXME: we should check that url is a numerical address, otherwise
481 * the SDP will be quite broken (regardless of the IP protocol version)
482 * Also it might be IPv6 with no ':' if it is a DNS name.
484 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
486 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
488 msg_Err( p_stream, "cannot create the access out for %s://%s",
493 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
494 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
496 /* better than nothing */
499 p_sys->i_mtu -= MTU_REDUCE;
501 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
502 p_grab = p_sys->p_grab =
503 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
504 p_grab->p_module = NULL;
505 p_grab->p_sout = p_sout;
506 p_grab->psz_access = strdup( "grab" );
507 p_grab->p_cfg = NULL;
508 p_grab->psz_name = strdup( "" );
509 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
510 p_grab->pf_seek = NULL;
511 p_grab->pf_write = AccessOutGrabberWrite;
514 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
516 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
517 sout_AccessOutDelete( p_sys->p_grab );
518 sout_AccessOutDelete( p_sys->p_access );
523 /* create the SDP for a muxed stream (only once) */
524 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
525 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
526 o= - should be local username (no spaces allowed)
527 o= time should be hashed with some other value to garantue uniqueness
528 o= we need IP6 support?
529 o= don't use the localhost address. use fully qualified domain name or IP4 address
530 p= international phone number (pass via vars?)
532 a= recvonly (missing)
533 a= type:broadcast (missing)
534 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
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"
555 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
556 "a=tool:"PACKAGE_STRING"\r\n"
558 "m=video %d RTP/AVP %d\r\n"
559 "a=rtpmap:%d %s\r\n",
560 p_sys->i_sdp_id, p_sys->i_sdp_version,
561 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
562 p_sys->psz_session_name,
563 p_sys->psz_session_description,
564 p_sys->psz_session_url,
565 p_sys->psz_session_email,
566 ipv, p_sys->psz_destination, psz_ttl,
567 p_sys->i_port, p_sys->i_payload_type,
568 p_sys->i_payload_type, psz_rtpmap );
569 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
571 /* create the rtp context */
572 p_sys->ssrc[0] = rand()&0xff;
573 p_sys->ssrc[1] = rand()&0xff;
574 p_sys->ssrc[2] = rand()&0xff;
575 p_sys->ssrc[3] = rand()&0xff;
576 p_sys->i_sequence = rand()&0xffff;
577 p_sys->i_timestamp_start = rand()&0xffffffff;
578 p_sys->packet = NULL;
583 p_sys->p_access = NULL;
584 p_sys->p_grab = NULL;
586 free( val.psz_string );
589 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
590 if( *val.psz_string )
594 SDPHandleUrl( p_stream, val.psz_string );
596 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
598 if( !strcmp( p_cfg->psz_name, "sdp" ) )
600 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
603 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
606 SDPHandleUrl( p_stream, p_cfg->psz_value );
610 free( val.psz_string );
612 /* update p_sout->i_out_pace_nocontrol */
613 p_stream->p_sout->i_out_pace_nocontrol++;
618 /*****************************************************************************
620 *****************************************************************************/
621 static void Close( vlc_object_t * p_this )
623 sout_stream_t *p_stream = (sout_stream_t*)p_this;
624 sout_stream_sys_t *p_sys = p_stream->p_sys;
626 /* update p_sout->i_out_pace_nocontrol */
627 p_stream->p_sout->i_out_pace_nocontrol--;
631 sout_MuxDelete( p_sys->p_mux );
632 sout_AccessOutDelete( p_sys->p_access );
633 sout_AccessOutDelete( p_sys->p_grab );
636 block_Release( p_sys->packet );
638 if( p_sys->b_export_sap )
641 SapSetup( p_stream );
645 while( p_sys->i_rtsp > 0 )
647 RtspClientDel( p_stream, p_sys->rtsp[0] );
650 vlc_mutex_destroy( &p_sys->lock_sdp );
652 if( p_sys->p_httpd_file )
654 httpd_FileDelete( p_sys->p_httpd_file );
656 if( p_sys->p_httpd_host )
658 httpd_HostDelete( p_sys->p_httpd_host );
660 if( p_sys->p_rtsp_url )
662 httpd_UrlDelete( p_sys->p_rtsp_url );
664 if( p_sys->p_rtsp_host )
666 httpd_HostDelete( p_sys->p_rtsp_host );
668 if( p_sys->psz_session_name )
670 free( p_sys->psz_session_name );
671 p_sys->psz_session_name = NULL;
673 if( p_sys->psz_session_description )
675 free( p_sys->psz_session_description );
676 p_sys->psz_session_description = NULL;
678 if( p_sys->psz_session_url )
680 free( p_sys->psz_session_url );
681 p_sys->psz_session_url = NULL;
683 if( p_sys->psz_session_email )
685 free( p_sys->psz_session_email );
686 p_sys->psz_session_email = NULL;
690 free( p_sys->psz_sdp );
691 p_sys->psz_sdp = NULL;
696 /*****************************************************************************
698 *****************************************************************************/
699 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
701 sout_stream_sys_t *p_sys = p_stream->p_sys;
704 vlc_UrlParse( &url, psz_url, 0 );
705 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
707 if( p_sys->p_httpd_file )
709 msg_Err( p_stream, "you can use sdp=http:// only once" );
713 if( HttpSetup( p_stream, &url ) )
715 msg_Err( p_stream, "cannot export sdp as http" );
718 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
720 if( p_sys->p_rtsp_url )
722 msg_Err( p_stream, "you can use sdp=rtsp:// only once" );
726 /* FIXME test if destination is multicast or no destination at all FIXME */
727 if( RtspSetup( p_stream, &url ) )
729 msg_Err( p_stream, "cannot export sdp as rtsp" );
732 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
733 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
735 p_sys->b_export_sap = VLC_TRUE;
736 SapSetup( p_stream );
738 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
740 if( p_sys->b_export_sdp_file )
742 msg_Err( p_stream, "you can use sdp=file:// only once" );
745 p_sys->b_export_sdp_file = VLC_TRUE;
746 psz_url = &psz_url[5];
747 if( psz_url[0] == '/' && psz_url[1] == '/' )
749 p_sys->psz_sdp_file = strdup( psz_url );
753 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
756 vlc_UrlClean( &url );
759 /*****************************************************************************
761 *****************************************************************************/
762 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
763 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
764 o= - should be local username (no spaces allowed)
765 o= time should be hashed with some other value to garantue uniqueness
766 o= we need IP6 support?
767 o= don't use the localhost address. use fully qualified domain name or IP4 address
768 p= international phone number (pass via vars?)
770 a= recvonly (missing)
771 a= type:broadcast (missing)
772 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
773 a= x-plgroup: (missing)
774 RTP packets need to get the correct src IP address */
775 static char *SDPGenerate( const sout_stream_t *p_stream,
776 const char *psz_destination, vlc_bool_t b_rtsp )
778 sout_stream_sys_t *p_sys = p_stream->p_sys;
780 char *psz_sdp, *p, ipv;
783 /* FIXME: breaks IP version check on unknown destination */
784 if( psz_destination == NULL )
785 psz_destination = "0.0.0.0";
787 i_size = sizeof( "v=0\r\n" ) +
788 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
789 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
790 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
791 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
792 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
793 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
794 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
795 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
796 strlen( psz_destination ) ;
797 for( i = 0; i < p_sys->i_es; i++ )
799 sout_stream_id_t *id = p_sys->es[i];
801 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
804 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
808 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
812 i_size += strlen( "b=AS: *\r\n") + 10;
816 i_size += strlen( "a=control:*/trackID=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
821 i_size += strlen( "m=video %d RTP/AVP %d\r\n" ) +10 +10;
824 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
826 p = psz_sdp = malloc( i_size );
827 p += sprintf( p, "v=0\r\n" );
828 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
829 p_sys->i_sdp_id, p_sys->i_sdp_version,
830 ipv, ipv == '6' ? "::" : "127.0.0.1" );
831 if( *p_sys->psz_session_name )
832 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
833 if( *p_sys->psz_session_description )
834 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
835 if( *p_sys->psz_session_url )
836 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
837 if( *p_sys->psz_session_email )
838 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
840 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
841 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
843 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
845 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
847 /* Add the ttl if it is a multicast address */
848 /* FIXME: 1 is not a correct default value in the case of IPv6 */
849 p += sprintf( p, "/%d\r\n", p_sys->i_ttl ?: 1 );
853 p += sprintf( p, "\r\n" );
856 for( i = 0; i < p_sys->i_es; i++ )
858 sout_stream_id_t *id = p_sys->es[i];
860 if( id->i_cat == AUDIO_ES )
862 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
863 id->i_port, id->i_payload_type );
865 else if( id->i_cat == VIDEO_ES )
867 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
868 id->i_port, id->i_payload_type );
876 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
881 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
886 p += sprintf(p,"b=AS:%d\r\n",id->i_bitrate);
890 p += sprintf( p, "a=control:trackID=%d\r\n", i );
895 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
896 p_sys->i_port, p_sys->i_payload_type );
902 /*****************************************************************************
904 *****************************************************************************/
905 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
906 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
907 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
908 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
909 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
910 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
911 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
912 static int rtp_packetize_mp4a_latm ( sout_stream_t *, sout_stream_id_t *, block_t * );
913 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
914 static int rtp_packetize_h264 ( sout_stream_t *, sout_stream_id_t *, block_t * );
915 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
917 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
919 static const char hex[16] = "0123456789abcdef";
922 for( i = 0; i < i_data; i++ )
924 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
925 s[2*i+1] = hex[(p_data[i] )&0xf];
930 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
932 sout_instance_t *p_sout = p_stream->p_sout;
933 sout_stream_sys_t *p_sys = p_stream->p_sys;
934 sout_stream_id_t *id;
935 sout_access_out_t *p_access = NULL;
939 if( p_sys->p_mux != NULL )
941 sout_input_t *p_input = NULL;
942 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
944 msg_Err( p_stream, "cannot add this stream to the muxer" );
948 id = malloc( sizeof( sout_stream_id_t ) );
949 memset( id, 0, sizeof( sout_stream_id_t ) );
951 id->p_input = p_input;
952 id->pf_packetize= NULL;
953 id->p_rtsp_url = NULL;
959 /* Choose the port */
961 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
963 i_port = p_sys->i_port_audio;
964 p_sys->i_port_audio = 0;
966 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
968 i_port = p_sys->i_port_video;
969 p_sys->i_port_video = 0;
973 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
975 i_port = p_sys->i_port;
982 if( p_sys->psz_destination )
985 char url[NI_MAXHOST + 8];
987 /* first try to create the access out */
990 snprintf( access, sizeof( access ), "udp{raw,ttl=%d}",
992 access[sizeof( access ) - 1] = '\0';
995 strcpy( access, "udp{raw}" );
997 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
998 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
999 p_sys->psz_destination, i_port );
1000 url[sizeof( url ) - 1] = '\0';
1002 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
1004 msg_Err( p_stream, "cannot create the access out for %s://%s",
1008 msg_Dbg( p_stream, "access out %s:%s", access, url );
1011 /* not create the rtp specific stuff */
1012 id = malloc( sizeof( sout_stream_id_t ) );
1013 memset( id, 0, sizeof( sout_stream_id_t ) );
1014 id->p_stream = p_stream;
1015 id->p_access = p_access;
1017 id->psz_rtpmap = NULL;
1018 id->psz_fmtp = NULL;
1019 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
1020 id->i_port = i_port;
1021 id->p_rtsp_url = NULL;
1022 vlc_mutex_init( p_stream, &id->lock_rtsp );
1023 id->i_rtsp_access = 0;
1024 id->rtsp_access = NULL;
1026 switch( p_fmt->i_codec )
1028 case VLC_FOURCC( 's', '1', '6', 'b' ):
1029 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
1031 id->i_payload_type = 11;
1033 else if( p_fmt->audio.i_channels == 2 &&
1034 p_fmt->audio.i_rate == 44100 )
1036 id->i_payload_type = 10;
1040 id->i_payload_type = p_sys->i_payload_type++;
1042 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
1043 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
1044 p_fmt->audio.i_channels );
1045 id->i_clock_rate = p_fmt->audio.i_rate;
1046 id->pf_packetize = rtp_packetize_l16;
1048 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
1049 id->i_payload_type = p_sys->i_payload_type++;
1050 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
1051 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
1052 p_fmt->audio.i_channels );
1053 id->i_clock_rate = p_fmt->audio.i_rate;
1054 id->pf_packetize = rtp_packetize_l8;
1056 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1057 id->i_payload_type = 14;
1058 id->i_clock_rate = 90000;
1059 id->psz_rtpmap = strdup( "MPA/90000" );
1060 id->pf_packetize = rtp_packetize_mpa;
1062 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1063 id->i_payload_type = 32;
1064 id->i_clock_rate = 90000;
1065 id->psz_rtpmap = strdup( "MPV/90000" );
1066 id->pf_packetize = rtp_packetize_mpv;
1068 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1069 id->i_payload_type = p_sys->i_payload_type++;
1070 id->i_clock_rate = 90000;
1071 id->psz_rtpmap = strdup( "ac3/90000" );
1072 id->pf_packetize = rtp_packetize_ac3;
1074 case VLC_FOURCC( 'H', '2', '6', '3' ):
1075 id->i_payload_type = p_sys->i_payload_type++;
1076 id->i_clock_rate = 90000;
1077 id->psz_rtpmap = strdup( "H263-1998/90000" );
1078 id->pf_packetize = rtp_packetize_h263;
1080 case VLC_FOURCC( 'h', '2', '6', '4' ):
1081 id->i_payload_type = p_sys->i_payload_type++;
1082 id->i_clock_rate = 90000;
1083 id->psz_rtpmap = strdup( "H264/90000" );
1084 id->pf_packetize = rtp_packetize_h264;
1085 id->psz_fmtp = strdup( "packetization-mode=1" );
1088 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1090 char hexa[2*p_fmt->i_extra +1];
1092 id->i_payload_type = p_sys->i_payload_type++;
1093 id->i_clock_rate = 90000;
1094 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1095 id->pf_packetize = rtp_packetize_split;
1096 if( p_fmt->i_extra > 0 )
1098 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1099 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1100 sprintf( id->psz_fmtp,
1101 "profile-level-id=3; config=%s;", hexa );
1105 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1107 id->i_payload_type = p_sys->i_payload_type++;
1108 id->i_clock_rate = p_fmt->audio.i_rate;
1112 char hexa[2*p_fmt->i_extra +1];
1114 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1115 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1116 id->pf_packetize = rtp_packetize_mp4a;
1117 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1118 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1119 sprintf( id->psz_fmtp,
1120 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1121 "config=%s; SizeLength=13;IndexLength=3; "
1122 "IndexDeltaLength=3; Profile=1;", hexa );
1128 unsigned char config[6];
1129 unsigned int aacsrates[15] = {
1130 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1131 16000, 12000, 11025, 8000, 7350, 0, 0 };
1133 for( i = 0; i < 15; i++ )
1134 if( p_fmt->audio.i_rate == aacsrates[i] )
1140 config[3]=p_fmt->audio.i_channels<<4;
1144 asprintf( &id->psz_rtpmap, "MP4A-LATM/%d/%d",
1145 p_fmt->audio.i_rate, p_fmt->audio.i_channels );
1146 id->pf_packetize = rtp_packetize_mp4a_latm;
1147 sprintf_hexa( hexa, config, 6 );
1148 asprintf( &id->psz_fmtp, "profile-level-id=15; "
1149 "object=2; cpresent=0; config=%s", hexa );
1153 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1154 id->i_payload_type = p_sys->i_payload_type++;
1155 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1156 "AMR/8000/2" : "AMR/8000" );
1157 id->psz_fmtp = strdup( "octet-align=1" );
1158 id->i_clock_rate = p_fmt->audio.i_rate;
1159 id->pf_packetize = rtp_packetize_amr;
1161 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1162 id->i_payload_type = p_sys->i_payload_type++;
1163 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1164 "AMR-WB/16000/2" : "AMR-WB/16000" );
1165 id->psz_fmtp = strdup( "octet-align=1" );
1166 id->i_clock_rate = p_fmt->audio.i_rate;
1167 id->pf_packetize = rtp_packetize_amr;
1171 msg_Err( p_stream, "cannot add this stream (unsupported "
1172 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1175 sout_AccessOutDelete( p_access );
1180 id->i_cat = p_fmt->i_cat;
1182 id->ssrc[0] = rand()&0xff;
1183 id->ssrc[1] = rand()&0xff;
1184 id->ssrc[2] = rand()&0xff;
1185 id->ssrc[3] = rand()&0xff;
1186 id->i_sequence = rand()&0xffff;
1187 id->i_timestamp_start = rand()&0xffffffff;
1188 id->i_bitrate = p_fmt->i_bitrate/1000; /* Stream bitrate in kbps */
1190 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1191 if( id->i_mtu <= 16 + MTU_REDUCE )
1193 /* better than nothing */
1196 id->i_mtu -= MTU_REDUCE;
1197 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1199 if( p_sys->p_rtsp_url )
1201 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1203 sprintf( psz_urlc, "%s/trackID=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1204 msg_Dbg( p_stream, "rtsp: adding %s\n", psz_urlc );
1205 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1207 if( id->p_rtsp_url )
1209 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1210 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1211 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1216 /* Update p_sys context */
1217 vlc_mutex_lock( &p_sys->lock_es );
1218 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1219 vlc_mutex_unlock( &p_sys->lock_es );
1221 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1223 vlc_mutex_lock( &p_sys->lock_sdp );
1224 free( p_sys->psz_sdp );
1225 p_sys->psz_sdp = psz_sdp;
1226 vlc_mutex_unlock( &p_sys->lock_sdp );
1228 p_sys->i_sdp_version++;
1230 msg_Dbg( p_stream, "sdp=%s", p_sys->psz_sdp );
1232 /* Update SDP (sap/file) */
1233 if( p_sys->b_export_sap ) SapSetup( p_stream );
1234 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1239 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1241 sout_stream_sys_t *p_sys = p_stream->p_sys;
1243 vlc_mutex_lock( &p_sys->lock_es );
1244 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1245 vlc_mutex_unlock( &p_sys->lock_es );
1248 if( id->i_port > 0 )
1250 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1251 p_sys->i_port_audio = id->i_port;
1252 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1253 p_sys->i_port_video = id->i_port;
1258 if( id->psz_rtpmap )
1260 free( id->psz_rtpmap );
1264 free( id->psz_fmtp );
1266 if( id->psz_destination )
1267 free( id->psz_destination );
1268 sout_AccessOutDelete( id->p_access );
1270 else if( id->p_input )
1272 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1274 if( id->p_rtsp_url )
1276 httpd_UrlDelete( id->p_rtsp_url );
1278 vlc_mutex_destroy( &id->lock_rtsp );
1279 if( id->rtsp_access ) free( id->rtsp_access );
1281 /* Update SDP (sap/file) */
1282 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1283 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1289 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1294 if( p_stream->p_sys->p_mux )
1296 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1302 p_next = p_buffer->p_next;
1303 if( id->pf_packetize( p_stream, id, p_buffer ) )
1307 block_Release( p_buffer );
1315 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1318 sout_stream_sys_t *p_sys = p_stream->p_sys;
1320 int64_t i_dts = p_buffer->i_dts;
1321 uint32_t i_timestamp = i_dts * 9 / 100;
1323 uint8_t *p_data = p_buffer->p_buffer;
1324 unsigned int i_data = p_buffer->i_buffer;
1325 unsigned int i_max = p_sys->i_mtu - 12;
1327 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1331 unsigned int i_size;
1333 /* output complete packet */
1334 if( p_sys->packet &&
1335 p_sys->packet->i_buffer + i_data > i_max )
1337 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1338 p_sys->packet = NULL;
1341 if( p_sys->packet == NULL )
1343 /* allocate a new packet */
1344 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1345 p_sys->packet->p_buffer[ 0] = 0x80;
1346 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1347 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1348 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1349 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1350 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1351 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1352 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1353 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1354 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1355 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1356 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1357 p_sys->packet->i_buffer = 12;
1359 p_sys->packet->i_dts = i_dts;
1360 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1361 i_dts += p_sys->packet->i_length;
1363 p_sys->i_sequence++;
1366 i_size = __MIN( i_data,
1367 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1369 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1372 p_sys->packet->i_buffer += i_size;
1380 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1383 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1385 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1391 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1393 p_next = p_buffer->p_next;
1394 block_Release( p_buffer );
1401 /****************************************************************************
1403 ****************************************************************************/
1404 static int SapSetup( sout_stream_t *p_stream )
1406 sout_stream_sys_t *p_sys = p_stream->p_sys;
1407 sout_instance_t *p_sout = p_stream->p_sout;
1408 announce_method_t *p_method = sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
1410 /* Remove the previous session */
1411 if( p_sys->p_session != NULL)
1413 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1414 sout_AnnounceSessionDestroy( p_sys->p_session );
1415 p_sys->p_session = NULL;
1418 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1420 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1421 p_sys->psz_destination,
1429 /****************************************************************************
1431 ****************************************************************************/
1432 static int FileSetup( sout_stream_t *p_stream )
1434 sout_stream_sys_t *p_sys = p_stream->p_sys;
1437 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1439 msg_Err( p_stream, "cannot open file '%s' (%s)",
1440 p_sys->psz_sdp_file, strerror(errno) );
1441 return VLC_EGENERIC;
1444 fprintf( f, "%s", p_sys->psz_sdp );
1450 /****************************************************************************
1452 ****************************************************************************/
1453 static int HttpCallback( httpd_file_sys_t *p_args,
1454 httpd_file_t *, uint8_t *p_request,
1455 uint8_t **pp_data, int *pi_data );
1457 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1459 sout_stream_sys_t *p_sys = p_stream->p_sys;
1461 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1462 if( p_sys->p_httpd_host )
1464 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1465 url->psz_path ? url->psz_path : "/",
1468 HttpCallback, (void*)p_sys );
1470 if( p_sys->p_httpd_file == NULL )
1472 return VLC_EGENERIC;
1477 static int HttpCallback( httpd_file_sys_t *p_args,
1478 httpd_file_t *f, uint8_t *p_request,
1479 uint8_t **pp_data, int *pi_data )
1481 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1483 vlc_mutex_lock( &p_sys->lock_sdp );
1484 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1486 *pi_data = strlen( p_sys->psz_sdp );
1487 *pp_data = malloc( *pi_data );
1488 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1495 vlc_mutex_unlock( &p_sys->lock_sdp );
1500 /****************************************************************************
1502 ****************************************************************************/
1503 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1505 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1507 rtsp->psz_session = psz_session;
1509 rtsp->b_playing = VLC_FALSE;
1513 rtsp->access = NULL;
1515 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1519 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1523 if( psz_session ) return NULL;
1525 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1527 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1529 return p_stream->p_sys->rtsp[i];
1535 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1538 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1540 for( i = 0; i < rtsp->i_access; i++ )
1542 sout_AccessOutDelete( rtsp->access[i] );
1544 if( rtsp->id ) free( rtsp->id );
1545 if( rtsp->access ) free( rtsp->access );
1547 free( rtsp->psz_session );
1551 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1553 sout_stream_sys_t *p_sys = p_stream->p_sys;
1555 msg_Dbg( p_stream, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1557 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1558 if( p_sys->p_rtsp_host == NULL )
1560 return VLC_EGENERIC;
1563 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1564 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1565 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1566 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1568 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1569 if( p_sys->p_rtsp_url == 0 )
1571 return VLC_EGENERIC;
1573 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1574 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1575 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1576 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1581 static int RtspCallback( httpd_callback_sys_t *p_args,
1583 httpd_message_t *answer, httpd_message_t *query )
1585 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1586 sout_stream_sys_t *p_sys = p_stream->p_sys;
1587 char *psz_destination = p_sys->psz_destination;
1588 char *psz_session = NULL;
1589 char *psz_cseq = NULL;
1592 if( answer == NULL || query == NULL )
1596 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1598 answer->i_proto = HTTPD_PROTO_RTSP;
1599 answer->i_version= query->i_version;
1600 answer->i_type = HTTPD_MSG_ANSWER;
1602 switch( query->i_type )
1604 case HTTPD_MSG_DESCRIBE:
1606 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1608 answer->i_status = 200;
1609 answer->psz_status = strdup( "OK" );
1610 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1612 answer->p_body = (uint8_t *)psz_sdp;
1613 answer->i_body = strlen( psz_sdp );
1617 case HTTPD_MSG_PLAY:
1619 rtsp_client_t *rtsp;
1620 /* for now only multicast so easy */
1621 answer->i_status = 200;
1622 answer->psz_status = strdup( "OK" );
1624 answer->p_body = NULL;
1626 psz_session = httpd_MsgGet( query, "Session" );
1627 rtsp = RtspClientGet( p_stream, psz_session );
1628 if( rtsp && !rtsp->b_playing )
1632 rtsp->b_playing = VLC_TRUE;
1634 vlc_mutex_lock( &p_sys->lock_es );
1635 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1637 sout_stream_id_t *id = rtsp->id[i_id];
1640 for( i = 0; i < p_sys->i_es; i++ )
1642 if( id == p_sys->es[i] )
1645 if( i >= p_sys->i_es ) continue;
1647 vlc_mutex_lock( &id->lock_rtsp );
1648 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1649 vlc_mutex_unlock( &id->lock_rtsp );
1651 vlc_mutex_unlock( &p_sys->lock_es );
1655 case HTTPD_MSG_PAUSE:
1657 return VLC_EGENERIC;
1658 case HTTPD_MSG_TEARDOWN:
1660 rtsp_client_t *rtsp;
1662 /* for now only multicast so easy again */
1663 answer->i_status = 200;
1664 answer->psz_status = strdup( "OK" );
1666 answer->p_body = NULL;
1668 psz_session = httpd_MsgGet( query, "Session" );
1669 rtsp = RtspClientGet( p_stream, psz_session );
1674 vlc_mutex_lock( &p_sys->lock_es );
1675 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1677 sout_stream_id_t *id = rtsp->id[i_id];
1680 for( i = 0; i < p_sys->i_es; i++ )
1682 if( id == p_sys->es[i] )
1685 if( i >= p_sys->i_es ) continue;
1687 vlc_mutex_lock( &id->lock_rtsp );
1688 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1689 vlc_mutex_unlock( &id->lock_rtsp );
1691 vlc_mutex_unlock( &p_sys->lock_es );
1693 RtspClientDel( p_stream, rtsp );
1699 return VLC_EGENERIC;
1701 httpd_MsgAdd( answer, "Server", PACKAGE_STRING );
1702 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1703 psz_cseq = httpd_MsgGet( query, "Cseq" );
1705 i_cseq = atoi( psz_cseq );
1708 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1709 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1713 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1718 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1720 httpd_message_t *answer, httpd_message_t *query )
1722 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1723 sout_stream_t *p_stream = id->p_stream;
1724 sout_stream_sys_t *p_sys = p_stream->p_sys;
1725 char *psz_session = NULL;
1726 char *psz_cseq = NULL;
1730 if( answer == NULL || query == NULL )
1734 //fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1736 answer->i_proto = HTTPD_PROTO_RTSP;
1737 answer->i_version= query->i_version;
1738 answer->i_type = HTTPD_MSG_ANSWER;
1740 switch( query->i_type )
1742 case HTTPD_MSG_SETUP:
1744 char *psz_transport = httpd_MsgGet( query, "Transport" );
1746 //fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1748 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1750 //fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1751 answer->i_status = 200;
1752 answer->psz_status = strdup( "OK" );
1754 answer->p_body = NULL;
1755 psz_session = httpd_MsgGet( query, "Session" );
1758 psz_session = malloc( 100 );
1759 sprintf( psz_session, "%d", rand() );
1761 httpd_MsgAdd( answer, "Transport",
1762 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1763 id->psz_destination, id->i_port,id->i_port+1,
1766 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1768 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1769 char ip[NI_MAXNUMERICHOST], psz_access[17], psz_url[NI_MAXNUMERICHOST + 8];
1771 sout_access_out_t *p_access;
1773 rtsp_client_t *rtsp = NULL;
1775 if( httpd_ClientIP( cl, ip ) == NULL )
1777 answer->i_status = 500;
1778 answer->psz_status = strdup( "Internal server error" );
1780 answer->p_body = NULL;
1784 //fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n", ip, i_port );
1786 psz_session = httpd_MsgGet( query, "Session" );
1789 psz_session = malloc( 100 );
1790 sprintf( psz_session, "%d", rand() );
1792 rtsp = RtspClientNew( p_stream, psz_session );
1796 rtsp = RtspClientGet( p_stream, psz_session );
1799 answer->i_status = 454;
1800 answer->psz_status = strdup( "Unknown session id" );
1802 answer->p_body = NULL;
1807 /* first try to create the access out */
1809 snprintf( psz_access, sizeof( psz_access ),
1810 "udp{raw,ttl=%d}", p_sys->i_ttl );
1812 strlcpy( psz_access, "udp{raw}", sizeof( psz_access ) );
1814 snprintf( psz_url, sizeof( psz_url ),
1815 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
1818 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1820 msg_Err( p_stream, "cannot create the access out for %s://%s",
1821 psz_access, psz_url );
1822 answer->i_status = 500;
1823 answer->psz_status = strdup( "Internal server error" );
1825 answer->p_body = NULL;
1829 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1830 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1832 answer->i_status = 200;
1833 answer->psz_status = strdup( "OK" );
1835 answer->p_body = NULL;
1837 httpd_MsgAdd( answer, "Transport",
1838 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1840 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1842 answer->i_status = 461;
1843 answer->psz_status = strdup( "Unsupported Transport" );
1845 answer->p_body = NULL;
1851 return VLC_EGENERIC;
1853 httpd_MsgAdd( answer, "Server", "VLC Server" );
1854 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1855 psz_cseq = httpd_MsgGet( query, "Cseq" );
1857 i_cseq = atoi( psz_cseq );
1860 httpd_MsgAdd( answer, "Cseq", "%d", i_cseq );
1861 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1865 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1870 /****************************************************************************
1872 ****************************************************************************/
1873 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1874 int b_marker, int64_t i_pts )
1876 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1878 out->p_buffer[0] = 0x80;
1879 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1880 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1881 out->p_buffer[3] = ( id->i_sequence )&0xff;
1882 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1883 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1884 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1885 out->p_buffer[7] = ( i_timestamp )&0xff;
1887 out->p_buffer[ 8] = id->ssrc[0];
1888 out->p_buffer[ 9] = id->ssrc[1];
1889 out->p_buffer[10] = id->ssrc[2];
1890 out->p_buffer[11] = id->ssrc[3];
1896 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1899 vlc_mutex_lock( &id->lock_rtsp );
1900 for( i = 0; i < id->i_rtsp_access; i++ )
1902 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1904 vlc_mutex_unlock( &id->lock_rtsp );
1908 sout_AccessOutWrite( id->p_access, out );
1912 block_Release( out );
1916 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1919 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1920 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1922 uint8_t *p_data = in->p_buffer;
1923 int i_data = in->i_buffer;
1926 for( i = 0; i < i_count; i++ )
1928 int i_payload = __MIN( i_max, i_data );
1929 block_t *out = block_New( p_stream, 16 + i_payload );
1931 /* rtp common header */
1932 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1934 out->p_buffer[12] = 0;
1935 out->p_buffer[13] = 0;
1936 /* fragment offset in the current frame */
1937 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1938 out->p_buffer[15] = ( (i*i_max) )&0xff;
1939 memcpy( &out->p_buffer[16], p_data, i_payload );
1941 out->i_buffer = 16 + i_payload;
1942 out->i_dts = in->i_dts + i * in->i_length / i_count;
1943 out->i_length = in->i_length / i_count;
1945 rtp_packetize_send( id, out );
1947 p_data += i_payload;
1948 i_data -= i_payload;
1955 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1958 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1959 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1961 uint8_t *p_data = in->p_buffer;
1962 int i_data = in->i_buffer;
1964 int b_sequence_start = 0;
1965 int i_temporal_ref = 0;
1966 int i_picture_coding_type = 0;
1967 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1968 int b_start_slice = 0;
1970 /* preparse this packet to get some info */
1971 if( in->i_buffer > 4 )
1973 uint8_t *p = p_data;
1974 int i_rest = in->i_buffer;
1978 while( i_rest > 4 &&
1979 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1993 /* sequence start code */
1994 b_sequence_start = 1;
1996 else if( *p == 0x00 && i_rest >= 4 )
1999 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
2000 i_picture_coding_type = (p[2] >> 3)&0x07;
2002 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
2003 i_picture_coding_type == 3 ) )
2005 i_ffv = (p[3] >> 2)&0x01;
2006 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
2007 if( i_rest > 4 && i_picture_coding_type == 3 )
2009 i_fbv = (p[4]>>6)&0x01;
2010 i_bfc = (p[4]>>3)&0x07;
2014 else if( *p <= 0xaf )
2021 for( i = 0; i < i_count; i++ )
2023 int i_payload = __MIN( i_max, i_data );
2024 block_t *out = block_New( p_stream,
2026 uint32_t h = ( i_temporal_ref << 16 )|
2027 ( b_sequence_start << 13 )|
2028 ( b_start_slice << 12 )|
2029 ( i == i_count - 1 ? 1 << 11 : 0 )|
2030 ( i_picture_coding_type << 8 )|
2031 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
2033 /* rtp common header */
2034 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2035 in->i_pts > 0 ? in->i_pts : in->i_dts );
2037 /* 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 */
2038 out->p_buffer[12] = ( h >> 24 )&0xff;
2039 out->p_buffer[13] = ( h >> 16 )&0xff;
2040 out->p_buffer[14] = ( h >> 8 )&0xff;
2041 out->p_buffer[15] = ( h )&0xff;
2043 memcpy( &out->p_buffer[16], p_data, i_payload );
2045 out->i_buffer = 16 + i_payload;
2046 out->i_dts = in->i_dts + i * in->i_length / i_count;
2047 out->i_length = in->i_length / i_count;
2049 rtp_packetize_send( id, out );
2051 p_data += i_payload;
2052 i_data -= i_payload;
2058 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
2061 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
2062 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2064 uint8_t *p_data = in->p_buffer;
2065 int i_data = in->i_buffer;
2068 for( i = 0; i < i_count; i++ )
2070 int i_payload = __MIN( i_max, i_data );
2071 block_t *out = block_New( p_stream, 14 + i_payload );
2073 /* rtp common header */
2074 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
2076 out->p_buffer[12] = 1;
2078 out->p_buffer[13] = 0x00;
2080 memcpy( &out->p_buffer[14], p_data, i_payload );
2082 out->i_buffer = 14 + i_payload;
2083 out->i_dts = in->i_dts + i * in->i_length / i_count;
2084 out->i_length = in->i_length / i_count;
2086 rtp_packetize_send( id, out );
2088 p_data += i_payload;
2089 i_data -= i_payload;
2095 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
2098 int i_max = id->i_mtu - 12; /* payload max in one packet */
2099 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2101 uint8_t *p_data = in->p_buffer;
2102 int i_data = in->i_buffer;
2105 for( i = 0; i < i_count; i++ )
2107 int i_payload = __MIN( i_max, i_data );
2108 block_t *out = block_New( p_stream, 12 + i_payload );
2110 /* rtp common header */
2111 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2112 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2113 memcpy( &out->p_buffer[12], p_data, i_payload );
2115 out->i_buffer = 12 + i_payload;
2116 out->i_dts = in->i_dts + i * in->i_length / i_count;
2117 out->i_length = in->i_length / i_count;
2119 rtp_packetize_send( id, out );
2121 p_data += i_payload;
2122 i_data -= i_payload;
2129 static int rtp_packetize_mp4a_latm( sout_stream_t *p_stream, sout_stream_id_t *id,
2132 int i_max = id->i_mtu - 14; /* payload max in one packet */
2133 int latmhdrsize = in->i_buffer / 0xff + 1;
2134 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2136 uint8_t *p_data = in->p_buffer, *p_header = NULL;
2137 int i_data = in->i_buffer;
2140 for( i = 0; i < i_count; i++ )
2142 int i_payload = __MIN( i_max, i_data );
2147 out = block_New( p_stream, 12 + latmhdrsize + i_payload );
2149 /* rtp common header */
2150 rtp_packetize_common( id, out, ((i == i_count - 1) ? 1 : 0),
2151 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2155 int tmp = in->i_buffer;
2157 p_header=out->p_buffer+12;
2167 memcpy( &out->p_buffer[12+latmhdrsize], p_data, i_payload );
2169 out->i_buffer = 12 + latmhdrsize + 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_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
2185 int i_max = id->i_mtu - 12; /* 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;
2194 int i_payload = (__MIN( i_max, i_data )/4)*4;
2195 block_t *out = block_New( p_stream, 12 + i_payload );
2197 /* rtp common header */
2198 rtp_packetize_common( id, out, 0,
2199 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2200 memcpy( &out->p_buffer[12], p_data, i_payload );
2202 out->i_buffer = 12 + i_payload;
2203 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2204 out->i_length = in->i_length / i_count;
2206 rtp_packetize_send( id, out );
2208 p_data += i_payload;
2209 i_data -= i_payload;
2216 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2219 int i_max = id->i_mtu - 12; /* payload max in one packet */
2220 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2222 uint8_t *p_data = in->p_buffer;
2223 int i_data = in->i_buffer;
2228 int i_payload = (__MIN( i_max, i_data )/2)*2;
2229 block_t *out = block_New( p_stream, 12 + i_payload );
2231 /* rtp common header */
2232 rtp_packetize_common( id, out, 0,
2233 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2234 memcpy( &out->p_buffer[12], p_data, i_payload );
2236 out->i_buffer = 12 + i_payload;
2237 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2238 out->i_length = in->i_length / i_count;
2240 rtp_packetize_send( id, out );
2242 p_data += i_payload;
2243 i_data -= i_payload;
2249 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2252 int i_max = id->i_mtu - 16; /* payload max in one packet */
2253 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2255 uint8_t *p_data = in->p_buffer;
2256 int i_data = in->i_buffer;
2259 for( i = 0; i < i_count; i++ )
2261 int i_payload = __MIN( i_max, i_data );
2262 block_t *out = block_New( p_stream, 16 + i_payload );
2264 /* rtp common header */
2265 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2266 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2268 /* AU headers length (bits) */
2269 out->p_buffer[12] = 0;
2270 out->p_buffer[13] = 2*8;
2271 /* for each AU length 13 bits + idx 3bits, */
2272 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2273 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2275 memcpy( &out->p_buffer[16], p_data, i_payload );
2277 out->i_buffer = 16 + i_payload;
2278 out->i_dts = in->i_dts + i * in->i_length / i_count;
2279 out->i_length = in->i_length / i_count;
2281 rtp_packetize_send( id, out );
2283 p_data += i_payload;
2284 i_data -= i_payload;
2292 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2293 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2294 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2297 uint8_t *p_data = in->p_buffer;
2298 int i_data = in->i_buffer;
2300 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2303 int b_v_bit = 0; // no pesky error resilience
2304 int i_plen = 0; // normally plen=0 for PSC packet
2305 int i_pebit = 0; // because plen=0
2310 return VLC_EGENERIC;
2312 if( p_data[0] || p_data[1] )
2314 return VLC_EGENERIC;
2316 /* remove 2 leading 0 bytes */
2319 i_count = ( i_data + i_max - 1 ) / i_max;
2321 for( i = 0; i < i_count; i++ )
2323 int i_payload = __MIN( i_max, i_data );
2324 block_t *out = block_New( p_stream,
2325 RTP_H263_PAYLOAD_START + i_payload );
2326 b_p_bit = (i == 0) ? 1 : 0;
2327 h = ( b_p_bit << 10 )|
2332 /* rtp common header */
2333 //b_m_bit = 1; // always contains end of frame
2334 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2335 in->i_pts > 0 ? in->i_pts : in->i_dts );
2338 out->p_buffer[12] = ( h >> 8 )&0xff;
2339 out->p_buffer[13] = ( h )&0xff;
2340 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2342 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2343 out->i_dts = in->i_dts + i * in->i_length / i_count;
2344 out->i_length = in->i_length / i_count;
2346 rtp_packetize_send( id, out );
2348 p_data += i_payload;
2349 i_data -= i_payload;
2356 static int rtp_packetize_h264( sout_stream_t *p_stream, sout_stream_id_t *id,
2359 int i_max = id->i_mtu - 12; /* payload max in one packet */
2360 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2361 uint8_t *p_data = in->p_buffer;
2362 int i_data = in->i_buffer;
2365 if( i_data <= i_max )
2370 uint8_t *p = p_data;
2371 int i_rest = in->i_buffer;
2373 while( i_rest > 4 &&
2374 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
2386 i_payload = __MIN( i_max, i_rest );
2387 out = block_New( p_stream, 12 + i_payload );
2389 /* rtp common header */
2390 rtp_packetize_common( id, out, 1,
2391 in->i_pts > 0 ? in->i_pts : in->i_dts );
2393 memcpy( &out->p_buffer[12], p, i_payload );
2395 out->i_buffer = 12 + i_payload;
2396 out->i_dts = in->i_dts;
2397 out->i_length = in->i_length;
2399 rtp_packetize_send( id, out );
2401 /*msg_Dbg( p_stream, "nal-out plain %d %02x", i_payload, out->p_buffer[16] );*/
2408 uint8_t *p = p_data;
2409 int i_rest = in->i_buffer;
2410 int start=1, end=0, first=0, nalh=-1;
2412 while( i_rest > 4 &&
2413 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
2429 i_max = id->i_mtu - 14;
2430 i_count = ( i_rest + i_max - 1 ) / i_max;
2432 /*msg_Dbg( p_stream, "nal-out fragmented %02x %d", nalh, i_rest);*/
2436 i_payload = __MIN( i_max, i_rest );
2437 out = block_New( p_stream, 14 + i_payload );
2439 if (i_rest==i_payload)
2442 /* rtp common header */
2443 rtp_packetize_common( id, out, (end)?1:0,
2444 in->i_pts > 0 ? in->i_pts : in->i_dts );
2447 out->p_buffer[12] = (nalh&0x60)|28;
2449 out->p_buffer[13] = (start<<7)|(end<<6)|(nalh&0x1f);
2451 memcpy( &out->p_buffer[14], p+first, i_payload );
2453 out->i_buffer = 14 + i_payload;
2455 // not sure what of these should be used and what it does :)
2456 out->i_dts = in->i_dts + i * in->i_length / i_count;
2457 out->i_length = in->i_length / i_count;
2458 //out->i_dts = in->i_dts;
2459 //out->i_length = in->i_length;
2461 rtp_packetize_send( id, out );
2463 /*msg_Dbg( p_stream, "nal-out fragmented: frag %d %d %02x %02x %d", start,end,
2464 out->p_buffer[12], out->p_buffer[13], i_payload );*/
2466 i_rest -= i_payload;
2475 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2478 int i_max = id->i_mtu - 14; /* payload max in one packet */
2479 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2481 uint8_t *p_data = in->p_buffer;
2482 int i_data = in->i_buffer;
2485 /* Only supports octet-aligned mode */
2486 for( i = 0; i < i_count; i++ )
2488 int i_payload = __MIN( i_max, i_data );
2489 block_t *out = block_New( p_stream, 14 + i_payload );
2491 /* rtp common header */
2492 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2493 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2494 /* Payload header */
2495 out->p_buffer[12] = 0xF0; /* CMR */
2496 out->p_buffer[13] = p_data[0]&0x7C; /* ToC */ /* FIXME: frame type */
2498 /* FIXME: are we fed multiple frames ? */
2499 memcpy( &out->p_buffer[14], p_data+1, i_payload-1 );
2501 out->i_buffer = 14 + i_payload-1;
2502 out->i_dts = in->i_dts + i * in->i_length / i_count;
2503 out->i_length = in->i_length / i_count;
2505 rtp_packetize_send( id, out );
2507 p_data += i_payload;
2508 i_data -= i_payload;