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 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
43 set_description( _("RTP stream output") );
44 set_capability( "sout stream", 0 );
45 add_shortcut( "rtp" );
46 set_callbacks( Open, Close );
49 /*****************************************************************************
51 *****************************************************************************/
52 static sout_stream_id_t *Add ( sout_stream_t *, es_format_t * );
53 static int Del ( sout_stream_t *, sout_stream_id_t * );
54 static int Send( sout_stream_t *, sout_stream_id_t *,
57 struct sout_stream_sys_t
66 httpd_host_t *p_httpd_host;
67 httpd_file_t *p_httpd_file;
69 httpd_host_t *p_rtsp_host;
70 httpd_url_t *p_rtsp_url;
71 char *psz_rtsp_control;
75 char *psz_destination;
79 /* when need to use a private one or when using muxer */
82 /* in case we do TS/PS over rtp */
84 sout_access_out_t *p_access;
86 sout_access_out_t *p_grab;
88 uint32_t i_timestamp_start;
94 sout_stream_id_t **es;
97 typedef int (*pf_rtp_packetizer_t)( sout_stream_t *, sout_stream_id_t *,
100 struct sout_stream_id_t
103 uint8_t i_payload_type;
105 uint32_t i_timestamp_start;
112 char *psz_destination;
116 /* Packetizer specific fields */
117 pf_rtp_packetizer_t pf_packetize;
120 /* for sending the packets */
121 sout_access_out_t *p_access;
122 sout_input_t *p_input;
124 /* RTSP url control */
125 httpd_url_t *p_rtsp_url;
128 static int AccessOutGrabberWrite( sout_access_out_t *, block_t * );
130 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t * );
131 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t * );
139 sout_access_out_t **p_access;
143 static rtsp_session_t *RtspSessionNew ( sout_stream_t *p_stream );
144 static rtsp_session_t *RtspSessionFind ( sout_stream_t *p_stream, char *psz_session );
145 static void RtspSessionDelete( sout_stream_t *p_stream, rtsp_session_t *sess );
148 static int RtspCallback( httpd_callback_sys_t *, httpd_client_t *,
149 httpd_message_t *, httpd_message_t * );
152 /*****************************************************************************
154 *****************************************************************************/
155 static int Open( vlc_object_t *p_this )
157 sout_stream_t *p_stream = (sout_stream_t*)p_this;
158 sout_instance_t *p_sout = p_stream->p_sout;
159 sout_stream_sys_t *p_sys;
163 p_sys = malloc( sizeof( sout_stream_sys_t ) );
164 p_sys->psz_destination = sout_cfg_find_value( p_stream->p_cfg, "dst" );
165 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "port" ) ) )
167 p_sys->i_port = atoi( val );
169 if( !p_sys->psz_destination || *p_sys->psz_destination == '\0' ||
172 msg_Err( p_stream, "invalid/missing dst or port" );
176 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "ttl" ) ) )
178 p_sys->i_ttl = atoi( val );
182 p_sys->i_ttl = config_GetInt( p_stream, "ttl" );
185 p_sys->i_payload_type = 96;
188 p_sys->psz_sdp = NULL;
190 p_sys->i_sdp_id = mdate();
191 p_sys->i_sdp_version = 1;
192 p_sys->psz_sdp = NULL;
193 p_sys->p_httpd_host = NULL;
194 p_sys->p_httpd_file = NULL;
195 p_sys->p_rtsp_host = NULL;
196 p_sys->p_rtsp_url = NULL;
197 p_sys->psz_rtsp_control = NULL;
198 p_sys->psz_rtsp_path = NULL;
200 vlc_mutex_init( p_stream, &p_sys->lock_sdp );
202 p_stream->pf_add = Add;
203 p_stream->pf_del = Del;
204 p_stream->pf_send = Send;
206 p_stream->p_sys = p_sys;
208 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "mux" ) ) )
210 sout_access_out_t *p_grab;
214 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
216 /* Check muxer type */
217 if( !strncasecmp( val, "ps", 2 ) || !strncasecmp( val, "mpeg1", 5 ) )
219 psz_rtpmap = "MP2P/90000";
221 else if( !strncasecmp( val, "ts", 2 ) )
223 psz_rtpmap = "MP2T/90000";
224 p_sys->i_payload_type = 33;
228 msg_Err( p_stream, "unsupported muxer type with rtp (only ts/ps)" );
232 /* create the access out */
233 if( p_sys->i_ttl > 0 )
235 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
239 sprintf( access, "udp{raw}" );
241 sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
242 if( !( p_sys->p_access = sout_AccessOutNew( p_sout, access, url ) ) )
244 msg_Err( p_stream, "cannot create the access out for %s://%s",
249 p_sys->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beurk */
250 if( p_sys->i_mtu <= 16 )
252 /* better than nothing */
256 /* the access out grabber TODO export it as sout_AccessOutGrabberNew */
257 p_grab = p_sys->p_grab =
258 vlc_object_create( p_sout, sizeof( sout_access_out_t ) );
259 p_grab->p_module = NULL;
260 p_grab->p_sout = p_sout;
261 p_grab->psz_access = strdup( "grab" );
262 p_grab->p_cfg = NULL;
263 p_grab->psz_name = strdup( "" );
264 p_grab->p_sys = (sout_access_out_sys_t*)p_stream;
265 p_grab->pf_seek = NULL;
266 p_grab->pf_write = AccessOutGrabberWrite;
269 if( !( p_sys->p_mux = sout_MuxNew( p_sout, val, p_sys->p_grab ) ) )
271 msg_Err( p_stream, "cannot create the muxer (%s)", val );
272 sout_AccessOutDelete( p_sys->p_grab );
273 sout_AccessOutDelete( p_sys->p_access );
278 /* create the SDP only once */
280 malloc( 200 + 20 + 10 + strlen( p_sys->psz_destination ) +
281 10 + 10 + 10 + 10 + strlen( psz_rtpmap ) );
282 sprintf( p_sys->psz_sdp,
284 "o=- "I64Fd" %d IN IP4 127.0.0.1\n"
287 "m=video %d RTP/AVP %d\n"
289 p_sys->i_sdp_id, p_sys->i_sdp_version,
290 p_sys->psz_destination, p_sys->i_ttl,
291 p_sys->i_port, p_sys->i_payload_type,
292 p_sys->i_payload_type, psz_rtpmap );
294 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
296 /* create the rtp context */
297 p_sys->ssrc[0] = rand()&0xff;
298 p_sys->ssrc[1] = rand()&0xff;
299 p_sys->ssrc[2] = rand()&0xff;
300 p_sys->ssrc[3] = rand()&0xff;
301 p_sys->i_sequence = rand()&0xffff;
302 p_sys->i_timestamp_start = rand()&0xffffffff;
303 p_sys->packet = NULL;
308 p_sys->p_access = NULL;
309 p_sys->p_grab = NULL;
312 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "sdp" ) ) )
316 vlc_UrlParse( &url, val, 0 );
317 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
319 if( HttpSetup( p_stream, &url ) )
321 msg_Err( p_stream, "cannot export sdp as http" );
324 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
326 if( RtspSetup( p_stream, &url ) )
328 msg_Err( p_stream, "cannot export sdp as rtsp" );
333 msg_Warn( p_stream, "unknow protocol for SDP (%s)",
336 vlc_UrlClean( &url );
339 /* update p_sout->i_out_pace_nocontrol */
340 p_stream->p_sout->i_out_pace_nocontrol++;
345 /*****************************************************************************
347 *****************************************************************************/
348 static void Close( vlc_object_t * p_this )
350 sout_stream_t *p_stream = (sout_stream_t*)p_this;
351 sout_stream_sys_t *p_sys = p_stream->p_sys;
353 /* update p_sout->i_out_pace_nocontrol */
354 p_stream->p_sout->i_out_pace_nocontrol--;
358 sout_MuxDelete( p_sys->p_mux );
359 sout_AccessOutDelete( p_sys->p_access );
360 sout_AccessOutDelete( p_sys->p_grab );
363 block_Release( p_sys->packet );
367 vlc_mutex_destroy( &p_sys->lock_sdp );
369 if( p_sys->p_httpd_file )
371 httpd_FileDelete( p_sys->p_httpd_file );
373 if( p_sys->p_httpd_host )
375 httpd_HostDelete( p_sys->p_httpd_host );
377 if( p_sys->p_rtsp_url )
379 httpd_UrlDelete( p_sys->p_rtsp_url );
381 if( p_sys->p_rtsp_host )
383 httpd_HostDelete( p_sys->p_rtsp_host );
388 free( p_sys->psz_sdp );
393 /*****************************************************************************
395 *****************************************************************************/
396 static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
398 sout_stream_sys_t *p_sys = p_stream->p_sys;
403 i_size = strlen( "v=0\n" ) +
404 strlen( "o=- * * IN IP4 127.0.0.1\n" ) +
405 strlen( "s=NONE\n" ) +
406 strlen( "c=IN IP4 */*\n" ) +
407 strlen( psz_destination ) +
409 for( i = 0; i < p_sys->i_es; i++ )
411 sout_stream_id_t *id = p_sys->es[i];
413 i_size += strlen( "m=**d*o * RTP/AVP *\n" ) + 10 + 10;
416 i_size += strlen( "a=rtpmap:* *\n" ) + strlen( id->psz_rtpmap )+10;
420 i_size += strlen( "a=fmtp:* *\n" ) + strlen( id->psz_fmtp ) + 10;
424 i_size += strlen( "a=control:*/trackid=*\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
428 p = psz_sdp = malloc( i_size );
429 p += sprintf( p, "v=0\n" );
430 p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\n",
431 p_sys->i_sdp_id, p_sys->i_sdp_version );
432 p += sprintf( p, "s=NONE\n" );
433 p += sprintf( p, "c=IN IP4 %s/%d\n", psz_destination,
436 for( i = 0; i < p_sys->i_es; i++ )
438 sout_stream_id_t *id = p_sys->es[i];
440 if( id->i_cat == AUDIO_ES )
442 p += sprintf( p, "m=audio %d RTP/AVP %d\n",
443 id->i_port, id->i_payload_type );
445 else if( id->i_cat == VIDEO_ES )
447 p += sprintf( p, "m=video %d RTP/AVP %d\n",
448 id->i_port, id->i_payload_type );
456 p += sprintf( p, "a=rtpmap:%d %s\n", id->i_payload_type,
461 p += sprintf( p, "a=fmtp:%d %s\n", id->i_payload_type,
466 p += sprintf( p, "a=control:%s/trackid=%d\n", p_sys->psz_rtsp_control, i );
473 /*****************************************************************************
475 *****************************************************************************/
476 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, block_t * );
477 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, block_t * );
478 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, block_t * );
479 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, block_t * );
480 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, block_t * );
481 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, block_t * );
482 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, block_t * );
484 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
486 static const char hex[16] = "0123456789abcdef";
489 for( i = 0; i < i_data; i++ )
491 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
492 s[2*i+1] = hex[(p_data[i] )&0xf];
497 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
499 sout_instance_t *p_sout = p_stream->p_sout;
500 sout_stream_sys_t *p_sys = p_stream->p_sys;
501 sout_stream_id_t *id;
502 sout_access_out_t *p_access = NULL;
505 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
507 if( p_sys->p_mux != NULL )
509 sout_input_t *p_input = NULL;
510 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
512 msg_Err( p_stream, "cannot add this stream to the muxer" );
516 id = malloc( sizeof( sout_stream_id_t ) );
518 id->p_input = p_input;
519 id->pf_packetize= NULL;
524 /* first try to create the access out */
525 if( p_sys->i_ttl > 0 )
527 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
531 sprintf( access, "udp{raw}" );
533 sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
534 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
536 msg_Err( p_stream, "cannot create the access out for %s://%s",
541 /* not create the rtp specific stuff */
542 id = malloc( sizeof( sout_stream_id_t ) );
543 id->p_access = p_access;
545 id->psz_rtpmap = NULL;
547 id->psz_destination = strdup( p_sys->psz_destination );
548 id->i_port = p_sys->i_port;
549 id->p_rtsp_url = NULL;
551 switch( p_fmt->i_codec )
553 case VLC_FOURCC( 's', '1', '6', 'b' ):
554 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
556 id->i_payload_type = 11;
558 else if( p_fmt->audio.i_channels == 2 &&
559 p_fmt->audio.i_rate == 44100 )
561 id->i_payload_type = 10;
565 id->i_payload_type = p_sys->i_payload_type++;
567 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
568 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
569 p_fmt->audio.i_channels );
570 id->i_clock_rate = p_fmt->audio.i_rate;
571 id->pf_packetize = rtp_packetize_l16;
573 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
574 id->i_payload_type = p_sys->i_payload_type++;
575 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
576 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
577 p_fmt->audio.i_channels );
578 id->i_clock_rate = p_fmt->audio.i_rate;
579 id->pf_packetize = rtp_packetize_l8;
581 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
582 id->i_payload_type = 14;
583 id->i_clock_rate = 90000;
584 id->psz_rtpmap = strdup( "MPA/90000" );
585 id->pf_packetize = rtp_packetize_mpa;
587 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
588 id->i_payload_type = 32;
589 id->i_clock_rate = 90000;
590 id->psz_rtpmap = strdup( "MPV/90000" );
591 id->pf_packetize = rtp_packetize_mpv;
593 case VLC_FOURCC( 'a', '5', '2', ' ' ):
594 id->i_payload_type = p_sys->i_payload_type++;
595 id->i_clock_rate = 90000;
596 id->psz_rtpmap = strdup( "ac3/90000" );
597 id->pf_packetize = rtp_packetize_ac3;
599 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
601 char hexa[2*p_fmt->i_extra +1];
603 id->i_payload_type = p_sys->i_payload_type++;
604 id->i_clock_rate = 90000;
605 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
606 id->pf_packetize = rtp_packetize_split;
607 if( p_fmt->i_extra > 0 )
609 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
610 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
611 sprintf( id->psz_fmtp,
612 "profile-level-id=3; config=%s;", hexa );
616 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
618 char hexa[2*p_fmt->i_extra +1];
620 id->i_payload_type = p_sys->i_payload_type++;
621 id->i_clock_rate = p_fmt->audio.i_rate;
622 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
623 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
624 id->pf_packetize = rtp_packetize_mp4a;
625 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
626 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
627 sprintf( id->psz_fmtp,
628 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
629 "config=%s; SizeLength=13;IndexLength=3; "
630 "IndexDeltaLength=3; Profile=1;", hexa );
635 msg_Err( p_stream, "cannot add this stream (unsupported "
636 "codec:%4.4s)", (char*)&p_fmt->i_codec );
640 id->i_cat = p_fmt->i_cat;
642 id->ssrc[0] = rand()&0xff;
643 id->ssrc[1] = rand()&0xff;
644 id->ssrc[2] = rand()&0xff;
645 id->ssrc[3] = rand()&0xff;
646 id->i_sequence = rand()&0xffff;
647 id->i_timestamp_start = rand()&0xffffffff;
649 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
650 if( id->i_mtu <= 16 )
652 /* better than nothing */
655 msg_Dbg( p_stream, "access out %s:%s mtu=%d", access, url, id->i_mtu );
657 if( p_sys->p_rtsp_url )
659 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
661 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
662 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
663 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
667 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
668 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
669 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
670 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
675 /* Update p_sys context */
676 /* update port used (2 -> 1 rtp, 1 rtcp )*/
677 TAB_APPEND( p_sys->i_es, p_sys->es, id );
678 if( p_sys->p_mux == NULL )
682 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
684 vlc_mutex_lock( &p_sys->lock_sdp );
685 free( p_sys->psz_sdp );
686 p_sys->psz_sdp = psz_sdp;
687 vlc_mutex_unlock( &p_sys->lock_sdp );
689 p_sys->i_sdp_version++;
691 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
697 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
699 sout_stream_sys_t *p_sys = p_stream->p_sys;
701 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
707 free( id->psz_rtpmap );
711 free( id->psz_fmtp );
713 free( id->psz_destination );
714 sout_AccessOutDelete( id->p_access );
716 else if( id->p_input )
718 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
722 httpd_UrlDelete( id->p_rtsp_url );
728 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
733 if( p_stream->p_sys->p_mux )
735 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
741 p_next = p_buffer->p_next;
742 if( id->pf_packetize( p_stream, id, p_buffer ) )
746 block_Release( p_buffer );
754 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
757 sout_stream_sys_t *p_sys = p_stream->p_sys;
759 int64_t i_dts = p_buffer->i_dts;
760 uint32_t i_timestamp = i_dts * 9 / 100;
762 uint8_t *p_data = p_buffer->p_buffer;
763 unsigned int i_data = p_buffer->i_buffer;
764 unsigned int i_max = p_sys->i_mtu - 12;
766 int i_packet = ( p_buffer->i_buffer + i_max - 1 ) / i_max;
772 /* output complete packet */
774 p_sys->packet->i_buffer + i_data > i_max )
776 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
777 p_sys->packet = NULL;
780 if( p_sys->packet == NULL )
782 /* allocate a new packet */
783 p_sys->packet = block_New( p_stream, p_sys->i_mtu );
784 p_sys->packet->p_buffer[ 0] = 0x80;
785 p_sys->packet->p_buffer[ 1] = p_sys->i_payload_type;
786 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
787 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
788 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
789 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
790 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
791 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
792 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
793 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
794 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
795 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
796 p_sys->packet->i_buffer = 12;
798 p_sys->packet->i_dts = i_dts;
799 p_sys->packet->i_length = p_buffer->i_length / i_packet;
800 i_dts += p_sys->packet->i_length;
805 i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_buffer );
807 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_buffer],
810 p_sys->packet->i_buffer += i_size;
818 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
821 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
823 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_buffer );
829 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
831 p_next = p_buffer->p_next;
832 block_Release( p_buffer );
839 /****************************************************************************
841 ****************************************************************************/
842 static int HttpCallback( httpd_file_sys_t *p_args,
843 httpd_file_t *, uint8_t *p_request,
844 uint8_t **pp_data, int *pi_data );
846 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
848 sout_stream_sys_t *p_sys = p_stream->p_sys;
850 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
851 if( p_sys->p_httpd_host )
853 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
854 url->psz_path ? url->psz_path : "/",
857 HttpCallback, (void*)p_sys );
859 if( p_sys->p_httpd_file == NULL )
866 static int HttpCallback( httpd_file_sys_t *p_args,
867 httpd_file_t *f, uint8_t *p_request,
868 uint8_t **pp_data, int *pi_data )
870 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
872 vlc_mutex_lock( &p_sys->lock_sdp );
873 if( p_sys->psz_sdp && *p_sys->psz_sdp )
875 *pi_data = strlen( p_sys->psz_sdp );
876 *pp_data = malloc( *pi_data );
877 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
884 vlc_mutex_unlock( &p_sys->lock_sdp );
889 /****************************************************************************
891 ****************************************************************************/
892 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
894 sout_stream_sys_t *p_sys = p_stream->p_sys;
896 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
898 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
899 if( p_sys->p_rtsp_host == NULL )
904 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
905 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
906 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
907 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
909 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
910 if( p_sys->p_rtsp_url == 0 )
914 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
915 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
916 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
917 /* httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream ); */
918 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
923 static int RtspCallback( httpd_callback_sys_t *p_args,
925 httpd_message_t *answer, httpd_message_t *query )
927 sout_stream_t *p_stream = (sout_stream_t*)p_args;
928 char *psz_destination = p_stream->p_sys->psz_destination;
929 char *psz_session = NULL;
931 if( psz_destination && *psz_destination == 0 )
933 psz_destination = NULL;
936 if( answer == NULL || query == NULL )
940 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
942 answer->i_proto = HTTPD_PROTO_RTSP;
943 answer->i_version= query->i_version;
944 answer->i_type = HTTPD_MSG_ANSWER;
946 switch( query->i_type )
948 case HTTPD_MSG_DESCRIBE:
950 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
952 answer->i_status = 200;
953 answer->psz_status = strdup( "OK" );
954 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
956 answer->p_body = psz_sdp;
957 answer->i_body = strlen( psz_sdp );
961 case HTTPD_MSG_SETUP:
963 char *psz_transport = httpd_MsgGet( query, "Transport" );
965 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
966 if( strstr( psz_transport, "multicast" ) )
968 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
969 answer->i_status = 200;
970 answer->psz_status = strdup( "OK" );
972 answer->p_body = NULL;
973 psz_session = httpd_MsgGet( query, "Session" );
974 if( *psz_session == 0 )
976 psz_session = malloc( 100 );
977 sprintf( psz_session, "%d", rand() );
980 else /*if( strstr( psz_transport, "unicast" ) ||
981 strstr( psz_transport, "interleaved" ) ) */
983 answer->i_status = 400;
984 answer->psz_status = strdup( "Bad Request" );
986 answer->p_body = NULL;
992 /* for now only multicast so easy */
993 answer->i_status = 200;
994 answer->psz_status = strdup( "OK" );
996 answer->p_body = NULL;
998 psz_session = httpd_MsgGet( query, "Session" );
1001 case HTTPD_MSG_PAUSE:
1003 return VLC_EGENERIC;
1004 case HTTPD_MSG_TEARDOWN:
1005 /* for now only multicast so easy again */
1006 answer->i_status = 200;
1007 answer->psz_status = strdup( "OK" );
1009 answer->p_body = NULL;
1011 psz_session = httpd_MsgGet( query, "Session" );
1015 return VLC_EGENERIC;
1017 httpd_MsgAdd( answer, "Server", "VLC Server" );
1018 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1019 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1020 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1024 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1029 /****************************************************************************
1031 ****************************************************************************/
1032 static void rtp_packetize_common( sout_stream_id_t *id, block_t *out,
1033 int b_marker, int64_t i_pts )
1035 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1037 out->p_buffer[0] = 0x80;
1038 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1039 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1040 out->p_buffer[3] = ( id->i_sequence )&0xff;
1041 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1042 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1043 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1044 out->p_buffer[7] = ( i_timestamp )&0xff;
1046 out->p_buffer[ 8] = id->ssrc[0];
1047 out->p_buffer[ 9] = id->ssrc[1];
1048 out->p_buffer[10] = id->ssrc[2];
1049 out->p_buffer[11] = id->ssrc[3];
1055 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1058 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1059 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1061 uint8_t *p_data = in->p_buffer;
1062 int i_data = in->i_buffer;
1065 for( i = 0; i < i_count; i++ )
1067 int i_payload = __MIN( i_max, i_data );
1068 block_t *out = block_New( p_stream, 16 + i_payload );
1070 /* rtp common header */
1071 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1073 out->p_buffer[12] = 0;
1074 out->p_buffer[13] = 0;
1075 /* fragment offset in the current frame */
1076 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1077 out->p_buffer[15] = ( (i*i_max) )&0xff;
1078 memcpy( &out->p_buffer[16], p_data, i_payload );
1080 out->i_buffer = 16 + i_payload;
1081 out->i_dts = in->i_dts + i * in->i_length / i_count;
1082 out->i_length = in->i_length / i_count;
1084 sout_AccessOutWrite( id->p_access, out );
1086 p_data += i_payload;
1087 i_data -= i_payload;
1094 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1097 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1098 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1100 uint8_t *p_data = in->p_buffer;
1101 int i_data = in->i_buffer;
1103 int b_sequence_start = 0;
1104 int i_temporal_ref = 0;
1105 int i_picture_coding_type = 0;
1106 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1107 int b_start_slice = 0;
1109 /* preparse this packet to get some info */
1110 if( in->i_buffer > 4 )
1112 uint8_t *p = p_data;
1113 int i_rest = in->i_buffer;
1117 while( i_rest > 4 &&
1118 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1132 /* sequence start code */
1133 b_sequence_start = 1;
1135 else if( *p == 0x00 && i_rest >= 4 )
1138 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1139 i_picture_coding_type = (p[2] >> 3)&0x07;
1141 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1142 i_picture_coding_type == 3 ) )
1144 i_ffv = (p[3] >> 2)&0x01;
1145 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1146 if( i_rest > 4 && i_picture_coding_type == 3 )
1148 i_fbv = (p[4]>>6)&0x01;
1149 i_bfc = (p[4]>>3)&0x07;
1153 else if( *p <= 0xaf )
1160 for( i = 0; i < i_count; i++ )
1162 int i_payload = __MIN( i_max, i_data );
1163 block_t *out = block_New( p_stream,
1165 uint32_t h = ( i_temporal_ref << 16 )|
1166 ( b_sequence_start << 13 )|
1167 ( b_start_slice << 12 )|
1168 ( i == i_count - 1 ? 1 << 11 : 0 )|
1169 ( i_picture_coding_type << 8 )|
1170 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1172 /* rtp common header */
1173 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1174 in->i_pts > 0 ? in->i_pts : in->i_dts );
1176 /* 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 */
1177 out->p_buffer[12] = ( h >> 24 )&0xff;
1178 out->p_buffer[13] = ( h >> 16 )&0xff;
1179 out->p_buffer[14] = ( h >> 8 )&0xff;
1180 out->p_buffer[15] = ( h )&0xff;
1182 memcpy( &out->p_buffer[16], p_data, i_payload );
1184 out->i_buffer = 16 + i_payload;
1185 out->i_dts = in->i_dts + i * in->i_length / i_count;
1186 out->i_length = in->i_length / i_count;
1188 sout_AccessOutWrite( id->p_access, out );
1190 p_data += i_payload;
1191 i_data -= i_payload;
1196 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1199 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1200 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1202 uint8_t *p_data = in->p_buffer;
1203 int i_data = in->i_buffer;
1206 for( i = 0; i < i_count; i++ )
1208 int i_payload = __MIN( i_max, i_data );
1209 block_t *out = block_New( p_stream, 14 + i_payload );
1211 /* rtp common header */
1212 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1214 out->p_buffer[12] = 1;
1216 out->p_buffer[13] = 0x00;
1218 memcpy( &out->p_buffer[14], p_data, i_payload );
1220 out->i_buffer = 14 + i_payload;
1221 out->i_dts = in->i_dts + i * in->i_length / i_count;
1222 out->i_length = in->i_length / i_count;
1224 sout_AccessOutWrite( id->p_access, out );
1226 p_data += i_payload;
1227 i_data -= i_payload;
1233 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1236 int i_max = id->i_mtu - 12; /* payload max in one packet */
1237 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1239 uint8_t *p_data = in->p_buffer;
1240 int i_data = in->i_buffer;
1243 for( i = 0; i < i_count; i++ )
1245 int i_payload = __MIN( i_max, i_data );
1246 block_t *out = block_New( p_stream, 12 + i_payload );
1248 /* rtp common header */
1249 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1250 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1251 memcpy( &out->p_buffer[12], p_data, i_payload );
1253 out->i_buffer = 12 + i_payload;
1254 out->i_dts = in->i_dts + i * in->i_length / i_count;
1255 out->i_length = in->i_length / i_count;
1257 sout_AccessOutWrite( id->p_access, out );
1259 p_data += i_payload;
1260 i_data -= i_payload;
1266 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1269 int i_max = id->i_mtu - 12; /* payload max in one packet */
1270 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1272 uint8_t *p_data = in->p_buffer;
1273 int i_data = in->i_buffer;
1278 int i_payload = (__MIN( i_max, i_data )/4)*4;
1279 block_t *out = block_New( p_stream, 12 + i_payload );
1281 /* rtp common header */
1282 rtp_packetize_common( id, out, 0,
1283 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1284 memcpy( &out->p_buffer[12], p_data, i_payload );
1286 out->i_buffer = 12 + i_payload;
1287 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1288 out->i_length = in->i_length / i_count;
1290 sout_AccessOutWrite( id->p_access, out );
1292 p_data += i_payload;
1293 i_data -= i_payload;
1300 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1303 int i_max = id->i_mtu - 12; /* payload max in one packet */
1304 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1306 uint8_t *p_data = in->p_buffer;
1307 int i_data = in->i_buffer;
1312 int i_payload = (__MIN( i_max, i_data )/2)*2;
1313 block_t *out = block_New( p_stream, 12 + i_payload );
1315 /* rtp common header */
1316 rtp_packetize_common( id, out, 0,
1317 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1318 memcpy( &out->p_buffer[12], p_data, i_payload );
1320 out->i_buffer = 12 + i_payload;
1321 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1322 out->i_length = in->i_length / i_count;
1324 sout_AccessOutWrite( id->p_access, out );
1326 p_data += i_payload;
1327 i_data -= i_payload;
1333 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1336 int i_max = id->i_mtu - 16; /* payload max in one packet */
1337 int i_count = ( in->i_buffer + i_max - 1 ) / i_max;
1339 uint8_t *p_data = in->p_buffer;
1340 int i_data = in->i_buffer;
1343 for( i = 0; i < i_count; i++ )
1345 int i_payload = __MIN( i_max, i_data );
1346 block_t *out = block_New( p_stream, 16 + i_payload );
1348 /* rtp common header */
1349 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1350 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1352 /* AU headers length (bits) */
1353 out->p_buffer[12] = 0;
1354 out->p_buffer[13] = 2*8;
1355 /* for each AU length 13 bits + idx 3bits, */
1356 out->p_buffer[14] = ( in->i_buffer >> 5 )&0xff;
1357 out->p_buffer[15] = ( (in->i_buffer&0xff)<<3 )|0;
1359 memcpy( &out->p_buffer[16], p_data, i_payload );
1361 out->i_buffer = 16 + i_payload;
1362 out->i_dts = in->i_dts + i * in->i_length / i_count;
1363 out->i_length = in->i_length / i_count;
1365 sout_AccessOutWrite( id->p_access, out );
1367 p_data += i_payload;
1368 i_data -= i_payload;