Remove LegacyException support from bindings script
[WebKit-https.git] / Source / WebCore / platform / mock / mediasource / MockMediaPlayerMediaSource.cpp
1 /*
2  * Copyright (C) 2013-2015 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 #include "config.h"
27 #include "MockMediaPlayerMediaSource.h"
28
29 #if ENABLE(MEDIA_SOURCE)
30
31 #include "MediaPlayer.h"
32 #include "MediaSourcePrivateClient.h"
33 #include "MockMediaSourcePrivate.h"
34 #include <wtf/MainThread.h>
35 #include <wtf/NeverDestroyed.h>
36 #include <wtf/text/WTFString.h>
37
38 namespace WebCore {
39
40 // MediaPlayer Enigne Support
41 void MockMediaPlayerMediaSource::registerMediaEngine(MediaEngineRegistrar registrar)
42 {
43     registrar([](MediaPlayer* player) { return std::make_unique<MockMediaPlayerMediaSource>(player); }, getSupportedTypes,
44         supportsType, 0, 0, 0, 0);
45 }
46
47 static const HashSet<String, ASCIICaseInsensitiveHash>& mimeTypeCache()
48 {
49     static NeverDestroyed<HashSet<String, ASCIICaseInsensitiveHash>> cache;
50     static bool isInitialized = false;
51
52     if (!isInitialized) {
53         isInitialized = true;
54         cache.get().add(ASCIILiteral("video/mock"));
55         cache.get().add(ASCIILiteral("audio/mock"));
56     }
57
58     return cache;
59 }
60
61 void MockMediaPlayerMediaSource::getSupportedTypes(HashSet<String, ASCIICaseInsensitiveHash>& supportedTypes)
62 {
63     supportedTypes = mimeTypeCache();
64 }
65
66 MediaPlayer::SupportsType MockMediaPlayerMediaSource::supportsType(const MediaEngineSupportParameters& parameters)
67 {
68     if (!parameters.isMediaSource)
69         return MediaPlayer::IsNotSupported;
70
71     if (parameters.type.isEmpty() || !mimeTypeCache().contains(parameters.type))
72         return MediaPlayer::IsNotSupported;
73
74     if (parameters.codecs.isEmpty())
75         return MediaPlayer::MayBeSupported;
76
77     return parameters.codecs == "mock" ? MediaPlayer::IsSupported : MediaPlayer::MayBeSupported;
78 }
79
80 MockMediaPlayerMediaSource::MockMediaPlayerMediaSource(MediaPlayer* player)
81     : m_player(player)
82     , m_currentTime(MediaTime::zeroTime())
83     , m_readyState(MediaPlayer::HaveNothing)
84     , m_networkState(MediaPlayer::Empty)
85     , m_playing(false)
86     , m_seekCompleted(true)
87 {
88 }
89
90 MockMediaPlayerMediaSource::~MockMediaPlayerMediaSource()
91 {
92 }
93
94 void MockMediaPlayerMediaSource::load(const String&)
95 {
96     ASSERT_NOT_REACHED();
97 }
98
99 void MockMediaPlayerMediaSource::load(const String&, MediaSourcePrivateClient* source)
100 {
101     m_mediaSourcePrivate = MockMediaSourcePrivate::create(this, source);
102 }
103
104 void MockMediaPlayerMediaSource::cancelLoad()
105 {
106 }
107
108 void MockMediaPlayerMediaSource::play()
109 {
110     m_playing = 1;
111     callOnMainThread([this] {
112         advanceCurrentTime();
113     });
114 }
115
116 void MockMediaPlayerMediaSource::pause()
117 {
118     m_playing = 0;
119 }
120
121 FloatSize MockMediaPlayerMediaSource::naturalSize() const
122 {
123     return FloatSize();
124 }
125
126 bool MockMediaPlayerMediaSource::hasVideo() const
127 {
128     return m_mediaSourcePrivate ? m_mediaSourcePrivate->hasVideo() : false;
129 }
130
131 bool MockMediaPlayerMediaSource::hasAudio() const
132 {
133     return m_mediaSourcePrivate ? m_mediaSourcePrivate->hasAudio() : false;
134 }
135
136 void MockMediaPlayerMediaSource::setVisible(bool)
137 {
138 }
139
140 bool MockMediaPlayerMediaSource::seeking() const
141 {
142     return !m_seekCompleted;
143 }
144
145 bool MockMediaPlayerMediaSource::paused() const
146 {
147     return !m_playing;
148 }
149
150 MediaPlayer::NetworkState MockMediaPlayerMediaSource::networkState() const
151 {
152     return m_networkState;
153 }
154
155 MediaPlayer::ReadyState MockMediaPlayerMediaSource::readyState() const
156 {
157     return m_readyState;
158 }
159
160 MediaTime MockMediaPlayerMediaSource::maxMediaTimeSeekable() const
161 {
162     return m_duration;
163 }
164
165 std::unique_ptr<PlatformTimeRanges> MockMediaPlayerMediaSource::buffered() const
166 {
167     if (m_mediaSourcePrivate)
168         return m_mediaSourcePrivate->buffered();
169
170     return std::make_unique<PlatformTimeRanges>();
171 }
172
173 bool MockMediaPlayerMediaSource::didLoadingProgress() const
174 {
175     return false;
176 }
177
178 void MockMediaPlayerMediaSource::setSize(const IntSize&)
179 {
180 }
181
182 void MockMediaPlayerMediaSource::paint(GraphicsContext&, const FloatRect&)
183 {
184 }
185
186 MediaTime MockMediaPlayerMediaSource::currentMediaTime() const
187 {
188     return m_currentTime;
189 }
190
191 MediaTime MockMediaPlayerMediaSource::durationMediaTime() const
192 {
193     return m_mediaSourcePrivate ? m_mediaSourcePrivate->duration() : MediaTime::zeroTime();
194 }
195
196 void MockMediaPlayerMediaSource::seekWithTolerance(const MediaTime& time, const MediaTime& negativeTolerance, const MediaTime& positiveTolerance)
197 {
198     if (!negativeTolerance && !positiveTolerance) {
199         m_currentTime = time;
200         m_mediaSourcePrivate->seekToTime(time);
201     } else
202         m_currentTime = m_mediaSourcePrivate->seekToTime(time, negativeTolerance, positiveTolerance);
203
204     if (m_seekCompleted) {
205         m_player->timeChanged();
206
207         if (m_playing)
208             callOnMainThread([this] {
209                 advanceCurrentTime();
210             });
211     }
212 }
213
214 void MockMediaPlayerMediaSource::advanceCurrentTime()
215 {
216     if (!m_mediaSourcePrivate)
217         return;
218
219     auto buffered = m_mediaSourcePrivate->buffered();
220     size_t pos = buffered->find(m_currentTime);
221     if (pos == notFound)
222         return;
223
224     bool ignoreError;
225     m_currentTime = std::min(m_duration, buffered->end(pos, ignoreError));
226     m_player->timeChanged();
227 }
228
229 void MockMediaPlayerMediaSource::updateDuration(const MediaTime& duration)
230 {
231     if (m_duration == duration)
232         return;
233
234     m_duration = duration;
235     m_player->durationChanged();
236 }
237
238 void MockMediaPlayerMediaSource::setReadyState(MediaPlayer::ReadyState readyState)
239 {
240     if (readyState == m_readyState)
241         return;
242
243     m_readyState = readyState;
244     m_player->readyStateChanged();
245 }
246
247 void MockMediaPlayerMediaSource::setNetworkState(MediaPlayer::NetworkState networkState)
248 {
249     if (networkState == m_networkState)
250         return;
251
252     m_networkState = networkState;
253     m_player->networkStateChanged();
254 }
255
256 void MockMediaPlayerMediaSource::waitForSeekCompleted()
257 {
258     m_seekCompleted = false;
259 }
260
261 void MockMediaPlayerMediaSource::seekCompleted()
262 {
263     if (m_seekCompleted)
264         return;
265     m_seekCompleted = true;
266
267     m_player->timeChanged();
268
269     if (m_playing)
270         callOnMainThread([this] {
271             advanceCurrentTime();
272         });
273 }
274
275 unsigned long MockMediaPlayerMediaSource::totalVideoFrames()
276 {
277     return m_mediaSourcePrivate ? m_mediaSourcePrivate->totalVideoFrames() : 0;
278 }
279
280 unsigned long MockMediaPlayerMediaSource::droppedVideoFrames()
281 {
282     return m_mediaSourcePrivate ? m_mediaSourcePrivate->droppedVideoFrames() : 0;
283 }
284
285 unsigned long MockMediaPlayerMediaSource::corruptedVideoFrames()
286 {
287     return m_mediaSourcePrivate ? m_mediaSourcePrivate->corruptedVideoFrames() : 0;
288 }
289
290 MediaTime MockMediaPlayerMediaSource::totalFrameDelay()
291 {
292     return m_mediaSourcePrivate ? m_mediaSourcePrivate->totalFrameDelay() : MediaTime::zeroTime();
293 }
294
295 }
296
297 #endif