Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: media/base/download_rate_monitor.h

Issue 8399023: Fire canplaythrough event at the proper time for audio/video (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Responses to CR Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 explicit DownloadRateMonitor();
scherkus (not reviewing) 2011/11/16 02:15:17 nit: explicit isn't needed
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
29 // Notifies the monitor of the current number of bytes buffered by the media
30 // file at what timestamp. The monitor expects subsequent calls to
31 // SetBufferedBytes to have monotonically nondecreasing timestamps.
32 // Calls to the method are ignored if monitor has not been started or is
33 // stopped.
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; }
scherkus (not reviewing) 2011/11/16 02:15:17 trivial setters/getters should be unix_hacker() st
42
43 // Sets whether the media file is a fully loaded source.
44 void SetLoaded(bool loaded) { loaded_ = loaded; }
scherkus (not reviewing) 2011/11/16 02:15:17 set_loaded()
45
46 // Stop monitoring download rate. This does not discard previously learned
47 // information, but it will no longer factor incoming information into its
48 // canplaythrough estimation.
49 void Stop();
50
51 // Resets monitor to uninitialized state.
52 void Reset();
53
54 private:
55 // Represents a point in time in which the media was buffering data.
56 struct BufferingPoint {
57 // The number of bytes buffered by the media player at |timestamp|.
58 int64 buffered_bytes;
59 // Time at which buffering measurement was taken.
60 base::Time timestamp;
61 };
62
63 // Represents a span of time in which the media was buffering data.
64 class Sample {
65 public:
66 explicit Sample();
scherkus (not reviewing) 2011/11/16 02:15:17 no explicit
67 Sample(const BufferingPoint& start, const BufferingPoint& end);
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698