2015-03-03 07:38:45 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef TIME_UNITS_H
|
|
|
|
#define TIME_UNITS_H
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
#include "Intervals.h"
|
2015-05-04 14:16:34 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-03-03 07:38:45 +03:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2015-05-28 22:13:26 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2017-03-27 05:59:56 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2015-03-03 07:38:45 +03:00
|
|
|
|
2015-05-25 08:09:16 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace media {
|
|
|
|
class TimeIntervals;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|
2017-08-21 06:08:25 +03:00
|
|
|
// CopyChooser specialization for nsTArray
|
2015-05-25 08:09:16 +03:00
|
|
|
template <>
|
|
|
|
struct nsTArray_CopyChooser<mozilla::media::TimeIntervals> {
|
|
|
|
typedef nsTArray_CopyWithConstructors<mozilla::media::TimeIntervals> Type;
|
|
|
|
};
|
|
|
|
|
2015-03-03 07:38:45 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-05-18 09:15:47 +03:00
|
|
|
// Number of microseconds per second. 1e6.
|
|
|
|
static const int64_t USECS_PER_S = 1000000;
|
|
|
|
|
|
|
|
// Number of microseconds per millisecond.
|
|
|
|
static const int64_t USECS_PER_MS = 1000;
|
|
|
|
|
2015-05-28 22:13:26 +03:00
|
|
|
namespace media {
|
|
|
|
|
2015-05-18 09:15:47 +03:00
|
|
|
// Number of nanoseconds per second. 1e9.
|
|
|
|
static const int64_t NSECS_PER_S = 1000000000;
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
// TimeUnit at present uses a CheckedInt64 as storage.
|
|
|
|
// INT64_MAX has the special meaning of being +oo.
|
2017-09-12 12:09:15 +03:00
|
|
|
class TimeUnit final {
|
2015-05-04 14:16:34 +03:00
|
|
|
public:
|
2017-09-12 12:09:15 +03:00
|
|
|
static TimeUnit FromSeconds(double aValue) {
|
2015-05-04 14:16:34 +03:00
|
|
|
MOZ_ASSERT(!IsNaN(aValue));
|
|
|
|
|
2015-05-18 09:13:20 +03:00
|
|
|
if (mozilla::IsInfinite<double>(aValue)) {
|
2019-05-08 09:27:03 +03:00
|
|
|
return aValue > 0 ? FromInfinity() : FromNegativeInfinity();
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
2015-11-20 03:06:22 +03:00
|
|
|
// Due to internal double representation, this
|
|
|
|
// operation is not commutative, do not attempt to simplify.
|
2018-02-23 16:15:21 +03:00
|
|
|
double halfUsec = .0000005;
|
|
|
|
double val =
|
|
|
|
(aValue <= 0 ? aValue - halfUsec : aValue + halfUsec) * USECS_PER_S;
|
2015-05-04 14:16:34 +03:00
|
|
|
if (val >= double(INT64_MAX)) {
|
|
|
|
return FromMicroseconds(INT64_MAX);
|
|
|
|
} else if (val <= double(INT64_MIN)) {
|
|
|
|
return FromMicroseconds(INT64_MIN);
|
|
|
|
} else {
|
|
|
|
return FromMicroseconds(int64_t(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
static constexpr TimeUnit FromMicroseconds(int64_t aValue) {
|
2015-05-04 14:16:34 +03:00
|
|
|
return TimeUnit(aValue);
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
static constexpr TimeUnit FromNanoseconds(int64_t aValue) {
|
2015-05-18 09:15:47 +03:00
|
|
|
return TimeUnit(aValue / 1000);
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
static constexpr TimeUnit FromInfinity() { return TimeUnit(INT64_MAX); }
|
2015-05-18 09:13:20 +03:00
|
|
|
|
2019-05-08 09:27:03 +03:00
|
|
|
static constexpr TimeUnit FromNegativeInfinity() {
|
|
|
|
return TimeUnit(INT64_MIN);
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
static TimeUnit FromTimeDuration(const TimeDuration& aDuration) {
|
2017-03-27 05:59:56 +03:00
|
|
|
return FromSeconds(aDuration.ToSeconds());
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
static constexpr TimeUnit Zero() { return TimeUnit(0); }
|
2017-03-27 05:59:56 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
static TimeUnit Invalid() {
|
2015-07-01 09:50:27 +03:00
|
|
|
TimeUnit ret;
|
|
|
|
ret.mValue = CheckedInt64(INT64_MAX);
|
|
|
|
// Force an overflow to render the CheckedInt invalid.
|
|
|
|
ret.mValue += 1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
int64_t ToMicroseconds() const { return mValue.value(); }
|
2015-05-04 14:16:34 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
int64_t ToNanoseconds() const { return mValue.value() * 1000; }
|
2015-05-18 09:15:47 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
double ToSeconds() const {
|
2019-05-08 09:27:03 +03:00
|
|
|
if (IsPosInf()) {
|
2015-05-18 09:13:20 +03:00
|
|
|
return PositiveInfinity<double>();
|
|
|
|
}
|
2019-05-08 09:27:03 +03:00
|
|
|
if (IsNegInf()) {
|
|
|
|
return NegativeInfinity<double>();
|
|
|
|
}
|
2015-05-04 14:16:34 +03:00
|
|
|
return double(mValue.value()) / USECS_PER_S;
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
TimeDuration ToTimeDuration() const {
|
2017-03-27 05:59:56 +03:00
|
|
|
return TimeDuration::FromMicroseconds(mValue.value());
|
|
|
|
}
|
|
|
|
|
2019-05-08 09:27:03 +03:00
|
|
|
bool IsInfinite() const { return IsPosInf() || IsNegInf(); }
|
2015-05-18 09:13:20 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
bool IsPositive() const { return mValue.value() > 0; }
|
2017-04-12 11:41:36 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
bool IsNegative() const { return mValue.value() < 0; }
|
2017-04-12 12:27:34 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
bool operator==(const TimeUnit& aOther) const {
|
2015-05-07 03:24:10 +03:00
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return mValue.value() == aOther.mValue.value();
|
|
|
|
}
|
2017-09-12 12:09:15 +03:00
|
|
|
bool operator!=(const TimeUnit& aOther) const {
|
2015-06-05 04:53:05 +03:00
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return mValue.value() != aOther.mValue.value();
|
|
|
|
}
|
2017-09-12 12:09:15 +03:00
|
|
|
bool operator>=(const TimeUnit& aOther) const {
|
2015-05-04 14:16:34 +03:00
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return mValue.value() >= aOther.mValue.value();
|
|
|
|
}
|
2017-09-12 12:09:15 +03:00
|
|
|
bool operator>(const TimeUnit& aOther) const { return !(*this <= aOther); }
|
|
|
|
bool operator<=(const TimeUnit& aOther) const {
|
2015-05-04 14:16:34 +03:00
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return mValue.value() <= aOther.mValue.value();
|
|
|
|
}
|
2017-09-12 12:09:15 +03:00
|
|
|
bool operator<(const TimeUnit& aOther) const { return !(*this >= aOther); }
|
2019-04-19 16:16:43 +03:00
|
|
|
TimeUnit operator%(const TimeUnit& aOther) const {
|
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return TimeUnit(mValue % aOther.mValue);
|
|
|
|
}
|
2019-05-08 09:27:03 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
TimeUnit operator+(const TimeUnit& aOther) const {
|
2015-05-18 09:13:20 +03:00
|
|
|
if (IsInfinite() || aOther.IsInfinite()) {
|
2019-05-08 09:27:03 +03:00
|
|
|
// When adding at least one infinite value, the result is either
|
|
|
|
// +/-Inf, or NaN. So do the calculation in floating point for
|
|
|
|
// simplicity.
|
|
|
|
double result = ToSeconds() + aOther.ToSeconds();
|
|
|
|
return IsNaN(result) ? TimeUnit::Invalid() : FromSeconds(result);
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
2015-05-04 14:16:34 +03:00
|
|
|
return TimeUnit(mValue + aOther.mValue);
|
|
|
|
}
|
2019-05-08 09:27:03 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
TimeUnit operator-(const TimeUnit& aOther) const {
|
2019-05-08 09:27:03 +03:00
|
|
|
if (IsInfinite() || aOther.IsInfinite()) {
|
|
|
|
// When subtracting at least one infinite value, the result is either
|
|
|
|
// +/-Inf, or NaN. So do the calculation in floating point for
|
|
|
|
// simplicity.
|
|
|
|
double result = ToSeconds() - aOther.ToSeconds();
|
|
|
|
return IsNaN(result) ? TimeUnit::Invalid() : FromSeconds(result);
|
2015-05-18 09:13:20 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(!IsInfinite() && !aOther.IsInfinite());
|
2015-05-04 14:16:34 +03:00
|
|
|
return TimeUnit(mValue - aOther.mValue);
|
|
|
|
}
|
2017-09-12 12:09:15 +03:00
|
|
|
TimeUnit& operator+=(const TimeUnit& aOther) {
|
2015-06-11 08:49:49 +03:00
|
|
|
*this = *this + aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
2017-09-12 12:09:15 +03:00
|
|
|
TimeUnit& operator-=(const TimeUnit& aOther) {
|
2015-06-11 08:49:49 +03:00
|
|
|
*this = *this - aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
template <typename T>
|
|
|
|
TimeUnit operator*(T aVal) const {
|
2017-03-28 09:10:31 +03:00
|
|
|
// See bug 853398 for the reason to block double multiplier.
|
|
|
|
// If required, use MultDouble below and with caution.
|
|
|
|
static_assert(mozilla::IsIntegral<T>::value, "Must be an integral type");
|
|
|
|
return TimeUnit(mValue * aVal);
|
|
|
|
}
|
2017-09-12 12:09:15 +03:00
|
|
|
TimeUnit MultDouble(double aVal) const {
|
2017-03-28 09:10:31 +03:00
|
|
|
return TimeUnit::FromSeconds(ToSeconds() * aVal);
|
2015-06-11 08:49:49 +03:00
|
|
|
}
|
2019-04-19 16:16:43 +03:00
|
|
|
friend TimeUnit operator/(const TimeUnit& aUnit, int64_t aVal) {
|
2019-07-13 01:05:06 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(0 <= aVal && aVal <= UINT32_MAX);
|
2015-06-28 05:09:14 +03:00
|
|
|
return TimeUnit(aUnit.mValue / aVal);
|
|
|
|
}
|
2019-04-19 16:16:43 +03:00
|
|
|
friend TimeUnit operator%(const TimeUnit& aUnit, int64_t aVal) {
|
2019-07-13 01:05:06 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(0 <= aVal && aVal <= UINT32_MAX);
|
2017-11-24 05:28:42 +03:00
|
|
|
return TimeUnit(aUnit.mValue % aVal);
|
|
|
|
}
|
2015-05-04 14:16:34 +03:00
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
bool IsValid() const { return mValue.isValid(); }
|
2015-05-04 14:16:34 +03:00
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
constexpr TimeUnit() : mValue(CheckedInt64(0)) {}
|
|
|
|
|
2015-05-04 14:16:34 +03:00
|
|
|
TimeUnit(const TimeUnit&) = default;
|
|
|
|
|
2017-09-12 12:09:15 +03:00
|
|
|
TimeUnit& operator=(const TimeUnit&) = default;
|
2015-05-04 14:16:34 +03:00
|
|
|
|
2019-05-08 09:27:03 +03:00
|
|
|
bool IsPosInf() const {
|
|
|
|
return mValue.isValid() && mValue.value() == INT64_MAX;
|
|
|
|
}
|
|
|
|
bool IsNegInf() const {
|
|
|
|
return mValue.isValid() && mValue.value() == INT64_MIN;
|
|
|
|
}
|
|
|
|
|
2015-05-04 14:16:34 +03:00
|
|
|
private:
|
2017-03-27 05:59:56 +03:00
|
|
|
explicit constexpr TimeUnit(CheckedInt64 aMicroseconds)
|
2015-05-04 14:16:34 +03:00
|
|
|
: mValue(aMicroseconds) {}
|
|
|
|
|
|
|
|
// Our internal representation is in microseconds.
|
|
|
|
CheckedInt64 mValue;
|
|
|
|
};
|
2015-03-03 07:38:45 +03:00
|
|
|
|
2015-05-28 22:13:26 +03:00
|
|
|
typedef Maybe<TimeUnit> NullableTimeUnit;
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
typedef Interval<TimeUnit> TimeInterval;
|
|
|
|
|
|
|
|
class TimeIntervals : public IntervalSet<TimeUnit> {
|
|
|
|
public:
|
|
|
|
typedef IntervalSet<TimeUnit> BaseType;
|
|
|
|
|
|
|
|
// We can't use inherited constructors yet. So we have to duplicate all the
|
|
|
|
// constructors found in IntervalSet base class.
|
|
|
|
// all this could be later replaced with:
|
|
|
|
// using IntervalSet<TimeUnit>::IntervalSet;
|
|
|
|
|
|
|
|
// MOZ_IMPLICIT as we want to enable initialization in the form:
|
|
|
|
// TimeIntervals i = ... like we would do with IntervalSet<T> i = ...
|
|
|
|
MOZ_IMPLICIT TimeIntervals(const BaseType& aOther) : BaseType(aOther) {}
|
|
|
|
MOZ_IMPLICIT TimeIntervals(BaseType&& aOther) : BaseType(std::move(aOther)) {}
|
|
|
|
explicit TimeIntervals(const BaseType::ElemType& aOther) : BaseType(aOther) {}
|
|
|
|
explicit TimeIntervals(BaseType::ElemType&& aOther)
|
2018-05-30 22:15:35 +03:00
|
|
|
: BaseType(std::move(aOther)) {}
|
2015-05-07 03:24:10 +03:00
|
|
|
|
2015-05-18 09:15:47 +03:00
|
|
|
static TimeIntervals Invalid() {
|
2019-05-08 09:27:03 +03:00
|
|
|
return TimeIntervals(TimeInterval(TimeUnit::FromNegativeInfinity(),
|
|
|
|
TimeUnit::FromNegativeInfinity()));
|
2015-05-18 09:15:47 +03:00
|
|
|
}
|
2015-06-18 01:07:46 +03:00
|
|
|
bool IsInvalid() const {
|
2019-05-08 09:27:03 +03:00
|
|
|
return Length() == 1 && Start(0).IsNegInf() && End(0).IsNegInf();
|
2015-05-18 09:15:47 +03:00
|
|
|
}
|
|
|
|
|
2015-05-07 03:24:10 +03:00
|
|
|
TimeIntervals() = default;
|
|
|
|
};
|
|
|
|
|
2015-05-04 14:16:34 +03:00
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|
2015-03-03 07:38:45 +03:00
|
|
|
|
|
|
|
#endif // TIME_UNITS_H
|