Skip to content

Commit

Permalink
fix: cleanup mediasource listeners on dispose (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey authored Jun 18, 2020
1 parent ca73cac commit e50f4c9
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/master-playlist-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,15 @@ export class MasterPlaylistController extends videojs.EventTarget {

this.mediaSource = new window.MediaSource();

this.mediaSource.addEventListener('durationchange', () => {
this.tech_.trigger('durationchange');
});
this.handleDurationChange_ = this.handleDurationChange_.bind(this);
this.handleSourceOpen_ = this.handleSourceOpen_.bind(this);
this.handleSourceEnded_ = this.handleSourceEnded_.bind(this);

this.mediaSource.addEventListener('durationchange', this.handleDurationChange_);

// load the media source into the player
this.mediaSource.addEventListener('sourceopen', this.handleSourceOpen_.bind(this));
this.mediaSource.addEventListener('sourceended', this.handleSourceEnded_.bind(this));
this.mediaSource.addEventListener('sourceopen', this.handleSourceOpen_);
this.mediaSource.addEventListener('sourceended', this.handleSourceEnded_);
// we don't have to handle sourceclose since dispose will handle termination of
// everything, and the MediaSource should not be detached without a proper disposal

Expand Down Expand Up @@ -781,6 +784,15 @@ export class MasterPlaylistController extends videojs.EventTarget {
Number.MAX_VALUE : duration;
}

/**
* handle the durationchange event on the MediaSource
*
* @private
*/
handleDurationChange_() {
this.tech_.trigger('durationchange');
}

/**
* Calls endOfStream on the media source when all active stream types have called
* endOfStream
Expand Down Expand Up @@ -1152,8 +1164,13 @@ export class MasterPlaylistController extends videojs.EventTarget {
* Update the player duration
*/
updateDuration(isLive) {
if (this.updateDuration_) {
this.mediaSource.removeEventListener('sourceopen', this.updateDuration_);
this.updateDuration_ = null;
}
if (this.mediaSource.readyState !== 'open') {
this.mediaSource.addEventListener('sourceopen', this.updateDuration.bind(this, isLive));
this.updateDuration_ = this.updateDuration.bind(this, isLive);
this.mediaSource.addEventListener('sourceopen', this.updateDuration_);
return;
}

Expand Down Expand Up @@ -1232,6 +1249,15 @@ export class MasterPlaylistController extends videojs.EventTarget {
this.subtitleSegmentLoader_.dispose();
this.sourceUpdater_.dispose();
this.timelineChangeController_.dispose();
if (this.updateDuration_) {
this.mediaSource.removeEventListener('sourceopen', this.updateDuration_);
}

this.mediaSource.removeEventListener('durationchange', this.handleDurationChange_);

// load the media source into the player
this.mediaSource.removeEventListener('sourceopen', this.handleSourceOpen_);
this.mediaSource.removeEventListener('sourceended', this.handleSourceEnded_);
this.off();
}

Expand Down

0 comments on commit e50f4c9

Please sign in to comment.