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