Use "= default" to denote default constructor or destructor
[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 // FIXME: What does the word "cache" mean here?
48 static const HashSet<String, ASCIICaseInsensitiveHash>& mimeTypeCache()
49 {
50     static const auto cache = makeNeverDestroyed(HashSet<String, ASCIICaseInsensitiveHash> {
51         "video/mock",
52         "audio/mock",
53     });
54     return cache;
55 }
56
57 void MockMediaPlayerMediaSource::getSupportedTypes(HashSet<String, ASCIICaseInsensitiveHash>& supportedTypes)
58 {
59     supportedTypes = mimeTypeCache();
60 }
61
62 MediaPlayer::SupportsType MockMediaPlayerMediaSource::supportsType(const MediaEngineSupportParameters& parameters)
63 {
64     if (!parameters.isMediaSource)
65         return MediaPlayer::IsNotSupported;
66
67     auto containerType = parameters.type.containerType();
68     if (containerType.isEmpty() || !mimeTypeCache().contains(containerType))
69         return MediaPlayer::IsNotSupported;
70
71     auto codecs = parameters.type.parameter(ContentType::codecsParameter());
72     if (codecs.isEmpty())
73         return MediaPlayer::MayBeSupported;
74
75     return codecs == "mock" ? MediaPlayer::IsSupported : MediaPlayer::MayBeSupported;
76 }
77
78 MockMediaPlayerMediaSource::MockMediaPlayerMediaSource(MediaPlayer* player)
79     : m_player(player)
80     , m_currentTime(MediaTime::zeroTime())
81     , m_readyState(MediaPlayer::HaveNothing)
82     , m_networkState(MediaPlayer::Empty)
83     , m_playing(false)
84     , m_seekCompleted(true)
85 {
86 }
87
88 MockMediaPlayerMediaSource::~MockMediaPlayerMediaSource() = default;
89
90 void MockMediaPlayerMediaSource::load(const String&)
91 {
92     ASSERT_NOT_REACHED();
93 }
94
95 void MockMediaPlayerMediaSource::load(const String&, MediaSourcePrivateClient* source)
96 {
97     m_mediaSourcePrivate = MockMediaSourcePrivate::create(this, source);
98 }
99
100 void MockMediaPlayerMediaSource::cancelLoad()
101 {
102 }
103
104 void MockMediaPlayerMediaSource::play()
105 {
106     m_playing = 1;
107     callOnMainThread([this] {
108         advanceCurrentTime();
109     });
110 }
111
112 void MockMediaPlayerMediaSource::pause()
113 {
114     m_playing = 0;
115 }
116
117 FloatSize MockMediaPlayerMediaSource::naturalSize() const
118 {
119     return FloatSize();
120 }
121
122 bool MockMediaPlayerMediaSource::hasVideo() const
123 {
124     return m_mediaSourcePrivate ? m_mediaSourcePrivate->hasVideo() : false;
125 }
126
127 bool MockMediaPlayerMediaSource::hasAudio() const
128 {
129     return m_mediaSourcePrivate ? m_mediaSourcePrivate->hasAudio() : false;
130 }
131
132 void MockMediaPlayerMediaSource::setVisible(bool)
133 {
134 }
135
136 bool MockMediaPlayerMediaSource::seeking() const
137 {
138     return !m_seekCompleted;
139 }
140
141 bool MockMediaPlayerMediaSource::paused() const
142 {
143     return !m_playing;
144 }
145
146 MediaPlayer::NetworkState MockMediaPlayerMediaSource::networkState() const
147 {
148     return m_networkState;
149 }
150
151 MediaPlayer::ReadyState MockMediaPlayerMediaSource::readyState() const
152 {
153     return m_readyState;
154 }
155
156 MediaTime MockMediaPlayerMediaSource::maxMediaTimeSeekable() const
157 {
158     return m_duration;
159 }
160
161 std::unique_ptr<PlatformTimeRanges> MockMediaPlayerMediaSource::buffered() const
162 {
163     if (m_mediaSourcePrivate)
164         return m_mediaSourcePrivate->buffered();
165
166     return std::make_unique<PlatformTimeRanges>();
167 }
168
169 bool MockMediaPlayerMediaSource::didLoadingProgress() const
170 {
171     return false;
172 }
173
174 void MockMediaPlayerMediaSource::setSize(const IntSize&)
175 {
176 }
177
178 void MockMediaPlayerMediaSource::paint(GraphicsContext&, const FloatRect&)
179 {
180 }
181
182 MediaTime MockMediaPlayerMediaSource::currentMediaTime() const
183 {
184     return m_currentTime;
185 }
186
187 MediaTime MockMediaPlayerMediaSource::durationMediaTime() const
188 {
189     return m_mediaSourcePrivate ? m_mediaSourcePrivate->duration() : MediaTime::zeroTime();
190 }
191
192 void MockMediaPlayerMediaSource::seekWithTolerance(const MediaTime& time, const MediaTime& negativeTolerance, const MediaTime& positiveTolerance)
193 {
194     if (!negativeTolerance && !positiveTolerance) {
195         m_currentTime = time;
196         m_mediaSourcePrivate->seekToTime(time);
197     } else
198         m_currentTime = m_mediaSourcePrivate->seekToTime(time, negativeTolerance, positiveTolerance);
199
200     if (m_seekCompleted) {
201         m_player->timeChanged();
202
203         if (m_playing)
204             callOnMainThread([this] {
205                 advanceCurrentTime();
206             });
207     }
208 }
209
210 void MockMediaPlayerMediaSource::advanceCurrentTime()
211 {
212     if (!m_mediaSourcePrivate)
213         return;
214
215     auto buffered = m_mediaSourcePrivate->buffered();
216     size_t pos = buffered->find(m_currentTime);
217     if (pos == notFound)
218         return;
219
220     bool ignoreError;
221     m_currentTime = std::min(m_duration, buffered->end(pos, ignoreError));
222     m_player->timeChanged();
223 }
224
225 void MockMediaPlayerMediaSource::updateDuration(const MediaTime& duration)
226 {
227     if (m_duration == duration)
228         return;
229
230     m_duration = duration;
231     m_player->durationChanged();
232 }
233
234 void MockMediaPlayerMediaSource::setReadyState(MediaPlayer::ReadyState readyState)
235 {
236     if (readyState == m_readyState)
237         return;
238
239     m_readyState = readyState;
240     m_player->readyStateChanged();
241 }
242
243 void MockMediaPlayerMediaSource::setNetworkState(MediaPlayer::NetworkState networkState)
244 {
245     if (networkState == m_networkState)
246         return;
247
248     m_networkState = networkState;
249     m_player->networkStateChanged();
250 }
251
252 void MockMediaPlayerMediaSource::waitForSeekCompleted()
253 {
254     m_seekCompleted = false;
255 }
256
257 void MockMediaPlayerMediaSource::seekCompleted()
258 {
259     if (m_seekCompleted)
260         return;
261     m_seekCompleted = true;
262
263     m_player->timeChanged();
264
265     if (m_playing)
266         callOnMainThread([this] {
267             advanceCurrentTime();
268         });
269 }
270
271 std::optional<PlatformVideoPlaybackQualityMetrics> MockMediaPlayerMediaSource::videoPlaybackQualityMetrics()
272 {
273     return m_mediaSourcePrivate ? m_mediaSourcePrivate->videoPlaybackQualityMetrics() : std::nullopt;
274 }
275
276 }
277
278 #endif