Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromecast / media / cma / base / buffering_controller.h
blobb0ccecee1a17013e397cbc48d46e700129026eca
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_CONTROLLER_H
6 #define CHROMECAST_MEDIA_CMA_BASE_BUFFERING_CONTROLLER_H
8 #include <list>
9 #include <string>
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/time/time.h"
18 namespace chromecast {
19 namespace media {
20 class BufferingConfig;
21 class BufferingState;
23 class BufferingController {
24 public:
25 typedef base::Callback<void(bool)> BufferingNotificationCB;
27 // Creates a buffering controller where the conditions to trigger rebuffering
28 // are given by |config|. The whole point of the buffering controller is to
29 // derive a single buffering state from the buffering state of various
30 // streams.
31 // |buffering_notification_cb| is a callback invoked to inform about possible
32 // changes of the buffering state.
33 BufferingController(
34 const scoped_refptr<BufferingConfig>& config,
35 const BufferingNotificationCB& buffering_notification_cb);
36 ~BufferingController();
38 // Creates a buffering state for one stream. This state is added to the list
39 // of streams monitored by the buffering controller.
40 scoped_refptr<BufferingState> AddStream(const std::string& stream_id);
42 // Sets the playback time.
43 void SetMediaTime(base::TimeDelta time);
45 // Returns the maximum media time available for rendering.
46 // Return kNoTimestamp() if unknown.
47 base::TimeDelta GetMaxRenderingTime() const;
49 // Returns whether there is an active buffering phase.
50 bool IsBuffering() const { return is_buffering_; }
52 // Resets the buffering controller. This includes removing all the streams
53 // that were previously added.
54 void Reset();
56 private:
57 // Invoked each time the buffering state of one of the streams has changed.
58 // If |force_notification| is set, |buffering_notification_cb_| is invoked
59 // regardless whether the buffering state has changed or not.
60 // If |buffering_timeout| is set, then the condition to leave the buffering
61 // state is relaxed (we don't want to wait more).
62 void OnBufferingStateChanged(bool force_notification,
63 bool buffering_timeout);
65 // Updates the high buffer level threshold to |high_level_threshold|
66 // if needed.
67 // This condition is triggered when one of the stream reached its maximum
68 // capacity. In that case, to avoid possible race condition (the buffering
69 // controller waits for more data to come but the buffer is to small to
70 // accomodate additional data), the thresholds in |config_| are adjusted
71 // accordingly.
72 void UpdateHighLevelThreshold(base::TimeDelta high_level_threshold);
74 // Determines the overall buffer level based on the buffer level of each
75 // stream.
76 bool IsHighBufferLevel();
77 bool IsLowBufferLevel();
79 // Logs the state of the buffering controller.
80 void DumpState() const;
82 base::ThreadChecker thread_checker_;
84 // Settings used to determine when to start/stop buffering.
85 scoped_refptr<BufferingConfig> config_;
87 // Callback invoked each time there is a change of the buffering state.
88 BufferingNotificationCB buffering_notification_cb_;
90 // State of the buffering controller.
91 bool is_buffering_;
93 // Start time of a re-buffering phase.
94 base::Time begin_buffering_time_;
95 bool initial_buffering_;
97 // Buffering level for each individual stream.
98 typedef std::list<scoped_refptr<BufferingState> > StreamList;
99 StreamList stream_list_;
101 base::WeakPtr<BufferingController> weak_this_;
102 base::WeakPtrFactory<BufferingController> weak_factory_;
104 DISALLOW_COPY_AND_ASSIGN(BufferingController);
107 } // namespace media
108 } // namespace chromecast
110 #endif // CHROMECAST_MEDIA_CMA_BASE_BUFFERING_CONTROLLER_H