1 /*****************************************************************************
2 * rtp.c: rtp stream output module
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
33 #include "vlc_httpd.h"
36 /*****************************************************************************
38 *****************************************************************************/
39 #define DST_TEXT N_("Destination")
40 #define DST_LONGTEXT N_( \
41 "Allows you to specify the output URL used for the streaming output." )
42 #define NAME_TEXT N_("Session name")
43 #define NAME_LONGTEXT N_( \
44 "Allows you to specify the session name used for the streaming output." )
45 #define SDP_TEXT N_("SDP")
46 #define SDP_LONGTEXT N_( \
47 "Allows you to specify the SDP used for the streaming output. " \
48 "You must use an url: http://location to access the SDP via HTTP, " \
49 "rtsp://location for RTSP access, and sap:// for the SDP to be " \
51 #define MUX_TEXT N_("Muxer")
52 #define MUX_LONGTEXT N_( \
53 "Allows you to specify the muxer used for the streaming output." )
54 #define PORT_TEXT N_("Port")
55 #define PORT_LONGTEXT N_( \
56 "Allows you to specify the base port used for the RTP streaming" )
57 #define TTL_TEXT N_("Time to live")
58 #define TTL_LONGTEXT N_( \
59 "Allows you to specify the time to live for the output stream." )
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
64 #define SOUT_CFG_PREFIX "sout-rtp-"
67 set_description( _("RTP stream output") );
68 set_capability( "sout stream", 0 );
69 add_shortcut( "rtp" );
71 add_string( SOUT_CFG_PREFIX "dst", "", NULL, DST_TEXT,
72 DST_LONGTEXT, VLC_TRUE );
73 add_string( SOUT_CFG_PREFIX "name", "", NULL, NAME_TEXT,
74 NAME_LONGTEXT, VLC_TRUE );
75 add_string( SOUT_CFG_PREFIX "sdp", "", NULL, SDP_TEXT,
76 SDP_LONGTEXT, VLC_TRUE );
77 add_string( SOUT_CFG_PREFIX "mux", "", NULL, MUX_TEXT,
78 MUX_LONGTEXT, VLC_TRUE );
80 add_integer( SOUT_CFG_PREFIX "port", 1234, NULL, PORT_TEXT,
81 PORT_LONGTEXT, VLC_TRUE );
82 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL, TTL_TEXT,
83 TTL_LONGTEXT, VLC_TRUE );
85 set_callbacks( Open, Close );
88 /*****************************************************************************
90 *****************************************************************************/
91 static const char *ppsz_sout_options[] = {
92 "dst", "name", "port", "sdp", "ttl", "mux", NULL
95 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
96 static int Del ( sout_stream_t *, sout_stream_id_t * );
97 static int Send( sout_stream_t *, sout_stream_id_t *,
100 /* For unicast/interleaved streaming */
104 int64_t i_last; /* for timeout */
106 /* is it in "play" state */
107 vlc_bool_t b_playing;
109 /* output (id-access) */
111 sout_stream_id_t **id;
113 sout_access_out_t **access;
116 struct sout_stream_sys_t
122 vlc_mutex_t lock_sdp;
124 char *psz_session_name;
127 vlc_bool_t b_export_sap;
128 session_descriptor_t *p_session;
130 httpd_host_t *p_httpd_host;
131 httpd_file_t *p_httpd_file;
133 httpd_host_t *p_rtsp_host;
134 httpd_url_t *p_rtsp_url;
135 char *psz_rtsp_control;
139 char *psz_destination;
143 /* when need to use a private one or when using muxer */
146 /* in case we do TS/PS over rtp */
148 sout_access_out_t *p_access;
150 sout_access_out_t *p_grab;
152 uint32_t i_timestamp_start;
159 sout_stream_id_t **es;
163 rtsp_client_t **rtsp;
166 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
169 struct sout_stream_id_t
171 sout_stream_t *p_stream;
173 uint8_t i_payload_type;
175 uint32_t i_timestamp_start;
182 char *psz_destination;
186 /* Packetizer specific fields */
187 pf_rtp_packetizer_t pf_packetize;
190 /* for sending the packets */
191 sout_access_out_t *p_access;
193 vlc_mutex_t lock_rtsp;
195 sout_access_out_t **rtsp_access;
198 sout_input_t *p_input;
200 /* RTSP url control */
201 httpd_url_t *p_rtsp_url;
204 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
206 static int SapSetup( sout_stream_t *p_stream );
207 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
208 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
210 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
211 httpd_message_t *, httpd_message_t * );
212 static int RtspCallbackId( httpd_callback_sys_t *, httpd_client_t *,
213 httpd_message_t *, httpd_message_t * );
216 static rtsp_client_t *RtspClientNew( sout_stream_t *, char *psz_session );
217 static rtsp_client_t *RtspClientGet( sout_stream_t *, char *psz_session );
218 static void RtspClientDel( sout_stream_t *, rtsp_client_t * );
220 /*****************************************************************************
222 *****************************************************************************/
223 static int Open( vlc_object_t *p_this )
225 sout_stream_t *p_stream = (sout_stream_t*)p_this;
226 sout_instance_t *p_sout = p_stream->p_sout;
227 sout_stream_sys_t *p_sys;
230 sout_ParseCfg( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg );
232 p_sys = malloc( sizeof( sout_stream_sys_t ) );
234 var_Get( p_stream, SOUT_CFG_PREFIX "dst", &val );
235 p_sys->psz_destination = *val.psz_string ? val.psz_string : NULL;
237 var_Get( p_stream, SOUT_CFG_PREFIX "name", &val );
238 p_sys->psz_session_name = *val.psz_string ? val.psz_string : NULL;
240 var_Get( p_stream, SOUT_CFG_PREFIX "port", &val );
241 p_sys->i_port = val.i_int;
244 if( !p_sys->psz_session_name )
246 if( p_sys->psz_destination )
247 p_sys->psz_session_name = strdup( p_sys->psz_destination );
249 p_sys->psz_session_name = strdup( "NONE" );
252 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' )
254 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
256 if( strncasecmp( val.psz_string, "rtsp", 4 ) )
258 msg_Err( p_stream, "missing destination and not in rtsp mode" );
262 p_sys->psz_destination = NULL;
263 free( val.psz_string );
265 else if( p_sys->i_port <= 0 )
267 msg_Err( p_stream, "invalid port" );
272 var_Get( p_stream, SOUT_CFG_PREFIX "ttl", &val );
273 p_sys->i_ttl = val.i_int;
275 p_sys->i_payload_type = 96;
280 p_sys->psz_sdp = NULL;
282 p_sys->i_sdp_id = mdate();
283 p_sys->i_sdp_version = 1;
284 p_sys->psz_sdp = NULL;
286 p_sys->b_export_sap = VLC_FALSE;
287 p_sys->p_session = NULL;
289 p_sys->p_httpd_host = NULL;
290 p_sys->p_httpd_file = NULL;
291 p_sys->p_rtsp_host = NULL;
292 p_sys->p_rtsp_url = NULL;
293 p_sys->psz_rtsp_control = NULL;
294 p_sys->psz_rtsp_path = NULL;
296 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
297 vlc_mutex_init( p_stream, &p_sys->lock_es );
299 p_stream->pf_add = Add;
300 p_stream->pf_del = Del;
301 p_stream->pf_send = Send;
303 p_stream->p_sys = p_sys;
305 var_Get( p_stream, SOUT_CFG_PREFIX "mux", &val );
306 if( *val.psz_string )
308 sout_access_out_t *p_grab;
312 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
314 /* Check muxer type */
315 if( !strncasecmp( val.psz_string, "ps", 2 ) || !strncasecmp( val.psz_string, "mpeg1", 5 ) )
317 psz_rtpmap = "MP2P/90000";
319 else if( !strncasecmp( val.psz_string, "ts", 2 ) )
321 psz_rtpmap = "MP2T/90000";
322 p_sys->i_payload_type = 33;
326 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
330 /* create the access out */
331 if( p_sys->i_ttl > 0 )
333 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
337 sprintf( access, "udp{raw}" );
339 sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
340 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
342 msg_Err( p_stream, "cannot create the access out for %s://%s",
347 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
348 if( p_sys->i_mtu <= 16 )
350 /* better than nothing */
354 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
355 p_grab = p_sys->p_grab =
356 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
357 p_grab->p_module = NULL;
358 p_grab->p_sout = p_sout;
359 p_grab->psz_access = strdup( "grab" );
360 p_grab->p_cfg = NULL;
361 p_grab->psz_name = strdup( "" );
362 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
363 p_grab->pf_seek = NULL;
364 p_grab->pf_write = AccessOutGrabberWrite;
367 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val.psz_string, p_sys->p_grab ) ) )
369 msg_Err( p_stream, "cannot create the muxer (%s)", val.psz_string );
370 sout_AccessOutDelete( p_sys->p_grab );
371 sout_AccessOutDelete( p_sys->p_access );
376 /* create the SDP only once */
378 malloc( 200 + 20 + 10 + strlen( p_sys->psz_destination ) +
379 10 + 10 + 10 + 10 + strlen( psz_rtpmap ) );
380 sprintf( p_sys->psz_sdp,
382 "o=- "I64Fd" %d IN IP4 127.0.0.1\n"
385 "m=video %d RTP/AVP %d\n"
387 p_sys->i_sdp_id, p_sys->i_sdp_version,
388 p_sys->psz_session_name,
389 p_sys->psz_destination, p_sys->i_ttl,
390 p_sys->i_port, p_sys->i_payload_type,
391 p_sys->i_payload_type, psz_rtpmap );
392 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
394 /* create the rtp context */
395 p_sys->ssrc[0] = rand()&0xff;
396 p_sys->ssrc[1] = rand()&0xff;
397 p_sys->ssrc[2] = rand()&0xff;
398 p_sys->ssrc[3] = rand()&0xff;
399 p_sys->i_sequence = rand()&0xffff;
400 p_sys->i_timestamp_start = rand()&0xffffffff;
401 p_sys->packet = NULL;
406 p_sys->p_access = NULL;
407 p_sys->p_grab = NULL;
409 free( val.psz_string );
412 var_Get( p_stream, SOUT_CFG_PREFIX "sdp", &val );
413 if( *val.psz_string )
417 vlc_UrlParse( &url, val.psz_string, 0 );
418 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
420 if( HttpSetup( p_stream, &url ) )
422 msg_Err( p_stream, "cannot export sdp as http" );
425 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
427 /* FIXME test if destination is multicast or no destination at all FIXME */
428 if( RtspSetup( p_stream, &url ) )
430 msg_Err( p_stream, "cannot export sdp as rtsp" );
433 else if( url.psz_protocol && !strcasecmp( url.psz_protocol, "sap" ) )
435 p_sys->b_export_sap = VLC_TRUE;
436 SapSetup( p_stream );
440 msg_Warn( p_stream, "unknow protocol for SDP (%s)",
443 vlc_UrlClean( &url );
445 free( val.psz_string );
447 /* update p_sout->i_out_pace_nocontrol */
448 p_stream->p_sout->i_out_pace_nocontrol++;
453 /*****************************************************************************
455 *****************************************************************************/
456 static void Close( vlc_object_t * p_this )
458 sout_stream_t *p_stream = (sout_stream_t*)p_this;
459 sout_stream_sys_t *p_sys = p_stream->p_sys;
461 /* update p_sout->i_out_pace_nocontrol */
462 p_stream->p_sout->i_out_pace_nocontrol--;
466 sout_MuxDelete( p_sys->p_mux );
467 sout_AccessOutDelete( p_sys->p_access );
468 sout_AccessOutDelete( p_sys->p_grab );
471 block_Release( p_sys->packet );
475 while( p_sys->i_rtsp > 0 )
477 RtspClientDel( p_stream, p_sys->rtsp[0] );
480 vlc_mutex_destroy( &p_sys->lock_sdp );
482 if( p_sys->p_httpd_file )
484 httpd_FileDelete( p_sys->p_httpd_file );
486 if( p_sys->p_httpd_host )
488 httpd_HostDelete( p_sys->p_httpd_host );
490 if( p_sys->p_rtsp_url )
492 httpd_UrlDelete( p_sys->p_rtsp_url );
494 if( p_sys->p_rtsp_host )
496 httpd_HostDelete( p_sys->p_rtsp_host );
499 if( p_sys->psz_session_name )
501 free( p_sys->psz_session_name );
502 p_sys->psz_session_name = NULL;
507 free( p_sys->psz_sdp );
512 /*****************************************************************************
514 *****************************************************************************/
515 static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
517 sout_stream_sys_t *p_sys = p_stream->p_sys;
522 i_size = strlen( "v=0\n" ) +
523 strlen( "o=- * * IN IP4 127.0.0.1\n" ) +
525 strlen( "c=IN IP4 */*\n" ) +
526 strlen( psz_destination ? psz_destination : "0.0.0.0") +
527 strlen( p_sys->psz_session_name ) +
529 for( i = 0; i < p_sys->i_es; i++ )
531 sout_stream_id_t *id = p_sys->es[i];
533 i_size += strlen( "m=**d*o * RTP/AVP *\n" ) + 10 + 10;
536 i_size += strlen( "a=rtpmap:* *\n" ) + strlen( id->psz_rtpmap )+10;
540 i_size += strlen( "a=fmtp:* *\n" ) + strlen( id->psz_fmtp ) + 10;
544 i_size += strlen( "a=control:*/trackid=*\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
548 p = psz_sdp = malloc( i_size );
549 p += sprintf( p, "v=0\n" );
550 p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\n",
551 p_sys->i_sdp_id, p_sys->i_sdp_version );
552 p += sprintf( p, "s=%s\n", p_sys->psz_session_name );
553 p += sprintf( p, "c=IN IP4 %s/%d\n", psz_destination ? psz_destination : "0.0.0.0",
556 for( i = 0; i < p_sys->i_es; i++ )
558 sout_stream_id_t *id = p_sys->es[i];
560 if( id->i_cat == AUDIO_ES )
562 p += sprintf( p, "m=audio %d RTP/AVP %d\n",
563 id->i_port, id->i_payload_type );
565 else if( id->i_cat == VIDEO_ES )
567 p += sprintf( p, "m=video %d RTP/AVP %d\n",
568 id->i_port, id->i_payload_type );
576 p += sprintf( p, "a=rtpmap:%d %s\n", id->i_payload_type,
581 p += sprintf( p, "a=fmtp:%d %s\n", id->i_payload_type,
586 p += sprintf( p, "a=control:%s/trackid=%d\n", p_sys->psz_rtsp_control, i );
593 /*****************************************************************************
595 *****************************************************************************/
596 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
597 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
598 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
599 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
600 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
601 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
602 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
603 static int rtp_packetize_h263 ( sout_stream_t *, sout_stream_id_t *, block_t * );
605 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
607 static const char hex[16] = "0123456789abcdef";
610 for( i = 0; i < i_data; i++ )
612 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
613 s[2*i+1] = hex[(p_data[i] )&0xf];
618 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
620 sout_instance_t *p_sout = p_stream->p_sout;
621 sout_stream_sys_t *p_sys = p_stream->p_sys;
622 sout_stream_id_t *id;
623 sout_access_out_t *p_access = NULL;
625 if( p_sys->p_mux != NULL )
627 sout_input_t *p_input = NULL;
628 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
630 msg_Err( p_stream, "cannot add this stream to the muxer" );
634 id = malloc( sizeof( sout_stream_id_t ) );
635 memset( id, 0, sizeof( sout_stream_id_t ) );
637 id->p_input = p_input;
638 id->pf_packetize= NULL;
639 id->p_rtsp_url = NULL;
644 if( p_sys->psz_destination )
647 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
649 /* first try to create the access out */
650 if( p_sys->i_ttl > 0 )
652 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
656 sprintf( access, "udp{raw}" );
658 sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
659 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
661 msg_Err( p_stream, "cannot create the access out for %s://%s",
665 msg_Dbg( p_stream, "access out %s:%s", access, url );
668 /* not create the rtp specific stuff */
669 id = malloc( sizeof( sout_stream_id_t ) );
670 memset( id, 0, sizeof( sout_stream_id_t ) );
671 id->p_stream = p_stream;
672 id->p_access = p_access;
674 id->psz_rtpmap = NULL;
676 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
677 id->i_port = p_sys->i_port;
678 id->p_rtsp_url = NULL;
679 vlc_mutex_init( p_stream, &id->lock_rtsp );
680 id->i_rtsp_access = 0;
681 id->rtsp_access = NULL;
683 switch( p_fmt->i_codec )
685 case VLC_FOURCC( 's', '1', '6', 'b' ):
686 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
688 id->i_payload_type = 11;
690 else if( p_fmt->audio.i_channels == 2 &&
691 p_fmt->audio.i_rate == 44100 )
693 id->i_payload_type = 10;
697 id->i_payload_type = p_sys->i_payload_type++;
699 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
700 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
701 p_fmt->audio.i_channels );
702 id->i_clock_rate = p_fmt->audio.i_rate;
703 id->pf_packetize = rtp_packetize_l16;
705 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
706 id->i_payload_type = p_sys->i_payload_type++;
707 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
708 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
709 p_fmt->audio.i_channels );
710 id->i_clock_rate = p_fmt->audio.i_rate;
711 id->pf_packetize = rtp_packetize_l8;
713 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
714 id->i_payload_type = 14;
715 id->i_clock_rate = 90000;
716 id->psz_rtpmap = strdup( "MPA/90000" );
717 id->pf_packetize = rtp_packetize_mpa;
719 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
720 id->i_payload_type = 32;
721 id->i_clock_rate = 90000;
722 id->psz_rtpmap = strdup( "MPV/90000" );
723 id->pf_packetize = rtp_packetize_mpv;
725 case VLC_FOURCC( 'a', '5', '2', ' ' ):
726 id->i_payload_type = p_sys->i_payload_type++;
727 id->i_clock_rate = 90000;
728 id->psz_rtpmap = strdup( "ac3/90000" );
729 id->pf_packetize = rtp_packetize_ac3;
731 case VLC_FOURCC( 'H', '2', '6', '3' ):
732 id->i_payload_type = p_sys->i_payload_type++;
733 id->i_clock_rate = 90000;
734 id->psz_rtpmap = strdup( "H263-1998/90000" );
735 id->pf_packetize = rtp_packetize_h263;
738 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
740 char hexa[2*p_fmt->i_extra +1];
742 id->i_payload_type = p_sys->i_payload_type++;
743 id->i_clock_rate = 90000;
744 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
745 id->pf_packetize = rtp_packetize_split;
746 if( p_fmt->i_extra > 0 )
748 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
749 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
750 sprintf( id->psz_fmtp,
751 "profile-level-id=3; config=%s;", hexa );
755 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
757 char hexa[2*p_fmt->i_extra +1];
759 id->i_payload_type = p_sys->i_payload_type++;
760 id->i_clock_rate = p_fmt->audio.i_rate;
761 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
762 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
763 id->pf_packetize = rtp_packetize_mp4a;
764 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
765 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
766 sprintf( id->psz_fmtp,
767 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
768 "config=%s; SizeLength=13;IndexLength=3; "
769 "IndexDeltaLength=3; Profile=1;", hexa );
774 msg_Err( p_stream, "cannot add this stream (unsupported "
775 "codec:%4.4s)", (char*)&p_fmt->i_codec );
779 id->i_cat = p_fmt->i_cat;
781 id->ssrc[0] = rand()&0xff;
782 id->ssrc[1] = rand()&0xff;
783 id->ssrc[2] = rand()&0xff;
784 id->ssrc[3] = rand()&0xff;
785 id->i_sequence = rand()&0xffff;
786 id->i_timestamp_start = rand()&0xffffffff;
788 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
789 if( id->i_mtu <= 16 )
791 /* better than nothing */
794 msg_Dbg( p_stream, "using mtu=%d", id->i_mtu );
796 if( p_sys->p_rtsp_url )
798 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
800 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
801 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
802 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
806 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
807 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
808 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
813 /* Update p_sys context */
814 /* update port used (2 -> 1 rtp, 1 rtcp )*/
815 vlc_mutex_lock( &p_sys->lock_es );
816 TAB_APPEND( p_sys->i_es, p_sys->es, id );
817 vlc_mutex_unlock( &p_sys->lock_es );
819 if( p_sys->p_mux == NULL )
823 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
825 vlc_mutex_lock( &p_sys->lock_sdp );
826 free( p_sys->psz_sdp );
827 p_sys->psz_sdp = psz_sdp;
828 vlc_mutex_unlock( &p_sys->lock_sdp );
830 p_sys->i_sdp_version++;
832 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
834 /* Update the SAP announce */
835 if( p_sys->b_export_sap )
837 SapSetup( p_stream );
844 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
846 sout_stream_sys_t *p_sys = p_stream->p_sys;
848 vlc_mutex_lock( &p_sys->lock_es );
849 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
850 vlc_mutex_unlock( &p_sys->lock_es );
856 free( id->psz_rtpmap );
860 free( id->psz_fmtp );
862 if( id->psz_destination )
863 free( id->psz_destination );
864 sout_AccessOutDelete( id->p_access );
866 else if( id->p_input )
868 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
872 httpd_UrlDelete( id->p_rtsp_url );
874 vlc_mutex_destroy( &id->lock_rtsp );
875 if( id->rtsp_access ) free( id->rtsp_access );
877 /* Update the SAP announce */
878 if( p_sys->b_export_sap )
880 SapSetup( p_stream );
887 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
892 if( p_stream->p_sys->p_mux )
894 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
900 p_next = p_buffer->p_next;
901 if( id->pf_packetize( p_stream, id, p_buffer ) )
905 block_Release( p_buffer );
913 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
916 sout_stream_sys_t *p_sys = p_stream->p_sys;
918 int64_t i_dts = p_buffer->i_dts;
919 uint32_t i_timestamp = i_dts * 9 / 100;
921 uint8_t *p_data = p_buffer->p_buffer;
922 unsigned int i_data = p_buffer->i_buffer;
923 unsigned int i_max = p_sys->i_mtu - 12;
925 int i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
931 /* output complete packet */
933 p_sys->packet->i_buffer + i_data > i_max )
935 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
936 p_sys->packet = NULL;
939 if( p_sys->packet == NULL )
941 /* allocate a new packet */
942 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
943 p_sys->packet->p_buffer[ 0] = 0x80;
944 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
945 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
946 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
947 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
948 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
949 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
950 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
951 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
952 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
953 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
954 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
955 p_sys->packet->i_buffer = 12;
957 p_sys->packet->i_dts = i_dts;
958 p_sys->packet->i_length = p_buffer->i_length / i_packet;
959 i_dts += p_sys->packet->i_length;
964 i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_buffer );
966 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
969 p_sys->packet->i_buffer += i_size;
977 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
980 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
982 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
988 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
990 p_next = p_buffer->p_next;
991 block_Release( p_buffer );
998 /****************************************************************************
1000 ****************************************************************************/
1001 static int SapSetup( sout_stream_t *p_stream )
1003 sout_stream_sys_t *p_sys = p_stream->p_sys;
1004 sout_instance_t *p_sout = p_stream->p_sout;
1005 announce_method_t *p_method = (announce_method_t *)
1006 malloc(sizeof(announce_method_t));
1008 /* Remove the previous session */
1009 if( p_sys->p_session != NULL)
1011 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1012 sout_AnnounceSessionDestroy( p_sys->p_session );
1013 p_sys->p_session = NULL;
1015 p_method->i_type = METHOD_TYPE_SAP;
1016 p_method->psz_address = NULL; /* FIXME */
1017 p_method->i_ip_version = 4; /* FIXME ! */
1019 if( p_sys->i_es > 0 && p_sys->psz_sdp && *p_sys->psz_sdp )
1021 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1029 /****************************************************************************
1031 ****************************************************************************/
1032 static int HttpCallback( httpd_file_sys_t *p_args,
1033 httpd_file_t *, uint8_t *p_request,
1034 uint8_t **pp_data, int *pi_data );
1036 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1038 sout_stream_sys_t *p_sys = p_stream->p_sys;
1040 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
1041 if( p_sys->p_httpd_host )
1043 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1044 url->psz_path ? url->psz_path : "/",
1047 HttpCallback, (void*)p_sys );
1049 if( p_sys->p_httpd_file == NULL )
1051 return VLC_EGENERIC;
1056 static int HttpCallback( httpd_file_sys_t *p_args,
1057 httpd_file_t *f, uint8_t *p_request,
1058 uint8_t **pp_data, int *pi_data )
1060 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1062 vlc_mutex_lock( &p_sys->lock_sdp );
1063 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1065 *pi_data = strlen( p_sys->psz_sdp );
1066 *pp_data = malloc( *pi_data );
1067 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1074 vlc_mutex_unlock( &p_sys->lock_sdp );
1079 /****************************************************************************
1081 ****************************************************************************/
1082 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1084 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1086 rtsp->psz_session = psz_session;
1088 rtsp->b_playing = VLC_FALSE;
1092 rtsp->access = NULL;
1094 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1098 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1101 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1103 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1105 return p_stream->p_sys->rtsp[i];
1111 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1114 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1116 for( i = 0; i < rtsp->i_access; i++ )
1118 sout_AccessOutDelete( rtsp->access[i] );
1120 if( rtsp->id ) free( rtsp->id );
1121 if( rtsp->access ) free( rtsp->access );
1123 free( rtsp->psz_session );
1127 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1129 sout_stream_sys_t *p_sys = p_stream->p_sys;
1131 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1133 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1134 if( p_sys->p_rtsp_host == NULL )
1136 return VLC_EGENERIC;
1139 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1140 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1141 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1142 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1144 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
1145 if( p_sys->p_rtsp_url == 0 )
1147 return VLC_EGENERIC;
1149 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1150 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1151 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1152 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1157 static int RtspCallback( httpd_callback_sys_t *p_args,
1159 httpd_message_t *answer, httpd_message_t *query )
1161 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1162 sout_stream_sys_t *p_sys = p_stream->p_sys;
1163 char *psz_destination = p_sys->psz_destination;
1164 char *psz_session = NULL;
1166 if( answer == NULL || query == NULL )
1170 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1172 answer->i_proto = HTTPD_PROTO_RTSP;
1173 answer->i_version= query->i_version;
1174 answer->i_type = HTTPD_MSG_ANSWER;
1176 switch( query->i_type )
1178 case HTTPD_MSG_DESCRIBE:
1180 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1182 answer->i_status = 200;
1183 answer->psz_status = strdup( "OK" );
1184 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1186 answer->p_body = psz_sdp;
1187 answer->i_body = strlen( psz_sdp );
1191 case HTTPD_MSG_PLAY:
1193 rtsp_client_t *rtsp;
1194 /* for now only multicast so easy */
1195 answer->i_status = 200;
1196 answer->psz_status = strdup( "OK" );
1198 answer->p_body = NULL;
1200 psz_session = httpd_MsgGet( query, "Session" );
1201 rtsp = RtspClientGet( p_stream, psz_session );
1202 if( rtsp && !rtsp->b_playing )
1206 rtsp->b_playing = VLC_TRUE;
1208 vlc_mutex_lock( &p_sys->lock_es );
1209 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1211 sout_stream_id_t *id = rtsp->id[i_id];
1214 for( i = 0; i < p_sys->i_es; i++ )
1216 if( id == p_sys->es[i] )
1219 if( i >= p_sys->i_es ) continue;
1221 vlc_mutex_lock( &id->lock_rtsp );
1222 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1223 vlc_mutex_unlock( &id->lock_rtsp );
1225 vlc_mutex_unlock( &p_sys->lock_es );
1229 case HTTPD_MSG_PAUSE:
1231 return VLC_EGENERIC;
1232 case HTTPD_MSG_TEARDOWN:
1234 rtsp_client_t *rtsp;
1236 /* for now only multicast so easy again */
1237 answer->i_status = 200;
1238 answer->psz_status = strdup( "OK" );
1240 answer->p_body = NULL;
1242 psz_session = httpd_MsgGet( query, "Session" );
1243 rtsp = RtspClientGet( p_stream, psz_session );
1248 vlc_mutex_lock( &p_sys->lock_es );
1249 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1251 sout_stream_id_t *id = rtsp->id[i_id];
1254 for( i = 0; i < p_sys->i_es; i++ )
1256 if( id == p_sys->es[i] )
1259 if( i >= p_sys->i_es ) continue;
1261 vlc_mutex_lock( &id->lock_rtsp );
1262 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1263 vlc_mutex_unlock( &id->lock_rtsp );
1265 vlc_mutex_unlock( &p_sys->lock_es );
1267 RtspClientDel( p_stream, rtsp );
1273 return VLC_EGENERIC;
1275 httpd_MsgAdd( answer, "Server", "VLC Server" );
1276 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1277 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1278 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1282 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1287 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1289 httpd_message_t *answer, httpd_message_t *query )
1291 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1292 sout_stream_t *p_stream = id->p_stream;
1293 sout_stream_sys_t *p_sys = p_stream->p_sys;
1294 char *psz_session = NULL;
1296 if( answer == NULL || query == NULL )
1300 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1302 answer->i_proto = HTTPD_PROTO_RTSP;
1303 answer->i_version= query->i_version;
1304 answer->i_type = HTTPD_MSG_ANSWER;
1306 switch( query->i_type )
1308 case HTTPD_MSG_SETUP:
1310 char *psz_transport = httpd_MsgGet( query, "Transport" );
1312 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1314 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1316 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1317 answer->i_status = 200;
1318 answer->psz_status = strdup( "OK" );
1320 answer->p_body = NULL;
1321 psz_session = httpd_MsgGet( query, "Session" );
1322 if( *psz_session == 0 )
1324 psz_session = malloc( 100 );
1325 sprintf( psz_session, "%d", rand() );
1327 httpd_MsgAdd( answer, "Transport",
1328 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1329 id->psz_destination, id->i_port,id->i_port+1, p_sys->i_ttl );
1331 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1333 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1334 char *ip = httpd_ClientIP( cl );
1336 char psz_access[100];
1339 sout_access_out_t *p_access;
1341 rtsp_client_t *rtsp = NULL;
1343 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1346 psz_session = httpd_MsgGet( query, "Session" );
1347 if( *psz_session == 0 )
1349 psz_session = malloc( 100 );
1350 sprintf( psz_session, "%d", rand() );
1352 rtsp = RtspClientNew( p_stream, psz_session );
1356 rtsp = RtspClientGet( p_stream, psz_session );
1359 /* FIXME right error code */
1360 answer->i_status = 400;
1361 answer->psz_status = strdup( "Unknown session id" );
1363 answer->p_body = NULL;
1368 /* first try to create the access out */
1369 if( p_sys->i_ttl > 0 )
1370 sprintf( psz_access, "udp{raw,ttl=%d}", p_sys->i_ttl );
1372 sprintf( psz_access, "udp{raw}" );
1373 sprintf( psz_url, "%s:%d", ip, i_port );
1374 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1376 msg_Err( p_stream, "cannot create the access out for %s://%s",
1377 psz_access, psz_url );
1378 answer->i_status = 400;
1379 answer->psz_status = strdup( "Server internal error" );
1381 answer->p_body = NULL;
1385 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1386 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1388 answer->i_status = 200;
1389 answer->psz_status = strdup( "OK" );
1391 answer->p_body = NULL;
1393 httpd_MsgAdd( answer, "Transport",
1394 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1396 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1398 answer->i_status = 400;
1399 answer->psz_status = strdup( "Bad Request" );
1401 answer->p_body = NULL;
1407 return VLC_EGENERIC;
1409 httpd_MsgAdd( answer, "Server", "VLC Server" );
1410 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1411 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1412 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1416 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1421 /****************************************************************************
1423 ****************************************************************************/
1424 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1425 int b_marker, int64_t i_pts )
1427 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1429 out->p_buffer[0] = 0x80;
1430 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1431 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1432 out->p_buffer[3] = ( id->i_sequence )&0xff;
1433 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1434 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1435 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1436 out->p_buffer[7] = ( i_timestamp )&0xff;
1438 out->p_buffer[ 8] = id->ssrc[0];
1439 out->p_buffer[ 9] = id->ssrc[1];
1440 out->p_buffer[10] = id->ssrc[2];
1441 out->p_buffer[11] = id->ssrc[3];
1447 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1450 vlc_mutex_lock( &id->lock_rtsp );
1451 for( i = 0; i < id->i_rtsp_access; i++ )
1453 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1455 vlc_mutex_unlock( &id->lock_rtsp );
1459 sout_AccessOutWrite( id->p_access, out );
1463 block_Release( out );
1467 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1470 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1471 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1473 uint8_t *p_data = in->p_buffer;
1474 int i_data = in->i_buffer;
1477 for( i = 0; i < i_count; i++ )
1479 int i_payload = __MIN( i_max, i_data );
1480 block_t *out = block_New( p_stream, 16 + i_payload );
1482 /* rtp common header */
1483 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1485 out->p_buffer[12] = 0;
1486 out->p_buffer[13] = 0;
1487 /* fragment offset in the current frame */
1488 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1489 out->p_buffer[15] = ( (i*i_max) )&0xff;
1490 memcpy( &out->p_buffer[16], p_data, i_payload );
1492 out->i_buffer = 16 + i_payload;
1493 out->i_dts = in->i_dts + i * in->i_length / i_count;
1494 out->i_length = in->i_length / i_count;
1496 rtp_packetize_send( id, out );
1498 p_data += i_payload;
1499 i_data -= i_payload;
1506 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1509 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1510 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1512 uint8_t *p_data = in->p_buffer;
1513 int i_data = in->i_buffer;
1515 int b_sequence_start = 0;
1516 int i_temporal_ref = 0;
1517 int i_picture_coding_type = 0;
1518 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1519 int b_start_slice = 0;
1521 /* preparse this packet to get some info */
1522 if( in->i_buffer > 4 )
1524 uint8_t *p = p_data;
1525 int i_rest = in->i_buffer;
1529 while( i_rest > 4 &&
1530 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1544 /* sequence start code */
1545 b_sequence_start = 1;
1547 else if( *p == 0x00 && i_rest >= 4 )
1550 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1551 i_picture_coding_type = (p[2] >> 3)&0x07;
1553 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1554 i_picture_coding_type == 3 ) )
1556 i_ffv = (p[3] >> 2)&0x01;
1557 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1558 if( i_rest > 4 && i_picture_coding_type == 3 )
1560 i_fbv = (p[4]>>6)&0x01;
1561 i_bfc = (p[4]>>3)&0x07;
1565 else if( *p <= 0xaf )
1572 for( i = 0; i < i_count; i++ )
1574 int i_payload = __MIN( i_max, i_data );
1575 block_t *out = block_New( p_stream,
1577 uint32_t h = ( i_temporal_ref << 16 )|
1578 ( b_sequence_start << 13 )|
1579 ( b_start_slice << 12 )|
1580 ( i == i_count - 1 ? 1 << 11 : 0 )|
1581 ( i_picture_coding_type << 8 )|
1582 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1584 /* rtp common header */
1585 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1586 in->i_pts > 0 ? in->i_pts : in->i_dts );
1588 /* 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 */
1589 out->p_buffer[12] = ( h >> 24 )&0xff;
1590 out->p_buffer[13] = ( h >> 16 )&0xff;
1591 out->p_buffer[14] = ( h >> 8 )&0xff;
1592 out->p_buffer[15] = ( h )&0xff;
1594 memcpy( &out->p_buffer[16], p_data, i_payload );
1596 out->i_buffer = 16 + i_payload;
1597 out->i_dts = in->i_dts + i * in->i_length / i_count;
1598 out->i_length = in->i_length / i_count;
1600 rtp_packetize_send( id, out );
1602 p_data += i_payload;
1603 i_data -= i_payload;
1608 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1611 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1612 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1614 uint8_t *p_data = in->p_buffer;
1615 int i_data = in->i_buffer;
1618 for( i = 0; i < i_count; i++ )
1620 int i_payload = __MIN( i_max, i_data );
1621 block_t *out = block_New( p_stream, 14 + i_payload );
1623 /* rtp common header */
1624 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1626 out->p_buffer[12] = 1;
1628 out->p_buffer[13] = 0x00;
1630 memcpy( &out->p_buffer[14], p_data, i_payload );
1632 out->i_buffer = 14 + i_payload;
1633 out->i_dts = in->i_dts + i * in->i_length / i_count;
1634 out->i_length = in->i_length / i_count;
1636 rtp_packetize_send( id, out );
1638 p_data += i_payload;
1639 i_data -= i_payload;
1645 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1648 int i_max = id->i_mtu - 12; /* payload max in one packet */
1649 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1651 uint8_t *p_data = in->p_buffer;
1652 int i_data = in->i_buffer;
1655 for( i = 0; i < i_count; i++ )
1657 int i_payload = __MIN( i_max, i_data );
1658 block_t *out = block_New( p_stream, 12 + i_payload );
1660 /* rtp common header */
1661 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1662 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1663 memcpy( &out->p_buffer[12], p_data, i_payload );
1665 out->i_buffer = 12 + i_payload;
1666 out->i_dts = in->i_dts + i * in->i_length / i_count;
1667 out->i_length = in->i_length / i_count;
1669 rtp_packetize_send( id, out );
1671 p_data += i_payload;
1672 i_data -= i_payload;
1678 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1681 int i_max = id->i_mtu - 12; /* payload max in one packet */
1682 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1684 uint8_t *p_data = in->p_buffer;
1685 int i_data = in->i_buffer;
1690 int i_payload = (__MIN( i_max, i_data )/4)*4;
1691 block_t *out = block_New( p_stream, 12 + i_payload );
1693 /* rtp common header */
1694 rtp_packetize_common( id, out, 0,
1695 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1696 memcpy( &out->p_buffer[12], p_data, i_payload );
1698 out->i_buffer = 12 + i_payload;
1699 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1700 out->i_length = in->i_length / i_count;
1702 rtp_packetize_send( id, out );
1704 p_data += i_payload;
1705 i_data -= i_payload;
1712 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1715 int i_max = id->i_mtu - 12; /* payload max in one packet */
1716 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1718 uint8_t *p_data = in->p_buffer;
1719 int i_data = in->i_buffer;
1724 int i_payload = (__MIN( i_max, i_data )/2)*2;
1725 block_t *out = block_New( p_stream, 12 + i_payload );
1727 /* rtp common header */
1728 rtp_packetize_common( id, out, 0,
1729 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1730 memcpy( &out->p_buffer[12], p_data, i_payload );
1732 out->i_buffer = 12 + i_payload;
1733 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1734 out->i_length = in->i_length / i_count;
1736 rtp_packetize_send( id, out );
1738 p_data += i_payload;
1739 i_data -= i_payload;
1745 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1748 int i_max = id->i_mtu - 16; /* payload max in one packet */
1749 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1751 uint8_t *p_data = in->p_buffer;
1752 int i_data = in->i_buffer;
1755 for( i = 0; i < i_count; i++ )
1757 int i_payload = __MIN( i_max, i_data );
1758 block_t *out = block_New( p_stream, 16 + i_payload );
1760 /* rtp common header */
1761 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1762 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1764 /* AU headers length (bits) */
1765 out->p_buffer[12] = 0;
1766 out->p_buffer[13] = 2*8;
1767 /* for each AU length 13 bits + idx 3bits, */
1768 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1769 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1771 memcpy( &out->p_buffer[16], p_data, i_payload );
1773 out->i_buffer = 16 + i_payload;
1774 out->i_dts = in->i_dts + i * in->i_length / i_count;
1775 out->i_length = in->i_length / i_count;
1777 rtp_packetize_send( id, out );
1779 p_data += i_payload;
1780 i_data -= i_payload;
1788 #define RTP_H263_HEADER_SIZE (2) // plen = 0
1789 #define RTP_H263_PAYLOAD_START (14) // plen = 0
1790 static int rtp_packetize_h263( sout_stream_t *p_stream, sout_stream_id_t *id,
1793 uint8_t *p_data = in->p_buffer;
1794 int i_data = in->i_buffer;
1796 int i_max = id->i_mtu - 12 - RTP_H263_HEADER_SIZE; /* payload max in one packet */
1799 int b_v_bit = 0; // no pesky error resilience
1800 int i_plen = 0; // normally plen=0 for PSC packet
1801 int i_pebit = 0; // because plen=0
1806 return VLC_EGENERIC;
1808 if( p_data[0] || p_data[1] )
1810 return VLC_EGENERIC;
1812 /* remove 2 leading 0 bytes */
1815 i_count = ( i_data + i_max - 1 ) / i_max;
1817 for( i = 0; i < i_count; i++ )
1819 int i_payload = __MIN( i_max, i_data );
1820 block_t *out = block_New( p_stream,
1821 RTP_H263_PAYLOAD_START + i_payload );
1822 b_p_bit = (i == 0) ? 1 : 0;
1823 h = ( b_p_bit << 10 )|
1828 /* rtp common header */
1829 //b_m_bit = 1; // always contains end of frame
1830 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1831 in->i_pts > 0 ? in->i_pts : in->i_dts );
1834 out->p_buffer[12] = ( h >> 8 )&0xff;
1835 out->p_buffer[13] = ( h )&0xff;
1836 memcpy( &out->p_buffer[RTP_H263_PAYLOAD_START], p_data, i_payload );
1838 out->i_buffer = RTP_H263_PAYLOAD_START + i_payload;
1839 out->i_dts = in->i_dts + i * in->i_length / i_count;
1840 out->i_length = in->i_length / i_count;
1842 rtp_packetize_send( id, out );
1844 p_data += i_payload;
1845 i_data -= i_payload;