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 "media/base/buffers.h"
12 namespace chromecast
{
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
),
33 state_changed_cb_(state_changed_cb
),
34 high_level_buffer_cb_(high_level_buffer_cb
),
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
;
54 UpdateState(GetBufferLevelState());
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
;
75 UpdateState(GetBufferLevelState());
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";
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 {
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();
107 BufferingState::State
BufferingState::GetBufferLevelState() const {
108 if (media_time_
== ::media::kNoTimestamp() ||
109 buffered_time_
== ::media::kNoTimestamp()) {
113 base::TimeDelta buffer_duration
= buffered_time_
- media_time_
;
114 if (buffer_duration
< config_
->low_level())
116 if (buffer_duration
>= config_
->high_level())
121 void BufferingState::UpdateState(State new_state
) {
122 if (new_state
== state_
)
126 if (!state_changed_cb_
.is_null())
127 state_changed_cb_
.Run();
131 } // namespace chromecast