/************************************************************************/ #region Private methods private async void StartVideoRtsp() { int tryCount = 0; string requestUri = string.Empty; while (tryCount < Parms.ConnectionAttempts) { try { tokenSource = new CancellationTokenSource(); token = tokenSource.Token; requestUri = $"{GetDeviceRoot(TransportProtocol.Rtsp)}/{VideoStreams[VideoStreamIndex].Path}"; /* if retrying, wait before another attempt */ if (tryCount > 0) { await Task.Delay(Parms.RetryWaitTime, token); } var serverUri = new Uri(requestUri); var credentials = new NetworkCredential(Parms.UserId, Parms.Password); var connectionParms = new RtspClientSharp.ConnectionParameters(serverUri, credentials) { RtpTransport = RtpTransportProtocol.TCP, ConnectTimeout = TimeSpan.FromMilliseconds(Parms.Timeout), }; using (var rtspClient = new RtspClient(connectionParms)) { rtspClient.FrameReceived += RtspFrameRecieved; await rtspClient.ConnectAsync(token).ConfigureAwait(false); /* Once connected, reset the try count. If there's a network problem * while receiving, we get another full set of retries */ tryCount = 0; await rtspClient.ReceiveAsync(token).ConfigureAwait(false); } } catch (OperationCanceledException) { /* when canceled, no more attempts */ tryCount = Parms.ConnectionAttempts; } catch (InvalidCredentialException ex) { /* bad credentials, no more attempts */ OnPluginException(new PluginException(requestUri, ex)); tryCount = Parms.ConnectionAttempts; } catch (Exception ex) { OnPluginException(new PluginException(requestUri, ex)); tryCount++; } } }
public RtspClient(ConnectionParameters connectionParameters) { ConnectionParameters = connectionParameters ?? throw new ArgumentNullException(nameof(connectionParameters)); }