add transcode extension to player songUrl

This commit is contained in:
jeffvli
2026-03-31 01:54:10 -07:00
parent 7e353c4723
commit 833d4d3aac
7 changed files with 291 additions and 117 deletions
@@ -1,4 +1,5 @@
import { useMemo, useRef } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useEffect, useRef } from 'react';
import { api } from '/@/renderer/api';
import { TranscodingConfig } from '/@/renderer/store';
@@ -10,46 +11,58 @@ export function useSongUrl(
transcode: TranscodingConfig,
): string | undefined {
const prior = useRef(['', '']);
const shouldReusePrior = Boolean(
song?._serverId && current && prior.current[0] === song._uniqueId && prior.current[1],
);
return useMemo(() => {
if (song?._serverId) {
// If we are the current track, we do not want a transcoding
// reconfiguration to force a restart.
if (current && prior.current[0] === song._uniqueId) {
return prior.current[1];
}
const url = api.controller.getStreamUrl({
apiClientProps: { serverId: song._serverId },
const { data: queryStreamUrl } = useQuery({
enabled: Boolean(song?._serverId) && !shouldReusePrior,
queryFn: () =>
api.controller.getStreamUrl({
apiClientProps: { serverId: song!._serverId },
query: {
bitrate: transcode.bitrate,
format: transcode.format,
id: song.id,
id: song!.id,
transcode: transcode.enabled,
},
});
}),
queryKey: [
song?._serverId,
'stream-url',
song?.id,
shouldReusePrior ? 'reuse-prior' : transcode.bitrate,
shouldReusePrior ? 'reuse-prior' : transcode.format,
shouldReusePrior ? 'reuse-prior' : transcode.enabled,
] as const,
staleTime: 60 * 1000,
});
// transcoding enabled; save the updated result
prior.current = [song._uniqueId, url];
return url;
useEffect(() => {
if (!song?._serverId) {
prior.current = ['', ''];
return;
}
// no track; clear result
prior.current = ['', ''];
return undefined;
}, [
song?._serverId,
song?._uniqueId,
song?.id,
current,
transcode.bitrate,
transcode.format,
transcode.enabled,
]);
if (!queryStreamUrl) {
return;
}
// Save resolved URL to avoid restarting current track on transcode setting changes.
prior.current = [song._uniqueId, queryStreamUrl];
}, [song?._serverId, song?._uniqueId, queryStreamUrl]);
useEffect(() => {
if (!song?._serverId) {
prior.current = ['', ''];
}
}, [song?._serverId]);
return shouldReusePrior ? prior.current[1] : queryStreamUrl;
}
export const getSongUrl = (song: QueueSong, transcode: TranscodingConfig) => {
return api.controller.getStreamUrl({
export const getSongUrl = async (song: QueueSong, transcode: TranscodingConfig) => {
const url = await api.controller.getStreamUrl({
apiClientProps: { serverId: song._serverId },
query: {
bitrate: transcode.bitrate,
@@ -58,4 +71,6 @@ export const getSongUrl = (song: QueueSong, transcode: TranscodingConfig) => {
transcode: transcode.enabled,
},
});
return url;
};