]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-live-ending.ts
Fix broken replay with long live video name
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
CommitLineData
5a921e7b 1import { Job } from 'bullmq'
5333788c 2import { readdir, remove } from 'fs-extra'
a5cf76af 3import { join } from 'path'
4ec52d04
C
4import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
5import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
cfd57d2c 6import { cleanupAndDestroyPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live'
92083e42 7import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '@server/lib/paths'
daf6e480 8import { generateVideoMiniature } from '@server/lib/thumbnail'
0c9668f7
C
9import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/hls-transcoding'
10import { VideoPathManager } from '@server/lib/video-path-manager'
0305db28 11import { moveToNextState } from '@server/lib/video-state'
a5cf76af 12import { VideoModel } from '@server/models/video/video'
26e3e98f 13import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
68e70a74 14import { VideoFileModel } from '@server/models/video/video-file'
b5b68755 15import { VideoLiveModel } from '@server/models/video/video-live'
0c9668f7 16import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting'
26e3e98f 17import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
a5cf76af 18import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
26e3e98f 19import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models'
0c9668f7 20import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '@shared/ffmpeg'
053aed43 21import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
53023be3 22import { logger, loggerTagsFactory } from '../../../helpers/logger'
ae22c59f
C
23import { peertubeTruncate } from '@server/helpers/core-utils'
24import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
53023be3
C
25
26const lTags = loggerTagsFactory('live', 'job')
a5cf76af 27
41fb13c3 28async function processVideoLiveEnding (job: Job) {
a5cf76af
C
29 const payload = job.data as VideoLiveEndingPayload
30
53023be3 31 logger.info('Processing video live ending for %s.', payload.videoId, { payload, ...lTags() })
4ec52d04 32
68e70a74 33 function logError () {
53023be3 34 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId, lTags())
68e70a74
C
35 }
36
c8fa571f 37 const video = await VideoModel.load(payload.videoId)
b5b68755 38 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
26e3e98f 39 const liveSession = await VideoLiveSessionModel.load(payload.liveSessionId)
b5b68755 40
c8fa571f 41 if (!video || !live || !liveSession) {
68e70a74
C
42 logError()
43 return
44 }
45
e37ca6cb
C
46 const permanentLive = live.permanentLive
47
c8fa571f
C
48 liveSession.endingProcessed = true
49 await liveSession.save()
50
51 if (liveSession.saveReplay !== true) {
52 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
b5b68755
C
53 }
54
c8fa571f
C
55 if (permanentLive) {
56 await saveReplayToExternalVideo({
57 liveVideo: video,
58 liveSession,
59 publishedAt: payload.publishedAt,
60 replayDirectory: payload.replayDirectory
61 })
4ec52d04 62
c8fa571f 63 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
4ec52d04
C
64 }
65
05a60d85
W
66 return replaceLiveByReplay({
67 video,
68 liveSession,
69 live,
70 permanentLive,
71 replayDirectory: payload.replayDirectory
72 })
b5b68755
C
73}
74
75// ---------------------------------------------------------------------------
76
77export {
8ebf2a5d 78 processVideoLiveEnding
b5b68755
C
79}
80
81// ---------------------------------------------------------------------------
82
26e3e98f
C
83async function saveReplayToExternalVideo (options: {
84 liveVideo: MVideo
85 liveSession: MVideoLiveSession
86 publishedAt: string
87 replayDirectory: string
88}) {
89 const { liveVideo, liveSession, publishedAt, replayDirectory } = options
90
05a60d85
W
91 const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId)
92
ae22c59f
C
93 const videoNameSuffix = ` - ${new Date(publishedAt).toLocaleString()}`
94 const truncatedVideoName = peertubeTruncate(liveVideo.name, {
95 length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max - videoNameSuffix.length
96 })
97
c8fa571f 98 const replayVideo = new VideoModel({
ae22c59f 99 name: truncatedVideoName + videoNameSuffix,
4ec52d04
C
100 isLive: false,
101 state: VideoState.TO_TRANSCODE,
102 duration: 0,
103
104 remote: liveVideo.remote,
105 category: liveVideo.category,
106 licence: liveVideo.licence,
107 language: liveVideo.language,
108 commentsEnabled: liveVideo.commentsEnabled,
109 downloadEnabled: liveVideo.downloadEnabled,
26e3e98f 110 waitTranscoding: true,
4ec52d04
C
111 nsfw: liveVideo.nsfw,
112 description: liveVideo.description,
113 support: liveVideo.support,
05a60d85 114 privacy: replaySettings.privacy,
4ec52d04
C
115 channelId: liveVideo.channelId
116 }) as MVideoWithAllFiles
117
c8fa571f
C
118 replayVideo.Thumbnails = []
119 replayVideo.VideoFiles = []
120 replayVideo.VideoStreamingPlaylists = []
4ec52d04 121
c8fa571f 122 replayVideo.url = getLocalVideoActivityPubUrl(replayVideo)
4ec52d04 123
c8fa571f 124 await replayVideo.save()
4ec52d04 125
c8fa571f 126 liveSession.replayVideoId = replayVideo.id
26e3e98f
C
127 await liveSession.save()
128
4ec52d04
C
129 // If live is blacklisted, also blacklist the replay
130 const blacklist = await VideoBlacklistModel.loadByVideoId(liveVideo.id)
131 if (blacklist) {
132 await VideoBlacklistModel.create({
c8fa571f 133 videoId: replayVideo.id,
4ec52d04
C
134 unfederated: blacklist.unfederated,
135 reason: blacklist.reason,
136 type: blacklist.type
137 })
138 }
139
c8fa571f 140 await assignReplayFilesToVideo({ video: replayVideo, replayDirectory })
937581b8 141
4ec52d04
C
142 await remove(replayDirectory)
143
144 for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) {
c8fa571f
C
145 const image = await generateVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type })
146 await replayVideo.addAndSaveThumbnail(image)
4ec52d04 147 }
937581b8 148
c8fa571f 149 await moveToNextState({ video: replayVideo, isNewVideo: true })
4ec52d04 150}
937581b8 151
26e3e98f 152async function replaceLiveByReplay (options: {
c8fa571f 153 video: MVideo
26e3e98f
C
154 liveSession: MVideoLiveSession
155 live: MVideoLive
c8fa571f 156 permanentLive: boolean
26e3e98f
C
157 replayDirectory: string
158}) {
c8fa571f 159 const { video, liveSession, live, permanentLive, replayDirectory } = options
26e3e98f 160
05a60d85 161 const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId)
cfd57d2c
C
162 const videoWithFiles = await VideoModel.loadFull(video.id)
163 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
164
165 await cleanupTMPLiveFiles(videoWithFiles, hlsPlaylist)
b5b68755 166
31c82cd9
C
167 await live.destroy()
168
cfd57d2c 169 videoWithFiles.isLive = false
05a60d85 170 videoWithFiles.privacy = replaySettings.privacy
cfd57d2c
C
171 videoWithFiles.waitTranscoding = true
172 videoWithFiles.state = VideoState.TO_TRANSCODE
d846d99c 173
cfd57d2c 174 await videoWithFiles.save()
26e3e98f 175
cfd57d2c 176 liveSession.replayVideoId = videoWithFiles.id
26e3e98f 177 await liveSession.save()
b5b68755 178
97969c4e 179 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
764b1a14
C
180
181 // Reset playlist
97969c4e 182 hlsPlaylist.VideoFiles = []
764b1a14
C
183 hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename()
184 hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename()
185 await hlsPlaylist.save()
97969c4e 186
26e3e98f 187 await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory })
4ec52d04 188
c8fa571f 189 if (permanentLive) { // Remove session replay
5333788c
C
190 await remove(replayDirectory)
191 } else { // We won't stream again in this live, we can delete the base replay directory
192 await remove(getLiveReplayBaseDirectory(videoWithFiles))
193 }
4ec52d04
C
194
195 // Regenerate the thumbnail & preview?
196 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
197 const miniature = await generateVideoMiniature({
198 video: videoWithFiles,
199 videoFile: videoWithFiles.getMaxQualityFile(),
200 type: ThumbnailType.MINIATURE
201 })
26e3e98f 202 await videoWithFiles.addAndSaveThumbnail(miniature)
4ec52d04
C
203 }
204
205 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
206 const preview = await generateVideoMiniature({
207 video: videoWithFiles,
208 videoFile: videoWithFiles.getMaxQualityFile(),
209 type: ThumbnailType.PREVIEW
210 })
26e3e98f 211 await videoWithFiles.addAndSaveThumbnail(preview)
4ec52d04
C
212 }
213
26e3e98f
C
214 // We consider this is a new video
215 await moveToNextState({ video: videoWithFiles, isNewVideo: true })
4ec52d04
C
216}
217
26e3e98f
C
218async function assignReplayFilesToVideo (options: {
219 video: MVideo
220 replayDirectory: string
221}) {
222 const { video, replayDirectory } = options
223
4ec52d04
C
224 const concatenatedTsFiles = await readdir(replayDirectory)
225
226 for (const concatenatedTsFile of concatenatedTsFiles) {
3545e72c 227 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
a687879e 228 await video.reload()
3545e72c 229
3851e732 230 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
2650d6d4 231
e772bdf1
C
232 const probe = await ffprobePromise(concatenatedTsFilePath)
233 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
84cae54e 234 const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe)
0c9668f7 235 const fps = await getVideoStreamFPS(concatenatedTsFilePath, probe)
2650d6d4 236
3545e72c
C
237 try {
238 await generateHlsPlaylistResolutionFromTS({
239 video,
240 inputFileMutexReleaser,
241 concatenatedTsFilePath,
242 resolution,
0c9668f7 243 fps,
3545e72c
C
244 isAAC: audioStream?.codec_name === 'aac'
245 })
246 } catch (err) {
247 logger.error('Cannot generate HLS playlist resolution from TS files.', { err })
248 }
249
250 inputFileMutexReleaser()
97969c4e 251 }
d846d99c 252
4ec52d04
C
253 return video
254}
053aed43 255
26e3e98f 256async function cleanupLiveAndFederate (options: {
5333788c 257 video: MVideo
c8fa571f 258 permanentLive: boolean
cdd83816 259 streamingPlaylistId: number
26e3e98f 260}) {
c8fa571f 261 const { permanentLive, video, streamingPlaylistId } = options
bb4ba6d9 262
cdd83816 263 const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId)
a5cf76af 264
cdd83816 265 if (streamingPlaylist) {
c8fa571f 266 if (permanentLive) {
cfd57d2c 267 await cleanupAndDestroyPermanentLive(video, streamingPlaylist)
cdd83816 268 } else {
53023be3 269 await cleanupUnsavedNormalLive(video, streamingPlaylist)
cdd83816 270 }
a5cf76af 271 }
5333788c 272
c8fdfab0 273 try {
4fae2b1f 274 const fullVideo = await VideoModel.loadFull(video.id)
c8fdfab0
C
275 return federateVideoIfNeeded(fullVideo, false, undefined)
276 } catch (err) {
277 logger.warn('Cannot federate live after cleanup', { videoId: video.id, err })
278 }
a5cf76af 279}