Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromecast / media / cma / base / buffering_state.h
blob3e1fe5d811c85b556efb13c3b6df9c6ca27480a7
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 #ifndef CHROMECAST_MEDIA_CMA_BASE_BUFFERING_STATE_H_
6 #define CHROMECAST_MEDIA_CMA_BASE_BUFFERING_STATE_H_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/time/time.h"
15 namespace chromecast {
16 namespace media {
18 class BufferingConfig : public base::RefCountedThreadSafe<BufferingConfig> {
19 public:
20 BufferingConfig(base::TimeDelta low_level_threshold,
21 base::TimeDelta high_level_threshold);
23 base::TimeDelta low_level() const { return low_level_threshold_; }
24 base::TimeDelta high_level() const { return high_level_threshold_; }
26 void set_low_level(base::TimeDelta low_level) {
27 low_level_threshold_ = low_level;
29 void set_high_level(base::TimeDelta high_level) {
30 high_level_threshold_ = high_level;
33 private:
34 friend class base::RefCountedThreadSafe<BufferingConfig>;
35 virtual ~BufferingConfig();
37 base::TimeDelta low_level_threshold_;
38 base::TimeDelta high_level_threshold_;
40 DISALLOW_COPY_AND_ASSIGN(BufferingConfig);
43 class BufferingState
44 : public base::RefCountedThreadSafe<BufferingState> {
45 public:
46 typedef base::Callback<void(base::TimeDelta)> HighLevelBufferCB;
48 enum State {
49 kLowLevel,
50 kMediumLevel,
51 kHighLevel,
52 kEosReached,
55 // Creates a new buffering state. The initial state is |kLowLevel|.
56 // |state_changed_cb| is used to notify about possible state changes.
57 // |high_level_buffer_cb| is used to adjust the high buffer threshold
58 // when the underlying buffer is not large enough to accomodate
59 // the current high buffer level.
60 BufferingState(const std::string& stream_id,
61 const scoped_refptr<BufferingConfig>& config,
62 const base::Closure& state_changed_cb,
63 const HighLevelBufferCB& high_level_buffer_cb);
65 // Returns the buffering state.
66 State GetState() const { return state_; }
68 // Invoked when the buffering configuration has changed.
69 // Based on the new configuration, the buffering state might change.
70 // However, |state_changed_cb_| is not triggered in that case.
71 void OnConfigChanged();
73 // Sets the current rendering time for this stream.
74 void SetMediaTime(base::TimeDelta media_time);
76 // Sets/gets the maximum rendering media time for this stream.
77 // The maximum rendering time is always lower than the buffered time.
78 void SetMaxRenderingTime(base::TimeDelta max_rendering_time);
79 base::TimeDelta GetMaxRenderingTime() const;
81 // Sets the buffered time.
82 void SetBufferedTime(base::TimeDelta buffered_time);
84 // Notifies the buffering state that all the frames for this stream have been
85 // buffered, i.e. the end of stream has been reached.
86 void NotifyEos();
88 // Notifies the buffering state the underlying buffer has reached
89 // its maximum capacity.
90 // The maximum frame timestamp in the buffer is given by |buffered_time|.
91 // Note: this timestamp can be different from the one provided through
92 // SetBufferedTime since SetBufferedTime takes the timestamp of a playable
93 // frame which is not necessarily the case here (e.g. missing key id).
94 void NotifyMaxCapacity(base::TimeDelta buffered_time);
96 // Buffering state as a human readable string, for debugging.
97 std::string ToString() const;
99 private:
100 friend class base::RefCountedThreadSafe<BufferingState>;
101 virtual ~BufferingState();
103 // Returns the state solely based on the buffered time.
104 State GetBufferLevelState() const;
106 // Updates the state to |new_state|.
107 void UpdateState(State new_state);
109 std::string const stream_id_;
110 scoped_refptr<BufferingConfig> const config_;
112 // Callback invoked each time there is a change of state.
113 base::Closure state_changed_cb_;
115 // Callback invoked to adjust the high buffer level.
116 HighLevelBufferCB high_level_buffer_cb_;
118 // State.
119 State state_;
121 // Playback media time.
122 // Equal to kNoTimestamp() when not known.
123 base::TimeDelta media_time_;
125 // Maximum rendering media time.
126 // This corresponds to the timestamp of the last frame sent to the hardware
127 // decoder/renderer.
128 base::TimeDelta max_rendering_time_;
130 // Buffered media time.
131 // Equal to kNoTimestamp() when not known.
132 base::TimeDelta buffered_time_;
134 DISALLOW_COPY_AND_ASSIGN(BufferingState);
137 } // namespace media
138 } // namespace chromecast
140 #endif // CHROMECAST_MEDIA_CMA_BASE_BUFFERING_STATE_H_