aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/live/live-segment-sha-store.ts
blob: 17e0c6a8296227f8a3b980ac150d4c2a611d0044 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { move, writeJson } from 'fs-extra'
import PQueue from 'p-queue'
import { basename } from 'path'
import { mapToJSON } from '@server/helpers/core-utils'
import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { MStreamingPlaylistVideo } from '@server/types/models'
import { buildSha256Segment } from '../hls'
import { storeHLSFileFromPath } from '../object-storage'

const lTags = loggerTagsFactory('live')

class LiveSegmentShaStore {

  private readonly segmentsSha256 = new Map<string, string>()

  private readonly videoUUID: string

  private readonly sha256Path: string
  private readonly sha256PathTMP: string

  private readonly streamingPlaylist: MStreamingPlaylistVideo
  private readonly sendToObjectStorage: boolean
  private readonly writeQueue = new PQueue({ concurrency: 1 })

  constructor (options: {
    videoUUID: string
    sha256Path: string
    streamingPlaylist: MStreamingPlaylistVideo
    sendToObjectStorage: boolean
  }) {
    this.videoUUID = options.videoUUID

    this.sha256Path = options.sha256Path
    this.sha256PathTMP = options.sha256Path + '.tmp'

    this.streamingPlaylist = options.streamingPlaylist
    this.sendToObjectStorage = options.sendToObjectStorage
  }

  async addSegmentSha (segmentPath: string) {
    logger.debug('Adding live sha segment %s.', segmentPath, lTags(this.videoUUID))

    const shaResult = await buildSha256Segment(segmentPath)

    const segmentName = basename(segmentPath)
    this.segmentsSha256.set(segmentName, shaResult)

    try {
      await this.writeToDisk()
    } catch (err) {
      logger.error('Cannot write sha segments to disk.', { err })
    }
  }

  async removeSegmentSha (segmentPath: string) {
    const segmentName = basename(segmentPath)

    logger.debug('Removing live sha segment %s.', segmentPath, lTags(this.videoUUID))

    if (!this.segmentsSha256.has(segmentName)) {
      logger.warn(
        'Unknown segment in live segment hash store for video %s and segment %s.',
        this.videoUUID, segmentPath, lTags(this.videoUUID)
      )
      return
    }

    this.segmentsSha256.delete(segmentName)

    await this.writeToDisk()
  }

  private writeToDisk () {
    return this.writeQueue.add(async () => {
      // Atomic write
      await writeJson(this.sha256PathTMP, mapToJSON(this.segmentsSha256))
      await move(this.sha256PathTMP, this.sha256Path, { overwrite: true })

      if (this.sendToObjectStorage) {
        const url = await storeHLSFileFromPath(this.streamingPlaylist, this.sha256Path)

        if (this.streamingPlaylist.segmentsSha256Url !== url) {
          this.streamingPlaylist.segmentsSha256Url = url
          await this.streamingPlaylist.save()
        }
      }
    })
  }
}

export {
  LiveSegmentShaStore
}