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()
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 speaks an old and unstandard dialect of RTSP. " \
68 "When you set this parameter, VLC will try this dialect for communication. In " \
69 "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_name( "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;
134 char *p_sdp; /* XXX mallocated */
137 TaskScheduler *scheduler;
138 UsageEnvironment *env ;
143 live_track_t **track; /* XXX mallocated */
158 static int Demux ( demux_t * );
159 static int Control( demux_t *, int, va_list );
161 static int ParseASF( demux_t * );
163 static void StreamRead( void *p_private, unsigned int i_size, unsigned int i_truncated_bytes, struct timeval pts, unsigned int i_duration );
164 static void StreamClose( void *p_private );
165 static void TaskInterrupt( void *p_private );
168 /*****************************************************************************
170 *****************************************************************************/
171 static int Open ( vlc_object_t *p_this )
173 demux_t *p_demux = (demux_t*)p_this;
176 MediaSubsessionIterator *iter;
177 MediaSubsession *sub;
179 vlc_bool_t b_rtsp_tcp;
188 /* See if it looks like a SDP
189 v, o, s fields are mandatory and in this order */
190 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 )
192 msg_Err( p_demux, "cannot peek" );
195 if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) && strncmp( (char*)p_peek, "v=0\n", 4 ) &&
196 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
198 msg_Warn( p_demux, "SDP module discarded" );
204 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
207 p_demux->pf_demux = Demux;
208 p_demux->pf_control= Control;
209 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
211 p_sys->scheduler = NULL;
218 p_sys->i_pcr_start = 0;
221 p_sys->p_out_asf = NULL;
224 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
226 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
229 if( ( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) == NULL )
231 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
235 if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
240 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/, "VLC Media Player" ) ) == NULL )
242 msg_Err( p_demux, "RTSPClient::createNew failed (%s)", p_sys->env->getResultMsg() );
245 psz_url = (char*)malloc( strlen( p_demux->psz_path ) + 8 );
246 sprintf( psz_url, "rtsp://%s", p_demux->psz_path );
248 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
250 delete [] psz_options;
252 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url,
253 NULL, var_CreateGetBool( p_demux, "rtsp-kasenna" ) );
256 msg_Err( p_demux, "describeURL failed (%s)", p_sys->env->getResultMsg() );
263 /* malloc-ated copy */
264 p_sys->p_sdp = strdup( (char*)p_sdp );
266 fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
268 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
270 p_sys->p_sdp = strdup( p_demux->psz_path );
274 /* Gather the complete sdp file */
277 p_sdp = (uint8_t*)malloc( i_sdp_max );
280 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp], i_sdp_max - i_sdp - 1 );
284 msg_Err( p_demux, "failed to read SDP" );
291 if( i_read < i_sdp_max - i_sdp - 1 )
298 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
300 p_sys->p_sdp = (char*)p_sdp;
302 msg_Dbg( p_demux, "sdp=%s\n", p_sys->p_sdp );
304 if( ( p_sys->ms = MediaSession::createNew(*p_sys->env, p_sys->p_sdp ) ) == NULL )
306 msg_Err( p_demux, "MediaSession::createNew failed" );
310 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
312 /* Initialise each media subsession */
313 iter = new MediaSubsessionIterator( *p_sys->ms );
314 while( ( sub = iter->next() ) != NULL )
316 unsigned int i_buffer = 0;
319 /* Value taken from mplayer */
320 if( !strcmp( sub->mediumName(), "audio" ) )
322 else if( !strcmp( sub->mediumName(), "video" ) )
327 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
328 bInit = sub->initiate( 4 ); /* Constant ? */
330 bInit = sub->initiate();
334 msg_Warn( p_demux, "RTP subsession '%s/%s' failed(%s)", sub->mediumName(), sub->codecName(), p_sys->env->getResultMsg() );
338 if( sub->rtpSource() )
340 int fd = sub->rtpSource()->RTPgs()->socketNum();
341 /* Increase the buffer size */
342 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
345 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(), sub->codecName() );
347 /* Issue the SETUP */
350 p_sys->rtsp->setupMediaSubsession( *sub, False, b_rtsp_tcp ? True : False );
358 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
360 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
365 /* Create all es struct */
367 while( ( sub = iter->next() ) != NULL )
371 if( sub->readSource() == NULL )
376 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
377 tk->p_demux = p_demux;
380 tk->b_quicktime = VLC_FALSE;
381 tk->b_muxed = VLC_FALSE;
382 tk->b_asf = VLC_FALSE;
383 tk->b_rtcp_sync = VLC_FALSE;
384 tk->p_out_muxed = NULL;
386 tk->i_buffer = 65536;
387 tk->p_buffer = (uint8_t *)malloc( 65536 );
389 /* Value taken from mplayer */
390 if( !strcmp( sub->mediumName(), "audio" ) )
392 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
393 tk->fmt.audio.i_channels = sub->numChannels();
394 tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
396 if( !strcmp( sub->codecName(), "MPA" ) ||
397 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
398 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
400 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
401 tk->fmt.audio.i_rate = 0;
403 else if( !strcmp( sub->codecName(), "AC3" ) )
405 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
406 tk->fmt.audio.i_rate = 0;
408 else if( !strcmp( sub->codecName(), "L16" ) )
410 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
411 tk->fmt.audio.i_bitspersample = 16;
413 else if( !strcmp( sub->codecName(), "L8" ) )
415 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
416 tk->fmt.audio.i_bitspersample = 8;
418 else if( !strcmp( sub->codecName(), "PCMU" ) )
420 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
422 else if( !strcmp( sub->codecName(), "PCMA" ) )
424 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
426 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
428 unsigned int i_extra;
431 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
433 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(), i_extra ) ) )
435 tk->fmt.i_extra = i_extra;
436 tk->fmt.p_extra = malloc( i_extra );
437 memcpy( tk->fmt.p_extra, p_extra, i_extra );
441 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
443 unsigned int i_extra;
446 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
448 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(), i_extra ) ) )
450 tk->fmt.i_extra = i_extra;
451 tk->fmt.p_extra = malloc( i_extra );
452 memcpy( tk->fmt.p_extra, p_extra, i_extra );
456 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
458 tk->b_asf = VLC_TRUE;
459 if( p_sys->p_out_asf == NULL )
460 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf", p_demux->out );;
463 else if( !strcmp( sub->mediumName(), "video" ) )
465 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
466 if( !strcmp( sub->codecName(), "MPV" ) )
468 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
470 else if( !strcmp( sub->codecName(), "H263" ) ||
471 !strcmp( sub->codecName(), "H263-1998" ) ||
472 !strcmp( sub->codecName(), "H263-2000" ) )
474 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '3' );
476 else if( !strcmp( sub->codecName(), "H261" ) )
478 tk->fmt.i_codec = VLC_FOURCC( 'H', '2', '6', '1' );
480 else if( !strcmp( sub->codecName(), "JPEG" ) )
482 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
484 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
486 unsigned int i_extra;
489 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
491 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(), i_extra ) ) )
493 tk->fmt.i_extra = i_extra;
494 tk->fmt.p_extra = malloc( i_extra );
495 memcpy( tk->fmt.p_extra, p_extra, i_extra );
499 else if( !strcmp( sub->codecName(), "X-QT" ) || !strcmp( sub->codecName(), "X-QUICKTIME" ) ||
500 !strcmp( sub->codecName(), "X-QDM" ) || !strcmp( sub->codecName(), "X-SV3V-ES" ) ||
501 !strcmp( sub->codecName(), "X-SORENSONVIDEO" ) )
503 tk->b_quicktime = VLC_TRUE;
505 else if( !strcmp( sub->codecName(), "MP2T" ) )
507 tk->b_muxed = VLC_TRUE;
508 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
510 else if( !strcmp( sub->codecName(), "MP2P" ) || !strcmp( sub->codecName(), "MP1S" ) )
512 tk->b_muxed = VLC_TRUE;
513 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps", p_demux->out );
515 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
517 tk->b_asf = VLC_TRUE;
518 if( p_sys->p_out_asf == NULL )
519 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf", p_demux->out );;
523 if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
525 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
528 if( sub->rtcpInstance() != NULL )
530 sub->rtcpInstance()->setByeHandler( StreamClose, tk );
533 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
535 tk->readSource = sub->readSource();
536 tk->rtpSource = sub->rtpSource();
539 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
540 p_sys->track[p_sys->i_track++] = tk;
550 if( p_sys->p_out_asf && ParseASF( p_demux ) )
552 msg_Err( p_demux, "cannot find a usable asf header" );
553 /* TODO Clean tracks */
557 p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
558 if( p_sys->i_length < 0 )
562 else if( p_sys->i_length > 0 )
565 /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
568 if( p_sys->i_track <= 0 )
570 msg_Err( p_demux, "no codec supported, aborting" );
577 if( p_sys->p_out_asf )
579 stream_DemuxDelete( p_sys->p_out_asf );
583 Medium::close( p_sys->ms );
587 Medium::close( p_sys->rtsp );
591 RECLAIM_ENV(p_sys->env);
593 if( p_sys->scheduler )
595 delete p_sys->scheduler;
599 free( p_sys->p_sdp );
607 /*****************************************************************************
609 *****************************************************************************/
610 static void Close( vlc_object_t *p_this )
612 demux_t *p_demux = (demux_t*)p_this;
613 demux_sys_t *p_sys = p_demux->p_sys;
616 for( i = 0; i < p_sys->i_track; i++ )
618 live_track_t *tk = p_sys->track[i];
622 stream_DemuxDelete( tk->p_out_muxed );
624 free( tk->p_buffer );
629 free( p_sys->track );
631 if( p_sys->p_out_asf )
633 stream_DemuxDelete( p_sys->p_out_asf );
636 if( p_sys->rtsp && p_sys->ms )
639 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
641 Medium::close( p_sys->ms );
644 Medium::close( p_sys->rtsp );
649 RECLAIM_ENV(p_sys->env);
651 if( p_sys->scheduler )
653 delete p_sys->scheduler;
657 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,
717 /* Create a task that will be called if we wait more than 300ms */
718 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
721 p_sys->scheduler->doEventLoop( &p_sys->event );
723 /* remove the task */
724 p_sys->scheduler->unscheduleDelayedTask( task );
726 /* Check for gap in pts value */
727 for( i = 0; i < p_sys->i_track; i++ )
729 live_track_t *tk = p_sys->track[i];
731 if( !tk->b_muxed && !tk->b_rtcp_sync && 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 return p_demux->b_error ? 0 : 1;
749 /*****************************************************************************
751 *****************************************************************************/
752 static int Control( demux_t *p_demux, int i_query, va_list args )
754 demux_sys_t *p_sys = p_demux->p_sys;
757 vlc_bool_t *pb, b_bool;
763 pi64 = (int64_t*)va_arg( args, int64_t * );
764 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
767 case DEMUX_GET_LENGTH:
768 pi64 = (int64_t*)va_arg( args, int64_t * );
769 *pi64 = p_sys->i_length;
772 case DEMUX_GET_POSITION:
773 pf = (double*)va_arg( args, double* );
774 if( p_sys->i_length > 0 )
776 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start)/
777 (double)(p_sys->i_length);
785 case DEMUX_SET_POSITION:
789 f = (double)va_arg( args, double );
790 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
792 if( p_sys->rtsp && p_sys->i_length > 0 )
794 MediaSubsessionIterator *iter = new MediaSubsessionIterator( *p_sys->ms );
795 MediaSubsession *sub;
798 while( ( sub = iter->next() ) != NULL )
800 p_sys->rtsp->playMediaSubsession( *sub, time );
803 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
804 p_sys->i_pcr_start = 0;
807 for( i = 0; i < p_sys->i_track; i++ )
809 p_sys->track[i]->i_pts = 0;
816 /* Special for access_demux */
817 case DEMUX_CAN_PAUSE:
818 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
819 if( p_sys->rtsp && p_sys->i_length )
820 *pb = VLC_TRUE; /* Not always true, but will be handled in SET_PAUSE_STATE */
825 case DEMUX_CAN_CONTROL_PACE:
826 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
830 case DEMUX_SET_PAUSE_STATE:
832 MediaSubsessionIterator *iter;
833 MediaSubsession *sub;
835 d_npt = ( (double)( p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start ) ) / 1000000.00;
837 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
838 if( p_sys->rtsp == NULL )
841 iter = new MediaSubsessionIterator( *p_sys->ms );
842 while( ( sub = iter->next() ) != NULL )
844 if( ( b_bool && !p_sys->rtsp->pauseMediaSubsession( *sub ) ) ||
845 ( !b_bool && !p_sys->rtsp->playMediaSubsession( *sub, d_npt > 0 ? d_npt : -1 ) ) )
853 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
854 for( i = 0; i < p_sys->i_track; i++ )
856 p_sys->track[i]->i_pts = 0;
858 p_sys->i_pcr_start = 0; /* FIXME Wrong */
863 case DEMUX_GET_TITLE_INFO:
864 case DEMUX_SET_TITLE:
865 case DEMUX_SET_SEEKPOINT:
868 case DEMUX_GET_PTS_DELAY:
869 pi64 = (int64_t*)va_arg( args, int64_t * );
870 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * I64C(1000);
878 /*****************************************************************************
880 *****************************************************************************/
881 static void StreamRead( void *p_private, unsigned int i_size, unsigned int i_truncated_bytes, struct timeval pts, unsigned int duration )
883 live_track_t *tk = (live_track_t*)p_private;
884 demux_t *p_demux = tk->p_demux;
885 demux_sys_t *p_sys = p_demux->p_sys;
888 mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) + (uint64_t)pts.tv_usec;
890 /* XXX Beurk beurk beurk Avoid having negative value XXX */
891 i_pts &= UI64C(0x00ffffffffffffff);
893 if( tk->b_quicktime && tk->p_es == NULL )
895 QuickTimeGenericRTPSource *qtRTPSource = (QuickTimeGenericRTPSource*)tk->rtpSource;
896 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
897 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
899 if( qtState.sdAtomSize < 16 + 32 )
906 tk->fmt.i_codec = VLC_FOURCC( sdAtom[0], sdAtom[1], sdAtom[2], sdAtom[3] );
907 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
908 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
910 tk->fmt.i_extra = qtState.sdAtomSize - 16;
911 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
912 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
914 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
918 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
920 pts.tv_sec * 1000000LL + pts.tv_usec );
923 /* grow buffer if it looks like buffer is too small, but don't eat
924 * up all the memory on strange streams */
925 if( i_truncated_bytes > 0 && tk->i_buffer < 2000000 )
928 msg_Dbg( p_demux, "lost %d bytes", i_truncated_bytes );
929 msg_Dbg( p_demux, "increasing buffer size to %d", tk->i_buffer * 2 );
931 p_tmp = realloc( tk->p_buffer, tk->i_buffer );
934 msg_Warn( p_demux, "realloc failed" );
938 tk->p_buffer = (uint8_t*)p_tmp;
941 if( i_size > tk->i_buffer )
943 msg_Warn( p_demux, "buffer overflow" );
945 /* FIXME could i_size be > buffer size ? */
946 if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') )
948 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
949 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
950 uint32_t header = h261Source->lastSpecialHeader();
953 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
955 p_block = block_New( p_demux, i_size + 4 );
956 memcpy( p_block->p_buffer, &header, 4 );
957 memcpy( p_block->p_buffer + 4, tk->p_buffer, i_size );
961 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, i_size );
962 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
964 memcpy( p_block->p_buffer, tk->p_buffer, i_copy );
968 p_block = block_New( p_demux, i_size );
969 memcpy( p_block->p_buffer, tk->p_buffer, i_size );
971 if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') &&
972 tk->rtpSource->curPacketMarkerBit() )
974 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
976 //p_block->i_rate = p_input->stream.control.i_rate;
978 if( i_pts != tk->i_pts && !tk->b_muxed )
980 p_block->i_dts = i_pts;
981 p_block->i_pts = i_pts;
983 //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );
987 stream_DemuxSend( tk->p_out_muxed, p_block );
991 stream_DemuxSend( p_sys->p_out_asf, p_block );
995 es_out_Send( p_demux->out, tk->p_es, p_block );
1001 /* we have read data */
1004 if( i_pts > 0 && !tk->b_muxed )
1010 /*****************************************************************************
1012 *****************************************************************************/
1013 static void StreamClose( void *p_private )
1015 live_track_t *tk = (live_track_t*)p_private;
1016 demux_t *p_demux = tk->p_demux;
1017 demux_sys_t *p_sys = p_demux->p_sys;
1019 fprintf( stderr, "StreamClose\n" );
1021 p_sys->event = 0xff;
1022 p_demux->b_error = VLC_TRUE;
1026 /*****************************************************************************
1028 *****************************************************************************/
1029 static void TaskInterrupt( void *p_private )
1031 demux_t *p_demux = (demux_t*)p_private;
1033 fprintf( stderr, "TaskInterrupt\n" );
1036 p_demux->p_sys->event = 0xff;
1039 /*****************************************************************************
1041 *****************************************************************************/
1042 static int b64_decode( char *dest, char *src );
1044 static int ParseASF( demux_t *p_demux )
1046 demux_sys_t *p_sys = p_demux->p_sys;
1048 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1049 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1053 /* Parse the asf header */
1054 if( psz_asf == NULL )
1055 return VLC_EGENERIC;
1057 psz_asf += strlen( psz_marker );
1058 psz_asf = strdup( psz_asf ); /* Duplicate it */
1059 psz_end = strchr( psz_asf, '\n' );
1061 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1064 if( psz_asf >= psz_end )
1067 return VLC_EGENERIC;
1070 /* Always smaller */
1071 p_header = block_New( p_demux, psz_end - psz_asf );
1072 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1073 fprintf( stderr, "Size=%d Hdrb64=%s\n", p_header->i_buffer, psz_asf );
1074 if( p_header->i_buffer <= 0 )
1077 return VLC_EGENERIC;
1080 /* Parse it to get packet size */
1081 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1083 /* Send it to demuxer */
1084 stream_DemuxSend( p_sys->p_out_asf, p_header );
1089 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1090 static int b64_decode( char *dest, char *src )
1092 const char *dest_start = dest;
1096 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1097 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1098 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1099 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1100 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1101 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1102 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1103 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1104 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1105 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1106 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1107 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1108 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1109 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1110 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1111 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1114 for( i_level = 0; *src != '\0'; src++ )
1118 c = b64[(unsigned int)*src];
1130 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1134 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1138 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1146 return dest - dest_start;