From cb7ca883df6098f234543b5187b884f211f3271f Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Thu, 14 Mar 2024 16:04:38 +0100 Subject: [PATCH] stream: add time field to pw_buffer For capture streams, place the time of the cycle when we received this buffer in the new buffer time field. Without this (and without timestamps in an optional header metadata) it's impossible to determine when a buffer was created. With this field and the pw_stream_get_time_n() and pw_stream_get_nsec() we can derive the mapping to ticks and delay in the graph. --- src/pipewire/stream.c | 4 +++- src/pipewire/stream.h | 9 +++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/pipewire/stream.c b/src/pipewire/stream.c index 868be9bfd..6c582b7ba 100644 --- a/src/pipewire/stream.c +++ b/src/pipewire/stream.c @@ -1036,7 +1036,7 @@ static int impl_node_process_input(void *object) struct stream *impl = object; struct pw_stream *stream = &impl->this; struct spa_io_buffers *io = impl->io; - struct buffer *b; + struct buffer *b = NULL; if (io == NULL) return -EIO; @@ -1055,6 +1055,8 @@ static int impl_node_process_input(void *object) } copy_position(impl, impl->dequeued.incount); + if (b != NULL) + b->this.time = impl->time.now; if (!queue_is_empty(impl, &impl->dequeued)) call_process(impl); diff --git a/src/pipewire/stream.h b/src/pipewire/stream.h index 51668642c..9f44e821a 100644 --- a/src/pipewire/stream.h +++ b/src/pipewire/stream.h @@ -187,7 +187,7 @@ enum pw_stream_state { }; /** a buffer structure obtained from pw_stream_dequeue_buffer(). The size of this - * structure can grow as more field are added in the future */ + * structure can grow as more fields are added in the future */ struct pw_buffer { struct spa_buffer *buffer; /**< the spa buffer */ void *user_data; /**< user data attached to the buffer. The user of @@ -197,7 +197,7 @@ struct pw_buffer { * event. The user data is returned unmodified each * time a buffer is dequeued. */ uint64_t size; /**< This field is set by the user and the sum of - * all queued buffer is returned in the time info. + * all queued buffers is returned in the time info. * For audio, it is advised to use the number of * samples in the buffer for this field. */ uint64_t requested; /**< For playback streams, this field contains the @@ -205,6 +205,11 @@ struct pw_buffer { * streams this will be the amount of samples * required by the resampler. This field is 0 * when no suggestion is provided. Since 0.3.49 */ + uint64_t time; /**< For capture streams, this field contains the + * cycle time in nanoseconds when this buffer was + * queued in the stream. It can be compared against + * the pw_time values or pw_stream_get_nsec() + * Since 1.0.5 */ }; struct pw_stream_control {