diff --git a/Dockerfile b/Dockerfile index a5f81a0..1ff710b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,7 @@ WORKDIR /app COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc ./ COPY apps/web/package.json ./apps/web/ +COPY packages/convex/package.json ./packages/convex/ RUN --mount=type=cache,target=/root/.local/share/pnpm/store \ corepack enable pnpm && \ @@ -37,6 +38,8 @@ COPY --from=dependencies /app/node_modules ./node_modules COPY --from=dependencies /app/apps/web/node_modules ./apps/web/node_modules COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc ./ COPY apps/web ./apps/web +COPY packages/convex ./packages/convex +COPY --from=dependencies /app/packages/convex/node_modules ./packages/convex/node_modules ENV NODE_ENV=production diff --git a/apps/web/components/playback-provider.tsx b/apps/web/components/playback-provider.tsx index 1ddefbe..4f7b9b5 100644 --- a/apps/web/components/playback-provider.tsx +++ b/apps/web/components/playback-provider.tsx @@ -19,6 +19,7 @@ import { toast } from "sonner" import { api } from "@otunes/convex/_generated/api" import type { Id } from "@otunes/convex/_generated/dataModel" import type { BrowseSong } from "@otunes/convex/types" +import { playbackStreamPlan } from "@otunes/convex/playback-stream" import { supportsDolbyAtmosPlayback } from "@/lib/dolby-atmos-support" import { songHlsPlaylistUrl, songPlayUrl } from "@/lib/song-play-url" @@ -126,12 +127,17 @@ function playbackLoadSettings( preferAtmos?: boolean playbackQuality?: "low" | "medium" | "high" | "lossless" }, - dolbyAtmosSupported: boolean | null + dolbyAtmosSupported: boolean | null, + files: Record ): PlaybackLoadSettings { - const preferAtmos = dolbyAtmosSupported === true && (user.preferAtmos ?? false) - const playbackQuality = user.playbackQuality ?? "medium" - const useHls = playbackQuality !== "lossless" && !preferAtmos - return { preferAtmos, playbackQuality, useHls } + const preferAtmos = + dolbyAtmosSupported === true && (user.preferAtmos ?? false) + const plan = playbackStreamPlan(files, user.playbackQuality, preferAtmos) + return { + preferAtmos: plan.preferAtmos, + playbackQuality: plan.playbackQuality, + useHls: plan.useHls, + } } function songLoadKey(songId: string, settings: PlaybackLoadSettings) { @@ -290,12 +296,20 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { }, []) const currentSong = playback?.currentSong ?? null + const currentSongDoc = useQuery( + api.library.getSong, + currentSong ? { id: currentSong.id } : "skip" + ) const isPlaybackActive = Boolean(currentSong && isPlaying) const repeatEnabled = playback?.repeatEnabled ?? "none" const queueLength = playback?.queueSongIds.length ?? 0 const queueSongIds = playback?.queueSongIds ?? [] const nextQueueSongId = queueSongIds.length > 1 ? queueSongIds[1] : null + const nextQueueSongDoc = useQuery( + api.library.getSong, + nextQueueSongId ? { id: nextQueueSongId } : "skip" + ) const seekTo = useCallback( (seconds: number) => { @@ -547,11 +561,15 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { return } - if (user === undefined || !user) { + if (user === undefined || !user || !currentSongDoc) { return } - const settings = playbackLoadSettings(user, dolbyAtmosSupported) + const settings = playbackLoadSettings( + user, + dolbyAtmosSupported, + currentSongDoc.files + ) const loadKey = songLoadKey(currentSong.id, settings) if (loadedSongKeyRef.current === loadKey) { @@ -605,6 +623,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { } }, [ currentSong?.id, + currentSongDoc, user === undefined, user?.playbackQuality, user?.preferAtmos, @@ -622,11 +641,15 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { return } - if (user === undefined || !user) { + if (user === undefined || !user || !nextQueueSongDoc) { return } - const settings = playbackLoadSettings(user, dolbyAtmosSupported) + const settings = playbackLoadSettings( + user, + dolbyAtmosSupported, + nextQueueSongDoc.files + ) const loadKey = songLoadKey(nextQueueSongId, settings) if (preloadedSongKeyRef.current === loadKey) { @@ -662,6 +685,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { }, [ currentSong?.id, nextQueueSongId, + nextQueueSongDoc, user === undefined, user?.playbackQuality, user?.preferAtmos, @@ -726,8 +750,12 @@ export function PlaybackProvider({ children }: { children: ReactNode }) { } const preloadAudio = preloadAudioRef.current - if (shouldContinuePlaying && preloadAudio && nextQueueSongId && user) { - const settings = playbackLoadSettings(user, dolbyAtmosSupported) + if (shouldContinuePlaying && preloadAudio && nextQueueSongId && user && nextQueueSongDoc) { + const settings = playbackLoadSettings( + user, + dolbyAtmosSupported, + nextQueueSongDoc.files + ) const nextLoadKey = songLoadKey(nextQueueSongId, settings) if ( diff --git a/packages/convex/.gitignore b/packages/convex/.gitignore new file mode 100644 index 0000000..8c5fbb9 --- /dev/null +++ b/packages/convex/.gitignore @@ -0,0 +1,2 @@ + +.env.local diff --git a/packages/convex/convex/_generated/api.d.ts b/packages/convex/convex/_generated/api.d.ts index a2c0df9..a4607bb 100644 --- a/packages/convex/convex/_generated/api.d.ts +++ b/packages/convex/convex/_generated/api.d.ts @@ -16,6 +16,7 @@ import type * as http from "../http.js"; import type * as library from "../library.js"; import type * as playback from "../playback.js"; import type * as playlists from "../playlists.js"; +import type * as types from "../types.js"; import type * as users from "../users.js"; import type { @@ -33,6 +34,7 @@ declare const fullApi: ApiFromModules<{ library: typeof library; playback: typeof playback; playlists: typeof playlists; + types: typeof types; users: typeof users; }>; diff --git a/packages/convex/package.json b/packages/convex/package.json index d8aa052..0f96867 100644 --- a/packages/convex/package.json +++ b/packages/convex/package.json @@ -14,6 +14,10 @@ "./types": { "types": "./convex/types.ts", "default": "./convex/types.ts" + }, + "./playback-stream": { + "types": "./playback-stream.ts", + "default": "./playback-stream.ts" } }, "scripts": { diff --git a/packages/convex/playback-stream.ts b/packages/convex/playback-stream.ts new file mode 100644 index 0000000..b97862e --- /dev/null +++ b/packages/convex/playback-stream.ts @@ -0,0 +1,51 @@ +export type PlaybackQuality = "low" | "medium" | "high" | "lossless" + +export function qualityKbps(quality: PlaybackQuality | null | undefined) { + if (quality === "low") { + return 64 + } + + if (quality === "high") { + return 256 + } + + if (quality === "medium") { + return 128 + } + + return 0 +} + +export type PlaybackStreamPlan = { + playbackQuality: PlaybackQuality + preferAtmos: boolean + transcodeNeeded: boolean + useHls: boolean +} + +export function playbackStreamPlan( + files: Record, + quality: PlaybackQuality | undefined, + preferAtmos: boolean +): PlaybackStreamPlan { + const playbackQuality = quality ?? "medium" + const kbps = + playbackQuality === "lossless" ? 0 : qualityKbps(playbackQuality) + + let transcodeNeeded = true + + if (preferAtmos && "atmos" in files) { + transcodeNeeded = false + } else if (kbps === 0 && "lossless" in files) { + transcodeNeeded = false + } else if (kbps === 0 || (kbps === 256 && "normal" in files)) { + transcodeNeeded = false + } + + return { + playbackQuality, + preferAtmos, + transcodeNeeded, + useHls: transcodeNeeded && !preferAtmos, + } +}