Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chromecast/media/cma/pipeline/media_pipeline_impl.h" | 5 #include "chromecast/media/cma/pipeline/media_pipeline_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | |
| 18 #include "chromecast/base/metrics/cast_metrics_helper.h" | 17 #include "chromecast/base/metrics/cast_metrics_helper.h" |
| 19 #include "chromecast/media/cdm/browser_cdm_cast.h" | 18 #include "chromecast/media/cdm/browser_cdm_cast.h" |
| 20 #include "chromecast/media/cma/base/buffering_controller.h" | 19 #include "chromecast/media/cma/base/buffering_controller.h" |
| 21 #include "chromecast/media/cma/base/buffering_state.h" | 20 #include "chromecast/media/cma/base/buffering_state.h" |
| 22 #include "chromecast/media/cma/base/cma_logging.h" | 21 #include "chromecast/media/cma/base/cma_logging.h" |
| 23 #include "chromecast/media/cma/base/coded_frame_provider.h" | 22 #include "chromecast/media/cma/base/coded_frame_provider.h" |
| 24 #include "chromecast/media/cma/pipeline/audio_decoder_software_wrapper.h" | 23 #include "chromecast/media/cma/pipeline/audio_decoder_software_wrapper.h" |
| 25 #include "chromecast/media/cma/pipeline/audio_pipeline_impl.h" | 24 #include "chromecast/media/cma/pipeline/audio_pipeline_impl.h" |
| 26 #include "chromecast/media/cma/pipeline/video_pipeline_impl.h" | 25 #include "chromecast/media/cma/pipeline/video_pipeline_impl.h" |
| 27 #include "chromecast/public/media/media_pipeline_backend.h" | 26 #include "chromecast/public/media/media_pipeline_backend.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 45 base::TimeDelta::FromMilliseconds(300)); | 44 base::TimeDelta::FromMilliseconds(300)); |
| 46 | 45 |
| 47 // Interval between two updates of the media time. | 46 // Interval between two updates of the media time. |
| 48 const base::TimeDelta kTimeUpdateInterval( | 47 const base::TimeDelta kTimeUpdateInterval( |
| 49 base::TimeDelta::FromMilliseconds(250)); | 48 base::TimeDelta::FromMilliseconds(250)); |
| 50 | 49 |
| 51 // Interval between two updates of the statistics is equal to: | 50 // Interval between two updates of the statistics is equal to: |
| 52 // kTimeUpdateInterval * kStatisticsUpdatePeriod. | 51 // kTimeUpdateInterval * kStatisticsUpdatePeriod. |
| 53 const int kStatisticsUpdatePeriod = 4; | 52 const int kStatisticsUpdatePeriod = 4; |
| 54 | 53 |
| 54 void LogEstimatedBitrate(int decoded_bytes, | |
| 55 base::TimeDelta elapsed_time, | |
| 56 const char* tag, | |
| 57 const char* metric) { | |
| 58 int estimated_bitrate_in_kbps = | |
| 59 8 * decoded_bytes / elapsed_time.InMilliseconds(); | |
| 60 | |
| 61 if (estimated_bitrate_in_kbps <= 0) { | |
|
kmackay
2016/03/11 21:07:26
nit: braces not necessary
ejason
2016/03/11 21:11:44
Done.
| |
| 62 return; | |
| 63 } | |
| 64 | |
| 65 CMALOG(kLogControl) << "Estimated " << tag << " bitrate is " | |
| 66 << estimated_bitrate_in_kbps << " kbps"; | |
| 67 metrics::CastMetricsHelper* metrics_helper = | |
| 68 metrics::CastMetricsHelper::GetInstance(); | |
| 69 metrics_helper->RecordSimpleActionWithValue(metric, | |
| 70 estimated_bitrate_in_kbps); | |
| 71 } | |
| 72 | |
| 55 } // namespace | 73 } // namespace |
| 56 | 74 |
| 57 struct MediaPipelineImpl::FlushTask { | 75 struct MediaPipelineImpl::FlushTask { |
| 58 bool audio_flushed; | 76 bool audio_flushed; |
| 59 bool video_flushed; | 77 bool video_flushed; |
| 60 base::Closure done_cb; | 78 base::Closure done_cb; |
| 61 }; | 79 }; |
| 62 | 80 |
| 63 MediaPipelineImpl::MediaPipelineImpl() | 81 MediaPipelineImpl::MediaPipelineImpl() |
| 64 : cdm_(nullptr), | 82 : cdm_(nullptr), |
| 65 backend_state_(BACKEND_STATE_UNINITIALIZED), | 83 backend_state_(BACKEND_STATE_UNINITIALIZED), |
| 66 playback_rate_(1.0f), | 84 playback_rate_(1.0f), |
| 67 audio_decoder_(nullptr), | 85 audio_decoder_(nullptr), |
| 68 video_decoder_(nullptr), | 86 video_decoder_(nullptr), |
| 69 pending_time_update_task_(false), | 87 pending_time_update_task_(false), |
| 70 statistics_rolling_counter_(0), | 88 statistics_rolling_counter_(0), |
| 89 last_sample_time_(base::TimeTicks()), | |
|
kmackay
2016/03/11 21:07:26
nit: these initializers are probably not necessary
ejason
2016/03/11 21:11:44
Done.
| |
| 90 elapsed_time_delta_(base::TimeDelta()), | |
| 91 video_bytes_for_bitrate_estimation_(0), | |
| 71 weak_factory_(this) { | 92 weak_factory_(this) { |
| 72 CMALOG(kLogControl) << __FUNCTION__; | 93 CMALOG(kLogControl) << __FUNCTION__; |
| 73 weak_this_ = weak_factory_.GetWeakPtr(); | 94 weak_this_ = weak_factory_.GetWeakPtr(); |
| 74 thread_checker_.DetachFromThread(); | 95 thread_checker_.DetachFromThread(); |
| 75 } | 96 } |
| 76 | 97 |
| 77 MediaPipelineImpl::~MediaPipelineImpl() { | 98 MediaPipelineImpl::~MediaPipelineImpl() { |
| 78 CMALOG(kLogControl) << __FUNCTION__; | 99 CMALOG(kLogControl) << __FUNCTION__; |
| 79 DCHECK(thread_checker_.CalledOnValidThread()); | 100 DCHECK(thread_checker_.CalledOnValidThread()); |
| 80 | 101 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 197 } | 218 } |
| 198 backend_state_ = BACKEND_STATE_INITIALIZED; | 219 backend_state_ = BACKEND_STATE_INITIALIZED; |
| 199 } | 220 } |
| 200 | 221 |
| 201 // Start the backend. | 222 // Start the backend. |
| 202 if (!media_pipeline_backend_->Start(time.InMicroseconds())) { | 223 if (!media_pipeline_backend_->Start(time.InMicroseconds())) { |
| 203 OnError(::media::PIPELINE_ERROR_ABORT); | 224 OnError(::media::PIPELINE_ERROR_ABORT); |
| 204 return; | 225 return; |
| 205 } | 226 } |
| 206 backend_state_ = BACKEND_STATE_PLAYING; | 227 backend_state_ = BACKEND_STATE_PLAYING; |
| 228 ResetBitrateState(); | |
| 207 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction( | 229 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction( |
| 208 "Cast.Platform.Playing"); | 230 "Cast.Platform.Playing"); |
| 209 | 231 |
| 210 // Enable time updates. | 232 // Enable time updates. |
| 211 statistics_rolling_counter_ = 0; | 233 statistics_rolling_counter_ = 0; |
| 212 if (!pending_time_update_task_) { | 234 if (!pending_time_update_task_) { |
| 213 pending_time_update_task_ = true; | 235 pending_time_update_task_ = true; |
| 214 base::ThreadTaskRunnerHandle::Get()->PostTask( | 236 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 215 FROM_HERE, base::Bind(&MediaPipelineImpl::UpdateMediaTime, weak_this_)); | 237 FROM_HERE, base::Bind(&MediaPipelineImpl::UpdateMediaTime, weak_this_)); |
| 216 } | 238 } |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 311 | 333 |
| 312 playback_rate_ = rate; | 334 playback_rate_ = rate; |
| 313 if (buffering_controller_ && buffering_controller_->IsBuffering()) | 335 if (buffering_controller_ && buffering_controller_->IsBuffering()) |
| 314 return; | 336 return; |
| 315 | 337 |
| 316 if (rate != 0.0f) { | 338 if (rate != 0.0f) { |
| 317 media_pipeline_backend_->SetPlaybackRate(rate); | 339 media_pipeline_backend_->SetPlaybackRate(rate); |
| 318 if (backend_state_ == BACKEND_STATE_PAUSED) { | 340 if (backend_state_ == BACKEND_STATE_PAUSED) { |
| 319 media_pipeline_backend_->Resume(); | 341 media_pipeline_backend_->Resume(); |
| 320 backend_state_ = BACKEND_STATE_PLAYING; | 342 backend_state_ = BACKEND_STATE_PLAYING; |
| 343 ResetBitrateState(); | |
| 321 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction( | 344 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction( |
| 322 "Cast.Platform.Playing"); | 345 "Cast.Platform.Playing"); |
| 323 } | 346 } |
| 324 } else if (backend_state_ == BACKEND_STATE_PLAYING) { | 347 } else if (backend_state_ == BACKEND_STATE_PLAYING) { |
| 325 media_pipeline_backend_->Pause(); | 348 media_pipeline_backend_->Pause(); |
| 326 backend_state_ = BACKEND_STATE_PAUSED; | 349 backend_state_ = BACKEND_STATE_PAUSED; |
| 327 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction( | 350 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction( |
| 328 "Cast.Platform.Pause"); | 351 "Cast.Platform.Pause"); |
| 329 } | 352 } |
| 330 } | 353 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 pending_time_update_task_ = false; | 431 pending_time_update_task_ = false; |
| 409 if ((backend_state_ != BACKEND_STATE_PLAYING) && | 432 if ((backend_state_ != BACKEND_STATE_PLAYING) && |
| 410 (backend_state_ != BACKEND_STATE_PAUSED)) | 433 (backend_state_ != BACKEND_STATE_PAUSED)) |
| 411 return; | 434 return; |
| 412 | 435 |
| 413 if (statistics_rolling_counter_ == 0) { | 436 if (statistics_rolling_counter_ == 0) { |
| 414 if (audio_pipeline_) | 437 if (audio_pipeline_) |
| 415 audio_pipeline_->UpdateStatistics(); | 438 audio_pipeline_->UpdateStatistics(); |
| 416 if (video_pipeline_) | 439 if (video_pipeline_) |
| 417 video_pipeline_->UpdateStatistics(); | 440 video_pipeline_->UpdateStatistics(); |
| 441 | |
| 442 if (backend_state_ == BACKEND_STATE_PLAYING) { | |
| 443 base::TimeTicks current_time = base::TimeTicks::Now(); | |
| 444 audio_bytes_for_bitrate_estimation_ += | |
| 445 audio_pipeline_->bytes_decoded_since_last_update(); | |
| 446 video_bytes_for_bitrate_estimation_ += | |
| 447 video_pipeline_->bytes_decoded_since_last_update(); | |
| 448 elapsed_time_delta_ += current_time - last_sample_time_; | |
| 449 if (elapsed_time_delta_.InMilliseconds() > 5000) { | |
| 450 LogEstimatedBitrate(audio_bytes_for_bitrate_estimation_, | |
| 451 elapsed_time_delta_, "audio", | |
| 452 "Cast.Platform.AudioBitrate"); | |
| 453 LogEstimatedBitrate(video_bytes_for_bitrate_estimation_, | |
| 454 elapsed_time_delta_, "video", | |
| 455 "Cast.Platform.VideoBitrate"); | |
| 456 ResetBitrateState(); | |
| 457 } | |
| 458 last_sample_time_ = current_time; | |
| 459 } | |
| 418 } | 460 } |
| 461 | |
| 419 statistics_rolling_counter_ = | 462 statistics_rolling_counter_ = |
| 420 (statistics_rolling_counter_ + 1) % kStatisticsUpdatePeriod; | 463 (statistics_rolling_counter_ + 1) % kStatisticsUpdatePeriod; |
| 421 | 464 |
| 422 base::TimeDelta media_time = base::TimeDelta::FromMicroseconds( | 465 base::TimeDelta media_time = base::TimeDelta::FromMicroseconds( |
| 423 media_pipeline_backend_->GetCurrentPts()); | 466 media_pipeline_backend_->GetCurrentPts()); |
| 424 if (media_time == ::media::kNoTimestamp()) { | 467 if (media_time == ::media::kNoTimestamp()) { |
| 425 pending_time_update_task_ = true; | 468 pending_time_update_task_ = true; |
| 426 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 469 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 427 FROM_HERE, base::Bind(&MediaPipelineImpl::UpdateMediaTime, weak_this_), | 470 FROM_HERE, base::Bind(&MediaPipelineImpl::UpdateMediaTime, weak_this_), |
| 428 kTimeUpdateInterval); | 471 kTimeUpdateInterval); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 461 DCHECK(thread_checker_.CalledOnValidThread()); | 504 DCHECK(thread_checker_.CalledOnValidThread()); |
| 462 DCHECK_NE(error, ::media::PIPELINE_OK) << "PIPELINE_OK is not an error!"; | 505 DCHECK_NE(error, ::media::PIPELINE_OK) << "PIPELINE_OK is not an error!"; |
| 463 | 506 |
| 464 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction( | 507 metrics::CastMetricsHelper::GetInstance()->RecordSimpleAction( |
| 465 "Cast.Platform.Error"); | 508 "Cast.Platform.Error"); |
| 466 | 509 |
| 467 if (!client_.error_cb.is_null()) | 510 if (!client_.error_cb.is_null()) |
| 468 client_.error_cb.Run(error); | 511 client_.error_cb.Run(error); |
| 469 } | 512 } |
| 470 | 513 |
| 514 void MediaPipelineImpl::ResetBitrateState() { | |
| 515 elapsed_time_delta_ = base::TimeDelta::FromSeconds(0); | |
| 516 audio_bytes_for_bitrate_estimation_ = 0; | |
| 517 video_bytes_for_bitrate_estimation_ = 0; | |
| 518 last_sample_time_ = base::TimeTicks::Now(); | |
| 519 } | |
| 520 | |
| 471 } // namespace media | 521 } // namespace media |
| 472 } // namespace chromecast | 522 } // namespace chromecast |
| OLD | NEW |