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

[spike] Use signal-based triggers #303

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,46 @@
</head>
<body>
<script type="module">
import { BigscreenPlayer } from "./dist-local/esm/main.js"
import { BigscreenPlayer, WindowTypes } from "./dist-local/esm/main.js"

window.bigscreenPlayer = { playbackStrategy: "msestrategy" }

let player = BigscreenPlayer()
let playbackElement = document.createElement("div")
const player = BigscreenPlayer()
const playbackElement = document.createElement("div")

const DebugController = class {
constructor() {
this.signal = document.createDocumentFragment()
}

failover() {
this.signal.dispatchEvent(new Event("triggerfailover"))
}

reload(seconds) {
const event = new Event("triggerreload")
const detail = { time: seconds }
event.detail = detail
this.signal.dispatchEvent(event)
}
}

const controller = new DebugController()

window.bsp_player = player
window.debug_controller = controller

document.body.appendChild(playbackElement)

playbackElement.style.position = "absolute"
playbackElement.style.height = "720px"
playbackElement.style.width = "1280px"

let windowType = "staticWindow"
let enableSubtitles = false
const windowType = WindowTypes.STATIC
const enableSubtitles = false

let minimalData = {
const minimalData = {
signal: controller.signal,
initialPlaybackTime: 30,
media: {
captions: [],
Expand All @@ -46,6 +67,10 @@
player.init(playbackElement, minimalData, windowType, enableSubtitles, {
onSuccess: function () {
player.toggleDebug()
player.registerPlugin({
onErrorHandled: (err) => console.error(err),
onManifestLoaded: (manifest) => console.log(manifest),
})
},
onError: function () {
player.toggleDebug()
Expand Down
19 changes: 19 additions & 0 deletions src/debugger/triggers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function setupTrigger(signal, [triggerName, triggerHandle] = []) {
const callTriggerHandle = (event) => typeof triggerHandle === "function" && triggerHandle(event.detail)

signal.addEventListener(triggerName, callTriggerHandle)

return function cleanupTrigger() {
signal.removeEventListener(triggerName, callTriggerHandle)
}
}

export default function setupAllTriggers(signal, ...triggers) {
const cleanupFunctions = triggers.map((trigger) => setupTrigger(signal, trigger))

return function cleanupAllTriggers() {
for (const cleanupFunc of cleanupFunctions) {
cleanupFunc()
}
}
}
23 changes: 23 additions & 0 deletions src/playercomponent.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import setupAllTriggers from "./debugger/triggers"
import MediaState from "./models/mediastate"
import WindowTypes from "./models/windowtypes"
import PluginData from "./plugindata"
Expand Down Expand Up @@ -30,6 +31,23 @@ function PlayerComponent(
let fatalErrorTimeout
let fatalError

let teardownTriggers

if (bigscreenPlayerData.signal) {
teardownTriggers = setupAllTriggers(
bigscreenPlayerData.signal,
[
"triggerfailover",
() =>
attemptCdnFailover({
code: PluginEnums.ERROR_CODES.BUFFERING_TIMEOUT,
message: PluginEnums.ERROR_MESSAGES.BUFFERING_TIMEOUT,
}),
],
["triggerreload", ({ time }) => reloadMediaElement(time)]
)
}

StrategyPicker()
.then((strategy) => {
playbackStrategy = strategy(
Expand Down Expand Up @@ -387,6 +405,11 @@ function PlayerComponent(
mediaMetaData = undefined
fatalErrorTimeout = undefined
fatalError = undefined

if (typeof teardownTriggers === "function") {
teardownTriggers()
teardownTriggers = undefined
}
}

return {
Expand Down