mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2026-04-04 07:15:35 -04:00
bluez5: iso-io: more accurate resync after overrun
Take active rate correction properly into account when dropping data on overrun resync. Drop data only for the currently processed stream, after data has been consumed from it. Make sure the rate correction factor is updated after this for the next cycle of the stream. Also fix buffer fill level calculation: the fill level interpolation should use node rate corr, not clock rate diff, since the calculations are done in system clock domain. Fix same issue in fractional delay calculation, and take no resampler prefill into account. Later, we maybe need some more resampler APIs to avoid such details leaking in. Previously, stream could have its old rate correction locked in, and its fill level would then end up off the target on the next cycle.
This commit is contained in:
parent
b535534611
commit
11389d101a
3 changed files with 86 additions and 43 deletions
|
|
@ -21,12 +21,12 @@
|
|||
|
||||
#include "media-codecs.h"
|
||||
#include "defs.h"
|
||||
#include "decode-buffer.h"
|
||||
|
||||
SPA_LOG_TOPIC_DEFINE_STATIC(log_topic, "spa.bluez5.iso");
|
||||
#undef SPA_LOG_TOPIC_DEFAULT
|
||||
#define SPA_LOG_TOPIC_DEFAULT &log_topic
|
||||
|
||||
#include "decode-buffer.h"
|
||||
#include "bt-latency.h"
|
||||
|
||||
#define IDLE_TIME (500 * SPA_NSEC_PER_MSEC)
|
||||
|
|
@ -684,6 +684,7 @@ void spa_bt_iso_io_set_source_buffer(struct spa_bt_iso_io *this, struct spa_bt_d
|
|||
buffer->no_overrun_drop = true;
|
||||
buffer->avg_period = ISO_BUFFERING_AVG_PERIOD;
|
||||
buffer->rate_diff_max = ISO_BUFFERING_RATE_DIFF_MAX;
|
||||
stream->this.need_resync = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -827,8 +828,38 @@ void spa_bt_iso_io_check_rx_sync(struct spa_bt_iso_io *this, uint64_t position)
|
|||
if (!stream->source_buf)
|
||||
return;
|
||||
|
||||
/* Act on pending resync */
|
||||
target = stream->source_buf->target;
|
||||
|
||||
if (stream->source_buf && stream->this.need_resync) {
|
||||
int32_t level;
|
||||
|
||||
stream->this.need_resync = false;
|
||||
|
||||
/* Resync level */;
|
||||
spa_bt_decode_buffer_recover(stream->source_buf);
|
||||
level = (int32_t)round(stream->source_buf->level +
|
||||
(double)stream->source_buf->duration_ns * stream->source_buf->rate / SPA_NSEC_PER_SEC);
|
||||
|
||||
if (level > target) {
|
||||
uint32_t drop = (level - target) * stream->source_buf->frame_size;
|
||||
uint32_t avail = spa_bt_decode_buffer_get_size(stream->source_buf);
|
||||
|
||||
drop = SPA_MIN(drop, avail);
|
||||
|
||||
spa_log_debug(group->log, "%p: ISO overrun group:%u fd:%d level:%f target:%d drop:%u",
|
||||
group, group->id, stream->fd,
|
||||
stream->source_buf->level + stream->source_buf->prev_samples,
|
||||
target,
|
||||
drop/stream->source_buf->frame_size);
|
||||
|
||||
spa_bt_decode_buffer_read(stream->source_buf, drop);
|
||||
}
|
||||
}
|
||||
|
||||
/* Check sync after all input streams have completed process() on same cycle */
|
||||
stream->position = position;
|
||||
|
||||
spa_list_for_each(s, &group->streams, link) {
|
||||
if (!s->source_buf)
|
||||
continue;
|
||||
|
|
@ -836,8 +867,6 @@ void spa_bt_iso_io_check_rx_sync(struct spa_bt_iso_io *this, uint64_t position)
|
|||
return;
|
||||
}
|
||||
|
||||
target = stream->source_buf->target;
|
||||
|
||||
/* Rate match ISO clock */
|
||||
corr = spa_bt_rate_control_update(&sync->dll, sync->avg_err, 0,
|
||||
group->duration_rx, CLOCK_SYNC_AVG_PERIOD, CLOCK_SYNC_RATE_DIFF_MAX);
|
||||
|
|
@ -856,7 +885,7 @@ void spa_bt_iso_io_check_rx_sync(struct spa_bt_iso_io *this, uint64_t position)
|
|||
sync->avg_err = 0;
|
||||
sync->avg_num = 0;
|
||||
|
||||
/* Handle overrun (e.g. resyncs streams after initial buffering) */
|
||||
/* Detect overrun */
|
||||
spa_list_for_each(s, &group->streams, link) {
|
||||
if (s->source_buf) {
|
||||
double level = s->source_buf->level;
|
||||
|
|
@ -867,30 +896,10 @@ void spa_bt_iso_io_check_rx_sync(struct spa_bt_iso_io *this, uint64_t position)
|
|||
}
|
||||
}
|
||||
|
||||
if (!overrun)
|
||||
return;
|
||||
|
||||
spa_list_for_each(s, &group->streams, link) {
|
||||
if (!s->source_buf)
|
||||
continue;
|
||||
|
||||
int32_t level = (int32_t)s->source_buf->level;
|
||||
|
||||
if (level > target) {
|
||||
uint32_t drop = (level - target) * s->source_buf->frame_size;
|
||||
uint32_t avail = spa_bt_decode_buffer_get_size(s->source_buf);
|
||||
|
||||
drop = SPA_MIN(drop, avail);
|
||||
|
||||
spa_log_debug(group->log, "%p: ISO overrun group:%u fd:%d level:%f target:%d drop:%u",
|
||||
group, group->id, s->fd,
|
||||
s->source_buf->level,
|
||||
target,
|
||||
drop/s->source_buf->frame_size);
|
||||
|
||||
spa_bt_decode_buffer_read(s->source_buf, drop);
|
||||
if (overrun) {
|
||||
spa_list_for_each(s, &group->streams, link) {
|
||||
if (s->source_buf)
|
||||
s->this.need_resync = true;
|
||||
}
|
||||
|
||||
spa_bt_decode_buffer_recover(s->source_buf);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue