Skip to content

Commit

Permalink
fix: Crash when alt-tabbing fullscreen games
Browse files Browse the repository at this point in the history
Related to #16
  • Loading branch information
lulzsun committed Aug 11, 2022
1 parent 655ffa9 commit 95663c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 55 deletions.
68 changes: 13 additions & 55 deletions Classes/Recorders/LibObsRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class LibObsRecorder : BaseRecorder {

static IntPtr windowHandle = IntPtr.Zero;
static IntPtr output = IntPtr.Zero;
static Rect currentSize;

Dictionary<string, IntPtr> audioSources = new(), videoSources = new();
Dictionary<string, IntPtr> audioEncoders = new(), videoEncoders = new();
Expand Down Expand Up @@ -49,8 +50,15 @@ public override void Start() {
if (formattedMsg == "[game-capture: 'gameplay'] Starting capture") {
if(signalGCHookSuccess != false && RecordingService.IsRecording) {
// everytime the "Starting capture" signal occurs, there could be a possibility that the game window has resized
// if it has resized, restart output with correct size
RestartOutput();
// check to see if windowSize is different from currentSize, if so, restart recording with correct output resolution
Rect windowSize = GetWindowSize(windowHandle);
if ((windowSize.GetWidth() > 1 && windowSize.GetHeight() > 1) && // fullscreen tabbing check
(currentSize.GetWidth() != windowSize.GetWidth() || currentSize.GetHeight() != windowSize.GetHeight())) {
RestartRecording();
}
else {
Logger.WriteLine("Fullscreen game coming into focus? Ignoring attempt to restart recording.");
}
}
signalGCHookSuccess = true;
}
Expand Down Expand Up @@ -301,61 +309,11 @@ public override async Task<bool> StopRecording() {
return true;
}

public static async void RestartOutput() {
public static void RestartRecording() {
if (output == IntPtr.Zero) return;

// Stop output
obs_output_stop(output);
// attempt to check if output signalled stop
int retryAttempt = 0;
while (signalOutputStop == false && retryAttempt < maxRetryAttempts) {
Logger.WriteLine(string.Format("Waiting for obs_output to stop... retry attempt #{0}", retryAttempt));
await Task.Delay(retryInterval);
retryAttempt++;
}
if (signalOutputStop == false && retryAttempt >= maxRetryAttempts) {
Logger.WriteLine("Issue trying to stop output, giving up.");
return;
}
retryAttempt = 0;

videoSavePath = Path.Join(Path.GetDirectoryName(videoSavePath), DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + "-ses.mp4");

// SETUP OUTPUT SETTINGS
IntPtr outputSettings = obs_data_create();
obs_data_set_string(outputSettings, "path", videoSavePath);
obs_output_update(output, outputSettings);
obs_data_release(outputSettings);

// get game's window size and change output to match
Rect windowSize = GetWindowSize(windowHandle);
// sometimes, the inital window size might be in a middle of a transition, and gives us a weird dimension
// this is a quick a dirty check: if there aren't more than 1120 pixels, we can assume it needs a retry
while (windowSize.GetWidth() + windowSize.GetHeight() < 1120 && retryAttempt < maxRetryAttempts) {
Logger.WriteLine(string.Format("Waiting to retrieve correct window size (currently {1}x{2})... retry attempt #{0}",
retryAttempt, windowSize.GetWidth(), windowSize.GetHeight()));
await Task.Delay(retryInterval);
retryAttempt++;
windowSize = GetWindowSize(windowHandle);
}
if (windowSize.GetWidth() + windowSize.GetHeight() < 1120 && retryAttempt >= maxRetryAttempts) {
Logger.WriteLine(String.Format("Possible issue in getting correct window size {0}x{1}", windowSize.GetWidth(), windowSize.GetHeight()));
ResetVideo();
}
else {
Logger.WriteLine(String.Format("Game capture window size: {0}x{1}", windowSize.GetWidth(), windowSize.GetHeight()));
ResetVideo(windowSize.GetWidth(), windowSize.GetHeight());
}

// preparations complete, launch the rocket
bool outputStartSuccess = obs_output_start(output);
if (outputStartSuccess != true) {
Logger.WriteLine("LibObs output recording error: '" + obs_output_get_last_error(output) + "'");
}
else {
Logger.WriteLine("Force Restart Output successful.");
signalOutputStop = false;
}
if(RecordingService.ActiveRecorder.GetType() == typeof(LibObsRecorder))
RecordingService.RestartRecording();
}

public IntPtr obs_audio_source_create(string id, string name, IntPtr settings = new(), string deviceId = "default") {
Expand Down
15 changes: 15 additions & 0 deletions Classes/Services/RecordingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,20 @@ public static async void StopRecording() {
//DetectionService.LoadDetections();
}
}

public static async void RestartRecording() {
if (!IsRecording) return;

bool stopResult = await ActiveRecorder.StopRecording();
bool startResult = await ActiveRecorder.StartRecording();

if (stopResult && startResult) {
Logger.WriteLine("Recording restart successful");
}
else {
Logger.WriteLine($"Issue trying to restart recording: {stopResult} {startResult}");
IsRecording = false;
}
}
}
}

0 comments on commit 95663c7

Please sign in to comment.