2015-05-03 22:32:37 +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: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-08-05 11:40:35 +04:00
|
|
|
|
2013-03-02 23:14:44 +04:00
|
|
|
#include "mozilla/dom/TimeRanges.h"
|
2013-03-02 23:16:43 +04:00
|
|
|
#include "mozilla/dom/TimeRangesBinding.h"
|
2013-03-19 16:23:54 +04:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2017-08-21 06:08:25 +03:00
|
|
|
#include "TimeUnits.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2010-08-05 11:40:35 +04:00
|
|
|
|
2013-03-02 23:14:44 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2010-08-05 11:40:35 +04:00
|
|
|
|
2015-10-19 09:10:47 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TimeRanges, mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(TimeRanges)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(TimeRanges)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TimeRanges)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
2010-08-05 11:40:35 +04:00
|
|
|
|
2013-03-02 23:14:44 +04:00
|
|
|
TimeRanges::TimeRanges() : mParent(nullptr) {}
|
2015-10-19 09:10:47 +03:00
|
|
|
|
|
|
|
TimeRanges::TimeRanges(nsISupports* aParent) : mParent(aParent) {}
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2017-08-21 06:08:25 +03:00
|
|
|
TimeRanges::TimeRanges(nsISupports* aParent,
|
|
|
|
const media::TimeIntervals& aTimeIntervals)
|
|
|
|
: TimeRanges(aParent) {
|
|
|
|
if (aTimeIntervals.IsInvalid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const media::TimeInterval& interval : aTimeIntervals) {
|
|
|
|
Add(interval.mStart.ToSeconds(), interval.mEnd.ToSeconds());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeRanges::TimeRanges(const media::TimeIntervals& aTimeIntervals)
|
|
|
|
: TimeRanges(nullptr, aTimeIntervals) {}
|
|
|
|
|
|
|
|
media::TimeIntervals TimeRanges::ToTimeIntervals() const {
|
|
|
|
media::TimeIntervals t;
|
|
|
|
for (uint32_t i = 0; i < Length(); i++) {
|
2018-02-16 21:36:40 +03:00
|
|
|
t += media::TimeInterval(media::TimeUnit::FromSeconds(Start(i)),
|
|
|
|
media::TimeUnit::FromSeconds(End(i)));
|
2017-08-21 06:08:25 +03:00
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2013-03-02 23:14:44 +04:00
|
|
|
TimeRanges::~TimeRanges() {}
|
2011-03-24 01:28:57 +03:00
|
|
|
|
2017-08-21 06:08:25 +03:00
|
|
|
double TimeRanges::Start(uint32_t aIndex, ErrorResult& aRv) const {
|
2013-03-02 23:16:43 +04:00
|
|
|
if (aIndex >= mRanges.Length()) {
|
|
|
|
aRv = NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-16 21:36:40 +03:00
|
|
|
return Start(aIndex);
|
2013-03-02 23:16:43 +04:00
|
|
|
}
|
|
|
|
|
2017-08-21 06:08:25 +03:00
|
|
|
double TimeRanges::End(uint32_t aIndex, ErrorResult& aRv) const {
|
2013-03-02 23:16:43 +04:00
|
|
|
if (aIndex >= mRanges.Length()) {
|
|
|
|
aRv = NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-16 21:36:40 +03:00
|
|
|
return End(aIndex);
|
2010-08-05 11:40:35 +04:00
|
|
|
}
|
|
|
|
|
2013-03-02 23:14:44 +04:00
|
|
|
void TimeRanges::Add(double aStart, double aEnd) {
|
2012-05-01 04:29:24 +04:00
|
|
|
if (aStart > aEnd) {
|
|
|
|
NS_WARNING("Can't add a range if the end is older that the start.");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-05 11:40:35 +04:00
|
|
|
mRanges.AppendElement(TimeRange(aStart, aEnd));
|
|
|
|
}
|
2012-05-01 04:29:24 +04:00
|
|
|
|
2014-04-14 15:23:00 +04:00
|
|
|
double TimeRanges::GetStartTime() {
|
2013-01-29 06:34:27 +04:00
|
|
|
if (mRanges.IsEmpty()) {
|
|
|
|
return -1.0;
|
|
|
|
}
|
2014-04-14 15:23:00 +04:00
|
|
|
return mRanges[0].mStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
double TimeRanges::GetEndTime() {
|
|
|
|
if (mRanges.IsEmpty()) {
|
|
|
|
return -1.0;
|
|
|
|
}
|
|
|
|
return mRanges[mRanges.Length() - 1].mEnd;
|
2013-01-29 06:34:27 +04:00
|
|
|
}
|
|
|
|
|
2015-01-31 04:45:49 +03:00
|
|
|
void TimeRanges::Normalize(double aTolerance) {
|
2012-05-01 04:29:24 +04:00
|
|
|
if (mRanges.Length() >= 2) {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<TimeRange, 4> normalized;
|
2012-05-01 04:29:24 +04:00
|
|
|
|
|
|
|
mRanges.Sort(CompareTimeRanges());
|
|
|
|
|
|
|
|
// This merges the intervals.
|
|
|
|
TimeRange current(mRanges[0]);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 1; i < mRanges.Length(); i++) {
|
2012-06-07 03:43:13 +04:00
|
|
|
if (current.mStart <= mRanges[i].mStart &&
|
|
|
|
current.mEnd >= mRanges[i].mEnd) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-31 04:45:49 +03:00
|
|
|
if (current.mEnd + aTolerance >= mRanges[i].mStart) {
|
2012-05-01 04:29:24 +04:00
|
|
|
current.mEnd = mRanges[i].mEnd;
|
|
|
|
} else {
|
|
|
|
normalized.AppendElement(current);
|
|
|
|
current = mRanges[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
normalized.AppendElement(current);
|
|
|
|
|
|
|
|
mRanges = normalized;
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 23:14:44 +04:00
|
|
|
|
2015-01-31 04:45:49 +03:00
|
|
|
void TimeRanges::Union(const TimeRanges* aOtherRanges, double aTolerance) {
|
2014-08-11 08:32:21 +04:00
|
|
|
mRanges.AppendElements(aOtherRanges->mRanges);
|
2015-01-31 04:45:49 +03:00
|
|
|
Normalize(aTolerance);
|
2014-08-11 08:32:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TimeRanges::Intersection(const TimeRanges* aOtherRanges) {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<TimeRange, 4> intersection;
|
2014-08-11 08:32:21 +04:00
|
|
|
|
|
|
|
const nsTArray<TimeRange>& otherRanges = aOtherRanges->mRanges;
|
|
|
|
for (index_type i = 0, j = 0;
|
|
|
|
i < mRanges.Length() && j < otherRanges.Length();) {
|
|
|
|
double start = std::max(mRanges[i].mStart, otherRanges[j].mStart);
|
|
|
|
double end = std::min(mRanges[i].mEnd, otherRanges[j].mEnd);
|
|
|
|
if (start < end) {
|
|
|
|
intersection.AppendElement(TimeRange(start, end));
|
|
|
|
}
|
|
|
|
if (mRanges[i].mEnd < otherRanges[j].mEnd) {
|
|
|
|
i += 1;
|
|
|
|
} else {
|
|
|
|
j += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mRanges = intersection;
|
|
|
|
}
|
|
|
|
|
2015-01-31 04:45:49 +03:00
|
|
|
TimeRanges::index_type TimeRanges::Find(double aTime,
|
|
|
|
double aTolerance /* = 0 */) {
|
2014-08-11 06:05:09 +04:00
|
|
|
for (index_type i = 0; i < mRanges.Length(); ++i) {
|
2015-01-31 04:45:49 +03:00
|
|
|
if (aTime < mRanges[i].mEnd && (aTime + aTolerance) >= mRanges[i].mStart) {
|
2014-08-11 06:05:09 +04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoIndex;
|
|
|
|
}
|
|
|
|
|
2015-10-19 09:10:47 +03:00
|
|
|
JSObject* TimeRanges::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return TimeRanges_Binding::Wrap(aCx, this, aGivenProto);
|
2015-10-19 09:10:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports* TimeRanges::GetParentObject() const { return mParent; }
|
2013-03-02 23:16:43 +04:00
|
|
|
|
2015-02-09 15:28:59 +03:00
|
|
|
void TimeRanges::Shift(double aOffset) {
|
|
|
|
for (index_type i = 0; i < mRanges.Length(); ++i) {
|
|
|
|
mRanges[i].mStart += aOffset;
|
|
|
|
mRanges[i].mEnd += aOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-02 23:14:44 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|