stream: add pw_stream_get_nsec() to get current time

Make a method to get the current time to compare agains the pw_time-now
field. This is currently CLOCK_MONOTONIC but make this into a method
so that we can more easily change it later.
This commit is contained in:
Wim Taymans 2024-03-04 12:59:26 +01:00
parent 81d8ecf0dc
commit f4e391dd41
16 changed files with 53 additions and 58 deletions

View file

@ -1993,6 +1993,14 @@ int pw_filter_get_time(struct pw_filter *filter, struct pw_time *time)
return 0;
}
SPA_EXPORT
uint64_t pw_filter_get_nsec(struct pw_filter *filter)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return SPA_TIMESPEC_TO_NSEC(&ts);
}
SPA_EXPORT
struct pw_buffer *pw_filter_dequeue_buffer(void *port_data)
{

View file

@ -215,6 +215,10 @@ pw_filter_update_params(struct pw_filter *filter, /**< a \ref pw_filter */
SPA_DEPRECATED
int pw_filter_get_time(struct pw_filter *filter, struct pw_time *time);
/** Get the current time in nanoseconds. This value can be compared with
* the nsec value in the spa_io_position. Since 1.1.0 */
uint64_t pw_filter_get_nsec(struct pw_filter *filter);
/** Get a buffer that can be filled for output ports or consumed
* for input ports. */
struct pw_buffer *pw_filter_dequeue_buffer(void *port_data);

View file

@ -2409,6 +2409,14 @@ int pw_stream_get_time_n(struct pw_stream *stream, struct pw_time *time, size_t
return 0;
}
SPA_EXPORT
uint64_t pw_stream_get_nsec(struct pw_stream *stream)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return SPA_TIMESPEC_TO_NSEC(&ts);
}
static int
do_trigger_deprecated(struct spa_loop *loop,
bool async, uint32_t seq, const void *data, size_t size, void *user_data)

View file

@ -245,9 +245,8 @@ struct pw_stream_control {
* to the current time like this:
*
*\code{.c}
* struct timespec ts;
* clock_gettime(CLOCK_MONOTONIC, &ts);
* int64_t diff = SPA_TIMESPEC_TO_NSEC(&ts) - pw_time.now;
* uint64_t now = pw_stream_get_nsec(stream);
* int64_t diff = now - pw_time.now;
* int64_t elapsed = (pw_time.rate.denom * diff) / (pw_time.rate.num * SPA_NSEC_PER_SEC);
*\endcode
*
@ -523,6 +522,10 @@ int pw_stream_set_control(struct pw_stream *stream, uint32_t id, uint32_t n_valu
/** Query the time on the stream */
int pw_stream_get_time_n(struct pw_stream *stream, struct pw_time *time, size_t size);
/** Get the current time in nanoseconds. This value can be compared with
* the pw_time_now value. Since 1.1.0 */
uint64_t pw_stream_get_nsec(struct pw_stream *stream);
/** Query the time on the stream, deprecated since 0.3.50,
* use pw_stream_get_time_n() to get the fields added since 0.3.50. */
SPA_DEPRECATED