1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#ifndef PlatformTimeRanges_h
27#define PlatformTimeRanges_h
28
29#include <algorithm>
30#include <wtf/MediaTime.h>
31#include <wtf/Vector.h>
32
33namespace WTF {
34class PrintStream;
35}
36
37namespace WebCore {
38
39class PlatformTimeRanges {
40 WTF_MAKE_FAST_ALLOCATED;
41public:
42 explicit PlatformTimeRanges() { }
43 PlatformTimeRanges(const MediaTime& start, const MediaTime& end);
44
45 WEBCORE_EXPORT MediaTime start(unsigned index) const;
46 MediaTime start(unsigned index, bool& valid) const;
47 WEBCORE_EXPORT MediaTime end(unsigned index) const;
48 MediaTime end(unsigned index, bool& valid) const;
49 MediaTime duration(unsigned index) const;
50 MediaTime maximumBufferedTime() const;
51
52 void invert();
53 void intersectWith(const PlatformTimeRanges&);
54 void unionWith(const PlatformTimeRanges&);
55
56 unsigned length() const { return m_ranges.size(); }
57
58 void add(const MediaTime& start, const MediaTime& end);
59
60 bool contain(const MediaTime&) const;
61
62 size_t find(const MediaTime&) const;
63
64 MediaTime nearest(const MediaTime&) const;
65
66 MediaTime totalDuration() const;
67
68 void dump(WTF::PrintStream&) const;
69
70private:
71 // We consider all the Ranges to be semi-bounded as follow: [start, end[
72 struct Range {
73 Range() { }
74 Range(const MediaTime& start, const MediaTime& end)
75 : m_start(start)
76 , m_end(end)
77 {
78 }
79
80 MediaTime m_start;
81 MediaTime m_end;
82
83 inline bool isPointInRange(const MediaTime& point) const
84 {
85 return m_start <= point && point < m_end;
86 }
87
88 inline bool isOverlappingRange(const Range& range) const
89 {
90 return isPointInRange(range.m_start) || isPointInRange(range.m_end) || range.isPointInRange(m_start);
91 }
92
93 inline bool isContiguousWithRange(const Range& range) const
94 {
95 return range.m_start == m_end || range.m_end == m_start;
96 }
97
98 inline Range unionWithOverlappingOrContiguousRange(const Range& range) const
99 {
100 Range ret;
101
102 ret.m_start = std::min(m_start, range.m_start);
103 ret.m_end = std::max(m_end, range.m_end);
104
105 return ret;
106 }
107
108 inline bool isBeforeRange(const Range& range) const
109 {
110 return range.m_start >= m_end;
111 }
112 };
113
114 Vector<Range> m_ranges;
115};
116
117} // namespace WebCore
118
119#endif
120