1 /*****************************************************************************
2 * live.cpp : live.com support.
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
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 */
148 mtime_t i_pcr_previous;
149 mtime_t i_pcr_repeatdate;
161 vlc_bool_t b_multicast; /* true if one of the tracks is multicasted */
162 vlc_bool_t b_no_data; /* true if we never receive any data */
163 int i_no_data_ti; /* consecutive number of TaskInterrupt */
168 static int Demux ( demux_t * );
169 static int Control( demux_t *, int, va_list );
171 static int ParseASF( demux_t * );
173 static int RollOverTcp( demux_t * );
175 static void StreamRead( void *, unsigned int, unsigned int,
176 struct timeval, unsigned int );
177 static void StreamClose( void * );
178 static void TaskInterrupt( void * );
180 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
181 static unsigned char* parseH264ConfigStr( char const* configStr,
182 unsigned int& configSize );
185 /*****************************************************************************
187 *****************************************************************************/
188 static int Open ( vlc_object_t *p_this )
190 demux_t *p_demux = (demux_t*)p_this;
193 MediaSubsessionIterator *iter;
194 MediaSubsession *sub;
196 vlc_bool_t b_rtsp_tcp;
205 /* See if it looks like a SDP
206 v, o, s fields are mandatory and in this order */
207 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
209 if( memcmp( (char*)p_peek, "v=0\r\n", 5 ) &&
210 memcmp( (char*)p_peek, "v=0\n", 4 ) &&
211 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
218 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
221 p_demux->pf_demux = Demux;
222 p_demux->pf_control= Control;
223 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
225 p_sys->scheduler = NULL;
232 p_sys->i_pcr_start = 0;
233 p_sys->i_pcr_previous = 0;
234 p_sys->i_pcr_repeatdate = 0;
235 p_sys->i_pcr_repeats = 0;
238 p_sys->p_out_asf = NULL;
239 p_sys->b_no_data = VLC_TRUE;
240 p_sys->i_no_data_ti = 0;
241 p_sys->b_multicast = VLC_FALSE;
242 p_sys->psz_path = strdup(p_demux->psz_path);
245 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
247 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
250 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
252 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
256 if( strcasecmp( p_demux->psz_access, "sdp" ) )
258 char *p = p_sys->psz_path;
259 while( (p = strchr( p, ' ' )) != NULL ) *p = '+';
262 if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
267 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
268 "VLC Media Player" ) ) == NULL )
270 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
271 p_sys->env->getResultMsg() );
274 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
275 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
277 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
278 if( psz_options ) delete [] psz_options;
280 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
281 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) ) ;
284 msg_Err( p_demux, "describeURL failed (%s)",
285 p_sys->env->getResultMsg() );
291 /* malloc-ated copy */
292 p_sys->p_sdp = strdup( (char*)p_sdp );
294 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
296 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
298 p_sys->p_sdp = strdup( p_sys->psz_path );
302 /* Gather the complete sdp file */
305 p_sdp = (uint8_t*)malloc( i_sdp_max );
308 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
309 i_sdp_max - i_sdp - 1 );
313 msg_Err( p_demux, "failed to read SDP" );
320 if( i_read < i_sdp_max - i_sdp - 1 )
327 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
329 p_sys->p_sdp = (char*)p_sdp;
331 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
333 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
335 msg_Err( p_demux, "MediaSession::createNew failed" );
339 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
341 /* Initialise each media subsession */
342 iter = new MediaSubsessionIterator( *p_sys->ms );
343 while( ( sub = iter->next() ) != NULL )
345 unsigned int i_buffer = 0;
348 /* Value taken from mplayer */
349 if( !strcmp( sub->mediumName(), "audio" ) )
351 else if( !strcmp( sub->mediumName(), "video" ) )
356 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
357 bInit = sub->initiate( 4 ); /* Constant ? */
359 bInit = sub->initiate();
363 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
364 sub->mediumName(), sub->codecName(),
365 p_sys->env->getResultMsg() );
369 if( sub->rtpSource() )
371 int fd = sub->rtpSource()->RTPgs()->socketNum();
372 /* Increase the buffer size */
373 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
376 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
379 /* Issue the SETUP */
382 p_sys->rtsp->setupMediaSubsession( *sub, False,
383 b_rtsp_tcp ? True : False );
385 if( !p_sys->b_multicast )
387 /* Check, because we need diff. rollover behaviour for multicast */
388 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
396 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
398 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
404 /* Create all es struct */
406 while( ( sub = iter->next() ) != NULL )
410 if( sub->readSource() == NULL ) continue;
412 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
413 tk->p_demux = p_demux;
416 tk->b_quicktime = VLC_FALSE;
417 tk->b_muxed = VLC_FALSE;
418 tk->b_asf = VLC_FALSE;
419 tk->b_rtcp_sync = VLC_FALSE;
420 tk->p_out_muxed = NULL;
422 tk->i_buffer = 65536;
423 tk->p_buffer = (uint8_t *)malloc( 65536 );
425 /* Value taken from mplayer */
426 if( !strcmp( sub->mediumName(), "audio" ) )
428 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
429 tk->fmt.audio.i_channels = sub->numChannels();
430 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
432 if( !strcmp( sub->codecName(), "MPA" ) ||
433 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
434 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
436 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
437 tk->fmt.audio.i_rate = 0;
439 else if( !strcmp( sub->codecName(), "AC3" ) )
441 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
442 tk->fmt.audio.i_rate = 0;
444 else if( !strcmp( sub->codecName(), "L16" ) )
446 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
447 tk->fmt.audio.i_bitspersample = 16;
449 else if( !strcmp( sub->codecName(), "L8" ) )
451 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
452 tk->fmt.audio.i_bitspersample = 8;
454 else if( !strcmp( sub->codecName(), "PCMU" ) )
456 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
458 else if( !strcmp( sub->codecName(), "PCMA" ) )
460 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
462 else if( !strcmp( sub->codecName(), "AMR" ) )
464 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
466 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
468 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
470 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
472 unsigned int i_extra;
475 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
477 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
480 tk->fmt.i_extra = i_extra;
481 tk->fmt.p_extra = malloc( i_extra );
482 memcpy( tk->fmt.p_extra, p_extra, i_extra );
486 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
488 unsigned int i_extra;
491 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
493 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
496 tk->fmt.i_extra = i_extra;
497 tk->fmt.p_extra = malloc( i_extra );
498 memcpy( tk->fmt.p_extra, p_extra, i_extra );
502 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
504 tk->b_asf = VLC_TRUE;
505 if( p_sys->p_out_asf == NULL )
506 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
510 else if( !strcmp( sub->mediumName(), "video" ) )
512 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
513 if( !strcmp( sub->codecName(), "MPV" ) )
515 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
517 else if( !strcmp( sub->codecName(), "H263" ) ||
518 !strcmp( sub->codecName(), "H263-1998" ) ||
519 !strcmp( sub->codecName(), "H263-2000" ) )
521 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
523 else if( !strcmp( sub->codecName(), "H261" ) )
525 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
527 else if( !strcmp( sub->codecName(), "H264" ) )
529 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
530 unsigned int i_extra = 0;
531 uint8_t *p_extra = NULL;
533 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '4' );
534 tk->fmt.b_packetized = VLC_FALSE;
536 /* XXX not the right minimal version I fear */
537 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
538 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
541 tk->fmt.i_extra = i_extra;
542 tk->fmt.p_extra = malloc( i_extra );
543 memcpy( tk->fmt.p_extra, p_extra, i_extra );
549 else if( !strcmp( sub->codecName(), "JPEG" ) )
551 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
553 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
555 unsigned int i_extra;
558 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
560 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
563 tk->fmt.i_extra = i_extra;
564 tk->fmt.p_extra = malloc( i_extra );
565 memcpy( tk->fmt.p_extra, p_extra, i_extra );
569 else if( !strcmp( sub->codecName(), "X-QT" ) ||
570 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
571 !strcmp( sub->codecName(), "X-QDM" ) ||
572 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
573 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
575 tk->b_quicktime = VLC_TRUE;
577 else if( !strcmp( sub->codecName(), "MP2T" ) )
579 tk->b_muxed = VLC_TRUE;
580 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
582 else if( !strcmp( sub->codecName(), "MP2P" ) ||
583 !strcmp( sub->codecName(), "MP1S" ) )
585 tk->b_muxed = VLC_TRUE;
586 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
589 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
591 tk->b_asf = VLC_TRUE;
592 if( p_sys->p_out_asf == NULL )
593 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
598 if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
600 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
603 if( sub->rtcpInstance() != NULL )
605 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
608 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
610 tk->readSource = sub->readSource();
611 tk->rtpSource = sub->rtpSource();
614 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
615 p_sys->track[p_sys->i_track++] = tk;
625 if( p_sys->p_out_asf && ParseASF( p_demux ) )
627 msg_Err( p_demux, "cannot find a usable asf header" );
628 /* TODO Clean tracks */
632 p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
633 if( p_sys->i_length < 0 )
637 else if( p_sys->i_length > 0 )
640 /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
643 if( p_sys->i_track <= 0 )
645 msg_Err( p_demux, "no codec supported, aborting" );
652 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
653 if( p_sys->ms ) Medium::close( p_sys->ms );
654 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
655 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
656 if( p_sys->scheduler ) delete p_sys->scheduler;
657 if( p_sys->p_sdp ) free( p_sys->p_sdp );
658 if( p_sys->psz_path ) free( p_sys->psz_path );
664 /*****************************************************************************
666 *****************************************************************************/
667 static void Close( vlc_object_t *p_this )
669 demux_t *p_demux = (demux_t*)p_this;
670 demux_sys_t *p_sys = p_demux->p_sys;
673 for( i = 0; i < p_sys->i_track; i++ )
675 live_track_t *tk = p_sys->track[i];
677 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
678 free( tk->p_buffer );
682 if( p_sys->i_track ) free( p_sys->track );
683 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
685 if( p_sys->rtsp && p_sys->ms )
688 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
691 Medium::close( p_sys->ms );
693 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
694 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
695 if( p_sys->scheduler ) delete p_sys->scheduler;
696 if( p_sys->p_sdp ) free( p_sys->p_sdp );
697 if( p_sys->psz_path ) free( p_sys->psz_path );
701 /*****************************************************************************
703 *****************************************************************************/
704 static int Demux( demux_t *p_demux )
706 demux_sys_t *p_sys = p_demux->p_sys;
709 vlc_bool_t b_send_pcr = VLC_TRUE;
713 for( i = 0; i < p_sys->i_track; i++ )
715 live_track_t *tk = p_sys->track[i];
717 if( tk->b_asf || tk->b_muxed )
718 b_send_pcr = VLC_FALSE;
724 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
729 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
731 p_sys->i_pcr = i_pcr;
734 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
735 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
736 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
738 p_sys->i_pcr_start = i_pcr;
742 /* When a On Demand QT stream ends, the last frame keeps going with the same PCR/PTS value */
743 /* This tests for that, so we can later decide to end this session */
744 if( i_pcr > 0 && p_sys->i_pcr == p_sys->i_pcr_previous )
746 if( p_sys->i_pcr_repeats == 0 )
747 p_sys->i_pcr_repeatdate = mdate();
748 p_sys->i_pcr_repeats++;
752 p_sys->i_pcr_previous = p_sys->i_pcr;
753 p_sys->i_pcr_repeatdate = 0;
754 p_sys->i_pcr_repeats = 0;
757 if( p_sys->i_pcr_repeats > 5 && mdate() > p_sys->i_pcr_repeatdate + 1000000 )
759 /* We need at least 5 repeats over at least a second of time before we EOF */
760 msg_Dbg( p_demux, "suspect EOF due to end of VoD session" );
764 /* First warm we want to read data */
766 for( i = 0; i < p_sys->i_track; i++ )
768 live_track_t *tk = p_sys->track[i];
770 if( tk->waiting == 0 )
773 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
774 StreamRead, tk, StreamClose, tk );
777 /* Create a task that will be called if we wait more than 300ms */
778 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
781 p_sys->scheduler->doEventLoop( &p_sys->event );
783 /* remove the task */
784 p_sys->scheduler->unscheduleDelayedTask( task );
786 /* Check for gap in pts value */
787 for( i = 0; i < p_sys->i_track; i++ )
789 live_track_t *tk = p_sys->track[i];
791 if( !tk->b_muxed && !tk->b_rtcp_sync &&
792 tk->rtpSource && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
794 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
796 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
797 tk->b_rtcp_sync = VLC_TRUE;
799 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
801 p_sys->i_pcr_start = 0;
807 if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
809 /* FIXME Make this configurable
810 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
814 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
816 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
818 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
820 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
821 if( RollOverTcp( p_demux ) )
823 msg_Err( p_demux, "TCP rollover failed, aborting" );
826 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
828 else if( p_sys->i_no_data_ti > 34 )
830 msg_Err( p_demux, "no data received in 10s, aborting" );
834 else if( !p_sys->b_multicast && p_sys->b_no_data&& p_sys->i_no_data_ti > 34 )
837 msg_Warn( p_demux, "no data received in 10s, eof ?" );
841 return p_demux->b_error ? 0 : 1;
844 /*****************************************************************************
846 *****************************************************************************/
847 static int Control( demux_t *p_demux, int i_query, va_list args )
849 demux_sys_t *p_sys = p_demux->p_sys;
852 vlc_bool_t *pb, b_bool;
857 pi64 = (int64_t*)va_arg( args, int64_t * );
858 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
861 case DEMUX_GET_LENGTH:
862 pi64 = (int64_t*)va_arg( args, int64_t * );
863 *pi64 = p_sys->i_length;
866 case DEMUX_GET_POSITION:
867 pf = (double*)va_arg( args, double* );
868 if( p_sys->i_length > 0 )
870 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
871 p_sys->i_start ) / (double)(p_sys->i_length);
879 case DEMUX_SET_POSITION:
883 f = (double)va_arg( args, double );
884 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
886 if( p_sys->rtsp && p_sys->i_length > 0 )
890 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time ) )
892 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
895 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
896 p_sys->i_pcr_start = 0;
899 #if 0 /* disabled. probably useless */
900 for( i = 0; i < p_sys->i_track; i++ )
902 p_sys->track[i]->i_pts = 0;
910 /* Special for access_demux */
911 case DEMUX_CAN_PAUSE:
912 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
913 if( p_sys->rtsp && p_sys->i_length )
914 /* Not always true, but will be handled in SET_PAUSE_STATE */
920 case DEMUX_CAN_CONTROL_PACE:
921 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
923 #if 0 /* Disable for now until we have a clock synchro algo
924 * which works with something else than MPEG over UDP */
931 case DEMUX_SET_PAUSE_STATE:
934 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
935 p_sys->i_start ) ) / 1000000.00;
937 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
938 if( p_sys->rtsp == NULL )
941 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
942 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
943 d_npt > 0 ? d_npt : -1 ) ) )
945 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
949 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
950 for( i = 0; i < p_sys->i_track; i++ )
952 p_sys->track[i]->i_pts = 0;
954 p_sys->i_pcr_start = 0; /* FIXME Wrong */
959 case DEMUX_GET_TITLE_INFO:
960 case DEMUX_SET_TITLE:
961 case DEMUX_SET_SEEKPOINT:
964 case DEMUX_GET_PTS_DELAY:
965 pi64 = (int64_t*)va_arg( args, int64_t * );
966 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
974 /*****************************************************************************
975 * RollOverTcp: reopen the rtsp into TCP mode
976 * XXX: ugly, a lot of code are duplicated from Open()
977 *****************************************************************************/
978 static int RollOverTcp( demux_t *p_demux )
980 demux_sys_t *p_sys = p_demux->p_sys;
981 MediaSubsessionIterator *iter;
982 MediaSubsession *sub;
988 /* We close the old RTSP session */
989 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
991 Medium::close( p_sys->ms );
992 Medium::close( p_sys->rtsp );
997 /* Reopen rtsp client */
998 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
999 "VLC Media Player" ) ) == NULL )
1001 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
1002 p_sys->env->getResultMsg() );
1003 return VLC_EGENERIC;
1006 asprintf( &psz_url, "rtsp://%s", p_sys->psz_path );
1008 if( ( psz_options = p_sys->rtsp->sendOptionsCmd( psz_url ) ) )
1009 delete [] psz_options;
1011 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
1012 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
1016 msg_Err( p_demux, "describeURL failed (%s)",
1017 p_sys->env->getResultMsg() );
1018 return VLC_EGENERIC;
1021 /* malloc-ated copy */
1022 p_sys->p_sdp = strdup( (char*)p_sdp );
1025 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
1027 msg_Err( p_demux, "MediaSession::createNew failed" );
1028 return VLC_EGENERIC;
1031 /* Initialise each media subsession */
1032 iter = new MediaSubsessionIterator( *p_sys->ms );
1033 while( ( sub = iter->next() ) != NULL )
1037 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
1038 bInit = sub->initiate( 4 ); /* Constant ? */
1040 bInit = sub->initiate();
1044 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
1045 sub->mediumName(), sub->codecName(),
1046 p_sys->env->getResultMsg() );
1049 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
1052 /* Issue the SETUP */
1053 p_sys->rtsp->setupMediaSubsession( *sub, False, True /* tcp */ );
1057 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
1059 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
1060 return VLC_EGENERIC;
1063 /* Update all tracks */
1066 while( ( sub = iter->next() ) != NULL )
1070 if( sub->readSource() == NULL )
1072 if( i_tk >= p_sys->i_track )
1074 msg_Err( p_demux, "WTF !" );
1078 tk = p_sys->track[i_tk];
1083 tk->b_rtcp_sync = VLC_FALSE;
1085 if( sub->rtcpInstance() != NULL )
1086 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1088 tk->readSource = sub->readSource();
1089 tk->rtpSource = sub->rtpSource();
1100 /*****************************************************************************
1102 *****************************************************************************/
1103 static void StreamRead( void *p_private, unsigned int i_size,
1104 unsigned int i_truncated_bytes, struct timeval pts,
1105 unsigned int duration )
1107 live_track_t *tk = (live_track_t*)p_private;
1108 demux_t *p_demux = tk->p_demux;
1109 demux_sys_t *p_sys = p_demux->p_sys;
1112 mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1113 (uint64_t)pts.tv_usec;
1115 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1116 i_pts &= UI64C(0x00ffffffffffffff);
1118 if( tk->b_quicktime && tk->p_es == NULL )
1120 QuickTimeGenericRTPSource *qtRTPSource =
1121 (QuickTimeGenericRTPSource*)tk->rtpSource;
1122 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1123 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1125 if( qtState.sdAtomSize < 16 + 32 )
1128 p_sys->event = 0xff;
1132 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1133 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1134 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1136 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1137 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1138 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1140 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1144 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1146 pts.tv_sec * 1000000LL + pts.tv_usec );
1149 /* grow buffer if it looks like buffer is too small, but don't eat
1150 * up all the memory on strange streams */
1151 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1154 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1155 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1157 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1160 msg_Warn( p_demux, "realloc failed" );
1164 tk->p_buffer = (uint8_t*)p_tmp;
1167 if( i_size > tk->i_buffer )
1169 msg_Warn( p_demux, "buffer overflow" );
1171 /* FIXME could i_size be > buffer size ? */
1172 if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1174 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1175 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1176 uint32_t header = h261Source->lastSpecialHeader();
1178 uint32_t header = 0;
1179 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1181 p_block = block_New( p_demux, i_size + 4 );
1182 memcpy( p_block->p_buffer, &header, 4 );
1183 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1185 if( tk->rtpSource->curPacketMarkerBit() )
1186 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1188 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','4') )
1190 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1191 msg_Warn( p_demux, "unsupported NAL type for H264" );
1193 /* Normal NAL type */
1194 p_block = block_New( p_demux, i_size + 4 );
1195 p_block->p_buffer[0] = 0x00;
1196 p_block->p_buffer[1] = 0x00;
1197 p_block->p_buffer[2] = 0x00;
1198 p_block->p_buffer[3] = 0x01;
1199 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1201 else if( tk->b_asf )
1203 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1204 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1206 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1210 p_block = block_New( p_demux, i_size );
1211 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1214 //p_block->i_rate = p_input->stream.control.i_rate;
1216 if( i_pts != tk->i_pts && !tk->b_muxed )
1218 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1219 p_block->i_pts = i_pts;
1224 stream_DemuxSend( tk->p_out_muxed, p_block );
1226 else if( tk->b_asf )
1228 stream_DemuxSend( p_sys->p_out_asf, p_block );
1232 es_out_Send( p_demux->out, tk->p_es, p_block );
1235 /* warm that's ok */
1236 p_sys->event = 0xff;
1238 /* we have read data */
1240 p_demux->p_sys->b_no_data = VLC_FALSE;
1241 p_demux->p_sys->i_no_data_ti = 0;
1243 if( i_pts > 0 && !tk->b_muxed )
1249 /*****************************************************************************
1251 *****************************************************************************/
1252 static void StreamClose( void *p_private )
1254 live_track_t *tk = (live_track_t*)p_private;
1255 demux_t *p_demux = tk->p_demux;
1256 demux_sys_t *p_sys = p_demux->p_sys;
1258 msg_Dbg( p_demux, "StreamClose" );
1260 p_sys->event = 0xff;
1261 p_demux->b_error = VLC_TRUE;
1265 /*****************************************************************************
1267 *****************************************************************************/
1268 static void TaskInterrupt( void *p_private )
1270 demux_t *p_demux = (demux_t*)p_private;
1272 p_demux->p_sys->i_no_data_ti++;
1275 p_demux->p_sys->event = 0xff;
1278 /*****************************************************************************
1280 *****************************************************************************/
1281 static int b64_decode( char *dest, char *src );
1283 static int ParseASF( demux_t *p_demux )
1285 demux_sys_t *p_sys = p_demux->p_sys;
1287 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1288 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1292 /* Parse the asf header */
1293 if( psz_asf == NULL )
1294 return VLC_EGENERIC;
1296 psz_asf += strlen( psz_marker );
1297 psz_asf = strdup( psz_asf ); /* Duplicate it */
1298 psz_end = strchr( psz_asf, '\n' );
1300 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1303 if( psz_asf >= psz_end )
1306 return VLC_EGENERIC;
1309 /* Always smaller */
1310 p_header = block_New( p_demux, psz_end - psz_asf );
1311 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1312 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1313 if( p_header->i_buffer <= 0 )
1316 return VLC_EGENERIC;
1319 /* Parse it to get packet size */
1320 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1322 /* Send it to demuxer */
1323 stream_DemuxSend( p_sys->p_out_asf, p_header );
1329 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
1330 static unsigned char* parseH264ConfigStr( char const* configStr,
1331 unsigned int& configSize )
1338 if( configStr == NULL || *configStr == '\0' )
1341 psz = dup = strdup( configStr );
1343 unsigned char *cfg = new unsigned char[5 * strlen(psz)];
1346 char *p = strchr( psz, ',' );
1350 cfg[configSize++] = 0x00;
1351 cfg[configSize++] = 0x00;
1352 cfg[configSize++] = 0x00;
1353 cfg[configSize++] = 0x01;
1354 configSize += b64_decode( (char*)&cfg[configSize], psz );
1361 if( dup ) free( dup );
1366 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1367 static int b64_decode( char *dest, char *src )
1369 const char *dest_start = dest;
1373 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1374 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1375 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1376 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1377 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1378 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1379 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1380 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1381 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1382 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1383 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1384 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1385 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1386 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1387 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1388 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1391 for( i_level = 0; *src != '\0'; src++ )
1395 c = b64[(unsigned int)*src];
1407 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1411 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1415 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1423 return dest - dest_start;