aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-05-25 14:54:16 +0200
committerChocobozzz <me@florianbigard.com>2022-05-25 14:54:16 +0200
commit5333788c08ab6152303829d4624774b5d788ff40 (patch)
tree0377ba8504e798f71018a771742ac8d6bdbf6b51 /server
parentb34ee7fa5f6558bd6fb870756ace1cd12e40e94c (diff)
downloadPeerTube-5333788c08ab6152303829d4624774b5d788ff40.tar.gz
PeerTube-5333788c08ab6152303829d4624774b5d788ff40.tar.zst
PeerTube-5333788c08ab6152303829d4624774b5d788ff40.zip
Fix saving permanent live replay on quick restream
Diffstat (limited to 'server')
-rw-r--r--server/lib/job-queue/handlers/video-live-ending.ts51
-rw-r--r--server/lib/live/live-manager.ts6
-rw-r--r--server/lib/live/live-utils.ts40
-rw-r--r--server/tests/api/live/live-save-replay.ts34
-rw-r--r--server/tests/shared/live.ts24
5 files changed, 116 insertions, 39 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..79aa547ba 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,19 @@ 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
237 const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(liveVideo.id)
238 return federateVideoIfNeeded(fullVideo, false, undefined)
239}
240
241async function cleanupTMPLiveFiles (hlsDirectory: string) {
242 if (!await pathExists(hlsDirectory)) return
243 238
244 const files = await readdir(hlsDirectory) 239 const streamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
245 240
246 for (const filename of files) { 241 if (live.permanentLive) {
247 if ( 242 await cleanupPermanentLive(video, streamingPlaylist)
248 filename.endsWith('.ts') || 243 } else {
249 filename.endsWith('.m3u8') || 244 await cleanupNormalLive(video, streamingPlaylist)
250 filename.endsWith('.mpd') ||
251 filename.endsWith('.m4s') ||
252 filename.endsWith('.tmp')
253 ) {
254 const p = join(hlsDirectory, filename)
255
256 remove(p)
257 .catch(err => logger.error('Cannot remove %s.', p, { err }))
258 }
259 } 245 }
246
247 const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(video.id)
248 return federateVideoIfNeeded(fullVideo, false, undefined)
260} 249}
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/tests/api/live/live-save-replay.ts b/server/tests/api/live/live-save-replay.ts
index 7ddcb04ef..007af51e9 100644
--- a/server/tests/api/live/live-save-replay.ts
+++ b/server/tests/api/live/live-save-replay.ts
@@ -441,6 +441,40 @@ describe('Save replay setting', function () {
441 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404) 441 await checkVideosExist(liveVideoUUID, false, HttpStatusCode.NOT_FOUND_404)
442 await checkLiveCleanup(servers[0], liveVideoUUID, []) 442 await checkLiveCleanup(servers[0], liveVideoUUID, [])
443 }) 443 })
444
445 it('Should correctly save replays with multiple sessions', async function () {
446 this.timeout(120000)
447
448 liveVideoUUID = await createLiveWrapper({ permanent: true, replay: true })
449 await waitJobs(servers)
450
451 // Streaming session #1
452 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
453 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
454 await stopFfmpeg(ffmpegCommand)
455 await servers[0].live.waitUntilWaiting({ videoId: liveVideoUUID })
456
457 // Streaming session #2
458 ffmpegCommand = await servers[0].live.sendRTMPStreamInVideo({ videoId: liveVideoUUID })
459 await waitUntilLivePublishedOnAllServers(servers, liveVideoUUID)
460 await stopFfmpeg(ffmpegCommand)
461 await waitUntilLiveWaitingOnAllServers(servers, liveVideoUUID)
462
463 // Wait for replays
464 await waitJobs(servers)
465
466 const { total, data: sessions } = await servers[0].live.listSessions({ videoId: liveVideoUUID })
467
468 expect(total).to.equal(2)
469 expect(sessions).to.have.lengthOf(2)
470
471 for (const session of sessions) {
472 expect(session.error).to.be.null
473 expect(session.replayVideo).to.exist
474
475 await servers[0].videos.get({ id: session.replayVideo.uuid })
476 }
477 })
444 }) 478 })
445 479
446 after(async function () { 480 after(async function () {
diff --git a/server/tests/shared/live.ts b/server/tests/shared/live.ts
index 6ee4899b0..4bd4786fc 100644
--- a/server/tests/shared/live.ts
+++ b/server/tests/shared/live.ts
@@ -3,15 +3,35 @@
3import { expect } from 'chai' 3import { expect } from 'chai'
4import { pathExists, readdir } from 'fs-extra' 4import { pathExists, readdir } from 'fs-extra'
5import { join } from 'path' 5import { join } from 'path'
6import { LiveVideo } from '@shared/models'
6import { PeerTubeServer } from '@shared/server-commands' 7import { PeerTubeServer } from '@shared/server-commands'
7 8
8async function checkLiveCleanup (server: PeerTubeServer, videoUUID: string, savedResolutions: number[] = []) { 9async function checkLiveCleanup (server: PeerTubeServer, videoUUID: string, savedResolutions: number[] = []) {
10 let live: LiveVideo
11
12 try {
13 live = await server.live.get({ videoId: videoUUID })
14 } catch {}
15
9 const basePath = server.servers.buildDirectory('streaming-playlists') 16 const basePath = server.servers.buildDirectory('streaming-playlists')
10 const hlsPath = join(basePath, 'hls', videoUUID) 17 const hlsPath = join(basePath, 'hls', videoUUID)
11 18
12 if (savedResolutions.length === 0) { 19 if (savedResolutions.length === 0) {
13 const result = await pathExists(hlsPath) 20
14 expect(result).to.be.false 21 if (live?.permanentLive) {
22 expect(await pathExists(hlsPath)).to.be.true
23
24 const hlsFiles = await readdir(hlsPath)
25 expect(hlsFiles).to.have.lengthOf(1) // Only replays directory
26
27 const replayDir = join(hlsPath, 'replay')
28 expect(await pathExists(replayDir)).to.be.true
29
30 const replayFiles = await readdir(join(hlsPath, 'replay'))
31 expect(replayFiles).to.have.lengthOf(0)
32 } else {
33 expect(await pathExists(hlsPath)).to.be.false
34 }
15 35
16 return 36 return
17 } 37 }