Gamp v0.0.7-54-gccdc599
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
PTS.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright Gothel Software e.K.
4 *
5 * SPDX-License-Identifier: MIT
6 *
7 * This Source Code Form is subject to the terms of the MIT License
8 * If a copy of the MIT was not distributed with this file,
9 * you can obtain one at https://opensource.org/license/mit/.
10 */
11#ifndef GAMP_AV_PTS_HPP_
12#define GAMP_AV_PTS_HPP_
13
14#include <jau/basic_types.hpp>
15#include <jau/fraction_type.hpp>
17#include <jau/string_util.hpp>
18
19#include <chrono>
20#include <cstdint>
21#include <gamp/GampTypes.hpp>
23
24namespace gamp::av {
25
26 /** @defgroup Gamp_AV Gamp Audio Video
27 * Gamp audio video support utilities.
28 *
29 * @{
30 */
31
32 /**
33 * Presentation Timestamp (PTS) with added System Clock Reference (SCR) via
34 * {@link #set(long, int)} and its interpolation via {@link #get(long)}, as well as giving raw access via {@link #getLast()}.
35 * <p>
36 * The relative millisecond PTS since start of the presentation stored in uint32_t
37 * covers a time span of 4'294'967'293 ms
38 * or 4'294'967 seconds or 49 days.
39 * </p>
40 */
41 class PTS {
42 public:
43 /** An external float value getter */
44 typedef jau::function<float()> FloatValue;
45
46 private:
47 FloatValue m_speed;
48 /** System Clock Reference (SCR) of last PTS update. */
50 /** Last updated PTS value */
52
53 public:
54 /**
55 * Create new instance, initializing pts with {@link TimeFrameI#INVALID_PTS} and system-clock timestamp with zero.
56 * @param speed external {@link FloatValue} getter for playback speed.
57 * @see #set(long, int)
58 */
59 PTS(FloatValue speed) noexcept
60 : m_speed(std::move(speed)), m_scr(0), m_pts(TimeFrameI::INVALID_PTS)
61 { }
62
63 /**
64 * Create new instance.
65 * @param speed external {@link FloatValue} getter for playback speed.
66 * @param scr System Clock Reference (SCR) in milliseconds of taken pts value, i.e. {@link Clock#currentMillis()}.
67 * @param pts the presentation timestamp (PTS) in milliseconds
68 * @see #set(long, int)
69 */
70 PTS(FloatValue speed, uint64_t scr, uint32_t pts) noexcept
71 : m_speed(std::move(speed)), m_scr(scr), m_pts(pts)
72 { }
73
74 /** Returns true if {@link #getLast()} is unequal to {@link TimeFrameI#INVALID_PTS}. */
75 constexpr_atomic bool isValid() const noexcept { return TimeFrameI::INVALID_PTS != m_pts; }
76
77 /** Returns true if {@link #getLast()} equals to {@link TimeFrameI#END_OF_STREAM_PTS}, indicating end of stream (EOS). */
78 constexpr_atomic bool isEOS() const noexcept { return TimeFrameI::END_OF_STREAM_PTS == m_pts; }
79
80 /** Returns the System Clock Reference (SCR) in milliseconds of last PTS update via {@link #set(long, int)}. */
81 constexpr_atomic uint64_t scr() const noexcept { return m_scr; }
82
83 /** Returns scr() as time string representation via {@link #toTimeStr(long, boolean)}. */
84 std::string getSCRTimeStr(bool addFractions) const noexcept {
85 return toTimeStr(scr(), addFractions);
86 }
87
88 /** Returns the last updated PTS value via {@link #set(long, int)} w/o System Clock Reference (SCR) interpolation. */
89 constexpr_atomic uint32_t last() const noexcept { return m_pts; }
90
91 /** Returns {@link #getLast()} as time string representation via {@link #toTimeStr(long, boolean)}. */
92 std::string getLastTimeStr(bool addFractions) const noexcept {
93 return toTimeStr(last(), addFractions);
94 }
95
96 /** Returns the external playback speed. */
97 float getSpeed() const noexcept { return m_speed(); }
98
99 /**
100 * Updates the PTS value with given System Clock Reference (SCR) in milliseconds.
101 * @param scr System Clock Reference (SCR) in milliseconds of taken PTS value, i.e. {@link Clock#currentMillis()}.
102 * @param pts the presentation timestamp (PTS) in milliseconds
103 */
104 void set(uint64_t scr, uint32_t pts) noexcept {
105 m_scr = scr;
106 m_pts = pts;
107 }
108 /** Sets the PTS value, see {@link #set(long, int)}. */
109 void setPTS(uint32_t pts) noexcept { m_pts = pts; }
110 /** Sets the System Clock Reference (SCR) in milliseconds of last PTS update, see {@link #set(long, int)}. */
111 void setSCR(uint64_t currentMillis) noexcept { m_scr = currentMillis; }
112
113 /**
114 * Updates the PTS value with values from other {@link PTS} instance.
115 * @param other source {@link PTS} values
116 * @see #get(long)
117 */
118 void set(const PTS& other) noexcept {
119 m_scr = other.scr();
120 m_pts = other.last();
121 }
122
123 /**
124 * Returns the last() updated PTS, interpolated by scr() System Clock Reference (SCR) delta to given `currentMillis` and playback getSpeed().
125 * <pre>
126 * last_pts + (uint32_t) ( ( currentMillis - SCR ) * speed + 0.5f )
127 * </pre>
128 * @param currentMillis current system clock in milliseconds, i.e. {@link Clock#currentMillis()}.
129 * @see #set(long, int)
130 */
131 constexpr_atomic uint32_t get(uint64_t currentMillis) const noexcept {
132 const float dpts = static_cast<float>( currentMillis - m_scr ) * m_speed() + 0.5f;
133 return m_pts + static_cast<uint32_t>( dpts );
134 }
135 /** Returns {@link #get(long)} passing {@link Clock#currentMillis()}. */
136 uint32_t getCurrent() const noexcept { return get( jau::getCurrentMilliseconds() ); }
137
138 /** Returns {@link #get(long)} as time string representation via {@link #toTimeStr(long, boolean)}. */
139 std::string getTimeStr(uint64_t currentMillis, bool addFractions) {
140 return toTimeStr(get(currentMillis), addFractions);
141 }
142
143 /** Returns {@link #getLast()} - rhs.{@link #getLast()}. */
144 constexpr_atomic uint32_t diffLast(const PTS& rhs) const noexcept {
145 return m_pts - rhs.last();
146 }
147
148 /** Returns {@link #get(long)} - rhs.{@link #get(long)}. */
149 constexpr_atomic uint32_t diff(uint64_t currentMillis, const PTS& rhs) const noexcept {
150 return get(currentMillis) - rhs.get(currentMillis);
151 }
152
153 std::string toString() noexcept { return std::to_string(m_pts); }
154
155 std::string toString(uint64_t currentMillis) noexcept { return "last "+std::to_string(m_pts)+" ms, current "+std::to_string(get(currentMillis))+" ms"; }
156
157 /**
158 * Returns a time string representation '[HH:]mm:ss[.SSS]', dropping unused hour quantities and fractions of seconds optionally.
159 * @param millis complete time in milliseconds
160 * @param addFractions toggle for fractions of seconds
161 * @see #toTimeStr(long)
162 */
163 std::string toTimeStr(uint64_t millis, bool addFractions) const noexcept {
164 using namespace std::chrono;
165 milliseconds ms(millis);
166 seconds sec = duration_cast<seconds>(ms);
167 ms -= duration_cast<milliseconds>(sec);
168 minutes m = duration_cast<minutes>(sec);
169 sec -= duration_cast<seconds>(m);
170 hours h = duration_cast<hours>(m);
171 m -= duration_cast<minutes>(h);
172
173 if( addFractions ) {
174 if( 0 < h.count() ) {
175 return jau::unsafe::format_string_n(12, "%02d:%02d:%02d.%03d", h, m, sec, millis);
176 } else {
177 return jau::unsafe::format_string_n( 9, "%02d:%02d.%03d", m, sec, millis);
178 }
179 } else {
180 if( 0 < h.count() ) {
181 return jau::unsafe::format_string_n(12, "%02d:%02d:%02d", h, m, sec);
182 } else {
183 return jau::unsafe::format_string_n( 5, "%02d:%02d", m, sec);
184 }
185 }
186 }
187
188 /**
189 * Returns a full time string representation 'HH:mm:ss.SSS'.
190 * @param millis complete time in milliseconds
191 * @see #toTimeStr(long, boolean)
192 */
193 std::string toTimeStr(uint64_t millis) {
194 using namespace std::chrono;
195 milliseconds ms(millis);
196 seconds sec = duration_cast<seconds>(ms);
197 ms -= duration_cast<milliseconds>(sec);
198 minutes m = duration_cast<minutes>(sec);
199 sec -= duration_cast<seconds>(m);
200 hours h = duration_cast<hours>(m);
201 m -= duration_cast<minutes>(h);
202 return jau::unsafe::format_string_n(12, "%02d:%02d:%02d.%03d", h, m, sec, millis);
203 }
204
205 /**
206 * Returns milliseconds from given string representation in '[H[H]:]m[m]:s[s][.S*]'
207 * @param v the timestamp string to parse.
208 */
209 static uint32_t toMillis(const std::string& datestr) noexcept;
210
211 /** Returns milliseconds from given string representation in '[H[H]:]m[m]:s[s][.S*]' or {@code -1} for parsing error. */
212 // int toMillis(const std::string& datestr v) { return toMillis(v); }
213 };
214
215 /**@}*/
216
217} // namespace gamp::av
218
219#endif /* GAMP_AV_PTS_HPP_ */
std::string toString() noexcept
Definition PTS.hpp:153
std::string toTimeStr(uint64_t millis)
Returns a full time string representation 'HH:mm:ss.SSS'.
Definition PTS.hpp:193
uint32_t getCurrent() const noexcept
Returns get(long) passing Clock#currentMillis().
Definition PTS.hpp:136
jau::function< float()> FloatValue
An external float value getter.
Definition PTS.hpp:44
void set(const PTS &other) noexcept
Updates the PTS value with values from other PTS instance.
Definition PTS.hpp:118
constexpr_atomic uint64_t scr() const noexcept
Returns the System Clock Reference (SCR) in milliseconds of last PTS update via set(long,...
Definition PTS.hpp:81
std::string getTimeStr(uint64_t currentMillis, bool addFractions)
Returns get(long) as time string representation via toTimeStr(long, boolean).
Definition PTS.hpp:139
PTS(FloatValue speed, uint64_t scr, uint32_t pts) noexcept
Create new instance.
Definition PTS.hpp:70
void set(uint64_t scr, uint32_t pts) noexcept
Updates the PTS value with given System Clock Reference (SCR) in milliseconds.
Definition PTS.hpp:104
std::string toString(uint64_t currentMillis) noexcept
Definition PTS.hpp:155
constexpr_atomic uint32_t diff(uint64_t currentMillis, const PTS &rhs) const noexcept
Returns get(long) - rhs.
Definition PTS.hpp:149
constexpr_atomic bool isEOS() const noexcept
Returns true if getLast() equals to TimeFrameI#END_OF_STREAM_PTS, indicating end of stream (EOS).
Definition PTS.hpp:78
static uint32_t toMillis(const std::string &datestr) noexcept
Returns milliseconds from given string representation in '[H[H]:]m[m]:s[s][.S*]'.
std::string toTimeStr(uint64_t millis, bool addFractions) const noexcept
Returns a time string representation '[HH:]mm:ss[.SSS]', dropping unused hour quantities and fraction...
Definition PTS.hpp:163
void setSCR(uint64_t currentMillis) noexcept
Sets the System Clock Reference (SCR) in milliseconds of last PTS update, see set(long,...
Definition PTS.hpp:111
void setPTS(uint32_t pts) noexcept
Sets the PTS value, see set(long, int).
Definition PTS.hpp:109
constexpr_atomic bool isValid() const noexcept
Returns true if getLast() is unequal to TimeFrameI#INVALID_PTS.
Definition PTS.hpp:75
std::string getLastTimeStr(bool addFractions) const noexcept
Returns getLast() as time string representation via toTimeStr(long, boolean).
Definition PTS.hpp:92
std::string getSCRTimeStr(bool addFractions) const noexcept
Returns scr() as time string representation via toTimeStr(long, boolean).
Definition PTS.hpp:84
PTS(FloatValue speed) noexcept
Create new instance, initializing pts with TimeFrameI#INVALID_PTS and system-clock timestamp with zer...
Definition PTS.hpp:59
float getSpeed() const noexcept
Returns the external playback speed.
Definition PTS.hpp:97
constexpr_atomic uint32_t get(uint64_t currentMillis) const noexcept
Returns the last() updated PTS, interpolated by scr() System Clock Reference (SCR) delta to given cur...
Definition PTS.hpp:131
constexpr_atomic uint32_t last() const noexcept
Returns the last updated PTS value via set(long, int) w/o System Clock Reference (SCR) interpolation.
Definition PTS.hpp:89
constexpr_atomic uint32_t diffLast(const PTS &rhs) const noexcept
Returns getLast() - rhs.
Definition PTS.hpp:144
static constexpr uint32_t END_OF_STREAM_PTS
Constant marking the end of the stream PTS, i.e.
static constexpr uint32_t INVALID_PTS
Constant marking an invalid (or undefined) PTS, i.e.
Class template jau::function is a general-purpose static-polymorphic function wrapper.
ordered_atomic< uint64_t, std::memory_order_relaxed > relaxed_atomic_uint64
Relaxed non-SC atomic integral scalar uint64_t.
ordered_atomic< uint32_t, std::memory_order_relaxed > relaxed_atomic_uint32
Relaxed non-SC atomic integral scalar uint32_t.
#define constexpr_atomic
Used when designed to declare a function constexpr, but prohibited by its specific implementation.
constexpr std::string format_string_n(const std::size_t maxStrLen, const std::string_view &format, const Args &...args)
Returns a (potentially truncated) string according to snprintf() formatting rules and variable number...
uint64_t getCurrentMilliseconds() noexcept
Returns current monotonic time in milliseconds.