Skip to content

Commit 73dfc95

Browse files
committed
F Added support for client reconnection
1 parent e501724 commit 73dfc95

2 files changed

Lines changed: 53 additions & 12 deletions

File tree

src/RTSPClientApp/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
client.ReceivedVideoData += (sender, e) => Console.Write("*");
1414
client.NewAudioStream += (sender, e) => Console.WriteLine(e.ToString());
1515
client.ReceivedAudioData += (sender, e) => Console.Write("+");
16+
client.AuthenticationFailed += (sender, e) =>
17+
{
18+
Console.WriteLine("Stopped");
19+
client.Connect(rtspUri, RTPTransport.TCP, userName, password, MediaRequest.VIDEO_AND_AUDIO, false, null, true);
20+
};
1621

17-
client.Connect(rtspUri, RTPTransport.TCP, userName, password);
22+
client.Connect(rtspUri, RTPTransport.TCP, userName, password, MediaRequest.VIDEO_AND_AUDIO, false, null, true);
1823

1924
Console.WriteLine("Press any key to exit");
2025
while (!Console.KeyAvailable)

src/SharpRTSPClient/RTSPClient.cs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)