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>
35 # include <winsock2.h>
38 #include "BasicUsageEnvironment.hh"
39 #include "GroupsockHelper.hh"
40 #include "liveMedia.hh"
43 #include "../access/mms/asf.h" /* Who said ugly ? */
46 #if (LIVEMEDIA_LIBRARY_VERSION_INT < 1089936000)
47 #define RECLAIM_ENV(env) delete (env)
49 #define RECLAIM_ENV(env) (env)->reclaim()
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
60 #define CACHING_TEXT N_("Caching value (ms)")
61 #define CACHING_LONGTEXT N_( \
62 "Allows you to modify the default caching value for RTSP streams. This " \
63 "value should be set in millisecond units." )
65 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
66 #define KASENNA_LONGTEXT N_( "Kasenna server speak an old and unstandard " \
67 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
68 "for communication. In this mode you cannot talk to normal RTSP servers." )
71 set_description( _("live.com (RTSP/RTP/SDP) demuxer" ) );
72 set_capability( "demux2", 50 );
73 set_shortname( "Live.com RTP/RTSP");
74 set_callbacks( Open, Close );
75 add_shortcut( "live" );
76 set_category( CAT_INPUT );
77 set_subcategory( SUBCAT_INPUT_DEMUX );
80 set_description( _("RTSP/RTP access and demux") );
81 add_shortcut( "rtsp" );
82 add_shortcut( "sdp" );
83 set_capability( "access_demux", 0 );
84 set_callbacks( Open, Close );
85 add_bool( "rtsp-tcp", 0, NULL,
86 N_("Use RTP over RTSP (TCP)"),
87 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
88 add_integer( "rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
89 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
90 add_bool( "rtsp-kasenna", VLC_FALSE, NULL, KASENNA_TEXT,
91 KASENNA_LONGTEXT, VLC_TRUE );
95 * - Improve support of PS/TS
96 * - Support X-QT/X-QUICKTIME generic codec for audio.
98 * - Check memory leak, delete/free -> still one when using rtsp-tcp but I'm
99 * not sure if it comes from me.
103 /*****************************************************************************
105 *****************************************************************************/
110 vlc_bool_t b_quicktime;
117 stream_t *p_out_muxed; /* for muxed stream */
119 RTPSource *rtpSource;
120 FramedSource *readSource;
121 vlc_bool_t b_rtcp_sync;
124 unsigned int i_buffer;
134 char *p_sdp; /* XXX mallocated */
137 TaskScheduler *scheduler;
138 UsageEnvironment *env ;
143 live_track_t **track; /* XXX mallocated */
156 vlc_bool_t b_no_data; /* true if we never receive any data */
157 int i_no_data_ti; /* consecutive number of TaskInterrupt */
162 static int Demux ( demux_t * );
163 static int Control( demux_t *, int, va_list );
165 static int ParseASF( demux_t * );
167 static int RollOverTcp( demux_t * );
169 static void StreamRead( void *, unsigned int, unsigned int,
170 struct timeval, unsigned int );
171 static void StreamClose( void * );
172 static void TaskInterrupt( void * );
174 /*****************************************************************************
176 *****************************************************************************/
177 static int Open ( vlc_object_t *p_this )
179 demux_t *p_demux = (demux_t*)p_this;
182 MediaSubsessionIterator *iter;
183 MediaSubsession *sub;
185 vlc_bool_t b_rtsp_tcp;
194 /* See if it looks like a SDP
195 v, o, s fields are mandatory and in this order */
196 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
198 if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) &&
199 strncmp( (char*)p_peek, "v=0\n", 4 ) &&
200 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
207 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
210 p_demux->pf_demux = Demux;
211 p_demux->pf_control= Control;
212 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
214 p_sys->scheduler = NULL;
221 p_sys->i_pcr_start = 0;
224 p_sys->p_out_asf = NULL;
225 p_sys->b_no_data = VLC_TRUE;
226 p_sys->i_no_data_ti = 0;
229 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
231 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
234 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
236 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
240 if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
245 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
246 "VLC Media Player" ) ) == NULL )
248 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
249 p_sys->env->getResultMsg() );
252 psz_url = (char*)malloc( strlen( p_demux->psz_path ) + 8 );
253 sprintf( psz_url, "rtsp://%s", p_demux->psz_path );
255 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
257 delete [] psz_options;
259 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
260 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
263 msg_Err( p_demux, "describeURL failed (%s)",
264 p_sys->env->getResultMsg() );
270 /* malloc-ated copy */
271 p_sys->p_sdp = strdup( (char*)p_sdp );
273 fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
275 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
277 p_sys->p_sdp = strdup( p_demux->psz_path );
281 /* Gather the complete sdp file */
284 p_sdp = (uint8_t*)malloc( i_sdp_max );
287 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
288 i_sdp_max - i_sdp - 1 );
292 msg_Err( p_demux, "failed to read SDP" );
299 if( i_read < i_sdp_max - i_sdp - 1 )
306 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
308 p_sys->p_sdp = (char*)p_sdp;
310 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
312 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
314 msg_Err( p_demux, "MediaSession::createNew failed" );
318 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
320 /* Initialise each media subsession */
321 iter = new MediaSubsessionIterator( *p_sys->ms );
322 while( ( sub = iter->next() ) != NULL )
324 unsigned int i_buffer = 0;
327 /* Value taken from mplayer */
328 if( !strcmp( sub->mediumName(), "audio" ) )
330 else if( !strcmp( sub->mediumName(), "video" ) )
335 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
336 bInit = sub->initiate( 4 ); /* Constant ? */
338 bInit = sub->initiate();
342 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
343 sub->mediumName(), sub->codecName(),
344 p_sys->env->getResultMsg() );
348 if( sub->rtpSource() )
350 int fd = sub->rtpSource()->RTPgs()->socketNum();
351 /* Increase the buffer size */
352 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
355 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
358 /* Issue the SETUP */
361 p_sys->rtsp->setupMediaSubsession( *sub, False,
362 b_rtsp_tcp ? True : False );
370 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
372 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
377 /* Create all es struct */
379 while( ( sub = iter->next() ) != NULL )
383 if( sub->readSource() == NULL )
388 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
389 tk->p_demux = p_demux;
392 tk->b_quicktime = VLC_FALSE;
393 tk->b_muxed = VLC_FALSE;
394 tk->b_asf = VLC_FALSE;
395 tk->b_rtcp_sync = VLC_FALSE;
396 tk->p_out_muxed = NULL;
398 tk->i_buffer = 65536;
399 tk->p_buffer = (uint8_t *)malloc( 65536 );
401 /* Value taken from mplayer */
402 if( !strcmp( sub->mediumName(), "audio" ) )
404 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
405 tk->fmt.audio.i_channels = sub->numChannels();
406 tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
408 if( !strcmp( sub->codecName(), "MPA" ) ||
409 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
410 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
412 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
413 tk->fmt.audio.i_rate = 0;
415 else if( !strcmp( sub->codecName(), "AC3" ) )
417 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
418 tk->fmt.audio.i_rate = 0;
420 else if( !strcmp( sub->codecName(), "L16" ) )
422 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
423 tk->fmt.audio.i_bitspersample = 16;
425 else if( !strcmp( sub->codecName(), "L8" ) )
427 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
428 tk->fmt.audio.i_bitspersample = 8;
430 else if( !strcmp( sub->codecName(), "PCMU" ) )
432 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
434 else if( !strcmp( sub->codecName(), "PCMA" ) )
436 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
438 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
440 unsigned int i_extra;
443 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
445 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
448 tk->fmt.i_extra = i_extra;
449 tk->fmt.p_extra = malloc( i_extra );
450 memcpy( tk->fmt.p_extra, p_extra, i_extra );
454 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
456 unsigned int i_extra;
459 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
461 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
464 tk->fmt.i_extra = i_extra;
465 tk->fmt.p_extra = malloc( i_extra );
466 memcpy( tk->fmt.p_extra, p_extra, i_extra );
470 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
472 tk->b_asf = VLC_TRUE;
473 if( p_sys->p_out_asf == NULL )
474 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
478 else if( !strcmp( sub->mediumName(), "video" ) )
480 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
481 if( !strcmp( sub->codecName(), "MPV" ) )
483 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
485 else if( !strcmp( sub->codecName(), "H263" ) ||
486 !strcmp( sub->codecName(), "H263-1998" ) ||
487 !strcmp( sub->codecName(), "H263-2000" ) )
489 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
491 else if( !strcmp( sub->codecName(), "H261" ) )
493 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
495 else if( !strcmp( sub->codecName(), "JPEG" ) )
497 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
499 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
501 unsigned int i_extra;
504 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
506 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
509 tk->fmt.i_extra = i_extra;
510 tk->fmt.p_extra = malloc( i_extra );
511 memcpy( tk->fmt.p_extra, p_extra, i_extra );
515 else if( !strcmp( sub->codecName(), "X-QT" ) ||
516 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
517 !strcmp( sub->codecName(), "X-QDM" ) ||
518 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
519 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
521 tk->b_quicktime = VLC_TRUE;
523 else if( !strcmp( sub->codecName(), "MP2T" ) )
525 tk->b_muxed = VLC_TRUE;
526 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
528 else if( !strcmp( sub->codecName(), "MP2P" ) ||
529 !strcmp( sub->codecName(), "MP1S" ) )
531 tk->b_muxed = VLC_TRUE;
532 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
535 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
537 tk->b_asf = VLC_TRUE;
538 if( p_sys->p_out_asf == NULL )
539 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
544 if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
546 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
549 if( sub->rtcpInstance() != NULL )
551 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
554 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
556 tk->readSource = sub->readSource();
557 tk->rtpSource = sub->rtpSource();
560 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
561 p_sys->track[p_sys->i_track++] = tk;
571 if( p_sys->p_out_asf && ParseASF( p_demux ) )
573 msg_Err( p_demux, "cannot find a usable asf header" );
574 /* TODO Clean tracks */
578 p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
579 if( p_sys->i_length < 0 )
583 else if( p_sys->i_length > 0 )
586 /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
589 if( p_sys->i_track <= 0 )
591 msg_Err( p_demux, "no codec supported, aborting" );
598 if( p_sys->p_out_asf )
600 stream_DemuxDelete( p_sys->p_out_asf );
604 Medium::close( p_sys->ms );
608 Medium::close( p_sys->rtsp );
612 RECLAIM_ENV(p_sys->env);
614 if( p_sys->scheduler )
616 delete p_sys->scheduler;
620 free( p_sys->p_sdp );
626 /*****************************************************************************
628 *****************************************************************************/
629 static void Close( vlc_object_t *p_this )
631 demux_t *p_demux = (demux_t*)p_this;
632 demux_sys_t *p_sys = p_demux->p_sys;
635 for( i = 0; i < p_sys->i_track; i++ )
637 live_track_t *tk = p_sys->track[i];
639 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
640 free( tk->p_buffer );
644 if( p_sys->i_track ) free( p_sys->track );
645 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
647 if( p_sys->rtsp && p_sys->ms )
650 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
653 Medium::close( p_sys->ms );
655 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
656 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
657 if( p_sys->scheduler ) delete p_sys->scheduler;
658 if( p_sys->p_sdp ) free( p_sys->p_sdp );
662 /*****************************************************************************
664 *****************************************************************************/
665 static int Demux( demux_t *p_demux )
667 demux_sys_t *p_sys = p_demux->p_sys;
670 vlc_bool_t b_send_pcr = VLC_TRUE;
674 for( i = 0; i < p_sys->i_track; i++ )
676 live_track_t *tk = p_sys->track[i];
678 if( tk->b_asf || tk->b_muxed )
679 b_send_pcr = VLC_FALSE;
685 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
690 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
692 p_sys->i_pcr = i_pcr;
695 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
696 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
697 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
699 p_sys->i_pcr_start = i_pcr;
703 /* First warm we want to read data */
705 for( i = 0; i < p_sys->i_track; i++ )
707 live_track_t *tk = p_sys->track[i];
709 if( tk->waiting == 0 )
712 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
713 StreamRead, tk, StreamClose, tk );
716 /* Create a task that will be called if we wait more than 300ms */
717 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
720 p_sys->scheduler->doEventLoop( &p_sys->event );
722 /* remove the task */
723 p_sys->scheduler->unscheduleDelayedTask( task );
725 /* Check for gap in pts value */
726 for( i = 0; i < p_sys->i_track; i++ )
728 live_track_t *tk = p_sys->track[i];
730 if( !tk->b_muxed && !tk->b_rtcp_sync &&
731 tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
733 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
735 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
736 tk->b_rtcp_sync = VLC_TRUE;
738 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
740 p_sys->i_pcr_start = 0;
746 if( p_sys->b_no_data && p_sys->i_no_data_ti > 3 )
748 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
750 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
752 msg_Warn( p_demux, "no data received in 900ms. Switching to TCP" );
753 if( RollOverTcp( p_demux ) )
755 msg_Err( p_demux, "TCP rollover failed, aborting" );
758 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
760 else if( p_sys->i_no_data_ti > 10 )
762 msg_Err( p_demux, "no data received in 3s, aborting" );
766 else if( p_sys->i_no_data_ti > 10 )
769 msg_Warn( p_demux, "no data received in 3s, eof ?" );
773 return p_demux->b_error ? 0 : 1;
776 /*****************************************************************************
778 *****************************************************************************/
779 static int Control( demux_t *p_demux, int i_query, va_list args )
781 demux_sys_t *p_sys = p_demux->p_sys;
784 vlc_bool_t *pb, b_bool;
789 pi64 = (int64_t*)va_arg( args, int64_t * );
790 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
793 case DEMUX_GET_LENGTH:
794 pi64 = (int64_t*)va_arg( args, int64_t * );
795 *pi64 = p_sys->i_length;
798 case DEMUX_GET_POSITION:
799 pf = (double*)va_arg( args, double* );
800 if( p_sys->i_length > 0 )
802 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
803 p_sys->i_start ) / (double)(p_sys->i_length);
811 case DEMUX_SET_POSITION:
815 f = (double)va_arg( args, double );
816 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
818 if( p_sys->rtsp && p_sys->i_length > 0 )
820 MediaSubsessionIterator *iter =
821 new MediaSubsessionIterator( *p_sys->ms );
822 MediaSubsession *sub;
825 while( ( sub = iter->next() ) != NULL )
827 p_sys->rtsp->playMediaSubsession( *sub, time );
830 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
831 p_sys->i_pcr_start = 0;
834 for( i = 0; i < p_sys->i_track; i++ )
836 p_sys->track[i]->i_pts = 0;
844 /* Special for access_demux */
845 case DEMUX_CAN_PAUSE:
846 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
847 if( p_sys->rtsp && p_sys->i_length )
848 /* Not always true, but will be handled in SET_PAUSE_STATE */
854 case DEMUX_CAN_CONTROL_PACE:
855 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
857 #if 0 /* Disable for now until we have a clock synchro algo
858 * which works with something else than MPEG over UDP */
864 case DEMUX_SET_PAUSE_STATE:
866 MediaSubsessionIterator *iter;
867 MediaSubsession *sub;
869 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
870 p_sys->i_start ) ) / 1000000.00;
872 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
873 if( p_sys->rtsp == NULL )
876 iter = new MediaSubsessionIterator( *p_sys->ms );
877 while( ( sub = iter->next() ) != NULL )
879 if( ( b_bool && !p_sys->rtsp->pauseMediaSubsession( *sub ) ) ||
880 ( !b_bool && !p_sys->rtsp->playMediaSubsession( *sub,
881 d_npt > 0 ? d_npt : -1 ) ) )
889 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
890 for( i = 0; i < p_sys->i_track; i++ )
892 p_sys->track[i]->i_pts = 0;
894 p_sys->i_pcr_start = 0; /* FIXME Wrong */
899 case DEMUX_GET_TITLE_INFO:
900 case DEMUX_SET_TITLE:
901 case DEMUX_SET_SEEKPOINT:
904 case DEMUX_GET_PTS_DELAY:
905 pi64 = (int64_t*)va_arg( args, int64_t * );
906 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
914 /*****************************************************************************
915 * RollOverTcp: reopen the rtsp into TCP mode
916 * XXX: ugly, a lot of code are duplicated from Open()
917 *****************************************************************************/
918 static int RollOverTcp( demux_t *p_demux )
920 demux_sys_t *p_sys = p_demux->p_sys;
921 MediaSubsessionIterator *iter;
922 MediaSubsession *sub;
928 /* We close the old RTSP session */
929 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
931 Medium::close( p_sys->ms );
932 Medium::close( p_sys->rtsp );
937 /* Reopen rtsp client */
938 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
939 "VLC Media Player" ) ) == NULL )
941 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
942 p_sys->env->getResultMsg() );
946 asprintf( &psz_url, "rtsp://%s", p_demux->psz_path );
948 if( ( psz_options = p_sys->rtsp->sendOptionsCmd( psz_url ) ) )
949 delete [] psz_options;
951 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
952 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
956 msg_Err( p_demux, "describeURL failed (%s)",
957 p_sys->env->getResultMsg() );
961 /* malloc-ated copy */
962 p_sys->p_sdp = strdup( (char*)p_sdp );
965 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
967 msg_Err( p_demux, "MediaSession::createNew failed" );
971 /* Initialise each media subsession */
972 iter = new MediaSubsessionIterator( *p_sys->ms );
973 while( ( sub = iter->next() ) != NULL )
977 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
978 bInit = sub->initiate( 4 ); /* Constant ? */
980 bInit = sub->initiate();
984 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
985 sub->mediumName(), sub->codecName(),
986 p_sys->env->getResultMsg() );
989 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
992 /* Issue the SETUP */
993 p_sys->rtsp->setupMediaSubsession( *sub, False, True /* tcp */ );
997 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
999 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
1000 return VLC_EGENERIC;
1003 /* Update all tracks */
1006 while( ( sub = iter->next() ) != NULL )
1010 if( sub->readSource() == NULL )
1012 if( i_tk >= p_sys->i_track )
1014 msg_Err( p_demux, "WTF !" );
1018 tk = p_sys->track[i_tk];
1023 tk->b_rtcp_sync = VLC_FALSE;
1025 if( sub->rtcpInstance() != NULL )
1026 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1028 tk->readSource = sub->readSource();
1029 tk->rtpSource = sub->rtpSource();
1040 /*****************************************************************************
1042 *****************************************************************************/
1043 static void StreamRead( void *p_private, unsigned int i_size,
1044 unsigned int i_truncated_bytes, struct timeval pts,
1045 unsigned int duration )
1047 live_track_t *tk = (live_track_t*)p_private;
1048 demux_t *p_demux = tk->p_demux;
1049 demux_sys_t *p_sys = p_demux->p_sys;
1052 mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1053 (uint64_t)pts.tv_usec;
1055 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1056 i_pts &= UI64C(0x00ffffffffffffff);
1058 if( tk->b_quicktime && tk->p_es == NULL )
1060 QuickTimeGenericRTPSource *qtRTPSource =
1061 (QuickTimeGenericRTPSource*)tk->rtpSource;
1062 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1063 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1065 if( qtState.sdAtomSize < 16 + 32 )
1068 p_sys->event = 0xff;
1072 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1073 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1074 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1076 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1077 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1078 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1080 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1084 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1086 pts.tv_sec * 1000000LL + pts.tv_usec );
1089 /* grow buffer if it looks like buffer is too small, but don't eat
1090 * up all the memory on strange streams */
1091 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1094 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1095 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1097 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1100 msg_Warn( p_demux, "realloc failed" );
1104 tk->p_buffer = (uint8_t*)p_tmp;
1107 if( i_size > tk->i_buffer )
1109 msg_Warn( p_demux, "buffer overflow" );
1111 /* FIXME could i_size be > buffer size ? */
1112 if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') )
1114 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1115 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1116 uint32_t header = h261Source->lastSpecialHeader();
1118 uint32_t header = 0;
1119 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1121 p_block = block_New( p_demux, i_size + 4 );
1122 memcpy( p_block->p_buffer, &header, 4 );
1123 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1125 else if( tk->b_asf )
1127 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1128 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1130 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1134 p_block = block_New( p_demux, i_size );
1135 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1137 if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') &&
1138 tk->rtpSource->curPacketMarkerBit() )
1140 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1142 //p_block->i_rate = p_input->stream.control.i_rate;
1144 if( i_pts != tk->i_pts && !tk->b_muxed )
1146 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1147 p_block->i_pts = i_pts;
1149 //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );
1153 stream_DemuxSend( tk->p_out_muxed, p_block );
1155 else if( tk->b_asf )
1157 stream_DemuxSend( p_sys->p_out_asf, p_block );
1161 es_out_Send( p_demux->out, tk->p_es, p_block );
1164 /* warm that's ok */
1165 p_sys->event = 0xff;
1167 /* we have read data */
1169 p_demux->p_sys->b_no_data = VLC_FALSE;
1170 p_demux->p_sys->i_no_data_ti = 0;
1172 if( i_pts > 0 && !tk->b_muxed )
1178 /*****************************************************************************
1180 *****************************************************************************/
1181 static void StreamClose( void *p_private )
1183 live_track_t *tk = (live_track_t*)p_private;
1184 demux_t *p_demux = tk->p_demux;
1185 demux_sys_t *p_sys = p_demux->p_sys;
1187 fprintf( stderr, "StreamClose\n" );
1189 p_sys->event = 0xff;
1190 p_demux->b_error = VLC_TRUE;
1194 /*****************************************************************************
1196 *****************************************************************************/
1197 static void TaskInterrupt( void *p_private )
1199 demux_t *p_demux = (demux_t*)p_private;
1201 fprintf( stderr, "TaskInterrupt\n" );
1203 p_demux->p_sys->i_no_data_ti++;
1206 p_demux->p_sys->event = 0xff;
1209 /*****************************************************************************
1211 *****************************************************************************/
1212 static int b64_decode( char *dest, char *src );
1214 static int ParseASF( demux_t *p_demux )
1216 demux_sys_t *p_sys = p_demux->p_sys;
1218 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1219 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1223 /* Parse the asf header */
1224 if( psz_asf == NULL )
1225 return VLC_EGENERIC;
1227 psz_asf += strlen( psz_marker );
1228 psz_asf = strdup( psz_asf ); /* Duplicate it */
1229 psz_end = strchr( psz_asf, '\n' );
1231 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1234 if( psz_asf >= psz_end )
1237 return VLC_EGENERIC;
1240 /* Always smaller */
1241 p_header = block_New( p_demux, psz_end - psz_asf );
1242 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1243 fprintf( stderr, "Size=%d Hdrb64=%s\n", p_header->i_buffer, psz_asf );
1244 if( p_header->i_buffer <= 0 )
1247 return VLC_EGENERIC;
1250 /* Parse it to get packet size */
1251 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1253 /* Send it to demuxer */
1254 stream_DemuxSend( p_sys->p_out_asf, p_header );
1259 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1260 static int b64_decode( char *dest, char *src )
1262 const char *dest_start = dest;
1266 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1267 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1268 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1269 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1270 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1271 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1272 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1273 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1274 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1275 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1276 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1277 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1278 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1279 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1280 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1281 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1284 for( i_level = 0; *src != '\0'; src++ )
1288 c = b64[(unsigned int)*src];
1300 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1304 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1308 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1316 return dest - dest_start;