Fix pipewire sink setup (#717)
This commit is contained in:
@@ -104,6 +104,14 @@ struct pipewire_device : public audio_device
|
|||||||
.sample_rate = info.speaker->sample_rate,
|
.sample_rate = info.speaker->sample_rate,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Calculate quantum size: 5ms buffer for low latency while maintaining stability
|
||||||
|
// Smaller buffers (<5ms) risk underruns, larger ones (>10ms) add perceptible latency
|
||||||
|
uint32_t quantum_size = (desc.speaker->sample_rate * 5) / 1000;
|
||||||
|
uint32_t frame_size = desc.speaker->num_channels * sizeof(int16_t);
|
||||||
|
|
||||||
|
std::string rate_str = std::format("1/{}", desc.speaker->sample_rate);
|
||||||
|
std::string latency_str = std::format("{}/{}", quantum_size, desc.speaker->sample_rate);
|
||||||
|
|
||||||
speaker.reset(pw_stream_new_simple(
|
speaker.reset(pw_stream_new_simple(
|
||||||
pw_main_loop_get_loop(pw_loop.get()),
|
pw_main_loop_get_loop(pw_loop.get()),
|
||||||
sink_name.c_str(),
|
sink_name.c_str(),
|
||||||
@@ -120,6 +128,19 @@ struct pipewire_device : public audio_device
|
|||||||
"Audio/Sink",
|
"Audio/Sink",
|
||||||
PW_KEY_MEDIA_ROLE,
|
PW_KEY_MEDIA_ROLE,
|
||||||
"Game",
|
"Game",
|
||||||
|
PW_KEY_NODE_VIRTUAL,
|
||||||
|
"true",
|
||||||
|
// Set stream rate to match client, preventing PipeWire from doing
|
||||||
|
// unnecessary resampling which degrades audio quality
|
||||||
|
PW_KEY_NODE_RATE,
|
||||||
|
rate_str.c_str(),
|
||||||
|
// Declare target latency to help PipeWire optimize buffering
|
||||||
|
PW_KEY_NODE_LATENCY,
|
||||||
|
latency_str.c_str(),
|
||||||
|
// Force constant quantum size to prevent dynamic buffer size changes
|
||||||
|
// Variable buffer sizes cause crackling in effect processors (EasyEffects)
|
||||||
|
PW_KEY_NODE_FORCE_QUANTUM,
|
||||||
|
latency_str.c_str(),
|
||||||
NULL),
|
NULL),
|
||||||
&speaker_events,
|
&speaker_events,
|
||||||
this));
|
this));
|
||||||
@@ -127,22 +148,26 @@ struct pipewire_device : public audio_device
|
|||||||
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()));
|
||||||
|
|
||||||
spa_audio_info_raw info{
|
spa_audio_info_raw audio_info{
|
||||||
.format = SPA_AUDIO_FORMAT_S16,
|
.format = SPA_AUDIO_FORMAT_S16,
|
||||||
.rate = desc.speaker->sample_rate,
|
.rate = desc.speaker->sample_rate,
|
||||||
.channels = desc.speaker->num_channels,
|
.channels = desc.speaker->num_channels,
|
||||||
};
|
};
|
||||||
const spa_pod * param = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &info);
|
|
||||||
|
|
||||||
|
const spa_pod * params[1];
|
||||||
|
params[0] = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &audio_info);
|
||||||
|
|
||||||
|
// Stream flags:
|
||||||
|
// - DRIVER: makes this node the graph clock master, preventing sync drift in the audio chain
|
||||||
if (pw_stream_connect(
|
if (pw_stream_connect(
|
||||||
speaker.get(),
|
speaker.get(),
|
||||||
PW_DIRECTION_INPUT,
|
PW_DIRECTION_INPUT,
|
||||||
PW_ID_ANY,
|
PW_ID_ANY,
|
||||||
pw_stream_flags(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS),
|
pw_stream_flags(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS | PW_STREAM_FLAG_RT_PROCESS),
|
||||||
¶m,
|
params,
|
||||||
1) < 0)
|
1) < 0)
|
||||||
throw std::runtime_error("failed to connect speaker stream");
|
throw std::runtime_error("failed to connect speaker stream");
|
||||||
U_LOG_I("pipewire speaker stream created");
|
U_LOG_I("pipewire speaker stream created (quantum: %u frames, %.2f ms)", quantum_size, (quantum_size * 1000.0) / desc.speaker->sample_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info.microphone)
|
if (info.microphone)
|
||||||
@@ -152,6 +177,14 @@ struct pipewire_device : public audio_device
|
|||||||
.sample_rate = info.microphone->sample_rate,
|
.sample_rate = info.microphone->sample_rate,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Calculate quantum size: 5ms buffer for low latency while maintaining stability
|
||||||
|
// Smaller buffers (<5ms) risk underruns, larger ones (>10ms) add perceptible latency
|
||||||
|
uint32_t quantum_size = (desc.microphone->sample_rate * 5) / 1000;
|
||||||
|
uint32_t frame_size = desc.microphone->num_channels * sizeof(int16_t);
|
||||||
|
|
||||||
|
std::string rate_str = std::format("1/{}", desc.microphone->sample_rate);
|
||||||
|
std::string latency_str = std::format("{}/{}", quantum_size, desc.microphone->sample_rate);
|
||||||
|
|
||||||
microphone.reset(pw_stream_new_simple(
|
microphone.reset(pw_stream_new_simple(
|
||||||
pw_main_loop_get_loop(pw_loop.get()),
|
pw_main_loop_get_loop(pw_loop.get()),
|
||||||
source_name.c_str(),
|
source_name.c_str(),
|
||||||
@@ -168,28 +201,42 @@ struct pipewire_device : public audio_device
|
|||||||
"Audio/Source",
|
"Audio/Source",
|
||||||
PW_KEY_MEDIA_ROLE,
|
PW_KEY_MEDIA_ROLE,
|
||||||
"Game",
|
"Game",
|
||||||
|
PW_KEY_NODE_VIRTUAL,
|
||||||
|
"true",
|
||||||
|
// Set stream rate to match client, preventing PipeWire from doing
|
||||||
|
// unnecessary resampling which degrades audio quality
|
||||||
|
PW_KEY_NODE_RATE,
|
||||||
|
rate_str.c_str(),
|
||||||
|
// Declare target latency to help PipeWire optimize buffering
|
||||||
|
PW_KEY_NODE_LATENCY,
|
||||||
|
latency_str.c_str(),
|
||||||
|
// Force constant quantum size to prevent dynamic buffer size changes
|
||||||
|
PW_KEY_NODE_FORCE_QUANTUM,
|
||||||
|
latency_str.c_str(),
|
||||||
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()));
|
||||||
|
|
||||||
spa_audio_info_raw info{
|
spa_audio_info_raw audio_info{
|
||||||
.format = SPA_AUDIO_FORMAT_S16,
|
.format = SPA_AUDIO_FORMAT_S16,
|
||||||
.rate = desc.microphone->sample_rate,
|
.rate = desc.microphone->sample_rate,
|
||||||
.channels = desc.microphone->num_channels,
|
.channels = desc.microphone->num_channels,
|
||||||
};
|
};
|
||||||
const spa_pod * param = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &info);
|
|
||||||
|
const spa_pod * params[1];
|
||||||
|
params[0] = spa_format_audio_raw_build(&b, SPA_PARAM_EnumFormat, &audio_info);
|
||||||
|
|
||||||
if (pw_stream_connect(
|
if (pw_stream_connect(
|
||||||
microphone.get(),
|
microphone.get(),
|
||||||
PW_DIRECTION_OUTPUT,
|
PW_DIRECTION_OUTPUT,
|
||||||
PW_ID_ANY,
|
PW_ID_ANY,
|
||||||
pw_stream_flags(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS),
|
pw_stream_flags(PW_STREAM_FLAG_AUTOCONNECT | PW_STREAM_FLAG_MAP_BUFFERS),
|
||||||
¶m,
|
params,
|
||||||
1) < 0)
|
1) < 0)
|
||||||
throw std::runtime_error("failed to connect microphone stream");
|
throw std::runtime_error("failed to connect microphone stream");
|
||||||
U_LOG_I("pipewire microphone stream created");
|
U_LOG_I("pipewire microphone stream created (quantum: %u frames, %.2f ms)", quantum_size, (quantum_size * 1000.0) / desc.microphone->sample_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (desc.speaker or desc.microphone)
|
if (desc.speaker or desc.microphone)
|
||||||
@@ -217,10 +264,14 @@ void pipewire_device::mic_process(void * self_v)
|
|||||||
const auto & data = buffer->buffer->datas[0];
|
const auto & data = buffer->buffer->datas[0];
|
||||||
uint8_t * data_ptr = (uint8_t *)data.data;
|
uint8_t * data_ptr = (uint8_t *)data.data;
|
||||||
if (not data.data)
|
if (not data.data)
|
||||||
|
{
|
||||||
|
pw_stream_queue_buffer(self->microphone.get(), buffer);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const size_t frame_size = self->desc.microphone->num_channels * sizeof(int16_t);
|
const size_t frame_size = self->desc.microphone->num_channels * sizeof(int16_t);
|
||||||
|
|
||||||
|
// Use consistent buffer size based on stream quantum
|
||||||
#if PW_CHECK_VERSION(0, 3, 49)
|
#if PW_CHECK_VERSION(0, 3, 49)
|
||||||
size_t num_frames = buffer->requested;
|
size_t num_frames = buffer->requested;
|
||||||
#else
|
#else
|
||||||
@@ -228,7 +279,8 @@ void pipewire_device::mic_process(void * self_v)
|
|||||||
#endif
|
#endif
|
||||||
if (num_frames == 0)
|
if (num_frames == 0)
|
||||||
{
|
{
|
||||||
num_frames = data.maxsize / frame_size;
|
uint32_t quantum_size = (self->desc.microphone->sample_rate * 5) / 1000;
|
||||||
|
num_frames = std::min<size_t>(quantum_size, data.maxsize / frame_size);
|
||||||
}
|
}
|
||||||
data.chunk->offset = 0;
|
data.chunk->offset = 0;
|
||||||
data.chunk->size = 0;
|
data.chunk->size = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user