[MediaRouter] Update MR-2-Extension's PostMessage to return boolean.
[chromium-blink-merge.git] / chromecast / media / cma / base / buffering_state.cc
blobdec0cb42095a7a7234556cbc30a43ce0df41d1ef
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
3 // found in the LICENSE file.
5 #include "chromecast/media/cma/base/buffering_state.h"
7 #include <sstream>
9 #include "base/logging.h"
10 #include "media/base/buffers.h"
12 namespace chromecast {
13 namespace media {
15 BufferingConfig::BufferingConfig(
16 base::TimeDelta low_level_threshold,
17 base::TimeDelta high_level_threshold)
18 : low_level_threshold_(low_level_threshold),
19 high_level_threshold_(high_level_threshold) {
22 BufferingConfig::~BufferingConfig() {
26 BufferingState::BufferingState(
27 const std::string& stream_id,
28 const scoped_refptr<BufferingConfig>& config,
29 const base::Closure& state_changed_cb,
30 const HighLevelBufferCB& high_level_buffer_cb)
31 : stream_id_(stream_id),
32 config_(config),
33 state_changed_cb_(state_changed_cb),
34 high_level_buffer_cb_(high_level_buffer_cb),
35 state_(kLowLevel),
36 media_time_(::media::kNoTimestamp()),
37 max_rendering_time_(::media::kNoTimestamp()),
38 buffered_time_(::media::kNoTimestamp()) {
41 BufferingState::~BufferingState() {
44 void BufferingState::OnConfigChanged() {
45 state_ = GetBufferLevelState();
48 void BufferingState::SetMediaTime(base::TimeDelta media_time) {
49 media_time_ = media_time;
50 switch (state_) {
51 case kLowLevel:
52 case kMediumLevel:
53 case kHighLevel:
54 UpdateState(GetBufferLevelState());
55 break;
56 case kEosReached:
57 break;
61 void BufferingState::SetMaxRenderingTime(base::TimeDelta max_rendering_time) {
62 max_rendering_time_ = max_rendering_time;
65 base::TimeDelta BufferingState::GetMaxRenderingTime() const {
66 return max_rendering_time_;
69 void BufferingState::SetBufferedTime(base::TimeDelta buffered_time) {
70 buffered_time_ = buffered_time;
71 switch (state_) {
72 case kLowLevel:
73 case kMediumLevel:
74 case kHighLevel:
75 UpdateState(GetBufferLevelState());
76 break;
77 case kEosReached:
78 break;
82 void BufferingState::NotifyEos() {
83 UpdateState(kEosReached);
86 void BufferingState::NotifyMaxCapacity(base::TimeDelta buffered_time) {
87 if (media_time_ == ::media::kNoTimestamp() ||
88 buffered_time == ::media::kNoTimestamp()) {
89 LOG(WARNING) << "Max capacity with no timestamp";
90 return;
92 base::TimeDelta buffer_duration = buffered_time - media_time_;
93 if (buffer_duration < config_->high_level())
94 high_level_buffer_cb_.Run(buffer_duration);
97 std::string BufferingState::ToString() const {
98 std::ostringstream s;
99 s << stream_id_ << " state=" << state_
100 << " media_time_ms=" << media_time_.InMilliseconds()
101 << " buffered_time_ms=" << buffered_time_.InMilliseconds()
102 << " low_level_ms=" << config_->low_level().InMilliseconds()
103 << " high_level_ms=" << config_->high_level().InMilliseconds();
104 return s.str();
107 BufferingState::State BufferingState::GetBufferLevelState() const {
108 if (media_time_ == ::media::kNoTimestamp() ||
109 buffered_time_ == ::media::kNoTimestamp()) {
110 return kLowLevel;
113 base::TimeDelta buffer_duration = buffered_time_ - media_time_;
114 if (buffer_duration < config_->low_level())
115 return kLowLevel;
116 if (buffer_duration >= config_->high_level())
117 return kHighLevel;
118 return kMediumLevel;
121 void BufferingState::UpdateState(State new_state) {
122 if (new_state == state_)
123 return;
125 state_ = new_state;
126 if (!state_changed_cb_.is_null())
127 state_changed_cb_.Run();
130 } // namespace media
131 } // namespace chromecast