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