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 = strdup(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 ) *p = '+';
256 if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
261 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
262 "VLC Media Player" ) ) == NULL )
264 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
265 p_sys->env->getResultMsg() );
268 psz_url = (char*)malloc( strlen( p_sys->psz_path ) + 8 );
269 sprintf( psz_url, "rtsp://%s", p_sys->psz_path );
271 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
272 if( psz_options ) delete [] psz_options;
274 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
275 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
278 msg_Err( p_demux, "describeURL failed (%s)",
279 p_sys->env->getResultMsg() );
285 /* malloc-ated copy */
286 p_sys->p_sdp = strdup( (char*)p_sdp );
288 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
290 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
292 p_sys->p_sdp = strdup( p_sys->psz_path );
296 /* Gather the complete sdp file */
299 p_sdp = (uint8_t*)malloc( i_sdp_max );
302 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp],
303 i_sdp_max - i_sdp - 1 );
307 msg_Err( p_demux, "failed to read SDP" );
314 if( i_read < i_sdp_max - i_sdp - 1 )
321 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
323 p_sys->p_sdp = (char*)p_sdp;
325 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
327 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
329 msg_Err( p_demux, "MediaSession::createNew failed" );
333 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
335 /* Initialise each media subsession */
336 iter = new MediaSubsessionIterator( *p_sys->ms );
337 while( ( sub = iter->next() ) != NULL )
339 unsigned int i_buffer = 0;
342 /* Value taken from mplayer */
343 if( !strcmp( sub->mediumName(), "audio" ) )
345 else if( !strcmp( sub->mediumName(), "video" ) )
350 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
351 bInit = sub->initiate( 4 ); /* Constant ? */
353 bInit = sub->initiate();
357 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
358 sub->mediumName(), sub->codecName(),
359 p_sys->env->getResultMsg() );
363 if( sub->rtpSource() )
365 int fd = sub->rtpSource()->RTPgs()->socketNum();
366 /* Increase the buffer size */
367 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
370 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
373 /* Issue the SETUP */
376 p_sys->rtsp->setupMediaSubsession( *sub, False,
377 b_rtsp_tcp ? True : False );
379 if( !p_sys->b_multicast )
381 /* Check, because we need diff. rollover behaviour for multicast */
382 p_sys->b_multicast = IsMulticastAddress( sub->connectionEndpointAddress() );
390 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
392 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
397 /* Create all es struct */
399 while( ( sub = iter->next() ) != NULL )
403 if( sub->readSource() == NULL ) continue;
405 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
406 tk->p_demux = p_demux;
409 tk->b_quicktime = VLC_FALSE;
410 tk->b_muxed = VLC_FALSE;
411 tk->b_asf = VLC_FALSE;
412 tk->b_rtcp_sync = VLC_FALSE;
413 tk->p_out_muxed = NULL;
415 tk->i_buffer = 65536;
416 tk->p_buffer = (uint8_t *)malloc( 65536 );
418 /* Value taken from mplayer */
419 if( !strcmp( sub->mediumName(), "audio" ) )
421 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('u','n','d','f') );
422 tk->fmt.audio.i_channels = sub->numChannels();
423 tk->fmt.audio.i_rate = sub->rtpTimestampFrequency();
425 if( !strcmp( sub->codecName(), "MPA" ) ||
426 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
427 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
429 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
430 tk->fmt.audio.i_rate = 0;
432 else if( !strcmp( sub->codecName(), "AC3" ) )
434 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
435 tk->fmt.audio.i_rate = 0;
437 else if( !strcmp( sub->codecName(), "L16" ) )
439 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
440 tk->fmt.audio.i_bitspersample = 16;
442 else if( !strcmp( sub->codecName(), "L8" ) )
444 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
445 tk->fmt.audio.i_bitspersample = 8;
447 else if( !strcmp( sub->codecName(), "PCMU" ) )
449 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
451 else if( !strcmp( sub->codecName(), "PCMA" ) )
453 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
455 else if( !strcmp( sub->codecName(), "AMR" ) )
457 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'm', 'r' );
459 else if( !strcmp( sub->codecName(), "AMR-WB" ) )
461 tk->fmt.i_codec = VLC_FOURCC( 's', 'a', 'w', 'b' );
463 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
465 unsigned int i_extra;
468 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
470 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(),
473 tk->fmt.i_extra = i_extra;
474 tk->fmt.p_extra = malloc( i_extra );
475 memcpy( tk->fmt.p_extra, p_extra, i_extra );
479 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
481 unsigned int i_extra;
484 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
486 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
489 tk->fmt.i_extra = i_extra;
490 tk->fmt.p_extra = malloc( i_extra );
491 memcpy( tk->fmt.p_extra, p_extra, i_extra );
495 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
497 tk->b_asf = VLC_TRUE;
498 if( p_sys->p_out_asf == NULL )
499 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
503 else if( !strcmp( sub->mediumName(), "video" ) )
505 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('u','n','d','f') );
506 if( !strcmp( sub->codecName(), "MPV" ) )
508 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
510 else if( !strcmp( sub->codecName(), "H263" ) ||
511 !strcmp( sub->codecName(), "H263-1998" ) ||
512 !strcmp( sub->codecName(), "H263-2000" ) )
514 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
516 else if( !strcmp( sub->codecName(), "H261" ) )
518 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
520 else if( !strcmp( sub->codecName(), "H264" ) )
522 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
523 unsigned int i_extra = 0;
524 uint8_t *p_extra = NULL;
526 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '4' );
527 tk->fmt.b_packetized = VLC_FALSE;
529 /* XXX not the right minimal version I fear */
530 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
531 if((p_extra=parseH264ConfigStr( sub->fmtp_spropparametersets(),
534 tk->fmt.i_extra = i_extra;
535 tk->fmt.p_extra = malloc( i_extra );
536 memcpy( tk->fmt.p_extra, p_extra, i_extra );
542 else if( !strcmp( sub->codecName(), "JPEG" ) )
544 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
546 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
548 unsigned int i_extra;
551 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
553 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(),
556 tk->fmt.i_extra = i_extra;
557 tk->fmt.p_extra = malloc( i_extra );
558 memcpy( tk->fmt.p_extra, p_extra, i_extra );
562 else if( !strcmp( sub->codecName(), "X-QT" ) ||
563 !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
564 !strcmp( sub->codecName(), "X-QDM" ) ||
565 !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
566 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
568 tk->b_quicktime = VLC_TRUE;
570 else if( !strcmp( sub->codecName(), "MP2T" ) )
572 tk->b_muxed = VLC_TRUE;
573 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
575 else if( !strcmp( sub->codecName(), "MP2P" ) ||
576 !strcmp( sub->codecName(), "MP1S" ) )
578 tk->b_muxed = VLC_TRUE;
579 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps",
582 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
584 tk->b_asf = VLC_TRUE;
585 if( p_sys->p_out_asf == NULL )
586 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf",
591 if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
593 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
596 if( sub->rtcpInstance() != NULL )
598 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
601 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
603 tk->readSource = sub->readSource();
604 tk->rtpSource = sub->rtpSource();
607 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
608 p_sys->track[p_sys->i_track++] = tk;
618 if( p_sys->p_out_asf && ParseASF( p_demux ) )
620 msg_Err( p_demux, "cannot find a usable asf header" );
621 /* TODO Clean tracks */
625 p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
626 if( p_sys->i_length < 0 )
630 else if( p_sys->i_length > 0 )
633 /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
636 if( p_sys->i_track <= 0 )
638 msg_Err( p_demux, "no codec supported, aborting" );
645 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
646 if( p_sys->ms ) Medium::close( p_sys->ms );
647 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
648 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
649 if( p_sys->scheduler ) delete p_sys->scheduler;
650 if( p_sys->p_sdp ) free( p_sys->p_sdp );
651 if( p_sys->psz_path ) free( p_sys->psz_path );
657 /*****************************************************************************
659 *****************************************************************************/
660 static void Close( vlc_object_t *p_this )
662 demux_t *p_demux = (demux_t*)p_this;
663 demux_sys_t *p_sys = p_demux->p_sys;
666 for( i = 0; i < p_sys->i_track; i++ )
668 live_track_t *tk = p_sys->track[i];
670 if( tk->b_muxed ) stream_DemuxDelete( tk->p_out_muxed );
671 free( tk->p_buffer );
675 if( p_sys->i_track ) free( p_sys->track );
676 if( p_sys->p_out_asf ) stream_DemuxDelete( p_sys->p_out_asf );
678 if( p_sys->rtsp && p_sys->ms )
681 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
684 Medium::close( p_sys->ms );
686 if( p_sys->rtsp ) Medium::close( p_sys->rtsp );
687 if( p_sys->env ) RECLAIM_ENV(p_sys->env);
688 if( p_sys->scheduler ) delete p_sys->scheduler;
689 if( p_sys->p_sdp ) free( p_sys->p_sdp );
690 if( p_sys->psz_path ) free( p_sys->psz_path );
694 /*****************************************************************************
696 *****************************************************************************/
697 static int Demux( demux_t *p_demux )
699 demux_sys_t *p_sys = p_demux->p_sys;
702 vlc_bool_t b_send_pcr = VLC_TRUE;
706 for( i = 0; i < p_sys->i_track; i++ )
708 live_track_t *tk = p_sys->track[i];
710 if( tk->b_asf || tk->b_muxed )
711 b_send_pcr = VLC_FALSE;
717 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
722 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
724 p_sys->i_pcr = i_pcr;
727 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
728 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
729 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
731 p_sys->i_pcr_start = i_pcr;
735 /* First warm we want to read data */
737 for( i = 0; i < p_sys->i_track; i++ )
739 live_track_t *tk = p_sys->track[i];
741 if( tk->waiting == 0 )
744 tk->readSource->getNextFrame( tk->p_buffer, tk->i_buffer,
745 StreamRead, tk, StreamClose, tk );
748 /* Create a task that will be called if we wait more than 300ms */
749 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
752 p_sys->scheduler->doEventLoop( &p_sys->event );
754 /* remove the task */
755 p_sys->scheduler->unscheduleDelayedTask( task );
757 /* Check for gap in pts value */
758 for( i = 0; i < p_sys->i_track; i++ )
760 live_track_t *tk = p_sys->track[i];
762 if( !tk->b_muxed && !tk->b_rtcp_sync &&
763 tk->rtpSource && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
765 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
767 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
768 tk->b_rtcp_sync = VLC_TRUE;
770 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
772 p_sys->i_pcr_start = 0;
778 if( p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 120 )
780 /* FIXME Make this configurable
781 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
785 else if( !p_sys->b_multicast && p_sys->b_no_data && p_sys->i_no_data_ti > 34 )
787 vlc_bool_t b_rtsp_tcp = var_GetBool( p_demux, "rtsp-tcp" );
789 if( !b_rtsp_tcp && p_sys->rtsp && p_sys->ms )
791 msg_Warn( p_demux, "no data received in 10s. Switching to TCP" );
792 if( RollOverTcp( p_demux ) )
794 msg_Err( p_demux, "TCP rollover failed, aborting" );
797 var_SetBool( p_demux, "rtsp-tcp", VLC_TRUE );
799 else if( p_sys->i_no_data_ti > 34 )
801 msg_Err( p_demux, "no data received in 10s, aborting" );
805 else if( !p_sys->b_multicast && p_sys->b_no_data&& p_sys->i_no_data_ti > 34 )
808 msg_Warn( p_demux, "no data received in 10s, eof ?" );
812 return p_demux->b_error ? 0 : 1;
815 /*****************************************************************************
817 *****************************************************************************/
818 static int Control( demux_t *p_demux, int i_query, va_list args )
820 demux_sys_t *p_sys = p_demux->p_sys;
823 vlc_bool_t *pb, b_bool;
828 pi64 = (int64_t*)va_arg( args, int64_t * );
829 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
832 case DEMUX_GET_LENGTH:
833 pi64 = (int64_t*)va_arg( args, int64_t * );
834 *pi64 = p_sys->i_length;
837 case DEMUX_GET_POSITION:
838 pf = (double*)va_arg( args, double* );
839 if( p_sys->i_length > 0 )
841 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start +
842 p_sys->i_start ) / (double)(p_sys->i_length);
850 case DEMUX_SET_POSITION:
854 f = (double)va_arg( args, double );
855 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
857 if( p_sys->rtsp && p_sys->i_length > 0 )
861 if( !p_sys->rtsp->playMediaSession( *p_sys->ms, time ) )
863 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
866 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
867 p_sys->i_pcr_start = 0;
870 for( i = 0; i < p_sys->i_track; i++ )
872 p_sys->track[i]->i_pts = 0;
880 /* Special for access_demux */
881 case DEMUX_CAN_PAUSE:
882 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
883 if( p_sys->rtsp && p_sys->i_length )
884 /* Not always true, but will be handled in SET_PAUSE_STATE */
890 case DEMUX_CAN_CONTROL_PACE:
891 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
893 #if 0 /* Disable for now until we have a clock synchro algo
894 * which works with something else than MPEG over UDP */
900 case DEMUX_SET_PAUSE_STATE:
903 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start +
904 p_sys->i_start ) ) / 1000000.00;
906 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
907 if( p_sys->rtsp == NULL )
910 if( ( b_bool && !p_sys->rtsp->pauseMediaSession( *p_sys->ms ) ) ||
911 ( !b_bool && !p_sys->rtsp->playMediaSession( *p_sys->ms,
912 d_npt > 0 ? d_npt : -1 ) ) )
914 msg_Err( p_demux, "PLAY or PAUSE failed %s", p_sys->env->getResultMsg() );
918 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
919 for( i = 0; i < p_sys->i_track; i++ )
921 p_sys->track[i]->i_pts = 0;
923 p_sys->i_pcr_start = 0; /* FIXME Wrong */
928 case DEMUX_GET_TITLE_INFO:
929 case DEMUX_SET_TITLE:
930 case DEMUX_SET_SEEKPOINT:
933 case DEMUX_GET_PTS_DELAY:
934 pi64 = (int64_t*)va_arg( args, int64_t * );
935 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * 1000;
943 /*****************************************************************************
944 * RollOverTcp: reopen the rtsp into TCP mode
945 * XXX: ugly, a lot of code are duplicated from Open()
946 *****************************************************************************/
947 static int RollOverTcp( demux_t *p_demux )
949 demux_sys_t *p_sys = p_demux->p_sys;
950 MediaSubsessionIterator *iter;
951 MediaSubsession *sub;
957 /* We close the old RTSP session */
958 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
960 Medium::close( p_sys->ms );
961 Medium::close( p_sys->rtsp );
966 /* Reopen rtsp client */
967 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/,
968 "VLC Media Player" ) ) == NULL )
970 msg_Err( p_demux, "RTSPClient::createNew failed (%s)",
971 p_sys->env->getResultMsg() );
975 asprintf( &psz_url, "rtsp://%s", p_sys->psz_path );
977 if( ( psz_options = p_sys->rtsp->sendOptionsCmd( psz_url ) ) )
978 delete [] psz_options;
980 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
981 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
985 msg_Err( p_demux, "describeURL failed (%s)",
986 p_sys->env->getResultMsg() );
990 /* malloc-ated copy */
991 p_sys->p_sdp = strdup( (char*)p_sdp );
994 if( !( p_sys->ms = MediaSession::createNew( *p_sys->env, p_sys->p_sdp ) ) )
996 msg_Err( p_demux, "MediaSession::createNew failed" );
1000 /* Initialise each media subsession */
1001 iter = new MediaSubsessionIterator( *p_sys->ms );
1002 while( ( sub = iter->next() ) != NULL )
1006 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
1007 bInit = sub->initiate( 4 ); /* Constant ? */
1009 bInit = sub->initiate();
1013 msg_Warn( p_demux, "RTP subsession '%s/%s' failed (%s)",
1014 sub->mediumName(), sub->codecName(),
1015 p_sys->env->getResultMsg() );
1018 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(),
1021 /* Issue the SETUP */
1022 p_sys->rtsp->setupMediaSubsession( *sub, False, True /* tcp */ );
1026 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
1028 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
1029 return VLC_EGENERIC;
1032 /* Update all tracks */
1035 while( ( sub = iter->next() ) != NULL )
1039 if( sub->readSource() == NULL )
1041 if( i_tk >= p_sys->i_track )
1043 msg_Err( p_demux, "WTF !" );
1047 tk = p_sys->track[i_tk];
1052 tk->b_rtcp_sync = VLC_FALSE;
1054 if( sub->rtcpInstance() != NULL )
1055 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
1057 tk->readSource = sub->readSource();
1058 tk->rtpSource = sub->rtpSource();
1069 /*****************************************************************************
1071 *****************************************************************************/
1072 static void StreamRead( void *p_private, unsigned int i_size,
1073 unsigned int i_truncated_bytes, struct timeval pts,
1074 unsigned int duration )
1076 live_track_t *tk = (live_track_t*)p_private;
1077 demux_t *p_demux = tk->p_demux;
1078 demux_sys_t *p_sys = p_demux->p_sys;
1081 mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) +
1082 (uint64_t)pts.tv_usec;
1084 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1085 i_pts &= UI64C(0x00ffffffffffffff);
1087 if( tk->b_quicktime && tk->p_es == NULL )
1089 QuickTimeGenericRTPSource *qtRTPSource =
1090 (QuickTimeGenericRTPSource*)tk->rtpSource;
1091 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
1092 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
1094 if( qtState.sdAtomSize < 16 + 32 )
1097 p_sys->event = 0xff;
1101 tk->fmt.i_codec = VLC_FOURCC(sdAtom[0],sdAtom[1],sdAtom[2],sdAtom[3]);
1102 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
1103 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
1105 tk->fmt.i_extra = qtState.sdAtomSize - 16;
1106 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1107 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
1109 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1113 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
1115 pts.tv_sec * 1000000LL + pts.tv_usec );
1118 /* grow buffer if it looks like buffer is too small, but don't eat
1119 * up all the memory on strange streams */
1120 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
1123 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
1124 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
1126 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
1129 msg_Warn( p_demux, "realloc failed" );
1133 tk->p_buffer = (uint8_t*)p_tmp;
1136 if( i_size > tk->i_buffer )
1138 msg_Warn( p_demux, "buffer overflow" );
1140 /* FIXME could i_size be > buffer size ? */
1141 if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','1') )
1143 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1144 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
1145 uint32_t header = h261Source->lastSpecialHeader();
1147 uint32_t header = 0;
1148 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
1150 p_block = block_New( p_demux, i_size + 4 );
1151 memcpy( p_block->p_buffer, &header, 4 );
1152 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
1154 if( tk->rtpSource->curPacketMarkerBit() )
1155 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
1157 else if( tk->fmt.i_codec == VLC_FOURCC('H','2','6','4') )
1159 if( (tk->p_buffer[0] & 0x1f) >= 24 )
1160 msg_Warn( p_demux, "unsupported NAL type for H264" );
1162 /* Normal NAL type */
1163 p_block = block_New( p_demux, i_size + 4 );
1164 p_block->p_buffer[0] = 0x00;
1165 p_block->p_buffer[1] = 0x00;
1166 p_block->p_buffer[2] = 0x00;
1167 p_block->p_buffer[3] = 0x01;
1168 memcpy( &p_block->p_buffer[4], tk->p_buffer, i_size );
1170 else if( tk->b_asf )
1172 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, (int)i_size );
1173 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
1175 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
1179 p_block = block_New( p_demux, i_size );
1180 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
1183 //p_block->i_rate = p_input->stream.control.i_rate;
1185 if( i_pts != tk->i_pts && !tk->b_muxed )
1187 p_block->i_dts = ( tk->fmt.i_cat == VIDEO_ES ) ? 0 : i_pts;
1188 p_block->i_pts = i_pts;
1190 //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );
1194 stream_DemuxSend( tk->p_out_muxed, p_block );
1196 else if( tk->b_asf )
1198 stream_DemuxSend( p_sys->p_out_asf, p_block );
1202 es_out_Send( p_demux->out, tk->p_es, p_block );
1205 /* warm that's ok */
1206 p_sys->event = 0xff;
1208 /* we have read data */
1210 p_demux->p_sys->b_no_data = VLC_FALSE;
1211 p_demux->p_sys->i_no_data_ti = 0;
1213 if( i_pts > 0 && !tk->b_muxed )
1219 /*****************************************************************************
1221 *****************************************************************************/
1222 static void StreamClose( void *p_private )
1224 live_track_t *tk = (live_track_t*)p_private;
1225 demux_t *p_demux = tk->p_demux;
1226 demux_sys_t *p_sys = p_demux->p_sys;
1228 fprintf( stderr, "StreamClose\n" );
1230 p_sys->event = 0xff;
1231 p_demux->b_error = VLC_TRUE;
1235 /*****************************************************************************
1237 *****************************************************************************/
1238 static void TaskInterrupt( void *p_private )
1240 demux_t *p_demux = (demux_t*)p_private;
1242 p_demux->p_sys->i_no_data_ti++;
1245 p_demux->p_sys->event = 0xff;
1248 /*****************************************************************************
1250 *****************************************************************************/
1251 static int b64_decode( char *dest, char *src );
1253 static int ParseASF( demux_t *p_demux )
1255 demux_sys_t *p_sys = p_demux->p_sys;
1257 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1258 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1262 /* Parse the asf header */
1263 if( psz_asf == NULL )
1264 return VLC_EGENERIC;
1266 psz_asf += strlen( psz_marker );
1267 psz_asf = strdup( psz_asf ); /* Duplicate it */
1268 psz_end = strchr( psz_asf, '\n' );
1270 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1273 if( psz_asf >= psz_end )
1276 return VLC_EGENERIC;
1279 /* Always smaller */
1280 p_header = block_New( p_demux, psz_end - psz_asf );
1281 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1282 fprintf( stderr, "Size=%d Hdrb64=%s\n", p_header->i_buffer, psz_asf );
1283 if( p_header->i_buffer <= 0 )
1286 return VLC_EGENERIC;
1289 /* Parse it to get packet size */
1290 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1292 /* Send it to demuxer */
1293 stream_DemuxSend( p_sys->p_out_asf, p_header );
1299 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1117756800
1300 static unsigned char* parseH264ConfigStr( char const* configStr,
1301 unsigned int& configSize )
1308 if( configStr == NULL || *configStr == '\0' )
1311 psz = dup = strdup( configStr );
1313 unsigned char *cfg = new unsigned char[5 * strlen(psz)];
1316 char *p = strchr( psz, ',' );
1320 cfg[configSize++] = 0x00;
1321 cfg[configSize++] = 0x00;
1322 cfg[configSize++] = 0x00;
1323 cfg[configSize++] = 0x01;
1324 configSize += b64_decode( (char*)&cfg[configSize], psz );
1331 if( dup ) free( dup );
1336 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1337 static int b64_decode( char *dest, char *src )
1339 const char *dest_start = dest;
1343 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1344 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1345 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1346 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1347 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1348 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1349 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1350 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1351 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1352 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1353 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1354 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1355 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1356 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1357 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1358 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1361 for( i_level = 0; *src != '\0'; src++ )
1365 c = b64[(unsigned int)*src];
1377 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1381 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1385 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1393 return dest - dest_start;