Skip to content

Commit

Permalink
Add buffering info to the UI.
Browse files Browse the repository at this point in the history
Adds "buffered ahead" and "buffered behind" indicators to the UI.

Issue #47

Change-Id: I3e6a60ea387e97c98003a47c0e3a599e907ee184
  • Loading branch information
Timothy Drews committed Jun 8, 2015
1 parent eb6d573 commit b534b70
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
61 changes: 58 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ app.video_ = null;
app.videoResDebug_ = null;


/**
* The buffered ahead debug element owned by the app.
*
* @private {Element}
*/
app.bufferedAheadDebug_ = null;


/**
* The buffered behind debug element owned by the app.
*
* @private {Element}
*/
app.bufferedBehindDebug_ = null;


/**
* True if the aspect ratio has been set for this playback.
*
Expand Down Expand Up @@ -101,7 +117,9 @@ app.init = function() {
app.video_ =
/** @type {!HTMLVideoElement} */ (document.getElementById('video'));
app.videoResDebug_ = document.getElementById('videoResDebug');
window.setInterval(app.updateVideoSize_, 50);
app.bufferedAheadDebug_ = document.getElementById('bufferedAheadDebug');
app.bufferedBehindDebug_ = document.getElementById('bufferedBehindDebug');
window.setInterval(app.updateDebugInfo_, 50);

var fields = location.search.split('?').pop();
fields = fields ? fields.split(';') : [];
Expand Down Expand Up @@ -739,10 +757,22 @@ app.displayMetadata_ = function() {


/**
* Update video resolution information.
* Update the debug information.
* @private
*/
app.updateDebugInfo_ = function() {
app.updateVideoResDebug_();
app.updateBufferDebug_();
};


/**
* Update the video resolution information.
* @private
*/
app.updateVideoSize_ = function() {
app.updateVideoResDebug_ = function() {
console.assert(app.videoResDebug_);

if (app.aspectRatioSet_ == false) {
var aspect = app.video_.videoWidth / app.video_.videoHeight;
if (aspect) {
Expand All @@ -768,6 +798,31 @@ app.updateVideoSize_ = function() {
};


/**
* Update the buffer information.
* @private
*/
app.updateBufferDebug_ = function() {
console.assert(app.bufferedAheadDebug_ && app.bufferedBehindDebug_);

var currentTime = app.video_.currentTime;
var buffered = app.video_.buffered;
var ahead = 0;
var behind = 0;

for (var i = 0; i < buffered.length; ++i) {
if (buffered.start(i) <= currentTime && buffered.end(i) >= currentTime) {
ahead = buffered.end(i) - currentTime;
behind = currentTime - buffered.start(i);
break;
}
}

app.bufferedAheadDebug_.textContent = Math.round(ahead) + ' seconds';
app.bufferedBehindDebug_.textContent = Math.round(behind) + ' seconds';
};


/**
* Installs the polyfills if the have not yet been installed.
* @private
Expand Down
10 changes: 9 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

<table>
<tr>
<td colspan="2" class="heading">Stream info</td>
<td colspan="2" class="heading">Stream Info</td>
<td id="version"></td>
</tr>
<tr>
Expand Down Expand Up @@ -168,6 +168,14 @@
<td>Active resolution:</td>
<td id="videoResDebug" colspan="2"></td>
</tr>
<tr>
<td>Buffered ahead:</td>
<td id="bufferedAheadDebug" colspan="2"></td>
</tr>
<tr>
<td>Buffered behind:</td>
<td id="bufferedBehindDebug" colspan="2"></td>
</tr>
</table>

<br>
Expand Down

0 comments on commit b534b70

Please sign in to comment.