| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/webmediaplayer_ms.h" | 5 #include "content/renderer/media/webmediaplayer_ms.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "gpu/blink/webgraphicscontext3d_impl.h" | 25 #include "gpu/blink/webgraphicscontext3d_impl.h" |
| 26 #include "media/base/media_log.h" | 26 #include "media/base/media_log.h" |
| 27 #include "media/base/video_frame.h" | 27 #include "media/base/video_frame.h" |
| 28 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" | 28 #include "third_party/WebKit/public/platform/WebMediaPlayerClient.h" |
| 29 #include "third_party/WebKit/public/platform/WebRect.h" | 29 #include "third_party/WebKit/public/platform/WebRect.h" |
| 30 #include "third_party/WebKit/public/platform/WebSize.h" | 30 #include "third_party/WebKit/public/platform/WebSize.h" |
| 31 #include "third_party/WebKit/public/platform/WebURL.h" | 31 #include "third_party/WebKit/public/platform/WebURL.h" |
| 32 | 32 |
| 33 namespace content { | 33 namespace content { |
| 34 | 34 |
| 35 namespace { | |
| 36 | |
| 37 static const float DEFAULT_AUDIO_VOLUME = 1.0f; | |
| 38 | |
| 39 } // anonymous namespace | |
| 40 | |
| 41 WebMediaPlayerMS::WebMediaPlayerMS( | 35 WebMediaPlayerMS::WebMediaPlayerMS( |
| 42 blink::WebFrame* frame, | 36 blink::WebFrame* frame, |
| 43 blink::WebMediaPlayerClient* client, | 37 blink::WebMediaPlayerClient* client, |
| 44 base::WeakPtr<media::WebMediaPlayerDelegate> delegate, | 38 base::WeakPtr<media::WebMediaPlayerDelegate> delegate, |
| 45 media::MediaLog* media_log, | 39 media::MediaLog* media_log, |
| 46 scoped_ptr<MediaStreamRendererFactory> factory, | 40 scoped_ptr<MediaStreamRendererFactory> factory, |
| 47 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner, | 41 const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner, |
| 48 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, | 42 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| 49 const scoped_refptr<base::TaskRunner>& worker_task_runner, | 43 const scoped_refptr<base::TaskRunner>& worker_task_runner, |
| 50 media::GpuVideoAcceleratorFactories* gpu_factories, | 44 media::GpuVideoAcceleratorFactories* gpu_factories, |
| 51 const blink::WebString& sink_id, | 45 const blink::WebString& sink_id, |
| 52 const blink::WebSecurityOrigin& security_origin) | 46 const blink::WebSecurityOrigin& security_origin) |
| 53 : RenderFrameObserver(RenderFrame::FromWebFrame(frame)), | 47 : frame_(frame), |
| 54 frame_(frame), | |
| 55 network_state_(WebMediaPlayer::NetworkStateEmpty), | 48 network_state_(WebMediaPlayer::NetworkStateEmpty), |
| 56 ready_state_(WebMediaPlayer::ReadyStateHaveNothing), | 49 ready_state_(WebMediaPlayer::ReadyStateHaveNothing), |
| 57 buffered_(static_cast<size_t>(0)), | 50 buffered_(static_cast<size_t>(0)), |
| 58 client_(client), | 51 client_(client), |
| 59 delegate_(delegate), | 52 delegate_(delegate), |
| 60 paused_(true), | 53 paused_(true), |
| 61 render_frame_suspended_(false), | 54 render_frame_suspended_(false), |
| 62 received_first_frame_(false), | 55 received_first_frame_(false), |
| 63 media_log_(media_log), | 56 media_log_(media_log), |
| 64 renderer_factory_(std::move(factory)), | 57 renderer_factory_(std::move(factory)), |
| 65 media_task_runner_(media_task_runner), | 58 media_task_runner_(media_task_runner), |
| 66 worker_task_runner_(worker_task_runner), | 59 worker_task_runner_(worker_task_runner), |
| 67 gpu_factories_(gpu_factories), | 60 gpu_factories_(gpu_factories), |
| 68 compositor_task_runner_(compositor_task_runner), | 61 compositor_task_runner_(compositor_task_runner), |
| 69 initial_audio_output_device_id_(sink_id.utf8()), | 62 initial_audio_output_device_id_(sink_id.utf8()), |
| 70 initial_security_origin_(security_origin.isNull() | 63 initial_security_origin_(security_origin.isNull() |
| 71 ? url::Origin() | 64 ? url::Origin() |
| 72 : url::Origin(security_origin)) { | 65 : url::Origin(security_origin)), |
| 66 volume_(1.0), |
| 67 volume_multiplier_(1.0) { |
| 73 DVLOG(1) << __FUNCTION__; | 68 DVLOG(1) << __FUNCTION__; |
| 74 DCHECK(client); | 69 DCHECK(client); |
| 70 if (delegate) |
| 71 delegate->AddObserver(this); |
| 72 |
| 75 media_log_->AddEvent( | 73 media_log_->AddEvent( |
| 76 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED)); | 74 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_CREATED)); |
| 77 } | 75 } |
| 78 | 76 |
| 79 WebMediaPlayerMS::~WebMediaPlayerMS() { | 77 WebMediaPlayerMS::~WebMediaPlayerMS() { |
| 80 DVLOG(1) << __FUNCTION__; | 78 DVLOG(1) << __FUNCTION__; |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | 79 DCHECK(thread_checker_.CalledOnValidThread()); |
| 82 | 80 |
| 83 if (compositor_ && !compositor_task_runner_->BelongsToCurrentThread()) | 81 if (compositor_ && !compositor_task_runner_->BelongsToCurrentThread()) |
| 84 compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_.release()); | 82 compositor_task_runner_->DeleteSoon(FROM_HERE, compositor_.release()); |
| 85 | 83 |
| 86 get_client()->setWebLayer(nullptr); | 84 get_client()->setWebLayer(nullptr); |
| 87 | 85 |
| 88 if (video_frame_provider_) | 86 if (video_frame_provider_) |
| 89 video_frame_provider_->Stop(); | 87 video_frame_provider_->Stop(); |
| 90 | 88 |
| 91 if (audio_renderer_) | 89 if (audio_renderer_) |
| 92 audio_renderer_->Stop(); | 90 audio_renderer_->Stop(); |
| 93 | 91 |
| 94 media_log_->AddEvent( | 92 media_log_->AddEvent( |
| 95 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED)); | 93 media_log_->CreateEvent(media::MediaLogEvent::WEBMEDIAPLAYER_DESTROYED)); |
| 96 | 94 |
| 97 if (delegate_.get()) | 95 if (delegate_) { |
| 98 delegate_->PlayerGone(this); | 96 delegate_->PlayerGone(this); |
| 97 delegate_->RemoveObserver(this); |
| 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 void WebMediaPlayerMS::load(LoadType load_type, | 101 void WebMediaPlayerMS::load(LoadType load_type, |
| 102 const blink::WebURL& url, | 102 const blink::WebURL& url, |
| 103 CORSMode /*cors_mode*/) { | 103 CORSMode /*cors_mode*/) { |
| 104 DVLOG(1) << __FUNCTION__; | 104 DVLOG(1) << __FUNCTION__; |
| 105 DCHECK(thread_checker_.CalledOnValidThread()); | 105 DCHECK(thread_checker_.CalledOnValidThread()); |
| 106 | 106 |
| 107 // TODO(acolwell): Change this to DCHECK_EQ(load_type, LoadTypeMediaStream) | 107 // TODO(acolwell): Change this to DCHECK_EQ(load_type, LoadTypeMediaStream) |
| 108 // once Blink-side changes land. | 108 // once Blink-side changes land. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 130 url, frame->GetRoutingID(), initial_audio_output_device_id_, | 130 url, frame->GetRoutingID(), initial_audio_output_device_id_, |
| 131 initial_security_origin_); | 131 initial_security_origin_); |
| 132 } | 132 } |
| 133 | 133 |
| 134 if (!video_frame_provider_ && !audio_renderer_) { | 134 if (!video_frame_provider_ && !audio_renderer_) { |
| 135 SetNetworkState(WebMediaPlayer::NetworkStateNetworkError); | 135 SetNetworkState(WebMediaPlayer::NetworkStateNetworkError); |
| 136 return; | 136 return; |
| 137 } | 137 } |
| 138 | 138 |
| 139 if (audio_renderer_) { | 139 if (audio_renderer_) { |
| 140 audio_renderer_->SetVolume(DEFAULT_AUDIO_VOLUME); | 140 audio_renderer_->SetVolume(volume_); |
| 141 audio_renderer_->Start(); | 141 audio_renderer_->Start(); |
| 142 } | 142 } |
| 143 if (video_frame_provider_) | 143 if (video_frame_provider_) |
| 144 video_frame_provider_->Start(); | 144 video_frame_provider_->Start(); |
| 145 if (audio_renderer_ && !video_frame_provider_) { | 145 if (audio_renderer_ && !video_frame_provider_) { |
| 146 // This is audio-only mode. | 146 // This is audio-only mode. |
| 147 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata); | 147 SetReadyState(WebMediaPlayer::ReadyStateHaveMetadata); |
| 148 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData); | 148 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData); |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 | 151 |
| 152 void WebMediaPlayerMS::play() { | 152 void WebMediaPlayerMS::play() { |
| 153 DVLOG(1) << __FUNCTION__; | 153 DVLOG(1) << __FUNCTION__; |
| 154 DCHECK(thread_checker_.CalledOnValidThread()); | 154 DCHECK(thread_checker_.CalledOnValidThread()); |
| 155 if (paused_) { | 155 if (paused_) { |
| 156 if (video_frame_provider_.get()) | 156 if (video_frame_provider_.get()) |
| 157 video_frame_provider_->Play(); | 157 video_frame_provider_->Play(); |
| 158 | 158 |
| 159 compositor_->StartRendering(); | 159 compositor_->StartRendering(); |
| 160 | 160 |
| 161 if (audio_renderer_.get()) | 161 if (audio_renderer_.get()) |
| 162 audio_renderer_->Play(); | 162 audio_renderer_->Play(); |
| 163 | 163 |
| 164 if (delegate_.get()) | 164 if (delegate_.get()) { |
| 165 delegate_->DidPlay(this); | 165 delegate_->DidPlay(this, hasVideo(), hasAudio(), false, |
| 166 media::kInfiniteDuration()); |
| 167 } |
| 166 } | 168 } |
| 167 | 169 |
| 168 paused_ = false; | 170 paused_ = false; |
| 169 | 171 |
| 170 media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PLAY)); | 172 media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PLAY)); |
| 171 } | 173 } |
| 172 | 174 |
| 173 void WebMediaPlayerMS::pause() { | 175 void WebMediaPlayerMS::pause() { |
| 174 DVLOG(1) << __FUNCTION__; | 176 DVLOG(1) << __FUNCTION__; |
| 175 DCHECK(thread_checker_.CalledOnValidThread()); | 177 DCHECK(thread_checker_.CalledOnValidThread()); |
| 176 | 178 |
| 177 if (video_frame_provider_.get()) | 179 if (video_frame_provider_.get()) |
| 178 video_frame_provider_->Pause(); | 180 video_frame_provider_->Pause(); |
| 179 | 181 |
| 180 compositor_->StopRendering(); | 182 compositor_->StopRendering(); |
| 181 compositor_->ReplaceCurrentFrameWithACopy(); | 183 compositor_->ReplaceCurrentFrameWithACopy(); |
| 182 | 184 |
| 183 if (!paused_) { | 185 if (!paused_) { |
| 184 if (audio_renderer_.get()) | 186 if (audio_renderer_.get()) |
| 185 audio_renderer_->Pause(); | 187 audio_renderer_->Pause(); |
| 186 | 188 |
| 187 if (delegate_.get()) | 189 if (delegate_.get()) |
| 188 delegate_->DidPause(this); | 190 delegate_->DidPause(this, false); |
| 189 } | 191 } |
| 190 | 192 |
| 191 paused_ = true; | 193 paused_ = true; |
| 192 | 194 |
| 193 media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PAUSE)); | 195 media_log_->AddEvent(media_log_->CreateEvent(media::MediaLogEvent::PAUSE)); |
| 194 } | 196 } |
| 195 | 197 |
| 196 bool WebMediaPlayerMS::supportsSave() const { | 198 bool WebMediaPlayerMS::supportsSave() const { |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); | 199 DCHECK(thread_checker_.CalledOnValidThread()); |
| 198 return false; | 200 return false; |
| 199 } | 201 } |
| 200 | 202 |
| 201 void WebMediaPlayerMS::seek(double seconds) { | 203 void WebMediaPlayerMS::seek(double seconds) { |
| 202 DCHECK(thread_checker_.CalledOnValidThread()); | 204 DCHECK(thread_checker_.CalledOnValidThread()); |
| 203 } | 205 } |
| 204 | 206 |
| 205 void WebMediaPlayerMS::setRate(double rate) { | 207 void WebMediaPlayerMS::setRate(double rate) { |
| 206 DCHECK(thread_checker_.CalledOnValidThread()); | 208 DCHECK(thread_checker_.CalledOnValidThread()); |
| 207 } | 209 } |
| 208 | 210 |
| 209 void WebMediaPlayerMS::setVolume(double volume) { | 211 void WebMediaPlayerMS::setVolume(double volume) { |
| 210 DVLOG(1) << __FUNCTION__ << "(volume=" << volume << ")"; | 212 DVLOG(1) << __FUNCTION__ << "(volume=" << volume << ")"; |
| 211 DCHECK(thread_checker_.CalledOnValidThread()); | 213 DCHECK(thread_checker_.CalledOnValidThread()); |
| 214 volume_ = volume; |
| 212 if (audio_renderer_.get()) | 215 if (audio_renderer_.get()) |
| 213 audio_renderer_->SetVolume(volume); | 216 audio_renderer_->SetVolume(volume_ * volume_multiplier_); |
| 214 } | 217 } |
| 215 | 218 |
| 216 void WebMediaPlayerMS::setSinkId( | 219 void WebMediaPlayerMS::setSinkId( |
| 217 const blink::WebString& sink_id, | 220 const blink::WebString& sink_id, |
| 218 const blink::WebSecurityOrigin& security_origin, | 221 const blink::WebSecurityOrigin& security_origin, |
| 219 blink::WebSetSinkIdCallbacks* web_callback) { | 222 blink::WebSetSinkIdCallbacks* web_callback) { |
| 220 DVLOG(1) << __FUNCTION__; | 223 DVLOG(1) << __FUNCTION__; |
| 221 DCHECK(thread_checker_.CalledOnValidThread()); | 224 DCHECK(thread_checker_.CalledOnValidThread()); |
| 222 const media::SwitchOutputDeviceCB callback = | 225 const media::SwitchOutputDeviceCB callback = |
| 223 media::ConvertToSwitchOutputDeviceCB(web_callback); | 226 media::ConvertToSwitchOutputDeviceCB(web_callback); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 NOTIMPLEMENTED(); | 356 NOTIMPLEMENTED(); |
| 354 return 0; | 357 return 0; |
| 355 } | 358 } |
| 356 | 359 |
| 357 unsigned WebMediaPlayerMS::videoDecodedByteCount() const { | 360 unsigned WebMediaPlayerMS::videoDecodedByteCount() const { |
| 358 DCHECK(thread_checker_.CalledOnValidThread()); | 361 DCHECK(thread_checker_.CalledOnValidThread()); |
| 359 NOTIMPLEMENTED(); | 362 NOTIMPLEMENTED(); |
| 360 return 0; | 363 return 0; |
| 361 } | 364 } |
| 362 | 365 |
| 363 void WebMediaPlayerMS::WasHidden() { | 366 void WebMediaPlayerMS::OnHidden() { |
| 364 #if defined(OS_ANDROID) | 367 #if defined(OS_ANDROID) |
| 365 DCHECK(thread_checker_.CalledOnValidThread()); | 368 DCHECK(thread_checker_.CalledOnValidThread()); |
| 366 DCHECK(!render_frame_suspended_); | 369 DCHECK(!render_frame_suspended_); |
| 367 | 370 |
| 368 // Method called when the RenderFrame is sent to background and suspended | 371 // Method called when the RenderFrame is sent to background and suspended |
| 369 // (android). Substitute the displayed VideoFrame with a copy to avoid | 372 // (android). Substitute the displayed VideoFrame with a copy to avoid |
| 370 // holding on to it unnecessarily. | 373 // holding on to it unnecessarily. |
| 371 render_frame_suspended_=true; | 374 render_frame_suspended_ = true; |
| 372 if (!paused_) | 375 if (!paused_) |
| 373 compositor_->ReplaceCurrentFrameWithACopy(); | 376 compositor_->ReplaceCurrentFrameWithACopy(); |
| 374 #endif // defined(OS_ANDROID) | 377 #endif // defined(OS_ANDROID) |
| 375 } | 378 } |
| 376 | 379 |
| 377 void WebMediaPlayerMS::WasShown() { | 380 void WebMediaPlayerMS::OnShown() { |
| 378 #if defined(OS_ANDROID) | 381 #if defined(OS_ANDROID) |
| 379 DCHECK(thread_checker_.CalledOnValidThread()); | 382 DCHECK(thread_checker_.CalledOnValidThread()); |
| 380 | 383 |
| 381 render_frame_suspended_ = false; | 384 render_frame_suspended_ = false; |
| 382 #endif // defined(OS_ANDROID) | 385 #endif // defined(OS_ANDROID) |
| 383 } | 386 } |
| 384 | 387 |
| 388 void WebMediaPlayerMS::OnPlay() { |
| 389 play(); |
| 390 client_->playbackStateChanged(); |
| 391 } |
| 392 |
| 393 void WebMediaPlayerMS::OnPause() { |
| 394 pause(); |
| 395 client_->playbackStateChanged(); |
| 396 } |
| 397 |
| 398 void WebMediaPlayerMS::OnVolumeMultiplierUpdate(double multiplier) { |
| 399 volume_multiplier_ = multiplier; |
| 400 setVolume(volume_); |
| 401 } |
| 402 |
| 385 bool WebMediaPlayerMS::copyVideoTextureToPlatformTexture( | 403 bool WebMediaPlayerMS::copyVideoTextureToPlatformTexture( |
| 386 blink::WebGraphicsContext3D* web_graphics_context, | 404 blink::WebGraphicsContext3D* web_graphics_context, |
| 387 unsigned int texture, | 405 unsigned int texture, |
| 388 unsigned int internal_format, | 406 unsigned int internal_format, |
| 389 unsigned int type, | 407 unsigned int type, |
| 390 bool premultiply_alpha, | 408 bool premultiply_alpha, |
| 391 bool flip_y) { | 409 bool flip_y) { |
| 392 TRACE_EVENT0("media", "WebMediaPlayerMS:copyVideoTextureToPlatformTexture"); | 410 TRACE_EVENT0("media", "WebMediaPlayerMS:copyVideoTextureToPlatformTexture"); |
| 393 DCHECK(thread_checker_.CalledOnValidThread()); | 411 DCHECK(thread_checker_.CalledOnValidThread()); |
| 394 | 412 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 media::SkCanvasVideoRenderer* WebMediaPlayerMS::GetSkCanvasVideoRenderer() { | 500 media::SkCanvasVideoRenderer* WebMediaPlayerMS::GetSkCanvasVideoRenderer() { |
| 483 return &video_renderer_; | 501 return &video_renderer_; |
| 484 } | 502 } |
| 485 | 503 |
| 486 void WebMediaPlayerMS::ResetCanvasCache() { | 504 void WebMediaPlayerMS::ResetCanvasCache() { |
| 487 DCHECK(thread_checker_.CalledOnValidThread()); | 505 DCHECK(thread_checker_.CalledOnValidThread()); |
| 488 video_renderer_.ResetCache(); | 506 video_renderer_.ResetCache(); |
| 489 } | 507 } |
| 490 | 508 |
| 491 } // namespace content | 509 } // namespace content |
| OLD | NEW |