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 ? */
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
54 #define CACHING_TEXT N_("Caching value (ms)")
55 #define CACHING_LONGTEXT N_( \
56 "Allows you to modify the default caching value for RTSP streams. This " \
57 "value should be set in millisecond units." )
60 set_description( _("live.com (RTSP/RTP/SDP) demuxer" ) );
61 set_capability( "demux2", 50 );
62 set_callbacks( Open, Close );
63 add_shortcut( "live" );
66 set_description( _("RTSP/RTP access and demux") );
67 add_shortcut( "rtsp" );
68 add_shortcut( "sdp" );
69 set_capability( "access_demux", 0 );
70 set_callbacks( Open, Close );
71 add_bool( "rtsp-tcp", 0, NULL,
72 N_("Use RTP over RTSP (TCP)"),
73 N_("Use RTP over RTSP (TCP)"), VLC_TRUE );
74 add_integer( "rtsp-caching", 4 * DEFAULT_PTS_DELAY / 1000, NULL,
75 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
79 * - Improve support of PS/TS
80 * - Support X-QT/X-QUICKTIME generic codec for audio.
82 * - Check memory leak, delete/free -> still one when using rtsp-tcp but I'm
83 * not sure if it comes from me.
87 /*****************************************************************************
89 *****************************************************************************/
94 vlc_bool_t b_quicktime;
101 stream_t *p_out_muxed; /* for muxed stream */
103 RTPSource *rtpSource;
104 FramedSource *readSource;
105 vlc_bool_t b_rtcp_sync;
107 uint8_t buffer[65536];
116 char *p_sdp; /* XXX mallocated */
119 TaskScheduler *scheduler;
120 UsageEnvironment *env ;
125 live_track_t **track; /* XXX mallocated */
140 static int Demux ( demux_t * );
141 static int Control( demux_t *, int, va_list );
143 static int ParseASF( demux_t * );
145 /*****************************************************************************
147 *****************************************************************************/
148 static int Open ( vlc_object_t *p_this )
150 demux_t *p_demux = (demux_t*)p_this;
153 MediaSubsessionIterator *iter;
154 MediaSubsession *sub;
156 vlc_bool_t b_rtsp_tcp;
165 /* See if it looks like a SDP
166 v, o, s fields are mandatory and in this order */
167 if( stream_Peek( p_demux->s, &p_peek, 7 ) < 7 )
169 msg_Err( p_demux, "cannot peek" );
172 if( strncmp( (char*)p_peek, "v=0\r\n", 5 ) && strncmp( (char*)p_peek, "v=0\n", 4 ) &&
173 ( p_peek[0] < 'a' || p_peek[0] > 'z' || p_peek[1] != '=' ) )
175 msg_Warn( p_demux, "SDP module discarded" );
181 var_Create( p_demux, "rtsp-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
184 p_demux->pf_demux = Demux;
185 p_demux->pf_control= Control;
186 p_demux->p_sys = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
188 p_sys->scheduler = NULL;
195 p_sys->i_pcr_start = 0;
198 p_sys->p_out_asf = NULL;
201 if( ( p_sys->scheduler = BasicTaskScheduler::createNew() ) == NULL )
203 msg_Err( p_demux, "BasicTaskScheduler::createNew failed" );
206 if( ( p_sys->env = BasicUsageEnvironment::createNew(*p_sys->scheduler) ) == NULL )
208 msg_Err( p_demux, "BasicUsageEnvironment::createNew failed" );
212 if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "rtsp" ) )
217 if( ( p_sys->rtsp = RTSPClient::createNew(*p_sys->env, 1/*verbose*/, "VLC Media Player" ) ) == NULL )
219 msg_Err( p_demux, "RTSPClient::createNew failed (%s)", p_sys->env->getResultMsg() );
222 psz_url = (char*)malloc( strlen( p_demux->psz_path ) + 8 );
223 sprintf( psz_url, "rtsp://%s", p_demux->psz_path );
225 psz_options = p_sys->rtsp->sendOptionsCmd( psz_url );
227 delete [] psz_options;
229 p_sdp = (uint8_t*)p_sys->rtsp->describeURL( psz_url );
232 msg_Err( p_demux, "describeURL failed (%s)", p_sys->env->getResultMsg() );
239 /* malloc-ated copy */
240 p_sys->p_sdp = strdup( (char*)p_sdp );
242 fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
244 else if( p_demux->s == NULL && !strcasecmp( p_demux->psz_access, "sdp" ) )
246 p_sys->p_sdp = strdup( p_demux->psz_path );
250 /* Gather the complete sdp file */
253 p_sdp = (uint8_t*)malloc( i_sdp_max );
256 int i_read = stream_Read( p_demux->s, &p_sdp[i_sdp], i_sdp_max - i_sdp - 1 );
260 msg_Err( p_demux, "failed to read SDP" );
267 if( i_read < i_sdp_max - i_sdp - 1 )
274 p_sdp = (uint8_t*)realloc( p_sdp, i_sdp_max );
276 p_sys->p_sdp = (char*)p_sdp;
278 fprintf( stderr, "sdp=%s\n", p_sys->p_sdp );
280 if( ( p_sys->ms = MediaSession::createNew(*p_sys->env, p_sys->p_sdp ) ) == NULL )
282 msg_Err( p_demux, "MediaSession::createNew failed" );
286 b_rtsp_tcp = var_CreateGetBool( p_demux, "rtsp-tcp" );
288 /* Initialise each media subsession */
289 iter = new MediaSubsessionIterator( *p_sys->ms );
290 while( ( sub = iter->next() ) != NULL )
292 unsigned int i_buffer = 0;
295 /* Value taken from mplayer */
296 if( !strcmp( sub->mediumName(), "audio" ) )
298 else if( !strcmp( sub->mediumName(), "video" ) )
303 if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
304 bInit = sub->initiate( 4 ); /* Constant ? */
306 bInit = sub->initiate();
310 msg_Warn( p_demux, "RTP subsession '%s/%s' failed(%s)", sub->mediumName(), sub->codecName(), p_sys->env->getResultMsg() );
314 if( sub->rtpSource() )
316 int fd = sub->rtpSource()->RTPgs()->socketNum();
317 /* Increase the buffer size */
318 increaseReceiveBufferTo( *p_sys->env, fd, i_buffer );
321 msg_Dbg( p_demux, "RTP subsession '%s/%s'", sub->mediumName(), sub->codecName() );
323 /* Issue the SETUP */
326 p_sys->rtsp->setupMediaSubsession( *sub, False, b_rtsp_tcp ? True : False );
334 if( !p_sys->rtsp->playMediaSession( *p_sys->ms ) )
336 msg_Err( p_demux, "PLAY failed %s", p_sys->env->getResultMsg() );
341 /* Create all es struct */
343 while( ( sub = iter->next() ) != NULL )
347 if( sub->readSource() == NULL )
352 tk = (live_track_t*)malloc( sizeof( live_track_t ) );
353 tk->p_demux = p_demux;
356 tk->b_quicktime = VLC_FALSE;
357 tk->b_muxed = VLC_FALSE;
358 tk->b_asf = VLC_FALSE;
359 tk->b_rtcp_sync = VLC_FALSE;
360 tk->p_out_muxed = NULL;
363 /* Value taken from mplayer */
364 if( !strcmp( sub->mediumName(), "audio" ) )
366 es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
367 tk->fmt.audio.i_channels = sub->numChannels();
368 tk->fmt.audio.i_rate = sub->rtpSource()->timestampFrequency();
370 if( !strcmp( sub->codecName(), "MPA" ) ||
371 !strcmp( sub->codecName(), "MPA-ROBUST" ) ||
372 !strcmp( sub->codecName(), "X-MP3-DRAFT-00" ) )
374 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
375 tk->fmt.audio.i_rate = 0;
377 else if( !strcmp( sub->codecName(), "AC3" ) )
379 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
380 tk->fmt.audio.i_rate = 0;
382 else if( !strcmp( sub->codecName(), "L16" ) )
384 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
385 tk->fmt.audio.i_bitspersample = 16;
387 else if( !strcmp( sub->codecName(), "L8" ) )
389 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
390 tk->fmt.audio.i_bitspersample = 8;
392 else if( !strcmp( sub->codecName(), "PCMU" ) )
394 tk->fmt.i_codec = VLC_FOURCC( 'u', 'l', 'a', 'w' );
396 else if( !strcmp( sub->codecName(), "PCMA" ) )
398 tk->fmt.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
400 else if( !strcmp( sub->codecName(), "MP4A-LATM" ) )
402 unsigned int i_extra;
405 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
407 if( ( p_extra = parseStreamMuxConfigStr( sub->fmtp_config(), i_extra ) ) )
409 tk->fmt.i_extra = i_extra;
410 tk->fmt.p_extra = malloc( i_extra );
411 memcpy( tk->fmt.p_extra, p_extra, i_extra );
415 else if( !strcmp( sub->codecName(), "MPEG4-GENERIC" ) )
417 unsigned int i_extra;
420 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
422 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(), i_extra ) ) )
424 tk->fmt.i_extra = i_extra;
425 tk->fmt.p_extra = malloc( i_extra );
426 memcpy( tk->fmt.p_extra, p_extra, i_extra );
430 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
432 tk->b_asf = VLC_TRUE;
433 if( p_sys->p_out_asf == NULL )
434 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf", p_demux->out );;
437 else if( !strcmp( sub->mediumName(), "video" ) )
439 es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC( 'u', 'n', 'd', 'f' ) );
440 if( !strcmp( sub->codecName(), "MPV" ) )
442 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
444 else if( !strcmp( sub->codecName(), "H263" ) ||
445 !strcmp( sub->codecName(), "H263-1998" ) ||
446 !strcmp( sub->codecName(), "H263-2000" ) )
448 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '3' );
450 else if( !strcmp( sub->codecName(), "H261" ) )
452 tk->fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '1' );
454 else if( !strcmp( sub->codecName(), "JPEG" ) )
456 tk->fmt.i_codec = VLC_FOURCC( 'M', 'J', 'P', 'G' );
458 else if( !strcmp( sub->codecName(), "MP4V-ES" ) )
460 unsigned int i_extra;
463 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
465 if( ( p_extra = parseGeneralConfigStr( sub->fmtp_config(), i_extra ) ) )
467 tk->fmt.i_extra = i_extra;
468 tk->fmt.p_extra = malloc( i_extra );
469 memcpy( tk->fmt.p_extra, p_extra, i_extra );
473 else if( !strcmp( sub->codecName(), "X-QT" ) || !strcmp( sub->codecName(), "X-QUICKTIME" ) )
475 tk->b_quicktime = VLC_TRUE;
477 else if( !strcmp( sub->codecName(), "MP2T" ) )
479 tk->b_muxed = VLC_TRUE;
480 tk->p_out_muxed = stream_DemuxNew( p_demux, "ts", p_demux->out );
482 else if( !strcmp( sub->codecName(), "MP2P" ) || !strcmp( sub->codecName(), "MP1S" ) )
484 tk->b_muxed = VLC_TRUE;
485 tk->p_out_muxed = stream_DemuxNew( p_demux, "ps", p_demux->out );
487 else if( !strcmp( sub->codecName(), "X-ASF-PF" ) )
489 tk->b_asf = VLC_TRUE;
490 if( p_sys->p_out_asf == NULL )
491 p_sys->p_out_asf = stream_DemuxNew( p_demux, "asf", p_demux->out );;
495 if( tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
497 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
500 if( tk->p_es || tk->b_quicktime || tk->b_muxed || tk->b_asf )
502 tk->readSource = sub->readSource();
503 tk->rtpSource = sub->rtpSource();
506 p_sys->track = (live_track_t**)realloc( p_sys->track, sizeof( live_track_t ) * ( p_sys->i_track + 1 ) );
507 p_sys->track[p_sys->i_track++] = tk;
517 if( p_sys->p_out_asf && ParseASF( p_demux ) )
519 msg_Err( p_demux, "cannot find a usable asf header" );
520 /* TODO Clean tracks */
524 p_sys->i_length = (mtime_t)(p_sys->ms->playEndTime() * 1000000.0);
525 if( p_sys->i_length < 0 )
529 else if( p_sys->i_length > 0 )
532 /* p_input->stream.p_selected_area->i_size = 1000;*/ /* needed for now */
535 if( p_sys->i_track <= 0 )
537 msg_Err( p_demux, "no codec supported, aborting" );
544 if( p_sys->p_out_asf )
546 stream_DemuxDelete( p_sys->p_out_asf );
550 Medium::close( p_sys->ms );
554 Medium::close( p_sys->rtsp );
560 if( p_sys->scheduler )
562 delete p_sys->scheduler;
566 free( p_sys->p_sdp );
574 /*****************************************************************************
576 *****************************************************************************/
577 static void Close( vlc_object_t *p_this )
579 demux_t *p_demux = (demux_t*)p_this;
580 demux_sys_t *p_sys = p_demux->p_sys;
583 for( i = 0; i < p_sys->i_track; i++ )
585 live_track_t *tk = p_sys->track[i];
589 stream_DemuxDelete( tk->p_out_muxed );
596 free( p_sys->track );
598 if( p_sys->p_out_asf )
600 stream_DemuxDelete( p_sys->p_out_asf );
603 if( p_sys->rtsp && p_sys->ms )
606 p_sys->rtsp->teardownMediaSession( *p_sys->ms );
608 Medium::close( p_sys->ms );
611 Medium::close( p_sys->rtsp );
618 if( p_sys->scheduler )
620 delete p_sys->scheduler;
624 free( p_sys->p_sdp );
630 static void StreamRead( void *p_private, unsigned int i_size, struct timeval pts );
631 static void StreamClose( void *p_private );
632 static void TaskInterrupt( void *p_private );
634 /*****************************************************************************
636 *****************************************************************************/
637 static int Demux( demux_t *p_demux )
639 demux_sys_t *p_sys = p_demux->p_sys;
642 vlc_bool_t b_send_pcr = VLC_TRUE;
646 for( i = 0; i < p_sys->i_track; i++ )
648 live_track_t *tk = p_sys->track[i];
650 if( tk->b_asf || tk->b_muxed )
651 b_send_pcr = VLC_FALSE;
657 else if( tk->i_pts != 0 && i_pcr > tk->i_pts )
662 if( i_pcr != p_sys->i_pcr && i_pcr > 0 )
664 p_sys->i_pcr = i_pcr;
667 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
668 if( p_sys->i_pcr_start <= 0 || p_sys->i_pcr_start > i_pcr ||
669 ( p_sys->i_length > 0 && i_pcr - p_sys->i_pcr_start > p_sys->i_length ) )
671 p_sys->i_pcr_start = i_pcr;
675 /* First warm we want to read data */
677 for( i = 0; i < p_sys->i_track; i++ )
679 live_track_t *tk = p_sys->track[i];
681 if( tk->waiting == 0 )
684 tk->readSource->getNextFrame( tk->buffer, 65536,
689 /* Create a task that will be called if we wait more than 300ms */
690 task = p_sys->scheduler->scheduleDelayedTask( 300000, TaskInterrupt, p_demux );
693 p_sys->scheduler->doEventLoop( &p_sys->event );
695 /* remove the task */
696 p_sys->scheduler->unscheduleDelayedTask( task );
698 /* Check for gap in pts value */
699 for( i = 0; i < p_sys->i_track; i++ )
701 live_track_t *tk = p_sys->track[i];
703 if( !tk->b_muxed && !tk->b_rtcp_sync && tk->rtpSource->hasBeenSynchronizedUsingRTCP() )
705 msg_Dbg( p_demux, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
707 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
708 tk->b_rtcp_sync = VLC_TRUE;
710 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
712 p_sys->i_pcr_start = 0;
718 return p_demux->b_error ? 0 : 1;
721 /*****************************************************************************
723 *****************************************************************************/
724 static int Control( demux_t *p_demux, int i_query, va_list args )
726 demux_sys_t *p_sys = p_demux->p_sys;
729 vlc_bool_t *pb, b_bool;
735 pi64 = (int64_t*)va_arg( args, int64_t * );
736 *pi64 = p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start;
739 case DEMUX_GET_LENGTH:
740 pi64 = (int64_t*)va_arg( args, int64_t * );
741 *pi64 = p_sys->i_length;
744 case DEMUX_GET_POSITION:
745 pf = (double*)va_arg( args, double* );
746 if( p_sys->i_length > 0 )
748 *pf = (double)( p_sys->i_pcr - p_sys->i_pcr_start + p_sys->i_start)/
749 (double)(p_sys->i_length);
757 case DEMUX_SET_POSITION:
761 f = (double)va_arg( args, double );
762 time = f * (double)p_sys->i_length / 1000000.0; /* in second */
764 if( p_sys->rtsp && p_sys->i_length > 0 )
766 MediaSubsessionIterator *iter = new MediaSubsessionIterator( *p_sys->ms );
767 MediaSubsession *sub;
770 while( ( sub = iter->next() ) != NULL )
772 p_sys->rtsp->playMediaSubsession( *sub, time );
775 p_sys->i_start = (mtime_t)(f * (double)p_sys->i_length);
776 p_sys->i_pcr_start = 0;
778 for( i = 0; i < p_sys->i_track; i++ )
780 p_sys->track[i]->i_pts = 0;
787 /* Special for access_demux */
788 case DEMUX_CAN_PAUSE:
789 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
791 *pb = VLC_TRUE; /* Not always true, but will be handled in SET_PAUSE_STATE */
796 case DEMUX_CAN_CONTROL_PACE:
797 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
801 case DEMUX_SET_PAUSE_STATE:
802 MediaSubsessionIterator *iter;
803 MediaSubsession *sub;
805 b_bool = (vlc_bool_t)va_arg( args, vlc_bool_t );
806 if( p_sys->rtsp == NULL )
809 iter = new MediaSubsessionIterator( *p_sys->ms );
810 while( ( sub = iter->next() ) != NULL )
812 if( ( b_bool && !p_sys->rtsp->pauseMediaSubsession( *sub ) ) ||
813 ( !b_bool && !p_sys->rtsp->playMediaSubsession( *sub, -1 ) ) )
821 /* reset PCR and PCR start, mmh won't work well for multi-stream I fear */
822 for( i = 0; i < p_sys->i_track; i++ )
824 live_track_t *tk = p_sys->track[i];
827 p_sys->i_pcr_start = 0; /* FIXME Wrong */
832 case DEMUX_GET_TITLE_INFO:
833 case DEMUX_SET_TITLE:
834 case DEMUX_SET_SEEKPOINT:
837 case DEMUX_GET_PTS_DELAY:
838 pi64 = (int64_t*)va_arg( args, int64_t * );
839 *pi64 = (int64_t)var_GetInteger( p_demux, "rtsp-caching" ) * I64C(1000);
847 /*****************************************************************************
849 *****************************************************************************/
850 static void StreamRead( void *p_private, unsigned int i_size, struct timeval pts )
852 live_track_t *tk = (live_track_t*)p_private;
853 demux_t *p_demux = tk->p_demux;
854 demux_sys_t *p_sys = p_demux->p_sys;
857 mtime_t i_pts = (uint64_t)pts.tv_sec * UI64C(1000000) + (uint64_t)pts.tv_usec;
859 /* XXX Beurk beurk beurk Avoid having negative value XXX */
860 i_pts &= UI64C(0x00ffffffffffffff);
862 if( tk->b_quicktime && tk->p_es == NULL )
864 QuickTimeGenericRTPSource *qtRTPSource = (QuickTimeGenericRTPSource*)tk->rtpSource;
865 QuickTimeGenericRTPSource::QTState &qtState = qtRTPSource->qtState;
866 uint8_t *sdAtom = (uint8_t*)&qtState.sdAtom[4];
868 if( qtState.sdAtomSize < 16 + 32 )
875 tk->fmt.i_codec = VLC_FOURCC( sdAtom[0], sdAtom[1], sdAtom[2], sdAtom[3] );
876 tk->fmt.video.i_width = (sdAtom[28] << 8) | sdAtom[29];
877 tk->fmt.video.i_height = (sdAtom[30] << 8) | sdAtom[31];
879 tk->fmt.i_extra = qtState.sdAtomSize - 16;
880 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
881 memcpy( tk->fmt.p_extra, &sdAtom[12], tk->fmt.i_extra );
883 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
887 fprintf( stderr, "StreamRead size=%d pts=%lld\n",
889 pts.tv_sec * 1000000LL + pts.tv_usec );
893 msg_Warn( p_demux, "buffer overflow" );
895 /* FIXME could i_size be > buffer size ? */
896 if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') )
898 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
899 H261VideoRTPSource *h261Source = (H261VideoRTPSource*)tk->rtpSource;
900 uint32_t header = h261Source->lastSpecialHeader();
903 msg_Warn( p_demux, "need livemedia library >= \"2004.04.09\"" );
905 p_block = block_New( p_demux, i_size + 4 );
906 memcpy( p_block->p_buffer, &header, 4 );
907 memcpy( p_block->p_buffer + 4, tk->buffer, i_size );
911 int i_copy = __MIN( p_sys->asfh.i_min_data_packet_size, i_size );
912 p_block = block_New( p_demux, p_sys->asfh.i_min_data_packet_size );
914 memcpy( p_block->p_buffer, tk->buffer, i_copy );
918 p_block = block_New( p_demux, i_size );
919 memcpy( p_block->p_buffer, tk->buffer, i_size );
921 if( tk->fmt.i_codec == VLC_FOURCC('h','2','6','1') &&
922 tk->rtpSource->curPacketMarkerBit() )
924 p_block->i_flags |= BLOCK_FLAG_END_OF_FRAME;
926 //p_block->i_rate = p_input->stream.control.i_rate;
928 if( i_pts != tk->i_pts && !tk->b_muxed )
930 p_block->i_dts = i_pts;
931 p_block->i_pts = i_pts;
933 //fprintf( stderr, "tk -> dpts=%lld\n", i_pts - tk->i_pts );
937 stream_DemuxSend( tk->p_out_muxed, p_block );
941 stream_DemuxSend( p_sys->p_out_asf, p_block );
945 es_out_Send( p_demux->out, tk->p_es, p_block );
951 /* we have read data */
954 if( i_pts > 0 && !tk->b_muxed )
960 /*****************************************************************************
962 *****************************************************************************/
963 static void StreamClose( void *p_private )
965 live_track_t *tk = (live_track_t*)p_private;
966 demux_t *p_demux = tk->p_demux;
967 demux_sys_t *p_sys = p_demux->p_sys;
969 fprintf( stderr, "StreamClose\n" );
972 p_demux->b_error = VLC_TRUE;
976 /*****************************************************************************
978 *****************************************************************************/
979 static void TaskInterrupt( void *p_private )
981 demux_t *p_demux = (demux_t*)p_private;
983 fprintf( stderr, "TaskInterrupt\n" );
986 p_demux->p_sys->event = 0xff;
989 /*****************************************************************************
991 *****************************************************************************/
992 static int b64_decode( char *dest, char *src );
994 static int ParseASF( demux_t *p_demux )
996 demux_sys_t *p_sys = p_demux->p_sys;
998 const char *psz_marker = "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
999 char *psz_asf = strcasestr( p_sys->p_sdp, psz_marker );
1003 /* Parse the asf header */
1004 if( psz_asf == NULL )
1005 return VLC_EGENERIC;
1007 psz_asf += strlen( psz_marker );
1008 psz_asf = strdup( psz_asf ); /* Duplicate it */
1009 psz_end = strchr( psz_asf, '\n' );
1011 while( psz_end > psz_asf && ( *psz_end == '\n' || *psz_end == '\r' ) )
1014 if( psz_asf >= psz_end )
1017 return VLC_EGENERIC;
1020 /* Always smaller */
1021 p_header = block_New( p_demux, psz_end - psz_asf );
1022 p_header->i_buffer = b64_decode( (char*)p_header->p_buffer, psz_asf );
1023 fprintf( stderr, "Size=%d Hdrb64=%s\n", p_header->i_buffer, psz_asf );
1024 if( p_header->i_buffer <= 0 )
1027 return VLC_EGENERIC;
1030 /* Parse it to get packet size */
1031 E_(asf_HeaderParse)( &p_sys->asfh, p_header->p_buffer, p_header->i_buffer );
1033 /* Send it to demuxer */
1034 stream_DemuxSend( p_sys->p_out_asf, p_header );
1039 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1040 static int b64_decode( char *dest, char *src )
1042 const char *dest_start = dest;
1046 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1047 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1048 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1049 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1050 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1051 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1052 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1053 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1054 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1055 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1056 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1057 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1058 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1059 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1060 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1061 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1064 for( i_level = 0; *src != '\0'; src++ )
1068 c = b64[(unsigned int)*src];
1080 *dest++ = ( last << 2 ) | ( ( c >> 4)&0x03 );
1084 *dest++ = ( ( last << 4 )&0xf0 ) | ( ( c >> 2 )&0x0f );
1088 *dest++ = ( ( last &0x03 ) << 6 ) | c;
1096 return dest - dest_start;