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/. */
7 #include "mozilla/dom/TimeRanges.h"
8 #include "mozilla/dom/TimeRangesBinding.h"
9 #include "mozilla/dom/HTMLMediaElement.h"
10 #include "TimeUnits.h"
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TimeRanges
, mParent
)
17 NS_IMPL_CYCLE_COLLECTING_ADDREF(TimeRanges
)
18 NS_IMPL_CYCLE_COLLECTING_RELEASE(TimeRanges
)
19 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TimeRanges
)
20 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
21 NS_INTERFACE_MAP_ENTRY(nsISupports
)
24 TimeRanges::TimeRanges()
29 TimeRanges::TimeRanges(nsISupports
* aParent
)
34 TimeRanges::TimeRanges(nsISupports
* aParent
,
35 const media::TimeIntervals
& aTimeIntervals
)
38 if (aTimeIntervals
.IsInvalid()) {
41 for (const media::TimeInterval
& interval
: aTimeIntervals
) {
42 Add(interval
.mStart
.ToSeconds(), interval
.mEnd
.ToSeconds());
46 TimeRanges::TimeRanges(const media::TimeIntervals
& aTimeIntervals
)
47 : TimeRanges(nullptr, aTimeIntervals
)
52 TimeRanges::ToTimeIntervals() const
54 media::TimeIntervals t
;
55 for (uint32_t i
= 0; i
< Length(); i
++) {
56 t
+= media::TimeInterval(media::TimeUnit::FromSeconds(Start(i
)),
57 media::TimeUnit::FromSeconds(End(i
)));
62 TimeRanges::~TimeRanges()
67 TimeRanges::Start(uint32_t aIndex
, ErrorResult
& aRv
) const
69 if (aIndex
>= mRanges
.Length()) {
70 aRv
= NS_ERROR_DOM_INDEX_SIZE_ERR
;
78 TimeRanges::End(uint32_t aIndex
, ErrorResult
& aRv
) const
80 if (aIndex
>= mRanges
.Length()) {
81 aRv
= NS_ERROR_DOM_INDEX_SIZE_ERR
;
89 TimeRanges::Add(double aStart
, double aEnd
)
92 NS_WARNING("Can't add a range if the end is older that the start.");
95 mRanges
.AppendElement(TimeRange(aStart
,aEnd
));
99 TimeRanges::GetStartTime()
101 if (mRanges
.IsEmpty()) {
104 return mRanges
[0].mStart
;
108 TimeRanges::GetEndTime()
110 if (mRanges
.IsEmpty()) {
113 return mRanges
[mRanges
.Length() - 1].mEnd
;
117 TimeRanges::Normalize(double aTolerance
)
119 if (mRanges
.Length() >= 2) {
120 AutoTArray
<TimeRange
,4> normalized
;
122 mRanges
.Sort(CompareTimeRanges());
124 // This merges the intervals.
125 TimeRange
current(mRanges
[0]);
126 for (uint32_t i
= 1; i
< mRanges
.Length(); i
++) {
127 if (current
.mStart
<= mRanges
[i
].mStart
&&
128 current
.mEnd
>= mRanges
[i
].mEnd
) {
131 if (current
.mEnd
+ aTolerance
>= mRanges
[i
].mStart
) {
132 current
.mEnd
= mRanges
[i
].mEnd
;
134 normalized
.AppendElement(current
);
135 current
= mRanges
[i
];
139 normalized
.AppendElement(current
);
141 mRanges
= normalized
;
146 TimeRanges::Union(const TimeRanges
* aOtherRanges
, double aTolerance
)
148 mRanges
.AppendElements(aOtherRanges
->mRanges
);
149 Normalize(aTolerance
);
153 TimeRanges::Intersection(const TimeRanges
* aOtherRanges
)
155 AutoTArray
<TimeRange
,4> intersection
;
157 const nsTArray
<TimeRange
>& otherRanges
= aOtherRanges
->mRanges
;
158 for (index_type i
= 0, j
= 0; i
< mRanges
.Length() && j
< otherRanges
.Length();) {
159 double start
= std::max(mRanges
[i
].mStart
, otherRanges
[j
].mStart
);
160 double end
= std::min(mRanges
[i
].mEnd
, otherRanges
[j
].mEnd
);
162 intersection
.AppendElement(TimeRange(start
, end
));
164 if (mRanges
[i
].mEnd
< otherRanges
[j
].mEnd
) {
171 mRanges
= intersection
;
174 TimeRanges::index_type
175 TimeRanges::Find(double aTime
, double aTolerance
/* = 0 */)
177 for (index_type i
= 0; i
< mRanges
.Length(); ++i
) {
178 if (aTime
< mRanges
[i
].mEnd
&& (aTime
+ aTolerance
) >= mRanges
[i
].mStart
) {
186 TimeRanges::WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
)
188 return TimeRanges_Binding::Wrap(aCx
, this, aGivenProto
);
192 TimeRanges::GetParentObject() const
198 TimeRanges::Shift(double aOffset
)
200 for (index_type i
= 0; i
< mRanges
.Length(); ++i
) {
201 mRanges
[i
].mStart
+= aOffset
;
202 mRanges
[i
].mEnd
+= aOffset
;
207 } // namespace mozilla