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) :
app_info(std::move(info))
{
#ifdef __ANDROID__
// 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
PFN_xrInitializeLoaderKHR initializeLoader = nullptr;
if (XR_SUCCEEDED(xrGetInstanceProcAddr(XR_NULL_HANDLE, "xrInitializeLoaderKHR", (PFN_xrVoidFunction *)(&initializeLoader))))
@@ -1050,6 +1057,8 @@ void application::cleanup()
i->spaces_by_name.clear();
}
wifi.reset();
#ifdef __ANDROID__
jni::jni_thread::detach();
#endif
@@ -1315,7 +1324,6 @@ void application::session_state_changed(XrSessionState new_state, XrTime timesta
case XR_SESSION_STATE_FOCUSED:
session_visible = true;
session_focused = true;
wifi_lock::set_enabled(true);
break;
case XR_SESSION_STATE_STOPPING:
@@ -1323,7 +1331,6 @@ void application::session_state_changed(XrSessionState new_state, XrTime timesta
session_focused = false;
xr_session.end_session();
session_running = false;
wifi_lock::set_enabled(false);
break;
case XR_SESSION_STATE_EXITING:
+8 -2
View File
@@ -20,6 +20,7 @@
#pragma once
#include "configuration.h"
#include "wifi_lock.h"
#include "xr/fb_face_tracker2.h"
#include "xr/hand_tracker.h"
#ifdef __ANDROID__
@@ -143,6 +144,8 @@ class application : public singleton<application>
std::filesystem::path config_path;
std::filesystem::path cache_path;
std::shared_ptr<wifi_lock> wifi;
std::string server_address;
std::mutex scene_stack_lock;
@@ -182,8 +185,6 @@ public:
#ifdef __ANDROID__
void setup_jni();
void set_wifi_locks(bool enabled);
static AAssetManager * asset_manager()
{
return instance().app_info.native_app->activity->assetManager;
@@ -317,6 +318,11 @@ public:
return instance().eye_gaze_space;
};
static wifi_lock & get_wifi_lock()
{
return *instance().wifi;
}
static void ignore_debug_reports_for(void * object)
{
#ifndef NDEBUG
+2 -3
View File
@@ -29,7 +29,6 @@
#include "stream.h"
#include "utils/contains.h"
#include "version.h"
#include "wifi_lock.h"
#include "wivrn_client.h"
#include "xr/passthrough.h"
#include <glm/gtc/matrix_access.hpp>
@@ -744,7 +743,7 @@ void scenes::lobby::on_focused()
about_picture = imgui_ctx->load_texture("wivrn.png");
}
setup_passthrough();
wifi_lock::want_multicast(true);
multicast = application::get_wifi_lock().get_multicast_lock();
}
void scenes::lobby::setup_passthrough()
@@ -792,7 +791,7 @@ void scenes::lobby::on_unfocused()
swapchains_controllers.clear();
swapchain_imgui = xr::swapchain();
passthrough.emplace<std::monostate>();
wifi_lock::want_multicast(false);
multicast.reset();
}
void scenes::lobby::on_session_state_changed(XrSessionState state)
+2
View File
@@ -24,6 +24,7 @@
#include "render/scene_data.h"
#include "scene.h"
#include "scenes/hand_model.h"
#include "wifi_lock.h"
#include "wivrn_discover.h"
#include <vulkan/vulkan_raii.hpp>
@@ -46,6 +47,7 @@ class stream;
class lobby : public scene_impl<lobby>
{
std::optional<wivrn_discover> discover;
wifi_lock::multicast multicast;
char add_server_window_prettyname[200];
char add_server_window_hostname[200];
+2 -3
View File
@@ -30,7 +30,6 @@
#include "utils/ranges.h"
#include "vk/pipeline.h"
#include "vk/shader.h"
#include "wifi_lock.h"
#include "wivrn_packets.h"
#include <algorithm>
#include <mutex>
@@ -215,7 +214,7 @@ void scenes::stream::on_focused()
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);
setup_reprojection_swapchain();
}
@@ -224,7 +223,7 @@ void scenes::stream::on_unfocused()
{
imgui_ctx.reset();
swapchain_imgui = xr::swapchain();
wifi_lock::want_low_latency(false);
wifi.reset();
}
scenes::stream::~stream()
+3
View File
@@ -24,6 +24,7 @@
#include "render/imgui_impl.h"
#include "scene.h"
#include "stream_reprojection.h"
#include "wifi_lock.h"
#include "wivrn_client.h"
#include "wivrn_packets.h"
#include <mutex>
@@ -62,6 +63,8 @@ private:
std::vector<uint64_t> frames() const;
};
wifi_lock::wifi wifi;
// for frames inside accumulator images
std::mutex frames_mutex;
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 "spdlog/spdlog.h"
#ifdef __ANDROID__
#include "application.h"
#include "spdlog/spdlog.h"
#include <android/native_activity.h>
#include <sys/system_properties.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::string lock_name("WiVRn");
@@ -47,51 +42,78 @@ void wifi_lock::apply()
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
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 lock = system_service.call<jni::object<"android/net/wifi/WifiManager$MulticastLock">>("createMulticastLock", lock_name);
lock.call<void>("setReferenceCounted", jni::Bool(false));
lock.call<void>((enabled and wants_multicast) ? "acquire" : "release");
if (lock.call<jni::Bool>("isHeld"))
{
spdlog::info("MulticastLock acquired");
}
else
{
spdlog::info("MulticastLock is not acquired");
}
return std::shared_ptr<wifi_lock>(new wifi_lock(
system_service.call<jni::object<"android/net/wifi/WifiManager$MulticastLock">>("createMulticastLock", lock_name),
system_service.call<jni::object<"android/net/wifi/WifiManager$WifiLock">>(
"createWifiLock",
jni::Int(api_level >= 29 ? 4 /*WIFI_MODE_FULL_LOW_LATENCY*/ : 3 /*WIFI_MODE_FULL_HIGH_PERF*/),
lock_name)));
}
auto wifi_lock = system_service.call<jni::object<"android/net/wifi/WifiManager$WifiLock">>(
"createWifiLock",
jni::Int(api_level >= 29 ? 4 /*WIFI_MODE_FULL_LOW_LATENCY*/ : 3 /*WIFI_MODE_FULL_HIGH_PERF*/),
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"))
{
void wifi_lock::print_wifi()
{
if (wifi_.call<jni::Bool>("isHeld"))
spdlog::info("WifiLock low latency acquired");
}
else
{
spdlog::info("WifiLock low latency is not acquired");
}
spdlog::info("WifiLock low latency released");
}
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
}
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
#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;
int wants_multicast = 0;
int wants_low_latency = 0;
bool enabled = true;
void apply();
jni::object<"android/net/wifi/WifiManager$MulticastLock"> multicast_;
jni::object<"android/net/wifi/WifiManager$WifiLock"> wifi_;
wifi_lock(decltype(multicast_), decltype(wifi_));
void print_wifi();
void acquire_wifi();
void release_wifi();
void print_multicast();
void acquire_multicast();
void release_multicast();
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);
static void want_low_latency(bool enabled);
public:
using multicast = std::shared_ptr<void>;
using wifi = std::shared_ptr<void>;
multicast get_multicast_lock();
wifi get_wifi_lock();
};