|
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 "Frame.h" |
|
11 #include "GraphicsContext.h" |
|
12 #include "HTMLMediaElement.h" |
|
13 #include "IntSize.h" |
|
14 #include "KURL.h" |
|
15 #include "MediaPlayer.h" |
|
16 #include "NotImplemented.h" |
|
17 #include "RenderView.h" |
|
18 #include "TimeRanges.h" |
|
19 #include "VideoLayerChromium.h" |
|
20 |
|
21 #if USE(ACCELERATED_COMPOSITING) |
|
22 #include "RenderLayerCompositor.h" |
|
23 #endif |
|
24 |
|
25 #include "WebCanvas.h" |
|
26 #include "WebCString.h" |
|
27 #include "WebFrameClient.h" |
|
28 #include "WebFrameImpl.h" |
|
29 #include "WebKit.h" |
|
30 #include "WebKitClient.h" |
|
31 #include "WebMediaElement.h" |
|
32 #include "WebMediaPlayer.h" |
|
33 #include "WebMimeRegistry.h" |
|
34 #include "WebRect.h" |
|
35 #include "WebSize.h" |
|
36 #include "WebString.h" |
|
37 #include "WebURL.h" |
|
38 #include "WebViewImpl.h" |
|
39 |
|
40 // WebCommon.h defines WEBKIT_USING_SKIA so this has to be included last. |
|
41 #if WEBKIT_USING_SKIA |
|
42 #include "PlatformContextSkia.h" |
|
43 #endif |
|
44 |
|
45 #include <wtf/Assertions.h> |
|
46 #include <wtf/text/CString.h> |
|
47 |
|
48 using namespace WebCore; |
|
49 |
|
50 namespace WebKit { |
|
51 |
|
52 static WebMediaPlayer* createWebMediaPlayer( |
|
53 WebMediaPlayerClient* client, Frame* frame) |
|
54 { |
|
55 WebFrameImpl* webFrame = WebFrameImpl::fromFrame(frame); |
|
56 |
|
57 if (!webFrame->client()) |
|
58 return 0; |
|
59 return webFrame->client()->createMediaPlayer(webFrame, client); |
|
60 } |
|
61 |
|
62 bool WebMediaPlayerClientImpl::m_isEnabled = false; |
|
63 |
|
64 bool WebMediaPlayerClientImpl::isEnabled() |
|
65 { |
|
66 return m_isEnabled; |
|
67 } |
|
68 |
|
69 void WebMediaPlayerClientImpl::setIsEnabled(bool isEnabled) |
|
70 { |
|
71 m_isEnabled = isEnabled; |
|
72 } |
|
73 |
|
74 void WebMediaPlayerClientImpl::registerSelf(MediaEngineRegistrar registrar) |
|
75 { |
|
76 if (m_isEnabled) { |
|
77 registrar(WebMediaPlayerClientImpl::create, |
|
78 WebMediaPlayerClientImpl::getSupportedTypes, |
|
79 WebMediaPlayerClientImpl::supportsType); |
|
80 } |
|
81 } |
|
82 |
|
83 WebMediaPlayerClientImpl* WebMediaPlayerClientImpl::fromMediaElement(const WebMediaElement* element) |
|
84 { |
|
85 PlatformMedia pm = element->constUnwrap<HTMLMediaElement>()->platformMedia(); |
|
86 return static_cast<WebMediaPlayerClientImpl*>(pm.media.chromiumMediaPlayer); |
|
87 } |
|
88 |
|
89 WebMediaPlayer* WebMediaPlayerClientImpl::mediaPlayer() const |
|
90 { |
|
91 return m_webMediaPlayer.get(); |
|
92 } |
|
93 |
|
94 // WebMediaPlayerClient -------------------------------------------------------- |
|
95 |
|
96 void WebMediaPlayerClientImpl::networkStateChanged() |
|
97 { |
|
98 ASSERT(m_mediaPlayer); |
|
99 m_mediaPlayer->networkStateChanged(); |
|
100 } |
|
101 |
|
102 void WebMediaPlayerClientImpl::readyStateChanged() |
|
103 { |
|
104 ASSERT(m_mediaPlayer); |
|
105 m_mediaPlayer->readyStateChanged(); |
|
106 } |
|
107 |
|
108 void WebMediaPlayerClientImpl::volumeChanged(float newVolume) |
|
109 { |
|
110 ASSERT(m_mediaPlayer); |
|
111 m_mediaPlayer->volumeChanged(newVolume); |
|
112 } |
|
113 |
|
114 void WebMediaPlayerClientImpl::muteChanged(bool newMute) |
|
115 { |
|
116 ASSERT(m_mediaPlayer); |
|
117 m_mediaPlayer->muteChanged(newMute); |
|
118 } |
|
119 |
|
120 void WebMediaPlayerClientImpl::timeChanged() |
|
121 { |
|
122 ASSERT(m_mediaPlayer); |
|
123 m_mediaPlayer->timeChanged(); |
|
124 } |
|
125 |
|
126 void WebMediaPlayerClientImpl::repaint() |
|
127 { |
|
128 ASSERT(m_mediaPlayer); |
|
129 m_mediaPlayer->repaint(); |
|
130 } |
|
131 |
|
132 void WebMediaPlayerClientImpl::durationChanged() |
|
133 { |
|
134 ASSERT(m_mediaPlayer); |
|
135 m_mediaPlayer->durationChanged(); |
|
136 } |
|
137 |
|
138 void WebMediaPlayerClientImpl::rateChanged() |
|
139 { |
|
140 ASSERT(m_mediaPlayer); |
|
141 m_mediaPlayer->rateChanged(); |
|
142 } |
|
143 |
|
144 void WebMediaPlayerClientImpl::sizeChanged() |
|
145 { |
|
146 ASSERT(m_mediaPlayer); |
|
147 m_mediaPlayer->sizeChanged(); |
|
148 } |
|
149 |
|
150 void WebMediaPlayerClientImpl::sawUnsupportedTracks() |
|
151 { |
|
152 ASSERT(m_mediaPlayer); |
|
153 m_mediaPlayer->mediaPlayerClient()->mediaPlayerSawUnsupportedTracks(m_mediaPlayer); |
|
154 } |
|
155 |
|
156 float WebMediaPlayerClientImpl::volume() const |
|
157 { |
|
158 if (m_mediaPlayer) |
|
159 return m_mediaPlayer->volume(); |
|
160 return 0.0f; |
|
161 } |
|
162 |
|
163 // MediaPlayerPrivateInterface ------------------------------------------------- |
|
164 |
|
165 void WebMediaPlayerClientImpl::load(const String& url) |
|
166 { |
|
167 Frame* frame = static_cast<HTMLMediaElement*>( |
|
168 m_mediaPlayer->mediaPlayerClient())->document()->frame(); |
|
169 |
|
170 m_webMediaPlayer.set(createWebMediaPlayer(this, frame)); |
|
171 if (m_webMediaPlayer.get()) |
|
172 m_webMediaPlayer->load(KURL(ParsedURLString, url)); |
|
173 } |
|
174 |
|
175 void WebMediaPlayerClientImpl::cancelLoad() |
|
176 { |
|
177 if (m_webMediaPlayer.get()) |
|
178 m_webMediaPlayer->cancelLoad(); |
|
179 } |
|
180 |
|
181 #if USE(ACCELERATED_COMPOSITING) |
|
182 PlatformLayer* WebMediaPlayerClientImpl::platformLayer() const |
|
183 { |
|
184 ASSERT(m_supportsAcceleratedCompositing); |
|
185 return m_videoLayer.get(); |
|
186 } |
|
187 #endif |
|
188 |
|
189 PlatformMedia WebMediaPlayerClientImpl::platformMedia() const |
|
190 { |
|
191 PlatformMedia pm; |
|
192 pm.type = PlatformMedia::ChromiumMediaPlayerType; |
|
193 pm.media.chromiumMediaPlayer = const_cast<WebMediaPlayerClientImpl*>(this); |
|
194 return pm; |
|
195 } |
|
196 |
|
197 void WebMediaPlayerClientImpl::play() |
|
198 { |
|
199 if (m_webMediaPlayer.get()) |
|
200 m_webMediaPlayer->play(); |
|
201 } |
|
202 |
|
203 void WebMediaPlayerClientImpl::pause() |
|
204 { |
|
205 if (m_webMediaPlayer.get()) |
|
206 m_webMediaPlayer->pause(); |
|
207 } |
|
208 |
|
209 IntSize WebMediaPlayerClientImpl::naturalSize() const |
|
210 { |
|
211 if (m_webMediaPlayer.get()) |
|
212 return m_webMediaPlayer->naturalSize(); |
|
213 return IntSize(); |
|
214 } |
|
215 |
|
216 bool WebMediaPlayerClientImpl::hasVideo() const |
|
217 { |
|
218 if (m_webMediaPlayer.get()) |
|
219 return m_webMediaPlayer->hasVideo(); |
|
220 return false; |
|
221 } |
|
222 |
|
223 bool WebMediaPlayerClientImpl::hasAudio() const |
|
224 { |
|
225 if (m_webMediaPlayer.get()) |
|
226 return m_webMediaPlayer->hasAudio(); |
|
227 return false; |
|
228 } |
|
229 |
|
230 void WebMediaPlayerClientImpl::setVisible(bool visible) |
|
231 { |
|
232 if (m_webMediaPlayer.get()) |
|
233 m_webMediaPlayer->setVisible(visible); |
|
234 } |
|
235 |
|
236 float WebMediaPlayerClientImpl::duration() const |
|
237 { |
|
238 if (m_webMediaPlayer.get()) |
|
239 return m_webMediaPlayer->duration(); |
|
240 return 0.0f; |
|
241 } |
|
242 |
|
243 float WebMediaPlayerClientImpl::currentTime() const |
|
244 { |
|
245 if (m_webMediaPlayer.get()) |
|
246 return m_webMediaPlayer->currentTime(); |
|
247 return 0.0f; |
|
248 } |
|
249 |
|
250 void WebMediaPlayerClientImpl::seek(float time) |
|
251 { |
|
252 if (m_webMediaPlayer.get()) |
|
253 m_webMediaPlayer->seek(time); |
|
254 } |
|
255 |
|
256 bool WebMediaPlayerClientImpl::seeking() const |
|
257 { |
|
258 if (m_webMediaPlayer.get()) |
|
259 return m_webMediaPlayer->seeking(); |
|
260 return false; |
|
261 } |
|
262 |
|
263 void WebMediaPlayerClientImpl::setEndTime(float time) |
|
264 { |
|
265 if (m_webMediaPlayer.get()) |
|
266 m_webMediaPlayer->setEndTime(time); |
|
267 } |
|
268 |
|
269 void WebMediaPlayerClientImpl::setRate(float rate) |
|
270 { |
|
271 if (m_webMediaPlayer.get()) |
|
272 m_webMediaPlayer->setRate(rate); |
|
273 } |
|
274 |
|
275 bool WebMediaPlayerClientImpl::paused() const |
|
276 { |
|
277 if (m_webMediaPlayer.get()) |
|
278 return m_webMediaPlayer->paused(); |
|
279 return false; |
|
280 } |
|
281 |
|
282 bool WebMediaPlayerClientImpl::supportsFullscreen() const |
|
283 { |
|
284 if (m_webMediaPlayer.get()) |
|
285 return m_webMediaPlayer->supportsFullscreen(); |
|
286 return false; |
|
287 } |
|
288 |
|
289 bool WebMediaPlayerClientImpl::supportsSave() const |
|
290 { |
|
291 if (m_webMediaPlayer.get()) |
|
292 return m_webMediaPlayer->supportsSave(); |
|
293 return false; |
|
294 } |
|
295 |
|
296 void WebMediaPlayerClientImpl::setVolume(float volume) |
|
297 { |
|
298 if (m_webMediaPlayer.get()) |
|
299 m_webMediaPlayer->setVolume(volume); |
|
300 } |
|
301 |
|
302 MediaPlayer::NetworkState WebMediaPlayerClientImpl::networkState() const |
|
303 { |
|
304 if (m_webMediaPlayer.get()) |
|
305 return static_cast<MediaPlayer::NetworkState>(m_webMediaPlayer->networkState()); |
|
306 return MediaPlayer::Empty; |
|
307 } |
|
308 |
|
309 MediaPlayer::ReadyState WebMediaPlayerClientImpl::readyState() const |
|
310 { |
|
311 if (m_webMediaPlayer.get()) |
|
312 return static_cast<MediaPlayer::ReadyState>(m_webMediaPlayer->readyState()); |
|
313 return MediaPlayer::HaveNothing; |
|
314 } |
|
315 |
|
316 float WebMediaPlayerClientImpl::maxTimeSeekable() const |
|
317 { |
|
318 if (m_webMediaPlayer.get()) |
|
319 return m_webMediaPlayer->maxTimeSeekable(); |
|
320 return 0.0f; |
|
321 } |
|
322 |
|
323 PassRefPtr<TimeRanges> WebMediaPlayerClientImpl::buffered() const |
|
324 { |
|
325 if (m_webMediaPlayer.get()) { |
|
326 const WebTimeRanges& webRanges = m_webMediaPlayer->buffered(); |
|
327 |
|
328 // FIXME: Save the time ranges in a member variable and update it when needed. |
|
329 RefPtr<TimeRanges> ranges = TimeRanges::create(); |
|
330 for (size_t i = 0; i < webRanges.size(); ++i) |
|
331 ranges->add(webRanges[i].start, webRanges[i].end); |
|
332 return ranges.release(); |
|
333 } |
|
334 return TimeRanges::create(); |
|
335 } |
|
336 |
|
337 int WebMediaPlayerClientImpl::dataRate() const |
|
338 { |
|
339 if (m_webMediaPlayer.get()) |
|
340 return m_webMediaPlayer->dataRate(); |
|
341 return 0; |
|
342 } |
|
343 |
|
344 bool WebMediaPlayerClientImpl::totalBytesKnown() const |
|
345 { |
|
346 if (m_webMediaPlayer.get()) |
|
347 return m_webMediaPlayer->totalBytesKnown(); |
|
348 return false; |
|
349 } |
|
350 |
|
351 unsigned WebMediaPlayerClientImpl::totalBytes() const |
|
352 { |
|
353 if (m_webMediaPlayer.get()) |
|
354 return static_cast<unsigned>(m_webMediaPlayer->totalBytes()); |
|
355 return 0; |
|
356 } |
|
357 |
|
358 unsigned WebMediaPlayerClientImpl::bytesLoaded() const |
|
359 { |
|
360 if (m_webMediaPlayer.get()) |
|
361 return static_cast<unsigned>(m_webMediaPlayer->bytesLoaded()); |
|
362 return 0; |
|
363 } |
|
364 |
|
365 void WebMediaPlayerClientImpl::setSize(const IntSize& size) |
|
366 { |
|
367 if (m_webMediaPlayer.get()) |
|
368 m_webMediaPlayer->setSize(WebSize(size.width(), size.height())); |
|
369 } |
|
370 |
|
371 void WebMediaPlayerClientImpl::paint(GraphicsContext* context, const IntRect& rect) |
|
372 { |
|
373 // Normally GraphicsContext operations do nothing when painting is disabled. |
|
374 // Since we're accessing platformContext() directly we have to manually |
|
375 // check. |
|
376 if (m_webMediaPlayer.get() && !context->paintingDisabled()) { |
|
377 #if WEBKIT_USING_SKIA |
|
378 m_webMediaPlayer->paint(context->platformContext()->canvas(), rect); |
|
379 #elif WEBKIT_USING_CG |
|
380 m_webMediaPlayer->paint(context->platformContext(), rect); |
|
381 #else |
|
382 notImplemented(); |
|
383 #endif |
|
384 } |
|
385 } |
|
386 |
|
387 void WebMediaPlayerClientImpl::setAutobuffer(bool autoBuffer) |
|
388 { |
|
389 if (m_webMediaPlayer.get()) |
|
390 m_webMediaPlayer->setAutoBuffer(autoBuffer); |
|
391 } |
|
392 |
|
393 bool WebMediaPlayerClientImpl::hasSingleSecurityOrigin() const |
|
394 { |
|
395 if (m_webMediaPlayer.get()) |
|
396 return m_webMediaPlayer->hasSingleSecurityOrigin(); |
|
397 return false; |
|
398 } |
|
399 |
|
400 #if USE(ACCELERATED_COMPOSITING) |
|
401 bool WebMediaPlayerClientImpl::supportsAcceleratedRendering() const |
|
402 { |
|
403 return m_supportsAcceleratedCompositing; |
|
404 } |
|
405 #endif |
|
406 |
|
407 MediaPlayer::MovieLoadType WebMediaPlayerClientImpl::movieLoadType() const |
|
408 { |
|
409 if (m_webMediaPlayer.get()) |
|
410 return static_cast<MediaPlayer::MovieLoadType>( |
|
411 m_webMediaPlayer->movieLoadType()); |
|
412 return MediaPlayer::Unknown; |
|
413 } |
|
414 |
|
415 MediaPlayerPrivateInterface* WebMediaPlayerClientImpl::create(MediaPlayer* player) |
|
416 { |
|
417 WebMediaPlayerClientImpl* client = new WebMediaPlayerClientImpl(); |
|
418 client->m_mediaPlayer = player; |
|
419 |
|
420 #if USE(ACCELERATED_COMPOSITING) |
|
421 Frame* frame = static_cast<HTMLMediaElement*>( |
|
422 client->m_mediaPlayer->mediaPlayerClient())->document()->frame(); |
|
423 |
|
424 // This does not actually check whether the hardware can support accelerated |
|
425 // compositing, but only if the flag is set. However, this is checked lazily |
|
426 // in WebViewImpl::setIsAcceleratedCompositingActive() and will fail there |
|
427 // if necessary. |
|
428 client->m_supportsAcceleratedCompositing = |
|
429 frame->contentRenderer()->compositor()->hasAcceleratedCompositing(); |
|
430 |
|
431 if (client->m_supportsAcceleratedCompositing) |
|
432 client->m_videoLayer = VideoLayerChromium::create(0); |
|
433 #endif |
|
434 |
|
435 return client; |
|
436 } |
|
437 |
|
438 void WebMediaPlayerClientImpl::getSupportedTypes(HashSet<String>& supportedTypes) |
|
439 { |
|
440 // FIXME: integrate this list with WebMediaPlayerClientImpl::supportsType. |
|
441 notImplemented(); |
|
442 } |
|
443 |
|
444 MediaPlayer::SupportsType WebMediaPlayerClientImpl::supportsType(const String& type, |
|
445 const String& codecs) |
|
446 { |
|
447 WebMimeRegistry::SupportsType supportsType = |
|
448 webKitClient()->mimeRegistry()->supportsMediaMIMEType(type, codecs); |
|
449 |
|
450 switch (supportsType) { |
|
451 default: |
|
452 ASSERT_NOT_REACHED(); |
|
453 case WebMimeRegistry::IsNotSupported: |
|
454 return MediaPlayer::IsNotSupported; |
|
455 case WebMimeRegistry::IsSupported: |
|
456 return MediaPlayer::IsSupported; |
|
457 case WebMimeRegistry::MayBeSupported: |
|
458 return MediaPlayer::MayBeSupported; |
|
459 } |
|
460 return MediaPlayer::IsNotSupported; |
|
461 } |
|
462 |
|
463 WebMediaPlayerClientImpl::WebMediaPlayerClientImpl() |
|
464 : m_mediaPlayer(0) |
|
465 #if USE(ACCELERATED_COMPOSITING) |
|
466 , m_videoLayer(0) |
|
467 , m_supportsAcceleratedCompositing(false) |
|
468 #endif |
|
469 { |
|
470 } |
|
471 |
|
472 } // namespace WebKit |
|
473 |
|
474 #endif // ENABLE(VIDEO) |