1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "Intervals.h"
11 #include "mozilla/CheckedInt.h"
12 #include "mozilla/FloatingPoint.h"
13 #include "mozilla/Maybe.h"
14 #include "mozilla/TimeStamp.h"
20 } // namespace mozilla
21 // CopyChooser specialization for nsTArray
23 struct nsTArray_CopyChooser
<mozilla::media::TimeIntervals
>
25 typedef nsTArray_CopyWithConstructors
<mozilla::media::TimeIntervals
> Type
;
30 // Number of microseconds per second. 1e6.
31 static const int64_t USECS_PER_S
= 1000000;
33 // Number of microseconds per millisecond.
34 static const int64_t USECS_PER_MS
= 1000;
38 // Number of nanoseconds per second. 1e9.
39 static const int64_t NSECS_PER_S
= 1000000000;
41 // TimeUnit at present uses a CheckedInt64 as storage.
42 // INT64_MAX has the special meaning of being +oo.
46 static TimeUnit
FromSeconds(double aValue
)
48 MOZ_ASSERT(!IsNaN(aValue
));
50 if (mozilla::IsInfinite
<double>(aValue
)) {
51 return FromInfinity();
53 // Due to internal double representation, this
54 // operation is not commutative, do not attempt to simplify.
55 double halfUsec
= .0000005;
57 (aValue
<= 0 ? aValue
- halfUsec
: aValue
+ halfUsec
) * USECS_PER_S
;
58 if (val
>= double(INT64_MAX
)) {
59 return FromMicroseconds(INT64_MAX
);
60 } else if (val
<= double(INT64_MIN
)) {
61 return FromMicroseconds(INT64_MIN
);
63 return FromMicroseconds(int64_t(val
));
67 static constexpr TimeUnit
FromMicroseconds(int64_t aValue
)
69 return TimeUnit(aValue
);
72 static constexpr TimeUnit
FromNanoseconds(int64_t aValue
)
74 return TimeUnit(aValue
/ 1000);
77 static constexpr TimeUnit
FromInfinity() { return TimeUnit(INT64_MAX
); }
79 static TimeUnit
FromTimeDuration(const TimeDuration
& aDuration
)
81 return FromSeconds(aDuration
.ToSeconds());
84 static constexpr TimeUnit
Zero() { return TimeUnit(0); }
86 static TimeUnit
Invalid()
89 ret
.mValue
= CheckedInt64(INT64_MAX
);
90 // Force an overflow to render the CheckedInt invalid.
95 int64_t ToMicroseconds() const { return mValue
.value(); }
97 int64_t ToNanoseconds() const { return mValue
.value() * 1000; }
99 double ToSeconds() const
102 return PositiveInfinity
<double>();
104 return double(mValue
.value()) / USECS_PER_S
;
107 TimeDuration
ToTimeDuration() const
109 return TimeDuration::FromMicroseconds(mValue
.value());
112 bool IsInfinite() const { return mValue
.value() == INT64_MAX
; }
114 bool IsPositive() const { return mValue
.value() > 0; }
116 bool IsNegative() const { return mValue
.value() < 0; }
118 bool operator==(const TimeUnit
& aOther
) const
120 MOZ_ASSERT(IsValid() && aOther
.IsValid());
121 return mValue
.value() == aOther
.mValue
.value();
123 bool operator!=(const TimeUnit
& aOther
) const
125 MOZ_ASSERT(IsValid() && aOther
.IsValid());
126 return mValue
.value() != aOther
.mValue
.value();
128 bool operator>=(const TimeUnit
& aOther
) const
130 MOZ_ASSERT(IsValid() && aOther
.IsValid());
131 return mValue
.value() >= aOther
.mValue
.value();
133 bool operator>(const TimeUnit
& aOther
) const { return !(*this <= aOther
); }
134 bool operator<=(const TimeUnit
& aOther
) const
136 MOZ_ASSERT(IsValid() && aOther
.IsValid());
137 return mValue
.value() <= aOther
.mValue
.value();
139 bool operator<(const TimeUnit
& aOther
) const { return !(*this >= aOther
); }
140 TimeUnit
operator+(const TimeUnit
& aOther
) const
142 if (IsInfinite() || aOther
.IsInfinite()) {
143 return FromInfinity();
145 return TimeUnit(mValue
+ aOther
.mValue
);
147 TimeUnit
operator-(const TimeUnit
& aOther
) const
149 if (IsInfinite() && !aOther
.IsInfinite()) {
150 return FromInfinity();
152 MOZ_ASSERT(!IsInfinite() && !aOther
.IsInfinite());
153 return TimeUnit(mValue
- aOther
.mValue
);
155 TimeUnit
& operator+=(const TimeUnit
& aOther
)
157 *this = *this + aOther
;
160 TimeUnit
& operator-=(const TimeUnit
& aOther
)
162 *this = *this - aOther
;
167 TimeUnit
operator*(T aVal
) const
169 // See bug 853398 for the reason to block double multiplier.
170 // If required, use MultDouble below and with caution.
171 static_assert(mozilla::IsIntegral
<T
>::value
, "Must be an integral type");
172 return TimeUnit(mValue
* aVal
);
174 TimeUnit
MultDouble(double aVal
) const
176 return TimeUnit::FromSeconds(ToSeconds() * aVal
);
178 friend TimeUnit
operator/(const TimeUnit
& aUnit
, int aVal
)
180 return TimeUnit(aUnit
.mValue
/ aVal
);
182 friend TimeUnit
operator%(const TimeUnit
& aUnit
, int aVal
)
184 return TimeUnit(aUnit
.mValue
% aVal
);
187 bool IsValid() const { return mValue
.isValid(); }
190 : mValue(CheckedInt64(0))
194 TimeUnit(const TimeUnit
&) = default;
196 TimeUnit
& operator=(const TimeUnit
&) = default;
199 explicit constexpr TimeUnit(CheckedInt64 aMicroseconds
)
200 : mValue(aMicroseconds
)
204 // Our internal representation is in microseconds.
208 typedef Maybe
<TimeUnit
> NullableTimeUnit
;
210 typedef Interval
<TimeUnit
> TimeInterval
;
212 class TimeIntervals
: public IntervalSet
<TimeUnit
>
215 typedef IntervalSet
<TimeUnit
> BaseType
;
217 // We can't use inherited constructors yet. So we have to duplicate all the
218 // constructors found in IntervalSet base class.
219 // all this could be later replaced with:
220 // using IntervalSet<TimeUnit>::IntervalSet;
222 // MOZ_IMPLICIT as we want to enable initialization in the form:
223 // TimeIntervals i = ... like we would do with IntervalSet<T> i = ...
224 MOZ_IMPLICIT
TimeIntervals(const BaseType
& aOther
)
228 MOZ_IMPLICIT
TimeIntervals(BaseType
&& aOther
)
229 : BaseType(Move(aOther
))
232 explicit TimeIntervals(const BaseType::ElemType
& aOther
)
236 explicit TimeIntervals(BaseType::ElemType
&& aOther
)
237 : BaseType(Move(aOther
))
241 static TimeIntervals
Invalid()
243 return TimeIntervals(TimeInterval(TimeUnit::FromMicroseconds(INT64_MIN
),
244 TimeUnit::FromMicroseconds(INT64_MIN
)));
246 bool IsInvalid() const
248 return Length() == 1 && Start(0).ToMicroseconds() == INT64_MIN
&&
249 End(0).ToMicroseconds() == INT64_MIN
;
252 TimeIntervals() = default;
256 } // namespace mozilla
258 #endif // TIME_UNITS_H