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 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 #endif | 340 #endif |
341 | 341 |
342 paused_ = false; | 342 paused_ = false; |
343 | 343 |
344 pipeline_.SetPlaybackRate(playback_rate_); | 344 pipeline_.SetPlaybackRate(playback_rate_); |
345 if (data_source_) | 345 if (data_source_) |
346 data_source_->MediaIsPlaying(); | 346 data_source_->MediaIsPlaying(); |
347 | 347 |
348 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY)); | 348 media_log_->AddEvent(media_log_->CreateEvent(MediaLogEvent::PLAY)); |
349 | 349 |
350 if (playback_rate_ > 0) | 350 if (playback_rate_ > 0) { |
| 351 // Resume the player if playback was initiated in the foreground. |
| 352 if (suspended_ && !resuming_ && delegate_ && !delegate_->IsHidden()) { |
| 353 Resume(); |
| 354 return; |
| 355 } |
| 356 |
351 NotifyPlaybackStarted(); | 357 NotifyPlaybackStarted(); |
| 358 } |
352 } | 359 } |
353 | 360 |
354 void WebMediaPlayerImpl::pause() { | 361 void WebMediaPlayerImpl::pause() { |
355 DVLOG(1) << __FUNCTION__; | 362 DVLOG(1) << __FUNCTION__; |
356 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 363 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
357 | 364 |
358 const bool was_already_paused = paused_ || playback_rate_ == 0; | 365 const bool was_already_paused = paused_ || playback_rate_ == 0; |
359 paused_ = true; | 366 paused_ = true; |
360 | 367 |
361 #if defined(OS_ANDROID) // WMPI_CAST | 368 #if defined(OS_ANDROID) // WMPI_CAST |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 | 437 |
431 // Schedule a seek once the current suspend or seek finishes. | 438 // Schedule a seek once the current suspend or seek finishes. |
432 pending_seek_ = true; | 439 pending_seek_ = true; |
433 pending_seek_time_ = new_seek_time; | 440 pending_seek_time_ = new_seek_time; |
434 | 441 |
435 // In the case of seeking while suspended, the seek is considered to have | 442 // In the case of seeking while suspended, the seek is considered to have |
436 // started immediately (but won't complete until the pipeline is resumed). | 443 // started immediately (but won't complete until the pipeline is resumed). |
437 if (is_suspended) { | 444 if (is_suspended) { |
438 seeking_ = true; | 445 seeking_ = true; |
439 seek_time_ = new_seek_time; | 446 seek_time_ = new_seek_time; |
| 447 |
| 448 // Resume the pipeline if the seek is initiated in the foreground so that |
| 449 // the correct frame is displayed. |
| 450 if (delegate_ && !delegate_->IsHidden()) |
| 451 Resume(); |
440 } | 452 } |
441 | 453 |
442 return; | 454 return; |
443 } | 455 } |
444 | 456 |
445 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); | 457 media_log_->AddEvent(media_log_->CreateSeekEvent(seconds)); |
446 | 458 |
447 // Update our paused time. | 459 // Update our paused time. |
448 // For non-MSE playbacks, in paused state ignore the seek operations to | 460 // For non-MSE playbacks, in paused state ignore the seek operations to |
449 // current time if the loading is completed and generate | 461 // current time if the loading is completed and generate |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
880 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed, | 892 void WebMediaPlayerImpl::OnPipelineSeeked(bool time_changed, |
881 PipelineStatus status) { | 893 PipelineStatus status) { |
882 DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")"; | 894 DVLOG(1) << __FUNCTION__ << "(" << time_changed << ", " << status << ")"; |
883 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 895 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
884 | 896 |
885 if (status != PIPELINE_OK) { | 897 if (status != PIPELINE_OK) { |
886 OnPipelineError(status); | 898 OnPipelineError(status); |
887 return; | 899 return; |
888 } | 900 } |
889 | 901 |
890 // If we we're resuming into the playing state, notify the delegate. | |
891 if (resuming_ && playback_rate_ > 0 && !paused_) | |
892 NotifyPlaybackStarted(); | |
893 | |
894 // Whether or not the seek was caused by a resume, we're not suspended now. | 902 // Whether or not the seek was caused by a resume, we're not suspended now. |
| 903 const bool was_resuming = resuming_; |
895 resuming_ = false; | 904 resuming_ = false; |
896 suspended_ = false; | 905 suspended_ = false; |
897 | 906 |
| 907 // If we we're resuming into the playing state, notify the delegate. |
| 908 if (was_resuming && playback_rate_ > 0 && !paused_) |
| 909 NotifyPlaybackStarted(); |
| 910 |
898 // If there is a pending suspend, the seek does not complete until after the | 911 // If there is a pending suspend, the seek does not complete until after the |
899 // next resume. | 912 // next resume. |
900 if (pending_suspend_) { | 913 if (pending_suspend_) { |
901 pending_suspend_ = false; | 914 pending_suspend_ = false; |
902 pending_time_change_ = time_changed; | 915 pending_time_change_ = time_changed; |
903 Suspend(); | 916 Suspend(); |
904 return; | 917 return; |
905 } | 918 } |
906 | 919 |
907 // Clear seek state. Note that if the seek was caused by a resume, then | 920 // Clear seek state. Note that if the seek was caused by a resume, then |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1144 switches::kDisableMediaSuspend)) { | 1157 switches::kDisableMediaSuspend)) { |
1145 return; | 1158 return; |
1146 } | 1159 } |
1147 | 1160 |
1148 #if defined(OS_ANDROID) | 1161 #if defined(OS_ANDROID) |
1149 // If we're remote, the pipeline should stay suspended. | 1162 // If we're remote, the pipeline should stay suspended. |
1150 if (isRemote()) | 1163 if (isRemote()) |
1151 return; | 1164 return; |
1152 #endif | 1165 #endif |
1153 | 1166 |
1154 ScheduleResume(); | 1167 if (!ended_ && !paused_) |
| 1168 ScheduleResume(); |
1155 } | 1169 } |
1156 | 1170 |
1157 void WebMediaPlayerImpl::ScheduleResume() { | 1171 void WebMediaPlayerImpl::ScheduleResume() { |
1158 if (!pipeline_.IsRunning()) | 1172 if (!pipeline_.IsRunning()) |
1159 return; | 1173 return; |
1160 | 1174 |
1161 if (suspending_) { | 1175 if (suspending_) { |
1162 pending_resume_ = true; | 1176 pending_resume_ = true; |
1163 return; | 1177 return; |
1164 } | 1178 } |
(...skipping 350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1515 // pause() may be called after playback has ended and the HTMLMediaElement | 1529 // pause() may be called after playback has ended and the HTMLMediaElement |
1516 // requires that currentTime() == duration() after ending. We want to ensure | 1530 // requires that currentTime() == duration() after ending. We want to ensure |
1517 // |paused_time_| matches currentTime() in this case or a future seek() may | 1531 // |paused_time_| matches currentTime() in this case or a future seek() may |
1518 // incorrectly discard what it thinks is a seek to the existing time. | 1532 // incorrectly discard what it thinks is a seek to the existing time. |
1519 paused_time_ = | 1533 paused_time_ = |
1520 ended_ ? pipeline_.GetMediaDuration() : pipeline_.GetMediaTime(); | 1534 ended_ ? pipeline_.GetMediaDuration() : pipeline_.GetMediaTime(); |
1521 } | 1535 } |
1522 | 1536 |
1523 void WebMediaPlayerImpl::NotifyPlaybackStarted() { | 1537 void WebMediaPlayerImpl::NotifyPlaybackStarted() { |
1524 #if defined(OS_ANDROID) // WMPI_CAST | 1538 #if defined(OS_ANDROID) // WMPI_CAST |
1525 // We do not tell our delegates about remote playback, becuase that would | 1539 // We do not tell our delegates about remote playback, because that would |
1526 // keep the device awake, which is not what we want. | 1540 // keep the device awake, which is not what we want. |
1527 if (isRemote()) | 1541 if (isRemote()) |
1528 return; | 1542 return; |
1529 #endif | 1543 #endif |
| 1544 |
| 1545 // Don't send delegate notifications when suspended; upon suspend we send |
| 1546 // PlayerGone() to the delegate -- no more notifications should be sent until |
| 1547 // after resume. |
| 1548 if (suspended_) |
| 1549 return; |
| 1550 |
1530 if (delegate_) { | 1551 if (delegate_) { |
1531 delegate_->DidPlay(delegate_id_, hasVideo(), hasAudio(), false, | 1552 delegate_->DidPlay(delegate_id_, hasVideo(), hasAudio(), false, |
1532 pipeline_.GetMediaDuration()); | 1553 pipeline_.GetMediaDuration()); |
1533 } | 1554 } |
1534 if (!memory_usage_reporting_timer_.IsRunning()) { | 1555 if (!memory_usage_reporting_timer_.IsRunning()) { |
1535 memory_usage_reporting_timer_.Start(FROM_HERE, | 1556 memory_usage_reporting_timer_.Start(FROM_HERE, |
1536 base::TimeDelta::FromSeconds(2), this, | 1557 base::TimeDelta::FromSeconds(2), this, |
1537 &WebMediaPlayerImpl::ReportMemoryUsage); | 1558 &WebMediaPlayerImpl::ReportMemoryUsage); |
1538 } | 1559 } |
1539 } | 1560 } |
1540 | 1561 |
1541 void WebMediaPlayerImpl::NotifyPlaybackPaused() { | 1562 void WebMediaPlayerImpl::NotifyPlaybackPaused() { |
1542 #if defined(OS_ANDROID) // WMPI_CAST | 1563 #if defined(OS_ANDROID) // WMPI_CAST |
1543 if (isRemote()) | 1564 if (isRemote()) |
1544 return; | 1565 return; |
1545 #endif | 1566 #endif |
1546 if (delegate_) | 1567 // Don't send delegate notifications when suspended; upon suspend we send |
| 1568 // PlayerGone() to the delegate -- no more notifications should be sent until |
| 1569 // after resume. |
| 1570 if (!suspended_ && delegate_) |
1547 delegate_->DidPause(delegate_id_, ended_); | 1571 delegate_->DidPause(delegate_id_, ended_); |
1548 memory_usage_reporting_timer_.Stop(); | 1572 memory_usage_reporting_timer_.Stop(); |
1549 ReportMemoryUsage(); | 1573 ReportMemoryUsage(); |
1550 } | 1574 } |
1551 | 1575 |
1552 void WebMediaPlayerImpl::ReportMemoryUsage() { | 1576 void WebMediaPlayerImpl::ReportMemoryUsage() { |
1553 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1577 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1554 | 1578 |
1555 // About base::Unretained() usage below: We destroy |demuxer_| on the main | 1579 // About base::Unretained() usage below: We destroy |demuxer_| on the main |
1556 // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the | 1580 // thread. Before that, however, ~WebMediaPlayerImpl() posts a task to the |
(...skipping 23 matching lines...) Expand all Loading... |
1580 << ", Video: " << stats.video_memory_usage << ", DataSource: " | 1604 << ", Video: " << stats.video_memory_usage << ", DataSource: " |
1581 << (data_source_ ? data_source_->GetMemoryUsage() : 0) | 1605 << (data_source_ ? data_source_->GetMemoryUsage() : 0) |
1582 << ", Demuxer: " << demuxer_memory_usage; | 1606 << ", Demuxer: " << demuxer_memory_usage; |
1583 | 1607 |
1584 const int64_t delta = current_memory_usage - last_reported_memory_usage_; | 1608 const int64_t delta = current_memory_usage - last_reported_memory_usage_; |
1585 last_reported_memory_usage_ = current_memory_usage; | 1609 last_reported_memory_usage_ = current_memory_usage; |
1586 adjust_allocated_memory_cb_.Run(delta); | 1610 adjust_allocated_memory_cb_.Run(delta); |
1587 } | 1611 } |
1588 | 1612 |
1589 } // namespace media | 1613 } // namespace media |
OLD | NEW |