mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-24 03:47:58 +00:00
feat(transcode): add HLS live streaming support (#436)
* feat(transcode): add HLS live streaming support * fix(hls): resolve version stacks for public HLS and add workflow E2E test
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
getTargetHlsResolutions,
|
||||
resolutionToDimensions,
|
||||
getTargetVideoResolutions,
|
||||
isMimePsd,
|
||||
} from './transcode-utils'
|
||||
|
||||
describe('transcode-utils', () => {
|
||||
describe('getTargetHlsResolutions', () => {
|
||||
it('uses default ladders when configuredLadders is undefined or empty', () => {
|
||||
// 1080p source video (1920x1080)
|
||||
const res1 = getTargetHlsResolutions(undefined, 1920, 1080)
|
||||
expect(res1).toEqual(['1080p', '720p', '480p'])
|
||||
|
||||
const res2 = getTargetHlsResolutions([], 1920, 1080)
|
||||
expect(res2).toEqual(['1080p', '720p', '480p'])
|
||||
})
|
||||
|
||||
it('caps ladders up to source resolution (no upscaling)', () => {
|
||||
// 1080p source with 4K configured ladders -> should cap to 1080p
|
||||
const res = getTargetHlsResolutions(['480p', '720p', '1080p', '1440p', '2160p'], 1920, 1080)
|
||||
expect(res).toEqual(['1080p', '720p', '480p'])
|
||||
})
|
||||
|
||||
it('includes higher ladders when source resolution supports them', () => {
|
||||
// 4K source (3840x2160)
|
||||
const res = getTargetHlsResolutions(['480p', '720p', '1080p', '1440p', '2160p'], 3840, 2160)
|
||||
expect(res).toEqual(['2160p', '1440p', '1080p', '720p', '480p'])
|
||||
|
||||
// 1440p source (2560x1440)
|
||||
const res1440 = getTargetHlsResolutions(
|
||||
['480p', '720p', '1080p', '1440p', '2160p'],
|
||||
2560,
|
||||
1440,
|
||||
)
|
||||
expect(res1440).toEqual(['1440p', '1080p', '720p', '480p'])
|
||||
})
|
||||
|
||||
it('handles vertical videos correctly by using the longest side', () => {
|
||||
// 1080x1920 (vertical 1080p)
|
||||
const res = getTargetHlsResolutions(['480p', '720p', '1080p', '1440p', '2160p'], 1080, 1920)
|
||||
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)
|
||||
const res = getTargetHlsResolutions(undefined, 640, 360)
|
||||
expect(res).toEqual([])
|
||||
})
|
||||
|
||||
it('respects selective subset of configured ladders', () => {
|
||||
// User only selected 720p and 1440p
|
||||
const res = getTargetHlsResolutions(['720p', '1440p'], 2560, 1440)
|
||||
expect(res).toEqual(['1440p', '720p'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolutionToDimensions', () => {
|
||||
it('calculates dimensions correctly for 1440p and 480p', () => {
|
||||
const [w1440, h1440] = resolutionToDimensions('1440p', 3840, 2160)
|
||||
expect(w1440).toBe(2560)
|
||||
expect(h1440).toBe(1440)
|
||||
|
||||
const [w480, h480] = resolutionToDimensions('480p', 1920, 1080)
|
||||
expect(w480).toBe(854)
|
||||
expect(h480).toBe(480)
|
||||
})
|
||||
|
||||
it('ensures even dimensions for odd calculations', () => {
|
||||
const [w, h] = resolutionToDimensions('720p', 1921, 1081)
|
||||
expect(w % 2).toBe(0)
|
||||
expect(h % 2).toBe(0)
|
||||
})
|
||||
|
||||
it('returns [0, 0] for unknown resolution', () => {
|
||||
expect(resolutionToDimensions('invalid', 1920, 1080)).toEqual([0, 0])
|
||||
})
|
||||
})
|
||||
|
||||
describe('getTargetVideoResolutions', () => {
|
||||
it('returns best match resolution for standard video', () => {
|
||||
const resolutions = getTargetVideoResolutions('best_match', 1920, 1080)
|
||||
expect(resolutions).toEqual(['180p', '1080p'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('isMimePsd', () => {
|
||||
it('identifies PSD mime types', () => {
|
||||
expect(isMimePsd('image/vnd.adobe.photoshop')).toBe(true)
|
||||
expect(isMimePsd('image/x-photoshop')).toBe(true)
|
||||
expect(isMimePsd('image/psd')).toBe(true)
|
||||
expect(isMimePsd('image/png')).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -13,13 +13,42 @@ export function isMimePsd(mimeType: string): boolean {
|
||||
|
||||
const RESOLUTION_LONG_SIDES: Record<string, number> = {
|
||||
'2160p': 3840,
|
||||
'1440p': 2560,
|
||||
'1080p': 1920,
|
||||
'720p': 1280,
|
||||
'540p': 960,
|
||||
'480p': 854,
|
||||
'360p': 640,
|
||||
'180p': 320,
|
||||
}
|
||||
|
||||
export const HLS_LADDER_ORDER: PrismaJson.HlsResolutionLadder[] = [
|
||||
'2160p',
|
||||
'1440p',
|
||||
'1080p',
|
||||
'720p',
|
||||
'480p',
|
||||
]
|
||||
|
||||
export function getTargetHlsResolutions(
|
||||
configuredLadders: PrismaJson.HlsResolutionLadder[] | undefined,
|
||||
originalWidth: number,
|
||||
originalHeight: number,
|
||||
): PrismaJson.HlsResolutionLadder[] {
|
||||
const selectedLadders =
|
||||
configuredLadders && configuredLadders.length > 0
|
||||
? configuredLadders
|
||||
: (['480p', '720p', '1080p'] as PrismaJson.HlsResolutionLadder[])
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
export function resolutionToDimensions(
|
||||
resolution: string,
|
||||
originalWidth: number,
|
||||
|
||||
@@ -25,6 +25,7 @@ describe('transcodeVideoWorkflow', () => {
|
||||
getAssetActivity: Object.assign(vi.fn(), { _activityName: 'getAssetActivity' }),
|
||||
getMediaInfoActivity: Object.assign(vi.fn(), { _activityName: 'getMediaInfoActivity' }),
|
||||
transcodeVideoActivity: Object.assign(vi.fn(), { _activityName: 'transcodeVideoActivity' }),
|
||||
transcodeHlsActivity: Object.assign(vi.fn(), { _activityName: 'transcodeHlsActivity' }),
|
||||
transcodeAudioActivity: Object.assign(vi.fn(), { _activityName: 'transcodeAudioActivity' }),
|
||||
transcodeImageActivity: Object.assign(vi.fn(), { _activityName: 'transcodeImageActivity' }),
|
||||
generateSpriteActivity: Object.assign(vi.fn(), { _activityName: 'generateSpriteActivity' }),
|
||||
@@ -702,4 +703,97 @@ describe('transcodeVideoWorkflow', () => {
|
||||
status: WorkflowTaskStatus.completed,
|
||||
})
|
||||
})
|
||||
|
||||
it('should trigger HLS transcoding when hlsEnabled is true', async () => {
|
||||
const task: WorkflowTask = {
|
||||
id: 'task-hls',
|
||||
assetId: 'asset-hls',
|
||||
type: WorkflowTaskType.transcode_video,
|
||||
status: WorkflowTaskStatus.pending,
|
||||
sessionId: null,
|
||||
output: null,
|
||||
payload: {
|
||||
projectId: 'proj-1',
|
||||
transcode: {
|
||||
videoStrategy: 'best_match',
|
||||
hlsEnabled: true,
|
||||
hlsResolutions: ['480p', '720p', '1080p'],
|
||||
},
|
||||
},
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
heartbeat: null,
|
||||
teamId: 'team-1',
|
||||
projectId: 'proj-1',
|
||||
uid: 'task-uid-hls',
|
||||
model: null,
|
||||
inputTokens: 0,
|
||||
outputTokens: 0,
|
||||
}
|
||||
|
||||
mockActivities.getAssetActivity.mockResolvedValue({
|
||||
id: 'asset-hls',
|
||||
storageKey: { key: 'files/asset-hls/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,
|
||||
videoBitRate: 850000,
|
||||
hasAudio: false,
|
||||
format: {},
|
||||
},
|
||||
videoTranscodes: [],
|
||||
imageTranscodes: [],
|
||||
})
|
||||
|
||||
mockActivities.transcodeVideoActivity.mockResolvedValue({
|
||||
key: 'files/asset-hls/video-1080p.mp4',
|
||||
width: 1920,
|
||||
height: 1080,
|
||||
resolution: '1080p',
|
||||
})
|
||||
|
||||
mockActivities.transcodeHlsActivity.mockResolvedValue({
|
||||
key: 'files/asset-hls/hls/master.m3u8',
|
||||
resolutions: ['1080p', '720p', '480p'],
|
||||
})
|
||||
|
||||
await transcodeVideoWorkflow(task)
|
||||
|
||||
expect(mockActivities.transcodeHlsActivity).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
taskId: 'task-hls',
|
||||
assetKey: 'files/asset-hls/video.mp4',
|
||||
filePath: '/tmp/video.mp4',
|
||||
ladders: ['1080p', '720p', '480p'],
|
||||
originalWidth: 1920,
|
||||
originalHeight: 1080,
|
||||
}),
|
||||
)
|
||||
|
||||
expect(mockActivities.updateAssetMediaActivity).toHaveBeenCalledWith({
|
||||
assetId: 'asset-hls',
|
||||
mediaInfo: expect.objectContaining({
|
||||
isHls: true,
|
||||
hls: {
|
||||
key: 'files/asset-hls/hls/master.m3u8',
|
||||
resolutions: ['1080p', '720p', '480p'],
|
||||
},
|
||||
}),
|
||||
})
|
||||
|
||||
expect(mockActivities.updateTaskStatusActivity).toHaveBeenCalledWith({
|
||||
taskId: 'task-hls',
|
||||
status: WorkflowTaskStatus.completed,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,7 +8,11 @@ import {
|
||||
failTask,
|
||||
cleanupTmpDir,
|
||||
} from './common'
|
||||
import { getTargetVideoResolutions, resolutionToDimensions } from './transcode-utils'
|
||||
import {
|
||||
getTargetVideoResolutions,
|
||||
getTargetHlsResolutions,
|
||||
resolutionToDimensions,
|
||||
} from './transcode-utils'
|
||||
|
||||
export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void> {
|
||||
let tmpDir: string | undefined
|
||||
@@ -21,6 +25,7 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
|
||||
updateAssetStatusActivity,
|
||||
getMediaInfoActivity,
|
||||
transcodeVideoActivity,
|
||||
transcodeHlsActivity,
|
||||
transcodeAudioActivity,
|
||||
transcodeImageActivity,
|
||||
extractPosterActivity,
|
||||
@@ -189,6 +194,35 @@ export async function transcodeVideoWorkflow(task: WorkflowTask): Promise<void>
|
||||
|
||||
mediaInfo.videoTranscodes.push(videoTranscode)
|
||||
}
|
||||
|
||||
if (spec.hlsEnabled) {
|
||||
const hlsLadders = getTargetHlsResolutions(
|
||||
spec.hlsResolutions,
|
||||
metadata.originalWidth,
|
||||
metadata.originalHeight,
|
||||
)
|
||||
if (hlsLadders.length > 0) {
|
||||
const hlsResult = await executeActivity(workerQueue, transcodeHlsActivity, {
|
||||
taskId: task.id,
|
||||
assetKey: key,
|
||||
filePath,
|
||||
ladders: hlsLadders,
|
||||
originalWidth: metadata.originalWidth,
|
||||
originalHeight: metadata.originalHeight,
|
||||
originalFps: metadata.frameRate,
|
||||
sourceVideoBitrate: metadata.videoBitRate || metadata.bitRate,
|
||||
hardwareAcceleration: spec.hardwareAcceleration,
|
||||
threads: spec.threads,
|
||||
sourceIsHdr: metadata.isHdr,
|
||||
sourceHdrType: metadata.hdrType,
|
||||
sourceColorTransfer: metadata.colorTransfer,
|
||||
sourceColorPrimaries: metadata.colorPrimaries,
|
||||
sourceColorSpace: metadata.colorSpace,
|
||||
})
|
||||
mediaInfo.isHls = true
|
||||
mediaInfo.hls = hlsResult
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const isAudio = mediaInfo.proxyType === 'audio'
|
||||
|
||||
Reference in New Issue
Block a user