]>
Commit | Line | Data |
---|---|---|
5333788c C |
1 | import { pathExists, readdir, remove } from 'fs-extra' |
2 | import { basename, join } from 'path' | |
3 | import { logger } from '@server/helpers/logger' | |
0c9668f7 | 4 | import { VIDEO_LIVE } from '@server/initializers/constants' |
cfd57d2c | 5 | import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models' |
0c9668f7 | 6 | import { LiveVideoLatencyMode, VideoStorage } from '@shared/models' |
aa887096 | 7 | import { listHLSFileKeysOf, removeHLSFileObjectStorageByFullKey, removeHLSObjectStorage } from '../object-storage' |
0305db28 | 8 | import { getLiveDirectory } from '../paths' |
8ebf2a5d C |
9 | |
10 | function buildConcatenatedName (segmentOrPlaylistPath: string) { | |
11 | const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/) | |
12 | ||
13 | return 'concat-' + num[1] + '.ts' | |
14 | } | |
15 | ||
cfd57d2c C |
16 | async function cleanupAndDestroyPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
17 | await cleanupTMPLiveFiles(video, streamingPlaylist) | |
5333788c | 18 | |
53023be3 | 19 | await streamingPlaylist.destroy() |
5333788c C |
20 | } |
21 | ||
53023be3 | 22 | async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
0305db28 | 23 | const hlsDirectory = getLiveDirectory(video) |
8ebf2a5d | 24 | |
cfd57d2c C |
25 | // We uploaded files to object storage too, remove them |
26 | if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) { | |
27 | await removeHLSObjectStorage(streamingPlaylist.withVideo(video)) | |
28 | } | |
29 | ||
8ebf2a5d C |
30 | await remove(hlsDirectory) |
31 | ||
53023be3 | 32 | await streamingPlaylist.destroy() |
cfd57d2c | 33 | } |
92083e42 | 34 | |
cfd57d2c C |
35 | async function cleanupTMPLiveFiles (video: MVideo, streamingPlaylist: MStreamingPlaylist) { |
36 | await cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video)) | |
37 | ||
38 | await cleanupTMPLiveFilesFromFilesystem(video) | |
8ebf2a5d C |
39 | } |
40 | ||
0c9668f7 C |
41 | function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) { |
42 | if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) { | |
43 | return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY | |
44 | } | |
45 | ||
46 | return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY | |
47 | } | |
48 | ||
cfd57d2c C |
49 | export { |
50 | cleanupAndDestroyPermanentLive, | |
51 | cleanupUnsavedNormalLive, | |
52 | cleanupTMPLiveFiles, | |
0c9668f7 | 53 | getLiveSegmentTime, |
cfd57d2c C |
54 | buildConcatenatedName |
55 | } | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
92083e42 | 58 | |
cfd57d2c C |
59 | function isTMPLiveFile (name: string) { |
60 | return name.endsWith('.ts') || | |
61 | name.endsWith('.m3u8') || | |
62 | name.endsWith('.json') || | |
63 | name.endsWith('.mpd') || | |
64 | name.endsWith('.m4s') || | |
65 | name.endsWith('.tmp') | |
66 | } | |
67 | ||
68 | async function cleanupTMPLiveFilesFromFilesystem (video: MVideo) { | |
69 | const hlsDirectory = getLiveDirectory(video) | |
92083e42 | 70 | |
5333788c C |
71 | if (!await pathExists(hlsDirectory)) return |
72 | ||
cfd57d2c | 73 | logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory) |
92083e42 | 74 | |
5333788c C |
75 | const files = await readdir(hlsDirectory) |
76 | ||
77 | for (const filename of files) { | |
cfd57d2c | 78 | if (isTMPLiveFile(filename)) { |
5333788c C |
79 | const p = join(hlsDirectory, filename) |
80 | ||
81 | remove(p) | |
82 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
cfd57d2c C |
87 | async function cleanupTMPLiveFilesFromObjectStorage (streamingPlaylist: MStreamingPlaylistVideo) { |
88 | if (streamingPlaylist.storage !== VideoStorage.OBJECT_STORAGE) return | |
89 | ||
aa887096 C |
90 | logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid) |
91 | ||
cfd57d2c C |
92 | const keys = await listHLSFileKeysOf(streamingPlaylist) |
93 | ||
94 | for (const key of keys) { | |
95 | if (isTMPLiveFile(key)) { | |
aa887096 | 96 | await removeHLSFileObjectStorageByFullKey(key) |
cfd57d2c C |
97 | } |
98 | } | |
8ebf2a5d | 99 | } |