From 3f0fe0032f5290a1cc36b0378a1246fda4d1c1ab Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Tue, 26 Nov 2024 17:02:02 +0100 Subject: [PATCH] node: add a clock flag to mark rate/duration inaccurate Some clocks (v4l2) don't process exactly process buffers at the given rate/duration so mark this in the clock flags. We need to use the nsec field in the clock to derive ticks in pw-stream in that case to get a good clock. --- spa/include/spa/node/io.h | 3 +++ src/pipewire/stream.c | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/spa/include/spa/node/io.h b/spa/include/spa/node/io.h index 409fdde19..a0433a09e 100644 --- a/spa/include/spa/node/io.h +++ b/spa/include/spa/node/io.h @@ -128,6 +128,9 @@ struct spa_io_clock { #define SPA_IO_CLOCK_FLAG_FREEWHEEL (1u<<0) /* graph is freewheeling */ #define SPA_IO_CLOCK_FLAG_XRUN_RECOVER (1u<<1) /* recovering from xrun */ #define SPA_IO_CLOCK_FLAG_LAZY (1u<<2) /* lazy scheduling */ +#define SPA_IO_CLOCK_FLAG_NO_RATE (1u<<3) /* the rate of the clock is only approximately. + * it is recommended to use the nsec as a clock source. + * The rate_diff contains the measured inaccuracy. */ uint32_t flags; /**< Clock flags */ uint32_t id; /**< Unique clock id, set by host application */ char name[64]; /**< Clock name prefixed with API, set by node when it receives diff --git a/src/pipewire/stream.c b/src/pipewire/stream.c index 3d8d71d5d..9a6383ce7 100644 --- a/src/pipewire/stream.c +++ b/src/pipewire/stream.c @@ -638,7 +638,12 @@ static inline void copy_position(struct stream *impl, int64_t queued) impl->base_pos = p->clock.position - impl->time.ticks; impl->clock_id = p->clock.id; } - impl->time.ticks = p->clock.position - impl->base_pos; + if (SPA_FLAG_IS_SET(p->clock.flags, SPA_IO_CLOCK_FLAG_NO_RATE)) + impl->time.ticks = p->clock.nsec * p->clock.rate.denom / + (SPA_NSEC_PER_SEC * p->clock.rate.num); + else + impl->time.ticks = p->clock.position - impl->base_pos; + impl->time.delay = 0; impl->time.queued = queued; impl->quantum = p->clock.duration;