1 import { pathExists, readdir, remove } from 'fs-extra'
2 import { basename, join } from 'path'
3 import { logger } from '@server/helpers/logger'
4 import { VIDEO_LIVE } from '@server/initializers/constants'
5 import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models'
6 import { LiveVideoLatencyMode, VideoStorage } from '@shared/models'
7 import { listHLSFileKeysOf, removeHLSFileObjectStorageByFullKey, removeHLSObjectStorage } from '../object-storage'
8 import { getLiveDirectory } from '../paths'
10 function buildConcatenatedName (segmentOrPlaylistPath: string) {
11 const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
13 return 'concat-' + num[1] + '.ts'
16 async function cleanupAndDestroyPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
17 await cleanupTMPLiveFiles(video, streamingPlaylist)
19 await streamingPlaylist.destroy()
22 async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
23 const hlsDirectory = getLiveDirectory(video)
25 // We uploaded files to object storage too, remove them
26 if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) {
27 await removeHLSObjectStorage(streamingPlaylist.withVideo(video))
30 await remove(hlsDirectory)
32 await streamingPlaylist.destroy()
35 async function cleanupTMPLiveFiles (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
36 await cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video))
38 await cleanupTMPLiveFilesFromFilesystem(video)
41 function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) {
42 if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) {
43 return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY
46 return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY
50 cleanupAndDestroyPermanentLive,
51 cleanupUnsavedNormalLive,
57 // ---------------------------------------------------------------------------
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') ||
68 async function cleanupTMPLiveFilesFromFilesystem (video: MVideo) {
69 const hlsDirectory = getLiveDirectory(video)
71 if (!await pathExists(hlsDirectory)) return
73 logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory)
75 const files = await readdir(hlsDirectory)
77 for (const filename of files) {
78 if (isTMPLiveFile(filename)) {
79 const p = join(hlsDirectory, filename)
82 .catch(err => logger.error('Cannot remove %s.', p, { err }))
87 async function cleanupTMPLiveFilesFromObjectStorage (streamingPlaylist: MStreamingPlaylistVideo) {
88 if (streamingPlaylist.storage !== VideoStorage.OBJECT_STORAGE) return
90 logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid)
92 const keys = await listHLSFileKeysOf(streamingPlaylist)
94 for (const key of keys) {
95 if (isTMPLiveFile(key)) {
96 await removeHLSFileObjectStorageByFullKey(key)