sskodje / ScreenRecorderLib

A .NET library for screen recording in Windows, using native Microsoft Media Foundation for realtime encoding to h264 video or PNG images.
MIT License
408 stars 93 forks source link

Application using ScreenRecorderLib sometimes crashes with exception thrown from ScreenRecorderLib.dll #267

Closed ryssel closed 2 months ago

ryssel commented 11 months ago

After doing some screenrecordings from the same application suddenly my application crashes. There is no trace of the crash anywhere, except in Windows EventViewer.

Is there any way to try to localize the error, it looks like it happens when the recording is started? I'm doing RecorderOptions.AudioOptions.IsAudioEnabled = false;

Faulting application name: TrackMan.Gui.Shell.exe, version: 9.3.0.0, time stamp: 0x6514d416 Faulting module name: ScreenRecorderLib.dll, version: 5.3.0.0, time stamp: 0x649615a6 Exception code: 0xc0000005 Fault offset: 0x00000000000829b5 Faulting process id: 0x0xBAB0 Faulting application start time: 0x0x1DA05B06CF68B54 Faulting application path: C:\git\PerformanceStudio\projects\TrackMan.Gui.Shell\bin\x64\Debug\Modules\TrackMan.Gui.Shell.exe Faulting module path: C:\git\PerformanceStudio\projects\TrackMan.Gui.Shell\bin\x64\Debug\Modules\ScreenRecorderLib.dll Report Id: 063d2838-af31-4920-b608-30fb838a3c21 Faulting package full name: Faulting package-relative application ID:

The log from the screenrecorder says: 2023-10-23 15:24:23.094 [DEBUG] [RecordingManager.cpp | ConfigureOutputDir: 142] >> Video output folder is ready 2023-10-23 15:24:23.103 [INFO] [RecordingManager.cpp | operator (): 226] >> Starting recording task 2023-10-23 15:24:23.299 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 197] >> Resampler created for AudioOutputDevice 2023-10-23 15:24:23.300 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 198] >> Resampler (bits): 16 -> 16 2023-10-23 15:24:23.301 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 199] >> Resampler (channels): 2 -> 2 2023-10-23 15:24:23.302 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 200] >> Resampler (sampleFormat): 0 -> 0 2023-10-23 15:24:23.303 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 201] >> Resampler (sampleRate): 44100 -> 48000 2023-10-23 15:24:23.304 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 202] >> Resampler (validBitsPerSample): 16 -> 16 2023-10-23 15:24:23.308 [DEBUG] [AudioManager.cpp | ConfigureAudioCaptu: 137] >> Created WASAPI capture on AudioOutputDevice 2023-10-23 15:24:23.330 [INFO] [AudioManager.cpp | StartDeviceCapture: 114] >> Started audio capture on AudioOutputDevice: Headphones (Jabra Move SE v1.0.0) 2023-10-23 15:24:23.356 [DEBUG] [WASAPICapture.cpp | StartCaptureLoop: 372] >> Probably spurious glitch reported on first packet on AudioOutputDevice

If it is a successful recording with no crash it looks like: 2023-10-23 15:23:28.873 [DEBUG] [RecordingManager.cpp | ConfigureOutputDir: 142] >> Video output folder is ready 2023-10-23 15:23:28.875 [INFO] [RecordingManager.cpp | operator (): 226] >> Starting recording task 2023-10-23 15:23:29.072 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 197] >> Resampler created for AudioOutputDevice 2023-10-23 15:23:29.073 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 198] >> Resampler (bits): 16 -> 16 2023-10-23 15:23:29.074 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 199] >> Resampler (channels): 2 -> 2 2023-10-23 15:23:29.077 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 200] >> Resampler (sampleFormat): 0 -> 0 2023-10-23 15:23:29.086 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 201] >> Resampler (sampleRate): 44100 -> 48000 2023-10-23 15:23:29.090 [DEBUG] [WASAPICapture.cpp | InitializeResampler: 202] >> Resampler (validBitsPerSample): 16 -> 16 2023-10-23 15:23:29.093 [DEBUG] [AudioManager.cpp | ConfigureAudioCaptu: 137] >> Created WASAPI capture on AudioOutputDevice 2023-10-23 15:23:29.104 [INFO] [AudioManager.cpp | StartDeviceCapture: 114] >> Started audio capture on AudioOutputDevice: Headphones (Jabra Move SE v1.0.0) 2023-10-23 15:23:29.113 [DEBUG] [WASAPICapture.cpp | StartCaptureLoop: 372] >> Probably spurious glitch reported on first packet on AudioOutputDevice 2023-10-23 15:23:30.078 [INFO] [WindowsGraphicsCapture.c | ProcessRecordingTim: 456] >> Restarted Windows Graphics Capture 2023-10-23 15:23:30.374 [DEBUG] [OutputManager.cpp | BeginRecording: 104] >> Sink Writer initialized 2023-10-23 15:23:30.443 [DEBUG] [RecordingManager.cpp | StartRecorderLoop: 657] >> Changed Recording Status to Recording 2023-10-23 15:23:31.944 [WARN] [RecordingManager.cpp | ~RecordingManager: 101] >> Recording is in progress while destructing, cancelling recording task and waiting for completion. 2023-10-23 15:23:55.720 [WARN] [RecordingManager.cpp | ~RecordingManager: 101] >> Recording is in progress while destructing, cancelling recording task and waiting for completion. 2023-10-23 15:24:06.759 [INFO] [WindowsGraphicsCapture.c | ProcessRecordingTim: 456] >> Restarted Windows Graphics Capture 2023-10-23 15:24:20.510 [DEBUG] [RecordingManager.cpp | EndRecording: 276] >> Stopped recording task 2023-10-23 15:24:20.528 [DEBUG] [RecordingManager.cpp | StartRecorderLoop: 650] >> Recording task was cancelled 2023-10-23 15:24:20.558 [DEBUG] [WASAPICapture.cpp | StartCaptureLoop: 432] >> Received stop event after 8623 passes and 2268504 frames on AudioOutputDevice 2023-10-23 15:24:20.623 [DEBUG] [ScreenCaptureManager.cpp | CaptureThreadProc: 961] >> Exiting CaptureThreadProc 2023-10-23 15:24:20.698 [INFO] [OutputManager.cpp | FinalizeRecording: 134] >> Cleaning up resources 2023-10-23 15:24:20.700 [INFO] [OutputManager.cpp | FinalizeRecording: 135] >> Finalizing recording 2023-10-23 15:24:20.894 [DEBUG] [CMFSinkWriterCallback.h | OnFinalize: 18] >> CMFSinkWriterCallback::OnFinalize 2023-10-23 15:24:20.974 [DEBUG] [OutputManager.cpp | FinalizeRecording: 158] >> Shut down IMFMediaSink 2023-10-23 15:24:20.976 [INFO] [RecordingManager.cpp | operator (): 244] >> Exiting recording task 2023-10-23 15:24:21.304 [DEBUG] [RecordingManager.cpp | SetRecordingComplet: 345] >> Changed Recording Status to Idle

sskodje commented 11 months ago

Logging with "trace" will help identify where in the code the crash happens. It looks to either crash in the Audio capture, or when initializing the video encoder. Turning of audio capture will help rule it out, and changing recording and video encoder settings can help rule out other issues. From the logs, i can see a couple things that might be issues also.

2023-10-23 15:23:30.443 [DEBUG] [RecordingManager.cpp | StartRecorderLoop: 657] >> Changed Recording Status to Recording
2023-10-23 15:23:31.944 [WARN] [RecordingManager.cpp | ~RecordingManager: 101] >> Recording is in progress while destructing, cancelling recording task and waiting for completion.
2023-10-23 15:23:55.720 [WARN] [RecordingManager.cpp | ~RecordingManager: 101] >> Recording is in progress while destructing, cancelling recording task and waiting for completion.
2023-10-23 15:24:06.759 [INFO] [WindowsGraphicsCapture.c | ProcessRecordingTim: 456] >> Restarted Windows Graphics Capture
2023-10-23 15:24:20.510 [DEBUG] [RecordingManager.cpp | EndRecording: 276] >> Stopped recording task

These are not in order, and doesn't make sense. Are you running several recordings in parallell, logging to the same file? This is not thread safe.

Recording is in progress while destructing, cancelling recording task and waiting for completion.

This shouldn't really happen. It means you are disposing of the Recorder instance before it has cleaned up and shut down. It's mostly handled gracefully if it happens, but you should wait for the appropriate events before disposing.