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