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

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

Issue 2681863005: [Video] MediaSession API event handlers can resume background video. (Closed)
Patch Set: Restore permanent 5s pause. 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 #include "third_party/WebKit/public/platform/WebMediaPlayerSource.h" 56 #include "third_party/WebKit/public/platform/WebMediaPlayerSource.h"
57 #include "third_party/WebKit/public/platform/WebMediaSource.h" 57 #include "third_party/WebKit/public/platform/WebMediaSource.h"
58 #include "third_party/WebKit/public/platform/WebRect.h" 58 #include "third_party/WebKit/public/platform/WebRect.h"
59 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" 59 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
60 #include "third_party/WebKit/public/platform/WebSize.h" 60 #include "third_party/WebKit/public/platform/WebSize.h"
61 #include "third_party/WebKit/public/platform/WebString.h" 61 #include "third_party/WebKit/public/platform/WebString.h"
62 #include "third_party/WebKit/public/platform/WebURL.h" 62 #include "third_party/WebKit/public/platform/WebURL.h"
63 #include "third_party/WebKit/public/web/WebDocument.h" 63 #include "third_party/WebKit/public/web/WebDocument.h"
64 #include "third_party/WebKit/public/web/WebFrame.h" 64 #include "third_party/WebKit/public/web/WebFrame.h"
65 #include "third_party/WebKit/public/web/WebLocalFrame.h" 65 #include "third_party/WebKit/public/web/WebLocalFrame.h"
66 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
66 #include "third_party/WebKit/public/web/WebView.h" 67 #include "third_party/WebKit/public/web/WebView.h"
67 68
68 #if defined(OS_ANDROID) 69 #if defined(OS_ANDROID)
69 #include "media/base/android/media_codec_util.h" 70 #include "media/base/android/media_codec_util.h"
70 #endif 71 #endif
71 72
72 using blink::WebCanvas; 73 using blink::WebCanvas;
73 using blink::WebMediaPlayer; 74 using blink::WebMediaPlayer;
74 using blink::WebRect; 75 using blink::WebRect;
75 using blink::WebSize; 76 using blink::WebSize;
(...skipping 1879 matching lines...) Expand 10 before | Expand all | Expand 10 after
1955 bool have_future_data = 1956 bool have_future_data =
1956 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData; 1957 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData;
1957 1958
1958 // Background suspend is not enabled for audio-only players unless paused, 1959 // Background suspend is not enabled for audio-only players unless paused,
1959 // though in the case of audio-only the session should be kept. 1960 // though in the case of audio-only the session should be kept.
1960 // Videos are not suspended if the user resumed the playback via the remote 1961 // Videos are not suspended if the user resumed the playback via the remote
1961 // controls earlier and it's still playing. 1962 // controls earlier and it's still playing.
1962 bool is_backgrounded_video = is_backgrounded && have_metadata && hasVideo(); 1963 bool is_backgrounded_video = is_backgrounded && have_metadata && hasVideo();
1963 bool can_play_backgrounded = is_backgrounded_video && !is_remote && 1964 bool can_play_backgrounded = is_backgrounded_video && !is_remote &&
1964 hasAudio() && IsResumeBackgroundVideosEnabled(); 1965 hasAudio() && IsResumeBackgroundVideosEnabled();
1965 bool is_background_playing = delegate_->IsBackgroundVideoPlaybackUnlocked(); 1966 bool is_background_playing_allowed =
DaleCurtis 2017/02/13 22:44:54 Needs a comment explaining use of the user gesture
sandersd (OOO until July 31) 2017/02/13 22:56:20 Indeed; the entire design goal of this function is
whywhat 2017/02/14 01:36:28 Something like play_called_with_user_gesture_ set
whywhat 2017/02/15 17:13:12 I simplified the whole background logic in Compute
1966 bool background_suspended = !is_streaming && is_backgrounded_video && 1967 blink::WebUserGestureIndicator::isProcessingUserGesture() ||
1967 !(can_play_backgrounded && is_background_playing); 1968 delegate_->IsBackgroundVideoPlaybackUnlocked();
1969 bool background_suspended =
1970 !is_streaming && is_backgrounded_video &&
1971 !(can_play_backgrounded && is_background_playing_allowed);
1968 bool background_pause_suspended = 1972 bool background_pause_suspended =
1969 !is_streaming && is_backgrounded && paused_ && have_future_data; 1973 !is_streaming && is_backgrounded && paused_ && have_future_data;
1970 1974
1971 // Idle suspension is allowed prior to have future data since there exist 1975 // Idle suspension is allowed prior to have future data since there exist
1972 // mechanisms to exit the idle state when the player is capable of reaching 1976 // mechanisms to exit the idle state when the player is capable of reaching
1973 // the have future data state; see didLoadingProgress(). 1977 // the have future data state; see didLoadingProgress().
1974 // 1978 //
1975 // TODO(sandersd): Make the delegate suspend idle players immediately when 1979 // TODO(sandersd): Make the delegate suspend idle players immediately when
1976 // hidden. 1980 // hidden.
1977 bool idle_suspended = 1981 bool idle_suspended =
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2092 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource", 2096 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.DataSource",
2093 data_source_memory_usage / 1024); 2097 data_source_memory_usage / 1024);
2094 } 2098 }
2095 if (demuxer_) { 2099 if (demuxer_) {
2096 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer", 2100 UMA_HISTOGRAM_MEMORY_KB("Media.WebMediaPlayerImpl.Memory.Demuxer",
2097 demuxer_memory_usage / 1024); 2101 demuxer_memory_usage / 1024);
2098 } 2102 }
2099 } 2103 }
2100 2104
2101 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() { 2105 void WebMediaPlayerImpl::ScheduleIdlePauseTimer() {
2102 // Only schedule the pause timer if we're playing and are suspended. 2106 // Only schedule the pause timer if we're not paused or paused but going to
2103 if (paused_ || !pipeline_controller_.IsSuspended()) 2107 // resume when foregrounded, and are suspended.
2108 if ((paused_ && !paused_when_hidden_) || !pipeline_controller_.IsSuspended())
2104 return; 2109 return;
2105 2110
2106 #if defined(OS_ANDROID) 2111 #if defined(OS_ANDROID)
2107 // Remote players will be suspended and locally paused. 2112 // Remote players will be suspended and locally paused.
2108 if (isRemote()) 2113 if (isRemote())
2109 return; 2114 return;
2110 #endif 2115 #endif
2111 2116
2112 // Idle timeout chosen arbitrarily. 2117 // Idle timeout chosen arbitrarily.
2113 background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5), 2118 background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5),
(...skipping 27 matching lines...) Expand all
2141 return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0; 2146 return pipeline_metadata_.video_rotation == VIDEO_ROTATION_0;
2142 } 2147 }
2143 2148
2144 void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) { 2149 void WebMediaPlayerImpl::ActivateViewportIntersectionMonitoring(bool activate) {
2145 DCHECK(main_task_runner_->BelongsToCurrentThread()); 2150 DCHECK(main_task_runner_->BelongsToCurrentThread());
2146 2151
2147 client_->activateViewportIntersectionMonitoring(activate); 2152 client_->activateViewportIntersectionMonitoring(activate);
2148 } 2153 }
2149 2154
2150 bool WebMediaPlayerImpl::ShouldPauseVideoWhenHidden() const { 2155 bool WebMediaPlayerImpl::ShouldPauseVideoWhenHidden() const {
2151 #if !defined(OS_ANDROID) 2156 if (IsBackgroundedSuspendEnabled()) {
2152 // On desktop, this behavior is behind the feature flag. 2157 // If we intend to pause background videos, pause them as long as
2153 if (!IsBackgroundVideoTrackOptimizationEnabled()) 2158 // they're not remote or audible and unlocked for background
2154 return false; 2159 // playback.
2155 #endif 2160 return hasVideo() && !isRemote() &&
2161 (!hasAudio() || !delegate_->IsBackgroundVideoPlaybackUnlocked());
2162 }
2156 2163
2157 // Pause video-only players that match the criteria for being optimized. 2164 // Otherwise only pause if the optimization is on and it's a video-only
2158 return !hasAudio() && IsBackgroundOptimizationCandidate(); 2165 // optimization candidate.
2166 return IsBackgroundVideoTrackOptimizationEnabled() && !hasAudio() &&
2167 IsBackgroundOptimizationCandidate();
2159 } 2168 }
2160 2169
2161 bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const { 2170 bool WebMediaPlayerImpl::ShouldDisableVideoWhenHidden() const {
2162 // This optimization is behind the flag on all platforms. 2171 // This optimization is behind the flag on all platforms.
2163 if (!IsBackgroundVideoTrackOptimizationEnabled()) 2172 if (!IsBackgroundVideoTrackOptimizationEnabled())
2164 return false; 2173 return false;
2165 2174
2166 // Disable video track only for players with audio that match the criteria for 2175 // Disable video track only for players with audio that match the criteria for
2167 // being optimized. 2176 // being optimized.
2168 return hasAudio() && IsBackgroundOptimizationCandidate(); 2177 return hasAudio() && IsBackgroundOptimizationCandidate();
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
2281 UMA_HISTOGRAM_TIMES( 2290 UMA_HISTOGRAM_TIMES(
2282 "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack", 2291 "Media.Video.TimeFromForegroundToFirstFrame.DisableTrack",
2283 time_to_first_frame); 2292 time_to_first_frame);
2284 } else { 2293 } else {
2285 UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused", 2294 UMA_HISTOGRAM_TIMES("Media.Video.TimeFromForegroundToFirstFrame.Paused",
2286 time_to_first_frame); 2295 time_to_first_frame);
2287 } 2296 }
2288 } 2297 }
2289 2298
2290 } // namespace media 2299 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698