Skip to content

Commit

Permalink
feat: Add KeyStatusChanged and UpdateState events (#5695)
Browse files Browse the repository at this point in the history
Closes #1365
  • Loading branch information
avelad authored Oct 2, 2023
1 parent 985e57c commit 2f97fa7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
38 changes: 33 additions & 5 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,26 @@ goog.requireType('shaka.routing.Payload');
*/


/**
* @event shaka.Player.KeyStatusChanged
* @description Fired when the key status changed.
* @property {string} type
* 'keystatuschanged'
* @exportDoc
*/


/**
* @event shaka.Player.StateChanged
* @description Fired when player state is changed.
* @property {string} type
* 'statechanged'
* @property {string} newstate
* The new state.
* @exportDoc
*/


/**
* @summary The main player object for Shaka Player.
*
Expand Down Expand Up @@ -5709,15 +5729,19 @@ shaka.Player = class extends shaka.util.FakeEventTarget {

const history = this.stats_.getStateHistory();

let updateState = 'playing';
if (this.bufferObserver_.getState() == State.STARVING) {
history.update('buffering');
updateState = 'buffering';
} else if (this.video_.paused) {
history.update('paused');
updateState = 'paused';
} else if (this.video_.ended) {
history.update('ended');
} else {
history.update('playing');
updateState = 'ended';
}
history.update(updateState);

const eventName = shaka.util.FakeEvent.EventName.StateChanged;
const data = (new Map()).set('newstate', updateState);
this.dispatchEvent(this.makeEvent_(eventName, data));
}

/**
Expand Down Expand Up @@ -6493,6 +6517,10 @@ shaka.Player = class extends shaka.util.FakeEventTarget {
return;
}

const event =
this.makeEvent_(shaka.util.FakeEvent.EventName.KeyStatusChanged);
this.dispatchEvent(event);

const keyIds = Object.keys(keyStatusMap);
if (keyIds.length == 0) {
shaka.log.warning(
Expand Down
2 changes: 2 additions & 0 deletions lib/util/fake_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ shaka.util.FakeEvent.EventName = {
Error: 'error',
ExpirationUpdated: 'expirationupdated',
GapJumped: 'gapjumped',
KeyStatusChanged: 'keystatuschanged',
Loaded: 'loaded',
Loading: 'loading',
ManifestParsed: 'manifestparsed',
Expand All @@ -176,6 +177,7 @@ shaka.util.FakeEvent.EventName = {
SegmentAppended: 'segmentappended',
SessionDataEvent: 'sessiondata',
StallDetected: 'stalldetected',
StateChanged: 'statechanged',
Streaming: 'streaming',
TextChanged: 'textchanged',
TextTrackVisibility: 'texttrackvisibility',
Expand Down

0 comments on commit 2f97fa7

Please sign in to comment.