Skip to content

Commit

Permalink
Pin current display when monitors are added/removed
Browse files Browse the repository at this point in the history
  • Loading branch information
ClassicOldSong committed Aug 24, 2024
1 parent ad42ef4 commit 19630a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
16 changes: 8 additions & 8 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,14 @@ namespace proc {
}
#endif

// Probe encoders again before streaming to ensure our chosen
// encoder matches the active GPU (which could have changed
// due to hotplugging, driver crash, primary monitor change,
// or any number of other factors).
if (video::probe_encoders()) {
return 503;
}

// Add Stream-specific environment variables
_env["SUNSHINE_APP_ID"] = _app.id;
_env["SUNSHINE_APP_NAME"] = _app.name;
Expand Down Expand Up @@ -290,14 +298,6 @@ namespace proc {
#endif
}

// Probe encoders again before streaming to ensure our chosen
// encoder matches the active GPU (which could have changed
// due to hotplugging, driver crash, primary monitor change,
// or any number of other factors).
if (video::probe_encoders()) {
return 503;
}

std::error_code ec;
_app_prep_begin = std::begin(_app.prep_cmds);
_app_prep_it = _app_prep_begin;
Expand Down
24 changes: 8 additions & 16 deletions src/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,10 +1018,10 @@ namespace video {
*/
void
refresh_displays(platf::mem_type_e dev_type, std::vector<std::string> &display_names, int &current_display_index, std::string &preferred_display_name) {
std::string current_display_name;
std::string current_display_name = preferred_display_name;

// If we have a current display index, let's start with that
if (current_display_index >= 0 && current_display_index < display_names.size()) {
if (current_display_name.empty() && current_display_index >= 0 && current_display_index < display_names.size()) {
current_display_name = display_names.at(current_display_index);
}

Expand All @@ -1042,6 +1042,10 @@ namespace video {
// We now have a new display name list, so reset the index back to 0
current_display_index = 0;

if (current_display_name.empty()) {
current_display_name = config::video.output_name;
}

// If we had a name previously, let's try to find it in the new list
if (!current_display_name.empty()) {
for (int x = 0; x < display_names.size(); ++x) {
Expand All @@ -1052,19 +1056,7 @@ namespace video {
}

// The old display was removed, so we'll start back at the first display again
BOOST_LOG(warning) << "Previous active display ["sv << current_display_name << "] is no longer present"sv;
}
else {
current_display_name = preferred_display_name;
if (current_display_name.empty()) {
current_display_name = config::video.output_name;
}
for (int x = 0; x < display_names.size(); ++x) {
if (display_names[x] == current_display_name) {
current_display_index = x;
return;
}
}
BOOST_LOG(warning) << "Desired display ["sv << current_display_name << "] does not exist"sv;
}
}

Expand Down Expand Up @@ -1303,7 +1295,7 @@ namespace video {
disp.reset();

// Refresh display names since a display removal might have caused the reinitialization
refresh_displays(encoder.platform_formats->dev_type, display_names, display_p);
refresh_displays(encoder.platform_formats->dev_type, display_names, display_p, proc::proc.display_name);

// Process any pending display switch with the new list of displays
if (switch_display_event->peek()) {
Expand Down

0 comments on commit 19630a9

Please sign in to comment.