Suspend microphone when unused (pipewire only)

This commit is contained in:
Patrick Nicolas
2024-11-23 13:26:34 +01:00
parent b832c84d7b
commit d6db46db09
8 changed files with 59 additions and 14 deletions
+24 -10
View File
@@ -34,7 +34,7 @@ void wivrn::android::audio::exit()
speaker_stop_ack.wait(false); speaker_stop_ack.wait(false);
AAudioStream_requestStop(speaker); AAudioStream_requestStop(speaker);
} }
if (microphone) if (microphone and AAudioStream_getState(microphone) == AAUDIO_STREAM_STATE_STARTED)
{ {
microphone_stop_ack.wait(false); microphone_stop_ack.wait(false);
AAudioStream_requestStop(microphone); AAudioStream_requestStop(microphone);
@@ -161,15 +161,7 @@ void wivrn::android::audio::build_microphone(AAudioStreamBuilder * builder, int3
aaudio_result_t result = AAudioStreamBuilder_openStream(builder, &microphone); aaudio_result_t result = AAudioStreamBuilder_openStream(builder, &microphone);
if (result != AAUDIO_OK) if (result != AAUDIO_OK)
spdlog::error("Cannot create input stream: {}", AAudio_convertResultToText(result)); spdlog::error("Cannot create input stream: {}", AAudio_convertResultToText(result));
// Microphone is started manually by a tracking_control packet
result = AAudioStream_requestStart(microphone);
if (result == AAUDIO_OK)
spdlog::info("Microphone stream started");
else
{
AAudioStream_close(microphone);
spdlog::warn("Microphone stream failed to start: {}", AAudio_convertResultToText(result));
}
} }
void wivrn::android::audio::build_speaker(AAudioStreamBuilder * builder, int32_t sample_rate, int32_t num_channels) void wivrn::android::audio::build_speaker(AAudioStreamBuilder * builder, int32_t sample_rate, int32_t num_channels)
@@ -214,7 +206,18 @@ void wivrn::android::audio::recreate_stream(audio * self, AAudioStreamStruct * s
if (stream == self->speaker) if (stream == self->speaker)
self->build_speaker(builder, sample_rate, num_channels); self->build_speaker(builder, sample_rate, num_channels);
else if (stream == self->microphone) else if (stream == self->microphone)
{
self->build_microphone(builder, sample_rate, num_channels); self->build_microphone(builder, sample_rate, num_channels);
result = AAudioStream_requestStart(self->microphone);
if (result == AAUDIO_OK)
spdlog::info("Microphone stream started");
else
{
AAudioStream_close(self->microphone);
spdlog::warn("Microphone stream failed to start: {}", AAudio_convertResultToText(result));
}
}
else else
spdlog::error("Stream to recreate is neither speaker, not microphone!"); spdlog::error("Stream to recreate is neither speaker, not microphone!");
@@ -272,6 +275,17 @@ void wivrn::android::audio::operator()(wivrn::audio_data && data)
buffer_size_bytes.fetch_add(size); buffer_size_bytes.fetch_add(size);
} }
void wivrn::android::audio::set_mic_sate(bool running)
{
if (not microphone)
return;
if (running)
AAudioStream_requestStart(microphone);
else
AAudioStream_requestStop(microphone);
}
void wivrn::android::audio::get_audio_description(wivrn::from_headset::headset_info_packet & info) void wivrn::android::audio::get_audio_description(wivrn::from_headset::headset_info_packet & info)
{ {
AAudioStreamBuilder * builder; AAudioStreamBuilder * builder;
+2
View File
@@ -72,6 +72,8 @@ public:
void operator()(wivrn::audio_data &&); void operator()(wivrn::audio_data &&);
void set_mic_sate(bool running);
static void get_audio_description(wivrn::from_headset::headset_info_packet & info); static void get_audio_description(wivrn::from_headset::headset_info_packet & info);
}; };
} // namespace wivrn::android } // namespace wivrn::android
+1
View File
@@ -42,6 +42,7 @@ public:
~audio() = default; ~audio() = default;
void operator()(wivrn::audio_data &&) {} void operator()(wivrn::audio_data &&) {}
void set_mic_sate(bool running) {}
static void get_audio_description(wivrn::from_headset::headset_info_packet & info) {} static void get_audio_description(wivrn::from_headset::headset_info_packet & info) {}
}; };
+1 -1
View File
@@ -85,7 +85,7 @@ private:
std::atomic<bool> exiting = false; std::atomic<bool> exiting = false;
std::thread network_thread; std::thread network_thread;
std::mutex tracking_control_mutex; std::mutex tracking_control_mutex;
to_headset::tracking_control tracking_control; to_headset::tracking_control tracking_control{};
std::atomic<bool> recenter_requested = false; std::atomic<bool> recenter_requested = false;
std::atomic<XrDuration> display_time_phase = 0; std::atomic<XrDuration> display_time_phase = 0;
std::atomic<XrDuration> display_time_period = 0; std::atomic<XrDuration> display_time_period = 0;
+4
View File
@@ -352,5 +352,9 @@ void scenes::stream::tracking()
void scenes::stream::operator()(to_headset::tracking_control && packet) void scenes::stream::operator()(to_headset::tracking_control && packet)
{ {
std::lock_guard lock(tracking_control_mutex); std::lock_guard lock(tracking_control_mutex);
auto m = size_t(to_headset::tracking_control::id::microphone);
if (tracking_control.enabled[m] != packet.enabled[m] and audio_handle)
audio_handle->set_mic_sate(packet.enabled[m]);
tracking_control = packet; tracking_control = packet;
} }
+3 -2
View File
@@ -38,7 +38,7 @@ namespace wivrn
// Default port for server to listen, both TCP and UDP // Default port for server to listen, both TCP and UDP
static const int default_port = 9757; static const int default_port = 9757;
static constexpr int protocol_revision = 0; static constexpr int protocol_revision = 1;
enum class device_id : uint8_t enum class device_id : uint8_t
{ {
@@ -396,8 +396,9 @@ struct tracking_control
right_hand, right_hand,
face, face,
battery, battery,
microphone,
last = battery, last = microphone,
}; };
std::chrono::nanoseconds offset; std::chrono::nanoseconds offset;
std::array<bool, size_t(id::last) + 1> enabled; std::array<bool, size_t(id::last) + 1> enabled;
+23 -1
View File
@@ -22,6 +22,7 @@
#include "os/os_time.h" #include "os/os_time.h"
#include "util/u_logging.h" #include "util/u_logging.h"
#include "utils/ring_buffer.h" #include "utils/ring_buffer.h"
#include <magic_enum.hpp>
#include <memory> #include <memory>
#include <pipewire/pipewire.h> #include <pipewire/pipewire.h>
#include <spa/param/audio/format-utils.h> #include <spa/param/audio/format-utils.h>
@@ -62,6 +63,7 @@ struct pipewire_device : public audio_device
std::unique_ptr<pw_stream, deleter> microphone; std::unique_ptr<pw_stream, deleter> microphone;
pw_stream_events mic_events{ pw_stream_events mic_events{
.version = PW_VERSION_STREAM_EVENTS, .version = PW_VERSION_STREAM_EVENTS,
.state_changed = &pipewire_device::mic_state_changed,
.process = &pipewire_device::mic_process, .process = &pipewire_device::mic_process,
}; };
std::jthread thread; std::jthread thread;
@@ -73,6 +75,7 @@ struct pipewire_device : public audio_device
static void speaker_process(void * self_v); static void speaker_process(void * self_v);
static void mic_process(void * self_v); static void mic_process(void * self_v);
static void mic_state_changed(void * self_v, pw_stream_state old, pw_stream_state state, const char * error);
void process_mic_data(wivrn::audio_data &&) override; void process_mic_data(wivrn::audio_data &&) override;
@@ -168,7 +171,6 @@ struct pipewire_device : public audio_device
NULL), NULL),
&mic_events, &mic_events,
this)); this));
std::vector<uint8_t> buffer(1024); std::vector<uint8_t> buffer(1024);
spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer.data(), uint32_t(buffer.size())); spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer.data(), uint32_t(buffer.size()));
@@ -203,6 +205,7 @@ struct pipewire_device : public audio_device
void pipewire_device::mic_process(void * self_v) void pipewire_device::mic_process(void * self_v)
{ {
// std::cerr << "mic_process" << std::endl;
auto self = (pipewire_device *)self_v; auto self = (pipewire_device *)self_v;
auto buffer = pw_stream_dequeue_buffer(self->microphone.get()); auto buffer = pw_stream_dequeue_buffer(self->microphone.get());
if (not buffer) if (not buffer)
@@ -269,6 +272,25 @@ void pipewire_device::mic_process(void * self_v)
} }
} }
void pipewire_device::mic_state_changed(void * self_v, pw_stream_state old, pw_stream_state state, const char * error)
{
auto self = (pipewire_device *)self_v;
switch (state)
{
case PW_STREAM_STATE_ERROR:
U_LOG_W("Error on microphone stream: %s", error);
return;
case PW_STREAM_STATE_UNCONNECTED:
case PW_STREAM_STATE_CONNECTING:
case PW_STREAM_STATE_PAUSED:
self->session.set_enabled(to_headset::tracking_control::id::microphone, false);
return;
case PW_STREAM_STATE_STREAMING:
self->session.set_enabled(to_headset::tracking_control::id::microphone, true);
return;
}
}
void pipewire_device::speaker_process(void * self_v) void pipewire_device::speaker_process(void * self_v)
{ {
auto self = (pipewire_device *)self_v; auto self = (pipewire_device *)self_v;
+1
View File
@@ -426,6 +426,7 @@ struct pulse_device : public audio_device
if (not mic_pipe) if (not mic_pipe)
throw std::system_error(errno, std::system_category(), "failed to open mic pipe " + microphone->socket.string()); throw std::system_error(errno, std::system_category(), "failed to open mic pipe " + microphone->socket.string());
mic_thread = std::thread([this]() { run_mic(); }); mic_thread = std::thread([this]() { run_mic(); });
session.set_enabled(to_headset::tracking_control::id::microphone, true);
} }
if (info.speaker) if (info.speaker)