Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / chromecast / media / cma / base / buffering_state.cc
blobeb145e1835260454de5680d0697554774500d3c8
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 "base/strings/string_number_conversions.h"
11 #include "media/base/buffers.h"
13 namespace chromecast {
14 namespace media {
16 BufferingConfig::BufferingConfig(
17 base::TimeDelta low_level_threshold,
18 base::TimeDelta high_level_threshold)
19 : low_level_threshold_(low_level_threshold),
20 high_level_threshold_(high_level_threshold) {
23 BufferingConfig::~BufferingConfig() {
27 BufferingState::BufferingState(
28 const std::string& stream_id,
29 const scoped_refptr<BufferingConfig>& config,
30 const base::Closure& state_changed_cb,
31 const HighLevelBufferCB& high_level_buffer_cb)
32 : stream_id_(stream_id),
33 config_(config),
34 state_changed_cb_(state_changed_cb),
35 high_level_buffer_cb_(high_level_buffer_cb),
36 state_(kLowLevel),
37 media_time_(::media::kNoTimestamp()),
38 max_rendering_time_(::media::kNoTimestamp()),
39 buffered_time_(::media::kNoTimestamp()) {
42 BufferingState::~BufferingState() {
45 void BufferingState::OnConfigChanged() {
46 state_ = GetBufferLevelState();
49 void BufferingState::SetMediaTime(base::TimeDelta media_time) {
50 media_time_ = media_time;
51 switch (state_) {
52 case kLowLevel:
53 case kMediumLevel:
54 case kHighLevel:
55 UpdateState(GetBufferLevelState());
56 break;
57 case kEosReached:
58 break;
62 void BufferingState::SetMaxRenderingTime(base::TimeDelta max_rendering_time) {
63 max_rendering_time_ = max_rendering_time;
66 base::TimeDelta BufferingState::GetMaxRenderingTime() const {
67 return max_rendering_time_;
70 void BufferingState::SetBufferedTime(base::TimeDelta buffered_time) {
71 buffered_time_ = buffered_time;
72 switch (state_) {
73 case kLowLevel:
74 case kMediumLevel:
75 case kHighLevel:
76 UpdateState(GetBufferLevelState());
77 break;
78 case kEosReached:
79 break;
83 void BufferingState::NotifyEos() {
84 UpdateState(kEosReached);
87 void BufferingState::NotifyMaxCapacity(base::TimeDelta buffered_time) {
88 if (media_time_ == ::media::kNoTimestamp() ||
89 buffered_time == ::media::kNoTimestamp()) {
90 LOG(WARNING) << "Max capacity with no timestamp";
91 return;
93 base::TimeDelta buffer_duration = buffered_time - media_time_;
94 if (buffer_duration < config_->high_level())
95 high_level_buffer_cb_.Run(buffer_duration);
98 static const char* StateToString(BufferingState::State state) {
99 switch(state) {
100 case BufferingState::kLowLevel:
101 return "kLowLevel";
102 case BufferingState::kMediumLevel:
103 return "kMediumLevel";
104 case BufferingState::kHighLevel:
105 return "kHighLevel";
106 case BufferingState::kEosReached:
107 return "kEosReached";
108 default:
109 NOTREACHED();
111 NOTREACHED();
112 return "";
115 static std::string TimeDeltaToString(const base::TimeDelta& t) {
116 if (t == ::media::kNoTimestamp())
117 return "kNoTimestamp";
118 return base::DoubleToString(t.InSecondsF());
121 std::string BufferingState::ToString() const {
122 std::ostringstream s;
123 s << stream_id_ << " state=" << StateToString(state_)
124 << " media_time=" << TimeDeltaToString(media_time_)
125 << " buffered_time=" << TimeDeltaToString(buffered_time_);
126 return s.str();
129 BufferingState::State BufferingState::GetBufferLevelState() const {
130 if (media_time_ == ::media::kNoTimestamp() ||
131 buffered_time_ == ::media::kNoTimestamp()) {
132 return kLowLevel;
135 base::TimeDelta buffer_duration = buffered_time_ - media_time_;
136 if (buffer_duration < config_->low_level())
137 return kLowLevel;
138 if (buffer_duration >= config_->high_level())
139 return kHighLevel;
140 return kMediumLevel;
143 void BufferingState::UpdateState(State new_state) {
144 if (new_state == state_)
145 return;
147 state_ = new_state;
148 if (!state_changed_cb_.is_null())
149 state_changed_cb_.Run();
152 } // namespace media
153 } // namespace chromecast