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

Unified 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: Rebase and minor fix 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 side-by-side diff with in-line comments
Download patch
Index: media/base/download_rate_monitor.h
diff --git a/media/base/download_rate_monitor.h b/media/base/download_rate_monitor.h
new file mode 100644
index 0000000000000000000000000000000000000000..145f9488d17c7982f35ccb5c6f26bae01a582c04
--- /dev/null
+++ b/media/base/download_rate_monitor.h
@@ -0,0 +1,144 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_
+#define MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_
+
+#include <deque>
+#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 :)
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/time.h"
+
+namespace media {
+
+// Measures download speed based on the rate at which a media file is buffering.
+// Signals when it believes the media file can be played back without needing to
+// pause to buffer.
+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!
+ public:
+ typedef base::Callback<void()> CanPlayThroughCB;
+
+ DownloadRateMonitor();
+
+ // Begin measuring download rate. The monitor will run |canplaythrough_cb|
+ // when it believes the media can be played back without needing to pause to
+ // buffer. |media_bitrate| is the bitrate of the video.
+ 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
+
+ // Notifies the monitor of the current number of bytes buffered by the media
+ // file at what timestamp. The monitor expects subsequent calls to
+ // SetBufferedBytes to have monotonically nondecreasing values.
+ void SetBufferedBytes(int64 buffered_bytes, const base::Time& timestamp);
+
+ // Notifies the monitor when the media file has paused or continued
+ // downloading data.
+ void SetNetworkActivity(bool is_downloading_data);
+
+ // Sets the size of the media file in bytes.
+ void SetTotalBytes(int64 total_bytes) { total_bytes_ = total_bytes; }
+
+ // Sets whether the media file is a fully loaded source.
+ void SetLoaded(bool loaded) { loaded_ = loaded; }
+
+ private:
+ // Represents a span of time in which the media was buffering data.
+ class Sample {
+ public:
+ Sample();
+
+ // The buffering state at a point in time. The first value represents the
+ // number of bytes buffered by the media player, the second value represents
+ // the time at which the buffering measurement was taken.
+ 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.
+
+ // Set the start point of the data sample.
+ void SetStartPoint(const Point& new_start);
+
+ // Set the end point of the data sample.
+ void SetEndPoint(const Point& new_end);
+
+ 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.
+ const Point& end_point() const { return end_; }
+
+ // Returns the bytes downloaded per second in this sample. Returns -1.0 if
+ // sample is invalid.
+ float bytes_per_second() const;
+
+ // Returns the seconds elapsed in this sample. Returns -1.0 if the sample
+ // has not begun yet.
+ float seconds_elapsed() const;
+
+ // Returns bytes downloaded in this sample.
+ int64 bytes_downloaded() const;
+
+ // 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.
+ bool is_null() const;
+
+ // Resets the sample to an uninitialized state.
+ void Reset();
+
+ // Restarts the sample to begin its measurement at what was previously its
+ // end point.
+ void RestartAtEndPoint();
+
+ private:
+ Point start_;
+ Point end_;
+ };
+
+ int64 bytes_downloaded_in_window() const;
+ float seconds_elapsed_in_window() const;
+
+ // Updates window with latest sample if it is ready.
+ void UpdateSampleWindow();
+
+ // Returns an approximation of the current download rate in bytes per second.
+ // Returns -1.0 if unknown.
+ float ApproximateDownloadByteRate() const;
+
+ // Helper method that returns true if the monitor believes it should fire the
+ // |canplaythrough_cb_|.
+ bool ShouldNotifyCanPlayThrough();
+
+ // Examines the download rate and fires the |canplaythrough_cb_| callback if
+ // the monitor deems it the right time.
+ void NotifyCanPlayThroughIfNeeded();
+
+ // Callback to run when the monitor believes the media can play through
+ // without needing to pause to buffer.
+ CanPlayThroughCB canplaythrough_cb_;
+
+ // Indicates whether the monitor has run the |canplaythrough_cb_|.
+ bool has_notified_can_play_through_;
+
+ // Measurements used to approximate download speed.
+ Sample current_sample_;
+ std::deque<Sample> sample_window_;
+
+ // True if actively downloading bytes, false otherwise.
+ bool is_downloading_data_;
+
+ // Total number of bytes in the media file, 0 if unknown or undefined.
+ int64 total_bytes_;
+
+ // Amount of bytes buffered.
+ int64 buffered_bytes_;
+
+ // True if the media file is a fully loaded source, e.g. file:// protocol.
+ bool loaded_;
+
+ // Bitrate of the media file, 0 if unknown.
+ int bitrate_;
+
+ // True if monitor has been started, false otherwise.
+ bool started_;
+
+ DISALLOW_COPY_AND_ASSIGN(DownloadRateMonitor);
+};
+
+} // namespace media
+
+#endif // MEDIA_BASE_DOWNLOAD_RATE_MONITOR_H_

Powered by Google App Engine
This is Rietveld 408576698