Skip to content

Commit

Permalink
Use Network Information API to get bandwidth estimate when possible.
Browse files Browse the repository at this point in the history
Issue shaka-project#994

Change-Id: Id4a33ce7467c2e89613acb191fb304f02d10ed65
  • Loading branch information
ismena authored and joeyparrish committed Dec 4, 2017
1 parent c24cf79 commit 891b971
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
52 changes: 52 additions & 0 deletions externs/networkinformation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @fileoverview Externs for NetworkInformation based on
* {@link http://goo.gl/Cr1L15 Network Information API
* draft 07 September 2017}
*
* @externs
*/


/** @const {NetworkInformation} */
Navigator.prototype.connection;



/**
* @interface
* @extends {EventTarget}
*/
function NetworkInformation() {}


/** @const {string} */
NetworkInformation.prototype.effectiveType;


/** @const {number} */
NetworkInformation.prototype.downlink;


/** @const {boolean} */
NetworkInformation.prototype.saveData;


/** @const {string} */
NetworkInformation.prototype.type;
2 changes: 2 additions & 0 deletions lib/abr/simple_abr_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ shaka.abr.SimpleAbrManager = function() {

/** @private {shaka.abr.EwmaBandwidthEstimator} */
this.bandwidthEstimator_ = new shaka.abr.EwmaBandwidthEstimator();
// TODO: Consider using NetworkInformation's change event to throw out an old
// estimate based on changing network types, such as wifi => 3g.

/**
* A filtered list of Variants to choose from.
Expand Down
24 changes: 20 additions & 4 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,25 @@ shaka.Player.prototype.configOverrides_ = function() {
* @private
*/
shaka.Player.prototype.defaultConfig_ = function() {
// This is a relatively safe default, since 3G cell connections
// are faster than this. For slower connections, such as 2G,
// the default estimate may be too high.
var bandwidthEstimate = 500e3; // 500kbps

// Some browsers implement the Network Information API, which allows
// retrieving information about a user's network connection.
//
// We are excluding connection.type == undefined to avoid getting bogus data
// on platforms where the implementation is incomplete. Currently, desktop
// Chrome 61 returns connection type undefined and a bogus downlink value.
if (navigator.connection && navigator.connection.type) {
// If it's available, get the bandwidth estimate from the browser (in
// megabits per second) and use it as defaultBandwidthEstimate.
bandwidthEstimate = navigator.connection.downlink * 1e6;
// TODO: move this into AbrManager, where changes to the estimate can be
// observed and absorbed.
}

return {
drm: {
retryParameters: shaka.net.NetworkingEngine.defaultRetryParameters(),
Expand Down Expand Up @@ -2009,10 +2028,7 @@ shaka.Player.prototype.defaultConfig_ = function() {
}.bind(null, this.video_),
abr: {
enabled: true,
// This is a relatively safe default, since 3G cell connections
// are faster than this. For slower connections, such as 2G,
// the default estimate may be too high.
defaultBandwidthEstimate: 500e3, // 500kbps
defaultBandwidthEstimate: bandwidthEstimate,
switchInterval: 8,
bandwidthUpgradeTarget: 0.85,
bandwidthDowngradeTarget: 0.95,
Expand Down

0 comments on commit 891b971

Please sign in to comment.