From 0468712fea56b05d76ad85fdf79eccfe1e54fad5 Mon Sep 17 00:00:00 2001 From: Michael Tretter Date: Fri, 24 Jan 2025 12:11:03 +0100 Subject: [PATCH] stream: fix ticks calculation if rate is 0/0 If the rate is 0/0, converting nsec to ticks doesn't work and will result in 0 ticks, and it is not possible to convert ticks back to a timestamp. This can be reproduced by connecting a GStreamer pipewiresrc to a libcamera node. The libcamera-utils has a rate of 0/0 and the pipewireclock won't be able to determine a correct time with that. This error was caused by Commit 89993a3cc628 ("gst: enable the pipewire ticks as a clock source"). Fix this by using the nsec as ticks and setting the appropriate rate. --- src/pipewire/stream.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/pipewire/stream.c b/src/pipewire/stream.c index 94956bda2..ce6a95f90 100644 --- a/src/pipewire/stream.c +++ b/src/pipewire/stream.c @@ -638,11 +638,18 @@ 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; } - 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 + if (SPA_FLAG_IS_SET(p->clock.flags, SPA_IO_CLOCK_FLAG_NO_RATE)) { + if (p->clock.rate.num == 0 || p->clock.rate.denom == 0) { + impl->time.ticks = p->clock.nsec; + impl->time.rate.num = 1; + impl->time.rate.denom = SPA_NSEC_PER_SEC; + } else { + 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;