Skip to content

Commit

Permalink
chore: Simplify listener management to be able to use the same basic …
Browse files Browse the repository at this point in the history
…listeners in MSE and src= (#7501)

Related to #5022
  • Loading branch information
avelad authored Oct 25, 2024
1 parent 30068a1 commit 6d9fa2f
Showing 1 changed file with 79 additions and 96 deletions.
175 changes: 79 additions & 96 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2437,6 +2437,81 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
this.mediaSourceEngine_ = mediaSourceEngine;
}

/**
* Adds the basic media listeners
*
* @param {HTMLMediaElement} mediaElement
* @param {number} startTimeOfLoad
* @private
*/
addBasicMediaListeners_(mediaElement, startTimeOfLoad) {
const updateStateHistory = () => this.updateStateHistory_();
const onRateChange = () => this.onRateChange_();
this.loadEventManager_.listen(mediaElement, 'playing', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'pause', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'ended', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'ratechange', onRateChange);
if (mediaElement.remote) {
this.loadEventManager_.listen(mediaElement.remote, 'connect',
() => this.onTracksChanged_());
this.loadEventManager_.listen(mediaElement.remote, 'connecting',
() => this.onTracksChanged_());
this.loadEventManager_.listen(mediaElement.remote, 'disconnect',
() => this.onTracksChanged_());
}
if (mediaElement.audioTracks) {
this.loadEventManager_.listen(mediaElement.audioTracks, 'addtrack',
() => this.onTracksChanged_());
this.loadEventManager_.listen(mediaElement.audioTracks, 'removetrack',
() => this.onTracksChanged_());
this.loadEventManager_.listen(mediaElement.audioTracks, 'change',
() => this.onTracksChanged_());
}

if (mediaElement.textTracks) {
this.loadEventManager_.listen(
mediaElement.textTracks, 'addtrack', (e) => {
const trackEvent = /** @type {!TrackEvent} */(e);
if (trackEvent.track) {
const track = trackEvent.track;
goog.asserts.assert(
track instanceof TextTrack, 'Wrong track type!');

switch (track.kind) {
case 'metadata':
this.processTimedMetadataSrcEqls_(track);
break;

case 'chapters':
this.activateChaptersTrack_(track);
break;

default:
this.onTracksChanged_();
break;
}
}
});
this.loadEventManager_.listen(mediaElement.textTracks, 'removetrack',
() => this.onTracksChanged_());
this.loadEventManager_.listen(mediaElement.textTracks, 'change',
() => this.onTracksChanged_());
}

// Wait for the 'loadedmetadata' event to measure load() latency, but only
// if preload is set in a way that would result in this event firing
// automatically.
// See https://github.com/shaka-project/shaka-player/issues/2483
if (mediaElement.preload != 'none') {
this.loadEventManager_.listenOnce(
mediaElement, 'loadedmetadata', () => {
const now = Date.now() / 1000;
const delta = now - startTimeOfLoad;
this.stats_.setLoadLatency(delta);
});
}
}

/**
* Starts loading the content described by the parsed manifest.
*
Expand Down Expand Up @@ -2466,13 +2541,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
movePlayhead: (delta) => { mediaElement.currentTime += delta; },
});

const updateStateHistory = () => this.updateStateHistory_();
const onRateChange = () => this.onRateChange_();
this.loadEventManager_.listen(
mediaElement, 'playing', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'pause', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'ended', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'ratechange', onRateChange);
// Add all media element listeners.
this.addBasicMediaListeners_(mediaElement, startTimeOfLoad);

// Check the status of the LCEVC Dec Object. Reset, create, or close
// depending on the config.
Expand Down Expand Up @@ -2501,24 +2571,6 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
// "streaming" so that they can access internal information.
this.loadMode_ = shaka.Player.LoadMode.MEDIA_SOURCE;

if (mediaElement.textTracks) {
this.loadEventManager_.listen(
mediaElement.textTracks, 'addtrack', (e) => {
const trackEvent = /** @type {!TrackEvent} */(e);
if (trackEvent.track) {
const track = trackEvent.track;
goog.asserts.assert(
track instanceof TextTrack, 'Wrong track type!');

switch (track.kind) {
case 'chapters':
this.activateChaptersTrack_(track);
break;
}
}
});
}

// The event must be fired after we filter by restrictions but before the
// active stream is picked to allow those listening for the "streaming"
// event to make changes before streaming starts.
Expand Down Expand Up @@ -2726,13 +2778,6 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}

this.fullyLoaded_ = true;

// Wait for the 'loadedmetadata' event to measure load() latency.
this.loadEventManager_.listenOnce(mediaElement, 'loadedmetadata', () => {
const now = Date.now() / 1000;
const delta = now - startTimeOfLoad;
this.stats_.setLoadLatency(delta);
});
}

/**
Expand Down Expand Up @@ -2896,72 +2941,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
const rebufferThreshold = this.config_.streaming.rebufferingGoal;
this.startBufferManagement_(mediaElement, rebufferThreshold);

// Add all media element listeners.
const updateStateHistory = () => this.updateStateHistory_();
const onRateChange = () => this.onRateChange_();
this.loadEventManager_.listen(
mediaElement, 'playing', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'pause', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'ended', updateStateHistory);
this.loadEventManager_.listen(mediaElement, 'ratechange', onRateChange);

// Wait for the 'loadedmetadata' event to measure load() latency, but only
// if preload is set in a way that would result in this event firing
// automatically.
// See https://github.com/shaka-project/shaka-player/issues/2483
if (mediaElement.preload != 'none') {
this.loadEventManager_.listenOnce(
mediaElement, 'loadedmetadata', () => {
const now = Date.now() / 1000;
const delta = now - startTimeOfLoad;
this.stats_.setLoadLatency(delta);
});
}

// The audio tracks are only available on Safari at the moment, but this
// drives the tracks API for Safari's native HLS. So when they change,
// fire the corresponding Shaka Player event.
if (mediaElement.audioTracks) {
this.loadEventManager_.listen(mediaElement.audioTracks, 'addtrack',
() => this.onTracksChanged_());
this.loadEventManager_.listen(mediaElement.audioTracks, 'removetrack',
() => this.onTracksChanged_());
this.loadEventManager_.listen(mediaElement.audioTracks, 'change',
() => this.onTracksChanged_());
}

if (mediaElement.textTracks) {
this.createTextDisplayer_();
this.loadEventManager_.listen(
mediaElement.textTracks, 'addtrack', (e) => {
const trackEvent = /** @type {!TrackEvent} */(e);
if (trackEvent.track) {
const track = trackEvent.track;
goog.asserts.assert(
track instanceof TextTrack, 'Wrong track type!');

switch (track.kind) {
case 'metadata':
this.processTimedMetadataSrcEqls_(track);
break;

case 'chapters':
this.activateChaptersTrack_(track);
break;

default:
this.onTracksChanged_();
break;
}
}
});

this.loadEventManager_.listen(
mediaElement.textTracks, 'removetrack',
() => this.onTracksChanged_());
this.loadEventManager_.listen(
mediaElement.textTracks, 'change',
() => this.onTracksChanged_());
this.loadEventManager_.listen(
mediaElement, 'enterpictureinpicture', () => {
const track = this.getFilteredTextTracks_()
Expand All @@ -2979,6 +2960,8 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
}
});
}
// Add all media element listeners.
this.addBasicMediaListeners_(mediaElement, startTimeOfLoad);

// By setting |src| we are done "loading" with src=. We don't need to set
// the current time because |playhead| will do that for us.
Expand Down

0 comments on commit 6d9fa2f

Please sign in to comment.