modules: use pw_stream_set_rate() when we can

This commit is contained in:
Wim Taymans 2024-11-22 09:49:27 +01:00
parent 433afeaa1e
commit 804df3389a
7 changed files with 32 additions and 72 deletions

View file

@ -196,7 +196,6 @@ struct impl {
void *buffer;
uint32_t target_buffer;
struct spa_io_rate_match *rate_match;
struct spa_io_position *position;
struct spa_dll dll;
@ -364,22 +363,17 @@ static void playback_stream_process(void *data)
static void update_rate(struct impl *impl, uint32_t filled)
{
float error;
double error;
if (impl->rate_match == NULL)
return;
error = (float)impl->target_buffer - (float)(filled);
error = SPA_CLAMP(error, -impl->max_error, impl->max_error);
error = (double)impl->target_buffer - (double)(filled);
error = SPA_CLAMPD(error, -impl->max_error, impl->max_error);
impl->corr = spa_dll_update(&impl->dll, error);
pw_log_debug("error:%f corr:%f current:%u target:%u",
error, impl->corr, filled, impl->target_buffer);
if (!impl->driving) {
SPA_FLAG_SET(impl->rate_match->flags, SPA_IO_RATE_MATCH_FLAG_ACTIVE);
impl->rate_match->rate = 1.0 / impl->corr;
}
if (!impl->driving)
pw_stream_set_rate(impl->stream, 1.0 / impl->corr);
}
static void capture_stream_process(void *data)
@ -452,9 +446,6 @@ static void stream_io_changed(void *data, uint32_t id, void *area, uint32_t size
{
struct impl *impl = data;
switch (id) {
case SPA_IO_RateMatch:
impl->rate_match = area;
break;
case SPA_IO_Position:
impl->position = area;
break;

View file

@ -187,9 +187,8 @@ struct impl {
uint32_t target_latency;
uint32_t current_latency;
uint32_t target_buffer;
struct spa_io_rate_match *rate_match;
struct spa_dll dll;
float max_error;
double max_error;
unsigned resync:1;
bool do_disconnect:1;
@ -332,23 +331,19 @@ static void stream_param_changed(void *d, uint32_t id, const struct spa_pod *par
static void update_rate(struct impl *impl, uint32_t filled)
{
float error, corr;
double error, corr;
uint32_t current_latency;
if (impl->rate_match == NULL)
return;
current_latency = impl->current_latency + filled;
error = (float)impl->target_latency - (float)(current_latency);
error = SPA_CLAMP(error, -impl->max_error, impl->max_error);
error = (double)impl->target_latency - (double)(current_latency);
error = SPA_CLAMPD(error, -impl->max_error, impl->max_error);
corr = (float)spa_dll_update(&impl->dll, error);
corr = spa_dll_update(&impl->dll, error);
pw_log_debug("error:%f corr:%f current:%u target:%u",
error, corr,
current_latency, impl->target_latency);
SPA_FLAG_SET(impl->rate_match->flags, SPA_IO_RATE_MATCH_FLAG_ACTIVE);
impl->rate_match->rate = 1.0f / corr;
pw_stream_set_rate(impl->stream, 1.0 / corr);
}
static void playback_stream_process(void *d)
@ -441,21 +436,10 @@ static void capture_stream_process(void *d)
pw_stream_queue_buffer(impl->stream, buf);
}
static void stream_io_changed(void *data, uint32_t id, void *area, uint32_t size)
{
struct impl *impl = data;
switch (id) {
case SPA_IO_RateMatch:
impl->rate_match = area;
break;
}
}
static const struct pw_stream_events playback_stream_events = {
PW_VERSION_STREAM_EVENTS,
.destroy = stream_destroy,
.state_changed = stream_state_changed,
.io_changed = stream_io_changed,
.param_changed = stream_param_changed,
.process = playback_stream_process
};
@ -464,7 +448,6 @@ static const struct pw_stream_events capture_stream_events = {
PW_VERSION_STREAM_EVENTS,
.destroy = stream_destroy,
.state_changed = stream_state_changed,
.io_changed = stream_io_changed,
.param_changed = stream_param_changed,
.process = capture_stream_process
};
@ -1225,7 +1208,7 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
goto error;
}
spa_dll_set_bw(&impl->dll, SPA_DLL_BW_MIN, 128, impl->info.rate);
impl->max_error = 256.0f;
impl->max_error = 256.0;
impl->core = pw_context_get_object(impl->context, PW_TYPE_INTERFACE_Core);
if (impl->core == NULL) {

View file

@ -44,7 +44,7 @@ static void rtp_audio_process_playback(void *data)
pw_log(level, "underrun %d/%u < %u",
avail, target_buffer, wanted);
} else {
float error, corr;
double error, corr;
if (impl->first) {
if ((uint32_t)avail > target_buffer) {
uint32_t skip = avail - target_buffer;
@ -63,19 +63,15 @@ static void rtp_audio_process_playback(void *data)
/* when not using direct timestamp and clocks are not
* in sync, try to adjust our playback rate to keep the
* requested target_buffer bytes in the ringbuffer */
error = (float)target_buffer - (float)avail;
error = SPA_CLAMP(error, -impl->max_error, impl->max_error);
error = (double)target_buffer - (double)avail;
error = SPA_CLAMPD(error, -impl->max_error, impl->max_error);
corr = (float)spa_dll_update(&impl->dll, error);
corr = spa_dll_update(&impl->dll, error);
pw_log_trace("avail:%u target:%u error:%f corr:%f", avail,
target_buffer, error, corr);
if (impl->io_rate_match) {
SPA_FLAG_SET(impl->io_rate_match->flags,
SPA_IO_RATE_MATCH_FLAG_ACTIVE);
impl->io_rate_match->rate = 1.0f / corr;
}
pw_stream_set_rate(impl->stream, 1.0 / corr);
}
spa_ringbuffer_read_data(&impl->ring,
impl->buffer,
@ -481,7 +477,7 @@ static void ptp_sender_process(void *d, struct spa_io_position *position)
* [1] http://kokkinizita.linuxaudio.org/papers/adapt-resamp.pdf
*/
error = delay - impl->target_buffer;
error = SPA_CLAMP(error, -impl->max_error, impl->max_error);
error = SPA_CLAMPD(error, -impl->max_error, impl->max_error);
impl->ptp_corr = spa_dll_update(&impl->ptp_dll, error);
pw_log_debug("filled:%u in_flight:%g delay:%g target:%u error:%f corr:%f",

View file

@ -49,7 +49,7 @@ static void rtp_opus_process_playback(void *data)
pw_log(level, "underrun %d/%u < %u",
avail, target_buffer, wanted);
} else {
float error, corr;
double error, corr;
if (impl->first) {
if ((uint32_t)avail > target_buffer) {
uint32_t skip = avail - target_buffer;
@ -68,19 +68,16 @@ static void rtp_opus_process_playback(void *data)
/* when not using direct timestamp and clocks are not
* in sync, try to adjust our playback rate to keep the
* requested target_buffer bytes in the ringbuffer */
error = (float)target_buffer - (float)avail;
error = SPA_CLAMP(error, -impl->max_error, impl->max_error);
error = (double)target_buffer - (double)avail;
error = SPA_CLAMPD(error, -impl->max_error, impl->max_error);
corr = (float)spa_dll_update(&impl->dll, error);
corr = spa_dll_update(&impl->dll, error);
pw_log_trace("avail:%u target:%u error:%f corr:%f", avail,
target_buffer, error, corr);
if (impl->io_rate_match) {
SPA_FLAG_SET(impl->io_rate_match->flags,
SPA_IO_RATE_MATCH_FLAG_ACTIVE);
impl->io_rate_match->rate = 1.0f / corr;
}
pw_stream_set_rate(impl->stream, 1.0 / corr);
}
spa_ringbuffer_read_data(&impl->ring,
impl->buffer,

View file

@ -81,7 +81,7 @@ struct impl {
struct spa_dll dll;
double corr;
uint32_t target_buffer;
float max_error;
double max_error;
float last_timestamp;
float last_time;

View file

@ -37,7 +37,7 @@ static void vban_audio_process_playback(void *data)
pw_log(level, "underrun %d/%u < %u",
avail, target_buffer, wanted);
} else {
float error, corr;
double error, corr;
if (impl->first) {
if ((uint32_t)avail > target_buffer) {
uint32_t skip = avail - target_buffer;
@ -54,19 +54,16 @@ static void vban_audio_process_playback(void *data)
}
/* try to adjust our playback rate to keep the
* requested target_buffer bytes in the ringbuffer */
error = (float)target_buffer - (float)avail;
error = SPA_CLAMP(error, -impl->max_error, impl->max_error);
error = (double)target_buffer - (double)avail;
error = SPA_CLAMPD(error, -impl->max_error, impl->max_error);
corr = (float)spa_dll_update(&impl->dll, error);
corr = spa_dll_update(&impl->dll, error);
pw_log_debug("avail:%u target:%u error:%f corr:%f", avail,
target_buffer, error, corr);
if (impl->io_rate_match) {
SPA_FLAG_SET(impl->io_rate_match->flags,
SPA_IO_RATE_MATCH_FLAG_ACTIVE);
impl->io_rate_match->rate = 1.0f / corr;
}
pw_stream_set_rate(impl->stream, 1.0 / corr);
spa_ringbuffer_read_data(&impl->ring,
impl->buffer,
BUFFER_SIZE,

View file

@ -65,12 +65,11 @@ struct impl {
struct spa_ringbuffer ring;
uint8_t buffer[BUFFER_SIZE];
struct spa_io_rate_match *io_rate_match;
struct spa_io_position *io_position;
struct spa_dll dll;
double corr;
uint32_t target_buffer;
float max_error;
double max_error;
float last_timestamp;
float last_time;
@ -108,9 +107,6 @@ static void stream_io_changed(void *data, uint32_t id, void *area, uint32_t size
{
struct impl *impl = data;
switch (id) {
case SPA_IO_RateMatch:
impl->io_rate_match = area;
break;
case SPA_IO_Position:
impl->io_position = area;
break;