1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "WebMediaPlayerClientImpl.h"
7
8 #if ENABLE(VIDEO)
9
10 #include "CString.h"
11 #include "Frame.h"
12 #include "GraphicsContext.h"
13 #include "HTMLMediaElement.h"
14 #include "IntSize.h"
15 #include "KURL.h"
16 #include "MediaPlayer.h"
17 #include "NotImplemented.h"
18 #include "TimeRanges.h"
19
20 #include "WebCanvas.h"
21 #include "WebCString.h"
22 #include "WebFrameClient.h"
23 #include "WebFrameImpl.h"
24 #include "WebKit.h"
25 #include "WebKitClient.h"
26 #include "WebMediaPlayer.h"
27 #include "WebMimeRegistry.h"
28 #include "WebRect.h"
29 #include "WebSize.h"
30 #include "WebString.h"
31 #include "WebURL.h"
32
33 // WebCommon.h defines WEBKIT_USING_SKIA so this has to be included last.
34 #if WEBKIT_USING_SKIA
35 #include "PlatformContextSkia.h"
36 #endif
37
38 #include <wtf/Assertions.h>
39
40 using namespace WebCore;
41
42 namespace WebKit {
43
createWebMediaPlayer(WebMediaPlayerClient * client,Frame * frame)44 static WebMediaPlayer* createWebMediaPlayer(
45 WebMediaPlayerClient* client, Frame* frame)
46 {
47 WebFrameImpl* webFrame = WebFrameImpl::fromFrame(frame);
48 if (!webFrame->client())
49 return 0;
50 return webFrame->client()->createMediaPlayer(webFrame, client);
51 }
52
53 bool WebMediaPlayerClientImpl::m_isEnabled = false;
54
isEnabled()55 bool WebMediaPlayerClientImpl::isEnabled()
56 {
57 return m_isEnabled;
58 }
59
setIsEnabled(bool isEnabled)60 void WebMediaPlayerClientImpl::setIsEnabled(bool isEnabled)
61 {
62 m_isEnabled = isEnabled;
63 }
64
registerSelf(MediaEngineRegistrar registrar)65 void WebMediaPlayerClientImpl::registerSelf(MediaEngineRegistrar registrar)
66 {
67 if (m_isEnabled) {
68 registrar(WebMediaPlayerClientImpl::create,
69 WebMediaPlayerClientImpl::getSupportedTypes,
70 WebMediaPlayerClientImpl::supportsType);
71 }
72 }
73
74 // WebMediaPlayerClient --------------------------------------------------------
75
networkStateChanged()76 void WebMediaPlayerClientImpl::networkStateChanged()
77 {
78 ASSERT(m_mediaPlayer);
79 m_mediaPlayer->networkStateChanged();
80 }
81
readyStateChanged()82 void WebMediaPlayerClientImpl::readyStateChanged()
83 {
84 ASSERT(m_mediaPlayer);
85 m_mediaPlayer->readyStateChanged();
86 }
87
volumeChanged(float newVolume)88 void WebMediaPlayerClientImpl::volumeChanged(float newVolume)
89 {
90 ASSERT(m_mediaPlayer);
91 m_mediaPlayer->volumeChanged(newVolume);
92 }
93
muteChanged(bool newMute)94 void WebMediaPlayerClientImpl::muteChanged(bool newMute)
95 {
96 ASSERT(m_mediaPlayer);
97 m_mediaPlayer->muteChanged(newMute);
98 }
99
timeChanged()100 void WebMediaPlayerClientImpl::timeChanged()
101 {
102 ASSERT(m_mediaPlayer);
103 m_mediaPlayer->timeChanged();
104 }
105
repaint()106 void WebMediaPlayerClientImpl::repaint()
107 {
108 ASSERT(m_mediaPlayer);
109 m_mediaPlayer->repaint();
110 }
111
durationChanged()112 void WebMediaPlayerClientImpl::durationChanged()
113 {
114 ASSERT(m_mediaPlayer);
115 m_mediaPlayer->durationChanged();
116 }
117
rateChanged()118 void WebMediaPlayerClientImpl::rateChanged()
119 {
120 ASSERT(m_mediaPlayer);
121 m_mediaPlayer->rateChanged();
122 }
123
sizeChanged()124 void WebMediaPlayerClientImpl::sizeChanged()
125 {
126 ASSERT(m_mediaPlayer);
127 m_mediaPlayer->sizeChanged();
128 }
129
sawUnsupportedTracks()130 void WebMediaPlayerClientImpl::sawUnsupportedTracks()
131 {
132 ASSERT(m_mediaPlayer);
133 m_mediaPlayer->mediaPlayerClient()->mediaPlayerSawUnsupportedTracks(m_mediaPlayer);
134 }
135
136 // MediaPlayerPrivateInterface -------------------------------------------------
137
load(const String & url)138 void WebMediaPlayerClientImpl::load(const String& url)
139 {
140 Frame* frame = static_cast<HTMLMediaElement*>(
141 m_mediaPlayer->mediaPlayerClient())->document()->frame();
142 m_webMediaPlayer.set(createWebMediaPlayer(this, frame));
143 if (m_webMediaPlayer.get())
144 m_webMediaPlayer->load(KURL(ParsedURLString, url));
145 }
146
cancelLoad()147 void WebMediaPlayerClientImpl::cancelLoad()
148 {
149 if (m_webMediaPlayer.get())
150 m_webMediaPlayer->cancelLoad();
151 }
152
play()153 void WebMediaPlayerClientImpl::play()
154 {
155 if (m_webMediaPlayer.get())
156 m_webMediaPlayer->play();
157 }
158
pause()159 void WebMediaPlayerClientImpl::pause()
160 {
161 if (m_webMediaPlayer.get())
162 m_webMediaPlayer->pause();
163 }
164
naturalSize() const165 IntSize WebMediaPlayerClientImpl::naturalSize() const
166 {
167 if (m_webMediaPlayer.get())
168 return m_webMediaPlayer->naturalSize();
169 return IntSize();
170 }
171
hasVideo() const172 bool WebMediaPlayerClientImpl::hasVideo() const
173 {
174 if (m_webMediaPlayer.get())
175 return m_webMediaPlayer->hasVideo();
176 return false;
177 }
178
hasAudio() const179 bool WebMediaPlayerClientImpl::hasAudio() const
180 {
181 if (m_webMediaPlayer.get())
182 return m_webMediaPlayer->hasAudio();
183 return false;
184 }
185
setVisible(bool visible)186 void WebMediaPlayerClientImpl::setVisible(bool visible)
187 {
188 if (m_webMediaPlayer.get())
189 m_webMediaPlayer->setVisible(visible);
190 }
191
duration() const192 float WebMediaPlayerClientImpl::duration() const
193 {
194 if (m_webMediaPlayer.get())
195 return m_webMediaPlayer->duration();
196 return 0.0f;
197 }
198
currentTime() const199 float WebMediaPlayerClientImpl::currentTime() const
200 {
201 if (m_webMediaPlayer.get())
202 return m_webMediaPlayer->currentTime();
203 return 0.0f;
204 }
205
seek(float time)206 void WebMediaPlayerClientImpl::seek(float time)
207 {
208 if (m_webMediaPlayer.get())
209 m_webMediaPlayer->seek(time);
210 }
211
seeking() const212 bool WebMediaPlayerClientImpl::seeking() const
213 {
214 if (m_webMediaPlayer.get())
215 return m_webMediaPlayer->seeking();
216 return false;
217 }
218
setEndTime(float time)219 void WebMediaPlayerClientImpl::setEndTime(float time)
220 {
221 if (m_webMediaPlayer.get())
222 m_webMediaPlayer->setEndTime(time);
223 }
224
setRate(float rate)225 void WebMediaPlayerClientImpl::setRate(float rate)
226 {
227 if (m_webMediaPlayer.get())
228 m_webMediaPlayer->setRate(rate);
229 }
230
paused() const231 bool WebMediaPlayerClientImpl::paused() const
232 {
233 if (m_webMediaPlayer.get())
234 return m_webMediaPlayer->paused();
235 return false;
236 }
237
supportsFullscreen() const238 bool WebMediaPlayerClientImpl::supportsFullscreen() const
239 {
240 if (m_webMediaPlayer.get())
241 return m_webMediaPlayer->supportsFullscreen();
242 return false;
243 }
244
supportsSave() const245 bool WebMediaPlayerClientImpl::supportsSave() const
246 {
247 if (m_webMediaPlayer.get())
248 return m_webMediaPlayer->supportsSave();
249 return false;
250 }
251
setVolume(float volume)252 void WebMediaPlayerClientImpl::setVolume(float volume)
253 {
254 if (m_webMediaPlayer.get())
255 m_webMediaPlayer->setVolume(volume);
256 }
257
networkState() const258 MediaPlayer::NetworkState WebMediaPlayerClientImpl::networkState() const
259 {
260 if (m_webMediaPlayer.get())
261 return static_cast<MediaPlayer::NetworkState>(m_webMediaPlayer->networkState());
262 return MediaPlayer::Empty;
263 }
264
readyState() const265 MediaPlayer::ReadyState WebMediaPlayerClientImpl::readyState() const
266 {
267 if (m_webMediaPlayer.get())
268 return static_cast<MediaPlayer::ReadyState>(m_webMediaPlayer->readyState());
269 return MediaPlayer::HaveNothing;
270 }
271
maxTimeSeekable() const272 float WebMediaPlayerClientImpl::maxTimeSeekable() const
273 {
274 if (m_webMediaPlayer.get())
275 return m_webMediaPlayer->maxTimeSeekable();
276 return 0.0f;
277 }
278
buffered() const279 PassRefPtr<TimeRanges> WebMediaPlayerClientImpl::buffered() const
280 {
281 if (m_webMediaPlayer.get()) {
282 const WebTimeRanges& webRanges = m_webMediaPlayer->buffered();
283
284 // FIXME: Save the time ranges in a member variable and update it when needed.
285 RefPtr<TimeRanges> ranges = TimeRanges::create();
286 for (size_t i = 0; i < webRanges.size(); ++i)
287 ranges->add(webRanges[i].start, webRanges[i].end);
288 return ranges.release();
289 }
290 return TimeRanges::create();
291 }
292
dataRate() const293 int WebMediaPlayerClientImpl::dataRate() const
294 {
295 if (m_webMediaPlayer.get())
296 return m_webMediaPlayer->dataRate();
297 return 0;
298 }
299
totalBytesKnown() const300 bool WebMediaPlayerClientImpl::totalBytesKnown() const
301 {
302 if (m_webMediaPlayer.get())
303 return m_webMediaPlayer->totalBytesKnown();
304 return false;
305 }
306
totalBytes() const307 unsigned WebMediaPlayerClientImpl::totalBytes() const
308 {
309 if (m_webMediaPlayer.get())
310 return static_cast<unsigned>(m_webMediaPlayer->totalBytes());
311 return 0;
312 }
313
bytesLoaded() const314 unsigned WebMediaPlayerClientImpl::bytesLoaded() const
315 {
316 if (m_webMediaPlayer.get())
317 return static_cast<unsigned>(m_webMediaPlayer->bytesLoaded());
318 return 0;
319 }
320
setSize(const IntSize & size)321 void WebMediaPlayerClientImpl::setSize(const IntSize& size)
322 {
323 if (m_webMediaPlayer.get())
324 m_webMediaPlayer->setSize(WebSize(size.width(), size.height()));
325 }
326
paint(GraphicsContext * context,const IntRect & rect)327 void WebMediaPlayerClientImpl::paint(GraphicsContext* context, const IntRect& rect)
328 {
329 // Normally GraphicsContext operations do nothing when painting is disabled.
330 // Since we're accessing platformContext() directly we have to manually
331 // check.
332 if (m_webMediaPlayer.get() && !context->paintingDisabled()) {
333 #if WEBKIT_USING_SKIA
334 m_webMediaPlayer->paint(context->platformContext()->canvas(), rect);
335 #elif WEBKIT_USING_CG
336 m_webMediaPlayer->paint(context->platformContext(), rect);
337 #else
338 notImplemented();
339 #endif
340 }
341 }
342
setAutobuffer(bool autoBuffer)343 void WebMediaPlayerClientImpl::setAutobuffer(bool autoBuffer)
344 {
345 if (m_webMediaPlayer.get())
346 m_webMediaPlayer->setAutoBuffer(autoBuffer);
347 }
348
hasSingleSecurityOrigin() const349 bool WebMediaPlayerClientImpl::hasSingleSecurityOrigin() const
350 {
351 if (m_webMediaPlayer.get())
352 return m_webMediaPlayer->hasSingleSecurityOrigin();
353 return false;
354 }
355
movieLoadType() const356 MediaPlayer::MovieLoadType WebMediaPlayerClientImpl::movieLoadType() const
357 {
358 if (m_webMediaPlayer.get())
359 return static_cast<MediaPlayer::MovieLoadType>(
360 m_webMediaPlayer->movieLoadType());
361 return MediaPlayer::Unknown;
362 }
363
create(MediaPlayer * player)364 MediaPlayerPrivateInterface* WebMediaPlayerClientImpl::create(MediaPlayer* player)
365 {
366 WebMediaPlayerClientImpl* client = new WebMediaPlayerClientImpl();
367 client->m_mediaPlayer = player;
368 return client;
369 }
370
getSupportedTypes(HashSet<String> & supportedTypes)371 void WebMediaPlayerClientImpl::getSupportedTypes(HashSet<String>& supportedTypes)
372 {
373 // FIXME: integrate this list with WebMediaPlayerClientImpl::supportsType.
374 notImplemented();
375 }
376
supportsType(const String & type,const String & codecs)377 MediaPlayer::SupportsType WebMediaPlayerClientImpl::supportsType(const String& type,
378 const String& codecs)
379 {
380 WebMimeRegistry::SupportsType supportsType =
381 webKitClient()->mimeRegistry()->supportsMediaMIMEType(type, codecs);
382
383 switch (supportsType) {
384 default:
385 ASSERT_NOT_REACHED();
386 case WebMimeRegistry::IsNotSupported:
387 return MediaPlayer::IsNotSupported;
388 case WebMimeRegistry::IsSupported:
389 return MediaPlayer::IsSupported;
390 case WebMimeRegistry::MayBeSupported:
391 return MediaPlayer::MayBeSupported;
392 }
393 return MediaPlayer::IsNotSupported;
394 }
395
WebMediaPlayerClientImpl()396 WebMediaPlayerClientImpl::WebMediaPlayerClientImpl()
397 : m_mediaPlayer(0)
398 {
399 }
400
401 } // namespace WebKit
402
403 #endif // ENABLE(VIDEO)
404