Просмотр исходного кода

fix: stop leaking PulseAudio capture streams on sink/source events (#694)

* fix: stop leaking PulseAudio capture streams on sink/source events

pa_server_info_cb runs on every sink/source subscription event (a device
suspending or resuming, an output appearing or changing) and replaced the
capture stream with pa_stream_unref() alone. Unref drops the client's
reference but leaves the stream connected, so each event left another
monitor recording stream running on the server. Over a long session they
pile up (hundreds), the sound server's CPU and memory grow with them, and
every other PulseAudio client gets slow.

Now the existing stream is kept while it already records the default sink's
monitor, and a stream that does get replaced is disconnected before it is
unreferenced (also in the destructor).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: keep the capture stream only while it records the default sink's monitor

A ready stream can be moved to another source, so compare the source it
records from now (pa_stream_get_device_name) instead of the one it was
created for; a stream still connecting is compared with the requested one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* style: format PulseAudio capture stream condition

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Almamu <wpengine@alma.mu>
Greg Gage 4 недель назад
Родитель
Сommit
a0ddce586e

+ 23 - 3
src/WallpaperEngine/Audio/Drivers/Recorders/PulseAudioPlaybackRecorder.cpp

@@ -107,18 +107,37 @@ void pa_server_info_cb (pa_context* ctx, const pa_server_info* info, void* userd
     spec.rate = 44100;
     spec.rate = 44100;
     spec.channels = 1;
     spec.channels = 1;
 
 
+    std::string monitor_name (info->default_sink_name);
+    monitor_name += ".monitor";
+
+    // Every sink/source event lands here; keep a healthy stream that already
+    // records the default sink's monitor instead of opening another one.
     if (recorder->captureStream) {
     if (recorder->captureStream) {
+	const pa_stream_state_t state = pa_stream_get_state (recorder->captureStream);
+	// A ready stream can be moved to another source: check where it records now.
+	// One still connecting is checked against the source it was asked for.
+	const char* device = state == PA_STREAM_READY ? pa_stream_get_device_name (recorder->captureStream) : nullptr;
+	const bool current = state == PA_STREAM_READY
+	    ? (device != nullptr && monitor_name == device)
+	    : (state == PA_STREAM_CREATING && recorder->monitorName == monitor_name);
+	if (current) {
+	    return;
+	}
+    }
+
+    // unref alone leaves a connected stream recording on the server: disconnect first.
+    if (recorder->captureStream) {
+	pa_stream_disconnect (recorder->captureStream);
 	pa_stream_unref (recorder->captureStream);
 	pa_stream_unref (recorder->captureStream);
+	recorder->captureStream = nullptr;
     }
     }
+    recorder->monitorName = monitor_name;
 
 
     recorder->captureStream = pa_stream_new (ctx, "output monitor", &spec, nullptr);
     recorder->captureStream = pa_stream_new (ctx, "output monitor", &spec, nullptr);
 
 
     pa_stream_set_state_callback (recorder->captureStream, &pa_stream_notify_cb, userdata);
     pa_stream_set_state_callback (recorder->captureStream, &pa_stream_notify_cb, userdata);
     pa_stream_set_read_callback (recorder->captureStream, &pa_stream_read_cb, userdata);
     pa_stream_set_read_callback (recorder->captureStream, &pa_stream_read_cb, userdata);
 
 
-    std::string monitor_name (info->default_sink_name);
-    monitor_name += ".monitor";
-
     // setup latency
     // setup latency
     pa_buffer_attr attr {};
     pa_buffer_attr attr {};
 
 
@@ -199,6 +218,7 @@ PulseAudioPlaybackRecorder::PulseAudioPlaybackRecorder () :
 
 
 PulseAudioPlaybackRecorder::~PulseAudioPlaybackRecorder () {
 PulseAudioPlaybackRecorder::~PulseAudioPlaybackRecorder () {
     if (m_captureData.captureStream) {
     if (m_captureData.captureStream) {
+	pa_stream_disconnect (m_captureData.captureStream);
 	pa_stream_unref (m_captureData.captureStream);
 	pa_stream_unref (m_captureData.captureStream);
     }
     }
 
 

+ 2 - 0
src/WallpaperEngine/Audio/Drivers/Recorders/PulseAudioPlaybackRecorder.h

@@ -3,6 +3,7 @@
 #include "PlaybackRecorder.h"
 #include "PlaybackRecorder.h"
 #include "kiss_fftr.h"
 #include "kiss_fftr.h"
 #include <pulse/pulseaudio.h>
 #include <pulse/pulseaudio.h>
+#include <string>
 
 
 #define WAVE_BUFFER_SIZE 1024
 #define WAVE_BUFFER_SIZE 1024
 
 
@@ -21,6 +22,7 @@ public:
 	size_t currentWritePointer;
 	size_t currentWritePointer;
 	bool fullFrameReady;
 	bool fullFrameReady;
 	pa_stream* captureStream;
 	pa_stream* captureStream;
+	std::string monitorName;
     };
     };
 
 
     PulseAudioPlaybackRecorder ();
     PulseAudioPlaybackRecorder ();