Skip to content

Commit 67062c7

Browse files
authored
Refactor logging to use structured logging patterns for Windows media (#1690)
Update all logging statements to use structured logging with named placeholders and argument lists. Add logger.IsEnabled checks to guard value processing. Change logger in WindowsAudioEndPoint to static. Refactor video format logging in WindowsVideoEndPoint to only run when debug logging is enabled and use structured logging.
1 parent 85ac3b8 commit 67062c7

2 files changed

Lines changed: 35 additions & 19 deletions

File tree

src/SIPSorceryMedia.Windows/WindowsAudioEndPoint.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class WindowsAudioEndPoint : IAudioEndPoint
4646

4747
public readonly static AudioSamplingRatesEnum DefaultAudioPlaybackRate = AudioSamplingRatesEnum.Rate8KHz;
4848

49-
private ILogger logger = SIPSorcery.LogFactory.CreateLogger<WindowsAudioEndPoint>();
49+
private static ILogger logger = SIPSorcery.LogFactory.CreateLogger<WindowsAudioEndPoint>();
5050

5151
private WaveFormat _waveSinkFormat;
5252
private WaveFormat _waveSourceFormat;
@@ -170,7 +170,9 @@ public void SetAudioSourceFormat(AudioFormat audioFormat)
170170
if (_waveSourceFormat.SampleRate != _audioFormatManager.SelectedFormat.ClockRate)
171171
{
172172
// Reinitialise the audio capture device.
173-
logger.LogDebug($"Windows audio end point adjusting capture rate from {_waveSourceFormat.SampleRate} to {_audioFormatManager.SelectedFormat.ClockRate}.");
173+
logger.LogDebug("Windows audio end point adjusting capture rate from {CurrentCaptureRate} to {SelectedCaptureRate}.",
174+
_waveSourceFormat.SampleRate,
175+
_audioFormatManager.SelectedFormat.ClockRate);
174176

175177
InitCaptureDevice(_audioInDeviceIndex, _audioFormatManager.SelectedFormat.ClockRate, _audioFormatManager.SelectedFormat.ChannelCount);
176178
}
@@ -186,7 +188,9 @@ public void SetAudioSinkFormat(AudioFormat audioFormat)
186188
if (_waveSinkFormat.SampleRate != _audioFormatManager.SelectedFormat.ClockRate)
187189
{
188190
// Reinitialise the audio output device.
189-
logger.LogDebug($"Windows audio end point adjusting playback rate from {_waveSinkFormat.SampleRate} to {_audioFormatManager.SelectedFormat.ClockRate}.");
191+
logger.LogDebug("Windows audio end point adjusting playback rate from {CurrentPlaybackRate} to {SelectedPlaybackRate}.",
192+
_waveSinkFormat.SampleRate,
193+
_audioFormatManager.SelectedFormat.ClockRate);
190194

191195
InitPlaybackDevice(_audioOutDeviceIndex, _audioFormatManager.SelectedFormat.ClockRate, _audioFormatManager.SelectedFormat.ChannelCount);
192196
}
@@ -322,7 +326,9 @@ private void InitCaptureDevice(int audioInDeviceIndex, int audioSourceSampleRate
322326
}
323327
else
324328
{
325-
logger.LogWarning($"The requested audio input device index {audioInDeviceIndex} exceeds the maximum index of {WaveInEvent.DeviceCount - 1}.");
329+
logger.LogWarning("The requested audio input device index {AudioInputDeviceIndex} exceeds the maximum index of {MaximumDeviceIndex}.",
330+
audioInDeviceIndex,
331+
WaveInEvent.DeviceCount - 1);
326332
OnAudioSourceError?.Invoke($"The requested audio input device index {audioInDeviceIndex} exceeds the maximum index of {WaveInEvent.DeviceCount - 1}.");
327333
}
328334
}

src/SIPSorceryMedia.Windows/WindowsVideoEndPoint.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,21 @@ public static async Task ListDevicesAndFormats()
449449
MediaCapture mediaCapture = new MediaCapture();
450450
await mediaCapture.InitializeAsync(mediaCaptureSettings);
451451

452-
foreach (var srcFmtList in mediaCapture.FrameSources.Values.Select(x => x.SupportedFormats).Select(y => y.ToList()))
452+
if (logger.IsEnabled(LogLevel.Debug))
453453
{
454-
foreach (var srcFmt in srcFmtList)
454+
foreach (var srcFmtList in mediaCapture.FrameSources.Values)
455455
{
456-
var vidFmt = srcFmt.VideoFormat;
457-
float vidFps = vidFmt.MediaFrameFormat.FrameRate.Numerator / vidFmt.MediaFrameFormat.FrameRate.Denominator;
458-
string pixFmt = vidFmt.MediaFrameFormat.Subtype == MF_I420_PIXEL_FORMAT ? "I420" : vidFmt.MediaFrameFormat.Subtype;
459-
logger.LogDebug("Video Capture device {VideoDeviceName} format {Width}x{Height} {Fps:0.##}fps {PixelFormat}",
460-
vidCapDevice.Name, vidFmt.Width, vidFmt.Height, vidFps, pixFmt);
456+
foreach (var srcFmt in srcFmtList.SupportedFormats)
457+
{
458+
var vidFps = srcFmt.VideoFormat.MediaFrameFormat.FrameRate.Numerator / srcFmt.VideoFormat.MediaFrameFormat.FrameRate.Denominator;
459+
var pixFmt = srcFmt.VideoFormat.MediaFrameFormat.Subtype == MF_I420_PIXEL_FORMAT ? "I420" : srcFmt.VideoFormat.MediaFrameFormat.Subtype;
460+
logger.LogDebug("Video Capture device {VideoDeviceName} format {Width}x{Height} {FrameRate:0.##}fps {PixelFormat}",
461+
vidCapDevice.Name,
462+
srcFmt.VideoFormat.Width,
463+
srcFmt.VideoFormat.Height,
464+
vidFps,
465+
pixFmt);
466+
}
461467
}
462468
}
463469
}
@@ -678,17 +684,21 @@ private void SetBitmapData(byte[] buffer, SoftwareBitmap sbmp, VideoPixelFormats
678684
/// </summary>
679685
private void PrintFrameSourceInfo(MediaFrameSource frameSource)
680686
{
681-
var width = frameSource.CurrentFormat.VideoFormat.Width;
682-
var height = frameSource.CurrentFormat.VideoFormat.Height;
683-
var fpsNumerator = frameSource.CurrentFormat.FrameRate.Numerator;
684-
var fpsDenominator = frameSource.CurrentFormat.FrameRate.Denominator;
687+
if (!logger.IsEnabled(LogLevel.Information))
688+
{
689+
return;
690+
}
685691

686-
double fps = fpsNumerator / fpsDenominator;
687-
string pixFmt = frameSource.CurrentFormat.Subtype;
688-
string deviceName = frameSource.Info.DeviceInformation.Name;
692+
var currentFormat = frameSource.CurrentFormat;
693+
var videoFormat = currentFormat.VideoFormat;
694+
var frameRate = currentFormat.FrameRate;
689695

690696
logger.LogInformation("Video capture device {VideoDeviceName} successfully initialised: {Width}x{Height} {Fps:0.##}fps pixel format {PixelFormat}.",
691-
deviceName, width, height, fps, pixFmt);
697+
frameSource.Info.DeviceInformation.Name,
698+
videoFormat.Width,
699+
videoFormat.Height,
700+
frameRate.Numerator / frameRate.Denominator,
701+
currentFormat.Subtype);
692702
}
693703

694704
public void Dispose()

0 commit comments

Comments
 (0)