1 /*****************************************************************************
2 * live555.cpp : LIVE555 Streaming Media support.
3 *****************************************************************************
4 * Copyright (C) 2003-2007 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Derk-Jan Hartman <hartman at videolan. org>
9 * Derk-Jan Hartman <djhartman at m2x .dot. nl> for M2X
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <stdlib.h> /* malloc(), free() */
34 #include <vlc_demux.h>
35 #include <vlc_interface.h>
36 #include <vlc_network.h>
41 # include <winsock2.h>
44 #include "BasicUsageEnvironment.hh"
45 #include "GroupsockHelper.hh"
46 #include "liveMedia.hh"
49 #include "../access/mms/asf.h" /* Who said ugly ? */
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t
* );
58 static void Close( vlc_object_t
* );
60 #define CACHING_TEXT N_("Caching value (ms)")
61 #define CACHING_LONGTEXT N_( \
62 "Allows you to modify the default caching value for RTSP streams. This " \
63 "value should be set in millisecond units." )
65 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
66 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and unstandard " \
67 "dialect of RTSP. When you set this parameter, VLC will try this dialect "\
68 "for communication. In this mode you cannot connect to normal RTSP servers." )
70 #define USER_TEXT N_("RTSP user name")
71 #define USER_LONGTEXT N_("Allows you to modify the user name that will " \
72 "be used for authenticating the connection.")
73 #define PASS_TEXT N_("RTSP password")
74 #define PASS_LONGTEXT N_("Allows you to modify the password that will be " \
75 "used for the connection.")
78 set_description( _("RTP/RTSP/SDP demuxer (using Live555)" ) );
79 set_capability( "demux2", 50 );
80 set_shortname( "RTP/RTSP");
81 set_callbacks( Open
, Close
);
82 add_shortcut( "live" );
83 add_shortcut( "livedotcom" );
84 set_category( CAT_INPUT
);
85 set_subcategory( SUBCAT_INPUT_DEMUX
);
88 set_description( _("RTSP/RTP access and demux") );
89 add_shortcut( "rtsp" );
90 add_shortcut( "sdp" );
91 set_capability( "access_demux", 0 );
92 set_callbacks( Open
, Close
);
93 add_bool( "rtsp-tcp", 0, NULL
,
94 N_("Use RTP over RTSP (TCP)"),
95 N_("Use RTP over RTSP (TCP)"), VLC_TRUE
);
96 add_integer( "rtp-client-port", -1, NULL
,
98 N_("Port to use for the RTP source of the session"), VLC_TRUE
);
99 add_bool( "rtsp-http", 0, NULL
,
100 N_("Tunnel RTSP and RTP over HTTP"),
101 N_("Tunnel RTSP and RTP over HTTP"), VLC_TRUE
);
102 add_integer( "rtsp-http-port", 80, NULL
,
103 N_("HTTP tunnel port"),
104 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
106 add_integer("rtsp-caching", 4 * DEFAULT_PTS_DELAY
/ 1000, NULL
,
107 CACHING_TEXT
, CACHING_LONGTEXT
, VLC_TRUE
);
108 add_bool( "rtsp-kasenna", VLC_FALSE
, NULL
, KASENNA_TEXT
,
109 KASENNA_LONGTEXT
, VLC_TRUE
);
110 add_string( "rtsp-user", NULL
, NULL
, USER_TEXT
,
111 USER_LONGTEXT
, VLC_TRUE
);
112 add_string( "rtsp-pwd", NULL
, NULL
, PASS_TEXT
,
113 PASS_LONGTEXT
, VLC_TRUE
);
117 /*****************************************************************************
119 *****************************************************************************/
124 MediaSubsession
*sub
;
130 vlc_bool_t b_quicktime
;
132 stream_t
*p_out_muxed
; /* for muxed stream */
135 unsigned int i_buffer
;
137 vlc_bool_t b_rtcp_sync
;
140 u_int32_t i_start_seq
;
144 struct timeout_thread_t
154 char *p_sdp
; /* XXX mallocated */
155 char *psz_path
; /* URL-encoded path */
158 TaskScheduler
*scheduler
;
159 UsageEnvironment
*env
;
164 live_track_t
**track
; /* XXX mallocated */
172 int64_t i_pcr
; /* The clock */
173 int64_t i_npt
; /* The current time in the stream */
174 int64_t i_npt_length
;
177 /* timeout thread information */
178 int i_timeout
; /* session timeout value in seconds */
179 vlc_bool_t b_timeout_call
;/* mark to send an RTSP call to prevent server timeout */
180 timeout_thread_t
*p_timeout
; /* the actual thread that makes sure we don't timeout */
183 vlc_bool_t b_multicast
; /* true if one of the tracks is multicasted */
184 vlc_bool_t b_no_data
; /* true if we never receive any data */
185 int i_no_data_ti
; /* consecutive number of TaskInterrupt */
190 static int Demux ( demux_t
* );
191 static int Control( demux_t
*, int, va_list );
193 static int Connect ( demux_t
* );
194 static int SessionsSetup( demux_t
* );
195 static int Play ( demux_t
*);
196 static int ParseASF ( demux_t
* );
197 static int RollOverTcp ( demux_t
* );
199 static void StreamRead ( void *, unsigned int, unsigned int,
200 struct timeval
, unsigned int );
201 static void StreamClose ( void * );
202 static void TaskInterrupt( void * );
204 static void TimeoutPrevention( timeout_thread_t
* );
206 static unsigned char* parseH264ConfigStr( char const* configStr
,
207 unsigned int& configSize
);
209 /*****************************************************************************
211 *****************************************************************************/
212 static int Open ( vlc_object_t
*p_this
)
214 demux_t
*p_demux
= (demux_t
*)p_this
;
215 demux_sys_t
*p_sys
= NULL
;
217 MediaSubsessionIterator
*iter
= NULL
;
218 MediaSubsession
*sub
= NULL
;
223 /* See if it looks like a SDP
224 v, o, s fields are mandatory and in this order */
226 if( stream_Peek( p_demux
->s
, &p_peek
, 7 ) < 7 ) return VLC_EGENERIC
;
228 if( memcmp( (char*)p_peek
, "v=0\r\n", 5 ) &&
229 memcmp( (char*)p_peek
, "v=0\n", 4 ) &&
230 ( p_peek
[0] < 'a' || p_peek
[0] > 'z' || p_peek
[1] != '=' ) )
237 var_Create( p_demux
, "rtsp-caching", VLC_VAR_INTEGER
|VLC_VAR_DOINHERIT
);
240 p_demux
->pf_demux
= Demux
;
241 p_demux
->pf_control
= Control
;
242 p_demux
->p_sys
= p_sys
= (demux_sys_t
*)malloc( sizeof( demux_sys_t
) );
243 if( !p_sys
) return VLC_ENOMEM
;
246 p_sys
->scheduler
= NULL
;
254 p_sys
->i_npt_start
= 0;
255 p_sys
->i_npt_length
= 0;
256 p_sys
->p_out_asf
= NULL
;
257 p_sys
->b_no_data
= VLC_TRUE
;
258 p_sys
->i_no_data_ti
= 0;
259 p_sys
->p_timeout
= NULL
;
260 p_sys
->i_timeout
= 0;
261 p_sys
->b_timeout_call
= VLC_FALSE
;
262 p_sys
->b_multicast
= VLC_FALSE
;
263 p_sys
->b_real
= VLC_FALSE
;
264 p_sys
->psz_path
= strdup( p_demux
->psz_path
);
266 if( ( p_sys
->scheduler
= BasicTaskScheduler::createNew() ) == NULL
)
268 msg_Err( p_demux
, "BasicTaskScheduler::createNew failed" );
271 if( !( p_sys
->env
= BasicUsageEnvironment::createNew(*p_sys
->scheduler
) ) )
273 msg_Err( p_demux
, "BasicUsageEnvironment::createNew failed" );
277 if( strcasecmp( p_demux
->psz_access
, "sdp" ) )
279 char *p
= p_sys
->psz_path
;
280 while( (p
= strchr( p
, ' ' )) != NULL
) *p
= '+';
283 if( p_demux
->s
!= NULL
)
285 /* Gather the complete sdp file */
287 int i_sdp_max
= 1000;
288 uint8_t *p_sdp
= (uint8_t*) malloc( i_sdp_max
);
292 int i_read
= stream_Read( p_demux
->s
, &p_sdp
[i_sdp
],
293 i_sdp_max
- i_sdp
- 1 );
297 msg_Err( p_demux
, "failed to read SDP" );
304 if( i_read
< i_sdp_max
- i_sdp
- 1 )
311 p_sdp
= (uint8_t*)realloc( p_sdp
, i_sdp_max
);
313 p_sys
->p_sdp
= (char*)p_sdp
;
315 else if( p_demux
->s
== NULL
&& !strcasecmp( p_demux
->psz_access
, "sdp" ) )
317 /* sdp:// link from SAP */
318 p_sys
->p_sdp
= strdup( p_sys
->psz_path
);
320 else if( ( i_return
= Connect( p_demux
) ) != VLC_SUCCESS
)
322 msg_Err( p_demux
, "Failed to connect with rtsp://%s", p_sys
->psz_path
);
326 if( p_sys
->p_sdp
== NULL
)
328 msg_Err( p_demux
, "Failed to retrieve the RTSP Session Description" );
332 if( ( i_return
= SessionsSetup( p_demux
) ) != VLC_SUCCESS
)
334 msg_Err( p_demux
, "Nothing to play for rtsp://%s", p_sys
->psz_path
);
338 if( p_sys
->b_real
) goto error
;
340 if( ( i_return
= Play( p_demux
) ) != VLC_SUCCESS
)
343 if( p_sys
->p_out_asf
&& ParseASF( p_demux
) )
345 msg_Err( p_demux
, "cannot find a usable asf header" );
346 /* TODO Clean tracks */
350 if( p_sys
->i_track
<= 0 )
356 for( i
= 0; i
< p_sys
->i_track
; i
++ )
358 live_track_t
*tk
= p_sys
->track
[i
];
360 if( tk
->b_muxed
) stream_DemuxDelete( tk
->p_out_muxed
);
361 free( tk
->p_buffer
);
365 if( p_sys
->i_track
) free( p_sys
->track
);
366 if( p_sys
->p_out_asf
) stream_DemuxDelete( p_sys
->p_out_asf
);
367 if( p_sys
->rtsp
&& p_sys
->ms
) p_sys
->rtsp
->teardownMediaSession( *p_sys
->ms
);
368 if( p_sys
->ms
) Medium::close( p_sys
->ms
);
369 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
370 if( p_sys
->env
) p_sys
->env
->reclaim();
371 if( p_sys
->p_timeout
)
373 p_sys
->p_timeout
->b_die
= VLC_TRUE
;
374 vlc_thread_join( p_sys
->p_timeout
);
375 vlc_object_detach( p_sys
->p_timeout
);
376 vlc_object_destroy( p_sys
->p_timeout
);
378 if( p_sys
->scheduler
) delete p_sys
->scheduler
;
379 if( p_sys
->p_sdp
) free( p_sys
->p_sdp
);
380 if( p_sys
->psz_path
) free( p_sys
->psz_path
);
386 /*****************************************************************************
388 *****************************************************************************/
389 static void Close( vlc_object_t
*p_this
)
391 demux_t
*p_demux
= (demux_t
*)p_this
;
392 demux_sys_t
*p_sys
= p_demux
->p_sys
;
395 for( i
= 0; i
< p_sys
->i_track
; i
++ )
397 live_track_t
*tk
= p_sys
->track
[i
];
399 if( tk
->b_muxed
) stream_DemuxDelete( tk
->p_out_muxed
);
400 free( tk
->p_buffer
);
404 if( p_sys
->i_track
) free( p_sys
->track
);
405 if( p_sys
->p_out_asf
) stream_DemuxDelete( p_sys
->p_out_asf
);
406 if( p_sys
->rtsp
&& p_sys
->ms
) p_sys
->rtsp
->teardownMediaSession( *p_sys
->ms
);
407 if( p_sys
->ms
) Medium::close( p_sys
->ms
);
408 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
409 if( p_sys
->env
) p_sys
->env
->reclaim();
410 if( p_sys
->p_timeout
)
412 p_sys
->p_timeout
->b_die
= VLC_TRUE
;
413 vlc_thread_join( p_sys
->p_timeout
);
414 vlc_object_detach( p_sys
->p_timeout
);
415 vlc_object_destroy( p_sys
->p_timeout
);
417 if( p_sys
->scheduler
) delete p_sys
->scheduler
;
418 if( p_sys
->p_sdp
) free( p_sys
->p_sdp
);
419 if( p_sys
->psz_path
) free( p_sys
->psz_path
);
424 /*****************************************************************************
425 * Connect: connects to the RTSP server to setup the session DESCRIBE
426 *****************************************************************************/
427 static int Connect( demux_t
*p_demux
)
429 demux_sys_t
*p_sys
= p_demux
->p_sys
;
430 Authenticator authenticator
;
432 char *psz_user
= NULL
;
433 char *psz_pwd
= NULL
;
434 char *psz_url
= NULL
;
435 char *psz_options
= NULL
;
438 int i_ret
= VLC_SUCCESS
;
441 if( var_CreateGetBool( p_demux
, "rtsp-http" ) )
442 i_http_port
= var_CreateGetInteger( p_demux
, "rtsp-http-port" );
444 if( ( p_sys
->rtsp
= RTSPClient::createNew(*p_sys
->env
, 1 /*verbose*/,
445 "VLC media player", i_http_port
) ) == NULL
)
447 msg_Err( p_demux
, "RTSPClient::createNew failed (%s)",
448 p_sys
->env
->getResultMsg() );
451 psz_url
= (char*)malloc( strlen( p_sys
->psz_path
) + 8 );
452 sprintf( psz_url
, "rtsp://%s", p_sys
->psz_path
);
454 psz_options
= p_sys
->rtsp
->sendOptionsCmd( psz_url
);
455 if( psz_options
) delete [] psz_options
;
457 psz_user
= var_CreateGetString( p_demux
, "rtsp-user" );
458 psz_pwd
= var_CreateGetString( p_demux
, "rtsp-pwd" );
461 authenticator
.setUsernameAndPassword( (const char*)psz_user
, (const char*)psz_pwd
);
462 p_sdp
= p_sys
->rtsp
->describeURL( psz_url
,
463 &authenticator
, var_CreateGetBool( p_demux
, "rtsp-kasenna" ) );
465 if( psz_user
) free( psz_user
);
466 if( psz_pwd
) free( psz_pwd
);
470 /* failure occurred */
472 const char *psz_error
= p_sys
->env
->getResultMsg();
474 msg_Dbg( p_demux
, "DESCRIBE failed with %d: %s", i_code
, psz_error
);
475 sscanf( psz_error
, "%*sRTSP/%*s%3u", &i_code
);
479 char *psz_login
= NULL
; char *psz_password
= NULL
;
480 msg_Dbg( p_demux
, "authentication failed" );
482 i_ret
= intf_UserLoginPassword( p_demux
, _("RTSP authentication"),
483 _("Please enter a valid login name and a password."),
484 &psz_login
, &psz_password
);
485 if( i_ret
== DIALOG_OK_YES
)
487 msg_Dbg( p_demux
, "retrying with user=%s, pwd=%s",
488 psz_login
, psz_password
);
489 if( psz_login
) psz_user
= psz_login
;
490 if( psz_password
) psz_pwd
= psz_password
;
493 if( psz_login
) free( psz_login
);
494 if( psz_password
) free( psz_password
);
496 else if( !var_CreateGetBool( p_demux
, "rtsp-http" ) )
498 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
500 val
.b_bool
= VLC_TRUE
;
501 msg_Dbg( p_demux
, "we will now try HTTP tunneling mode" );
502 var_Set( p_demux
, "rtsp-http", val
);
503 if( psz_url
) free( psz_url
);
504 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
507 i_ret
= VLC_EGENERIC
;
509 if( psz_url
) free( psz_url
);
511 /* malloc-ated copy */
512 if( p_sys
->p_sdp
) free( p_sys
->p_sdp
);
513 if( p_sdp
) p_sys
->p_sdp
= strdup( (char*)p_sdp
);
519 /*****************************************************************************
520 * Connect: prepares the subsessions and does the SETUP
521 *****************************************************************************/
522 static int SessionsSetup( demux_t
*p_demux
)
524 demux_sys_t
*p_sys
= p_demux
->p_sys
;
525 MediaSubsessionIterator
*iter
= NULL
;
526 MediaSubsession
*sub
= NULL
;
528 vlc_bool_t b_rtsp_tcp
= VLC_FALSE
;
530 int i_return
= VLC_SUCCESS
;
531 unsigned int i_buffer
= 0;
532 unsigned const thresh
= 200000; /* RTP reorder threshold .2 second (default .1) */
534 b_rtsp_tcp
= var_CreateGetBool( p_demux
, "rtsp-tcp" );
535 i_client_port
= var_CreateGetInteger( p_demux
, "rtp-client-port" );
537 /* Create the session from the SDP */
538 if( !( p_sys
->ms
= MediaSession::createNew( *p_sys
->env
, p_sys
->p_sdp
) ) )
540 msg_Err( p_demux
, "Could not create the RTSP Session: %s",
541 p_sys
->env
->getResultMsg() );
545 /* Initialise each media subsession */
546 iter
= new MediaSubsessionIterator( *p_sys
->ms
);
547 while( ( sub
= iter
->next() ) != NULL
)
552 /* Value taken from mplayer */
553 if( !strcmp( sub
->mediumName(), "audio" ) )
555 else if( !strcmp( sub
->mediumName(), "video" ) )
559 if( i_client_port
!= -1 )
561 sub
->setClientPortNum( i_client_port
);
565 if( strcasestr( sub
->codecName(), "REAL" ) )
567 msg_Info( p_demux
, "real codec detected, using real-RTSP instead" );
568 p_sys
->b_real
= VLC_TRUE
; /* This is a problem, we'll handle it later */
572 if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
573 bInit
= sub
->initiate( 4 ); /* Constant ? */
575 bInit
= sub
->initiate();
579 msg_Warn( p_demux
, "RTP subsession '%s/%s' failed (%s)",
580 sub
->mediumName(), sub
->codecName(),
581 p_sys
->env
->getResultMsg() );
585 if( sub
->rtpSource() != NULL
)
587 int fd
= sub
->rtpSource()->RTPgs()->socketNum();
589 /* Increase the buffer size */
591 increaseReceiveBufferTo( *p_sys
->env
, fd
, i_buffer
);
593 /* Increase the RTP reorder timebuffer just a bit */
594 sub
->rtpSource()->setPacketReorderingThresholdTime(thresh
);
596 msg_Dbg( p_demux
, "RTP subsession '%s/%s'", sub
->mediumName(),
599 /* Issue the SETUP */
602 if( !( p_sys
->rtsp
->setupMediaSubsession( *sub
, False
,
603 b_rtsp_tcp
? True
: False
) ) )
605 msg_Err( p_demux
, "SETUP of'%s/%s' failed %s", sub
->mediumName(),
606 sub
->codecName(), p_sys
->env
->getResultMsg() );
611 /* Check if we will receive data from this subsession for this track */
612 if( sub
->readSource() == NULL
) continue;
613 if( !p_sys
->b_multicast
)
615 /* Check, because we need diff. rollover behaviour for multicast */
616 p_sys
->b_multicast
= IsMulticastAddress( sub
->connectionEndpointAddress() );
619 tk
= (live_track_t
*)malloc( sizeof( live_track_t
) );
620 tk
->p_demux
= p_demux
;
623 tk
->b_quicktime
= VLC_FALSE
;
624 tk
->b_asf
= VLC_FALSE
;
625 tk
->b_muxed
= VLC_FALSE
;
626 tk
->p_out_muxed
= NULL
;
628 tk
->b_rtcp_sync
= VLC_FALSE
;
630 tk
->i_buffer
= 65536;
631 tk
->p_buffer
= (uint8_t *)malloc( 65536 );
633 /* Value taken from mplayer */
634 if( !strcmp( sub
->mediumName(), "audio" ) )
636 es_format_Init( &tk
->fmt
, AUDIO_ES
, VLC_FOURCC('u','n','d','f') );
637 tk
->fmt
.audio
.i_channels
= sub
->numChannels();
638 tk
->fmt
.audio
.i_rate
= sub
->rtpTimestampFrequency();
640 if( !strcmp( sub
->codecName(), "MPA" ) ||
641 !strcmp( sub
->codecName(), "MPA-ROBUST" ) ||
642 !strcmp( sub
->codecName(), "X-MP3-DRAFT-00" ) )
644 tk
->fmt
.i_codec
= VLC_FOURCC( 'm', 'p', 'g', 'a' );
645 tk
->fmt
.audio
.i_rate
= 0;
647 else if( !strcmp( sub
->codecName(), "AC3" ) )
649 tk
->fmt
.i_codec
= VLC_FOURCC( 'a', '5', '2', ' ' );
650 tk
->fmt
.audio
.i_rate
= 0;
652 else if( !strcmp( sub
->codecName(), "L16" ) )
654 tk
->fmt
.i_codec
= VLC_FOURCC( 't', 'w', 'o', 's' );
655 tk
->fmt
.audio
.i_bitspersample
= 16;
657 else if( !strcmp( sub
->codecName(), "L8" ) )
659 tk
->fmt
.i_codec
= VLC_FOURCC( 'a', 'r', 'a', 'w' );
660 tk
->fmt
.audio
.i_bitspersample
= 8;
662 else if( !strcmp( sub
->codecName(), "PCMU" ) )
664 tk
->fmt
.i_codec
= VLC_FOURCC( 'u', 'l', 'a', 'w' );
666 else if( !strcmp( sub
->codecName(), "PCMA" ) )
668 tk
->fmt
.i_codec
= VLC_FOURCC( 'a', 'l', 'a', 'w' );
670 else if( !strncmp( sub
->codecName(), "G726", 4 ) )
672 tk
->fmt
.i_codec
= VLC_FOURCC( 'g', '7', '2', '6' );
673 tk
->fmt
.audio
.i_rate
= 8000;
674 tk
->fmt
.audio
.i_channels
= 1;
675 if( !strcmp( sub
->codecName()+5, "40" ) )
676 tk
->fmt
.i_bitrate
= 40000;
677 else if( !strcmp( sub
->codecName()+5, "32" ) )
678 tk
->fmt
.i_bitrate
= 32000;
679 else if( !strcmp( sub
->codecName()+5, "24" ) )
680 tk
->fmt
.i_bitrate
= 24000;
681 else if( !strcmp( sub
->codecName()+5, "16" ) )
682 tk
->fmt
.i_bitrate
= 16000;
684 else if( !strcmp( sub
->codecName(), "AMR" ) )
686 tk
->fmt
.i_codec
= VLC_FOURCC( 's', 'a', 'm', 'r' );
688 else if( !strcmp( sub
->codecName(), "AMR-WB" ) )
690 tk
->fmt
.i_codec
= VLC_FOURCC( 's', 'a', 'w', 'b' );
692 else if( !strcmp( sub
->codecName(), "MP4A-LATM" ) )
694 unsigned int i_extra
;
697 tk
->fmt
.i_codec
= VLC_FOURCC( 'm', 'p', '4', 'a' );
699 if( ( p_extra
= parseStreamMuxConfigStr( sub
->fmtp_config(),
702 tk
->fmt
.i_extra
= i_extra
;
703 tk
->fmt
.p_extra
= malloc( i_extra
);
704 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
707 /* Because the "faad" decoder does not handle the LATM data length field
708 at the start of each returned LATM frame, tell the RTP source to omit it. */
709 ((MPEG4LATMAudioRTPSource
*)sub
->rtpSource())->omitLATMDataLengthField();
711 else if( !strcmp( sub
->codecName(), "MPEG4-GENERIC" ) )
713 unsigned int i_extra
;
716 tk
->fmt
.i_codec
= VLC_FOURCC( 'm', 'p', '4', 'a' );
718 if( ( p_extra
= parseGeneralConfigStr( sub
->fmtp_config(),
721 tk
->fmt
.i_extra
= i_extra
;
722 tk
->fmt
.p_extra
= malloc( i_extra
);
723 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
727 else if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
729 tk
->b_asf
= VLC_TRUE
;
730 if( p_sys
->p_out_asf
== NULL
)
731 p_sys
->p_out_asf
= stream_DemuxNew( p_demux
, "asf",
734 else if( !strcmp( sub
->codecName(), "X-QT" ) ||
735 !strcmp( sub
->codecName(), "X-QUICKTIME" ) )
737 tk
->b_quicktime
= VLC_TRUE
;
740 else if( !strcmp( sub
->mediumName(), "video" ) )
742 es_format_Init( &tk
->fmt
, VIDEO_ES
, VLC_FOURCC('u','n','d','f') );
743 if( !strcmp( sub
->codecName(), "MPV" ) )
745 tk
->fmt
.i_codec
= VLC_FOURCC( 'm', 'p', 'g', 'v' );
747 else if( !strcmp( sub
->codecName(), "H263" ) ||
748 !strcmp( sub
->codecName(), "H263-1998" ) ||
749 !strcmp( sub
->codecName(), "H263-2000" ) )
751 tk
->fmt
.i_codec
= VLC_FOURCC( 'H', '2', '6', '3' );
753 else if( !strcmp( sub
->codecName(), "H261" ) )
755 tk
->fmt
.i_codec
= VLC_FOURCC( 'H', '2', '6', '1' );
757 else if( !strcmp( sub
->codecName(), "H264" ) )
759 unsigned int i_extra
= 0;
760 uint8_t *p_extra
= NULL
;
762 tk
->fmt
.i_codec
= VLC_FOURCC( 'h', '2', '6', '4' );
763 tk
->fmt
.b_packetized
= VLC_FALSE
;
765 if((p_extra
=parseH264ConfigStr( sub
->fmtp_spropparametersets(),
768 tk
->fmt
.i_extra
= i_extra
;
769 tk
->fmt
.p_extra
= malloc( i_extra
);
770 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
775 else if( !strcmp( sub
->codecName(), "JPEG" ) )
777 tk
->fmt
.i_codec
= VLC_FOURCC( 'M', 'J', 'P', 'G' );
779 else if( !strcmp( sub
->codecName(), "MP4V-ES" ) )
781 unsigned int i_extra
;
784 tk
->fmt
.i_codec
= VLC_FOURCC( 'm', 'p', '4', 'v' );
786 if( ( p_extra
= parseGeneralConfigStr( sub
->fmtp_config(),
789 tk
->fmt
.i_extra
= i_extra
;
790 tk
->fmt
.p_extra
= malloc( i_extra
);
791 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
795 else if( !strcmp( sub
->codecName(), "X-QT" ) ||
796 !strcmp( sub
->codecName(), "X-QUICKTIME" ) ||
797 !strcmp( sub
->codecName(), "X-QDM" ) ||
798 !strcmp( sub
->codecName(), "X-SV3V-ES" ) ||
799 !strcmp( sub
->codecName(), "X-SORENSONVIDEO" ) )
801 tk
->b_quicktime
= VLC_TRUE
;
803 else if( !strcmp( sub
->codecName(), "MP2T" ) )
805 tk
->b_muxed
= VLC_TRUE
;
806 tk
->p_out_muxed
= stream_DemuxNew( p_demux
, "ts", p_demux
->out
);
808 else if( !strcmp( sub
->codecName(), "MP2P" ) ||
809 !strcmp( sub
->codecName(), "MP1S" ) )
811 tk
->b_muxed
= VLC_TRUE
;
812 tk
->p_out_muxed
= stream_DemuxNew( p_demux
, "ps",
815 else if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
817 tk
->b_asf
= VLC_TRUE
;
818 if( p_sys
->p_out_asf
== NULL
)
819 p_sys
->p_out_asf
= stream_DemuxNew( p_demux
, "asf",
824 if( !tk
->b_quicktime
&& !tk
->b_muxed
&& !tk
->b_asf
)
826 tk
->p_es
= es_out_Add( p_demux
->out
, &tk
->fmt
);
829 if( sub
->rtcpInstance() != NULL
)
831 sub
->rtcpInstance()->setByeHandler( StreamClose
, tk
);
834 if( tk
->p_es
|| tk
->b_quicktime
|| tk
->b_muxed
|| tk
->b_asf
)
837 p_sys
->track
= (live_track_t
**)realloc( p_sys
->track
, sizeof( live_track_t
) * ( p_sys
->i_track
+ 1 ) );
838 p_sys
->track
[p_sys
->i_track
++] = tk
;
843 msg_Err( p_demux
, "unusable RTSP track. this should not happen" );
849 if( p_sys
->i_track
<= 0 ) i_return
= VLC_EGENERIC
;
851 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
852 /* Retrieve the starttime if possible */
853 p_sys
->i_npt_start
= (int64_t)( p_sys
->ms
->playStartTime() * (double)1000000.0 );
855 p_sys
->i_npt_start
= (int64_t) -1;
857 if( p_sys
->i_npt_start
< 0 )
858 p_sys
->i_npt_start
= -1;
860 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
861 /* Retrieve the duration if possible */
862 p_sys
->i_npt_length
= (int64_t)( p_sys
->ms
->playEndTime() * (double)1000000.0 );
864 p_sys
->i_npt_length
= (int64_t) -1;
866 if( p_sys
->i_npt_length
< 0 )
867 p_sys
->i_npt_length
= -1;
869 msg_Dbg( p_demux
, "setup start: %lld stop:%lld", p_sys
->i_npt_start
, p_sys
->i_npt_length
);
873 /*****************************************************************************
874 * Play: starts the actual playback of the stream
875 *****************************************************************************/
876 static int Play( demux_t
*p_demux
)
878 demux_sys_t
*p_sys
= p_demux
->p_sys
;
884 if( !p_sys
->rtsp
->playMediaSession( *p_sys
->ms
, p_sys
->i_npt_start
, -1, 1 ) )
886 msg_Err( p_demux
, "RTSP PLAY failed %s", p_sys
->env
->getResultMsg() );
890 /* Retrieve the timeout value and set up a timeout prevention thread */
891 p_sys
->i_timeout
= p_sys
->rtsp
->sessionTimeoutParameter();
892 if( p_sys
->i_timeout
> 0 && !p_sys
->p_timeout
)
894 msg_Dbg( p_demux
, "We have a timeout of %d seconds", p_sys
->i_timeout
);
895 p_sys
->p_timeout
= (timeout_thread_t
*)vlc_object_create( p_demux
, sizeof(timeout_thread_t
) );
896 p_sys
->p_timeout
->p_sys
= p_demux
->p_sys
; /* lol, object recursion :D */
897 if( vlc_thread_create( p_sys
->p_timeout
, "liveMedia-timeout", TimeoutPrevention
,
898 VLC_THREAD_PRIORITY_LOW
, VLC_TRUE
) )
900 msg_Err( p_demux
, "cannot spawn liveMedia timeout thread" );
901 vlc_object_destroy( p_sys
->p_timeout
);
903 msg_Dbg( p_demux
, "spawned timeout thread" );
904 vlc_object_attach( p_sys
->p_timeout
, p_demux
);
911 for( i
= 0; i
< p_sys
->i_track
; i
++ )
913 //p_sys->track[i]->i_pts = 0;
914 p_sys
->track
[i
]->i_start_seq
= (int)p_sys
->track
[i
]->sub
->rtpInfo
.seqNum
;
915 msg_Dbg( p_demux
, "set startseq: %u", p_sys
->track
[i
]->i_start_seq
);
919 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
920 /* Retrieve the starttime if possible */
921 p_sys
->i_npt_start
= (int64_t)( p_sys
->ms
->playStartTime() * (double)1000000.0 );
923 p_sys
->i_npt_start
= -1;
925 if( p_sys
->i_npt_start
< 0 )
927 p_sys
->i_npt_start
= -1;
931 p_sys
->i_npt
= p_sys
->i_npt_start
;
933 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
934 /* Retrieve the duration if possible */
935 p_sys
->i_npt_length
= (int64_t)( p_sys
->ms
->playEndTime() * (double)1000000.0 );
937 p_sys
->i_npt_length
= -1;
939 if( p_sys
->i_npt_length
< 0 )
940 p_sys
->i_npt_length
= -1;
942 msg_Dbg( p_demux
, "play start: %lld stop:%lld", p_sys
->i_npt_start
, p_sys
->i_npt_length
);
947 /*****************************************************************************
949 *****************************************************************************/
950 static int Demux( demux_t
*p_demux
)
952 demux_sys_t
*p_sys
= p_demux
->p_sys
;
955 vlc_bool_t b_send_pcr
= VLC_TRUE
;
959 /* Check if we need to send the server a Keep-A-Live signal */
960 if( p_sys
->b_timeout_call
&& p_sys
->rtsp
&& p_sys
->ms
)
962 char *psz_bye
= NULL
;
963 p_sys
->rtsp
->getMediaSessionParameter( *p_sys
->ms
, NULL
, psz_bye
);
964 p_sys
->b_timeout_call
= VLC_FALSE
;
968 for( i
= 0; i
< p_sys
->i_track
; i
++ )
970 live_track_t
*tk
= p_sys
->track
[i
];
972 if( tk
->b_asf
|| tk
->b_muxed
)
973 b_send_pcr
= VLC_FALSE
;
979 else if( tk
->i_pts
!= 0 && i_pcr
> tk
->i_pts
)
985 if( p_sys
->i_pcr
> 0 )
988 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_sys
->i_pcr
);
991 /* First warn we want to read data */
993 for( i
= 0; i
< p_sys
->i_track
; i
++ )
995 live_track_t
*tk
= p_sys
->track
[i
];
997 if( tk
->waiting
== 0 )
1000 tk
->sub
->readSource()->getNextFrame( tk
->p_buffer
, tk
->i_buffer
,
1001 StreamRead
, tk
, StreamClose
, tk
);
1004 /* Create a task that will be called if we wait more than 300ms */
1005 task
= p_sys
->scheduler
->scheduleDelayedTask( 300000, TaskInterrupt
, p_demux
);
1008 p_sys
->scheduler
->doEventLoop( &p_sys
->event
);
1010 /* remove the task */
1011 p_sys
->scheduler
->unscheduleDelayedTask( task
);
1013 /* Check for gap in pts value */
1014 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1016 live_track_t
*tk
= p_sys
->track
[i
];
1018 if( !tk
->b_muxed
&& !tk
->b_rtcp_sync
&&
1019 tk
->sub
->rtpSource() && tk
->sub
->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1021 msg_Dbg( p_demux
, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1023 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
1024 tk
->b_rtcp_sync
= VLC_TRUE
;
1034 if( p_sys
->b_multicast
&& p_sys
->b_no_data
&& p_sys
->i_no_data_ti
> 120 )
1036 /* FIXME Make this configurable
1037 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1041 else if( !p_sys
->b_multicast
&& p_sys
->b_no_data
&& p_sys
->i_no_data_ti
> 34 )
1043 vlc_bool_t b_rtsp_tcp
= var_GetBool( p_demux
, "rtsp-tcp" );
1045 if( !b_rtsp_tcp
&& p_sys
->rtsp
&& p_sys
->ms
)
1047 msg_Warn( p_demux
, "no data received in 10s. Switching to TCP" );
1048 if( RollOverTcp( p_demux
) )
1050 msg_Err( p_demux
, "TCP rollover failed, aborting" );
1054 else if( p_sys
->i_no_data_ti
> 34 )
1056 msg_Err( p_demux
, "no data received in 10s, aborting" );
1060 else if( !p_sys
->b_multicast
&& p_sys
->b_no_data
&& p_sys
->i_no_data_ti
> 34 )
1063 msg_Warn( p_demux
, "no data received in 10s, eof ?" );
1066 return p_demux
->b_error
? 0 : 1;
1069 /*****************************************************************************
1071 *****************************************************************************/
1072 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
1074 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1077 vlc_bool_t
*pb
, b_bool
;
1081 case DEMUX_GET_TIME
:
1082 pi64
= (int64_t*)va_arg( args
, int64_t * );
1083 if( p_sys
->i_npt
> 0 )
1085 *pi64
= p_sys
->i_npt
;
1088 return VLC_EGENERIC
;
1090 case DEMUX_GET_LENGTH
:
1091 pi64
= (int64_t*)va_arg( args
, int64_t * );
1092 if( p_sys
->i_npt_length
> 0 )
1094 *pi64
= p_sys
->i_npt_length
;
1097 return VLC_EGENERIC
;
1099 case DEMUX_GET_POSITION
:
1100 pf
= (double*)va_arg( args
, double* );
1101 if( p_sys
->i_npt_length
> 0 && p_sys
->i_npt
> 0 )
1103 *pf
= (double)p_sys
->i_npt
/ (double)p_sys
->i_npt_length
;
1106 return VLC_EGENERIC
;
1108 case DEMUX_SET_POSITION
:
1112 f
= (double)va_arg( args
, double );
1113 if( p_sys
->rtsp
&& p_sys
->i_npt_length
> 0 )
1116 time
= f
* (double)p_sys
->i_npt_length
/ 1000000.0; /* in second */
1117 if( !p_sys
->rtsp
->playMediaSession( *p_sys
->ms
, time
, -1, 1 ) )
1119 msg_Err( p_demux
, "PLAY failed %s",
1120 p_sys
->env
->getResultMsg() );
1121 return VLC_EGENERIC
;
1123 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
1126 /* Retrieve RTP-Info values */
1127 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1129 //p_sys->track[i]->i_pts = 0;
1130 p_sys
->track
[i
]->i_start_seq
= p_sys
->track
[i
]->sub
->rtpInfo
.seqNum
;
1131 msg_Dbg( p_demux
, "set pos startseq: %u", p_sys
->track
[i
]->i_start_seq
);
1134 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1135 /* Retrieve the starttime if possible */
1136 p_sys
->i_npt_start
= (int64_t)( p_sys
->ms
->playStartTime() * (double)1000000.0 );
1138 p_sys
->i_npt_start
= -1;
1140 if( p_sys
->i_npt_start
< 0 )
1142 p_sys
->i_npt_start
= -1;
1146 p_sys
->i_npt
= p_sys
->i_npt_start
;
1148 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1149 /* Retrieve the duration if possible */
1150 p_sys
->i_npt_length
= (int64_t)( p_sys
->ms
->playEndTime() * (double)1000000.0 );
1152 p_sys
->i_npt_length
= -1;
1154 if( p_sys
->i_npt_length
< 0 )
1155 p_sys
->i_npt_length
= -1;
1157 msg_Dbg( p_demux
, "seek start: %lld stop:%lld", p_sys
->i_npt_start
, p_sys
->i_npt_length
);
1160 return VLC_EGENERIC
;
1162 /* Special for access_demux */
1163 case DEMUX_CAN_PAUSE
:
1164 pb
= (vlc_bool_t
*)va_arg( args
, vlc_bool_t
* );
1165 if( p_sys
->rtsp
&& p_sys
->i_npt_length
)
1166 /* Not always true, but will be handled in SET_PAUSE_STATE */
1172 case DEMUX_CAN_CONTROL_PACE
:
1173 pb
= (vlc_bool_t
*)va_arg( args
, vlc_bool_t
* );
1175 #if 1 /* Disable for now until we have a clock synchro algo
1176 * which works with something else than MPEG over UDP */
1183 case DEMUX_SET_PAUSE_STATE
:
1185 double d_npt
= (double) p_sys
->i_npt
/ I64C(1000000);
1188 b_bool
= (vlc_bool_t
)va_arg( args
, vlc_bool_t
);
1189 if( p_sys
->rtsp
== NULL
)
1190 return VLC_EGENERIC
;
1193 if( ( b_bool
&& !p_sys
->rtsp
->pauseMediaSession( *p_sys
->ms
) ) ||
1194 ( !b_bool
&& !p_sys
->rtsp
->playMediaSession( *p_sys
->ms
,
1195 d_npt
> 0 ? d_npt
: -1 ) ) )
1197 msg_Err( p_demux
, "PLAY or PAUSE failed %s", p_sys
->env
->getResultMsg() );
1198 return VLC_EGENERIC
;
1200 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
1203 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1205 //p_sys->track[i]->i_pts = 0;
1206 p_sys
->track
[i
]->i_start_seq
= p_sys
->track
[i
]->sub
->rtpInfo
.seqNum
;
1207 msg_Dbg( p_demux
, "set pause startseq: %u", p_sys
->track
[i
]->i_start_seq
);
1210 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1211 /* Retrieve the starttime if possible */
1212 p_sys
->i_npt_start
= (int64_t)( p_sys
->ms
->playStartTime() * (double)1000000.0 );
1214 p_sys
->i_npt_start
= -1;
1216 if( p_sys
->i_npt_start
< 0 )
1218 p_sys
->i_npt_start
= -1;
1222 p_sys
->i_npt
= p_sys
->i_npt_start
;
1224 #if (LIVEMEDIA_LIBRARY_VERSION_INT >= 9999999999)
1225 /* Retrieve the duration if possible */
1226 p_sys
->i_npt_length
= (int64_t)( p_sys
->ms
->playEndTime() * (double)1000000.0 );
1228 p_sys
->i_npt_length
= -1;
1230 if( p_sys
->i_npt_length
< 0 )
1231 p_sys
->i_npt_length
= -1;
1233 msg_Dbg( p_demux
, "pause start: %lld stop:%lld", p_sys
->i_npt_start
, p_sys
->i_npt_length
);
1236 case DEMUX_GET_TITLE_INFO
:
1237 case DEMUX_SET_TITLE
:
1238 case DEMUX_SET_SEEKPOINT
:
1239 return VLC_EGENERIC
;
1241 case DEMUX_GET_PTS_DELAY
:
1242 pi64
= (int64_t*)va_arg( args
, int64_t * );
1243 *pi64
= (int64_t)var_GetInteger( p_demux
, "rtsp-caching" ) * 1000;
1247 return VLC_EGENERIC
;
1251 /*****************************************************************************
1252 * RollOverTcp: reopen the rtsp into TCP mode
1253 * XXX: ugly, a lot of code are duplicated from Open()
1254 * This should REALLY be fixed
1255 *****************************************************************************/
1256 static int RollOverTcp( demux_t
*p_demux
)
1258 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1261 var_SetBool( p_demux
, "rtsp-tcp", VLC_TRUE
);
1263 /* We close the old RTSP session */
1264 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1266 live_track_t
*tk
= p_sys
->track
[i
];
1268 if( tk
->b_muxed
) stream_DemuxDelete( tk
->p_out_muxed
);
1269 free( tk
->p_buffer
);
1272 if( p_sys
->i_track
) free( p_sys
->track
);
1273 if( p_sys
->p_out_asf
) stream_DemuxDelete( p_sys
->p_out_asf
);
1275 p_sys
->rtsp
->teardownMediaSession( *p_sys
->ms
);
1276 Medium::close( p_sys
->ms
);
1277 RTSPClient::close( p_sys
->rtsp
);
1281 p_sys
->track
= NULL
;
1284 /* Reopen rtsp client */
1285 if( p_demux
->s
!= NULL
&& ( i_return
= Connect( p_demux
) ) != VLC_SUCCESS
)
1287 msg_Err( p_demux
, "Failed to connect with rtsp://%s", p_sys
->psz_path
);
1291 if( p_sys
->p_sdp
== NULL
)
1293 msg_Err( p_demux
, "Failed to retrieve the RTSP Session Description" );
1297 if( ( i_return
= SessionsSetup( p_demux
) ) != VLC_SUCCESS
)
1299 msg_Err( p_demux
, "Nothing to play for rtsp://%s", p_sys
->psz_path
);
1303 if( ( i_return
= Play( p_demux
) ) != VLC_SUCCESS
)
1309 return VLC_EGENERIC
;
1313 /*****************************************************************************
1315 *****************************************************************************/
1316 static void StreamRead( void *p_private
, unsigned int i_size
,
1317 unsigned int i_truncated_bytes
, struct timeval pts
,
1318 unsigned int duration
)
1320 live_track_t
*tk
= (live_track_t
*)p_private
;
1321 demux_t
*p_demux
= tk
->p_demux
;
1322 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1325 msg_Dbg( p_demux
, "pts: %d", pts
.tv_sec
);
1327 int64_t i_pts
= (uint64_t)pts
.tv_sec
* UI64C(1000000) +
1328 (uint64_t)pts
.tv_usec
;
1330 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1331 i_pts
&= UI64C(0x00ffffffffffffff);
1333 if( tk
->b_quicktime
&& tk
->p_es
== NULL
)
1335 QuickTimeGenericRTPSource
*qtRTPSource
=
1336 (QuickTimeGenericRTPSource
*)tk
->sub
->rtpSource();
1337 QuickTimeGenericRTPSource::QTState
&qtState
= qtRTPSource
->qtState
;
1338 uint8_t *sdAtom
= (uint8_t*)&qtState
.sdAtom
[4];
1340 if( tk
->fmt
.i_cat
== VIDEO_ES
) {
1341 if( qtState
.sdAtomSize
< 16 + 32 )
1344 p_sys
->event
= 0xff;
1348 tk
->fmt
.i_codec
= VLC_FOURCC(sdAtom
[0],sdAtom
[1],sdAtom
[2],sdAtom
[3]);
1349 tk
->fmt
.video
.i_width
= (sdAtom
[28] << 8) | sdAtom
[29];
1350 tk
->fmt
.video
.i_height
= (sdAtom
[30] << 8) | sdAtom
[31];
1352 if( tk
->fmt
.i_codec
== VLC_FOURCC('a', 'v', 'c', '1') )
1354 uint8_t *pos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
+ 86;
1355 uint8_t *endpos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
1356 + qtRTPSource
->qtState
.sdAtomSize
;
1357 while (pos
+8 < endpos
) {
1358 unsigned atomLength
= pos
[0]<<24 | pos
[1]<<16 | pos
[2]<<8 | pos
[3];
1359 if( atomLength
== 0 || atomLength
> endpos
-pos
) break;
1360 if( memcmp(pos
+4, "avcC", 4) == 0 &&
1362 atomLength
<= INT_MAX
)
1364 tk
->fmt
.i_extra
= atomLength
-8;
1365 tk
->fmt
.p_extra
= malloc( tk
->fmt
.i_extra
);
1366 memcpy(tk
->fmt
.p_extra
, pos
+8, atomLength
-8);
1374 tk
->fmt
.i_extra
= qtState
.sdAtomSize
- 16;
1375 tk
->fmt
.p_extra
= malloc( tk
->fmt
.i_extra
);
1376 memcpy( tk
->fmt
.p_extra
, &sdAtom
[12], tk
->fmt
.i_extra
);
1380 if( qtState
.sdAtomSize
< 4 )
1383 p_sys
->event
= 0xff;
1387 tk
->fmt
.i_codec
= VLC_FOURCC(sdAtom
[0],sdAtom
[1],sdAtom
[2],sdAtom
[3]);
1389 tk
->p_es
= es_out_Add( p_demux
->out
, &tk
->fmt
);
1393 fprintf( stderr
, "StreamRead size=%d pts=%lld\n",
1395 pts
.tv_sec
* 1000000LL + pts
.tv_usec
);
1398 /* grow buffer if it looks like buffer is too small, but don't eat
1399 * up all the memory on strange streams */
1400 if( i_truncated_bytes
> 0 && tk
->i_buffer
< 2000000 )
1403 msg_Dbg( p_demux
, "lost %d bytes", i_truncated_bytes
);
1404 msg_Dbg( p_demux
, "increasing buffer size to %d", tk
->i_buffer
* 2 );
1406 p_tmp
= realloc( tk
->p_buffer
, tk
->i_buffer
);
1409 msg_Warn( p_demux
, "realloc failed" );
1413 tk
->p_buffer
= (uint8_t*)p_tmp
;
1416 if( i_size
> tk
->i_buffer
)
1418 msg_Warn( p_demux
, "buffer overflow" );
1420 /* FIXME could i_size be > buffer size ? */
1421 if( tk
->fmt
.i_codec
== VLC_FOURCC('s','a','m','r') ||
1422 tk
->fmt
.i_codec
== VLC_FOURCC('s','a','w','b') )
1424 AMRAudioSource
*amrSource
= (AMRAudioSource
*)tk
->sub
->readSource();
1426 p_block
= block_New( p_demux
, i_size
+ 1 );
1427 p_block
->p_buffer
[0] = amrSource
->lastFrameHeader();
1428 memcpy( p_block
->p_buffer
+ 1, tk
->p_buffer
, i_size
);
1430 else if( tk
->fmt
.i_codec
== VLC_FOURCC('H','2','6','1') )
1432 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1081468800
1433 H261VideoRTPSource
*h261Source
= (H261VideoRTPSource
*)tk
->sub
->rtpSource();
1434 uint32_t header
= h261Source
->lastSpecialHeader();
1436 uint32_t header
= 0;
1437 msg_Warn( p_demux
, "need livemedia library >= \"2004.04.09\"" );
1439 p_block
= block_New( p_demux
, i_size
+ 4 );
1440 memcpy( p_block
->p_buffer
, &header
, 4 );
1441 memcpy( p_block
->p_buffer
+ 4, tk
->p_buffer
, i_size
);
1443 if( tk
->sub
->rtpSource()->curPacketMarkerBit() )
1444 p_block
->i_flags
|= BLOCK_FLAG_END_OF_FRAME
;
1446 else if( tk
->fmt
.i_codec
== VLC_FOURCC('h','2','6','4') )
1448 if( (tk
->p_buffer
[0] & 0x1f) >= 24 )
1449 msg_Warn( p_demux
, "unsupported NAL type for H264" );
1451 /* Normal NAL type */
1452 p_block
= block_New( p_demux
, i_size
+ 4 );
1453 p_block
->p_buffer
[0] = 0x00;
1454 p_block
->p_buffer
[1] = 0x00;
1455 p_block
->p_buffer
[2] = 0x00;
1456 p_block
->p_buffer
[3] = 0x01;
1457 memcpy( &p_block
->p_buffer
[4], tk
->p_buffer
, i_size
);
1459 else if( tk
->b_asf
)
1461 int i_copy
= __MIN( p_sys
->asfh
.i_min_data_packet_size
, (int)i_size
);
1462 p_block
= block_New( p_demux
, p_sys
->asfh
.i_min_data_packet_size
);
1464 memcpy( p_block
->p_buffer
, tk
->p_buffer
, i_copy
);
1468 p_block
= block_New( p_demux
, i_size
);
1469 memcpy( p_block
->p_buffer
, tk
->p_buffer
, i_size
);
1473 //msg_Dbg( p_demux, "current %d, start_seq %u", (int)tk->sub->rtpSource()->curPacketRTPSeqNum(), tk->i_start_seq );
1474 if( (tk
->fmt
.i_cat
== VIDEO_ES
) && (p_sys
->i_pcr
< i_pts
) &&
1475 (i_pts
> 0) && (p_sys
->i_pcr
> 0) )
1477 p_sys
->i_npt
+= __MAX( 0, i_pts
- p_sys
->i_pcr
);
1478 p_sys
->i_pcr
= i_pts
;
1479 msg_Dbg( p_demux
, "npt update" );
1481 else if( (tk
->fmt
.i_cat
== VIDEO_ES
) && (p_sys
->i_pcr
< i_pts
) )
1483 p_sys
->i_pcr
= i_pts
;
1485 msg_Dbg( p_demux
, "npt %lld", p_sys
->i_npt
);
1487 if( (i_pts
!= tk
->i_pts
) && (!tk
->b_muxed
) )
1489 p_block
->i_dts
= ( tk
->fmt
.i_cat
== VIDEO_ES
) ? 0 : i_pts
;
1490 p_block
->i_pts
= i_pts
;
1495 stream_DemuxSend( tk
->p_out_muxed
, p_block
);
1497 else if( tk
->b_asf
)
1499 stream_DemuxSend( p_sys
->p_out_asf
, p_block
);
1503 es_out_Send( p_demux
->out
, tk
->p_es
, p_block
);
1506 /* warn that's ok */
1507 p_sys
->event
= 0xff;
1509 /* we have read data */
1511 p_demux
->p_sys
->b_no_data
= VLC_FALSE
;
1512 p_demux
->p_sys
->i_no_data_ti
= 0;
1514 if( i_pts
> 0 && !tk
->b_muxed
)
1520 /*****************************************************************************
1522 *****************************************************************************/
1523 static void StreamClose( void *p_private
)
1525 live_track_t
*tk
= (live_track_t
*)p_private
;
1526 demux_t
*p_demux
= tk
->p_demux
;
1527 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1529 msg_Dbg( p_demux
, "StreamClose" );
1531 p_sys
->event
= 0xff;
1532 p_demux
->b_error
= VLC_TRUE
;
1536 /*****************************************************************************
1538 *****************************************************************************/
1539 static void TaskInterrupt( void *p_private
)
1541 demux_t
*p_demux
= (demux_t
*)p_private
;
1543 p_demux
->p_sys
->i_no_data_ti
++;
1546 p_demux
->p_sys
->event
= 0xff;
1549 /*****************************************************************************
1551 *****************************************************************************/
1552 static void TimeoutPrevention( timeout_thread_t
*p_timeout
)
1554 p_timeout
->b_die
= VLC_FALSE
;
1555 p_timeout
->i_remain
= (int64_t)p_timeout
->p_sys
->i_timeout
-2;
1556 p_timeout
->i_remain
*= 1000000;
1558 vlc_thread_ready( p_timeout
);
1561 while( !p_timeout
->b_die
)
1563 if( p_timeout
->i_remain
<= 0 )
1565 p_timeout
->i_remain
= (int64_t)p_timeout
->p_sys
->i_timeout
-2;
1566 p_timeout
->i_remain
*= 1000000;
1567 p_timeout
->p_sys
->b_timeout_call
= VLC_TRUE
;
1568 msg_Dbg( p_timeout
, "reset the timeout timer" );
1570 p_timeout
->i_remain
-= 200000;
1571 msleep( 200000 ); /* 200 ms */
1575 /*****************************************************************************
1577 *****************************************************************************/
1578 static int b64_decode( char *dest
, char *src
);
1580 static int ParseASF( demux_t
*p_demux
)
1582 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1584 const char *psz_marker
= "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
1585 char *psz_asf
= strcasestr( p_sys
->p_sdp
, psz_marker
);
1589 /* Parse the asf header */
1590 if( psz_asf
== NULL
)
1591 return VLC_EGENERIC
;
1593 psz_asf
+= strlen( psz_marker
);
1594 psz_asf
= strdup( psz_asf
); /* Duplicate it */
1595 psz_end
= strchr( psz_asf
, '\n' );
1597 while( psz_end
> psz_asf
&& ( *psz_end
== '\n' || *psz_end
== '\r' ) )
1600 if( psz_asf
>= psz_end
)
1603 return VLC_EGENERIC
;
1606 /* Always smaller */
1607 p_header
= block_New( p_demux
, psz_end
- psz_asf
);
1608 p_header
->i_buffer
= b64_decode( (char*)p_header
->p_buffer
, psz_asf
);
1609 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
1610 if( p_header
->i_buffer
<= 0 )
1613 return VLC_EGENERIC
;
1616 /* Parse it to get packet size */
1617 E_(asf_HeaderParse
)( &p_sys
->asfh
, p_header
->p_buffer
, p_header
->i_buffer
);
1619 /* Send it to demuxer */
1620 stream_DemuxSend( p_sys
->p_out_asf
, p_header
);
1627 static unsigned char* parseH264ConfigStr( char const* configStr
,
1628 unsigned int& configSize
)
1631 int i
, i_records
= 1;
1636 if( configStr
== NULL
|| *configStr
== '\0' )
1639 psz
= dup
= strdup( configStr
);
1641 /* Count the number of comma's */
1642 for( psz
= dup
; *psz
!= '\0'; ++psz
)
1651 unsigned char *cfg
= new unsigned char[5 * strlen(dup
)];
1653 for( i
= 0; i
< i_records
; i
++ )
1655 cfg
[configSize
++] = 0x00;
1656 cfg
[configSize
++] = 0x00;
1657 cfg
[configSize
++] = 0x00;
1658 cfg
[configSize
++] = 0x01;
1660 configSize
+= b64_decode( (char*)&cfg
[configSize
], psz
);
1661 psz
+= strlen(psz
)+1;
1664 if( dup
) free( dup
);
1668 /*char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
1669 static int b64_decode( char *dest
, char *src
)
1671 const char *dest_start
= dest
;
1675 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
1676 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
1677 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
1678 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
1679 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
1680 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
1681 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
1682 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
1683 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
1684 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
1685 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
1686 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
1687 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
1688 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
1689 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
1690 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
1693 for( i_level
= 0; *src
!= '\0'; src
++ )
1697 c
= b64
[(unsigned int)*src
];
1709 *dest
++ = ( last
<< 2 ) | ( ( c
>> 4)&0x03 );
1713 *dest
++ = ( ( last
<< 4 )&0xf0 ) | ( ( c
>> 2 )&0x0f );
1717 *dest
++ = ( ( last
&0x03 ) << 6 ) | c
;
1725 return dest
- dest_start
;