From c161ca023fe9be31016407172f52145cddc6f94b Mon Sep 17 00:00:00 2001 From: Lorenzo Miniero Date: Tue, 9 Jul 2024 16:59:31 +0200 Subject: [PATCH] Normalize the return value of janus_get_monotonic_time() --- src/janus.c | 1 + src/utils.c | 12 +++++++++++- src/utils.h | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/janus.c b/src/janus.c index f0153d27da..3151e8b1d4 100644 --- a/src/janus.c +++ b/src/janus.c @@ -4408,6 +4408,7 @@ gint main(int argc, char *argv[]) { g_print("Janus version: %d (%s)\n", janus_version, janus_version_string); g_print("Janus commit: %s\n", janus_build_git_sha); g_print("Compiled on: %s\n\n", janus_build_git_time); + janus_mark_started(); /* Initialize some command line options defaults */ options.debug_level = -1; diff --git a/src/utils.c b/src/utils.c index 0aa13f3823..409a0cc50b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -31,12 +31,22 @@ #include "mach_gettime.h" #endif -gint64 janus_get_monotonic_time(void) { +static gint64 janus_get_monotonic_time_internal(void) { struct timespec ts; clock_gettime (CLOCK_MONOTONIC, &ts); return (ts.tv_sec*G_GINT64_CONSTANT(1000000)) + (ts.tv_nsec/G_GINT64_CONSTANT(1000)); } +static gint64 janus_started = 0; +void janus_mark_started(void) { + if(janus_started == 0) + janus_started = janus_get_monotonic_time_internal(); +} + +gint64 janus_get_monotonic_time(void) { + return janus_get_monotonic_time_internal() - janus_started; +} + gint64 janus_get_real_time(void) { struct timespec ts; clock_gettime (CLOCK_REALTIME, &ts); diff --git a/src/utils.h b/src/utils.h index 4630a1b6c4..6bb68981e1 100644 --- a/src/utils.h +++ b/src/utils.h @@ -40,8 +40,12 @@ struct janus_json_parameter { #endif +/*! Helper method used by the core to mark when Janus started */ +void janus_mark_started(void); + /*! \brief Helper to retrieve the system monotonic time, as Glib's * g_get_monotonic_time may not be available (only since 2.28) + * @note The monotonic time will be normalized from the Janus start time * @returns The system monotonic time */ gint64 janus_get_monotonic_time(void);