1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28
29#if ENABLE(VIDEO_TRACK)
30
31#include "TextTrackCueList.h"
32
33// Checking sorting is too slow for general use; turn it on explicitly when working on this class.
34#undef CHECK_SORTING
35
36#ifdef CHECK_SORTING
37#define ASSERT_SORTED(begin, end) ASSERT(std::is_sorted(begin, end, compareCues))
38#else
39#define ASSERT_SORTED(begin, end) ((void)0)
40#endif
41
42namespace WebCore {
43
44static inline bool compareCues(const RefPtr<TextTrackCue>& a, const RefPtr<TextTrackCue>& b)
45{
46 return a->isOrderedBefore(b.get());
47}
48
49unsigned TextTrackCueList::cueIndex(TextTrackCue& cue) const
50{
51 ASSERT(m_vector.contains(&cue));
52 return m_vector.find(&cue);
53}
54
55TextTrackCue* TextTrackCueList::item(unsigned index) const
56{
57 if (index >= m_vector.size())
58 return nullptr;
59 return m_vector[index].get();
60}
61
62TextTrackCue* TextTrackCueList::getCueById(const String& id) const
63{
64 for (auto& cue : m_vector) {
65 if (cue->id() == id)
66 return cue.get();
67 }
68 return nullptr;
69}
70
71TextTrackCueList& TextTrackCueList::activeCues()
72{
73 if (!m_activeCues)
74 m_activeCues = create();
75
76 Vector<RefPtr<TextTrackCue>> activeCuesVector;
77 for (auto& cue : m_vector) {
78 if (cue->isActive())
79 activeCuesVector.append(cue);
80 }
81 ASSERT_SORTED(activeCuesVector.begin(), activeCuesVector.end());
82 m_activeCues->m_vector = WTFMove(activeCuesVector);
83
84 // FIXME: This list of active cues is not updated as cues are added, removed, become active, and become inactive.
85 // Instead it is only updated each time this function is called again. That is not consistent with other dynamic DOM lists.
86 return *m_activeCues;
87}
88
89void TextTrackCueList::add(Ref<TextTrackCue>&& cue)
90{
91 ASSERT(!m_vector.contains(cue.ptr()));
92 ASSERT(cue->startMediaTime() >= MediaTime::zeroTime());
93 ASSERT(cue->endMediaTime() >= MediaTime::zeroTime());
94
95 RefPtr<TextTrackCue> cueRefPtr { WTFMove(cue) };
96 unsigned insertionPosition = std::upper_bound(m_vector.begin(), m_vector.end(), cueRefPtr, compareCues) - m_vector.begin();
97 ASSERT_SORTED(m_vector.begin(), m_vector.end());
98 m_vector.insert(insertionPosition, WTFMove(cueRefPtr));
99 ASSERT_SORTED(m_vector.begin(), m_vector.end());
100}
101
102void TextTrackCueList::remove(TextTrackCue& cue)
103{
104 ASSERT_SORTED(m_vector.begin(), m_vector.end());
105 m_vector.remove(cueIndex(cue));
106 ASSERT_SORTED(m_vector.begin(), m_vector.end());
107}
108
109void TextTrackCueList::clear()
110{
111 m_vector.clear();
112 if (m_activeCues)
113 m_activeCues->m_vector.clear();
114}
115
116void TextTrackCueList::updateCueIndex(TextTrackCue& cue)
117{
118 auto cuePosition = m_vector.begin() + cueIndex(cue);
119 auto afterCuePosition = cuePosition + 1;
120
121 ASSERT_SORTED(m_vector.begin(), cuePosition);
122 ASSERT_SORTED(afterCuePosition, m_vector.end());
123
124 auto reinsertionPosition = std::upper_bound(m_vector.begin(), cuePosition, *cuePosition, compareCues);
125 if (reinsertionPosition != cuePosition)
126 std::rotate(reinsertionPosition, cuePosition, afterCuePosition);
127 else {
128 reinsertionPosition = std::upper_bound(afterCuePosition, m_vector.end(), *cuePosition, compareCues);
129 if (reinsertionPosition != afterCuePosition)
130 std::rotate(cuePosition, afterCuePosition, reinsertionPosition);
131 }
132
133 ASSERT_SORTED(m_vector.begin(), m_vector.end());
134}
135
136} // namespace WebCore
137
138#endif
139