OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #ifndef MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_ |
| 6 #define MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_ |
| 7 |
| 8 #include <deque> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/time.h" |
| 13 #include "media/base/media_export.h" |
| 14 |
| 15 namespace media { |
| 16 |
| 17 // Measures download speed based on the rate at which a media file is buffering. |
| 18 // Signals when it believes the media file can be played back without needing to |
| 19 // pause to buffer. |
| 20 class MEDIA_EXPORT DownloadRateMonitor { |
| 21 public: |
| 22 DownloadRateMonitor(); |
| 23 ~DownloadRateMonitor(); |
| 24 |
| 25 // Begin measuring download rate. The monitor will run |canplaythrough_cb| |
| 26 // when it believes the media can be played back without needing to pause to |
| 27 // buffer. |media_bitrate| is the bitrate of the video. |
| 28 void Start(const base::Closure& canplaythrough_cb, int media_bitrate); |
| 29 |
| 30 // Notifies the monitor of the current number of bytes buffered by the media |
| 31 // file at what timestamp. The monitor expects subsequent calls to |
| 32 // SetBufferedBytes to have monotonically nondecreasing timestamps. |
| 33 // Calls to the method are ignored if monitor has not been started or is |
| 34 // stopped. |
| 35 void SetBufferedBytes(int64 buffered_bytes, const base::Time& timestamp); |
| 36 |
| 37 // Notifies the monitor when the media file has paused or continued |
| 38 // downloading data. |
| 39 void SetNetworkActivity(bool is_downloading_data); |
| 40 |
| 41 void set_total_bytes(int64 total_bytes) { total_bytes_ = total_bytes; } |
| 42 |
| 43 void set_loaded(bool loaded) { loaded_ = loaded; } |
| 44 |
| 45 // Stop monitoring download rate. This does not discard previously learned |
| 46 // information, but it will no longer factor incoming information into its |
| 47 // canplaythrough estimation. |
| 48 void Stop(); |
| 49 |
| 50 // Resets monitor to uninitialized state. |
| 51 void Reset(); |
| 52 |
| 53 private: |
| 54 // Represents a point in time in which the media was buffering data. |
| 55 struct BufferingPoint { |
| 56 // The number of bytes buffered by the media player at |timestamp|. |
| 57 int64 buffered_bytes; |
| 58 // Time at which buffering measurement was taken. |
| 59 base::Time timestamp; |
| 60 }; |
| 61 |
| 62 // Represents a span of time in which the media was buffering data. |
| 63 class Sample { |
| 64 public: |
| 65 Sample(); |
| 66 Sample(const BufferingPoint& start, const BufferingPoint& end); |
| 67 ~Sample(); |
| 68 |
| 69 // Set the end point of the data sample. |
| 70 void set_end(const BufferingPoint& new_end); |
| 71 |
| 72 const BufferingPoint& start() const { return start_; } |
| 73 const BufferingPoint& end() const { return end_; } |
| 74 |
| 75 // Returns the bytes downloaded per second in this sample. Returns -1.0 if |
| 76 // sample is invalid. |
| 77 float bytes_per_second() const; |
| 78 |
| 79 // Returns the seconds elapsed in this sample. Returns -1.0 if the sample |
| 80 // has not begun yet. |
| 81 float seconds_elapsed() const; |
| 82 |
| 83 // Returns bytes downloaded in this sample. Returns -1.0 if the sample has |
| 84 // not begun yet. |
| 85 int64 bytes_downloaded() const; |
| 86 |
| 87 // Returns true if Sample has not been initialized. |
| 88 bool is_null() const; |
| 89 |
| 90 // Resets the sample to an uninitialized state. |
| 91 void Reset(); |
| 92 |
| 93 // Restarts the sample to begin its measurement at what was previously its |
| 94 // end point. |
| 95 void RestartAtEndBufferingPoint(); |
| 96 |
| 97 private: |
| 98 BufferingPoint start_; |
| 99 BufferingPoint end_; |
| 100 }; |
| 101 |
| 102 int64 bytes_downloaded_in_window() const; |
| 103 float seconds_elapsed_in_window() const; |
| 104 |
| 105 // Updates window with latest sample if it is ready. |
| 106 void UpdateSampleWindow(); |
| 107 |
| 108 // Returns an approximation of the current download rate in bytes per second. |
| 109 // Returns -1.0 if unknown. |
| 110 float ApproximateDownloadByteRate() const; |
| 111 |
| 112 // Helper method that returns true if the monitor believes it should fire the |
| 113 // |canplaythrough_cb_|. |
| 114 bool ShouldNotifyCanPlayThrough(); |
| 115 |
| 116 // Examines the download rate and fires the |canplaythrough_cb_| callback if |
| 117 // the monitor deems it the right time. |
| 118 void NotifyCanPlayThroughIfNeeded(); |
| 119 |
| 120 // Callback to run when the monitor believes the media can play through |
| 121 // without needing to pause to buffer. |
| 122 base::Closure canplaythrough_cb_; |
| 123 |
| 124 // Indicates whether the monitor has run the |canplaythrough_cb_|. |
| 125 bool has_notified_can_play_through_; |
| 126 |
| 127 // Measurements used to approximate download speed. |
| 128 Sample current_sample_; |
| 129 std::deque<Sample> sample_window_; |
| 130 |
| 131 // True if actively downloading bytes, false otherwise. |
| 132 bool is_downloading_data_; |
| 133 |
| 134 // Total number of bytes in the media file, 0 if unknown or undefined. |
| 135 int64 total_bytes_; |
| 136 |
| 137 // Amount of bytes buffered. |
| 138 int64 buffered_bytes_; |
| 139 |
| 140 // True if the media file is a fully loaded source, e.g. file:// protocol. |
| 141 bool loaded_; |
| 142 |
| 143 // Bitrate of the media file, 0 if unknown. |
| 144 int bitrate_; |
| 145 |
| 146 // True if the monitor has not yet started or has been stopped, false |
| 147 // otherwise. |
| 148 bool stopped_; |
| 149 |
| 150 DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitor); |
| 151 }; |
| 152 |
| 153 } // namespace media |
| 154 |
| 155 #endif // MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_ |
OLD | NEW |