Shaka Packager SDK
bandwidth_estimator.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef MPD_BASE_BANDWIDTH_ESTIMATOR_H_
8 #define MPD_BASE_BANDWIDTH_ESTIMATOR_H_
9 
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include <deque>
14 
15 namespace shaka {
16 
18  public:
20  static constexpr size_t kUseAllBlocks = 0;
21  explicit BandwidthEstimator(size_t num_blocks);
23 
26  void AddBlock(uint64_t size, double duration);
27 
31  uint64_t Estimate() const;
32 
36  uint64_t Max() const;
37 
38  private:
39  BandwidthEstimator(const BandwidthEstimator&) = delete;
40  BandwidthEstimator& operator=(const BandwidthEstimator&) = delete;
41 
42  // A sliding queue that provide convenient functions to get the minimum value
43  // and the sum when window slides.
44  class SlidingQueue {
45  public:
46  // |window_size| defines the size of the sliding window. 0 uses all.
47  explicit SlidingQueue(size_t window_size);
48 
49  // Add a new value. Old values may be moved out.
50  void Add(double value);
51 
52  // Return the sum of the values in the sliding window.
53  double sum() const { return sum_; }
54  // Return the number of values in the sliding window.
55  double size() const { return size_; }
56  // Return the minimum value of the values in the sliding window.
57  double min() const { return min_.front(); }
58 
59  private:
60  SlidingQueue(const SlidingQueue&) = delete;
61  SlidingQueue& operator=(const SlidingQueue&) = delete;
62 
63  const size_t window_size_;
64  size_t size_ = 0;
65  double sum_ = 0;
66  // Keeps track of the values in the sliding window. Not needed if
67  // |window_size| is kUseAllBlocks.
68  std::deque<double> window_;
69  // Keeps track of a monotonic non-decreasing sequence of values, i.e.
70  // local minimum values in the sliding window. The front() is always the
71  // global minimum, i.e. the minimum value in the sliding window.
72  // This is achieved through:
73  // (1) New value is added to the back with the original values before it
74  // that are larger removed as they are no longer useful.
75  // (2) When a value is removed from |window_|, if it is the minimum value,
76  // it is also removed from |min_|; if it is not, it means the value is not
77  // present in |min_|.
78  std::deque<double> min_;
79  };
80  SlidingQueue sliding_queue_;
81 };
82 
83 } // namespace shaka
84 
85 #endif // MPD_BASE_BANDWIDTH_ESTIMATOR_H_
static constexpr size_t kUseAllBlocks
All the methods that are virtual are virtual for mocking.
void AddBlock(uint64_t size, double duration)