diff --git a/src/components/maincontroller.ts b/src/components/maincontroller.ts index 685df040..546922da 100644 --- a/src/components/maincontroller.ts +++ b/src/components/maincontroller.ts @@ -12,7 +12,8 @@ import { getInstantMixItems, translateRequestedItems, broadcastToMessageBus, - ticksToSeconds + ticksToSeconds, + TicksPerSecond } from '../helpers'; import { reportPlaybackStart, @@ -153,11 +154,7 @@ export function disableTimeUpdateListener(): void { enableTimeUpdateListener(); window.addEventListener('beforeunload', () => { - // Try to cleanup after ourselves before the page closes - const playbackState = PlaybackManager.playbackState; - disableTimeUpdateListener(); - reportPlaybackStopped(playbackState, getReportingParams(playbackState)); }); window.playerManager.addEventListener( @@ -194,8 +191,25 @@ function defaultOnStop(): void { window.playerManager.addEventListener( cast.framework.events.EventType.MEDIA_FINISHED, - defaultOnStop + (mediaFinishedEvent): void => { + const playbackState = PlaybackManager.playbackState; + + // Don't notify server or client if changing streams, but notify next time. + if (!playbackState.isChangingStream) { + reportPlaybackStopped(playbackState, { + ...getReportingParams(playbackState), + PositionTicks: + (mediaFinishedEvent.currentMediaTime ?? + getCurrentPositionTicks(playbackState)) * TicksPerSecond + }); + + defaultOnStop(); + } else { + playbackState.isChangingStream = false; + } + } ); + window.playerManager.addEventListener( cast.framework.events.EventType.ABORT, defaultOnStop @@ -211,7 +225,6 @@ window.playerManager.addEventListener( return; } - reportPlaybackStopped(playbackState, getReportingParams(playbackState)); PlaybackManager.resetPlaybackScope(); if (!PlaybackManager.playNextItem()) { @@ -231,16 +244,6 @@ window.playerManager.addEventListener( ); } ); -// Notify of playback end just before stopping it, to get a good tick position -window.playerManager.addEventListener( - cast.framework.events.EventType.REQUEST_STOP, - (): void => { - reportPlaybackStopped( - PlaybackManager.playbackState, - getReportingParams(PlaybackManager.playbackState) - ); - } -); // Set the active subtitle track once the player has loaded window.playerManager.addEventListener( @@ -513,6 +516,8 @@ export async function changeStream( // await stopActiveEncodings($scope.playSessionId); //} + state.isChangingStream = true; + // @ts-expect-error is possible here return await PlaybackManager.playItemInternal(state.item, { audioStreamIndex: params.AudioStreamIndex ?? state.audioStreamIndex, diff --git a/src/components/playbackManager.ts b/src/components/playbackManager.ts index b0406c9d..51b5ead1 100644 --- a/src/components/playbackManager.ts +++ b/src/components/playbackManager.ts @@ -194,7 +194,6 @@ export abstract class PlaybackManager { item: BaseItemDto, options: any // eslint-disable-line @typescript-eslint/no-explicit-any ): Promise { - this.playbackState.isChangingStream = false; DocumentManager.setAppStatus(AppStatus.Loading); const maxBitrate = await getMaxBitrate(); @@ -286,7 +285,10 @@ export abstract class PlaybackManager { loadRequestData.currentTime = ticksToSeconds(startPositionTicks); } + const isChangingStream = this.playbackState.isChangingStream; + load(mediaInfo.customData, item); + this.playbackState.isChangingStream = isChangingStream; this.playerManager.load(loadRequestData); this.playbackState.PlaybackMediaSource = mediaSource;