2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "PlatformTimeRanges.h"
30 #include <wtf/PrintStream.h>
34 PlatformTimeRanges::PlatformTimeRanges(const MediaTime& start, const MediaTime& end)
39 PlatformTimeRanges::PlatformTimeRanges(const PlatformTimeRanges& other)
44 PlatformTimeRanges& PlatformTimeRanges::operator=(const PlatformTimeRanges& other)
49 PlatformTimeRanges& PlatformTimeRanges::copy(const PlatformTimeRanges& other)
51 unsigned size = other.m_ranges.size();
52 for (unsigned i = 0; i < size; i++)
53 add(other.m_ranges[i].m_start, other.m_ranges[i].m_end);
58 void PlatformTimeRanges::invert()
60 PlatformTimeRanges inverted;
61 MediaTime posInf = MediaTime::positiveInfiniteTime();
62 MediaTime negInf = MediaTime::negativeInfiniteTime();
65 inverted.add(negInf, posInf);
67 MediaTime start = m_ranges.first().m_start;
69 inverted.add(negInf, start);
71 for (size_t index = 0; index + 1 < m_ranges.size(); ++index)
72 inverted.add(m_ranges[index].m_end, m_ranges[index + 1].m_start);
74 MediaTime end = m_ranges.last().m_end;
76 inverted.add(end, posInf);
79 m_ranges.swap(inverted.m_ranges);
82 void PlatformTimeRanges::intersectWith(const PlatformTimeRanges& other)
84 PlatformTimeRanges invertedOther(other);
86 invertedOther.invert();
88 unionWith(invertedOther);
92 void PlatformTimeRanges::unionWith(const PlatformTimeRanges& other)
94 PlatformTimeRanges unioned(*this);
96 for (size_t index = 0; index < other.m_ranges.size(); ++index) {
97 const Range& range = other.m_ranges[index];
98 unioned.add(range.m_start, range.m_end);
101 m_ranges.swap(unioned.m_ranges);
104 MediaTime PlatformTimeRanges::start(unsigned index) const
107 return start(index, ignoredValid);
110 MediaTime PlatformTimeRanges::start(unsigned index, bool& valid) const
112 if (index >= length()) {
114 return MediaTime::zeroTime();
118 return m_ranges[index].m_start;
121 MediaTime PlatformTimeRanges::end(unsigned index) const
124 return end(index, ignoredValid);
127 MediaTime PlatformTimeRanges::end(unsigned index, bool& valid) const
129 if (index >= length()) {
131 return MediaTime::zeroTime();
135 return m_ranges[index].m_end;
138 MediaTime PlatformTimeRanges::duration(unsigned index) const
140 if (index >= length())
141 return MediaTime::invalidTime();
143 return m_ranges[index].m_end - m_ranges[index].m_start;
146 MediaTime PlatformTimeRanges::maximumBufferedTime() const
149 return MediaTime::invalidTime();
151 return m_ranges[length() - 1].m_end;
154 void PlatformTimeRanges::add(const MediaTime& start, const MediaTime& end)
156 ASSERT(start <= end);
157 unsigned overlappingArcIndex;
158 Range addedRange(start, end);
160 // For each present range check if we need to:
161 // - merge with the added range, in case we are overlapping or contiguous
162 // - Need to insert in place, we we are completely, not overlapping and not contiguous
163 // in between two ranges.
165 // TODO: Given that we assume that ranges are correctly ordered, this could be optimized.
167 for (overlappingArcIndex = 0; overlappingArcIndex < m_ranges.size(); overlappingArcIndex++) {
168 if (addedRange.isOverlappingRange(m_ranges[overlappingArcIndex]) || addedRange.isContiguousWithRange(m_ranges[overlappingArcIndex])) {
169 // We need to merge the addedRange and that range.
170 addedRange = addedRange.unionWithOverlappingOrContiguousRange(m_ranges[overlappingArcIndex]);
171 m_ranges.remove(overlappingArcIndex);
172 overlappingArcIndex--;
174 // Check the case for which there is no more to do
175 if (!overlappingArcIndex) {
176 if (addedRange.isBeforeRange(m_ranges[0])) {
177 // First index, and we are completely before that range (and not contiguous, nor overlapping).
178 // We just need to be inserted here.
182 if (m_ranges[overlappingArcIndex - 1].isBeforeRange(addedRange) && addedRange.isBeforeRange(m_ranges[overlappingArcIndex])) {
183 // We are exactly after the current previous range, and before the current range, while
184 // not overlapping with none of them. Insert here.
191 // Now that we are sure we don't overlap with any range, just add it.
192 m_ranges.insert(overlappingArcIndex, addedRange);
195 bool PlatformTimeRanges::contain(const MediaTime& time) const
197 return find(time) != notFound;
200 size_t PlatformTimeRanges::find(const MediaTime& time) const
203 for (unsigned n = 0; n < length(); n++) {
204 if (time >= start(n, ignoreInvalid) && time <= end(n, ignoreInvalid))
210 MediaTime PlatformTimeRanges::nearest(const MediaTime& time) const
212 MediaTime closestDelta = MediaTime::positiveInfiniteTime();
213 MediaTime closestTime = MediaTime::zeroTime();
214 unsigned count = length();
217 for (unsigned ndx = 0; ndx < count; ndx++) {
218 MediaTime startTime = start(ndx, ignoreInvalid);
219 MediaTime endTime = end(ndx, ignoreInvalid);
220 if (time >= startTime && time <= endTime)
223 MediaTime startTimeDelta = abs(startTime - time);
224 if (startTimeDelta < closestDelta) {
225 closestTime = startTime;
226 closestDelta = startTimeDelta;
229 MediaTime endTimeDelta = abs(endTime - time);
230 if (endTimeDelta < closestDelta) {
231 closestTime = endTime;
232 closestDelta = endTimeDelta;
238 MediaTime PlatformTimeRanges::totalDuration() const
240 MediaTime total = MediaTime::zeroTime();
242 for (unsigned n = 0; n < length(); n++)
243 total += abs(end(n) - start(n));
247 void PlatformTimeRanges::dump(PrintStream& out) const
252 for (size_t i = 0; i < length(); ++i)
253 out.print("[", start(i), "..", end(i), "] ");