1 /*****************************************************************************
2 * live555.cpp : LIVE555 Streaming Media support.
3 *****************************************************************************
4 * Copyright (C) 2003-2007 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Derk-Jan Hartman <hartman at videolan. org>
8 * Derk-Jan Hartman <djhartman at m2x .dot. nl> for M2X
9 * Sébastien Escudier <sebastien-devel celeos eu>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_input.h>
39 #include <vlc_demux.h>
40 #include <vlc_dialog.h>
42 #include <vlc_strings.h>
43 #include <vlc_interrupt.h>
44 #include <vlc_keystore.h>
45 #include <vlc_threads.h>
46 #include <vlc_cxx_helpers.hpp>
54 # include <winsock2.h>
57 #include <UsageEnvironment.hh>
58 #include <BasicUsageEnvironment.hh>
59 #include <GroupsockHelper.hh>
60 #include <liveMedia.hh>
61 #include <liveMedia_version.hh>
65 #include "../access/mms/asf.h" /* Who said ugly ? */
66 #include "live555_dtsgen.h"
69 /*****************************************************************************
71 *****************************************************************************/
72 static int Open ( vlc_object_t
* );
73 static void Close( vlc_object_t
* );
75 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
76 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and nonstandard " \
77 "dialect of RTSP. With this parameter VLC will try this dialect, but "\
78 "then it cannot connect to normal RTSP servers." )
80 #define WMSERVER_TEXT N_("WMServer RTSP dialect")
81 #define WMSERVER_LONGTEXT N_("WMServer uses a nonstandard dialect " \
82 "of RTSP. Selecting this parameter will tell VLC to assume some " \
83 "options contrary to RFC 2326 guidelines.")
85 #define USER_TEXT N_("Username")
86 #define USER_LONGTEXT N_("Sets the username for the connection, " \
87 "if no username or password are set in the url.")
88 #define PASS_TEXT N_("Password")
89 #define PASS_LONGTEXT N_("Sets the password for the connection, " \
90 "if no username or password are set in the url.")
91 #define FRAME_BUFFER_SIZE_TEXT N_("RTSP frame buffer size")
92 #define FRAME_BUFFER_SIZE_LONGTEXT N_("RTSP start frame buffer size of the video " \
93 "track, can be increased in case of broken pictures due " \
94 "to too small buffer.")
95 #define DEFAULT_FRAME_BUFFER_SIZE 250000
98 set_description( N_("RTP/RTSP/SDP demuxer (using Live555)" ) )
99 set_capability( "demux", 50 )
100 set_shortname( "RTP/RTSP")
101 set_callbacks( Open
, Close
)
102 add_shortcut( "live", "livedotcom" )
103 set_category( CAT_INPUT
)
104 set_subcategory( SUBCAT_INPUT_DEMUX
)
107 set_description( N_("RTSP/RTP access and demux") )
108 add_shortcut( "rtsp", "pnm", "live", "livedotcom" )
109 set_capability( "access", 300 )
110 set_callbacks( Open
, Close
)
111 add_bool( "rtsp-tcp", false,
112 N_("Use RTP over RTSP (TCP)"),
113 N_("Use RTP over RTSP (TCP)"), true )
115 add_integer( "rtp-client-port", -1,
117 N_("Port to use for the RTP source of the session"), true )
118 add_bool( "rtsp-mcast", false,
119 N_("Force multicast RTP via RTSP"),
120 N_("Force multicast RTP via RTSP"), true )
122 add_bool( "rtsp-http", false,
123 N_("Tunnel RTSP and RTP over HTTP"),
124 N_("Tunnel RTSP and RTP over HTTP"), true )
126 add_integer( "rtsp-http-port", 80,
127 N_("HTTP tunnel port"),
128 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
130 add_bool( "rtsp-kasenna", false, KASENNA_TEXT
,
131 KASENNA_LONGTEXT
, true )
133 add_bool( "rtsp-wmserver", false, WMSERVER_TEXT
,
134 WMSERVER_LONGTEXT
, true)
136 add_string( "rtsp-user", NULL
, USER_TEXT
,
137 USER_LONGTEXT
, true )
139 add_password("rtsp-pwd", NULL
, PASS_TEXT
, PASS_LONGTEXT
)
140 add_integer( "rtsp-frame-buffer-size", DEFAULT_FRAME_BUFFER_SIZE
,
141 FRAME_BUFFER_SIZE_TEXT
, FRAME_BUFFER_SIZE_LONGTEXT
,
146 /*****************************************************************************
148 *****************************************************************************/
155 MediaSubsession
*sub
;
168 block_t
*p_asf_block
;
169 bool b_discard_trunc
;
170 vlc_demux_chained_t
*p_out_muxed
; /* for muxed stream */
173 unsigned int i_buffer
;
176 bool b_flushing_discontinuity
;
177 int i_next_block_flags
;
179 vlc_tick_t i_prevpts
;
183 struct dtsgen_t dtsgen
;
197 #define CAP_RATE_CONTROL (1 << 1)
198 #define CAP_SUBSESSION_TEARDOWN (1 << 2)
199 #define CAP_SUBSESSION_PAUSE (1 << 3)
200 #define CAPS_DEFAULT CAP_RATE_CONTROL
204 char *p_sdp
; /* XXX mallocated */
205 char *psz_pl_url
; /* password-less URL */
209 TaskScheduler
*scheduler
;
210 UsageEnvironment
*env
;
212 int capabilities
; /* Server capabilities workaround */
216 live_track_t
**track
;
220 vlc_demux_chained_t
*p_out_asf
;
224 vlc_tick_t i_pcr
; /* The clock */
225 bool b_rtcp_sync
; /* At least one track received sync */
230 /* timeout thread information */
232 vlc::threads::mutex timeout_mutex
; /* Serialise calls to live555 in timeout thread w.r.t. Demux()/Control() */
236 bool b_multicast
; /* if one of the tracks is multicasted */
237 bool b_no_data
; /* if we never received any data */
238 int i_no_data_ti
; /* consecutive number of TaskInterrupt */
243 bool b_get_param
; /* Does the server support GET_PARAMETER */
244 bool b_paused
; /* Are we paused? */
246 int i_live555_ret
; /* live555 callback return code */
248 float f_seek_request
;/* In case we receive a seek request while paused*/
252 class RTSPClientVlc
: public RTSPClient
255 RTSPClientVlc( UsageEnvironment
& env
, char const* rtspURL
, int verbosityLevel
,
256 char const* applicationName
, portNumBits tunnelOverHTTPPortNum
,
257 demux_sys_t
*p_sys
) :
258 RTSPClient( env
, rtspURL
, verbosityLevel
, applicationName
,
259 tunnelOverHTTPPortNum
260 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1373932800
272 static int Demux ( demux_t
* );
273 static int Control( demux_t
*, int, va_list );
275 static int Connect ( demux_t
* );
276 static int SessionsSetup( demux_t
* );
277 static int Play ( demux_t
*);
278 static int ParseASF ( demux_t
* );
279 static int RollOverTcp ( demux_t
* );
281 static void StreamRead ( void *, unsigned int, unsigned int,
282 struct timeval
, unsigned int );
283 static void StreamClose ( void * );
284 static void TaskInterruptData( void * );
285 static void TaskInterruptRTSP( void * );
287 static void TimeoutPrevention( void * );
289 static unsigned char* parseH264ConfigStr( char const* configStr
,
290 unsigned int& configSize
);
291 static unsigned char* parseVorbisConfigStr( char const* configStr
,
292 unsigned int& configSize
);
294 static char *passwordLessURL( vlc_url_t
*url
);
296 #define PCR_OBS VLC_TICK_FROM_MS(250)
297 #define PCR_OFF PCR_OBS
299 /*****************************************************************************
301 *****************************************************************************/
302 static int Open ( vlc_object_t
*p_this
)
304 demux_t
*p_demux
= (demux_t
*)p_this
;
305 demux_sys_t
*p_sys
= NULL
;
308 int i_error
= VLC_EGENERIC
;
310 if (p_demux
->out
== NULL
)
313 /* if the rtsp URL may contain a sat.ip fake DNS, bail-out early and
314 * let the SAT>IP module handle that */
315 if( !strncmp(p_demux
->psz_location
, "sat.ip", 6) )
317 msg_Err( p_demux
, "SAT>IP server, bailing out");
320 /* If satip-host is set on the item, we shall assume it is a rtsp for the
321 * SAT>IP module and bail-out early. */
322 char *psz_host
= var_InheritString(p_demux
, "satip-host");
323 if (psz_host
!= NULL
) {
324 msg_Err( p_demux
, "URL is for SAT>IP, bailing out");
331 /* See if it looks like a SDP
332 v, o, s fields are mandatory and in this order */
333 const uint8_t *p_peek
;
334 if( vlc_stream_Peek( p_demux
->s
, &p_peek
, 7 ) < 7 ) return VLC_EGENERIC
;
336 if( memcmp( p_peek
, "v=0\r\n", 5 ) &&
337 memcmp( p_peek
, "v=0\n", 4 ) &&
338 ( p_peek
[0] < 'a' || p_peek
[0] > 'z' || p_peek
[1] != '=' ) )
344 p_demux
->pf_demux
= Demux
;
345 p_demux
->pf_control
= Control
;
346 p_demux
->p_sys
= p_sys
= new (std::nothrow
)demux_sys_t();
347 if( !p_sys
) return VLC_ENOMEM
;
349 if( vlc_timer_create(&p_sys
->timer
, TimeoutPrevention
, p_demux
) )
355 msg_Dbg( p_demux
, "version " LIVEMEDIA_LIBRARY_VERSION_STRING
);
357 p_sys
->capabilities
= CAPS_DEFAULT
;
358 if( var_GetBool( p_demux
, "rtsp-kasenna" ) ||
359 var_GetBool( p_demux
, "rtsp-wmserver" ) )
361 p_sys
->capabilities
&= ~CAP_RATE_CONTROL
;
364 TAB_INIT( p_sys
->i_track
, p_sys
->track
);
365 p_sys
->b_no_data
= true;
366 p_sys
->b_force_mcast
= var_InheritBool( p_demux
, "rtsp-mcast" );
367 p_sys
->f_seek_request
= -1;
369 /* parse URL for rtsp://[user:[passwd]@]serverip:port/options */
370 vlc_UrlParse( &p_sys
->url
, p_demux
->psz_url
);
372 if( ( p_sys
->psz_pl_url
= passwordLessURL( &p_sys
->url
) ) == NULL
)
374 i_error
= VLC_ENOMEM
;
378 if( ( p_sys
->scheduler
= BasicTaskScheduler::createNew() ) == NULL
)
380 msg_Err( p_demux
, "BasicTaskScheduler::createNew failed" );
383 if( !( p_sys
->env
= BasicUsageEnvironment::createNew(*p_sys
->scheduler
) ) )
385 msg_Err( p_demux
, "BasicUsageEnvironment::createNew failed" );
389 if( p_demux
->s
!= NULL
)
391 char *p
= p_sys
->psz_pl_url
;
392 while( (p
= strchr( p
, ' ' )) != NULL
) *p
= '+';
395 if( p_demux
->s
!= NULL
)
397 /* Gather the complete sdp file */
399 int i_sdp_max
= 1000;
400 uint8_t *p_sdp
= (uint8_t*) malloc( i_sdp_max
);
404 i_error
= VLC_ENOMEM
;
410 int i_read
= vlc_stream_Read( p_demux
->s
, &p_sdp
[i_sdp
],
411 i_sdp_max
- i_sdp
- 1 );
414 msg_Err( p_demux
, "failed to read SDP" );
421 if( i_read
< i_sdp_max
- i_sdp
- 1 )
428 p_sdp
= (uint8_t*)xrealloc( p_sdp
, i_sdp_max
);
430 p_sys
->p_sdp
= (char*)p_sdp
;
432 else if( ( i_return
= Connect( p_demux
) ) != VLC_SUCCESS
)
434 msg_Err( p_demux
, "Failed to connect with %s", p_sys
->psz_pl_url
);
438 if( p_sys
->p_sdp
== NULL
)
440 msg_Err( p_demux
, "Failed to retrieve the RTSP Session Description" );
441 i_error
= VLC_ENOMEM
;
445 if( ( i_return
= SessionsSetup( p_demux
) ) != VLC_SUCCESS
)
447 msg_Err( p_demux
, "Nothing to play for %s", p_sys
->psz_pl_url
);
451 if( p_sys
->b_real
) goto error
;
453 if( ( i_return
= Play( p_demux
) ) != VLC_SUCCESS
)
456 if( p_sys
->p_out_asf
&& ParseASF( p_demux
) )
458 msg_Err( p_demux
, "cannot find a usable asf header" );
459 /* TODO Clean tracks */
463 if( p_sys
->i_track
<= 0 )
473 /*****************************************************************************
475 *****************************************************************************/
476 static void Close( vlc_object_t
*p_this
)
478 demux_t
*p_demux
= (demux_t
*)p_this
;
479 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
481 vlc_timer_destroy(p_sys
->timer
);
483 if( p_sys
->rtsp
&& p_sys
->ms
) p_sys
->rtsp
->sendTeardownCommand( *p_sys
->ms
, NULL
);
484 if( p_sys
->ms
) Medium::close( p_sys
->ms
);
485 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
486 if( p_sys
->env
) p_sys
->env
->reclaim();
488 for( int i
= 0; i
< p_sys
->i_track
; i
++ )
490 live_track_t
*tk
= p_sys
->track
[i
];
492 if( tk
->p_out_muxed
)
493 vlc_demux_chained_Delete( tk
->p_out_muxed
);
494 es_format_Clean( &tk
->fmt
);
495 dtsgen_Clean( &tk
->dtsgen
);
496 free( tk
->p_buffer
);
499 TAB_CLEAN( p_sys
->i_track
, p_sys
->track
);
500 if( p_sys
->p_out_asf
)
501 vlc_demux_chained_Delete( p_sys
->p_out_asf
);
502 delete p_sys
->scheduler
;
503 free( p_sys
->p_sdp
);
504 free( p_sys
->psz_pl_url
);
506 vlc_UrlClean( &p_sys
->url
);
511 static inline Boolean
toBool( bool b
) { return b
?True
:False
; } // silly, no?
513 static void default_live555_callback( RTSPClient
* client
, int result_code
, char* result_string
)
515 RTSPClientVlc
*client_vlc
= static_cast<RTSPClientVlc
*> ( client
);
516 demux_sys_t
*p_sys
= client_vlc
->p_sys
;
517 delete []result_string
;
518 p_sys
->i_live555_ret
= result_code
;
519 p_sys
->b_error
= p_sys
->i_live555_ret
!= 0;
520 p_sys
->event_rtsp
= 1;
523 /* return true if the RTSP command succeeded */
524 static bool wait_Live555_response( demux_t
*p_demux
, int i_timeout
= 0 /* ms */ )
527 demux_sys_t
* p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
528 p_sys
->event_rtsp
= 0;
531 /* Create a task that will be called if we wait more than timeout ms */
532 task
= p_sys
->scheduler
->scheduleDelayedTask( i_timeout
*1000,
536 p_sys
->event_rtsp
= 0;
537 p_sys
->b_error
= true;
538 p_sys
->i_live555_ret
= 0;
539 p_sys
->scheduler
->doEventLoop( &p_sys
->event_rtsp
);
540 //here, if b_error is true and i_live555_ret = 0 we didn't receive a response
543 /* remove the task */
544 p_sys
->scheduler
->unscheduleDelayedTask( task
);
546 return !p_sys
->b_error
;
549 static void continueAfterDESCRIBE( RTSPClient
* client
, int result_code
,
550 char* result_string
)
552 RTSPClientVlc
*client_vlc
= static_cast<RTSPClientVlc
*> ( client
);
553 demux_sys_t
*p_sys
= client_vlc
->p_sys
;
554 p_sys
->i_live555_ret
= result_code
;
555 if ( result_code
== 0 )
557 char* sdpDescription
= result_string
;
558 free( p_sys
->p_sdp
);
562 p_sys
->p_sdp
= strdup( sdpDescription
);
563 p_sys
->b_error
= false;
567 p_sys
->b_error
= true;
568 delete[] result_string
;
569 p_sys
->event_rtsp
= 1;
570 #ifdef VLC_PATCH_RTSPCLIENT_SERVERSTRING
571 if( client_vlc
->serverString() )
573 if( !strncmp(client_vlc
->serverString(), "Kasenna", 7) ||
574 !strncmp(client_vlc
->serverString(), "WMServer", 8) )
575 p_sys
->capabilities
&= ~CAP_RATE_CONTROL
;
576 if( !strncmp(client_vlc
->serverString(), "VLC/", 4) )
577 p_sys
->capabilities
|= (CAP_SUBSESSION_TEARDOWN
|CAP_SUBSESSION_PAUSE
);
582 static void continueAfterOPTIONS( RTSPClient
* client
, int result_code
,
583 char* result_string
)
585 RTSPClientVlc
*client_vlc
= static_cast<RTSPClientVlc
*> (client
);
586 demux_sys_t
*p_sys
= client_vlc
->p_sys
;
588 // If OPTIONS fails, assume GET_PARAMETER is not supported but
589 // still continue on with the stream. Some servers (foscam)
590 // return 501/not implemented for OPTIONS.
592 && result_string
!= NULL
593 && strstr( result_string
, "GET_PARAMETER" ) != NULL
;
594 client
->sendDescribeCommand( continueAfterDESCRIBE
);
595 delete[] result_string
;
598 /*****************************************************************************
599 * Connect: connects to the RTSP server to setup the session DESCRIBE
600 *****************************************************************************/
601 static int Connect( demux_t
*p_demux
)
603 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
604 Authenticator authenticator
;
605 vlc_credential credential
;
606 const char *psz_user
= NULL
;
607 const char *psz_pwd
= NULL
;
609 int i_ret
= VLC_SUCCESS
;
610 const int i_timeout
= var_InheritInteger( p_demux
, "ipv4-timeout" );
612 vlc_credential_init( &credential
, &p_sys
->url
);
614 /* Credentials can be NULL since they may not be needed */
615 if( vlc_credential_get( &credential
, p_demux
, "rtsp-user", "rtsp-pwd",
618 psz_user
= credential
.psz_username
;
619 psz_pwd
= credential
.psz_password
;
623 /* FIXME: This is naive and incorrect; it does not prevent the thread
624 * getting stuck in blocking socket operations. */
627 i_ret
= VLC_EGENERIC
;
631 if( var_CreateGetBool( p_demux
, "rtsp-http" ) )
632 i_http_port
= var_InheritInteger( p_demux
, "rtsp-http-port" );
634 p_sys
->rtsp
= new (std::nothrow
) RTSPClientVlc( *p_sys
->env
, p_sys
->psz_pl_url
,
635 var_InheritInteger( p_demux
, "verbose" ) > 1 ? 1 : 0,
636 "LibVLC/" VERSION
, i_http_port
, p_sys
);
639 msg_Err( p_demux
, "RTSPClient::createNew failed (%s)",
640 p_sys
->env
->getResultMsg() );
641 i_ret
= VLC_EGENERIC
;
645 /* Kasenna enables KeepAlive by analysing the User-Agent string.
646 * Appending _KA to the string should be enough to enable this feature,
647 * however, there is a bug where the _KA doesn't get parsed from the
648 * default User-Agent as created by VLC/Live555 code. This is probably due
649 * to spaces in the string or the string being too long. Here we override
650 * the default string with a more compact version.
652 if( var_InheritBool( p_demux
, "rtsp-kasenna" ))
654 p_sys
->rtsp
->setUserAgentString( "VLC_MEDIA_PLAYER_KA" );
658 authenticator
.setUsernameAndPassword( psz_user
, psz_pwd
);
660 p_sys
->rtsp
->sendOptionsCommand( &continueAfterOPTIONS
, &authenticator
);
662 if( !wait_Live555_response( p_demux
, i_timeout
) )
664 int i_code
= p_sys
->i_live555_ret
;
667 msg_Dbg( p_demux
, "authentication failed" );
669 if( vlc_credential_get( &credential
, p_demux
, "rtsp-user", "rtsp-pwd",
670 _("RTSP authentication"),
671 _("Please enter a valid login name and a password.") ) )
673 psz_user
= credential
.psz_username
;
674 psz_pwd
= credential
.psz_password
;
675 msg_Dbg( p_demux
, "retrying with user=%s", psz_user
);
679 else if( i_code
> 0 && i_code
!= 404 && !var_GetBool( p_demux
, "rtsp-http" ) )
681 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
682 msg_Dbg( p_demux
, "we will now try HTTP tunneling mode" );
683 var_SetBool( p_demux
, "rtsp-http", true );
684 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
691 msg_Dbg( p_demux
, "connection timeout" );
694 msg_Dbg( p_demux
, "connection error %d", i_code
);
696 vlc_dialog_display_error( p_demux
, _("RTSP connection failed"),
697 _("Access to the stream is denied by the server configuration.") );
699 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
702 i_ret
= VLC_EGENERIC
;
705 vlc_credential_store( &credential
, p_demux
);
708 vlc_credential_clean( &credential
);
713 /*****************************************************************************
714 * SessionsSetup: prepares the subsessions and does the SETUP
715 *****************************************************************************/
716 static int SessionsSetup( demux_t
*p_demux
)
718 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
719 MediaSubsessionIterator
*iter
= NULL
;
720 MediaSubsession
*sub
= NULL
;
724 int i_return
= VLC_SUCCESS
;
725 unsigned int i_receive_buffer
= 0;
726 int i_frame_buffer
= DEFAULT_FRAME_BUFFER_SIZE
;
727 unsigned const thresh
= 200000; /* RTP reorder threshold .2 second (default .1) */
728 const char *p_sess_lang
= NULL
;
731 b_rtsp_tcp
= var_CreateGetBool( p_demux
, "rtsp-tcp" ) ||
732 var_GetBool( p_demux
, "rtsp-http" );
733 i_client_port
= var_InheritInteger( p_demux
, "rtp-client-port" );
736 /* Create the session from the SDP */
737 if( !( p_sys
->ms
= MediaSession::createNew( *p_sys
->env
, p_sys
->p_sdp
) ) )
739 msg_Err( p_demux
, "Could not create the RTSP Session: %s",
740 p_sys
->env
->getResultMsg() );
744 if( strcmp( p_sys
->p_sdp
, "m=" ) != 0 )
746 const char *p_sess_attr_end
;
748 p_sess_attr_end
= strstr( p_sys
->p_sdp
, "\nm=" );
749 if( !p_sess_attr_end
)
750 p_sess_attr_end
= strstr( p_sys
->p_sdp
, "\rm=" );
752 p_sess_lang
= p_sess_attr_end
? strstr( p_sys
->p_sdp
, "a=lang:" ) : NULL
;
754 p_sess_lang
- p_sys
->p_sdp
> p_sess_attr_end
- p_sys
->p_sdp
)
758 /* Initialise each media subsession */
759 iter
= new MediaSubsessionIterator( *p_sys
->ms
);
760 while( ( sub
= iter
->next() ) != NULL
)
765 /* Value taken from mplayer */
766 if( !strcmp( sub
->mediumName(), "audio" ) )
767 i_receive_buffer
= 100000;
768 else if( !strcmp( sub
->mediumName(), "video" ) )
770 int i_var_buf_size
= var_InheritInteger( p_demux
, "rtsp-frame-buffer-size" );
771 if( i_var_buf_size
> 0 )
772 i_frame_buffer
= i_var_buf_size
;
773 i_receive_buffer
= 2000000;
775 else if( !strcmp( sub
->mediumName(), "text" ) )
779 if( strcasestr( sub
->codecName(), "REAL" ) )
781 msg_Info( p_demux
, "real codec detected, using real-RTSP instead" );
782 p_sys
->b_real
= true; /* This is a problem, we'll handle it later */
786 if( p_sys
->rtsp
&& i_client_port
!= -1 )
788 sub
->setClientPortNum( i_client_port
);
792 if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
793 bInit
= sub
->initiate( 0 );
795 bInit
= sub
->initiate();
799 msg_Warn( p_demux
, "RTP subsession '%s/%s' failed (%s)",
800 sub
->mediumName(), sub
->codecName(),
801 p_sys
->env
->getResultMsg() );
805 if( sub
->rtpSource() != NULL
)
807 int fd
= sub
->rtpSource()->RTPgs()->socketNum();
809 /* Increase the buffer size */
810 if( i_receive_buffer
> 0 )
811 increaseReceiveBufferTo( *p_sys
->env
, fd
, i_receive_buffer
);
813 /* Increase the RTP reorder timebuffer just a bit */
814 sub
->rtpSource()->setPacketReorderingThresholdTime(thresh
);
816 msg_Dbg( p_demux
, "RTP subsession '%s/%s'", sub
->mediumName(),
819 /* Issue the SETUP */
822 p_sys
->rtsp
->sendSetupCommand( *sub
, default_live555_callback
, False
,
823 toBool( b_rtsp_tcp
),
824 toBool( p_sys
->b_force_mcast
&& !b_rtsp_tcp
) );
825 if( !wait_Live555_response( p_demux
) )
827 /* if we get an unsupported transport error, toggle TCP
828 * use and try again */
829 if( p_sys
->i_live555_ret
== 461 )
830 p_sys
->rtsp
->sendSetupCommand( *sub
, default_live555_callback
, False
,
831 !toBool( b_rtsp_tcp
), False
);
832 if( p_sys
->i_live555_ret
!= 461 || !wait_Live555_response( p_demux
) )
834 msg_Err( p_demux
, "SETUP of'%s/%s' failed %s",
835 sub
->mediumName(), sub
->codecName(),
836 p_sys
->env
->getResultMsg() );
841 var_SetBool( p_demux
, "rtsp-tcp", true );
847 /* Check if we will receive data from this subsession for
849 if( sub
->readSource() == NULL
) continue;
850 if( !p_sys
->b_multicast
)
852 /* We need different rollover behaviour for multicast */
853 p_sys
->b_multicast
= IsMulticastAddress( sub
->connectionEndpointAddress() );
856 tk
= (live_track_t
*)malloc( sizeof( live_track_t
) );
862 tk
->p_demux
= p_demux
;
865 tk
->format
= live_track_t::SINGLE_STREAM
;
866 tk
->p_asf_block
= NULL
;
867 tk
->b_discard_trunc
= false;
868 tk
->p_out_muxed
= NULL
;
870 tk
->b_rtcp_sync
= false;
871 tk
->b_flushing_discontinuity
= false;
872 tk
->i_next_block_flags
= 0;
873 tk
->i_prevpts
= VLC_TICK_INVALID
;
874 tk
->i_pcr
= VLC_TICK_INVALID
;
876 dtsgen_Init( &tk
->dtsgen
);
877 tk
->state
= live_track_t::STATE_SELECTED
;
878 tk
->i_buffer
= i_frame_buffer
;
879 tk
->p_buffer
= (uint8_t *)malloc( i_frame_buffer
);
888 /* Value taken from mplayer */
889 if( !strcmp( sub
->mediumName(), "audio" ) )
891 es_format_Init( &tk
->fmt
, AUDIO_ES
, VLC_CODEC_UNKNOWN
);
892 tk
->fmt
.audio
.i_channels
= sub
->numChannels();
893 tk
->fmt
.audio
.i_rate
= sub
->rtpTimestampFrequency();
895 if( !strcmp( sub
->codecName(), "MPA" ) ||
896 !strcmp( sub
->codecName(), "MPA-ROBUST" ) ||
897 !strcmp( sub
->codecName(), "X-MP3-DRAFT-00" ) )
899 tk
->fmt
.i_codec
= VLC_CODEC_MPGA
;
900 tk
->fmt
.audio
.i_rate
= 0;
902 else if( !strcmp( sub
->codecName(), "AC3" ) )
904 tk
->fmt
.i_codec
= VLC_CODEC_A52
;
905 tk
->fmt
.audio
.i_rate
= 0;
907 else if( !strcmp( sub
->codecName(), "L16" ) )
909 tk
->fmt
.i_codec
= VLC_CODEC_S16B
;
910 tk
->fmt
.audio
.i_bitspersample
= 16;
912 else if( !strcmp( sub
->codecName(), "L20" ) )
914 tk
->fmt
.i_codec
= VLC_CODEC_S20B
;
915 tk
->fmt
.audio
.i_bitspersample
= 20;
917 else if( !strcmp( sub
->codecName(), "L24" ) )
919 tk
->fmt
.i_codec
= VLC_CODEC_S24B
;
920 tk
->fmt
.audio
.i_bitspersample
= 24;
922 else if( !strcmp( sub
->codecName(), "L8" ) )
924 tk
->fmt
.i_codec
= VLC_CODEC_U8
;
925 tk
->fmt
.audio
.i_bitspersample
= 8;
927 else if( !strcmp( sub
->codecName(), "DAT12" ) )
929 tk
->fmt
.i_codec
= VLC_CODEC_DAT12
;
930 tk
->fmt
.audio
.i_bitspersample
= 12;
932 else if( !strcmp( sub
->codecName(), "PCMU" ) )
934 tk
->fmt
.i_codec
= VLC_CODEC_MULAW
;
935 tk
->fmt
.audio
.i_bitspersample
= 8;
937 else if( !strcmp( sub
->codecName(), "PCMA" ) )
939 tk
->fmt
.i_codec
= VLC_CODEC_ALAW
;
940 tk
->fmt
.audio
.i_bitspersample
= 8;
942 else if( !strncmp( sub
->codecName(), "G726", 4 ) )
944 tk
->fmt
.i_codec
= VLC_CODEC_ADPCM_G726
;
945 tk
->fmt
.audio
.i_rate
= 8000;
946 tk
->fmt
.audio
.i_channels
= 1;
947 if( !strcmp( sub
->codecName()+5, "40" ) )
948 tk
->fmt
.i_bitrate
= 40000;
949 else if( !strcmp( sub
->codecName()+5, "32" ) )
950 tk
->fmt
.i_bitrate
= 32000;
951 else if( !strcmp( sub
->codecName()+5, "24" ) )
952 tk
->fmt
.i_bitrate
= 24000;
953 else if( !strcmp( sub
->codecName()+5, "16" ) )
954 tk
->fmt
.i_bitrate
= 16000;
956 else if( !strcmp( sub
->codecName(), "AMR" ) )
958 tk
->fmt
.i_codec
= VLC_CODEC_AMR_NB
;
960 else if( !strcmp( sub
->codecName(), "AMR-WB" ) )
962 tk
->fmt
.i_codec
= VLC_CODEC_AMR_WB
;
964 else if( !strcmp( sub
->codecName(), "MP4A-LATM" ) )
966 unsigned int i_extra
;
969 tk
->fmt
.i_codec
= VLC_CODEC_MP4A
;
971 if( ( p_extra
= parseStreamMuxConfigStr( sub
->fmtp_config(),
974 tk
->fmt
.i_extra
= i_extra
;
975 tk
->fmt
.p_extra
= xmalloc( i_extra
);
976 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
979 /* Because the "faad" decoder does not handle the LATM
980 * data length field at the start of each returned LATM
981 * frame, tell the RTP source to omit. */
982 ((MPEG4LATMAudioRTPSource
*)sub
->rtpSource())->omitLATMDataLengthField();
984 else if( !strcmp( sub
->codecName(), "MPEG4-GENERIC" ) )
986 unsigned int i_extra
;
989 tk
->fmt
.i_codec
= VLC_CODEC_MP4A
;
991 if( ( p_extra
= parseGeneralConfigStr( sub
->fmtp_config(),
994 tk
->fmt
.i_extra
= i_extra
;
995 tk
->fmt
.p_extra
= xmalloc( i_extra
);
996 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
1000 else if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
1002 tk
->format
= live_track_t::ASF_STREAM
;
1003 if( p_sys
->p_out_asf
== NULL
)
1005 vlc_demux_chained_New( VLC_OBJECT(p_demux
), "asf",
1008 else if( !strcmp( sub
->codecName(), "X-QT" ) ||
1009 !strcmp( sub
->codecName(), "X-QUICKTIME" ) )
1011 tk
->format
= live_track_t::QUICKTIME_STREAM
;
1013 else if( !strcmp( sub
->codecName(), "SPEEX" ) )
1015 tk
->fmt
.i_codec
= VLC_FOURCC( 's', 'p', 'x', 'r' );
1017 else if( !strcmp( sub
->codecName(), "VORBIS" ) )
1019 tk
->fmt
.i_codec
= VLC_CODEC_VORBIS
;
1020 unsigned int i_extra
;
1021 unsigned char *p_extra
;
1022 if( ( p_extra
=parseVorbisConfigStr( sub
->fmtp_config(),
1025 tk
->fmt
.i_extra
= i_extra
;
1026 tk
->fmt
.p_extra
= p_extra
;
1029 msg_Warn( p_demux
,"Missing or unsupported vorbis header." );
1031 else if( !strcmp( sub
->codecName(), "OPUS" ) )
1033 tk
->fmt
.i_codec
= VLC_CODEC_OPUS
;
1036 else if( !strcmp( sub
->mediumName(), "video" ) )
1038 es_format_Init( &tk
->fmt
, VIDEO_ES
, VLC_CODEC_UNKNOWN
);
1039 if( !strcmp( sub
->codecName(), "MPV" ) )
1041 tk
->fmt
.i_codec
= VLC_CODEC_MPGV
;
1042 tk
->fmt
.b_packetized
= false;
1044 else if( !strcmp( sub
->codecName(), "H263" ) ||
1045 !strcmp( sub
->codecName(), "H263-1998" ) ||
1046 !strcmp( sub
->codecName(), "H263-2000" ) )
1048 tk
->fmt
.i_codec
= VLC_CODEC_H263
;
1050 else if( !strcmp( sub
->codecName(), "H261" ) )
1052 tk
->fmt
.i_codec
= VLC_CODEC_H261
;
1054 else if( !strcmp( sub
->codecName(), "H264" ) )
1056 unsigned int i_extra
= 0;
1057 uint8_t *p_extra
= NULL
;
1059 tk
->fmt
.i_codec
= VLC_CODEC_H264
;
1060 tk
->fmt
.b_packetized
= false;
1062 if((p_extra
=parseH264ConfigStr( sub
->fmtp_spropparametersets(),
1065 tk
->fmt
.i_extra
= i_extra
;
1066 tk
->fmt
.p_extra
= xmalloc( i_extra
);
1067 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
1072 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1393372800 // 2014.02.26
1073 else if( !strcmp( sub
->codecName(), "H265" ) )
1075 unsigned int i_extra1
= 0, i_extra2
= 0, i_extra3
= 0, i_extraTot
;
1076 uint8_t *p_extra1
= NULL
, *p_extra2
= NULL
, *p_extra3
= NULL
;
1078 tk
->fmt
.i_codec
= VLC_CODEC_HEVC
;
1079 tk
->fmt
.b_packetized
= false;
1081 p_extra1
= parseH264ConfigStr( sub
->fmtp_spropvps(), i_extra1
);
1082 p_extra2
= parseH264ConfigStr( sub
->fmtp_spropsps(), i_extra2
);
1083 p_extra3
= parseH264ConfigStr( sub
->fmtp_sproppps(), i_extra3
);
1084 i_extraTot
= i_extra1
+ i_extra2
+ i_extra3
;
1085 if( i_extraTot
> 0 )
1087 tk
->fmt
.i_extra
= i_extraTot
;
1088 tk
->fmt
.p_extra
= xmalloc( i_extraTot
);
1091 memcpy( tk
->fmt
.p_extra
, p_extra1
, i_extra1
);
1095 memcpy( ((char*)tk
->fmt
.p_extra
)+i_extra1
, p_extra2
, i_extra2
);
1099 memcpy( ((char*)tk
->fmt
.p_extra
)+i_extra1
+i_extra2
, p_extra3
, i_extra3
);
1102 delete[] p_extra1
; delete[] p_extra2
; delete[] p_extra3
;
1106 else if( !strcmp( sub
->codecName(), "JPEG" ) )
1108 tk
->fmt
.i_codec
= VLC_CODEC_MJPG
;
1110 else if( !strcmp( sub
->codecName(), "MP4V-ES" ) )
1112 unsigned int i_extra
;
1115 tk
->fmt
.i_codec
= VLC_CODEC_MP4V
;
1117 if( ( p_extra
= parseGeneralConfigStr( sub
->fmtp_config(),
1120 tk
->fmt
.i_extra
= i_extra
;
1121 tk
->fmt
.p_extra
= xmalloc( i_extra
);
1122 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
1126 else if( !strcmp( sub
->codecName(), "X-QT" ) ||
1127 !strcmp( sub
->codecName(), "X-QUICKTIME" ) ||
1128 !strcmp( sub
->codecName(), "X-QDM" ) ||
1129 !strcmp( sub
->codecName(), "X-SV3V-ES" ) ||
1130 !strcmp( sub
->codecName(), "X-SORENSONVIDEO" ) )
1132 tk
->format
= live_track_t::QUICKTIME_STREAM
;
1134 else if( !strcmp( sub
->codecName(), "MP2T" ) )
1136 tk
->format
= live_track_t::MULTIPLEXED_STREAM
;
1138 vlc_demux_chained_New( VLC_OBJECT(p_demux
), "ts",
1141 else if( !strcmp( sub
->codecName(), "MP2P" ) ||
1142 !strcmp( sub
->codecName(), "MP1S" ) )
1144 tk
->format
= live_track_t::MULTIPLEXED_STREAM
;
1146 vlc_demux_chained_New( VLC_OBJECT(p_demux
), "ps",
1149 else if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
1151 tk
->format
= live_track_t::ASF_STREAM
;
1152 if( p_sys
->p_out_asf
== NULL
)
1154 vlc_demux_chained_New( VLC_OBJECT(p_demux
),
1155 "asf", p_demux
->out
);
1157 else if( !strcmp( sub
->codecName(), "DV" ) )
1159 tk
->format
= live_track_t::MULTIPLEXED_STREAM
;
1160 tk
->b_discard_trunc
= true;
1162 vlc_demux_chained_New( VLC_OBJECT(p_demux
), "rawdv",
1165 else if( !strcmp( sub
->codecName(), "VP8" ) )
1167 tk
->fmt
.i_codec
= VLC_CODEC_VP8
;
1169 else if( !strcmp( sub
->codecName(), "THEORA" ) )
1171 tk
->fmt
.i_codec
= VLC_CODEC_THEORA
;
1172 unsigned int i_extra
;
1173 unsigned char *p_extra
;
1174 if( ( p_extra
=parseVorbisConfigStr( sub
->fmtp_config(),
1177 tk
->fmt
.i_extra
= i_extra
;
1178 tk
->fmt
.p_extra
= p_extra
;
1181 msg_Warn( p_demux
,"Missing or unsupported theora header." );
1184 else if( !strcmp( sub
->mediumName(), "text" ) )
1186 es_format_Init( &tk
->fmt
, SPU_ES
, VLC_CODEC_UNKNOWN
);
1188 if( !strcmp( sub
->codecName(), "T140" ) )
1190 tk
->fmt
.i_codec
= VLC_CODEC_ITU_T140
;
1194 /* Try and parse a=lang: attribute */
1195 p_lang
= strstr( sub
->savedSDPLines(), "a=lang:" );
1197 p_lang
= p_sess_lang
;
1201 unsigned i_lang_len
;
1203 i_lang_len
= strcspn( p_lang
, " \r\n" );
1204 tk
->fmt
.psz_language
= strndup( p_lang
, i_lang_len
);
1207 if( tk
->format
== live_track_t::SINGLE_STREAM
)
1209 tk
->p_es
= es_out_Add( p_demux
->out
, &tk
->fmt
);
1212 if( sub
->rtcpInstance() != NULL
)
1214 sub
->rtcpInstance()->setByeHandler( StreamClose
, tk
);
1218 tk
->format
== live_track_t::QUICKTIME_STREAM
||
1219 (tk
->format
== live_track_t::MULTIPLEXED_STREAM
&& tk
->p_out_muxed
) ||
1220 (tk
->format
== live_track_t::ASF_STREAM
&& p_sys
->p_out_asf
) )
1222 TAB_APPEND_CAST( (live_track_t
**), p_sys
->i_track
, p_sys
->track
, tk
);
1227 msg_Err( p_demux
, "unusable RTSP track. this should not happen" );
1228 es_format_Clean( &tk
->fmt
);
1234 if( p_sys
->i_track
<= 0 ) i_return
= VLC_EGENERIC
;
1236 /* Retrieve the starttime if possible */
1237 p_sys
->f_npt_start
= p_sys
->ms
->playStartTime();
1239 /* Retrieve the duration if possible */
1240 p_sys
->f_npt_length
= p_sys
->ms
->playEndTime();
1243 msg_Dbg( p_demux
, "setup start: %f stop:%f", p_sys
->f_npt_start
, p_sys
->f_npt_length
);
1246 p_sys
->b_no_data
= true;
1247 p_sys
->i_no_data_ti
= 0;
1248 p_sys
->b_rtcp_sync
= false;
1249 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1254 /*****************************************************************************
1255 * Play: starts the actual playback of the stream
1256 *****************************************************************************/
1257 static int Play( demux_t
*p_demux
)
1259 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
1264 p_sys
->rtsp
->sendPlayCommand( *p_sys
->ms
, default_live555_callback
, p_sys
->f_npt_start
, -1, 1 );
1266 if( !wait_Live555_response(p_demux
) )
1268 msg_Err( p_demux
, "RTSP PLAY failed %s", p_sys
->env
->getResultMsg() );
1269 return VLC_EGENERIC
;
1272 /* Retrieve the timeout value and set up a timeout prevention thread */
1273 int timeout
= p_sys
->rtsp
->sessionTimeoutParameter();
1275 timeout
= 60; /* default value from RFC2326 */
1276 msg_Dbg( p_demux
, "We have a timeout of %d seconds", timeout
);
1278 vlc_tick_t interval
= vlc_tick_from_sec(timeout
- 2);
1279 vlc_timer_schedule( p_sys
->timer
, false, interval
, interval
);
1281 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1283 /* Retrieve the starttime if possible */
1284 p_sys
->f_npt_start
= p_sys
->ms
->playStartTime();
1285 if( p_sys
->ms
->playEndTime() > 0 )
1286 p_sys
->f_npt_length
= p_sys
->ms
->playEndTime();
1288 msg_Dbg( p_demux
, "play start: %f stop:%f", p_sys
->f_npt_start
, p_sys
->f_npt_length
);
1292 /*****************************************************************************
1293 * HasSharedSession: returns if the session is shared with another stream
1294 *****************************************************************************/
1295 static bool HasSharedSession( MediaSubsession
*session
)
1297 if( session
->sessionId() == NULL
)
1299 MediaSubsessionIterator
*it
=
1300 new MediaSubsessionIterator( session
->parentSession() );
1301 MediaSubsession
*subsession
;
1302 bool b_shared
= false;
1303 while( (subsession
= it
->next()) != NULL
)
1305 if( session
== subsession
)
1307 if( subsession
->sessionId() != NULL
&&
1308 !strcmp( session
->sessionId(), subsession
->sessionId() ) )
1318 /*****************************************************************************
1319 * ResumeTrack: setup or resume a silenced track
1320 *****************************************************************************/
1321 static void ResumeTrack( demux_t
*p_demux
, live_track_t
*tk
)
1323 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
1325 bool b_rtsp_tcp
= var_GetBool( p_demux
, "rtsp-tcp" ) ||
1326 var_GetBool( p_demux
, "rtsp-http" );
1327 p_sys
->rtsp
->sendSetupCommand( *tk
->sub
, default_live555_callback
, False
,
1328 toBool( b_rtsp_tcp
),
1329 toBool( p_sys
->b_force_mcast
&& !b_rtsp_tcp
) );
1330 if( !wait_Live555_response( p_demux
) )
1332 msg_Err( p_demux
, "SETUP of'%s/%s' failed %s",
1333 tk
->sub
->mediumName(), tk
->sub
->codecName(),
1334 p_sys
->env
->getResultMsg() );
1338 p_sys
->rtsp
->sendPlayCommand( *tk
->sub
, default_live555_callback
, -1, -1, p_sys
->ms
->scale() );
1339 if( !wait_Live555_response(p_demux
) )
1341 msg_Err( p_demux
, "RTSP PLAY failed %s", p_sys
->env
->getResultMsg() );
1342 if( (p_sys
->capabilities
& CAP_SUBSESSION_TEARDOWN
) ||
1343 !HasSharedSession( tk
->sub
) )
1345 tk
->state
= live_track_t::STATE_TEARDOWN
;
1346 p_sys
->rtsp
->sendTeardownCommand( *tk
->sub
, NULL
);
1350 tk
->state
= live_track_t::STATE_SELECTED
;
1354 /*****************************************************************************
1356 *****************************************************************************/
1357 static int Demux( demux_t
*p_demux
)
1359 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
1362 bool b_send_pcr
= true;
1365 /* Protect Live555 from simultaneous calls in TimeoutPrevention()
1367 vlc::threads::mutex_locker
locker( p_sys
->timeout_mutex
);
1369 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1371 live_track_t
*tk
= p_sys
->track
[i
];
1376 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
1377 if( !b
&& (tk
->state
== live_track_t::STATE_SELECTED
) && p_sys
->rtsp
)
1379 if( (p_sys
->capabilities
& CAP_SUBSESSION_TEARDOWN
) ||
1380 !HasSharedSession( tk
->sub
) )
1382 tk
->state
= live_track_t::STATE_TEARDOWN
;
1383 p_sys
->rtsp
->sendTeardownCommand( *tk
->sub
, NULL
);
1385 else tk
->state
= live_track_t::STATE_IGNORED
;
1387 else if( b
&& tk
->state
!= live_track_t::STATE_SELECTED
)
1389 if( tk
->state
!= live_track_t::STATE_IGNORED
)
1390 ResumeTrack( p_demux
, tk
);
1392 tk
->state
= live_track_t::STATE_SELECTED
;
1394 if( tk
->state
!= live_track_t::STATE_SELECTED
)
1395 es_out_Control( p_demux
->out
, ES_OUT_SET_ES_STATE
, tk
->p_es
, false );
1399 if( tk
->format
== live_track_t::ASF_STREAM
||
1400 tk
->format
== live_track_t::MULTIPLEXED_STREAM
)
1406 /* First warn we want to read data */
1407 p_sys
->event_data
= 0;
1408 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1410 live_track_t
*tk
= p_sys
->track
[i
];
1412 if( tk
->waiting
== 0 )
1415 tk
->sub
->readSource()->getNextFrame( tk
->p_buffer
, tk
->i_buffer
,
1416 StreamRead
, tk
, StreamClose
, tk
);
1419 /* Create a task that will be called if we wait more than 300ms */
1420 task
= p_sys
->scheduler
->scheduleDelayedTask( 300000, TaskInterruptData
, p_demux
);
1423 p_sys
->scheduler
->doEventLoop( &p_sys
->event_data
);
1425 /* remove the task */
1426 p_sys
->scheduler
->unscheduleDelayedTask( task
);
1430 vlc_tick_t i_minpcr
= VLC_TICK_INVALID
;
1431 bool b_need_flush
= false;
1433 /* Check for gap in pts value */
1434 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1436 live_track_t
*tk
= p_sys
->track
[i
];
1438 if( tk
->state
!= live_track_t::STATE_SELECTED
||
1439 (p_sys
->b_rtcp_sync
&& !tk
->b_rtcp_sync
) )
1442 /* Check for gap in pts value */
1443 b_need_flush
|= (tk
->b_flushing_discontinuity
);
1445 if( i_minpcr
== VLC_TICK_INVALID
|| ( tk
->i_pcr
!= VLC_TICK_INVALID
&& i_minpcr
> tk
->i_pcr
) )
1446 i_minpcr
= tk
->i_pcr
;
1449 if( p_sys
->i_pcr
!= VLC_TICK_INVALID
&& b_need_flush
)
1451 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
1452 p_sys
->i_pcr
= i_minpcr
;
1455 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1457 live_track_t
*tk
= p_sys
->track
[i
];
1458 tk
->i_prevpts
= VLC_TICK_INVALID
;
1459 tk
->i_pcr
= VLC_TICK_INVALID
;
1461 tk
->b_flushing_discontinuity
= false;
1462 tk
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1464 if( p_sys
->i_pcr
!= VLC_TICK_INVALID
)
1465 es_out_SetPCR( p_demux
->out
, VLC_TICK_0
+
1466 __MAX(0, p_sys
->i_pcr
- PCR_OFF
) );
1468 else if( p_sys
->i_pcr
== VLC_TICK_INVALID
||
1469 i_minpcr
> p_sys
->i_pcr
+ PCR_OBS
)
1471 p_sys
->i_pcr
= __MAX(0, i_minpcr
- PCR_OFF
);
1472 if( p_sys
->i_pcr
!= VLC_TICK_INVALID
)
1473 es_out_SetPCR( p_demux
->out
, VLC_TICK_0
+ p_sys
->i_pcr
);
1477 if( p_sys
->b_multicast
&& p_sys
->b_no_data
&&
1478 ( p_sys
->i_no_data_ti
> 120 ) )
1480 /* FIXME Make this configurable
1481 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1485 else if( !p_sys
->b_multicast
&& !p_sys
->b_paused
&&
1486 p_sys
->b_no_data
&& ( p_sys
->i_no_data_ti
> 34 ) )
1488 bool b_rtsp_tcp
= var_GetBool( p_demux
, "rtsp-tcp" ) ||
1489 var_GetBool( p_demux
, "rtsp-http" );
1491 if( !b_rtsp_tcp
&& p_sys
->rtsp
&& p_sys
->ms
)
1493 msg_Warn( p_demux
, "no data received in 10s. Switching to TCP" );
1494 if( RollOverTcp( p_demux
) )
1496 msg_Err( p_demux
, "TCP rollover failed, aborting" );
1501 msg_Err( p_demux
, "no data received in 10s, aborting" );
1504 else if( !p_sys
->b_multicast
&& !p_sys
->b_paused
&&
1505 ( p_sys
->i_no_data_ti
> 34 ) )
1508 msg_Warn( p_demux
, "no data received in 10s, eof ?" );
1511 return p_sys
->b_error
? 0 : 1;
1514 /*****************************************************************************
1516 *****************************************************************************/
1517 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
1519 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
1523 vlc::threads::mutex_locker
locker( p_sys
->timeout_mutex
); /* (see same in Demux) */
1527 case DEMUX_GET_TIME
:
1528 if( p_sys
->f_npt
> 0 )
1530 *va_arg( args
, vlc_tick_t
* ) = vlc_tick_from_sec(p_sys
->f_npt
);
1533 return VLC_EGENERIC
;
1535 case DEMUX_GET_LENGTH
:
1536 if( p_sys
->f_npt_length
> 0 )
1538 if( unlikely(p_sys
->f_npt_length
>= (double)(INT64_MAX
/ CLOCK_FREQ
)) )
1539 *va_arg( args
, vlc_tick_t
* ) = INT64_MAX
;
1541 *va_arg( args
, vlc_tick_t
* ) = vlc_tick_from_sec(p_sys
->f_npt_length
);
1544 return VLC_EGENERIC
;
1546 case DEMUX_GET_POSITION
:
1547 pf
= va_arg( args
, double * );
1548 if( (p_sys
->f_npt_length
> 0) && (p_sys
->f_npt
> 0) )
1550 *pf
= p_sys
->f_npt
/ p_sys
->f_npt_length
;
1553 return VLC_EGENERIC
;
1555 case DEMUX_SET_POSITION
:
1556 case DEMUX_SET_TIME
:
1557 if( p_sys
->rtsp
&& (p_sys
->f_npt_length
> 0) )
1561 if( (i_query
== DEMUX_SET_TIME
) && (p_sys
->f_npt
> 0) )
1563 time
= secf_from_vlc_tick(va_arg( args
, vlc_tick_t
)); /* in second */
1565 else if( i_query
== DEMUX_SET_TIME
)
1566 return VLC_EGENERIC
;
1569 f
= va_arg( args
, double );
1570 time
= f
* p_sys
->f_npt_length
; /* in second */
1573 if( p_sys
->b_paused
)
1575 p_sys
->f_seek_request
= time
;
1579 p_sys
->rtsp
->sendPauseCommand( *p_sys
->ms
, default_live555_callback
);
1581 if( !wait_Live555_response( p_demux
) )
1583 msg_Err( p_demux
, "PAUSE before seek failed %s",
1584 p_sys
->env
->getResultMsg() );
1585 return VLC_EGENERIC
;
1588 p_sys
->rtsp
->sendPlayCommand( *p_sys
->ms
, default_live555_callback
, time
, -1, 1 );
1590 if( !wait_Live555_response( p_demux
) )
1592 msg_Err( p_demux
, "seek PLAY failed %s",
1593 p_sys
->env
->getResultMsg() );
1594 return VLC_EGENERIC
;
1596 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1598 for( int i
= 0; i
< p_sys
->i_track
; i
++ )
1600 p_sys
->track
[i
]->b_rtcp_sync
= false;
1601 p_sys
->track
[i
]->i_prevpts
= VLC_TICK_INVALID
;
1602 p_sys
->track
[i
]->i_pcr
= VLC_TICK_INVALID
;
1603 dtsgen_Resync( &p_sys
->track
[i
]->dtsgen
);
1606 /* Retrieve the starttime if possible */
1607 p_sys
->f_npt
= p_sys
->f_npt_start
= p_sys
->ms
->playStartTime();
1609 /* Retrieve the duration if possible */
1610 if( p_sys
->ms
->playEndTime() > 0 )
1611 p_sys
->f_npt_length
= p_sys
->ms
->playEndTime();
1613 msg_Dbg( p_demux
, "seek start: %f stop:%f", p_sys
->f_npt_start
, p_sys
->f_npt_length
);
1616 return VLC_EGENERIC
;
1618 /* Special for access_demux */
1619 case DEMUX_CAN_PAUSE
:
1620 case DEMUX_CAN_SEEK
:
1621 pb
= va_arg( args
, bool * );
1622 if( p_sys
->rtsp
&& p_sys
->f_npt_length
> 0 )
1623 /* Not always true, but will be handled in SET_PAUSE_STATE */
1629 case DEMUX_CAN_CONTROL_PACE
:
1630 pb
= va_arg( args
, bool * );
1632 #if 1 /* Disable for now until we have a clock synchro algo
1633 * which works with something else than MPEG over UDP */
1640 case DEMUX_CAN_CONTROL_RATE
:
1641 pb
= va_arg( args
, bool * );
1643 *pb
= (p_sys
->rtsp
!= NULL
) &&
1644 (p_sys
->f_npt_length
> 0) &&
1645 (p_sys
->capabilities
& CAP_RATE_CONTROL
);
1648 case DEMUX_SET_RATE
:
1650 float *pf_scale
, f_scale
;
1653 if( !p_sys
->rtsp
|| (p_sys
->f_npt_length
<= 0) ||
1654 !(p_sys
->capabilities
& CAP_RATE_CONTROL
) )
1655 return VLC_EGENERIC
;
1657 /* According to RFC 2326 p56 chapter 12.35 a RTSP server that
1660 * "[...] should try to approximate the viewing rate, but
1661 * may restrict the range of scale values that it supports.
1662 * The response MUST contain the actual scale value chosen
1665 * Scale = 1 indicates normal play
1666 * Scale > 1 indicates fast forward
1667 * Scale < 1 && Scale > 0 indicates slow motion
1668 * Scale < 0 value indicates rewind
1671 pf_scale
= va_arg( args
, float * );
1672 f_scale
= *pf_scale
;
1673 f_old_scale
= p_sys
->ms
->scale();
1675 /* Passing -1 for the start and end time will mean liveMedia won't
1676 * create a Range: section for the RTSP message. The server should
1677 * pick up from the current position */
1678 p_sys
->rtsp
->sendPlayCommand( *p_sys
->ms
, default_live555_callback
, -1, -1, f_scale
);
1680 if( !wait_Live555_response( p_demux
) )
1682 msg_Err( p_demux
, "PLAY with Scale %0.2f failed %s", f_scale
,
1683 p_sys
->env
->getResultMsg() );
1684 return VLC_EGENERIC
;
1687 if( p_sys
->ms
->scale() == f_old_scale
)
1689 msg_Err( p_demux
, "no scale change using old Scale %0.2f",
1690 p_sys
->ms
->scale() );
1691 return VLC_EGENERIC
;
1694 /* ReSync the stream */
1695 p_sys
->f_npt_start
= 0;
1696 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1699 *pf_scale
= p_sys
->ms
->scale() ;
1700 msg_Dbg( p_demux
, "PLAY with new Scale %0.2f", p_sys
->ms
->scale() );
1704 case DEMUX_SET_PAUSE_STATE
:
1706 bool b_pause
= (bool)va_arg( args
, int );
1707 if( p_sys
->rtsp
== NULL
)
1708 return VLC_EGENERIC
;
1710 if( b_pause
== p_sys
->b_paused
)
1713 p_sys
->rtsp
->sendPauseCommand( *p_sys
->ms
, default_live555_callback
);
1715 p_sys
->rtsp
->sendPlayCommand( *p_sys
->ms
, default_live555_callback
, p_sys
->f_seek_request
,
1716 -1.0f
, p_sys
->ms
->scale() );
1718 if( !wait_Live555_response( p_demux
) )
1720 msg_Err( p_demux
, "PLAY or PAUSE failed %s", p_sys
->env
->getResultMsg() );
1721 return VLC_EGENERIC
;
1723 p_sys
->f_seek_request
= -1;
1724 p_sys
->b_paused
= b_pause
;
1726 if( !p_sys
->b_paused
)
1728 for( int i
= 0; i
< p_sys
->i_track
; i
++ )
1730 live_track_t
*tk
= p_sys
->track
[i
];
1731 tk
->b_rtcp_sync
= false;
1732 tk
->b_flushing_discontinuity
= false;
1733 tk
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
1734 tk
->i_prevpts
= VLC_TICK_INVALID
;
1735 tk
->i_pcr
= VLC_TICK_INVALID
;
1737 p_sys
->i_pcr
= VLC_TICK_INVALID
;
1738 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
1741 /* Reset data received counter */
1742 p_sys
->i_no_data_ti
= 0;
1744 /* Retrieve the starttime if possible */
1745 p_sys
->f_npt_start
= p_sys
->ms
->playStartTime();
1747 /* Retrieve the duration if possible */
1748 if( p_sys
->ms
->playEndTime() )
1749 p_sys
->f_npt_length
= p_sys
->ms
->playEndTime();
1751 msg_Dbg( p_demux
, "pause start: %f stop:%f", p_sys
->f_npt_start
, p_sys
->f_npt_length
);
1755 case DEMUX_GET_TITLE_INFO
:
1756 case DEMUX_SET_TITLE
:
1757 case DEMUX_SET_SEEKPOINT
:
1758 return VLC_EGENERIC
;
1760 case DEMUX_GET_PTS_DELAY
:
1761 *va_arg( args
, vlc_tick_t
* ) =
1762 VLC_TICK_FROM_MS(var_InheritInteger( p_demux
, "network-caching" ));
1766 return VLC_EGENERIC
;
1770 /*****************************************************************************
1771 * RollOverTcp: reopen the rtsp into TCP mode
1772 * XXX: ugly, a lot of code are duplicated from Open()
1773 * This should REALLY be fixed
1774 *****************************************************************************/
1775 static int RollOverTcp( demux_t
*p_demux
)
1777 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
1780 var_SetBool( p_demux
, "rtsp-tcp", true );
1782 /* We close the old RTSP session */
1783 vlc_timer_disarm(p_sys
->timer
);
1784 p_sys
->rtsp
->sendTeardownCommand( *p_sys
->ms
, NULL
);
1785 Medium::close( p_sys
->ms
);
1786 RTSPClient::close( p_sys
->rtsp
);
1788 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1790 live_track_t
*tk
= p_sys
->track
[i
];
1792 if( tk
->p_out_muxed
) vlc_demux_chained_Delete( tk
->p_out_muxed
);
1793 if( tk
->p_es
) es_out_Del( p_demux
->out
, tk
->p_es
);
1794 if( tk
->p_asf_block
) block_Release( tk
->p_asf_block
);
1795 es_format_Clean( &tk
->fmt
);
1796 free( tk
->p_buffer
);
1799 TAB_CLEAN( p_sys
->i_track
, p_sys
->track
);
1800 if( p_sys
->p_out_asf
) vlc_demux_chained_Delete( p_sys
->p_out_asf
);
1804 p_sys
->b_no_data
= true;
1805 p_sys
->i_no_data_ti
= 0;
1806 p_sys
->p_out_asf
= NULL
;
1808 /* Reopen rtsp client */
1809 if( ( i_return
= Connect( p_demux
) ) != VLC_SUCCESS
)
1811 msg_Err( p_demux
, "Failed to connect with %s", p_sys
->psz_pl_url
);
1815 if( p_sys
->p_sdp
== NULL
)
1817 msg_Err( p_demux
, "Failed to retrieve the RTSP Session Description" );
1821 if( ( i_return
= SessionsSetup( p_demux
) ) != VLC_SUCCESS
)
1823 msg_Err( p_demux
, "Nothing to play for %s", p_sys
->psz_pl_url
);
1827 if( ( i_return
= Play( p_demux
) ) != VLC_SUCCESS
)
1833 return VLC_EGENERIC
;
1837 /*****************************************************************************
1839 *****************************************************************************/
1840 static block_t
*StreamParseAsf( demux_t
*p_demux
, live_track_t
*tk
,
1842 const uint8_t *p_data
, unsigned i_size
)
1844 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
1845 const unsigned i_packet_size
= p_sys
->asfh
.i_min_data_packet_size
;
1846 block_t
*p_list
= NULL
;
1848 while( i_size
>= 4 )
1850 unsigned i_flags
= p_data
[0];
1851 unsigned i_length_offset
= (p_data
[1] << 16) |
1854 bool b_length
= i_flags
& 0x40;
1855 bool b_relative_ts
= i_flags
& 0x20;
1856 bool b_duration
= i_flags
& 0x10;
1857 bool b_location_id
= i_flags
& 0x08;
1859 //msg_Dbg( p_demux, "ASF: marker=%d size=%d : %c=%d id=%d",
1860 // b_marker, i_size, b_length ? 'L' : 'O', i_length_offset );
1861 unsigned i_header_size
= 4;
1869 if( i_header_size
> i_size
)
1871 msg_Warn( p_demux
, "Invalid header size" );
1876 * When b_length is true, the streams I found do not seems to respect
1877 * the documentation.
1878 * From them, I have failed to find which choice between '__MIN()' or
1879 * 'i_length_offset - i_header_size' is the right one.
1883 i_payload
= __MIN( i_length_offset
, i_size
- i_header_size
);
1885 i_payload
= i_size
- i_header_size
;
1887 if( !tk
->p_asf_block
)
1889 tk
->p_asf_block
= block_Alloc( i_packet_size
);
1890 if( !tk
->p_asf_block
)
1892 tk
->p_asf_block
->i_buffer
= 0;
1894 unsigned i_offset
= b_length
? 0 : i_length_offset
;
1895 if( i_offset
== tk
->p_asf_block
->i_buffer
&& i_offset
+ i_payload
<= i_packet_size
)
1897 memcpy( &tk
->p_asf_block
->p_buffer
[i_offset
], &p_data
[i_header_size
], i_payload
);
1898 tk
->p_asf_block
->i_buffer
+= i_payload
;
1901 /* We have a complete packet */
1902 tk
->p_asf_block
->i_buffer
= i_packet_size
;
1903 block_ChainAppend( &p_list
, tk
->p_asf_block
);
1904 tk
->p_asf_block
= NULL
;
1909 /* Reset on broken stream */
1910 msg_Err( p_demux
, "Broken packet detected (%d vs %zu or %d + %d vs %d)",
1911 i_offset
, tk
->p_asf_block
->i_buffer
, i_offset
, i_payload
, i_packet_size
);
1912 tk
->p_asf_block
->i_buffer
= 0;
1916 p_data
+= i_header_size
+ i_payload
;
1917 i_size
-= i_header_size
+ i_payload
;
1922 /*****************************************************************************
1924 *****************************************************************************/
1925 static void StreamRead( void *p_private
, unsigned int i_size
,
1926 unsigned int i_truncated_bytes
, struct timeval pts
,
1927 unsigned int duration
)
1929 VLC_UNUSED( duration
);
1931 live_track_t
*tk
= (live_track_t
*)p_private
;
1932 demux_t
*p_demux
= tk
->p_demux
;
1933 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
1936 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1938 vlc_tick_t i_pts
= vlc_tick_from_timeval( &pts
);
1940 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1941 i_pts
&= INT64_C(0x00ffffffffffffff);
1943 /* Retrieve NPT for this pts */
1944 tk
->f_npt
= tk
->sub
->getNormalPlayTime(pts
);
1946 if( tk
->format
== live_track_t::QUICKTIME_STREAM
&& tk
->p_es
== NULL
)
1948 QuickTimeGenericRTPSource
*qtRTPSource
=
1949 (QuickTimeGenericRTPSource
*)tk
->sub
->rtpSource();
1950 QuickTimeGenericRTPSource::QTState
&qtState
= qtRTPSource
->qtState
;
1951 uint8_t *sdAtom
= (uint8_t*)&qtState
.sdAtom
[4];
1953 /* Get codec information from the quicktime atoms :
1954 * http://developer.apple.com/quicktime/icefloe/dispatch026.html */
1955 if( tk
->fmt
.i_cat
== VIDEO_ES
) {
1956 if( qtState
.sdAtomSize
< 16 + 32 )
1959 p_sys
->event_data
= 0xff;
1963 tk
->fmt
.i_codec
= VLC_FOURCC(sdAtom
[0],sdAtom
[1],sdAtom
[2],sdAtom
[3]);
1964 tk
->fmt
.video
.i_width
= (sdAtom
[28] << 8) | sdAtom
[29];
1965 tk
->fmt
.video
.i_height
= (sdAtom
[30] << 8) | sdAtom
[31];
1967 if( tk
->fmt
.i_codec
== VLC_FOURCC('a', 'v', 'c', '1') )
1969 uint8_t *pos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
+ 86;
1970 uint8_t *endpos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
1971 + qtRTPSource
->qtState
.sdAtomSize
;
1972 while (pos
+8 < endpos
) {
1973 unsigned int atomLength
= pos
[0]<<24 | pos
[1]<<16 | pos
[2]<<8 | pos
[3];
1974 if( atomLength
== 0 || atomLength
> (unsigned int)(endpos
-pos
)) break;
1975 if( memcmp(pos
+4, "avcC", 4) == 0 &&
1977 atomLength
<= INT_MAX
)
1979 tk
->fmt
.i_extra
= atomLength
-8;
1980 tk
->fmt
.p_extra
= xmalloc( tk
->fmt
.i_extra
);
1981 memcpy(tk
->fmt
.p_extra
, pos
+8, atomLength
-8);
1989 tk
->fmt
.i_extra
= qtState
.sdAtomSize
- 16;
1990 tk
->fmt
.p_extra
= xmalloc( tk
->fmt
.i_extra
);
1991 memcpy( tk
->fmt
.p_extra
, &sdAtom
[12], tk
->fmt
.i_extra
);
1995 if( qtState
.sdAtomSize
< 24 )
1998 p_sys
->event_data
= 0xff;
2002 tk
->fmt
.i_codec
= VLC_FOURCC(sdAtom
[0],sdAtom
[1],sdAtom
[2],sdAtom
[3]);
2003 tk
->fmt
.audio
.i_bitspersample
= (sdAtom
[22] << 8) | sdAtom
[23];
2005 tk
->p_es
= es_out_Add( p_demux
->out
, &tk
->fmt
);
2009 fprintf( stderr
, "StreamRead size=%d pts=%lld\n",
2010 i_size
, vlc_tick_from_timeval( &pts
) );
2013 /* grow buffer if it looks like buffer is too small, but don't eat
2014 * up all the memory on strange streams */
2015 if( i_truncated_bytes
> 0 )
2017 if( tk
->i_buffer
< 2000000 )
2020 msg_Dbg( p_demux
, "lost %d bytes", i_truncated_bytes
);
2021 msg_Dbg( p_demux
, "increasing buffer size to %d", tk
->i_buffer
* 2 );
2022 p_tmp
= realloc( tk
->p_buffer
, tk
->i_buffer
* 2 );
2025 msg_Warn( p_demux
, "realloc failed" );
2029 tk
->p_buffer
= (uint8_t*)p_tmp
;
2034 if( tk
->b_discard_trunc
)
2036 p_sys
->event_data
= 0xff;
2042 assert( i_size
<= tk
->i_buffer
);
2044 if( tk
->fmt
.i_codec
== VLC_CODEC_AMR_NB
||
2045 tk
->fmt
.i_codec
== VLC_CODEC_AMR_WB
)
2047 AMRAudioSource
*amrSource
= (AMRAudioSource
*)tk
->sub
->readSource();
2049 if( (p_block
= block_Alloc( i_size
+ 1 )) )
2051 p_block
->p_buffer
[0] = amrSource
->lastFrameHeader();
2052 memcpy( p_block
->p_buffer
+ 1, tk
->p_buffer
, i_size
);
2055 else if( tk
->fmt
.i_codec
== VLC_CODEC_H261
)
2057 H261VideoRTPSource
*h261Source
= (H261VideoRTPSource
*)tk
->sub
->rtpSource();
2058 uint32_t header
= h261Source
->lastSpecialHeader();
2059 if( (p_block
= block_Alloc( i_size
+ 4 )) )
2061 memcpy( p_block
->p_buffer
, &header
, 4 );
2062 memcpy( p_block
->p_buffer
+ 4, tk
->p_buffer
, i_size
);
2065 else if( tk
->fmt
.i_codec
== VLC_CODEC_H264
|| tk
->fmt
.i_codec
== VLC_CODEC_HEVC
)
2067 if( tk
->fmt
.i_codec
== VLC_CODEC_H264
&& (tk
->p_buffer
[0] & 0x1f) >= 24 )
2068 msg_Warn( p_demux
, "unsupported NAL type for H264" );
2069 else if( tk
->fmt
.i_codec
== VLC_CODEC_HEVC
&& ((tk
->p_buffer
[0] & 0x7e)>>1) >= 48 )
2070 msg_Warn( p_demux
, "unsupported NAL type for H265" );
2072 /* Normal NAL type */
2073 if( (p_block
= block_Alloc( i_size
+ 4 )) )
2075 p_block
->p_buffer
[0] = 0x00;
2076 p_block
->p_buffer
[1] = 0x00;
2077 p_block
->p_buffer
[2] = 0x00;
2078 p_block
->p_buffer
[3] = 0x01;
2079 memcpy( &p_block
->p_buffer
[4], tk
->p_buffer
, i_size
);
2082 else if( tk
->format
== live_track_t::ASF_STREAM
)
2084 p_block
= StreamParseAsf( p_demux
, tk
,
2085 tk
->sub
->rtpSource()->curPacketMarkerBit(),
2086 tk
->p_buffer
, i_size
);
2090 if( (p_block
= block_Alloc( i_size
)) )
2091 memcpy( p_block
->p_buffer
, tk
->p_buffer
, i_size
);
2094 /* No data sent. Always in sync then */
2095 if( !tk
->b_rtcp_sync
&& tk
->sub
->rtpSource() &&
2096 tk
->sub
->rtpSource()->hasBeenSynchronizedUsingRTCP() )
2098 msg_Dbg( p_demux
, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
2099 p_sys
->b_rtcp_sync
= tk
->b_rtcp_sync
= true;
2100 if( tk
->i_pcr
!= VLC_TICK_INVALID
)
2102 tk
->i_next_block_flags
|= BLOCK_FLAG_DISCONTINUITY
;
2103 const vlc_tick_t i_max_diff
= vlc_tick_from_sec(( tk
->fmt
.i_cat
== SPU_ES
) ? 60 : 1);
2104 tk
->b_flushing_discontinuity
= (llabs(i_pts
- tk
->i_pcr
) > i_max_diff
);
2106 tk
->dtsgen
.count
= 0;
2110 /* Update our global npt value */
2111 if( tk
->f_npt
> 0 &&
2112 ( tk
->f_npt
< p_sys
->f_npt_length
|| p_sys
->f_npt_length
<= 0 ) )
2113 p_sys
->f_npt
= tk
->f_npt
;
2117 switch( tk
->format
)
2119 case live_track_t::ASF_STREAM
:
2120 vlc_demux_chained_Send( p_sys
->p_out_asf
, p_block
);
2122 case live_track_t::MULTIPLEXED_STREAM
:
2123 vlc_demux_chained_Send( tk
->p_out_muxed
, p_block
);
2126 if( i_pts
!= tk
->i_prevpts
)
2128 p_block
->i_pts
= VLC_TICK_0
+ i_pts
;
2129 tk
->i_prevpts
= i_pts
;
2131 dtsgen_AddNextPTS( &tk
->dtsgen
, i_pts
);
2134 /*FIXME: for h264 you should check that packetization-mode=1 in sdp-file */
2135 switch( tk
->fmt
.i_codec
)
2137 case VLC_CODEC_MPGV
:
2138 case VLC_CODEC_H264
:
2139 case VLC_CODEC_HEVC
:
2140 p_block
->i_dts
= dtsgen_GetDTS( &tk
->dtsgen
);
2141 dtsgen_Debug( VLC_OBJECT(p_demux
), &tk
->dtsgen
, p_block
->i_dts
, p_block
->i_pts
);
2145 p_block
->i_dts
= VLC_TICK_0
+ i_pts
;
2149 if( i_truncated_bytes
)
2150 p_block
->i_flags
|= BLOCK_FLAG_CORRUPTED
;
2152 if( unlikely(tk
->i_next_block_flags
) )
2154 p_block
->i_flags
|= tk
->i_next_block_flags
;
2155 tk
->i_next_block_flags
= 0;
2158 vlc_tick_t i_pcr
= p_block
->i_dts
> VLC_TICK_INVALID
? p_block
->i_dts
: p_block
->i_pts
;
2159 es_out_Send( p_demux
->out
, tk
->p_es
, p_block
);
2160 if( i_pcr
> VLC_TICK_INVALID
)
2162 if( tk
->i_pcr
< i_pcr
)
2169 /* warn that's ok */
2170 p_sys
->event_data
= 0xff;
2172 /* we have read data */
2174 p_sys
->b_no_data
= false;
2175 p_sys
->i_no_data_ti
= 0;
2178 /*****************************************************************************
2180 *****************************************************************************/
2181 static void StreamClose( void *p_private
)
2183 live_track_t
*tk
= (live_track_t
*)p_private
;
2184 demux_t
*p_demux
= tk
->p_demux
;
2185 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
2186 tk
->state
= live_track_t::STATE_IGNORED
;
2187 p_sys
->event_rtsp
= 0xff;
2188 p_sys
->event_data
= 0xff;
2191 es_out_Control( p_demux
->out
, ES_OUT_SET_ES_STATE
, tk
->p_es
, false );
2194 for( int i
= 0; i
< p_sys
->i_track
; i
++ )
2196 if( p_sys
->track
[i
]->state
== live_track_t::STATE_SELECTED
)
2199 msg_Dbg( p_demux
, "RTSP track Close, %d track remaining", nb_tracks
);
2201 p_sys
->b_error
= true;
2205 /*****************************************************************************
2207 *****************************************************************************/
2208 static void TaskInterruptRTSP( void *p_private
)
2210 demux_t
*p_demux
= (demux_t
*)p_private
;
2211 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
2214 p_sys
->event_rtsp
= 0xff;
2217 static void TaskInterruptData( void *p_private
)
2219 demux_t
*p_demux
= (demux_t
*)p_private
;
2220 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
2222 p_sys
->i_no_data_ti
++;
2225 p_sys
->event_data
= 0xff;
2228 /*****************************************************************************
2230 *****************************************************************************/
2231 static void TimeoutPrevention( void *p_data
)
2233 demux_t
*p_demux
= (demux_t
*) p_data
;
2234 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
2237 if( var_GetBool( p_demux
, "rtsp-tcp" ) )
2240 /* Protect Live555 from us calling their functions simultaneously
2241 with Demux() or Control() */
2242 vlc::threads::mutex_locker
locker( p_sys
->timeout_mutex
);
2244 /* If the timer fires while the demuxer owns the lock, and the demuxer
2245 * then torns the session down, the pointers will become NULL. By the time
2246 * this timer callback obtains the callback, either a new session was
2247 * created and the timer is rescheduled, or the pointers are still NULL
2248 * and the timer is descheduled. In the second case, bail out (then wait
2249 * for the timer to be rescheduled or destroyed). In the first case, this
2250 * might send an early refresh - that´s harmless but suboptimal (FIXME). */
2251 if( p_sys
->rtsp
== NULL
|| p_sys
->ms
== NULL
)
2254 bool use_get_param
= p_sys
->b_get_param
;
2256 /* Use GET_PARAMETERS if supported. wmserver dialect supports
2257 * it, but does not report this properly. */
2258 if( var_GetBool( p_demux
, "rtsp-wmserver" ) )
2259 use_get_param
= true;
2262 p_sys
->rtsp
->sendGetParameterCommand( *p_sys
->ms
,
2263 default_live555_callback
, bye
);
2265 p_sys
->rtsp
->sendOptionsCommand( default_live555_callback
, NULL
);
2267 if( !wait_Live555_response( p_demux
) )
2269 msg_Err( p_demux
, "keep-alive failed: %s",
2270 p_sys
->env
->getResultMsg() );
2271 /* Just continue, worst case is we get timed out later */
2275 /*****************************************************************************
2277 *****************************************************************************/
2278 static int ParseASF( demux_t
*p_demux
)
2280 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
2282 const char *psz_marker
= "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
2283 char *psz_asf
= strcasestr( p_sys
->p_sdp
, psz_marker
);
2287 /* Parse the asf header */
2288 if( psz_asf
== NULL
)
2289 return VLC_EGENERIC
;
2291 psz_asf
+= strlen( psz_marker
);
2292 psz_asf
= strdup( psz_asf
); /* Duplicate it */
2293 psz_end
= strchr( psz_asf
, '\n' );
2295 while( psz_end
> psz_asf
&& ( *psz_end
== '\n' || *psz_end
== '\r' ) )
2298 if( psz_asf
>= psz_end
)
2301 return VLC_EGENERIC
;
2304 /* Always smaller */
2305 p_header
= block_Alloc( psz_end
- psz_asf
);
2306 p_header
->i_buffer
= vlc_b64_decode_binary_to_buffer( p_header
->p_buffer
,
2307 p_header
->i_buffer
, psz_asf
);
2308 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
2309 if( p_header
->i_buffer
<= 0 )
2312 return VLC_EGENERIC
;
2315 /* Parse it to get packet size */
2316 asf_HeaderParse( &p_sys
->asfh
, p_header
->p_buffer
, p_header
->i_buffer
);
2318 /* Send it to demuxer */
2319 vlc_demux_chained_Send( p_sys
->p_out_asf
, p_header
);
2326 static unsigned char* parseH264ConfigStr( char const* configStr
,
2327 unsigned int& configSize
)
2330 size_t i_records
= 1;
2334 if( configStr
== NULL
|| *configStr
== '\0' )
2337 psz
= dup
= strdup( configStr
);
2339 /* Count the number of commas */
2340 for( psz
= dup
; *psz
!= '\0'; ++psz
)
2349 size_t configMax
= 5*strlen(dup
);
2350 unsigned char *cfg
= new unsigned char[configMax
];
2352 for( size_t i
= 0; i
< i_records
; ++i
)
2354 cfg
[configSize
++] = 0x00;
2355 cfg
[configSize
++] = 0x00;
2356 cfg
[configSize
++] = 0x00;
2357 cfg
[configSize
++] = 0x01;
2359 configSize
+= vlc_b64_decode_binary_to_buffer( cfg
+configSize
,
2360 configMax
-configSize
, psz
);
2361 psz
+= strlen(psz
)+1;
2368 static uint8_t *parseVorbisConfigStr( char const* configStr
,
2369 unsigned int& configSize
)
2372 if( configStr
== NULL
|| *configStr
== '\0' )
2374 #if LIVEMEDIA_LIBRARY_VERSION_INT >= 1332115200 // 2012.03.20
2375 unsigned char *p_cfg
= base64Decode( configStr
, configSize
);
2377 char* configStr_dup
= strdup( configStr
);
2378 unsigned char *p_cfg
= base64Decode( configStr_dup
, configSize
);
2379 free( configStr_dup
);
2381 uint8_t *p_extra
= NULL
;
2382 /* skip header count, ident number and length (cf. RFC 5215) */
2383 const unsigned int headerSkip
= 9;
2384 if( configSize
> headerSkip
&& ((uint8_t*)p_cfg
)[3] == 1 )
2386 configSize
-= headerSkip
;
2387 p_extra
= (uint8_t*)xmalloc( configSize
);
2388 memcpy( p_extra
, p_cfg
+headerSkip
, configSize
);
2394 static char *passwordLessURL( vlc_url_t
*p_url
)
2398 memcpy( &url
, p_url
, sizeof( vlc_url_t
) );
2400 url
.psz_username
= NULL
;
2401 url
.psz_password
= NULL
;
2402 if( url
.i_port
== 0 )
2404 return vlc_uri_compose( &url
);