]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
Add permanent live support
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
1 import * as Bull from 'bull'
2 import { copy, pathExists, readdir, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { getDurationFromVideoFile, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
5 import { VIDEO_LIVE } from '@server/initializers/constants'
6 import { generateVideoMiniature } from '@server/lib/thumbnail'
7 import { publishAndFederateIfNeeded } from '@server/lib/video'
8 import { getHLSDirectory } from '@server/lib/video-paths'
9 import { generateHlsPlaylistFromTS } from '@server/lib/video-transcoding'
10 import { VideoModel } from '@server/models/video/video'
11 import { VideoFileModel } from '@server/models/video/video-file'
12 import { VideoLiveModel } from '@server/models/video/video-live'
13 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
14 import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
15 import { ThumbnailType, VideoLiveEndingPayload, VideoState } from '@shared/models'
16 import { logger } from '../../../helpers/logger'
17 import { LiveManager } from '@server/lib/live-manager'
18
19 async function processVideoLiveEnding (job: Bull.Job) {
20 const payload = job.data as VideoLiveEndingPayload
21
22 function logError () {
23 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
24 }
25
26 const video = await VideoModel.load(payload.videoId)
27 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
28
29 if (!video || !live) {
30 logError()
31 return
32 }
33
34 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
35 if (!streamingPlaylist) {
36 logError()
37 return
38 }
39
40 LiveManager.Instance.cleanupShaSegments(video.uuid)
41
42 if (live.saveReplay !== true) {
43 return cleanupLive(video, streamingPlaylist)
44 }
45
46 return saveLive(video, live)
47 }
48
49 async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
50 const hlsDirectory = getHLSDirectory(video)
51
52 await remove(hlsDirectory)
53
54 await streamingPlaylist.destroy()
55 }
56
57 // ---------------------------------------------------------------------------
58
59 export {
60 processVideoLiveEnding,
61 cleanupLive
62 }
63
64 // ---------------------------------------------------------------------------
65
66 async function saveLive (video: MVideo, live: MVideoLive) {
67 const hlsDirectory = getHLSDirectory(video, false)
68 const replayDirectory = join(hlsDirectory, VIDEO_LIVE.REPLAY_DIRECTORY)
69
70 const rootFiles = await readdir(hlsDirectory)
71
72 const playlistFiles: string[] = []
73
74 for (const file of rootFiles) {
75 // Move remaining files in the replay directory
76 if (file.endsWith('.ts') || file.endsWith('.m3u8')) {
77 await copy(join(hlsDirectory, file), join(replayDirectory, file))
78 }
79
80 if (file.endsWith('.m3u8') && file !== 'master.m3u8') {
81 playlistFiles.push(file)
82 }
83 }
84
85 await cleanupLiveFiles(hlsDirectory)
86
87 await live.destroy()
88
89 video.isLive = false
90 // Reinit views
91 video.views = 0
92 video.state = VideoState.TO_TRANSCODE
93
94 await video.save()
95
96 // Remove old HLS playlist video files
97 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
98
99 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
100 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
101 hlsPlaylist.VideoFiles = []
102
103 const replayFiles = await readdir(replayDirectory)
104 let durationDone: boolean
105
106 for (const playlistFile of playlistFiles) {
107 const playlistPath = join(replayDirectory, playlistFile)
108 const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(playlistPath)
109
110 // Playlist name is for example 3.m3u8
111 // Segments names are 3-0.ts 3-1.ts etc
112 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
113
114 const segmentFiles = replayFiles.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
115
116 const outputPath = await generateHlsPlaylistFromTS({
117 video: videoWithFiles,
118 replayDirectory,
119 segmentFiles,
120 resolution: videoFileResolution,
121 isPortraitMode
122 })
123
124 if (!durationDone) {
125 videoWithFiles.duration = await getDurationFromVideoFile(outputPath)
126 await videoWithFiles.save()
127
128 durationDone = true
129 }
130 }
131
132 await remove(replayDirectory)
133
134 // Regenerate the thumbnail & preview?
135 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
136 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE)
137 }
138
139 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
140 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW)
141 }
142
143 await publishAndFederateIfNeeded(videoWithFiles, true)
144 }
145
146 async function cleanupLiveFiles (hlsDirectory: string) {
147 if (!await pathExists(hlsDirectory)) return
148
149 const files = await readdir(hlsDirectory)
150
151 for (const filename of files) {
152 if (
153 filename.endsWith('.ts') ||
154 filename.endsWith('.m3u8') ||
155 filename.endsWith('.mpd') ||
156 filename.endsWith('.m4s') ||
157 filename.endsWith('.tmp')
158 ) {
159 const p = join(hlsDirectory, filename)
160
161 remove(p)
162 .catch(err => logger.error('Cannot remove %s.', p, { err }))
163 }
164 }
165 }