Skip to content

Commit 2a93538

Browse files
authored
Use explicit string comparisons in tests and examples (#1643)
Update test and example code to use clearer string comparison patterns and raw string fixtures where appropriate. Add Polyfill references to test projects for shared line-ending helpers.
1 parent b5cdc6b commit 2a93538

54 files changed

Lines changed: 959 additions & 2624 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/AzureExamples/TextToPcm/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static async Task Main(string[] args)
7070
Console.WriteLine($"Speech synthesized to speaker for text [{text}]");
7171

7272
var buffer = ttsOutStream.GetPcmBuffer();
73-
string saveFilename = DateTime.Now.Ticks.ToString() + ".pcm16k";
73+
string saveFilename = $"{DateTime.Now.Ticks}.pcm16k";
7474

7575
using (StreamWriter sw = new StreamWriter(saveFilename))
7676
{

examples/OpenAIExamples/AliceAndBob/AudioScope/AudioScopeOpenGL.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void Initialise(OpenGL gl)
9393
// going to throw an exception.
9494
if (_prog.GetLinkStatus(gl) == false)
9595
{
96-
throw new SharpGL.Shaders.ShaderCompilationException(string.Format("Failed to link shader program with ID {0}.", _prog.ShaderProgramObject), _prog.GetInfoLog(gl));
96+
throw new SharpGL.Shaders.ShaderCompilationException($"Failed to link shader program with ID {_prog.ShaderProgramObject}.", _prog.GetInfoLog(gl));
9797
}
9898

9999
// Load clear program.
@@ -119,7 +119,7 @@ public void Initialise(OpenGL gl)
119119
// going to throw an exception.
120120
if (_clearProg.GetLinkStatus(gl) == false)
121121
{
122-
throw new SharpGL.Shaders.ShaderCompilationException(string.Format("Failed to link the clear shader program with ID {0}.", _clearProg.ShaderProgramObject), _clearProg.GetInfoLog(gl));
122+
throw new SharpGL.Shaders.ShaderCompilationException($"Failed to link the clear shader program with ID {_clearProg.ShaderProgramObject}.", _clearProg.GetInfoLog(gl));
123123
}
124124

125125
_clearRectangle = new float[] { -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f };

examples/SIPExamples/SIPProxy/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ static void Main()
129129
}
130130
catch (Exception excp)
131131
{
132-
Console.WriteLine("Exception Main. " + excp);
132+
Console.WriteLine($"Exception Main. {excp}");
133133
}
134134
}
135135

examples/Softphone/Signalling/SIPClient.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public async Task Call(string destination)
117117
else
118118
{
119119
// This call will use the pre-configured SIP account.
120-
callURI = SIPURI.ParseSIPURIRelaxed(destination + "@" + m_sipServer);
120+
callURI = SIPURI.ParseSIPURIRelaxed($"{destination}@{m_sipServer}");
121121
sipUsername = m_sipUsername;
122122
sipPassword = m_sipPassword;
123123
fromHeader = (new SIPFromHeader(m_sipFromName, new SIPURI(m_sipUsername, m_sipServer, null), null)).ToString();
@@ -151,7 +151,7 @@ public async Task Call(string destination)
151151
/// </summary>
152152
public void Cancel()
153153
{
154-
StatusMessage(this, "Cancelling SIP call to " + m_userAgent.CallDescriptor?.Uri + ".");
154+
StatusMessage(this, $"Cancelling SIP call to {m_userAgent.CallDescriptor?.Uri}.");
155155
m_userAgent.Cancel();
156156
}
157157

@@ -215,7 +215,7 @@ public void Redirect(string destination)
215215
/// Puts the remote call party on hold.
216216
/// </summary>
217217
public async Task PutOnHold()
218-
{
218+
{
219219
await MediaSession.PutOnHold();
220220
m_userAgent.PutOnHold();
221221
StatusMessage(this, "Local party put on hold");
@@ -319,23 +319,23 @@ private VoIPMediaSession CreateMediaSession()
319319
/// </summary>
320320
private void CallTrying(ISIPClientUserAgent uac, SIPResponse sipResponse)
321321
{
322-
StatusMessage(this, "Call trying: " + sipResponse.StatusCode + " " + sipResponse.ReasonPhrase + ".");
322+
StatusMessage(this, $"Call trying: {sipResponse.StatusCode} {sipResponse.ReasonPhrase}.");
323323
}
324324

325325
/// <summary>
326326
/// A ringing response has been received from the remote SIP UAS on an outgoing call.
327327
/// </summary>
328328
private void CallRinging(ISIPClientUserAgent uac, SIPResponse sipResponse)
329329
{
330-
StatusMessage(this, "Call ringing: " + sipResponse.StatusCode + " " + sipResponse.ReasonPhrase + ".");
330+
StatusMessage(this, $"Call ringing: {sipResponse.StatusCode} {sipResponse.ReasonPhrase}.");
331331
}
332332

333333
/// <summary>
334334
/// An outgoing call was rejected by the remote SIP UAS on an outgoing call.
335335
/// </summary>
336336
private void CallFailed(ISIPClientUserAgent uac, string errorMessage, SIPResponse failureResponse)
337337
{
338-
StatusMessage(this, "Call failed: " + errorMessage + ".");
338+
StatusMessage(this, $"Call failed: {errorMessage}.");
339339
CallFinished(null);
340340
}
341341

@@ -346,7 +346,7 @@ private void CallFailed(ISIPClientUserAgent uac, string errorMessage, SIPRespons
346346
/// <param name="sipResponse">The SIP answer response received from the remote party.</param>
347347
private void CallAnswered(ISIPClientUserAgent uac, SIPResponse sipResponse)
348348
{
349-
StatusMessage(this, "Call answered: " + sipResponse.StatusCode + " " + sipResponse.ReasonPhrase + ".");
349+
StatusMessage(this, $"Call answered: {sipResponse.StatusCode} {sipResponse.ReasonPhrase}.");
350350
CallAnswer?.Invoke(this);
351351
}
352352

examples/WebRTCExamples/FfmpegToWebRTC/Program.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
//
77
// Author(s):
88
// Aaron Clauson (aaron@sipsorcery.com)
9-
//
9+
//
1010
// History:
1111
// 08 Jul 2020 Aaron Clauson Created, Dublin, Ireland.
1212
//
13-
// License:
13+
// License:
1414
// BSD 3-Clause "New" or "Revised" License, see included LICENSE.md file.
1515
//-----------------------------------------------------------------------------
1616

@@ -89,26 +89,24 @@ static async Task Main(string[] args)
8989

9090
if (args?.Length > 0)
9191
{
92-
switch(args[0].ToLower())
92+
if (string.Equals(args[0], FFMPEG_VP8_CODEC, StringComparison.OrdinalIgnoreCase) ||
93+
string.Equals(args[0], FFMPEG_VP9_CODEC, StringComparison.OrdinalIgnoreCase) ||
94+
string.Equals(args[0], FFMPEG_H264_CODEC, StringComparison.OrdinalIgnoreCase) ||
95+
string.Equals(args[0], FFMPEG_H265_CODEC, StringComparison.OrdinalIgnoreCase))
96+
{
97+
videoCodec = args[0].ToLower();
98+
}
99+
else
93100
{
94-
case FFMPEG_VP8_CODEC:
95-
case FFMPEG_VP9_CODEC:
96-
case FFMPEG_H264_CODEC:
97-
case FFMPEG_H265_CODEC:
98-
videoCodec = args[0].ToLower();
99-
break;
100-
101-
default:
102-
Console.WriteLine($"Video codec option not recognised. Valid values are {FFMPEG_VP8_CODEC}, {FFMPEG_VP9_CODEC} and {FFMPEG_H264_CODEC}. Using {videoCodec}.");
103-
break;
101+
Console.WriteLine($"Video codec option not recognised. Valid values are {FFMPEG_VP8_CODEC}, {FFMPEG_VP9_CODEC} and {FFMPEG_H264_CODEC}. Using {videoCodec}.");
104102
}
105103
}
106104

107105
CancellationTokenSource exitCts = new CancellationTokenSource();
108106

109107
logger = AddConsoleLogger();
110108

111-
string ffmpegCommand = String.Format(FFMPEG_DEFAULT_COMMAND, videoCodec, FFMPEG_DEFAULT_RTP_PORT, SSRC_REMOTE_VIDEO, FFMPEG_SDP_FILE);
109+
string ffmpegCommand = $"ffmpeg -re -f lavfi -i testsrc=size=640x480:rate=10 {videoCodec} -pix_fmt yuv420p -strict experimental -g 1 -ssrc {SSRC_REMOTE_VIDEO} -f rtp rtp://127.0.0.1:{FFMPEG_DEFAULT_RTP_PORT} -sdp_file {FFMPEG_SDP_FILE}";
112110

113111
// Start web socket.
114112
Console.WriteLine("Starting web socket server...");
@@ -294,7 +292,7 @@ private static void WebSocketMessageReceived(WebSocketContext context, RTCPeerCo
294292
{
295293
logger.LogDebug("ICE Candidate: " + message);
296294

297-
if (string.IsNullOrWhiteSpace(message) || message.Trim().ToLower() == SDP.END_ICE_CANDIDATES_ATTRIBUTE)
295+
if (string.IsNullOrWhiteSpace(message) || message.AsSpan().Trim().Equals(SDP.END_ICE_CANDIDATES_ATTRIBUTE, StringComparison.OrdinalIgnoreCase))
298296
{
299297
logger.LogDebug("End of candidates message received.");
300298
}

examples/WebRTCExamples/RtspToWebRTCAudioAndVideo/DemuxerConfig.cs

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,18 @@ public string Args
4747
switch (outputStream)
4848
{
4949
case StreamsEnum.videoAndAudio:
50-
return String.Format(commandTemplateDic[StreamsEnum.videoAndAudio],
51-
rtspUrl,
52-
vcodec,
53-
videoSsrc,
54-
serverIP,
55-
videoPort,
56-
acodec,
57-
audioSsrc,
58-
audioPort,
59-
sdpPath
60-
);
50+
return $"-use_wallclock_as_timestamps 1 -i {rtspUrl} -map 0:v -c:v {vcodec} -ssrc {videoSsrc} -f rtp rtp://{serverIP}:{videoPort} -map 0:a -c:a {acodec} -ssrc {audioSsrc} -f rtp rtp://{serverIP}:{audioPort} -sdp_file {sdpPath} -y";
6151
case StreamsEnum.videoAndAudioUdp:
62-
return String.Format(commandTemplateDic[StreamsEnum.videoAndAudioUdp],
63-
rtspUrl,
64-
vcodec,
65-
videoSsrc,
66-
serverIP,
67-
videoPort,
68-
acodec,
69-
audioSsrc,
70-
audioPort,
71-
sdpPath
72-
);
52+
return $"-use_wallclock_as_timestamps 1 -rtsp_transport udp -i {rtspUrl} -map 0:v -c:v {vcodec} -ssrc {videoSsrc} -f rtp rtp://{serverIP}:{videoPort} -map 0:a -c:a {acodec} -ssrc {audioSsrc} -f rtp rtp://{serverIP}:{audioPort} -sdp_file {sdpPath} -y";
7353
case StreamsEnum.audio:
74-
return String.Format(commandTemplateDic[StreamsEnum.audio],
75-
rtspUrl,
76-
acodec,
77-
audioSsrc,
78-
serverIP,
79-
audioPort,
80-
sdpPath
81-
);
54+
return $"-re -i {rtspUrl} -vn -acodec {acodec} -ssrc {audioSsrc} -f rtp rtp://{serverIP}:{audioPort} -sdp_file {sdpPath}";
8255
case StreamsEnum.video:
83-
return String.Format(commandTemplateDic[StreamsEnum.video],
84-
rtspUrl,
85-
vcodec,
86-
videoSsrc,
87-
serverIP,
88-
videoPort,
89-
sdpPath
90-
);
56+
return $"-re -i {rtspUrl} -an -vcodec {vcodec} -ssrc {videoSsrc} -f rtp rtp://{serverIP}:{videoPort} -sdp_file {sdpPath}";
9157
default:
9258
return "";
9359
}
94-
95-
60+
61+
9662
}
9763
}
9864
}

examples/WebRTCExamples/RtspToWebRTCAudioAndVideo/WebSocketSignalingServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private async Task WebSocketMessageReceived(WebSocketContext context, RTCPeerCon
158158
{
159159
_logger.LogInformation("ICE Candidate: " + message);
160160

161-
if (string.IsNullOrWhiteSpace(message) || message.Trim().ToLower() == SDP.END_ICE_CANDIDATES_ATTRIBUTE)
161+
if (string.IsNullOrWhiteSpace(message) || message.AsSpan().Trim().Equals(SDP.END_ICE_CANDIDATES_ATTRIBUTE, StringComparison.OrdinalIgnoreCase))
162162
{
163163
_logger.LogDebug("End of candidates message received.");
164164
}

examples/WebRTCExamples/UnityVideoSink/Assets/Scripts/PlayerController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void OnMove(InputValue movementValue)
4747

4848
void SetCountText()
4949
{
50-
countText.text = "Count: " + count.ToString();
50+
countText.text = $"Count: {count.ToString()}";
5151

5252
if (count >= 10)
5353
{

examples/WebRTCExamples/UnityVideoSink/Assets/Scripts/WebRTCStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public bool IsEnabled(LogLevel logLevel)
261261

262262
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
263263
{
264-
Debug.Log("[" + eventId + "] " + formatter(state, exception));
264+
Debug.Log($"[{eventId}] {formatter(state, exception)}");
265265
System.Diagnostics.Debug.WriteLine("[" + eventId + "] " + formatter(state, exception));
266266
}
267267
}

examples/WebRTCExamples/UnityVideoSource/Assets/Scripts/PlayerController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel)
250250

251251
public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
252252
{
253-
Debug.Log("[" + eventId + "] " + formatter(state, exception));
253+
Debug.Log($"[{eventId}] {formatter(state, exception)}");
254254
System.Diagnostics.Debug.WriteLine("[" + eventId + "] " + formatter(state, exception));
255255
}
256256
}
257-

0 commit comments

Comments
 (0)