]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import { Job } from 'bullmq'
2 import { readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
5 import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
6 import { cleanupAndDestroyPermanentLive, cleanupTMPLiveFiles, cleanupUnsavedNormalLive } from '@server/lib/live'
7 import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '@server/lib/paths'
8 import { generateVideoMiniature } from '@server/lib/thumbnail'
9 import { generateHlsPlaylistResolutionFromTS } from '@server/lib/transcoding/hls-transcoding'
10 import { VideoPathManager } from '@server/lib/video-path-manager'
11 import { moveToNextState } from '@server/lib/video-state'
12 import { VideoModel } from '@server/models/video/video'
13 import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
14 import { VideoFileModel } from '@server/models/video/video-file'
15 import { VideoLiveModel } from '@server/models/video/video-live'
16 import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting'
17 import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
18 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
19 import { MVideo, MVideoLive, MVideoLiveSession, MVideoWithAllFiles } from '@server/types/models'
20 import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo, getVideoStreamFPS } from '@shared/ffmpeg'
21 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
22 import { logger, loggerTagsFactory } from '../../../helpers/logger'
23 import { peertubeTruncate } from '@server/helpers/core-utils'
24 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
25
26 const lTags = loggerTagsFactory('live', 'job')
27
28 async function processVideoLiveEnding (job: Job) {
29 const payload = job.data as VideoLiveEndingPayload
30
31 logger.info('Processing video live ending for %s.', payload.videoId, { payload, ...lTags() })
32
33 function logError () {
34 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId, lTags())
35 }
36
37 const video = await VideoModel.load(payload.videoId)
38 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
39 const liveSession = await VideoLiveSessionModel.load(payload.liveSessionId)
40
41 if (!video || !live || !liveSession) {
42 logError()
43 return
44 }
45
46 const permanentLive = live.permanentLive
47
48 liveSession.endingProcessed = true
49 await liveSession.save()
50
51 if (liveSession.saveReplay !== true) {
52 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
53 }
54
55 if (permanentLive) {
56 await saveReplayToExternalVideo({
57 liveVideo: video,
58 liveSession,
59 publishedAt: payload.publishedAt,
60 replayDirectory: payload.replayDirectory
61 })
62
63 return cleanupLiveAndFederate({ permanentLive, video, streamingPlaylistId: payload.streamingPlaylistId })
64 }
65
66 return replaceLiveByReplay({
67 video,
68 liveSession,
69 live,
70 permanentLive,
71 replayDirectory: payload.replayDirectory
72 })
73 }
74
75 // ---------------------------------------------------------------------------
76
77 export {
78 processVideoLiveEnding
79 }
80
81 // ---------------------------------------------------------------------------
82
83 async function saveReplayToExternalVideo (options: {
84 liveVideo: MVideo
85 liveSession: MVideoLiveSession
86 publishedAt: string
87 replayDirectory: string
88 }) {
89 const { liveVideo, liveSession, publishedAt, replayDirectory } = options
90
91 const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId)
92
93 const videoNameSuffix = ` - ${new Date(publishedAt).toLocaleString()}`
94 const truncatedVideoName = peertubeTruncate(liveVideo.name, {
95 length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max - videoNameSuffix.length
96 })
97
98 const replayVideo = new VideoModel({
99 name: truncatedVideoName + videoNameSuffix,
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,
110 waitTranscoding: true,
111 nsfw: liveVideo.nsfw,
112 description: liveVideo.description,
113 support: liveVideo.support,
114 privacy: replaySettings.privacy,
115 channelId: liveVideo.channelId
116 }) as MVideoWithAllFiles
117
118 replayVideo.Thumbnails = []
119 replayVideo.VideoFiles = []
120 replayVideo.VideoStreamingPlaylists = []
121
122 replayVideo.url = getLocalVideoActivityPubUrl(replayVideo)
123
124 await replayVideo.save()
125
126 liveSession.replayVideoId = replayVideo.id
127 await liveSession.save()
128
129 // If live is blacklisted, also blacklist the replay
130 const blacklist = await VideoBlacklistModel.loadByVideoId(liveVideo.id)
131 if (blacklist) {
132 await VideoBlacklistModel.create({
133 videoId: replayVideo.id,
134 unfederated: blacklist.unfederated,
135 reason: blacklist.reason,
136 type: blacklist.type
137 })
138 }
139
140 await assignReplayFilesToVideo({ video: replayVideo, replayDirectory })
141
142 await remove(replayDirectory)
143
144 for (const type of [ ThumbnailType.MINIATURE, ThumbnailType.PREVIEW ]) {
145 const image = await generateVideoMiniature({ video: replayVideo, videoFile: replayVideo.getMaxQualityFile(), type })
146 await replayVideo.addAndSaveThumbnail(image)
147 }
148
149 await moveToNextState({ video: replayVideo, isNewVideo: true })
150 }
151
152 async function replaceLiveByReplay (options: {
153 video: MVideo
154 liveSession: MVideoLiveSession
155 live: MVideoLive
156 permanentLive: boolean
157 replayDirectory: string
158 }) {
159 const { video, liveSession, live, permanentLive, replayDirectory } = options
160
161 const replaySettings = await VideoLiveReplaySettingModel.load(liveSession.replaySettingId)
162 const videoWithFiles = await VideoModel.loadFull(video.id)
163 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
164
165 await cleanupTMPLiveFiles(videoWithFiles, hlsPlaylist)
166
167 await live.destroy()
168
169 videoWithFiles.isLive = false
170 videoWithFiles.privacy = replaySettings.privacy
171 videoWithFiles.waitTranscoding = true
172 videoWithFiles.state = VideoState.TO_TRANSCODE
173
174 await videoWithFiles.save()
175
176 liveSession.replayVideoId = videoWithFiles.id
177 await liveSession.save()
178
179 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
180
181 // Reset playlist
182 hlsPlaylist.VideoFiles = []
183 hlsPlaylist.playlistFilename = generateHLSMasterPlaylistFilename()
184 hlsPlaylist.segmentsSha256Filename = generateHlsSha256SegmentsFilename()
185 await hlsPlaylist.save()
186
187 await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory })
188
189 if (permanentLive) { // Remove session replay
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 }
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 })
202 await videoWithFiles.addAndSaveThumbnail(miniature)
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 })
211 await videoWithFiles.addAndSaveThumbnail(preview)
212 }
213
214 // We consider this is a new video
215 await moveToNextState({ video: videoWithFiles, isNewVideo: true })
216 }
217
218 async function assignReplayFilesToVideo (options: {
219 video: MVideo
220 replayDirectory: string
221 }) {
222 const { video, replayDirectory } = options
223
224 const concatenatedTsFiles = await readdir(replayDirectory)
225
226 for (const concatenatedTsFile of concatenatedTsFiles) {
227 const inputFileMutexReleaser = await VideoPathManager.Instance.lockFiles(video.uuid)
228 await video.reload()
229
230 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
231
232 const probe = await ffprobePromise(concatenatedTsFilePath)
233 const { audioStream } = await getAudioStream(concatenatedTsFilePath, probe)
234 const { resolution } = await getVideoStreamDimensionsInfo(concatenatedTsFilePath, probe)
235 const fps = await getVideoStreamFPS(concatenatedTsFilePath, probe)
236
237 try {
238 await generateHlsPlaylistResolutionFromTS({
239 video,
240 inputFileMutexReleaser,
241 concatenatedTsFilePath,
242 resolution,
243 fps,
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()
251 }
252
253 return video
254 }
255
256 async function cleanupLiveAndFederate (options: {
257 video: MVideo
258 permanentLive: boolean
259 streamingPlaylistId: number
260 }) {
261 const { permanentLive, video, streamingPlaylistId } = options
262
263 const streamingPlaylist = await VideoStreamingPlaylistModel.loadWithVideo(streamingPlaylistId)
264
265 if (streamingPlaylist) {
266 if (permanentLive) {
267 await cleanupAndDestroyPermanentLive(video, streamingPlaylist)
268 } else {
269 await cleanupUnsavedNormalLive(video, streamingPlaylist)
270 }
271 }
272
273 try {
274 const fullVideo = await VideoModel.loadFull(video.id)
275 return federateVideoIfNeeded(fullVideo, false, undefined)
276 } catch (err) {
277 logger.warn('Cannot federate live after cleanup', { videoId: video.id, err })
278 }
279 }