Skip to content
Merged
11 changes: 10 additions & 1 deletion src/bigscreenplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import ReadyHelper from "./readyhelper"
import Subtitles from "./subtitles/subtitles"
import { ManifestType } from "./models/manifesttypes"
import { Timeline } from "./models/timeline"
import { AbortSignal } from "./utils/abortutils"
import { AbortStages } from "./models/abortstages"

/**
* @import {
Expand Down Expand Up @@ -53,6 +55,8 @@ function BigscreenPlayer() {

const END_OF_STREAM_TOLERANCE = 10

const abortSignal = new AbortSignal()

function mediaStateUpdateCallback(evt) {
if (evt.timeUpdate) {
callCallbacks(_callbacks.timeUpdate, {
Expand Down Expand Up @@ -106,6 +110,8 @@ function BigscreenPlayer() {
}

function bigscreenPlayerDataLoaded({ media, enableSubtitles, enableAudioDescribed }) {
abortSignal.throwIfAborted(AbortStages.DATA_LOADED)

const initialPresentationTime =
initialPlaybackTime == null ? undefined : convertPlaybackTimeToPresentationTimeInSeconds(initialPlaybackTime)

Expand All @@ -120,7 +126,8 @@ function BigscreenPlayer() {
mediaSources,
mediaStateUpdateCallback,
_callbacks.playerError,
callAudioDescribedCallbacks
callAudioDescribedCallbacks,
abortSignal
)

readyHelper = ReadyHelper(
Expand Down Expand Up @@ -311,6 +318,8 @@ function BigscreenPlayer() {
* @name tearDown
*/
tearDown() {
abortSignal.abort()

if (subtitles) {
subtitles.tearDown()
subtitles = undefined
Expand Down
38 changes: 31 additions & 7 deletions src/bigscreenplayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import MediaState from "./models/mediastate"
import PauseTriggers from "./models/pausetriggers"
import { Timeline } from "./models/timeline"
import getError, { NoErrorThrownError } from "./testutils/geterror"
import { AbortStages } from "./models/abortstages"

let bigscreenPlayer
let bigscreenPlayerData
Expand Down Expand Up @@ -194,7 +195,8 @@ describe("Bigscreen Player", () => {
expect.any(Object),
expect.any(Function),
expect.any(Function),
expect.any(Function)
expect.any(Function),
expect.objectContaining({ aborted: false })
)
})

Expand Down Expand Up @@ -234,7 +236,8 @@ describe("Bigscreen Player", () => {
expect.any(Object),
expect.any(Function),
expect.any(Function),
expect.any(Function)
expect.any(Function),
expect.objectContaining({ aborted: false })
)
})

Expand All @@ -253,7 +256,8 @@ describe("Bigscreen Player", () => {
expect.any(Object),
expect.any(Function),
expect.any(Function),
expect.any(Function)
expect.any(Function),
expect.objectContaining({ aborted: false })
)
})

Expand All @@ -273,7 +277,8 @@ describe("Bigscreen Player", () => {
expect.any(Object),
expect.any(Function),
expect.any(Function),
expect.any(Function)
expect.any(Function),
expect.objectContaining({ aborted: false })
)
})

Expand All @@ -297,7 +302,8 @@ describe("Bigscreen Player", () => {
expect.any(Object),
expect.any(Function),
expect.any(Function),
expect.any(Function)
expect.any(Function),
expect.objectContaining({ aborted: false })
)
})

Expand All @@ -321,7 +327,8 @@ describe("Bigscreen Player", () => {
expect.any(Object),
expect.any(Function),
expect.any(Function),
expect.any(Function)
expect.any(Function),
expect.objectContaining({ aborted: false })
)
})

Expand All @@ -346,10 +353,27 @@ describe("Bigscreen Player", () => {
expect.any(Object),
expect.any(Function),
expect.any(Function),
expect.any(Function)
expect.any(Function),
expect.objectContaining({ aborted: false })
)
})
})

describe("aborting during initialization", () => {
it("aborts if bigscreen player has been torn down", async () => {
bigscreenPlayer.tearDown()
const error = await getError(() => asyncInitialiseBigscreenPlayer(createPlaybackElement(), bigscreenPlayerData))

expect(error).toHaveProperty("name", "AbortError")
expect(error).toHaveProperty("message", `bigscreen-player aborted at ${AbortStages.DATA_LOADED}`)
})

it("does not abort if bigscreen player has not been torn down", async () => {
const error = await getError(() => asyncInitialiseBigscreenPlayer(createPlaybackElement(), bigscreenPlayerData))

expect(error).toBeInstanceOf(NoErrorThrownError)
})
})
})

describe("tearDown", () => {
Expand Down
7 changes: 7 additions & 0 deletions src/models/abortstages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const AbortStages = {
DATA_LOADED: "bigscreen-player-data-loaded",
PLAYER_COMPONENT: "bigscreen-player-player-component",
STRATEGY: "bigscreen-player-strategy",
} as const

export type AbortStages = (typeof AbortStages)[keyof typeof AbortStages]
4 changes: 2 additions & 2 deletions src/models/mediakinds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const AUDIO = "audio" as const
const VIDEO = "video" as const
const AUDIO = "audio"
const VIDEO = "video"

export const MediaKinds = { AUDIO, VIDEO } as const

Expand Down
7 changes: 6 additions & 1 deletion src/playercomponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import PluginData from "./plugindata"
import PluginEnums from "./pluginenums"
import Plugins from "./plugins"
import DebugTool from "./debugger/debugtool"
import { AbortStages } from "./models/abortstages"

/**
* @import { InitData } from './types.d.ts'
Expand Down Expand Up @@ -41,7 +42,8 @@ function PlayerComponent(
mediaSources,
stateUpdateCallback,
errorCallback,
audioDescribedCallback
audioDescribedCallback,
abortSignal
) {
let setSubtitlesState
let _stateUpdateCallback = stateUpdateCallback
Expand All @@ -57,10 +59,13 @@ function PlayerComponent(

StrategyPicker()
.then((strategy) => {
abortSignal.throwIfAborted(AbortStages.PLAYER_COMPONENT)

playbackStrategy = strategy(
mediaSources,
mediaKind,
playbackElement,
abortSignal,
bigscreenPlayerData.media.isUHD,
bigscreenPlayerData.media.playerSettings,
{
Expand Down
Loading