| 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/base/buffering_state.h" | 5 #include "chromecast/media/cma/base/buffering_state.h" |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "media/base/timestamp_constants.h" | 11 #include "media/base/timestamp_constants.h" |
| 12 | 12 |
| 13 namespace chromecast { | 13 namespace chromecast { |
| 14 namespace media { | 14 namespace media { |
| 15 | 15 |
| 16 BufferingConfig::BufferingConfig( | 16 BufferingConfig::BufferingConfig( |
| 17 base::TimeDelta low_level_threshold, | 17 base::TimeDelta low_level_threshold, |
| 18 base::TimeDelta high_level_threshold) | 18 base::TimeDelta high_level_threshold) |
| 19 : low_level_threshold_(low_level_threshold), | 19 : low_level_threshold_(low_level_threshold), |
| 20 high_level_threshold_(high_level_threshold) { | 20 high_level_threshold_(high_level_threshold) { |
| 21 } | 21 } |
| 22 | 22 |
| 23 BufferingConfig::~BufferingConfig() { | 23 BufferingConfig::~BufferingConfig() { |
| 24 } | 24 } |
| 25 | 25 |
| 26 | 26 BufferingState::BufferingState(const std::string& stream_id, |
| 27 BufferingState::BufferingState( | 27 const scoped_refptr<BufferingConfig>& config, |
| 28 const std::string& stream_id, | 28 const base::Closure& state_changed_cb, |
| 29 const scoped_refptr<BufferingConfig>& config, | 29 const HighLevelBufferCB& high_level_buffer_cb) |
| 30 const base::Closure& state_changed_cb, | |
| 31 const HighLevelBufferCB& high_level_buffer_cb) | |
| 32 : stream_id_(stream_id), | 30 : stream_id_(stream_id), |
| 33 config_(config), | 31 config_(config), |
| 34 state_changed_cb_(state_changed_cb), | 32 state_changed_cb_(state_changed_cb), |
| 35 high_level_buffer_cb_(high_level_buffer_cb), | 33 high_level_buffer_cb_(high_level_buffer_cb), |
| 36 state_(kLowLevel), | 34 state_(kLowLevel), |
| 37 media_time_(::media::kNoTimestamp()), | 35 media_time_(::media::kNoTimestamp), |
| 38 max_rendering_time_(::media::kNoTimestamp()), | 36 max_rendering_time_(::media::kNoTimestamp), |
| 39 buffered_time_(::media::kNoTimestamp()) { | 37 buffered_time_(::media::kNoTimestamp) {} |
| 40 } | |
| 41 | 38 |
| 42 BufferingState::~BufferingState() { | 39 BufferingState::~BufferingState() { |
| 43 } | 40 } |
| 44 | 41 |
| 45 void BufferingState::OnConfigChanged() { | 42 void BufferingState::OnConfigChanged() { |
| 46 state_ = GetBufferLevelState(); | 43 state_ = GetBufferLevelState(); |
| 47 } | 44 } |
| 48 | 45 |
| 49 void BufferingState::SetMediaTime(base::TimeDelta media_time) { | 46 void BufferingState::SetMediaTime(base::TimeDelta media_time) { |
| 50 media_time_ = media_time; | 47 media_time_ = media_time; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 78 case kEosReached: | 75 case kEosReached: |
| 79 break; | 76 break; |
| 80 } | 77 } |
| 81 } | 78 } |
| 82 | 79 |
| 83 void BufferingState::NotifyEos() { | 80 void BufferingState::NotifyEos() { |
| 84 UpdateState(kEosReached); | 81 UpdateState(kEosReached); |
| 85 } | 82 } |
| 86 | 83 |
| 87 void BufferingState::NotifyMaxCapacity(base::TimeDelta buffered_time) { | 84 void BufferingState::NotifyMaxCapacity(base::TimeDelta buffered_time) { |
| 88 if (media_time_ == ::media::kNoTimestamp() || | 85 if (media_time_ == ::media::kNoTimestamp || |
| 89 buffered_time == ::media::kNoTimestamp()) { | 86 buffered_time == ::media::kNoTimestamp) { |
| 90 LOG(WARNING) << "Max capacity with no timestamp"; | 87 LOG(WARNING) << "Max capacity with no timestamp"; |
| 91 return; | 88 return; |
| 92 } | 89 } |
| 93 base::TimeDelta buffer_duration = buffered_time - media_time_; | 90 base::TimeDelta buffer_duration = buffered_time - media_time_; |
| 94 if (buffer_duration < config_->high_level()) | 91 if (buffer_duration < config_->high_level()) |
| 95 high_level_buffer_cb_.Run(buffer_duration); | 92 high_level_buffer_cb_.Run(buffer_duration); |
| 96 } | 93 } |
| 97 | 94 |
| 98 static const char* StateToString(BufferingState::State state) { | 95 static const char* StateToString(BufferingState::State state) { |
| 99 switch(state) { | 96 switch (state) { |
| 100 case BufferingState::kLowLevel: | 97 case BufferingState::kLowLevel: |
| 101 return "kLowLevel"; | 98 return "kLowLevel"; |
| 102 case BufferingState::kMediumLevel: | 99 case BufferingState::kMediumLevel: |
| 103 return "kMediumLevel"; | 100 return "kMediumLevel"; |
| 104 case BufferingState::kHighLevel: | 101 case BufferingState::kHighLevel: |
| 105 return "kHighLevel"; | 102 return "kHighLevel"; |
| 106 case BufferingState::kEosReached: | 103 case BufferingState::kEosReached: |
| 107 return "kEosReached"; | 104 return "kEosReached"; |
| 108 default: | 105 default: |
| 109 NOTREACHED(); | 106 NOTREACHED(); |
| 110 } | 107 } |
| 111 NOTREACHED(); | 108 NOTREACHED(); |
| 112 return ""; | 109 return ""; |
| 113 } | 110 } |
| 114 | 111 |
| 115 static std::string TimeDeltaToString(const base::TimeDelta& t) { | 112 static std::string TimeDeltaToString(const base::TimeDelta& t) { |
| 116 if (t == ::media::kNoTimestamp()) | 113 if (t == ::media::kNoTimestamp) |
| 117 return "kNoTimestamp"; | 114 return "kNoTimestamp"; |
| 118 return base::DoubleToString(t.InSecondsF()); | 115 return base::DoubleToString(t.InSecondsF()); |
| 119 } | 116 } |
| 120 | 117 |
| 121 std::string BufferingState::ToString() const { | 118 std::string BufferingState::ToString() const { |
| 122 std::ostringstream s; | 119 std::ostringstream s; |
| 123 s << stream_id_ << " state=" << StateToString(state_) | 120 s << stream_id_ << " state=" << StateToString(state_) |
| 124 << " media_time=" << TimeDeltaToString(media_time_) | 121 << " media_time=" << TimeDeltaToString(media_time_) |
| 125 << " buffered_time=" << TimeDeltaToString(buffered_time_); | 122 << " buffered_time=" << TimeDeltaToString(buffered_time_); |
| 126 return s.str(); | 123 return s.str(); |
| 127 } | 124 } |
| 128 | 125 |
| 129 BufferingState::State BufferingState::GetBufferLevelState() const { | 126 BufferingState::State BufferingState::GetBufferLevelState() const { |
| 130 if (media_time_ == ::media::kNoTimestamp() || | 127 if (media_time_ == ::media::kNoTimestamp || |
| 131 buffered_time_ == ::media::kNoTimestamp()) { | 128 buffered_time_ == ::media::kNoTimestamp) { |
| 132 return kLowLevel; | 129 return kLowLevel; |
| 133 } | 130 } |
| 134 | 131 |
| 135 base::TimeDelta buffer_duration = buffered_time_ - media_time_; | 132 base::TimeDelta buffer_duration = buffered_time_ - media_time_; |
| 136 if (buffer_duration < config_->low_level()) | 133 if (buffer_duration < config_->low_level()) |
| 137 return kLowLevel; | 134 return kLowLevel; |
| 138 if (buffer_duration >= config_->high_level()) | 135 if (buffer_duration >= config_->high_level()) |
| 139 return kHighLevel; | 136 return kHighLevel; |
| 140 return kMediumLevel; | 137 return kMediumLevel; |
| 141 } | 138 } |
| 142 | 139 |
| 143 void BufferingState::UpdateState(State new_state) { | 140 void BufferingState::UpdateState(State new_state) { |
| 144 if (new_state == state_) | 141 if (new_state == state_) |
| 145 return; | 142 return; |
| 146 | 143 |
| 147 state_ = new_state; | 144 state_ = new_state; |
| 148 if (!state_changed_cb_.is_null()) | 145 if (!state_changed_cb_.is_null()) |
| 149 state_changed_cb_.Run(); | 146 state_changed_cb_.Run(); |
| 150 } | 147 } |
| 151 | 148 |
| 152 } // namespace media | 149 } // namespace media |
| 153 } // namespace chromecast | 150 } // namespace chromecast |
| OLD | NEW |