Skip to content

Commit

Permalink
Merge pull request #668 from 3flex/stop-on-media-finished
Browse files Browse the repository at this point in the history
Report playback stopped when media finishes
  • Loading branch information
nielsvanvelzen authored Nov 4, 2024
2 parents bad3440 + 3cd9151 commit 5c51cdf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
39 changes: 22 additions & 17 deletions src/components/maincontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
getInstantMixItems,
translateRequestedItems,
broadcastToMessageBus,
ticksToSeconds
ticksToSeconds,
TicksPerSecond
} from '../helpers';
import {
reportPlaybackStart,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -211,7 +225,6 @@ window.playerManager.addEventListener(
return;
}

reportPlaybackStopped(playbackState, getReportingParams(playbackState));
PlaybackManager.resetPlaybackScope();

if (!PlaybackManager.playNextItem()) {
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/components/playbackManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ export abstract class PlaybackManager {
item: BaseItemDto,
options: any // eslint-disable-line @typescript-eslint/no-explicit-any
): Promise<void> {
this.playbackState.isChangingStream = false;
DocumentManager.setAppStatus(AppStatus.Loading);

const maxBitrate = await getMaxBitrate();
Expand Down Expand Up @@ -293,7 +292,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;
Expand Down

0 comments on commit 5c51cdf

Please sign in to comment.