[MSE] Bring end-of-stream algorithm section up to current spec.
[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 COMPUTER, 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 COMPUTER, 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 "HTMLMediaSource.h"
33 #include "MediaPlayer.h"
34 #include "MockMediaSourcePrivate.h"
35 #include <wtf/Functional.h>
36 #include <wtf/MainThread.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(create, getSupportedTypes, supportsType, 0, 0, 0);
45 }
46
47 PassOwnPtr<MediaPlayerPrivateInterface> MockMediaPlayerMediaSource::create(MediaPlayer* player)
48 {
49     return adoptPtr(new MockMediaPlayerMediaSource(player));
50 }
51
52 static HashSet<String> mimeTypeCache()
53 {
54     DEFINE_STATIC_LOCAL(HashSet<String>, cache, ());
55     static bool isInitialized = false;
56
57     if (!isInitialized) {
58         isInitialized = true;
59         cache.add(ASCIILiteral("video/mock"));
60     }
61
62     return cache;
63 }
64
65 void MockMediaPlayerMediaSource::getSupportedTypes(HashSet<String>& supportedTypes)
66 {
67     supportedTypes = mimeTypeCache();
68 }
69
70 MediaPlayer::SupportsType MockMediaPlayerMediaSource::supportsType(const MediaEngineSupportParameters& parameters)
71 {
72     if (!parameters.isMediaSource)
73         return MediaPlayer::IsNotSupported;
74
75     if (!mimeTypeCache().contains(parameters.type))
76         return MediaPlayer::IsNotSupported;
77
78     if (parameters.codecs.isEmpty())
79         return MediaPlayer::MayBeSupported;
80
81     return parameters.codecs == "mock" ? MediaPlayer::IsSupported : MediaPlayer::MayBeSupported;
82 }
83
84 MockMediaPlayerMediaSource::MockMediaPlayerMediaSource(MediaPlayer* player)
85     : m_player(player)
86     , m_currentTime(MediaTime::zeroTime())
87     , m_duration(0)
88     , m_readyState(MediaPlayer::HaveNothing)
89     , m_networkState(MediaPlayer::Empty)
90     , m_playing(false)
91 {
92 }
93
94 MockMediaPlayerMediaSource::~MockMediaPlayerMediaSource()
95 {
96 }
97
98 void MockMediaPlayerMediaSource::load(const String&)
99 {
100     ASSERT_NOT_REACHED();
101 }
102
103 void MockMediaPlayerMediaSource::load(const String&, PassRefPtr<HTMLMediaSource> source)
104 {
105     m_mediaSource = source;
106     m_mediaSourcePrivate = MockMediaSourcePrivate::create(this);
107     m_mediaSource->setPrivateAndOpen(*m_mediaSourcePrivate);
108 }
109
110 void MockMediaPlayerMediaSource::cancelLoad()
111 {
112 }
113
114 void MockMediaPlayerMediaSource::play()
115 {
116     m_playing = 1;
117     callOnMainThread(bind(&MockMediaPlayerMediaSource::advanceCurrentTime, this));
118 }
119
120 void MockMediaPlayerMediaSource::pause()
121 {
122     m_playing = 0;
123 }
124
125 IntSize MockMediaPlayerMediaSource::naturalSize() const
126 {
127     return IntSize();
128 }
129
130 bool MockMediaPlayerMediaSource::hasVideo() const
131 {
132     return m_mediaSourcePrivate ? m_mediaSourcePrivate->hasVideo() : false;
133 }
134
135 bool MockMediaPlayerMediaSource::hasAudio() const
136 {
137     return m_mediaSourcePrivate ? m_mediaSourcePrivate->hasAudio() : false;
138 }
139
140 void MockMediaPlayerMediaSource::setVisible(bool)
141 {
142 }
143
144 bool MockMediaPlayerMediaSource::seeking() const
145 {
146     return false;
147 }
148
149 bool MockMediaPlayerMediaSource::paused() const
150 {
151     return !m_playing;
152 }
153
154 MediaPlayer::NetworkState MockMediaPlayerMediaSource::networkState() const
155 {
156     return m_networkState;
157 }
158
159 MediaPlayer::ReadyState MockMediaPlayerMediaSource::readyState() const
160 {
161     return m_readyState;
162 }
163
164 double MockMediaPlayerMediaSource::maxTimeSeekableDouble() const
165 {
166     return m_duration;
167 }
168
169 PassRefPtr<TimeRanges> MockMediaPlayerMediaSource::buffered() const
170 {
171     return m_mediaSource ? m_mediaSource->buffered() : TimeRanges::create();
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 IntRect&)
184 {
185 }
186
187 double MockMediaPlayerMediaSource::currentTimeDouble() const
188 {
189     return m_currentTime.toDouble();
190 }
191
192 double MockMediaPlayerMediaSource::durationDouble() const
193 {
194     return m_duration;
195 }
196
197 void MockMediaPlayerMediaSource::seekWithTolerance(double time, double negativeTolerance, double positiveTolerance)
198 {
199     if (!negativeTolerance && !positiveTolerance) {
200         m_currentTime = MediaTime::createWithDouble(time);
201         m_mediaSourcePrivate->seekToTime(MediaTime::createWithDouble(time));
202     } else
203         m_currentTime = m_mediaSourcePrivate->seekToTime(MediaTime::createWithDouble(time), MediaTime::createWithDouble(negativeTolerance), MediaTime::createWithDouble(positiveTolerance));
204     m_player->timeChanged();
205
206     if (m_playing)
207         callOnMainThread(bind(&MockMediaPlayerMediaSource::advanceCurrentTime, this));
208 }
209
210 void MockMediaPlayerMediaSource::advanceCurrentTime()
211 {
212     RefPtr<TimeRanges> buffered = this->buffered();
213     size_t pos = buffered->find(m_currentTime.toDouble());
214     if (pos == notFound)
215         return;
216
217     m_currentTime = MediaTime::createWithDouble(std::min(m_duration, buffered->end(pos, IGNORE_EXCEPTION)));
218     m_player->timeChanged();
219 }
220
221 void MockMediaPlayerMediaSource::updateDuration(double duration)
222 {
223     if (m_duration == duration)
224         return;
225
226     m_duration = duration;
227     m_player->durationChanged();
228 }
229
230 void MockMediaPlayerMediaSource::setReadyState(MediaPlayer::ReadyState readyState)
231 {
232     if (readyState == m_readyState)
233         return;
234
235     m_readyState = readyState;
236     m_player->readyStateChanged();
237 }
238
239 void MockMediaPlayerMediaSource::setNetworkState(MediaPlayer::NetworkState networkState)
240 {
241     if (networkState == m_networkState)
242         return;
243
244     m_networkState = networkState;
245     m_player->networkStateChanged();
246 }
247
248 }
249
250 #endif