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"
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "media/base/buffers.h"
13 namespace chromecast
{
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
),
34 state_changed_cb_(state_changed_cb
),
35 high_level_buffer_cb_(high_level_buffer_cb
),
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
;
55 UpdateState(GetBufferLevelState());
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
;
76 UpdateState(GetBufferLevelState());
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";
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
) {
100 case BufferingState::kLowLevel
:
102 case BufferingState::kMediumLevel
:
103 return "kMediumLevel";
104 case BufferingState::kHighLevel
:
106 case BufferingState::kEosReached
:
107 return "kEosReached";
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_
);
129 BufferingState::State
BufferingState::GetBufferLevelState() const {
130 if (media_time_
== ::media::kNoTimestamp() ||
131 buffered_time_
== ::media::kNoTimestamp()) {
135 base::TimeDelta buffer_duration
= buffered_time_
- media_time_
;
136 if (buffer_duration
< config_
->low_level())
138 if (buffer_duration
>= config_
->high_level())
143 void BufferingState::UpdateState(State new_state
) {
144 if (new_state
== state_
)
148 if (!state_changed_cb_
.is_null())
149 state_changed_cb_
.Run();
153 } // namespace chromecast