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"
38 /*****************************************************************************
40 *****************************************************************************/
44 #define DST_TEXT N_("Destination")
45 #define DST_LONGTEXT N_( \
46 "Allows you to specify the output URL used for the streaming output." )
47 #define SDP_TEXT N_("SDP")
48 #define SDP_LONGTEXT N_( \
49 "Allows you to specify the SDP used for the streaming output. " \
50 "You must use an url: http://location to access the SDP via HTTP, " \
51 "rtsp://location for RTSP access, and sap:// for the SDP to be " \
52 "announced via SAP." )
53 #define MUX_TEXT N_("Muxer")
54 #define MUX_LONGTEXT N_( \
55 "Allows you to specify the muxer used for the streaming output." )
57 #define NAME_TEXT N_("Session name")
58 #define NAME_LONGTEXT N_( \
59 "Allows you to specify the session name used for the streaming output." )
60 #define DESC_TEXT N_("Session description")
61 #define DESC_LONGTEXT N_( \
62 "Allows you to give a broader description of the stream." )
63 #define URL_TEXT N_("Session URL")
64 #define URL_LONGTEXT N_( \
65 "Allows you to specify a URL with additional information on the stream." )
66 #define EMAIL_TEXT N_("Session email")
67 #define EMAIL_LONGTEXT N_( \
68 "Allows you to specify contact e-mail address for this session." )
70 #define PORT_TEXT N_("Port")
71 #define PORT_LONGTEXT N_( \
72 "Allows you to specify the base port used for the RTP streaming." )
73 #define PORT_AUDIO_TEXT N_("Audio port")
74 #define PORT_AUDIO_LONGTEXT N_( \
75 "Allows you to specify the default audio port used for the RTP streaming." )
76 #define PORT_VIDEO_TEXT N_("Video port")
77 #define PORT_VIDEO_LONGTEXT N_( \
78 "Allows you to specify the default video port used for the RTP streaming." )
80 #define TTL_TEXT N_("Time-To-Live (TTL)")
81 #define TTL_LONGTEXT N_( \
82 "Allows you to specify the Time-To-Live for the output stream." )
84 static int Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
87 #define SOUT_CFG_PREFIX "sout-rtp-"
90 set_shortname( _("RTP"));
91 set_description( _("RTP stream output") );
92 set_capability( "sout stream", 0 );
93 add_shortcut( "rtp" );
94 set_category( CAT_SOUT );
95 set_subcategory( SUBCAT_SOUT_STREAM );
97 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
98 DST_LONGTEXT, VLC_TRUE );
99 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
100 SDP_LONGTEXT, VLC_TRUE );
101 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
102 MUX_LONGTEXT, VLC_TRUE );
104 add_string( SOUT_CFG_PREFIX "name", "NONE", NULL, NAME_TEXT,
105 NAME_LONGTEXT, VLC_TRUE );
106 add_string( SOUT_CFG_PREFIX "description", "", NULL, DESC_TEXT,
107 DESC_LONGTEXT, VLC_TRUE );
108 add_string( SOUT_CFG_PREFIX "url", "", NULL, URL_TEXT,
109 URL_LONGTEXT, VLC_TRUE );
110 add_string( SOUT_CFG_PREFIX "email", "", NULL, EMAIL_TEXT,
111 EMAIL_LONGTEXT, VLC_TRUE );
113 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
114 PORT_LONGTEXT, VLC_TRUE );
115 add_integer( SOUT_CFG_PREFIX "port-audio", 1230, NULL, PORT_AUDIO_TEXT,
116 PORT_AUDIO_LONGTEXT, VLC_TRUE );
117 add_integer( SOUT_CFG_PREFIX "port-video", 1232, NULL, PORT_VIDEO_TEXT,
118 PORT_VIDEO_LONGTEXT, VLC_TRUE );
120 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
121 TTL_LONGTEXT, VLC_TRUE );
123 set_callbacks( Open, Close );
126 /*****************************************************************************
127 * Exported prototypes
128 *****************************************************************************/
129 static const char *ppsz_sout_options[] = {
130 "dst", "name", "port", "port-audio", "port-video", "*sdp", "ttl", "mux",
131 "description", "url","email", NULL
134 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
135 static int Del ( sout_stream_t *, sout_stream_id_t * );
136 static int Send( sout_stream_t *, sout_stream_id_t *,
139 /* For unicast/interleaved streaming */
143 int64_t i_last; /* for timeout */
145 /* is it in "play" state */
146 vlc_bool_t b_playing;
148 /* output (id-access) */
150 sout_stream_id_t **id;
152 sout_access_out_t **access;
155 struct sout_stream_sys_t
161 vlc_mutex_t lock_sdp;
163 char *psz_session_name;
164 char *psz_session_description;
165 char *psz_session_url;
166 char *psz_session_email;
169 vlc_bool_t b_export_sdp_file;
172 vlc_bool_t b_export_sap;
173 session_descriptor_t *p_session;
175 httpd_host_t *p_httpd_host;
176 httpd_file_t *p_httpd_file;
178 httpd_host_t *p_rtsp_host;
179 httpd_url_t *p_rtsp_url;
180 char *psz_rtsp_control;
184 char *psz_destination;
190 /* when need to use a private one or when using muxer */
193 /* in case we do TS/PS over rtp */
195 sout_access_out_t *p_access;
197 sout_access_out_t *p_grab;
199 uint32_t i_timestamp_start;
206 sout_stream_id_t **es;
210 rtsp_client_t **rtsp;
213 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
216 struct sout_stream_id_t
218 sout_stream_t *p_stream;
220 uint8_t i_payload_type;
222 uint32_t i_timestamp_start;
229 char *psz_destination;
233 /* Packetizer specific fields */
234 pf_rtp_packetizer_t pf_packetize;
237 /* for sending the packets */
238 sout_access_out_t *p_access;
240 vlc_mutex_t lock_rtsp;
242 sout_access_out_t **rtsp_access;
245 sout_input_t *p_input;
247 /* RTSP url control */
248 httpd_url_t *p_rtsp_url;
251 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
253 static void SDPHandleUrl( sout_stream_t *, char * );
255 static int SapSetup( sout_stream_t *p_stream );
256 static int FileSetup( sout_stream_t *p_stream );
257 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
258 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
260 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
261 httpd_message_t *, httpd_message_t * );
262 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
263 httpd_message_t *, httpd_message_t * );
266 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
267 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
268 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
270 /*****************************************************************************
272 *****************************************************************************/
273 static int Open( vlc_object_t *p_this )
275 sout_stream_t *p_stream = (sout_stream_t*)p_this;
276 sout_instance_t *p_sout = p_stream->p_sout;
277 sout_stream_sys_t *p_sys;
280 sout_CfgParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
282 p_sys = malloc( sizeof( sout_stream_sys_t ) );
284 p_sys->psz_destination = var_GetString( p_stream, SOUT_CFG_PREFIX "dst" );
285 if( *p_sys->psz_destination == '\0' )
287 free( p_sys->psz_destination );
288 p_sys->psz_destination = NULL;
291 p_sys->psz_session_name = var_GetString( p_stream, SOUT_CFG_PREFIX "name" );
292 p_sys->psz_session_description = var_GetString( p_stream, SOUT_CFG_PREFIX "description" );
293 p_sys->psz_session_url = var_GetString( p_stream, SOUT_CFG_PREFIX "url" );
294 p_sys->psz_session_email = var_GetString( p_stream, SOUT_CFG_PREFIX "email" );
296 p_sys->i_port = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port" );
297 p_sys->i_port_audio = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-audio" );
298 p_sys->i_port_video = var_GetInteger( p_stream, SOUT_CFG_PREFIX "port-video" );
300 p_sys->psz_sdp_file = NULL;
302 if( p_sys->i_port_audio == p_sys->i_port_video )
304 msg_Err( p_stream, "audio and video port cannot be the same" );
305 p_sys->i_port_audio = 0;
306 p_sys->i_port_video = 0;
309 if( !p_sys->psz_session_name )
311 if( p_sys->psz_destination )
312 p_sys->psz_session_name = strdup( p_sys->psz_destination );
314 p_sys->psz_session_name = strdup( "NONE" );
317 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
320 vlc_bool_t b_ok = VLC_FALSE;
322 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
324 if( !strcmp( p_cfg->psz_name, "sdp" ) )
326 if( p_cfg->psz_value && !strncasecmp( p_cfg->psz_value, "rtsp", 4 ) )
336 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val2 );
337 if( !strncasecmp( val2.psz_string, "rtsp", 4 ) )
339 free( val2.psz_string );
344 msg_Err( p_stream, "missing destination and not in rtsp mode" );
348 p_sys->psz_destination = NULL;
350 else if( p_sys->i_port <= 0 )
352 msg_Err( p_stream, "invalid port" );
357 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
360 /* Normally, we should let the default hop limit up to the core,
361 * but we have to know it to build our SDP properly, which is why
362 * we ask the core. FIXME: broken when neither sout-rtp-ttl nor
364 val.i_int = config_getInt( p_stream, "ttl" );
366 if( p_sys->i_ttl > 255 ) p_sys->i_ttl = 255;
367 /* must not exceed 999 once formatted */
369 if( p_sys->i_ttl < 0 )
371 msg_Err( p_stream, "illegal TTL %d", val.i_int );
375 p_sys->i_ttl = val.i_int;
377 p_sys->i_payload_type = 96;
382 p_sys->psz_sdp = NULL;
384 p_sys->i_sdp_id = mdate();
385 p_sys->i_sdp_version = 1;
386 p_sys->psz_sdp = NULL;
388 p_sys->b_export_sap = VLC_FALSE;
389 p_sys->b_export_sdp_file = VLC_FALSE;
390 p_sys->p_session = NULL;
392 p_sys->p_httpd_host = NULL;
393 p_sys->p_httpd_file = NULL;
394 p_sys->p_rtsp_host = NULL;
395 p_sys->p_rtsp_url = NULL;
396 p_sys->psz_rtsp_control = NULL;
397 p_sys->psz_rtsp_path = NULL;
399 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
400 vlc_mutex_init( p_stream, &p_sys->lock_es );
402 p_stream->pf_add = Add;
403 p_stream->pf_del = Del;
404 p_stream->pf_send = Send;
406 p_stream->p_sys = p_sys;
408 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
409 if( *val.psz_string )
411 sout_access_out_t *p_grab;
412 char *psz_rtpmap, url[NI_MAXHOST + 8], access[17], psz_ttl[5], ipv;
414 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
416 msg_Err( p_stream, "rtp needs a destination when muxing" );
421 /* Check muxer type */
422 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
424 psz_rtpmap = "MP2P/90000";
426 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
428 psz_rtpmap = "MP2T/90000";
429 p_sys->i_payload_type = 33;
433 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
438 /* create the access out */
439 if( p_sys->i_ttl > 0 )
441 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
445 sprintf( access, "udp{raw}" );
448 /* IPv6 needs brackets if not already present */
449 snprintf( url, sizeof( url ),
450 ( ( p_sys->psz_destination[0] != '[' )
451 && ( strchr( p_sys->psz_destination, ':' ) != NULL ) )
452 ? "[%s]:%d" : "%s:%d", p_sys->psz_destination,
454 url[sizeof( url ) - 1] = '\0';
455 /* FIXME: we should check that url is a numerical address, otherwise
456 * the SDP will be quite broken (regardless of the IP protocol version)
458 ipv = ( strchr( p_sys->psz_destination, ':' ) != NULL ) ? '6' : '4';
460 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
462 msg_Err( p_stream, "cannot create the access out for %s://%s",
467 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
468 if( p_sys->i_mtu <= 16 + MTU_REDUCE )
470 /* better than nothing */
473 p_sys->i_mtu -= MTU_REDUCE;
475 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
476 p_grab = p_sys->p_grab =
477 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
478 p_grab->p_module = NULL;
479 p_grab->p_sout = p_sout;
480 p_grab->psz_access = strdup( "grab" );
481 p_grab->p_cfg = NULL;
482 p_grab->psz_name = strdup( "" );
483 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
484 p_grab->pf_seek = NULL;
485 p_grab->pf_write = AccessOutGrabberWrite;
488 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
490 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
491 sout_AccessOutDelete( p_sys->p_grab );
492 sout_AccessOutDelete( p_sys->p_access );
497 /* create the SDP for a muxed stream (only once) */
498 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
499 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
500 o= - should be local username (no spaces allowed)
501 o= time should be hashed with some other value to garantue uniqueness
502 o= we need IP6 support?
503 o= don't use the localhost address. use fully qualified domain name or IP4 address
504 p= international phone number (pass via vars?)
506 a= recvonly (missing)
507 a= type:broadcast (missing)
508 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
509 a= x-plgroup: (missing)
510 RTP packets need to get the correct src IP address */
511 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, p_sys->psz_destination ) )
513 snprintf( psz_ttl, sizeof( psz_ttl ), "/%d", p_sys->i_ttl );
514 psz_ttl[sizeof( psz_ttl ) - 1] = '\0';
521 asprintf( &p_sys->psz_sdp,
523 /* FIXME: source address not known :( */
524 "o=- "I64Fd" %d IN IP%c %s\r\n"
529 "t=0 0\r\n" /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
530 "a=tool:"PACKAGE_STRING"\r\n"
532 "m=video %d RTP/AVP %d\r\n"
533 "a=rtpmap:%d %s\r\n",
534 p_sys->i_sdp_id, p_sys->i_sdp_version,
535 ipv, ipv == '6' ? "::1" : "127.0.0.1" /* FIXME */,
536 p_sys->psz_session_name,
537 p_sys->psz_session_description,
538 p_sys->psz_session_url,
539 p_sys->psz_session_email,
540 ipv, p_sys->psz_destination, psz_ttl,
541 p_sys->i_port, p_sys->i_payload_type,
542 p_sys->i_payload_type, psz_rtpmap );
543 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
545 /* create the rtp context */
546 p_sys->ssrc[0] = rand()&0xff;
547 p_sys->ssrc[1] = rand()&0xff;
548 p_sys->ssrc[2] = rand()&0xff;
549 p_sys->ssrc[3] = rand()&0xff;
550 p_sys->i_sequence = rand()&0xffff;
551 p_sys->i_timestamp_start = rand()&0xffffffff;
552 p_sys->packet = NULL;
557 p_sys->p_access = NULL;
558 p_sys->p_grab = NULL;
560 free( val.psz_string );
563 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
564 if( *val.psz_string )
568 SDPHandleUrl( p_stream, val.psz_string );
570 for( p_cfg = p_stream->p_cfg; p_cfg != NULL; p_cfg = p_cfg->p_next )
572 if( !strcmp( p_cfg->psz_name, "sdp" ) )
574 if( p_cfg->psz_value == NULL || *p_cfg->psz_value == '\0' )
577 if( !strcmp( p_cfg->psz_value, val.psz_string ) ) /* needed both :sout-rtp-sdp= and rtp{sdp=} can be used */
580 SDPHandleUrl( p_stream, p_cfg->psz_value );
584 free( val.psz_string );
586 /* update p_sout->i_out_pace_nocontrol */
587 p_stream->p_sout->i_out_pace_nocontrol++;
592 /*****************************************************************************
594 *****************************************************************************/
595 static void Close( vlc_object_t * p_this )
597 sout_stream_t *p_stream = (sout_stream_t*)p_this;
598 sout_stream_sys_t *p_sys = p_stream->p_sys;
600 /* update p_sout->i_out_pace_nocontrol */
601 p_stream->p_sout->i_out_pace_nocontrol--;
605 sout_MuxDelete( p_sys->p_mux );
606 sout_AccessOutDelete( p_sys->p_access );
607 sout_AccessOutDelete( p_sys->p_grab );
610 block_Release( p_sys->packet );
612 if( p_sys->b_export_sap )
615 SapSetup( p_stream );
619 while( p_sys->i_rtsp > 0 )
621 RtspClientDel( p_stream, p_sys->rtsp[0] );
624 vlc_mutex_destroy( &p_sys->lock_sdp );
626 if( p_sys->p_httpd_file )
628 httpd_FileDelete( p_sys->p_httpd_file );
630 if( p_sys->p_httpd_host )
632 httpd_HostDelete( p_sys->p_httpd_host );
634 if( p_sys->p_rtsp_url )
636 httpd_UrlDelete( p_sys->p_rtsp_url );
638 if( p_sys->p_rtsp_host )
640 httpd_HostDelete( p_sys->p_rtsp_host );
643 /* why? is this disabled? */
644 if( p_sys->psz_session_name )
646 free( p_sys->psz_session_name );
647 p_sys->psz_session_name = NULL;
649 if( p_sys->psz_session_description )
651 free( p_sys->psz_session_description );
652 p_sys->psz_session_description = NULL;
654 if( p_sys->psz_session_url )
656 free( p_sys->psz_session_url );
657 p_sys->psz_session_url = NULL;
659 if( p_sys->psz_session_email )
661 free( p_sys->psz_session_email );
662 p_sys->psz_session_email = NULL;
667 free( p_sys->psz_sdp );
672 /*****************************************************************************
674 *****************************************************************************/
675 static void SDPHandleUrl( sout_stream_t *p_stream, char *psz_url )
677 sout_stream_sys_t *p_sys = p_stream->p_sys;
680 vlc_UrlParse( &url, psz_url, 0 );
681 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
683 if( p_sys->p_httpd_file )
685 msg_Err( p_stream, "You can used sdp=http:// only once" );
689 if( HttpSetup( p_stream, &url ) )
691 msg_Err( p_stream, "cannot export sdp as http" );
694 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
696 if( p_sys->p_rtsp_url )
698 msg_Err( p_stream, "You can used sdp=rtsp:// only once" );
702 /* FIXME test if destination is multicast or no destination at all FIXME */
703 if( RtspSetup( p_stream, &url ) )
705 msg_Err( p_stream, "cannot export sdp as rtsp" );
708 else if( ( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) ) ||
709 ( url.psz_host && !strcasecmp( url.psz_host, "sap" ) ) )
711 p_sys->b_export_sap = VLC_TRUE;
712 SapSetup( p_stream );
714 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "file" ) )
716 if( p_sys->b_export_sdp_file )
718 msg_Err( p_stream, "You can used sdp=file:// only once" );
721 p_sys->b_export_sdp_file = VLC_TRUE;
722 psz_url = &psz_url[5];
723 if( psz_url[0] == '/' && psz_url[1] == '/' )
725 p_sys->psz_sdp_file = strdup( psz_url );
729 msg_Warn( p_stream, "unknown protocol for SDP (%s)",
732 vlc_UrlClean( &url );
735 /*****************************************************************************
737 *****************************************************************************/
738 /* FIXME http://www.faqs.org/rfcs/rfc2327.html
739 All text fields should be UTF-8 encoded. Use global a:charset to announce this.
740 o= - should be local username (no spaces allowed)
741 o= time should be hashed with some other value to garantue uniqueness
742 o= we need IP6 support?
743 o= don't use the localhost address. use fully qualified domain name or IP4 address
744 p= international phone number (pass via vars?)
746 a= recvonly (missing)
747 a= type:broadcast (missing)
748 a= charset: (normally charset should be UTF-8, this can be used to override s= and i=)
749 a= x-plgroup: (missing)
750 RTP packets need to get the correct src IP address */
751 static char *SDPGenerate( const sout_stream_t *p_stream,
752 const char *psz_destination, vlc_bool_t b_rtsp )
754 sout_stream_sys_t *p_sys = p_stream->p_sys;
755 sout_instance_t *p_sout = p_stream->p_sout;
757 char *psz_sdp, *p, ipv;
760 /* FIXME: breaks IP version check on unknown destination */
761 if( psz_destination == NULL )
762 psz_destination = "0.0.0.0";
764 i_size = sizeof( "v=0\r\n" ) +
765 sizeof( "o=- * * IN IP4 127.0.0.1\r\n" ) + 10 + 10 +
766 sizeof( "s=*\r\n" ) + strlen( p_sys->psz_session_name ) +
767 sizeof( "i=*\r\n" ) + strlen( p_sys->psz_session_description ) +
768 sizeof( "u=*\r\n" ) + strlen( p_sys->psz_session_url ) +
769 sizeof( "e=*\r\n" ) + strlen( p_sys->psz_session_email ) +
770 sizeof( "t=0 0\r\n" ) + /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
771 sizeof( "a=tool:"PACKAGE_STRING"\r\n" ) +
772 sizeof( "c=IN IP4 */*\r\n" ) + 20 + 10 +
773 strlen( psz_destination ) ;
774 for( i = 0; i < p_sys->i_es; i++ )
776 sout_stream_id_t *id = p_sys->es[i];
778 i_size += strlen( "m=**d*o * RTP/AVP *\r\n" ) + 10 + 10;
781 i_size += strlen( "a=rtpmap:* *\r\n" ) + strlen( id->psz_rtpmap )+10;
785 i_size += strlen( "a=fmtp:* *\r\n" ) + strlen( id->psz_fmtp ) + 10;
789 i_size += strlen( "a=control:*/trackid=*\r\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
793 ipv = ( strchr( psz_destination, ':' ) != NULL ) ? '6' : '4';
795 p = psz_sdp = malloc( i_size );
796 p += sprintf( p, "v=0\r\n" );
797 p += sprintf( p, "o=- "I64Fd" %d IN IP%c %s\r\n",
798 p_sys->i_sdp_id, p_sys->i_sdp_version,
799 ipv, ipv == '6' ? "::" : "127.0.0.1" );
800 if( *p_sys->psz_session_name )
801 p += sprintf( p, "s=%s\r\n", p_sys->psz_session_name );
802 if( *p_sys->psz_session_description )
803 p += sprintf( p, "i=%s\r\n", p_sys->psz_session_description );
804 if( *p_sys->psz_session_url )
805 p += sprintf( p, "u=%s\r\n", p_sys->psz_session_url );
806 if( *p_sys->psz_session_email )
807 p += sprintf( p, "e=%s\r\n", p_sys->psz_session_email );
809 p += sprintf( p, "t=0 0\r\n" ); /* permanent stream */ /* when scheduled from vlm, we should set this info correctly */
810 p += sprintf( p, "a=tool:"PACKAGE_STRING"\r\n" );
812 p += sprintf( p, "c=IN IP%c %s", ipv, psz_destination );
814 if( net_AddressIsMulticast( (vlc_object_t *)p_stream, psz_destination ) )
816 /* Add the ttl if it is a multicast address */
817 p += sprintf( p, "/%d\r\n", p_sys->i_ttl );
821 p += sprintf( p, "\r\n" );
824 for( i = 0; i < p_sys->i_es; i++ )
826 sout_stream_id_t *id = p_sys->es[i];
828 if( id->i_cat == AUDIO_ES )
830 p += sprintf( p, "m=audio %d RTP/AVP %d\r\n",
831 id->i_port, id->i_payload_type );
833 else if( id->i_cat == VIDEO_ES )
835 p += sprintf( p, "m=video %d RTP/AVP %d\r\n",
836 id->i_port, id->i_payload_type );
844 p += sprintf( p, "a=rtpmap:%d %s\r\n", id->i_payload_type,
849 p += sprintf( p, "a=fmtp:%d %s\r\n", id->i_payload_type,
854 p += sprintf( p, "a=control:%s/trackid=%d\r\n", p_sys->psz_rtsp_control, i );
861 /*****************************************************************************
863 *****************************************************************************/
864 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
865 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
866 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
867 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
868 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
869 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
870 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
871 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
872 static int rtp_packetize_amr ( sout_stream_t *, sout_stream_id_t *, block_t * );
874 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
876 static const char hex[16] = "0123456789abcdef";
879 for( i = 0; i < i_data; i++ )
881 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
882 s[2*i+1] = hex[(p_data[i] )&0xf];
887 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
889 sout_instance_t *p_sout = p_stream->p_sout;
890 sout_stream_sys_t *p_sys = p_stream->p_sys;
891 sout_stream_id_t *id;
892 sout_access_out_t *p_access = NULL;
896 if( p_sys->p_mux != NULL )
898 sout_input_t *p_input = NULL;
899 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
901 msg_Err( p_stream, "cannot add this stream to the muxer" );
905 id = malloc( sizeof( sout_stream_id_t ) );
906 memset( id, 0, sizeof( sout_stream_id_t ) );
908 id->p_input = p_input;
909 id->pf_packetize= NULL;
910 id->p_rtsp_url = NULL;
916 /* Choose the port */
918 if( p_fmt->i_cat == AUDIO_ES && p_sys->i_port_audio > 0 )
920 i_port = p_sys->i_port_audio;
921 p_sys->i_port_audio = 0;
923 else if( p_fmt->i_cat == VIDEO_ES && p_sys->i_port_video > 0 )
925 i_port = p_sys->i_port_video;
926 p_sys->i_port_video = 0;
930 if( p_sys->i_port != p_sys->i_port_audio && p_sys->i_port != p_sys->i_port_video )
932 i_port = p_sys->i_port;
939 if( p_sys->psz_destination )
942 char url[NI_MAXHOST + 8];
944 /* first try to create the access out */
945 if( p_sys->i_ttl > 0 )
947 snprintf( access, sizeof( access ), "udp{raw,ttl=%d}",
949 access[sizeof( access ) - 1] = '\0';
952 strcpy( access, "udp{raw}" );
954 snprintf( url, sizeof( url ), (( p_sys->psz_destination[0] != '[' ) &&
955 strchr( p_sys->psz_destination, ':' )) ? "[%s]:%d" : "%s:%d",
956 p_sys->psz_destination, i_port );
957 url[sizeof( url ) - 1] = '\0';
959 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
961 msg_Err( p_stream, "cannot create the access out for %s://%s",
965 msg_Dbg( p_stream, "access out %s:%s", access, url );
968 /* not create the rtp specific stuff */
969 id = malloc( sizeof( sout_stream_id_t ) );
970 memset( id, 0, sizeof( sout_stream_id_t ) );
971 id->p_stream = p_stream;
972 id->p_access = p_access;
974 id->psz_rtpmap = NULL;
976 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
978 id->p_rtsp_url = NULL;
979 vlc_mutex_init( p_stream, &id->lock_rtsp );
980 id->i_rtsp_access = 0;
981 id->rtsp_access = NULL;
983 switch( p_fmt->i_codec )
985 case VLC_FOURCC( 's', '1', '6', 'b' ):
986 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
988 id->i_payload_type = 11;
990 else if( p_fmt->audio.i_channels == 2 &&
991 p_fmt->audio.i_rate == 44100 )
993 id->i_payload_type = 10;
997 id->i_payload_type = p_sys->i_payload_type++;
999 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
1000 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
1001 p_fmt->audio.i_channels );
1002 id->i_clock_rate = p_fmt->audio.i_rate;
1003 id->pf_packetize = rtp_packetize_l16;
1005 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
1006 id->i_payload_type = p_sys->i_payload_type++;
1007 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
1008 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
1009 p_fmt->audio.i_channels );
1010 id->i_clock_rate = p_fmt->audio.i_rate;
1011 id->pf_packetize = rtp_packetize_l8;
1013 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1014 id->i_payload_type = 14;
1015 id->i_clock_rate = 90000;
1016 id->psz_rtpmap = strdup( "MPA/90000" );
1017 id->pf_packetize = rtp_packetize_mpa;
1019 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
1020 id->i_payload_type = 32;
1021 id->i_clock_rate = 90000;
1022 id->psz_rtpmap = strdup( "MPV/90000" );
1023 id->pf_packetize = rtp_packetize_mpv;
1025 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1026 id->i_payload_type = p_sys->i_payload_type++;
1027 id->i_clock_rate = 90000;
1028 id->psz_rtpmap = strdup( "ac3/90000" );
1029 id->pf_packetize = rtp_packetize_ac3;
1031 case VLC_FOURCC( 'H', '2', '6', '3' ):
1032 id->i_payload_type = p_sys->i_payload_type++;
1033 id->i_clock_rate = 90000;
1034 id->psz_rtpmap = strdup( "H263-1998/90000" );
1035 id->pf_packetize = rtp_packetize_h263;
1038 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
1040 char hexa[2*p_fmt->i_extra +1];
1042 id->i_payload_type = p_sys->i_payload_type++;
1043 id->i_clock_rate = 90000;
1044 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
1045 id->pf_packetize = rtp_packetize_split;
1046 if( p_fmt->i_extra > 0 )
1048 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
1049 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1050 sprintf( id->psz_fmtp,
1051 "profile-level-id=3; config=%s;", hexa );
1055 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
1057 char hexa[2*p_fmt->i_extra +1];
1059 id->i_payload_type = p_sys->i_payload_type++;
1060 id->i_clock_rate = p_fmt->audio.i_rate;
1061 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
1062 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
1063 id->pf_packetize = rtp_packetize_mp4a;
1064 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
1065 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
1066 sprintf( id->psz_fmtp,
1067 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
1068 "config=%s; SizeLength=13;IndexLength=3; "
1069 "IndexDeltaLength=3; Profile=1;", hexa );
1072 case VLC_FOURCC( 's', 'a', 'm', 'r' ):
1073 id->i_payload_type = p_sys->i_payload_type++;
1074 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1075 "AMR/8000/2" : "AMR/8000" );
1076 id->psz_fmtp = strdup( "octet-align=1" );
1077 id->i_clock_rate = p_fmt->audio.i_rate;
1078 id->pf_packetize = rtp_packetize_amr;
1080 case VLC_FOURCC( 's', 'a', 'w', 'b' ):
1081 id->i_payload_type = p_sys->i_payload_type++;
1082 id->psz_rtpmap = strdup( p_fmt->audio.i_channels == 2 ?
1083 "AMR-WB/16000/2" : "AMR-WB/16000" );
1084 id->psz_fmtp = strdup( "octet-align=1" );
1085 id->i_clock_rate = p_fmt->audio.i_rate;
1086 id->pf_packetize = rtp_packetize_amr;
1090 msg_Err( p_stream, "cannot add this stream (unsupported "
1091 "codec:%4.4s)", (char*)&p_fmt->i_codec );
1094 sout_AccessOutDelete( p_access );
1099 id->i_cat = p_fmt->i_cat;
1101 id->ssrc[0] = rand()&0xff;
1102 id->ssrc[1] = rand()&0xff;
1103 id->ssrc[2] = rand()&0xff;
1104 id->ssrc[3] = rand()&0xff;
1105 id->i_sequence = rand()&0xffff;
1106 id->i_timestamp_start = rand()&0xffffffff;
1108 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
1109 if( id->i_mtu <= 16 + MTU_REDUCE )
1111 /* better than nothing */
1114 id->i_mtu -= MTU_REDUCE;
1115 msg_Dbg( p_stream, "maximum RTP packet size: %d bytes", id->i_mtu );
1117 if( p_sys->p_rtsp_url )
1119 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
1121 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
1122 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
1123 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL, NULL );
1125 if( id->p_rtsp_url )
1127 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
1128 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1129 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1134 /* Update p_sys context */
1135 vlc_mutex_lock( &p_sys->lock_es );
1136 TAB_APPEND( p_sys->i_es, p_sys->es, id );
1137 vlc_mutex_unlock( &p_sys->lock_es );
1139 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
1141 vlc_mutex_lock( &p_sys->lock_sdp );
1142 free( p_sys->psz_sdp );
1143 p_sys->psz_sdp = psz_sdp;
1144 vlc_mutex_unlock( &p_sys->lock_sdp );
1146 p_sys->i_sdp_version++;
1148 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
1150 /* Update SDP (sap/file) */
1151 if( p_sys->b_export_sap ) SapSetup( p_stream );
1152 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1157 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
1159 sout_stream_sys_t *p_sys = p_stream->p_sys;
1161 vlc_mutex_lock( &p_sys->lock_es );
1162 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
1163 vlc_mutex_unlock( &p_sys->lock_es );
1166 if( id->i_port > 0 )
1168 if( id->i_cat == AUDIO_ES && p_sys->i_port_audio == 0 )
1169 p_sys->i_port_audio = id->i_port;
1170 else if( id->i_cat == VIDEO_ES && p_sys->i_port_video == 0 )
1171 p_sys->i_port_video = id->i_port;
1176 if( id->psz_rtpmap )
1178 free( id->psz_rtpmap );
1182 free( id->psz_fmtp );
1184 if( id->psz_destination )
1185 free( id->psz_destination );
1186 sout_AccessOutDelete( id->p_access );
1188 else if( id->p_input )
1190 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
1192 if( id->p_rtsp_url )
1194 httpd_UrlDelete( id->p_rtsp_url );
1196 vlc_mutex_destroy( &id->lock_rtsp );
1197 if( id->rtsp_access ) free( id->rtsp_access );
1199 /* Update SDP (sap/file) */
1200 if( p_sys->b_export_sap && !p_sys->p_mux ) SapSetup( p_stream );
1201 if( p_sys->b_export_sdp_file ) FileSetup( p_stream );
1207 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
1212 if( p_stream->p_sys->p_mux )
1214 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
1220 p_next = p_buffer->p_next;
1221 if( id->pf_packetize( p_stream, id, p_buffer ) )
1225 block_Release( p_buffer );
1233 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
1236 sout_stream_sys_t *p_sys = p_stream->p_sys;
1238 int64_t i_dts = p_buffer->i_dts;
1239 uint32_t i_timestamp = i_dts * 9 / 100;
1241 uint8_t *p_data = p_buffer->p_buffer;
1242 unsigned int i_data = p_buffer->i_buffer;
1243 unsigned int i_max = p_sys->i_mtu - 12;
1245 unsigned i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
1249 unsigned int i_size;
1251 /* output complete packet */
1252 if( p_sys->packet &&
1253 p_sys->packet->i_buffer + i_data > i_max )
1255 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
1256 p_sys->packet = NULL;
1259 if( p_sys->packet == NULL )
1261 /* allocate a new packet */
1262 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
1263 p_sys->packet->p_buffer[ 0] = 0x80;
1264 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
1265 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
1266 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
1267 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
1268 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
1269 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
1270 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
1271 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
1272 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
1273 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
1274 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
1275 p_sys->packet->i_buffer = 12;
1277 p_sys->packet->i_dts = i_dts;
1278 p_sys->packet->i_length = p_buffer->i_length / i_packet;
1279 i_dts += p_sys->packet->i_length;
1281 p_sys->i_sequence++;
1284 i_size = __MIN( i_data,
1285 (unsigned)(p_sys->i_mtu - p_sys->packet->i_buffer) );
1287 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
1290 p_sys->packet->i_buffer += i_size;
1298 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
1301 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
1303 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
1309 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
1311 p_next = p_buffer->p_next;
1312 block_Release( p_buffer );
1319 /****************************************************************************
1321 ****************************************************************************/
1322 static int SapSetup( sout_stream_t *p_stream )
1324 sout_stream_sys_t *p_sys = p_stream->p_sys;
1325 sout_instance_t *p_sout = p_stream->p_sout;
1326 announce_method_t *p_method = sout_AnnounceMethodCreate( METHOD_TYPE_SAP );
1328 /* Remove the previous session */
1329 if( p_sys->p_session != NULL)
1331 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1332 sout_AnnounceSessionDestroy( p_sys->p_session );
1333 p_sys->p_session = NULL;
1336 if( ( p_sys->i_es > 0 || p_sys->p_mux ) && p_sys->psz_sdp && *p_sys->psz_sdp )
1338 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1339 p_sys->psz_destination,
1347 /****************************************************************************
1349 ****************************************************************************/
1350 static int FileSetup( sout_stream_t *p_stream )
1352 sout_stream_sys_t *p_sys = p_stream->p_sys;
1355 if( ( f = fopen( p_sys->psz_sdp_file, "wt" ) ) == NULL )
1357 msg_Err( p_stream, "cannot open file '%s' (%s)",
1358 p_sys->psz_sdp_file, strerror(errno) );
1359 return VLC_EGENERIC;
1362 fprintf( f, "%s", p_sys->psz_sdp );
1368 /****************************************************************************
1370 ****************************************************************************/
1371 static int HttpCallback( httpd_file_sys_t *p_args,
1372 httpd_file_t *, uint8_t *p_request,
1373 uint8_t **pp_data, int *pi_data );
1375 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1377 sout_stream_sys_t *p_sys = p_stream->p_sys;
1379 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 80 );
1380 if( p_sys->p_httpd_host )
1382 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1383 url->psz_path ? url->psz_path : "/",
1386 HttpCallback, (void*)p_sys );
1388 if( p_sys->p_httpd_file == NULL )
1390 return VLC_EGENERIC;
1395 static int HttpCallback( httpd_file_sys_t *p_args,
1396 httpd_file_t *f, uint8_t *p_request,
1397 uint8_t **pp_data, int *pi_data )
1399 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1401 vlc_mutex_lock( &p_sys->lock_sdp );
1402 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1404 *pi_data = strlen( p_sys->psz_sdp );
1405 *pp_data = malloc( *pi_data );
1406 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1413 vlc_mutex_unlock( &p_sys->lock_sdp );
1418 /****************************************************************************
1420 ****************************************************************************/
1421 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1423 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1425 rtsp->psz_session = psz_session;
1427 rtsp->b_playing = VLC_FALSE;
1431 rtsp->access = NULL;
1433 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1437 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1440 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1442 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1444 return p_stream->p_sys->rtsp[i];
1450 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1453 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1455 for( i = 0; i < rtsp->i_access; i++ )
1457 sout_AccessOutDelete( rtsp->access[i] );
1459 if( rtsp->id ) free( rtsp->id );
1460 if( rtsp->access ) free( rtsp->access );
1462 free( rtsp->psz_session );
1466 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1468 sout_stream_sys_t *p_sys = p_stream->p_sys;
1470 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1472 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1473 if( p_sys->p_rtsp_host == NULL )
1475 return VLC_EGENERIC;
1478 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1479 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1480 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1481 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1483 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL, NULL );
1484 if( p_sys->p_rtsp_url == 0 )
1486 return VLC_EGENERIC;
1488 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1489 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1490 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1491 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1496 static int RtspCallback( httpd_callback_sys_t *p_args,
1498 httpd_message_t *answer, httpd_message_t *query )
1500 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1501 sout_stream_sys_t *p_sys = p_stream->p_sys;
1502 char *psz_destination = p_sys->psz_destination;
1503 char *psz_session = NULL;
1505 if( answer == NULL || query == NULL )
1509 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1511 answer->i_proto = HTTPD_PROTO_RTSP;
1512 answer->i_version= query->i_version;
1513 answer->i_type = HTTPD_MSG_ANSWER;
1515 switch( query->i_type )
1517 case HTTPD_MSG_DESCRIBE:
1519 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1521 answer->i_status = 200;
1522 answer->psz_status = strdup( "OK" );
1523 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1525 answer->p_body = (uint8_t *)psz_sdp;
1526 answer->i_body = strlen( psz_sdp );
1530 case HTTPD_MSG_PLAY:
1532 rtsp_client_t *rtsp;
1533 /* for now only multicast so easy */
1534 answer->i_status = 200;
1535 answer->psz_status = strdup( "OK" );
1537 answer->p_body = NULL;
1539 psz_session = httpd_MsgGet( query, "Session" );
1540 rtsp = RtspClientGet( p_stream, psz_session );
1541 if( rtsp && !rtsp->b_playing )
1545 rtsp->b_playing = VLC_TRUE;
1547 vlc_mutex_lock( &p_sys->lock_es );
1548 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1550 sout_stream_id_t *id = rtsp->id[i_id];
1553 for( i = 0; i < p_sys->i_es; i++ )
1555 if( id == p_sys->es[i] )
1558 if( i >= p_sys->i_es ) continue;
1560 vlc_mutex_lock( &id->lock_rtsp );
1561 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1562 vlc_mutex_unlock( &id->lock_rtsp );
1564 vlc_mutex_unlock( &p_sys->lock_es );
1568 case HTTPD_MSG_PAUSE:
1570 return VLC_EGENERIC;
1571 case HTTPD_MSG_TEARDOWN:
1573 rtsp_client_t *rtsp;
1575 /* for now only multicast so easy again */
1576 answer->i_status = 200;
1577 answer->psz_status = strdup( "OK" );
1579 answer->p_body = NULL;
1581 psz_session = httpd_MsgGet( query, "Session" );
1582 rtsp = RtspClientGet( p_stream, psz_session );
1587 vlc_mutex_lock( &p_sys->lock_es );
1588 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1590 sout_stream_id_t *id = rtsp->id[i_id];
1593 for( i = 0; i < p_sys->i_es; i++ )
1595 if( id == p_sys->es[i] )
1598 if( i >= p_sys->i_es ) continue;
1600 vlc_mutex_lock( &id->lock_rtsp );
1601 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1602 vlc_mutex_unlock( &id->lock_rtsp );
1604 vlc_mutex_unlock( &p_sys->lock_es );
1606 RtspClientDel( p_stream, rtsp );
1612 return VLC_EGENERIC;
1614 httpd_MsgAdd( answer, "Server", "VLC Server" );
1615 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1616 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1617 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1621 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1626 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1628 httpd_message_t *answer, httpd_message_t *query )
1630 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1631 sout_stream_t *p_stream = id->p_stream;
1632 sout_instance_t *p_sout = p_stream->p_sout;
1633 sout_stream_sys_t *p_sys = p_stream->p_sys;
1634 char *psz_session = NULL;
1636 if( answer == NULL || query == NULL )
1640 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1642 answer->i_proto = HTTPD_PROTO_RTSP;
1643 answer->i_version= query->i_version;
1644 answer->i_type = HTTPD_MSG_ANSWER;
1646 switch( query->i_type )
1648 case HTTPD_MSG_SETUP:
1650 char *psz_transport = httpd_MsgGet( query, "Transport" );
1652 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1654 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1656 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1657 answer->i_status = 200;
1658 answer->psz_status = strdup( "OK" );
1660 answer->p_body = NULL;
1661 psz_session = httpd_MsgGet( query, "Session" );
1662 if( *psz_session == 0 )
1664 psz_session = malloc( 100 );
1665 sprintf( psz_session, "%d", rand() );
1667 httpd_MsgAdd( answer, "Transport",
1668 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1669 id->psz_destination, id->i_port,id->i_port+1,
1672 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1674 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1675 char ip[NI_MAXNUMERICHOST], psz_access[17], psz_url[NI_MAXNUMERICHOST + 8];
1677 sout_access_out_t *p_access;
1679 rtsp_client_t *rtsp = NULL;
1681 if( httpd_ClientIP( cl, ip ) == NULL )
1683 answer->i_status = 500;
1684 answer->psz_status = strdup( "Internal server error" );
1686 answer->p_body = NULL;
1690 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1693 psz_session = httpd_MsgGet( query, "Session" );
1694 if( *psz_session == 0 )
1696 psz_session = malloc( 100 );
1697 sprintf( psz_session, "%d", rand() );
1699 rtsp = RtspClientNew( p_stream, psz_session );
1703 rtsp = RtspClientGet( p_stream, psz_session );
1706 answer->i_status = 454;
1707 answer->psz_status = strdup( "Unknown session id" );
1709 answer->p_body = NULL;
1714 /* first try to create the access out */
1715 if( p_sys->i_ttl > 0 )
1716 snprintf( psz_access, sizeof( psz_access ),
1717 "udp{raw,ttl=%d}", p_sys->i_ttl );
1719 strncpy( psz_access, "udp{raw}", sizeof( psz_access ) );
1720 psz_access[sizeof( psz_access ) - 1] = '\0';
1722 snprintf( psz_url, sizeof( psz_url ),
1723 ( strchr( ip, ':' ) != NULL ) ? "[%s]:%d" : "%s:%d",
1726 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1728 msg_Err( p_stream, "cannot create the access out for %s://%s",
1729 psz_access, psz_url );
1730 answer->i_status = 500;
1731 answer->psz_status = strdup( "Internal server error" );
1733 answer->p_body = NULL;
1737 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1738 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1740 answer->i_status = 200;
1741 answer->psz_status = strdup( "OK" );
1743 answer->p_body = NULL;
1745 httpd_MsgAdd( answer, "Transport",
1746 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1748 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1750 answer->i_status = 461;
1751 answer->psz_status = strdup( "Unsupported Transport" );
1753 answer->p_body = NULL;
1759 return VLC_EGENERIC;
1761 httpd_MsgAdd( answer, "Server", "VLC Server" );
1762 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1763 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1764 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1768 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1773 /****************************************************************************
1775 ****************************************************************************/
1776 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1777 int b_marker, int64_t i_pts )
1779 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1781 out->p_buffer[0] = 0x80;
1782 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1783 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1784 out->p_buffer[3] = ( id->i_sequence )&0xff;
1785 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1786 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1787 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1788 out->p_buffer[7] = ( i_timestamp )&0xff;
1790 out->p_buffer[ 8] = id->ssrc[0];
1791 out->p_buffer[ 9] = id->ssrc[1];
1792 out->p_buffer[10] = id->ssrc[2];
1793 out->p_buffer[11] = id->ssrc[3];
1799 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1802 vlc_mutex_lock( &id->lock_rtsp );
1803 for( i = 0; i < id->i_rtsp_access; i++ )
1805 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1807 vlc_mutex_unlock( &id->lock_rtsp );
1811 sout_AccessOutWrite( id->p_access, out );
1815 block_Release( out );
1819 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1822 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1823 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1825 uint8_t *p_data = in->p_buffer;
1826 int i_data = in->i_buffer;
1829 for( i = 0; i < i_count; i++ )
1831 int i_payload = __MIN( i_max, i_data );
1832 block_t *out = block_New( p_stream, 16 + i_payload );
1834 /* rtp common header */
1835 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1837 out->p_buffer[12] = 0;
1838 out->p_buffer[13] = 0;
1839 /* fragment offset in the current frame */
1840 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1841 out->p_buffer[15] = ( (i*i_max) )&0xff;
1842 memcpy( &out->p_buffer[16], p_data, i_payload );
1844 out->i_buffer = 16 + i_payload;
1845 out->i_dts = in->i_dts + i * in->i_length / i_count;
1846 out->i_length = in->i_length / i_count;
1848 rtp_packetize_send( id, out );
1850 p_data += i_payload;
1851 i_data -= i_payload;
1858 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1861 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1862 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1864 uint8_t *p_data = in->p_buffer;
1865 int i_data = in->i_buffer;
1867 int b_sequence_start = 0;
1868 int i_temporal_ref = 0;
1869 int i_picture_coding_type = 0;
1870 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1871 int b_start_slice = 0;
1873 /* preparse this packet to get some info */
1874 if( in->i_buffer > 4 )
1876 uint8_t *p = p_data;
1877 int i_rest = in->i_buffer;
1881 while( i_rest > 4 &&
1882 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1896 /* sequence start code */
1897 b_sequence_start = 1;
1899 else if( *p == 0x00 && i_rest >= 4 )
1902 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1903 i_picture_coding_type = (p[2] >> 3)&0x07;
1905 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1906 i_picture_coding_type == 3 ) )
1908 i_ffv = (p[3] >> 2)&0x01;
1909 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1910 if( i_rest > 4 && i_picture_coding_type == 3 )
1912 i_fbv = (p[4]>>6)&0x01;
1913 i_bfc = (p[4]>>3)&0x07;
1917 else if( *p <= 0xaf )
1924 for( i = 0; i < i_count; i++ )
1926 int i_payload = __MIN( i_max, i_data );
1927 block_t *out = block_New( p_stream,
1929 uint32_t h = ( i_temporal_ref << 16 )|
1930 ( b_sequence_start << 13 )|
1931 ( b_start_slice << 12 )|
1932 ( i == i_count - 1 ? 1 << 11 : 0 )|
1933 ( i_picture_coding_type << 8 )|
1934 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1936 /* rtp common header */
1937 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1938 in->i_pts > 0 ? in->i_pts : in->i_dts );
1940 /* 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 */
1941 out->p_buffer[12] = ( h >> 24 )&0xff;
1942 out->p_buffer[13] = ( h >> 16 )&0xff;
1943 out->p_buffer[14] = ( h >> 8 )&0xff;
1944 out->p_buffer[15] = ( h )&0xff;
1946 memcpy( &out->p_buffer[16], p_data, i_payload );
1948 out->i_buffer = 16 + i_payload;
1949 out->i_dts = in->i_dts + i * in->i_length / i_count;
1950 out->i_length = in->i_length / i_count;
1952 rtp_packetize_send( id, out );
1954 p_data += i_payload;
1955 i_data -= i_payload;
1960 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1963 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1964 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1966 uint8_t *p_data = in->p_buffer;
1967 int i_data = in->i_buffer;
1970 for( i = 0; i < i_count; i++ )
1972 int i_payload = __MIN( i_max, i_data );
1973 block_t *out = block_New( p_stream, 14 + i_payload );
1975 /* rtp common header */
1976 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1978 out->p_buffer[12] = 1;
1980 out->p_buffer[13] = 0x00;
1982 memcpy( &out->p_buffer[14], p_data, i_payload );
1984 out->i_buffer = 14 + i_payload;
1985 out->i_dts = in->i_dts + i * in->i_length / i_count;
1986 out->i_length = in->i_length / i_count;
1988 rtp_packetize_send( id, out );
1990 p_data += i_payload;
1991 i_data -= i_payload;
1997 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
2000 int i_max = id->i_mtu - 12; /* payload max in one packet */
2001 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2003 uint8_t *p_data = in->p_buffer;
2004 int i_data = in->i_buffer;
2007 for( i = 0; i < i_count; i++ )
2009 int i_payload = __MIN( i_max, i_data );
2010 block_t *out = block_New( p_stream, 12 + i_payload );
2012 /* rtp common header */
2013 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2014 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2015 memcpy( &out->p_buffer[12], p_data, i_payload );
2017 out->i_buffer = 12 + i_payload;
2018 out->i_dts = in->i_dts + i * in->i_length / i_count;
2019 out->i_length = in->i_length / i_count;
2021 rtp_packetize_send( id, out );
2023 p_data += i_payload;
2024 i_data -= i_payload;
2030 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
2033 int i_max = id->i_mtu - 12; /* payload max in one packet */
2034 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2036 uint8_t *p_data = in->p_buffer;
2037 int i_data = in->i_buffer;
2042 int i_payload = (__MIN( i_max, i_data )/4)*4;
2043 block_t *out = block_New( p_stream, 12 + i_payload );
2045 /* rtp common header */
2046 rtp_packetize_common( id, out, 0,
2047 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2048 memcpy( &out->p_buffer[12], p_data, i_payload );
2050 out->i_buffer = 12 + i_payload;
2051 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2052 out->i_length = in->i_length / i_count;
2054 rtp_packetize_send( id, out );
2056 p_data += i_payload;
2057 i_data -= i_payload;
2064 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
2067 int i_max = id->i_mtu - 12; /* payload max in one packet */
2068 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2070 uint8_t *p_data = in->p_buffer;
2071 int i_data = in->i_buffer;
2076 int i_payload = (__MIN( i_max, i_data )/2)*2;
2077 block_t *out = block_New( p_stream, 12 + i_payload );
2079 /* rtp common header */
2080 rtp_packetize_common( id, out, 0,
2081 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2082 memcpy( &out->p_buffer[12], p_data, i_payload );
2084 out->i_buffer = 12 + i_payload;
2085 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
2086 out->i_length = in->i_length / i_count;
2088 rtp_packetize_send( id, out );
2090 p_data += i_payload;
2091 i_data -= i_payload;
2097 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
2100 int i_max = id->i_mtu - 16; /* payload max in one packet */
2101 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2103 uint8_t *p_data = in->p_buffer;
2104 int i_data = in->i_buffer;
2107 for( i = 0; i < i_count; i++ )
2109 int i_payload = __MIN( i_max, i_data );
2110 block_t *out = block_New( p_stream, 16 + i_payload );
2112 /* rtp common header */
2113 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2114 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2116 /* AU headers length (bits) */
2117 out->p_buffer[12] = 0;
2118 out->p_buffer[13] = 2*8;
2119 /* for each AU length 13 bits + idx 3bits, */
2120 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
2121 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
2123 memcpy( &out->p_buffer[16], p_data, i_payload );
2125 out->i_buffer = 16 + i_payload;
2126 out->i_dts = in->i_dts + i * in->i_length / i_count;
2127 out->i_length = in->i_length / i_count;
2129 rtp_packetize_send( id, out );
2131 p_data += i_payload;
2132 i_data -= i_payload;
2140 #define RTP_H263_HEADER_SIZE (2) // plen = 0
2141 #define RTP_H263_PAYLOAD_START (14) // plen = 0
2142 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
2145 uint8_t *p_data = in->p_buffer;
2146 int i_data = in->i_buffer;
2148 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
2151 int b_v_bit = 0; // no pesky error resilience
2152 int i_plen = 0; // normally plen=0 for PSC packet
2153 int i_pebit = 0; // because plen=0
2158 return VLC_EGENERIC;
2160 if( p_data[0] || p_data[1] )
2162 return VLC_EGENERIC;
2164 /* remove 2 leading 0 bytes */
2167 i_count = ( i_data + i_max - 1 ) / i_max;
2169 for( i = 0; i < i_count; i++ )
2171 int i_payload = __MIN( i_max, i_data );
2172 block_t *out = block_New( p_stream,
2173 RTP_H263_PAYLOAD_START + i_payload );
2174 b_p_bit = (i == 0) ? 1 : 0;
2175 h = ( b_p_bit << 10 )|
2180 /* rtp common header */
2181 //b_m_bit = 1; // always contains end of frame
2182 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
2183 in->i_pts > 0 ? in->i_pts : in->i_dts );
2186 out->p_buffer[12] = ( h >> 8 )&0xff;
2187 out->p_buffer[13] = ( h )&0xff;
2188 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
2190 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
2191 out->i_dts = in->i_dts + i * in->i_length / i_count;
2192 out->i_length = in->i_length / i_count;
2194 rtp_packetize_send( id, out );
2196 p_data += i_payload;
2197 i_data -= i_payload;
2203 static int rtp_packetize_amr( sout_stream_t *p_stream, sout_stream_id_t *id,
2206 int i_max = id->i_mtu - 14; /* payload max in one packet */
2207 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
2209 uint8_t *p_data = in->p_buffer;
2210 int i_data = in->i_buffer;
2213 /* Only supports octet-aligned mode */
2214 for( i = 0; i < i_count; i++ )
2216 int i_payload = __MIN( i_max, i_data );
2217 block_t *out = block_New( p_stream, 14 + i_payload );
2219 /* rtp common header */
2220 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
2221 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
2222 /* Payload header */
2223 out->p_buffer[12] = 0xF0; /* CMR */
2224 out->p_buffer[13] = 0x00; /* ToC */ /* FIXME: frame type */
2226 /* FIXME: are we fed multiple frames ? */
2227 memcpy( &out->p_buffer[14], p_data, i_payload );
2229 out->i_buffer = 14 + i_payload;
2230 out->i_dts = in->i_dts + i * in->i_length / i_count;
2231 out->i_length = in->i_length / i_count;
2233 rtp_packetize_send( id, out );
2235 p_data += i_payload;
2236 i_data -= i_payload;