1 | /* |
2 | * Copyright (C) 2013 Google 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 are |
6 | * met: |
7 | * |
8 | * * Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * * Redistributions in binary form must reproduce the above |
11 | * copyright notice, this list of conditions and the following disclaimer |
12 | * in the documentation and/or other materials provided with the |
13 | * distribution. |
14 | * * Neither the name of Google Inc. nor the names of its |
15 | * contributors may be used to endorse or promote products derived from |
16 | * this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #include "config.h" |
32 | #include "SourceBufferList.h" |
33 | |
34 | #if ENABLE(MEDIA_SOURCE) |
35 | |
36 | #include "Event.h" |
37 | #include "EventNames.h" |
38 | #include "SourceBuffer.h" |
39 | #include <wtf/IsoMallocInlines.h> |
40 | |
41 | namespace WebCore { |
42 | |
43 | WTF_MAKE_ISO_ALLOCATED_IMPL(SourceBufferList); |
44 | |
45 | SourceBufferList::SourceBufferList(ScriptExecutionContext* context) |
46 | : ActiveDOMObject(context) |
47 | , m_asyncEventQueue(*this) |
48 | { |
49 | suspendIfNeeded(); |
50 | } |
51 | |
52 | SourceBufferList::~SourceBufferList() |
53 | { |
54 | ASSERT(m_list.isEmpty()); |
55 | } |
56 | |
57 | void SourceBufferList::add(Ref<SourceBuffer>&& buffer) |
58 | { |
59 | m_list.append(WTFMove(buffer)); |
60 | scheduleEvent(eventNames().addsourcebufferEvent); |
61 | } |
62 | |
63 | void SourceBufferList::remove(SourceBuffer& buffer) |
64 | { |
65 | size_t index = m_list.find(&buffer); |
66 | if (index == notFound) |
67 | return; |
68 | m_list.remove(index); |
69 | scheduleEvent(eventNames().removesourcebufferEvent); |
70 | } |
71 | |
72 | void SourceBufferList::clear() |
73 | { |
74 | m_list.clear(); |
75 | scheduleEvent(eventNames().removesourcebufferEvent); |
76 | } |
77 | |
78 | void SourceBufferList::swap(Vector<RefPtr<SourceBuffer>>& other) |
79 | { |
80 | int changeInSize = other.size() - m_list.size(); |
81 | int addedEntries = 0; |
82 | for (auto& sourceBuffer : other) { |
83 | if (!m_list.contains(sourceBuffer)) |
84 | ++addedEntries; |
85 | } |
86 | int removedEntries = addedEntries - changeInSize; |
87 | |
88 | m_list.swap(other); |
89 | |
90 | if (addedEntries) |
91 | scheduleEvent(eventNames().addsourcebufferEvent); |
92 | if (removedEntries) |
93 | scheduleEvent(eventNames().removesourcebufferEvent); |
94 | } |
95 | |
96 | void SourceBufferList::scheduleEvent(const AtomicString& eventName) |
97 | { |
98 | auto event = Event::create(eventName, Event::CanBubble::No, Event::IsCancelable::No); |
99 | event->setTarget(this); |
100 | |
101 | m_asyncEventQueue.enqueueEvent(WTFMove(event)); |
102 | } |
103 | |
104 | bool SourceBufferList::canSuspendForDocumentSuspension() const |
105 | { |
106 | return !m_asyncEventQueue.hasPendingEvents(); |
107 | } |
108 | |
109 | void SourceBufferList::suspend(ReasonForSuspension reason) |
110 | { |
111 | switch (reason) { |
112 | case ReasonForSuspension::PageCache: |
113 | case ReasonForSuspension::PageWillBeSuspended: |
114 | m_asyncEventQueue.suspend(); |
115 | break; |
116 | case ReasonForSuspension::JavaScriptDebuggerPaused: |
117 | case ReasonForSuspension::WillDeferLoading: |
118 | // Do nothing, we don't pause media playback in these cases. |
119 | break; |
120 | } |
121 | } |
122 | |
123 | void SourceBufferList::resume() |
124 | { |
125 | m_asyncEventQueue.resume(); |
126 | } |
127 | |
128 | void SourceBufferList::stop() |
129 | { |
130 | m_asyncEventQueue.close(); |
131 | } |
132 | |
133 | const char* SourceBufferList::activeDOMObjectName() const |
134 | { |
135 | return "SourceBufferList" ; |
136 | } |
137 | |
138 | } // namespace WebCore |
139 | |
140 | #endif |
141 | |