Commit eb9098d5 authored by avayvod's avatar avayvod Committed by Commit bot

[Video] Pause videos with no audio when in the background.

Pause video-only players when they go to background and continue playback when they go into foreground and hasn't been paused explicitly in the mean time.
Behind the experiment for optimizing background video playback.

BUG=671381
TEST=Manual

Review-Url: https://codereview.chromium.org/2567833002
Cr-Commit-Position: refs/heads/master@{#442110}
parent 1e3963fb
...@@ -205,6 +205,7 @@ WebMediaPlayerImpl::WebMediaPlayerImpl( ...@@ -205,6 +205,7 @@ WebMediaPlayerImpl::WebMediaPlayerImpl(
opaque_(false), opaque_(false),
playback_rate_(0.0), playback_rate_(0.0),
paused_(true), paused_(true),
paused_when_hidden_(false),
seeking_(false), seeking_(false),
pending_suspend_resume_cycle_(false), pending_suspend_resume_cycle_(false),
ended_(false), ended_(false),
...@@ -448,6 +449,9 @@ void WebMediaPlayerImpl::pause() { ...@@ -448,6 +449,9 @@ void WebMediaPlayerImpl::pause() {
// paused state. // paused state.
paused_ = true; paused_ = true;
// No longer paused because it was hidden.
paused_when_hidden_ = false;
#if defined(OS_ANDROID) // WMPI_CAST #if defined(OS_ANDROID) // WMPI_CAST
if (isRemote()) { if (isRemote()) {
cast_impl_.pause(); cast_impl_.pause();
...@@ -1342,12 +1346,21 @@ void WebMediaPlayerImpl::OnVideoOpacityChange(bool opaque) { ...@@ -1342,12 +1346,21 @@ void WebMediaPlayerImpl::OnVideoOpacityChange(bool opaque) {
void WebMediaPlayerImpl::OnHidden() { void WebMediaPlayerImpl::OnHidden() {
DCHECK(main_task_runner_->BelongsToCurrentThread()); DCHECK(main_task_runner_->BelongsToCurrentThread());
if (IsBackgroundVideoTrackOptimizationEnabled())
selectedVideoTrackChanged(nullptr);
if (watch_time_reporter_) if (watch_time_reporter_)
watch_time_reporter_->OnHidden(); watch_time_reporter_->OnHidden();
if (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);
}
UpdatePlayState(); UpdatePlayState();
// Schedule suspended playing media to be paused if the user doesn't come back // Schedule suspended playing media to be paused if the user doesn't come back
...@@ -1365,10 +1378,17 @@ void WebMediaPlayerImpl::OnShown() { ...@@ -1365,10 +1378,17 @@ void WebMediaPlayerImpl::OnShown() {
base::Bind(&VideoFrameCompositor::SetForegroundTime, base::Bind(&VideoFrameCompositor::SetForegroundTime,
base::Unretained(compositor_), base::TimeTicks::Now())); base::Unretained(compositor_), base::TimeTicks::Now()));
if (IsBackgroundVideoTrackOptimizationEnabled() && if (IsBackgroundVideoTrackOptimizationEnabled()) {
client_->hasSelectedVideoTrack()) { if (paused_when_hidden_) {
WebMediaPlayer::TrackId trackId = client_->getSelectedVideoTrackId(); paused_when_hidden_ = false;
selectedVideoTrackChanged(&trackId); OnPlay(); // Calls UpdatePlayState() so return afterwards.
return;
}
if (client_->hasSelectedVideoTrack()) {
WebMediaPlayer::TrackId trackId = client_->getSelectedVideoTrackId();
selectedVideoTrackChanged(&trackId);
}
} }
must_suspend_ = false; must_suspend_ = false;
...@@ -2051,4 +2071,13 @@ void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { ...@@ -2051,4 +2071,13 @@ void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) {
client_->activateViewportIntersectionMonitoring(activate); client_->activateViewportIntersectionMonitoring(activate);
} }
bool WebMediaPlayerImpl::ShouldPauseWhenHidden() const {
#if defined(OS_ANDROID) // WMPI_CAST
if (isRemote())
return false;
#endif // defined(OS_ANDROID) // WMPI_CAST
return hasVideo() && !hasAudio();
}
} // namespace media } // namespace media
...@@ -368,6 +368,9 @@ class MEDIA_BLINK_EXPORT WebMediaPlayerImpl ...@@ -368,6 +368,9 @@ class MEDIA_BLINK_EXPORT WebMediaPlayerImpl
// is intended for android. // is intended for android.
bool DoesOverlaySupportMetadata() const; bool DoesOverlaySupportMetadata() const;
// Whether the media should be paused when hidden.
bool ShouldPauseWhenHidden() const;
blink::WebLocalFrame* frame_; blink::WebLocalFrame* frame_;
// The playback state last reported to |delegate_|, to avoid setting duplicate // The playback state last reported to |delegate_|, to avoid setting duplicate
...@@ -431,6 +434,10 @@ class MEDIA_BLINK_EXPORT WebMediaPlayerImpl ...@@ -431,6 +434,10 @@ class MEDIA_BLINK_EXPORT WebMediaPlayerImpl
bool paused_; bool paused_;
base::TimeDelta paused_time_; base::TimeDelta paused_time_;
// Set if paused automatically when hidden and need to resume when visible.
// Reset if paused for any other reason.
bool paused_when_hidden_;
// Set when starting, seeking, and resuming (all of which require a Pipeline // Set when starting, seeking, and resuming (all of which require a Pipeline
// seek). |seek_time_| is only valid when |seeking_| is true. // seek). |seek_time_| is only valid when |seeking_| is true.
bool seeking_; bool seeking_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment