Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 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 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 8 | #include "WebDocument.h" |
| 9 | #include "WebFrameClient.h" |
| 10 | #include "WebFrameImpl.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 11 | #include "WebViewImpl.h" |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 12 | #include "core/frame/LocalFrame.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 13 | #include "core/html/HTMLMediaElement.h" |
| 14 | #include "core/html/TimeRanges.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 15 | #include "core/rendering/RenderView.h" |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 16 | #include "core/rendering/compositing/RenderLayerCompositor.h" |
| 17 | #include "modules/encryptedmedia/HTMLMediaElementEncryptedMedia.h" |
| 18 | #include "modules/encryptedmedia/MediaKeyNeededEvent.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 19 | #include "modules/mediastream/MediaStreamRegistry.h" |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 20 | #include "platform/audio/AudioBus.h" |
| 21 | #include "platform/audio/AudioSourceProviderClient.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 22 | #include "platform/geometry/IntSize.h" |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 23 | #include "platform/graphics/GraphicsContext.h" |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 24 | #include "platform/graphics/GraphicsLayer.h" |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 25 | #include "platform/graphics/gpu/Extensions3DUtil.h" |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 26 | #include "platform/graphics/skia/GaneshUtils.h" |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 27 | #include "public/platform/Platform.h" |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 28 | #include "public/platform/WebAudioSourceProvider.h" |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 29 | #include "public/platform/WebCString.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 30 | #include "public/platform/WebCanvas.h" |
| 31 | #include "public/platform/WebCompositorSupport.h" |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 32 | #include "public/platform/WebContentDecryptionModule.h" |
| 33 | #include "public/platform/WebGraphicsContext3DProvider.h" |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 34 | #include "public/platform/WebInbandTextTrack.h" |
| 35 | #include "public/platform/WebMediaPlayer.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 36 | #include "public/platform/WebRect.h" |
| 37 | #include "public/platform/WebString.h" |
| 38 | #include "public/platform/WebURL.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 39 | |
| 40 | #if OS(ANDROID) |
| 41 | #include "GrContext.h" |
| 42 | #include "GrTypes.h" |
| 43 | #include "SkCanvas.h" |
| 44 | #include "SkGrPixelRef.h" |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 45 | #endif |
| 46 | |
| 47 | |
| 48 | #include "wtf/Assertions.h" |
| 49 | #include "wtf/text/CString.h" |
| 50 | |
| 51 | using namespace WebCore; |
| 52 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 53 | namespace blink { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 54 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 55 | static PassOwnPtr<WebMediaPlayer> createWebMediaPlayer(WebMediaPlayerClient* client, const WebURL& url, LocalFrame* frame) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 56 | { |
| 57 | WebFrameImpl* webFrame = WebFrameImpl::fromFrame(frame); |
| 58 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 59 | if (!webFrame || !webFrame->client()) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 60 | return nullptr; |
| 61 | return adoptPtr(webFrame->client()->createMediaPlayer(webFrame, url, client)); |
| 62 | } |
| 63 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 64 | WebMediaPlayer* WebMediaPlayerClientImpl::webMediaPlayer() const |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 65 | { |
| 66 | return m_webMediaPlayer.get(); |
| 67 | } |
| 68 | |
| 69 | // WebMediaPlayerClient -------------------------------------------------------- |
| 70 | |
| 71 | WebMediaPlayerClientImpl::~WebMediaPlayerClientImpl() |
| 72 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 73 | HTMLMediaElementEncryptedMedia::playerDestroyed(mediaElement()); |
| 74 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 75 | // Explicitly destroy the WebMediaPlayer to allow verification of tear down. |
| 76 | m_webMediaPlayer.clear(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void WebMediaPlayerClientImpl::networkStateChanged() |
| 80 | { |
| 81 | m_client->mediaPlayerNetworkStateChanged(); |
| 82 | } |
| 83 | |
| 84 | void WebMediaPlayerClientImpl::readyStateChanged() |
| 85 | { |
| 86 | m_client->mediaPlayerReadyStateChanged(); |
| 87 | } |
| 88 | |
| 89 | void WebMediaPlayerClientImpl::timeChanged() |
| 90 | { |
| 91 | m_client->mediaPlayerTimeChanged(); |
| 92 | } |
| 93 | |
| 94 | void WebMediaPlayerClientImpl::repaint() |
| 95 | { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 96 | m_client->mediaPlayerRepaint(); |
| 97 | } |
| 98 | |
| 99 | void WebMediaPlayerClientImpl::durationChanged() |
| 100 | { |
| 101 | m_client->mediaPlayerDurationChanged(); |
| 102 | } |
| 103 | |
| 104 | void WebMediaPlayerClientImpl::sizeChanged() |
| 105 | { |
| 106 | m_client->mediaPlayerSizeChanged(); |
| 107 | } |
| 108 | |
| 109 | void WebMediaPlayerClientImpl::setOpaque(bool opaque) |
| 110 | { |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 111 | m_client->mediaPlayerSetOpaque(opaque); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | double WebMediaPlayerClientImpl::volume() const |
| 115 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 116 | return mediaElement().playerVolume(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void WebMediaPlayerClientImpl::playbackStateChanged() |
| 120 | { |
| 121 | m_client->mediaPlayerPlaybackStateChanged(); |
| 122 | } |
| 123 | |
| 124 | WebMediaPlayer::Preload WebMediaPlayerClientImpl::preload() const |
| 125 | { |
| 126 | return static_cast<WebMediaPlayer::Preload>(m_preload); |
| 127 | } |
| 128 | |
| 129 | void WebMediaPlayerClientImpl::keyAdded(const WebString& keySystem, const WebString& sessionId) |
| 130 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 131 | HTMLMediaElementEncryptedMedia::keyAdded(mediaElement(), keySystem, sessionId); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void WebMediaPlayerClientImpl::keyError(const WebString& keySystem, const WebString& sessionId, MediaKeyErrorCode errorCode, unsigned short systemCode) |
| 135 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 136 | HTMLMediaElementEncryptedMedia::keyError(mediaElement(), keySystem, sessionId, errorCode, systemCode); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void WebMediaPlayerClientImpl::keyMessage(const WebString& keySystem, const WebString& sessionId, const unsigned char* message, unsigned messageLength, const WebURL& defaultURL) |
| 140 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 141 | HTMLMediaElementEncryptedMedia::keyMessage(mediaElement(), keySystem, sessionId, message, messageLength, defaultURL); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 144 | void WebMediaPlayerClientImpl::keyNeeded(const WebString& contentType, const unsigned char* initData, unsigned initDataLength) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 145 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 146 | HTMLMediaElementEncryptedMedia::keyNeeded(mediaElement(), contentType, initData, initDataLength); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 149 | void WebMediaPlayerClientImpl::setWebLayer(blink::WebLayer* layer) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 150 | { |
Torne (Richard Coles) | a854de0 | 2013-12-18 16:25:25 +0000 | [diff] [blame] | 151 | m_client->mediaPlayerSetWebLayer(layer); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void WebMediaPlayerClientImpl::addTextTrack(WebInbandTextTrack* textTrack) |
| 155 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 156 | m_client->mediaPlayerDidAddTextTrack(textTrack); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | void WebMediaPlayerClientImpl::removeTextTrack(WebInbandTextTrack* textTrack) |
| 160 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 161 | m_client->mediaPlayerDidRemoveTextTrack(textTrack); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame] | 164 | void WebMediaPlayerClientImpl::mediaSourceOpened(WebMediaSource* webMediaSource) |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 165 | { |
| 166 | ASSERT(webMediaSource); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 167 | m_client->mediaPlayerMediaSourceOpened(webMediaSource); |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Torne (Richard Coles) | 1e20218 | 2013-10-18 15:46:42 +0100 | [diff] [blame] | 170 | void WebMediaPlayerClientImpl::requestFullscreen() |
| 171 | { |
| 172 | m_client->mediaPlayerRequestFullscreen(); |
| 173 | } |
| 174 | |
Torne (Richard Coles) | e1f1df5 | 2013-08-23 16:39:30 +0100 | [diff] [blame] | 175 | void WebMediaPlayerClientImpl::requestSeek(double time) |
| 176 | { |
| 177 | m_client->mediaPlayerRequestSeek(time); |
| 178 | } |
| 179 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 180 | // MediaPlayer ------------------------------------------------- |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 181 | void WebMediaPlayerClientImpl::load(WebMediaPlayer::LoadType loadType, const WTF::String& url, WebMediaPlayer::CORSMode corsMode) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 182 | { |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 183 | ASSERT(!m_webMediaPlayer); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 184 | |
| 185 | // FIXME: Remove this cast |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 186 | LocalFrame* frame = mediaElement().document().frame(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 187 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 188 | WebURL poster = m_client->mediaPlayerPosterURL(); |
| 189 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 190 | KURL kurl(ParsedURLString, url); |
| 191 | m_webMediaPlayer = createWebMediaPlayer(this, kurl, frame); |
| 192 | if (!m_webMediaPlayer) |
| 193 | return; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 194 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 195 | #if ENABLE(WEB_AUDIO) |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 196 | // Make sure if we create/re-create the WebMediaPlayer that we update our wrapper. |
| 197 | m_audioSourceProvider.wrap(m_webMediaPlayer->audioSourceProvider()); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 198 | #endif |
| 199 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 200 | m_webMediaPlayer->setVolume(mediaElement().playerVolume()); |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 201 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 202 | m_webMediaPlayer->setPoster(poster); |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame] | 203 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 204 | #if OS(ANDROID) |
| 205 | m_usePaintOnAndroid = (loadType != WebMediaPlayer::LoadTypeMediaStream); |
| 206 | #endif |
Torne (Richard Coles) | c0e19a6 | 2013-08-30 15:15:11 +0100 | [diff] [blame] | 207 | |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 208 | // Tell WebMediaPlayer about any connected CDM (may be null). |
| 209 | m_webMediaPlayer->setContentDecryptionModule(HTMLMediaElementEncryptedMedia::contentDecryptionModule(mediaElement())); |
| 210 | m_webMediaPlayer->load(loadType, kurl, corsMode); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 213 | void WebMediaPlayerClientImpl::play() |
| 214 | { |
| 215 | if (m_webMediaPlayer) |
| 216 | m_webMediaPlayer->play(); |
| 217 | } |
| 218 | |
| 219 | void WebMediaPlayerClientImpl::pause() |
| 220 | { |
| 221 | if (m_webMediaPlayer) |
| 222 | m_webMediaPlayer->pause(); |
| 223 | } |
| 224 | |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 225 | void WebMediaPlayerClientImpl::showFullscreenOverlay() |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 226 | { |
| 227 | if (m_webMediaPlayer) |
| 228 | m_webMediaPlayer->enterFullscreen(); |
| 229 | } |
| 230 | |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 231 | void WebMediaPlayerClientImpl::hideFullscreenOverlay() |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 232 | { |
| 233 | if (m_webMediaPlayer) |
| 234 | m_webMediaPlayer->exitFullscreen(); |
| 235 | } |
| 236 | |
Torne (Richard Coles) | 06f816c | 2013-09-26 13:25:12 +0100 | [diff] [blame] | 237 | bool WebMediaPlayerClientImpl::canShowFullscreenOverlay() const |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 238 | { |
| 239 | return m_webMediaPlayer && m_webMediaPlayer->canEnterFullscreen(); |
| 240 | } |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 241 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 242 | IntSize WebMediaPlayerClientImpl::naturalSize() const |
| 243 | { |
| 244 | if (m_webMediaPlayer) |
| 245 | return m_webMediaPlayer->naturalSize(); |
| 246 | return IntSize(); |
| 247 | } |
| 248 | |
| 249 | bool WebMediaPlayerClientImpl::hasVideo() const |
| 250 | { |
| 251 | if (m_webMediaPlayer) |
| 252 | return m_webMediaPlayer->hasVideo(); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | bool WebMediaPlayerClientImpl::hasAudio() const |
| 257 | { |
| 258 | if (m_webMediaPlayer) |
| 259 | return m_webMediaPlayer->hasAudio(); |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | double WebMediaPlayerClientImpl::duration() const |
| 264 | { |
| 265 | if (m_webMediaPlayer) |
| 266 | return m_webMediaPlayer->duration(); |
| 267 | return 0.0; |
| 268 | } |
| 269 | |
| 270 | double WebMediaPlayerClientImpl::currentTime() const |
| 271 | { |
| 272 | if (m_webMediaPlayer) |
| 273 | return m_webMediaPlayer->currentTime(); |
| 274 | return 0.0; |
| 275 | } |
| 276 | |
| 277 | void WebMediaPlayerClientImpl::seek(double time) |
| 278 | { |
| 279 | if (m_webMediaPlayer) |
| 280 | m_webMediaPlayer->seek(time); |
| 281 | } |
| 282 | |
| 283 | bool WebMediaPlayerClientImpl::seeking() const |
| 284 | { |
| 285 | if (m_webMediaPlayer) |
| 286 | return m_webMediaPlayer->seeking(); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | double WebMediaPlayerClientImpl::rate() const |
| 291 | { |
| 292 | return m_rate; |
| 293 | } |
| 294 | |
| 295 | void WebMediaPlayerClientImpl::setRate(double rate) |
| 296 | { |
| 297 | m_rate = rate; |
| 298 | if (m_webMediaPlayer) |
| 299 | m_webMediaPlayer->setRate(rate); |
| 300 | } |
| 301 | |
| 302 | bool WebMediaPlayerClientImpl::paused() const |
| 303 | { |
| 304 | if (m_webMediaPlayer) |
| 305 | return m_webMediaPlayer->paused(); |
| 306 | return false; |
| 307 | } |
| 308 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 309 | bool WebMediaPlayerClientImpl::supportsSave() const |
| 310 | { |
| 311 | if (m_webMediaPlayer) |
| 312 | return m_webMediaPlayer->supportsSave(); |
| 313 | return false; |
| 314 | } |
| 315 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 316 | void WebMediaPlayerClientImpl::setPoster(const KURL& poster) |
| 317 | { |
| 318 | if (m_webMediaPlayer) |
| 319 | m_webMediaPlayer->setPoster(WebURL(poster)); |
| 320 | } |
| 321 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 322 | MediaPlayer::NetworkState WebMediaPlayerClientImpl::networkState() const |
| 323 | { |
| 324 | if (m_webMediaPlayer) |
| 325 | return static_cast<MediaPlayer::NetworkState>(m_webMediaPlayer->networkState()); |
| 326 | return MediaPlayer::Empty; |
| 327 | } |
| 328 | |
| 329 | MediaPlayer::ReadyState WebMediaPlayerClientImpl::readyState() const |
| 330 | { |
| 331 | if (m_webMediaPlayer) |
| 332 | return static_cast<MediaPlayer::ReadyState>(m_webMediaPlayer->readyState()); |
| 333 | return MediaPlayer::HaveNothing; |
| 334 | } |
| 335 | |
| 336 | double WebMediaPlayerClientImpl::maxTimeSeekable() const |
| 337 | { |
| 338 | if (m_webMediaPlayer) |
| 339 | return m_webMediaPlayer->maxTimeSeekable(); |
| 340 | return 0.0; |
| 341 | } |
| 342 | |
| 343 | PassRefPtr<TimeRanges> WebMediaPlayerClientImpl::buffered() const |
| 344 | { |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 345 | if (m_webMediaPlayer) |
| 346 | return TimeRanges::create(m_webMediaPlayer->buffered()); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 347 | return TimeRanges::create(); |
| 348 | } |
| 349 | |
| 350 | bool WebMediaPlayerClientImpl::didLoadingProgress() const |
| 351 | { |
| 352 | return m_webMediaPlayer && m_webMediaPlayer->didLoadingProgress(); |
| 353 | } |
| 354 | |
| 355 | void WebMediaPlayerClientImpl::paint(GraphicsContext* context, const IntRect& rect) |
| 356 | { |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 357 | // Normally GraphicsContext operations do nothing when painting is disabled. |
| 358 | // Since we're accessing platformContext() directly we have to manually |
| 359 | // check. |
| 360 | if (m_webMediaPlayer && !context->paintingDisabled()) { |
| 361 | // On Android, video frame is emitted as GL_TEXTURE_EXTERNAL_OES texture. We use a different path to |
| 362 | // paint the video frame into the context. |
| 363 | #if OS(ANDROID) |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 364 | if (m_usePaintOnAndroid) { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 365 | paintOnAndroid(context, rect, context->getNormalizedAlpha()); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 366 | return; |
| 367 | } |
| 368 | #endif |
| 369 | WebCanvas* canvas = context->canvas(); |
| 370 | m_webMediaPlayer->paint(canvas, rect, context->getNormalizedAlpha()); |
| 371 | } |
| 372 | } |
| 373 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 374 | bool WebMediaPlayerClientImpl::copyVideoTextureToPlatformTexture(WebGraphicsContext3D* context, Platform3DObject texture, GLint level, GLenum type, GLenum internalFormat, bool premultiplyAlpha, bool flipY) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 375 | { |
| 376 | if (!context || !m_webMediaPlayer) |
| 377 | return false; |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 378 | if (!Extensions3DUtil::canUseCopyTextureCHROMIUM(internalFormat, type, level) || !context->makeContextCurrent()) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 379 | return false; |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 380 | |
| 381 | return m_webMediaPlayer->copyVideoTextureToPlatformTexture(context, texture, level, internalFormat, type, premultiplyAlpha, flipY); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void WebMediaPlayerClientImpl::setPreload(MediaPlayer::Preload preload) |
| 385 | { |
| 386 | m_preload = preload; |
| 387 | |
| 388 | if (m_webMediaPlayer) |
| 389 | m_webMediaPlayer->setPreload(static_cast<WebMediaPlayer::Preload>(preload)); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | bool WebMediaPlayerClientImpl::hasSingleSecurityOrigin() const |
| 393 | { |
| 394 | if (m_webMediaPlayer) |
| 395 | return m_webMediaPlayer->hasSingleSecurityOrigin(); |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | bool WebMediaPlayerClientImpl::didPassCORSAccessCheck() const |
| 400 | { |
| 401 | if (m_webMediaPlayer) |
| 402 | return m_webMediaPlayer->didPassCORSAccessCheck(); |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | double WebMediaPlayerClientImpl::mediaTimeForTimeValue(double timeValue) const |
| 407 | { |
| 408 | if (m_webMediaPlayer) |
| 409 | return m_webMediaPlayer->mediaTimeForTimeValue(timeValue); |
| 410 | return timeValue; |
| 411 | } |
| 412 | |
| 413 | unsigned WebMediaPlayerClientImpl::decodedFrameCount() const |
| 414 | { |
| 415 | if (m_webMediaPlayer) |
| 416 | return m_webMediaPlayer->decodedFrameCount(); |
| 417 | return 0; |
| 418 | } |
| 419 | |
| 420 | unsigned WebMediaPlayerClientImpl::droppedFrameCount() const |
| 421 | { |
| 422 | if (m_webMediaPlayer) |
| 423 | return m_webMediaPlayer->droppedFrameCount(); |
| 424 | return 0; |
| 425 | } |
| 426 | |
Torne (Richard Coles) | 19cde67 | 2013-11-06 12:28:04 +0000 | [diff] [blame] | 427 | unsigned WebMediaPlayerClientImpl::corruptedFrameCount() const |
| 428 | { |
| 429 | if (m_webMediaPlayer) |
| 430 | return m_webMediaPlayer->corruptedFrameCount(); |
| 431 | return 0; |
| 432 | } |
| 433 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 434 | unsigned WebMediaPlayerClientImpl::audioDecodedByteCount() const |
| 435 | { |
| 436 | if (m_webMediaPlayer) |
| 437 | return m_webMediaPlayer->audioDecodedByteCount(); |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | unsigned WebMediaPlayerClientImpl::videoDecodedByteCount() const |
| 442 | { |
| 443 | if (m_webMediaPlayer) |
| 444 | return m_webMediaPlayer->videoDecodedByteCount(); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | #if ENABLE(WEB_AUDIO) |
| 449 | AudioSourceProvider* WebMediaPlayerClientImpl::audioSourceProvider() |
| 450 | { |
| 451 | return &m_audioSourceProvider; |
| 452 | } |
| 453 | #endif |
| 454 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 455 | PassOwnPtr<MediaPlayer> WebMediaPlayerClientImpl::create(MediaPlayerClient* client) |
| 456 | { |
| 457 | return adoptPtr(new WebMediaPlayerClientImpl(client)); |
| 458 | } |
| 459 | |
| 460 | #if OS(ANDROID) |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 461 | void WebMediaPlayerClientImpl::paintOnAndroid(WebCore::GraphicsContext* context, const IntRect& rect, uint8_t alpha) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 462 | { |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 463 | OwnPtr<blink::WebGraphicsContext3DProvider> provider = adoptPtr(blink::Platform::current()->createSharedOffscreenGraphicsContext3DProvider()); |
| 464 | if (!provider) |
| 465 | return; |
| 466 | WebGraphicsContext3D* context3D = provider->context3d(); |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 467 | if (!context || !context3D || !m_webMediaPlayer || context->paintingDisabled()) |
| 468 | return; |
| 469 | |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 470 | if (!context3D->makeContextCurrent()) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 471 | return; |
| 472 | |
| 473 | // Copy video texture into a RGBA texture based bitmap first as video texture on Android is GL_TEXTURE_EXTERNAL_OES |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 474 | // which is not supported by Skia yet. The bitmap's size needs to be the same as the video and use naturalSize() here. |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 475 | // Check if we could reuse existing texture based bitmap. |
| 476 | // Otherwise, release existing texture based bitmap and allocate a new one based on video size. |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 477 | if (!ensureTextureBackedSkBitmap(provider->grContext(), m_bitmap, naturalSize(), kTopLeft_GrSurfaceOrigin, kSkia8888_GrPixelConfig)) |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 478 | return; |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 479 | |
| 480 | // Copy video texture to bitmap texture. |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 481 | WebCanvas* canvas = context->canvas(); |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 482 | unsigned textureId = static_cast<unsigned>((m_bitmap.getTexture())->getTextureHandle()); |
Torne (Richard Coles) | 0938029 | 2014-02-21 12:17:33 +0000 | [diff] [blame] | 483 | if (!m_webMediaPlayer->copyVideoTextureToPlatformTexture(context3D, textureId, 0, GL_RGBA, GL_UNSIGNED_BYTE, true, false)) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 484 | return; |
| 485 | |
| 486 | // Draw the texture based bitmap onto the Canvas. If the canvas is hardware based, this will do a GPU-GPU texture copy. If the canvas is software based, |
| 487 | // the texture based bitmap will be readbacked to system memory then draw onto the canvas. |
| 488 | SkRect dest; |
| 489 | dest.set(rect.x(), rect.y(), rect.x() + rect.width(), rect.y() + rect.height()); |
| 490 | SkPaint paint; |
| 491 | paint.setAlpha(alpha); |
| 492 | // It is not necessary to pass the dest into the drawBitmap call since all the context have been set up before calling paintCurrentFrameInContext. |
| 493 | canvas->drawBitmapRect(m_bitmap, NULL, dest, &paint); |
| 494 | } |
| 495 | #endif |
| 496 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 497 | WebMediaPlayerClientImpl::WebMediaPlayerClientImpl(MediaPlayerClient* client) |
| 498 | : m_client(client) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 499 | , m_preload(MediaPlayer::Auto) |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 500 | , m_rate(1.0) |
Ben Murdoch | 07a852d | 2014-03-31 11:51:52 +0100 | [diff] [blame] | 501 | #if OS(ANDROID) |
| 502 | , m_usePaintOnAndroid(false) |
| 503 | #endif |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 504 | { |
| 505 | ASSERT(m_client); |
| 506 | } |
| 507 | |
Torne (Richard Coles) | d5428f3 | 2014-03-18 10:21:16 +0000 | [diff] [blame] | 508 | WebCore::HTMLMediaElement& WebMediaPlayerClientImpl::mediaElement() const |
| 509 | { |
| 510 | return *static_cast<HTMLMediaElement*>(m_client); |
| 511 | } |
| 512 | |
Ben Murdoch | e69819b | 2013-07-17 14:56:49 +0100 | [diff] [blame] | 513 | #if ENABLE(WEB_AUDIO) |
| 514 | void WebMediaPlayerClientImpl::AudioSourceProviderImpl::wrap(WebAudioSourceProvider* provider) |
| 515 | { |
| 516 | MutexLocker locker(provideInputLock); |
| 517 | |
| 518 | if (m_webAudioSourceProvider && provider != m_webAudioSourceProvider) |
| 519 | m_webAudioSourceProvider->setClient(0); |
| 520 | |
| 521 | m_webAudioSourceProvider = provider; |
| 522 | if (m_webAudioSourceProvider) |
| 523 | m_webAudioSourceProvider->setClient(m_client.get()); |
| 524 | } |
| 525 | |
| 526 | void WebMediaPlayerClientImpl::AudioSourceProviderImpl::setClient(AudioSourceProviderClient* client) |
| 527 | { |
| 528 | MutexLocker locker(provideInputLock); |
| 529 | |
| 530 | if (client) |
| 531 | m_client = adoptPtr(new WebMediaPlayerClientImpl::AudioClientImpl(client)); |
| 532 | else |
| 533 | m_client.clear(); |
| 534 | |
| 535 | if (m_webAudioSourceProvider) |
| 536 | m_webAudioSourceProvider->setClient(m_client.get()); |
| 537 | } |
| 538 | |
| 539 | void WebMediaPlayerClientImpl::AudioSourceProviderImpl::provideInput(AudioBus* bus, size_t framesToProcess) |
| 540 | { |
| 541 | ASSERT(bus); |
| 542 | if (!bus) |
| 543 | return; |
| 544 | |
| 545 | MutexTryLocker tryLocker(provideInputLock); |
| 546 | if (!tryLocker.locked() || !m_webAudioSourceProvider || !m_client.get()) { |
| 547 | bus->zero(); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | // Wrap the AudioBus channel data using WebVector. |
| 552 | size_t n = bus->numberOfChannels(); |
| 553 | WebVector<float*> webAudioData(n); |
| 554 | for (size_t i = 0; i < n; ++i) |
| 555 | webAudioData[i] = bus->channel(i)->mutableData(); |
| 556 | |
| 557 | m_webAudioSourceProvider->provideInput(webAudioData, framesToProcess); |
| 558 | } |
| 559 | |
| 560 | void WebMediaPlayerClientImpl::AudioClientImpl::setFormat(size_t numberOfChannels, float sampleRate) |
| 561 | { |
| 562 | if (m_client) |
| 563 | m_client->setFormat(numberOfChannels, sampleRate); |
| 564 | } |
| 565 | |
| 566 | #endif |
| 567 | |
Torne (Richard Coles) | 51b2906 | 2013-11-28 11:56:03 +0000 | [diff] [blame] | 568 | } // namespace blink |