2023-07-31 07:34:36 -05:00
|
|
|
import { pathExists, remove } from 'fs-extra/esm'
|
|
|
|
import { readdir } from 'fs/promises'
|
2022-05-25 07:54:16 -05:00
|
|
|
import { basename, join } from 'path'
|
2024-02-12 03:47:52 -06:00
|
|
|
import { LiveVideoLatencyMode, LiveVideoLatencyModeType, FileStorage } from '@peertube/peertube-models'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { logger } from '@server/helpers/logger.js'
|
|
|
|
import { VIDEO_LIVE } from '@server/initializers/constants.js'
|
|
|
|
import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models/index.js'
|
|
|
|
import { listHLSFileKeysOf, removeHLSFileObjectStorageByFullKey, removeHLSObjectStorage } from '../object-storage/index.js'
|
|
|
|
import { getLiveDirectory } from '../paths.js'
|
2021-06-16 08:14:41 -05:00
|
|
|
|
|
|
|
function buildConcatenatedName (segmentOrPlaylistPath: string) {
|
|
|
|
const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
|
|
|
|
|
|
|
|
return 'concat-' + num[1] + '.ts'
|
|
|
|
}
|
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
async function cleanupAndDestroyPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
|
|
|
|
await cleanupTMPLiveFiles(video, streamingPlaylist)
|
2022-05-25 07:54:16 -05:00
|
|
|
|
2022-06-23 03:29:43 -05:00
|
|
|
await streamingPlaylist.destroy()
|
2022-05-25 07:54:16 -05:00
|
|
|
}
|
|
|
|
|
2022-06-23 03:29:43 -05:00
|
|
|
async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
|
2021-08-17 01:26:20 -05:00
|
|
|
const hlsDirectory = getLiveDirectory(video)
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
// We uploaded files to object storage too, remove them
|
2024-02-12 03:47:52 -06:00
|
|
|
if (streamingPlaylist.storage === FileStorage.OBJECT_STORAGE) {
|
2022-10-04 03:03:17 -05:00
|
|
|
await removeHLSObjectStorage(streamingPlaylist.withVideo(video))
|
|
|
|
}
|
|
|
|
|
2021-06-16 08:14:41 -05:00
|
|
|
await remove(hlsDirectory)
|
|
|
|
|
2022-06-23 03:29:43 -05:00
|
|
|
await streamingPlaylist.destroy()
|
2022-10-04 03:03:17 -05:00
|
|
|
}
|
2022-06-24 02:34:56 -05:00
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
async function cleanupTMPLiveFiles (video: MVideo, streamingPlaylist: MStreamingPlaylist) {
|
|
|
|
await cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video))
|
|
|
|
|
|
|
|
await cleanupTMPLiveFilesFromFilesystem(video)
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
|
2023-07-31 07:34:36 -05:00
|
|
|
function getLiveSegmentTime (latencyMode: LiveVideoLatencyModeType) {
|
2023-04-21 07:55:10 -05:00
|
|
|
if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) {
|
|
|
|
return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY
|
|
|
|
}
|
|
|
|
|
|
|
|
return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY
|
|
|
|
}
|
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
export {
|
|
|
|
cleanupAndDestroyPermanentLive,
|
|
|
|
cleanupUnsavedNormalLive,
|
|
|
|
cleanupTMPLiveFiles,
|
2023-04-21 07:55:10 -05:00
|
|
|
getLiveSegmentTime,
|
2022-10-04 03:03:17 -05:00
|
|
|
buildConcatenatedName
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2022-06-24 02:34:56 -05:00
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
function isTMPLiveFile (name: string) {
|
|
|
|
return name.endsWith('.ts') ||
|
|
|
|
name.endsWith('.m3u8') ||
|
|
|
|
name.endsWith('.json') ||
|
|
|
|
name.endsWith('.mpd') ||
|
|
|
|
name.endsWith('.m4s') ||
|
|
|
|
name.endsWith('.tmp')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanupTMPLiveFilesFromFilesystem (video: MVideo) {
|
|
|
|
const hlsDirectory = getLiveDirectory(video)
|
2022-06-24 02:34:56 -05:00
|
|
|
|
2022-05-25 07:54:16 -05:00
|
|
|
if (!await pathExists(hlsDirectory)) return
|
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory)
|
2022-06-24 02:34:56 -05:00
|
|
|
|
2022-05-25 07:54:16 -05:00
|
|
|
const files = await readdir(hlsDirectory)
|
|
|
|
|
|
|
|
for (const filename of files) {
|
2022-10-04 03:03:17 -05:00
|
|
|
if (isTMPLiveFile(filename)) {
|
2022-05-25 07:54:16 -05:00
|
|
|
const p = join(hlsDirectory, filename)
|
|
|
|
|
|
|
|
remove(p)
|
|
|
|
.catch(err => logger.error('Cannot remove %s.', p, { err }))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
async function cleanupTMPLiveFilesFromObjectStorage (streamingPlaylist: MStreamingPlaylistVideo) {
|
2024-02-12 03:47:52 -06:00
|
|
|
if (streamingPlaylist.storage !== FileStorage.OBJECT_STORAGE) return
|
2022-10-04 03:03:17 -05:00
|
|
|
|
2022-10-25 07:18:59 -05:00
|
|
|
logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid)
|
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
const keys = await listHLSFileKeysOf(streamingPlaylist)
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
if (isTMPLiveFile(key)) {
|
2022-10-25 07:18:59 -05:00
|
|
|
await removeHLSFileObjectStorageByFullKey(key)
|
2022-10-04 03:03:17 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|