Skip to content

Commit

Permalink
chore(ABR): Use EventManager for network change events (#6405)
Browse files Browse the repository at this point in the history
  • Loading branch information
tykus160 committed Apr 5, 2024
1 parent 9838622 commit 83d3f51
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions lib/abr/simple_abr_manager.js
Expand Up @@ -6,10 +6,11 @@

goog.provide('shaka.abr.SimpleAbrManager');

goog.require('shaka.util.IReleasable');
goog.require('goog.asserts');
goog.require('shaka.abr.EwmaBandwidthEstimator');
goog.require('shaka.log');
goog.require('shaka.util.EventManager');
goog.require('shaka.util.IReleasable');
goog.require('shaka.util.StreamUtils');
goog.require('shaka.util.Timer');

Expand Down Expand Up @@ -50,30 +51,30 @@ shaka.abr.SimpleAbrManager = class {
/** @private {shaka.abr.EwmaBandwidthEstimator} */
this.bandwidthEstimator_ = new shaka.abr.EwmaBandwidthEstimator();

/** @private {?function():void} */
this.onNetworkInformationChange_ = null;
/** @private {!shaka.util.EventManager} */
this.eventManager_ = new shaka.util.EventManager();

// Some browsers implement the Network Information API, which allows
// retrieving information about a user's network connection. We listen
// to the change event to be able to make quick changes in case the type
// of connectivity changes.
if (navigator.connection && navigator.connection.addEventListener) {
this.onNetworkInformationChange_ = () => {
if (this.enabled_ && this.config_.useNetworkInformation) {
this.bandwidthEstimator_ = new shaka.abr.EwmaBandwidthEstimator();
if (this.config_) {
this.bandwidthEstimator_.configure(this.config_.advanced);
}
const chosenVariant = this.chooseVariant();
if (chosenVariant && navigator.onLine) {
this.switch_(chosenVariant, this.config_.clearBufferSwitch,
this.config_.safeMarginSwitch);
}
}
};

navigator.connection.addEventListener(
'change', this.onNetworkInformationChange_);
this.eventManager_.listen(
/** @type {EventTarget} */(navigator.connection),
'change',
() => {
if (this.enabled_ && this.config_.useNetworkInformation) {
this.bandwidthEstimator_ = new shaka.abr.EwmaBandwidthEstimator();
if (this.config_) {
this.bandwidthEstimator_.configure(this.config_.advanced);
}
const chosenVariant = this.chooseVariant();
if (chosenVariant && navigator.onLine) {
this.switch_(chosenVariant, this.config_.clearBufferSwitch,
this.config_.safeMarginSwitch);
}
}
});
}

/**
Expand Down Expand Up @@ -151,13 +152,7 @@ shaka.abr.SimpleAbrManager = class {
*/
release() {
// stop() should already have been called for unload

if (navigator.connection && navigator.connection.removeEventListener) {
navigator.connection.removeEventListener(
'change', this.onNetworkInformationChange_);
this.onNetworkInformationChange_ = null;
}

this.eventManager_.release();
this.resizeObserverTimer_ = null;
}

Expand Down

0 comments on commit 83d3f51

Please sign in to comment.