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

Report playback stopped when media finishes #668

Merged
merged 2 commits into from
Nov 4, 2024
Merged
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
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 @@ -194,7 +194,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 @@ -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;
Expand Down
Loading