feat: Spotify-style transfer between 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
- 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
This commit is contained in:
1 parent
a98663691d
commit
fa29331c1c
12 files changed
+421
-21
No files matched your search
@@ -0,0 +1,120 @@
|
||||
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;
|
||||
};
|
||||
@@ -69,6 +69,7 @@ export const generateAudioStreamUrl = (
|
||||
deviceId: string,
|
||||
basePath: string,
|
||||
api_key: string,
|
||||
startTimeTicks = 0,
|
||||
) => {
|
||||
const transcodingProfile = playbackProfile.TranscodingProfiles?.filter(
|
||||
(val) => val.Type === "Audio" && val.Context === "Streaming",
|
||||
@@ -100,7 +101,7 @@ export const generateAudioStreamUrl = (
|
||||
transcodingProtocol: transcodingProfile?.Protocol,
|
||||
audioCodec: transcodingProfile?.AudioCodec,
|
||||
playSessionId: Date.now(),
|
||||
startTimeTicks: 0,
|
||||
startTimeTicks,
|
||||
enableRemoteMedia: false,
|
||||
enableAudioVbrEncoding: true,
|
||||
};
|
||||
|
||||
@@ -1,19 +1,36 @@
|
||||
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
||||
import { create } from "zustand";
|
||||
|
||||
export type RemoteTransfer = {
|
||||
/** Item to offer for "Play on..." / "Transfer here". */
|
||||
item?: BaseItemDto | null;
|
||||
/** Where the transfer starts from (used when moving local playback). */
|
||||
kind?: "audio" | "video" | "item";
|
||||
positionTicks?: number;
|
||||
queue?: BaseItemDto[];
|
||||
startIndex?: number;
|
||||
};
|
||||
|
||||
type RemoteControlState = {
|
||||
open: boolean;
|
||||
/** Item to offer for "Play on ..." when the dialog is opened from a player. */
|
||||
item: BaseItemDto | null;
|
||||
openDialog: (item?: BaseItemDto | null) => void;
|
||||
transfer: RemoteTransfer | null;
|
||||
openDialog: (arg?: BaseItemDto | RemoteTransfer | null) => void;
|
||||
closeDialog: () => void;
|
||||
};
|
||||
|
||||
const toTransfer = (
|
||||
arg?: BaseItemDto | RemoteTransfer | null,
|
||||
): RemoteTransfer | null => {
|
||||
if (!arg) return null;
|
||||
if ("Type" in arg) return { item: arg as BaseItemDto };
|
||||
return arg as RemoteTransfer;
|
||||
};
|
||||
|
||||
const useRemoteControlStore = create<RemoteControlState>((set) => ({
|
||||
open: false,
|
||||
item: null,
|
||||
openDialog: (item = null) => set({ open: true, item: item ?? null }),
|
||||
closeDialog: () => set({ open: false, item: null }),
|
||||
transfer: null,
|
||||
openDialog: (arg = null) => set({ open: true, transfer: toTransfer(arg) }),
|
||||
closeDialog: () => set({ open: false, transfer: null }),
|
||||
}));
|
||||
|
||||
export default useRemoteControlStore;
|
||||
Reference in new issue
Block a user