MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
time.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Contributors to the Media eXchange Layer project.
2// SPDX-License-Identifier: Apache-2.0
3
4#include "mxl/time.h"
8
9extern "C"
11uint64_t mxlGetTime()
12{
14}
15
16extern "C"
18uint64_t mxlGetCurrentIndex(mxlRational const* editRate)
19{
20 if ((editRate == nullptr) || (editRate->denominator == 0) || (editRate->numerator == 0))
21 {
23 }
24
25 auto const now = mxlGetTime();
26 return now ? mxlTimestampToIndex(editRate, now) : MXL_UNDEFINED_INDEX;
27}
28
29extern "C"
31uint64_t mxlTimestampToIndex(mxlRational const* editRate, uint64_t timestamp)
32{
33 return (editRate != nullptr) ? mxl::lib::timestampToIndex(*editRate, mxl::lib::Timepoint(timestamp)) : MXL_UNDEFINED_INDEX;
34}
35
36extern "C"
38uint64_t mxlIndexToTimestamp(mxlRational const* editRate, uint64_t index)
39{
40 if (editRate != nullptr)
41 {
42 auto const res = mxl::lib::indexToTimestamp(*editRate, index);
43 return (res || (index == 0)) ? res.value : MXL_UNDEFINED_INDEX;
44 }
46}
47
48extern "C"
50uint64_t mxlGetNsUntilIndex(uint64_t index, mxlRational const* editRate)
51{
52 // Validate the edit rate
53 if ((editRate == nullptr) || (editRate->denominator == 0) || (editRate->numerator == 0))
54 {
56 }
57
58 auto const targetNs = mxlIndexToTimestamp(editRate, index);
59 auto const nowNs = mxlGetTime();
60 return ((nowNs != 0ULL) && (targetNs >= nowNs)) ? targetNs - nowNs : 0ULL;
61}
62
63extern "C"
69
70extern "C"
int sleepUntil(Timepoint timepoint, Clock clock=Clock::Realtime) noexcept
Definition Thread.cpp:56
Duration sleep(Duration duration, Clock clock=Clock::Realtime) noexcept
Definition Thread.cpp:33
constexpr Timepoint indexToTimestamp(mxlRational const &editRate, std::uint64_t index) noexcept
constexpr std::uint64_t timestampToIndex(mxlRational const &editRate, Timepoint timestamp) noexcept
#define MXL_EXPORT
Definition platform.h:9
#define MXL_UNDEFINED_INDEX
Definition rational.h:17
struct mxlRational_t mxlRational
int64_t numerator
Definition rational.h:21
int64_t denominator
Definition rational.h:22
uint64_t index
auto const now
auto const timestamp
Definition test_time.cpp:61
auto const currentTime
Definition test_time.cpp:58
MXL_EXPORT uint64_t mxlIndexToTimestamp(mxlRational const *editRate, uint64_t index)
Definition time.cpp:38
MXL_EXPORT void mxlSleepForNs(uint64_t ns)
Definition time.cpp:65
MXL_EXPORT uint64_t mxlTimestampToIndex(mxlRational const *editRate, uint64_t timestamp)
Definition time.cpp:31
MXL_EXPORT uint64_t mxlGetNsUntilIndex(uint64_t index, mxlRational const *editRate)
Definition time.cpp:50
MXL_EXPORT uint64_t mxlGetCurrentIndex(mxlRational const *editRate)
Definition time.cpp:18
MXL_EXPORT uint64_t mxlGetTime()
Definition time.cpp:11
MXL_EXPORT void mxlSleepUntil(uint64_t timestamp)
Definition time.cpp:72