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