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 "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 Loading... |
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 1294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1432 } | 1428 } |
1433 | 1429 |
1434 void WebMediaPlayerImpl::OnFrameHidden() { | 1430 void WebMediaPlayerImpl::OnFrameHidden() { |
1435 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1431 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1436 | 1432 |
1437 if (watch_time_reporter_) | 1433 if (watch_time_reporter_) |
1438 watch_time_reporter_->OnHidden(); | 1434 watch_time_reporter_->OnHidden(); |
1439 | 1435 |
1440 // OnFrameHidden() can be called when frame is closed, then IsHidden() will | 1436 // OnFrameHidden() can be called when frame is closed, then IsHidden() will |
1441 // return false, so check explicitly. | 1437 // return false, so check explicitly. |
1442 if (IsHidden()) { | 1438 if (IsHidden()) |
1443 if (ShouldPauseVideoWhenHidden()) { | 1439 client_->wasBackgrounded(); |
1444 PauseVideoIfNeeded(); | |
1445 return; | |
1446 } else { | |
1447 DisableVideoTrackIfNeeded(); | |
1448 } | |
1449 } | |
1450 | 1440 |
| 1441 UpdateBackgroundVideoOptimizationState(); |
1451 UpdatePlayState(); | 1442 UpdatePlayState(); |
1452 | 1443 |
1453 // Schedule suspended playing media to be paused if the user doesn't come back | 1444 // 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. | 1445 // to it within some timeout period to avoid any autoplay surprises. |
1455 ScheduleIdlePauseTimer(); | 1446 ScheduleIdlePauseTimer(); |
1456 } | 1447 } |
1457 | 1448 |
1458 void WebMediaPlayerImpl::OnFrameClosed() { | 1449 void WebMediaPlayerImpl::OnFrameClosed() { |
1459 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1450 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1460 UpdatePlayState(); | 1451 UpdatePlayState(); |
(...skipping 14 matching lines...) Expand all Loading... |
1475 VideoFrameCompositor::OnNewProcessedFrameCB new_processed_frame_cb = | 1466 VideoFrameCompositor::OnNewProcessedFrameCB new_processed_frame_cb = |
1476 BindToCurrentLoop(base::Bind( | 1467 BindToCurrentLoop(base::Bind( |
1477 &WebMediaPlayerImpl::ReportTimeFromForegroundToFirstFrame, | 1468 &WebMediaPlayerImpl::ReportTimeFromForegroundToFirstFrame, |
1478 AsWeakPtr(), base::TimeTicks::Now())); | 1469 AsWeakPtr(), base::TimeTicks::Now())); |
1479 compositor_task_runner_->PostTask( | 1470 compositor_task_runner_->PostTask( |
1480 FROM_HERE, | 1471 FROM_HERE, |
1481 base::Bind(&VideoFrameCompositor::SetOnNewProcessedFrameCallback, | 1472 base::Bind(&VideoFrameCompositor::SetOnNewProcessedFrameCallback, |
1482 base::Unretained(compositor_), new_processed_frame_cb)); | 1473 base::Unretained(compositor_), new_processed_frame_cb)); |
1483 } | 1474 } |
1484 | 1475 |
| 1476 EnableVideoTrackIfNeeded(); |
| 1477 |
1485 if (paused_when_hidden_) { | 1478 if (paused_when_hidden_) { |
1486 paused_when_hidden_ = false; | 1479 paused_when_hidden_ = false; |
1487 OnPlay(); // Calls UpdatePlayState() so return afterwards. | 1480 OnPlay(); // Calls UpdatePlayState() so return afterwards. |
1488 return; | 1481 return; |
1489 } | 1482 } |
1490 | 1483 |
1491 EnableVideoTrackIfNeeded(); | |
1492 | |
1493 UpdatePlayState(); | 1484 UpdatePlayState(); |
1494 } | 1485 } |
1495 | 1486 |
1496 void WebMediaPlayerImpl::OnIdleTimeout() { | 1487 void WebMediaPlayerImpl::OnIdleTimeout() { |
1497 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1488 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1498 | 1489 |
1499 // If we are attempting preroll, clear the stale flag. | 1490 // If we are attempting preroll, clear the stale flag. |
1500 if (IsPrerollAttemptNeeded()) { | 1491 if (IsPrerollAttemptNeeded()) { |
1501 delegate_->ClearStaleFlag(delegate_id_); | 1492 delegate_->ClearStaleFlag(delegate_id_); |
1502 return; | 1493 return; |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1947 bool is_backgrounded) { | 1938 bool is_backgrounded) { |
1948 PlayState result; | 1939 PlayState result; |
1949 | 1940 |
1950 bool must_suspend = delegate_->IsFrameClosed(); | 1941 bool must_suspend = delegate_->IsFrameClosed(); |
1951 bool is_stale = delegate_->IsStale(delegate_id_); | 1942 bool is_stale = delegate_->IsStale(delegate_id_); |
1952 | 1943 |
1953 // This includes both data source (before pipeline startup) and pipeline | 1944 // This includes both data source (before pipeline startup) and pipeline |
1954 // errors. | 1945 // errors. |
1955 bool has_error = IsNetworkStateError(network_state_); | 1946 bool has_error = IsNetworkStateError(network_state_); |
1956 | 1947 |
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; | 1948 // After HaveFutureData, Blink will call play() if the state is not paused; |
1961 // prior to this point |paused_| is not accurate. | 1949 // prior to this point |paused_| is not accurate. |
1962 bool have_future_data = | 1950 bool have_future_data = |
1963 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData; | 1951 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData; |
1964 | 1952 |
1965 // Background suspend is not enabled for audio-only players unless paused, | 1953 // Background suspend is only enabled for paused players. |
1966 // though in the case of audio-only the session should be kept. | 1954 // 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 | 1955 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; | 1956 can_auto_suspend && is_backgrounded && paused_ && have_future_data; |
1977 | 1957 |
1978 // Idle suspension is allowed prior to have future data since there exist | 1958 // 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 | 1959 // mechanisms to exit the idle state when the player is capable of reaching |
1980 // the have future data state; see didLoadingProgress(). | 1960 // the have future data state; see didLoadingProgress(). |
1981 // | 1961 // |
1982 // TODO(sandersd): Make the delegate suspend idle players immediately when | 1962 // TODO(sandersd): Make the delegate suspend idle players immediately when |
1983 // hidden. | 1963 // hidden. |
1984 bool idle_suspended = | 1964 bool idle_suspended = |
1985 can_auto_suspend && is_stale && paused_ && !seeking_ && !overlay_enabled_; | 1965 can_auto_suspend && is_stale && paused_ && !seeking_ && !overlay_enabled_; |
1986 | 1966 |
1987 // If we're already suspended, see if we can wait for user interaction. Prior | 1967 // 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| | 1968 // 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. | 1969 // will be cleared when we receive data which may take us to HaveFutureData. |
1990 bool can_stay_suspended = | 1970 bool can_stay_suspended = |
1991 (is_stale || have_future_data) && is_suspended && paused_ && !seeking_; | 1971 (is_stale || have_future_data) && is_suspended && paused_ && !seeking_; |
1992 | 1972 |
1993 // Combined suspend state. | 1973 // Combined suspend state. |
1994 result.is_suspended = is_remote || must_suspend || idle_suspended || | 1974 result.is_suspended = is_remote || must_suspend || idle_suspended || |
1995 background_suspended || background_pause_suspended || | 1975 background_suspended || can_stay_suspended; |
1996 can_stay_suspended; | |
1997 | 1976 |
1998 // We do not treat |playback_rate_| == 0 as paused. For the media session, | 1977 // 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 | 1978 // 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 | 1979 // case. For memory usage reporting, we just use the same definition (but we |
2001 // don't have to). | 1980 // don't have to). |
2002 // | 1981 // |
2003 // Similarly, we don't consider |ended_| to be paused. Blink will immediately | 1982 // Similarly, we don't consider |ended_| to be paused. Blink will immediately |
2004 // call pause() or seek(), so |ended_| should not affect the computation. | 1983 // call pause() or seek(), so |ended_| should not affect the computation. |
2005 // Despite that, |ended_| does result in a separate paused state, to simplfy | 1984 // Despite that, |ended_| does result in a separate paused state, to simplfy |
2006 // the contract for SetDelegateState(). | 1985 // the contract for SetDelegateState(). |
2007 // | 1986 // |
2008 // |has_session| is used to decide when to create a media session. Idle | 1987 // |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 | 1988 // suspension does not destroy the media session, because we expect that the |
2010 // notification controls (and audio focus) remain. We also require: | 1989 // notification controls (and audio focus) remain. We also require: |
2011 // - |have_metadata|, since the tracks and duration are passed to DidPlay(). | 1990 // - |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 | 1991 // - |have_future_data|, since we need to know whether we are paused to |
2013 // correctly configure the session. | 1992 // correctly configure the session. |
2014 // | 1993 // |
2015 // TODO(sandersd): If Blink told us the paused state sooner, we could create | 1994 // TODO(sandersd): If Blink told us the paused state sooner, we could create |
2016 // the media session sooner. | 1995 // the media session sooner. |
2017 bool can_play = !has_error && !is_remote && have_future_data; | 1996 bool can_play = !has_error && !is_remote && have_future_data; |
2018 bool has_session_playing = can_play && !must_suspend && !background_suspended; | 1997 bool has_session = |
2019 | 1998 can_play && !must_suspend && hasAudio() && |
2020 // |has_session_suspended| means the player is suspended from the media | 1999 (!hasVideo() || !is_backgrounded || !IsBackgroundedSuspendEnabled() || |
2021 // element point of view but paused and can be resumed from the delegate point | 2000 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 | 2001 |
2028 if (!has_session) { | 2002 if (!has_session) { |
2029 result.delegate_state = DelegateState::GONE; | 2003 result.delegate_state = DelegateState::GONE; |
2030 result.is_idle = delegate_->IsIdle(delegate_id_); | 2004 result.is_idle = delegate_->IsIdle(delegate_id_); |
2031 } else if (paused_ || has_session_suspended) { | 2005 } else if (paused_) { |
2032 // TODO(sandersd): Is it possible to have a suspended session, be ended, | 2006 // 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. | 2007 // and not be paused? If so we should be in a PLAYING state. |
2034 result.delegate_state = | 2008 result.delegate_state = |
2035 ended_ ? DelegateState::GONE : DelegateState::PAUSED; | 2009 ended_ ? DelegateState::GONE : DelegateState::PAUSED; |
2036 result.is_idle = !seeking_; | 2010 result.is_idle = !seeking_; |
2037 } else { | 2011 } else { |
2038 result.delegate_state = DelegateState::PLAYING; | 2012 result.delegate_state = DelegateState::PLAYING; |
2039 result.is_idle = false; | 2013 result.is_idle = false; |
2040 } | 2014 } |
2041 | 2015 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2099 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource", | 2073 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource", |
2100 data_source_memory_usage / 1024); | 2074 data_source_memory_usage / 1024); |
2101 } | 2075 } |
2102 if (demuxer_) { | 2076 if (demuxer_) { |
2103 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer", | 2077 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer", |
2104 demuxer_memory_usage / 1024); | 2078 demuxer_memory_usage / 1024); |
2105 } | 2079 } |
2106 } | 2080 } |
2107 | 2081 |
2108 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { | 2082 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { |
2109 // Only schedule the pause timer if we're playing and are suspended. | 2083 // Only schedule the pause timer if we're not paused or paused but going to |
2110 if (paused_ || !pipeline_controller_.IsSuspended()) | 2084 // resume when foregrounded, and are suspended. |
| 2085 if ((paused_ && !paused_when_hidden_) || !pipeline_controller_.IsSuspended()) |
2111 return; | 2086 return; |
2112 | 2087 |
2113 #if defined(OS_ANDROID) | 2088 #if defined(OS_ANDROID) |
2114 // Remote players will be suspended and locally paused. | 2089 // Remote players will be suspended and locally paused. |
2115 if (isRemote()) | 2090 if (isRemote()) |
2116 return; | 2091 return; |
2117 #endif | 2092 #endif |
2118 | 2093 |
2119 // Idle timeout chosen arbitrarily. | 2094 // Idle timeout chosen arbitrarily. |
2120 background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5), | 2095 background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5), |
(...skipping 27 matching lines...) Expand all Loading... |
2148 return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0; | 2123 return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0; |
2149 } | 2124 } |
2150 | 2125 |
2151 void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { | 2126 void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { |
2152 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 2127 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
2153 | 2128 |
2154 client_->activateViewportIntersectionMonitoring(activate); | 2129 client_->activateViewportIntersectionMonitoring(activate); |
2155 } | 2130 } |
2156 | 2131 |
2157 bool WebMediaPlayerImpl::ShouldPauseVideoWhenHidden() const { | 2132 bool WebMediaPlayerImpl::ShouldPauseVideoWhenHidden() const { |
2158 #if !defined(OS_ANDROID) | 2133 // If suspending background video, pause any video that's not remoted or |
2159 // On desktop, this behavior is behind the feature flag. | 2134 // not unlocked to play in the background. |
2160 if (!IsBackgroundVideoTrackOptimizationEnabled()) | 2135 if (IsBackgroundedSuspendEnabled()) { |
2161 return false; | 2136 if (!hasVideo()) |
| 2137 return false; |
| 2138 |
| 2139 #if defined(OS_ANDROID) |
| 2140 if (isRemote()) |
| 2141 return false; |
2162 #endif | 2142 #endif |
2163 | 2143 |
2164 // Pause video-only players that match the criteria for being optimized. | 2144 return !hasAudio() || !client_->isBackgroundVideoPlaybackUnlocked(); |
2165 return !hasAudio() && IsBackgroundOptimizationCandidate(); | 2145 } |
| 2146 |
| 2147 // Otherwise only pause if the optimization is on and it's a video-only |
| 2148 // optimization candidate. |
| 2149 return IsBackgroundVideoTrackOptimizationEnabled() && !hasAudio() && |
| 2150 IsBackgroundOptimizationCandidate(); |
2166 } | 2151 } |
2167 | 2152 |
2168 bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { | 2153 bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { |
2169 // This optimization is behind the flag on all platforms. | 2154 // This optimization is behind the flag on all platforms. |
2170 if (!IsBackgroundVideoTrackOptimizationEnabled()) | 2155 if (!IsBackgroundVideoTrackOptimizationEnabled()) |
2171 return false; | 2156 return false; |
2172 | 2157 |
2173 // Disable video track only for players with audio that match the criteria for | 2158 // Disable video track only for players with audio that match the criteria for |
2174 // being optimized. | 2159 // being optimized. |
2175 return hasAudio() && IsBackgroundOptimizationCandidate(); | 2160 return hasAudio() && IsBackgroundOptimizationCandidate(); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2286 base::TimeDelta time_to_first_frame = new_frame_time - foreground_time; | 2271 base::TimeDelta time_to_first_frame = new_frame_time - foreground_time; |
2287 if (hasAudio()) { | 2272 if (hasAudio()) { |
2288 UMA_HISTOGRAM_TIMES( | 2273 UMA_HISTOGRAM_TIMES( |
2289 "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack", | 2274 "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack", |
2290 time_to_first_frame); | 2275 time_to_first_frame); |
2291 } else { | 2276 } else { |
2292 UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused", | 2277 UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused", |
2293 time_to_first_frame); | 2278 time_to_first_frame); |
2294 } | 2279 } |
2295 } | 2280 } |
2296 | |
2297 void WebMediaPlayerImpl::SwitchRenderer(bool disable_pipeline_auto_suspend) { | 2281 void WebMediaPlayerImpl::SwitchRenderer(bool disable_pipeline_auto_suspend) { |
2298 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 2282 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
2299 disable_pipeline_auto_suspend_ = disable_pipeline_auto_suspend; | 2283 disable_pipeline_auto_suspend_ = disable_pipeline_auto_suspend; |
2300 ScheduleRestart(); | 2284 ScheduleRestart(); |
2301 } | 2285 } |
2302 | 2286 |
2303 } // namespace media | 2287 } // namespace media |
OLD | NEW |