diff --git a/client/application.h b/client/application.h index 9467a211..9d1aa0b6 100644 --- a/client/application.h +++ b/client/application.h @@ -168,6 +168,11 @@ public: { return instance().app_info.native_app->activity->assetManager; } + + static android_app* native_app() + { + return instance().app_info.native_app; + } #endif static bool is_session_running() diff --git a/client/audio/android/audio.cpp b/client/audio/android/audio.cpp index 19871ba7..32735073 100644 --- a/client/audio/android/audio.cpp +++ b/client/audio/android/audio.cpp @@ -102,7 +102,10 @@ void wivrn::android::audio::input(AAudioStream * stream, const xrt::drivers::wiv xrt::drivers::wivrn::audio_data packet; const int frame_size = format.num_channels * sizeof(int16_t); - const int max_frames = (format.sample_rate * 10) / 1000; // 10ms + // Try to make packets fit in a single TCP frame, and at most 10ms data + const int max_frames = std::min( + (format.sample_rate * 10) / 1000, // 10ms + 1400 / frame_size); auto & buffer = packet.data.c; buffer.resize(frame_size * max_frames); diff --git a/client/jnipp.h b/client/jnipp.h index cc479da3..8d0090ea 100644 --- a/client/jnipp.h +++ b/client/jnipp.h @@ -69,6 +69,15 @@ public: } }; +struct Void +{ + static std::string type() + { + return "V"; + } + constexpr static const auto call_method = &_JNIEnv::CallVoidMethod; +}; + namespace details { @@ -78,6 +87,10 @@ struct deleter { jni_thread::env().DeleteLocalRef(j); } + void operator()(jobjectArray j) + { + jni_thread::env().DeleteLocalRef(j); + } }; template @@ -106,6 +119,21 @@ auto handle(const T & t, Args &&... args) { return std::tuple_cat(std::make_tuple(t.handle()), handle(std::forward(args)...)); } + +template +struct type_map +{ + using type = T; +}; + +template <> +struct type_map +{ + using type = jni::Void; +}; + +template +using type_map_t = type_map::type; } // namespace details struct klass @@ -122,11 +150,13 @@ struct klass { auto & env = jni_thread::env(); self.reset(env.GetObjectClass(instance)); + assert(self.get()); } klass(const char * name) : self(jni_thread::env().FindClass(name)) { + assert(self.get()); } template @@ -138,15 +168,6 @@ struct klass } }; -struct Void -{ - static std::string type() - { - return "V"; - } - constexpr static const auto call_method = &_JNIEnv::CallVoidMethod; -}; - struct Bool { static std::string type() @@ -171,6 +192,7 @@ struct Bool struct Int { constexpr static auto static_field = &_JNIEnv::GetStaticIntField; + constexpr static const auto call_method = &_JNIEnv::CallIntMethod; static std::string type() { @@ -190,24 +212,6 @@ struct Int } }; -namespace details -{ -template -struct type_map -{ - using type = T; -}; - -template <> -struct type_map -{ - using type = jni::Void; -}; - -template -using type_map_t = type_map::type; -}; // namespace details - template struct object { @@ -233,6 +237,7 @@ struct object auto & env = jni_thread::env(); std::string signature = "(" + details::build_type(args...) + ")" + R1::type(); auto method_id = env.GetMethodID(klass(), method, signature.c_str()); + assert(method_id); auto handles = details::handle(std::forward(args)...); return R(std::apply([&](auto &... t) { return (env.*R1::call_method)(*this, method_id, t...); @@ -282,4 +287,27 @@ struct string : public string_t return res; } }; + +template +struct array +{ + static std::string type() + { + return std::string("[") + T::type(); + } + + std::unique_ptr, details::deleter> self; + + auto handle() const + { + return self.get(); + } + + array(T& element) + { + auto & env = jni_thread::env(); + self.reset(env.NewObjectArray(1, element.klass(), element)); + } +}; + } // namespace jni diff --git a/client/main.cpp b/client/main.cpp index 66c0f0fa..c21e4280 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -103,7 +103,7 @@ void real_main() freeaddrinfo(addresses); if (not session) throw std::runtime_error("Unable to connect to " + server_address + ":" + std::to_string(port)); - app.push_scene(scenes::stream::create(std::move(session), false)); + app.push_scene(scenes::stream::create(std::move(session), false, true)); } app.run(); diff --git a/client/scenes/lobby.cpp b/client/scenes/lobby.cpp index bffdca2b..6a76cf00 100644 --- a/client/scenes/lobby.cpp +++ b/client/scenes/lobby.cpp @@ -219,6 +219,10 @@ scenes::lobby::lobby() preferred_refresh_rate = rate.get_double(); session.set_refresh_rate(preferred_refresh_rate); } + + auto mic = root["microphone"]; + if (mic.is_bool()) + microphone = mic.get_bool(); } catch(std::exception& e) { @@ -287,6 +291,7 @@ void scenes::lobby::save_config() << "\"show_performance_metrics\":" << std::boolalpha << show_performance_metrics; if (preferred_refresh_rate != 0.) json << ",\"preferred_refresh_rate\":" << preferred_refresh_rate ; + json << ",\"microphone\":" << std::boolalpha << microphone; json << "}"; } @@ -528,7 +533,7 @@ void scenes::lobby::render(XrTime predicted_display_time, bool should_render) { auto session = async_session.get(); if (session) - next_scene = stream::create(std::move(session), show_performance_metrics); + next_scene = stream::create(std::move(session), show_performance_metrics, microphone); async_session.reset(); } diff --git a/client/scenes/lobby.h b/client/scenes/lobby.h index a2461468..0e129bd4 100644 --- a/client/scenes/lobby.h +++ b/client/scenes/lobby.h @@ -113,6 +113,7 @@ class lobby : public scene_impl float preferred_refresh_rate = 0; bool show_performance_metrics = false; + bool microphone = false; void gui_connecting(); void gui_server_list(); diff --git a/client/scenes/lobby_gui.cpp b/client/scenes/lobby_gui.cpp index 64712132..8ec78ada 100644 --- a/client/scenes/lobby_gui.cpp +++ b/client/scenes/lobby_gui.cpp @@ -29,6 +29,10 @@ #include #include +#ifdef __ANDROID__ +#include "jnipp.h" +#endif + #include "IconsFontAwesome6.h" static void CenterTextH(const std::string& text) @@ -352,6 +356,33 @@ void scenes::lobby::gui_settings() } } + if (ImGui::Checkbox("Enable microphone", µphone)) + { +#ifdef __ANDROID__ + if (microphone) + { + jni::object<""> act(application::native_app()->activity->clazz); + auto app = act.call>("getApplication"); + auto ctx = app.call>("getApplicationContext"); + + jni::string permission("android.permission.RECORD_AUDIO"); + auto result = ctx.call("checkSelfPermission", permission); + if (result != 0 /*PERMISSION_GRANTED*/) + { + spdlog::info("RECORD_AUDIO permission not granted, requesting it"); + jni::array permissions(permission); + act.call("requestPermissions", permissions, jni::Int(0)); + } + else + { + spdlog::info("RECORD_AUDIO permission already granted"); + } + } +#endif + save_config(); + } + vibrate_on_hover(); + if (ImGui::Checkbox("Show performance metrics", &show_performance_metrics)) save_config(); vibrate_on_hover(); diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index b1b3b1d9..fbc9dbcd 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -74,7 +74,7 @@ static const std::array supported_formats = vk::Format::eB8G8R8A8Srgb }; -std::shared_ptr scenes::stream::create(std::unique_ptr network_session, bool show_performance_metrics) +std::shared_ptr scenes::stream::create(std::unique_ptr network_session, bool show_performance_metrics, bool enable_microphone) { std::shared_ptr self{new stream}; spdlog::info("decoder_mutex.native_handle() = {}", (void*)self->decoder_mutex.native_handle()); @@ -112,6 +112,8 @@ std::shared_ptr scenes::stream::create(std::unique_ptrnetwork_session->send_control(info); diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 351ffc87..5ebf3fdb 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -117,7 +117,7 @@ private: public: ~stream(); - static std::shared_ptr create(std::unique_ptr session, bool show_performance_metrics); + static std::shared_ptr create(std::unique_ptr session, bool show_performance_metrics, bool enable_microphone); void render(XrTime predicted_display_time, bool should_render) override; void on_focused() override; diff --git a/server/driver/audio_pulse.cpp b/server/driver/audio_pulse.cpp index 45a3dc67..9654b72e 100644 --- a/server/driver/audio_pulse.cpp +++ b/server/driver/audio_pulse.cpp @@ -169,7 +169,6 @@ module_entry ensure_source(pa_context * ctx, const char * name, const std::strin << " file=" << std::quoted(fifo.string()) << " channels=" << channels << " rate=" << sample_rate - << " use_system_clock_for_timing=yes" << " source_properties=" << PA_PROP_DEVICE_DESCRIPTION << "=" << std::quoted(description) << PA_PROP_DEVICE_ICON_NAME << "=network-wireless"; ; @@ -181,6 +180,9 @@ module_entry ensure_source(pa_context * ctx, const char * name, const std::strin if (not source) throw std::runtime_error("failed to create audio source " + std::string(name)); source->socket = fifo; + + add_cleanup_function(unload_module, source->module); + return *source; } @@ -360,7 +362,7 @@ struct pulse_device : public audio_device { pollfd pfd{}; pfd.fd = mic_pipe.get_fd(); - pfd.events = POLL_OUT; + pfd.events = POLLOUT; int r = poll(&pfd, 1, 100); if (r < 0) diff --git a/server/driver/wivrn_session.cpp b/server/driver/wivrn_session.cpp index a7cbe9a8..d557ad7c 100644 --- a/server/driver/wivrn_session.cpp +++ b/server/driver/wivrn_session.cpp @@ -102,7 +102,7 @@ xrt_result_t xrt::drivers::wivrn::wivrn_session::create_session(xrt::drivers::wi { self->audio_handle = audio_device::create( "wivrn.source", - "WiVRn microphone", + "WiVRn(microphone)", "wivrn.sink", "WiVRn", info,