From f9373ae30970b424181c750465b94417561e3093 Mon Sep 17 00:00:00 2001 From: Nicolas Graziano Date: Fri, 15 Nov 2024 16:46:25 +0100 Subject: [PATCH] Update style and solve some warning --- RTSP/Messages/RTSPMessage.cs | 4 ++-- RtspCameraExample/RtspServer.cs | 12 ++++++------ RtspCameraExample/TestCard.cs | 33 ++++++++++++++++++--------------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/RTSP/Messages/RTSPMessage.cs b/RTSP/Messages/RTSPMessage.cs index c57ad6e..c6128d0 100644 --- a/RTSP/Messages/RTSPMessage.cs +++ b/RTSP/Messages/RTSPMessage.cs @@ -27,7 +27,7 @@ public static RtspMessage GetRtspMessage(string aRequestLine) // We can't determine the message if (string.IsNullOrEmpty(aRequestLine)) return new RtspMessage(); - + var requestParts = aRequestLine.Split(' ', 3); RtspMessage returnValue; if (requestParts.Length == 3) @@ -253,7 +253,7 @@ public override string ToString() return stringBuilder.ToString(); } - + public override object Clone() { RtspMessage returnValue = GetRtspMessage(Command); diff --git a/RtspCameraExample/RtspServer.cs b/RtspCameraExample/RtspServer.cs index 1730a57..933b60a 100644 --- a/RtspCameraExample/RtspServer.cs +++ b/RtspCameraExample/RtspServer.cs @@ -52,8 +52,8 @@ public class RtspServer : IDisposable private readonly NetworkCredential credential; private readonly Authentication? auth; - private bool _useRTSPS = false; - private string _pfxFile = ""; + private readonly bool _useRTSPS = false; + private readonly string _pfxFile = ""; /// /// Initializes a new instance of the class. @@ -100,7 +100,7 @@ public RtspServer(int portNumber, string username, string password, string pfxFi { if (string.IsNullOrEmpty(pfxFile)) { - throw new ArgumentOutOfRangeException("PFX File must not be null or empty for RTSPS mode"); + throw new ArgumentOutOfRangeException(nameof(pfxFile), "PFX File must not be null or empty for RTSPS mode"); } _useRTSPS = true; _pfxFile = pfxFile; @@ -139,7 +139,7 @@ private void AcceptConnection() } else { - var certificate = new X509Certificate2(_pfxFile); + var certificate = X509CertificateLoader.LoadPkcs12FromFile(_pfxFile, ""); rtsp_socket = new RtspTcpTlsTransport(oneClient, certificate); // NOTE - we can add a callback where we can validate the TLS Certificates here } @@ -594,10 +594,10 @@ public void FeedInRawNAL(uint timestamp_ms, List nal_array) // ToArray makes a temp copy of the list. // This lets us delete items in the foreach // eg when there is Write Error - rtspConnectionListCopy = rtspConnectionList.ToArray(); + rtspConnectionListCopy = [.. rtspConnectionList]; } // Go through each RTSP connection and output the NAL on the Video Session - var tasks = rtspConnectionList.Select(async (connection) => + var tasks = rtspConnectionListCopy.Select(async (connection) => { // Only process Sessions in Play Mode if (!connection.play) return; diff --git a/RtspCameraExample/TestCard.cs b/RtspCameraExample/TestCard.cs index e8e2c69..3d49eb8 100644 --- a/RtspCameraExample/TestCard.cs +++ b/RtspCameraExample/TestCard.cs @@ -1,8 +1,11 @@ -using System; +namespace RtspCameraExample; + +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Globalization; +using System.Threading; // (c) Roger Hardiman 2016, 2021 @@ -21,19 +24,18 @@ public class TestCard public delegate void ReceivedAudioFrameHandler(uint timestamp, short[] data); // Local variables - private Stopwatch stopwatch; - private System.Timers.Timer frame_timer; + private readonly Stopwatch stopwatch; + private readonly System.Timers.Timer frame_timer; private readonly byte[] yuv_frame; private int x_position = 0; private int y_position = 0; - private int fps = 0; - private int width = 0; - private int height = 0; - private readonly object generate_lock = new(); + private readonly int width = 0; + private readonly int height = 0; + private readonly Lock generate_lock = new(); private long frame_count = 0; - private System.Timers.Timer audio_timer; - private int audio_duration_ms = 20; // duration of sound samples for mono PCM audio. Hinted at in origial RTP standard from 1996 that mentions 160 audio samples + private readonly System.Timers.Timer audio_timer; + private readonly int audio_duration_ms = 20; // duration of sound samples for mono PCM audio. Hinted at in origial RTP standard from 1996 that mentions 160 audio samples private long audio_count = 0; // ASCII Font @@ -61,7 +63,6 @@ public TestCard(int width, int height, int fps) { this.width = width; this.height = height; - this.fps = fps; // YUV size int y_size = width * height; @@ -98,9 +99,11 @@ public TestCard(int width, int height, int fps) frame_timer.Start(); // Start timer. The Timer will generate each Audio frame - audio_timer = new System.Timers.Timer(); - audio_timer.Interval = 1; // on first pass timer will fire straight away (cannot have zero interval) - audio_timer.AutoReset = false; // do not restart timer after the time has elapsed + audio_timer = new System.Timers.Timer + { + Interval = 1, // on first pass timer will fire straight away (cannot have zero interval) + AutoReset = false // do not restart timer after the time has elapsed + }; audio_timer.Elapsed += (object? sender, System.Timers.ElapsedEventArgs e) => { // send an audio frame @@ -243,11 +246,11 @@ private void Send_Audio_Frame() long currentSeconds = (timestamp_ms / 1000); long currentMilliSeconds = timestamp_ms % 1000; - int soundToPlay = 0; // 0 = silence + int soundToPlay; if (((currentSeconds % 60) == 0) && (currentMilliSeconds < 300)) soundToPlay = 1; else if (((currentSeconds % 10) == 0) && (currentMilliSeconds < 100)) soundToPlay = 1; else if (((currentSeconds % 10) != 0) && (currentMilliSeconds < 100)) soundToPlay = 2; - else soundToPlay = 0; + else soundToPlay = 0; // 0 = silence // Add the sound for (int i = 0; i < audio_frame.Length; i++)