diff --git a/client/application.cpp b/client/application.cpp index 6c0bd94b..7bce061b 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -935,6 +935,7 @@ std::pair 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(); +#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: diff --git a/client/application.h b/client/application.h index a66b0f71..6a7efd05 100644 --- a/client/application.h +++ b/client/application.h @@ -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 std::filesystem::path config_path; std::filesystem::path cache_path; + std::shared_ptr 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 diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index c564318a..14eb95be 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -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 @@ -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(); - wifi_lock::want_multicast(false); + multicast.reset(); } void scenes::lobby::on_session_state_changed(XrSessionState state) diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index 55fe7263..d17fbca3 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -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 @@ -46,6 +47,7 @@ class stream; class lobby : public scene_impl { std::optional discover; + wifi_lock::multicast multicast; char add_server_window_prettyname[200]; char add_server_window_hostname[200]; diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 082914ac..f1c2b31d 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -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 #include @@ -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() diff --git a/client/scenes/stream.h b/client/scenes/stream.h index a8fca2eb..ac7e6732 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -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 @@ -62,6 +63,8 @@ private: std::vector frames() const; }; + wifi_lock::wifi wifi; + // for frames inside accumulator images std::mutex frames_mutex; std::vector> common_frame(XrTime display_time); diff --git a/client/wifi_lock.cpp b/client/wifi_lock.cpp index 6972825f..ee75c336 100644 --- a/client/wifi_lock.cpp +++ b/client/wifi_lock.cpp @@ -18,25 +18,20 @@ */ #include "wifi_lock.h" -#include "spdlog/spdlog.h" #ifdef __ANDROID__ #include "application.h" +#include "spdlog/spdlog.h" #include #include #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::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>("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"); - } + return std::shared_ptr(new wifi_lock( + system_service.call>("createMulticastLock", lock_name), + system_service.call>( + "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>( - "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")) - { +void wifi_lock::print_wifi() +{ + if (wifi_.call("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("acquire"); + print_wifi(); +} +void wifi_lock::release_wifi() +{ + std::lock_guard lock(mutex); + wifi_.call("release"); + print_wifi(); +} + +void wifi_lock::print_multicast() +{ + if (multicast_.call("isHeld")) + spdlog::info("MulticastLock acquired"); + else + spdlog::info("MulticastLock released"); +} + +void wifi_lock::acquire_multicast() +{ + std::lock_guard lock(mutex); + multicast_.call("acquire"); + print_multicast(); +} +void wifi_lock::release_multicast() +{ + std::lock_guard lock(mutex); + multicast_.call("release"); + print_multicast(); +} + +std::shared_ptr wifi_lock::get_wifi_lock() +{ + acquire_wifi(); + // The shared pointer has a separate reference counter: destructor only calls a method + return std::shared_ptr(this, [self = shared_from_this()](void *) { self->release_wifi(); }); +} +std::shared_ptr wifi_lock::get_multicast_lock() +{ + acquire_multicast(); + // The shared pointer has a separate reference counter: destructor only calls a method + return std::shared_ptr(this, [self = shared_from_this()](void *) { self->release_multicast(); }); +} + +#else + +std::shared_ptr wifi_lock::get_wifi_lock() +{ + return nullptr; +} +std::shared_ptr 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(); -} diff --git a/client/wifi_lock.h b/client/wifi_lock.h index c754ac2d..f59c0225 100644 --- a/client/wifi_lock.h +++ b/client/wifi_lock.h @@ -18,19 +18,36 @@ */ #pragma once -#include +#include -class wifi_lock +#ifdef __ANDROID__ +#include "jnipp.h" +#include +#endif + +class wifi_lock : public std::enable_shared_from_this { +#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 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; + using wifi = std::shared_ptr; + + multicast get_multicast_lock(); + wifi get_wifi_lock(); };