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