Skip to content

Commit

Permalink
优化锁屏中串流屏幕切换的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
qiin2333 committed Sep 22, 2024
1 parent 1a28b15 commit 9ef80ad
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 61 deletions.
1 change: 1 addition & 0 deletions cmake/packaging/windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,5 @@ set(CPACK_COMPONENT_GAMEPAD_GROUP "Scripts")
# Virtual Display Driver
set(CPACK_COMPONENT_VDD_DISPLAY_NAME "IddSampleDriver")
set(CPACK_COMPONENT_VDD_DESCRIPTION "支持HDR的虚拟显示器驱动安装")
set(CPACK_COMPONENT_VDD_REQUIRED OFF)
set(CPACK_COMPONENT_VDD_GROUP "Scripts")
31 changes: 25 additions & 6 deletions src/display_device/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ namespace display_device {
const auto vdd_devices { display_device::find_device_by_friendlyname(zako_name) };
if (!devices.empty()) {
BOOST_LOG(info) << "Available display devices: " << to_string(devices);
zako_device_id = vdd_devices;
// 大多数哔叽本开机默认虚拟屏优先导致黑屏
if (!vdd_devices.empty() && devices.size() > 1) {
session_t::get().disable_vdd();
Expand Down Expand Up @@ -268,22 +269,40 @@ namespace display_device {

if (should_toggle_vdd) {
write_resolutions << to_string(*config.resolution) << "]";
write_fps << to_string(*config.refresh_rate) << "]";
if (is_cached_fps) {
std::string str = write_fps.str();
str.pop_back();
write_fps.str("");
write_fps << str << "]";
}
else {
write_fps << to_string(*config.refresh_rate) << "]";
}

confighttp::saveVddSettings(write_resolutions.str(), write_fps.str(), config::video.adapter_name);
BOOST_LOG(info) << "Set Client request res to VDD: "sv << new_setting.str();
display_device::session_t::get().last_vdd_setting = new_setting.str();
}
}

while (settings.is_changing_settings_going_to_fail()) {
std::this_thread::sleep_for(5s);
}

bool should_reset_zako_hdr = false;
int retry_count = 0;
auto device_zako = display_device::find_device_by_friendlyname(zako_name);
if (device_zako.empty()) {
// 解锁后启动vdd,避免捕获不到流串流黑屏
if (settings.is_changing_settings_going_to_fail()) {
std::thread { [this]() {
while (settings.is_changing_settings_going_to_fail()) {
std::this_thread::sleep_for(777ms);
BOOST_LOG(warning) << "Fisrt time Enable vdd will fail - retrying later...";
}
session_t::get().enable_vdd();
config::video.output_name = zako_device_id;
} }
.detach();
config.device_id = zako_device_id;
return;
}
session_t::get().enable_vdd();
}
else if (should_toggle_vdd) {
Expand All @@ -294,7 +313,7 @@ namespace display_device {
}

device_zako = display_device::find_device_by_friendlyname(zako_name);
while (device_zako.empty() && retry_count < 30) {
while (device_zako.empty() && retry_count < 50) {
BOOST_LOG(info) << "Find zako retry_count : "sv << retry_count;
retry_count += 1;
device_zako = display_device::find_device_by_friendlyname(zako_name);
Expand Down
1 change: 1 addition & 0 deletions src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ bool display_cursor = true;
#ifdef _WIN32
nvprefs::nvprefs_interface nvprefs_instance;
std::string zako_name = "VDD by MTT";
std::string zako_device_id;
#endif
1 change: 1 addition & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern bool display_cursor;
extern nvprefs::nvprefs_interface nvprefs_instance;

extern std::string zako_name;
extern std::string zako_device_id;
#endif

/**
Expand Down
76 changes: 38 additions & 38 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ main(int argc, char *argv[]) {
return fn->second(argv[0], config::sunshine.cmd.argc, config::sunshine.cmd.argv);
}

// Adding this guard here first as it also performs recovery after crash,
// otherwise people could theoretically end up without display output.
// It also should be run be destroyed before forced shutdown.
auto display_device_deinit_guard = display_device::session_t::init();
if (!display_device_deinit_guard) {
BOOST_LOG(error) << "Display device session failed to initialize"sv;
}

#ifdef WIN32
// Modify relevant NVIDIA control panel settings if the system has corresponding gpu
if (nvprefs_instance.load()) {
Expand Down Expand Up @@ -229,6 +237,36 @@ main(int argc, char *argv[]) {
system_tray::run_tray();
#endif

// Create signal handler after logging has been initialized
auto shutdown_event = mail::man->event<bool>(mail::shutdown);
on_signal(SIGINT, [&force_shutdown, &display_device_deinit_guard, shutdown_event]() {
BOOST_LOG(info) << "Interrupt handler called"sv;

auto task = []() {
BOOST_LOG(fatal) << "10 seconds passed, yet Sunshine's still running: Forcing shutdown"sv;
logging::log_flush();
lifetime::debug_trap();
};
force_shutdown = task_pool.pushDelayed(task, 10s).task_id;

shutdown_event->raise(true);
display_device_deinit_guard.reset();
});

on_signal(SIGTERM, [&force_shutdown, &display_device_deinit_guard, shutdown_event]() {
BOOST_LOG(info) << "Terminate handler called"sv;

auto task = []() {
BOOST_LOG(fatal) << "10 seconds passed, yet Sunshine's still running: Forcing shutdown"sv;
logging::log_flush();
lifetime::debug_trap();
};
force_shutdown = task_pool.pushDelayed(task, 10s).task_id;

shutdown_event->raise(true);
display_device_deinit_guard.reset();
});

#ifdef _WIN32
// Terminate gracefully on Windows when console window is closed
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
Expand Down Expand Up @@ -281,44 +319,6 @@ main(int argc, char *argv[]) {
upnp_unmap = upnp::start();
});

// Adding this guard here first as it also performs recovery after crash,
// otherwise people could theoretically end up without display output.
// It also should be run be destroyed before forced shutdown.
auto display_device_deinit_guard = display_device::session_t::init();
if (!display_device_deinit_guard) {
BOOST_LOG(error) << "Display device session failed to initialize"sv;
}

// Create signal handler after logging has been initialized
auto shutdown_event = mail::man->event<bool>(mail::shutdown);
on_signal(SIGINT, [&force_shutdown, &display_device_deinit_guard, shutdown_event]() {
BOOST_LOG(info) << "Interrupt handler called"sv;

auto task = []() {
BOOST_LOG(fatal) << "10 seconds passed, yet Sunshine's still running: Forcing shutdown"sv;
logging::log_flush();
lifetime::debug_trap();
};
force_shutdown = task_pool.pushDelayed(task, 10s).task_id;

shutdown_event->raise(true);
display_device_deinit_guard.reset();
});

on_signal(SIGTERM, [&force_shutdown, &display_device_deinit_guard, shutdown_event]() {
BOOST_LOG(info) << "Terminate handler called"sv;

auto task = []() {
BOOST_LOG(fatal) << "10 seconds passed, yet Sunshine's still running: Forcing shutdown"sv;
logging::log_flush();
lifetime::debug_trap();
};
force_shutdown = task_pool.pushDelayed(task, 10s).task_id;

shutdown_event->raise(true);
display_device_deinit_guard.reset();
});

// FIXME: Temporary workaround: Simple-Web_server needs to be updated or replaced
if (shutdown_event->peek()) {
return lifetime::desired_exit_code;
Expand Down
32 changes: 16 additions & 16 deletions src/platform/windows/display_device/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

// local includes
#include "settings_topology.h"
#include "src/globals.h"
#include "src/audio.h"
#include "src/display_device/session.h"
#include "src/display_device/to_string.h"
#include "src/globals.h"
#include "src/logging.h"
#include "windows_utils.h"

Expand Down Expand Up @@ -707,21 +707,6 @@ namespace display_device {

bool
settings_t::revert_settings() {
const auto devices { display_device::enum_available_devices() };
const auto vdd_devices { display_device::find_device_by_friendlyname(zako_name) };

if (config::video.preferUseVdd) {
if (!vdd_devices.empty() && devices.size() > 1) {
Sleep(777);
BOOST_LOG(info) << "preferUseVdd && devices.size() > 1, turning off vdd";
display_device::session_t::get().disable_vdd();
}
}
else if (vdd_devices.empty()) {
BOOST_LOG(info) << "Vdd resident so turning on it";
display_device::session_t::get().enable_vdd();
}

if (!persistent_data) {
BOOST_LOG(info) << "Loading persistent display device settings.";
persistent_data = load_settings(filepath);
Expand Down Expand Up @@ -751,6 +736,21 @@ namespace display_device {
BOOST_LOG(info) << "Display device configuration reverted.";
}

const auto devices { display_device::enum_available_devices() };
const auto vdd_devices { display_device::find_device_by_friendlyname(zako_name) };

if (config::video.preferUseVdd) {
if (!vdd_devices.empty() && devices.size() > 1) {
Sleep(777);
BOOST_LOG(info) << "preferUseVdd && devices.size() > 1, turning off vdd";
display_device::session_t::get().disable_vdd();
}
}
else if (vdd_devices.empty()) {
BOOST_LOG(info) << "Vdd resident so turning on it";
display_device::session_t::get().enable_vdd();
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src_assets/common/sunshine-control-panel

0 comments on commit 9ef80ad

Please sign in to comment.