Safer threads for audio recreate
This commit is contained in:
@@ -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 and AAudioStream_getState(microphone) == AAUDIO_STREAM_STATE_STARTED)
|
if (microphone and mic_running)
|
||||||
{
|
{
|
||||||
microphone_stop_ack.wait(false);
|
microphone_stop_ack.wait(false);
|
||||||
AAudioStream_requestStop(microphone);
|
AAudioStream_requestStop(microphone);
|
||||||
@@ -122,6 +122,7 @@ int32_t wivrn::android::audio::microphone_data_cb(AAudioStream * stream, void *
|
|||||||
{
|
{
|
||||||
self->microphone_stop_ack = true;
|
self->microphone_stop_ack = true;
|
||||||
self->microphone_stop_ack.notify_all();
|
self->microphone_stop_ack.notify_all();
|
||||||
|
self->mic_running = false;
|
||||||
return AAUDIO_CALLBACK_RESULT_STOP;
|
return AAUDIO_CALLBACK_RESULT_STOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +139,7 @@ int32_t wivrn::android::audio::microphone_data_cb(AAudioStream * stream, void *
|
|||||||
{
|
{
|
||||||
self->microphone_stop_ack = true;
|
self->microphone_stop_ack = true;
|
||||||
self->microphone_stop_ack.notify_all();
|
self->microphone_stop_ack.notify_all();
|
||||||
|
self->mic_running = false;
|
||||||
return AAUDIO_CALLBACK_RESULT_STOP;
|
return AAUDIO_CALLBACK_RESULT_STOP;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,59 +191,60 @@ void wivrn::android::audio::build_speaker(AAudioStreamBuilder * builder, int32_t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wivrn::android::audio::recreate_stream(audio * self, AAudioStreamStruct * stream)
|
void wivrn::android::audio::recreate_stream(AAudioStreamStruct * stream)
|
||||||
{
|
{
|
||||||
size_t num_channels = AAudioStream_getChannelCount(stream);
|
if (exiting)
|
||||||
size_t sample_rate = AAudioStream_getSampleRate(stream);
|
return;
|
||||||
|
std::unique_lock lock(mutex);
|
||||||
|
|
||||||
AAudioStream_requestStop(stream);
|
if (recreate_thread.joinable())
|
||||||
AAudioStream_close(stream);
|
|
||||||
|
|
||||||
AAudioStreamBuilder * builder;
|
|
||||||
aaudio_result_t result = AAudio_createStreamBuilder(&builder);
|
|
||||||
if (result != AAUDIO_OK)
|
|
||||||
throw std::runtime_error(std::string("Cannot create stream builder: ") + AAudio_convertResultToText(result));
|
|
||||||
|
|
||||||
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);
|
assert(recreate_thread.get_id() != std::this_thread::get_id());
|
||||||
|
recreate_thread.join();
|
||||||
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!");
|
|
||||||
|
|
||||||
AAudioStreamBuilder_delete(builder);
|
recreate_thread = utils::named_thread("recreate_audio", [=, this]() {
|
||||||
|
std::unique_lock lock(mutex);
|
||||||
|
size_t num_channels = AAudioStream_getChannelCount(stream);
|
||||||
|
size_t sample_rate = AAudioStream_getSampleRate(stream);
|
||||||
|
|
||||||
|
AAudioStream_requestStop(stream);
|
||||||
|
AAudioStream_close(stream);
|
||||||
|
|
||||||
|
AAudioStreamBuilder * builder;
|
||||||
|
aaudio_result_t result = AAudio_createStreamBuilder(&builder);
|
||||||
|
if (result != AAUDIO_OK)
|
||||||
|
spdlog::error("Cannot create stream builder: {}", AAudio_convertResultToText(result));
|
||||||
|
|
||||||
|
if (stream == speaker)
|
||||||
|
build_speaker(builder, sample_rate, num_channels);
|
||||||
|
else if (stream == microphone)
|
||||||
|
build_microphone(builder, sample_rate, num_channels);
|
||||||
|
else
|
||||||
|
spdlog::error("Stream to recreate is neither speaker, not microphone!");
|
||||||
|
|
||||||
|
AAudioStreamBuilder_delete(builder);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void wivrn::android::audio::speaker_error_cb(AAudioStream * stream, void * userdata, aaudio_result_t error)
|
void wivrn::android::audio::speaker_error_cb(AAudioStream * stream, void * userdata, aaudio_result_t error)
|
||||||
{
|
{
|
||||||
auto self = (wivrn::android::audio *)userdata;
|
auto self = (wivrn::android::audio *)userdata;
|
||||||
spdlog::warn("Speaker stream interrupted: {}", AAudio_convertResultToText(error));
|
spdlog::warn("Speaker stream interrupted: {}", AAudio_convertResultToText(error));
|
||||||
std::thread recreate_thread = utils::named_thread("recreate_stream", &self->recreate_stream, self, stream);
|
self->recreate_stream(stream);
|
||||||
recreate_thread.detach();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wivrn::android::audio::microphone_error_cb(AAudioStream * stream, void * userdata, aaudio_result_t error)
|
void wivrn::android::audio::microphone_error_cb(AAudioStream * stream, void * userdata, aaudio_result_t error)
|
||||||
{
|
{
|
||||||
auto self = (wivrn::android::audio *)userdata;
|
auto self = (wivrn::android::audio *)userdata;
|
||||||
spdlog::warn("Microphone stream interrupted: {}", AAudio_convertResultToText(error));
|
spdlog::warn("Microphone stream interrupted: {}", AAudio_convertResultToText(error));
|
||||||
std::thread recreate_thread = utils::named_thread("recreate_stream", &self->recreate_stream, self, stream);
|
self->recreate_stream(stream);
|
||||||
recreate_thread.detach();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wivrn::android::audio::audio(const wivrn::to_headset::audio_stream_description & desc, wivrn_session & session, xr::instance & instance) :
|
wivrn::android::audio::audio(const wivrn::to_headset::audio_stream_description & desc, wivrn_session & session, xr::instance & instance) :
|
||||||
session(session), instance(instance)
|
session(session), instance(instance)
|
||||||
{
|
{
|
||||||
|
std::unique_lock lock(mutex);
|
||||||
AAudioStreamBuilder * builder;
|
AAudioStreamBuilder * builder;
|
||||||
aaudio_result_t result = AAudio_createStreamBuilder(&builder);
|
aaudio_result_t result = AAudio_createStreamBuilder(&builder);
|
||||||
if (result != AAUDIO_OK)
|
if (result != AAUDIO_OK)
|
||||||
@@ -265,6 +268,10 @@ wivrn::android::audio::~audio()
|
|||||||
if (stream)
|
if (stream)
|
||||||
AAudioStream_close(stream);
|
AAudioStream_close(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_lock lock(mutex);
|
||||||
|
if (recreate_thread.joinable())
|
||||||
|
recreate_thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
void wivrn::android::audio::operator()(wivrn::audio_data && data)
|
void wivrn::android::audio::operator()(wivrn::audio_data && data)
|
||||||
|
|||||||
@@ -22,6 +22,8 @@
|
|||||||
#include "utils/ring_buffer.h"
|
#include "utils/ring_buffer.h"
|
||||||
#include "wivrn_packets.h"
|
#include "wivrn_packets.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <mutex>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
struct AAudioStreamStruct;
|
struct AAudioStreamStruct;
|
||||||
struct AAudioStreamBuilderStruct;
|
struct AAudioStreamBuilderStruct;
|
||||||
@@ -43,8 +45,9 @@ class audio
|
|||||||
static void microphone_error_cb(AAudioStreamStruct *, void *, int32_t);
|
static void microphone_error_cb(AAudioStreamStruct *, void *, int32_t);
|
||||||
static void speaker_error_cb(AAudioStreamStruct *, void *, int32_t);
|
static void speaker_error_cb(AAudioStreamStruct *, void *, int32_t);
|
||||||
|
|
||||||
static void recreate_stream(audio *, AAudioStreamStruct *);
|
void recreate_stream(AAudioStreamStruct *);
|
||||||
|
|
||||||
|
// must own the mutex to call the method
|
||||||
void build_microphone(AAudioStreamBuilderStruct *, int32_t, int32_t);
|
void build_microphone(AAudioStreamBuilderStruct *, int32_t, int32_t);
|
||||||
void build_speaker(AAudioStreamBuilderStruct *, int32_t, int32_t);
|
void build_speaker(AAudioStreamBuilderStruct *, int32_t, int32_t);
|
||||||
|
|
||||||
@@ -61,7 +64,9 @@ class audio
|
|||||||
wivrn_session & session;
|
wivrn_session & session;
|
||||||
xr::instance & instance;
|
xr::instance & instance;
|
||||||
|
|
||||||
|
std::mutex mutex;
|
||||||
std::atomic<bool> exiting = false;
|
std::atomic<bool> exiting = false;
|
||||||
|
std::thread recreate_thread;
|
||||||
|
|
||||||
void exit();
|
void exit();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user