| 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 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 client_(client), | 208 client_(client), |
| 209 encrypted_client_(encrypted_client), | 209 encrypted_client_(encrypted_client), |
| 210 delegate_(delegate), | 210 delegate_(delegate), |
| 211 delegate_id_(0), | 211 delegate_id_(0), |
| 212 defer_load_cb_(params.defer_load_cb()), | 212 defer_load_cb_(params.defer_load_cb()), |
| 213 context_3d_cb_(params.context_3d_cb()), | 213 context_3d_cb_(params.context_3d_cb()), |
| 214 adjust_allocated_memory_cb_(params.adjust_allocated_memory_cb()), | 214 adjust_allocated_memory_cb_(params.adjust_allocated_memory_cb()), |
| 215 last_reported_memory_usage_(0), | 215 last_reported_memory_usage_(0), |
| 216 supports_save_(true), | 216 supports_save_(true), |
| 217 chunk_demuxer_(NULL), | 217 chunk_demuxer_(NULL), |
| 218 buffered_data_source_host_( |
| 219 base::Bind(&WebMediaPlayerImpl::OnProgress, AsWeakPtr())), |
| 218 url_index_(url_index), | 220 url_index_(url_index), |
| 219 // Threaded compositing isn't enabled universally yet. | 221 // Threaded compositing isn't enabled universally yet. |
| 220 compositor_task_runner_(params.compositor_task_runner() | 222 compositor_task_runner_(params.compositor_task_runner() |
| 221 ? params.compositor_task_runner() | 223 ? params.compositor_task_runner() |
| 222 : base::ThreadTaskRunnerHandle::Get()), | 224 : base::ThreadTaskRunnerHandle::Get()), |
| 223 compositor_(new VideoFrameCompositor(compositor_task_runner_)), | 225 compositor_(new VideoFrameCompositor(compositor_task_runner_)), |
| 224 #if defined(OS_ANDROID) // WMPI_CAST | 226 #if defined(OS_ANDROID) // WMPI_CAST |
| 225 cast_impl_(this, client_, params.context_3d_cb()), | 227 cast_impl_(this, client_, params.context_3d_cb()), |
| 226 #endif | 228 #endif |
| 227 volume_(1.0), | 229 volume_(1.0), |
| (...skipping 608 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 836 tick_clock_->NowTicks() - preroll_attempt_start_time_; | 838 tick_clock_->NowTicks() - preroll_attempt_start_time_; |
| 837 return preroll_attempt_duration < kPrerollAttemptTimeout; | 839 return preroll_attempt_duration < kPrerollAttemptTimeout; |
| 838 } | 840 } |
| 839 | 841 |
| 840 bool WebMediaPlayerImpl::didLoadingProgress() { | 842 bool WebMediaPlayerImpl::didLoadingProgress() { |
| 841 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 843 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 842 | 844 |
| 843 // Note: Separate variables used to ensure both methods are called every time. | 845 // Note: Separate variables used to ensure both methods are called every time. |
| 844 const bool pipeline_progress = pipeline_controller_.DidLoadingProgress(); | 846 const bool pipeline_progress = pipeline_controller_.DidLoadingProgress(); |
| 845 const bool data_progress = buffered_data_source_host_.DidLoadingProgress(); | 847 const bool data_progress = buffered_data_source_host_.DidLoadingProgress(); |
| 846 const bool did_loading_progress = pipeline_progress || data_progress; | 848 return pipeline_progress || data_progress; |
| 847 | |
| 848 if (did_loading_progress && | |
| 849 highest_ready_state_ < ReadyState::ReadyStateHaveFutureData) { | |
| 850 // Reset the preroll attempt clock. | |
| 851 preroll_attempt_pending_ = true; | |
| 852 preroll_attempt_start_time_ = base::TimeTicks(); | |
| 853 | |
| 854 // Clear any 'stale' flag and give the pipeline a chance to resume. If we | |
| 855 // are already resumed, this will cause |preroll_attempt_start_time_| to be | |
| 856 // set. | |
| 857 // TODO(sandersd): Should this be on the same stack? It might be surprising | |
| 858 // that didLoadingProgress() can synchronously change state. | |
| 859 delegate_->ClearStaleFlag(delegate_id_); | |
| 860 UpdatePlayState(); | |
| 861 } | |
| 862 | |
| 863 return did_loading_progress; | |
| 864 } | 849 } |
| 865 | 850 |
| 866 void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas, | 851 void WebMediaPlayerImpl::paint(blink::WebCanvas* canvas, |
| 867 const blink::WebRect& rect, | 852 const blink::WebRect& rect, |
| 868 cc::PaintFlags& flags) { | 853 cc::PaintFlags& flags) { |
| 869 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 854 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 870 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint"); | 855 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint"); |
| 871 | 856 |
| 872 // TODO(sandersd): Move this check into GetCurrentFrameFromCompositor() when | 857 // TODO(sandersd): Move this check into GetCurrentFrameFromCompositor() when |
| 873 // we have other ways to check if decoder owns video frame. | 858 // we have other ways to check if decoder owns video frame. |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1267 client_->setWebLayer(video_weblayer_.get()); | 1252 client_->setWebLayer(video_weblayer_.get()); |
| 1268 } | 1253 } |
| 1269 | 1254 |
| 1270 if (observer_) | 1255 if (observer_) |
| 1271 observer_->OnMetadataChanged(pipeline_metadata_); | 1256 observer_->OnMetadataChanged(pipeline_metadata_); |
| 1272 | 1257 |
| 1273 CreateWatchTimeReporter(); | 1258 CreateWatchTimeReporter(); |
| 1274 UpdatePlayState(); | 1259 UpdatePlayState(); |
| 1275 } | 1260 } |
| 1276 | 1261 |
| 1262 void WebMediaPlayerImpl::OnProgress() { |
| 1263 if (highest_ready_state_ < ReadyState::ReadyStateHaveFutureData) { |
| 1264 // Reset the preroll attempt clock. |
| 1265 preroll_attempt_pending_ = true; |
| 1266 preroll_attempt_start_time_ = base::TimeTicks(); |
| 1267 |
| 1268 // Clear any 'stale' flag and give the pipeline a chance to resume. If we |
| 1269 // are already resumed, this will cause |preroll_attempt_start_time_| to |
| 1270 // be set. |
| 1271 // TODO(sandersd): Should this be on the same stack? It might be |
| 1272 // surprising that didLoadingProgress() can synchronously change state. |
| 1273 delegate_->ClearStaleFlag(delegate_id_); |
| 1274 UpdatePlayState(); |
| 1275 } |
| 1276 if (ready_state_ == ReadyState::ReadyStateHaveFutureData && |
| 1277 CanPlayThrough()) { |
| 1278 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData); |
| 1279 } |
| 1280 } |
| 1281 |
| 1282 bool WebMediaPlayerImpl::CanPlayThrough() { |
| 1283 if (chunk_demuxer_) |
| 1284 return true; |
| 1285 return buffered_data_source_host_.CanPlayThrough( |
| 1286 base::TimeDelta::FromSecondsD(currentTime()), |
| 1287 base::TimeDelta::FromSecondsD(duration()), |
| 1288 playback_rate_ == 0.0 ? 1.0 : playback_rate_); |
| 1289 } |
| 1290 |
| 1277 void WebMediaPlayerImpl::OnBufferingStateChange(BufferingState state) { | 1291 void WebMediaPlayerImpl::OnBufferingStateChange(BufferingState state) { |
| 1278 DVLOG(1) << __func__ << "(" << state << ")"; | 1292 DVLOG(1) << __func__ << "(" << state << ")"; |
| 1279 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1293 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 1280 | 1294 |
| 1281 // Ignore buffering state changes until we've completed all outstanding | 1295 // Ignore buffering state changes until we've completed all outstanding |
| 1282 // operations. | 1296 // operations. |
| 1283 if (!pipeline_controller_.IsStable()) | 1297 if (!pipeline_controller_.IsStable()) |
| 1284 return; | 1298 return; |
| 1285 | 1299 |
| 1286 media_log_->AddEvent(media_log_->CreateBufferingStateChangedEvent( | 1300 media_log_->AddEvent(media_log_->CreateBufferingStateChangedEvent( |
| 1287 "pipeline_buffering_state", state)); | 1301 "pipeline_buffering_state", state)); |
| 1288 | 1302 |
| 1289 if (state == BUFFERING_HAVE_ENOUGH) { | 1303 if (state == BUFFERING_HAVE_ENOUGH) { |
| 1290 if (highest_ready_state_ < WebMediaPlayer::ReadyStateHaveEnoughData) { | 1304 if (highest_ready_state_ < WebMediaPlayer::ReadyStateHaveEnoughData) { |
| 1291 // Record a zero value for underflow histogram so that the histogram | 1305 // Record a zero value for underflow histogram so that the histogram |
| 1292 // includes playbacks which never encounter an underflow event. | 1306 // includes playbacks which never encounter an underflow event. |
| 1293 RecordUnderflowDuration(base::TimeDelta()); | 1307 RecordUnderflowDuration(base::TimeDelta()); |
| 1294 } | 1308 } |
| 1295 | 1309 |
| 1296 // TODO(chcunningham): Monitor playback position vs buffered. Potentially | 1310 SetReadyState(CanPlayThrough() ? WebMediaPlayer::ReadyStateHaveEnoughData |
| 1297 // transition to HAVE_FUTURE_DATA here if not enough is buffered. | 1311 : WebMediaPlayer::ReadyStateHaveFutureData); |
| 1298 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData); | |
| 1299 | 1312 |
| 1300 // Let the DataSource know we have enough data. It may use this information | 1313 // Let the DataSource know we have enough data. It may use this information |
| 1301 // to release unused network connections. | 1314 // to release unused network connections. |
| 1302 if (data_source_) | 1315 if (data_source_) |
| 1303 data_source_->OnBufferingHaveEnough(false); | 1316 data_source_->OnBufferingHaveEnough(false); |
| 1304 | 1317 |
| 1305 // Blink expects a timeChanged() in response to a seek(). | 1318 // Blink expects a timeChanged() in response to a seek(). |
| 1306 if (should_notify_time_changed_) { | 1319 if (should_notify_time_changed_) { |
| 1307 should_notify_time_changed_ = false; | 1320 should_notify_time_changed_ = false; |
| 1308 client_->timeChanged(); | 1321 client_->timeChanged(); |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1637 | 1650 |
| 1638 StartPipeline(); | 1651 StartPipeline(); |
| 1639 } | 1652 } |
| 1640 | 1653 |
| 1641 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { | 1654 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { |
| 1642 DVLOG(1) << __func__; | 1655 DVLOG(1) << __func__; |
| 1643 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading) | 1656 if (!is_downloading && network_state_ == WebMediaPlayer::NetworkStateLoading) |
| 1644 SetNetworkState(WebMediaPlayer::NetworkStateIdle); | 1657 SetNetworkState(WebMediaPlayer::NetworkStateIdle); |
| 1645 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle) | 1658 else if (is_downloading && network_state_ == WebMediaPlayer::NetworkStateIdle) |
| 1646 SetNetworkState(WebMediaPlayer::NetworkStateLoading); | 1659 SetNetworkState(WebMediaPlayer::NetworkStateLoading); |
| 1660 if (ready_state_ == ReadyState::ReadyStateHaveFutureData && !is_downloading) |
| 1661 SetReadyState(WebMediaPlayer::ReadyStateHaveEnoughData); |
| 1647 media_log_->AddEvent( | 1662 media_log_->AddEvent( |
| 1648 media_log_->CreateBooleanEvent(MediaLogEvent::NETWORK_ACTIVITY_SET, | 1663 media_log_->CreateBooleanEvent(MediaLogEvent::NETWORK_ACTIVITY_SET, |
| 1649 "is_downloading_data", is_downloading)); | 1664 "is_downloading_data", is_downloading)); |
| 1650 } | 1665 } |
| 1651 | 1666 |
| 1652 void WebMediaPlayerImpl::OnSurfaceCreated(int surface_id) { | 1667 void WebMediaPlayerImpl::OnSurfaceCreated(int surface_id) { |
| 1653 overlay_surface_id_ = surface_id; | 1668 overlay_surface_id_ = surface_id; |
| 1654 if (!set_surface_cb_.is_null()) { | 1669 if (!set_surface_cb_.is_null()) { |
| 1655 // If restart is required, the callback is one-shot only. | 1670 // If restart is required, the callback is one-shot only. |
| 1656 if (decoder_requires_restart_for_overlay_) | 1671 if (decoder_requires_restart_for_overlay_) |
| (...skipping 668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2325 | 2340 |
| 2326 void WebMediaPlayerImpl::RecordUnderflowDuration(base::TimeDelta duration) { | 2341 void WebMediaPlayerImpl::RecordUnderflowDuration(base::TimeDelta duration) { |
| 2327 DCHECK(data_source_ || chunk_demuxer_); | 2342 DCHECK(data_source_ || chunk_demuxer_); |
| 2328 if (data_source_) | 2343 if (data_source_) |
| 2329 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration", duration); | 2344 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration", duration); |
| 2330 else | 2345 else |
| 2331 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration.MSE", duration); | 2346 UMA_HISTOGRAM_TIMES("Media.UnderflowDuration.MSE", duration); |
| 2332 } | 2347 } |
| 2333 | 2348 |
| 2334 } // namespace media | 2349 } // namespace media |
| OLD | NEW |