2 import * as Bluebird from 'bluebird'
3 import * as chokidar from 'chokidar'
4 import { FfmpegCommand } from 'fluent-ffmpeg'
5 import { appendFile, ensureDir, readFile, stat } from 'fs-extra'
6 import { basename, join } from 'path'
7 import { EventEmitter } from 'stream'
8 import { getLiveMuxingCommand, getLiveTranscodingCommand } from '@server/helpers/ffmpeg-utils'
9 import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger'
10 import { CONFIG } from '@server/initializers/config'
11 import { MEMOIZE_TTL, VIDEO_LIVE } from '@server/initializers/constants'
12 import { VideoFileModel } from '@server/models/video/video-file'
13 import { MStreamingPlaylistVideo, MUserId, MVideoLiveVideo } from '@server/types/models'
14 import { VideoTranscodingProfilesManager } from '../../transcoding/video-transcoding-profiles'
15 import { isAbleToUploadVideo } from '../../user'
16 import { getHLSDirectory } from '../../video-paths'
17 import { LiveQuotaStore } from '../live-quota-store'
18 import { LiveSegmentShaStore } from '../live-segment-sha-store'
19 import { buildConcatenatedName } from '../live-utils'
21 import memoizee = require('memoizee')
23 interface MuxingSessionEvents {
24 'master-playlist-created': ({ videoId: number }) => void
26 'bad-socket-health': ({ videoId: number }) => void
27 'duration-exceeded': ({ videoId: number }) => void
28 'quota-exceeded': ({ videoId: number }) => void
30 'ffmpeg-end': ({ videoId: number }) => void
31 'ffmpeg-error': ({ sessionId: string }) => void
33 'after-cleanup': ({ videoId: number }) => void
36 declare interface MuxingSession {
37 on<U extends keyof MuxingSessionEvents>(
38 event: U, listener: MuxingSessionEvents[U]
41 emit<U extends keyof MuxingSessionEvents>(
42 event: U, ...args: Parameters<MuxingSessionEvents[U]>
46 class MuxingSession extends EventEmitter {
48 private ffmpegCommand: FfmpegCommand
50 private readonly context: any
51 private readonly user: MUserId
52 private readonly sessionId: string
53 private readonly videoLive: MVideoLiveVideo
54 private readonly streamingPlaylist: MStreamingPlaylistVideo
55 private readonly rtmpUrl: string
56 private readonly fps: number
57 private readonly allResolutions: number[]
59 private readonly videoId: number
60 private readonly videoUUID: string
61 private readonly saveReplay: boolean
63 private readonly lTags: LoggerTagsFn
65 private segmentsToProcessPerPlaylist: { [playlistId: string]: string[] } = {}
67 private tsWatcher: chokidar.FSWatcher
68 private masterWatcher: chokidar.FSWatcher
70 private readonly isAbleToUploadVideoWithCache = memoizee((userId: number) => {
71 return isAbleToUploadVideo(userId, 1000)
72 }, { maxAge: MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD })
74 private readonly hasClientSocketInBadHealthWithCache = memoizee((sessionId: string) => {
75 return this.hasClientSocketInBadHealth(sessionId)
76 }, { maxAge: MEMOIZE_TTL.LIVE_CHECK_SOCKET_HEALTH })
78 constructor (options: {
82 videoLive: MVideoLiveVideo
83 streamingPlaylist: MStreamingPlaylistVideo
86 allResolutions: number[]
90 this.context = options.context
91 this.user = options.user
92 this.sessionId = options.sessionId
93 this.videoLive = options.videoLive
94 this.streamingPlaylist = options.streamingPlaylist
95 this.rtmpUrl = options.rtmpUrl
96 this.fps = options.fps
97 this.allResolutions = options.allResolutions
99 this.videoId = this.videoLive.Video.id
100 this.videoUUID = this.videoLive.Video.uuid
102 this.saveReplay = this.videoLive.saveReplay
104 this.lTags = loggerTagsFactory('live', this.sessionId, this.videoUUID)
110 const outPath = await this.prepareDirectories()
112 this.ffmpegCommand = CONFIG.LIVE.TRANSCODING.ENABLED
113 ? await getLiveTranscodingCommand({
114 rtmpUrl: this.rtmpUrl,
116 resolutions: this.allResolutions,
118 availableEncoders: VideoTranscodingProfilesManager.Instance.getAvailableEncoders(),
119 profile: CONFIG.LIVE.TRANSCODING.PROFILE
121 : getLiveMuxingCommand(this.rtmpUrl, outPath)
123 logger.info('Running live muxing/transcoding for %s.', this.videoUUID, this.lTags)
125 this.watchTSFiles(outPath)
126 this.watchMasterFile(outPath)
128 this.ffmpegCommand.on('error', (err, stdout, stderr) => {
129 this.onFFmpegError(err, stdout, stderr, outPath)
132 this.ffmpegCommand.on('end', () => this.onFFmpegEnded(outPath))
134 this.ffmpegCommand.run()
138 if (!this.ffmpegCommand) return false
140 this.ffmpegCommand.kill('SIGINT')
144 private onFFmpegError (err: any, stdout: string, stderr: string, outPath: string) {
145 this.onFFmpegEnded(outPath)
147 // Don't care that we killed the ffmpeg process
148 if (err?.message?.includes('Exiting normally')) return
150 logger.error('Live transcoding error.', { err, stdout, stderr, ...this.lTags })
152 this.emit('ffmpeg-error', ({ sessionId: this.sessionId }))
155 private onFFmpegEnded (outPath: string) {
156 logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', this.rtmpUrl, this.lTags)
159 // Wait latest segments generation, and close watchers
161 Promise.all([ this.tsWatcher.close(), this.masterWatcher.close() ])
163 // Process remaining segments hash
164 for (const key of Object.keys(this.segmentsToProcessPerPlaylist)) {
165 this.processSegments(outPath, this.segmentsToProcessPerPlaylist[key])
170 'Cannot close watchers of %s or process remaining hash segments.', outPath,
171 { err, ...this.lTags }
175 this.emit('after-cleanup', { videoId: this.videoId })
179 private watchMasterFile (outPath: string) {
180 this.masterWatcher = chokidar.watch(outPath + '/master.m3u8')
182 this.masterWatcher.on('add', async () => {
183 this.emit('master-playlist-created', { videoId: this.videoId })
185 this.masterWatcher.close()
186 .catch(err => logger.error('Cannot close master watcher of %s.', outPath, { err, ...this.lTags }))
190 private watchTSFiles (outPath: string) {
191 const startStreamDateTime = new Date().getTime()
193 this.tsWatcher = chokidar.watch(outPath + '/*.ts')
195 const playlistIdMatcher = /^([\d+])-/
197 const addHandler = async segmentPath => {
198 logger.debug('Live add handler of %s.', segmentPath, this.lTags)
200 const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
202 const segmentsToProcess = this.segmentsToProcessPerPlaylist[playlistId] || []
203 this.processSegments(outPath, segmentsToProcess)
205 this.segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
207 if (this.hasClientSocketInBadHealthWithCache(this.sessionId)) {
208 this.emit('bad-socket-health', { videoId: this.videoId })
212 // Duration constraint check
213 if (this.isDurationConstraintValid(startStreamDateTime) !== true) {
214 this.emit('duration-exceeded', { videoId: this.videoId })
218 // Check user quota if the user enabled replay saving
219 if (await this.isQuotaExceeded(segmentPath) === true) {
220 this.emit('quota-exceeded', { videoId: this.videoId })
224 const deleteHandler = segmentPath => LiveSegmentShaStore.Instance.removeSegmentSha(this.videoUUID, segmentPath)
226 this.tsWatcher.on('add', p => addHandler(p))
227 this.tsWatcher.on('unlink', p => deleteHandler(p))
230 private async isQuotaExceeded (segmentPath: string) {
231 if (this.saveReplay !== true) return false
234 const segmentStat = await stat(segmentPath)
236 LiveQuotaStore.Instance.addQuotaTo(this.user.id, this.videoLive.id, segmentStat.size)
238 const canUpload = await this.isAbleToUploadVideoWithCache(this.user.id)
240 return canUpload !== true
242 logger.error('Cannot stat %s or check quota of %d.', segmentPath, this.user.id, { err, ...this.lTags })
246 private createFiles () {
247 for (let i = 0; i < this.allResolutions.length; i++) {
248 const resolution = this.allResolutions[i]
250 const file = new VideoFileModel({
256 videoStreamingPlaylistId: this.streamingPlaylist.id
259 VideoFileModel.customUpsert(file, 'streaming-playlist', null)
260 .catch(err => logger.error('Cannot create file for live streaming.', { err, ...this.lTags }))
264 private async prepareDirectories () {
265 const outPath = getHLSDirectory(this.videoLive.Video)
266 await ensureDir(outPath)
268 const replayDirectory = join(outPath, VIDEO_LIVE.REPLAY_DIRECTORY)
270 if (this.videoLive.saveReplay === true) {
271 await ensureDir(replayDirectory)
277 private isDurationConstraintValid (streamingStartTime: number) {
278 const maxDuration = CONFIG.LIVE.MAX_DURATION
280 if (maxDuration < 0) return true
282 const now = new Date().getTime()
283 const max = streamingStartTime + maxDuration
288 private processSegments (hlsVideoPath: string, segmentPaths: string[]) {
289 Bluebird.mapSeries(segmentPaths, async previousSegment => {
290 // Add sha hash of previous segments, because ffmpeg should have finished generating them
291 await LiveSegmentShaStore.Instance.addSegmentSha(this.videoUUID, previousSegment)
293 if (this.saveReplay) {
294 await this.addSegmentToReplay(hlsVideoPath, previousSegment)
296 }).catch(err => logger.error('Cannot process segments in %s', hlsVideoPath, { err, ...this.lTags }))
299 private hasClientSocketInBadHealth (sessionId: string) {
300 const rtmpSession = this.context.sessions.get(sessionId)
303 logger.warn('Cannot get session %s to check players socket health.', sessionId, this.lTags)
307 for (const playerSessionId of rtmpSession.players) {
308 const playerSession = this.context.sessions.get(playerSessionId)
310 if (!playerSession) {
311 logger.error('Cannot get player session %s to check socket health.', playerSession, this.lTags)
315 if (playerSession.socket.writableLength > VIDEO_LIVE.MAX_SOCKET_WAITING_DATA) {
323 private async addSegmentToReplay (hlsVideoPath: string, segmentPath: string) {
324 const segmentName = basename(segmentPath)
325 const dest = join(hlsVideoPath, VIDEO_LIVE.REPLAY_DIRECTORY, buildConcatenatedName(segmentName))
328 const data = await readFile(segmentPath)
330 await appendFile(dest, data)
332 logger.error('Cannot copy segment %s to replay directory.', segmentPath, { err, ...this.lTags })
337 // ---------------------------------------------------------------------------