Chromium Code Reviews| Index: media/blink/webmediaplayer_impl.cc |
| diff --git a/media/blink/webmediaplayer_impl.cc b/media/blink/webmediaplayer_impl.cc |
| index a73d688ffb666568451ea1889d549ee59786de9f..c44ada4b28883a4085ef020e6e0df6d22797948b 100644 |
| --- a/media/blink/webmediaplayer_impl.cc |
| +++ b/media/blink/webmediaplayer_impl.cc |
| @@ -200,6 +200,8 @@ WebMediaPlayerImpl::WebMediaPlayerImpl( |
| base::Unretained(this)), |
| base::Bind(&WebMediaPlayerImpl::OnPipelineSeeked, AsWeakPtr()), |
| base::Bind(&WebMediaPlayerImpl::OnPipelineSuspended, AsWeakPtr()), |
| + base::Bind(&WebMediaPlayerImpl::OnBeforePipelineResume, AsWeakPtr()), |
| + base::Bind(&WebMediaPlayerImpl::OnPipelineResumed, AsWeakPtr()), |
| base::Bind(&WebMediaPlayerImpl::OnError, AsWeakPtr())), |
| load_type_(LoadTypeURL), |
| opaque_(false), |
| @@ -474,6 +476,7 @@ void WebMediaPlayerImpl::pause() { |
| DCHECK(watch_time_reporter_); |
| watch_time_reporter_->OnPaused(); |
| media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PAUSE)); |
| + |
| UpdatePlayState(); |
| } |
| @@ -671,9 +674,7 @@ void WebMediaPlayerImpl::selectedVideoTrackChanged( |
| std::ostringstream logstr; |
| std::vector<MediaTrack::Id> selectedVideoMediaTrackId; |
| - bool canAddVideoTrack = |
| - !IsBackgroundVideoTrackOptimizationEnabled() || !IsHidden(); |
| - if (selectedTrackId && canAddVideoTrack) { |
| + if (selectedTrackId && !video_track_disabled_) { |
| selectedVideoMediaTrackId.push_back(selectedTrackId->utf8().data()); |
| logstr << selectedVideoMediaTrackId[0]; |
| } |
| @@ -1064,6 +1065,7 @@ void WebMediaPlayerImpl::OnCdmAttached(bool success) { |
| void WebMediaPlayerImpl::OnPipelineSeeked(bool time_updated) { |
| seeking_ = false; |
| seek_time_ = base::TimeDelta(); |
| + |
| if (paused_) { |
| #if defined(OS_ANDROID) // WMPI_CAST |
| if (isRemote()) { |
| @@ -1084,6 +1086,10 @@ void WebMediaPlayerImpl::OnPipelineSeeked(bool time_updated) { |
| // Reset underflow count upon seek; this prevents looping videos and user |
| // actions from artificially inflating the underflow count. |
| underflow_count_ = 0; |
| + |
| + // Apply optimizations for the hidden videos if hidden. |
| + if (IsHidden()) |
| + OnHidden(); |
|
sandersd (OOO until July 31)
2017/01/10 21:34:37
OnHidden() does a bit more than what is requested,
whywhat
2017/01/11 19:41:30
I think that's the point of it, no? We initialize
sandersd (OOO until July 31)
2017/01/11 21:54:51
The target state for this code is that OnHidden()
|
| } |
| void WebMediaPlayerImpl::OnPipelineSuspended() { |
| @@ -1111,6 +1117,26 @@ void WebMediaPlayerImpl::OnPipelineSuspended() { |
| } |
| } |
| +void WebMediaPlayerImpl::OnBeforePipelineResume() { |
| + // Enable video track if we disabled it in the background - this way the new |
| + // renderer will attach its callbacks to the video stream properly. |
| + // TODO(avayvod): Remove this when disabling and enabling video tracks in |
| + // non-playing state works correctly. See https://crbug.com/678374. |
| + EnableVideoTrackIfNeeded(); |
| + is_pipeline_resuming_ = true; |
| +} |
| + |
| +void WebMediaPlayerImpl::OnPipelineResumed() { |
| + is_pipeline_resuming_ = false; |
| + |
| + if (IsHidden()) { |
| + DisableVideoTrackIfNeeded(); |
| + return; |
|
sandersd (OOO until July 31)
2017/01/10 21:34:37
Nit: This may be a case where early return is more
whywhat
2017/01/11 19:41:30
Done. For some reason, I expect lint to complain t
|
| + } |
| + |
| + EnableVideoTrackIfNeeded(); |
| +} |
| + |
| void WebMediaPlayerImpl::OnDemuxerOpened() { |
| DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| client_->mediaSourceOpened( |
| @@ -1353,18 +1379,16 @@ void WebMediaPlayerImpl::OnHidden() { |
| if (watch_time_reporter_) |
| watch_time_reporter_->OnHidden(); |
| - if (!IsStreaming() && IsBackgroundVideoTrackOptimizationEnabled()) { |
| - if (ShouldPauseWhenHidden()) { |
| - // OnPause() will set |paused_when_hidden_| to false and call |
| - // UpdatePlayState(), so set the flag to true after and then return. |
| - OnPause(); |
| - paused_when_hidden_ = true; |
| - return; |
| - } |
| - |
| - selectedVideoTrackChanged(nullptr); |
| + if (!paused_when_hidden_ && ShouldPauseWhenHidden()) { |
|
sandersd (OOO until July 31)
2017/01/10 21:34:37
As a matter of style, I prefer idempotency whereve
whywhat
2017/01/11 19:41:30
I think it won't because ShouldPauseWhenHidden and
|
| + // OnPause() will set |paused_when_hidden_| to false and call |
| + // UpdatePlayState(), so set the flag to true after and then return. |
| + OnPause(); |
| + paused_when_hidden_ = true; |
| + return; |
| } |
| + DisableVideoTrackIfNeeded(); |
| + |
| UpdatePlayState(); |
| // Schedule suspended playing media to be paused if the user doesn't come back |
| @@ -1382,19 +1406,14 @@ void WebMediaPlayerImpl::OnShown() { |
| base::Bind(&VideoFrameCompositor::SetForegroundTime, |
| base::Unretained(compositor_), base::TimeTicks::Now())); |
| - if (!IsStreaming() && IsBackgroundVideoTrackOptimizationEnabled()) { |
| - if (paused_when_hidden_) { |
| - paused_when_hidden_ = false; |
| - OnPlay(); // Calls UpdatePlayState() so return afterwards. |
| - return; |
| - } |
| - |
| - if (client_->hasSelectedVideoTrack()) { |
| - WebMediaPlayer::TrackId trackId = client_->getSelectedVideoTrackId(); |
| - selectedVideoTrackChanged(&trackId); |
| - } |
| + if (paused_when_hidden_) { |
|
sandersd (OOO until July 31)
2017/01/10 21:34:37
By the same reasoning, this should go right before
whywhat
2017/01/11 19:41:30
Moved the state update above the new logic.
I don
|
| + paused_when_hidden_ = false; |
| + OnPlay(); // Calls UpdatePlayState() so return afterwards. |
| + return; |
| } |
| + EnableVideoTrackIfNeeded(); |
| + |
| must_suspend_ = false; |
| background_pause_timer_.Stop(); |
| @@ -2080,12 +2099,53 @@ void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { |
| } |
| bool WebMediaPlayerImpl::ShouldPauseWhenHidden() const { |
| + DCHECK(IsHidden()); |
| +// Don't pause videos being Cast (Android only) or if the background video |
| +// optimizations are off (desktop only). |
| #if defined(OS_ANDROID) // WMPI_CAST |
| if (isRemote()) |
| return false; |
| -#endif // defined(OS_ANDROID) // WMPI_CAST |
| +#else // defined(OS_ANDROID) |
| + if (!IsBackgroundVideoTrackOptimizationEnabled()) |
| + return false; |
| +#endif // defined(OS_ANDROID) |
| return hasVideo() && !hasAudio(); |
|
sandersd (OOO until July 31)
2017/01/10 21:34:37
Probably worth a comment somewhere: these conditio
whywhat
2017/01/11 19:41:30
Should I make an even stronger comment that they w
sandersd (OOO until July 31)
2017/01/11 21:54:51
OnMetadata is called after demuxer initialization,
|
| } |
| +bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { |
| + DCHECK(IsHidden()); |
| + return IsBackgroundVideoTrackOptimizationEnabled() && hasVideo() && |
| + hasAudio() && !IsStreaming(); |
| +} |
| + |
| +void WebMediaPlayerImpl::EnableVideoTrackIfNeeded() { |
| + DCHECK(!IsHidden()); |
| + |
| + // Don't change video track while the pipeline is resuming or seeking. |
| + if (is_pipeline_resuming_ || seeking_) |
| + return; |
| + |
| + if (video_track_disabled_) { |
| + video_track_disabled_ = false; |
| + if (client_->hasSelectedVideoTrack()) { |
| + WebMediaPlayer::TrackId trackId = client_->getSelectedVideoTrackId(); |
| + selectedVideoTrackChanged(&trackId); |
| + } |
| + } |
| +} |
| + |
| +void WebMediaPlayerImpl::DisableVideoTrackIfNeeded() { |
| + DCHECK(IsHidden()); |
| + |
| + // Don't change video track while the pipeline is resuming or seeking. |
| + if (is_pipeline_resuming_ || seeking_) |
| + return; |
| + |
| + if (!video_track_disabled_ && ShouldDisableVideoWhenHidden()) { |
| + video_track_disabled_ = true; |
| + selectedVideoTrackChanged(nullptr); |
| + } |
| +} |
| + |
| } // namespace media |