moonlight-stream / moonlight-qt

GameStream client for PCs (Windows, Mac, Linux, and Steam Link)
GNU General Public License v3.0
10.05k stars 586 forks source link

Use Windows WiFi API function "WlanSetInterface" to disable background scanning while client is streaming on WiFi #1368

Open KingKaom opened 1 month ago

KingKaom commented 1 month ago

PROBLEM Ping spikes are observed every 5-10 minutes as shown here when the client is on WiFi. Their solution/workaround does not apply to non Intel WiFi adapters (Qualcomm FastConnect 7800 found in the Surface Laptop 7 for example).

SOLUTION The workaround to this issue from the FAQ reads as follows: "If you're streaming to a Windows PC with an Intel WiFi adapter, try setting "Global BG scan blocking" to "On Good RSSI" in the Device Manager Properties for the WiFi adapter. If "On Good RSSI" does not show up, try using "Always" instead."

This workaround only applies to Intel WiFi adapters. A more generic approach (using the Windows WIFi API) would mean this fix would apply to anyone on Windows and it could be enabled only while Moonlight is active. WlanSetInterface even has an option to enable streaming mode on the WiFi adapter, further improving latency and preventing power saving functions from interfering with streaming.

https://learn.microsoft.com/en-us/windows/win32/api/wlanapi/nf-wlanapi-wlansetinterface

ALTERNATIVES In order to prevent the occasional ping spikes I've tried:

KingKaom commented 1 month ago

As a workaround, running netsh wlan set autoconfig enabled=no interface="Wi-Fi" from elevated privileges PowerShell will fix this lag.

Undo it by running netsh wlan set autoconfig enabled=yes interface="Wi-Fi" after streaming.

cgutman commented 1 month ago

Hmm, we do request low latency mode which is supposed to suppress unnecessary background scanning. I wonder why it doesn't work in your case.

https://github.com/moonlight-stream/moonlight-common-c/blob/8599b6042a4ba27749b0f94134dd614b4328a9bc/src/PlatformSockets.c#L883-L900

I was aware of wlan_intf_opcode_background_scan_enabled but was hesistant to use it because I am worried it could prevent optimal roaming on mesh networks. We could suppress scanning if we see multiple BSSIDs in range for the current SSID, but that would probably prevent cross-frequency roaming between 2.4 and 5 GHz on the same AP 🤔

KingKaom commented 1 month ago

I actually returned the Qualcomm laptop and now have one with a Mediatek WiFi 7 adapter and am having the same issue. Running netsh wlan set autoconfig enabled=no interface="Wi-Fi" fixes it on this one as well.

Would it be possible to have a toggle in the settings for people like me that still get lag spikes even with the pfnWlanSetInterface method you referenced? Maybe the toggle could use wlan_intf_opcode_background_scan_enabled but warn users of mesh networks that it would affect their ability to pick up a new access point if they move while running Moonlight. I don't really know many people who play games while running around their house but I suppose it could happen lol.