2023-05-16 10:06:07 +02:00
|
|
|
import { rename, writeJson } from 'fs-extra'
|
2023-05-15 11:09:16 +02:00
|
|
|
import PQueue from 'p-queue'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { basename } from 'path'
|
2022-10-04 10:03:17 +02:00
|
|
|
import { mapToJSON } from '@server/helpers/core-utils'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
2022-10-04 10:03:17 +02:00
|
|
|
import { MStreamingPlaylistVideo } from '@server/types/models'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { buildSha256Segment } from '../hls'
|
2022-10-04 10:03:17 +02:00
|
|
|
import { storeHLSFileFromPath } from '../object-storage'
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('live')
|
|
|
|
|
|
|
|
class LiveSegmentShaStore {
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
private readonly segmentsSha256 = new Map<string, string>()
|
|
|
|
|
|
|
|
private readonly videoUUID: string
|
2023-05-15 11:09:16 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
private readonly sha256Path: string
|
2023-05-15 11:09:16 +02:00
|
|
|
private readonly sha256PathTMP: string
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
private readonly streamingPlaylist: MStreamingPlaylistVideo
|
|
|
|
private readonly sendToObjectStorage: boolean
|
2022-10-19 10:43:53 +02:00
|
|
|
private readonly writeQueue = new PQueue({ concurrency: 1 })
|
2022-10-04 10:03:17 +02:00
|
|
|
|
|
|
|
constructor (options: {
|
|
|
|
videoUUID: string
|
|
|
|
sha256Path: string
|
|
|
|
streamingPlaylist: MStreamingPlaylistVideo
|
|
|
|
sendToObjectStorage: boolean
|
|
|
|
}) {
|
|
|
|
this.videoUUID = options.videoUUID
|
2023-05-15 11:09:16 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
this.sha256Path = options.sha256Path
|
2023-05-15 11:09:16 +02:00
|
|
|
this.sha256PathTMP = options.sha256Path + '.tmp'
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
this.streamingPlaylist = options.streamingPlaylist
|
|
|
|
this.sendToObjectStorage = options.sendToObjectStorage
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
async addSegmentSha (segmentPath: string) {
|
|
|
|
logger.debug('Adding live sha segment %s.', segmentPath, lTags(this.videoUUID))
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
const shaResult = await buildSha256Segment(segmentPath)
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
const segmentName = basename(segmentPath)
|
|
|
|
this.segmentsSha256.set(segmentName, shaResult)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2022-10-19 10:43:53 +02:00
|
|
|
try {
|
|
|
|
await this.writeToDisk()
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot write sha segments to disk.', { err })
|
|
|
|
}
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
async removeSegmentSha (segmentPath: string) {
|
2021-06-16 15:14:41 +02:00
|
|
|
const segmentName = basename(segmentPath)
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
logger.debug('Removing live sha segment %s.', segmentPath, lTags(this.videoUUID))
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
if (!this.segmentsSha256.has(segmentName)) {
|
2023-04-21 14:55:10 +02:00
|
|
|
logger.warn(
|
|
|
|
'Unknown segment in live segment hash store for video %s and segment %s.',
|
|
|
|
this.videoUUID, segmentPath, lTags(this.videoUUID)
|
|
|
|
)
|
2021-06-16 15:14:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
this.segmentsSha256.delete(segmentName)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2022-10-04 10:03:17 +02:00
|
|
|
await this.writeToDisk()
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 10:43:53 +02:00
|
|
|
private writeToDisk () {
|
|
|
|
return this.writeQueue.add(async () => {
|
2023-05-22 14:31:35 +02:00
|
|
|
logger.debug(`Writing segment sha JSON ${this.sha256Path} of ${this.videoUUID} on disk %s.`, lTags(this.videoUUID))
|
|
|
|
|
2023-05-16 10:06:07 +02:00
|
|
|
// Atomic write: use rename instead of move that is not atomic
|
2023-05-15 11:09:16 +02:00
|
|
|
await writeJson(this.sha256PathTMP, mapToJSON(this.segmentsSha256))
|
2023-05-16 10:06:07 +02:00
|
|
|
await rename(this.sha256PathTMP, this.sha256Path)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2022-10-19 10:43:53 +02:00
|
|
|
if (this.sendToObjectStorage) {
|
|
|
|
const url = await storeHLSFileFromPath(this.streamingPlaylist, this.sha256Path)
|
2022-10-04 10:03:17 +02:00
|
|
|
|
2022-10-19 10:43:53 +02:00
|
|
|
if (this.streamingPlaylist.segmentsSha256Url !== url) {
|
|
|
|
this.streamingPlaylist.segmentsSha256Url = url
|
|
|
|
await this.streamingPlaylist.save()
|
|
|
|
}
|
2022-10-04 10:03:17 +02:00
|
|
|
}
|
2022-10-19 10:43:53 +02:00
|
|
|
})
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
LiveSegmentShaStore
|
|
|
|
}
|