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