@@ -42,6 +42,7 @@ public class RTSPClient : IDisposable
4242 public event EventHandler < NewStreamEventArgs > NewAudioStream ;
4343 public event EventHandler < SimpleDataEventArgs > ReceivedVideoData ;
4444 public event EventHandler < SimpleDataEventArgs > ReceivedAudioData ;
45+ public event EventHandler < EventArgs > AuthenticationFailed ;
4546
4647 public bool ProcessRTCP { get ; set ; } = true ; // answer RTCP
4748 public event EventHandler < RawRtcpDataEventArgs > ReceivedRawVideoRTCP ;
@@ -148,9 +149,10 @@ public RTSPClient(ILoggerFactory loggerFactory)
148149 /// <param name="mediaRequest">Media request type <see cref="MediaRequest>."/></param>
149150 /// <param name="playbackSession">Playback session.</param>
150151 /// <param name="userCertificateSelectionCallback">Callback for user certificate selection.</param>
151- public void Connect ( string url , RTPTransport rtpTransport , string username = null , string password = null , MediaRequest mediaRequest = MediaRequest . VIDEO_AND_AUDIO , bool playbackSession = false , System . Net . Security . RemoteCertificateValidationCallback userCertificateSelectionCallback = null )
152+ /// <param name="autoReconnect">Automatically try to reconnect after losing the connection.</param>
153+ public void Connect ( string url , RTPTransport rtpTransport , string username = null , string password = null , MediaRequest mediaRequest = MediaRequest . VIDEO_AND_AUDIO , bool playbackSession = false , System . Net . Security . RemoteCertificateValidationCallback userCertificateSelectionCallback = null , bool autoReconnect = false )
152154 {
153- Connect ( new Uri ( url ) , rtpTransport , username , password , mediaRequest , playbackSession , userCertificateSelectionCallback ) ;
155+ Connect ( new Uri ( url ) , rtpTransport , username , password , mediaRequest , playbackSession , userCertificateSelectionCallback , autoReconnect ) ;
154156 }
155157
156158 /// <summary>
@@ -163,7 +165,8 @@ public void Connect(string url, RTPTransport rtpTransport, string username = nul
163165 /// <param name="mediaRequest">Media request type <see cref="MediaRequest>."/></param>
164166 /// <param name="playbackSession">Playback session.</param>
165167 /// <param name="userCertificateSelectionCallback">Callback for user certificate selection.</param>
166- public void Connect ( Uri uri , RTPTransport rtpTransport , string username , string password , MediaRequest mediaRequest , bool playbackSession , System . Net . Security . RemoteCertificateValidationCallback userCertificateSelectionCallback )
168+ /// <param name="autoReconnect">Automatically try to reconnect after losing the connection.</param>
169+ public void Connect ( Uri uri , RTPTransport rtpTransport , string username , string password , MediaRequest mediaRequest , bool playbackSession , System . Net . Security . RemoteCertificateValidationCallback userCertificateSelectionCallback , bool autoReconnect = false )
167170 {
168171 _logger . LogDebug ( "Connecting to {url} " , uri ) ;
169172
@@ -234,7 +237,7 @@ public void Connect(Uri uri, RTPTransport rtpTransport, string username, string
234237 // Connect a RTSP Listener to the RTSP Socket (or other Stream) to send RTSP messages and listen for RTSP replies
235238 _rtspClient = new RtspListener ( _rtspSocket , _loggerFactory . CreateLogger < RtspListener > ( ) )
236239 {
237- AutoReconnect = false
240+ AutoReconnect = autoReconnect
238241 } ;
239242
240243 _rtspClient . MessageReceived += RtspMessageReceived ;
@@ -398,6 +401,11 @@ public void Play(DateTime seekTimeFrom, DateTime seekTimeTo, double speed = 1.0)
398401 /// Stop playing.
399402 /// </summary>
400403 public void Stop ( )
404+ {
405+ StopClient ( ) ;
406+ }
407+
408+ private void StopClient ( )
401409 {
402410 // Send TEARDOWN
403411 RtspRequest teardown_message = new RtspRequestTeardown
@@ -409,14 +417,41 @@ public void Stop()
409417 _rtspClient ? . SendMessage ( teardown_message ) ;
410418
411419 // Stop the keepalive timer
412- _keepaliveTimer ? . Stop ( ) ;
420+ var keepaliveTimer = _keepaliveTimer ;
421+ if ( keepaliveTimer != null )
422+ {
423+ keepaliveTimer . Elapsed -= SendKeepAlive ;
424+ keepaliveTimer . Dispose ( ) ;
425+ _keepaliveTimer = null ;
426+ }
413427
414428 // clear up any UDP sockets
415- _videoRtpTransport ? . Stop ( ) ;
416- _audioRtpTransport ? . Stop ( ) ;
429+ var videoRtpTransport = _videoRtpTransport ;
430+ if ( videoRtpTransport != null )
431+ {
432+ videoRtpTransport . Stop ( ) ;
433+ videoRtpTransport . DataReceived -= VideoRtpDataReceived ;
434+ videoRtpTransport . ControlReceived -= VideoRtcpControlDataReceived ;
435+ _videoRtpTransport = null ;
436+ }
437+
438+ var audioRtpTransport = _audioRtpTransport ;
439+ if ( audioRtpTransport != null )
440+ {
441+ audioRtpTransport . Stop ( ) ;
442+ audioRtpTransport . DataReceived -= AudioRtpDataReceived ;
443+ audioRtpTransport . ControlReceived -= AudioRtcpControlDataReceived ;
444+ _audioRtpTransport = null ;
445+ }
417446
418447 // Drop the RTSP session
419- _rtspClient ? . Stop ( ) ;
448+ if ( _rtspClient != null )
449+ {
450+ var rtspClient = _rtspClient ;
451+ rtspClient . MessageReceived -= RtspMessageReceived ;
452+ rtspClient . Stop ( ) ;
453+ _rtspClient = null ;
454+ }
420455 }
421456
422457 /// <summary>
@@ -705,8 +740,9 @@ private void RtspMessageReceived(object sender, RtspChunkEventArgs e)
705740
706741 if ( message . ReturnCode == 401 && message . OriginalRequest ? . Headers . ContainsKey ( RtspHeaderNames . Authorization ) == true )
707742 {
708- _logger . LogError ( "Fail to authenticate stoping here" ) ;
709- Stop ( ) ;
743+ _logger . LogError ( "Fail to authenticate stopping here" ) ;
744+ StopClient ( ) ;
745+ AuthenticationFailed ? . Invoke ( this , EventArgs . Empty ) ;
710746 return ;
711747 }
712748
@@ -1262,7 +1298,7 @@ protected virtual void Dispose(bool disposing)
12621298 {
12631299 if ( disposing )
12641300 {
1265- Stop ( ) ;
1301+ StopClient ( ) ;
12661302
12671303 _rtspClient ? . Dispose ( ) ;
12681304 _videoRtpTransport ? . Dispose ( ) ;
0 commit comments