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