feat(playback): keep transfer position and sync devices
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
Transfers now start the current item first in the queue and send a corrective Seek for clients that ignore StartPositionTicks. Adds a Sync playback mode that keeps other sessions playing the same item aligned with the active device.
This commit is contained in:
1 parent
df392eb647
commit
4bdd0139bf
8 files changed
+461
-19
No files matched your search
@@ -3,6 +3,7 @@ import {
|
||||
type BaseItemDto,
|
||||
BaseItemKind,
|
||||
ItemFields,
|
||||
PlaystateCommand,
|
||||
type SessionInfoDto,
|
||||
} from "@jellyfin/sdk/lib/generated-client";
|
||||
import { getLibraryApi } from "@jellyfin/sdk/lib/utils/api/library-api";
|
||||
@@ -20,6 +21,96 @@ const fields = [
|
||||
ItemFields.Overview,
|
||||
];
|
||||
|
||||
const TICKS_PER_SECOND = 10_000_000;
|
||||
const sleep = (ms: number) =>
|
||||
new Promise<void>((resolve) => window.setTimeout(resolve, ms));
|
||||
|
||||
/**
|
||||
* Some clients ignore StartIndex as well as StartPositionTicks. Put the
|
||||
* current item first so a transfer always starts with what is playing now.
|
||||
*/
|
||||
export const queueStartingWith = (
|
||||
items: Array<{ Id?: string | null }>,
|
||||
currentItemId: string | undefined,
|
||||
): string[] => {
|
||||
const ids = items
|
||||
.map((item) => item.Id)
|
||||
.filter((id): id is string => Boolean(id));
|
||||
if (!currentItemId) return ids;
|
||||
const index = ids.indexOf(currentItemId);
|
||||
if (index <= 0) return ids;
|
||||
return [...ids.slice(index), ...ids.slice(0, index)];
|
||||
};
|
||||
|
||||
export type PlayOnSessionOptions = {
|
||||
itemIds: string[];
|
||||
startIndex?: number;
|
||||
startPositionTicks?: number;
|
||||
mediaSourceId?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Start playback on another Jellyfin session and make sure it begins at the
|
||||
* requested position. Some clients accept the Play command but ignore
|
||||
* StartPositionTicks, so verify the reported position and send a Seek when
|
||||
* they started from the beginning.
|
||||
*/
|
||||
export const playOnSession = async (
|
||||
api: Api,
|
||||
userId: string,
|
||||
sessionId: string,
|
||||
options: PlayOnSessionOptions,
|
||||
): Promise<void> => {
|
||||
if (options.itemIds.length === 0) return;
|
||||
|
||||
const sessionApi = getSessionApi(api);
|
||||
const startPositionTicks = Math.max(0, options.startPositionTicks ?? 0);
|
||||
|
||||
await sessionApi.play({
|
||||
sessionId,
|
||||
playCommand: "PlayNow",
|
||||
itemIds: options.itemIds,
|
||||
startIndex: options.startIndex ?? 0,
|
||||
startPositionTicks,
|
||||
mediaSourceId: options.mediaSourceId,
|
||||
});
|
||||
|
||||
if (startPositionTicks <= 0) return;
|
||||
|
||||
// Give the target a moment to load, then correct it if it ignored the
|
||||
// start position. Tolerance is generous: buffering can take a few seconds.
|
||||
for (const delay of [900, 1600, 2600]) {
|
||||
await sleep(delay);
|
||||
let target: SessionInfoDto | undefined;
|
||||
try {
|
||||
target = (await sessionApi.getSessions()).data.find(
|
||||
(session) => session.Id === sessionId,
|
||||
);
|
||||
} catch (error) {
|
||||
console.warn("Could not read the target session position", error);
|
||||
continue;
|
||||
}
|
||||
if (!target?.NowPlayingItem) continue;
|
||||
|
||||
const paused = target.PlayState?.IsPaused ?? false;
|
||||
const expected =
|
||||
startPositionTicks + (paused ? 0 : delay * (TICKS_PER_SECOND / 1000));
|
||||
const actual = target.PlayState?.PositionTicks ?? 0;
|
||||
if (Math.abs(actual - expected) <= 8 * TICKS_PER_SECOND) return;
|
||||
|
||||
try {
|
||||
await sessionApi.sendPlaystateCommand({
|
||||
sessionId,
|
||||
command: PlaystateCommand.Seek,
|
||||
seekPositionTicks: Math.round(expected),
|
||||
controllingUserId: userId,
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn("Could not seek the target session", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Spotify-Connect style transfer: take what another Jellyfin session is
|
||||
* playing (at its current position and with its queue) and play it on this
|
||||
|
||||
Reference in new issue
Block a user