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

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

Issue 2644723004: [Video] Don't pause video-only elements if they're already paused... (Closed)
Patch Set: Improved comments, extracted method Created 3 years, 11 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
« no previous file with comments | « media/blink/webmediaplayer_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 1103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1114 } else { 1114 } else {
1115 DCHECK(watch_time_reporter_); 1115 DCHECK(watch_time_reporter_);
1116 watch_time_reporter_->OnPlaying(); 1116 watch_time_reporter_->OnPlaying();
1117 } 1117 }
1118 if (time_updated) 1118 if (time_updated)
1119 should_notify_time_changed_ = true; 1119 should_notify_time_changed_ = true;
1120 1120
1121 // Reset underflow count upon seek; this prevents looping videos and user 1121 // Reset underflow count upon seek; this prevents looping videos and user
1122 // actions from artificially inflating the underflow count. 1122 // actions from artificially inflating the underflow count.
1123 underflow_count_ = 0; 1123 underflow_count_ = 0;
1124
1125 // Background video optimizations are delayed when shown/hidden if pipeline
1126 // is seeking.
1127 UpdateBackgroundVideoOptimizationState();
1124 } 1128 }
1125 1129
1126 void WebMediaPlayerImpl::OnPipelineSuspended() { 1130 void WebMediaPlayerImpl::OnPipelineSuspended() {
1127 #if defined(OS_ANDROID) 1131 #if defined(OS_ANDROID)
1128 if (isRemote()) { 1132 if (isRemote()) {
1129 scoped_refptr<VideoFrame> frame = cast_impl_.GetCastingBanner(); 1133 scoped_refptr<VideoFrame> frame = cast_impl_.GetCastingBanner();
1130 if (frame) 1134 if (frame)
1131 compositor_->PaintSingleFrame(frame); 1135 compositor_->PaintSingleFrame(frame);
1132 } 1136 }
1133 #endif 1137 #endif
(...skipping 19 matching lines...) Expand all
1153 // renderer will attach its callbacks to the video stream properly. 1157 // renderer will attach its callbacks to the video stream properly.
1154 // TODO(avayvod): Remove this when disabling and enabling video tracks in 1158 // TODO(avayvod): Remove this when disabling and enabling video tracks in
1155 // non-playing state works correctly. See https://crbug.com/678374. 1159 // non-playing state works correctly. See https://crbug.com/678374.
1156 EnableVideoTrackIfNeeded(); 1160 EnableVideoTrackIfNeeded();
1157 is_pipeline_resuming_ = true; 1161 is_pipeline_resuming_ = true;
1158 } 1162 }
1159 1163
1160 void WebMediaPlayerImpl::OnPipelineResumed() { 1164 void WebMediaPlayerImpl::OnPipelineResumed() {
1161 is_pipeline_resuming_ = false; 1165 is_pipeline_resuming_ = false;
1162 1166
1163 if (IsHidden()) { 1167 UpdateBackgroundVideoOptimizationState();
1164 DisableVideoTrackIfNeeded();
1165 } else {
1166 EnableVideoTrackIfNeeded();
1167 }
1168 } 1168 }
1169 1169
1170 void WebMediaPlayerImpl::OnDemuxerOpened() { 1170 void WebMediaPlayerImpl::OnDemuxerOpened() {
1171 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1171 DCHECK(main_task_runner_->BelongsToCurrentThread());
1172 client_->mediaSourceOpened( 1172 client_->mediaSourceOpened(
1173 new WebMediaSourceImpl(chunk_demuxer_, media_log_)); 1173 new WebMediaSourceImpl(chunk_demuxer_, media_log_));
1174 } 1174 }
1175 1175
1176 void WebMediaPlayerImpl::OnError(PipelineStatus status) { 1176 void WebMediaPlayerImpl::OnError(PipelineStatus status) {
1177 DVLOG(1) << __func__; 1177 DVLOG(1) << __func__;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
1403 if (video_weblayer_) 1403 if (video_weblayer_)
1404 video_weblayer_->layer()->SetContentsOpaque(opaque_); 1404 video_weblayer_->layer()->SetContentsOpaque(opaque_);
1405 } 1405 }
1406 1406
1407 void WebMediaPlayerImpl::OnFrameHidden() { 1407 void WebMediaPlayerImpl::OnFrameHidden() {
1408 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1408 DCHECK(main_task_runner_->BelongsToCurrentThread());
1409 1409
1410 if (watch_time_reporter_) 1410 if (watch_time_reporter_)
1411 watch_time_reporter_->OnHidden(); 1411 watch_time_reporter_->OnHidden();
1412 1412
1413 if (ShouldPauseVideoWhenHidden()) { 1413 if (ShouldPauseVideoWhenHidden()) {
sandersd (OOO until July 31) 2017/01/20 22:57:00 Should just call UpdateBackgroundVideoOptimization
whywhat 2017/01/20 23:24:45 Hm, here we already know we're hidden. We also wan
sandersd (OOO until July 31) 2017/01/20 23:29:42 Ah I see, I missed the early return when comparing
1414 if (!paused_when_hidden_) { 1414 PauseVideoIfNeeded();
1415 // OnPause() will set |paused_when_hidden_| to false and call 1415 return;
1416 // UpdatePlayState(), so set the flag to true after and then return.
1417 OnPause();
1418 paused_when_hidden_ = true;
1419 return;
1420 }
1421 } else { 1416 } else {
1422 DisableVideoTrackIfNeeded(); 1417 DisableVideoTrackIfNeeded();
1423 } 1418 }
1424 1419
1425 UpdatePlayState(); 1420 UpdatePlayState();
1426 1421
1427 // Schedule suspended playing media to be paused if the user doesn't come back 1422 // Schedule suspended playing media to be paused if the user doesn't come back
1428 // to it within some timeout period to avoid any autoplay surprises. 1423 // to it within some timeout period to avoid any autoplay surprises.
1429 ScheduleIdlePauseTimer(); 1424 ScheduleIdlePauseTimer();
1430 } 1425 }
(...skipping 744 matching lines...) Expand 10 before | Expand all | Expand 10 after
2175 base::TimeDelta duration = GetPipelineMediaDuration(); 2170 base::TimeDelta duration = GetPipelineMediaDuration();
2176 if (duration < max_keyframe_distance_to_disable_background_video_) 2171 if (duration < max_keyframe_distance_to_disable_background_video_)
2177 return true; 2172 return true;
2178 2173
2179 // Otherwise, only optimize videos with shorter average keyframe distance. 2174 // Otherwise, only optimize videos with shorter average keyframe distance.
2180 PipelineStatistics stats = GetPipelineStatistics(); 2175 PipelineStatistics stats = GetPipelineStatistics();
2181 return stats.video_keyframe_distance_average < 2176 return stats.video_keyframe_distance_average <
2182 max_keyframe_distance_to_disable_background_video_; 2177 max_keyframe_distance_to_disable_background_video_;
2183 } 2178 }
2184 2179
2180 void WebMediaPlayerImpl::UpdateBackgroundVideoOptimizationState() {
2181 if (IsHidden()) {
2182 if (ShouldPauseVideoWhenHidden())
2183 PauseVideoIfNeeded();
2184 else
2185 DisableVideoTrackIfNeeded();
2186 } else {
2187 EnableVideoTrackIfNeeded();
2188 }
2189 }
2190
2191 void WebMediaPlayerImpl::PauseVideoIfNeeded() {
2192 DCHECK(IsHidden());
2193
2194 // Don't pause video while the pipeline is stopped, resuming or seeking.
2195 // Also if the video is paused already.
2196 if (!pipeline_.IsRunning() || is_pipeline_resuming_ || seeking_ || paused_)
2197 return;
2198
2199 // OnPause() will set |paused_when_hidden_| to false and call
2200 // UpdatePlayState(), so set the flag to true after and then return.
2201 OnPause();
2202 paused_when_hidden_ = true;
2203 }
2204
2185 void WebMediaPlayerImpl::EnableVideoTrackIfNeeded() { 2205 void WebMediaPlayerImpl::EnableVideoTrackIfNeeded() {
2186 // Don't change video track while the pipeline is resuming or seeking. 2206 // Don't change video track while the pipeline is stopped, resuming or
2187 if (is_pipeline_resuming_ || seeking_) 2207 // seeking.
2208 if (!pipeline_.IsRunning() || is_pipeline_resuming_ || seeking_)
2188 return; 2209 return;
2189 2210
2190 if (video_track_disabled_) { 2211 if (video_track_disabled_) {
2191 video_track_disabled_ = false; 2212 video_track_disabled_ = false;
2192 if (client_->hasSelectedVideoTrack()) { 2213 if (client_->hasSelectedVideoTrack()) {
2193 WebMediaPlayer::TrackId trackId = client_->getSelectedVideoTrackId(); 2214 WebMediaPlayer::TrackId trackId = client_->getSelectedVideoTrackId();
2194 selectedVideoTrackChanged(&trackId); 2215 selectedVideoTrackChanged(&trackId);
2195 } 2216 }
2196 } 2217 }
2197 } 2218 }
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
2240 UMA_HISTOGRAM_TIMES( 2261 UMA_HISTOGRAM_TIMES(
2241 "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack", 2262 "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack",
2242 time_to_first_frame); 2263 time_to_first_frame);
2243 } else { 2264 } else {
2244 UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused", 2265 UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused",
2245 time_to_first_frame); 2266 time_to_first_frame);
2246 } 2267 }
2247 } 2268 }
2248 2269
2249 } // namespace media 2270 } // namespace media
OLDNEW
« no previous file with comments | « media/blink/webmediaplayer_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698