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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 client_(client), | 212 client_(client), |
213 encrypted_client_(encrypted_client), | 213 encrypted_client_(encrypted_client), |
214 delegate_(delegate), | 214 delegate_(delegate), |
215 delegate_id_(0), | 215 delegate_id_(0), |
216 defer_load_cb_(params->defer_load_cb()), | 216 defer_load_cb_(params->defer_load_cb()), |
217 context_3d_cb_(params->context_3d_cb()), | 217 context_3d_cb_(params->context_3d_cb()), |
218 adjust_allocated_memory_cb_(params->adjust_allocated_memory_cb()), | 218 adjust_allocated_memory_cb_(params->adjust_allocated_memory_cb()), |
219 last_reported_memory_usage_(0), | 219 last_reported_memory_usage_(0), |
220 supports_save_(true), | 220 supports_save_(true), |
221 chunk_demuxer_(NULL), | 221 chunk_demuxer_(NULL), |
| 222 tick_clock_(new base::DefaultTickClock()), |
| 223 buffered_data_source_host_( |
| 224 base::Bind(&WebMediaPlayerImpl::OnProgress, AsWeakPtr()), |
| 225 tick_clock_.get()), |
222 url_index_(url_index), | 226 url_index_(url_index), |
223 // Threaded compositing isn't enabled universally yet. | 227 // Threaded compositing isn't enabled universally yet. |
224 compositor_task_runner_(params->compositor_task_runner() | 228 compositor_task_runner_(params->compositor_task_runner() |
225 ? params->compositor_task_runner() | 229 ? params->compositor_task_runner() |
226 : base::ThreadTaskRunnerHandle::Get()), | 230 : base::ThreadTaskRunnerHandle::Get()), |
227 compositor_(new VideoFrameCompositor(compositor_task_runner_)), | 231 compositor_(new VideoFrameCompositor(compositor_task_runner_)), |
228 #if defined(OS_ANDROID) // WMPI_CAST | 232 #if defined(OS_ANDROID) // WMPI_CAST |
229 cast_impl_(this, client_, params->context_3d_cb()), | 233 cast_impl_(this, client_, params->context_3d_cb()), |
230 #endif | 234 #endif |
231 volume_(1.0), | 235 volume_(1.0), |
(...skipping 13 matching lines...) Expand all Loading... |
245 enable_instant_source_buffer_gc_( | 249 enable_instant_source_buffer_gc_( |
246 params->enable_instant_source_buffer_gc()), | 250 params->enable_instant_source_buffer_gc()), |
247 embedded_media_experience_enabled_( | 251 embedded_media_experience_enabled_( |
248 params->embedded_media_experience_enabled()) { | 252 params->embedded_media_experience_enabled()) { |
249 DVLOG(1) << __func__; | 253 DVLOG(1) << __func__; |
250 DCHECK(!adjust_allocated_memory_cb_.is_null()); | 254 DCHECK(!adjust_allocated_memory_cb_.is_null()); |
251 DCHECK(renderer_factory_selector_); | 255 DCHECK(renderer_factory_selector_); |
252 DCHECK(client_); | 256 DCHECK(client_); |
253 DCHECK(delegate_); | 257 DCHECK(delegate_); |
254 | 258 |
255 tick_clock_.reset(new base::DefaultTickClock()); | |
256 | |
257 force_video_overlays_ = base::CommandLine::ForCurrentProcess()->HasSwitch( | 259 force_video_overlays_ = base::CommandLine::ForCurrentProcess()->HasSwitch( |
258 switches::kForceVideoOverlays); | 260 switches::kForceVideoOverlays); |
259 | 261 |
260 enable_fullscreen_video_overlays_ = | 262 enable_fullscreen_video_overlays_ = |
261 base::FeatureList::IsEnabled(media::kOverlayFullscreenVideo); | 263 base::FeatureList::IsEnabled(media::kOverlayFullscreenVideo); |
262 | 264 |
263 delegate_id_ = delegate_->AddObserver(this); | 265 delegate_id_ = delegate_->AddObserver(this); |
264 delegate_->SetIdle(delegate_id_, true); | 266 delegate_->SetIdle(delegate_id_, true); |
265 | 267 |
266 media_log_->AddEvent( | 268 media_log_->AddEvent( |
(...skipping 580 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
847 tick_clock_->NowTicks() - preroll_attempt_start_time_; | 849 tick_clock_->NowTicks() - preroll_attempt_start_time_; |
848 return preroll_attempt_duration < kPrerollAttemptTimeout; | 850 return preroll_attempt_duration < kPrerollAttemptTimeout; |
849 } | 851 } |
850 | 852 |
851 bool WebMediaPlayerImpl::DidLoadingProgress() { | 853 bool WebMediaPlayerImpl::DidLoadingProgress() { |
852 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 854 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
853 | 855 |
854 // Note: Separate variables used to ensure both methods are called every time. | 856 // Note: Separate variables used to ensure both methods are called every time. |
855 const bool pipeline_progress = pipeline_controller_.DidLoadingProgress(); | 857 const bool pipeline_progress = pipeline_controller_.DidLoadingProgress(); |
856 const bool data_progress = buffered_data_source_host_.DidLoadingProgress(); | 858 const bool data_progress = buffered_data_source_host_.DidLoadingProgress(); |
857 const bool did_loading_progress = pipeline_progress || data_progress; | 859 return pipeline_progress || data_progress; |
858 | |
859 if (did_loading_progress && | |
860 highest_ready_state_ < ReadyState::kReadyStateHaveFutureData) { | |
861 // Reset the preroll attempt clock. | |
862 preroll_attempt_pending_ = true; | |
863 preroll_attempt_start_time_ = base::TimeTicks(); | |
864 | |
865 // Clear any 'stale' flag and give the pipeline a chance to resume. If we | |
866 // are already resumed, this will cause |preroll_attempt_start_time_| to be | |
867 // set. | |
868 // TODO(sandersd): Should this be on the same stack? It might be surprising | |
869 // that didLoadingProgress() can synchronously change state. | |
870 delegate_->ClearStaleFlag(delegate_id_); | |
871 UpdatePlayState(); | |
872 } | |
873 | |
874 return did_loading_progress; | |
875 } | 860 } |
876 | 861 |
877 void WebMediaPlayerImpl::Paint(blink::WebCanvas* canvas, | 862 void WebMediaPlayerImpl::Paint(blink::WebCanvas* canvas, |
878 const blink::WebRect& rect, | 863 const blink::WebRect& rect, |
879 cc::PaintFlags& flags) { | 864 cc::PaintFlags& flags) { |
880 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 865 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
881 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint"); | 866 TRACE_EVENT0("media", "WebMediaPlayerImpl:paint"); |
882 | 867 |
883 // We can't copy from protected frames. | 868 // We can't copy from protected frames. |
884 if (cdm_) | 869 if (cdm_) |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1288 client_->SetWebLayer(video_weblayer_.get()); | 1273 client_->SetWebLayer(video_weblayer_.get()); |
1289 } | 1274 } |
1290 | 1275 |
1291 if (observer_) | 1276 if (observer_) |
1292 observer_->OnMetadataChanged(pipeline_metadata_); | 1277 observer_->OnMetadataChanged(pipeline_metadata_); |
1293 | 1278 |
1294 CreateWatchTimeReporter(); | 1279 CreateWatchTimeReporter(); |
1295 UpdatePlayState(); | 1280 UpdatePlayState(); |
1296 } | 1281 } |
1297 | 1282 |
| 1283 void WebMediaPlayerImpl::OnProgress() { |
| 1284 DVLOG(1) << __func__; |
| 1285 if (highest_ready_state_ < ReadyState::kReadyStateHaveFutureData) { |
| 1286 // Reset the preroll attempt clock. |
| 1287 preroll_attempt_pending_ = true; |
| 1288 preroll_attempt_start_time_ = base::TimeTicks(); |
| 1289 |
| 1290 // Clear any 'stale' flag and give the pipeline a chance to resume. If we |
| 1291 // are already resumed, this will cause |preroll_attempt_start_time_| to |
| 1292 // be set. |
| 1293 delegate_->ClearStaleFlag(delegate_id_); |
| 1294 UpdatePlayState(); |
| 1295 } else if (ready_state_ == ReadyState::kReadyStateHaveFutureData && |
| 1296 CanPlayThrough()) { |
| 1297 SetReadyState(WebMediaPlayer::kReadyStateHaveEnoughData); |
| 1298 } |
| 1299 } |
| 1300 |
| 1301 bool WebMediaPlayerImpl::CanPlayThrough() { |
| 1302 if (chunk_demuxer_) |
| 1303 return true; |
| 1304 if (data_source_ && data_source_->assume_fully_buffered()) |
| 1305 return true; |
| 1306 // If we're not currently downloading, we have as much buffer as |
| 1307 // we're ever going to get, which means we say we can play through. |
| 1308 if (network_state_ == WebMediaPlayer::kNetworkStateIdle) |
| 1309 return true; |
| 1310 return buffered_data_source_host_.CanPlayThrough( |
| 1311 base::TimeDelta::FromSecondsD(CurrentTime()), |
| 1312 base::TimeDelta::FromSecondsD(Duration()), |
| 1313 playback_rate_ == 0.0 ? 1.0 : playback_rate_); |
| 1314 } |
| 1315 |
1298 void WebMediaPlayerImpl::OnBufferingStateChange(BufferingState state) { | 1316 void WebMediaPlayerImpl::OnBufferingStateChange(BufferingState state) { |
1299 DVLOG(1) << __func__ << "(" << state << ")"; | 1317 DVLOG(1) << __func__ << "(" << state << ")"; |
1300 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 1318 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
1301 | 1319 |
1302 // Ignore buffering state changes until we've completed all outstanding | 1320 // Ignore buffering state changes until we've completed all outstanding |
1303 // operations. | 1321 // operations. |
1304 if (!pipeline_controller_.IsStable()) | 1322 if (!pipeline_controller_.IsStable()) |
1305 return; | 1323 return; |
1306 | 1324 |
1307 media_log_->AddEvent(media_log_->CreateBufferingStateChangedEvent( | 1325 media_log_->AddEvent(media_log_->CreateBufferingStateChangedEvent( |
1308 "pipeline_buffering_state", state)); | 1326 "pipeline_buffering_state", state)); |
1309 | 1327 |
1310 if (state == BUFFERING_HAVE_ENOUGH) { | 1328 if (state == BUFFERING_HAVE_ENOUGH) { |
1311 if (highest_ready_state_ < WebMediaPlayer::kReadyStateHaveEnoughData) { | 1329 if (highest_ready_state_ < WebMediaPlayer::kReadyStateHaveEnoughData) { |
1312 // Record a zero value for underflow histogram so that the histogram | 1330 // Record a zero value for underflow histogram so that the histogram |
1313 // includes playbacks which never encounter an underflow event. | 1331 // includes playbacks which never encounter an underflow event. |
1314 RecordUnderflowDuration(base::TimeDelta()); | 1332 RecordUnderflowDuration(base::TimeDelta()); |
1315 } | 1333 } |
1316 | 1334 |
1317 // TODO(chcunningham): Monitor playback position vs buffered. Potentially | 1335 SetReadyState(CanPlayThrough() ? WebMediaPlayer::kReadyStateHaveEnoughData |
1318 // transition to HAVE_FUTURE_DATA here if not enough is buffered. | 1336 : WebMediaPlayer::kReadyStateHaveFutureData); |
1319 SetReadyState(WebMediaPlayer::kReadyStateHaveEnoughData); | |
1320 | 1337 |
1321 // Let the DataSource know we have enough data. It may use this information | 1338 // Let the DataSource know we have enough data. It may use this information |
1322 // to release unused network connections. | 1339 // to release unused network connections. |
1323 if (data_source_) | 1340 if (data_source_) |
1324 data_source_->OnBufferingHaveEnough(false); | 1341 data_source_->OnBufferingHaveEnough(false); |
1325 | 1342 |
1326 // Blink expects a timeChanged() in response to a seek(). | 1343 // Blink expects a timeChanged() in response to a seek(). |
1327 if (should_notify_time_changed_) { | 1344 if (should_notify_time_changed_) { |
1328 should_notify_time_changed_ = false; | 1345 should_notify_time_changed_ = false; |
1329 client_->TimeChanged(); | 1346 client_->TimeChanged(); |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1642 // least this makes sure that the error handling code is in sync. | 1659 // least this makes sure that the error handling code is in sync. |
1643 UpdatePlayState(); | 1660 UpdatePlayState(); |
1644 | 1661 |
1645 return; | 1662 return; |
1646 } | 1663 } |
1647 | 1664 |
1648 StartPipeline(); | 1665 StartPipeline(); |
1649 } | 1666 } |
1650 | 1667 |
1651 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { | 1668 void WebMediaPlayerImpl::NotifyDownloading(bool is_downloading) { |
1652 DVLOG(1) << __func__; | 1669 DVLOG(1) << __func__ << "(" << is_downloading << ")"; |
1653 if (!is_downloading && network_state_ == WebMediaPlayer::kNetworkStateLoading) | 1670 if (!is_downloading && network_state_ == WebMediaPlayer::kNetworkStateLoading) |
1654 SetNetworkState(WebMediaPlayer::kNetworkStateIdle); | 1671 SetNetworkState(WebMediaPlayer::kNetworkStateIdle); |
1655 else if (is_downloading && | 1672 else if (is_downloading && |
1656 network_state_ == WebMediaPlayer::kNetworkStateIdle) | 1673 network_state_ == WebMediaPlayer::kNetworkStateIdle) |
1657 SetNetworkState(WebMediaPlayer::kNetworkStateLoading); | 1674 SetNetworkState(WebMediaPlayer::kNetworkStateLoading); |
| 1675 if (ready_state_ == ReadyState::kReadyStateHaveFutureData && !is_downloading) |
| 1676 SetReadyState(WebMediaPlayer::kReadyStateHaveEnoughData); |
1658 media_log_->AddEvent( | 1677 media_log_->AddEvent( |
1659 media_log_->CreateBooleanEvent(MediaLogEvent::NETWORK_ACTIVITY_SET, | 1678 media_log_->CreateBooleanEvent(MediaLogEvent::NETWORK_ACTIVITY_SET, |
1660 "is_downloading_data", is_downloading)); | 1679 "is_downloading_data", is_downloading)); |
1661 } | 1680 } |
1662 | 1681 |
1663 void WebMediaPlayerImpl::OnSurfaceCreated(int surface_id) { | 1682 void WebMediaPlayerImpl::OnSurfaceCreated(int surface_id) { |
1664 overlay_surface_id_ = surface_id; | 1683 overlay_surface_id_ = surface_id; |
1665 if (!set_surface_cb_.is_null()) { | 1684 if (!set_surface_cb_.is_null()) { |
1666 // If restart is required, the callback is one-shot only. | 1685 // If restart is required, the callback is one-shot only. |
1667 if (decoder_requires_restart_for_overlay_) | 1686 if (decoder_requires_restart_for_overlay_) |
(...skipping 725 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2393 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.MSE", height); | 2412 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.MSE", height); |
2394 | 2413 |
2395 if (is_encrypted_) | 2414 if (is_encrypted_) |
2396 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.EME", height); | 2415 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.EME", height); |
2397 | 2416 |
2398 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.All", height); | 2417 UMA_HISTOGRAM_VIDEO_HEIGHT("Media.VideoHeight.Initial.All", height); |
2399 } | 2418 } |
2400 | 2419 |
2401 #undef UMA_HISTOGRAM_VIDEO_HEIGHT | 2420 #undef UMA_HISTOGRAM_VIDEO_HEIGHT |
2402 | 2421 |
| 2422 void WebMediaPlayerImpl::SetTickClockForTest(base::TickClock* tick_clock) { |
| 2423 tick_clock_.reset(tick_clock); |
| 2424 buffered_data_source_host_.SetTickClockForTest(tick_clock); |
| 2425 } |
| 2426 |
2403 } // namespace media | 2427 } // namespace media |
OLD | NEW |