2015-03-03 04:38:45 +00: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 00:24:10 +00:00
|
|
|
#include "Intervals.h"
|
2015-05-04 11:16:34 +00:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2015-03-03 04:38:45 +00:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
2015-05-28 19:13:26 +00:00
|
|
|
#include "mozilla/Maybe.h"
|
2015-05-07 00:24:10 +00:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2015-03-03 04:38:45 +00:00
|
|
|
|
2015-05-25 05:09:16 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace media {
|
|
|
|
class TimeIntervals;
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|
2015-05-25 05:09:16 +00:00
|
|
|
// CopyChooser specalization for nsTArray
|
|
|
|
template<>
|
|
|
|
struct nsTArray_CopyChooser<mozilla::media::TimeIntervals>
|
|
|
|
{
|
|
|
|
typedef nsTArray_CopyWithConstructors<mozilla::media::TimeIntervals> Type;
|
|
|
|
};
|
|
|
|
|
2015-03-03 04:38:45 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2015-05-18 06:15:47 +00: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 19:13:26 +00:00
|
|
|
namespace media {
|
|
|
|
|
2015-05-18 06:15:47 +00:00
|
|
|
// Number of nanoseconds per second. 1e9.
|
|
|
|
static const int64_t NSECS_PER_S = 1000000000;
|
|
|
|
|
2015-03-03 04:38:45 +00:00
|
|
|
struct Microseconds {
|
|
|
|
Microseconds()
|
|
|
|
: mValue(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
explicit Microseconds(int64_t aValue)
|
|
|
|
: mValue(aValue)
|
|
|
|
{}
|
|
|
|
|
|
|
|
double ToSeconds() {
|
|
|
|
return double(mValue) / USECS_PER_S;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Microseconds FromSeconds(double aValue) {
|
|
|
|
MOZ_ASSERT(!IsNaN(aValue));
|
|
|
|
|
|
|
|
double val = aValue * USECS_PER_S;
|
|
|
|
if (val >= double(INT64_MAX)) {
|
|
|
|
return Microseconds(INT64_MAX);
|
|
|
|
} else if (val <= double(INT64_MIN)) {
|
|
|
|
return Microseconds(INT64_MIN);
|
|
|
|
} else {
|
|
|
|
return Microseconds(int64_t(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 00:24:10 +00:00
|
|
|
bool operator == (const Microseconds& aOther) const {
|
|
|
|
return mValue == aOther.mValue;
|
|
|
|
}
|
2015-03-03 04:38:45 +00:00
|
|
|
bool operator > (const Microseconds& aOther) const {
|
|
|
|
return mValue > aOther.mValue;
|
|
|
|
}
|
|
|
|
bool operator >= (const Microseconds& aOther) const {
|
|
|
|
return mValue >= aOther.mValue;
|
|
|
|
}
|
|
|
|
bool operator < (const Microseconds& aOther) const {
|
|
|
|
return mValue < aOther.mValue;
|
|
|
|
}
|
|
|
|
bool operator <= (const Microseconds& aOther) const {
|
|
|
|
return mValue <= aOther.mValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t mValue;
|
|
|
|
};
|
|
|
|
|
2015-05-18 06:13:20 +00:00
|
|
|
// TimeUnit at present uses a CheckedInt64 as storage.
|
|
|
|
// INT64_MAX has the special meaning of being +oo.
|
2015-05-04 11:16:34 +00:00
|
|
|
class TimeUnit final {
|
|
|
|
public:
|
|
|
|
static TimeUnit FromSeconds(double aValue) {
|
|
|
|
MOZ_ASSERT(!IsNaN(aValue));
|
|
|
|
|
2015-05-18 06:13:20 +00:00
|
|
|
if (mozilla::IsInfinite<double>(aValue)) {
|
|
|
|
return FromInfinity();
|
|
|
|
}
|
2015-05-04 11:16:34 +00:00
|
|
|
double val = aValue * USECS_PER_S;
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static TimeUnit FromMicroseconds(int64_t aValue) {
|
|
|
|
return TimeUnit(aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
static TimeUnit FromMicroseconds(Microseconds aValue) {
|
|
|
|
return TimeUnit(aValue.mValue);
|
|
|
|
}
|
|
|
|
|
2015-05-18 06:15:47 +00:00
|
|
|
static TimeUnit FromNanoseconds(int64_t aValue) {
|
|
|
|
return TimeUnit(aValue / 1000);
|
|
|
|
}
|
|
|
|
|
2015-05-18 06:13:20 +00:00
|
|
|
static TimeUnit FromInfinity() {
|
|
|
|
return TimeUnit(INT64_MAX);
|
|
|
|
}
|
|
|
|
|
2015-07-01 06:50:27 +00:00
|
|
|
static TimeUnit Invalid() {
|
|
|
|
TimeUnit ret;
|
|
|
|
ret.mValue = CheckedInt64(INT64_MAX);
|
|
|
|
// Force an overflow to render the CheckedInt invalid.
|
|
|
|
ret.mValue += 1;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-04 11:16:34 +00:00
|
|
|
int64_t ToMicroseconds() const {
|
|
|
|
return mValue.value();
|
|
|
|
}
|
|
|
|
|
2015-05-18 06:15:47 +00:00
|
|
|
int64_t ToNanoseconds() const {
|
|
|
|
return mValue.value() * 1000;
|
|
|
|
}
|
|
|
|
|
2015-05-04 11:16:34 +00:00
|
|
|
double ToSeconds() const {
|
2015-05-18 06:13:20 +00:00
|
|
|
if (IsInfinite()) {
|
|
|
|
return PositiveInfinity<double>();
|
|
|
|
}
|
2015-05-04 11:16:34 +00:00
|
|
|
return double(mValue.value()) / USECS_PER_S;
|
|
|
|
}
|
|
|
|
|
2015-05-18 06:13:20 +00:00
|
|
|
bool IsInfinite() const {
|
|
|
|
return mValue.value() == INT64_MAX;
|
|
|
|
}
|
|
|
|
|
2015-05-07 00:24:10 +00:00
|
|
|
bool operator == (const TimeUnit& aOther) const {
|
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return mValue.value() == aOther.mValue.value();
|
|
|
|
}
|
2015-06-05 01:53:05 +00:00
|
|
|
bool operator != (const TimeUnit& aOther) const {
|
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return mValue.value() != aOther.mValue.value();
|
|
|
|
}
|
2015-05-04 11:16:34 +00:00
|
|
|
bool operator >= (const TimeUnit& aOther) const {
|
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return mValue.value() >= aOther.mValue.value();
|
|
|
|
}
|
|
|
|
bool operator > (const TimeUnit& aOther) const {
|
|
|
|
return !(*this <= aOther);
|
|
|
|
}
|
|
|
|
bool operator <= (const TimeUnit& aOther) const {
|
|
|
|
MOZ_ASSERT(IsValid() && aOther.IsValid());
|
|
|
|
return mValue.value() <= aOther.mValue.value();
|
|
|
|
}
|
|
|
|
bool operator < (const TimeUnit& aOther) const {
|
|
|
|
return !(*this >= aOther);
|
|
|
|
}
|
|
|
|
TimeUnit operator + (const TimeUnit& aOther) const {
|
2015-05-18 06:13:20 +00:00
|
|
|
if (IsInfinite() || aOther.IsInfinite()) {
|
|
|
|
return FromInfinity();
|
|
|
|
}
|
2015-05-04 11:16:34 +00:00
|
|
|
return TimeUnit(mValue + aOther.mValue);
|
|
|
|
}
|
|
|
|
TimeUnit operator - (const TimeUnit& aOther) const {
|
2015-05-18 06:13:20 +00:00
|
|
|
if (IsInfinite() && !aOther.IsInfinite()) {
|
|
|
|
return FromInfinity();
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!IsInfinite() && !aOther.IsInfinite());
|
2015-05-04 11:16:34 +00:00
|
|
|
return TimeUnit(mValue - aOther.mValue);
|
|
|
|
}
|
2015-06-11 05:49:49 +00:00
|
|
|
TimeUnit& operator += (const TimeUnit& aOther) {
|
|
|
|
*this = *this + aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
TimeUnit& operator -= (const TimeUnit& aOther) {
|
|
|
|
*this = *this - aOther;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend TimeUnit operator* (int aVal, const TimeUnit& aUnit) {
|
|
|
|
return TimeUnit(aUnit.mValue * aVal);
|
|
|
|
}
|
|
|
|
friend TimeUnit operator* (const TimeUnit& aUnit, int aVal) {
|
|
|
|
return TimeUnit(aUnit.mValue * aVal);
|
|
|
|
}
|
2015-06-28 02:09:14 +00:00
|
|
|
friend TimeUnit operator/ (const TimeUnit& aUnit, int aVal) {
|
|
|
|
return TimeUnit(aUnit.mValue / aVal);
|
|
|
|
}
|
2015-05-04 11:16:34 +00:00
|
|
|
|
|
|
|
bool IsValid() const
|
|
|
|
{
|
|
|
|
return mValue.isValid();
|
|
|
|
}
|
|
|
|
|
2015-05-07 00:24:10 +00:00
|
|
|
TimeUnit()
|
|
|
|
: mValue(CheckedInt64(0))
|
|
|
|
{}
|
|
|
|
|
2015-05-04 11:16:34 +00:00
|
|
|
explicit TimeUnit(const Microseconds& aMicroseconds)
|
|
|
|
: mValue(aMicroseconds.mValue)
|
|
|
|
{}
|
2015-05-07 00:24:10 +00:00
|
|
|
TimeUnit& operator = (const Microseconds& aMicroseconds)
|
|
|
|
{
|
|
|
|
mValue = aMicroseconds.mValue;
|
|
|
|
return *this;
|
|
|
|
}
|
2015-05-04 11:16:34 +00:00
|
|
|
|
|
|
|
TimeUnit(const TimeUnit&) = default;
|
|
|
|
|
|
|
|
TimeUnit& operator = (const TimeUnit&) = default;
|
|
|
|
|
|
|
|
private:
|
|
|
|
explicit TimeUnit(CheckedInt64 aMicroseconds)
|
|
|
|
: mValue(aMicroseconds)
|
|
|
|
{}
|
|
|
|
|
|
|
|
// Our internal representation is in microseconds.
|
|
|
|
CheckedInt64 mValue;
|
|
|
|
};
|
2015-03-03 04:38:45 +00:00
|
|
|
|
2015-05-28 19:13:26 +00:00
|
|
|
typedef Maybe<TimeUnit> NullableTimeUnit;
|
|
|
|
|
2015-05-07 00:24:10 +00: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(Move(aOther))
|
|
|
|
{}
|
|
|
|
explicit TimeIntervals(const BaseType::ElemType& aOther)
|
|
|
|
: BaseType(aOther)
|
|
|
|
{}
|
|
|
|
explicit TimeIntervals(BaseType::ElemType&& aOther)
|
|
|
|
: BaseType(Move(aOther))
|
|
|
|
{}
|
|
|
|
|
2015-05-18 06:15:47 +00:00
|
|
|
static TimeIntervals Invalid()
|
|
|
|
{
|
|
|
|
return TimeIntervals(TimeInterval(TimeUnit::FromMicroseconds(INT64_MIN),
|
|
|
|
TimeUnit::FromMicroseconds(INT64_MIN)));
|
|
|
|
}
|
2015-06-17 22:07:46 +00:00
|
|
|
bool IsInvalid() const
|
2015-05-18 06:15:47 +00:00
|
|
|
{
|
|
|
|
return Length() == 1 && Start(0).ToMicroseconds() == INT64_MIN &&
|
|
|
|
End(0).ToMicroseconds() == INT64_MIN;
|
|
|
|
}
|
|
|
|
|
2015-05-07 00:24:10 +00:00
|
|
|
TimeIntervals() = default;
|
|
|
|
|
|
|
|
// Make TimeIntervals interchangeable with dom::TimeRanges.
|
|
|
|
explicit TimeIntervals(dom::TimeRanges* aRanges)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < aRanges->Length(); i++) {
|
|
|
|
ErrorResult rv;
|
|
|
|
*this +=
|
|
|
|
TimeInterval(TimeUnit::FromSeconds(aRanges->Start(i, rv)),
|
|
|
|
TimeUnit::FromSeconds(aRanges->End(i, rv)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TimeIntervals& operator = (dom::TimeRanges* aRanges)
|
|
|
|
{
|
|
|
|
*this = TimeIntervals(aRanges);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TimeIntervals FromTimeRanges(dom::TimeRanges* aRanges)
|
|
|
|
{
|
|
|
|
return TimeIntervals(aRanges);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ToTimeRanges(dom::TimeRanges* aRanges) const
|
|
|
|
{
|
|
|
|
for (IndexType i = 0; i < Length(); i++) {
|
|
|
|
aRanges->Add(Start(i).ToSeconds(), End(i).ToSeconds());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-04 11:16:34 +00:00
|
|
|
} // namespace media
|
|
|
|
} // namespace mozilla
|
2015-03-03 04:38:45 +00:00
|
|
|
|
|
|
|
#endif // TIME_UNITS_H
|