]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/job-queue/handlers/video-live-ending.ts
Add check constraints live tests
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / video-live-ending.ts
CommitLineData
a5cf76af
C
1import * as Bull from 'bull'
2import { readdir, remove } from 'fs-extra'
3import { join } from 'path'
d846d99c
C
4import { getDurationFromVideoFile, getVideoFileResolution, hlsPlaylistToFragmentedMP4 } from '@server/helpers/ffmpeg-utils'
5import { publishAndFederateIfNeeded } from '@server/lib/video'
a5cf76af 6import { getHLSDirectory } from '@server/lib/video-paths'
b5b68755 7import { generateHlsPlaylist } from '@server/lib/video-transcoding'
a5cf76af 8import { VideoModel } from '@server/models/video/video'
b5b68755 9import { VideoLiveModel } from '@server/models/video/video-live'
a5cf76af 10import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
97969c4e 11import { MStreamingPlaylist, MVideo, MVideoLive, MVideoWithFile } from '@server/types/models'
b5b68755 12import { VideoLiveEndingPayload, VideoState } from '@shared/models'
a5cf76af 13import { logger } from '../../../helpers/logger'
97969c4e 14import { VideoFileModel } from '@server/models/video/video-file'
a5cf76af
C
15
16async function processVideoLiveEnding (job: Bull.Job) {
17 const payload = job.data as VideoLiveEndingPayload
18
b5b68755
C
19 const video = await VideoModel.load(payload.videoId)
20 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
21
22 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
23 if (!video || !streamingPlaylist || !live) {
24 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
a5cf76af
C
25 return
26 }
27
b5b68755
C
28 if (live.saveReplay !== true) {
29 return cleanupLive(video, streamingPlaylist)
30 }
31
31c82cd9 32 return saveLive(video, live)
b5b68755
C
33}
34
35// ---------------------------------------------------------------------------
36
37export {
38 processVideoLiveEnding
39}
40
41// ---------------------------------------------------------------------------
42
31c82cd9 43async function saveLive (video: MVideo, live: MVideoLive) {
b5b68755 44 const hlsDirectory = getHLSDirectory(video, false)
31c82cd9
C
45 const files = await readdir(hlsDirectory)
46
47 const playlistFiles = files.filter(f => f.endsWith('.m3u8') && f !== 'master.m3u8')
48 const resolutions: number[] = []
d846d99c 49 let duration: number
31c82cd9
C
50
51 for (const playlistFile of playlistFiles) {
52 const playlistPath = join(hlsDirectory, playlistFile)
53 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
b5b68755 54
31c82cd9 55 const mp4TmpName = buildMP4TmpName(videoFileResolution)
b5b68755 56
31c82cd9
C
57 // Playlist name is for example 3.m3u8
58 // Segments names are 3-0.ts 3-1.ts etc
59 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
60
61 const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
62 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpName)
63
97969c4e
C
64 for (const file of segmentFiles) {
65 await remove(join(hlsDirectory, file))
66 }
67
d846d99c
C
68 if (!duration) {
69 duration = await getDurationFromVideoFile(mp4TmpName)
70 }
71
31c82cd9 72 resolutions.push(videoFileResolution)
b5b68755
C
73 }
74
75 await cleanupLiveFiles(hlsDirectory)
76
31c82cd9
C
77 await live.destroy()
78
b5b68755
C
79 video.isLive = false
80 video.state = VideoState.TO_TRANSCODE
d846d99c
C
81 video.duration = duration
82
b5b68755
C
83 await video.save()
84
97969c4e 85 // Remove old HLS playlist video files
b5b68755
C
86 const videoWithFiles = await VideoModel.loadWithFiles(video.id)
87
97969c4e
C
88 const hlsPlaylist = videoWithFiles.getHLSPlaylist()
89 await VideoFileModel.removeHLSFilesOfVideoId(hlsPlaylist.id)
90 hlsPlaylist.VideoFiles = []
91
31c82cd9
C
92 for (const resolution of resolutions) {
93 const videoInputPath = buildMP4TmpName(resolution)
b5b68755
C
94 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
95
96 await generateHlsPlaylist({
97 video: videoWithFiles,
98 videoInputPath,
31c82cd9 99 resolution: resolution,
b5b68755
C
100 copyCodecs: true,
101 isPortraitMode
102 })
b5b68755 103
97969c4e
C
104 await remove(join(hlsDirectory, videoInputPath))
105 }
d846d99c 106
97969c4e 107 await publishAndFederateIfNeeded(video, true)
b5b68755
C
108}
109
110async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
a5cf76af
C
111 const hlsDirectory = getHLSDirectory(video, false)
112
b5b68755
C
113 await cleanupLiveFiles(hlsDirectory)
114
115 streamingPlaylist.destroy()
116 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
117}
118
119async function cleanupLiveFiles (hlsDirectory: string) {
a5cf76af
C
120 const files = await readdir(hlsDirectory)
121
122 for (const filename of files) {
123 if (
124 filename.endsWith('.ts') ||
125 filename.endsWith('.m3u8') ||
126 filename.endsWith('.mpd') ||
127 filename.endsWith('.m4s') ||
128 filename.endsWith('.tmp')
129 ) {
130 const p = join(hlsDirectory, filename)
131
132 remove(p)
133 .catch(err => logger.error('Cannot remove %s.', p, { err }))
134 }
135 }
a5cf76af
C
136}
137
b5b68755 138function buildMP4TmpName (resolution: number) {
31c82cd9 139 return resolution + '-tmp.mp4'
a5cf76af 140}