From 218db975d10738b4f6ce4f4bae92ef87cae5d6da Mon Sep 17 00:00:00 2001 From: Patrick Nicolas Date: Sun, 6 Oct 2024 14:31:32 +0200 Subject: [PATCH] Fix pipewire destruction --- server/audio/audio_pipewire.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/server/audio/audio_pipewire.cpp b/server/audio/audio_pipewire.cpp index 9303fdd2..0892d9f7 100644 --- a/server/audio/audio_pipewire.cpp +++ b/server/audio/audio_pipewire.cpp @@ -35,7 +35,6 @@ struct deleter { void operator()(pw_main_loop * loop) { - pw_main_loop_quit(loop); pw_main_loop_destroy(loop); } void operator()(pw_stream * stream) @@ -49,6 +48,8 @@ struct pipewire_device : public audio_device to_headset::audio_stream_description desc; wivrn_session & session; + std::unique_ptr pw_loop; + std::unique_ptr speaker; pw_stream_events speaker_events{ .version = PW_VERSION_STREAM_EVENTS, @@ -63,10 +64,7 @@ struct pipewire_device : public audio_device .version = PW_VERSION_STREAM_EVENTS, .process = &pipewire_device::mic_process, }; - - std::unique_ptr pw_loop; - - std::thread thread; + std::jthread thread; to_headset::audio_stream_description description() const override { @@ -78,7 +76,10 @@ struct pipewire_device : public audio_device void process_mic_data(wivrn::audio_data &&) override; - ~pipewire_device() = default; + ~pipewire_device() + { + pw_main_loop_quit(pw_loop.get()); + }; pipewire_device( const std::string & source_name, @@ -188,8 +189,12 @@ struct pipewire_device : public audio_device } if (desc.speaker or desc.microphone) - thread = std::thread( - [loop = pw_loop.get()]() { pw_main_loop_run(loop); }); + thread = std::jthread( + [this](std::stop_token) { + pw_main_loop_run(pw_loop.get()); + speaker.reset(); + microphone.reset(); + ; }); } }; } // namespace