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 * );
604 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
606 static const char hex[16] = "0123456789abcdef";
609 for( i = 0; i < i_data; i++ )
611 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
612 s[2*i+1] = hex[(p_data[i] )&0xf];
617 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
619 sout_instance_t *p_sout = p_stream->p_sout;
620 sout_stream_sys_t *p_sys = p_stream->p_sys;
621 sout_stream_id_t *id;
622 sout_access_out_t *p_access = NULL;
624 if( p_sys->p_mux != NULL )
626 sout_input_t *p_input = NULL;
627 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
629 msg_Err( p_stream, "cannot add this stream to the muxer" );
633 id = malloc( sizeof( sout_stream_id_t ) );
634 memset( id, 0, sizeof( sout_stream_id_t ) );
636 id->p_input = p_input;
637 id->pf_packetize= NULL;
638 id->p_rtsp_url = NULL;
643 if( p_sys->psz_destination )
646 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
648 /* first try to create the access out */
649 if( p_sys->i_ttl > 0 )
651 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
655 sprintf( access, "udp{raw}" );
657 sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
658 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
660 msg_Err( p_stream, "cannot create the access out for %s://%s",
664 msg_Dbg( p_stream, "access out %s:%s", access, url );
667 /* not create the rtp specific stuff */
668 id = malloc( sizeof( sout_stream_id_t ) );
669 memset( id, 0, sizeof( sout_stream_id_t ) );
670 id->p_stream = p_stream;
671 id->p_access = p_access;
673 id->psz_rtpmap = NULL;
675 id->psz_destination = p_sys->psz_destination ? strdup( p_sys->psz_destination ) : NULL;
676 id->i_port = p_sys->i_port;
677 id->p_rtsp_url = NULL;
678 vlc_mutex_init( p_stream, &id->lock_rtsp );
679 id->i_rtsp_access = 0;
680 id->rtsp_access = NULL;
682 switch( p_fmt->i_codec )
684 case VLC_FOURCC( 's', '1', '6', 'b' ):
685 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
687 id->i_payload_type = 11;
689 else if( p_fmt->audio.i_channels == 2 &&
690 p_fmt->audio.i_rate == 44100 )
692 id->i_payload_type = 10;
696 id->i_payload_type = p_sys->i_payload_type++;
698 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
699 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
700 p_fmt->audio.i_channels );
701 id->i_clock_rate = p_fmt->audio.i_rate;
702 id->pf_packetize = rtp_packetize_l16;
704 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
705 id->i_payload_type = p_sys->i_payload_type++;
706 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
707 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
708 p_fmt->audio.i_channels );
709 id->i_clock_rate = p_fmt->audio.i_rate;
710 id->pf_packetize = rtp_packetize_l8;
712 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
713 id->i_payload_type = 14;
714 id->i_clock_rate = 90000;
715 id->psz_rtpmap = strdup( "MPA/90000" );
716 id->pf_packetize = rtp_packetize_mpa;
718 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
719 id->i_payload_type = 32;
720 id->i_clock_rate = 90000;
721 id->psz_rtpmap = strdup( "MPV/90000" );
722 id->pf_packetize = rtp_packetize_mpv;
724 case VLC_FOURCC( 'a', '5', '2', ' ' ):
725 id->i_payload_type = p_sys->i_payload_type++;
726 id->i_clock_rate = 90000;
727 id->psz_rtpmap = strdup( "ac3/90000" );
728 id->pf_packetize = rtp_packetize_ac3;
730 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
732 char hexa[2*p_fmt->i_extra +1];
734 id->i_payload_type = p_sys->i_payload_type++;
735 id->i_clock_rate = 90000;
736 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
737 id->pf_packetize = rtp_packetize_split;
738 if( p_fmt->i_extra > 0 )
740 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
741 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
742 sprintf( id->psz_fmtp,
743 "profile-level-id=3; config=%s;", hexa );
747 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
749 char hexa[2*p_fmt->i_extra +1];
751 id->i_payload_type = p_sys->i_payload_type++;
752 id->i_clock_rate = p_fmt->audio.i_rate;
753 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
754 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
755 id->pf_packetize = rtp_packetize_mp4a;
756 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
757 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
758 sprintf( id->psz_fmtp,
759 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
760 "config=%s; SizeLength=13;IndexLength=3; "
761 "IndexDeltaLength=3; Profile=1;", hexa );
766 msg_Err( p_stream, "cannot add this stream (unsupported "
767 "codec:%4.4s)", (char*)&p_fmt->i_codec );
771 id->i_cat = p_fmt->i_cat;
773 id->ssrc[0] = rand()&0xff;
774 id->ssrc[1] = rand()&0xff;
775 id->ssrc[2] = rand()&0xff;
776 id->ssrc[3] = rand()&0xff;
777 id->i_sequence = rand()&0xffff;
778 id->i_timestamp_start = rand()&0xffffffff;
780 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
781 if( id->i_mtu <= 16 )
783 /* better than nothing */
786 msg_Dbg( p_stream, "using mtu=%d", id->i_mtu );
788 if( p_sys->p_rtsp_url )
790 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
792 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
793 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
794 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
798 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallbackId, (void*)id );
799 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
800 //httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
805 /* Update p_sys context */
806 /* update port used (2 -> 1 rtp, 1 rtcp )*/
807 vlc_mutex_lock( &p_sys->lock_es );
808 TAB_APPEND( p_sys->i_es, p_sys->es, id );
809 vlc_mutex_unlock( &p_sys->lock_es );
811 if( p_sys->p_mux == NULL )
815 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
817 vlc_mutex_lock( &p_sys->lock_sdp );
818 free( p_sys->psz_sdp );
819 p_sys->psz_sdp = psz_sdp;
820 vlc_mutex_unlock( &p_sys->lock_sdp );
822 p_sys->i_sdp_version++;
824 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
826 /* Update the SAP announce */
827 if( p_sys->b_export_sap )
829 SapSetup( p_stream );
836 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
838 sout_stream_sys_t *p_sys = p_stream->p_sys;
840 vlc_mutex_lock( &p_sys->lock_es );
841 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
842 vlc_mutex_unlock( &p_sys->lock_es );
848 free( id->psz_rtpmap );
852 free( id->psz_fmtp );
854 if( id->psz_destination )
855 free( id->psz_destination );
856 sout_AccessOutDelete( id->p_access );
858 else if( id->p_input )
860 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
864 httpd_UrlDelete( id->p_rtsp_url );
866 vlc_mutex_destroy( &id->lock_rtsp );
867 if( id->rtsp_access ) free( id->rtsp_access );
869 /* Update the SAP announce */
870 if( p_sys->b_export_sap )
872 SapSetup( p_stream );
879 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
884 if( p_stream->p_sys->p_mux )
886 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
892 p_next = p_buffer->p_next;
893 if( id->pf_packetize( p_stream, id, p_buffer ) )
897 block_Release( p_buffer );
905 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
908 sout_stream_sys_t *p_sys = p_stream->p_sys;
910 int64_t i_dts = p_buffer->i_dts;
911 uint32_t i_timestamp = i_dts * 9 / 100;
913 uint8_t *p_data = p_buffer->p_buffer;
914 unsigned int i_data = p_buffer->i_buffer;
915 unsigned int i_max = p_sys->i_mtu - 12;
917 int i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
923 /* output complete packet */
925 p_sys->packet->i_buffer + i_data > i_max )
927 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
928 p_sys->packet = NULL;
931 if( p_sys->packet == NULL )
933 /* allocate a new packet */
934 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
935 p_sys->packet->p_buffer[ 0] = 0x80;
936 p_sys->packet->p_buffer[ 1] = 0x80|p_sys->i_payload_type;
937 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
938 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
939 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
940 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
941 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
942 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
943 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
944 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
945 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
946 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
947 p_sys->packet->i_buffer = 12;
949 p_sys->packet->i_dts = i_dts;
950 p_sys->packet->i_length = p_buffer->i_length / i_packet;
951 i_dts += p_sys->packet->i_length;
956 i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_buffer );
958 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
961 p_sys->packet->i_buffer += i_size;
969 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
972 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
974 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
980 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
982 p_next = p_buffer->p_next;
983 block_Release( p_buffer );
990 /****************************************************************************
992 ****************************************************************************/
993 static int SapSetup( sout_stream_t *p_stream )
995 sout_stream_sys_t *p_sys = p_stream->p_sys;
996 sout_instance_t *p_sout = p_stream->p_sout;
997 announce_method_t *p_method = (announce_method_t *)
998 malloc(sizeof(announce_method_t));
1000 /* Remove the previous session */
1001 if( p_sys->p_session != NULL)
1003 sout_AnnounceUnRegister( p_sout, p_sys->p_session);
1004 sout_AnnounceSessionDestroy( p_sys->p_session );
1005 p_sys->p_session = NULL;
1007 p_method->i_type = METHOD_TYPE_SAP;
1008 p_method->psz_address = NULL; /* FIXME */
1009 p_method->i_ip_version = 4; /* FIXME ! */
1011 if( p_sys->i_es > 0 && p_sys->psz_sdp && *p_sys->psz_sdp )
1013 p_sys->p_session = sout_AnnounceRegisterSDP( p_sout, p_sys->psz_sdp,
1021 /****************************************************************************
1023 ****************************************************************************/
1024 static int HttpCallback( httpd_file_sys_t *p_args,
1025 httpd_file_t *, uint8_t *p_request,
1026 uint8_t **pp_data, int *pi_data );
1028 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
1030 sout_stream_sys_t *p_sys = p_stream->p_sys;
1032 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
1033 if( p_sys->p_httpd_host )
1035 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
1036 url->psz_path ? url->psz_path : "/",
1039 HttpCallback, (void*)p_sys );
1041 if( p_sys->p_httpd_file == NULL )
1043 return VLC_EGENERIC;
1048 static int HttpCallback( httpd_file_sys_t *p_args,
1049 httpd_file_t *f, uint8_t *p_request,
1050 uint8_t **pp_data, int *pi_data )
1052 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
1054 vlc_mutex_lock( &p_sys->lock_sdp );
1055 if( p_sys->psz_sdp && *p_sys->psz_sdp )
1057 *pi_data = strlen( p_sys->psz_sdp );
1058 *pp_data = malloc( *pi_data );
1059 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
1066 vlc_mutex_unlock( &p_sys->lock_sdp );
1071 /****************************************************************************
1073 ****************************************************************************/
1074 static rtsp_client_t *RtspClientNew( sout_stream_t *p_stream, char *psz_session )
1076 rtsp_client_t *rtsp = malloc( sizeof( rtsp_client_t ));
1078 rtsp->psz_session = psz_session;
1080 rtsp->b_playing = VLC_FALSE;
1084 rtsp->access = NULL;
1086 TAB_APPEND( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1090 static rtsp_client_t *RtspClientGet( sout_stream_t *p_stream, char *psz_session )
1093 for( i = 0; i < p_stream->p_sys->i_rtsp; i++ )
1095 if( !strcmp( p_stream->p_sys->rtsp[i]->psz_session, psz_session ) )
1097 return p_stream->p_sys->rtsp[i];
1103 static void RtspClientDel( sout_stream_t *p_stream, rtsp_client_t *rtsp )
1106 TAB_REMOVE( p_stream->p_sys->i_rtsp, p_stream->p_sys->rtsp, rtsp );
1108 for( i = 0; i < rtsp->i_access; i++ )
1110 sout_AccessOutDelete( rtsp->access[i] );
1112 if( rtsp->id ) free( rtsp->id );
1113 if( rtsp->access ) free( rtsp->access );
1115 free( rtsp->psz_session );
1119 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
1121 sout_stream_sys_t *p_sys = p_stream->p_sys;
1123 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
1125 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
1126 if( p_sys->p_rtsp_host == NULL )
1128 return VLC_EGENERIC;
1131 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
1132 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
1133 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
1134 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
1136 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
1137 if( p_sys->p_rtsp_url == 0 )
1139 return VLC_EGENERIC;
1141 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
1142 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
1143 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
1144 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
1149 static int RtspCallback( httpd_callback_sys_t *p_args,
1151 httpd_message_t *answer, httpd_message_t *query )
1153 sout_stream_t *p_stream = (sout_stream_t*)p_args;
1154 sout_stream_sys_t *p_sys = p_stream->p_sys;
1155 char *psz_destination = p_sys->psz_destination;
1156 char *psz_session = NULL;
1158 if( answer == NULL || query == NULL )
1162 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1164 answer->i_proto = HTTPD_PROTO_RTSP;
1165 answer->i_version= query->i_version;
1166 answer->i_type = HTTPD_MSG_ANSWER;
1168 switch( query->i_type )
1170 case HTTPD_MSG_DESCRIBE:
1172 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
1174 answer->i_status = 200;
1175 answer->psz_status = strdup( "OK" );
1176 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
1178 answer->p_body = psz_sdp;
1179 answer->i_body = strlen( psz_sdp );
1183 case HTTPD_MSG_PLAY:
1185 rtsp_client_t *rtsp;
1186 /* for now only multicast so easy */
1187 answer->i_status = 200;
1188 answer->psz_status = strdup( "OK" );
1190 answer->p_body = NULL;
1192 psz_session = httpd_MsgGet( query, "Session" );
1193 rtsp = RtspClientGet( p_stream, psz_session );
1194 if( rtsp && !rtsp->b_playing )
1198 rtsp->b_playing = VLC_TRUE;
1200 vlc_mutex_lock( &p_sys->lock_es );
1201 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1203 sout_stream_id_t *id = rtsp->id[i_id];
1206 for( i = 0; i < p_sys->i_es; i++ )
1208 if( id == p_sys->es[i] )
1211 if( i >= p_sys->i_es ) continue;
1213 vlc_mutex_lock( &id->lock_rtsp );
1214 TAB_APPEND( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1215 vlc_mutex_unlock( &id->lock_rtsp );
1217 vlc_mutex_unlock( &p_sys->lock_es );
1221 case HTTPD_MSG_PAUSE:
1223 return VLC_EGENERIC;
1224 case HTTPD_MSG_TEARDOWN:
1226 rtsp_client_t *rtsp;
1228 /* for now only multicast so easy again */
1229 answer->i_status = 200;
1230 answer->psz_status = strdup( "OK" );
1232 answer->p_body = NULL;
1234 psz_session = httpd_MsgGet( query, "Session" );
1235 rtsp = RtspClientGet( p_stream, psz_session );
1240 vlc_mutex_lock( &p_sys->lock_es );
1241 for( i_id = 0; i_id < rtsp->i_id; i_id++ )
1243 sout_stream_id_t *id = rtsp->id[i_id];
1246 for( i = 0; i < p_sys->i_es; i++ )
1248 if( id == p_sys->es[i] )
1251 if( i >= p_sys->i_es ) continue;
1253 vlc_mutex_lock( &id->lock_rtsp );
1254 TAB_REMOVE( id->i_rtsp_access, id->rtsp_access, rtsp->access[i_id] );
1255 vlc_mutex_unlock( &id->lock_rtsp );
1257 vlc_mutex_unlock( &p_sys->lock_es );
1259 RtspClientDel( p_stream, rtsp );
1265 return VLC_EGENERIC;
1267 httpd_MsgAdd( answer, "Server", "VLC Server" );
1268 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1269 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1270 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1274 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1279 static int RtspCallbackId( httpd_callback_sys_t *p_args,
1281 httpd_message_t *answer, httpd_message_t *query )
1283 sout_stream_id_t *id = (sout_stream_id_t*)p_args;
1284 sout_stream_t *p_stream = id->p_stream;
1285 sout_stream_sys_t *p_sys = p_stream->p_sys;
1286 char *psz_session = NULL;
1288 if( answer == NULL || query == NULL )
1292 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
1294 answer->i_proto = HTTPD_PROTO_RTSP;
1295 answer->i_version= query->i_version;
1296 answer->i_type = HTTPD_MSG_ANSWER;
1298 switch( query->i_type )
1300 case HTTPD_MSG_SETUP:
1302 char *psz_transport = httpd_MsgGet( query, "Transport" );
1304 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
1306 if( strstr( psz_transport, "multicast" ) && id->psz_destination )
1308 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
1309 answer->i_status = 200;
1310 answer->psz_status = strdup( "OK" );
1312 answer->p_body = NULL;
1313 psz_session = httpd_MsgGet( query, "Session" );
1314 if( *psz_session == 0 )
1316 psz_session = malloc( 100 );
1317 sprintf( psz_session, "%d", rand() );
1319 httpd_MsgAdd( answer, "Transport",
1320 "RTP/AVP/UDP;destination=%s;port=%d-%d;ttl=%d",
1321 id->psz_destination, id->i_port,id->i_port+1, p_sys->i_ttl );
1323 else if( strstr( psz_transport, "unicast" ) && strstr( psz_transport, "client_port=" ) )
1325 int i_port = atoi( strstr( psz_transport, "client_port=" ) + strlen("client_port=") );
1326 char *ip = httpd_ClientIP( cl );
1328 char psz_access[100];
1331 sout_access_out_t *p_access;
1333 rtsp_client_t *rtsp = NULL;
1335 fprintf( stderr, "HTTPD_MSG_SETUP: unicast ip=%s port=%d\n",
1338 psz_session = httpd_MsgGet( query, "Session" );
1339 if( *psz_session == 0 )
1341 psz_session = malloc( 100 );
1342 sprintf( psz_session, "%d", rand() );
1344 rtsp = RtspClientNew( p_stream, psz_session );
1348 rtsp = RtspClientGet( p_stream, psz_session );
1351 /* FIXME right error code */
1352 answer->i_status = 400;
1353 answer->psz_status = strdup( "Unknown session id" );
1355 answer->p_body = NULL;
1360 /* first try to create the access out */
1361 if( p_sys->i_ttl > 0 )
1362 sprintf( psz_access, "udp{raw,ttl=%d}", p_sys->i_ttl );
1364 sprintf( psz_access, "udp{raw}" );
1365 sprintf( psz_url, "%s:%d", ip, i_port );
1366 if( ( p_access = sout_AccessOutNew( p_stream->p_sout, psz_access, psz_url ) ) == NULL )
1368 msg_Err( p_stream, "cannot create the access out for %s://%s",
1369 psz_access, psz_url );
1370 answer->i_status = 400;
1371 answer->psz_status = strdup( "Server internal error" );
1373 answer->p_body = NULL;
1377 TAB_APPEND( rtsp->i_id, rtsp->id, id );
1378 TAB_APPEND( rtsp->i_access, rtsp->access, p_access );
1380 answer->i_status = 200;
1381 answer->psz_status = strdup( "OK" );
1383 answer->p_body = NULL;
1385 httpd_MsgAdd( answer, "Transport",
1386 "RTP/AVP/UDP;client_port=%d-%d", i_port, i_port + 1 );
1388 else /* TODO strstr( psz_transport, "interleaved" ) ) */
1390 answer->i_status = 400;
1391 answer->psz_status = strdup( "Bad Request" );
1393 answer->p_body = NULL;
1399 return VLC_EGENERIC;
1401 httpd_MsgAdd( answer, "Server", "VLC Server" );
1402 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1403 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1404 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1408 httpd_MsgAdd( answer, "Session", "%s"/*;timeout=5*/, psz_session );
1413 /****************************************************************************
1415 ****************************************************************************/
1416 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1417 int b_marker, int64_t i_pts )
1419 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1421 out->p_buffer[0] = 0x80;
1422 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1423 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1424 out->p_buffer[3] = ( id->i_sequence )&0xff;
1425 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1426 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1427 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1428 out->p_buffer[7] = ( i_timestamp )&0xff;
1430 out->p_buffer[ 8] = id->ssrc[0];
1431 out->p_buffer[ 9] = id->ssrc[1];
1432 out->p_buffer[10] = id->ssrc[2];
1433 out->p_buffer[11] = id->ssrc[3];
1439 static void rtp_packetize_send( sout_stream_id_t *id, block_t *out )
1442 vlc_mutex_lock( &id->lock_rtsp );
1443 for( i = 0; i < id->i_rtsp_access; i++ )
1445 sout_AccessOutWrite( id->rtsp_access[i], block_Duplicate( out ) );
1447 vlc_mutex_unlock( &id->lock_rtsp );
1451 sout_AccessOutWrite( id->p_access, out );
1455 block_Release( out );
1459 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1462 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1463 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1465 uint8_t *p_data = in->p_buffer;
1466 int i_data = in->i_buffer;
1469 for( i = 0; i < i_count; i++ )
1471 int i_payload = __MIN( i_max, i_data );
1472 block_t *out = block_New( p_stream, 16 + i_payload );
1474 /* rtp common header */
1475 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1477 out->p_buffer[12] = 0;
1478 out->p_buffer[13] = 0;
1479 /* fragment offset in the current frame */
1480 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1481 out->p_buffer[15] = ( (i*i_max) )&0xff;
1482 memcpy( &out->p_buffer[16], p_data, i_payload );
1484 out->i_buffer = 16 + i_payload;
1485 out->i_dts = in->i_dts + i * in->i_length / i_count;
1486 out->i_length = in->i_length / i_count;
1488 rtp_packetize_send( id, out );
1490 p_data += i_payload;
1491 i_data -= i_payload;
1498 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1501 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1502 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1504 uint8_t *p_data = in->p_buffer;
1505 int i_data = in->i_buffer;
1507 int b_sequence_start = 0;
1508 int i_temporal_ref = 0;
1509 int i_picture_coding_type = 0;
1510 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1511 int b_start_slice = 0;
1513 /* preparse this packet to get some info */
1514 if( in->i_buffer > 4 )
1516 uint8_t *p = p_data;
1517 int i_rest = in->i_buffer;
1521 while( i_rest > 4 &&
1522 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1536 /* sequence start code */
1537 b_sequence_start = 1;
1539 else if( *p == 0x00 && i_rest >= 4 )
1542 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1543 i_picture_coding_type = (p[2] >> 3)&0x07;
1545 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1546 i_picture_coding_type == 3 ) )
1548 i_ffv = (p[3] >> 2)&0x01;
1549 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1550 if( i_rest > 4 && i_picture_coding_type == 3 )
1552 i_fbv = (p[4]>>6)&0x01;
1553 i_bfc = (p[4]>>3)&0x07;
1557 else if( *p <= 0xaf )
1564 for( i = 0; i < i_count; i++ )
1566 int i_payload = __MIN( i_max, i_data );
1567 block_t *out = block_New( p_stream,
1569 uint32_t h = ( i_temporal_ref << 16 )|
1570 ( b_sequence_start << 13 )|
1571 ( b_start_slice << 12 )|
1572 ( i == i_count - 1 ? 1 << 11 : 0 )|
1573 ( i_picture_coding_type << 8 )|
1574 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1576 /* rtp common header */
1577 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1578 in->i_pts > 0 ? in->i_pts : in->i_dts );
1580 /* 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 */
1581 out->p_buffer[12] = ( h >> 24 )&0xff;
1582 out->p_buffer[13] = ( h >> 16 )&0xff;
1583 out->p_buffer[14] = ( h >> 8 )&0xff;
1584 out->p_buffer[15] = ( h )&0xff;
1586 memcpy( &out->p_buffer[16], p_data, i_payload );
1588 out->i_buffer = 16 + i_payload;
1589 out->i_dts = in->i_dts + i * in->i_length / i_count;
1590 out->i_length = in->i_length / i_count;
1592 rtp_packetize_send( id, out );
1594 p_data += i_payload;
1595 i_data -= i_payload;
1600 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1603 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1604 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1606 uint8_t *p_data = in->p_buffer;
1607 int i_data = in->i_buffer;
1610 for( i = 0; i < i_count; i++ )
1612 int i_payload = __MIN( i_max, i_data );
1613 block_t *out = block_New( p_stream, 14 + i_payload );
1615 /* rtp common header */
1616 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1618 out->p_buffer[12] = 1;
1620 out->p_buffer[13] = 0x00;
1622 memcpy( &out->p_buffer[14], p_data, i_payload );
1624 out->i_buffer = 14 + i_payload;
1625 out->i_dts = in->i_dts + i * in->i_length / i_count;
1626 out->i_length = in->i_length / i_count;
1628 rtp_packetize_send( id, out );
1630 p_data += i_payload;
1631 i_data -= i_payload;
1637 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1640 int i_max = id->i_mtu - 12; /* payload max in one packet */
1641 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1643 uint8_t *p_data = in->p_buffer;
1644 int i_data = in->i_buffer;
1647 for( i = 0; i < i_count; i++ )
1649 int i_payload = __MIN( i_max, i_data );
1650 block_t *out = block_New( p_stream, 12 + i_payload );
1652 /* rtp common header */
1653 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1654 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1655 memcpy( &out->p_buffer[12], p_data, i_payload );
1657 out->i_buffer = 12 + i_payload;
1658 out->i_dts = in->i_dts + i * in->i_length / i_count;
1659 out->i_length = in->i_length / i_count;
1661 rtp_packetize_send( id, out );
1663 p_data += i_payload;
1664 i_data -= i_payload;
1670 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1673 int i_max = id->i_mtu - 12; /* payload max in one packet */
1674 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1676 uint8_t *p_data = in->p_buffer;
1677 int i_data = in->i_buffer;
1682 int i_payload = (__MIN( i_max, i_data )/4)*4;
1683 block_t *out = block_New( p_stream, 12 + i_payload );
1685 /* rtp common header */
1686 rtp_packetize_common( id, out, 0,
1687 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1688 memcpy( &out->p_buffer[12], p_data, i_payload );
1690 out->i_buffer = 12 + i_payload;
1691 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1692 out->i_length = in->i_length / i_count;
1694 rtp_packetize_send( id, out );
1696 p_data += i_payload;
1697 i_data -= i_payload;
1704 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1707 int i_max = id->i_mtu - 12; /* payload max in one packet */
1708 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1710 uint8_t *p_data = in->p_buffer;
1711 int i_data = in->i_buffer;
1716 int i_payload = (__MIN( i_max, i_data )/2)*2;
1717 block_t *out = block_New( p_stream, 12 + i_payload );
1719 /* rtp common header */
1720 rtp_packetize_common( id, out, 0,
1721 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1722 memcpy( &out->p_buffer[12], p_data, i_payload );
1724 out->i_buffer = 12 + i_payload;
1725 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1726 out->i_length = in->i_length / i_count;
1728 rtp_packetize_send( id, out );
1730 p_data += i_payload;
1731 i_data -= i_payload;
1737 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1740 int i_max = id->i_mtu - 16; /* payload max in one packet */
1741 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1743 uint8_t *p_data = in->p_buffer;
1744 int i_data = in->i_buffer;
1747 for( i = 0; i < i_count; i++ )
1749 int i_payload = __MIN( i_max, i_data );
1750 block_t *out = block_New( p_stream, 16 + i_payload );
1752 /* rtp common header */
1753 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1754 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1756 /* AU headers length (bits) */
1757 out->p_buffer[12] = 0;
1758 out->p_buffer[13] = 2*8;
1759 /* for each AU length 13 bits + idx 3bits, */
1760 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1761 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1763 memcpy( &out->p_buffer[16], p_data, i_payload );
1765 out->i_buffer = 16 + i_payload;
1766 out->i_dts = in->i_dts + i * in->i_length / i_count;
1767 out->i_length = in->i_length / i_count;
1769 rtp_packetize_send( id, out );
1771 p_data += i_payload;
1772 i_data -= i_payload;