Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: media/blink/webmediaplayer_impl.cc

Issue 2681863005: [Video] MediaSession API event handlers can resume background video. (Closed)
Patch Set: has_session is true on desktop Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "media/blink/webmediaplayer_impl.h" 5 #include "media/blink/webmediaplayer_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <limits> 9 #include <limits>
10 #include <string> 10 #include <string>
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 #if !defined(OS_ANDROID) 114 #if !defined(OS_ANDROID)
115 // Suspend/Resume is only enabled by default on Android. 115 // Suspend/Resume is only enabled by default on Android.
116 return base::CommandLine::ForCurrentProcess()->HasSwitch( 116 return base::CommandLine::ForCurrentProcess()->HasSwitch(
117 switches::kEnableMediaSuspend); 117 switches::kEnableMediaSuspend);
118 #else 118 #else
119 return !base::CommandLine::ForCurrentProcess()->HasSwitch( 119 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
120 switches::kDisableMediaSuspend); 120 switches::kDisableMediaSuspend);
121 #endif 121 #endif
122 } 122 }
123 123
124 bool IsResumeBackgroundVideosEnabled() {
125 return base::FeatureList::IsEnabled(kResumeBackgroundVideo);
126 }
127
128 bool IsBackgroundVideoTrackOptimizationEnabled() { 124 bool IsBackgroundVideoTrackOptimizationEnabled() {
129 return base::FeatureList::IsEnabled(kBackgroundVideoTrackOptimization); 125 return base::FeatureList::IsEnabled(kBackgroundVideoTrackOptimization);
130 } 126 }
131 127
132 bool IsNetworkStateError(blink::WebMediaPlayer::NetworkState state) { 128 bool IsNetworkStateError(blink::WebMediaPlayer::NetworkState state) {
133 bool result = state == blink::WebMediaPlayer::NetworkStateFormatError || 129 bool result = state == blink::WebMediaPlayer::NetworkStateFormatError ||
134 state == blink::WebMediaPlayer::NetworkStateNetworkError || 130 state == blink::WebMediaPlayer::NetworkStateNetworkError ||
135 state == blink::WebMediaPlayer::NetworkStateDecodeError; 131 state == blink::WebMediaPlayer::NetworkStateDecodeError;
136 DCHECK_EQ(state > blink::WebMediaPlayer::NetworkStateLoaded, result); 132 DCHECK_EQ(state > blink::WebMediaPlayer::NetworkStateLoaded, result);
137 return result; 133 return result;
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 void WebMediaPlayerImpl::play() { 413 void WebMediaPlayerImpl::play() {
418 DVLOG(1) << __func__; 414 DVLOG(1) << __func__;
419 DCHECK(main_task_runner_->BelongsToCurrentThread()); 415 DCHECK(main_task_runner_->BelongsToCurrentThread());
420 416
421 #if defined(OS_ANDROID) // WMPI_CAST 417 #if defined(OS_ANDROID) // WMPI_CAST
422 if (isRemote()) { 418 if (isRemote()) {
423 cast_impl_.play(); 419 cast_impl_.play();
424 return; 420 return;
425 } 421 }
426 #endif 422 #endif
423
424 if (IsHidden() && ShouldPauseVideoWhenHidden())
425 return;
426
427 // TODO(sandersd): Do we want to reset the idle timer here? 427 // TODO(sandersd): Do we want to reset the idle timer here?
428 delegate_->SetIdle(delegate_id_, false); 428 delegate_->SetIdle(delegate_id_, false);
429 paused_ = false; 429 paused_ = false;
430 pipeline_.SetPlaybackRate(playback_rate_); 430 pipeline_.SetPlaybackRate(playback_rate_);
431 background_pause_timer_.Stop(); 431 background_pause_timer_.Stop();
432 432
433 if (data_source_) 433 if (data_source_)
434 data_source_->MediaIsPlaying(); 434 data_source_->MediaIsPlaying();
435 435
436 if (observer_) 436 if (observer_)
(...skipping 993 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 void WebMediaPlayerImpl::OnVideoAverageKeyframeDistanceUpdate() { 1430 void WebMediaPlayerImpl::OnVideoAverageKeyframeDistanceUpdate() {
1431 UpdateBackgroundVideoOptimizationState(); 1431 UpdateBackgroundVideoOptimizationState();
1432 } 1432 }
1433 1433
1434 void WebMediaPlayerImpl::OnFrameHidden() { 1434 void WebMediaPlayerImpl::OnFrameHidden() {
1435 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1435 DCHECK(main_task_runner_->BelongsToCurrentThread());
1436 1436
1437 if (watch_time_reporter_) 1437 if (watch_time_reporter_)
1438 watch_time_reporter_->OnHidden(); 1438 watch_time_reporter_->OnHidden();
1439 1439
1440 // OnFrameHidden() can be called when frame is closed, then IsHidden() will 1440 UpdateBackgroundVideoOptimizationState();
1441 // return false, so check explicitly.
1442 if (IsHidden()) {
1443 if (ShouldPauseVideoWhenHidden()) {
1444 PauseVideoIfNeeded();
1445 return;
1446 } else {
1447 DisableVideoTrackIfNeeded();
1448 }
1449 }
1450
1451 UpdatePlayState(); 1441 UpdatePlayState();
1452 1442
1453 // Schedule suspended playing media to be paused if the user doesn't come back 1443 // Schedule suspended playing media to be paused if the user doesn't come back
1454 // to it within some timeout period to avoid any autoplay surprises. 1444 // to it within some timeout period to avoid any autoplay surprises.
1455 ScheduleIdlePauseTimer(); 1445 ScheduleIdlePauseTimer();
1456 } 1446 }
1457 1447
1458 void WebMediaPlayerImpl::OnFrameClosed() { 1448 void WebMediaPlayerImpl::OnFrameClosed() {
1459 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1449 DCHECK(main_task_runner_->BelongsToCurrentThread());
1460 UpdatePlayState(); 1450 UpdatePlayState();
(...skipping 16 matching lines...) Expand all
1477 &WebMediaPlayerImpl::ReportTimeFromForegroundToFirstFrame, 1467 &WebMediaPlayerImpl::ReportTimeFromForegroundToFirstFrame,
1478 AsWeakPtr(), base::TimeTicks::Now())); 1468 AsWeakPtr(), base::TimeTicks::Now()));
1479 compositor_task_runner_->PostTask( 1469 compositor_task_runner_->PostTask(
1480 FROM_HERE, 1470 FROM_HERE,
1481 base::Bind(&VideoFrameCompositor::SetOnNewProcessedFrameCallback, 1471 base::Bind(&VideoFrameCompositor::SetOnNewProcessedFrameCallback,
1482 base::Unretained(compositor_), new_processed_frame_cb)); 1472 base::Unretained(compositor_), new_processed_frame_cb));
1483 } 1473 }
1484 1474
1485 if (paused_when_hidden_) { 1475 if (paused_when_hidden_) {
1486 paused_when_hidden_ = false; 1476 paused_when_hidden_ = false;
1487 OnPlay(); // Calls UpdatePlayState() so return afterwards. 1477 OnPlay();
DaleCurtis 2017/02/16 02:47:38 Comment is still true, so we end up updating play
whywhat 2017/02/16 13:19:31 Hm, yes, otherwise on Android we may not reenable
1488 return;
1489 } 1478 }
1490 1479
1491 EnableVideoTrackIfNeeded(); 1480 EnableVideoTrackIfNeeded();
1492 1481
1493 UpdatePlayState(); 1482 UpdatePlayState();
1494 } 1483 }
1495 1484
1496 void WebMediaPlayerImpl::OnIdleTimeout() { 1485 void WebMediaPlayerImpl::OnIdleTimeout() {
1497 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1486 DCHECK(main_task_runner_->BelongsToCurrentThread());
1498 1487
(...skipping 448 matching lines...) Expand 10 before | Expand all | Expand 10 after
1947 bool is_backgrounded) { 1936 bool is_backgrounded) {
1948 PlayState result; 1937 PlayState result;
1949 1938
1950 bool must_suspend = delegate_->IsFrameClosed(); 1939 bool must_suspend = delegate_->IsFrameClosed();
1951 bool is_stale = delegate_->IsStale(delegate_id_); 1940 bool is_stale = delegate_->IsStale(delegate_id_);
1952 1941
1953 // This includes both data source (before pipeline startup) and pipeline 1942 // This includes both data source (before pipeline startup) and pipeline
1954 // errors. 1943 // errors.
1955 bool has_error = IsNetworkStateError(network_state_); 1944 bool has_error = IsNetworkStateError(network_state_);
1956 1945
1957 // After HaveMetadata, we know which tracks are present and the duration.
1958 bool have_metadata = ready_state_ >= WebMediaPlayer::ReadyStateHaveMetadata;
1959
1960 // After HaveFutureData, Blink will call play() if the state is not paused; 1946 // After HaveFutureData, Blink will call play() if the state is not paused;
1961 // prior to this point |paused_| is not accurate. 1947 // prior to this point |paused_| is not accurate.
1962 bool have_future_data = 1948 bool have_future_data =
1963 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData; 1949 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData;
1964 1950
1965 // Background suspend is not enabled for audio-only players unless paused, 1951 // Background suspend is only enabled for paused players.
1966 // though in the case of audio-only the session should be kept. 1952 // In the case of players with audio the session should be kept.
1967 // Videos are not suspended if the user resumed the playback via the remote 1953 bool background_suspended =
1968 // controls earlier and it's still playing.
1969 bool is_backgrounded_video = is_backgrounded && have_metadata && hasVideo();
1970 bool can_play_backgrounded = is_backgrounded_video && !is_remote &&
1971 hasAudio() && IsResumeBackgroundVideosEnabled();
1972 bool is_background_playing = delegate_->IsBackgroundVideoPlaybackUnlocked();
1973 bool background_suspended = can_auto_suspend && is_backgrounded_video &&
1974 !(can_play_backgrounded && is_background_playing);
1975 bool background_pause_suspended =
1976 can_auto_suspend && is_backgrounded && paused_ && have_future_data; 1954 can_auto_suspend && is_backgrounded && paused_ && have_future_data;
1977 1955
1978 // Idle suspension is allowed prior to have future data since there exist 1956 // Idle suspension is allowed prior to have future data since there exist
1979 // mechanisms to exit the idle state when the player is capable of reaching 1957 // mechanisms to exit the idle state when the player is capable of reaching
1980 // the have future data state; see didLoadingProgress(). 1958 // the have future data state; see didLoadingProgress().
1981 // 1959 //
1982 // TODO(sandersd): Make the delegate suspend idle players immediately when 1960 // TODO(sandersd): Make the delegate suspend idle players immediately when
1983 // hidden. 1961 // hidden.
1984 bool idle_suspended = 1962 bool idle_suspended =
1985 can_auto_suspend && is_stale && paused_ && !seeking_ && !overlay_enabled_; 1963 can_auto_suspend && is_stale && paused_ && !seeking_ && !overlay_enabled_;
1986 1964
1987 // If we're already suspended, see if we can wait for user interaction. Prior 1965 // If we're already suspended, see if we can wait for user interaction. Prior
1988 // to HaveFutureData, we require |is_stale| to remain suspended. |is_stale| 1966 // to HaveFutureData, we require |is_stale| to remain suspended. |is_stale|
1989 // will be cleared when we receive data which may take us to HaveFutureData. 1967 // will be cleared when we receive data which may take us to HaveFutureData.
1990 bool can_stay_suspended = 1968 bool can_stay_suspended =
1991 (is_stale || have_future_data) && is_suspended && paused_ && !seeking_; 1969 (is_stale || have_future_data) && is_suspended && paused_ && !seeking_;
1992 1970
1993 // Combined suspend state. 1971 // Combined suspend state.
1994 result.is_suspended = is_remote || must_suspend || idle_suspended || 1972 result.is_suspended = is_remote || must_suspend || idle_suspended ||
1995 background_suspended || background_pause_suspended || 1973 background_suspended || can_stay_suspended;
1996 can_stay_suspended;
1997 1974
1998 // We do not treat |playback_rate_| == 0 as paused. For the media session, 1975 // We do not treat |playback_rate_| == 0 as paused. For the media session,
1999 // being paused implies displaying a play button, which is incorrect in this 1976 // being paused implies displaying a play button, which is incorrect in this
2000 // case. For memory usage reporting, we just use the same definition (but we 1977 // case. For memory usage reporting, we just use the same definition (but we
2001 // don't have to). 1978 // don't have to).
2002 // 1979 //
2003 // Similarly, we don't consider |ended_| to be paused. Blink will immediately 1980 // Similarly, we don't consider |ended_| to be paused. Blink will immediately
2004 // call pause() or seek(), so |ended_| should not affect the computation. 1981 // call pause() or seek(), so |ended_| should not affect the computation.
2005 // Despite that, |ended_| does result in a separate paused state, to simplfy 1982 // Despite that, |ended_| does result in a separate paused state, to simplfy
2006 // the contract for SetDelegateState(). 1983 // the contract for SetDelegateState().
2007 // 1984 //
2008 // |has_session| is used to decide when to create a media session. Idle 1985 // |has_session| is used to decide when to create a media session. Idle
2009 // suspension does not destroy the media session, because we expect that the 1986 // suspension does not destroy the media session, because we expect that the
2010 // notification controls (and audio focus) remain. We also require: 1987 // notification controls (and audio focus) remain. We also require:
2011 // - |have_metadata|, since the tracks and duration are passed to DidPlay(). 1988 // - |have_metadata|, since the tracks and duration are passed to DidPlay().
2012 // - |have_future_data|, since we need to know whether we are paused to 1989 // - |have_future_data|, since we need to know whether we are paused to
2013 // correctly configure the session. 1990 // correctly configure the session.
2014 // 1991 //
2015 // TODO(sandersd): If Blink told us the paused state sooner, we could create 1992 // TODO(sandersd): If Blink told us the paused state sooner, we could create
2016 // the media session sooner. 1993 // the media session sooner.
2017 bool can_play = !has_error && !is_remote && have_future_data; 1994 bool can_play = !has_error && !is_remote && have_future_data;
2018 bool has_session_playing = can_play && !must_suspend && !background_suspended; 1995 bool has_session =
2019 1996 can_play && !must_suspend && hasAudio() &&
2020 // |has_session_suspended| means the player is suspended from the media 1997 (!hasVideo() || !is_backgrounded || !IsBackgroundedSuspendEnabled() ||
2021 // element point of view but paused and can be resumed from the delegate point 1998 base::FeatureList::IsEnabled(kResumeBackgroundVideo));
2022 // of view. Therefore it behaves like |paused_| for the delegate.
2023 bool has_session_suspended = can_play && !must_suspend &&
2024 background_suspended && can_play_backgrounded;
2025
2026 bool has_session = has_session_playing || has_session_suspended;
2027 1999
2028 if (!has_session) { 2000 if (!has_session) {
2029 result.delegate_state = DelegateState::GONE; 2001 result.delegate_state = DelegateState::GONE;
2030 result.is_idle = delegate_->IsIdle(delegate_id_); 2002 result.is_idle = delegate_->IsIdle(delegate_id_);
2031 } else if (paused_ || has_session_suspended) { 2003 } else if (paused_) {
2032 // TODO(sandersd): Is it possible to have a suspended session, be ended, 2004 // TODO(sandersd): Is it possible to have a suspended session, be ended,
2033 // and not be paused? If so we should be in a PLAYING state. 2005 // and not be paused? If so we should be in a PLAYING state.
2034 result.delegate_state = 2006 result.delegate_state =
2035 ended_ ? DelegateState::GONE : DelegateState::PAUSED; 2007 ended_ ? DelegateState::GONE : DelegateState::PAUSED;
2036 result.is_idle = !seeking_; 2008 result.is_idle = !seeking_;
2037 } else { 2009 } else {
2038 result.delegate_state = DelegateState::PLAYING; 2010 result.delegate_state = DelegateState::PLAYING;
2039 result.is_idle = false; 2011 result.is_idle = false;
2040 } 2012 }
2041 2013
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
2099 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource", 2071 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource",
2100 data_source_memory_usage / 1024); 2072 data_source_memory_usage / 1024);
2101 } 2073 }
2102 if (demuxer_) { 2074 if (demuxer_) {
2103 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer", 2075 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer",
2104 demuxer_memory_usage / 1024); 2076 demuxer_memory_usage / 1024);
2105 } 2077 }
2106 } 2078 }
2107 2079
2108 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { 2080 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() {
2109 // Only schedule the pause timer if we're playing and are suspended. 2081 // Only schedule the pause timer if we're not paused or paused but going to
2110 if (paused_ || !pipeline_controller_.IsSuspended()) 2082 // resume when foregrounded, and are suspended.
2083 if ((paused_ && !paused_when_hidden_) || !pipeline_controller_.IsSuspended())
2111 return; 2084 return;
2112 2085
2113 #if defined(OS_ANDROID) 2086 #if defined(OS_ANDROID)
2114 // Remote players will be suspended and locally paused. 2087 // Remote players will be suspended and locally paused.
2115 if (isRemote()) 2088 if (isRemote())
2116 return; 2089 return;
2117 #endif 2090 #endif
2118 2091
2119 // Idle timeout chosen arbitrarily. 2092 // Idle timeout chosen arbitrarily.
2120 background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5), 2093 background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5),
(...skipping 27 matching lines...) Expand all
2148 return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0; 2121 return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0;
2149 } 2122 }
2150 2123
2151 void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { 2124 void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) {
2152 DCHECK(main_task_runner_->BelongsToCurrentThread()); 2125 DCHECK(main_task_runner_->BelongsToCurrentThread());
2153 2126
2154 client_->activateViewportIntersectionMonitoring(activate); 2127 client_->activateViewportIntersectionMonitoring(activate);
2155 } 2128 }
2156 2129
2157 bool WebMediaPlayerImpl::ShouldPauseVideoWhenHidden() const { 2130 bool WebMediaPlayerImpl::ShouldPauseVideoWhenHidden() const {
2158 #if !defined(OS_ANDROID) 2131 if (IsBackgroundedSuspendEnabled()) {
2159 // On desktop, this behavior is behind the feature flag. 2132 if (!hasVideo())
2160 if (!IsBackgroundVideoTrackOptimizationEnabled()) 2133 return false;
2161 return false; 2134
2135 #if defined(OS_ANDROID)
2136 if (isRemote())
2137 return false;
2162 #endif 2138 #endif
2163 2139
2164 // Pause video-only players that match the criteria for being optimized. 2140 if (hasAudio() && delegate_->IsBackgroundVideoPlaybackUnlocked())
2165 return !hasAudio() && IsBackgroundOptimizationCandidate(); 2141 return false;
2142
2143 return true;
2144 }
2145
2146 // Otherwise only pause if the optimization is on and it's a video-only
2147 // optimization candidate.
2148 return IsBackgroundVideoTrackOptimizationEnabled() && !hasAudio() &&
2149 IsBackgroundOptimizationCandidate();
2166 } 2150 }
2167 2151
2168 bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { 2152 bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const {
2169 // This optimization is behind the flag on all platforms. 2153 // This optimization is behind the flag on all platforms.
2170 if (!IsBackgroundVideoTrackOptimizationEnabled()) 2154 if (!IsBackgroundVideoTrackOptimizationEnabled())
2171 return false; 2155 return false;
2172 2156
2173 // Disable video track only for players with audio that match the criteria for 2157 // Disable video track only for players with audio that match the criteria for
2174 // being optimized. 2158 // being optimized.
2175 return hasAudio() && IsBackgroundOptimizationCandidate(); 2159 return hasAudio() && IsBackgroundOptimizationCandidate();
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
2286 base::TimeDelta time_to_first_frame = new_frame_time - foreground_time; 2270 base::TimeDelta time_to_first_frame = new_frame_time - foreground_time;
2287 if (hasAudio()) { 2271 if (hasAudio()) {
2288 UMA_HISTOGRAM_TIMES( 2272 UMA_HISTOGRAM_TIMES(
2289 "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack", 2273 "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack",
2290 time_to_first_frame); 2274 time_to_first_frame);
2291 } else { 2275 } else {
2292 UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused", 2276 UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused",
2293 time_to_first_frame); 2277 time_to_first_frame);
2294 } 2278 }
2295 } 2279 }
2296
2297 void WebMediaPlayerImpl::SwitchRenderer(bool disable_pipeline_auto_suspend) { 2280 void WebMediaPlayerImpl::SwitchRenderer(bool disable_pipeline_auto_suspend) {
2298 DCHECK(main_task_runner_->BelongsToCurrentThread()); 2281 DCHECK(main_task_runner_->BelongsToCurrentThread());
2299 disable_pipeline_auto_suspend_ = disable_pipeline_auto_suspend; 2282 disable_pipeline_auto_suspend_ = disable_pipeline_auto_suspend;
2300 ScheduleRestart(); 2283 ScheduleRestart();
2301 } 2284 }
2302 2285
2303 } // namespace media 2286 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698