aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/job-queue/handlers/video-live-ending.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-11-04 14:16:57 +0100
committerChocobozzz <chocobozzz@cpy.re>2020-11-09 15:33:04 +0100
commit68e70a745b2010cd0199864a2addd60d8f99c732 (patch)
treea0b1b061d205321e0f6eb80e5b44d7afd06aab14 /server/lib/job-queue/handlers/video-live-ending.ts
parent97969c4edf51b37eee691adba43368bb0fbb729b (diff)
downloadPeerTube-68e70a745b2010cd0199864a2addd60d8f99c732.tar.gz
PeerTube-68e70a745b2010cd0199864a2addd60d8f99c732.tar.zst
PeerTube-68e70a745b2010cd0199864a2addd60d8f99c732.zip
Add save replay live tests
Diffstat (limited to 'server/lib/job-queue/handlers/video-live-ending.ts')
-rw-r--r--server/lib/job-queue/handlers/video-live-ending.ts33
1 files changed, 21 insertions, 12 deletions
diff --git a/server/lib/job-queue/handlers/video-live-ending.ts b/server/lib/job-queue/handlers/video-live-ending.ts
index 1e964726e..2b900998a 100644
--- a/server/lib/job-queue/handlers/video-live-ending.ts
+++ b/server/lib/job-queue/handlers/video-live-ending.ts
@@ -6,22 +6,31 @@ import { publishAndFederateIfNeeded } from '@server/lib/video'
6import { getHLSDirectory } from '@server/lib/video-paths' 6import { getHLSDirectory } from '@server/lib/video-paths'
7import { generateHlsPlaylist } from '@server/lib/video-transcoding' 7import { generateHlsPlaylist } from '@server/lib/video-transcoding'
8import { VideoModel } from '@server/models/video/video' 8import { VideoModel } from '@server/models/video/video'
9import { VideoFileModel } from '@server/models/video/video-file'
9import { VideoLiveModel } from '@server/models/video/video-live' 10import { VideoLiveModel } from '@server/models/video/video-live'
10import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' 11import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
11import { MStreamingPlaylist, MVideo, MVideoLive, MVideoWithFile } from '@server/types/models' 12import { MStreamingPlaylist, MVideo, MVideoLive } from '@server/types/models'
12import { VideoLiveEndingPayload, VideoState } from '@shared/models' 13import { VideoLiveEndingPayload, VideoState } from '@shared/models'
13import { logger } from '../../../helpers/logger' 14import { logger } from '../../../helpers/logger'
14import { VideoFileModel } from '@server/models/video/video-file'
15 15
16async function processVideoLiveEnding (job: Bull.Job) { 16async function processVideoLiveEnding (job: Bull.Job) {
17 const payload = job.data as VideoLiveEndingPayload 17 const payload = job.data as VideoLiveEndingPayload
18 18
19 function logError () {
20 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId)
21 }
22
19 const video = await VideoModel.load(payload.videoId) 23 const video = await VideoModel.load(payload.videoId)
20 const live = await VideoLiveModel.loadByVideoId(payload.videoId) 24 const live = await VideoLiveModel.loadByVideoId(payload.videoId)
21 25
26 if (!video || !live) {
27 logError()
28 return
29 }
30
22 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) 31 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
23 if (!video || !streamingPlaylist || !live) { 32 if (!streamingPlaylist) {
24 logger.warn('Video live %d does not exist anymore. Cannot process live ending.', payload.videoId) 33 logError()
25 return 34 return
26 } 35 }
27 36
@@ -52,21 +61,21 @@ async function saveLive (video: MVideo, live: MVideoLive) {
52 const playlistPath = join(hlsDirectory, playlistFile) 61 const playlistPath = join(hlsDirectory, playlistFile)
53 const { videoFileResolution } = await getVideoFileResolution(playlistPath) 62 const { videoFileResolution } = await getVideoFileResolution(playlistPath)
54 63
55 const mp4TmpName = buildMP4TmpName(videoFileResolution) 64 const mp4TmpPath = buildMP4TmpPath(hlsDirectory, videoFileResolution)
56 65
57 // Playlist name is for example 3.m3u8 66 // Playlist name is for example 3.m3u8
58 // Segments names are 3-0.ts 3-1.ts etc 67 // Segments names are 3-0.ts 3-1.ts etc
59 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-' 68 const shouldStartWith = playlistFile.replace(/\.m3u8$/, '') + '-'
60 69
61 const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts')) 70 const segmentFiles = files.filter(f => f.startsWith(shouldStartWith) && f.endsWith('.ts'))
62 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpName) 71 await hlsPlaylistToFragmentedMP4(hlsDirectory, segmentFiles, mp4TmpPath)
63 72
64 for (const file of segmentFiles) { 73 for (const file of segmentFiles) {
65 await remove(join(hlsDirectory, file)) 74 await remove(join(hlsDirectory, file))
66 } 75 }
67 76
68 if (!duration) { 77 if (!duration) {
69 duration = await getDurationFromVideoFile(mp4TmpName) 78 duration = await getDurationFromVideoFile(mp4TmpPath)
70 } 79 }
71 80
72 resolutions.push(videoFileResolution) 81 resolutions.push(videoFileResolution)
@@ -90,7 +99,7 @@ async function saveLive (video: MVideo, live: MVideoLive) {
90 hlsPlaylist.VideoFiles = [] 99 hlsPlaylist.VideoFiles = []
91 100
92 for (const resolution of resolutions) { 101 for (const resolution of resolutions) {
93 const videoInputPath = buildMP4TmpName(resolution) 102 const videoInputPath = buildMP4TmpPath(hlsDirectory, resolution)
94 const { isPortraitMode } = await getVideoFileResolution(videoInputPath) 103 const { isPortraitMode } = await getVideoFileResolution(videoInputPath)
95 104
96 await generateHlsPlaylist({ 105 await generateHlsPlaylist({
@@ -101,7 +110,7 @@ async function saveLive (video: MVideo, live: MVideoLive) {
101 isPortraitMode 110 isPortraitMode
102 }) 111 })
103 112
104 await remove(join(hlsDirectory, videoInputPath)) 113 await remove(videoInputPath)
105 } 114 }
106 115
107 await publishAndFederateIfNeeded(video, true) 116 await publishAndFederateIfNeeded(video, true)
@@ -110,7 +119,7 @@ async function saveLive (video: MVideo, live: MVideoLive) {
110async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { 119async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
111 const hlsDirectory = getHLSDirectory(video, false) 120 const hlsDirectory = getHLSDirectory(video, false)
112 121
113 await cleanupLiveFiles(hlsDirectory) 122 await remove(hlsDirectory)
114 123
115 streamingPlaylist.destroy() 124 streamingPlaylist.destroy()
116 .catch(err => logger.error('Cannot remove live streaming playlist.', { err })) 125 .catch(err => logger.error('Cannot remove live streaming playlist.', { err }))
@@ -135,6 +144,6 @@ async function cleanupLiveFiles (hlsDirectory: string) {
135 } 144 }
136} 145}
137 146
138function buildMP4TmpName (resolution: number) { 147function buildMP4TmpPath (basePath: string, resolution: number) {
139 return resolution + '-tmp.mp4' 148 return join(basePath, resolution + '-tmp.mp4')
140} 149}