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);
AAudioStream_requestStop(speaker);
}
if (microphone)
if (microphone and AAudioStream_getState(microphone) == AAUDIO_STREAM_STATE_STARTED)
{
microphone_stop_ack.wait(false);
AAudioStream_requestStop(microphone);
@@ -161,15 +161,7 @@ void wivrn::android::audio::build_microphone(AAudioStreamBuilder * builder, int3
aaudio_result_t result = AAudioStreamBuilder_openStream(builder, &microphone);
if (result != AAUDIO_OK)
spdlog::error("Cannot create input stream: {}", AAudio_convertResultToText(result));
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));
}
// Microphone is started manually by a tracking_control packet
}
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)
self->build_speaker(builder, sample_rate, num_channels);
else if (stream == self->microphone)
{
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
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);
}
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)
{
AAudioStreamBuilder * builder;
+2
View File
@@ -72,6 +72,8 @@ public:
void operator()(wivrn::audio_data &&);
void set_mic_sate(bool running);
static void get_audio_description(wivrn::from_headset::headset_info_packet & info);
};
} // namespace wivrn::android
+1
View File
@@ -42,6 +42,7 @@ public:
~audio() = default;
void operator()(wivrn::audio_data &&) {}
void set_mic_sate(bool running) {}
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::thread network_thread;
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<XrDuration> display_time_phase = 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)
{
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;
}