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
- persistent remote playback bar when another session is playing (device, title, play/pause, Transfer here) - transfer remote -> local for audio and video, preserving position and queue, then stop the remote session - cast from local players now sends the queue, start index and position, and stops local playback after the transfer - full-screen player and remote dialog share the transfer payload
121 lines
3.2 KiB
TypeScript
121 lines
3.2 KiB
TypeScript
import type { Api } from "@jellyfin/sdk";
|
|
import {
|
|
type BaseItemDto,
|
|
BaseItemKind,
|
|
ItemFields,
|
|
type SessionInfoDto,
|
|
} from "@jellyfin/sdk/lib/generated-client";
|
|
import { getLibraryApi } from "@jellyfin/sdk/lib/utils/api/library-api";
|
|
import { getSessionApi } from "@jellyfin/sdk/lib/utils/api/session-api";
|
|
import { generateAudioStreamUrl, playAudio } from "@/utils/store/audioPlayback";
|
|
import { initializePlayback } from "@/utils/store/playback";
|
|
import { setQueue } from "@/utils/store/queue";
|
|
import { getPlaybackInfo } from "./playback";
|
|
|
|
const fields = [
|
|
ItemFields.MediaSources,
|
|
ItemFields.MediaStreams,
|
|
ItemFields.Chapters,
|
|
ItemFields.Trickplay,
|
|
ItemFields.Overview,
|
|
];
|
|
|
|
/**
|
|
* Spotify-Connect style transfer: take what another Jellyfin session is
|
|
* playing (at its current position and with its queue) and play it on this
|
|
* client, then stop the remote session.
|
|
*/
|
|
export const transferSessionToLocal = async (
|
|
api: Api | undefined,
|
|
userId: string | undefined,
|
|
session: SessionInfoDto,
|
|
): Promise<"audio" | "video" | "none"> => {
|
|
const item = session.NowPlayingItem;
|
|
if (!api || !userId || !item?.Id || !session.Id) {
|
|
return "none";
|
|
}
|
|
|
|
const positionTicks = session.PlayState?.PositionTicks ?? 0;
|
|
|
|
// Rebuild the remote queue when the server reports it.
|
|
let queue: BaseItemDto[] = [item];
|
|
let queueIndex = 0;
|
|
const queueIds = (session.NowPlayingQueue ?? [])
|
|
.map((queueItem) => queueItem.Id)
|
|
.filter((id): id is string => Boolean(id));
|
|
if (queueIds.length > 0) {
|
|
try {
|
|
const result = await getLibraryApi(api).getItems({
|
|
userId,
|
|
ids: queueIds,
|
|
fields,
|
|
enableUserData: true,
|
|
});
|
|
const items = result.data.Items ?? [];
|
|
if (items.length > 0) {
|
|
queue = items;
|
|
const index = items.findIndex((entry) => entry.Id === item.Id);
|
|
queueIndex = index >= 0 ? index : 0;
|
|
}
|
|
} catch (error) {
|
|
console.warn("Could not restore remote queue", error);
|
|
}
|
|
}
|
|
|
|
let kind: "audio" | "video" = "video";
|
|
if (item.Type === BaseItemKind.Audio || item.Type === BaseItemKind.AudioBook) {
|
|
kind = "audio";
|
|
const url = generateAudioStreamUrl(
|
|
item.Id,
|
|
userId,
|
|
api.deviceInfo.id,
|
|
api.basePath,
|
|
api.accessToken,
|
|
positionTicks,
|
|
);
|
|
playAudio(url, item, undefined);
|
|
setQueue(queue, queueIndex);
|
|
} else {
|
|
const fullItem =
|
|
(
|
|
await getLibraryApi(api).getItems({
|
|
userId,
|
|
ids: [item.Id],
|
|
fields,
|
|
enableUserData: true,
|
|
})
|
|
).data.Items?.[0] ?? item;
|
|
const playback = await getPlaybackInfo(api, userId, fullItem);
|
|
const queueItems: BaseItemDto[] = playback.item?.Items?.length
|
|
? playback.item.Items
|
|
: queue;
|
|
const index = Math.max(
|
|
0,
|
|
queueItems.findIndex((entry: BaseItemDto) => entry.Id === fullItem.Id),
|
|
);
|
|
await initializePlayback({
|
|
api,
|
|
userId,
|
|
item: fullItem,
|
|
mediaSource: playback.mediaSource,
|
|
mediaSegments: playback.mediaSegments,
|
|
queueItems,
|
|
queueIndex: index,
|
|
startPositionTicks: positionTicks,
|
|
});
|
|
}
|
|
|
|
// Transfer, not cast: the remote device should stop.
|
|
try {
|
|
await getSessionApi(api).sendPlaystateCommand({
|
|
sessionId: session.Id,
|
|
command: "Stop",
|
|
controllingUserId: userId,
|
|
});
|
|
} catch (error) {
|
|
console.warn("Could not stop the remote session", error);
|
|
}
|
|
|
|
return kind;
|
|
};
|