1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
import { move, remove } from 'fs-extra'
import { join } from 'path'
import { logger } from '@server/helpers/logger'
import { JOB_PRIORITY } from '@server/initializers/constants'
import { LiveManager } from '@server/lib/live'
import { MStreamingPlaylist, MVideo } from '@server/types/models'
import { MRunnerJob } from '@server/types/models/runners'
import { buildUUID } from '@shared/extra-utils'
import {
LiveRTMPHLSTranscodingSuccess,
LiveRTMPHLSTranscodingUpdatePayload,
LiveVideoError,
RunnerJobLiveRTMPHLSTranscodingPayload,
RunnerJobLiveRTMPHLSTranscodingPrivatePayload,
RunnerJobState
} from '@shared/models'
import { AbstractJobHandler } from './abstract-job-handler'
type CreateOptions = {
video: MVideo
playlist: MStreamingPlaylist
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) {
const { video, rtmpUrl, toTranscode, playlist, segmentDuration, segmentListSize, outputDirectory } = options
const jobUUID = buildUUID()
const payload: RunnerJobLiveRTMPHLSTranscodingPayload = {
input: {
rtmpUrl
},
output: {
toTranscode,
segmentListSize,
segmentDuration
}
}
const privatePayload: RunnerJobLiveRTMPHLSTranscodingPrivatePayload = {
videoUUID: video.uuid,
masterPlaylistName: playlist.playlistFilename,
outputDirectory
}
const job = await this.createRunnerJob({
type: 'live-rtmp-hls-transcoding',
jobUUID,
payload,
privatePayload,
priority: JOB_PRIORITY.TRANSCODING
})
return job
}
// ---------------------------------------------------------------------------
protected async specificUpdate (options: {
runnerJob: MRunnerJob
updatePayload: LiveRTMPHLSTranscodingUpdatePayload
}) {
const { runnerJob, updatePayload } = options
const privatePayload = runnerJob.privatePayload as RunnerJobLiveRTMPHLSTranscodingPrivatePayload
const outputDirectory = privatePayload.outputDirectory
const videoUUID = privatePayload.videoUUID
// Always process the chunk first before moving m3u8 that references this chunk
if (updatePayload.type === 'add-chunk') {
await move(
updatePayload.videoChunkFile as string,
join(outputDirectory, updatePayload.videoChunkFilename),
{ overwrite: true }
)
} else if (updatePayload.type === 'remove-chunk') {
await remove(join(outputDirectory, updatePayload.videoChunkFilename))
}
if (updatePayload.resolutionPlaylistFile && updatePayload.resolutionPlaylistFilename) {
await move(
updatePayload.resolutionPlaylistFile as string,
join(outputDirectory, updatePayload.resolutionPlaylistFilename),
{ overwrite: true }
)
}
if (updatePayload.masterPlaylistFile) {
await move(updatePayload.masterPlaylistFile as string, join(outputDirectory, privatePayload.masterPlaylistName), { overwrite: true })
}
logger.debug(
'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
nextState: RunnerJobState
}) {
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))
}
}
|