From d17c5ea24c01973cf9143ec53f7c76dc9a2aceec Mon Sep 17 00:00:00 2001 From: Martin Pulec Date: Tue, 8 Aug 2023 11:26:10 +0200 Subject: [PATCH] removed platform_time.c + references get_time_in_ns() should be used instead time_since_epoch_in_ms(). Having both may be misleading and may lead to errors when interchanged. --- Makefile.in | 1 - src/compat/platform_time.c | 81 --------------------- src/compat/platform_time.h | 42 ----------- src/debug.cpp | 1 - src/debug.h | 8 +- src/hd-rum-translator/hd-rum-translator.cpp | 1 - src/rtp/video_decoders.cpp | 1 - src/transmit.cpp | 19 ++--- src/video_capture/screen_pw.cpp | 18 +++-- src/video_compress.cpp | 2 +- src/video_display/decklink.cpp | 8 +- src/video_rxtx/ultragrid_rtp.cpp | 1 - tools/Makefile | 2 +- 13 files changed, 33 insertions(+), 152 deletions(-) delete mode 100644 src/compat/platform_time.c delete mode 100644 src/compat/platform_time.h diff --git a/Makefile.in b/Makefile.in index a70eb86c2..733f97f20 100644 --- a/Makefile.in +++ b/Makefile.in @@ -143,7 +143,6 @@ COMMON_OBJS = \ src/compat/dlfunc.o \ src/compat/platform_pipe.o \ src/compat/platform_semaphore.o \ - src/compat/platform_time.o \ src/compat/usleep.o \ src/crypto/crc_32.o \ src/crypto/crypt_aes.o \ diff --git a/src/compat/platform_time.c b/src/compat/platform_time.c deleted file mode 100644 index 729274bf4..000000000 --- a/src/compat/platform_time.c +++ /dev/null @@ -1,81 +0,0 @@ -/** - * @file compat/platform_time.c - * @author Martin Pulec - */ -/* - * Copyright (c) 2011-2021 CESNET, z. s. p. o. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, is permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of CESNET nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif -#include "config_unix.h" -#include "config_win32.h" -#include "debug.h" - -#include -#include "compat/platform_time.h" - -#ifdef __MACH__ -#include -#include -#endif - -uint64_t time_since_epoch_in_ms() -{ -#ifdef WIN32 - SYSTEMTIME t; - FILETIME f; - ULARGE_INTEGER i; - GetSystemTime(&t); - SystemTimeToFileTime(&t, &f); - i.LowPart = f.dwLowDateTime; - i.HighPart = f.dwHighDateTime; - - return (i.QuadPart - 1164444736000000000ll) / 10000; -#elif defined CLOCK_REALTIME - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - - return ts.tv_sec * 1000ll + ts.tv_nsec / 1000000ll; -#else - clock_serv_t cclock; - mach_timespec_t mts; - - host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); - clock_get_time(cclock, &mts); - mach_port_deallocate(mach_task_self(), cclock); - return mts.tv_sec * 1000ll + mts.tv_nsec / 1000000ll; -#endif - -} - diff --git a/src/compat/platform_time.h b/src/compat/platform_time.h deleted file mode 100644 index e6248fb71..000000000 --- a/src/compat/platform_time.h +++ /dev/null @@ -1,42 +0,0 @@ -/** - * @file compat/platform_time.h - * @author Martin Pulec - */ -/* - * Copyright (c) 2016 CESNET, z. s. p. o. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, is permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * 3. Neither the name of CESNET nor the names of its contributors may be - * used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, - * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO - * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifdef __cplusplus -extern "C" -#endif -uint64_t time_since_epoch_in_ms(void); - diff --git a/src/debug.cpp b/src/debug.cpp index 61f868683..9b0ca170f 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -52,7 +52,6 @@ #include #include -#include "compat/platform_time.h" #include "debug.h" #include "host.h" #include "utils/color_out.h" diff --git a/src/debug.h b/src/debug.h index 4964ad984..17c506376 100644 --- a/src/debug.h +++ b/src/debug.h @@ -113,7 +113,7 @@ bool parse_log_cfg(const char *conf_str, #include #include #include -#include "compat/platform_time.h" +#include "tv.h" #include "utils/color_out.h" class Log_output{ @@ -222,9 +222,9 @@ inline void Log_output::submit(){ if (show_timestamps == LOG_TIMESTAMP_ENABLED || (show_timestamps == LOG_TIMESTAMP_AUTO && log_level >= LOG_LEVEL_VERBOSE)) { - auto time_ms = time_since_epoch_in_ms(); - snprintf(ts_str, ts_bufsize - 1, "[%.3f] ", time_ms / 1000.0); - ts_str[ts_bufsize - 1] = '\0'; + const time_ns_t time_ns = get_time_in_ns(); + snprintf(ts_str, ts_bufsize, "[%.3f] ", + (double) time_ns / NS_IN_SEC_DBL); } const char *start_newline = ""; diff --git a/src/hd-rum-translator/hd-rum-translator.cpp b/src/hd-rum-translator/hd-rum-translator.cpp index b0dca8132..b1b53265c 100644 --- a/src/hd-rum-translator/hd-rum-translator.cpp +++ b/src/hd-rum-translator/hd-rum-translator.cpp @@ -57,7 +57,6 @@ #include #include -#include "compat/platform_time.h" #include "control_socket.h" #include "crypto/random.h" #include "host.h" diff --git a/src/rtp/video_decoders.cpp b/src/rtp/video_decoders.cpp index b391542d4..68626e527 100644 --- a/src/rtp/video_decoders.cpp +++ b/src/rtp/video_decoders.cpp @@ -99,7 +99,6 @@ #include "config_win32.h" #endif // HAVE_CONFIG_H -#include "compat/platform_time.h" #include "control_socket.h" #include "crypto/openssl_decrypt.h" #include "debug.h" diff --git a/src/transmit.cpp b/src/transmit.cpp index 0ef56e63e..1d53bea8e 100644 --- a/src/transmit.cpp +++ b/src/transmit.cpp @@ -82,7 +82,6 @@ #include "utils/random.h" #include "video.h" #include "video_codec.h" -#include "compat/platform_time.h" #include #include @@ -94,7 +93,7 @@ #define FEC_MAX_MULT 10 -#define CONTROL_PORT_BANDWIDTH_REPORT_INTERVAL_MS 1000 +#define CONTROL_PORT_BANDWIDTH_REPORT_INTERVAL_NS NS_IN_SEC #ifdef HAVE_MACOSX #define GET_STARTTIME gettimeofday(&start, NULL) @@ -705,12 +704,14 @@ tx_send_base(struct tx *tx, struct video_frame *frame, struct rtp *rtp_session, } if (control_stats_enabled(tx->control)) { - auto current_time_ms = time_since_epoch_in_ms(); - if(current_time_ms - tx->last_stat_report >= CONTROL_PORT_BANDWIDTH_REPORT_INTERVAL_MS){ + const time_ns_t current_time_ns = + get_time_in_ns(); + if (current_time_ns - tx->last_stat_report >= + CONTROL_PORT_BANDWIDTH_REPORT_INTERVAL_NS) { std::ostringstream oss; oss << "tx_send " << std::hex << rtp_my_ssrc(rtp_session) << std::dec << " video " << tx->sent_since_report; control_report_stats(tx->control, oss.str()); - tx->last_stat_report = current_time_ms; + tx->last_stat_report = current_time_ns; tx->sent_since_report = 0; } tx->sent_since_report += data_len + rtp_hdr_len; @@ -879,15 +880,15 @@ audio_tx_send_pkt(struct tx *tx, struct rtp *rtp_session, uint32_t timestamp, } if (control_stats_enabled(tx->control)) { - auto current_time_ms = time_since_epoch_in_ms(); - if (current_time_ms - tx->last_stat_report >= - CONTROL_PORT_BANDWIDTH_REPORT_INTERVAL_MS) { + const time_ns_t current_time_ns = get_time_in_ns(); + if (current_time_ns - tx->last_stat_report >= + CONTROL_PORT_BANDWIDTH_REPORT_INTERVAL_NS) { std::ostringstream oss; oss << "tx_send " << std::hex << rtp_my_ssrc(rtp_session) << std::dec << " audio " << tx->sent_since_report; control_report_stats(tx->control, oss.str()); - tx->last_stat_report = current_time_ms; + tx->last_stat_report = current_time_ns; tx->sent_since_report = 0; } tx->sent_since_report += data_len + rtp_hdr_len; diff --git a/src/video_capture/screen_pw.cpp b/src/video_capture/screen_pw.cpp index 9e5203059..23cdcfb9c 100644 --- a/src/video_capture/screen_pw.cpp +++ b/src/video_capture/screen_pw.cpp @@ -18,10 +18,11 @@ #include #include -#include "utils/synchronized_queue.h" +#include "tv.h" #include "debug.h" #include "lib_common.h" #include "utils/color_out.h" +#include "utils/synchronized_queue.h" #include "video.h" #include "video_capture.h" @@ -363,7 +364,7 @@ struct screen_cast_session { } int frame_count = 0; - uint64_t frame_counter_begin_time = time_since_epoch_in_ms(); + time_ns_t frame_counter_begin_time = get_time_in_ns(); int expecting_fps = DEFAULT_EXPECTING_FPS; ~pw_() { @@ -564,17 +565,20 @@ static void on_process(void *session_ptr) { pw_stream_queue_buffer(session.pw.stream, buffer); ++session.pw.frame_count; - uint64_t time_now = time_since_epoch_in_ms(); + const time_ns_t time_now = get_time_in_ns(); - uint64_t delta = time_now - session.pw.frame_counter_begin_time; - if(delta >= 5000) { - double average_fps = session.pw.frame_count / (static_cast(delta) / 1000.0); + const time_ns_t delta = + time_now - session.pw.frame_counter_begin_time; + if (delta >= 5 * NS_IN_SEC) { + const double average_fps = session.pw.frame_count / + static_cast(delta) / + NS_IN_SEC_DBL; LOG(LOG_LEVEL_VERBOSE) << "[screen_pw]: on process: average fps in last 5 seconds: " << average_fps << "\n"; session.pw.expecting_fps = static_cast(average_fps); if(session.pw.expecting_fps == 0) session.pw.expecting_fps = 1; session.pw.frame_count = 0; - session.pw.frame_counter_begin_time = time_since_epoch_in_ms(); + session.pw.frame_counter_begin_time = get_time_in_ns(); } } diff --git a/src/video_compress.cpp b/src/video_compress.cpp index 4418a0f05..c375ada28 100644 --- a/src/video_compress.cpp +++ b/src/video_compress.cpp @@ -550,7 +550,7 @@ void compress_state_real::async_tile_consumer(struct compress_state *s) } } - ret->compress_end = time_since_epoch_in_ms(); + ret->compress_end = get_time_in_ns(); compressed_tiles.resize(state.size(), nullptr); compressed_tiles[i] = std::move(ret); } diff --git a/src/video_display/decklink.cpp b/src/video_display/decklink.cpp index 21f440edb..47944089c 100644 --- a/src/video_display/decklink.cpp +++ b/src/video_display/decklink.cpp @@ -54,7 +54,6 @@ #include "audio/types.h" #include "blackmagic_common.hpp" -#include "compat/platform_time.h" #include "debug.h" #include "host.h" #include "lib_common.h" @@ -203,7 +202,12 @@ class PlaybackDelegate : public IDeckLinkVideoOutputCallback // , public IDeckLi BMD_STR timecode_str; if (timecode && timecode->GetString(&timecode_str) == S_OK) { char *timecode_cstr = get_cstr_from_bmd_api_str(timecode_str); - LOG(LOG_LEVEL_DEBUG) << "Frame " << timecode_cstr << " output at " << time_since_epoch_in_ms() / (double) 1e3 << '\n'; + LOG(LOG_LEVEL_DEBUG) + << "Frame " << timecode_cstr + << " output at " + << (double) get_time_in_ns() / + NS_IN_SEC_DBL + << '\n'; release_bmd_api_str(timecode_str); free(timecode_cstr); } diff --git a/src/video_rxtx/ultragrid_rtp.cpp b/src/video_rxtx/ultragrid_rtp.cpp index 758d39790..4dff8188a 100644 --- a/src/video_rxtx/ultragrid_rtp.cpp +++ b/src/video_rxtx/ultragrid_rtp.cpp @@ -48,7 +48,6 @@ #include #include -#include "compat/platform_time.h" #include "control_socket.h" #include "export.h" #include "host.h" diff --git a/tools/Makefile b/tools/Makefile index a4a3b17ca..36005a6e7 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -21,7 +21,7 @@ astat_lib: astat.a astat.a: astat.o src/compat/platform_pipe.o ar rcs astat.a $^ -convert: src/pixfmt_conv.o src/video_codec.o src/compat/platform_time.o convert.o src/debug.o src/utils/color_out.o src/utils/misc.o +convert: src/pixfmt_conv.o src/video_codec.o convert.o src/debug.o src/utils/color_out.o src/utils/misc.o $(CXX) $^ -o convert decklink_temperature: decklink_temperature.cpp ext-deps/DeckLink/Linux/DeckLinkAPIDispatch.o