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

Side by Side Diff: content/renderer/media/android/webmediaplayer_android.cc

Issue 2490783002: Refactor WebMediaPlayerDelegate interface. (Closed)
Patch Set: Unit tests found a real bug! 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
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 "content/renderer/media/android/webmediaplayer_android.h" 5 #include "content/renderer/media/android/webmediaplayer_android.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <limits> 9 #include <limits>
10 10
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 enable_texture_copy_(enable_texture_copy), 175 enable_texture_copy_(enable_texture_copy),
176 suppress_deleting_texture_(false), 176 suppress_deleting_texture_(false),
177 playback_completed_(false), 177 playback_completed_(false),
178 volume_(1.0), 178 volume_(1.0),
179 volume_multiplier_(1.0), 179 volume_multiplier_(1.0),
180 weak_factory_(this) { 180 weak_factory_(this) {
181 DCHECK(player_manager_); 181 DCHECK(player_manager_);
182 182
183 DCHECK(main_thread_checker_.CalledOnValidThread()); 183 DCHECK(main_thread_checker_.CalledOnValidThread());
184 184
185 if (delegate_) 185 if (delegate_) {
186 delegate_id_ = delegate_->AddObserver(this); 186 delegate_id_ = delegate_->AddObserver(this);
187 delegate_->SetIdle(delegate_id_, true);
188 }
187 189
188 player_id_ = player_manager_->RegisterMediaPlayer(this); 190 player_id_ = player_manager_->RegisterMediaPlayer(this);
189 191
190 TryCreateStreamTextureProxyIfNeeded(); 192 TryCreateStreamTextureProxyIfNeeded();
191 interpolator_.SetUpperBound(base::TimeDelta()); 193 interpolator_.SetUpperBound(base::TimeDelta());
192 } 194 }
193 195
194 WebMediaPlayerAndroid::~WebMediaPlayerAndroid() { 196 WebMediaPlayerAndroid::~WebMediaPlayerAndroid() {
195 DCHECK(main_thread_checker_.CalledOnValidThread()); 197 DCHECK(main_thread_checker_.CalledOnValidThread());
196 SetVideoFrameProviderClient(NULL); 198 SetVideoFrameProviderClient(NULL);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 return false; 290 return false;
289 } 291 }
290 292
291 void WebMediaPlayerAndroid::play() { 293 void WebMediaPlayerAndroid::play() {
292 DCHECK(main_thread_checker_.CalledOnValidThread()); 294 DCHECK(main_thread_checker_.CalledOnValidThread());
293 295
294 if (hasVideo() && player_manager_->render_frame()->IsHidden()) { 296 if (hasVideo() && player_manager_->render_frame()->IsHidden()) {
295 bool can_video_play_in_background = 297 bool can_video_play_in_background =
296 base::CommandLine::ForCurrentProcess()->HasSwitch( 298 base::CommandLine::ForCurrentProcess()->HasSwitch(
297 switches::kDisableMediaSuspend) || 299 switches::kDisableMediaSuspend) ||
298 (IsBackgroundVideoCandidate() && 300 (IsBackgroundVideoCandidate() && delegate_ &&
299 delegate_ && delegate_->IsPlayingBackgroundVideo()); 301 delegate_->IsBackgroundVideoPlaybackUnlocked());
300 if (!can_video_play_in_background) { 302 if (!can_video_play_in_background) {
301 is_play_pending_ = true; 303 is_play_pending_ = true;
302 return; 304 return;
303 } 305 }
304 } 306 }
305 is_play_pending_ = false; 307 is_play_pending_ = false;
306 308
307 TryCreateStreamTextureProxyIfNeeded(); 309 TryCreateStreamTextureProxyIfNeeded();
308 // There is no need to establish the surface texture peer for fullscreen 310 // There is no need to establish the surface texture peer for fullscreen
309 // video. 311 // video.
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
821 823
822 // Lazily allocate compositing layer. 824 // Lazily allocate compositing layer.
823 if (!video_weblayer_) { 825 if (!video_weblayer_) {
824 video_weblayer_.reset(new cc_blink::WebLayerImpl( 826 video_weblayer_.reset(new cc_blink::WebLayerImpl(
825 cc::VideoLayer::Create(this, media::VIDEO_ROTATION_0))); 827 cc::VideoLayer::Create(this, media::VIDEO_ROTATION_0)));
826 client_->setWebLayer(video_weblayer_.get()); 828 client_->setWebLayer(video_weblayer_.get());
827 829
828 // If we're paused after we receive metadata for the first time, tell the 830 // If we're paused after we receive metadata for the first time, tell the
829 // delegate we can now be safely suspended due to inactivity if a subsequent 831 // delegate we can now be safely suspended due to inactivity if a subsequent
830 // play event does not occur. 832 // play event does not occur.
831 if (paused() && delegate_) 833 if (paused() && delegate_) {
832 delegate_->DidPause(delegate_id_, false); 834 delegate_->DidPause(delegate_id_);
835 delegate_->SetIdle(delegate_id_, true);
836 }
833 } 837 }
834 } 838 }
835 839
836 void WebMediaPlayerAndroid::OnTimeUpdate(base::TimeDelta current_timestamp, 840 void WebMediaPlayerAndroid::OnTimeUpdate(base::TimeDelta current_timestamp,
837 base::TimeTicks current_time_ticks) { 841 base::TimeTicks current_time_ticks) {
838 DCHECK(main_thread_checker_.CalledOnValidThread()); 842 DCHECK(main_thread_checker_.CalledOnValidThread());
839 843
840 if (seeking()) 844 if (seeking())
841 return; 845 return;
842 846
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
1197 interpolator_.StartInterpolating(); 1201 interpolator_.StartInterpolating();
1198 else 1202 else
1199 interpolator_.StopInterpolating(); 1203 interpolator_.StopInterpolating();
1200 1204
1201 if (delegate_) { 1205 if (delegate_) {
1202 if (is_playing) { 1206 if (is_playing) {
1203 // We must specify either video or audio to the delegate, but neither may 1207 // We must specify either video or audio to the delegate, but neither may
1204 // be known at this point -- there are no video only containers, so only 1208 // be known at this point -- there are no video only containers, so only
1205 // send audio if we know for sure its audio. The browser side player will 1209 // send audio if we know for sure its audio. The browser side player will
1206 // fill in the correct value later for media sessions. 1210 // fill in the correct value later for media sessions.
1207 delegate_->DidPlay(delegate_id_, hasVideo(), !hasVideo(), isRemote(), 1211 if (isRemote()) {
1208 media::DurationToMediaContentType(duration_)); 1212 delegate_->PlayerGone(delegate_id_);
1213 } else {
1214 delegate_->DidPlay(delegate_id_, hasVideo(), !hasVideo(),
1215 media::DurationToMediaContentType(duration_));
1216 }
1217 delegate_->SetIdle(delegate_id_, false);
1209 } else { 1218 } else {
1210 // Even if OnPlaybackComplete() has not been called yet, Blink may have 1219 // Even if OnPlaybackComplete() has not been called yet, Blink may have
1211 // already fired the ended event based on current time relative to 1220 // already fired the ended event based on current time relative to
1212 // duration -- so we need to check both possibilities here. 1221 // duration -- so we need to check both possibilities here.
1213 delegate_->DidPause(delegate_id_, 1222 if (playback_completed_ || currentTime() >= duration()) {
1214 playback_completed_ || currentTime() >= duration()); 1223 delegate_->PlayerGone(delegate_id_);
1224 } else {
1225 delegate_->DidPause(delegate_id_);
1226 }
1227 delegate_->SetIdle(delegate_id_, true);
1215 } 1228 }
1216 } 1229 }
1217 } 1230 }
1218 1231
1219 void WebMediaPlayerAndroid::OnHidden() { 1232 void WebMediaPlayerAndroid::OnFrameHidden() {
1220 // Pause audible video preserving its session. 1233 // Pause audible video preserving its session.
1221 if (hasVideo() && IsBackgroundVideoCandidate() && !paused()) { 1234 if (hasVideo() && IsBackgroundVideoCandidate() && !paused()) {
1222 Pause(false); 1235 Pause(false);
1223 is_play_pending_ = true; 1236 is_play_pending_ = true;
1224 return; 1237 return;
1225 } 1238 }
1226 1239
1227 OnSuspendRequested(false); 1240 OnIdleTimeout();
1228 } 1241 }
1229 1242
1230 void WebMediaPlayerAndroid::OnShown() { 1243 void WebMediaPlayerAndroid::OnFrameClosed() {
1244 SuspendAndReleaseResources();
1245 }
1246
1247 void WebMediaPlayerAndroid::OnFrameShown() {
1231 if (is_play_pending_) 1248 if (is_play_pending_)
1232 play(); 1249 play();
1233 } 1250 }
1234 1251
1235 bool WebMediaPlayerAndroid::OnSuspendRequested(bool must_suspend) { 1252 void WebMediaPlayerAndroid::OnIdleTimeout() {
1236 if (!must_suspend && 1253 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1237 base::CommandLine::ForCurrentProcess()->HasSwitch(
1238 switches::kDisableMediaSuspend)) { 1254 switches::kDisableMediaSuspend)) {
1239 return true; 1255 return;
1240 } 1256 }
1241 1257
1242 // If we're idle or playing video, pause and release resources; audio only 1258 // If we're playing video or ended, pause and release resources; audio only
1243 // players are allowed to continue unless indicated otherwise by the call. 1259 // players are allowed to continue.
1244 if (must_suspend || (paused() && playback_completed_) || 1260 if ((hasVideo() && !IsBackgroundVideoCandidate()) ||
1245 (hasVideo() && !IsBackgroundVideoCandidate())) { 1261 (paused() && playback_completed_)) {
1246 SuspendAndReleaseResources(); 1262 SuspendAndReleaseResources();
1247 } 1263 }
1248
1249 return true;
1250 } 1264 }
1251 1265
1252 void WebMediaPlayerAndroid::OnPlay() { 1266 void WebMediaPlayerAndroid::OnPlay() {
1253 play(); 1267 play();
1254 client_->playbackStateChanged(); 1268 client_->playbackStateChanged();
1255 } 1269 }
1256 1270
1257 void WebMediaPlayerAndroid::OnPause() { 1271 void WebMediaPlayerAndroid::OnPause() {
1258 pause(); 1272 pause();
1259 client_->playbackStateChanged(); 1273 client_->playbackStateChanged();
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1320 1334
1321 bool WebMediaPlayerAndroid::IsBackgroundVideoCandidate() const { 1335 bool WebMediaPlayerAndroid::IsBackgroundVideoCandidate() const {
1322 DCHECK(hasVideo()); 1336 DCHECK(hasVideo());
1323 1337
1324 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 1338 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1325 switches::kDisableMediaSuspend)) { 1339 switches::kDisableMediaSuspend)) {
1326 return false; 1340 return false;
1327 } 1341 }
1328 1342
1329 return base::FeatureList::IsEnabled(media::kResumeBackgroundVideo) && 1343 return base::FeatureList::IsEnabled(media::kResumeBackgroundVideo) &&
1330 hasAudio() && !isRemote() && delegate_ && delegate_->IsHidden(); 1344 hasAudio() && !isRemote() && delegate_ && delegate_->IsFrameHidden() &&
1345 !delegate_->IsFrameClosed();
1331 } 1346 }
1332 1347
1333 } // namespace content 1348 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/android/webmediaplayer_android.h ('k') | content/renderer/media/renderer_webmediaplayer_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698