X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Flive%2Flive-utils.ts;h=3fb3ce1cee998886f96f8f3dce6d0888b678f0dc;hb=ef2e6aabf755feeec96011e70ff2522a491c5cb3;hp=3bf723b981618663c33fd83b798d3a1a63c6fb71;hpb=0305db28c98fd6cf43a3c50ba92c76215e99d512;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/live/live-utils.ts b/server/lib/live/live-utils.ts index 3bf723b98..3fb3ce1ce 100644 --- a/server/lib/live/live-utils.ts +++ b/server/lib/live/live-utils.ts @@ -1,6 +1,10 @@ -import { remove } from 'fs-extra' -import { basename } from 'path' -import { MStreamingPlaylist, MVideo } from '@server/types/models' +import { pathExists, readdir, remove } from 'fs-extra' +import { basename, join } from 'path' +import { logger } from '@server/helpers/logger' +import { VIDEO_LIVE } from '@server/initializers/constants' +import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models' +import { LiveVideoLatencyMode, VideoStorage } from '@shared/models' +import { listHLSFileKeysOf, removeHLSFileObjectStorageByFullKey, removeHLSObjectStorage } from '../object-storage' import { getLiveDirectory } from '../paths' function buildConcatenatedName (segmentOrPlaylistPath: string) { @@ -9,15 +13,87 @@ function buildConcatenatedName (segmentOrPlaylistPath: string) { return 'concat-' + num[1] + '.ts' } -async function cleanupLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { +async function cleanupAndDestroyPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { + await cleanupTMPLiveFiles(video, streamingPlaylist) + + await streamingPlaylist.destroy() +} + +async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { const hlsDirectory = getLiveDirectory(video) + // We uploaded files to object storage too, remove them + if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) { + await removeHLSObjectStorage(streamingPlaylist.withVideo(video)) + } + await remove(hlsDirectory) await streamingPlaylist.destroy() } +async function cleanupTMPLiveFiles (video: MVideo, streamingPlaylist: MStreamingPlaylist) { + await cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video)) + + await cleanupTMPLiveFilesFromFilesystem(video) +} + +function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) { + if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) { + return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY + } + + return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY +} + export { - cleanupLive, + cleanupAndDestroyPermanentLive, + cleanupUnsavedNormalLive, + cleanupTMPLiveFiles, + getLiveSegmentTime, buildConcatenatedName } + +// --------------------------------------------------------------------------- + +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) + + if (!await pathExists(hlsDirectory)) return + + logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory) + + const files = await readdir(hlsDirectory) + + for (const filename of files) { + if (isTMPLiveFile(filename)) { + const p = join(hlsDirectory, filename) + + remove(p) + .catch(err => logger.error('Cannot remove %s.', p, { err })) + } + } +} + +async function cleanupTMPLiveFilesFromObjectStorage (streamingPlaylist: MStreamingPlaylistVideo) { + if (streamingPlaylist.storage !== VideoStorage.OBJECT_STORAGE) return + + logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid) + + const keys = await listHLSFileKeysOf(streamingPlaylist) + + for (const key of keys) { + if (isTMPLiveFile(key)) { + await removeHLSFileObjectStorageByFullKey(key) + } + } +}