From 4eda6ef2cb72f3e9bce6669a89d78228e3c77d0b Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 14 Apr 2024 16:54:35 +0200 Subject: [PATCH] Selectively enable multicast/low latency locks --- client/application.cpp | 49 ++------------------ client/scenes/lobby.cpp | 3 ++ client/scenes/stream.cpp | 3 ++ client/wifi_lock.cpp | 97 ++++++++++++++++++++++++++++++++++++++++ client/wifi_lock.h | 36 +++++++++++++++ 5 files changed, 142 insertions(+), 46 deletions(-) create mode 100644 client/wifi_lock.cpp create mode 100644 client/wifi_lock.h diff --git a/client/application.cpp b/client/application.cpp index 93e4aa9f..18f09f3b 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -26,6 +26,7 @@ #include "utils/files.h" #include "utils/named_thread.h" #include "vk/check.h" +#include "wifi_lock.h" #include "xr/actionset.h" #include "xr/check.h" #include "xr/xr.h" @@ -938,46 +939,6 @@ void application::setup_jni() { jni::jni_thread::setup_thread(app_info.native_app->activity->vm); } - -void application::set_wifi_locks(bool enabled) -{ - jni::object<""> act(app_info.native_app->activity->clazz); - - jni::string lock_name("WiVRn"); - - static int api_level = jni::klass("android/os/Build$VERSION").field("SDK_INT"); - - auto app = act.call>("getApplication"); - auto ctx = app.call>("getApplicationContext"); - auto wifi_service_id = ctx.klass().field("WIFI_SERVICE"); - auto system_service = ctx.call>("getSystemService", wifi_service_id); - auto lock = system_service.call>("createMulticastLock", lock_name); - lock.call("setReferenceCounted", jni::Bool(false)); - lock.call(enabled ? "acquire" : "release"); - if (lock.call("isHeld")) - { - spdlog::info("MulticastLock acquired"); - } - else - { - spdlog::info("MulticastLock is not acquired"); - } - - auto wifi_lock = system_service.call>( - "createWifiLock", - jni::Int(api_level >= 29 ? 4 /*WIFI_MODE_FULL_LOW_LATENCY*/ : 3 /*WIFI_MODE_FULL_HIGH_PERF*/), - lock_name); - wifi_lock.call("setReferenceCounted", jni::Bool(false)); - wifi_lock.call(enabled ? "acquire" : "release"); - if (wifi_lock.call("isHeld")) - { - spdlog::info("WifiLock low latency acquired"); - } - else - { - spdlog::info("WifiLock low latency is not acquired"); - } -} #endif void application::cleanup() @@ -1258,9 +1219,7 @@ void application::session_state_changed(XrSessionState new_state, XrTime timesta case XR_SESSION_STATE_FOCUSED: session_visible = true; session_focused = true; -#ifdef __ANDROID__ - set_wifi_locks(true); -#endif + wifi_lock::set_enabled(true); break; case XR_SESSION_STATE_STOPPING: @@ -1268,9 +1227,7 @@ void application::session_state_changed(XrSessionState new_state, XrTime timesta session_focused = false; xr_session.end_session(); session_running = false; -#ifdef __ANDROID__ - set_wifi_locks(false); -#endif + wifi_lock::set_enabled(false); break; case XR_SESSION_STATE_EXITING: diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index 2f01d08b..56aaf3bd 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -29,6 +29,7 @@ #include "stream.h" #include "utils/contains.h" #include "version.h" +#include "wifi_lock.h" #include "wivrn_client.h" #include "xr/passthrough.h" #include @@ -858,6 +859,7 @@ void scenes::lobby::on_focused() about_picture = imgui_ctx->load_texture("wivrn.png"); } setup_passthrough(); + wifi_lock::want_multicast(true); } void scenes::lobby::setup_passthrough() @@ -906,6 +908,7 @@ void scenes::lobby::on_unfocused() swapchains_controllers.clear(); swapchain_imgui = xr::swapchain(); passthrough.emplace(); // default constructor does no passthrough + wifi_lock::want_multicast(false); } void scenes::lobby::on_session_state_changed(XrSessionState state) diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index de4a4d5a..f213ad99 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -31,6 +31,7 @@ #include "utils/sync_queue.h" #include "vk/pipeline.h" #include "vk/shader.h" +#include "wifi_lock.h" #include "wivrn_packets.h" #include #include @@ -203,6 +204,7 @@ void scenes::stream::on_focused() imgui_ctx->set_position({0, 0, -1}, {1, 0, 0, 0}); } + wifi_lock::want_low_latency(true); assert(video_stream_description); setup_reprojection_swapchain(); } @@ -211,6 +213,7 @@ void scenes::stream::on_unfocused() { imgui_ctx.reset(); swapchain_imgui = xr::swapchain(); + wifi_lock::want_low_latency(false); } scenes::stream::~stream() diff --git a/client/wifi_lock.cpp b/client/wifi_lock.cpp new file mode 100644 index 00000000..6972825f --- /dev/null +++ b/client/wifi_lock.cpp @@ -0,0 +1,97 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022-2024 Guillaume Meunier + * Copyright (C) 2022-2024 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "wifi_lock.h" +#include "spdlog/spdlog.h" + +#ifdef __ANDROID__ +#include "application.h" +#include +#include + +#include "jnipp.h" +#endif + +wifi_lock instance; + +void wifi_lock::apply() +{ +#ifdef __ANDROID__ + spdlog::debug("wifi locks: enabled {} multicast {} low latency {}", + enabled, + wants_multicast, + wants_low_latency); + jni::object<""> act(application::native_app()->activity->clazz); + + jni::string lock_name("WiVRn"); + + static int api_level = jni::klass("android/os/Build$VERSION").field("SDK_INT"); + + auto app = act.call>("getApplication"); + auto ctx = app.call>("getApplicationContext"); + auto wifi_service_id = ctx.klass().field("WIFI_SERVICE"); + auto system_service = ctx.call>("getSystemService", wifi_service_id); + auto lock = system_service.call>("createMulticastLock", lock_name); + lock.call("setReferenceCounted", jni::Bool(false)); + lock.call((enabled and wants_multicast) ? "acquire" : "release"); + if (lock.call("isHeld")) + { + spdlog::info("MulticastLock acquired"); + } + else + { + spdlog::info("MulticastLock is not acquired"); + } + + auto wifi_lock = system_service.call>( + "createWifiLock", + jni::Int(api_level >= 29 ? 4 /*WIFI_MODE_FULL_LOW_LATENCY*/ : 3 /*WIFI_MODE_FULL_HIGH_PERF*/), + lock_name); + wifi_lock.call("setReferenceCounted", jni::Bool(false)); + wifi_lock.call((enabled and wants_low_latency) ? "acquire" : "release"); + if (wifi_lock.call("isHeld")) + { + spdlog::info("WifiLock low latency acquired"); + } + else + { + spdlog::info("WifiLock low latency is not acquired"); + } +#endif +} + +void wifi_lock::set_enabled(bool enabled) +{ + std::lock_guard lock(instance.mutex); + instance.enabled = enabled; + instance.apply(); +} + +void wifi_lock::want_multicast(bool enabled) +{ + std::lock_guard lock(instance.mutex); + instance.wants_multicast += enabled ? 1 : -1; + instance.apply(); +} +void wifi_lock::want_low_latency(bool enabled) +{ + std::lock_guard lock(instance.mutex); + instance.wants_low_latency += enabled ? 1 : -1; + instance.apply(); +} diff --git a/client/wifi_lock.h b/client/wifi_lock.h new file mode 100644 index 00000000..c754ac2d --- /dev/null +++ b/client/wifi_lock.h @@ -0,0 +1,36 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022-2024 Guillaume Meunier + * Copyright (C) 2022-2024 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include + +class wifi_lock +{ + std::mutex mutex; + int wants_multicast = 0; + int wants_low_latency = 0; + bool enabled = true; + void apply(); + +public: + static void set_enabled(bool enabled); + + static void want_multicast(bool enabled); + static void want_low_latency(bool enabled); +};