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 */
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 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
178 static unsigned char* parseH264ConfigStr( char const* configStr,
179 unsigned int& configSize );
182 /*****************************************************************************
184 *****************************************************************************/
185 static int Open ( vlc_object_t *p_this )
187 demux_t *p_demux = (demux_t*)p_this;
190 MediaSubsessionIterator *iter;
191 MediaSubsession *sub;
193 vlc_bool_t b_rtsp_tcp;
202 /* See if it looks like a SDP
203 v, o, s fields are mandatory and in this order */
204 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 ) return VLC_EGENERIC;
206 if( memcmp( (char*)p_peek, "v=0\r\n", 5 ) &&
207 memcmp( (char*)p_peek, "v=0\n", 4 ) &&
208 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
215 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
218 p_demux->pf_demux = Demux;
219 p_demux->pf_control= Control;
220 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
222 p_sys->scheduler = NULL;
229 p_sys->i_pcr_start = 0;
232 p_sys->p_out_asf = NULL;
233 p_sys->b_no_data = VLC_TRUE;
234 p_sys->i_no_data_ti = 0;
235 p_sys->b_multicast = VLC_FALSE;
236 p_sys->psz_path = p_demux->psz_path;
239 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
241 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
244 if( !( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) )
246 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
250 if( strcasecmp( p_demux->psz_access, "sdp" ) )
252 char *p = p_sys->psz_path;
253 while( (p = strchr( p, ' ' )) != NULL )
257 if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
262 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
263 "VLC Media Player" ) ) == NULL )
265 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
266 p_sys->env->getResultMsg() );
269 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
270 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
272 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
274 delete [] psz_options;
276 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
277 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
280 msg_Err( p_demux, "describeURL failed (%s)",
281 p_sys->env->getResultMsg() );
287 /* malloc-ated copy */
288 p_sys->p_sdp = strdup( (char*)p_sdp );
290 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
292 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
294 p_sys->p_sdp = strdup( p_sys->psz_path );
298 /* Gather the complete sdp file */
301 p_sdp = (uint8_t*)malloc( i_sdp_max );
304 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
305 i_sdp_max - i_sdp - 1 );
309 msg_Err( p_demux, "failed to read SDP" );
316 if( i_read < i_sdp_max - i_sdp - 1 )
323 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
325 p_sys->p_sdp = (char*)p_sdp;
327 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
329 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
331 msg_Err( p_demux, "MediaSession::createNew failed" );
335 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
337 /* Initialise each media subsession */
338 iter = new MediaSubsessionIterator( *p_sys->ms );
339 while( ( sub = iter->next() ) != NULL )
341 unsigned int i_buffer = 0;
344 /* Value taken from mplayer */
345 if( !strcmp( sub->mediumName(), "audio" ) )
347 else if( !strcmp( sub->mediumName(), "video" ) )
352 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
353 bInit = sub->initiate( 4 ); /* Constant ? */
355 bInit = sub->initiate();
359 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
360 sub->mediumName(), sub->codecName(),
361 p_sys->env->getResultMsg() );
365 if( sub->rtpSource() )
367 int fd = sub->rtpSource()->RTPgs()->socketNum();
368 /* Increase the buffer size */
369 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
372 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
375 /* Issue the SETUP */
378 p_sys->rtsp->setupMediaSubsession( *sub, False,
379 b_rtsp_tcp ? True : False );
381 if( !p_sys->b_multicast )
383 /* Check, because we need diff. rollover behaviour for multicast */
384 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
392 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
394 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
399 /* Create all es struct */
401 while( ( sub = iter->next() ) != NULL )
405 if( sub->readSource() == NULL )
410 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
411 tk->p_demux = p_demux;
414 tk->b_quicktime = VLC_FALSE;
415 tk->b_muxed = VLC_FALSE;
416 tk->b_asf = VLC_FALSE;
417 tk->b_rtcp_sync = VLC_FALSE;
418 tk->p_out_muxed = NULL;
420 tk->i_buffer = 65536;
421 tk->p_buffer = (uint8_t *)malloc( 65536 );
423 /* Value taken from mplayer */
424 if( !strcmp( sub->mediumName(), "audio" ) )
426 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
427 tk->fmt.audio.i_channels = sub->numChannels();
428 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
430 if( !strcmp( sub->codecName(), "MPA" ) ||
431 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
432 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
434 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
435 tk->fmt.audio.i_rate = 0;
437 else if( !strcmp( sub->codecName(), "AC3" ) )
439 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
440 tk->fmt.audio.i_rate = 0;
442 else if( !strcmp( sub->codecName(), "L16" ) )
444 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
445 tk->fmt.audio.i_bitspersample = 16;
447 else if( !strcmp( sub->codecName(), "L8" ) )
449 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
450 tk->fmt.audio.i_bitspersample = 8;
452 else if( !strcmp( sub->codecName(), "PCMU" ) )
454 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
456 else if( !strcmp( sub->codecName(), "PCMA" ) )
458 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
460 else if( !strcmp( sub->codecName(), "AMR" ) )
462 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
464 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
466 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
468 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
470 unsigned int i_extra;
473 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
475 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
478 tk->fmt.i_extra = i_extra;
479 tk->fmt.p_extra = malloc( i_extra );
480 memcpy( tk->fmt.p_extra, p_extra, i_extra );
484 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
486 unsigned int i_extra;
489 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
491 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
494 tk->fmt.i_extra = i_extra;
495 tk->fmt.p_extra = malloc( i_extra );
496 memcpy( tk->fmt.p_extra, p_extra, i_extra );
500 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
502 tk->b_asf = VLC_TRUE;
503 if( p_sys->p_out_asf == NULL )
504 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
508 else if( !strcmp( sub->mediumName(), "video" ) )
510 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
511 if( !strcmp( sub->codecName(), "MPV" ) )
513 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
515 else if( !strcmp( sub->codecName(), "H263" ) ||
516 !strcmp( sub->codecName(), "H263-1998" ) ||
517 !strcmp( sub->codecName(), "H263-2000" ) )
519 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
521 else if( !strcmp( sub->codecName(), "H261" ) )
523 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
525 else if( !strcmp( sub->codecName(), "H264" ) )
527 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
528 unsigned int i_extra = 0;
529 uint8_t *p_extra = NULL;
531 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '4' );
532 tk->fmt.b_packetized = VLC_FALSE;
534 /* XXX not the right minimal version I fear */
535 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
536 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
539 tk->fmt.i_extra = i_extra;
540 tk->fmt.p_extra = malloc( i_extra );
541 memcpy( tk->fmt.p_extra, p_extra, i_extra );
547 else if( !strcmp( sub->codecName(), "JPEG" ) )
549 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
551 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
553 unsigned int i_extra;
556 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
558 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
561 tk->fmt.i_extra = i_extra;
562 tk->fmt.p_extra = malloc( i_extra );
563 memcpy( tk->fmt.p_extra, p_extra, i_extra );
567 else if( !strcmp( sub->codecName(), "X-QT" ) ||
568 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
569 !strcmp( sub->codecName(), "X-QDM" ) ||
570 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
571 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
573 tk->b_quicktime = VLC_TRUE;
575 else if( !strcmp( sub->codecName(), "MP2T" ) )
577 tk->b_muxed = VLC_TRUE;
578 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
580 else if( !strcmp( sub->codecName(), "MP2P" ) ||
581 !strcmp( sub->codecName(), "MP1S" ) )
583 tk->b_muxed = VLC_TRUE;
584 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
587 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
589 tk->b_asf = VLC_TRUE;
590 if( p_sys->p_out_asf == NULL )
591 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
596 if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
598 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
601 if( sub->rtcpInstance() != NULL )
603 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
606 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
608 tk->readSource = sub->readSource();
609 tk->rtpSource = sub->rtpSource();
612 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
613 p_sys->track[p_sys->i_track++] = tk;
623 if( p_sys->p_out_asf && ParseASF( p_demux ) )
625 msg_Err( p_demux, "cannot find a usable asf header" );
626 /* TODO Clean tracks */
630 p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
631 if( p_sys->i_length < 0 )
635 else if( p_sys->i_length > 0 )
638 /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
641 if( p_sys->i_track <= 0 )
643 msg_Err( p_demux, "no codec supported, aborting" );
650 if( p_sys->p_out_asf )
652 stream_DemuxDelete( p_sys->p_out_asf );
656 Medium::close( p_sys->ms );
660 Medium::close( p_sys->rtsp );
664 RECLAIM_ENV(p_sys->env);
666 if( p_sys->scheduler )
668 delete p_sys->scheduler;
672 free( p_sys->p_sdp );
674 if( ( p_sys->psz_path != NULL )
675 && ( p_sys->psz_path != p_demux->psz_path ) )
676 free( p_sys->psz_path );
682 /*****************************************************************************
684 *****************************************************************************/
685 static void Close( vlc_object_t *p_this )
687 demux_t *p_demux = (demux_t*)p_this;
688 demux_sys_t *p_sys = p_demux->p_sys;
691 for( i = 0; i < p_sys->i_track; i++ )
693 live_track_t *tk = p_sys->track[i];
695 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
696 free( tk->p_buffer );
700 if( p_sys->i_track ) free( p_sys->track );
701 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
703 if( p_sys->rtsp && p_sys->ms )
706 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
709 Medium::close( p_sys->ms );
711 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
712 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
713 if( p_sys->scheduler ) delete p_sys->scheduler;
714 if( p_sys->p_sdp ) free( p_sys->p_sdp );
715 if( p_sys->psz_path != p_demux->psz_path )
716 free( p_sys->psz_path );
720 /*****************************************************************************
722 *****************************************************************************/
723 static int Demux( demux_t *p_demux )
725 demux_sys_t *p_sys = p_demux->p_sys;
728 vlc_bool_t b_send_pcr = VLC_TRUE;
732 for( i = 0; i < p_sys->i_track; i++ )
734 live_track_t *tk = p_sys->track[i];
736 if( tk->b_asf || tk->b_muxed )
737 b_send_pcr = VLC_FALSE;
743 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
748 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
750 p_sys->i_pcr = i_pcr;
753 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
754 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
755 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
757 p_sys->i_pcr_start = i_pcr;
761 /* First warm we want to read data */
763 for( i = 0; i < p_sys->i_track; i++ )
765 live_track_t *tk = p_sys->track[i];
767 if( tk->waiting == 0 )
770 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
771 StreamRead, tk, StreamClose, tk );
774 /* Create a task that will be called if we wait more than 300ms */
775 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
778 p_sys->scheduler->doEventLoop( &p_sys->event );
780 /* remove the task */
781 p_sys->scheduler->unscheduleDelayedTask( task );
783 /* Check for gap in pts value */
784 for( i = 0; i < p_sys->i_track; i++ )
786 live_track_t *tk = p_sys->track[i];
788 if( !tk->b_muxed && !tk->b_rtcp_sync &&
789 tk->rtpSource && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
791 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
793 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
794 tk->b_rtcp_sync = VLC_TRUE;
796 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
798 p_sys->i_pcr_start = 0;
804 if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
806 /* FIXME Make this configurable
807 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
811 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
813 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
815 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
817 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
818 if( RollOverTcp( p_demux ) )
820 msg_Err( p_demux, "TCP rollover failed, aborting" );
823 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
825 else if( p_sys->i_no_data_ti > 34 )
827 msg_Err( p_demux, "no data received in 10s, aborting" );
831 else if( !p_sys->b_multicast && p_sys->b_no_data&& p_sys->i_no_data_ti > 34 )
834 msg_Warn( p_demux, "no data received in 10s, eof ?" );
838 return p_demux->b_error ? 0 : 1;
841 /*****************************************************************************
843 *****************************************************************************/
844 static int Control( demux_t *p_demux, int i_query, va_list args )
846 demux_sys_t *p_sys = p_demux->p_sys;
849 vlc_bool_t *pb, b_bool;
854 pi64 = (int64_t*)va_arg( args, int64_t * );
855 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
858 case DEMUX_GET_LENGTH:
859 pi64 = (int64_t*)va_arg( args, int64_t * );
860 *pi64 = p_sys->i_length;
863 case DEMUX_GET_POSITION:
864 pf = (double*)va_arg( args, double* );
865 if( p_sys->i_length > 0 )
867 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
868 p_sys->i_start ) / (double)(p_sys->i_length);
876 case DEMUX_SET_POSITION:
880 f = (double)va_arg( args, double );
881 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
883 if( p_sys->rtsp && p_sys->i_length > 0 )
887 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time ) )
889 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
892 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
893 p_sys->i_pcr_start = 0;
896 for( i = 0; i < p_sys->i_track; i++ )
898 p_sys->track[i]->i_pts = 0;
906 /* Special for access_demux */
907 case DEMUX_CAN_PAUSE:
908 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
909 if( p_sys->rtsp && p_sys->i_length )
910 /* Not always true, but will be handled in SET_PAUSE_STATE */
916 case DEMUX_CAN_CONTROL_PACE:
917 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
919 #if 0 /* Disable for now until we have a clock synchro algo
920 * which works with something else than MPEG over UDP */
926 case DEMUX_SET_PAUSE_STATE:
929 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
930 p_sys->i_start ) ) / 1000000.00;
932 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
933 if( p_sys->rtsp == NULL )
936 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
937 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
938 d_npt > 0 ? d_npt : -1 ) ) )
940 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
944 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
945 for( i = 0; i < p_sys->i_track; i++ )
947 p_sys->track[i]->i_pts = 0;
949 p_sys->i_pcr_start = 0; /* FIXME Wrong */
954 case DEMUX_GET_TITLE_INFO:
955 case DEMUX_SET_TITLE:
956 case DEMUX_SET_SEEKPOINT:
959 case DEMUX_GET_PTS_DELAY:
960 pi64 = (int64_t*)va_arg( args, int64_t * );
961 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
969 /*****************************************************************************
970 * RollOverTcp: reopen the rtsp into TCP mode
971 * XXX: ugly, a lot of code are duplicated from Open()
972 *****************************************************************************/
973 static int RollOverTcp( demux_t *p_demux )
975 demux_sys_t *p_sys = p_demux->p_sys;
976 MediaSubsessionIterator *iter;
977 MediaSubsession *sub;
983 /* We close the old RTSP session */
984 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
986 Medium::close( p_sys->ms );
987 Medium::close( p_sys->rtsp );
992 /* Reopen rtsp client */
993 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
994 "VLC Media Player" ) ) == NULL )
996 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
997 p_sys->env->getResultMsg() );
1001 asprintf( &psz_url, "rtsp://%s", p_sys->psz_path );
1003 if( ( psz_options = p_sys->rtsp->sendOptionsCmd( psz_url ) ) )
1004 delete [] psz_options;
1006 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
1007 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
1011 msg_Err( p_demux, "describeURL failed (%s)",
1012 p_sys->env->getResultMsg() );
1013 return VLC_EGENERIC;
1016 /* malloc-ated copy */
1017 p_sys->p_sdp = strdup( (char*)p_sdp );
1020 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
1022 msg_Err( p_demux, "MediaSession::createNew failed" );
1023 return VLC_EGENERIC;
1026 /* Initialise each media subsession */
1027 iter = new MediaSubsessionIterator( *p_sys->ms );
1028 while( ( sub = iter->next() ) != NULL )
1032 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
1033 bInit = sub->initiate( 4 ); /* Constant ? */
1035 bInit = sub->initiate();
1039 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
1040 sub->mediumName(), sub->codecName(),
1041 p_sys->env->getResultMsg() );
1044 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
1047 /* Issue the SETUP */
1048 p_sys->rtsp->setupMediaSubsession( *sub, False, True /* tcp */ );
1052 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
1054 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
1055 return VLC_EGENERIC;
1058 /* Update all tracks */
1061 while( ( sub = iter->next() ) != NULL )
1065 if( sub->readSource() == NULL )
1067 if( i_tk >= p_sys->i_track )
1069 msg_Err( p_demux, "WTF !" );
1073 tk = p_sys->track[i_tk];
1078 tk->b_rtcp_sync = VLC_FALSE;
1080 if( sub->rtcpInstance() != NULL )
1081 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1083 tk->readSource = sub->readSource();
1084 tk->rtpSource = sub->rtpSource();
1095 /*****************************************************************************
1097 *****************************************************************************/
1098 static void StreamRead( void *p_private, unsigned int i_size,
1099 unsigned int i_truncated_bytes, struct timeval pts,
1100 unsigned int duration )
1102 live_track_t *tk = (live_track_t*)p_private;
1103 demux_t *p_demux = tk->p_demux;
1104 demux_sys_t *p_sys = p_demux->p_sys;
1107 mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1108 (uint64_t)pts.tv_usec;
1110 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1111 i_pts &= UI64C(0x00ffffffffffffff);
1113 if( tk->b_quicktime && tk->p_es == NULL )
1115 QuickTimeGenericRTPSource *qtRTPSource =
1116 (QuickTimeGenericRTPSource*)tk->rtpSource;
1117 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1118 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1120 if( qtState.sdAtomSize < 16 + 32 )
1123 p_sys->event = 0xff;
1127 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1128 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1129 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1131 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1132 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1133 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1135 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1139 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1141 pts.tv_sec * 1000000LL + pts.tv_usec );
1144 /* grow buffer if it looks like buffer is too small, but don't eat
1145 * up all the memory on strange streams */
1146 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1149 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1150 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1152 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1155 msg_Warn( p_demux, "realloc failed" );
1159 tk->p_buffer = (uint8_t*)p_tmp;
1162 if( i_size > tk->i_buffer )
1164 msg_Warn( p_demux, "buffer overflow" );
1166 /* FIXME could i_size be > buffer size ? */
1167 if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1169 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1170 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1171 uint32_t header = h261Source->lastSpecialHeader();
1173 uint32_t header = 0;
1174 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1176 p_block = block_New( p_demux, i_size + 4 );
1177 memcpy( p_block->p_buffer, &header, 4 );
1178 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1180 if( tk->rtpSource->curPacketMarkerBit() )
1181 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1183 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','4') )
1185 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1186 msg_Warn( p_demux, "unsupported NAL type for H264" );
1188 /* Normal NAL type */
1189 p_block = block_New( p_demux, i_size + 4 );
1190 p_block->p_buffer[0] = 0x00;
1191 p_block->p_buffer[1] = 0x00;
1192 p_block->p_buffer[2] = 0x00;
1193 p_block->p_buffer[3] = 0x01;
1194 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1196 else if( tk->b_asf )
1198 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1199 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1201 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1205 p_block = block_New( p_demux, i_size );
1206 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1209 //p_block->i_rate = p_input->stream.control.i_rate;
1211 if( i_pts != tk->i_pts && !tk->b_muxed )
1213 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1214 p_block->i_pts = i_pts;
1216 //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );
1220 stream_DemuxSend( tk->p_out_muxed, p_block );
1222 else if( tk->b_asf )
1224 stream_DemuxSend( p_sys->p_out_asf, p_block );
1228 es_out_Send( p_demux->out, tk->p_es, p_block );
1231 /* warm that's ok */
1232 p_sys->event = 0xff;
1234 /* we have read data */
1236 p_demux->p_sys->b_no_data = VLC_FALSE;
1237 p_demux->p_sys->i_no_data_ti = 0;
1239 if( i_pts > 0 && !tk->b_muxed )
1245 /*****************************************************************************
1247 *****************************************************************************/
1248 static void StreamClose( void *p_private )
1250 live_track_t *tk = (live_track_t*)p_private;
1251 demux_t *p_demux = tk->p_demux;
1252 demux_sys_t *p_sys = p_demux->p_sys;
1254 fprintf( stderr, "StreamClose\n" );
1256 p_sys->event = 0xff;
1257 p_demux->b_error = VLC_TRUE;
1261 /*****************************************************************************
1263 *****************************************************************************/
1264 static void TaskInterrupt( void *p_private )
1266 demux_t *p_demux = (demux_t*)p_private;
1268 p_demux->p_sys->i_no_data_ti++;
1271 p_demux->p_sys->event = 0xff;
1274 /*****************************************************************************
1276 *****************************************************************************/
1277 static int b64_decode( char *dest, char *src );
1279 static int ParseASF( demux_t *p_demux )
1281 demux_sys_t *p_sys = p_demux->p_sys;
1283 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1284 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1288 /* Parse the asf header */
1289 if( psz_asf == NULL )
1290 return VLC_EGENERIC;
1292 psz_asf += strlen( psz_marker );
1293 psz_asf = strdup( psz_asf ); /* Duplicate it */
1294 psz_end = strchr( psz_asf, '\n' );
1296 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1299 if( psz_asf >= psz_end )
1302 return VLC_EGENERIC;
1305 /* Always smaller */
1306 p_header = block_New( p_demux, psz_end - psz_asf );
1307 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1308 fprintf( stderr, "Size=%d Hdrb64=%s\n", p_header->i_buffer, psz_asf );
1309 if( p_header->i_buffer <= 0 )
1312 return VLC_EGENERIC;
1315 /* Parse it to get packet size */
1316 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1318 /* Send it to demuxer */
1319 stream_DemuxSend( p_sys->p_out_asf, p_header );
1325 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
1326 static unsigned char* parseH264ConfigStr( char const* configStr,
1327 unsigned int& configSize )
1334 if( configStr == NULL || *configStr == '\0' )
1337 psz = dup = strdup( configStr );
1339 unsigned char *cfg = new unsigned char[5 * strlen(psz)];
1342 char *p = strchr( psz, ',' );
1346 cfg[configSize++] = 0x00;
1347 cfg[configSize++] = 0x00;
1348 cfg[configSize++] = 0x00;
1349 cfg[configSize++] = 0x01;
1350 configSize += b64_decode( (char*)&cfg[configSize], psz );
1357 if( dup ) free( dup );
1362 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1363 static int b64_decode( char *dest, char *src )
1365 const char *dest_start = dest;
1369 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1370 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1371 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1372 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1373 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1374 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1375 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1376 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1377 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1378 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1379 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1380 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1381 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1382 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1383 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1384 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1387 for( i_level = 0; *src != '\0'; src++ )
1391 c = b64[(unsigned int)*src];
1403 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1407 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1411 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1419 return dest - dest_start;