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

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

Issue 2039793005: Don't resume paused media. Don't resume playing media after timeout. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reimplement. Add tests. Created 4 years, 6 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') | media/blink/webmediaplayer_impl_unittest.cc » ('j') | 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 1095 matching lines...) Expand 10 before | Expand all | Expand 10 after
1106 opaque_ = opaque; 1106 opaque_ = opaque;
1107 // Modify content opaqueness of cc::Layer directly so that 1107 // Modify content opaqueness of cc::Layer directly so that
1108 // SetContentsOpaqueIsFixed is ignored. 1108 // SetContentsOpaqueIsFixed is ignored.
1109 if (video_weblayer_) 1109 if (video_weblayer_)
1110 video_weblayer_->layer()->SetContentsOpaque(opaque_); 1110 video_weblayer_->layer()->SetContentsOpaque(opaque_);
1111 } 1111 }
1112 1112
1113 void WebMediaPlayerImpl::OnHidden() { 1113 void WebMediaPlayerImpl::OnHidden() {
1114 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1114 DCHECK(main_task_runner_->BelongsToCurrentThread());
1115 UpdatePlayState(); 1115 UpdatePlayState();
1116
1117 // If the user doesn't come back to the suspended media in some time (chosen
1118 // by dice roll) then pause the media so the user isn't surprised later.
1119 if (pipeline_controller_.IsSuspended() && !paused_) {
sandersd (OOO until July 31) 2016/06/08 23:16:27 We shouldn't schedule this while casting either. P
DaleCurtis 2016/06/10 00:02:11 Done. I don't understand your concerns about HAVE_
1120 background_pause_timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(5),
1121 this, &WebMediaPlayerImpl::OnPause);
1122 }
1116 } 1123 }
1117 1124
1118 void WebMediaPlayerImpl::OnShown() { 1125 void WebMediaPlayerImpl::OnShown() {
1119 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1126 DCHECK(main_task_runner_->BelongsToCurrentThread());
1120 must_suspend_ = false; 1127 must_suspend_ = false;
1128 background_pause_timer_.Stop();
1121 UpdatePlayState(); 1129 UpdatePlayState();
1122 } 1130 }
1123 1131
1124 void WebMediaPlayerImpl::OnSuspendRequested(bool must_suspend) { 1132 void WebMediaPlayerImpl::OnSuspendRequested(bool must_suspend) {
1125 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1133 DCHECK(main_task_runner_->BelongsToCurrentThread());
1126 1134
1127 if (must_suspend) { 1135 if (must_suspend) {
1128 must_suspend_ = true; 1136 must_suspend_ = true;
1129 } else { 1137 } else {
1130 // TODO(sandersd): Remove this when idleness is separate from play state. 1138 // TODO(sandersd): Remove this when idleness is separate from play state.
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
1401 event.Wait(); 1409 event.Wait();
1402 return video_frame; 1410 return video_frame;
1403 } 1411 }
1404 1412
1405 void WebMediaPlayerImpl::UpdatePlayState() { 1413 void WebMediaPlayerImpl::UpdatePlayState() {
1406 #if defined(OS_ANDROID) // WMPI_CAST 1414 #if defined(OS_ANDROID) // WMPI_CAST
1407 bool is_remote = isRemote(); 1415 bool is_remote = isRemote();
1408 #else 1416 #else
1409 bool is_remote = false; 1417 bool is_remote = false;
1410 #endif 1418 #endif
1419 bool is_suspended = pipeline_controller_.IsSuspended();
1411 bool is_backgrounded = 1420 bool is_backgrounded =
1412 IsBackgroundedSuspendEnabled() && delegate_ && delegate_->IsHidden(); 1421 IsBackgroundedSuspendEnabled() && delegate_ && delegate_->IsHidden();
1413 PlayState state = 1422 PlayState state = UpdatePlayState_ComputePlayState(is_remote, is_suspended,
1414 UpdatePlayState_ComputePlayState(is_remote, is_backgrounded); 1423 is_backgrounded);
1415 SetDelegateState(state.delegate_state); 1424 SetDelegateState(state.delegate_state);
1416 SetMemoryReportingState(state.is_memory_reporting_enabled); 1425 SetMemoryReportingState(state.is_memory_reporting_enabled);
1417 SetSuspendState(state.is_suspended || pending_suspend_resume_cycle_); 1426 SetSuspendState(state.is_suspended || pending_suspend_resume_cycle_);
1418 } 1427 }
1419 1428
1420 void WebMediaPlayerImpl::SetDelegateState(DelegateState new_state) { 1429 void WebMediaPlayerImpl::SetDelegateState(DelegateState new_state) {
1421 if (!delegate_ || delegate_state_ == new_state) 1430 if (!delegate_ || delegate_state_ == new_state)
1422 return; 1431 return;
1423 1432
1424 delegate_state_ = new_state; 1433 delegate_state_ = new_state;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 1504
1496 if (is_suspended) { 1505 if (is_suspended) {
1497 pipeline_controller_.Suspend(); 1506 pipeline_controller_.Suspend();
1498 } else { 1507 } else {
1499 pipeline_controller_.Resume(); 1508 pipeline_controller_.Resume();
1500 } 1509 }
1501 } 1510 }
1502 1511
1503 WebMediaPlayerImpl::PlayState 1512 WebMediaPlayerImpl::PlayState
1504 WebMediaPlayerImpl::UpdatePlayState_ComputePlayState(bool is_remote, 1513 WebMediaPlayerImpl::UpdatePlayState_ComputePlayState(bool is_remote,
1514 bool is_suspended,
1505 bool is_backgrounded) { 1515 bool is_backgrounded) {
1506 PlayState result; 1516 PlayState result;
1507 1517
1508 // This includes both data source (before pipeline startup) and pipeline 1518 // This includes both data source (before pipeline startup) and pipeline
1509 // errors. 1519 // errors.
1510 bool has_error = IsNetworkStateError(network_state_); 1520 bool has_error = IsNetworkStateError(network_state_);
1511 1521
1512 // After HaveMetadata, we know which tracks are present and the duration. 1522 // After HaveMetadata, we know which tracks are present and the duration.
1513 bool have_metadata = ready_state_ >= WebMediaPlayer::ReadyStateHaveMetadata; 1523 bool have_metadata = ready_state_ >= WebMediaPlayer::ReadyStateHaveMetadata;
1514 1524
1515 // After HaveFutureData, Blink will call play() if the state is not paused. 1525 // After HaveFutureData, Blink will call play() if the state is not paused.
1516 bool have_future_data = 1526 bool have_future_data =
1517 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData; 1527 highest_ready_state_ >= WebMediaPlayer::ReadyStateHaveFutureData;
1518 1528
1519 // Background suspend is not enabled for audio-only players. 1529 // Background suspend is not enabled for audio-only players unless paused,
1530 // though in the case of audio-only the session should be kept.
1520 bool background_suspended = is_backgrounded && have_metadata && hasVideo(); 1531 bool background_suspended = is_backgrounded && have_metadata && hasVideo();
1532 bool background_audio_suspended = is_backgrounded && have_metadata && paused_;
sandersd (OOO until July 31) 2016/06/08 23:16:27 Should be |have_future_data| as a prerequisite for
DaleCurtis 2016/06/10 00:02:11 That's not what will happen since background_audio
1521 1533
1522 // Idle suspend is enabled once there is future data. We don't want to idle 1534 // Idle suspend is enabled once there is future data. We don't want to idle
1523 // suspend before that because play() may never be triggered to leave the idle 1535 // suspend before that because play() may never be triggered to leave the idle
1524 // state. There could be other theoretical problems if the page is waiting for 1536 // state. There could be other theoretical problems if the page is waiting for
1525 // other events before actually calling play(), but at least we don't break 1537 // other events before actually calling play(), but at least we don't break
1526 // Blink. 1538 // Blink.
1527 // 1539 //
1528 // TODO(sandersd): Make the delegate suspend idle players immediately when 1540 // TODO(sandersd): Make the delegate suspend idle players immediately when
1529 // hidden. 1541 // hidden.
1530 // TODO(sandersd): If Blink told us the paused state sooner, we could 1542 // TODO(sandersd): If Blink told us the paused state sooner, we could
1531 // idle suspend sooner. 1543 // idle suspend sooner.
1532 bool idle_suspended = is_idle_ && have_future_data; 1544 bool idle_suspended = is_idle_ && have_future_data;
1533 1545
1534 // Combined suspend state. 1546 // Combined suspend state.
1535 result.is_suspended = 1547 result.is_suspended = is_remote || must_suspend_ || idle_suspended ||
1536 is_remote || must_suspend_ || idle_suspended || background_suspended; 1548 background_suspended || background_audio_suspended;
1549
1550 // If we're already suspended and the target state is unsuspended, check to
1551 // see if we really must resume right now or can just wait for user action.
1552 if (is_suspended && !result.is_suspended && paused_)
sandersd (OOO until July 31) 2016/06/08 23:16:27 Better to move all the conditions up so that the a
DaleCurtis 2016/06/10 00:02:11 I prefer the current phrasing for readability, but
sandersd (OOO until July 31) 2016/06/10 01:15:09 I feel strongly because the this version doesn't m
DaleCurtis 2016/06/10 18:20:31 Done.
1553 result.is_suspended = !seeking_ && have_future_data;
1537 1554
1538 // We do not treat |playback_rate_| == 0 as paused. For the media session, 1555 // We do not treat |playback_rate_| == 0 as paused. For the media session,
1539 // being paused implies displaying a play button, which is incorrect in this 1556 // being paused implies displaying a play button, which is incorrect in this
1540 // case. For memory usage reporting, we just use the same definition (but we 1557 // case. For memory usage reporting, we just use the same definition (but we
1541 // don't have to). 1558 // don't have to).
1542 // 1559 //
1543 // Similarly, we don't consider |ended_| to be paused. Blink will immediately 1560 // Similarly, we don't consider |ended_| to be paused. Blink will immediately
1544 // call pause() or seek(), so |ended_| should not affect the computation. 1561 // call pause() or seek(), so |ended_| should not affect the computation.
1545 // Despite that, |ended_| does result in a separate paused state, to simplfy 1562 // Despite that, |ended_| does result in a separate paused state, to simplfy
1546 // the contract for SetDelegateState(). 1563 // the contract for SetDelegateState().
(...skipping 17 matching lines...) Expand all
1564 result.delegate_state = DelegateState::PAUSED_BUT_NOT_IDLE; 1581 result.delegate_state = DelegateState::PAUSED_BUT_NOT_IDLE;
1565 } else if (ended_) { 1582 } else if (ended_) {
1566 result.delegate_state = DelegateState::ENDED; 1583 result.delegate_state = DelegateState::ENDED;
1567 } else { 1584 } else {
1568 result.delegate_state = DelegateState::PAUSED; 1585 result.delegate_state = DelegateState::PAUSED;
1569 } 1586 }
1570 } else { 1587 } else {
1571 result.delegate_state = DelegateState::PLAYING; 1588 result.delegate_state = DelegateState::PLAYING;
1572 } 1589 }
1573 1590
1574 // It's not critical if some cases where memory usage can change are missed, 1591 // It's not critical if some cases where memory usage can change are missed,
1575 // since media memory changes are usually gradual. 1592 // since media memory changes are usually gradual.
1576 result.is_memory_reporting_enabled = 1593 result.is_memory_reporting_enabled =
1577 can_play && !result.is_suspended && !paused_; 1594 can_play && !result.is_suspended && !paused_;
1578 1595
1579 return result; 1596 return result;
1580 } 1597 }
1581 1598
1582 void WebMediaPlayerImpl::ReportMemoryUsage() { 1599 void WebMediaPlayerImpl::ReportMemoryUsage() {
1583 DCHECK(main_task_runner_->BelongsToCurrentThread()); 1600 DCHECK(main_task_runner_->BelongsToCurrentThread());
1584 1601
(...skipping 28 matching lines...) Expand all
1613 << ", Video: " << stats.video_memory_usage << ", DataSource: " 1630 << ", Video: " << stats.video_memory_usage << ", DataSource: "
1614 << (data_source_ ? data_source_->GetMemoryUsage() : 0) 1631 << (data_source_ ? data_source_->GetMemoryUsage() : 0)
1615 << ", Demuxer: " << demuxer_memory_usage; 1632 << ", Demuxer: " << demuxer_memory_usage;
1616 1633
1617 const int64_t delta = current_memory_usage - last_reported_memory_usage_; 1634 const int64_t delta = current_memory_usage - last_reported_memory_usage_;
1618 last_reported_memory_usage_ = current_memory_usage; 1635 last_reported_memory_usage_ = current_memory_usage;
1619 adjust_allocated_memory_cb_.Run(delta); 1636 adjust_allocated_memory_cb_.Run(delta);
1620 } 1637 }
1621 1638
1622 } // namespace media 1639 } // namespace media
OLDNEW
« no previous file with comments | « media/blink/webmediaplayer_impl.h ('k') | media/blink/webmediaplayer_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698