/************************************************************************/ #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 async void rtspClientStart() { rtspClient = new RtspClientSharp.RtspClient(new RtspClientSharp.ConnectionParameters(new Uri("rtsp://192.168.0.10:8554/H264Video")) { RtpTransport = RtpTransportProtocol.TCP, ConnectTimeout = TimeSpan.FromSeconds(3), ReceiveTimeout = TimeSpan.FromSeconds(3) }); rtspClient.FrameReceived += RtspClient_FrameReceived; rtspCancel = new CancellationTokenSource(); int delay = 200; using (rtspClient) while (true) { if (rtspCancel.Token.IsCancellationRequested) { return; } try { await rtspClient.ConnectAsync(rtspCancel.Token); iframestart = true; } catch (OperationCanceledException) { return; } catch (RtspClientException e) { Console.WriteLine(e.ToString()); await Task.Delay(delay, rtspCancel.Token); continue; } Console.WriteLine("Connected."); try { await rtspClient.ReceiveAsync(rtspCancel.Token); } catch (OperationCanceledException) { return; } catch (RtspClientException e) { Console.WriteLine(e.ToString()); await Task.Delay(delay, rtspCancel.Token); } } }
public async Task ConnectAsync_ConnectionTimeout_ThrowsException() { var transportClientMock = new Mock <IRtspTransportClient>(); transportClientMock.Setup(x => x.ConnectAsync(It.IsAny <CancellationToken>())).Returns(Task.Delay(10)); var rtspClient = new RtspClient(ConnectionParameters, () => transportClientMock.Object); await rtspClient.ConnectAsync(CancellationToken.None); }
public async Task ReceiveAsync_InterleavedModeAndOneRtcpByePacketInStream_SuccessfullyFinished() { var transportClient = new RtspTransportClientEmulator(); var rtspClient = new RtspClient(ConnectionParameters, () => transportClient); await rtspClient.ConnectAsync(CancellationToken.None); await rtspClient.ReceiveAsync(CancellationToken.None); }
private static async Task MakeSnapshotsAsync(Options options, CancellationToken token) { try { if (!Directory.Exists(options.Path)) { Directory.CreateDirectory(options.Path); } int intervalMs = options.Interval * 1000; int lastTimeSnapshotSaved = Environment.TickCount - intervalMs; var connectionParameters = new ConnectionParameters(options.Uri); using (var rtspClient = new RtspClient(connectionParameters)) { rtspClient.FrameReceived += (sender, frame) => { if (!(frame is RawJpegFrame)) { return; } int ticksNow = Environment.TickCount; if (Math.Abs(ticksNow - lastTimeSnapshotSaved) < intervalMs) { return; } lastTimeSnapshotSaved = ticksNow; string snapshotName = frame.Timestamp.ToString("O").Replace(":", "_") + ".jpg"; string path = Path.Combine(options.Path, snapshotName); ArraySegment <byte> frameSegment = frame.FrameSegment; using (var stream = File.OpenWrite(path)) stream.Write(frameSegment.Array, frameSegment.Offset, frameSegment.Count); Console.WriteLine($"[{DateTime.UtcNow}] Snapshot is saved to {snapshotName}"); }; Console.WriteLine("Connecting..."); await rtspClient.ConnectAsync(token); Console.WriteLine("Receiving..."); await rtspClient.ReceiveAsync(token); } } catch (OperationCanceledException) { } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private async Task ReceiveAsync(CancellationToken token) { try { _connectionParameters.ConnectTimeout = new System.TimeSpan(0, 0, 60); _connectionParameters.ReceiveTimeout = new System.TimeSpan(0, 0, 30); _connectionParameters.RtpTransport = RtpTransportProtocol.TCP; using (var rtspClient = new RtspClient(_connectionParameters)) { rtspClient.FrameReceived += RtspClientOnFrameReceived; while (true) { OnStatusChanged("Connecting..."); try { await rtspClient.ConnectAsync(token); } catch (InvalidCredentialException) { OnStatusChanged("Invalid login and/or password"); await Task.Delay(RetryDelay, token); continue; } catch (RtspClientException e) { OnStatusChanged(e.ToString()); await Task.Delay(RetryDelay, token); continue; } OnStatusChanged("Receiving frames..."); try { await rtspClient.ReceiveAsync(token); } catch (RtspClientException e) { OnStatusChanged(e.ToString()); await Task.Delay(RetryDelay, token); } } } } catch (OperationCanceledException) { } }
public async Task ReceiveAsync_CancellationRequested_ImmediateReturn() { var transportClient = new RtspTransportClientEmulator(); var rtspClient = new RtspClient(ConnectionParameters, () => transportClient); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.Cancel(); await rtspClient.ConnectAsync(CancellationToken.None); await rtspClient.ReceiveAsync(cancellationTokenSource.Token); }
public async Task ConnectAsync_CancellationRequested_ThrowsOperationCanceledException() { var transportClientMock = new Mock <IRtspTransportClient>(); transportClientMock.Setup(x => x.ConnectAsync(It.IsAny <CancellationToken>())).Returns(Task.Delay(10)); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.Cancel(); var rtspClient = new RtspClient(ConnectionParameters, () => transportClientMock.Object); await rtspClient.ConnectAsync(cancellationTokenSource.Token); }
private static async Task ConnectAsync(ConnectionParameters connectionParameters, CancellationToken token) { try { TimeSpan delay = TimeSpan.FromSeconds(5); using (var rtspClient = new RtspClient(connectionParameters)) { rtspClient.FrameReceived += (sender, frame) => Console.WriteLine($"New frame {frame.Timestamp}: {frame.GetType().Name}"); while (true) { Console.WriteLine("Connecting..."); try { await rtspClient.ConnectAsync(token); } catch (OperationCanceledException) { return; } catch (RtspClientException e) { Console.WriteLine(e.ToString()); await Task.Delay(delay, token); continue; } Console.WriteLine("Connected."); try { await rtspClient.ReceiveAsync(token); } catch (OperationCanceledException) { return; } catch (RtspClientException e) { Console.WriteLine(e.ToString()); await Task.Delay(delay, token); } } } } catch (OperationCanceledException) { } }
private async Task ReceiveAsync(CancellationToken token) { try { using (var rtspClient = new RtspClient(_connectionParameters)) { rtspClient.FrameReceived += RtspClientOnFrameReceived; while (true) { OnStatusChanged("Connecting..."); Console.WriteLine("Connecting..."); try { await rtspClient.ConnectAsync(token); } catch (InvalidCredentialException) { OnStatusChanged("Invalid login and/or password"); Console.WriteLine("Invalid login and/or password"); await Task.Delay(RetryDelay, token); continue; } catch (RtspClientException e) { OnStatusChanged(e.ToString()); await Task.Delay(RetryDelay, token); continue; } OnStatusChanged("Receiving frames..."); Console.WriteLine("Receiving frames..."); try { await rtspClient.ReceiveAsync(token); } catch (RtspClientException e) { OnStatusChanged("error => " + e.ToString()); await Task.Delay(RetryDelay, token); } } } } catch (OperationCanceledException) { } }
public static async void HandleRTSP() { var serverUri = new Uri("rtsp://*****:*****@192.168.6.45/rtsp_tunnel"); //var credentials = new NetworkCredential("admin", "123456"); var connectionParameters = new ConnectionParameters(serverUri); connectionParameters.RtpTransport = RtpTransportProtocol.TCP; using (var rtspClient = new RtspClient(connectionParameters)) { rtspClient.FrameReceived += (sender, frame) => { switch (frame) { case RawH264IFrame h264IFrame: DoSomething(); break; case RawH264PFrame h264PFrame: DoSomething(); break; case RawJpegFrame jpegFrame: DoSomething(); break; case RawAACFrame aacFrame: DoSomething(); break; case RawG711AFrame g711AFrame: DoSomething(); break; case RawG711UFrame g711UFrame: DoSomething(); break; case RawPCMFrame pcmFrame: DoSomething(); break; case RawG726Frame g726Frame: DoSomething(); break; } }; await rtspClient.ConnectAsync(CancellationToken.None); await rtspClient.ReceiveAsync(CancellationToken.None); } }
protected override async Task RunAsync(CancellationToken cancellationToken) { SetAudioState(false); SetVideoState(false); while (!cancellationToken.IsCancellationRequested) { try { var endpoint = new Uri($"rtsp://{ConnectionSettings.Default.Username}:{ConnectionSettings.Default.Password}@{ConnectionSettings.Default.Host}:554/av0_0"); var connectionParameters = new ConnectionParameters(endpoint) { RtpTransport = RtpTransportProtocol.TCP, CancelTimeout = TimeSpan.FromSeconds(1) }; using var client = new RtspClient(connectionParameters); _audioSource = GetAudioDecodingObservable(client); _videoSource = GetVideoDecodingObservable(client); if (_audioSubscription != null) { SetAudioState(false); } if (_videoSubscription != null) { SetVideoState(false); } await client.ConnectAsync(cancellationToken).ConfigureAwait(false); await client.ReceiveAsync(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { return; } catch { await Task.Delay(RetryDelay, cancellationToken); } } SetAudioState(true); SetVideoState(true); }
/// <summary> /// Asynchronously connects and receives media frames. /// </summary> /// <param name="cancellationToken">Cancellation token.</param> /// <returns>Asynchronous task.</returns> private async Task ReceiveAsync(CancellationToken cancellationToken) { using var client = new RtspClient(_connectionParameters); client.FrameReceived += OnFrameReceived; var remainingConnectAttempts = _numberOfAttempts; var remainingReceiveAttempts = _numberOfAttempts; while (true) { try { --remainingConnectAttempts; await client.ConnectAsync(cancellationToken); } catch (Exception) { if (remainingConnectAttempts <= 0) { throw; } await Task.Delay(_retryDelay, cancellationToken); continue; } remainingConnectAttempts = _numberOfAttempts; try { --remainingReceiveAttempts; await client.ReceiveAsync(cancellationToken); } catch (Exception) { if (remainingReceiveAttempts <= 0) { throw; } await Task.Delay(_retryDelay, cancellationToken); } } }
private static async Task RecordAsync(ConnectionParameters connectionParameters, CancellationToken token, string ipAddress, string timeSpan) { try { TimeSpan delay = TimeSpan.FromSeconds(5); using (var rtspClient = new RtspClient(connectionParameters)) { Console.WriteLine("Connecting..."); try { await rtspClient.ConnectAsync(token); Console.WriteLine("Connected"); } catch (OperationCanceledException) { } catch (RtspClientException e) { Console.WriteLine(e.ToString()); await Task.Delay(delay, token); } } } catch (OperationCanceledException) { } string args = $"-i rtsp://@{ipAddress} -t {timeSpan} -acodec copy -vcodec copy {DateTime.Now.ToFileTime().ToString()}.mp4"; var process = new Process { StartInfo = { FileName = "ffmpeg.exe", Arguments = args, UseShellExecute = false, CreateNoWindow = true, RedirectStandardInput = true, } }; process.Start(); process.WaitForExit(); }
public async Task ConnectAsync_Unauthorized_ThrowsInvalidCredentialException(bool emulateHttpException) { Exception exception; if (emulateHttpException) { exception = new HttpBadResponseCodeException(HttpStatusCode.Unauthorized); } else { exception = new RtspBadResponseCodeException(RtspStatusCode.Unauthorized); } var transportClientMock = new Mock <IRtspTransportClient>(); transportClientMock.Setup(x => x.ConnectAsync(It.IsAny <CancellationToken>())) .Throws(exception); var rtspClient = new RtspClient(ConnectionParameters, () => transportClientMock.Object); await rtspClient.ConnectAsync(CancellationToken.None); }
private static async Task SaveOnePicture(ConnectionParameters connectionParameters) { Bitmap bitmap = null; try { var cancellationTokenSource = new CancellationTokenSource(); using var rtspClient = new RtspClient(connectionParameters); rtspClient.FrameReceived += delegate(object o, RawFrame rawFrame) { if (!(rawFrame is RtspClientSharpCore.RawFrames.Video.RawVideoFrame rawVideoFrame)) { return; } var decodedFrame = FrameDecoder.TryDecode(rawVideoFrame); if (decodedFrame == null) { return; } bitmap = FrameTransformer.TransformToBitmap(decodedFrame); cancellationTokenSource.Cancel(); }; Console.WriteLine("Connecting..."); await rtspClient.ConnectAsync(cancellationTokenSource.Token); Console.WriteLine("Connected."); await rtspClient.ReceiveAsync(cancellationTokenSource.Token); } catch (OperationCanceledException) { } Console.WriteLine("Saving..."); bitmap?.Save(Path.Combine(pathToSaveImage, "image.jpg"), ImageFormat.Jpeg); }
public async void GetVideoByUrlAsync(string url) { var cancellationTokenSource = new CancellationTokenSource(); var serverUri = new Uri("rtsp://192.168.4.98:88/live/video"); //var serverUri = new Uri(url); var credentials = new NetworkCredential("admin", "admin"); var connectionParameters = new ConnectionParameters(serverUri, credentials); connectionParameters.RtpTransport = RtpTransportProtocol.TCP; using (var rtspClient = new RtspClient(connectionParameters)) { rtspClient.FrameReceived += (sender, frame) => { //process (e.g. decode/save to file) encoded frame here or //make deep copy to use it later because frame buffer (see FrameSegment property) will be reused by client switch (frame) { case RawH264IFrame h264IFrame: case RawH264PFrame h264PFrame: case RawJpegFrame jpegFrame: case RawAACFrame aacFrame: case RawG711AFrame g711AFrame: case RawG711UFrame g711UFrame: case RawPCMFrame pcmFrame: case RawG726Frame g726Frame: break; } }; await rtspClient.ConnectAsync(cancellationTokenSource.Token); await rtspClient.ReceiveAsync(cancellationTokenSource.Token); } }
private static async Task ConnectAsync(ConnectionParameters connectionParameters, CancellationToken token) { try { var delay = TimeSpan.FromSeconds(5); using (var rtspClient = new RtspClient(connectionParameters)) { rtspClient.FrameReceived += RtspClient_FrameReceived; while (true) { try { Console.WriteLine("Connecting..."); await rtspClient.ConnectAsync(token); Console.WriteLine("Connected."); await rtspClient.ReceiveAsync(token); } catch (OperationCanceledException) { return; } catch (RtspClientSharpCore.Rtsp.RtspClientException e) { Console.WriteLine(e.ToString()); await Task.Delay(delay, token); } } } } catch (OperationCanceledException) { } }
private async Task ConnectAsync() { //TimeSpan delay = TimeSpan.FromSeconds(5); var serverUri = new Uri("rtsp://192.168.0.103:554/videoMain"); var credentials = new NetworkCredential("admin", "admin"); var connectionParameters = new ConnectionParameters(serverUri, credentials); connectionParameters.RtpTransport = RtpTransportProtocol.TCP; this.rtspClient = new RtspClient(connectionParameters); this.rtspClient.FrameReceived += async(sender, frame) => { FrameCounter++; var fileName = $"{hostingEnvironment.ContentRootPath}/frames/frame-{FrameCounter}.mp4"; await File.WriteAllBytesAsync(fileName, frame.FrameSegment.Array); Func <string, string> outputFileNameBuilder = (number) => { return($"{hostingEnvironment.ContentRootPath}/images/frame-{FrameCounter}.png"); }; IMediaInfo info = await FFmpeg.GetMediaInfo(fileName); IVideoStream videoStream = info.VideoStreams.First().SetCodec(VideoCodec.h264); IConversionResult conversionResult = await FFmpeg.Conversions.New() .AddStream(videoStream) .ExtractNthFrame(1, outputFileNameBuilder) .Start(); var img = Convert.ToBase64String(frame.FrameSegment.Array); for (int i = 0; i < frameChannles.Count; i++) { var data = new StreamFrame() { Base64ImageString = img, TimeStamp = frame.Timestamp }; await frameChannles[i].Writer.WriteAsync(data, cancellationTokens[i]); LastFrame = DateTime.UtcNow; } if (FrameCounter > 50) { File.Delete($"{hostingEnvironment.ContentRootPath}/frames/frame-{FrameCounter-50}.mp4"); FrameCounter--; } ; }; //while (true) //{ Console.WriteLine("Connecting..."); await rtspClient.ConnectAsync(tokenSoruce.Token); Console.WriteLine("Connected."); var task = Task.Factory.StartNew(async() => { await rtspClient.ReceiveAsync(tokenSoruce.Token); }, TaskCreationOptions.LongRunning); }
public async Task ConnectAsync_TestTransportClientThatEmulatesRtspServer_ConnectionEstablished() { var transportClient = new RtspTransportClientEmulator(); var rtspClient = new RtspClient(ConnectionParameters, () => transportClient); await rtspClient.ConnectAsync(CancellationToken.None); }
protected void streamingThreadLoop() { try { CancellationToken cancellationToken = streamingCancelTokenSource.Token; Uri serverUri = new Uri(Url); ConnectionParameters connectionParameters; if (!string.IsNullOrWhiteSpace(Username)) { connectionParameters = new ConnectionParameters(serverUri, new NetworkCredential(Username, Password)); } else { connectionParameters = new ConnectionParameters(serverUri); } connectionParameters.RtpTransport = RtpTransportProtocol.TCP; string openH264DllPath = Globals.ApplicationDirectoryBase + "openh264-1.8.0-win" + (Environment.Is64BitProcess ? "64" : "32") + ".dll"; using (OpenH264Lib.Decoder decoder = new OpenH264Lib.Decoder(openH264DllPath)) using (RtspClient rtspClient = new RtspClient(connectionParameters)) { rtspClient.FrameReceived += (sender2, frame) => { //process (e.g. decode/save to file) encoded frame here or //make deep copy to use it later because frame buffer (see FrameSegment property) will be reused by client if (frame is RawH264IFrame) { RawH264IFrame iFrame = frame as RawH264IFrame; DecodeFrame(decoder, iFrame.SpsPpsSegment.ToArray(), frame.Timestamp); DecodeFrame(decoder, iFrame.FrameSegment.ToArray(), frame.Timestamp); } else if (frame is RawH264PFrame) { DecodeFrame(decoder, frame.FrameSegment.ToArray(), frame.Timestamp); } }; rtspClient.ConnectAsync(cancellationToken).Wait(); rtspClient.ReceiveAsync(cancellationToken).Wait(); } } catch (ThreadAbortException) { } catch (TaskCanceledException) { Stop(); } catch (Exception ex) { if (ex is AggregateException) { AggregateException aex = ex as AggregateException; if (aex.InnerExceptions.Count == 1 && aex.InnerException is TaskCanceledException) { return; } } Logger.Debug(ex); OnError(this, ex); Stop(); } }