feat(transcode): add multi-resolution strategy with configurable ladders and fallback (#438)

* feat(transcode): add multi-resolution strategy with configurable ladders and fallback

* feat(settings): show toast on settings update and add manual verification plan to PR template

* fix(webui): preserve video resolution and fix 480p label in download dropdowns

* fix(webui): display standard short-side resolution for vertical videos in seekbar

* fix(webui): fix portrait transcode resolution labeling and serialize settings toggles
This commit is contained in:
Yiling-J
2026-10-01 13:27:50 +08:00
committed by GitHub
parent a84da8678e
commit e007054f47
35 changed files with 1260 additions and 123 deletions
+27 -4
View File
@@ -245,17 +245,17 @@ describe('team api', () => {
})
it('PATCH /teams/:teamId/settings updates settings', async () => {
mockUpdateSettings.mockResolvedValue({ transcode: { videoStrategy: 'all' } })
mockUpdateSettings.mockResolvedValue({ transcode: { videoStrategy: 'multi' } })
const res = await app.request('/teams/t1/settings', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key: 'transcode.videoStrategy', value: 'all' }),
body: JSON.stringify({ key: 'transcode.videoStrategy', value: 'multi' }),
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.transcode.videoStrategy).toBe('all')
expect(data.transcode.videoStrategy).toBe('multi')
expect(authzService.hasPermission).toHaveBeenCalledWith(
expect.objectContaining({
type: ResourceType.Team,
@@ -263,7 +263,30 @@ describe('team api', () => {
permission: Permission.Admin,
}),
)
expect(mockUpdateSettings).toHaveBeenCalledWith('t1', 'transcode.videoStrategy', 'all')
expect(mockUpdateSettings).toHaveBeenCalledWith('t1', 'transcode.videoStrategy', 'multi')
})
it('PATCH /teams/:teamId/settings updates transcode.videoResolutions', async () => {
mockUpdateSettings.mockResolvedValue({
transcode: { videoResolutions: ['720p', '1080p'] },
})
const res = await app.request('/teams/t1/settings', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
key: 'transcode.videoResolutions',
value: ['720p', '1080p'],
}),
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data.transcode.videoResolutions).toEqual(['720p', '1080p'])
expect(mockUpdateSettings).toHaveBeenCalledWith('t1', 'transcode.videoResolutions', [
'720p',
'1080p',
])
})
it('PATCH /teams/:teamId/settings updates transcode.hardwareAcceleration', async () => {
+6 -2
View File
@@ -369,6 +369,7 @@ describe('TeamService', () => {
semanticSearchEnabled: false,
transcode: {
videoStrategy: 'best_match',
videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'],
hardwareAcceleration: 'off',
threads: 0,
hlsEnabled: false,
@@ -385,6 +386,7 @@ describe('TeamService', () => {
semanticSearchEnabled: false,
transcode: {
videoStrategy: 'best_match',
videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'],
hardwareAcceleration: 'off',
threads: 0,
hlsEnabled: false,
@@ -393,7 +395,8 @@ describe('TeamService', () => {
appearance: { hideAgent: false },
})
await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'all')
await teamService.updateSettings(team.id, 'transcode.videoStrategy', 'multi')
await teamService.updateSettings(team.id, 'transcode.videoResolutions', ['720p', '1080p'])
await teamService.updateSettings(team.id, 'transcode.hardwareAcceleration', 'auto')
await teamService.updateSettings(team.id, 'transcode.threads', 4)
await teamService.updateSettings(team.id, 'transcode.hlsEnabled', true)
@@ -408,7 +411,8 @@ describe('TeamService', () => {
expect(finalSettings).toEqual({
theme: 'dark',
transcode: {
videoStrategy: 'all',
videoStrategy: 'multi',
videoResolutions: ['720p', '1080p'],
hardwareAcceleration: 'auto',
threads: 4,
hlsEnabled: true,
+27 -1
View File
@@ -35,6 +35,7 @@ export class TeamService {
settings: {
transcode: {
videoStrategy: 'best_match',
videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'],
hardwareAcceleration: 'off',
threads: 0,
},
@@ -312,12 +313,22 @@ export class TeamService {
if (!settings.transcode) {
settings.transcode = {
videoStrategy: 'best_match',
videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'],
hardwareAcceleration: 'off',
threads: 0,
hlsEnabled: false,
hlsResolutions: ['480p', '720p', '1080p'],
}
} else {
const strategyStr = settings.transcode.videoStrategy as string | undefined
if (strategyStr === 'single' || strategyStr === 'disable') {
settings.transcode.videoStrategy = 'best_match'
} else if (strategyStr === 'full' || strategyStr === 'all') {
settings.transcode.videoStrategy = 'multi'
}
if (!settings.transcode.videoResolutions) {
settings.transcode.videoResolutions = ['480p', '720p', '1080p', '1440p', '2160p']
}
if (settings.transcode.hlsEnabled === undefined) {
settings.transcode.hlsEnabled = false
}
@@ -345,10 +356,25 @@ export class TeamService {
if (!settings.transcode) {
settings.transcode = {}
}
settings.transcode.videoStrategy = value as NonNullable<
const rawStrategy = value as string
let strategy = rawStrategy
if (rawStrategy === 'single' || rawStrategy === 'disable') {
strategy = 'best_match'
} else if (rawStrategy === 'full' || rawStrategy === 'all') {
strategy = 'multi'
}
settings.transcode.videoStrategy = strategy as NonNullable<
TeamSettingsResponse['transcode']
>['videoStrategy']
delete settings['transcode.videoStrategy']
} else if (key === 'transcode.videoResolutions') {
if (!settings.transcode) {
settings.transcode = {}
}
settings.transcode.videoResolutions = value as NonNullable<
TeamSettingsResponse['transcode']
>['videoResolutions']
delete settings['transcode.videoResolutions']
} else if (key === 'transcode.hardwareAcceleration') {
if (!settings.transcode) {
settings.transcode = {}
+5 -3
View File
@@ -203,13 +203,14 @@ describe('UploadService', () => {
})
})
it('should pass custom transcode.threads from team settings to transcode task', async () => {
it('should pass custom transcode.threads and videoResolutions from team settings to transcode task', async () => {
await prisma.team.update({
where: { id: teamId },
data: {
settings: {
transcode: {
videoStrategy: 'all',
videoStrategy: 'multi',
videoResolutions: ['720p', '1080p'],
hardwareAcceleration: 'auto',
threads: 12,
},
@@ -245,7 +246,8 @@ describe('UploadService', () => {
expect(workflowTask?.payload).toEqual({
projectId: projectId,
transcode: {
videoStrategy: 'all',
videoStrategy: 'multi',
videoResolutions: ['720p', '1080p'],
hardwareAcceleration: 'auto',
threads: 12,
sprite: true,
+2
View File
@@ -334,12 +334,14 @@ export class UploadService {
const threads = settings?.transcode?.threads ?? 0
if (isVideo) {
const strategy = settings?.transcode?.videoStrategy || 'best_match'
const videoResolutions = settings?.transcode?.videoResolutions
const hardwareAcceleration = settings?.transcode?.hardwareAcceleration || 'off'
const hlsEnabled = settings?.transcode?.hlsEnabled ?? false
const hlsResolutions = settings?.transcode?.hlsResolutions
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await new VideoTranscoder(tx as any, asset.id, team.id, projectId)
.setStrategy(strategy)
.setVideoResolutions(videoResolutions)
.setHardwareAcceleration(hardwareAcceleration)
.setThreads(threads)
.setHls(hlsEnabled, hlsResolutions)
@@ -0,0 +1,16 @@
-- Migrate videoStrategy 'all' / 'full' to 'multi'
UPDATE "teams"
SET "settings" = jsonb_set("settings", '{transcode,videoStrategy}', '"multi"')
WHERE "settings"->'transcode'->>'videoStrategy' IN ('all', 'full');
-- Normalize legacy videoStrategy values 'single' / 'disable' to 'best_match'
UPDATE "teams"
SET "settings" = jsonb_set("settings", '{transcode,videoStrategy}', '"best_match"')
WHERE "settings"->'transcode'->>'videoStrategy' IN ('single', 'disable');
-- Backfill default videoResolutions where transcode settings exist without videoResolutions
UPDATE "teams"
SET "settings" = jsonb_set("settings", '{transcode,videoResolutions}', '["480p", "720p", "1080p", "1440p", "2160p"]'::jsonb)
WHERE "settings" IS NOT NULL
AND jsonb_typeof("settings"->'transcode') = 'object'
AND NOT ("settings"->'transcode' ? 'videoResolutions');
+4 -1
View File
@@ -48,7 +48,8 @@ declare global {
// ----------------------------------------------------------------------
// Team Settings
// ----------------------------------------------------------------------
export type VideoTranscodeStrategy = 'best_match' | 'all'
export type VideoTranscodeStrategy = 'best_match' | 'multi' | 'all'
export type VideoResolutionLadder = '480p' | '720p' | '1080p' | '1440p' | '2160p'
export type HardwareAcceleration = 'off' | 'auto'
export type HdrType = 'pq' | 'hlg' | 'dovi_p5' | 'dovi_p8' | 'sdr'
@@ -61,6 +62,7 @@ declare global {
export interface TranscodeSettings {
videoStrategy: VideoTranscodeStrategy
videoResolutions?: VideoResolutionLadder[]
hardwareAcceleration?: HardwareAcceleration
threads?: number
hlsEnabled?: boolean
@@ -348,6 +350,7 @@ declare global {
// ----------------------------------------------------------------------
export interface TaskSpec {
videoStrategy?: VideoTranscodeStrategy
videoResolutions?: VideoResolutionLadder[]
hardwareAcceleration?: HardwareAcceleration
threads?: number
thumbnail?: boolean
+2
View File
@@ -105,6 +105,7 @@ export const assetInfoSchema = z.object({
width: z.number(),
height: z.number(),
size: z.number(),
resolution: z.string().optional(),
hdr: z.boolean().optional(),
}),
)
@@ -357,6 +358,7 @@ export interface VideoTranscode {
width: number
height: number
size: number
resolution?: string
hdr?: boolean
}
+24
View File
@@ -69,11 +69,30 @@ export const listMembersQuerySchema = z.object({
export const VideoTranscodeStrategy = {
// eslint-disable-next-line @typescript-eslint/naming-convention
best_match: 'best_match',
multi: 'multi',
all: 'all',
} as const
export type VideoTranscodeStrategy =
(typeof VideoTranscodeStrategy)[keyof typeof VideoTranscodeStrategy]
export const VideoResolutionLadder = {
r480p: '480p',
r720p: '720p',
r1080p: '1080p',
r1440p: '1440p',
r2160p: '2160p',
} as const
export type VideoResolutionLadder =
(typeof VideoResolutionLadder)[keyof typeof VideoResolutionLadder]
export const DEFAULT_VIDEO_RESOLUTIONS: VideoResolutionLadder[] = [
'480p',
'720p',
'1080p',
'1440p',
'2160p',
]
export const HardwareAcceleration = {
off: 'off',
auto: 'auto',
@@ -96,6 +115,10 @@ export const updateTeamSettingsRequestSchema = z.union([
key: z.literal('transcode.videoStrategy'),
value: z.nativeEnum(VideoTranscodeStrategy),
}),
z.object({
key: z.literal('transcode.videoResolutions'),
value: z.array(z.nativeEnum(VideoResolutionLadder)),
}),
z.object({
key: z.literal('transcode.hardwareAcceleration'),
value: z.nativeEnum(HardwareAcceleration),
@@ -126,6 +149,7 @@ export interface TeamAppearanceSettings {
export interface TeamSettingsResponse {
transcode?: {
videoStrategy?: VideoTranscodeStrategy
videoResolutions?: VideoResolutionLadder[]
hardwareAcceleration?: HardwareAcceleration
threads?: number
hlsEnabled?: boolean
+7
View File
@@ -22,6 +22,13 @@ export class VideoTranscoder {
return this
}
setVideoResolutions(resolutions?: PrismaJson.VideoResolutionLadder[]): this {
if (resolutions) {
this.spec.videoResolutions = resolutions
}
return this
}
setHardwareAcceleration(hardwareAcceleration?: PrismaJson.HardwareAcceleration): this {
if (hardwareAcceleration) {
this.spec.hardwareAcceleration = hardwareAcceleration
@@ -43,10 +43,14 @@ describe('transcode-utils', () => {
expect(res).toEqual(['1080p', '720p', '480p'])
})
it('returns empty array when source is smaller than smallest ladder', () => {
// 640x360 source is smaller than 480p (854 long side)
it('falls back to lowest selected ladder when source is smaller than smallest ladder', () => {
// 640x360 source is smaller than 480p (854 long side) -> falls back to lowest selected (480p)
const res = getTargetHlsResolutions(undefined, 640, 360)
expect(res).toEqual([])
expect(res).toEqual(['480p'])
// When user selected 720p and 1440p, and source is 480p (854 long side) -> falls back to lowest selected (720p)
const resCustom = getTargetHlsResolutions(['720p', '1440p'], 854, 480)
expect(resCustom).toEqual(['720p'])
})
it('respects selective subset of configured ladders', () => {
@@ -79,9 +83,82 @@ describe('transcode-utils', () => {
})
describe('getTargetVideoResolutions', () => {
it('returns best match resolution for standard video', () => {
const resolutions = getTargetVideoResolutions('best_match', 1920, 1080)
expect(resolutions).toEqual(['180p', '1080p'])
describe('best_match strategy', () => {
it('returns best match resolution for 4K video', () => {
const resolutions = getTargetVideoResolutions('best_match', 3840, 2160)
expect(resolutions).toEqual(['180p', '2160p'])
})
it('returns best match resolution for 1080p video', () => {
const resolutions = getTargetVideoResolutions('best_match', 1920, 1080)
expect(resolutions).toEqual(['180p', '1080p'])
})
it('returns best match resolution for 720p video', () => {
const resolutions = getTargetVideoResolutions('best_match', 1280, 720)
expect(resolutions).toEqual(['180p', '720p'])
})
it('falls back to 480p for smaller videos (460p and 230p)', () => {
const res460 = getTargetVideoResolutions('best_match', 818, 460)
expect(res460).toEqual(['180p', '480p'])
const res230 = getTargetVideoResolutions('best_match', 408, 230)
expect(res230).toEqual(['180p', '480p'])
})
})
describe('multi strategy', () => {
it('returns all supported resolutions up to source for 4K video', () => {
const resolutions = getTargetVideoResolutions('multi', 3840, 2160)
expect(resolutions).toEqual(['180p', '2160p', '1440p', '1080p', '720p', '480p'])
})
it('returns resolutions up to 720p for 1280x720 video', () => {
const resolutions = getTargetVideoResolutions('multi', 1280, 720)
expect(resolutions).toEqual(['180p', '720p', '480p'])
})
it('returns resolutions up to 1080p for 2276x1280 video', () => {
const resolutions = getTargetVideoResolutions('multi', 2276, 1280)
expect(resolutions).toEqual(['180p', '1080p', '720p', '480p'])
})
it('falls back to lowest selected resolution (480p) for 460p and 230p videos', () => {
const res460 = getTargetVideoResolutions('multi', 818, 460)
expect(res460).toEqual(['180p', '480p'])
const res230 = getTargetVideoResolutions('multi', 408, 230)
expect(res230).toEqual(['180p', '480p'])
})
it('respects user-selected ladders and falls back to lowest selected ladder', () => {
const customLadders = ['720p', '1440p', '2160p'] as PrismaJson.VideoResolutionLadder[]
// 4K video: matches 2160p, 1440p, 720p (1080p was not selected)
const res4k = getTargetVideoResolutions('multi', 3840, 2160, customLadders)
expect(res4k).toEqual(['180p', '2160p', '1440p', '720p'])
// 230p video: nothing matches <= 408 -> falls back to lowest selected (720p)
const res230 = getTargetVideoResolutions('multi', 408, 230, customLadders)
expect(res230).toEqual(['180p', '720p'])
})
it('normalizes legacy "all" and "full" strategy values to multi', () => {
const resAll = getTargetVideoResolutions(
'all' as unknown as PrismaJson.VideoTranscodeStrategy,
1280,
720,
)
expect(resAll).toEqual(['180p', '720p', '480p'])
const resFull = getTargetVideoResolutions(
'full' as unknown as PrismaJson.VideoTranscodeStrategy,
1280,
720,
)
expect(resFull).toEqual(['180p', '720p', '480p'])
})
})
})
@@ -30,6 +30,35 @@ export const HLS_LADDER_ORDER: PrismaJson.HlsResolutionLadder[] = [
'480p',
]
export const VIDEO_LADDER_ORDER: PrismaJson.VideoResolutionLadder[] = [
'2160p',
'1440p',
'1080p',
'720p',
'480p',
]
export function matchResolutionsByLongSide<T extends string>(
selectedLadders: T[],
ladderOrder: T[],
rawLongSide: number,
): T[] {
const orderedSelected = ladderOrder.filter((ladder) => selectedLadders.includes(ladder))
if (orderedSelected.length === 0) return []
const matched = orderedSelected.filter((ladder) => {
const targetLongSide = RESOLUTION_LONG_SIDES[ladder]
return targetLongSide !== undefined && targetLongSide <= rawLongSide
})
if (matched.length === 0) {
// Falls back to the lowest resolution selected if nothing matched
return [orderedSelected[orderedSelected.length - 1]]
}
return matched
}
export function getTargetHlsResolutions(
configuredLadders: PrismaJson.HlsResolutionLadder[] | undefined,
originalWidth: number,
@@ -42,11 +71,7 @@ export function getTargetHlsResolutions(
const rawLongSide = Math.max(originalWidth, originalHeight)
return HLS_LADDER_ORDER.filter((ladder) => {
if (!selectedLadders.includes(ladder)) return false
const targetLongSide = RESOLUTION_LONG_SIDES[ladder]
return targetLongSide <= rawLongSide
})
return matchResolutionsByLongSide(selectedLadders, HLS_LADDER_ORDER, rawLongSide)
}
export function resolutionToDimensions(
@@ -74,31 +99,11 @@ export function resolutionToDimensions(
return [width, height]
}
const TARGET_RESOLUTIONS = [
{ name: '2160p', longSide: 3840 },
{ name: '1080p', longSide: 1920 },
{ name: '720p', longSide: 1280 },
{ name: '540p', longSide: 960 },
{ name: '360p', longSide: 640 },
]
function getBestMatchResolution(
originalWidth: number,
originalHeight: number,
): { name: string; longSide: number } {
const rawLongSide = Math.max(originalWidth, originalHeight)
const lower = TARGET_RESOLUTIONS.filter((r) => r.longSide <= rawLongSide)
if (lower.length > 0) {
lower.sort((a, b) => b.longSide - a.longSide)
return lower[0]
}
return TARGET_RESOLUTIONS[TARGET_RESOLUTIONS.length - 1]
}
export function getTargetVideoResolutions(
strategy: PrismaJson.VideoTranscodeStrategy,
originalWidth: number,
originalHeight: number,
configuredResolutions?: PrismaJson.VideoResolutionLadder[],
): string[] {
const resolutions = ['180p']
@@ -106,21 +111,27 @@ export function getTargetVideoResolutions(
const stratStr = strategy as string
if (stratStr === 'single' || stratStr === 'disable') {
normalizedStrategy = 'best_match'
} else if (stratStr === 'full') {
normalizedStrategy = 'all'
} else if (stratStr === 'full' || stratStr === 'all' || stratStr === 'multi') {
normalizedStrategy = 'multi'
}
const bestMatch = getBestMatchResolution(originalWidth, originalHeight)
const selectedLadders =
configuredResolutions && configuredResolutions.length > 0
? configuredResolutions
: (['480p', '720p', '1080p', '1440p', '2160p'] as PrismaJson.VideoResolutionLadder[])
const rawLongSide = Math.max(originalWidth, originalHeight)
if (normalizedStrategy === 'best_match') {
resolutions.push(bestMatch.name)
} else if (normalizedStrategy === 'all') {
const bestMatchIndex = TARGET_RESOLUTIONS.findIndex((r) => r.name === bestMatch.name)
if (bestMatchIndex !== -1) {
for (let i = bestMatchIndex; i < TARGET_RESOLUTIONS.length; i++) {
resolutions.push(TARGET_RESOLUTIONS[i].name)
}
}
const matched = matchResolutionsByLongSide(
['480p', '720p', '1080p', '1440p', '2160p'] as PrismaJson.VideoResolutionLadder[],
VIDEO_LADDER_ORDER,
rawLongSide,
)
resolutions.push(matched[0])
} else if (normalizedStrategy === 'multi') {
const matched = matchResolutionsByLongSide(selectedLadders, VIDEO_LADDER_ORDER, rawLongSide)
resolutions.push(...matched)
}
return resolutions
@@ -796,4 +796,82 @@ describe('transcodeVideoWorkflow', () => {
status: WorkflowTaskStatus.completed,
})
})
it('should transcode selected multi resolutions when videoStrategy is multi', () => {
const task: WorkflowTask = {
id: 'task-multi',
assetId: 'asset-multi',
type: WorkflowTaskType.transcode_video,
status: WorkflowTaskStatus.pending,
sessionId: null,
output: null,
payload: {
projectId: 'proj-1',
transcode: {
videoStrategy: 'multi',
videoResolutions: ['720p', '1080p', '2160p'],
},
},
createdAt: new Date(),
updatedAt: new Date(),
heartbeat: null,
teamId: 'team-1',
projectId: 'proj-1',
uid: 'task-uid',
model: null,
inputTokens: 0,
outputTokens: 0,
}
mockActivities.getAssetActivity.mockResolvedValue({
id: 'asset-multi',
storageKey: { key: 'files/asset-multi/video.mp4' },
mediaType: 'video/mp4',
})
mockActivities.getMediaInfoActivity.mockResolvedValue({
proxyType: 'video',
metadata: {
originalWidth: 1920,
originalHeight: 1080,
duration: 10,
frameRate: 30,
totalFrames: 300,
startTimecode: '00:00:00:00',
bitRate: 1000,
hasAudio: false,
format: {},
},
videoTranscodes: [],
imageTranscodes: [],
})
mockActivities.transcodeVideoActivity.mockImplementation(async (params) => ({
key: `files/asset-multi/${params.videoSpec.resolution}.mp4`,
width: params.videoSpec.width,
height: params.videoSpec.height,
resolution: params.videoSpec.resolution,
}))
return transcodeVideoWorkflow(task).then(() => {
// 180p preview + 1080p + 720p (2160p is skipped since > 1920)
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledTimes(3)
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
expect.objectContaining({
videoSpec: expect.objectContaining({ resolution: '180p' }),
}),
)
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
expect.objectContaining({
videoSpec: expect.objectContaining({ resolution: '1080p' }),
}),
)
expect(mockActivities.transcodeVideoActivity).toHaveBeenCalledWith(
expect.objectContaining({
videoSpec: expect.objectContaining({ resolution: '720p' }),
}),
)
})
})
})
@@ -129,6 +129,7 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
spec.videoStrategy || 'best_match',
metadata.originalWidth,
metadata.originalHeight,
spec.videoResolutions,
)
for (const res of videoResolutions) {
const [width, height] = resolutionToDimensions(
@@ -1,6 +1,6 @@
// @vitest-environment happy-dom
import React from 'react'
import { cleanup, render, screen } from '@testing-library/react'
import { cleanup, fireEvent, render, screen } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { BreadcrumbNav } from './breadcrumb-nav'
@@ -160,4 +160,107 @@ describe('BreadcrumbNav component', () => {
const triggerButton = screen.getByRole('button', { name: 'sample.mov' })
expect(triggerButton).toBeDefined()
})
it('displays 480p instead of 360p for 480p video transcode in download menu', () => {
render(
<QueryClientProvider client={queryClient}>
<BreadcrumbNav
{...baseProps}
fileId="file-1"
currentAsset={{ id: 'file-1', name: 'sample.mov', type: 'file', version: 1 }}
allowDownload={true}
downloadInfo={{
originalKey: 'raw.mov',
videoTranscodes: [
{
key: 'sample-480p.mp4',
width: 854,
height: 480,
hdr: false,
},
],
}}
/>
</QueryClientProvider>,
)
const triggerButton = screen.getByRole('button', { name: 'sample.mov' })
fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false)
})
it('displays explicit resolution when provided in download menu', () => {
render(
<QueryClientProvider client={queryClient}>
<BreadcrumbNav
{...baseProps}
fileId="file-1"
currentAsset={{ id: 'file-1', name: 'sample.mov', type: 'file', version: 1 }}
allowDownload={true}
downloadInfo={{
originalKey: 'raw.mov',
videoTranscodes: [
{
key: 'sample-480p.mp4',
width: 854,
height: 480,
resolution: '480p',
hdr: false,
},
],
}}
/>
</QueryClientProvider>,
)
const triggerButton = screen.getByRole('button', { name: 'sample.mov' })
fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false)
})
it('correctly labels legacy portrait video transcode in download menu using long side fallback', () => {
render(
<QueryClientProvider client={queryClient}>
<BreadcrumbNav
{...baseProps}
fileId="file-portrait"
currentAsset={{ id: 'file-portrait', name: 'portrait.mp4', type: 'file', version: 1 }}
allowDownload={true}
downloadInfo={{
originalKey: 'raw.mp4',
videoTranscodes: [
{
key: 'portrait-legacy.mp4',
width: 1080,
height: 1920,
hdr: false,
},
],
}}
/>
</QueryClientProvider>,
)
const triggerButton = screen.getByRole('button', { name: 'portrait.mp4' })
fireEvent.pointerDown(triggerButton, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('1080p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('1920p'))).toBe(false)
})
})
+3 -8
View File
@@ -48,6 +48,7 @@ import { useState } from 'react'
import { useDualSidebarStore } from '@/ui/stores/dual-sidebar'
import { ManageVersionsDialog } from './manage-versions-dialog'
import { AssetLinkedTasksDialog } from './kanban/asset-linked-tasks-dialog'
import { getVideoResolutionLabel } from '@/ui/lib/media'
interface BreadcrumbNavProps {
teamId: string
@@ -84,6 +85,7 @@ interface BreadcrumbNavProps {
key: string
width: number
height: number
resolution?: string
hdr?: boolean
}>
}
@@ -287,14 +289,7 @@ export function BreadcrumbNav({
<DropdownMenuSubContent className="w-48">
<DropdownMenuLabel>{m.download()}</DropdownMenuLabel>
{downloadInfo?.videoTranscodes?.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
const resolution = getVideoResolutionLabel(t)
const label = t.hdr ? `${resolution} (HDR)` : resolution
return (
<DropdownMenuItem
@@ -145,6 +145,7 @@ export function MobileFileDetail({
key: t.key,
width: t.width,
height: t.height,
resolution: t.resolution,
hdr: t.hdr,
})),
}
@@ -119,4 +119,69 @@ describe('MobileFileHeader', () => {
expect(menuBtn).toBeDefined()
fireEvent.click(menuBtn)
})
it('displays 480p instead of 360p for 480p video transcode in download menu', () => {
render(
<MobileFileHeader
fileName="scene1.mp4"
activeFileId="file-1"
allowDownload={true}
onBack={vi.fn()}
downloadInfo={{
originalKey: 'raw.mov',
videoTranscodes: [
{
key: 'scene1-480p.mp4',
width: 854,
height: 480,
resolution: '480p',
hdr: false,
},
],
}}
/>,
)
const menuBtn = screen.getByLabelText(/More options/i)
fireEvent.pointerDown(menuBtn, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('480p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('360p'))).toBe(false)
})
it('correctly labels legacy portrait video transcode in download menu using long side fallback', () => {
render(
<MobileFileHeader
fileName="portrait.mp4"
activeFileId="file-portrait"
allowDownload={true}
onBack={vi.fn()}
downloadInfo={{
originalKey: 'raw.mp4',
videoTranscodes: [
{
key: 'portrait-legacy.mp4',
width: 1080,
height: 1920,
hdr: false,
},
],
}}
/>,
)
const menuBtn = screen.getByLabelText(/More options/i)
fireEvent.pointerDown(menuBtn, { button: 0, ctrlKey: false })
const downloadSubTrigger = screen.getByText('Download')
fireEvent.click(downloadSubTrigger)
const items = screen.getAllByRole('menuitem')
expect(items.some((i) => i.textContent?.includes('1080p'))).toBe(true)
expect(items.some((i) => i.textContent?.includes('1920p'))).toBe(false)
})
})
@@ -26,6 +26,7 @@ import {
Trash2,
} from 'lucide-react'
import React from 'react'
import { getVideoResolutionLabel } from '@/ui/lib/media'
export interface MobileFileHeaderProps {
fileName: string
@@ -46,6 +47,7 @@ export interface MobileFileHeaderProps {
key: string
width: number
height: number
resolution?: string
hdr?: boolean
}>
}
@@ -238,14 +240,7 @@ export function MobileFileHeader({
<DropdownMenuSubContent className="w-48 z-50">
<DropdownMenuLabel className="text-xs">{m.download()}</DropdownMenuLabel>
{downloadInfo?.videoTranscodes?.map((t) => {
const longSide = Math.max(t.width, t.height)
let resolution = `${t.height}p`
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
const resolution = getVideoResolutionLabel(t)
const label = t.hdr ? `${resolution} (HDR)` : resolution
return (
<DropdownMenuItem
@@ -440,6 +440,7 @@ export function PublicShareManager({
key: t.key,
width: t.width,
height: t.height,
resolution: t.resolution,
hdr: t.hdr,
})),
}
@@ -214,4 +214,95 @@ describe('CompareVideoPane', () => {
vi.useRealTimers()
}
})
it('reports 2160p instead of 3840p for 4K vertical video when HLS switches level on Auto', () => {
mockHlsInstance.levels = [{ height: 3840, width: 2160, bitrate: 10000000 }]
const hlsVideo: AssetInfo = {
id: 'video-hls-compare-vertical-4k',
name: 'vertical-4k.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [{ width: 2160, height: 3840, resolution: '2160p' }],
},
metadata: {
originalWidth: 2160,
originalHeight: 3840,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const ref = createRef<ComparePaneHandle>()
const reportedStates: PaneReportedState[] = []
render(
<CompareVideoPane
ref={ref}
file={hlsVideo}
isActive={true}
annotations={[]}
onActivate={vi.fn()}
onStateChange={(s) => reportedStates.push(s)}
/>,
)
const levelSwitchedHandler = mockHlsInstance.on.mock.calls.find(
([event]) => event === 'hlsLevelSwitched',
)?.[1]
expect(levelSwitchedHandler).toBeDefined()
act(() => {
levelSwitchedHandler('hlsLevelSwitched', { level: 0 })
})
const lastState = reportedStates[reportedStates.length - 1]
expect(lastState?.video?.activeAutoResolution).toBe('2160p')
})
it('derives resolution from long side for legacy portrait MP4 transcode without explicit resolution', () => {
const legacyPortraitVideo: AssetInfo = {
id: 'legacy-portrait-compare-mp4',
name: 'portrait.mp4',
proxyType: 'video',
media: {
videoTranscodes: [
{
key: 'portrait-1080p.mp4',
url: 'https://cdn.example.com/portrait-1080p.mp4',
width: 1080,
height: 1920,
size: 5000000,
},
],
metadata: {
originalWidth: 1080,
originalHeight: 1920,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const reportedStates: PaneReportedState[] = []
render(
<CompareVideoPane
file={legacyPortraitVideo}
isActive={true}
annotations={[]}
onActivate={vi.fn()}
onStateChange={(s) => reportedStates.push(s)}
/>,
)
const lastState = reportedStates[reportedStates.length - 1]
expect(lastState?.video?.resolutions?.[0]?.resolution).toBe('1080p')
})
})
@@ -11,6 +11,7 @@ import { useFramePlayer } from './use-frame-player'
import { calculateFrameCenterTime, resolveTotalFrames } from './utils'
import { clampFrame as clampFrameUtil } from '../../compare/compare-utils'
import type { ComparePaneHandle, DisplayTranscode, PaneReportedState } from '../../compare/types'
import { getVideoResolutionLabel } from '@/ui/lib/media'
interface CompareVideoPaneProps {
file: AssetInfo
@@ -36,20 +37,10 @@ function computeResolutions(file: AssetInfo): DisplayTranscode[] {
? (file.media?.hls?.resolutions ?? [])
: (file.media?.videoTranscodes ?? [])
return baseTranscodes.map((t) => {
const longSide = Math.max(t.width, t.height)
const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined
let resolution = resCandidate || `${t.height}p`
if (!resCandidate) {
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
}
return { ...t, resolution }
})
return baseTranscodes.map((t) => ({
...t,
resolution: getVideoResolutionLabel(t),
}))
}
function getInitialResolution(resolutions: DisplayTranscode[]): DisplayTranscode | null {
@@ -216,7 +207,18 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
const level = hls?.levels[eventData.level]
if (level) {
setActiveAutoResolution(`${level.height}p`)
const matched = resolutions.find(
(r) => r.width === level.width && r.height === level.height,
)
if (matched?.resolution) {
setActiveAutoResolution(matched.resolution)
} else {
const shortSide =
level.width && level.height
? Math.min(level.width, level.height)
: level.height || level.width
setActiveAutoResolution(shortSide ? `${shortSide}p` : undefined)
}
}
})
@@ -617,7 +619,7 @@ export const CompareVideoPane = forwardRef<ComparePaneHandle, CompareVideoPanePr
],
)
const displayAnnotations = [...annotations, ...(isActive ? draftAnnotations : [])]
const displayAnnotations = [...(annotations ?? []), ...(isActive ? draftAnnotations : [])]
const scale = zoom
const defaultPanX = (containerSize.width - vidW * scale) / 2
@@ -29,6 +29,7 @@ import {
import { Slider } from '../../ui/slider'
import ProgressBar from './progress-bar'
import { formatTime, formatTimecode } from './utils'
import { getVideoResolutionLabel } from '@/ui/lib/media'
export interface PlayerState {
isPlaying: boolean
@@ -431,8 +432,7 @@ export const VideoControlBar: React.FC<ControlBarProps> = ({
? data.media.videoTranscodes
: resolutions
).map((res) => {
const resLabel =
'resolution' in res && res.resolution ? res.resolution : `${res.height}p`
const resLabel = getVideoResolutionLabel(res)
const itemKey = res.key || `${resLabel}-${res.hdr ? 'hdr' : 'sdr'}`
return (
<DropdownMenuItem
@@ -495,4 +495,75 @@ describe('VideoViewer', () => {
vi.useRealTimers()
}
})
it('displays 2160p instead of 3840p for 4K vertical video when HLS switches level on Auto', () => {
mockHlsInstance.levels = [{ height: 3840, width: 2160, bitrate: 10000000 }]
const hlsVideo: AssetInfo = {
id: 'video-hls-vertical-4k',
name: 'vertical-4k.m3u8',
proxyType: 'video',
media: {
isHls: true,
hls: {
key: 'hls-key',
url: 'https://cdn.example.com/master.m3u8',
resolutions: [{ width: 2160, height: 3840, resolution: '2160p' }],
},
metadata: {
originalWidth: 2160,
originalHeight: 3840,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const { container } = render(<VideoViewer file={hlsVideo} />)
const levelSwitchedHandler = mockHlsInstance.on.mock.calls.find(
([event]) => event === 'hlsLevelSwitched',
)?.[1]
expect(levelSwitchedHandler).toBeDefined()
act(() => {
levelSwitchedHandler('hlsLevelSwitched', { level: 0 })
})
const settingsButton = container.querySelector('button:has(.lucide-settings)')
expect(settingsButton?.textContent).toContain('2160p')
expect(settingsButton?.textContent).not.toContain('3840p')
})
it('derives resolution from long side for legacy portrait MP4 transcode without explicit resolution', () => {
const legacyPortraitVideo: AssetInfo = {
id: 'legacy-portrait-mp4',
name: 'portrait.mp4',
proxyType: 'video',
media: {
videoTranscodes: [
{
key: 'portrait-1080p.mp4',
url: 'https://cdn.example.com/portrait-1080p.mp4',
width: 1080,
height: 1920,
size: 5000000,
},
],
metadata: {
originalWidth: 1080,
originalHeight: 1920,
duration: 10,
frameRate: 30,
totalFrames: 300,
},
},
} as unknown as AssetInfo
const { container } = render(<VideoViewer file={legacyPortraitVideo} />)
const settingsButton = container.querySelector('button:has(.lucide-settings)')
expect(settingsButton?.textContent).toContain('1080p')
expect(settingsButton?.textContent).not.toContain('1920p')
})
})
@@ -10,6 +10,7 @@ import { MobileVideoControlBar } from './mobile-video-control-bar'
import { useIsMobile } from '@/ui/hooks/use-mobile'
import DrawingCanvas from '@/ui/components/drawing-canvas'
import { useAnnotationStore } from '@/ui/stores/annotation-store'
import { getVideoResolutionLabel } from '@/ui/lib/media'
import { FileViewerProps, MediaController } from '../types'
import { centeredPan, fitScale, zoomAtPoint } from '../pan-zoom'
import { usePanZoomGestures } from '../use-pan-zoom'
@@ -39,24 +40,10 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
? (data.media?.hls?.resolutions ?? [])
: (data.media?.videoTranscodes ?? [])
const resolutions: DisplayTranscode[] = baseTranscodes.map((t) => {
const longSide = Math.max(t.width, t.height)
const resCandidate = 'resolution' in t && t.resolution ? t.resolution : undefined
let resolution = resCandidate || `${t.height}p`
if (!resCandidate) {
if (longSide >= 3840) resolution = '2160p'
else if (longSide >= 1920) resolution = '1080p'
else if (longSide >= 1280) resolution = '720p'
else if (longSide >= 960) resolution = '540p'
else if (longSide >= 640) resolution = '360p'
else if (longSide >= 320) resolution = '180p'
}
return {
...t,
resolution,
}
})
const resolutions: DisplayTranscode[] = baseTranscodes.map((t) => ({
...t,
resolution: getVideoResolutionLabel(t),
}))
// Only transcoded proxy versions are ever displayed; the raw original file
// is never used as a playback source.
const hasMedia = (isHls || resolutions.length > 0) && !!data.media?.metadata
@@ -365,8 +352,18 @@ const VideoViewer = React.forwardRef<MediaController, FileViewerProps>(
hls.on(Hls.Events.LEVEL_SWITCHED, (_event, eventData) => {
const level = hls?.levels[eventData.level]
if (level) {
const h = level.height || Math.min(level.width, level.height)
setActiveAutoResolution(h ? `${h}p` : undefined)
const matched = resolutions.find(
(r) => r.width === level.width && r.height === level.height,
)
if (matched?.resolution) {
setActiveAutoResolution(matched.resolution)
} else {
const shortSide =
level.width && level.height
? Math.min(level.width, level.height)
: level.height || level.width
setActiveAutoResolution(shortSide ? `${shortSide}p` : undefined)
}
}
})
+46 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { isImageFileName, getBestTranscode } from './media'
import { isImageFileName, getBestTranscode, getVideoResolutionLabel } from './media'
describe('isImageFileName', () => {
it('identifies web-supported image formats as images', () => {
@@ -49,3 +49,48 @@ describe('getBestTranscode', () => {
expect(getBestTranscode(transcodes, 1500)?.width).toBe(1200)
})
})
describe('getVideoResolutionLabel', () => {
it('preserves explicit resolution when present', () => {
expect(getVideoResolutionLabel({ width: 854, height: 480, resolution: '480p' })).toBe('480p')
expect(getVideoResolutionLabel({ width: 480, height: 854, resolution: '480p' })).toBe('480p')
expect(getVideoResolutionLabel({ width: 1920, height: 1080, resolution: '1080p' })).toBe(
'1080p',
)
})
it('derives resolution from long side for portrait legacy transcodes without explicit resolution', () => {
// 4K portrait
expect(getVideoResolutionLabel({ width: 2160, height: 3840 })).toBe('2160p')
// 2K / 1440p portrait
expect(getVideoResolutionLabel({ width: 1440, height: 2560 })).toBe('1440p')
// 1080p portrait
expect(getVideoResolutionLabel({ width: 1080, height: 1920 })).toBe('1080p')
// 720p portrait
expect(getVideoResolutionLabel({ width: 720, height: 1280 })).toBe('720p')
// 480p portrait
expect(getVideoResolutionLabel({ width: 480, height: 854 })).toBe('480p')
// 360p portrait
expect(getVideoResolutionLabel({ width: 360, height: 640 })).toBe('360p')
// 180p portrait
expect(getVideoResolutionLabel({ width: 180, height: 320 })).toBe('180p')
})
it('derives resolution from long side for landscape legacy transcodes without explicit resolution', () => {
expect(getVideoResolutionLabel({ width: 3840, height: 2160 })).toBe('2160p')
expect(getVideoResolutionLabel({ width: 2560, height: 1440 })).toBe('1440p')
expect(getVideoResolutionLabel({ width: 1920, height: 1080 })).toBe('1080p')
expect(getVideoResolutionLabel({ width: 1280, height: 720 })).toBe('720p')
expect(getVideoResolutionLabel({ width: 854, height: 480 })).toBe('480p')
expect(getVideoResolutionLabel({ width: 640, height: 360 })).toBe('360p')
expect(getVideoResolutionLabel({ width: 320, height: 180 })).toBe('180p')
})
it('falls back to short side or single dimension for non-standard sizes', () => {
expect(getVideoResolutionLabel({ width: 200, height: 100 })).toBe('100p')
expect(getVideoResolutionLabel({ height: 720 })).toBe('720p')
expect(getVideoResolutionLabel({ width: 500 })).toBe('500p')
expect(getVideoResolutionLabel(null)).toBe('')
expect(getVideoResolutionLabel(undefined)).toBe('')
})
})
+40
View File
@@ -40,3 +40,43 @@ export function getBestTranscode(
// If no candidate >= screenWidth, simply return the largest available (first in original sorted)
return sorted[0]
}
export function getVideoResolutionLabel(
transcode?: {
width?: number
height?: number
resolution?: string
} | null,
): string {
if (!transcode) return ''
if (transcode.resolution) {
return transcode.resolution
}
const width = transcode.width ?? 0
const height = transcode.height ?? 0
if (width > 0 && height > 0) {
const longSide = Math.max(width, height)
if (longSide >= 3840) return '2160p'
if (longSide >= 2560) return '1440p'
if (longSide >= 1920) return '1080p'
if (longSide >= 1280) return '720p'
if (longSide >= 854) return '480p'
if (longSide >= 640) return '360p'
if (longSide >= 320) return '180p'
const shortSide = Math.min(width, height)
return `${shortSide}p`
}
if (height > 0) {
return `${height}p`
}
if (width > 0) {
return `${width}p`
}
return ''
}
+4
View File
@@ -254,6 +254,10 @@
"video_strategy": "Video Strategy",
"best_match": "Best match",
"best_match_description": "Generates a single optimal resolution matching the source quality.",
"multi_resolutions": "Multi resolutions",
"multi_resolutions_description": "Generates selected resolutions up to the source quality.",
"video_resolution_ladders": "Resolution Ladders",
"video_resolution_ladders_description": "Select the target resolutions to generate for MP4 transcoding. Resolutions are only generated up to the source video resolution.",
"all_resolutions": "All resolutions",
"all_resolutions_description": "Generates all supported resolutions up to the source quality.",
"hardware_acceleration": "Hardware Acceleration",
+4
View File
@@ -254,6 +254,10 @@
"video_strategy": "视频策略",
"best_match": "最佳匹配",
"best_match_description": "生成与源质量匹配的单一最优分辨率。",
"multi_resolutions": "多分辨率",
"multi_resolutions_description": "生成选中的不超过源质量的分辨率。",
"video_resolution_ladders": "分辨率阶梯",
"video_resolution_ladders_description": "选择 MP4 转码的目标分辨率。仅生成不超过原始视频分辨率的分辨率阶梯。",
"all_resolutions": "所有分辨率",
"all_resolutions_description": "生成不超过源质量的所有支持分辨率。",
"hardware_acceleration": "硬件加速",
@@ -300,6 +300,7 @@ function FileViewPage() {
key: t.key,
width: t.width,
height: t.height,
resolution: t.resolution,
hdr: t.hdr,
})),
},
@@ -1,6 +1,8 @@
import { createLazyFileRoute } from '@tanstack/react-router'
import {
VideoTranscodeStrategy,
VideoResolutionLadder,
DEFAULT_VIDEO_RESOLUTIONS,
HardwareAcceleration,
UpdateTeamSettingsRequest,
TeamSettingsResponse,
@@ -97,7 +99,7 @@ function getTabFromHash(): SettingsTab {
: 'general'
}
function TeamSettingsPage() {
export function TeamSettingsPage() {
const { teamId } = Route.useParams()
const queryClient = useQueryClient()
const [activeTab, setActiveTabState] = useState<SettingsTab>(getTabFromHash)
@@ -297,6 +299,10 @@ function TeamSettingsPage() {
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.success(m.settings_updated())
},
onError: () => {
toast.error(m.failed_update_settings())
},
})
@@ -310,6 +316,102 @@ function TeamSettingsPage() {
})
}
const pendingVideoResolutionsRef = useRef<VideoResolutionLadder[] | null>(null)
const [optimisticVideoResolutions, setOptimisticVideoResolutions] = useState<
VideoResolutionLadder[] | null
>(null)
const videoResolutionsQueueRef = useRef<Promise<void>>(Promise.resolve())
const isVideoResolutionsUpdatingRef = useRef<boolean>(false)
const pendingHlsResolutionsRef = useRef<HlsResolutionLadder[] | null>(null)
const [optimisticHlsResolutions, setOptimisticHlsResolutions] = useState<
HlsResolutionLadder[] | null
>(null)
const hlsResolutionsQueueRef = useRef<Promise<void>>(Promise.resolve())
const isHlsResolutionsUpdatingRef = useRef<boolean>(false)
useEffect(() => {
if (!isVideoResolutionsUpdatingRef.current) {
pendingVideoResolutionsRef.current = null
setOptimisticVideoResolutions(null)
}
}, [settings])
useEffect(() => {
if (!isHlsResolutionsUpdatingRef.current) {
pendingHlsResolutionsRef.current = null
setOptimisticHlsResolutions(null)
}
}, [settings])
const handleVideoLadderToggle = (ladder: VideoResolutionLadder) => {
const current =
pendingVideoResolutionsRef.current ??
optimisticVideoResolutions ??
(settings as TeamSettingsResponse | undefined)?.transcode?.videoResolutions ??
DEFAULT_VIDEO_RESOLUTIONS
const updated = current.includes(ladder)
? current.filter((l) => l !== ladder)
: [...current, ladder]
if (updated.length === 0) return
pendingVideoResolutionsRef.current = updated
setOptimisticVideoResolutions(updated)
queryClient.setQueryData(
['teams', teamId, 'settings'],
(old: TeamSettingsResponse | undefined) => {
if (!old) return old
return {
...old,
transcode: {
...old.transcode,
videoResolutions: updated,
},
}
},
)
isVideoResolutionsUpdatingRef.current = true
videoResolutionsQueueRef.current = videoResolutionsQueueRef.current
.then(async () => {
const target = pendingVideoResolutionsRef.current
if (!target) return
const res = await client.api.teams[':teamId'].settings.$patch({
param: { teamId },
json: {
key: 'transcode.videoResolutions',
value: target,
},
})
if (!res.ok) throw new Error('Failed to update settings')
await res.json()
if (pendingVideoResolutionsRef.current === target) {
pendingVideoResolutionsRef.current = null
isVideoResolutionsUpdatingRef.current = false
setOptimisticVideoResolutions(null)
await queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.success(m.settings_updated())
}
})
.catch((err) => {
console.error(err)
pendingVideoResolutionsRef.current = null
isVideoResolutionsUpdatingRef.current = false
setOptimisticVideoResolutions(null)
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.error(m.failed_update_settings())
})
}
const handleHardwareAccelerationChange = (value: HardwareAcceleration) => {
updateSettings({
teamId,
@@ -349,6 +451,8 @@ function TeamSettingsPage() {
const handleHlsLadderToggle = (ladder: HlsResolutionLadder) => {
const current =
pendingHlsResolutionsRef.current ??
optimisticHlsResolutions ??
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsResolutions ??
DEFAULT_HLS_RESOLUTIONS
const updated = current.includes(ladder)
@@ -357,13 +461,60 @@ function TeamSettingsPage() {
if (updated.length === 0) return
updateSettings({
teamId,
data: {
key: 'transcode.hlsResolutions',
value: updated,
pendingHlsResolutionsRef.current = updated
setOptimisticHlsResolutions(updated)
queryClient.setQueryData(
['teams', teamId, 'settings'],
(old: TeamSettingsResponse | undefined) => {
if (!old) return old
return {
...old,
transcode: {
...old.transcode,
hlsResolutions: updated,
},
}
},
})
)
isHlsResolutionsUpdatingRef.current = true
hlsResolutionsQueueRef.current = hlsResolutionsQueueRef.current
.then(async () => {
const target = pendingHlsResolutionsRef.current
if (!target) return
const res = await client.api.teams[':teamId'].settings.$patch({
param: { teamId },
json: {
key: 'transcode.hlsResolutions',
value: target,
},
})
if (!res.ok) throw new Error('Failed to update settings')
await res.json()
if (pendingHlsResolutionsRef.current === target) {
pendingHlsResolutionsRef.current = null
isHlsResolutionsUpdatingRef.current = false
setOptimisticHlsResolutions(null)
await queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.success(m.settings_updated())
}
})
.catch((err) => {
console.error(err)
pendingHlsResolutionsRef.current = null
isHlsResolutionsUpdatingRef.current = false
setOptimisticHlsResolutions(null)
queryClient.invalidateQueries({
queryKey: ['teams', teamId, 'settings'],
})
toast.error(m.failed_update_settings())
})
}
if (isSettingsLoading || isMeLoading) {
@@ -383,10 +534,20 @@ function TeamSettingsPage() {
(settings as any)?.transcode?.videoStrategy || VideoTranscodeStrategy.best_match
if (currentVideoStrategy === 'single' || currentVideoStrategy === 'disable') {
currentVideoStrategy = VideoTranscodeStrategy.best_match
} else if (currentVideoStrategy === 'full') {
currentVideoStrategy = VideoTranscodeStrategy.all
} else if (
currentVideoStrategy === 'full' ||
currentVideoStrategy === 'all' ||
currentVideoStrategy === 'multi'
) {
currentVideoStrategy = VideoTranscodeStrategy.multi
}
const currentVideoResolutions: VideoResolutionLadder[] =
optimisticVideoResolutions ??
(settings as TeamSettingsResponse | undefined)?.transcode?.videoResolutions ??
DEFAULT_VIDEO_RESOLUTIONS
const ALL_VIDEO_LADDERS: VideoResolutionLadder[] = ['480p', '720p', '1080p', '1440p', '2160p']
const currentHardwareAcceleration =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(settings as any)?.transcode?.hardwareAcceleration || HardwareAcceleration.off
@@ -395,6 +556,7 @@ function TeamSettingsPage() {
const currentHlsEnabled =
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsEnabled ?? false
const currentHlsResolutions: HlsResolutionLadder[] =
optimisticHlsResolutions ??
(settings as TeamSettingsResponse | undefined)?.transcode?.hlsResolutions ??
DEFAULT_HLS_RESOLUTIONS
const ALL_HLS_LADDERS: HlsResolutionLadder[] = ['480p', '720p', '1080p', '1440p', '2160p']
@@ -842,17 +1004,47 @@ function TeamSettingsPage() {
<div
className={cn(
'cursor-pointer rounded-lg border p-4 transition-all hover:border-primary',
currentVideoStrategy === VideoTranscodeStrategy.all
currentVideoStrategy === VideoTranscodeStrategy.multi
? 'border-primary bg-primary/5'
: 'border-border',
)}
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.all)}
onClick={() => handleVideoStrategyChange(VideoTranscodeStrategy.multi)}
>
<div className="font-semibold">{m.all_resolutions()}</div>
<div className="font-semibold">{m.multi_resolutions()}</div>
<div className="text-sm text-muted-foreground">
{m.all_resolutions_description()}
{m.multi_resolutions_description()}
</div>
</div>
{currentVideoStrategy === VideoTranscodeStrategy.multi && (
<div className="space-y-3 pt-2 pl-1">
<div className="space-y-1">
<h4 className="text-sm font-medium">
{m.video_resolution_ladders()}
</h4>
<p className="text-xs text-muted-foreground">
{m.video_resolution_ladders_description()}
</p>
</div>
<div className="flex flex-wrap gap-4 pt-1">
{ALL_VIDEO_LADDERS.map((ladder) => {
const checked = currentVideoResolutions.includes(ladder)
return (
<label
key={ladder}
className="flex items-center space-x-2 cursor-pointer text-sm"
>
<Checkbox
checked={checked}
onCheckedChange={() => handleVideoLadderToggle(ladder)}
/>
<span>{ladder}</span>
</label>
)
})}
</div>
</div>
)}
</div>
</div>
@@ -0,0 +1,241 @@
// @vitest-environment happy-dom
import { cleanup, render, screen, fireEvent, waitFor, act } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { TeamSettingsPage } from './settings.lazy'
import { toast } from 'sonner'
import { VideoTranscodeStrategy } from '@shumai/dtos'
interface PatchSettingsArgs {
param: { teamId: string }
json: { key: string; value: string[] }
}
const mockGetSettings = vi.fn()
const mockPatchSettings = vi.fn()
const mockGetMe = vi.fn()
vi.mock('@tanstack/react-router', () => ({
createLazyFileRoute: () => (opts: Record<string, unknown>) => ({
...opts,
useParams: () => ({ teamId: 'team-1' }),
}),
useNavigate: () => vi.fn(),
Link: ({ children, ...props }: React.AnchorHTMLAttributes<HTMLAnchorElement>) => (
<a {...props}>{children}</a>
),
}))
vi.mock('@/ui/api/client', () => ({
client: {
api: {
teams: {
':teamId': {
settings: {
$get: () => mockGetSettings(),
$patch: (args: unknown) => mockPatchSettings(args),
},
me: {
$get: () => mockGetMe(),
$patch: vi.fn().mockResolvedValue({ ok: true, json: async () => ({}) }),
},
},
},
},
},
}))
vi.mock('sonner', () => ({
toast: {
success: vi.fn(),
error: vi.fn(),
},
}))
describe('TeamSettingsPage Transcode Settings', () => {
let queryClient: QueryClient
beforeEach(() => {
vi.clearAllMocks()
window.location.hash = '#transcode'
queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
mockGetMe.mockResolvedValue({
ok: true,
json: async () => ({
id: 'user-1',
name: 'Owner',
role: 'owner',
}),
})
mockGetSettings.mockResolvedValue({
ok: true,
json: async () => ({
transcode: {
videoStrategy: VideoTranscodeStrategy.multi,
videoResolutions: ['480p', '720p', '1080p', '1440p', '2160p'],
hardwareAcceleration: 'off',
threads: 0,
hlsEnabled: true,
hlsResolutions: ['480p', '720p', '1080p', '1440p', '2160p'],
},
}),
})
})
afterEach(() => {
cleanup()
})
it('keeps pending selections optimistically and serializes consecutive toggles without submitting stale arrays', async () => {
let resolveFirstPatch!: (value: unknown) => void
const firstPatchPromise = new Promise((resolve) => {
resolveFirstPatch = resolve
})
const patchCalls: PatchSettingsArgs[] = []
mockPatchSettings.mockImplementation(async (args: PatchSettingsArgs) => {
patchCalls.push(args)
if (patchCalls.length === 1) {
await firstPatchPromise
}
return {
ok: true,
json: async () => ({
transcode: {
videoResolutions: args.json.value,
},
}),
}
})
render(
<QueryClientProvider client={queryClient}>
<TeamSettingsPage />
</QueryClientProvider>,
)
// Wait for settings to load
await waitFor(() => {
expect(screen.getAllByText('480p').length).toBeGreaterThan(0)
expect(screen.getAllByText('720p').length).toBeGreaterThan(0)
})
const label480p = screen.getAllByText('480p')[0].closest('label')
const checkbox480p = label480p?.querySelector('button[role="checkbox"]') as HTMLElement
expect(checkbox480p).toBeDefined()
expect(checkbox480p.getAttribute('aria-checked')).toBe('true')
const label720p = screen.getAllByText('720p')[0].closest('label')
const checkbox720p = label720p?.querySelector('button[role="checkbox"]') as HTMLElement
expect(checkbox720p).toBeDefined()
expect(checkbox720p.getAttribute('aria-checked')).toBe('true')
// Click 480p to uncheck it
act(() => {
fireEvent.click(checkbox480p)
})
// Optimistically 480p should be unchecked immediately
expect(checkbox480p.getAttribute('aria-checked')).toBe('false')
// Wait for the first PATCH request to be in-flight
await waitFor(() => {
expect(patchCalls.length).toBe(1)
})
expect(patchCalls[0].json.value).toEqual(['720p', '1080p', '1440p', '2160p'])
// Rapid consecutive click: Click 720p to uncheck it WHILE first patch is in-flight
act(() => {
fireEvent.click(checkbox720p)
})
// Optimistically 720p should also be unchecked immediately
expect(checkbox720p.getAttribute('aria-checked')).toBe('false')
// Resolve first patch so the queue advances to the second queued update
await act(async () => {
resolveFirstPatch({
ok: true,
json: async () => ({
transcode: {
videoResolutions: ['720p', '1080p', '1440p', '2160p'],
},
}),
})
})
// Wait for the second queued patch to complete
await waitFor(() => {
expect(patchCalls.length).toBe(2)
})
// The second patch MUST NOT submit stale array with 480p present!
// It must contain neither 480p nor 720p
expect(patchCalls[1].json.value).toEqual(['1080p', '1440p', '2160p'])
expect(patchCalls[1].json.value.includes('480p')).toBe(false)
expect(patchCalls[1].json.value.includes('720p')).toBe(false)
// Verify toast notification on success
expect(toast.success).toHaveBeenCalled()
})
it('coalesces rapid consecutive toggles before network dispatch into a single PATCH', async () => {
const patchCalls: PatchSettingsArgs[] = []
mockPatchSettings.mockImplementation(async (args: PatchSettingsArgs) => {
patchCalls.push(args)
return {
ok: true,
json: async () => ({
transcode: {
videoResolutions: args.json.value,
},
}),
}
})
render(
<QueryClientProvider client={queryClient}>
<TeamSettingsPage />
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getAllByText('480p').length).toBeGreaterThan(0)
})
const label480p = screen.getAllByText('480p')[0].closest('label')
const checkbox480p = label480p?.querySelector('button[role="checkbox"]') as HTMLElement
const label720p = screen.getAllByText('720p')[0].closest('label')
const checkbox720p = label720p?.querySelector('button[role="checkbox"]') as HTMLElement
// Rapid synchronous clicks in the same event tick
act(() => {
fireEvent.click(checkbox480p)
fireEvent.click(checkbox720p)
})
// Both reflect optimistically
expect(checkbox480p.getAttribute('aria-checked')).toBe('false')
expect(checkbox720p.getAttribute('aria-checked')).toBe('false')
// Wait for the coalesced PATCH to complete
await waitFor(() => {
expect(patchCalls.length).toBeGreaterThanOrEqual(1)
})
// The submitted payload must reflect both toggles
const lastPatch = patchCalls[patchCalls.length - 1]
expect(lastPatch.json.value).toEqual(['1080p', '1440p', '2160p'])
})
})
+1
View File
@@ -23,6 +23,7 @@ export interface TopNavProjectState {
key: string
width: number
height: number
resolution?: string
hdr?: boolean
}>
}