]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/video-live-ending.ts
Fix audio issues with live replay
[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')) {
77 await LiveManager.Instance.addSegmentToReplay(hlsDirectory, join(hlsDirectory, 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 let durationDone: boolean
104
105 for (const playlistFile of playlistFiles) {
106 const concatenatedTsFile = LiveManager.Instance.buildConcatenatedName(playlistFile)
107 const concatenatedTsFilePath = join(replayDirectory, concatenatedTsFile)
108
109 const { videoFileResolution, isPortraitMode } = await getVideoFileResolution(concatenatedTsFilePath)
110
111 const outputPath = await generateHlsPlaylistFromTS({
112 video: videoWithFiles,
113 concatenatedTsFilePath,
114 resolution: videoFileResolution,
115 isPortraitMode
116 })
117
118 if (!durationDone) {
119 videoWithFiles.duration = await getDurationFromVideoFile(outputPath)
120 await videoWithFiles.save()
121
122 durationDone = true
123 }
124 }
125
126 await remove(replayDirectory)
127
128 // Regenerate the thumbnail & preview?
129 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
130 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.MINIATURE)
131 }
132
133 if (videoWithFiles.getPreview().automaticallyGenerated === true) {
134 await generateVideoMiniature(videoWithFiles, videoWithFiles.getMaxQualityFile(), ThumbnailType.PREVIEW)
135 }
136
137 await publishAndFederateIfNeeded(videoWithFiles, true)
138 }
139
140 async function cleanupLiveFiles (hlsDirectory: string) {
141 if (!await pathExists(hlsDirectory)) return
142
143 const files = await readdir(hlsDirectory)
144
145 for (const filename of files) {
146 if (
147 filename.endsWith('.ts') ||
148 filename.endsWith('.m3u8') ||
149 filename.endsWith('.mpd') ||
150 filename.endsWith('.m4s') ||
151 filename.endsWith('.tmp')
152 ) {
153 const p = join(hlsDirectory, filename)
154
155 remove(p)
156 .catch(err => logger.error('Cannot remove %s.', p, { err }))
157 }
158 }
159 }