Repository navigation
Expand file tree
/
Copy pathAampTime.h
More file actions
268 lines (214 loc) · 7.73 KB
/
Copy pathAampTime.h
File metadata and controls
268 lines (214 loc) · 7.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2023 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdint>
#include <ostream>
#include <cmath>
#ifndef AAMPTIME_H
#define AAMPTIME_H
/// @brief struct to hold time in ticks and timescale
struct AampTicks
{
int64_t ticks;
uint32_t timescale;
/// @brief Constructor
/// @param ticks
/// @param timescale
AampTicks(int64_t ticks, uint32_t timescale) : ticks(ticks), timescale(timescale) {}
/// @brief Get time in milliseconds
int64_t inMilli() { return (ticks * 1000) / (int64_t)timescale; }
};
/// @brief time class to work around the use of doubles within Aamp
// While operators are overloaded for comparisons, the underlying data type is integer
// But the code is tolerant of being treated as a double
class AampTime
{
public:
typedef enum { milli = 1000, micro = 1000000, nano = 1000000000 } TimeScale;
private:
static const uint64_t baseTimescale = nano;
int64_t baseTime;
public:
/// @brief Constructor
/// @param seconds time in seconds, as a double
constexpr AampTime(double seconds = 0.0) : baseTime(int64_t(seconds * baseTimescale)){}
/// @brief Copy constructor
/// @param rhs AampTime object to copy
constexpr AampTime(const AampTime& rhs) : baseTime(rhs.baseTime){}
/// @brief Constructor
/// @param time struct containing time in ticks and timescale
/// @note This is used to convert from AampTicks to AampTime; it is lossy and cannot be converted back
constexpr AampTime(AampTicks &time) : baseTime((time.ticks * (int64_t)baseTimescale) / (int64_t)time.timescale) {}
/// @brief Get the stored time
/// @return Time in seconds (double)
inline double inSeconds() const { return (baseTime / double(baseTimescale)); }
/// @brief Get the stored time in seconds
/// @return Time in seconds (integer)
inline int64_t seconds() const { return (baseTime / baseTimescale); }
/// @brief Get the stored time in milliseconds
/// @return Time in milliseconds (integer)
inline int64_t milliseconds() const { return (baseTime / (baseTimescale / milli)); }
// Equivalent to round() but in integer domain
inline int64_t nearestSecond() const
{
int64_t retval = this->seconds();
// Fractional part
int64_t tempval = baseTime - retval * baseTimescale;
if (tempval >= ((5 * baseTimescale)/10))
{
retval += 1;
}
return retval;
}
// Overloads for comparison operators to check AampTime : AampTime and AampTime : double
// Converting (and truncating) the double to the timescale should avoid the issues around epsilon for floating point
inline bool operator==(const AampTime &rhs) const
{
if (this == &rhs)
return true;
else
return (baseTime == rhs.baseTime);
}
inline bool operator==(const double &rhs) const { return (baseTime == int64_t(rhs * baseTimescale)); }
inline AampTime& operator=(const AampTime &rhs)
{
if (this == &rhs)
return *this;
baseTime = rhs.baseTime;
return *this;
}
inline AampTime& operator=(const double &rhs)
{
baseTime = int64_t(rhs * baseTimescale);
return *this;
}
inline AampTime operator-() const
{
AampTime temp(*this);
temp.baseTime = -baseTime;
return temp;
}
inline bool operator!=(const AampTime &rhs) const { return !(*this == rhs); }
inline bool operator!=(double &rhs) const { return !(*this == rhs); }
inline bool operator>(const AampTime &rhs) const { return (baseTime > rhs.baseTime); }
inline bool operator>(const double &rhs) const { return (baseTime > int64_t(rhs * baseTimescale)); }
inline bool operator<(const AampTime &rhs) const { return ((*this != rhs) && (!(*this > rhs))); }
inline bool operator<(const double &rhs) const { return ((*this != rhs) && (!(*this > rhs))); }
inline bool operator>=(const AampTime &rhs) const { return ((*this > rhs) || (*this == rhs)); }
inline bool operator>=(double rhs) const { return ((*this > rhs) || (*this == rhs)); }
inline bool operator<=(const AampTime &rhs) const { return ((*this < rhs) || (*this == rhs)); }
inline bool operator<=(double rhs) const { return ((*this < rhs) || (*this == rhs)); }
inline AampTime operator+(const AampTime &t) const
{
AampTime temp(*this);
temp.baseTime = baseTime + t.baseTime;
return temp;
}
inline AampTime operator+(const double &t) const
{
AampTime temp(*this);
temp.baseTime = baseTime + int64_t(t * baseTimescale);
return std::move(temp);
}
inline const AampTime &operator+=(const AampTime &t)
{
*this = *this + t;
return *this;
}
inline const AampTime &operator+=(const double &t)
{
*this = *this + t;
return *this;
}
inline AampTime operator-(const AampTime &t) const
{
AampTime temp(*this);
temp.baseTime = baseTime - t.baseTime;
return std::move(temp);
}
inline AampTime operator-(const double &t) const
{
AampTime temp(*this);
temp.baseTime = baseTime - int64_t(t * baseTimescale);
return std::move(temp);
}
inline const AampTime &operator-=(const AampTime &t)
{
*this = *this - t;
return *this;
}
inline const AampTime &operator-=(const double &t)
{
*this = *this - t;
return *this;
}
inline AampTime operator/(const double &t) const
{
AampTime temp(*this);
temp.baseTime = (int64_t)((double)baseTime/t);
return std::move(temp);
}
inline AampTime operator*(const double &t) const
{
AampTime temp(*this);
temp.baseTime = (int64_t)((double)baseTime * t);
return std::move(temp);
}
explicit operator double() const { return this->inSeconds(); }
explicit operator int64_t() const { return this->seconds(); }
};
// For those who like if (0.0 == b)
inline bool operator==(const double& lhs, const AampTime& rhs) { return (rhs.operator==(lhs)); };
inline bool operator!=(const double& lhs, const AampTime& rhs) { return !(rhs == lhs); };
inline AampTime operator+(const double &lhs, const AampTime &rhs) { return rhs + lhs; };
inline AampTime operator-(const double &lhs, const AampTime &rhs) { return -rhs + lhs; };
inline AampTime operator*(const int64_t &lhs, const AampTime &rhs) { return rhs * lhs; };
// Adding double & AampTime and expecting a double will need to use AampTime::inSeconds() instead
// Where a double is to be passed by reference, if the prototype cannot be rewritten or overloaded then
// a temporary double will be needed
inline double operator+=(double &lhs, const AampTime &rhs)
{
lhs = lhs + rhs.inSeconds();
return lhs;
}
inline bool operator>(const double &lhs, const AampTime &rhs) { return (rhs.operator<(lhs)); };
inline bool operator<(const double &lhs, const AampTime &rhs) { return (rhs.operator>(lhs)); };
inline bool operator<=(const double &lhs, const AampTime &rhs) { return (rhs >= lhs); };
inline bool operator>=(const double &lhs, const AampTime &rhs) { return (rhs <= lhs); };
// Is stream operator used?
inline std::ostream &operator<<(std::ostream &out, const AampTime& t)
{
return out << t.inSeconds();
}
inline double abs(AampTime t)
{
return std::abs(t.inSeconds());
}
inline double fabs(AampTime t)
{
return std::fabs(t.inSeconds());
}
inline double round(AampTime t)
{
return std::round(t.inSeconds());
}
inline double floor(AampTime t)
{
return std::floor(t.inSeconds());
}
#endif