Enable microphone support

This commit is contained in:
Patrick Nicolas
2024-03-15 15:17:35 +01:00
parent c006e6c31b
commit f2dc24c316
11 changed files with 112 additions and 35 deletions
+5
View File
@@ -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()
+4 -1
View File
@@ -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<int>(
(format.sample_rate * 10) / 1000, // 10ms
1400 / frame_size);
auto & buffer = packet.data.c;
buffer.resize(frame_size * max_frames);
+55 -27
View File
@@ -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 <typename... Args>
@@ -106,6 +119,21 @@ auto handle(const T & t, Args &&... args)
{
return std::tuple_cat(std::make_tuple(t.handle()), handle(std::forward<Args>(args)...));
}
template <typename T>
struct type_map
{
using type = T;
};
template <>
struct type_map<void>
{
using type = jni::Void;
};
template <typename T>
using type_map_t = type_map<T>::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 <typename T>
@@ -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 <typename T>
struct type_map
{
using type = T;
};
template <>
struct type_map<void>
{
using type = jni::Void;
};
template <typename T>
using type_map_t = type_map<T>::type;
}; // namespace details
template <details::string_literal Type>
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>(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 <typename T>
struct array
{
static std::string type()
{
return std::string("[") + T::type();
}
std::unique_ptr<std::remove_pointer_t<jobjectArray>, 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
+1 -1
View File
@@ -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();
+6 -1
View File
@@ -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();
}
+1
View File
@@ -113,6 +113,7 @@ class lobby : public scene_impl<lobby>
float preferred_refresh_rate = 0;
bool show_performance_metrics = false;
bool microphone = false;
void gui_connecting();
void gui_server_list();
+31
View File
@@ -29,6 +29,10 @@
#include <spdlog/fmt/fmt.h>
#include <utils/strings.h>
#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", &microphone))
{
#ifdef __ANDROID__
if (microphone)
{
jni::object<""> act(application::native_app()->activity->clazz);
auto app = act.call<jni::object<"android/app/Application">>("getApplication");
auto ctx = app.call<jni::object<"android/content/Context">>("getApplicationContext");
jni::string permission("android.permission.RECORD_AUDIO");
auto result = ctx.call<jni::Int>("checkSelfPermission", permission);
if (result != 0 /*PERMISSION_GRANTED*/)
{
spdlog::info("RECORD_AUDIO permission not granted, requesting it");
jni::array permissions(permission);
act.call<void>("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();
+3 -1
View File
@@ -74,7 +74,7 @@ static const std::array supported_formats =
vk::Format::eB8G8R8A8Srgb
};
std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_session> network_session, bool show_performance_metrics)
std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_session> network_session, bool show_performance_metrics, bool enable_microphone)
{
std::shared_ptr<stream> self{new stream};
spdlog::info("decoder_mutex.native_handle() = {}", (void*)self->decoder_mutex.native_handle());
@@ -112,6 +112,8 @@ std::shared_ptr<scenes::stream> scenes::stream::create(std::unique_ptr<wivrn_ses
info.hand_tracking = application::get_hand_tracking_supported();
audio::get_audio_description(info);
if (not enable_microphone)
info.microphone = {};
self->network_session->send_control(info);
+1 -1
View File
@@ -117,7 +117,7 @@ private:
public:
~stream();
static std::shared_ptr<stream> create(std::unique_ptr<wivrn_session> session, bool show_performance_metrics);
static std::shared_ptr<stream> create(std::unique_ptr<wivrn_session> session, bool show_performance_metrics, bool enable_microphone);
void render(XrTime predicted_display_time, bool should_render) override;
void on_focused() override;
+4 -2
View File
@@ -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)
+1 -1
View File
@@ -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,