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
@@ -2,21 +2,36 @@ import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
|||||||
import IconButton from "@mui/material/IconButton";
|
import IconButton from "@mui/material/IconButton";
|
||||||
import Tooltip from "@mui/material/Tooltip";
|
import Tooltip from "@mui/material/Tooltip";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import useRemoteControlStore from "@/utils/store/remoteControl";
|
import useRemoteControlStore, {
|
||||||
|
type RemoteTransfer,
|
||||||
|
} from "@/utils/store/remoteControl";
|
||||||
|
|
||||||
type CastButtonProps = {
|
type CastButtonProps = {
|
||||||
item?: BaseItemDto | null;
|
item?: BaseItemDto | null;
|
||||||
|
transfer?: Omit<RemoteTransfer, "item">;
|
||||||
|
/** Computed on click so playback position/queue are current. */
|
||||||
|
getTransfer?: () => Omit<RemoteTransfer, "item">;
|
||||||
size?: "small" | "medium" | "large";
|
size?: "small" | "medium" | "large";
|
||||||
};
|
};
|
||||||
|
|
||||||
const CastButton = ({ item, size = "medium" }: CastButtonProps) => {
|
const CastButton = ({
|
||||||
|
item,
|
||||||
|
transfer,
|
||||||
|
getTransfer,
|
||||||
|
size = "medium",
|
||||||
|
}: CastButtonProps) => {
|
||||||
const openDialog = useRemoteControlStore((s) => s.openDialog);
|
const openDialog = useRemoteControlStore((s) => s.openDialog);
|
||||||
return (
|
return (
|
||||||
<Tooltip title={item ? "Play on another device" : "Remote control"}>
|
<Tooltip title={item ? "Play on another device" : "Remote control"}>
|
||||||
<IconButton
|
<IconButton
|
||||||
size={size}
|
size={size}
|
||||||
aria-label={item ? "Play on another device" : "Remote control"}
|
aria-label={item ? "Play on another device" : "Remote control"}
|
||||||
onClick={() => openDialog(item ?? null)}
|
onClick={() =>
|
||||||
|
openDialog({
|
||||||
|
...(getTransfer ? getTransfer() : transfer),
|
||||||
|
item: item ?? null,
|
||||||
|
})
|
||||||
|
}
|
||||||
>
|
>
|
||||||
<span className="material-symbols-rounded">cast</span>
|
<span className="material-symbols-rounded">cast</span>
|
||||||
</IconButton>
|
</IconButton>
|
||||||
|
|||||||
@@ -12,15 +12,34 @@ interface PlayerActionsProps {
|
|||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
/** The track currently playing (for cast / instant mix). */
|
/** The track currently playing (for cast / instant mix). */
|
||||||
item?: BaseItemDto | null;
|
item?: BaseItemDto | null;
|
||||||
|
positionTicks?: number;
|
||||||
|
queue?: BaseItemDto[];
|
||||||
|
startIndex?: number;
|
||||||
/** Volume control, rendered between the queue and expand buttons. */
|
/** Volume control, rendered between the queue and expand buttons. */
|
||||||
children?: ReactNode;
|
children?: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PlayerActions = ({ onExpand, onClose, item, children }: PlayerActionsProps) => {
|
const PlayerActions = ({
|
||||||
|
onExpand,
|
||||||
|
onClose,
|
||||||
|
item,
|
||||||
|
positionTicks = 0,
|
||||||
|
queue = [],
|
||||||
|
startIndex = 0,
|
||||||
|
children,
|
||||||
|
}: PlayerActionsProps) => {
|
||||||
return (
|
return (
|
||||||
<div className="audio-player-actions">
|
<div className="audio-player-actions">
|
||||||
{item && <InstantMixButton item={item} />}
|
{item && <InstantMixButton item={item} />}
|
||||||
{item && <CastButton item={item} />}
|
{item && <CastButton
|
||||||
|
item={item}
|
||||||
|
transfer={{
|
||||||
|
kind: "audio",
|
||||||
|
positionTicks,
|
||||||
|
queue,
|
||||||
|
startIndex,
|
||||||
|
}}
|
||||||
|
/>}
|
||||||
<QueueButton />
|
<QueueButton />
|
||||||
<div className="audio-player-volume">{children}</div>
|
<div className="audio-player-volume">{children}</div>
|
||||||
<Tooltip title="Open player">
|
<Tooltip title="Open player">
|
||||||
|
|||||||
@@ -245,7 +245,14 @@ const AudioPlayer = () => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<PlayerActions onExpand={handleNavigate} onClose={handleClose} item={item}>
|
<PlayerActions
|
||||||
|
onExpand={handleNavigate}
|
||||||
|
onClose={handleClose}
|
||||||
|
item={item}
|
||||||
|
positionTicks={secToTicks(progress)}
|
||||||
|
queue={tracks ?? []}
|
||||||
|
startIndex={currentTrack}
|
||||||
|
>
|
||||||
<PlayerVolume
|
<PlayerVolume
|
||||||
volume={volume}
|
volume={volume}
|
||||||
isMuted={isMuted}
|
isMuted={isMuted}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import VideoPlayerSettingsMenu from "./settingsMenu";
|
|||||||
|
|
||||||
import "./controls.scss";
|
import "./controls.scss";
|
||||||
import { clearQueue } from "@/utils/store/queue";
|
import { clearQueue } from "@/utils/store/queue";
|
||||||
|
import useQueue from "@/utils/store/queue";
|
||||||
import ProgressDisplay from "./ProgressDisplay";
|
import ProgressDisplay from "./ProgressDisplay";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -307,7 +308,17 @@ const VideoPlayerControls = ({
|
|||||||
: "volume_up"}
|
: "volume_up"}
|
||||||
</span>
|
</span>
|
||||||
</IconButton>
|
</IconButton>
|
||||||
<CastButton item={item} />
|
<CastButton
|
||||||
|
item={item}
|
||||||
|
getTransfer={() => ({
|
||||||
|
kind: "video",
|
||||||
|
positionTicks: secToTicks(
|
||||||
|
usePlaybackStore.getState().getCurrentTime(),
|
||||||
|
),
|
||||||
|
queue: useQueue.getState().tracks ?? [],
|
||||||
|
startIndex: useQueue.getState().currentItemIndex,
|
||||||
|
})}
|
||||||
|
/>
|
||||||
<QueueButton />
|
<QueueButton />
|
||||||
|
|
||||||
<ChaptersListButton />
|
<ChaptersListButton />
|
||||||
|
|||||||
@@ -20,10 +20,14 @@ import {
|
|||||||
Typography,
|
Typography,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
|
import { useNavigate } from "@tanstack/react-router";
|
||||||
import { useSnackbar } from "notistack";
|
import { useSnackbar } from "notistack";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useApiInContext } from "@/utils/store/api";
|
import { useApiInContext } from "@/utils/store/api";
|
||||||
|
import { stopPlayback } from "@/utils/store/audioPlayback";
|
||||||
import { useCentralStore } from "@/utils/store/central";
|
import { useCentralStore } from "@/utils/store/central";
|
||||||
|
import { usePlaybackStore } from "@/utils/store/playback";
|
||||||
|
import { clearQueue } from "@/utils/store/queue";
|
||||||
import useRemoteControlStore from "@/utils/store/remoteControl";
|
import useRemoteControlStore from "@/utils/store/remoteControl";
|
||||||
|
|
||||||
const TICKS_PER_SECOND = 10_000_000;
|
const TICKS_PER_SECOND = 10_000_000;
|
||||||
@@ -42,10 +46,12 @@ const sessionLabel = (session: SessionInfoDto) =>
|
|||||||
session.DeviceName || session.Client || session.DeviceId || "Unknown device";
|
session.DeviceName || session.Client || session.DeviceId || "Unknown device";
|
||||||
|
|
||||||
const RemoteControlDialog = () => {
|
const RemoteControlDialog = () => {
|
||||||
const { open, item, closeDialog } = useRemoteControlStore();
|
const { open, transfer, closeDialog } = useRemoteControlStore();
|
||||||
|
const item = transfer?.item;
|
||||||
const api = useApiInContext((s) => s.api);
|
const api = useApiInContext((s) => s.api);
|
||||||
const user = useCentralStore((s) => s.currentUser);
|
const user = useCentralStore((s) => s.currentUser);
|
||||||
const { enqueueSnackbar } = useSnackbar();
|
const { enqueueSnackbar } = useSnackbar();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const sessions = useQuery({
|
const sessions = useQuery({
|
||||||
queryKey: ["remoteSessions"],
|
queryKey: ["remoteSessions"],
|
||||||
@@ -87,18 +93,33 @@ const RemoteControlDialog = () => {
|
|||||||
const playOn = useMutation({
|
const playOn = useMutation({
|
||||||
mutationFn: async (session: SessionInfoDto) => {
|
mutationFn: async (session: SessionInfoDto) => {
|
||||||
if (!api || !session.Id || !item?.Id) return;
|
if (!api || !session.Id || !item?.Id) return;
|
||||||
|
const queueIds =
|
||||||
|
transfer?.queue
|
||||||
|
?.map((queueItem) => queueItem.Id)
|
||||||
|
.filter((id): id is string => Boolean(id)) ?? [];
|
||||||
await getSessionApi(api).play({
|
await getSessionApi(api).play({
|
||||||
sessionId: session.Id,
|
sessionId: session.Id,
|
||||||
playCommand: PlayCommand.PlayNow,
|
playCommand: PlayCommand.PlayNow,
|
||||||
itemIds: [item.Id],
|
itemIds: queueIds.length > 0 ? queueIds : [item.Id],
|
||||||
startPositionTicks: 0,
|
startIndex: transfer?.startIndex ?? 0,
|
||||||
|
startPositionTicks: transfer?.positionTicks ?? 0,
|
||||||
mediaSourceId: item.MediaSources?.[0]?.Id ?? undefined,
|
mediaSourceId: item.MediaSources?.[0]?.Id ?? undefined,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onSuccess: (_data, session) => {
|
onSuccess: (_data, session) => {
|
||||||
enqueueSnackbar(`Playing on ${sessionLabel(session)}`, {
|
enqueueSnackbar(
|
||||||
variant: "success",
|
transfer?.kind === "audio" || transfer?.kind === "video"
|
||||||
});
|
? `Playback transferred to ${sessionLabel(session)}`
|
||||||
|
: `Playing on ${sessionLabel(session)}`,
|
||||||
|
{ variant: "success" },
|
||||||
|
);
|
||||||
|
if (transfer?.kind === "audio") {
|
||||||
|
stopPlayback();
|
||||||
|
} else if (transfer?.kind === "video") {
|
||||||
|
usePlaybackStore.setState(usePlaybackStore.getInitialState());
|
||||||
|
clearQueue();
|
||||||
|
navigate({ to: "/home" });
|
||||||
|
}
|
||||||
closeDialog();
|
closeDialog();
|
||||||
},
|
},
|
||||||
onError: () =>
|
onError: () =>
|
||||||
|
|||||||
@@ -0,0 +1,137 @@
|
|||||||
|
import { PlaystateCommand, type SessionInfoDto } from "@jellyfin/sdk/lib/generated-client";
|
||||||
|
import { getSessionApi } from "@jellyfin/sdk/lib/utils/api/session-api";
|
||||||
|
import { Button, IconButton, Tooltip, Typography } from "@mui/material";
|
||||||
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
|
import { useLocation, useNavigate } from "@tanstack/react-router";
|
||||||
|
import { useSnackbar } from "notistack";
|
||||||
|
import React from "react";
|
||||||
|
import { transferSessionToLocal } from "@/utils/methods/transferPlayback";
|
||||||
|
import { useApiInContext } from "@/utils/store/api";
|
||||||
|
import { useAudioPlayback } from "@/utils/store/audioPlayback";
|
||||||
|
import { useCentralStore } from "@/utils/store/central";
|
||||||
|
import useRemoteControlStore from "@/utils/store/remoteControl";
|
||||||
|
|
||||||
|
import "./remotePlaybackBar.scss";
|
||||||
|
|
||||||
|
const RemotePlaybackBar = () => {
|
||||||
|
const api = useApiInContext((s) => s.api);
|
||||||
|
const user = useCentralStore((s) => s.currentUser);
|
||||||
|
const location = useLocation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const { enqueueSnackbar } = useSnackbar();
|
||||||
|
const openDialog = useRemoteControlStore((s) => s.openDialog);
|
||||||
|
const audioDisplay = useAudioPlayback((s) => s.display);
|
||||||
|
|
||||||
|
const sessions = useQuery({
|
||||||
|
queryKey: ["remoteSessions"],
|
||||||
|
queryFn: async () => {
|
||||||
|
if (!api || !user?.Id) return [];
|
||||||
|
const result = await getSessionApi(api).getSessions({
|
||||||
|
controllableByUserId: user.Id,
|
||||||
|
});
|
||||||
|
return result.data;
|
||||||
|
},
|
||||||
|
enabled: !!api && !!user?.Id,
|
||||||
|
refetchInterval: 5000,
|
||||||
|
networkMode: "always",
|
||||||
|
});
|
||||||
|
|
||||||
|
const remote = (sessions.data ?? []).find(
|
||||||
|
(session: SessionInfoDto) =>
|
||||||
|
session.Id &&
|
||||||
|
session.DeviceId !== api?.deviceInfo.id &&
|
||||||
|
session.NowPlayingItem,
|
||||||
|
);
|
||||||
|
|
||||||
|
const command = useMutation({
|
||||||
|
mutationFn: async ({
|
||||||
|
session,
|
||||||
|
command,
|
||||||
|
}: {
|
||||||
|
session: SessionInfoDto;
|
||||||
|
command: PlaystateCommand;
|
||||||
|
}) => {
|
||||||
|
if (!api || !session.Id) return;
|
||||||
|
await getSessionApi(api).sendPlaystateCommand({
|
||||||
|
sessionId: session.Id,
|
||||||
|
command,
|
||||||
|
controllingUserId: user?.Id,
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onSettled: () => sessions.refetch(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const transfer = useMutation({
|
||||||
|
mutationFn: async (session: SessionInfoDto) =>
|
||||||
|
transferSessionToLocal(api, user?.Id, session),
|
||||||
|
onSuccess: async (kind) => {
|
||||||
|
await sessions.refetch();
|
||||||
|
if (kind === "none") {
|
||||||
|
enqueueSnackbar("Could not transfer playback", { variant: "error" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
enqueueSnackbar("Playback transferred to this device", {
|
||||||
|
variant: "success",
|
||||||
|
});
|
||||||
|
if (kind === "video") {
|
||||||
|
navigate({ to: "/player" });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onError: () =>
|
||||||
|
enqueueSnackbar("Could not transfer playback", { variant: "error" }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!remote || location.pathname.startsWith("/player")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const paused = remote.PlayState?.IsPaused;
|
||||||
|
const title = remote.NowPlayingItem?.Name ?? "Unknown";
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={`remote-playback-bar${audioDisplay ? " with-audio" : ""}`}>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
aria-label="Open remote control"
|
||||||
|
onClick={() => openDialog()}
|
||||||
|
>
|
||||||
|
<span className="material-symbols-rounded">cast_connected</span>
|
||||||
|
</IconButton>
|
||||||
|
<div className="remote-playback-bar-info">
|
||||||
|
<Typography variant="caption" className="remote-playback-bar-device">
|
||||||
|
Playing on {remote.DeviceName || remote.Client || "device"}
|
||||||
|
</Typography>
|
||||||
|
<Typography variant="body2" noWrap className="remote-playback-bar-title">
|
||||||
|
{title}
|
||||||
|
</Typography>
|
||||||
|
</div>
|
||||||
|
<Tooltip title={paused ? "Play" : "Pause"}>
|
||||||
|
<IconButton
|
||||||
|
size="small"
|
||||||
|
onClick={() =>
|
||||||
|
command.mutate({
|
||||||
|
session: remote,
|
||||||
|
command: paused
|
||||||
|
? PlaystateCommand.Unpause
|
||||||
|
: PlaystateCommand.Pause,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<span className="material-symbols-rounded">
|
||||||
|
{paused ? "play_arrow" : "pause"}
|
||||||
|
</span>
|
||||||
|
</IconButton>
|
||||||
|
</Tooltip>
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
variant="contained"
|
||||||
|
disabled={transfer.isPending}
|
||||||
|
onClick={() => transfer.mutate(remote)}
|
||||||
|
>
|
||||||
|
Transfer here
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default React.memo(RemotePlaybackBar);
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
.remote-playback-bar {
|
||||||
|
position: fixed;
|
||||||
|
left: 50%;
|
||||||
|
bottom: 1rem;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
z-index: 99;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.6rem;
|
||||||
|
width: min(620px, calc(100% - 2rem));
|
||||||
|
padding: 0.4rem 0.4rem 0.4rem 0.6rem;
|
||||||
|
box-sizing: border-box;
|
||||||
|
border-radius: 12px;
|
||||||
|
background: rgb(18 18 24 / 0.97);
|
||||||
|
border: 1px solid rgb(255 255 255 / 0.1);
|
||||||
|
box-shadow: 0 10px 30px rgb(0 0 0 / 0.45);
|
||||||
|
color: white;
|
||||||
|
|
||||||
|
&.with-audio {
|
||||||
|
bottom: 6.2rem; // above the 80px audio player
|
||||||
|
}
|
||||||
|
|
||||||
|
.material-symbols-rounded {
|
||||||
|
color: rgb(255 255 255 / 0.85);
|
||||||
|
}
|
||||||
|
|
||||||
|
&-info {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
line-height: 1.15;
|
||||||
|
}
|
||||||
|
|
||||||
|
&-device {
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
&-title {
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,7 @@ import { HTML5Backend } from "react-dnd-html5-backend";
|
|||||||
import Backdrop from "@/components/backdrop";
|
import Backdrop from "@/components/backdrop";
|
||||||
import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice";
|
import { ErrorNotice } from "@/components/notices/errorNotice/errorNotice";
|
||||||
import RemoteControlDialog from "@/components/remoteControl";
|
import RemoteControlDialog from "@/components/remoteControl";
|
||||||
|
import RemotePlaybackBar from "@/components/remotePlaybackBar";
|
||||||
import RouterLoading from "@/components/routerLoading";
|
import RouterLoading from "@/components/routerLoading";
|
||||||
import Search from "@/components/search";
|
import Search from "@/components/search";
|
||||||
import Updater from "@/components/updater";
|
import Updater from "@/components/updater";
|
||||||
@@ -58,6 +59,7 @@ export const Route = createRootRouteWithContext<ApiContext>()({
|
|||||||
<Backdrop />
|
<Backdrop />
|
||||||
<Search />
|
<Search />
|
||||||
<RemoteControlDialog />
|
<RemoteControlDialog />
|
||||||
|
<RemotePlaybackBar />
|
||||||
<AppBar />
|
<AppBar />
|
||||||
<AudioPlayer />
|
<AudioPlayer />
|
||||||
<Outlet />
|
<Outlet />
|
||||||
|
|||||||
@@ -207,7 +207,15 @@ function AudioPlayerRoute() {
|
|||||||
{item && <TrackSubtitle item={item} />}
|
{item && <TrackSubtitle item={item} />}
|
||||||
</div>
|
</div>
|
||||||
{item && <InstantMixButton item={item} />}
|
{item && <InstantMixButton item={item} />}
|
||||||
{item && <CastButton item={item} />}
|
{item && <CastButton
|
||||||
|
item={item}
|
||||||
|
transfer={{
|
||||||
|
kind: "audio",
|
||||||
|
positionTicks: secToTicks(progress),
|
||||||
|
queue: queue ?? [],
|
||||||
|
startIndex: currentTrack,
|
||||||
|
}}
|
||||||
|
/>}
|
||||||
<LikeButton
|
<LikeButton
|
||||||
itemId={item?.Id ?? undefined}
|
itemId={item?.Id ?? undefined}
|
||||||
itemName={item?.Name}
|
itemName={item?.Name}
|
||||||
|
|||||||
@@ -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,
|
deviceId: string,
|
||||||
basePath: string,
|
basePath: string,
|
||||||
api_key: string,
|
api_key: string,
|
||||||
|
startTimeTicks = 0,
|
||||||
) => {
|
) => {
|
||||||
const transcodingProfile = playbackProfile.TranscodingProfiles?.filter(
|
const transcodingProfile = playbackProfile.TranscodingProfiles?.filter(
|
||||||
(val) => val.Type === "Audio" && val.Context === "Streaming",
|
(val) => val.Type === "Audio" && val.Context === "Streaming",
|
||||||
@@ -100,7 +101,7 @@ export const generateAudioStreamUrl = (
|
|||||||
transcodingProtocol: transcodingProfile?.Protocol,
|
transcodingProtocol: transcodingProfile?.Protocol,
|
||||||
audioCodec: transcodingProfile?.AudioCodec,
|
audioCodec: transcodingProfile?.AudioCodec,
|
||||||
playSessionId: Date.now(),
|
playSessionId: Date.now(),
|
||||||
startTimeTicks: 0,
|
startTimeTicks,
|
||||||
enableRemoteMedia: false,
|
enableRemoteMedia: false,
|
||||||
enableAudioVbrEncoding: true,
|
enableAudioVbrEncoding: true,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,19 +1,36 @@
|
|||||||
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
import type { BaseItemDto } from "@jellyfin/sdk/lib/generated-client";
|
||||||
import { create } from "zustand";
|
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 = {
|
type RemoteControlState = {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
/** Item to offer for "Play on ..." when the dialog is opened from a player. */
|
transfer: RemoteTransfer | null;
|
||||||
item: BaseItemDto | null;
|
openDialog: (arg?: BaseItemDto | RemoteTransfer | null) => void;
|
||||||
openDialog: (item?: BaseItemDto | null) => void;
|
|
||||||
closeDialog: () => 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) => ({
|
const useRemoteControlStore = create<RemoteControlState>((set) => ({
|
||||||
open: false,
|
open: false,
|
||||||
item: null,
|
transfer: null,
|
||||||
openDialog: (item = null) => set({ open: true, item: item ?? null }),
|
openDialog: (arg = null) => set({ open: true, transfer: toTransfer(arg) }),
|
||||||
closeDialog: () => set({ open: false, item: null }),
|
closeDialog: () => set({ open: false, transfer: null }),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
export default useRemoteControlStore;
|
export default useRemoteControlStore;
|
||||||
Reference in new issue
Block a user