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
@@ -124,10 +124,10 @@ export const MpvPlayerEngine = (props: MpvPlayerEngineProps) => {
if (!radioState.currentStreamUrl) {
const playerData = usePlayerStore.getState().getPlayerData();
const currentSongUrl = playerData.currentSong
? getSongUrl(playerData.currentSong, transcode)
? await getSongUrl(playerData.currentSong, transcode)
: undefined;
const nextSongUrl = playerData.nextSong
? getSongUrl(playerData.nextSong, transcode)
? await getSongUrl(playerData.nextSong, transcode)
: undefined;
if (currentSongUrl && nextSongUrl && !hasPopulatedQueueRef.current && mpvPlayer) {
@@ -274,14 +274,14 @@ export const MpvPlayerEngine = (props: MpvPlayerEngineProps) => {
onMediaPrev: () => {
replaceMpvQueue(transcode);
},
onNextSongInsertion: (song) => {
onNextSongInsertion: async (song) => {
const radioState = useRadioStore.getState();
if (radioState.currentStreamUrl) {
return;
}
const nextSongUrl = song ? getSongUrl(song, transcode) : undefined;
const nextSongUrl = song ? await getSongUrl(song, transcode) : undefined;
mpvPlayer?.setQueueNext(nextSongUrl);
},
onPlayerPlay: () => {
@@ -339,19 +339,19 @@ export const MpvPlayerEngine = (props: MpvPlayerEngineProps) => {
MpvPlayerEngine.displayName = 'MpvPlayerEngine';
function handleMpvAutoNext(transcode: {
async function handleMpvAutoNext(transcode: {
bitrate?: number | undefined;
enabled: boolean;
format?: string | undefined;
}) {
const playerData = usePlayerStore.getState().getPlayerData();
const nextSongUrl = playerData.nextSong
? getSongUrl(playerData.nextSong, transcode)
? await getSongUrl(playerData.nextSong, transcode)
: undefined;
mpvPlayer?.autoNext(nextSongUrl);
}
function replaceMpvQueue(transcode: {
async function replaceMpvQueue(transcode: {
bitrate?: number | undefined;
enabled: boolean;
format?: string | undefined;
@@ -365,10 +365,10 @@ function replaceMpvQueue(transcode: {
const playerData = usePlayerStore.getState().getPlayerData();
const currentSongUrl = playerData.currentSong
? getSongUrl(playerData.currentSong, transcode)
? await getSongUrl(playerData.currentSong, transcode)
: undefined;
const nextSongUrl = playerData.nextSong
? getSongUrl(playerData.nextSong, transcode)
? await getSongUrl(playerData.nextSong, transcode)
: undefined;
mpvPlayer?.setQueue(currentSongUrl, nextSongUrl, false);
}
@@ -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;
};