2023-04-21 07:55:10 -05:00
|
|
|
import {
|
|
|
|
LiveRTMPHLSTranscodingSuccess,
|
|
|
|
LiveRTMPHLSTranscodingUpdatePayload,
|
|
|
|
LiveVideoError,
|
|
|
|
RunnerJobLiveRTMPHLSTranscodingPayload,
|
|
|
|
RunnerJobLiveRTMPHLSTranscodingPrivatePayload,
|
2023-07-31 07:34:36 -05:00
|
|
|
RunnerJobStateType
|
|
|
|
} from '@peertube/peertube-models'
|
|
|
|
import { buildUUID } from '@peertube/peertube-node-utils'
|
|
|
|
import { logger } from '@server/helpers/logger.js'
|
|
|
|
import { JOB_PRIORITY } from '@server/initializers/constants.js'
|
|
|
|
import { LiveManager } from '@server/lib/live/index.js'
|
|
|
|
import { MStreamingPlaylist, MVideo } from '@server/types/models/index.js'
|
|
|
|
import { MRunnerJob } from '@server/types/models/runners/index.js'
|
2023-10-05 04:09:11 -05:00
|
|
|
import { remove } from 'fs-extra/esm'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { join } from 'path'
|
|
|
|
import { AbstractJobHandler } from './abstract-job-handler.js'
|
2023-10-05 04:09:11 -05:00
|
|
|
import { tryAtomicMove } from '@server/helpers/fs.js'
|
2023-04-21 07:55:10 -05:00
|
|
|
|
|
|
|
type CreateOptions = {
|
|
|
|
video: MVideo
|
|
|
|
playlist: MStreamingPlaylist
|
|
|
|
|
2023-05-22 06:44:22 -05:00
|
|
|
sessionId: string
|
2023-04-21 07:55:10 -05:00
|
|
|
rtmpUrl: string
|
|
|
|
|
|
|
|
toTranscode: {
|
|
|
|
resolution: number
|
|
|
|
fps: number
|
|
|
|
}[]
|
|
|
|
|
|
|
|
segmentListSize: number
|
|
|
|
segmentDuration: number
|
|
|
|
|
|
|
|
outputDirectory: string
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
export class LiveRTMPHLSTranscodingJobHandler extends AbstractJobHandler<CreateOptions, LiveRTMPHLSTranscodingUpdatePayload, LiveRTMPHLSTranscodingSuccess> {
|
|
|
|
|
|
|
|
async create (options: CreateOptions) {
|
2023-05-22 06:44:22 -05:00
|
|
|
const { video, rtmpUrl, toTranscode, playlist, segmentDuration, segmentListSize, outputDirectory, sessionId } = options
|
2023-04-21 07:55:10 -05:00
|
|
|
|
|
|
|
const jobUUID = buildUUID()
|
|
|
|
const payload: RunnerJobLiveRTMPHLSTranscodingPayload = {
|
|
|
|
input: {
|
|
|
|
rtmpUrl
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
toTranscode,
|
|
|
|
segmentListSize,
|
|
|
|
segmentDuration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const privatePayload: RunnerJobLiveRTMPHLSTranscodingPrivatePayload = {
|
|
|
|
videoUUID: video.uuid,
|
|
|
|
masterPlaylistName: playlist.playlistFilename,
|
2023-05-22 06:44:22 -05:00
|
|
|
sessionId,
|
2023-04-21 07:55:10 -05:00
|
|
|
outputDirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
const job = await this.createRunnerJob({
|
|
|
|
type: 'live-rtmp-hls-transcoding',
|
|
|
|
jobUUID,
|
|
|
|
payload,
|
|
|
|
privatePayload,
|
|
|
|
priority: JOB_PRIORITY.TRANSCODING
|
|
|
|
})
|
|
|
|
|
|
|
|
return job
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2023-05-04 08:29:34 -05:00
|
|
|
protected async specificUpdate (options: {
|
2023-04-21 07:55:10 -05:00
|
|
|
runnerJob: MRunnerJob
|
|
|
|
updatePayload: LiveRTMPHLSTranscodingUpdatePayload
|
|
|
|
}) {
|
|
|
|
const { runnerJob, updatePayload } = options
|
|
|
|
|
|
|
|
const privatePayload = runnerJob.privatePayload as RunnerJobLiveRTMPHLSTranscodingPrivatePayload
|
|
|
|
const outputDirectory = privatePayload.outputDirectory
|
|
|
|
const videoUUID = privatePayload.videoUUID
|
|
|
|
|
2023-05-10 04:16:05 -05:00
|
|
|
// Always process the chunk first before moving m3u8 that references this chunk
|
2023-04-21 07:55:10 -05:00
|
|
|
if (updatePayload.type === 'add-chunk') {
|
2023-10-05 04:09:11 -05:00
|
|
|
await tryAtomicMove(
|
2023-04-21 07:55:10 -05:00
|
|
|
updatePayload.videoChunkFile as string,
|
2023-10-05 04:09:11 -05:00
|
|
|
join(outputDirectory, updatePayload.videoChunkFilename)
|
2023-04-21 07:55:10 -05:00
|
|
|
)
|
|
|
|
} else if (updatePayload.type === 'remove-chunk') {
|
|
|
|
await remove(join(outputDirectory, updatePayload.videoChunkFilename))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updatePayload.resolutionPlaylistFile && updatePayload.resolutionPlaylistFilename) {
|
2023-10-05 04:09:11 -05:00
|
|
|
await tryAtomicMove(
|
2023-04-21 07:55:10 -05:00
|
|
|
updatePayload.resolutionPlaylistFile as string,
|
2023-10-05 04:09:11 -05:00
|
|
|
join(outputDirectory, updatePayload.resolutionPlaylistFilename)
|
2023-04-21 07:55:10 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updatePayload.masterPlaylistFile) {
|
2023-10-05 04:09:11 -05:00
|
|
|
await tryAtomicMove(updatePayload.masterPlaylistFile as string, join(outputDirectory, privatePayload.masterPlaylistName))
|
2023-04-21 07:55:10 -05:00
|
|
|
}
|
|
|
|
|
2023-05-19 06:46:40 -05:00
|
|
|
logger.debug(
|
2023-04-21 07:55:10 -05:00
|
|
|
'Runner live RTMP to HLS job %s for %s updated.',
|
|
|
|
runnerJob.uuid, videoUUID, { updatePayload, ...this.lTags(videoUUID, runnerJob.uuid) }
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
protected specificComplete (options: {
|
|
|
|
runnerJob: MRunnerJob
|
|
|
|
}) {
|
|
|
|
return this.stopLive({
|
|
|
|
runnerJob: options.runnerJob,
|
|
|
|
type: 'ended'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
protected isAbortSupported () {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
protected specificAbort () {
|
|
|
|
throw new Error('Not implemented')
|
|
|
|
}
|
|
|
|
|
|
|
|
protected specificError (options: {
|
|
|
|
runnerJob: MRunnerJob
|
2023-07-31 07:34:36 -05:00
|
|
|
nextState: RunnerJobStateType
|
2023-04-21 07:55:10 -05:00
|
|
|
}) {
|
|
|
|
return this.stopLive({
|
|
|
|
runnerJob: options.runnerJob,
|
|
|
|
type: 'errored'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
protected specificCancel (options: {
|
|
|
|
runnerJob: MRunnerJob
|
|
|
|
}) {
|
|
|
|
return this.stopLive({
|
|
|
|
runnerJob: options.runnerJob,
|
|
|
|
type: 'cancelled'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private stopLive (options: {
|
|
|
|
runnerJob: MRunnerJob
|
|
|
|
type: 'ended' | 'errored' | 'cancelled'
|
|
|
|
}) {
|
|
|
|
const { runnerJob, type } = options
|
|
|
|
|
|
|
|
const privatePayload = runnerJob.privatePayload as RunnerJobLiveRTMPHLSTranscodingPrivatePayload
|
|
|
|
const videoUUID = privatePayload.videoUUID
|
|
|
|
|
|
|
|
const errorType = {
|
|
|
|
ended: null,
|
|
|
|
errored: LiveVideoError.RUNNER_JOB_ERROR,
|
|
|
|
cancelled: LiveVideoError.RUNNER_JOB_CANCEL
|
|
|
|
}
|
|
|
|
|
|
|
|
LiveManager.Instance.stopSessionOf(privatePayload.videoUUID, errorType[type])
|
|
|
|
|
|
|
|
logger.info('Runner live RTMP to HLS job %s for video %s %s.', runnerJob.uuid, videoUUID, type, this.lTags(runnerJob.uuid, videoUUID))
|
|
|
|
}
|
|
|
|
}
|