aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-05-25 15:22:28 +0200
committerChocobozzz <me@florianbigard.com>2022-05-25 15:22:28 +0200
commitab623c0e0b4815bd69a94887241a69aaa857ed26 (patch)
tree9140675d0aa501d9ddbbcf14bbdb5c28b6ef0712 /server/lib
parentc501cdef27e6769b7d2a13ac7b6deeb2e61eee46 (diff)
parentc8fdfab0e36cc7324c61710009bf334e836485d9 (diff)
downloadPeerTube-ab623c0e0b4815bd69a94887241a69aaa857ed26.tar.gz
PeerTube-ab623c0e0b4815bd69a94887241a69aaa857ed26.tar.zst
PeerTube-ab623c0e0b4815bd69a94887241a69aaa857ed26.zip
Merge branch 'release/4.2.0' into develop
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/job-queue/handlers/video-live-ending.ts53
-rw-r--r--server/lib/live/live-manager.ts6
-rw-r--r--server/lib/live/live-utils.ts40
-rw-r--r--server/lib/live/shared/muxing-session.ts41
4 files changed, 83 insertions, 57 deletions
diff --git a/server/lib/job-queue/handlers/video-live-ending.ts b/server/lib/job-queue/handlers/video-live-ending.ts
index 55fd09344..7607267f8 100644
--- a/server/lib/job-queue/handlers/video-live-ending.ts
+++ b/server/lib/job-queue/handlers/video-live-ending.ts
@@ -1,10 +1,10 @@
1import { Job } from 'bull' 1import { Job } from 'bull'
2import { pathExists, readdir, remove } from 'fs-extra' 2import { readdir, remove } from 'fs-extra'
3import { join } from 'path' 3import { join } from 'path'
4import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo, getVideoStreamDuration } from '@server/helpers/ffmpeg' 4import { ffprobePromise, getAudioStream, getVideoStreamDimensionsInfo, getVideoStreamDuration } from '@server/helpers/ffmpeg'
5import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' 5import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
6import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' 6import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
7import { cleanupLive, LiveSegmentShaStore } from '@server/lib/live' 7import { cleanupNormalLive, cleanupPermanentLive, cleanupTMPLiveFiles, LiveSegmentShaStore } from '@server/lib/live'
8import { 8import {
9 generateHLSMasterPlaylistFilename, 9 generateHLSMasterPlaylistFilename,
10 generateHlsSha256SegmentsFilename, 10 generateHlsSha256SegmentsFilename,
@@ -45,13 +45,13 @@ async function processVideoLiveEnding (job: Job) {
45 LiveSegmentShaStore.Instance.cleanupShaSegments(liveVideo.uuid) 45 LiveSegmentShaStore.Instance.cleanupShaSegments(liveVideo.uuid)
46 46
47 if (live.saveReplay !== true) { 47 if (live.saveReplay !== true) {
48 return cleanupLiveAndFederate({ liveVideo }) 48 return cleanupLiveAndFederate({ live, video: liveVideo })
49 } 49 }
50 50
51 if (live.permanentLive) { 51 if (live.permanentLive) {
52 await saveReplayToExternalVideo({ liveVideo, liveSession, publishedAt: payload.publishedAt, replayDirectory: payload.replayDirectory }) 52 await saveReplayToExternalVideo({ liveVideo, liveSession, publishedAt: payload.publishedAt, replayDirectory: payload.replayDirectory })
53 53
54 return cleanupLiveAndFederate({ liveVideo }) 54 return cleanupLiveAndFederate({ live, video: liveVideo })
55 } 55 }
56 56
57 return replaceLiveByReplay({ liveVideo, live, liveSession, replayDirectory: payload.replayDirectory }) 57 return replaceLiveByReplay({ liveVideo, live, liveSession, replayDirectory: payload.replayDirectory })
@@ -164,7 +164,11 @@ async function replaceLiveByReplay (options: {
164 164
165 await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory }) 165 await assignReplayFilesToVideo({ video: videoWithFiles, replayDirectory })
166 166
167 await remove(getLiveReplayBaseDirectory(videoWithFiles)) 167 if (live.permanentLive) { // Remove session replay
168 await remove(replayDirectory)
169 } else { // We won't stream again in this live, we can delete the base replay directory
170 await remove(getLiveReplayBaseDirectory(videoWithFiles))
171 }
168 172
169 // Regenerate the thumbnail & preview? 173 // Regenerate the thumbnail & preview?
170 if (videoWithFiles.getMiniature().automaticallyGenerated === true) { 174 if (videoWithFiles.getMiniature().automaticallyGenerated === true) {
@@ -227,34 +231,23 @@ async function assignReplayFilesToVideo (options: {
227} 231}
228 232
229async function cleanupLiveAndFederate (options: { 233async function cleanupLiveAndFederate (options: {
230 liveVideo: MVideo 234 live: MVideoLive
235 video: MVideo
231}) { 236}) {
232 const { liveVideo } = options 237 const { live, video } = options
233
234 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(liveVideo.id)
235 await cleanupLive(liveVideo, streamingPlaylist)
236 238
237 const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(liveVideo.id) 239 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
238 return federateVideoIfNeeded(fullVideo, false, undefined)
239}
240 240
241async function cleanupTMPLiveFiles (hlsDirectory: string) { 241 if (live.permanentLive) {
242 if (!await pathExists(hlsDirectory)) return 242 await cleanupPermanentLive(video, streamingPlaylist)
243 243 } else {
244 const files = await readdir(hlsDirectory) 244 await cleanupNormalLive(video, streamingPlaylist)
245 245 }
246 for (const filename of files) {
247 if (
248 filename.endsWith('.ts') ||
249 filename.endsWith('.m3u8') ||
250 filename.endsWith('.mpd') ||
251 filename.endsWith('.m4s') ||
252 filename.endsWith('.tmp')
253 ) {
254 const p = join(hlsDirectory, filename)
255 246
256 remove(p) 247 try {
257 .catch(err => logger.error('Cannot remove %s.', p, { err })) 248 const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
258 } 249 return federateVideoIfNeeded(fullVideo, false, undefined)
250 } catch (err) {
251 logger.warn('Cannot federate live after cleanup', { videoId: video.id, err })
259 } 252 }
260} 253}
diff --git a/server/lib/live/live-manager.ts b/server/lib/live/live-manager.ts
index e04ae9fef..0f14a6851 100644
--- a/server/lib/live/live-manager.ts
+++ b/server/lib/live/live-manager.ts
@@ -28,7 +28,7 @@ import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, g
28import { PeerTubeSocket } from '../peertube-socket' 28import { PeerTubeSocket } from '../peertube-socket'
29import { LiveQuotaStore } from './live-quota-store' 29import { LiveQuotaStore } from './live-quota-store'
30import { LiveSegmentShaStore } from './live-segment-sha-store' 30import { LiveSegmentShaStore } from './live-segment-sha-store'
31import { cleanupLive } from './live-utils' 31import { cleanupPermanentLive } from './live-utils'
32import { MuxingSession } from './shared' 32import { MuxingSession } from './shared'
33 33
34const NodeRtmpSession = require('node-media-server/src/node_rtmp_session') 34const NodeRtmpSession = require('node-media-server/src/node_rtmp_session')
@@ -224,7 +224,9 @@ class LiveManager {
224 224
225 const oldStreamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) 225 const oldStreamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
226 if (oldStreamingPlaylist) { 226 if (oldStreamingPlaylist) {
227 await cleanupLive(video, oldStreamingPlaylist) 227 if (!videoLive.permanentLive) throw new Error('Found previous session in a non permanent live: ' + video.uuid)
228
229 await cleanupPermanentLive(video, oldStreamingPlaylist)
228 } 230 }
229 231
230 this.videoSessions.set(video.id, sessionId) 232 this.videoSessions.set(video.id, sessionId)
diff --git a/server/lib/live/live-utils.ts b/server/lib/live/live-utils.ts
index 46c7fd2f8..6365e23db 100644
--- a/server/lib/live/live-utils.ts
+++ b/server/lib/live/live-utils.ts
@@ -1,5 +1,6 @@
1import { remove } from 'fs-extra' 1import { pathExists, readdir, remove } from 'fs-extra'
2import { basename } from 'path' 2import { basename, join } from 'path'
3import { logger } from '@server/helpers/logger'
3import { MStreamingPlaylist, MVideo } from '@server/types/models' 4import { MStreamingPlaylist, MVideo } from '@server/types/models'
4import { getLiveDirectory } from '../paths' 5import { getLiveDirectory } from '../paths'
5 6
@@ -9,7 +10,15 @@ function buildConcatenatedName (segmentOrPlaylistPath: string) {
9 return 'concat-' + num[1] + '.ts' 10 return 'concat-' + num[1] + '.ts'
10} 11}
11 12
12async function cleanupLive (video: MVideo, streamingPlaylist?: MStreamingPlaylist) { 13async function cleanupPermanentLive (video: MVideo, streamingPlaylist?: MStreamingPlaylist) {
14 const hlsDirectory = getLiveDirectory(video)
15
16 await cleanupTMPLiveFiles(hlsDirectory)
17
18 if (streamingPlaylist) await streamingPlaylist.destroy()
19}
20
21async function cleanupNormalLive (video: MVideo, streamingPlaylist?: MStreamingPlaylist) {
13 const hlsDirectory = getLiveDirectory(video) 22 const hlsDirectory = getLiveDirectory(video)
14 23
15 await remove(hlsDirectory) 24 await remove(hlsDirectory)
@@ -17,7 +26,30 @@ async function cleanupLive (video: MVideo, streamingPlaylist?: MStreamingPlaylis
17 if (streamingPlaylist) await streamingPlaylist.destroy() 26 if (streamingPlaylist) await streamingPlaylist.destroy()
18} 27}
19 28
29async function cleanupTMPLiveFiles (hlsDirectory: string) {
30 if (!await pathExists(hlsDirectory)) return
31
32 const files = await readdir(hlsDirectory)
33
34 for (const filename of files) {
35 if (
36 filename.endsWith('.ts') ||
37 filename.endsWith('.m3u8') ||
38 filename.endsWith('.mpd') ||
39 filename.endsWith('.m4s') ||
40 filename.endsWith('.tmp')
41 ) {
42 const p = join(hlsDirectory, filename)
43
44 remove(p)
45 .catch(err => logger.error('Cannot remove %s.', p, { err }))
46 }
47 }
48}
49
20export { 50export {
21 cleanupLive, 51 cleanupPermanentLive,
52 cleanupNormalLive,
53 cleanupTMPLiveFiles,
22 buildConcatenatedName 54 buildConcatenatedName
23} 55}
diff --git a/server/lib/live/shared/muxing-session.ts b/server/lib/live/shared/muxing-session.ts
index 1ee9b430f..98a7b2613 100644
--- a/server/lib/live/shared/muxing-session.ts
+++ b/server/lib/live/shared/muxing-session.ts
@@ -150,8 +150,8 @@ class MuxingSession extends EventEmitter {
150 150
151 logger.info('Running live muxing/transcoding for %s.', this.videoUUID, this.lTags()) 151 logger.info('Running live muxing/transcoding for %s.', this.videoUUID, this.lTags())
152 152
153 this.watchTSFiles(this.outDirectory) 153 this.watchTSFiles()
154 this.watchMasterFile(this.outDirectory) 154 this.watchMasterFile()
155 155
156 let ffmpegShellCommand: string 156 let ffmpegShellCommand: string
157 this.ffmpegCommand.on('start', cmdline => { 157 this.ffmpegCommand.on('start', cmdline => {
@@ -161,13 +161,13 @@ class MuxingSession extends EventEmitter {
161 }) 161 })
162 162
163 this.ffmpegCommand.on('error', (err, stdout, stderr) => { 163 this.ffmpegCommand.on('error', (err, stdout, stderr) => {
164 this.onFFmpegError({ err, stdout, stderr, outPath: this.outDirectory, ffmpegShellCommand }) 164 this.onFFmpegError({ err, stdout, stderr, ffmpegShellCommand })
165 }) 165 })
166 166
167 this.ffmpegCommand.on('end', () => { 167 this.ffmpegCommand.on('end', () => {
168 this.emit('ffmpeg-end', ({ videoId: this.videoId })) 168 this.emit('ffmpeg-end', ({ videoId: this.videoId }))
169 169
170 this.onFFmpegEnded(this.outDirectory) 170 this.onFFmpegEnded()
171 }) 171 })
172 172
173 this.ffmpegCommand.run() 173 this.ffmpegCommand.run()
@@ -189,12 +189,11 @@ class MuxingSession extends EventEmitter {
189 err: any 189 err: any
190 stdout: string 190 stdout: string
191 stderr: string 191 stderr: string
192 outPath: string
193 ffmpegShellCommand: string 192 ffmpegShellCommand: string
194 }) { 193 }) {
195 const { err, stdout, stderr, outPath, ffmpegShellCommand } = options 194 const { err, stdout, stderr, ffmpegShellCommand } = options
196 195
197 this.onFFmpegEnded(outPath) 196 this.onFFmpegEnded()
198 197
199 // Don't care that we killed the ffmpeg process 198 // Don't care that we killed the ffmpeg process
200 if (err?.message?.includes('Exiting normally')) return 199 if (err?.message?.includes('Exiting normally')) return
@@ -204,7 +203,7 @@ class MuxingSession extends EventEmitter {
204 this.emit('ffmpeg-error', ({ videoId: this.videoId })) 203 this.emit('ffmpeg-error', ({ videoId: this.videoId }))
205 } 204 }
206 205
207 private onFFmpegEnded (outPath: string) { 206 private onFFmpegEnded () {
208 logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', this.inputUrl, this.lTags()) 207 logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', this.inputUrl, this.lTags())
209 208
210 setTimeout(() => { 209 setTimeout(() => {
@@ -214,12 +213,12 @@ class MuxingSession extends EventEmitter {
214 .then(() => { 213 .then(() => {
215 // Process remaining segments hash 214 // Process remaining segments hash
216 for (const key of Object.keys(this.segmentsToProcessPerPlaylist)) { 215 for (const key of Object.keys(this.segmentsToProcessPerPlaylist)) {
217 this.processSegments(outPath, this.segmentsToProcessPerPlaylist[key]) 216 this.processSegments(this.segmentsToProcessPerPlaylist[key])
218 } 217 }
219 }) 218 })
220 .catch(err => { 219 .catch(err => {
221 logger.error( 220 logger.error(
222 'Cannot close watchers of %s or process remaining hash segments.', outPath, 221 'Cannot close watchers of %s or process remaining hash segments.', this.outDirectory,
223 { err, ...this.lTags() } 222 { err, ...this.lTags() }
224 ) 223 )
225 }) 224 })
@@ -228,21 +227,21 @@ class MuxingSession extends EventEmitter {
228 }, 1000) 227 }, 1000)
229 } 228 }
230 229
231 private watchMasterFile (outPath: string) { 230 private watchMasterFile () {
232 this.masterWatcher = watch(outPath + '/' + this.streamingPlaylist.playlistFilename) 231 this.masterWatcher = watch(this.outDirectory + '/' + this.streamingPlaylist.playlistFilename)
233 232
234 this.masterWatcher.on('add', () => { 233 this.masterWatcher.on('add', () => {
235 this.emit('master-playlist-created', { videoId: this.videoId }) 234 this.emit('master-playlist-created', { videoId: this.videoId })
236 235
237 this.masterWatcher.close() 236 this.masterWatcher.close()
238 .catch(err => logger.error('Cannot close master watcher of %s.', outPath, { err, ...this.lTags() })) 237 .catch(err => logger.error('Cannot close master watcher of %s.', this.outDirectory, { err, ...this.lTags() }))
239 }) 238 })
240 } 239 }
241 240
242 private watchTSFiles (outPath: string) { 241 private watchTSFiles () {
243 const startStreamDateTime = new Date().getTime() 242 const startStreamDateTime = new Date().getTime()
244 243
245 this.tsWatcher = watch(outPath + '/*.ts') 244 this.tsWatcher = watch(this.outDirectory + '/*.ts')
246 245
247 const playlistIdMatcher = /^([\d+])-/ 246 const playlistIdMatcher = /^([\d+])-/
248 247
@@ -252,7 +251,7 @@ class MuxingSession extends EventEmitter {
252 const playlistId = basename(segmentPath).match(playlistIdMatcher)[0] 251 const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
253 252
254 const segmentsToProcess = this.segmentsToProcessPerPlaylist[playlistId] || [] 253 const segmentsToProcess = this.segmentsToProcessPerPlaylist[playlistId] || []
255 this.processSegments(outPath, segmentsToProcess) 254 this.processSegments(segmentsToProcess)
256 255
257 this.segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ] 256 this.segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
258 257
@@ -273,7 +272,7 @@ class MuxingSession extends EventEmitter {
273 } 272 }
274 } 273 }
275 274
276 const deleteHandler = segmentPath => LiveSegmentShaStore.Instance.removeSegmentSha(this.videoUUID, segmentPath) 275 const deleteHandler = (segmentPath: string) => LiveSegmentShaStore.Instance.removeSegmentSha(this.videoUUID, segmentPath)
277 276
278 this.tsWatcher.on('add', p => addHandler(p)) 277 this.tsWatcher.on('add', p => addHandler(p))
279 this.tsWatcher.on('unlink', p => deleteHandler(p)) 278 this.tsWatcher.on('unlink', p => deleteHandler(p))
@@ -332,15 +331,15 @@ class MuxingSession extends EventEmitter {
332 return now <= max 331 return now <= max
333 } 332 }
334 333
335 private processSegments (hlsVideoPath: string, segmentPaths: string[]) { 334 private processSegments (segmentPaths: string[]) {
336 mapSeries(segmentPaths, async previousSegment => { 335 mapSeries(segmentPaths, async previousSegment => {
337 // Add sha hash of previous segments, because ffmpeg should have finished generating them 336 // Add sha hash of previous segments, because ffmpeg should have finished generating them
338 await LiveSegmentShaStore.Instance.addSegmentSha(this.videoUUID, previousSegment) 337 await LiveSegmentShaStore.Instance.addSegmentSha(this.videoUUID, previousSegment)
339 338
340 if (this.saveReplay) { 339 if (this.saveReplay) {
341 await this.addSegmentToReplay(hlsVideoPath, previousSegment) 340 await this.addSegmentToReplay(previousSegment)
342 } 341 }
343 }).catch(err => logger.error('Cannot process segments in %s', hlsVideoPath, { err, ...this.lTags() })) 342 }).catch(err => logger.error('Cannot process segments', { err, ...this.lTags() }))
344 } 343 }
345 344
346 private hasClientSocketInBadHealth (sessionId: string) { 345 private hasClientSocketInBadHealth (sessionId: string) {
@@ -367,7 +366,7 @@ class MuxingSession extends EventEmitter {
367 return false 366 return false
368 } 367 }
369 368
370 private async addSegmentToReplay (hlsVideoPath: string, segmentPath: string) { 369 private async addSegmentToReplay (segmentPath: string) {
371 const segmentName = basename(segmentPath) 370 const segmentName = basename(segmentPath)
372 const dest = join(this.replayDirectory, buildConcatenatedName(segmentName)) 371 const dest = join(this.replayDirectory, buildConcatenatedName(segmentName))
373 372