Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin for onQualityChangeRequested #356

Merged
merged 13 commits into from
Oct 14, 2024
Merged
28 changes: 14 additions & 14 deletions docs/tutorials/Plugins.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Plugins can be created to extend the functionality of the Bigscreen Player by adhering to an interface which propogates non state change events from the player. For example, when an error is raised or cleared.

The full interface is as follows:

- `onError`
- `onFatalError`
- `onErrorCleared`
Expand All @@ -11,6 +12,7 @@ The full interface is as follows:
- `onPlayerInfoUpdated`
- `onManifestLoaded`
- `onManifestParseError`
- `onQualityChangeRequested`
- `onQualityChangedRendered`
- `onSubtitlesLoadError`
- `onSubtitlesTimeout`
Expand All @@ -23,37 +25,35 @@ The full interface is as follows:
An example plugin may look like:

```javascript
function ExamplePlugin (appName) {

var name = appName;
function ExamplePlugin(appName) {
var name = appName

function onFatalError (evt) {
console.log('A fatal error has occured in the app: ' + name);
function onFatalError(evt) {
console.log("A fatal error has occured in the app: " + name)
}

function onErrorHandled (evt) {
console.log('The ' + name + ' app is handling a playback error');
function onErrorHandled(evt) {
console.log("The " + name + " app is handling a playback error")
}

return {
onFatalError: onFatalError,
onErrorHandled: onErrorHandled
};
onErrorHandled: onErrorHandled,
}
}
```

```javascript
var bigscreenPlayer = BigscreenPlayer();
var bigscreenPlayer = BigscreenPlayer()

var examplePlugin = ExamplePlugin('myApp');
var examplePlugin = ExamplePlugin("myApp")

bigscreenPlayer.registerPlugin(examplePlugin);
bigscreenPlayer.registerPlugin(examplePlugin)

// initialise bigscreenPlayer - see above

// you should unregister your plugins as part of your playback cleanup

// calling with no argument will unregister all plugins
bigscreenPlayer.unregisterPlugin(examplePlugin);

bigscreenPlayer.unregisterPlugin(examplePlugin)
```
35 changes: 22 additions & 13 deletions src/playbackstrategy/msestrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function MSEStrategy(mediaSources, windowType, mediaKind, playbackElement, isUHD
DOWNLOAD_INIT_SEGMENT_ERROR_CODE: 28,
UNSUPPORTED_CODEC: 30,
MANIFEST_VALIDITY_CHANGED: "manifestValidityChanged",
QUALITY_CHANGE_REQUESTED: "qualityChangeRequested",
QUALITY_CHANGE_RENDERED: "qualityChangeRendered",
BASE_URL_SELECTED: "baseUrlSelected",
SERVICE_LOCATION_AVAILABLE: "serviceLocationUnblacklisted",
Expand Down Expand Up @@ -354,25 +355,31 @@ function MSEStrategy(mediaSources, windowType, mediaKind, playbackElement, isUHD
return parseInt(bitrateInfoList[index].bitrate / 1000)
}

function onQualityChangeRendered(event) {
function logBitrate(event) {
const { mediaType, oldQuality, newQuality } = event

const oldBitrate = isNaN(oldQuality) ? "--" : playbackBitrateForRepresentationIndex(oldQuality, mediaType)
const newBitrate = isNaN(newQuality) ? "--" : playbackBitrateForRepresentationIndex(newQuality, mediaType)
function logBitrate(abrType, { mediaType, oldQuality, newQuality }) {
const oldBitrate = isNaN(oldQuality) ? "--" : playbackBitrateForRepresentationIndex(oldQuality, mediaType)
const newBitrate = isNaN(newQuality) ? "--" : playbackBitrateForRepresentationIndex(newQuality, mediaType)

const oldRepresentation = isNaN(oldQuality) ? "Start" : `${oldQuality} (${oldBitrate} kbps)`
const newRepresentation = `${newQuality} (${newBitrate} kbps)`
const oldRepresentation = isNaN(oldQuality) ? "Start" : `${oldQuality} (${oldBitrate} kbps)`
const newRepresentation = `${newQuality} (${newBitrate} kbps)`

DebugTool.dynamicMetric(`representation-${mediaType}`, [newQuality, newBitrate])
DebugTool.info(
`${mediaType} ABR Change ${abrType} From Representation ${oldRepresentation} to ${newRepresentation}`
)
}

DebugTool.info(
`${mediaType} ABR Change Rendered From Representation ${oldRepresentation} To ${newRepresentation}`
)
function onQualityChangeRequested(event) {
if (event.newQuality !== undefined) {
logBitrate("Requested", event)
}

event.throughput = mediaPlayer.getAverageThroughput(mediaKind)

Plugins.interface.onQualityChangeRequested(event)
}

function onQualityChangeRendered(event) {
if (event.newQuality !== undefined) {
logBitrate(event)
logBitrate("Rendered", event)
}

emitPlayerInfo()
Expand Down Expand Up @@ -544,6 +551,7 @@ function MSEStrategy(mediaSources, windowType, mediaKind, playbackElement, isUHD
mediaPlayer.on(DashJSEvents.MANIFEST_LOADED, onManifestLoaded)
mediaPlayer.on(DashJSEvents.STREAM_INITIALIZED, onStreamInitialised)
mediaPlayer.on(DashJSEvents.MANIFEST_VALIDITY_CHANGED, onManifestValidityChange)
mediaPlayer.on(DashJSEvents.QUALITY_CHANGE_REQUESTED, onQualityChangeRequested)
mediaPlayer.on(DashJSEvents.QUALITY_CHANGE_RENDERED, onQualityChangeRendered)
mediaPlayer.on(DashJSEvents.BASE_URL_SELECTED, onBaseUrlSelected)
mediaPlayer.on(DashJSEvents.METRIC_ADDED, onMetricAdded)
Expand Down Expand Up @@ -654,6 +662,7 @@ function MSEStrategy(mediaSources, windowType, mediaKind, playbackElement, isUHD
mediaPlayer.off(DashJSEvents.MANIFEST_VALIDITY_CHANGED, onManifestValidityChange)
mediaPlayer.off(DashJSEvents.STREAM_INITIALIZED, onStreamInitialised)
mediaPlayer.off(DashJSEvents.QUALITY_CHANGE_RENDERED, onQualityChangeRendered)
mediaPlayer.off(DashJSEvents.QUALITY_CHANGE_REQUESTED, onQualityChangeRequested)
mediaPlayer.off(DashJSEvents.METRIC_ADDED, onMetricAdded)
mediaPlayer.off(DashJSEvents.BASE_URL_SELECTED, onBaseUrlSelected)
mediaPlayer.off(DashJSEvents.LOG, onDebugLog)
Expand Down
1 change: 1 addition & 0 deletions src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default {
onPlayerInfoUpdated: (evt) => callOnAllPlugins("onPlayerInfoUpdated", evt),
onManifestLoaded: (manifest) => callOnAllPlugins("onManifestLoaded", manifest),
onManifestParseError: (evt) => callOnAllPlugins("onManifestParseError", evt),
onQualityChangeRequested: (evt) => callOnAllPlugins("onQualityChangeRequested", evt),
onQualityChangedRendered: (evt) => callOnAllPlugins("onQualityChangedRendered", evt),
onSubtitlesLoadError: (evt) => callOnAllPlugins("onSubtitlesLoadError", evt),
onSubtitlesTimeout: (evt) => callOnAllPlugins("onSubtitlesTimeout", evt),
Expand Down