1 /*****************************************************************************
2 * live.cpp : live.com support.
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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
36 # include <winsock2.h>
39 #include "BasicUsageEnvironment.hh"
40 #include "GroupsockHelper.hh"
41 #include "liveMedia.hh"
44 #include "../access/mms/asf.h" /* Who said ugly ? */
47 #if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)
48 #define RECLAIM_ENV(env) delete (env)
50 #define RECLAIM_ENV(env) (env)->reclaim()
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 #define CACHING_TEXT N_("Caching value (ms)")
62 #define CACHING_LONGTEXT N_( \
63 "Allows you to modify the default caching value for RTSP streams. This " \
64 "value should be set in millisecond units." )
66 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
67 #define KASENNA_LONGTEXT N_( "Kasenna server speak an old and unstandard " \
68 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
69 "for communication. In this mode you cannot talk to normal RTSP servers." )
72 set_description( _("live.com (RTSP/RTP/SDP) demuxer" ) );
73 set_capability( "demux2", 50 );
74 set_shortname( "Live.com RTP/RTSP");
75 set_callbacks( Open, Close );
76 add_shortcut( "live" );
77 set_category( CAT_INPUT );
78 set_subcategory( SUBCAT_INPUT_DEMUX );
81 set_description( _("RTSP/RTP access and demux") );
82 add_shortcut( "rtsp" );
83 add_shortcut( "sdp" );
84 set_capability( "access_demux", 0 );
85 set_callbacks( Open, Close );
86 add_bool( "rtsp-tcp", 0, NULL,
87 N_("Use RTP over RTSP (TCP)"),
88 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
89 add_integer( "rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
90 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
91 add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
92 KASENNA_LONGTEXT, VLC_TRUE );
96 * - Improve support of PS/TS
97 * - Support X-QT/X-QUICKTIME generic codec for audio.
99 * - Check memory leak, delete/free -> still one when using rtsp-tcp but I'm
100 * not sure if it comes from me.
104 /*****************************************************************************
106 *****************************************************************************/
111 vlc_bool_t b_quicktime;
118 stream_t *p_out_muxed; /* for muxed stream */
120 RTPSource *rtpSource;
121 FramedSource *readSource;
122 vlc_bool_t b_rtcp_sync;
125 unsigned int i_buffer;
135 char *p_sdp; /* XXX mallocated */
136 char *psz_path; /* URL-encoded path */
139 TaskScheduler *scheduler;
140 UsageEnvironment *env ;
145 live_track_t **track; /* XXX mallocated */
158 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
159 vlc_bool_t b_no_data; /* true if we never receive any data */
160 int i_no_data_ti; /* consecutive number of TaskInterrupt */
165 static int Demux ( demux_t * );
166 static int Control( demux_t *, int, va_list );
168 static int ParseASF( demux_t * );
170 static int RollOverTcp( demux_t * );
172 static void StreamRead( void *, unsigned int, unsigned int,
173 struct timeval, unsigned int );
174 static void StreamClose( void * );
175 static void TaskInterrupt( void * );
177 /*****************************************************************************
179 *****************************************************************************/
180 static int Open ( vlc_object_t *p_this )
182 demux_t *p_demux = (demux_t*)p_this;
185 MediaSubsessionIterator *iter;
186 MediaSubsession *sub;
188 vlc_bool_t b_rtsp_tcp;
197 /* See if it looks like a SDP
198 v, o, s fields are mandatory and in this order */
199 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
201 if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) &&
202 strncmp( (char*)p_peek, "v=0\n", 4 ) &&
203 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
210 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
213 p_demux->pf_demux = Demux;
214 p_demux->pf_control= Control;
215 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
217 p_sys->scheduler = NULL;
224 p_sys->i_pcr_start = 0;
227 p_sys->p_out_asf = NULL;
228 p_sys->b_no_data = VLC_TRUE;
229 p_sys->i_no_data_ti = 0;
230 p_sys->b_multicast = VLC_FALSE;
231 p_sys->psz_path = p_demux->psz_path;
234 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
236 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
239 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
241 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
245 if( strcasecmp( p_demux->psz_access, "sdp" ) &&
246 vlc_UrlIsNotEncoded( p_sys->psz_path ) )
248 p_sys->psz_path = vlc_UrlEncode( p_sys->psz_path );
249 if( p_sys->psz_path == NULL )
253 if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
258 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
259 "VLC Media Player" ) ) == NULL )
261 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
262 p_sys->env->getResultMsg() );
265 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
266 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
268 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
270 delete [] psz_options;
272 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
273 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
276 msg_Err( p_demux, "describeURL failed (%s)",
277 p_sys->env->getResultMsg() );
283 /* malloc-ated copy */
284 p_sys->p_sdp = strdup( (char*)p_sdp );
286 fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
288 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
290 p_sys->p_sdp = strdup( p_sys->psz_path );
294 /* Gather the complete sdp file */
297 p_sdp = (uint8_t*)malloc( i_sdp_max );
300 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
301 i_sdp_max - i_sdp - 1 );
305 msg_Err( p_demux, "failed to read SDP" );
312 if( i_read < i_sdp_max - i_sdp - 1 )
319 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
321 p_sys->p_sdp = (char*)p_sdp;
323 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
325 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
327 msg_Err( p_demux, "MediaSession::createNew failed" );
331 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
333 /* Initialise each media subsession */
334 iter = new MediaSubsessionIterator( *p_sys->ms );
335 while( ( sub = iter->next() ) != NULL )
337 unsigned int i_buffer = 0;
340 /* Value taken from mplayer */
341 if( !strcmp( sub->mediumName(), "audio" ) )
343 else if( !strcmp( sub->mediumName(), "video" ) )
348 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
349 bInit = sub->initiate( 4 ); /* Constant ? */
351 bInit = sub->initiate();
355 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
356 sub->mediumName(), sub->codecName(),
357 p_sys->env->getResultMsg() );
361 if( sub->rtpSource() )
363 int fd = sub->rtpSource()->RTPgs()->socketNum();
364 /* Increase the buffer size */
365 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
368 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
371 /* Issue the SETUP */
374 p_sys->rtsp->setupMediaSubsession( *sub, False,
375 b_rtsp_tcp ? True : False );
377 if( !p_sys->b_multicast )
379 /* Check, because we need diff. rollover behaviour for multicast */
380 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
388 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
390 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
395 /* Create all es struct */
397 while( ( sub = iter->next() ) != NULL )
401 if( sub->readSource() == NULL )
406 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
407 tk->p_demux = p_demux;
410 tk->b_quicktime = VLC_FALSE;
411 tk->b_muxed = VLC_FALSE;
412 tk->b_asf = VLC_FALSE;
413 tk->b_rtcp_sync = VLC_FALSE;
414 tk->p_out_muxed = NULL;
416 tk->i_buffer = 65536;
417 tk->p_buffer = (uint8_t *)malloc( 65536 );
419 /* Value taken from mplayer */
420 if( !strcmp( sub->mediumName(), "audio" ) )
422 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
423 tk->fmt.audio.i_channels = sub->numChannels();
424 tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
426 if( !strcmp( sub->codecName(), "MPA" ) ||
427 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
428 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
430 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
431 tk->fmt.audio.i_rate = 0;
433 else if( !strcmp( sub->codecName(), "AC3" ) )
435 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
436 tk->fmt.audio.i_rate = 0;
438 else if( !strcmp( sub->codecName(), "L16" ) )
440 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
441 tk->fmt.audio.i_bitspersample = 16;
443 else if( !strcmp( sub->codecName(), "L8" ) )
445 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
446 tk->fmt.audio.i_bitspersample = 8;
448 else if( !strcmp( sub->codecName(), "PCMU" ) )
450 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
452 else if( !strcmp( sub->codecName(), "PCMA" ) )
454 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
456 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
458 unsigned int i_extra;
461 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
463 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
466 tk->fmt.i_extra = i_extra;
467 tk->fmt.p_extra = malloc( i_extra );
468 memcpy( tk->fmt.p_extra, p_extra, i_extra );
472 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
474 unsigned int i_extra;
477 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
479 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
482 tk->fmt.i_extra = i_extra;
483 tk->fmt.p_extra = malloc( i_extra );
484 memcpy( tk->fmt.p_extra, p_extra, i_extra );
488 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
490 tk->b_asf = VLC_TRUE;
491 if( p_sys->p_out_asf == NULL )
492 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
496 else if( !strcmp( sub->mediumName(), "video" ) )
498 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
499 if( !strcmp( sub->codecName(), "MPV" ) )
501 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
503 else if( !strcmp( sub->codecName(), "H263" ) ||
504 !strcmp( sub->codecName(), "H263-1998" ) ||
505 !strcmp( sub->codecName(), "H263-2000" ) )
507 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
509 else if( !strcmp( sub->codecName(), "H261" ) )
511 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
513 else if( !strcmp( sub->codecName(), "JPEG" ) )
515 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
517 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
519 unsigned int i_extra;
522 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
524 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
527 tk->fmt.i_extra = i_extra;
528 tk->fmt.p_extra = malloc( i_extra );
529 memcpy( tk->fmt.p_extra, p_extra, i_extra );
533 else if( !strcmp( sub->codecName(), "X-QT" ) ||
534 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
535 !strcmp( sub->codecName(), "X-QDM" ) ||
536 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
537 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
539 tk->b_quicktime = VLC_TRUE;
541 else if( !strcmp( sub->codecName(), "MP2T" ) )
543 tk->b_muxed = VLC_TRUE;
544 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
546 else if( !strcmp( sub->codecName(), "MP2P" ) ||
547 !strcmp( sub->codecName(), "MP1S" ) )
549 tk->b_muxed = VLC_TRUE;
550 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
553 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
555 tk->b_asf = VLC_TRUE;
556 if( p_sys->p_out_asf == NULL )
557 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
562 if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
564 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
567 if( sub->rtcpInstance() != NULL )
569 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
572 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
574 tk->readSource = sub->readSource();
575 tk->rtpSource = sub->rtpSource();
578 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
579 p_sys->track[p_sys->i_track++] = tk;
589 if( p_sys->p_out_asf && ParseASF( p_demux ) )
591 msg_Err( p_demux, "cannot find a usable asf header" );
592 /* TODO Clean tracks */
596 p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
597 if( p_sys->i_length < 0 )
601 else if( p_sys->i_length > 0 )
604 /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
607 if( p_sys->i_track <= 0 )
609 msg_Err( p_demux, "no codec supported, aborting" );
616 if( p_sys->p_out_asf )
618 stream_DemuxDelete( p_sys->p_out_asf );
622 Medium::close( p_sys->ms );
626 Medium::close( p_sys->rtsp );
630 RECLAIM_ENV(p_sys->env);
632 if( p_sys->scheduler )
634 delete p_sys->scheduler;
638 free( p_sys->p_sdp );
640 if( ( p_sys->psz_path != NULL )
641 && ( p_sys->psz_path != p_demux->psz_path ) )
642 free( p_sys->psz_path );
648 /*****************************************************************************
650 *****************************************************************************/
651 static void Close( vlc_object_t *p_this )
653 demux_t *p_demux = (demux_t*)p_this;
654 demux_sys_t *p_sys = p_demux->p_sys;
657 for( i = 0; i < p_sys->i_track; i++ )
659 live_track_t *tk = p_sys->track[i];
661 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
662 free( tk->p_buffer );
666 if( p_sys->i_track ) free( p_sys->track );
667 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
669 if( p_sys->rtsp && p_sys->ms )
672 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
675 Medium::close( p_sys->ms );
677 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
678 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
679 if( p_sys->scheduler ) delete p_sys->scheduler;
680 if( p_sys->p_sdp ) free( p_sys->p_sdp );
681 if( p_sys->psz_path != p_demux->psz_path )
682 free( p_sys->psz_path );
686 /*****************************************************************************
688 *****************************************************************************/
689 static int Demux( demux_t *p_demux )
691 demux_sys_t *p_sys = p_demux->p_sys;
694 vlc_bool_t b_send_pcr = VLC_TRUE;
698 for( i = 0; i < p_sys->i_track; i++ )
700 live_track_t *tk = p_sys->track[i];
702 if( tk->b_asf || tk->b_muxed )
703 b_send_pcr = VLC_FALSE;
709 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
714 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
716 p_sys->i_pcr = i_pcr;
719 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
720 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
721 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
723 p_sys->i_pcr_start = i_pcr;
727 /* First warm we want to read data */
729 for( i = 0; i < p_sys->i_track; i++ )
731 live_track_t *tk = p_sys->track[i];
733 if( tk->waiting == 0 )
736 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
737 StreamRead, tk, StreamClose, tk );
740 /* Create a task that will be called if we wait more than 300ms */
741 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
744 p_sys->scheduler->doEventLoop( &p_sys->event );
746 /* remove the task */
747 p_sys->scheduler->unscheduleDelayedTask( task );
749 /* Check for gap in pts value */
750 for( i = 0; i < p_sys->i_track; i++ )
752 live_track_t *tk = p_sys->track[i];
754 if( !tk->b_muxed && !tk->b_rtcp_sync &&
755 tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
757 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
759 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
760 tk->b_rtcp_sync = VLC_TRUE;
762 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
764 p_sys->i_pcr_start = 0;
770 if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
772 /* FIXME Make this configurable
773 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
777 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 3 )
779 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
781 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
783 msg_Warn( p_demux, "no data received in 900ms. Switching to TCP" );
784 if( RollOverTcp( p_demux ) )
786 msg_Err( p_demux, "TCP rollover failed, aborting" );
789 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
791 else if( p_sys->i_no_data_ti > 10 )
793 msg_Err( p_demux, "no data received in 3s, aborting" );
797 else if( !p_sys->b_multicast && p_sys->b_no_data&& p_sys->i_no_data_ti > 10 )
800 msg_Warn( p_demux, "no data received in 3s, eof ?" );
804 return p_demux->b_error ? 0 : 1;
807 /*****************************************************************************
809 *****************************************************************************/
810 static int Control( demux_t *p_demux, int i_query, va_list args )
812 demux_sys_t *p_sys = p_demux->p_sys;
815 vlc_bool_t *pb, b_bool;
820 pi64 = (int64_t*)va_arg( args, int64_t * );
821 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
824 case DEMUX_GET_LENGTH:
825 pi64 = (int64_t*)va_arg( args, int64_t * );
826 *pi64 = p_sys->i_length;
829 case DEMUX_GET_POSITION:
830 pf = (double*)va_arg( args, double* );
831 if( p_sys->i_length > 0 )
833 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
834 p_sys->i_start ) / (double)(p_sys->i_length);
842 case DEMUX_SET_POSITION:
846 f = (double)va_arg( args, double );
847 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
849 if( p_sys->rtsp && p_sys->i_length > 0 )
851 MediaSubsessionIterator *iter =
852 new MediaSubsessionIterator( *p_sys->ms );
853 MediaSubsession *sub;
856 while( ( sub = iter->next() ) != NULL )
858 p_sys->rtsp->playMediaSubsession( *sub, time );
861 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
862 p_sys->i_pcr_start = 0;
865 for( i = 0; i < p_sys->i_track; i++ )
867 p_sys->track[i]->i_pts = 0;
875 /* Special for access_demux */
876 case DEMUX_CAN_PAUSE:
877 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
878 if( p_sys->rtsp && p_sys->i_length )
879 /* Not always true, but will be handled in SET_PAUSE_STATE */
885 case DEMUX_CAN_CONTROL_PACE:
886 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
888 #if 0 /* Disable for now until we have a clock synchro algo
889 * which works with something else than MPEG over UDP */
895 case DEMUX_SET_PAUSE_STATE:
897 MediaSubsessionIterator *iter;
898 MediaSubsession *sub;
900 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
901 p_sys->i_start ) ) / 1000000.00;
903 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
904 if( p_sys->rtsp == NULL )
907 iter = new MediaSubsessionIterator( *p_sys->ms );
908 while( ( sub = iter->next() ) != NULL )
910 if( ( b_bool && !p_sys->rtsp->pauseMediaSubsession( *sub ) ) ||
911 ( !b_bool && !p_sys->rtsp->playMediaSubsession( *sub,
912 d_npt > 0 ? d_npt : -1 ) ) )
920 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
921 for( i = 0; i < p_sys->i_track; i++ )
923 p_sys->track[i]->i_pts = 0;
925 p_sys->i_pcr_start = 0; /* FIXME Wrong */
930 case DEMUX_GET_TITLE_INFO:
931 case DEMUX_SET_TITLE:
932 case DEMUX_SET_SEEKPOINT:
935 case DEMUX_GET_PTS_DELAY:
936 pi64 = (int64_t*)va_arg( args, int64_t * );
937 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
945 /*****************************************************************************
946 * RollOverTcp: reopen the rtsp into TCP mode
947 * XXX: ugly, a lot of code are duplicated from Open()
948 *****************************************************************************/
949 static int RollOverTcp( demux_t *p_demux )
951 demux_sys_t *p_sys = p_demux->p_sys;
952 MediaSubsessionIterator *iter;
953 MediaSubsession *sub;
959 /* We close the old RTSP session */
960 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
962 Medium::close( p_sys->ms );
963 Medium::close( p_sys->rtsp );
968 /* Reopen rtsp client */
969 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
970 "VLC Media Player" ) ) == NULL )
972 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
973 p_sys->env->getResultMsg() );
977 asprintf( &psz_url, "rtsp://%s", p_sys->psz_path );
979 if( ( psz_options = p_sys->rtsp->sendOptionsCmd( psz_url ) ) )
980 delete [] psz_options;
982 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
983 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
987 msg_Err( p_demux, "describeURL failed (%s)",
988 p_sys->env->getResultMsg() );
992 /* malloc-ated copy */
993 p_sys->p_sdp = strdup( (char*)p_sdp );
996 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
998 msg_Err( p_demux, "MediaSession::createNew failed" );
1002 /* Initialise each media subsession */
1003 iter = new MediaSubsessionIterator( *p_sys->ms );
1004 while( ( sub = iter->next() ) != NULL )
1008 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
1009 bInit = sub->initiate( 4 ); /* Constant ? */
1011 bInit = sub->initiate();
1015 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
1016 sub->mediumName(), sub->codecName(),
1017 p_sys->env->getResultMsg() );
1020 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
1023 /* Issue the SETUP */
1024 p_sys->rtsp->setupMediaSubsession( *sub, False, True /* tcp */ );
1028 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
1030 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
1031 return VLC_EGENERIC;
1034 /* Update all tracks */
1037 while( ( sub = iter->next() ) != NULL )
1041 if( sub->readSource() == NULL )
1043 if( i_tk >= p_sys->i_track )
1045 msg_Err( p_demux, "WTF !" );
1049 tk = p_sys->track[i_tk];
1054 tk->b_rtcp_sync = VLC_FALSE;
1056 if( sub->rtcpInstance() != NULL )
1057 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1059 tk->readSource = sub->readSource();
1060 tk->rtpSource = sub->rtpSource();
1071 /*****************************************************************************
1073 *****************************************************************************/
1074 static void StreamRead( void *p_private, unsigned int i_size,
1075 unsigned int i_truncated_bytes, struct timeval pts,
1076 unsigned int duration )
1078 live_track_t *tk = (live_track_t*)p_private;
1079 demux_t *p_demux = tk->p_demux;
1080 demux_sys_t *p_sys = p_demux->p_sys;
1083 mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1084 (uint64_t)pts.tv_usec;
1086 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1087 i_pts &= UI64C(0x00ffffffffffffff);
1089 if( tk->b_quicktime && tk->p_es == NULL )
1091 QuickTimeGenericRTPSource *qtRTPSource =
1092 (QuickTimeGenericRTPSource*)tk->rtpSource;
1093 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1094 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1096 if( qtState.sdAtomSize < 16 + 32 )
1099 p_sys->event = 0xff;
1103 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1104 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1105 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1107 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1108 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1109 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1111 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1115 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1117 pts.tv_sec * 1000000LL + pts.tv_usec );
1120 /* grow buffer if it looks like buffer is too small, but don't eat
1121 * up all the memory on strange streams */
1122 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1125 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1126 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1128 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1131 msg_Warn( p_demux, "realloc failed" );
1135 tk->p_buffer = (uint8_t*)p_tmp;
1138 if( i_size > tk->i_buffer )
1140 msg_Warn( p_demux, "buffer overflow" );
1142 /* FIXME could i_size be > buffer size ? */
1143 if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') )
1145 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1146 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1147 uint32_t header = h261Source->lastSpecialHeader();
1149 uint32_t header = 0;
1150 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1152 p_block = block_New( p_demux, i_size + 4 );
1153 memcpy( p_block->p_buffer, &header, 4 );
1154 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1156 else if( tk->b_asf )
1158 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1159 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1161 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1165 p_block = block_New( p_demux, i_size );
1166 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1168 if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') &&
1169 tk->rtpSource->curPacketMarkerBit() )
1171 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1173 //p_block->i_rate = p_input->stream.control.i_rate;
1175 if( i_pts != tk->i_pts && !tk->b_muxed )
1177 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1178 p_block->i_pts = i_pts;
1180 //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );
1184 stream_DemuxSend( tk->p_out_muxed, p_block );
1186 else if( tk->b_asf )
1188 stream_DemuxSend( p_sys->p_out_asf, p_block );
1192 es_out_Send( p_demux->out, tk->p_es, p_block );
1195 /* warm that's ok */
1196 p_sys->event = 0xff;
1198 /* we have read data */
1200 p_demux->p_sys->b_no_data = VLC_FALSE;
1201 p_demux->p_sys->i_no_data_ti = 0;
1203 if( i_pts > 0 && !tk->b_muxed )
1209 /*****************************************************************************
1211 *****************************************************************************/
1212 static void StreamClose( void *p_private )
1214 live_track_t *tk = (live_track_t*)p_private;
1215 demux_t *p_demux = tk->p_demux;
1216 demux_sys_t *p_sys = p_demux->p_sys;
1218 fprintf( stderr, "StreamClose\n" );
1220 p_sys->event = 0xff;
1221 p_demux->b_error = VLC_TRUE;
1225 /*****************************************************************************
1227 *****************************************************************************/
1228 static void TaskInterrupt( void *p_private )
1230 demux_t *p_demux = (demux_t*)p_private;
1232 fprintf( stderr, "TaskInterrupt\n" );
1234 p_demux->p_sys->i_no_data_ti++;
1237 p_demux->p_sys->event = 0xff;
1240 /*****************************************************************************
1242 *****************************************************************************/
1243 static int b64_decode( char *dest, char *src );
1245 static int ParseASF( demux_t *p_demux )
1247 demux_sys_t *p_sys = p_demux->p_sys;
1249 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1250 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1254 /* Parse the asf header */
1255 if( psz_asf == NULL )
1256 return VLC_EGENERIC;
1258 psz_asf += strlen( psz_marker );
1259 psz_asf = strdup( psz_asf ); /* Duplicate it */
1260 psz_end = strchr( psz_asf, '\n' );
1262 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1265 if( psz_asf >= psz_end )
1268 return VLC_EGENERIC;
1271 /* Always smaller */
1272 p_header = block_New( p_demux, psz_end - psz_asf );
1273 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1274 fprintf( stderr, "Size=%d Hdrb64=%s\n", p_header->i_buffer, psz_asf );
1275 if( p_header->i_buffer <= 0 )
1278 return VLC_EGENERIC;
1281 /* Parse it to get packet size */
1282 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1284 /* Send it to demuxer */
1285 stream_DemuxSend( p_sys->p_out_asf, p_header );
1290 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1291 static int b64_decode( char *dest, char *src )
1293 const char *dest_start = dest;
1297 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1298 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1299 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1300 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1301 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1302 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1303 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1304 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1305 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1306 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1307 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1308 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1309 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1310 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1311 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1312 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1315 for( i_level = 0; *src != '\0'; src++ )
1319 c = b64[(unsigned int)*src];
1331 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1335 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1339 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1347 return dest - dest_start;