Enable media streaming mode for 802.11 WLAN interfaces on Windows

This commit is contained in:
Cameron Gutman
2020-08-11 22:10:42 -07:00
parent f2c1d03d8e
commit 5ddd53e17f
3 changed files with 73 additions and 0 deletions

View File

@@ -386,10 +386,14 @@ int initializePlatform(void) {
return err;
}
enterLowLatencyMode();
return 0;
}
void cleanupPlatform(void) {
exitLowLatencyMode();
cleanupPlatformSockets();
enet_deinitialize();

View File

@@ -9,6 +9,10 @@
#define TCPv4_MSS 536
#define TCPv6_MSS 1220
#if defined(LC_WINDOWS)
static HANDLE WlanHandle;
#endif
void addrToUrlSafeString(struct sockaddr_storage* addr, char* string)
{
char addrstr[INET6_ADDRSTRLEN];
@@ -508,6 +512,66 @@ int isPrivateNetworkAddress(struct sockaddr_storage* address) {
return 0;
}
// Enable platform-specific low latency options (best-effort)
void enterLowLatencyMode(void) {
#if defined(LC_WINDOWS)
DWORD negotiatedVersion;
PWLAN_INTERFACE_INFO_LIST wlanInterfaceList;
DWORD i;
// Reduce timer period to increase wait precision
timeBeginPeriod(1);
// Use the Vista+ WLAN API version
LC_ASSERT(WlanHandle == NULL);
if (WlanOpenHandle(WLAN_API_MAKE_VERSION(2, 0), NULL, &negotiatedVersion, &WlanHandle) != ERROR_SUCCESS) {
return;
}
if (WlanEnumInterfaces(WlanHandle, NULL, &wlanInterfaceList) != ERROR_SUCCESS) {
WlanCloseHandle(WlanHandle, NULL);
WlanHandle = NULL;
return;
}
for (i = 0; i < wlanInterfaceList->dwNumberOfItems; i++) {
if (wlanInterfaceList->InterfaceInfo[i].isState == wlan_interface_state_connected) {
DWORD error;
BOOL value;
// Enable media streaming mode for 802.11 wireless interfaces to reduce latency and
// unneccessary background scanning operations that cause packet loss and jitter.
//
// https://docs.microsoft.com/en-us/windows-hardware/drivers/network/oid-wdi-set-connection-quality
// https://docs.microsoft.com/en-us/previous-versions/windows/hardware/wireless/native-802-11-media-streaming
value = TRUE;
error = WlanSetInterface(WlanHandle, &wlanInterfaceList->InterfaceInfo[i].InterfaceGuid,
wlan_intf_opcode_media_streaming_mode, sizeof(value), &value, NULL);
if (error == ERROR_SUCCESS) {
Limelog("WLAN interface %d is now in low latency mode\n", i);
}
}
}
WlanFreeMemory(wlanInterfaceList);
#else
#endif
}
void exitLowLatencyMode(void) {
#if defined(LC_WINDOWS)
// Closing our WLAN client handle will undo our optimizations
if (WlanHandle != NULL) {
WlanCloseHandle(WlanHandle, NULL);
WlanHandle = NULL;
}
// Restore original timer period
timeEndPeriod(1);
#else
#endif
}
int initializePlatformSockets(void) {
#if defined(LC_WINDOWS)
WSADATA data;

View File

@@ -6,6 +6,8 @@
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <wlanapi.h>
#include <timeapi.h>
#define SetLastSocketError(x) WSASetLastError(x)
#define LastSocketError() WSAGetLastError()
@@ -63,5 +65,8 @@ void closeSocket(SOCKET s);
int isPrivateNetworkAddress(struct sockaddr_storage* address);
int pollSockets(struct pollfd* pollFds, int pollFdsCount, int timeoutMs);
void enterLowLatencyMode(void);
void exitLowLatencyMode(void);
int initializePlatformSockets(void);
void cleanupPlatformSockets(void);