Rework wifi/multicast locks

This commit is contained in:
Patrick Nicolas
2024-09-12 17:00:07 +02:00
parent 5822dddab4
commit b7f27e9300
8 changed files with 127 additions and 72 deletions
+9 -2
View File
@@ -935,6 +935,7 @@ std::pair<XrAction, XrActionType> application::get_action(const std::string & re
application::application(application_info info) : application::application(application_info info) :
app_info(std::move(info)) app_info(std::move(info))
{ {
#ifdef __ANDROID__ #ifdef __ANDROID__
// https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html // https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
@@ -995,6 +996,12 @@ application::application(application_info info) :
} }
}; };
#ifdef __ANDROID__
wifi = wifi_lock::make_wifi_lock(app_info.native_app->activity->clazz);
#else
wifi = std::make_shared<wifi_lock>();
#endif
// Initialize the loader for this platform // Initialize the loader for this platform
PFN_xrInitializeLoaderKHR initializeLoader = nullptr; PFN_xrInitializeLoaderKHR initializeLoader = nullptr;
if (XR_SUCCEEDED(xrGetInstanceProcAddr(XR_NULL_HANDLE, "xrInitializeLoaderKHR", (PFN_xrVoidFunction *)(&initializeLoader)))) if (XR_SUCCEEDED(xrGetInstanceProcAddr(XR_NULL_HANDLE, "xrInitializeLoaderKHR", (PFN_xrVoidFunction *)(&initializeLoader))))
@@ -1050,6 +1057,8 @@ void application::cleanup()
i->spaces_by_name.clear(); i->spaces_by_name.clear();
} }
wifi.reset();
#ifdef __ANDROID__ #ifdef __ANDROID__
jni::jni_thread::detach(); jni::jni_thread::detach();
#endif #endif
@@ -1315,7 +1324,6 @@ void application::session_state_changed(XrSessionState new_state, XrTime timesta
case XR_SESSION_STATE_FOCUSED: case XR_SESSION_STATE_FOCUSED:
session_visible = true; session_visible = true;
session_focused = true; session_focused = true;
wifi_lock::set_enabled(true);
break; break;
case XR_SESSION_STATE_STOPPING: case XR_SESSION_STATE_STOPPING:
@@ -1323,7 +1331,6 @@ void application::session_state_changed(XrSessionState new_state, XrTime timesta
session_focused = false; session_focused = false;
xr_session.end_session(); xr_session.end_session();
session_running = false; session_running = false;
wifi_lock::set_enabled(false);
break; break;
case XR_SESSION_STATE_EXITING: case XR_SESSION_STATE_EXITING:
+8 -2
View File
@@ -20,6 +20,7 @@
#pragma once #pragma once
#include "configuration.h" #include "configuration.h"
#include "wifi_lock.h"
#include "xr/fb_face_tracker2.h" #include "xr/fb_face_tracker2.h"
#include "xr/hand_tracker.h" #include "xr/hand_tracker.h"
#ifdef __ANDROID__ #ifdef __ANDROID__
@@ -143,6 +144,8 @@ class application : public singleton<application>
std::filesystem::path config_path; std::filesystem::path config_path;
std::filesystem::path cache_path; std::filesystem::path cache_path;
std::shared_ptr<wifi_lock> wifi;
std::string server_address; std::string server_address;
std::mutex scene_stack_lock; std::mutex scene_stack_lock;
@@ -182,8 +185,6 @@ public:
#ifdef __ANDROID__ #ifdef __ANDROID__
void setup_jni(); void setup_jni();
void set_wifi_locks(bool enabled);
static AAssetManager * asset_manager() static AAssetManager * asset_manager()
{ {
return instance().app_info.native_app->activity->assetManager; return instance().app_info.native_app->activity->assetManager;
@@ -317,6 +318,11 @@ public:
return instance().eye_gaze_space; return instance().eye_gaze_space;
}; };
static wifi_lock & get_wifi_lock()
{
return *instance().wifi;
}
static void ignore_debug_reports_for(void * object) static void ignore_debug_reports_for(void * object)
{ {
#ifndef NDEBUG #ifndef NDEBUG
+2 -3
View File
@@ -29,7 +29,6 @@
#include "stream.h" #include "stream.h"
#include "utils/contains.h" #include "utils/contains.h"
#include "version.h" #include "version.h"
#include "wifi_lock.h"
#include "wivrn_client.h" #include "wivrn_client.h"
#include "xr/passthrough.h" #include "xr/passthrough.h"
#include <glm/gtc/matrix_access.hpp> #include <glm/gtc/matrix_access.hpp>
@@ -744,7 +743,7 @@ void scenes::lobby::on_focused()
about_picture = imgui_ctx->load_texture("wivrn.png"); about_picture = imgui_ctx->load_texture("wivrn.png");
} }
setup_passthrough(); setup_passthrough();
wifi_lock::want_multicast(true); multicast = application::get_wifi_lock().get_multicast_lock();
} }
void scenes::lobby::setup_passthrough() void scenes::lobby::setup_passthrough()
@@ -792,7 +791,7 @@ void scenes::lobby::on_unfocused()
swapchains_controllers.clear(); swapchains_controllers.clear();
swapchain_imgui = xr::swapchain(); swapchain_imgui = xr::swapchain();
passthrough.emplace<std::monostate>(); passthrough.emplace<std::monostate>();
wifi_lock::want_multicast(false); multicast.reset();
} }
void scenes::lobby::on_session_state_changed(XrSessionState state) void scenes::lobby::on_session_state_changed(XrSessionState state)
+2
View File
@@ -24,6 +24,7 @@
#include "render/scene_data.h" #include "render/scene_data.h"
#include "scene.h" #include "scene.h"
#include "scenes/hand_model.h" #include "scenes/hand_model.h"
#include "wifi_lock.h"
#include "wivrn_discover.h" #include "wivrn_discover.h"
#include <vulkan/vulkan_raii.hpp> #include <vulkan/vulkan_raii.hpp>
@@ -46,6 +47,7 @@ class stream;
class lobby : public scene_impl<lobby> class lobby : public scene_impl<lobby>
{ {
std::optional<wivrn_discover> discover; std::optional<wivrn_discover> discover;
wifi_lock::multicast multicast;
char add_server_window_prettyname[200]; char add_server_window_prettyname[200];
char add_server_window_hostname[200]; char add_server_window_hostname[200];
+2 -3
View File
@@ -30,7 +30,6 @@
#include "utils/ranges.h" #include "utils/ranges.h"
#include "vk/pipeline.h" #include "vk/pipeline.h"
#include "vk/shader.h" #include "vk/shader.h"
#include "wifi_lock.h"
#include "wivrn_packets.h" #include "wivrn_packets.h"
#include <algorithm> #include <algorithm>
#include <mutex> #include <mutex>
@@ -215,7 +214,7 @@ void scenes::stream::on_focused()
plots_toggle_2 = get_action("plots_toggle_2").first; plots_toggle_2 = get_action("plots_toggle_2").first;
} }
wifi_lock::want_low_latency(true); wifi = application::get_wifi_lock().get_wifi_lock();
assert(video_stream_description); assert(video_stream_description);
setup_reprojection_swapchain(); setup_reprojection_swapchain();
} }
@@ -224,7 +223,7 @@ void scenes::stream::on_unfocused()
{ {
imgui_ctx.reset(); imgui_ctx.reset();
swapchain_imgui = xr::swapchain(); swapchain_imgui = xr::swapchain();
wifi_lock::want_low_latency(false); wifi.reset();
} }
scenes::stream::~stream() scenes::stream::~stream()
+3
View File
@@ -24,6 +24,7 @@
#include "render/imgui_impl.h" #include "render/imgui_impl.h"
#include "scene.h" #include "scene.h"
#include "stream_reprojection.h" #include "stream_reprojection.h"
#include "wifi_lock.h"
#include "wivrn_client.h" #include "wivrn_client.h"
#include "wivrn_packets.h" #include "wivrn_packets.h"
#include <mutex> #include <mutex>
@@ -62,6 +63,8 @@ private:
std::vector<uint64_t> frames() const; std::vector<uint64_t> frames() const;
}; };
wifi_lock::wifi wifi;
// for frames inside accumulator images // for frames inside accumulator images
std::mutex frames_mutex; std::mutex frames_mutex;
std::vector<std::shared_ptr<shard_accumulator::blit_handle>> common_frame(XrTime display_time); std::vector<std::shared_ptr<shard_accumulator::blit_handle>> common_frame(XrTime display_time);
+75 -53
View File
@@ -18,25 +18,20 @@
*/ */
#include "wifi_lock.h" #include "wifi_lock.h"
#include "spdlog/spdlog.h"
#ifdef __ANDROID__ #ifdef __ANDROID__
#include "application.h" #include "application.h"
#include "spdlog/spdlog.h"
#include <android/native_activity.h> #include <android/native_activity.h>
#include <sys/system_properties.h> #include <sys/system_properties.h>
#include "jnipp.h" #include "jnipp.h"
#endif
wifi_lock instance; wifi_lock::wifi_lock(decltype(multicast_) m, decltype(wifi_) w) :
multicast_(std::move(m)), wifi_(std::move(w)) {}
void wifi_lock::apply() std::shared_ptr<wifi_lock> wifi_lock::make_wifi_lock(jobject activity)
{ {
#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::object<""> act(application::native_app()->activity->clazz);
jni::string lock_name("WiVRn"); jni::string lock_name("WiVRn");
@@ -47,51 +42,78 @@ void wifi_lock::apply()
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext"); auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
auto wifi_service_id = ctx.klass().field<jni::string>("WIFI_SERVICE"); auto wifi_service_id = ctx.klass().field<jni::string>("WIFI_SERVICE");
auto system_service = ctx.call<jni::object<"java/lang/Object">>("getSystemService", wifi_service_id); auto system_service = ctx.call<jni::object<"java/lang/Object">>("getSystemService", wifi_service_id);
auto lock = system_service.call<jni::object<"android/net/wifi/WifiManager$MulticastLock">>("createMulticastLock", lock_name); return std::shared_ptr<wifi_lock>(new wifi_lock(
lock.call<void>("setReferenceCounted", jni::Bool(false)); system_service.call<jni::object<"android/net/wifi/WifiManager$MulticastLock">>("createMulticastLock", lock_name),
lock.call<void>((enabled and wants_multicast) ? "acquire" : "release"); system_service.call<jni::object<"android/net/wifi/WifiManager$WifiLock">>(
if (lock.call<jni::Bool>("isHeld")) "createWifiLock",
{ jni::Int(api_level >= 29 ? 4 /*WIFI_MODE_FULL_LOW_LATENCY*/ : 3 /*WIFI_MODE_FULL_HIGH_PERF*/),
spdlog::info("MulticastLock acquired"); lock_name)));
} }
else
{
spdlog::info("MulticastLock is not acquired");
}
auto wifi_lock = system_service.call<jni::object<"android/net/wifi/WifiManager$WifiLock">>( void wifi_lock::print_wifi()
"createWifiLock", {
jni::Int(api_level >= 29 ? 4 /*WIFI_MODE_FULL_LOW_LATENCY*/ : 3 /*WIFI_MODE_FULL_HIGH_PERF*/), if (wifi_.call<jni::Bool>("isHeld"))
lock_name);
wifi_lock.call<void>("setReferenceCounted", jni::Bool(false));
wifi_lock.call<void>((enabled and wants_low_latency) ? "acquire" : "release");
if (wifi_lock.call<jni::Bool>("isHeld"))
{
spdlog::info("WifiLock low latency acquired"); spdlog::info("WifiLock low latency acquired");
}
else else
{ spdlog::info("WifiLock low latency released");
spdlog::info("WifiLock low latency is not acquired"); }
}
void wifi_lock::acquire_wifi()
{
std::lock_guard lock(mutex);
wifi_.call<void>("acquire");
print_wifi();
}
void wifi_lock::release_wifi()
{
std::lock_guard lock(mutex);
wifi_.call<void>("release");
print_wifi();
}
void wifi_lock::print_multicast()
{
if (multicast_.call<jni::Bool>("isHeld"))
spdlog::info("MulticastLock acquired");
else
spdlog::info("MulticastLock released");
}
void wifi_lock::acquire_multicast()
{
std::lock_guard lock(mutex);
multicast_.call<void>("acquire");
print_multicast();
}
void wifi_lock::release_multicast()
{
std::lock_guard lock(mutex);
multicast_.call<void>("release");
print_multicast();
}
std::shared_ptr<void> wifi_lock::get_wifi_lock()
{
acquire_wifi();
// The shared pointer has a separate reference counter: destructor only calls a method
return std::shared_ptr<void>(this, [self = shared_from_this()](void *) { self->release_wifi(); });
}
std::shared_ptr<void> wifi_lock::get_multicast_lock()
{
acquire_multicast();
// The shared pointer has a separate reference counter: destructor only calls a method
return std::shared_ptr<void>(this, [self = shared_from_this()](void *) { self->release_multicast(); });
}
#else
std::shared_ptr<void> wifi_lock::get_wifi_lock()
{
return nullptr;
}
std::shared_ptr<void> wifi_lock::get_multicast_lock()
{
return nullptr;
}
#endif #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();
}
+26 -9
View File
@@ -18,19 +18,36 @@
*/ */
#pragma once #pragma once
#include <mutex> #include <memory>
class wifi_lock #ifdef __ANDROID__
#include "jnipp.h"
#include <mutex>
#endif
class wifi_lock : public std::enable_shared_from_this<wifi_lock>
{ {
#ifdef __ANDROID__
std::mutex mutex; std::mutex mutex;
int wants_multicast = 0; jni::object<"android/net/wifi/WifiManager$MulticastLock"> multicast_;
int wants_low_latency = 0; jni::object<"android/net/wifi/WifiManager$WifiLock"> wifi_;
bool enabled = true; wifi_lock(decltype(multicast_), decltype(wifi_));
void apply();
void print_wifi();
void acquire_wifi();
void release_wifi();
void print_multicast();
void acquire_multicast();
void release_multicast();
public: public:
static void set_enabled(bool enabled); static std::shared_ptr<wifi_lock> make_wifi_lock(jobject activity);
#endif
static void want_multicast(bool enabled); public:
static void want_low_latency(bool enabled); using multicast = std::shared_ptr<void>;
using wifi = std::shared_ptr<void>;
multicast get_multicast_lock();
wifi get_wifi_lock();
}; };