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
10 * Sébastien Escudier <sebastien-devel celeos eu>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 * Note: config.h may include inttypes.h, so make sure we define this option
34 #define __STDC_CONSTANT_MACROS 1
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_input.h>
45 #include <vlc_demux.h>
46 #include <vlc_dialog.h>
48 #include <vlc_strings.h>
55 # include <winsock2.h>
58 #include <UsageEnvironment.hh>
59 #include <BasicUsageEnvironment.hh>
60 #include <GroupsockHelper.hh>
61 #include <liveMedia.hh>
62 #include <liveMedia_version.hh>
65 #include "../access/mms/asf.h" /* Who said ugly ? */
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t
* );
74 static void Close( vlc_object_t
* );
76 #define KASENNA_TEXT N_( "Kasenna RTSP dialect")
77 #define KASENNA_LONGTEXT N_( "Kasenna servers use an old and nonstandard " \
78 "dialect of RTSP. With this parameter VLC will try this dialect, but "\
79 "then it cannot connect to normal RTSP servers." )
81 #define WMSERVER_TEXT N_("WMServer RTSP dialect")
82 #define WMSERVER_LONGTEXT N_("WMServer uses a nonstandard dialect " \
83 "of RTSP. Selecting this parameter will tell VLC to assume some " \
84 "options contrary to RFC 2326 guidelines.")
86 #define USER_TEXT N_("RTSP user name")
87 #define USER_LONGTEXT N_("Sets the username for the connection, " \
88 "if no username or password are set in the url.")
89 #define PASS_TEXT N_("RTSP password")
90 #define PASS_LONGTEXT N_("Sets the password for the connection, " \
91 "if no username or password are set in the url.")
94 set_description( N_("RTP/RTSP/SDP demuxer (using Live555)" ) )
95 set_capability( "demux", 50 )
96 set_shortname( "RTP/RTSP")
97 set_callbacks( Open
, Close
)
98 add_shortcut( "live", "livedotcom" )
99 set_category( CAT_INPUT
)
100 set_subcategory( SUBCAT_INPUT_DEMUX
)
103 set_description( N_("RTSP/RTP access and demux") )
104 add_shortcut( "rtsp", "pnm", "live", "livedotcom" )
105 set_capability( "access_demux", 0 )
106 set_callbacks( Open
, Close
)
107 add_bool( "rtsp-tcp", false,
108 N_("Use RTP over RTSP (TCP)"),
109 N_("Use RTP over RTSP (TCP)"), true )
111 add_integer( "rtp-client-port", -1,
113 N_("Port to use for the RTP source of the session"), true )
114 add_bool( "rtsp-mcast", false,
115 N_("Force multicast RTP via RTSP"),
116 N_("Force multicast RTP via RTSP"), true )
118 add_bool( "rtsp-http", false,
119 N_("Tunnel RTSP and RTP over HTTP"),
120 N_("Tunnel RTSP and RTP over HTTP"), true )
122 add_integer( "rtsp-http-port", 80,
123 N_("HTTP tunnel port"),
124 N_("Port to use for tunneling the RTSP/RTP over HTTP."),
126 add_bool( "rtsp-kasenna", false, KASENNA_TEXT
,
127 KASENNA_LONGTEXT
, true )
129 add_bool( "rtsp-wmserver", false, WMSERVER_TEXT
,
130 WMSERVER_LONGTEXT
, true)
132 add_string( "rtsp-user", NULL
, USER_TEXT
,
133 USER_LONGTEXT
, true )
135 add_password( "rtsp-pwd", NULL
, PASS_TEXT
,
136 PASS_LONGTEXT
, true )
141 /*****************************************************************************
143 *****************************************************************************/
148 MediaSubsession
*sub
;
156 block_t
*p_asf_block
;
157 bool b_discard_trunc
;
158 stream_t
*p_out_muxed
; /* for muxed stream */
161 unsigned int i_buffer
;
172 struct timeout_thread_t
176 bool b_handle_keep_alive
;
183 char *p_sdp
; /* XXX mallocated */
184 char *psz_path
; /* URL-encoded path */
188 TaskScheduler
*scheduler
;
189 UsageEnvironment
*env
;
194 live_track_t
**track
;
202 int64_t i_pcr
; /* The clock */
207 /* timeout thread information */
208 int i_timeout
; /* session timeout value in seconds */
209 bool b_timeout_call
;/* mark to send an RTSP call to prevent server timeout */
210 timeout_thread_t
*p_timeout
; /* the actual thread that makes sure we don't timeout */
214 bool b_multicast
; /* if one of the tracks is multicasted */
215 bool b_no_data
; /* if we never received any data */
216 int i_no_data_ti
; /* consecutive number of TaskInterrupt */
221 bool b_get_param
; /* Does the server support GET_PARAMETER */
222 bool b_paused
; /* Are we paused? */
224 int i_live555_ret
; /* live555 callback return code */
226 float f_seek_request
;/* In case we receive a seek request while paused*/
230 class RTSPClientVlc
: public RTSPClient
233 RTSPClientVlc( UsageEnvironment
& env
, char const* rtspURL
, int verbosityLevel
,
234 char const* applicationName
, portNumBits tunnelOverHTTPPortNum
,
235 demux_sys_t
*p_sys
) :
236 RTSPClient( env
, rtspURL
, verbosityLevel
, applicationName
,
237 tunnelOverHTTPPortNum
)
244 static int Demux ( demux_t
* );
245 static int Control( demux_t
*, int, va_list );
247 static int Connect ( demux_t
* );
248 static int SessionsSetup( demux_t
* );
249 static int Play ( demux_t
*);
250 static int ParseASF ( demux_t
* );
251 static int RollOverTcp ( demux_t
* );
253 static void StreamRead ( void *, unsigned int, unsigned int,
254 struct timeval
, unsigned int );
255 static void StreamClose ( void * );
256 static void TaskInterruptData( void * );
257 static void TaskInterruptRTSP( void * );
259 static void* TimeoutPrevention( void * );
261 static unsigned char* parseH264ConfigStr( char const* configStr
,
262 unsigned int& configSize
);
264 /*****************************************************************************
266 *****************************************************************************/
267 static int Open ( vlc_object_t
*p_this
)
269 demux_t
*p_demux
= (demux_t
*)p_this
;
270 demux_sys_t
*p_sys
= NULL
;
273 int i_error
= VLC_EGENERIC
;
277 /* See if it looks like a SDP
278 v, o, s fields are mandatory and in this order */
279 const uint8_t *p_peek
;
280 if( stream_Peek( p_demux
->s
, &p_peek
, 7 ) < 7 ) return VLC_EGENERIC
;
282 if( memcmp( p_peek
, "v=0\r\n", 5 ) &&
283 memcmp( p_peek
, "v=0\n", 4 ) &&
284 ( p_peek
[0] < 'a' || p_peek
[0] > 'z' || p_peek
[1] != '=' ) )
290 p_demux
->pf_demux
= Demux
;
291 p_demux
->pf_control
= Control
;
292 p_demux
->p_sys
= p_sys
= (demux_sys_t
*)malloc( sizeof( demux_sys_t
) );
293 if( !p_sys
) return VLC_ENOMEM
;
295 msg_Dbg( p_demux
, "version "LIVEMEDIA_LIBRARY_VERSION_STRING
);
298 p_sys
->scheduler
= NULL
;
302 TAB_INIT( p_sys
->i_track
, p_sys
->track
);
305 p_sys
->i_npt_start
= 0.;
306 p_sys
->i_npt_length
= 0.;
307 p_sys
->p_out_asf
= NULL
;
308 p_sys
->b_no_data
= true;
309 p_sys
->i_no_data_ti
= 0;
310 p_sys
->p_timeout
= NULL
;
311 p_sys
->i_timeout
= 0;
312 p_sys
->b_timeout_call
= false;
313 p_sys
->b_multicast
= false;
314 p_sys
->b_real
= false;
315 p_sys
->psz_path
= strdup( p_demux
->psz_location
);
316 p_sys
->b_force_mcast
= var_InheritBool( p_demux
, "rtsp-mcast" );
317 p_sys
->b_get_param
= false;
318 p_sys
->b_paused
= false;
319 p_sys
->f_seek_request
= -1;
320 p_sys
->b_error
= false;
321 p_sys
->i_live555_ret
= 0;
323 /* parse URL for rtsp://[user:[passwd]@]serverip:port/options */
324 vlc_UrlParse( &p_sys
->url
, p_sys
->psz_path
, 0 );
326 if( ( p_sys
->scheduler
= BasicTaskScheduler::createNew() ) == NULL
)
328 msg_Err( p_demux
, "BasicTaskScheduler::createNew failed" );
331 if( !( p_sys
->env
= BasicUsageEnvironment::createNew(*p_sys
->scheduler
) ) )
333 msg_Err( p_demux
, "BasicUsageEnvironment::createNew failed" );
337 if( strcasecmp( p_demux
->psz_access
, "sdp" ) )
339 char *p
= p_sys
->psz_path
;
340 while( (p
= strchr( p
, ' ' )) != NULL
) *p
= '+';
343 if( p_demux
->s
!= NULL
)
345 /* Gather the complete sdp file */
347 int i_sdp_max
= 1000;
348 uint8_t *p_sdp
= (uint8_t*) malloc( i_sdp_max
);
352 i_error
= VLC_ENOMEM
;
358 int i_read
= stream_Read( p_demux
->s
, &p_sdp
[i_sdp
],
359 i_sdp_max
- i_sdp
- 1 );
361 if( !vlc_object_alive (p_demux
) )
369 msg_Err( p_demux
, "failed to read SDP" );
376 if( i_read
< i_sdp_max
- i_sdp
- 1 )
383 p_sdp
= (uint8_t*)xrealloc( p_sdp
, i_sdp_max
);
385 p_sys
->p_sdp
= (char*)p_sdp
;
387 else if( ( i_return
= Connect( p_demux
) ) != VLC_SUCCESS
)
389 msg_Err( p_demux
, "Failed to connect with rtsp://%s", p_sys
->psz_path
);
393 if( p_sys
->p_sdp
== NULL
)
395 msg_Err( p_demux
, "Failed to retrieve the RTSP Session Description" );
396 i_error
= VLC_ENOMEM
;
400 if( ( i_return
= SessionsSetup( p_demux
) ) != VLC_SUCCESS
)
402 msg_Err( p_demux
, "Nothing to play for rtsp://%s", p_sys
->psz_path
);
406 if( p_sys
->b_real
) goto error
;
408 if( ( i_return
= Play( p_demux
) ) != VLC_SUCCESS
)
411 if( p_sys
->p_out_asf
&& ParseASF( p_demux
) )
413 msg_Err( p_demux
, "cannot find a usable asf header" );
414 /* TODO Clean tracks */
418 if( p_sys
->i_track
<= 0 )
428 /*****************************************************************************
430 *****************************************************************************/
431 static void Close( vlc_object_t
*p_this
)
433 demux_t
*p_demux
= (demux_t
*)p_this
;
434 demux_sys_t
*p_sys
= p_demux
->p_sys
;
436 if( p_sys
->p_timeout
)
438 vlc_cancel( p_sys
->p_timeout
->handle
);
439 vlc_join( p_sys
->p_timeout
->handle
, NULL
);
440 free( p_sys
->p_timeout
);
443 if( p_sys
->rtsp
&& p_sys
->ms
) p_sys
->rtsp
->sendTeardownCommand( *p_sys
->ms
, NULL
);
444 if( p_sys
->ms
) Medium::close( p_sys
->ms
);
445 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
446 if( p_sys
->env
) p_sys
->env
->reclaim();
448 for( int i
= 0; i
< p_sys
->i_track
; i
++ )
450 live_track_t
*tk
= p_sys
->track
[i
];
452 if( tk
->b_muxed
) stream_Delete( tk
->p_out_muxed
);
453 es_format_Clean( &tk
->fmt
);
454 free( tk
->p_buffer
);
457 TAB_CLEAN( p_sys
->i_track
, p_sys
->track
);
458 if( p_sys
->p_out_asf
) stream_Delete( p_sys
->p_out_asf
);
459 delete p_sys
->scheduler
;
460 free( p_sys
->p_sdp
);
461 free( p_sys
->psz_path
);
463 vlc_UrlClean( &p_sys
->url
);
468 static inline const char *strempty( const char *s
) { return s
?s
:""; }
469 static inline Boolean
toBool( bool b
) { return b
?True
:False
; } // silly, no?
471 static void default_live555_callback( RTSPClient
* client
, int result_code
, char* result_string
)
473 RTSPClientVlc
*client_vlc
= static_cast<RTSPClientVlc
*> ( client
);
474 demux_sys_t
*p_sys
= client_vlc
->p_sys
;
475 delete []result_string
;
476 p_sys
->i_live555_ret
= result_code
;
477 p_sys
->b_error
= p_sys
->i_live555_ret
!= 0;
478 p_sys
->event_rtsp
= 1;
481 /* return true if the RTSP command succeeded */
482 static bool wait_Live555_response( demux_t
*p_demux
, int i_timeout
= 0 /* ms */ )
485 demux_sys_t
* p_sys
= p_demux
->p_sys
;
486 p_sys
->event_rtsp
= 0;
489 /* Create a task that will be called if we wait more than timeout ms */
490 task
= p_sys
->scheduler
->scheduleDelayedTask( i_timeout
*1000,
494 p_sys
->event_rtsp
= 0;
495 p_sys
->b_error
= true;
496 p_sys
->i_live555_ret
= 0;
497 p_sys
->scheduler
->doEventLoop( &p_sys
->event_rtsp
);
498 //here, if b_error is true and i_live555_ret = 0 we didn't receive a response
501 /* remove the task */
502 p_sys
->scheduler
->unscheduleDelayedTask( task
);
504 return !p_sys
->b_error
;
507 static void continueAfterDESCRIBE( RTSPClient
* client
, int result_code
,
508 char* result_string
)
510 RTSPClientVlc
*client_vlc
= static_cast<RTSPClientVlc
*> ( client
);
511 demux_sys_t
*p_sys
= client_vlc
->p_sys
;
512 p_sys
->i_live555_ret
= result_code
;
513 if ( result_code
== 0 )
515 char* sdpDescription
= result_string
;
516 free( p_sys
->p_sdp
);
520 p_sys
->p_sdp
= strdup( sdpDescription
);
521 p_sys
->b_error
= false;
525 p_sys
->b_error
= true;
526 delete[] result_string
;
527 p_sys
->event_rtsp
= 1;
530 static void continueAfterOPTIONS( RTSPClient
* client
, int result_code
,
531 char* result_string
)
533 RTSPClientVlc
*client_vlc
= static_cast<RTSPClientVlc
*> (client
);
534 demux_sys_t
*p_sys
= client_vlc
->p_sys
;
535 p_sys
->i_live555_ret
= result_code
;
536 if ( result_code
!= 0 )
538 p_sys
->b_error
= true;
539 p_sys
->event_rtsp
= 1;
543 p_sys
->b_get_param
= result_string
!= NULL
&& strstr( result_string
, "GET_PARAMETER" ) != NULL
;
544 client
->sendDescribeCommand( continueAfterDESCRIBE
);
546 delete[] result_string
;
549 /*****************************************************************************
550 * Connect: connects to the RTSP server to setup the session DESCRIBE
551 *****************************************************************************/
552 static int Connect( demux_t
*p_demux
)
554 demux_sys_t
*p_sys
= p_demux
->p_sys
;
555 Authenticator authenticator
;
556 char *psz_user
= NULL
;
557 char *psz_pwd
= NULL
;
558 char *psz_url
= NULL
;
560 int i_ret
= VLC_SUCCESS
;
561 const int i_timeout
= var_InheritInteger( p_demux
, "ipv4-timeout" );
563 /* Get the user name and password */
564 if( p_sys
->url
.psz_username
|| p_sys
->url
.psz_password
)
566 /* Create the URL by stripping away the username/password part */
567 if( p_sys
->url
.i_port
== 0 )
568 p_sys
->url
.i_port
= 554;
569 if( asprintf( &psz_url
, "rtsp://%s:%d%s",
570 strempty( p_sys
->url
.psz_host
),
572 strempty( p_sys
->url
.psz_path
) ) == -1 )
575 psz_user
= strdup( strempty( p_sys
->url
.psz_username
) );
576 psz_pwd
= strdup( strempty( p_sys
->url
.psz_password
) );
580 if( asprintf( &psz_url
, "rtsp://%s", p_sys
->psz_path
) == -1 )
583 psz_user
= var_InheritString( p_demux
, "rtsp-user" );
584 psz_pwd
= var_InheritString( p_demux
, "rtsp-pwd" );
588 if( !vlc_object_alive (p_demux
) )
590 i_ret
= VLC_EGENERIC
;
594 if( var_CreateGetBool( p_demux
, "rtsp-http" ) )
595 i_http_port
= var_InheritInteger( p_demux
, "rtsp-http-port" );
597 p_sys
->rtsp
= new RTSPClientVlc( *p_sys
->env
, psz_url
,
598 var_InheritInteger( p_demux
, "verbose" ) > 1 ? 1 : 0,
599 "LibVLC/"VERSION
, i_http_port
, p_sys
);
602 msg_Err( p_demux
, "RTSPClient::createNew failed (%s)",
603 p_sys
->env
->getResultMsg() );
604 i_ret
= VLC_EGENERIC
;
608 /* Kasenna enables KeepAlive by analysing the User-Agent string.
609 * Appending _KA to the string should be enough to enable this feature,
610 * however, there is a bug where the _KA doesn't get parsed from the
611 * default User-Agent as created by VLC/Live555 code. This is probably due
612 * to spaces in the string or the string being too long. Here we override
613 * the default string with a more compact version.
615 if( var_InheritBool( p_demux
, "rtsp-kasenna" ))
617 p_sys
->rtsp
->setUserAgentString( "VLC_MEDIA_PLAYER_KA" );
621 authenticator
.setUsernameAndPassword( psz_user
, psz_pwd
);
623 p_sys
->rtsp
->sendOptionsCommand( &continueAfterOPTIONS
, &authenticator
);
625 if( !wait_Live555_response( p_demux
, i_timeout
) )
627 int i_code
= p_sys
->i_live555_ret
;
630 msg_Dbg( p_demux
, "authentication failed" );
634 dialog_Login( p_demux
, &psz_user
, &psz_pwd
,
635 _("RTSP authentication"), "%s",
636 _("Please enter a valid login name and a password.") );
637 if( psz_user
!= NULL
&& psz_pwd
!= NULL
)
639 msg_Dbg( p_demux
, "retrying with user=%s", psz_user
);
643 else if( i_code
> 0 && i_code
!= 404 && !var_GetBool( p_demux
, "rtsp-http" ) )
645 /* Perhaps a firewall is being annoying. Try HTTP tunneling mode */
646 msg_Dbg( p_demux
, "we will now try HTTP tunneling mode" );
647 var_SetBool( p_demux
, "rtsp-http", true );
648 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
655 msg_Dbg( p_demux
, "connection timeout" );
657 msg_Dbg( p_demux
, "connection error %d", i_code
);
658 if( p_sys
->rtsp
) RTSPClient::close( p_sys
->rtsp
);
661 i_ret
= VLC_EGENERIC
;
665 /* malloc-ated copy */
673 /*****************************************************************************
674 * SessionsSetup: prepares the subsessions and does the SETUP
675 *****************************************************************************/
676 static int SessionsSetup( demux_t
*p_demux
)
678 demux_sys_t
*p_sys
= p_demux
->p_sys
;
679 MediaSubsessionIterator
*iter
= NULL
;
680 MediaSubsession
*sub
= NULL
;
684 int i_return
= VLC_SUCCESS
;
685 unsigned int i_buffer
= 0;
686 unsigned const thresh
= 200000; /* RTP reorder threshold .2 second (default .1) */
688 b_rtsp_tcp
= var_CreateGetBool( p_demux
, "rtsp-tcp" ) ||
689 var_GetBool( p_demux
, "rtsp-http" );
690 i_client_port
= var_InheritInteger( p_demux
, "rtp-client-port" );
692 /* Create the session from the SDP */
693 if( !( p_sys
->ms
= MediaSession::createNew( *p_sys
->env
, p_sys
->p_sdp
) ) )
695 msg_Err( p_demux
, "Could not create the RTSP Session: %s",
696 p_sys
->env
->getResultMsg() );
700 /* Initialise each media subsession */
701 iter
= new MediaSubsessionIterator( *p_sys
->ms
);
702 while( ( sub
= iter
->next() ) != NULL
)
707 if( !vlc_object_alive (p_demux
) )
713 /* Value taken from mplayer */
714 if( !strcmp( sub
->mediumName(), "audio" ) )
716 else if( !strcmp( sub
->mediumName(), "video" ) )
718 else if( !strcmp( sub
->mediumName(), "text" ) )
722 if( i_client_port
!= -1 )
724 sub
->setClientPortNum( i_client_port
);
728 if( strcasestr( sub
->codecName(), "REAL" ) )
730 msg_Info( p_demux
, "real codec detected, using real-RTSP instead" );
731 p_sys
->b_real
= true; /* This is a problem, we'll handle it later */
735 if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
736 bInit
= sub
->initiate( 0 );
738 bInit
= sub
->initiate();
742 msg_Warn( p_demux
, "RTP subsession '%s/%s' failed (%s)",
743 sub
->mediumName(), sub
->codecName(),
744 p_sys
->env
->getResultMsg() );
748 if( sub
->rtpSource() != NULL
)
750 int fd
= sub
->rtpSource()->RTPgs()->socketNum();
752 /* Increase the buffer size */
754 increaseReceiveBufferTo( *p_sys
->env
, fd
, i_buffer
);
756 /* Increase the RTP reorder timebuffer just a bit */
757 sub
->rtpSource()->setPacketReorderingThresholdTime(thresh
);
759 msg_Dbg( p_demux
, "RTP subsession '%s/%s'", sub
->mediumName(),
762 /* Issue the SETUP */
765 p_sys
->rtsp
->sendSetupCommand( *sub
, default_live555_callback
, False
,
766 toBool( b_rtsp_tcp
),
767 toBool( p_sys
->b_force_mcast
&& !b_rtsp_tcp
) );
768 if( !wait_Live555_response( p_demux
) )
770 /* if we get an unsupported transport error, toggle TCP
771 * use and try again */
772 if( p_sys
->i_live555_ret
== 461 )
773 p_sys
->rtsp
->sendSetupCommand( *sub
, default_live555_callback
, False
,
774 !toBool( b_rtsp_tcp
), False
);
775 if( p_sys
->i_live555_ret
!= 461 || !wait_Live555_response( p_demux
) )
777 msg_Err( p_demux
, "SETUP of'%s/%s' failed %s",
778 sub
->mediumName(), sub
->codecName(),
779 p_sys
->env
->getResultMsg() );
784 var_SetBool( p_demux
, "rtsp-tcp", true );
790 /* Check if we will receive data from this subsession for
792 if( sub
->readSource() == NULL
) continue;
793 if( !p_sys
->b_multicast
)
795 /* We need different rollover behaviour for multicast */
796 p_sys
->b_multicast
= IsMulticastAddress( sub
->connectionEndpointAddress() );
799 tk
= (live_track_t
*)malloc( sizeof( live_track_t
) );
805 tk
->p_demux
= p_demux
;
808 tk
->b_quicktime
= false;
810 tk
->p_asf_block
= NULL
;
812 tk
->b_discard_trunc
= false;
813 tk
->p_out_muxed
= NULL
;
815 tk
->b_rtcp_sync
= false;
816 tk
->i_pts
= VLC_TS_INVALID
;
818 tk
->b_selected
= true;
819 tk
->i_buffer
= 65536;
820 tk
->p_buffer
= (uint8_t *)malloc( 65536 );
828 /* Value taken from mplayer */
829 if( !strcmp( sub
->mediumName(), "audio" ) )
831 es_format_Init( &tk
->fmt
, AUDIO_ES
, VLC_FOURCC('u','n','d','f') );
832 tk
->fmt
.audio
.i_channels
= sub
->numChannels();
833 tk
->fmt
.audio
.i_rate
= sub
->rtpTimestampFrequency();
835 if( !strcmp( sub
->codecName(), "MPA" ) ||
836 !strcmp( sub
->codecName(), "MPA-ROBUST" ) ||
837 !strcmp( sub
->codecName(), "X-MP3-DRAFT-00" ) )
839 tk
->fmt
.i_codec
= VLC_CODEC_MPGA
;
840 tk
->fmt
.audio
.i_rate
= 0;
842 else if( !strcmp( sub
->codecName(), "AC3" ) )
844 tk
->fmt
.i_codec
= VLC_CODEC_A52
;
845 tk
->fmt
.audio
.i_rate
= 0;
847 else if( !strcmp( sub
->codecName(), "L16" ) )
849 tk
->fmt
.i_codec
= VLC_CODEC_S16B
;
850 tk
->fmt
.audio
.i_bitspersample
= 16;
852 else if( !strcmp( sub
->codecName(), "L20" ) )
854 tk
->fmt
.i_codec
= VLC_CODEC_S20B
;
855 tk
->fmt
.audio
.i_bitspersample
= 20;
857 else if( !strcmp( sub
->codecName(), "L24" ) )
859 tk
->fmt
.i_codec
= VLC_CODEC_S24B
;
860 tk
->fmt
.audio
.i_bitspersample
= 24;
862 else if( !strcmp( sub
->codecName(), "L8" ) )
864 tk
->fmt
.i_codec
= VLC_CODEC_U8
;
865 tk
->fmt
.audio
.i_bitspersample
= 8;
867 else if( !strcmp( sub
->codecName(), "DAT12" ) )
869 tk
->fmt
.i_codec
= VLC_CODEC_DAT12
;
870 tk
->fmt
.audio
.i_bitspersample
= 12;
872 else if( !strcmp( sub
->codecName(), "PCMU" ) )
874 tk
->fmt
.i_codec
= VLC_CODEC_MULAW
;
875 tk
->fmt
.audio
.i_bitspersample
= 8;
877 else if( !strcmp( sub
->codecName(), "PCMA" ) )
879 tk
->fmt
.i_codec
= VLC_CODEC_ALAW
;
880 tk
->fmt
.audio
.i_bitspersample
= 8;
882 else if( !strncmp( sub
->codecName(), "G726", 4 ) )
884 tk
->fmt
.i_codec
= VLC_CODEC_ADPCM_G726
;
885 tk
->fmt
.audio
.i_rate
= 8000;
886 tk
->fmt
.audio
.i_channels
= 1;
887 if( !strcmp( sub
->codecName()+5, "40" ) )
888 tk
->fmt
.i_bitrate
= 40000;
889 else if( !strcmp( sub
->codecName()+5, "32" ) )
890 tk
->fmt
.i_bitrate
= 32000;
891 else if( !strcmp( sub
->codecName()+5, "24" ) )
892 tk
->fmt
.i_bitrate
= 24000;
893 else if( !strcmp( sub
->codecName()+5, "16" ) )
894 tk
->fmt
.i_bitrate
= 16000;
896 else if( !strcmp( sub
->codecName(), "AMR" ) )
898 tk
->fmt
.i_codec
= VLC_CODEC_AMR_NB
;
900 else if( !strcmp( sub
->codecName(), "AMR-WB" ) )
902 tk
->fmt
.i_codec
= VLC_CODEC_AMR_WB
;
904 else if( !strcmp( sub
->codecName(), "MP4A-LATM" ) )
906 unsigned int i_extra
;
909 tk
->fmt
.i_codec
= VLC_CODEC_MP4A
;
911 if( ( p_extra
= parseStreamMuxConfigStr( sub
->fmtp_config(),
914 tk
->fmt
.i_extra
= i_extra
;
915 tk
->fmt
.p_extra
= xmalloc( i_extra
);
916 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
919 /* Because the "faad" decoder does not handle the LATM
920 * data length field at the start of each returned LATM
921 * frame, tell the RTP source to omit it. */
922 ((MPEG4LATMAudioRTPSource
*)sub
->rtpSource())->omitLATMDataLengthField();
924 else if( !strcmp( sub
->codecName(), "MPEG4-GENERIC" ) )
926 unsigned int i_extra
;
929 tk
->fmt
.i_codec
= VLC_CODEC_MP4A
;
931 if( ( p_extra
= parseGeneralConfigStr( sub
->fmtp_config(),
934 tk
->fmt
.i_extra
= i_extra
;
935 tk
->fmt
.p_extra
= xmalloc( i_extra
);
936 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
940 else if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
943 if( p_sys
->p_out_asf
== NULL
)
944 p_sys
->p_out_asf
= stream_DemuxNew( p_demux
, "asf",
947 else if( !strcmp( sub
->codecName(), "X-QT" ) ||
948 !strcmp( sub
->codecName(), "X-QUICKTIME" ) )
950 tk
->b_quicktime
= true;
952 else if( !strcmp( sub
->codecName(), "SPEEX" ) )
954 tk
->fmt
.i_codec
= VLC_FOURCC( 's', 'p', 'x', 'r' );
955 if ( sub
->rtpTimestampFrequency() )
956 tk
->fmt
.audio
.i_rate
= sub
->rtpTimestampFrequency();
959 msg_Warn( p_demux
,"Using 8kHz as default sample rate." );
960 tk
->fmt
.audio
.i_rate
= 8000;
964 else if( !strcmp( sub
->mediumName(), "video" ) )
966 es_format_Init( &tk
->fmt
, VIDEO_ES
, VLC_FOURCC('u','n','d','f') );
967 if( !strcmp( sub
->codecName(), "MPV" ) )
969 tk
->fmt
.i_codec
= VLC_CODEC_MPGV
;
970 tk
->fmt
.b_packetized
= false;
972 else if( !strcmp( sub
->codecName(), "H263" ) ||
973 !strcmp( sub
->codecName(), "H263-1998" ) ||
974 !strcmp( sub
->codecName(), "H263-2000" ) )
976 tk
->fmt
.i_codec
= VLC_CODEC_H263
;
978 else if( !strcmp( sub
->codecName(), "H261" ) )
980 tk
->fmt
.i_codec
= VLC_CODEC_H261
;
982 else if( !strcmp( sub
->codecName(), "H264" ) )
984 unsigned int i_extra
= 0;
985 uint8_t *p_extra
= NULL
;
987 tk
->fmt
.i_codec
= VLC_CODEC_H264
;
988 tk
->fmt
.b_packetized
= false;
990 if((p_extra
=parseH264ConfigStr( sub
->fmtp_spropparametersets(),
993 tk
->fmt
.i_extra
= i_extra
;
994 tk
->fmt
.p_extra
= xmalloc( i_extra
);
995 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
1000 else if( !strcmp( sub
->codecName(), "JPEG" ) )
1002 tk
->fmt
.i_codec
= VLC_CODEC_MJPG
;
1004 else if( !strcmp( sub
->codecName(), "MP4V-ES" ) )
1006 unsigned int i_extra
;
1009 tk
->fmt
.i_codec
= VLC_CODEC_MP4V
;
1011 if( ( p_extra
= parseGeneralConfigStr( sub
->fmtp_config(),
1014 tk
->fmt
.i_extra
= i_extra
;
1015 tk
->fmt
.p_extra
= xmalloc( i_extra
);
1016 memcpy( tk
->fmt
.p_extra
, p_extra
, i_extra
);
1020 else if( !strcmp( sub
->codecName(), "X-QT" ) ||
1021 !strcmp( sub
->codecName(), "X-QUICKTIME" ) ||
1022 !strcmp( sub
->codecName(), "X-QDM" ) ||
1023 !strcmp( sub
->codecName(), "X-SV3V-ES" ) ||
1024 !strcmp( sub
->codecName(), "X-SORENSONVIDEO" ) )
1026 tk
->b_quicktime
= true;
1028 else if( !strcmp( sub
->codecName(), "MP2T" ) )
1031 tk
->p_out_muxed
= stream_DemuxNew( p_demux
, "ts", p_demux
->out
);
1033 else if( !strcmp( sub
->codecName(), "MP2P" ) ||
1034 !strcmp( sub
->codecName(), "MP1S" ) )
1037 tk
->p_out_muxed
= stream_DemuxNew( p_demux
, "ps",
1040 else if( !strcmp( sub
->codecName(), "X-ASF-PF" ) )
1043 if( p_sys
->p_out_asf
== NULL
)
1044 p_sys
->p_out_asf
= stream_DemuxNew( p_demux
, "asf",
1047 else if( !strcmp( sub
->codecName(), "DV" ) )
1050 tk
->b_discard_trunc
= true;
1051 tk
->p_out_muxed
= stream_DemuxNew( p_demux
, "rawdv",
1055 else if( !strcmp( sub
->mediumName(), "text" ) )
1057 es_format_Init( &tk
->fmt
, SPU_ES
, VLC_FOURCC('u','n','d','f') );
1059 if( !strcmp( sub
->codecName(), "T140" ) )
1061 tk
->fmt
.i_codec
= VLC_CODEC_ITU_T140
;
1065 if( !tk
->b_quicktime
&& !tk
->b_muxed
&& !tk
->b_asf
)
1067 tk
->p_es
= es_out_Add( p_demux
->out
, &tk
->fmt
);
1070 if( sub
->rtcpInstance() != NULL
)
1072 sub
->rtcpInstance()->setByeHandler( StreamClose
, tk
);
1075 if( tk
->p_es
|| tk
->b_quicktime
|| ( tk
->b_muxed
&& tk
->p_out_muxed
) ||
1076 ( tk
->b_asf
&& p_sys
->p_out_asf
) )
1078 TAB_APPEND_CAST( (live_track_t
**), p_sys
->i_track
, p_sys
->track
, tk
);
1083 msg_Err( p_demux
, "unusable RTSP track. this should not happen" );
1084 es_format_Clean( &tk
->fmt
);
1090 if( p_sys
->i_track
<= 0 ) i_return
= VLC_EGENERIC
;
1092 /* Retrieve the starttime if possible */
1093 p_sys
->i_npt_start
= p_sys
->ms
->playStartTime();
1095 /* Retrieve the duration if possible */
1096 p_sys
->i_npt_length
= p_sys
->ms
->playEndTime();
1099 msg_Dbg( p_demux
, "setup start: %f stop:%f", p_sys
->i_npt_start
, p_sys
->i_npt_length
);
1102 p_sys
->b_no_data
= true;
1103 p_sys
->i_no_data_ti
= 0;
1108 /*****************************************************************************
1109 * Play: starts the actual playback of the stream
1110 *****************************************************************************/
1111 static int Play( demux_t
*p_demux
)
1113 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1118 p_sys
->rtsp
->sendPlayCommand( *p_sys
->ms
, default_live555_callback
, p_sys
->i_npt_start
, -1, 1 );
1120 if( !wait_Live555_response(p_demux
) )
1122 msg_Err( p_demux
, "RTSP PLAY failed %s", p_sys
->env
->getResultMsg() );
1123 return VLC_EGENERIC
;
1126 /* Retrieve the timeout value and set up a timeout prevention thread */
1127 p_sys
->i_timeout
= p_sys
->rtsp
->sessionTimeoutParameter();
1128 if( p_sys
->i_timeout
<= 0 )
1129 p_sys
->i_timeout
= 60; /* default value from RFC2326 */
1131 /* start timeout-thread only if GET_PARAMETER is supported by the server */
1132 if( !p_sys
->p_timeout
&& p_sys
->b_get_param
)
1134 msg_Dbg( p_demux
, "We have a timeout of %d seconds", p_sys
->i_timeout
);
1135 p_sys
->p_timeout
= (timeout_thread_t
*)malloc( sizeof(timeout_thread_t
) );
1136 if( p_sys
->p_timeout
)
1138 memset( p_sys
->p_timeout
, 0, sizeof(timeout_thread_t
) );
1139 p_sys
->p_timeout
->p_sys
= p_demux
->p_sys
; /* lol, object recursion :D */
1140 if( vlc_clone( &p_sys
->p_timeout
->handle
, TimeoutPrevention
,
1141 p_sys
->p_timeout
, VLC_THREAD_PRIORITY_LOW
) )
1143 msg_Err( p_demux
, "cannot spawn liveMedia timeout thread" );
1144 free( p_sys
->p_timeout
);
1145 p_sys
->p_timeout
= NULL
;
1148 msg_Dbg( p_demux
, "spawned timeout thread" );
1151 msg_Err( p_demux
, "cannot spawn liveMedia timeout thread" );
1156 /* Retrieve the starttime if possible */
1157 p_sys
->i_npt_start
= p_sys
->ms
->playStartTime();
1158 if( p_sys
->ms
->playEndTime() > 0 )
1159 p_sys
->i_npt_length
= p_sys
->ms
->playEndTime();
1161 msg_Dbg( p_demux
, "play start: %f stop:%f", p_sys
->i_npt_start
, p_sys
->i_npt_length
);
1166 /*****************************************************************************
1168 *****************************************************************************/
1169 static int Demux( demux_t
*p_demux
)
1171 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1174 bool b_send_pcr
= true;
1178 /* Check if we need to send the server a Keep-A-Live signal */
1179 if( p_sys
->b_timeout_call
&& p_sys
->rtsp
&& p_sys
->ms
)
1181 char *psz_bye
= NULL
;
1182 p_sys
->rtsp
->sendGetParameterCommand( *p_sys
->ms
, NULL
, psz_bye
);
1183 p_sys
->b_timeout_call
= false;
1186 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1188 live_track_t
*tk
= p_sys
->track
[i
];
1193 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
, tk
->p_es
, &b
);
1194 if( !b
&& tk
->b_selected
)
1196 tk
->b_selected
= false;
1197 p_sys
->rtsp
->sendTeardownCommand( *tk
->sub
, NULL
);
1199 else if( b
&& !tk
->b_selected
)
1201 bool b_rtsp_tcp
= var_GetBool( p_demux
, "rtsp-tcp" ) ||
1202 var_GetBool( p_demux
, "rtsp-http" );
1203 p_sys
->rtsp
->sendSetupCommand( *tk
->sub
, default_live555_callback
, False
,
1204 toBool( b_rtsp_tcp
),
1205 toBool( p_sys
->b_force_mcast
&& !b_rtsp_tcp
) );
1206 if( !wait_Live555_response( p_demux
) )
1208 msg_Err( p_demux
, "SETUP of'%s/%s' failed %s",
1209 tk
->sub
->mediumName(), tk
->sub
->codecName(),
1210 p_sys
->env
->getResultMsg() );
1214 p_sys
->rtsp
->sendPlayCommand( *tk
->sub
, default_live555_callback
, -1, -1, p_sys
->ms
->scale() );
1215 if( !wait_Live555_response(p_demux
) )
1217 msg_Err( p_demux
, "RTSP PLAY failed %s", p_sys
->env
->getResultMsg() );
1218 p_sys
->rtsp
->sendTeardownCommand( *tk
->sub
, NULL
);
1221 tk
->b_selected
= true;
1223 if( !tk
->b_selected
)
1224 es_out_Control( p_demux
->out
, ES_OUT_SET_ES_STATE
, tk
->p_es
, false );
1228 if( tk
->b_asf
|| tk
->b_muxed
)
1235 else if( tk
->i_pts
!= 0 && i_pcr
> tk
->i_pts
)
1241 if( p_sys
->i_pcr
> 0 )
1244 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, 1 + p_sys
->i_pcr
);
1247 /* First warn we want to read data */
1248 p_sys
->event_data
= 0;
1249 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1251 live_track_t
*tk
= p_sys
->track
[i
];
1253 if( tk
->waiting
== 0 )
1256 tk
->sub
->readSource()->getNextFrame( tk
->p_buffer
, tk
->i_buffer
,
1257 StreamRead
, tk
, StreamClose
, tk
);
1260 /* Create a task that will be called if we wait more than 300ms */
1261 task
= p_sys
->scheduler
->scheduleDelayedTask( 300000, TaskInterruptData
, p_demux
);
1264 p_sys
->scheduler
->doEventLoop( &p_sys
->event_data
);
1266 /* remove the task */
1267 p_sys
->scheduler
->unscheduleDelayedTask( task
);
1269 /* Check for gap in pts value */
1270 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1272 live_track_t
*tk
= p_sys
->track
[i
];
1274 if( !tk
->b_muxed
&& !tk
->b_rtcp_sync
&&
1275 tk
->sub
->rtpSource() && tk
->sub
->rtpSource()->hasBeenSynchronizedUsingRTCP() )
1277 msg_Dbg( p_demux
, "tk->rtpSource->hasBeenSynchronizedUsingRTCP()" );
1279 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
1280 tk
->b_rtcp_sync
= true;
1282 tk
->i_pts
= VLC_TS_INVALID
;
1290 if( p_sys
->b_multicast
&& p_sys
->b_no_data
&&
1291 ( p_sys
->i_no_data_ti
> 120 ) )
1293 /* FIXME Make this configurable
1294 msg_Err( p_demux, "no multicast data received in 36s, aborting" );
1298 else if( !p_sys
->b_multicast
&& !p_sys
->b_paused
&&
1299 p_sys
->b_no_data
&& ( p_sys
->i_no_data_ti
> 34 ) )
1301 bool b_rtsp_tcp
= var_GetBool( p_demux
, "rtsp-tcp" ) ||
1302 var_GetBool( p_demux
, "rtsp-http" );
1304 if( !b_rtsp_tcp
&& p_sys
->rtsp
&& p_sys
->ms
)
1306 msg_Warn( p_demux
, "no data received in 10s. Switching to TCP" );
1307 if( RollOverTcp( p_demux
) )
1309 msg_Err( p_demux
, "TCP rollover failed, aborting" );
1314 msg_Err( p_demux
, "no data received in 10s, aborting" );
1317 else if( !p_sys
->b_multicast
&& !p_sys
->b_paused
&&
1318 ( p_sys
->i_no_data_ti
> 34 ) )
1321 msg_Warn( p_demux
, "no data received in 10s, eof ?" );
1324 return p_sys
->b_error
? 0 : 1;
1327 /*****************************************************************************
1329 *****************************************************************************/
1330 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
1332 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1340 case DEMUX_GET_TIME
:
1341 pi64
= (int64_t*)va_arg( args
, int64_t * );
1342 if( p_sys
->i_npt
> 0 )
1344 *pi64
= (int64_t)(p_sys
->i_npt
* 1000000.);
1347 return VLC_EGENERIC
;
1349 case DEMUX_GET_LENGTH
:
1350 pi64
= (int64_t*)va_arg( args
, int64_t * );
1351 if( p_sys
->i_npt_length
> 0 )
1353 *pi64
= (int64_t)((double)p_sys
->i_npt_length
* 1000000.0);
1356 return VLC_EGENERIC
;
1358 case DEMUX_GET_POSITION
:
1359 pf
= (double*)va_arg( args
, double* );
1360 if( (p_sys
->i_npt_length
> 0) && (p_sys
->i_npt
> 0) )
1362 *pf
= ( (double)p_sys
->i_npt
/ (double)p_sys
->i_npt_length
);
1365 return VLC_EGENERIC
;
1367 case DEMUX_SET_POSITION
:
1368 case DEMUX_SET_TIME
:
1369 if( p_sys
->rtsp
&& (p_sys
->i_npt_length
> 0) )
1374 if( (i_query
== DEMUX_SET_TIME
) && (p_sys
->i_npt
> 0) )
1376 i64
= (int64_t)va_arg( args
, int64_t );
1377 time
= (float)((double)i64
/ (double)1000000.0); /* in second */
1379 else if( i_query
== DEMUX_SET_TIME
)
1380 return VLC_EGENERIC
;
1383 f
= (double)va_arg( args
, double );
1384 time
= f
* (double)p_sys
->i_npt_length
; /* in second */
1387 if( p_sys
->b_paused
)
1389 p_sys
->f_seek_request
= time
;
1393 p_sys
->rtsp
->sendPauseCommand( *p_sys
->ms
, default_live555_callback
);
1395 if( !wait_Live555_response( p_demux
) )
1397 msg_Err( p_demux
, "PAUSE before seek failed %s",
1398 p_sys
->env
->getResultMsg() );
1399 return VLC_EGENERIC
;
1402 p_sys
->rtsp
->sendPlayCommand( *p_sys
->ms
, default_live555_callback
, time
, -1, 1 );
1404 if( !wait_Live555_response( p_demux
) )
1406 msg_Err( p_demux
, "seek PLAY failed %s",
1407 p_sys
->env
->getResultMsg() );
1408 return VLC_EGENERIC
;
1412 /* Retrieve RTP-Info values */
1413 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1415 p_sys
->track
[i
]->b_rtcp_sync
= false;
1416 p_sys
->track
[i
]->i_pts
= VLC_TS_INVALID
;
1419 /* Retrieve the starttime if possible */
1420 p_sys
->i_npt
= p_sys
->i_npt_start
= p_sys
->ms
->playStartTime();
1422 /* Retrieve the duration if possible */
1423 if( p_sys
->ms
->playEndTime() > 0 )
1424 p_sys
->i_npt_length
= p_sys
->ms
->playEndTime();
1426 msg_Dbg( p_demux
, "seek start: %f stop:%f", p_sys
->i_npt_start
, p_sys
->i_npt_length
);
1429 return VLC_EGENERIC
;
1431 /* Special for access_demux */
1432 case DEMUX_CAN_PAUSE
:
1433 case DEMUX_CAN_SEEK
:
1434 pb
= (bool*)va_arg( args
, bool * );
1435 if( p_sys
->rtsp
&& p_sys
->i_npt_length
> 0 )
1436 /* Not always true, but will be handled in SET_PAUSE_STATE */
1442 case DEMUX_CAN_CONTROL_PACE
:
1443 pb
= (bool*)va_arg( args
, bool * );
1445 #if 1 /* Disable for now until we have a clock synchro algo
1446 * which works with something else than MPEG over UDP */
1453 case DEMUX_CAN_CONTROL_RATE
:
1454 pb
= (bool*)va_arg( args
, bool * );
1455 pb2
= (bool*)va_arg( args
, bool * );
1457 *pb
= (p_sys
->rtsp
!= NULL
) &&
1458 (p_sys
->i_npt_length
> 0) &&
1459 ( !var_GetBool( p_demux
, "rtsp-kasenna" ) ||
1460 !var_GetBool( p_demux
, "rtsp-wmserver" ) );
1464 case DEMUX_SET_RATE
:
1466 double f_scale
, f_old_scale
;
1468 if( !p_sys
->rtsp
|| (p_sys
->i_npt_length
<= 0) ||
1469 var_GetBool( p_demux
, "rtsp-kasenna" ) ||
1470 var_GetBool( p_demux
, "rtsp-wmserver" ) )
1471 return VLC_EGENERIC
;
1473 /* According to RFC 2326 p56 chapter 12.35 a RTSP server that
1476 * "[...] should try to approximate the viewing rate, but
1477 * may restrict the range of scale values that it supports.
1478 * The response MUST contain the actual scale value chosen
1481 * Scale = 1 indicates normal play
1482 * Scale > 1 indicates fast forward
1483 * Scale < 1 && Scale > 0 indicates slow motion
1484 * Scale < 0 value indicates rewind
1487 pi_int
= (int*)va_arg( args
, int * );
1488 f_scale
= (double)INPUT_RATE_DEFAULT
/ (*pi_int
);
1489 f_old_scale
= p_sys
->ms
->scale();
1491 /* Passing -1 for the start and end time will mean liveMedia won't
1492 * create a Range: section for the RTSP message. The server should
1493 * pick up from the current position */
1494 p_sys
->rtsp
->sendPlayCommand( *p_sys
->ms
, default_live555_callback
, -1, -1, f_scale
);
1496 if( !wait_Live555_response( p_demux
) )
1498 msg_Err( p_demux
, "PLAY with Scale %0.2f failed %s", f_scale
,
1499 p_sys
->env
->getResultMsg() );
1500 return VLC_EGENERIC
;
1503 if( p_sys
->ms
->scale() == f_old_scale
)
1505 msg_Err( p_demux
, "no scale change using old Scale %0.2f",
1506 p_sys
->ms
->scale() );
1507 return VLC_EGENERIC
;
1510 /* ReSync the stream */
1511 p_sys
->i_npt_start
= 0;
1515 *pi_int
= (int)( INPUT_RATE_DEFAULT
/ p_sys
->ms
->scale() );
1516 msg_Dbg( p_demux
, "PLAY with new Scale %0.2f (%d)", p_sys
->ms
->scale(), (*pi_int
) );
1520 case DEMUX_SET_PAUSE_STATE
:
1522 bool b_pause
= (bool)va_arg( args
, int );
1523 if( p_sys
->rtsp
== NULL
)
1524 return VLC_EGENERIC
;
1526 if( b_pause
== p_sys
->b_paused
)
1529 p_sys
->rtsp
->sendPauseCommand( *p_sys
->ms
, default_live555_callback
);
1531 p_sys
->rtsp
->sendPlayCommand( *p_sys
->ms
, default_live555_callback
, p_sys
->f_seek_request
,
1532 -1.0f
, p_sys
->ms
->scale() );
1534 if( !wait_Live555_response( p_demux
) )
1536 msg_Err( p_demux
, "PLAY or PAUSE failed %s", p_sys
->env
->getResultMsg() );
1537 return VLC_EGENERIC
;
1539 p_sys
->f_seek_request
= -1;
1540 p_sys
->b_paused
= b_pause
;
1542 /* When we Pause, we'll need the TimeoutPrevention thread to
1543 * handle sending the "Keep Alive" message to the server.
1544 * Unfortunately Live555 isn't thread safe and so can't
1545 * do this normally while the main Demux thread is handling
1546 * a live stream. We end up with the Timeout thread blocking
1547 * waiting for a response from the server. So when we PAUSE
1548 * we set a flag that the TimeoutPrevention function will check
1549 * and if it's set, it will trigger the GET_PARAMETER message */
1550 if( p_sys
->b_paused
&& p_sys
->p_timeout
!= NULL
)
1551 p_sys
->p_timeout
->b_handle_keep_alive
= true;
1552 else if( !p_sys
->b_paused
&& p_sys
->p_timeout
!= NULL
)
1553 p_sys
->p_timeout
->b_handle_keep_alive
= false;
1555 if( !p_sys
->b_paused
)
1557 for( int i
= 0; i
< p_sys
->i_track
; i
++ )
1559 live_track_t
*tk
= p_sys
->track
[i
];
1560 tk
->b_rtcp_sync
= false;
1561 tk
->i_pts
= VLC_TS_INVALID
;
1563 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
1567 /* Reset data received counter */
1568 p_sys
->i_no_data_ti
= 0;
1570 /* Retrieve the starttime if possible */
1571 p_sys
->i_npt_start
= p_sys
->ms
->playStartTime();
1573 /* Retrieve the duration if possible */
1574 if( p_sys
->ms
->playEndTime() )
1575 p_sys
->i_npt_length
= p_sys
->ms
->playEndTime();
1577 msg_Dbg( p_demux
, "pause start: %f stop:%f", p_sys
->i_npt_start
, p_sys
->i_npt_length
);
1580 case DEMUX_GET_TITLE_INFO
:
1581 case DEMUX_SET_TITLE
:
1582 case DEMUX_SET_SEEKPOINT
:
1583 return VLC_EGENERIC
;
1585 case DEMUX_GET_PTS_DELAY
:
1586 pi64
= (int64_t*)va_arg( args
, int64_t * );
1587 *pi64
= INT64_C(1000)
1588 * var_InheritInteger( p_demux
, "network-caching" );
1592 return VLC_EGENERIC
;
1596 /*****************************************************************************
1597 * RollOverTcp: reopen the rtsp into TCP mode
1598 * XXX: ugly, a lot of code are duplicated from Open()
1599 * This should REALLY be fixed
1600 *****************************************************************************/
1601 static int RollOverTcp( demux_t
*p_demux
)
1603 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1606 var_SetBool( p_demux
, "rtsp-tcp", true );
1608 /* We close the old RTSP session */
1609 p_sys
->rtsp
->sendTeardownCommand( *p_sys
->ms
, NULL
);
1610 Medium::close( p_sys
->ms
);
1611 RTSPClient::close( p_sys
->rtsp
);
1613 for( i
= 0; i
< p_sys
->i_track
; i
++ )
1615 live_track_t
*tk
= p_sys
->track
[i
];
1617 if( tk
->b_muxed
) stream_Delete( tk
->p_out_muxed
);
1618 if( tk
->p_es
) es_out_Del( p_demux
->out
, tk
->p_es
);
1619 if( tk
->p_asf_block
) block_Release( tk
->p_asf_block
);
1620 es_format_Clean( &tk
->fmt
);
1621 free( tk
->p_buffer
);
1624 TAB_CLEAN( p_sys
->i_track
, p_sys
->track
);
1625 if( p_sys
->p_out_asf
) stream_Delete( p_sys
->p_out_asf
);
1629 p_sys
->b_no_data
= true;
1630 p_sys
->i_no_data_ti
= 0;
1631 p_sys
->p_out_asf
= NULL
;
1633 /* Reopen rtsp client */
1634 if( ( i_return
= Connect( p_demux
) ) != VLC_SUCCESS
)
1636 msg_Err( p_demux
, "Failed to connect with rtsp://%s",
1641 if( p_sys
->p_sdp
== NULL
)
1643 msg_Err( p_demux
, "Failed to retrieve the RTSP Session Description" );
1647 if( ( i_return
= SessionsSetup( p_demux
) ) != VLC_SUCCESS
)
1649 msg_Err( p_demux
, "Nothing to play for rtsp://%s", p_sys
->psz_path
);
1653 if( ( i_return
= Play( p_demux
) ) != VLC_SUCCESS
)
1659 return VLC_EGENERIC
;
1663 /*****************************************************************************
1665 *****************************************************************************/
1666 static block_t
*StreamParseAsf( demux_t
*p_demux
, live_track_t
*tk
,
1668 const uint8_t *p_data
, unsigned i_size
)
1670 const unsigned i_packet_size
= p_demux
->p_sys
->asfh
.i_min_data_packet_size
;
1671 block_t
*p_list
= NULL
;
1673 while( i_size
>= 4 )
1675 unsigned i_flags
= p_data
[0];
1676 unsigned i_length_offset
= (p_data
[1] << 16) |
1679 bool b_length
= i_flags
& 0x40;
1680 bool b_relative_ts
= i_flags
& 0x20;
1681 bool b_duration
= i_flags
& 0x10;
1682 bool b_location_id
= i_flags
& 0x08;
1684 //msg_Dbg( p_demux, "ASF: marker=%d size=%d : %c=%d id=%d",
1685 // b_marker, i_size, b_length ? 'L' : 'O', i_length_offset );
1686 unsigned i_header_size
= 4;
1694 if( i_header_size
> i_size
)
1696 msg_Warn( p_demux
, "Invalid header size" );
1701 * When b_length is true, the streams I found do not seems to respect
1702 * the documentation.
1703 * From them, I have failed to find which choice between '__MIN()' or
1704 * 'i_length_offset - i_header_size' is the right one.
1708 i_payload
= __MIN( i_length_offset
, i_size
- i_header_size
);
1710 i_payload
= i_size
- i_header_size
;
1712 if( !tk
->p_asf_block
)
1714 tk
->p_asf_block
= block_New( p_demux
, i_packet_size
);
1715 if( !tk
->p_asf_block
)
1717 tk
->p_asf_block
->i_buffer
= 0;
1719 unsigned i_offset
= b_length
? 0 : i_length_offset
;
1720 if( i_offset
== tk
->p_asf_block
->i_buffer
&& i_offset
+ i_payload
<= i_packet_size
)
1722 memcpy( &tk
->p_asf_block
->p_buffer
[i_offset
], &p_data
[i_header_size
], i_payload
);
1723 tk
->p_asf_block
->i_buffer
+= i_payload
;
1726 /* We have a complete packet */
1727 tk
->p_asf_block
->i_buffer
= i_packet_size
;
1728 block_ChainAppend( &p_list
, tk
->p_asf_block
);
1729 tk
->p_asf_block
= NULL
;
1734 /* Reset on broken stream */
1735 msg_Err( p_demux
, "Broken packet detected (%d vs %zu or %d + %d vs %d)",
1736 i_offset
, tk
->p_asf_block
->i_buffer
, i_offset
, i_payload
, i_packet_size
);
1737 tk
->p_asf_block
->i_buffer
= 0;
1741 p_data
+= i_header_size
+ i_payload
;
1742 i_size
-= i_header_size
+ i_payload
;
1747 /*****************************************************************************
1749 *****************************************************************************/
1750 static void StreamRead( void *p_private
, unsigned int i_size
,
1751 unsigned int i_truncated_bytes
, struct timeval pts
,
1752 unsigned int duration
)
1754 VLC_UNUSED( duration
);
1756 live_track_t
*tk
= (live_track_t
*)p_private
;
1757 demux_t
*p_demux
= tk
->p_demux
;
1758 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1761 //msg_Dbg( p_demux, "pts: %d", pts.tv_sec );
1763 int64_t i_pts
= (int64_t)pts
.tv_sec
* INT64_C(1000000) +
1764 (int64_t)pts
.tv_usec
;
1766 /* XXX Beurk beurk beurk Avoid having negative value XXX */
1767 i_pts
&= INT64_C(0x00ffffffffffffff);
1769 /* Retrieve NPT for this pts */
1770 tk
->i_npt
= tk
->sub
->getNormalPlayTime(pts
);
1772 if( tk
->b_quicktime
&& tk
->p_es
== NULL
)
1774 QuickTimeGenericRTPSource
*qtRTPSource
=
1775 (QuickTimeGenericRTPSource
*)tk
->sub
->rtpSource();
1776 QuickTimeGenericRTPSource::QTState
&qtState
= qtRTPSource
->qtState
;
1777 uint8_t *sdAtom
= (uint8_t*)&qtState
.sdAtom
[4];
1779 if( tk
->fmt
.i_cat
== VIDEO_ES
) {
1780 if( qtState
.sdAtomSize
< 16 + 32 )
1783 p_sys
->event_data
= 0xff;
1787 tk
->fmt
.i_codec
= VLC_FOURCC(sdAtom
[0],sdAtom
[1],sdAtom
[2],sdAtom
[3]);
1788 tk
->fmt
.video
.i_width
= (sdAtom
[28] << 8) | sdAtom
[29];
1789 tk
->fmt
.video
.i_height
= (sdAtom
[30] << 8) | sdAtom
[31];
1791 if( tk
->fmt
.i_codec
== VLC_FOURCC('a', 'v', 'c', '1') )
1793 uint8_t *pos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
+ 86;
1794 uint8_t *endpos
= (uint8_t*)qtRTPSource
->qtState
.sdAtom
1795 + qtRTPSource
->qtState
.sdAtomSize
;
1796 while (pos
+8 < endpos
) {
1797 unsigned int atomLength
= pos
[0]<<24 | pos
[1]<<16 | pos
[2]<<8 | pos
[3];
1798 if( atomLength
== 0 || atomLength
> (unsigned int)(endpos
-pos
)) break;
1799 if( memcmp(pos
+4, "avcC", 4) == 0 &&
1801 atomLength
<= INT_MAX
)
1803 tk
->fmt
.i_extra
= atomLength
-8;
1804 tk
->fmt
.p_extra
= xmalloc( tk
->fmt
.i_extra
);
1805 memcpy(tk
->fmt
.p_extra
, pos
+8, atomLength
-8);
1813 tk
->fmt
.i_extra
= qtState
.sdAtomSize
- 16;
1814 tk
->fmt
.p_extra
= xmalloc( tk
->fmt
.i_extra
);
1815 memcpy( tk
->fmt
.p_extra
, &sdAtom
[12], tk
->fmt
.i_extra
);
1819 if( qtState
.sdAtomSize
< 4 )
1822 p_sys
->event_data
= 0xff;
1826 tk
->fmt
.i_codec
= VLC_FOURCC(sdAtom
[0],sdAtom
[1],sdAtom
[2],sdAtom
[3]);
1828 tk
->p_es
= es_out_Add( p_demux
->out
, &tk
->fmt
);
1832 fprintf( stderr
, "StreamRead size=%d pts=%lld\n",
1834 pts
.tv_sec
* 1000000LL + pts
.tv_usec
);
1837 /* grow buffer if it looks like buffer is too small, but don't eat
1838 * up all the memory on strange streams */
1839 if( i_truncated_bytes
> 0 )
1841 if( tk
->i_buffer
< 2000000 )
1844 msg_Dbg( p_demux
, "lost %d bytes", i_truncated_bytes
);
1845 msg_Dbg( p_demux
, "increasing buffer size to %d", tk
->i_buffer
* 2 );
1846 p_tmp
= realloc( tk
->p_buffer
, tk
->i_buffer
* 2 );
1849 msg_Warn( p_demux
, "realloc failed" );
1853 tk
->p_buffer
= (uint8_t*)p_tmp
;
1858 if( tk
->b_discard_trunc
)
1860 p_sys
->event_data
= 0xff;
1866 assert( i_size
<= tk
->i_buffer
);
1868 if( tk
->fmt
.i_codec
== VLC_CODEC_AMR_NB
||
1869 tk
->fmt
.i_codec
== VLC_CODEC_AMR_WB
)
1871 AMRAudioSource
*amrSource
= (AMRAudioSource
*)tk
->sub
->readSource();
1873 p_block
= block_New( p_demux
, i_size
+ 1 );
1874 p_block
->p_buffer
[0] = amrSource
->lastFrameHeader();
1875 memcpy( p_block
->p_buffer
+ 1, tk
->p_buffer
, i_size
);
1877 else if( tk
->fmt
.i_codec
== VLC_CODEC_H261
)
1879 H261VideoRTPSource
*h261Source
= (H261VideoRTPSource
*)tk
->sub
->rtpSource();
1880 uint32_t header
= h261Source
->lastSpecialHeader();
1881 p_block
= block_New( p_demux
, i_size
+ 4 );
1882 memcpy( p_block
->p_buffer
, &header
, 4 );
1883 memcpy( p_block
->p_buffer
+ 4, tk
->p_buffer
, i_size
);
1885 if( tk
->sub
->rtpSource()->curPacketMarkerBit() )
1886 p_block
->i_flags
|= BLOCK_FLAG_END_OF_FRAME
;
1888 else if( tk
->fmt
.i_codec
== VLC_CODEC_H264
)
1890 if( (tk
->p_buffer
[0] & 0x1f) >= 24 )
1891 msg_Warn( p_demux
, "unsupported NAL type for H264" );
1893 /* Normal NAL type */
1894 p_block
= block_New( p_demux
, i_size
+ 4 );
1895 p_block
->p_buffer
[0] = 0x00;
1896 p_block
->p_buffer
[1] = 0x00;
1897 p_block
->p_buffer
[2] = 0x00;
1898 p_block
->p_buffer
[3] = 0x01;
1899 memcpy( &p_block
->p_buffer
[4], tk
->p_buffer
, i_size
);
1901 else if( tk
->b_asf
)
1903 p_block
= StreamParseAsf( p_demux
, tk
,
1904 tk
->sub
->rtpSource()->curPacketMarkerBit(),
1905 tk
->p_buffer
, i_size
);
1909 p_block
= block_New( p_demux
, i_size
);
1910 memcpy( p_block
->p_buffer
, tk
->p_buffer
, i_size
);
1913 if( p_sys
->i_pcr
< i_pts
)
1915 p_sys
->i_pcr
= i_pts
;
1918 /* Update our global npt value */
1919 if( tk
->i_npt
> 0 && tk
->i_npt
> p_sys
->i_npt
&&
1920 ( tk
->i_npt
< p_sys
->i_npt_length
|| p_sys
->i_npt_length
<= 0 ) )
1921 p_sys
->i_npt
= tk
->i_npt
;
1925 if( !tk
->b_muxed
&& !tk
->b_asf
)
1927 if( i_pts
!= tk
->i_pts
)
1928 p_block
->i_pts
= VLC_TS_0
+ i_pts
;
1929 /*FIXME: for h264 you should check that packetization-mode=1 in sdp-file */
1930 p_block
->i_dts
= ( tk
->fmt
.i_codec
== VLC_CODEC_MPGV
) ? VLC_TS_INVALID
: (VLC_TS_0
+ i_pts
);
1934 stream_DemuxSend( tk
->p_out_muxed
, p_block
);
1935 else if( tk
->b_asf
)
1936 stream_DemuxSend( p_sys
->p_out_asf
, p_block
);
1938 es_out_Send( p_demux
->out
, tk
->p_es
, p_block
);
1941 /* warn that's ok */
1942 p_sys
->event_data
= 0xff;
1944 /* we have read data */
1946 p_demux
->p_sys
->b_no_data
= false;
1947 p_demux
->p_sys
->i_no_data_ti
= 0;
1949 if( i_pts
> 0 && !tk
->b_muxed
)
1955 /*****************************************************************************
1957 *****************************************************************************/
1958 static void StreamClose( void *p_private
)
1960 live_track_t
*tk
= (live_track_t
*)p_private
;
1961 demux_t
*p_demux
= tk
->p_demux
;
1962 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1963 tk
->b_selected
= false;
1964 p_sys
->event_rtsp
= 0xff;
1965 p_sys
->event_data
= 0xff;
1968 es_out_Control( p_demux
->out
, ES_OUT_SET_ES_STATE
, tk
->p_es
, false );
1971 for( int i
= 0; i
< p_sys
->i_track
; i
++ )
1973 if( p_sys
->track
[i
]->b_selected
)
1976 msg_Dbg( p_demux
, "RTSP track Close, %d track remaining", nb_tracks
);
1978 p_sys
->b_error
= true;
1982 /*****************************************************************************
1984 *****************************************************************************/
1985 static void TaskInterruptRTSP( void *p_private
)
1987 demux_t
*p_demux
= (demux_t
*)p_private
;
1990 p_demux
->p_sys
->event_rtsp
= 0xff;
1993 static void TaskInterruptData( void *p_private
)
1995 demux_t
*p_demux
= (demux_t
*)p_private
;
1997 p_demux
->p_sys
->i_no_data_ti
++;
2000 p_demux
->p_sys
->event_data
= 0xff;
2003 /*****************************************************************************
2005 *****************************************************************************/
2007 static void* TimeoutPrevention( void *p_data
)
2009 timeout_thread_t
*p_timeout
= (timeout_thread_t
*)p_data
;
2013 /* Voodoo (= no) thread safety here! *Ahem* */
2014 if( p_timeout
->b_handle_keep_alive
)
2016 char *psz_bye
= NULL
;
2017 int canc
= vlc_savecancel ();
2019 p_timeout
->p_sys
->rtsp
->sendGetParameterCommand( *p_timeout
->p_sys
->ms
, NULL
, psz_bye
);
2020 vlc_restorecancel (canc
);
2022 p_timeout
->p_sys
->b_timeout_call
= !p_timeout
->b_handle_keep_alive
;
2024 msleep (((int64_t)p_timeout
->p_sys
->i_timeout
- 2) * CLOCK_FREQ
);
2026 assert(0); /* dead code */
2029 /*****************************************************************************
2031 *****************************************************************************/
2032 static int ParseASF( demux_t
*p_demux
)
2034 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2036 const char *psz_marker
= "a=pgmpu:data:application/vnd.ms.wms-hdr.asfv1;base64,";
2037 char *psz_asf
= strcasestr( p_sys
->p_sdp
, psz_marker
);
2041 /* Parse the asf header */
2042 if( psz_asf
== NULL
)
2043 return VLC_EGENERIC
;
2045 psz_asf
+= strlen( psz_marker
);
2046 psz_asf
= strdup( psz_asf
); /* Duplicate it */
2047 psz_end
= strchr( psz_asf
, '\n' );
2049 while( psz_end
> psz_asf
&& ( *psz_end
== '\n' || *psz_end
== '\r' ) )
2052 if( psz_asf
>= psz_end
)
2055 return VLC_EGENERIC
;
2058 /* Always smaller */
2059 p_header
= block_New( p_demux
, psz_end
- psz_asf
);
2060 p_header
->i_buffer
= vlc_b64_decode_binary_to_buffer( p_header
->p_buffer
,
2061 p_header
->i_buffer
, psz_asf
);
2062 //msg_Dbg( p_demux, "Size=%d Hdrb64=%s", p_header->i_buffer, psz_asf );
2063 if( p_header
->i_buffer
<= 0 )
2066 return VLC_EGENERIC
;
2069 /* Parse it to get packet size */
2070 asf_HeaderParse( &p_sys
->asfh
, p_header
->p_buffer
, p_header
->i_buffer
);
2072 /* Send it to demuxer */
2073 stream_DemuxSend( p_sys
->p_out_asf
, p_header
);
2080 static unsigned char* parseH264ConfigStr( char const* configStr
,
2081 unsigned int& configSize
)
2084 size_t i_records
= 1;
2088 if( configStr
== NULL
|| *configStr
== '\0' )
2091 psz
= dup
= strdup( configStr
);
2093 /* Count the number of commas */
2094 for( psz
= dup
; *psz
!= '\0'; ++psz
)
2103 size_t configMax
= 5*strlen(dup
);
2104 unsigned char *cfg
= new unsigned char[configMax
];
2106 for( size_t i
= 0; i
< i_records
; ++i
)
2108 cfg
[configSize
++] = 0x00;
2109 cfg
[configSize
++] = 0x00;
2110 cfg
[configSize
++] = 0x00;
2111 cfg
[configSize
++] = 0x01;
2113 configSize
+= vlc_b64_decode_binary_to_buffer( cfg
+configSize
,
2114 configMax
-configSize
, psz
);
2115 psz
+= strlen(psz
)+1;