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;
90 sout_buffer_t *packet;
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 *, sout_buffer_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 );
277 p_sout->i_preheader = __MAX( p_sout->i_preheader,
278 p_sys->p_mux->i_preheader );
280 /* create the SDP only once */
282 malloc( 200 + 20 + 10 + strlen( p_sys->psz_destination ) +
283 10 + 10 + 10 + 10 + strlen( psz_rtpmap ) );
284 sprintf( p_sys->psz_sdp,
286 "o=- "I64Fd" %d IN IP4 127.0.0.1\n"
289 "m=video %d RTP/AVP %d\n"
291 p_sys->i_sdp_id, p_sys->i_sdp_version,
292 p_sys->psz_destination, p_sys->i_ttl,
293 p_sys->i_port, p_sys->i_payload_type,
294 p_sys->i_payload_type, psz_rtpmap );
296 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
298 /* create the rtp context */
299 p_sys->ssrc[0] = rand()&0xff;
300 p_sys->ssrc[1] = rand()&0xff;
301 p_sys->ssrc[2] = rand()&0xff;
302 p_sys->ssrc[3] = rand()&0xff;
303 p_sys->i_sequence = rand()&0xffff;
304 p_sys->i_timestamp_start = rand()&0xffffffff;
305 p_sys->packet = NULL;
310 p_sys->p_access = NULL;
311 p_sys->p_grab = NULL;
314 if( ( val = sout_cfg_find_value( p_stream->p_cfg, "sdp" ) ) )
318 vlc_UrlParse( &url, val, 0 );
319 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "http" ) )
321 if( HttpSetup( p_stream, &url ) )
323 msg_Err( p_stream, "cannot export sdp as http" );
326 if( url.psz_protocol && !strcasecmp( url.psz_protocol, "rtsp" ) )
328 if( RtspSetup( p_stream, &url ) )
330 msg_Err( p_stream, "cannot export sdp as rtsp" );
335 msg_Warn( p_stream, "unknow protocol for SDP (%s)",
338 vlc_UrlClean( &url );
341 /* update p_sout->i_out_pace_nocontrol */
342 p_stream->p_sout->i_out_pace_nocontrol++;
347 /*****************************************************************************
349 *****************************************************************************/
350 static void Close( vlc_object_t * p_this )
352 sout_stream_t *p_stream = (sout_stream_t*)p_this;
353 sout_stream_sys_t *p_sys = p_stream->p_sys;
355 /* update p_sout->i_out_pace_nocontrol */
356 p_stream->p_sout->i_out_pace_nocontrol--;
360 sout_MuxDelete( p_sys->p_mux );
361 sout_AccessOutDelete( p_sys->p_access );
362 sout_AccessOutDelete( p_sys->p_grab );
365 sout_BufferDelete( p_stream->p_sout, p_sys->packet );
369 vlc_mutex_destroy( &p_sys->lock_sdp );
371 if( p_sys->p_httpd_file )
373 httpd_FileDelete( p_sys->p_httpd_file );
375 if( p_sys->p_httpd_host )
377 httpd_HostDelete( p_sys->p_httpd_host );
379 if( p_sys->p_rtsp_url )
381 httpd_UrlDelete( p_sys->p_rtsp_url );
383 if( p_sys->p_rtsp_host )
385 httpd_HostDelete( p_sys->p_rtsp_host );
390 free( p_sys->psz_sdp );
395 /*****************************************************************************
397 *****************************************************************************/
398 static char *SDPGenerate( sout_stream_t *p_stream, char *psz_destination, vlc_bool_t b_rtsp )
400 sout_stream_sys_t *p_sys = p_stream->p_sys;
405 i_size = strlen( "v=0\n" ) +
406 strlen( "o=- * * IN IP4 127.0.0.1\n" ) +
407 strlen( "s=NONE\n" ) +
408 strlen( "c=IN IP4 */*\n" ) +
409 strlen( psz_destination ) +
411 for( i = 0; i < p_sys->i_es; i++ )
413 sout_stream_id_t *id = p_sys->es[i];
415 i_size += strlen( "m=**d*o * RTP/AVP *\n" ) + 10 + 10;
418 i_size += strlen( "a=rtpmap:* *\n" ) + strlen( id->psz_rtpmap )+10;
422 i_size += strlen( "a=fmtp:* *\n" ) + strlen( id->psz_fmtp ) + 10;
426 i_size += strlen( "a=control:*/trackid=*\n" ) + strlen( p_sys->psz_rtsp_control ) + 10;
430 p = psz_sdp = malloc( i_size );
431 p += sprintf( p, "v=0\n" );
432 p += sprintf( p, "o=- "I64Fd" %d IN IP4 127.0.0.1\n",
433 p_sys->i_sdp_id, p_sys->i_sdp_version );
434 p += sprintf( p, "s=NONE\n" );
435 p += sprintf( p, "c=IN IP4 %s/%d\n", psz_destination,
438 for( i = 0; i < p_sys->i_es; i++ )
440 sout_stream_id_t *id = p_sys->es[i];
442 if( id->i_cat == AUDIO_ES )
444 p += sprintf( p, "m=audio %d RTP/AVP %d\n",
445 id->i_port, id->i_payload_type );
447 else if( id->i_cat == VIDEO_ES )
449 p += sprintf( p, "m=video %d RTP/AVP %d\n",
450 id->i_port, id->i_payload_type );
458 p += sprintf( p, "a=rtpmap:%d %s\n", id->i_payload_type,
463 p += sprintf( p, "a=fmtp:%d %s\n", id->i_payload_type,
468 p += sprintf( p, "a=control:%s/trackid=%d\n", p_sys->psz_rtsp_control, i );
475 /*****************************************************************************
477 *****************************************************************************/
478 static int rtp_packetize_l16 ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
479 static int rtp_packetize_l8 ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
480 static int rtp_packetize_mpa ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
481 static int rtp_packetize_mpv ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
482 static int rtp_packetize_ac3 ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
483 static int rtp_packetize_split( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
484 static int rtp_packetize_mp4a ( sout_stream_t *, sout_stream_id_t *, sout_buffer_t * );
486 static void sprintf_hexa( char *s, uint8_t *p_data, int i_data )
488 static const char hex[16] = "0123456789abcdef";
491 for( i = 0; i < i_data; i++ )
493 s[2*i+0] = hex[(p_data[i]>>4)&0xf];
494 s[2*i+1] = hex[(p_data[i] )&0xf];
499 static sout_stream_id_t *Add( sout_stream_t *p_stream, es_format_t *p_fmt )
501 sout_instance_t *p_sout = p_stream->p_sout;
502 sout_stream_sys_t *p_sys = p_stream->p_sys;
503 sout_stream_id_t *id;
504 sout_access_out_t *p_access = NULL;
507 char url[strlen( p_sys->psz_destination ) + 1 + 12 + 1];
509 if( p_sys->p_mux != NULL )
511 sout_input_t *p_input = NULL;
512 if( ( p_input = sout_MuxAddStream( p_sys->p_mux, p_fmt ) ) == NULL )
514 msg_Err( p_stream, "cannot add this stream to the muxer" );
518 id = malloc( sizeof( sout_stream_id_t ) );
520 id->p_input = p_input;
521 id->pf_packetize= NULL;
526 /* first try to create the access out */
527 if( p_sys->i_ttl > 0 )
529 sprintf( access, "udp{raw,ttl=%d}", p_sys->i_ttl );
533 sprintf( access, "udp{raw}" );
535 sprintf( url, "%s:%d", p_sys->psz_destination, p_sys->i_port );
536 if( ( p_access = sout_AccessOutNew( p_sout, access, url ) ) == NULL )
538 msg_Err( p_stream, "cannot create the access out for %s://%s",
543 /* not create the rtp specific stuff */
544 id = malloc( sizeof( sout_stream_id_t ) );
545 id->p_access = p_access;
547 id->psz_rtpmap = NULL;
549 id->psz_destination = strdup( p_sys->psz_destination );
550 id->i_port = p_sys->i_port;
551 id->p_rtsp_url = NULL;
553 switch( p_fmt->i_codec )
555 case VLC_FOURCC( 's', '1', '6', 'b' ):
556 if( p_fmt->audio.i_channels == 1 && p_fmt->audio.i_rate == 44100 )
558 id->i_payload_type = 11;
560 else if( p_fmt->audio.i_channels == 2 &&
561 p_fmt->audio.i_rate == 44100 )
563 id->i_payload_type = 10;
567 id->i_payload_type = p_sys->i_payload_type++;
569 id->psz_rtpmap = malloc( strlen( "L16/*/*" ) + 20+1 );
570 sprintf( id->psz_rtpmap, "L16/%d/%d", p_fmt->audio.i_rate,
571 p_fmt->audio.i_channels );
572 id->i_clock_rate = p_fmt->audio.i_rate;
573 id->pf_packetize = rtp_packetize_l16;
575 case VLC_FOURCC( 'u', '8', ' ', ' ' ):
576 id->i_payload_type = p_sys->i_payload_type++;
577 id->psz_rtpmap = malloc( strlen( "L8/*/*" ) + 20+1 );
578 sprintf( id->psz_rtpmap, "L8/%d/%d", p_fmt->audio.i_rate,
579 p_fmt->audio.i_channels );
580 id->i_clock_rate = p_fmt->audio.i_rate;
581 id->pf_packetize = rtp_packetize_l8;
583 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
584 id->i_payload_type = 14;
585 id->i_clock_rate = 90000;
586 id->psz_rtpmap = strdup( "MPA/90000" );
587 id->pf_packetize = rtp_packetize_mpa;
589 case VLC_FOURCC( 'm', 'p', 'g', 'v' ):
590 id->i_payload_type = 32;
591 id->i_clock_rate = 90000;
592 id->psz_rtpmap = strdup( "MPV/90000" );
593 id->pf_packetize = rtp_packetize_mpv;
595 case VLC_FOURCC( 'a', '5', '2', ' ' ):
596 id->i_payload_type = p_sys->i_payload_type++;
597 id->i_clock_rate = 90000;
598 id->psz_rtpmap = strdup( "ac3/90000" );
599 id->pf_packetize = rtp_packetize_ac3;
601 case VLC_FOURCC( 'm', 'p', '4', 'v' ):
603 char hexa[2*p_fmt->i_extra +1];
605 id->i_payload_type = p_sys->i_payload_type++;
606 id->i_clock_rate = 90000;
607 id->psz_rtpmap = strdup( "MP4V-ES/90000" );
608 id->pf_packetize = rtp_packetize_split;
609 if( p_fmt->i_extra > 0 )
611 id->psz_fmtp = malloc( 100 + 2 * p_fmt->i_extra );
612 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
613 sprintf( id->psz_fmtp,
614 "profile-level-id=3; config=%s;", hexa );
618 case VLC_FOURCC( 'm', 'p', '4', 'a' ):
620 char hexa[2*p_fmt->i_extra +1];
622 id->i_payload_type = p_sys->i_payload_type++;
623 id->i_clock_rate = p_fmt->audio.i_rate;
624 id->psz_rtpmap = malloc( strlen( "mpeg4-generic/" ) + 12 );
625 sprintf( id->psz_rtpmap, "mpeg4-generic/%d", p_fmt->audio.i_rate );
626 id->pf_packetize = rtp_packetize_mp4a;
627 id->psz_fmtp = malloc( 200 + 2 * p_fmt->i_extra );
628 sprintf_hexa( hexa, p_fmt->p_extra, p_fmt->i_extra );
629 sprintf( id->psz_fmtp,
630 "streamtype=5; profile-level-id=15; mode=AAC-hbr; "
631 "config=%s; SizeLength=13;IndexLength=3; "
632 "IndexDeltaLength=3; Profile=1;", hexa );
637 msg_Err( p_stream, "cannot add this stream (unsupported "
638 "codec:%4.4s)", (char*)&p_fmt->i_codec );
642 id->i_cat = p_fmt->i_cat;
644 id->ssrc[0] = rand()&0xff;
645 id->ssrc[1] = rand()&0xff;
646 id->ssrc[2] = rand()&0xff;
647 id->ssrc[3] = rand()&0xff;
648 id->i_sequence = rand()&0xffff;
649 id->i_timestamp_start = rand()&0xffffffff;
651 id->i_mtu = config_GetInt( p_stream, "mtu" ); /* XXX beuk */
652 if( id->i_mtu <= 16 )
654 /* better than nothing */
657 msg_Dbg( p_stream, "access out %s:%s mtu=%d", access, url, id->i_mtu );
659 if( p_sys->p_rtsp_url )
661 char psz_urlc[strlen( p_sys->psz_rtsp_control ) + 1 + 10];
663 sprintf( psz_urlc, "%s/trackid=%d", p_sys->psz_rtsp_path, p_sys->i_es );
664 fprintf( stderr, "rtsp: adding %s\n", psz_urlc );
665 id->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, psz_urlc, NULL, NULL );
669 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
670 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
671 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream );
672 httpd_UrlCatch( id->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
677 /* Update p_sys context */
678 /* update port used (2 -> 1 rtp, 1 rtcp )*/
679 TAB_APPEND( p_sys->i_es, p_sys->es, id );
680 if( p_sys->p_mux == NULL )
684 psz_sdp = SDPGenerate( p_stream, p_sys->psz_destination, VLC_FALSE );
686 vlc_mutex_lock( &p_sys->lock_sdp );
687 free( p_sys->psz_sdp );
688 p_sys->psz_sdp = psz_sdp;
689 vlc_mutex_unlock( &p_sys->lock_sdp );
691 p_sys->i_sdp_version++;
693 fprintf( stderr, "sdp=%s", p_sys->psz_sdp );
699 static int Del( sout_stream_t *p_stream, sout_stream_id_t *id )
701 sout_stream_sys_t *p_sys = p_stream->p_sys;
703 TAB_REMOVE( p_sys->i_es, p_sys->es, id );
709 free( id->psz_rtpmap );
713 free( id->psz_fmtp );
715 free( id->psz_destination );
716 sout_AccessOutDelete( id->p_access );
718 else if( id->p_input )
720 sout_MuxDeleteStream( p_sys->p_mux, id->p_input );
724 httpd_UrlDelete( id->p_rtsp_url );
730 static int Send( sout_stream_t *p_stream, sout_stream_id_t *id,
731 sout_buffer_t *p_buffer )
733 sout_buffer_t *p_next;
735 if( p_stream->p_sys->p_mux )
737 sout_MuxSendBuffer( p_stream->p_sys->p_mux, id->p_input, p_buffer );
743 p_next = p_buffer->p_next;
744 if( id->pf_packetize( p_stream, id, p_buffer ) )
748 sout_BufferDelete( p_stream->p_sout, p_buffer );
756 static int AccessOutGrabberWriteBuffer( sout_stream_t *p_stream,
757 sout_buffer_t *p_buffer )
759 sout_stream_sys_t *p_sys = p_stream->p_sys;
761 int64_t i_dts = p_buffer->i_dts;
762 uint32_t i_timestamp = i_dts * 9 / 100;
764 uint8_t *p_data = p_buffer->p_buffer;
765 unsigned int i_data = p_buffer->i_size;
766 unsigned int i_max = p_sys->i_mtu - 12;
768 int i_packet = ( p_buffer->i_size + i_max - 1 ) / i_max;
774 /* output complete packet */
776 p_sys->packet->i_size + i_data > i_max )
778 sout_AccessOutWrite( p_sys->p_access, p_sys->packet );
779 p_sys->packet = NULL;
782 if( p_sys->packet == NULL )
784 /* allocate a new packet */
785 p_sys->packet = sout_BufferNew( p_stream->p_sout, p_sys->i_mtu );
786 p_sys->packet->p_buffer[ 0] = 0x80;
787 p_sys->packet->p_buffer[ 1] = p_sys->i_payload_type;
788 p_sys->packet->p_buffer[ 2] = ( p_sys->i_sequence >> 8)&0xff;
789 p_sys->packet->p_buffer[ 3] = ( p_sys->i_sequence )&0xff;
790 p_sys->packet->p_buffer[ 4] = ( i_timestamp >> 24 )&0xff;
791 p_sys->packet->p_buffer[ 5] = ( i_timestamp >> 16 )&0xff;
792 p_sys->packet->p_buffer[ 6] = ( i_timestamp >> 8 )&0xff;
793 p_sys->packet->p_buffer[ 7] = ( i_timestamp )&0xff;
794 p_sys->packet->p_buffer[ 8] = p_sys->ssrc[0];
795 p_sys->packet->p_buffer[ 9] = p_sys->ssrc[1];
796 p_sys->packet->p_buffer[10] = p_sys->ssrc[2];
797 p_sys->packet->p_buffer[11] = p_sys->ssrc[3];
798 p_sys->packet->i_size = 12;
800 p_sys->packet->i_dts = i_dts;
801 p_sys->packet->i_length = p_buffer->i_length / i_packet;
802 i_dts += p_sys->packet->i_length;
807 i_size = __MIN( i_data, p_sys->i_mtu - p_sys->packet->i_size );
809 memcpy( &p_sys->packet->p_buffer[p_sys->packet->i_size],
813 p_sys->packet->i_size += i_size;
821 static int AccessOutGrabberWrite( sout_access_out_t *p_access,
822 sout_buffer_t *p_buffer )
824 sout_stream_t *p_stream = (sout_stream_t*)p_access->p_sys;
826 //fprintf( stderr, "received buffer size=%d\n", p_buffer->i_size );
830 sout_buffer_t *p_next;
832 AccessOutGrabberWriteBuffer( p_stream, p_buffer );
834 p_next = p_buffer->p_next;
835 sout_BufferDelete( p_access->p_sout, p_buffer );
842 /****************************************************************************
844 ****************************************************************************/
845 static int HttpCallback( httpd_file_sys_t *p_args,
846 httpd_file_t *, uint8_t *p_request,
847 uint8_t **pp_data, int *pi_data );
849 static int HttpSetup( sout_stream_t *p_stream, vlc_url_t *url)
851 sout_stream_sys_t *p_sys = p_stream->p_sys;
853 p_sys->p_httpd_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port );
854 if( p_sys->p_httpd_host )
856 p_sys->p_httpd_file = httpd_FileNew( p_sys->p_httpd_host,
857 url->psz_path ? url->psz_path : "/",
860 HttpCallback, (void*)p_sys );
862 if( p_sys->p_httpd_file == NULL )
869 static int HttpCallback( httpd_file_sys_t *p_args,
870 httpd_file_t *f, uint8_t *p_request,
871 uint8_t **pp_data, int *pi_data )
873 sout_stream_sys_t *p_sys = (sout_stream_sys_t*)p_args;
875 vlc_mutex_lock( &p_sys->lock_sdp );
876 if( p_sys->psz_sdp && *p_sys->psz_sdp )
878 *pi_data = strlen( p_sys->psz_sdp );
879 *pp_data = malloc( *pi_data );
880 memcpy( *pp_data, p_sys->psz_sdp, *pi_data );
887 vlc_mutex_unlock( &p_sys->lock_sdp );
892 /****************************************************************************
894 ****************************************************************************/
895 static int RtspSetup( sout_stream_t *p_stream, vlc_url_t *url )
897 sout_stream_sys_t *p_sys = p_stream->p_sys;
899 fprintf( stderr, "rtsp setup: %s : %d / %s\n", url->psz_host, url->i_port, url->psz_path );
901 p_sys->p_rtsp_host = httpd_HostNew( VLC_OBJECT(p_stream), url->psz_host, url->i_port > 0 ? url->i_port : 554 );
902 if( p_sys->p_rtsp_host == NULL )
907 p_sys->psz_rtsp_path = strdup( url->psz_path ? url->psz_path : "/" );
908 p_sys->psz_rtsp_control = malloc (strlen( url->psz_host ) + 20 + strlen( p_sys->psz_rtsp_path ) + 1 );
909 sprintf( p_sys->psz_rtsp_control, "rtsp://%s:%d%s",
910 url->psz_host, url->i_port > 0 ? url->i_port : 554, p_sys->psz_rtsp_path );
912 p_sys->p_rtsp_url = httpd_UrlNewUnique( p_sys->p_rtsp_host, p_sys->psz_rtsp_path, NULL, NULL );
913 if( p_sys->p_rtsp_url == 0 )
917 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_DESCRIBE, RtspCallback, (void*)p_stream );
918 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_SETUP, RtspCallback, (void*)p_stream );
919 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PLAY, RtspCallback, (void*)p_stream );
920 /* httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_PAUSE, RtspCallback, (void*)p_stream ); */
921 httpd_UrlCatch( p_sys->p_rtsp_url, HTTPD_MSG_TEARDOWN, RtspCallback, (void*)p_stream );
926 static int RtspCallback( httpd_callback_sys_t *p_args,
928 httpd_message_t *answer, httpd_message_t *query )
930 sout_stream_t *p_stream = (sout_stream_t*)p_args;
931 char *psz_destination = p_stream->p_sys->psz_destination;
932 char *psz_session = NULL;
934 if( psz_destination && *psz_destination == 0 )
936 psz_destination = NULL;
939 if( answer == NULL || query == NULL )
943 fprintf( stderr, "RtspCallback query: type=%d\n", query->i_type );
945 answer->i_proto = HTTPD_PROTO_RTSP;
946 answer->i_version= query->i_version;
947 answer->i_type = HTTPD_MSG_ANSWER;
949 switch( query->i_type )
951 case HTTPD_MSG_DESCRIBE:
953 char *psz_sdp = SDPGenerate( p_stream, psz_destination ? psz_destination : "0.0.0.0", VLC_TRUE );
955 answer->i_status = 200;
956 answer->psz_status = strdup( "OK" );
957 httpd_MsgAdd( answer, "Content-type", "%s", "application/sdp" );
959 answer->p_body = psz_sdp;
960 answer->i_body = strlen( psz_sdp );
964 case HTTPD_MSG_SETUP:
966 char *psz_transport = httpd_MsgGet( query, "Transport" );
968 fprintf( stderr, "HTTPD_MSG_SETUP: transport=%s\n", psz_transport );
969 if( strstr( psz_transport, "multicast" ) )
971 fprintf( stderr, "HTTPD_MSG_SETUP: multicast\n" );
972 answer->i_status = 200;
973 answer->psz_status = strdup( "OK" );
975 answer->p_body = NULL;
976 psz_session = httpd_MsgGet( query, "Session" );
977 if( *psz_session == 0 )
979 psz_session = malloc( 100 );
980 sprintf( psz_session, "%d", rand() );
983 else /*if( strstr( psz_transport, "unicast" ) ||
984 strstr( psz_transport, "interleaved" ) ) */
986 answer->i_status = 400;
987 answer->psz_status = strdup( "Bad Request" );
989 answer->p_body = NULL;
995 /* for now only multicast so easy */
996 answer->i_status = 200;
997 answer->psz_status = strdup( "OK" );
999 answer->p_body = NULL;
1001 psz_session = httpd_MsgGet( query, "Session" );
1004 case HTTPD_MSG_PAUSE:
1006 return VLC_EGENERIC;
1007 case HTTPD_MSG_TEARDOWN:
1008 /* for now only multicast so easy again */
1009 answer->i_status = 200;
1010 answer->psz_status = strdup( "OK" );
1012 answer->p_body = NULL;
1014 psz_session = httpd_MsgGet( query, "Session" );
1018 return VLC_EGENERIC;
1020 httpd_MsgAdd( answer, "Server", "VLC Server" );
1021 httpd_MsgAdd( answer, "Content-Length", "%d", answer->i_body );
1022 httpd_MsgAdd( answer, "Cseq", "%d", atoi( httpd_MsgGet( query, "Cseq" ) ) );
1023 httpd_MsgAdd( answer, "Cache-Control", "%s", "no-cache" );
1027 httpd_MsgAdd( answer, "Session", "%s;timeout=5", psz_session );
1032 /****************************************************************************
1034 ****************************************************************************/
1035 static void rtp_packetize_common( sout_stream_id_t *id, sout_buffer_t *out,
1036 int b_marker, int64_t i_pts )
1038 uint32_t i_timestamp = i_pts * (int64_t)id->i_clock_rate / I64C(1000000);
1040 out->p_buffer[0] = 0x80;
1041 out->p_buffer[1] = (b_marker?0x80:0x00)|id->i_payload_type;
1042 out->p_buffer[2] = ( id->i_sequence >> 8)&0xff;
1043 out->p_buffer[3] = ( id->i_sequence )&0xff;
1044 out->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
1045 out->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
1046 out->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
1047 out->p_buffer[7] = ( i_timestamp )&0xff;
1049 out->p_buffer[ 8] = id->ssrc[0];
1050 out->p_buffer[ 9] = id->ssrc[1];
1051 out->p_buffer[10] = id->ssrc[2];
1052 out->p_buffer[11] = id->ssrc[3];
1058 static int rtp_packetize_mpa( sout_stream_t *p_stream, sout_stream_id_t *id,
1061 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1062 int i_count = ( in->i_size + i_max - 1 ) / i_max;
1064 uint8_t *p_data = in->p_buffer;
1065 int i_data = in->i_size;
1068 for( i = 0; i < i_count; i++ )
1070 int i_payload = __MIN( i_max, i_data );
1071 sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 16 + i_payload );
1073 /* rtp common header */
1074 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1076 out->p_buffer[12] = 0;
1077 out->p_buffer[13] = 0;
1078 /* fragment offset in the current frame */
1079 out->p_buffer[14] = ( (i*i_max) >> 8 )&0xff;
1080 out->p_buffer[15] = ( (i*i_max) )&0xff;
1081 memcpy( &out->p_buffer[16], p_data, i_payload );
1083 out->i_size = 16 + i_payload;
1084 out->i_dts = in->i_dts + i * in->i_length / i_count;
1085 out->i_length = in->i_length / i_count;
1087 sout_AccessOutWrite( id->p_access, out );
1089 p_data += i_payload;
1090 i_data -= i_payload;
1097 static int rtp_packetize_mpv( sout_stream_t *p_stream, sout_stream_id_t *id,
1100 int i_max = id->i_mtu - 12 - 4; /* payload max in one packet */
1101 int i_count = ( in->i_size + i_max - 1 ) / i_max;
1103 uint8_t *p_data = in->p_buffer;
1104 int i_data = in->i_size;
1106 int b_sequence_start = 0;
1107 int i_temporal_ref = 0;
1108 int i_picture_coding_type = 0;
1109 int i_fbv = 0, i_bfc = 0, i_ffv = 0, i_ffc = 0;
1110 int b_start_slice = 0;
1112 /* preparse this packet to get some info */
1113 if( in->i_size > 4 )
1115 uint8_t *p = p_data;
1116 int i_rest = in->i_size;
1120 while( i_rest > 4 &&
1121 ( p[0] != 0x00 || p[1] != 0x00 || p[2] != 0x01 ) )
1135 /* sequence start code */
1136 b_sequence_start = 1;
1138 else if( *p == 0x00 && i_rest >= 4 )
1141 i_temporal_ref = ( p[1] << 2) |((p[2]>>6)&0x03);
1142 i_picture_coding_type = (p[2] >> 3)&0x07;
1144 if( i_rest >= 4 && ( i_picture_coding_type == 2 ||
1145 i_picture_coding_type == 3 ) )
1147 i_ffv = (p[3] >> 2)&0x01;
1148 i_ffc = ((p[3]&0x03) << 1)|((p[4]>>7)&0x01);
1149 if( i_rest > 4 && i_picture_coding_type == 3 )
1151 i_fbv = (p[4]>>6)&0x01;
1152 i_bfc = (p[4]>>3)&0x07;
1156 else if( *p <= 0xaf )
1163 for( i = 0; i < i_count; i++ )
1165 int i_payload = __MIN( i_max, i_data );
1166 sout_buffer_t *out = sout_BufferNew( p_stream->p_sout,
1168 uint32_t h = ( i_temporal_ref << 16 )|
1169 ( b_sequence_start << 13 )|
1170 ( b_start_slice << 12 )|
1171 ( i == i_count - 1 ? 1 << 11 : 0 )|
1172 ( i_picture_coding_type << 8 )|
1173 ( i_fbv << 7 )|( i_bfc << 4 )|( i_ffv << 3 )|i_ffc;
1175 /* rtp common header */
1176 rtp_packetize_common( id, out, (i == i_count - 1)?1:0,
1177 in->i_pts > 0 ? in->i_pts : in->i_dts );
1179 /* 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 */
1180 out->p_buffer[12] = ( h >> 24 )&0xff;
1181 out->p_buffer[13] = ( h >> 16 )&0xff;
1182 out->p_buffer[14] = ( h >> 8 )&0xff;
1183 out->p_buffer[15] = ( h )&0xff;
1185 memcpy( &out->p_buffer[16], p_data, i_payload );
1187 out->i_size = 16 + i_payload;
1188 out->i_dts = in->i_dts + i * in->i_length / i_count;
1189 out->i_length = in->i_length / i_count;
1191 sout_AccessOutWrite( id->p_access, out );
1193 p_data += i_payload;
1194 i_data -= i_payload;
1199 static int rtp_packetize_ac3( sout_stream_t *p_stream, sout_stream_id_t *id,
1202 int i_max = id->i_mtu - 12 - 2; /* payload max in one packet */
1203 int i_count = ( in->i_size + i_max - 1 ) / i_max;
1205 uint8_t *p_data = in->p_buffer;
1206 int i_data = in->i_size;
1209 for( i = 0; i < i_count; i++ )
1211 int i_payload = __MIN( i_max, i_data );
1212 sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 14 + i_payload );
1214 /* rtp common header */
1215 rtp_packetize_common( id, out, (i == i_count - 1)?1:0, in->i_pts );
1217 out->p_buffer[12] = 1;
1219 out->p_buffer[13] = 0x00;
1221 memcpy( &out->p_buffer[14], p_data, i_payload );
1223 out->i_size = 14 + i_payload;
1224 out->i_dts = in->i_dts + i * in->i_length / i_count;
1225 out->i_length = in->i_length / i_count;
1227 sout_AccessOutWrite( id->p_access, out );
1229 p_data += i_payload;
1230 i_data -= i_payload;
1236 static int rtp_packetize_split( sout_stream_t *p_stream, sout_stream_id_t *id,
1239 int i_max = id->i_mtu - 12; /* payload max in one packet */
1240 int i_count = ( in->i_size + i_max - 1 ) / i_max;
1242 uint8_t *p_data = in->p_buffer;
1243 int i_data = in->i_size;
1246 for( i = 0; i < i_count; i++ )
1248 int i_payload = __MIN( i_max, i_data );
1249 sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 12 + i_payload );
1251 /* rtp common header */
1252 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1253 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1254 memcpy( &out->p_buffer[12], p_data, i_payload );
1256 out->i_size = 12 + i_payload;
1257 out->i_dts = in->i_dts + i * in->i_length / i_count;
1258 out->i_length = in->i_length / i_count;
1260 sout_AccessOutWrite( id->p_access, out );
1262 p_data += i_payload;
1263 i_data -= i_payload;
1269 static int rtp_packetize_l16( sout_stream_t *p_stream, sout_stream_id_t *id,
1272 int i_max = id->i_mtu - 12; /* payload max in one packet */
1273 int i_count = ( in->i_size + i_max - 1 ) / i_max;
1275 uint8_t *p_data = in->p_buffer;
1276 int i_data = in->i_size;
1281 int i_payload = (__MIN( i_max, i_data )/4)*4;
1282 sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 12 + i_payload );
1284 /* rtp common header */
1285 rtp_packetize_common( id, out, 0,
1286 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1287 memcpy( &out->p_buffer[12], p_data, i_payload );
1289 out->i_size = 12 + i_payload;
1290 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1291 out->i_length = in->i_length / i_count;
1293 sout_AccessOutWrite( id->p_access, out );
1295 p_data += i_payload;
1296 i_data -= i_payload;
1303 static int rtp_packetize_l8( sout_stream_t *p_stream, sout_stream_id_t *id,
1306 int i_max = id->i_mtu - 12; /* payload max in one packet */
1307 int i_count = ( in->i_size + i_max - 1 ) / i_max;
1309 uint8_t *p_data = in->p_buffer;
1310 int i_data = in->i_size;
1315 int i_payload = (__MIN( i_max, i_data )/2)*2;
1316 sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 12 + i_payload );
1318 /* rtp common header */
1319 rtp_packetize_common( id, out, 0,
1320 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1321 memcpy( &out->p_buffer[12], p_data, i_payload );
1323 out->i_size = 12 + i_payload;
1324 out->i_dts = in->i_dts + i_packet * in->i_length / i_count;
1325 out->i_length = in->i_length / i_count;
1327 sout_AccessOutWrite( id->p_access, out );
1329 p_data += i_payload;
1330 i_data -= i_payload;
1336 static int rtp_packetize_mp4a( sout_stream_t *p_stream, sout_stream_id_t *id,
1339 int i_max = id->i_mtu - 16; /* payload max in one packet */
1340 int i_count = ( in->i_size + i_max - 1 ) / i_max;
1342 uint8_t *p_data = in->p_buffer;
1343 int i_data = in->i_size;
1346 for( i = 0; i < i_count; i++ )
1348 int i_payload = __MIN( i_max, i_data );
1349 sout_buffer_t *out = sout_BufferNew( p_stream->p_sout, 16 + i_payload );
1351 /* rtp common header */
1352 rtp_packetize_common( id, out, ((i == i_count - 1)?1:0),
1353 (in->i_pts > 0 ? in->i_pts : in->i_dts) );
1355 /* AU headers length (bits) */
1356 out->p_buffer[12] = 0;
1357 out->p_buffer[13] = 2*8;
1358 /* for each AU length 13 bits + idx 3bits, */
1359 out->p_buffer[14] = ( in->i_size >> 5 )&0xff;
1360 out->p_buffer[15] = ( (in->i_size&0xff)<<3 )|0;
1362 memcpy( &out->p_buffer[16], p_data, i_payload );
1364 out->i_size = 16 + i_payload;
1365 out->i_dts = in->i_dts + i * in->i_length / i_count;
1366 out->i_length = in->i_length / i_count;
1368 sout_AccessOutWrite( id->p_access, out );
1370 p_data += i_payload;
1371 i_data -= i_payload;