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 #include <stdio.h> | |
|
scherkus (not reviewing)
2011/11/09 02:55:15
what's stdio.h used for?
vrk (LEFT CHROMIUM)
2011/11/11 02:51:06
Uhh, debug printfs! Removed :)
| |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/time.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 DownloadRateMonitor { | |
|
scherkus (not reviewing)
2011/11/09 02:55:15
class looks like a pretty good candidate for unit
vrk (LEFT CHROMIUM)
2011/11/11 02:51:06
I agree!
| |
| 21 public: | |
| 22 typedef base::Callback<void()> CanPlayThroughCB; | |
| 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); | |
|
scherkus (not reviewing)
2011/11/09 02:55:15
is there no stop?
what happens if I call these ot
vrk (LEFT CHROMIUM)
2011/11/11 02:51:06
Added Stop(), added comments about Start() and Sto
| |
| 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 values. | |
| 34 void SetBufferedBytes(int64 buffered_bytes, const base::Time& timestamp); | |
| 35 | |
| 36 // Notifies the monitor when the media file has paused or continued | |
| 37 // downloading data. | |
| 38 void SetNetworkActivity(bool is_downloading_data); | |
| 39 | |
| 40 // Sets the size of the media file in bytes. | |
| 41 void SetTotalBytes(int64 total_bytes) { total_bytes_ = total_bytes; } | |
| 42 | |
| 43 // Sets whether the media file is a fully loaded source. | |
| 44 void SetLoaded(bool loaded) { loaded_ = loaded; } | |
| 45 | |
| 46 private: | |
| 47 // Represents a span of time in which the media was buffering data. | |
| 48 class Sample { | |
| 49 public: | |
| 50 Sample(); | |
| 51 | |
| 52 // The buffering state at a point in time. The first value represents the | |
| 53 // number of bytes buffered by the media player, the second value represents | |
| 54 // the time at which the buffering measurement was taken. | |
| 55 typedef std::pair<int64, base::Time> Point; | |
|
scherkus (not reviewing)
2011/11/09 02:55:15
favour a two-member struct over std::pair
struct
vrk (LEFT CHROMIUM)
2011/11/11 02:51:06
Done.
| |
| 56 | |
| 57 // Set the start point of the data sample. | |
| 58 void SetStartPoint(const Point& new_start); | |
| 59 | |
| 60 // Set the end point of the data sample. | |
| 61 void SetEndPoint(const Point& new_end); | |
| 62 | |
| 63 const Point& start_point() const { return start_; } | |
|
acolwell GONE FROM CHROMIUM
2011/11/09 00:50:22
nit accessor style: set_start(), set_end(), start(
vrk (LEFT CHROMIUM)
2011/11/11 02:51:06
Done.
| |
| 64 const Point& end_point() const { return end_; } | |
| 65 | |
| 66 // Returns the bytes downloaded per second in this sample. Returns -1.0 if | |
| 67 // sample is invalid. | |
| 68 float bytes_per_second() const; | |
| 69 | |
| 70 // Returns the seconds elapsed in this sample. Returns -1.0 if the sample | |
| 71 // has not begun yet. | |
| 72 float seconds_elapsed() const; | |
| 73 | |
| 74 // Returns bytes downloaded in this sample. | |
| 75 int64 bytes_downloaded() const; | |
| 76 | |
| 77 // Returns true if Sample is null, false otherwise. | |
|
acolwell GONE FROM CHROMIUM
2011/11/09 00:50:22
How about "Returns true if Sample has not been ini
vrk (LEFT CHROMIUM)
2011/11/11 02:51:06
Done.
| |
| 78 bool is_null() const; | |
| 79 | |
| 80 // Resets the sample to an uninitialized state. | |
| 81 void Reset(); | |
| 82 | |
| 83 // Restarts the sample to begin its measurement at what was previously its | |
| 84 // end point. | |
| 85 void RestartAtEndPoint(); | |
| 86 | |
| 87 private: | |
| 88 Point start_; | |
| 89 Point end_; | |
| 90 }; | |
| 91 | |
| 92 int64 bytes_downloaded_in_window() const; | |
| 93 float seconds_elapsed_in_window() const; | |
| 94 | |
| 95 // Updates window with latest sample if it is ready. | |
| 96 void UpdateSampleWindow(); | |
| 97 | |
| 98 // Returns an approximation of the current download rate in bytes per second. | |
| 99 // Returns -1.0 if unknown. | |
| 100 float ApproximateDownloadByteRate() const; | |
| 101 | |
| 102 // Helper method that returns true if the monitor believes it should fire the | |
| 103 // |canplaythrough_cb_|. | |
| 104 bool ShouldNotifyCanPlayThrough(); | |
| 105 | |
| 106 // Examines the download rate and fires the |canplaythrough_cb_| callback if | |
| 107 // the monitor deems it the right time. | |
| 108 void NotifyCanPlayThroughIfNeeded(); | |
| 109 | |
| 110 // Callback to run when the monitor believes the media can play through | |
| 111 // without needing to pause to buffer. | |
| 112 CanPlayThroughCB canplaythrough_cb_; | |
| 113 | |
| 114 // Indicates whether the monitor has run the |canplaythrough_cb_|. | |
| 115 bool has_notified_can_play_through_; | |
| 116 | |
| 117 // Measurements used to approximate download speed. | |
| 118 Sample current_sample_; | |
| 119 std::deque<Sample> sample_window_; | |
| 120 | |
| 121 // True if actively downloading bytes, false otherwise. | |
| 122 bool is_downloading_data_; | |
| 123 | |
| 124 // Total number of bytes in the media file, 0 if unknown or undefined. | |
| 125 int64 total_bytes_; | |
| 126 | |
| 127 // Amount of bytes buffered. | |
| 128 int64 buffered_bytes_; | |
| 129 | |
| 130 // True if the media file is a fully loaded source, e.g. file:// protocol. | |
| 131 bool loaded_; | |
| 132 | |
| 133 // Bitrate of the media file, 0 if unknown. | |
| 134 int bitrate_; | |
| 135 | |
| 136 // True if monitor has been started, false otherwise. | |
| 137 bool started_; | |
| 138 | |
| 139 DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitor); | |
| 140 }; | |
| 141 | |
| 142 } // namespace media | |
| 143 | |
| 144 #endif // MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_ | |
| OLD | NEW |