| 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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 } | 121 } |
| 122 | 122 |
| 123 bool IsResumeBackgroundVideosEnabled() { | 123 bool IsResumeBackgroundVideosEnabled() { |
| 124 return base::FeatureList::IsEnabled(kResumeBackgroundVideo); | 124 return base::FeatureList::IsEnabled(kResumeBackgroundVideo); |
| 125 } | 125 } |
| 126 | 126 |
| 127 bool IsBackgroundVideoTrackOptimizationEnabled() { | 127 bool IsBackgroundVideoTrackOptimizationEnabled() { |
| 128 return base::FeatureList::IsEnabled(kBackgroundVideoTrackOptimization); | 128 return base::FeatureList::IsEnabled(kBackgroundVideoTrackOptimization); |
| 129 } | 129 } |
| 130 | 130 |
| 131 bool IsBackgroundVideoPauseEnabled() { |
| 132 #if defined(OS_ANDROID) |
| 133 return true; |
| 134 #else |
| 135 return IsBackgroundVideoTrackOptimizationEnabled(); |
| 136 #endif |
| 137 } |
| 138 |
| 131 bool IsNetworkStateError(blink::WebMediaPlayer::NetworkState state) { | 139 bool IsNetworkStateError(blink::WebMediaPlayer::NetworkState state) { |
| 132 bool result = state == blink::WebMediaPlayer::NetworkStateFormatError || | 140 bool result = state == blink::WebMediaPlayer::NetworkStateFormatError || |
| 133 state == blink::WebMediaPlayer::NetworkStateNetworkError || | 141 state == blink::WebMediaPlayer::NetworkStateNetworkError || |
| 134 state == blink::WebMediaPlayer::NetworkStateDecodeError; | 142 state == blink::WebMediaPlayer::NetworkStateDecodeError; |
| 135 DCHECK_EQ(state > blink::WebMediaPlayer::NetworkStateLoaded, result); | 143 DCHECK_EQ(state > blink::WebMediaPlayer::NetworkStateLoaded, result); |
| 136 return result; | 144 return result; |
| 137 } | 145 } |
| 138 | 146 |
| 139 gfx::Size GetRotatedVideoSize(VideoRotation rotation, gfx::Size natural_size) { | 147 gfx::Size GetRotatedVideoSize(VideoRotation rotation, gfx::Size natural_size) { |
| 140 if (rotation == VIDEO_ROTATION_90 || rotation == VIDEO_ROTATION_270) | 148 if (rotation == VIDEO_ROTATION_90 || rotation == VIDEO_ROTATION_270) |
| (...skipping 1262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1403 if (video_weblayer_) | 1411 if (video_weblayer_) |
| 1404 video_weblayer_->layer()->SetContentsOpaque(opaque_); | 1412 video_weblayer_->layer()->SetContentsOpaque(opaque_); |
| 1405 } | 1413 } |
| 1406 | 1414 |
| 1407 void WebMediaPlayerImpl::OnFrameHidden() { | 1415 void WebMediaPlayerImpl::OnFrameHidden() { |
| 1408 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1416 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1409 | 1417 |
| 1410 if (watch_time_reporter_) | 1418 if (watch_time_reporter_) |
| 1411 watch_time_reporter_->OnHidden(); | 1419 watch_time_reporter_->OnHidden(); |
| 1412 | 1420 |
| 1413 if (ShouldPauseWhenHidden()) { | 1421 if (IsBackgroundVideoPauseEnabled() && ShouldPauseWhenHidden()) { |
| 1414 if (!paused_when_hidden_) { | 1422 if (!paused_when_hidden_) { |
| 1415 // OnPause() will set |paused_when_hidden_| to false and call | 1423 // OnPause() will set |paused_when_hidden_| to false and call |
| 1416 // UpdatePlayState(), so set the flag to true after and then return. | 1424 // UpdatePlayState(), so set the flag to true after and then return. |
| 1417 OnPause(); | 1425 OnPause(); |
| 1418 paused_when_hidden_ = true; | 1426 paused_when_hidden_ = true; |
| 1419 return; | 1427 return; |
| 1420 } | 1428 } |
| 1421 } else { | 1429 } else { |
| 1422 DisableVideoTrackIfNeeded(); | 1430 DisableVideoTrackIfNeeded(); |
| 1423 } | 1431 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1434 UpdatePlayState(); | 1442 UpdatePlayState(); |
| 1435 } | 1443 } |
| 1436 | 1444 |
| 1437 void WebMediaPlayerImpl::OnFrameShown() { | 1445 void WebMediaPlayerImpl::OnFrameShown() { |
| 1438 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1446 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1439 background_pause_timer_.Stop(); | 1447 background_pause_timer_.Stop(); |
| 1440 | 1448 |
| 1441 if (watch_time_reporter_) | 1449 if (watch_time_reporter_) |
| 1442 watch_time_reporter_->OnShown(); | 1450 watch_time_reporter_->OnShown(); |
| 1443 | 1451 |
| 1444 compositor_task_runner_->PostTask( | 1452 // Only track the time to the first frame if playing or about to play because |
| 1445 FROM_HERE, | 1453 // of being shown and only for videos we would optimize background playback |
| 1446 base::Bind(&VideoFrameCompositor::SetForegroundTime, | 1454 // for. |
| 1447 base::Unretained(compositor_), base::TimeTicks::Now())); | 1455 if (!paused_ && ShouldDisableVideoWhenHidden()) { |
| 1456 compositor_task_runner_->PostTask( |
| 1457 FROM_HERE, |
| 1458 base::Bind( |
| 1459 &VideoFrameCompositor::SetForegroundTime, |
| 1460 base::Unretained(compositor_), base::TimeTicks::Now(), |
| 1461 VideoFrameCompositor::BackgroundVideoOptimization::DisabledTrack)); |
| 1462 } else if (paused_when_hidden_ && ShouldPauseWhenHidden()) { |
| 1463 compositor_task_runner_->PostTask( |
| 1464 FROM_HERE, |
| 1465 base::Bind(&VideoFrameCompositor::SetForegroundTime, |
| 1466 base::Unretained(compositor_), base::TimeTicks::Now(), |
| 1467 VideoFrameCompositor::BackgroundVideoOptimization::Paused)); |
| 1468 } |
| 1448 | 1469 |
| 1449 if (paused_when_hidden_) { | 1470 if (paused_when_hidden_) { |
| 1450 paused_when_hidden_ = false; | 1471 paused_when_hidden_ = false; |
| 1451 OnPlay(); // Calls UpdatePlayState() so return afterwards. | 1472 OnPlay(); // Calls UpdatePlayState() so return afterwards. |
| 1452 return; | 1473 return; |
| 1453 } | 1474 } |
| 1454 | 1475 |
| 1455 EnableVideoTrackIfNeeded(); | 1476 EnableVideoTrackIfNeeded(); |
| 1456 | 1477 |
| 1457 UpdatePlayState(); | 1478 UpdatePlayState(); |
| (...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2116 return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0; | 2137 return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0; |
| 2117 } | 2138 } |
| 2118 | 2139 |
| 2119 void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { | 2140 void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { |
| 2120 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 2141 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2121 | 2142 |
| 2122 client_->activateViewportIntersectionMonitoring(activate); | 2143 client_->activateViewportIntersectionMonitoring(activate); |
| 2123 } | 2144 } |
| 2124 | 2145 |
| 2125 bool WebMediaPlayerImpl::ShouldPauseWhenHidden() const { | 2146 bool WebMediaPlayerImpl::ShouldPauseWhenHidden() const { |
| 2126 DCHECK(IsHidden()); | 2147 // Don't pause videos being Cast (Android only). |
| 2127 // Don't pause videos being Cast (Android only) or if the background video | |
| 2128 // optimizations are off (desktop only). | |
| 2129 #if defined(OS_ANDROID) // WMPI_CAST | 2148 #if defined(OS_ANDROID) // WMPI_CAST |
| 2130 if (isRemote()) | 2149 if (isRemote()) |
| 2131 return false; | 2150 return false; |
| 2132 #else // defined(OS_ANDROID) | |
| 2133 if (!IsBackgroundVideoTrackOptimizationEnabled()) | |
| 2134 return false; | |
| 2135 #endif // defined(OS_ANDROID) | 2151 #endif // defined(OS_ANDROID) |
| 2136 | 2152 |
| 2137 return hasVideo() && !hasAudio(); | 2153 return hasVideo() && !hasAudio(); |
| 2138 } | 2154 } |
| 2139 | 2155 |
| 2140 bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { | 2156 bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { |
| 2141 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 2157 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2142 DCHECK(IsHidden()); | |
| 2143 | 2158 |
| 2144 if (!IsBackgroundVideoTrackOptimizationEnabled() || !hasVideo() || | 2159 // Don't disable video track for videos being Cast (Android only). |
| 2145 !hasAudio() || IsStreaming()) { | 2160 #if defined(OS_ANDROID) // WMPI_CAST |
| 2161 if (isRemote()) |
| 2146 return false; | 2162 return false; |
| 2147 } | 2163 #endif // defined(OS_ANDROID) |
| 2164 |
| 2165 if (!hasVideo() || !hasAudio() || IsStreaming()) |
| 2166 return false; |
| 2148 | 2167 |
| 2149 PipelineStatistics stats = GetPipelineStatistics(); | 2168 PipelineStatistics stats = GetPipelineStatistics(); |
| 2150 return stats.video_keyframe_distance_average < | 2169 return stats.video_keyframe_distance_average < |
| 2151 max_keyframe_distance_to_disable_background_video_; | 2170 max_keyframe_distance_to_disable_background_video_; |
| 2152 } | 2171 } |
| 2153 | 2172 |
| 2154 void WebMediaPlayerImpl::EnableVideoTrackIfNeeded() { | 2173 void WebMediaPlayerImpl::EnableVideoTrackIfNeeded() { |
| 2155 DCHECK(!IsHidden()); | 2174 DCHECK(!IsHidden()); |
| 2156 | 2175 |
| 2157 // Don't change video track while the pipeline is resuming or seeking. | 2176 // Don't change video track while the pipeline is resuming or seeking. |
| 2158 if (is_pipeline_resuming_ || seeking_) | 2177 if (is_pipeline_resuming_ || seeking_) |
| 2159 return; | 2178 return; |
| 2160 | 2179 |
| 2161 if (video_track_disabled_) { | 2180 if (video_track_disabled_) { |
| 2162 video_track_disabled_ = false; | 2181 video_track_disabled_ = false; |
| 2163 if (client_->hasSelectedVideoTrack()) { | 2182 if (client_->hasSelectedVideoTrack()) { |
| 2164 WebMediaPlayer::TrackId trackId = client_->getSelectedVideoTrackId(); | 2183 WebMediaPlayer::TrackId trackId = client_->getSelectedVideoTrackId(); |
| 2165 selectedVideoTrackChanged(&trackId); | 2184 selectedVideoTrackChanged(&trackId); |
| 2166 } | 2185 } |
| 2167 } | 2186 } |
| 2168 } | 2187 } |
| 2169 | 2188 |
| 2170 void WebMediaPlayerImpl::DisableVideoTrackIfNeeded() { | 2189 void WebMediaPlayerImpl::DisableVideoTrackIfNeeded() { |
| 2171 DCHECK(IsHidden()); | 2190 DCHECK(IsHidden()); |
| 2172 | 2191 |
| 2173 // Don't change video track while the pipeline is resuming or seeking. | 2192 // Don't change video track while the pipeline is resuming or seeking. |
| 2174 if (is_pipeline_resuming_ || seeking_) | 2193 if (is_pipeline_resuming_ || seeking_) |
| 2175 return; | 2194 return; |
| 2176 | 2195 |
| 2177 if (!video_track_disabled_ && ShouldDisableVideoWhenHidden()) { | 2196 if (!video_track_disabled_ && IsBackgroundVideoTrackOptimizationEnabled() && |
| 2197 ShouldDisableVideoWhenHidden()) { |
| 2178 video_track_disabled_ = true; | 2198 video_track_disabled_ = true; |
| 2179 selectedVideoTrackChanged(nullptr); | 2199 selectedVideoTrackChanged(nullptr); |
| 2180 } | 2200 } |
| 2181 } | 2201 } |
| 2182 | 2202 |
| 2183 void WebMediaPlayerImpl::SetPipelineStatisticsForTest( | 2203 void WebMediaPlayerImpl::SetPipelineStatisticsForTest( |
| 2184 const PipelineStatistics& stats) { | 2204 const PipelineStatistics& stats) { |
| 2185 pipeline_statistics_for_test_ = base::make_optional(stats); | 2205 pipeline_statistics_for_test_ = base::make_optional(stats); |
| 2186 } | 2206 } |
| 2187 | 2207 |
| 2188 PipelineStatistics WebMediaPlayerImpl::GetPipelineStatistics() const { | 2208 PipelineStatistics WebMediaPlayerImpl::GetPipelineStatistics() const { |
| 2189 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 2209 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 2190 | 2210 |
| 2191 return pipeline_statistics_for_test_.value_or(pipeline_.GetStatistics()); | 2211 return pipeline_statistics_for_test_.value_or(pipeline_.GetStatistics()); |
| 2192 } | 2212 } |
| 2193 | 2213 |
| 2194 } // namespace media | 2214 } // namespace media |
| OLD | NEW |