/src/mozilla-central/dom/html/TimeRanges.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #include "mozilla/dom/TimeRanges.h" |
8 | | #include "mozilla/dom/TimeRangesBinding.h" |
9 | | #include "mozilla/dom/HTMLMediaElement.h" |
10 | | #include "TimeUnits.h" |
11 | | #include "nsError.h" |
12 | | |
13 | | namespace mozilla { |
14 | | namespace dom { |
15 | | |
16 | | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TimeRanges, mParent) |
17 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(TimeRanges) |
18 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(TimeRanges) |
19 | 0 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TimeRanges) |
20 | 0 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
21 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
22 | 0 | NS_INTERFACE_MAP_END |
23 | | |
24 | | TimeRanges::TimeRanges() |
25 | | : mParent(nullptr) |
26 | 0 | { |
27 | 0 | } |
28 | | |
29 | | TimeRanges::TimeRanges(nsISupports* aParent) |
30 | | : mParent(aParent) |
31 | 0 | { |
32 | 0 | } |
33 | | |
34 | | TimeRanges::TimeRanges(nsISupports* aParent, |
35 | | const media::TimeIntervals& aTimeIntervals) |
36 | | : TimeRanges(aParent) |
37 | 0 | { |
38 | 0 | if (aTimeIntervals.IsInvalid()) { |
39 | 0 | return; |
40 | 0 | } |
41 | 0 | for (const media::TimeInterval& interval : aTimeIntervals) { |
42 | 0 | Add(interval.mStart.ToSeconds(), interval.mEnd.ToSeconds()); |
43 | 0 | } |
44 | 0 | } |
45 | | |
46 | | TimeRanges::TimeRanges(const media::TimeIntervals& aTimeIntervals) |
47 | | : TimeRanges(nullptr, aTimeIntervals) |
48 | 0 | { |
49 | 0 | } |
50 | | |
51 | | media::TimeIntervals |
52 | | TimeRanges::ToTimeIntervals() const |
53 | 0 | { |
54 | 0 | media::TimeIntervals t; |
55 | 0 | for (uint32_t i = 0; i < Length(); i++) { |
56 | 0 | t += media::TimeInterval(media::TimeUnit::FromSeconds(Start(i)), |
57 | 0 | media::TimeUnit::FromSeconds(End(i))); |
58 | 0 | } |
59 | 0 | return t; |
60 | 0 | } |
61 | | |
62 | | TimeRanges::~TimeRanges() |
63 | 0 | { |
64 | 0 | } |
65 | | |
66 | | double |
67 | | TimeRanges::Start(uint32_t aIndex, ErrorResult& aRv) const |
68 | 0 | { |
69 | 0 | if (aIndex >= mRanges.Length()) { |
70 | 0 | aRv = NS_ERROR_DOM_INDEX_SIZE_ERR; |
71 | 0 | return 0; |
72 | 0 | } |
73 | 0 | |
74 | 0 | return Start(aIndex); |
75 | 0 | } |
76 | | |
77 | | double |
78 | | TimeRanges::End(uint32_t aIndex, ErrorResult& aRv) const |
79 | 0 | { |
80 | 0 | if (aIndex >= mRanges.Length()) { |
81 | 0 | aRv = NS_ERROR_DOM_INDEX_SIZE_ERR; |
82 | 0 | return 0; |
83 | 0 | } |
84 | 0 | |
85 | 0 | return End(aIndex); |
86 | 0 | } |
87 | | |
88 | | void |
89 | | TimeRanges::Add(double aStart, double aEnd) |
90 | 0 | { |
91 | 0 | if (aStart > aEnd) { |
92 | 0 | NS_WARNING("Can't add a range if the end is older that the start."); |
93 | 0 | return; |
94 | 0 | } |
95 | 0 | mRanges.AppendElement(TimeRange(aStart,aEnd)); |
96 | 0 | } |
97 | | |
98 | | double |
99 | | TimeRanges::GetStartTime() |
100 | 0 | { |
101 | 0 | if (mRanges.IsEmpty()) { |
102 | 0 | return -1.0; |
103 | 0 | } |
104 | 0 | return mRanges[0].mStart; |
105 | 0 | } |
106 | | |
107 | | double |
108 | | TimeRanges::GetEndTime() |
109 | 0 | { |
110 | 0 | if (mRanges.IsEmpty()) { |
111 | 0 | return -1.0; |
112 | 0 | } |
113 | 0 | return mRanges[mRanges.Length() - 1].mEnd; |
114 | 0 | } |
115 | | |
116 | | void |
117 | | TimeRanges::Normalize(double aTolerance) |
118 | 0 | { |
119 | 0 | if (mRanges.Length() >= 2) { |
120 | 0 | AutoTArray<TimeRange,4> normalized; |
121 | 0 |
|
122 | 0 | mRanges.Sort(CompareTimeRanges()); |
123 | 0 |
|
124 | 0 | // This merges the intervals. |
125 | 0 | TimeRange current(mRanges[0]); |
126 | 0 | for (uint32_t i = 1; i < mRanges.Length(); i++) { |
127 | 0 | if (current.mStart <= mRanges[i].mStart && |
128 | 0 | current.mEnd >= mRanges[i].mEnd) { |
129 | 0 | continue; |
130 | 0 | } |
131 | 0 | if (current.mEnd + aTolerance >= mRanges[i].mStart) { |
132 | 0 | current.mEnd = mRanges[i].mEnd; |
133 | 0 | } else { |
134 | 0 | normalized.AppendElement(current); |
135 | 0 | current = mRanges[i]; |
136 | 0 | } |
137 | 0 | } |
138 | 0 |
|
139 | 0 | normalized.AppendElement(current); |
140 | 0 |
|
141 | 0 | mRanges = normalized; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | | void |
146 | | TimeRanges::Union(const TimeRanges* aOtherRanges, double aTolerance) |
147 | 0 | { |
148 | 0 | mRanges.AppendElements(aOtherRanges->mRanges); |
149 | 0 | Normalize(aTolerance); |
150 | 0 | } |
151 | | |
152 | | void |
153 | | TimeRanges::Intersection(const TimeRanges* aOtherRanges) |
154 | 0 | { |
155 | 0 | AutoTArray<TimeRange,4> intersection; |
156 | 0 |
|
157 | 0 | const nsTArray<TimeRange>& otherRanges = aOtherRanges->mRanges; |
158 | 0 | for (index_type i = 0, j = 0; i < mRanges.Length() && j < otherRanges.Length();) { |
159 | 0 | double start = std::max(mRanges[i].mStart, otherRanges[j].mStart); |
160 | 0 | double end = std::min(mRanges[i].mEnd, otherRanges[j].mEnd); |
161 | 0 | if (start < end) { |
162 | 0 | intersection.AppendElement(TimeRange(start, end)); |
163 | 0 | } |
164 | 0 | if (mRanges[i].mEnd < otherRanges[j].mEnd) { |
165 | 0 | i += 1; |
166 | 0 | } else { |
167 | 0 | j += 1; |
168 | 0 | } |
169 | 0 | } |
170 | 0 |
|
171 | 0 | mRanges = intersection; |
172 | 0 | } |
173 | | |
174 | | TimeRanges::index_type |
175 | | TimeRanges::Find(double aTime, double aTolerance /* = 0 */) |
176 | 0 | { |
177 | 0 | for (index_type i = 0; i < mRanges.Length(); ++i) { |
178 | 0 | if (aTime < mRanges[i].mEnd && (aTime + aTolerance) >= mRanges[i].mStart) { |
179 | 0 | return i; |
180 | 0 | } |
181 | 0 | } |
182 | 0 | return NoIndex; |
183 | 0 | } |
184 | | |
185 | | JSObject* |
186 | | TimeRanges::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
187 | 0 | { |
188 | 0 | return TimeRanges_Binding::Wrap(aCx, this, aGivenProto); |
189 | 0 | } |
190 | | |
191 | | nsISupports* |
192 | | TimeRanges::GetParentObject() const |
193 | 0 | { |
194 | 0 | return mParent; |
195 | 0 | } |
196 | | |
197 | | void |
198 | | TimeRanges::Shift(double aOffset) |
199 | 0 | { |
200 | 0 | for (index_type i = 0; i < mRanges.Length(); ++i) { |
201 | 0 | mRanges[i].mStart += aOffset; |
202 | 0 | mRanges[i].mEnd += aOffset; |
203 | 0 | } |
204 | 0 | } |
205 | | |
206 | | } // namespace dom |
207 | | } // namespace mozilla |