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 "Allows you to specify the output URL used for the streaming output." )
49 #define SDP_TEXT N_("SDP")
50 #define SDP_LONGTEXT N_( \
51 "Allows you to specify the SDP used for the streaming output. " \
52 "You must use an url: http://location to access the SDP via HTTP, " \
53 "rtsp://location for RTSP access, and sap:// for the SDP to be " \
54 "announced via SAP." )
55 #define MUX_TEXT N_("Muxer")
56 #define MUX_LONGTEXT N_( \
57 "Allows you to specify the muxer used for the streaming output." )
59 #define NAME_TEXT N_("Session name")
60 #define NAME_LONGTEXT N_( \
61 "Allows you to specify the session name used for the streaming output." )
62 #define DESC_TEXT N_("Session description")
63 #define DESC_LONGTEXT N_( \
64 "Allows you to give a broader description of the stream." )
65 #define URL_TEXT N_("Session URL")
66 #define URL_LONGTEXT N_( \
67 "Allows you to specify a URL with additional information on the stream." )
68 #define EMAIL_TEXT N_("Session email")
69 #define EMAIL_LONGTEXT N_( \
70 "Allows you to specify contact e-mail address for this session." )
72 #define PORT_TEXT N_("Port")
73 #define PORT_LONGTEXT N_( \
74 "Allows you to specify the base port used for the RTP streaming." )
75 #define PORT_AUDIO_TEXT N_("Audio port")
76 #define PORT_AUDIO_LONGTEXT N_( \
77 "Allows you to specify the default audio port used for the RTP streaming." )
78 #define PORT_VIDEO_TEXT N_("Video port")
79 #define PORT_VIDEO_LONGTEXT N_( \
80 "Allows you to specify the default video port used for the RTP streaming." )
82 #define TTL_TEXT N_("Time-To-Live (TTL)")
83 #define TTL_LONGTEXT N_( \
84 "Allows you to specify the Time-To-Live for the output stream." )
86 static int Open ( vlc_object_t * );
87 static void Close( vlc_object_t * );
89 #define SOUT_CFG_PREFIX "sout-rtp-"
92 set_shortname( _("RTP"));
93 set_description( _("RTP stream output") );
94 set_capability( "sout stream", 0 );
95 add_shortcut( "rtp" );
96 set_category( CAT_SOUT );
97 set_subcategory( SUBCAT_SOUT_STREAM );
99 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
100 DST_LONGTEXT, VLC_TRUE );
101 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
102 SDP_LONGTEXT, VLC_TRUE );
103 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
104 MUX_LONGTEXT, VLC_TRUE );
106 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
107 NAME_LONGTEXT, VLC_TRUE );
108 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
109 DESC_LONGTEXT, VLC_TRUE );
110 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
111 URL_LONGTEXT, VLC_TRUE );
112 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
113 EMAIL_LONGTEXT, VLC_TRUE );
115 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
116 PORT_LONGTEXT, VLC_TRUE );
117 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
118 PORT_AUDIO_LONGTEXT, VLC_TRUE );
119 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
120 PORT_VIDEO_LONGTEXT, VLC_TRUE );
122 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
123 TTL_LONGTEXT, VLC_TRUE );
125 set_callbacks( Open, Close );
128 /*****************************************************************************
129 * Exported prototypes
130 *****************************************************************************/
131 static const char *ppsz_sout_options[] = {
132 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
133 "description", "url","email", NULL
136 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
137 static int Del ( sout_stream_t *, sout_stream_id_t * );
138 static int Send( sout_stream_t *, sout_stream_id_t *,
141 /* For unicast/interleaved streaming */
145 int64_t i_last; /* for timeout */
147 /* is it in "play" state */
148 vlc_bool_t b_playing;
150 /* output (id-access) */
152 sout_stream_id_t **id;
154 sout_access_out_t **access;
157 struct sout_stream_sys_t
163 vlc_mutex_t lock_sdp;
165 char *psz_session_name;
166 char *psz_session_description;
167 char *psz_session_url;
168 char *psz_session_email;
171 vlc_bool_t b_export_sdp_file;
174 vlc_bool_t b_export_sap;
175 session_descriptor_t *p_session;
177 httpd_host_t *p_httpd_host;
178 httpd_file_t *p_httpd_file;
180 httpd_host_t *p_rtsp_host;
181 httpd_url_t *p_rtsp_url;
182 char *psz_rtsp_control;
186 char *psz_destination;
192 /* when need to use a private one or when using muxer */
195 /* in case we do TS/PS over rtp */
197 sout_access_out_t *p_access;
199 sout_access_out_t *p_grab;
201 uint32_t i_timestamp_start;
208 sout_stream_id_t **es;
212 rtsp_client_t **rtsp;
215 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
218 struct sout_stream_id_t
220 sout_stream_t *p_stream;
222 uint8_t i_payload_type;
224 uint32_t i_timestamp_start;
231 char *psz_destination;
235 /* Packetizer specific fields */
236 pf_rtp_packetizer_t pf_packetize;
239 /* for sending the packets */
240 sout_access_out_t *p_access;
242 vlc_mutex_t lock_rtsp;
244 sout_access_out_t **rtsp_access;
247 sout_input_t *p_input;
249 /* RTSP url control */
250 httpd_url_t *p_rtsp_url;
253 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
255 static void SDPHandleUrl( sout_stream_t *, char * );
257 static int SapSetup( sout_stream_t *p_stream );
258 static int FileSetup( sout_stream_t *p_stream );
259 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
260 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
262 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
263 httpd_message_t *, httpd_message_t * );
264 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
265 httpd_message_t *, httpd_message_t * );
268 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
269 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
270 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
272 /*****************************************************************************
274 *****************************************************************************/
275 static int Open( vlc_object_t *p_this )
277 sout_stream_t *p_stream = (sout_stream_t*)p_this;
278 sout_instance_t *p_sout = p_stream->p_sout;
279 sout_stream_sys_t *p_sys;
282 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
284 p_sys = malloc( sizeof( sout_stream_sys_t ) );
286 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
287 if( *p_sys->psz_destination == '\0' )
289 free( p_sys->psz_destination );
290 p_sys->psz_destination = NULL;
293 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
294 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
295 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
296 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
298 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
299 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
300 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
302 p_sys->psz_sdp_file = NULL;
304 if( p_sys->i_port_audio == p_sys->i_port_video )
306 msg_Err( p_stream, "audio and video port cannot be the same" );
307 p_sys->i_port_audio = 0;
308 p_sys->i_port_video = 0;
311 if( !p_sys->psz_session_name )
313 if( p_sys->psz_destination )
314 p_sys->psz_session_name = strdup( p_sys->psz_destination );
316 p_sys->psz_session_name = strdup( "NONE" );
319 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
322 vlc_bool_t b_ok = VLC_FALSE;
324 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
326 if( !strcmp( p_cfg->psz_name, "sdp" ) )
328 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
338 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
339 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
341 free( val2.psz_string );
346 msg_Err( p_stream, "missing destination and not in rtsp mode" );
350 p_sys->psz_destination = NULL;
352 else if( p_sys->i_port <= 0 )
354 msg_Err( p_stream, "invalid port" );
359 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
362 /* Normally, we should let the default hop limit up to the core,
363 * but we have to know it to build our SDP properly, which is why
364 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
366 val.i_int = config_GetInt( p_stream, "ttl" );
368 if( p_sys->i_ttl > 255 ) p_sys->i_ttl = 255;
369 /* must not exceed 999 once formatted */
371 if( p_sys->i_ttl < 0 )
373 msg_Err( p_stream, "illegal TTL %d", val.i_int );
377 p_sys->i_ttl = val.i_int;
379 p_sys->i_payload_type = 96;
384 p_sys->psz_sdp = NULL;
386 p_sys->i_sdp_id = mdate();
387 p_sys->i_sdp_version = 1;
388 p_sys->psz_sdp = NULL;
390 p_sys->b_export_sap = VLC_FALSE;
391 p_sys->b_export_sdp_file = VLC_FALSE;
392 p_sys->p_session = NULL;
394 p_sys->p_httpd_host = NULL;
395 p_sys->p_httpd_file = NULL;
396 p_sys->p_rtsp_host = NULL;
397 p_sys->p_rtsp_url = NULL;
398 p_sys->psz_rtsp_control = NULL;
399 p_sys->psz_rtsp_path = NULL;
401 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
402 vlc_mutex_init( p_stream, &p_sys->lock_es );
404 p_stream->pf_add = Add;
405 p_stream->pf_del = Del;
406 p_stream->pf_send = Send;
408 p_stream->p_sys = p_sys;
410 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
411 if( *val.psz_string )
413 sout_access_out_t *p_grab;
414 char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv;
416 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
418 msg_Err( p_stream, "rtp needs a destination when muxing" );
423 /* Check muxer type */
424 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
426 psz_rtpmap = "MP2P/90000";
428 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
430 psz_rtpmap = "MP2T/90000";
431 p_sys->i_payload_type = 33;
435 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
440 /* create the access out */
441 if( p_sys->i_ttl > 0 )
443 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
447 sprintf( access, "udp{raw}" );
450 /* IPv6 needs brackets if not already present */
451 snprintf( url, sizeof( url ),
452 ( ( p_sys->psz_destination[0] != '[' )
453 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
454 ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
456 url[sizeof( url ) - 1] = '\0';
457 /* FIXME: we should check that url is a numerical address, otherwise
458 * the SDP will be quite broken (regardless of the IP protocol version)
459 * Also it might be IPv6 with no ':' if it is a DNS name.
461 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
463 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
465 msg_Err( p_stream, "cannot create the access out for %s://%s",
470 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
471 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
473 /* better than nothing */
476 p_sys->i_mtu -= MTU_REDUCE;
478 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
479 p_grab = p_sys->p_grab =
480 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
481 p_grab->p_module = NULL;
482 p_grab->p_sout = p_sout;
483 p_grab->psz_access = strdup( "grab" );
484 p_grab->p_cfg = NULL;
485 p_grab->psz_name = strdup( "" );
486 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
487 p_grab->pf_seek = NULL;
488 p_grab->pf_write = AccessOutGrabberWrite;
491 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
493 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
494 sout_AccessOutDelete( p_sys->p_grab );
495 sout_AccessOutDelete( p_sys->p_access );
500 /* create the SDP for a muxed stream (only once) */
501 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
502 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
503 o= - should be local username (no spaces allowed)
504 o= time should be hashed with some other value to garantue uniqueness
505 o= we need IP6 support?
506 o= don't use the localhost address. use fully qualified domain name or IP4 address
507 p= international phone number (pass via vars?)
509 a= recvonly (missing)
510 a= type:broadcast (missing)
511 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
512 a= x-plgroup: (missing)
513 RTP packets need to get the correct src IP address */
514 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
516 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
517 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
524 asprintf( &p_sys->psz_sdp,
526 /* FIXME: source address not known :( */
527 "o=- "I64Fd" %d IN IP%c %s\r\n"
532 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
533 "a=tool:"PACKAGE_STRING"\r\n"
535 "m=video %d RTP/AVP %d\r\n"
536 "a=rtpmap:%d %s\r\n",
537 p_sys->i_sdp_id, p_sys->i_sdp_version,
538 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
539 p_sys->psz_session_name,
540 p_sys->psz_session_description,
541 p_sys->psz_session_url,
542 p_sys->psz_session_email,
543 ipv, p_sys->psz_destination, psz_ttl,
544 p_sys->i_port, p_sys->i_payload_type,
545 p_sys->i_payload_type, psz_rtpmap );
546 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
548 /* create the rtp context */
549 p_sys->ssrc[0] = rand()&0xff;
550 p_sys->ssrc[1] = rand()&0xff;
551 p_sys->ssrc[2] = rand()&0xff;
552 p_sys->ssrc[3] = rand()&0xff;
553 p_sys->i_sequence = rand()&0xffff;
554 p_sys->i_timestamp_start = rand()&0xffffffff;
555 p_sys->packet = NULL;
560 p_sys->p_access = NULL;
561 p_sys->p_grab = NULL;
563 free( val.psz_string );
566 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
567 if( *val.psz_string )
571 SDPHandleUrl( p_stream, val.psz_string );
573 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
575 if( !strcmp( p_cfg->psz_name, "sdp" ) )
577 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
580 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
583 SDPHandleUrl( p_stream, p_cfg->psz_value );
587 free( val.psz_string );
589 /* update p_sout->i_out_pace_nocontrol */
590 p_stream->p_sout->i_out_pace_nocontrol++;
595 /*****************************************************************************
597 *****************************************************************************/
598 static void Close( vlc_object_t * p_this )
600 sout_stream_t *p_stream = (sout_stream_t*)p_this;
601 sout_stream_sys_t *p_sys = p_stream->p_sys;
603 /* update p_sout->i_out_pace_nocontrol */
604 p_stream->p_sout->i_out_pace_nocontrol--;
608 sout_MuxDelete( p_sys->p_mux );
609 sout_AccessOutDelete( p_sys->p_access );
610 sout_AccessOutDelete( p_sys->p_grab );
613 block_Release( p_sys->packet );
615 if( p_sys->b_export_sap )
618 SapSetup( p_stream );
622 while( p_sys->i_rtsp > 0 )
624 RtspClientDel( p_stream, p_sys->rtsp[0] );
627 vlc_mutex_destroy( &p_sys->lock_sdp );
629 if( p_sys->p_httpd_file )
631 httpd_FileDelete( p_sys->p_httpd_file );
633 if( p_sys->p_httpd_host )
635 httpd_HostDelete( p_sys->p_httpd_host );
637 if( p_sys->p_rtsp_url )
639 httpd_UrlDelete( p_sys->p_rtsp_url );
641 if( p_sys->p_rtsp_host )
643 httpd_HostDelete( p_sys->p_rtsp_host );
646 /* why? is this disabled? */
647 if( p_sys->psz_session_name )
649 free( p_sys->psz_session_name );
650 p_sys->psz_session_name = NULL;
652 if( p_sys->psz_session_description )
654 free( p_sys->psz_session_description );
655 p_sys->psz_session_description = NULL;
657 if( p_sys->psz_session_url )
659 free( p_sys->psz_session_url );
660 p_sys->psz_session_url = NULL;
662 if( p_sys->psz_session_email )
664 free( p_sys->psz_session_email );
665 p_sys->psz_session_email = NULL;
670 free( p_sys->psz_sdp );
675 /*****************************************************************************
677 *****************************************************************************/
678 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
680 sout_stream_sys_t *p_sys = p_stream->p_sys;
683 vlc_UrlParse( &url, psz_url, 0 );
684 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
686 if( p_sys->p_httpd_file )
688 msg_Err( p_stream, "You can used sdp=http:// only once" );
692 if( HttpSetup( p_stream, &url ) )
694 msg_Err( p_stream, "cannot export sdp as http" );
697 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
699 if( p_sys->p_rtsp_url )
701 msg_Err( p_stream, "You can used sdp=rtsp:// only once" );
705 /* FIXME test if destination is multicast or no destination at all FIXME */
706 if( RtspSetup( p_stream, &url ) )
708 msg_Err( p_stream, "cannot export sdp as rtsp" );
711 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
712 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
714 p_sys->b_export_sap = VLC_TRUE;
715 SapSetup( p_stream );
717 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
719 if( p_sys->b_export_sdp_file )
721 msg_Err( p_stream, "You can used sdp=file:// only once" );
724 p_sys->b_export_sdp_file = VLC_TRUE;
725 psz_url = &psz_url[5];
726 if( psz_url[0] == '/' && psz_url[1] == '/' )
728 p_sys->psz_sdp_file = strdup( psz_url );
732 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
735 vlc_UrlClean( &url );
738 /*****************************************************************************
740 *****************************************************************************/
741 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
742 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
743 o= - should be local username (no spaces allowed)
744 o= time should be hashed with some other value to garantue uniqueness
745 o= we need IP6 support?
746 o= don't use the localhost address. use fully qualified domain name or IP4 address
747 p= international phone number (pass via vars?)
749 a= recvonly (missing)
750 a= type:broadcast (missing)
751 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
752 a= x-plgroup: (missing)
753 RTP packets need to get the correct src IP address */
754 static char *SDPGenerate( const sout_stream_t *p_stream,
755 const char *psz_destination, vlc_bool_t b_rtsp )
757 sout_stream_sys_t *p_sys = p_stream->p_sys;
758 sout_instance_t *p_sout = p_stream->p_sout;
760 char *psz_sdp, *p, ipv;
763 /* FIXME: breaks IP version check on unknown destination */
764 if( psz_destination == NULL )
765 psz_destination = "0.0.0.0";
767 i_size = sizeof( "v=0\r\n" ) +
768 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
769 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
770 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
771 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
772 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
773 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
774 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
775 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
776 strlen( psz_destination ) ;
777 for( i = 0; i < p_sys->i_es; i++ )
779 sout_stream_id_t *id = p_sys->es[i];
781 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
784 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
788 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
792 i_size += strlen( "a=control:*/trackid=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
796 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
798 p = psz_sdp = malloc( i_size );
799 p += sprintf( p, "v=0\r\n" );
800 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
801 p_sys->i_sdp_id, p_sys->i_sdp_version,
802 ipv, ipv == '6' ? "::" : "127.0.0.1" );
803 if( *p_sys->psz_session_name )
804 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
805 if( *p_sys->psz_session_description )
806 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
807 if( *p_sys->psz_session_url )
808 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
809 if( *p_sys->psz_session_email )
810 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
812 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
813 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
815 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
817 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
819 /* Add the ttl if it is a multicast address */
820 /* FIXME: 1 is not a correct default value in the case of IPv6 */
821 p += sprintf( p, "/%d\r\n", p_sys->i_ttl ?: 1 );
825 p += sprintf( p, "\r\n" );
828 for( i = 0; i < p_sys->i_es; i++ )
830 sout_stream_id_t *id = p_sys->es[i];
832 if( id->i_cat == AUDIO_ES )
834 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
835 id->i_port, id->i_payload_type );
837 else if( id->i_cat == VIDEO_ES )
839 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
840 id->i_port, id->i_payload_type );
848 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
853 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
858 p += sprintf( p, "a=control:%s/trackid=%d\r\n", p_sys->psz_rtsp_control, i );
865 /*****************************************************************************
867 *****************************************************************************/
868 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
869 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
870 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
871 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
872 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
873 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
874 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
875 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
876 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
878 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
880 static const char hex[16] = "0123456789abcdef";
883 for( i = 0; i < i_data; i++ )
885 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
886 s[2*i+1] = hex[(p_data[i] )&0xf];
891 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
893 sout_instance_t *p_sout = p_stream->p_sout;
894 sout_stream_sys_t *p_sys = p_stream->p_sys;
895 sout_stream_id_t *id;
896 sout_access_out_t *p_access = NULL;
900 if( p_sys->p_mux != NULL )
902 sout_input_t *p_input = NULL;
903 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
905 msg_Err( p_stream, "cannot add this stream to the muxer" );
909 id = malloc( sizeof( sout_stream_id_t ) );
910 memset( id, 0, sizeof( sout_stream_id_t ) );
912 id->p_input = p_input;
913 id->pf_packetize= NULL;
914 id->p_rtsp_url = NULL;
920 /* Choose the port */
922 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
924 i_port = p_sys->i_port_audio;
925 p_sys->i_port_audio = 0;
927 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
929 i_port = p_sys->i_port_video;
930 p_sys->i_port_video = 0;
934 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
936 i_port = p_sys->i_port;
943 if( p_sys->psz_destination )
946 char url[NI_MAXHOST + 8];
948 /* first try to create the access out */
951 snprintf( access, sizeof( access ), "udp{raw,ttl=%d}",
953 access[sizeof( access ) - 1] = '\0';
956 strcpy( access, "udp{raw}" );
958 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
959 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
960 p_sys->psz_destination, i_port );
961 url[sizeof( url ) - 1] = '\0';
963 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
965 msg_Err( p_stream, "cannot create the access out for %s://%s",
969 msg_Dbg( p_stream, "access out %s:%s", access, url );
972 /* not create the rtp specific stuff */
973 id = malloc( sizeof( sout_stream_id_t ) );
974 memset( id, 0, sizeof( sout_stream_id_t ) );
975 id->p_stream = p_stream;
976 id->p_access = p_access;
978 id->psz_rtpmap = NULL;
980 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
982 id->p_rtsp_url = NULL;
983 vlc_mutex_init( p_stream, &id->lock_rtsp );
984 id->i_rtsp_access = 0;
985 id->rtsp_access = NULL;
987 switch( p_fmt->i_codec )
989 case VLC_FOURCC( 's', '1', '6', 'b' ):
990 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
992 id->i_payload_type = 11;
994 else if( p_fmt->audio.i_channels == 2 &&
995 p_fmt->audio.i_rate == 44100 )
997 id->i_payload_type = 10;
1001 id->i_payload_type = p_sys->i_payload_type++;
1003 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
1004 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
1005 p_fmt->audio.i_channels );
1006 id->i_clock_rate = p_fmt->audio.i_rate;
1007 id->pf_packetize = rtp_packetize_l16;
1009 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
1010 id->i_payload_type = p_sys->i_payload_type++;
1011 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
1012 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
1013 p_fmt->audio.i_channels );
1014 id->i_clock_rate = p_fmt->audio.i_rate;
1015 id->pf_packetize = rtp_packetize_l8;
1017 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1018 id->i_payload_type = 14;
1019 id->i_clock_rate = 90000;
1020 id->psz_rtpmap = strdup( "MPA/90000" );
1021 id->pf_packetize = rtp_packetize_mpa;
1023 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1024 id->i_payload_type = 32;
1025 id->i_clock_rate = 90000;
1026 id->psz_rtpmap = strdup( "MPV/90000" );
1027 id->pf_packetize = rtp_packetize_mpv;
1029 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1030 id->i_payload_type = p_sys->i_payload_type++;
1031 id->i_clock_rate = 90000;
1032 id->psz_rtpmap = strdup( "ac3/90000" );
1033 id->pf_packetize = rtp_packetize_ac3;
1035 case VLC_FOURCC( 'H', '2', '6', '3' ):
1036 id->i_payload_type = p_sys->i_payload_type++;
1037 id->i_clock_rate = 90000;
1038 id->psz_rtpmap = strdup( "H263-1998/90000" );
1039 id->pf_packetize = rtp_packetize_h263;
1042 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1044 char hexa[2*p_fmt->i_extra +1];
1046 id->i_payload_type = p_sys->i_payload_type++;
1047 id->i_clock_rate = 90000;
1048 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1049 id->pf_packetize = rtp_packetize_split;
1050 if( p_fmt->i_extra > 0 )
1052 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1053 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1054 sprintf( id->psz_fmtp,
1055 "profile-level-id=3; config=%s;", hexa );
1059 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1061 char hexa[2*p_fmt->i_extra +1];
1063 id->i_payload_type = p_sys->i_payload_type++;
1064 id->i_clock_rate = p_fmt->audio.i_rate;
1065 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1066 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1067 id->pf_packetize = rtp_packetize_mp4a;
1068 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1069 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1070 sprintf( id->psz_fmtp,
1071 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1072 "config=%s; SizeLength=13;IndexLength=3; "
1073 "IndexDeltaLength=3; Profile=1;", hexa );
1076 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1077 id->i_payload_type = p_sys->i_payload_type++;
1078 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1079 "AMR/8000/2" : "AMR/8000" );
1080 id->psz_fmtp = strdup( "octet-align=1" );
1081 id->i_clock_rate = p_fmt->audio.i_rate;
1082 id->pf_packetize = rtp_packetize_amr;
1084 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1085 id->i_payload_type = p_sys->i_payload_type++;
1086 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1087 "AMR-WB/16000/2" : "AMR-WB/16000" );
1088 id->psz_fmtp = strdup( "octet-align=1" );
1089 id->i_clock_rate = p_fmt->audio.i_rate;
1090 id->pf_packetize = rtp_packetize_amr;
1094 msg_Err( p_stream, "cannot add this stream (unsupported "
1095 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1098 sout_AccessOutDelete( p_access );
1103 id->i_cat = p_fmt->i_cat;
1105 id->ssrc[0] = rand()&0xff;
1106 id->ssrc[1] = rand()&0xff;
1107 id->ssrc[2] = rand()&0xff;
1108 id->ssrc[3] = rand()&0xff;
1109 id->i_sequence = rand()&0xffff;
1110 id->i_timestamp_start = rand()&0xffffffff;
1112 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1113 if( id->i_mtu <= 16 + MTU_REDUCE )
1115 /* better than nothing */
1118 id->i_mtu -= MTU_REDUCE;
1119 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1121 if( p_sys->p_rtsp_url )
1123 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1125 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1126 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
1127 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1129 if( id->p_rtsp_url )
1131 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1132 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1133 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1138 /* Update p_sys context */
1139 vlc_mutex_lock( &p_sys->lock_es );
1140 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1141 vlc_mutex_unlock( &p_sys->lock_es );
1143 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1145 vlc_mutex_lock( &p_sys->lock_sdp );
1146 free( p_sys->psz_sdp );
1147 p_sys->psz_sdp = psz_sdp;
1148 vlc_mutex_unlock( &p_sys->lock_sdp );
1150 p_sys->i_sdp_version++;
1152 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
1154 /* Update SDP (sap/file) */
1155 if( p_sys->b_export_sap ) SapSetup( p_stream );
1156 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1161 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1163 sout_stream_sys_t *p_sys = p_stream->p_sys;
1165 vlc_mutex_lock( &p_sys->lock_es );
1166 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1167 vlc_mutex_unlock( &p_sys->lock_es );
1170 if( id->i_port > 0 )
1172 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1173 p_sys->i_port_audio = id->i_port;
1174 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1175 p_sys->i_port_video = id->i_port;
1180 if( id->psz_rtpmap )
1182 free( id->psz_rtpmap );
1186 free( id->psz_fmtp );
1188 if( id->psz_destination )
1189 free( id->psz_destination );
1190 sout_AccessOutDelete( id->p_access );
1192 else if( id->p_input )
1194 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1196 if( id->p_rtsp_url )
1198 httpd_UrlDelete( id->p_rtsp_url );
1200 vlc_mutex_destroy( &id->lock_rtsp );
1201 if( id->rtsp_access ) free( id->rtsp_access );
1203 /* Update SDP (sap/file) */
1204 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1205 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1211 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1216 if( p_stream->p_sys->p_mux )
1218 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1224 p_next = p_buffer->p_next;
1225 if( id->pf_packetize( p_stream, id, p_buffer ) )
1229 block_Release( p_buffer );
1237 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1240 sout_stream_sys_t *p_sys = p_stream->p_sys;
1242 int64_t i_dts = p_buffer->i_dts;
1243 uint32_t i_timestamp = i_dts * 9 / 100;
1245 uint8_t *p_data = p_buffer->p_buffer;
1246 unsigned int i_data = p_buffer->i_buffer;
1247 unsigned int i_max = p_sys->i_mtu - 12;
1249 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1253 unsigned int i_size;
1255 /* output complete packet */
1256 if( p_sys->packet &&
1257 p_sys->packet->i_buffer + i_data > i_max )
1259 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1260 p_sys->packet = NULL;
1263 if( p_sys->packet == NULL )
1265 /* allocate a new packet */
1266 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1267 p_sys->packet->p_buffer[ 0] = 0x80;
1268 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1269 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1270 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1271 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1272 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1273 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1274 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1275 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1276 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1277 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1278 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1279 p_sys->packet->i_buffer = 12;
1281 p_sys->packet->i_dts = i_dts;
1282 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1283 i_dts += p_sys->packet->i_length;
1285 p_sys->i_sequence++;
1288 i_size = __MIN( i_data,
1289 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1291 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1294 p_sys->packet->i_buffer += i_size;
1302 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1305 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1307 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1313 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1315 p_next = p_buffer->p_next;
1316 block_Release( p_buffer );
1323 /****************************************************************************
1325 ****************************************************************************/
1326 static int SapSetup( sout_stream_t *p_stream )
1328 sout_stream_sys_t *p_sys = p_stream->p_sys;
1329 sout_instance_t *p_sout = p_stream->p_sout;
1330 announce_method_t *p_method = sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
1332 /* Remove the previous session */
1333 if( p_sys->p_session != NULL)
1335 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1336 sout_AnnounceSessionDestroy( p_sys->p_session );
1337 p_sys->p_session = NULL;
1340 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1342 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1343 p_sys->psz_destination,
1351 /****************************************************************************
1353 ****************************************************************************/
1354 static int FileSetup( sout_stream_t *p_stream )
1356 sout_stream_sys_t *p_sys = p_stream->p_sys;
1359 if( ( f = utf8_fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1361 msg_Err( p_stream, "cannot open file '%s' (%s)",
1362 p_sys->psz_sdp_file, strerror(errno) );
1363 return VLC_EGENERIC;
1366 fprintf( f, "%s", p_sys->psz_sdp );
1372 /****************************************************************************
1374 ****************************************************************************/
1375 static int HttpCallback( httpd_file_sys_t *p_args,
1376 httpd_file_t *, uint8_t *p_request,
1377 uint8_t **pp_data, int *pi_data );
1379 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1381 sout_stream_sys_t *p_sys = p_stream->p_sys;
1383 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1384 if( p_sys->p_httpd_host )
1386 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1387 url->psz_path ? url->psz_path : "/",
1390 HttpCallback, (void*)p_sys );
1392 if( p_sys->p_httpd_file == NULL )
1394 return VLC_EGENERIC;
1399 static int HttpCallback( httpd_file_sys_t *p_args,
1400 httpd_file_t *f, uint8_t *p_request,
1401 uint8_t **pp_data, int *pi_data )
1403 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1405 vlc_mutex_lock( &p_sys->lock_sdp );
1406 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1408 *pi_data = strlen( p_sys->psz_sdp );
1409 *pp_data = malloc( *pi_data );
1410 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1417 vlc_mutex_unlock( &p_sys->lock_sdp );
1422 /****************************************************************************
1424 ****************************************************************************/
1425 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1427 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1429 rtsp->psz_session = psz_session;
1431 rtsp->b_playing = VLC_FALSE;
1435 rtsp->access = NULL;
1437 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1441 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1444 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1446 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1448 return p_stream->p_sys->rtsp[i];
1454 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1457 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1459 for( i = 0; i < rtsp->i_access; i++ )
1461 sout_AccessOutDelete( rtsp->access[i] );
1463 if( rtsp->id ) free( rtsp->id );
1464 if( rtsp->access ) free( rtsp->access );
1466 free( rtsp->psz_session );
1470 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1472 sout_stream_sys_t *p_sys = p_stream->p_sys;
1474 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1476 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1477 if( p_sys->p_rtsp_host == NULL )
1479 return VLC_EGENERIC;
1482 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1483 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1484 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1485 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1487 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1488 if( p_sys->p_rtsp_url == 0 )
1490 return VLC_EGENERIC;
1492 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1493 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1494 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1495 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1500 static int RtspCallback( httpd_callback_sys_t *p_args,
1502 httpd_message_t *answer, httpd_message_t *query )
1504 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1505 sout_stream_sys_t *p_sys = p_stream->p_sys;
1506 char *psz_destination = p_sys->psz_destination;
1507 char *psz_session = NULL;
1509 if( answer == NULL || query == NULL )
1513 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1515 answer->i_proto = HTTPD_PROTO_RTSP;
1516 answer->i_version= query->i_version;
1517 answer->i_type = HTTPD_MSG_ANSWER;
1519 switch( query->i_type )
1521 case HTTPD_MSG_DESCRIBE:
1523 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1525 answer->i_status = 200;
1526 answer->psz_status = strdup( "OK" );
1527 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1529 answer->p_body = (uint8_t *)psz_sdp;
1530 answer->i_body = strlen( psz_sdp );
1534 case HTTPD_MSG_PLAY:
1536 rtsp_client_t *rtsp;
1537 /* for now only multicast so easy */
1538 answer->i_status = 200;
1539 answer->psz_status = strdup( "OK" );
1541 answer->p_body = NULL;
1543 psz_session = httpd_MsgGet( query, "Session" );
1544 rtsp = RtspClientGet( p_stream, psz_session );
1545 if( rtsp && !rtsp->b_playing )
1549 rtsp->b_playing = VLC_TRUE;
1551 vlc_mutex_lock( &p_sys->lock_es );
1552 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1554 sout_stream_id_t *id = rtsp->id[i_id];
1557 for( i = 0; i < p_sys->i_es; i++ )
1559 if( id == p_sys->es[i] )
1562 if( i >= p_sys->i_es ) continue;
1564 vlc_mutex_lock( &id->lock_rtsp );
1565 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1566 vlc_mutex_unlock( &id->lock_rtsp );
1568 vlc_mutex_unlock( &p_sys->lock_es );
1572 case HTTPD_MSG_PAUSE:
1574 return VLC_EGENERIC;
1575 case HTTPD_MSG_TEARDOWN:
1577 rtsp_client_t *rtsp;
1579 /* for now only multicast so easy again */
1580 answer->i_status = 200;
1581 answer->psz_status = strdup( "OK" );
1583 answer->p_body = NULL;
1585 psz_session = httpd_MsgGet( query, "Session" );
1586 rtsp = RtspClientGet( p_stream, psz_session );
1591 vlc_mutex_lock( &p_sys->lock_es );
1592 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1594 sout_stream_id_t *id = rtsp->id[i_id];
1597 for( i = 0; i < p_sys->i_es; i++ )
1599 if( id == p_sys->es[i] )
1602 if( i >= p_sys->i_es ) continue;
1604 vlc_mutex_lock( &id->lock_rtsp );
1605 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1606 vlc_mutex_unlock( &id->lock_rtsp );
1608 vlc_mutex_unlock( &p_sys->lock_es );
1610 RtspClientDel( p_stream, rtsp );
1616 return VLC_EGENERIC;
1618 httpd_MsgAdd( answer, "Server", "VLC Server" );
1619 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1620 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1621 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1625 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1630 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1632 httpd_message_t *answer, httpd_message_t *query )
1634 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1635 sout_stream_t *p_stream = id->p_stream;
1636 sout_instance_t *p_sout = p_stream->p_sout;
1637 sout_stream_sys_t *p_sys = p_stream->p_sys;
1638 char *psz_session = NULL;
1640 if( answer == NULL || query == NULL )
1644 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1646 answer->i_proto = HTTPD_PROTO_RTSP;
1647 answer->i_version= query->i_version;
1648 answer->i_type = HTTPD_MSG_ANSWER;
1650 switch( query->i_type )
1652 case HTTPD_MSG_SETUP:
1654 char *psz_transport = httpd_MsgGet( query, "Transport" );
1656 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1658 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1660 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1661 answer->i_status = 200;
1662 answer->psz_status = strdup( "OK" );
1664 answer->p_body = NULL;
1665 psz_session = httpd_MsgGet( query, "Session" );
1666 if( *psz_session == 0 )
1668 psz_session = malloc( 100 );
1669 sprintf( psz_session, "%d", rand() );
1671 httpd_MsgAdd( answer, "Transport",
1672 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1673 id->psz_destination, id->i_port,id->i_port+1,
1676 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1678 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1679 char ip[NI_MAXNUMERICHOST], psz_access[17], psz_url[NI_MAXNUMERICHOST + 8];
1681 sout_access_out_t *p_access;
1683 rtsp_client_t *rtsp = NULL;
1685 if( httpd_ClientIP( cl, ip ) == NULL )
1687 answer->i_status = 500;
1688 answer->psz_status = strdup( "Internal server error" );
1690 answer->p_body = NULL;
1694 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1697 psz_session = httpd_MsgGet( query, "Session" );
1698 if( *psz_session == 0 )
1700 psz_session = malloc( 100 );
1701 sprintf( psz_session, "%d", rand() );
1703 rtsp = RtspClientNew( p_stream, psz_session );
1707 rtsp = RtspClientGet( p_stream, psz_session );
1710 answer->i_status = 454;
1711 answer->psz_status = strdup( "Unknown session id" );
1713 answer->p_body = NULL;
1718 /* first try to create the access out */
1720 snprintf( psz_access, sizeof( psz_access ),
1721 "udp{raw,ttl=%d}", p_sys->i_ttl );
1723 strncpy( psz_access, "udp{raw}", sizeof( psz_access ) );
1724 psz_access[sizeof( psz_access ) - 1] = '\0';
1726 snprintf( psz_url, sizeof( psz_url ),
1727 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
1730 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1732 msg_Err( p_stream, "cannot create the access out for %s://%s",
1733 psz_access, psz_url );
1734 answer->i_status = 500;
1735 answer->psz_status = strdup( "Internal server error" );
1737 answer->p_body = NULL;
1741 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1742 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1744 answer->i_status = 200;
1745 answer->psz_status = strdup( "OK" );
1747 answer->p_body = NULL;
1749 httpd_MsgAdd( answer, "Transport",
1750 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1752 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1754 answer->i_status = 461;
1755 answer->psz_status = strdup( "Unsupported Transport" );
1757 answer->p_body = NULL;
1763 return VLC_EGENERIC;
1765 httpd_MsgAdd( answer, "Server", "VLC Server" );
1766 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1767 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1768 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1772 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1777 /****************************************************************************
1779 ****************************************************************************/
1780 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1781 int b_marker, int64_t i_pts )
1783 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1785 out->p_buffer[0] = 0x80;
1786 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1787 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1788 out->p_buffer[3] = ( id->i_sequence )&0xff;
1789 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1790 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1791 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1792 out->p_buffer[7] = ( i_timestamp )&0xff;
1794 out->p_buffer[ 8] = id->ssrc[0];
1795 out->p_buffer[ 9] = id->ssrc[1];
1796 out->p_buffer[10] = id->ssrc[2];
1797 out->p_buffer[11] = id->ssrc[3];
1803 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1806 vlc_mutex_lock( &id->lock_rtsp );
1807 for( i = 0; i < id->i_rtsp_access; i++ )
1809 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1811 vlc_mutex_unlock( &id->lock_rtsp );
1815 sout_AccessOutWrite( id->p_access, out );
1819 block_Release( out );
1823 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1826 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1827 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1829 uint8_t *p_data = in->p_buffer;
1830 int i_data = in->i_buffer;
1833 for( i = 0; i < i_count; i++ )
1835 int i_payload = __MIN( i_max, i_data );
1836 block_t *out = block_New( p_stream, 16 + i_payload );
1838 /* rtp common header */
1839 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1841 out->p_buffer[12] = 0;
1842 out->p_buffer[13] = 0;
1843 /* fragment offset in the current frame */
1844 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1845 out->p_buffer[15] = ( (i*i_max) )&0xff;
1846 memcpy( &out->p_buffer[16], p_data, i_payload );
1848 out->i_buffer = 16 + i_payload;
1849 out->i_dts = in->i_dts + i * in->i_length / i_count;
1850 out->i_length = in->i_length / i_count;
1852 rtp_packetize_send( id, out );
1854 p_data += i_payload;
1855 i_data -= i_payload;
1862 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1865 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1866 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1868 uint8_t *p_data = in->p_buffer;
1869 int i_data = in->i_buffer;
1871 int b_sequence_start = 0;
1872 int i_temporal_ref = 0;
1873 int i_picture_coding_type = 0;
1874 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1875 int b_start_slice = 0;
1877 /* preparse this packet to get some info */
1878 if( in->i_buffer > 4 )
1880 uint8_t *p = p_data;
1881 int i_rest = in->i_buffer;
1885 while( i_rest > 4 &&
1886 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1900 /* sequence start code */
1901 b_sequence_start = 1;
1903 else if( *p == 0x00 && i_rest >= 4 )
1906 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1907 i_picture_coding_type = (p[2] >> 3)&0x07;
1909 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1910 i_picture_coding_type == 3 ) )
1912 i_ffv = (p[3] >> 2)&0x01;
1913 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1914 if( i_rest > 4 && i_picture_coding_type == 3 )
1916 i_fbv = (p[4]>>6)&0x01;
1917 i_bfc = (p[4]>>3)&0x07;
1921 else if( *p <= 0xaf )
1928 for( i = 0; i < i_count; i++ )
1930 int i_payload = __MIN( i_max, i_data );
1931 block_t *out = block_New( p_stream,
1933 uint32_t h = ( i_temporal_ref << 16 )|
1934 ( b_sequence_start << 13 )|
1935 ( b_start_slice << 12 )|
1936 ( i == i_count - 1 ? 1 << 11 : 0 )|
1937 ( i_picture_coding_type << 8 )|
1938 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1940 /* rtp common header */
1941 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1942 in->i_pts > 0 ? in->i_pts : in->i_dts );
1944 /* 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 */
1945 out->p_buffer[12] = ( h >> 24 )&0xff;
1946 out->p_buffer[13] = ( h >> 16 )&0xff;
1947 out->p_buffer[14] = ( h >> 8 )&0xff;
1948 out->p_buffer[15] = ( h )&0xff;
1950 memcpy( &out->p_buffer[16], p_data, i_payload );
1952 out->i_buffer = 16 + i_payload;
1953 out->i_dts = in->i_dts + i * in->i_length / i_count;
1954 out->i_length = in->i_length / i_count;
1956 rtp_packetize_send( id, out );
1958 p_data += i_payload;
1959 i_data -= i_payload;
1964 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1967 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1968 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1970 uint8_t *p_data = in->p_buffer;
1971 int i_data = in->i_buffer;
1974 for( i = 0; i < i_count; i++ )
1976 int i_payload = __MIN( i_max, i_data );
1977 block_t *out = block_New( p_stream, 14 + i_payload );
1979 /* rtp common header */
1980 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1982 out->p_buffer[12] = 1;
1984 out->p_buffer[13] = 0x00;
1986 memcpy( &out->p_buffer[14], p_data, i_payload );
1988 out->i_buffer = 14 + i_payload;
1989 out->i_dts = in->i_dts + i * in->i_length / i_count;
1990 out->i_length = in->i_length / i_count;
1992 rtp_packetize_send( id, out );
1994 p_data += i_payload;
1995 i_data -= i_payload;
2001 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
2004 int i_max = id->i_mtu - 12; /* payload max in one packet */
2005 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2007 uint8_t *p_data = in->p_buffer;
2008 int i_data = in->i_buffer;
2011 for( i = 0; i < i_count; i++ )
2013 int i_payload = __MIN( i_max, i_data );
2014 block_t *out = block_New( p_stream, 12 + i_payload );
2016 /* rtp common header */
2017 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2018 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2019 memcpy( &out->p_buffer[12], p_data, i_payload );
2021 out->i_buffer = 12 + i_payload;
2022 out->i_dts = in->i_dts + i * in->i_length / i_count;
2023 out->i_length = in->i_length / i_count;
2025 rtp_packetize_send( id, out );
2027 p_data += i_payload;
2028 i_data -= i_payload;
2034 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
2037 int i_max = id->i_mtu - 12; /* payload max in one packet */
2038 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2040 uint8_t *p_data = in->p_buffer;
2041 int i_data = in->i_buffer;
2046 int i_payload = (__MIN( i_max, i_data )/4)*4;
2047 block_t *out = block_New( p_stream, 12 + i_payload );
2049 /* rtp common header */
2050 rtp_packetize_common( id, out, 0,
2051 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2052 memcpy( &out->p_buffer[12], p_data, i_payload );
2054 out->i_buffer = 12 + i_payload;
2055 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2056 out->i_length = in->i_length / i_count;
2058 rtp_packetize_send( id, out );
2060 p_data += i_payload;
2061 i_data -= i_payload;
2068 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2071 int i_max = id->i_mtu - 12; /* payload max in one packet */
2072 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2074 uint8_t *p_data = in->p_buffer;
2075 int i_data = in->i_buffer;
2080 int i_payload = (__MIN( i_max, i_data )/2)*2;
2081 block_t *out = block_New( p_stream, 12 + i_payload );
2083 /* rtp common header */
2084 rtp_packetize_common( id, out, 0,
2085 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2086 memcpy( &out->p_buffer[12], p_data, i_payload );
2088 out->i_buffer = 12 + i_payload;
2089 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2090 out->i_length = in->i_length / i_count;
2092 rtp_packetize_send( id, out );
2094 p_data += i_payload;
2095 i_data -= i_payload;
2101 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2104 int i_max = id->i_mtu - 16; /* payload max in one packet */
2105 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2107 uint8_t *p_data = in->p_buffer;
2108 int i_data = in->i_buffer;
2111 for( i = 0; i < i_count; i++ )
2113 int i_payload = __MIN( i_max, i_data );
2114 block_t *out = block_New( p_stream, 16 + i_payload );
2116 /* rtp common header */
2117 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2118 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2120 /* AU headers length (bits) */
2121 out->p_buffer[12] = 0;
2122 out->p_buffer[13] = 2*8;
2123 /* for each AU length 13 bits + idx 3bits, */
2124 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2125 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2127 memcpy( &out->p_buffer[16], p_data, i_payload );
2129 out->i_buffer = 16 + i_payload;
2130 out->i_dts = in->i_dts + i * in->i_length / i_count;
2131 out->i_length = in->i_length / i_count;
2133 rtp_packetize_send( id, out );
2135 p_data += i_payload;
2136 i_data -= i_payload;
2144 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2145 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2146 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2149 uint8_t *p_data = in->p_buffer;
2150 int i_data = in->i_buffer;
2152 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2155 int b_v_bit = 0; // no pesky error resilience
2156 int i_plen = 0; // normally plen=0 for PSC packet
2157 int i_pebit = 0; // because plen=0
2162 return VLC_EGENERIC;
2164 if( p_data[0] || p_data[1] )
2166 return VLC_EGENERIC;
2168 /* remove 2 leading 0 bytes */
2171 i_count = ( i_data + i_max - 1 ) / i_max;
2173 for( i = 0; i < i_count; i++ )
2175 int i_payload = __MIN( i_max, i_data );
2176 block_t *out = block_New( p_stream,
2177 RTP_H263_PAYLOAD_START + i_payload );
2178 b_p_bit = (i == 0) ? 1 : 0;
2179 h = ( b_p_bit << 10 )|
2184 /* rtp common header */
2185 //b_m_bit = 1; // always contains end of frame
2186 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2187 in->i_pts > 0 ? in->i_pts : in->i_dts );
2190 out->p_buffer[12] = ( h >> 8 )&0xff;
2191 out->p_buffer[13] = ( h )&0xff;
2192 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2194 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2195 out->i_dts = in->i_dts + i * in->i_length / i_count;
2196 out->i_length = in->i_length / i_count;
2198 rtp_packetize_send( id, out );
2200 p_data += i_payload;
2201 i_data -= i_payload;
2207 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2210 int i_max = id->i_mtu - 14; /* payload max in one packet */
2211 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2213 uint8_t *p_data = in->p_buffer;
2214 int i_data = in->i_buffer;
2217 /* Only supports octet-aligned mode */
2218 for( i = 0; i < i_count; i++ )
2220 int i_payload = __MIN( i_max, i_data );
2221 block_t *out = block_New( p_stream, 14 + i_payload );
2223 /* rtp common header */
2224 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2225 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2226 /* Payload header */
2227 out->p_buffer[12] = 0xF0; /* CMR */
2228 out->p_buffer[13] = 0x00; /* ToC */ /* FIXME: frame type */
2230 /* FIXME: are we fed multiple frames ? */
2231 memcpy( &out->p_buffer[14], p_data, i_payload );
2233 out->i_buffer = 14 + i_payload;
2234 out->i_dts = in->i_dts + i * in->i_length / i_count;
2235 out->i_length = in->i_length / i_count;
2237 rtp_packetize_send( id, out );
2239 p_data += i_payload;
2240 i_data -= i_payload;