]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/live/shared/muxing-session.ts
Don't run ffmpeg twice for lives
[github/Chocobozzz/PeerTube.git] / server / lib / live / shared / muxing-session.ts
CommitLineData
41fb13c3
C
1import { mapSeries } from 'bluebird'
2import { FSWatcher, watch } from 'chokidar'
0c9668f7 3import { EventEmitter } from 'events'
8ebf2a5d 4import { appendFile, ensureDir, readFile, stat } from 'fs-extra'
b3ce3606 5import PQueue from 'p-queue'
8ebf2a5d 6import { basename, join } from 'path'
0c9668f7 7import { computeOutputFPS } from '@server/helpers/ffmpeg'
8ebf2a5d
C
8import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger'
9import { CONFIG } from '@server/initializers/config'
afb371d9 10import { MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE } from '@server/initializers/constants'
34023e12
C
11import {
12 removeHLSFileObjectStorageByPath,
13 storeHLSFileFromContent,
14 storeHLSFileFromFilename,
15 storeHLSFileFromPath
16} from '@server/lib/object-storage'
8ebf2a5d 17import { VideoFileModel } from '@server/models/video/video-file'
afb371d9 18import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
8ebf2a5d 19import { MStreamingPlaylistVideo, MUserId, MVideoLiveVideo } from '@server/types/models'
afb371d9
C
20import { VideoStorage, VideoStreamingPlaylistType } from '@shared/models'
21import {
22 generateHLSMasterPlaylistFilename,
23 generateHlsSha256SegmentsFilename,
24 getLiveDirectory,
25 getLiveReplayBaseDirectory
26} from '../../paths'
8ebf2a5d 27import { isAbleToUploadVideo } from '../../user'
8ebf2a5d
C
28import { LiveQuotaStore } from '../live-quota-store'
29import { LiveSegmentShaStore } from '../live-segment-sha-store'
0c9668f7
C
30import { buildConcatenatedName, getLiveSegmentTime } from '../live-utils'
31import { AbstractTranscodingWrapper, FFmpegTranscodingWrapper, RemoteTranscodingWrapper } from './transcoding-wrapper'
8ebf2a5d
C
32
33import memoizee = require('memoizee')
8ebf2a5d 34interface MuxingSessionEvents {
0c9668f7 35 'live-ready': (options: { videoUUID: string }) => void
8ebf2a5d 36
0c9668f7
C
37 'bad-socket-health': (options: { videoUUID: string }) => void
38 'duration-exceeded': (options: { videoUUID: string }) => void
39 'quota-exceeded': (options: { videoUUID: string }) => void
8ebf2a5d 40
0c9668f7
C
41 'transcoding-end': (options: { videoUUID: string }) => void
42 'transcoding-error': (options: { videoUUID: string }) => void
8ebf2a5d 43
0c9668f7 44 'after-cleanup': (options: { videoUUID: string }) => void
8ebf2a5d
C
45}
46
47declare interface MuxingSession {
48 on<U extends keyof MuxingSessionEvents>(
49 event: U, listener: MuxingSessionEvents[U]
50 ): this
51
52 emit<U extends keyof MuxingSessionEvents>(
53 event: U, ...args: Parameters<MuxingSessionEvents[U]>
54 ): boolean
55}
56
57class MuxingSession extends EventEmitter {
58
0c9668f7 59 private transcodingWrapper: AbstractTranscodingWrapper
8ebf2a5d
C
60
61 private readonly context: any
62 private readonly user: MUserId
63 private readonly sessionId: string
64 private readonly videoLive: MVideoLiveVideo
28705705
C
65
66 private readonly inputLocalUrl: string
67 private readonly inputPublicUrl: string
68
8ebf2a5d
C
69 private readonly fps: number
70 private readonly allResolutions: number[]
71
679c12e6
C
72 private readonly bitrate: number
73 private readonly ratio: number
74
1ce4256a
C
75 private readonly hasAudio: boolean
76
8ebf2a5d
C
77 private readonly videoUUID: string
78 private readonly saveReplay: boolean
79
4ec52d04
C
80 private readonly outDirectory: string
81 private readonly replayDirectory: string
82
8ebf2a5d
C
83 private readonly lTags: LoggerTagsFn
84
34023e12
C
85 // Path -> Queue
86 private readonly objectStorageSendQueues = new Map<string, PQueue>()
87
8ebf2a5d
C
88 private segmentsToProcessPerPlaylist: { [playlistId: string]: string[] } = {}
89
afb371d9
C
90 private streamingPlaylist: MStreamingPlaylistVideo
91 private liveSegmentShaStore: LiveSegmentShaStore
92
3a0c2a77 93 private filesWatcher: FSWatcher
cfd57d2c
C
94
95 private masterPlaylistCreated = false
96 private liveReady = false
8ebf2a5d 97
39d117a4
C
98 private aborted = false
99
8ebf2a5d
C
100 private readonly isAbleToUploadVideoWithCache = memoizee((userId: number) => {
101 return isAbleToUploadVideo(userId, 1000)
102 }, { maxAge: MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD })
103
104 private readonly hasClientSocketInBadHealthWithCache = memoizee((sessionId: string) => {
105 return this.hasClientSocketInBadHealth(sessionId)
106 }, { maxAge: MEMOIZE_TTL.LIVE_CHECK_SOCKET_HEALTH })
107
108 constructor (options: {
109 context: any
110 user: MUserId
111 sessionId: string
112 videoLive: MVideoLiveVideo
28705705
C
113
114 inputLocalUrl: string
115 inputPublicUrl: string
116
8ebf2a5d 117 fps: number
c826f34a 118 bitrate: number
679c12e6 119 ratio: number
8ebf2a5d 120 allResolutions: number[]
1ce4256a 121 hasAudio: boolean
8ebf2a5d
C
122 }) {
123 super()
124
125 this.context = options.context
126 this.user = options.user
127 this.sessionId = options.sessionId
128 this.videoLive = options.videoLive
28705705
C
129
130 this.inputLocalUrl = options.inputLocalUrl
131 this.inputPublicUrl = options.inputPublicUrl
132
8ebf2a5d 133 this.fps = options.fps
679c12e6 134
c826f34a 135 this.bitrate = options.bitrate
41085b15 136 this.ratio = options.ratio
679c12e6 137
1ce4256a
C
138 this.hasAudio = options.hasAudio
139
8ebf2a5d
C
140 this.allResolutions = options.allResolutions
141
8ebf2a5d
C
142 this.videoUUID = this.videoLive.Video.uuid
143
144 this.saveReplay = this.videoLive.saveReplay
145
4ec52d04
C
146 this.outDirectory = getLiveDirectory(this.videoLive.Video)
147 this.replayDirectory = join(getLiveReplayBaseDirectory(this.videoLive.Video), new Date().toISOString())
148
8ebf2a5d
C
149 this.lTags = loggerTagsFactory('live', this.sessionId, this.videoUUID)
150 }
151
152 async runMuxing () {
afb371d9
C
153 this.streamingPlaylist = await this.createLivePlaylist()
154
155 this.createLiveShaStore()
8ebf2a5d
C
156 this.createFiles()
157
4ec52d04 158 await this.prepareDirectories()
8ebf2a5d 159
0c9668f7 160 this.transcodingWrapper = this.buildTranscodingWrapper()
764b1a14 161
0c9668f7
C
162 this.transcodingWrapper.on('end', () => this.onTranscodedEnded())
163 this.transcodingWrapper.on('error', () => this.onTranscodingError())
764b1a14 164
0c9668f7 165 await this.transcodingWrapper.run()
8ebf2a5d 166
3a0c2a77
C
167 this.filesWatcher = watch(this.outDirectory, { depth: 0 })
168
b34ee7fa 169 this.watchMasterFile()
cfd57d2c 170 this.watchTSFiles()
8ebf2a5d
C
171 }
172
173 abort () {
0c9668f7 174 if (!this.transcodingWrapper) return
8ebf2a5d 175
39d117a4 176 this.aborted = true
0c9668f7 177 this.transcodingWrapper.abort()
609a4442
C
178 }
179
180 destroy () {
181 this.removeAllListeners()
182 this.isAbleToUploadVideoWithCache.clear()
183 this.hasClientSocketInBadHealthWithCache.clear()
8ebf2a5d
C
184 }
185
b34ee7fa 186 private watchMasterFile () {
3a0c2a77
C
187 this.filesWatcher.on('add', async path => {
188 if (path !== join(this.outDirectory, this.streamingPlaylist.playlistFilename)) return
189 if (this.masterPlaylistCreated === true) return
8ebf2a5d 190
afb371d9
C
191 try {
192 if (this.streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) {
cfd57d2c
C
193 const url = await storeHLSFileFromFilename(this.streamingPlaylist, this.streamingPlaylist.playlistFilename)
194
195 this.streamingPlaylist.playlistUrl = url
cfd57d2c 196 }
afb371d9
C
197
198 this.streamingPlaylist.assignP2PMediaLoaderInfoHashes(this.videoLive.Video, this.allResolutions)
199
200 await this.streamingPlaylist.save()
201 } catch (err) {
202 logger.error('Cannot update streaming playlist.', { err, ...this.lTags() })
cfd57d2c
C
203 }
204
205 this.masterPlaylistCreated = true
8ebf2a5d 206
0c9668f7 207 logger.info('Master playlist file for %s has been created', this.videoUUID, this.lTags())
8ebf2a5d
C
208 })
209 }
210
b34ee7fa 211 private watchTSFiles () {
8ebf2a5d
C
212 const startStreamDateTime = new Date().getTime()
213
75b7117f 214 const addHandler = async (segmentPath: string) => {
3a0c2a77
C
215 if (segmentPath.endsWith('.ts') !== true) return
216
217 logger.debug('Live add handler of TS file %s.', segmentPath, this.lTags())
8ebf2a5d 218
34023e12 219 const playlistId = this.getPlaylistIdFromTS(segmentPath)
8ebf2a5d
C
220
221 const segmentsToProcess = this.segmentsToProcessPerPlaylist[playlistId] || []
b34ee7fa 222 this.processSegments(segmentsToProcess)
8ebf2a5d
C
223
224 this.segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
225
226 if (this.hasClientSocketInBadHealthWithCache(this.sessionId)) {
0c9668f7 227 this.emit('bad-socket-health', { videoUUID: this.videoUUID })
8ebf2a5d
C
228 return
229 }
230
231 // Duration constraint check
232 if (this.isDurationConstraintValid(startStreamDateTime) !== true) {
0c9668f7 233 this.emit('duration-exceeded', { videoUUID: this.videoUUID })
8ebf2a5d
C
234 return
235 }
236
237 // Check user quota if the user enabled replay saving
238 if (await this.isQuotaExceeded(segmentPath) === true) {
0c9668f7 239 this.emit('quota-exceeded', { videoUUID: this.videoUUID })
8ebf2a5d
C
240 }
241 }
242
cfd57d2c 243 const deleteHandler = async (segmentPath: string) => {
3a0c2a77
C
244 if (segmentPath.endsWith('.ts') !== true) return
245
246 logger.debug('Live delete handler of TS file %s.', segmentPath, this.lTags())
247
cfd57d2c
C
248 try {
249 await this.liveSegmentShaStore.removeSegmentSha(segmentPath)
250 } catch (err) {
251 logger.warn('Cannot remove segment sha %s from sha store', segmentPath, { err, ...this.lTags() })
252 }
253
254 if (this.streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) {
255 try {
aa887096 256 await removeHLSFileObjectStorageByPath(this.streamingPlaylist, segmentPath)
cfd57d2c
C
257 } catch (err) {
258 logger.error('Cannot remove segment %s from object storage', segmentPath, { err, ...this.lTags() })
259 }
260 }
261 }
8ebf2a5d 262
3a0c2a77
C
263 this.filesWatcher.on('add', p => addHandler(p))
264 this.filesWatcher.on('unlink', p => deleteHandler(p))
8ebf2a5d
C
265 }
266
267 private async isQuotaExceeded (segmentPath: string) {
268 if (this.saveReplay !== true) return false
29399256 269 if (this.aborted) return false
8ebf2a5d
C
270
271 try {
272 const segmentStat = await stat(segmentPath)
273
274 LiveQuotaStore.Instance.addQuotaTo(this.user.id, this.videoLive.id, segmentStat.size)
275
276 const canUpload = await this.isAbleToUploadVideoWithCache(this.user.id)
277
278 return canUpload !== true
279 } catch (err) {
4c6757f2 280 logger.error('Cannot stat %s or check quota of %d.', segmentPath, this.user.id, { err, ...this.lTags() })
8ebf2a5d
C
281 }
282 }
283
284 private createFiles () {
285 for (let i = 0; i < this.allResolutions.length; i++) {
286 const resolution = this.allResolutions[i]
287
288 const file = new VideoFileModel({
289 resolution,
290 size: -1,
291 extname: '.ts',
292 infoHash: null,
293 fps: this.fps,
cfd57d2c 294 storage: this.streamingPlaylist.storage,
8ebf2a5d
C
295 videoStreamingPlaylistId: this.streamingPlaylist.id
296 })
297
298 VideoFileModel.customUpsert(file, 'streaming-playlist', null)
4c6757f2 299 .catch(err => logger.error('Cannot create file for live streaming.', { err, ...this.lTags() }))
8ebf2a5d
C
300 }
301 }
302
303 private async prepareDirectories () {
4ec52d04 304 await ensureDir(this.outDirectory)
8ebf2a5d
C
305
306 if (this.videoLive.saveReplay === true) {
4ec52d04 307 await ensureDir(this.replayDirectory)
8ebf2a5d 308 }
8ebf2a5d
C
309 }
310
311 private isDurationConstraintValid (streamingStartTime: number) {
312 const maxDuration = CONFIG.LIVE.MAX_DURATION
313 // No limit
314 if (maxDuration < 0) return true
315
316 const now = new Date().getTime()
317 const max = streamingStartTime + maxDuration
318
319 return now <= max
320 }
321
b34ee7fa 322 private processSegments (segmentPaths: string[]) {
cfd57d2c
C
323 mapSeries(segmentPaths, previousSegment => this.processSegment(previousSegment))
324 .catch(err => {
325 if (this.aborted) return
326
327 logger.error('Cannot process segments', { err, ...this.lTags() })
328 })
329 }
8ebf2a5d 330
cfd57d2c
C
331 private async processSegment (segmentPath: string) {
332 // Add sha hash of previous segments, because ffmpeg should have finished generating them
333 await this.liveSegmentShaStore.addSegmentSha(segmentPath)
334
335 if (this.saveReplay) {
336 await this.addSegmentToReplay(segmentPath)
337 }
338
339 if (this.streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) {
340 try {
341 await storeHLSFileFromPath(this.streamingPlaylist, segmentPath)
34023e12
C
342
343 await this.processM3U8ToObjectStorage(segmentPath)
cfd57d2c
C
344 } catch (err) {
345 logger.error('Cannot store TS segment %s in object storage', segmentPath, { err, ...this.lTags() })
8ebf2a5d 346 }
cfd57d2c 347 }
29399256 348
cfd57d2c
C
349 // Master playlist and segment JSON file are created, live is ready
350 if (this.masterPlaylistCreated && !this.liveReady) {
351 this.liveReady = true
352
0c9668f7 353 this.emit('live-ready', { videoUUID: this.videoUUID })
cfd57d2c 354 }
8ebf2a5d
C
355 }
356
34023e12
C
357 private async processM3U8ToObjectStorage (segmentPath: string) {
358 const m3u8Path = join(this.outDirectory, this.getPlaylistNameFromTS(segmentPath))
359
360 logger.debug('Process M3U8 file %s.', m3u8Path, this.lTags())
361
362 const segmentName = basename(segmentPath)
363
364 const playlistContent = await readFile(m3u8Path, 'utf-8')
365 // Remove new chunk references, that will be processed later
366 const filteredPlaylistContent = playlistContent.substring(0, playlistContent.lastIndexOf(segmentName) + segmentName.length) + '\n'
367
368 try {
369 if (!this.objectStorageSendQueues.has(m3u8Path)) {
370 this.objectStorageSendQueues.set(m3u8Path, new PQueue({ concurrency: 1 }))
371 }
372
373 const queue = this.objectStorageSendQueues.get(m3u8Path)
374 await queue.add(() => storeHLSFileFromContent(this.streamingPlaylist, m3u8Path, filteredPlaylistContent))
375 } catch (err) {
376 logger.error('Cannot store in object storage m3u8 file %s', m3u8Path, { err, ...this.lTags() })
377 }
378 }
379
0c9668f7
C
380 private onTranscodingError () {
381 this.emit('transcoding-error', ({ videoUUID: this.videoUUID }))
382 }
383
384 private onTranscodedEnded () {
385 this.emit('transcoding-end', ({ videoUUID: this.videoUUID }))
386
28705705 387 logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', this.inputLocalUrl, this.lTags())
0c9668f7
C
388
389 setTimeout(() => {
390 // Wait latest segments generation, and close watchers
391
3a0c2a77
C
392 const promise = this.filesWatcher?.close() || Promise.resolve()
393 promise
0c9668f7
C
394 .then(() => {
395 // Process remaining segments hash
396 for (const key of Object.keys(this.segmentsToProcessPerPlaylist)) {
397 this.processSegments(this.segmentsToProcessPerPlaylist[key])
398 }
399 })
400 .catch(err => {
401 logger.error(
402 'Cannot close watchers of %s or process remaining hash segments.', this.outDirectory,
403 { err, ...this.lTags() }
404 )
405 })
406
407 this.emit('after-cleanup', { videoUUID: this.videoUUID })
408 }, 1000)
409 }
410
8ebf2a5d
C
411 private hasClientSocketInBadHealth (sessionId: string) {
412 const rtmpSession = this.context.sessions.get(sessionId)
413
414 if (!rtmpSession) {
4c6757f2 415 logger.warn('Cannot get session %s to check players socket health.', sessionId, this.lTags())
8ebf2a5d
C
416 return
417 }
418
419 for (const playerSessionId of rtmpSession.players) {
420 const playerSession = this.context.sessions.get(playerSessionId)
421
422 if (!playerSession) {
4c6757f2 423 logger.error('Cannot get player session %s to check socket health.', playerSession, this.lTags())
8ebf2a5d
C
424 continue
425 }
426
427 if (playerSession.socket.writableLength > VIDEO_LIVE.MAX_SOCKET_WAITING_DATA) {
428 return true
429 }
430 }
431
432 return false
433 }
434
b34ee7fa 435 private async addSegmentToReplay (segmentPath: string) {
8ebf2a5d 436 const segmentName = basename(segmentPath)
4ec52d04 437 const dest = join(this.replayDirectory, buildConcatenatedName(segmentName))
8ebf2a5d
C
438
439 try {
440 const data = await readFile(segmentPath)
441
442 await appendFile(dest, data)
443 } catch (err) {
4c6757f2 444 logger.error('Cannot copy segment %s to replay directory.', segmentPath, { err, ...this.lTags() })
8ebf2a5d
C
445 }
446 }
afb371d9
C
447
448 private async createLivePlaylist (): Promise<MStreamingPlaylistVideo> {
449 const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(this.videoLive.Video)
450
451 playlist.playlistFilename = generateHLSMasterPlaylistFilename(true)
452 playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(true)
453
454 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
455 playlist.type = VideoStreamingPlaylistType.HLS
456
457 playlist.storage = CONFIG.OBJECT_STORAGE.ENABLED
458 ? VideoStorage.OBJECT_STORAGE
459 : VideoStorage.FILE_SYSTEM
460
461 return playlist.save()
462 }
463
464 private createLiveShaStore () {
465 this.liveSegmentShaStore = new LiveSegmentShaStore({
466 videoUUID: this.videoLive.Video.uuid,
467 sha256Path: join(this.outDirectory, this.streamingPlaylist.segmentsSha256Filename),
468 streamingPlaylist: this.streamingPlaylist,
469 sendToObjectStorage: CONFIG.OBJECT_STORAGE.ENABLED
470 })
471 }
0c9668f7
C
472
473 private buildTranscodingWrapper () {
474 const options = {
475 streamingPlaylist: this.streamingPlaylist,
476 videoLive: this.videoLive,
477
478 lTags: this.lTags,
479
28705705
C
480 inputLocalUrl: this.inputLocalUrl,
481 inputPublicUrl: this.inputPublicUrl,
0c9668f7
C
482
483 toTranscode: this.allResolutions.map(resolution => ({
484 resolution,
485 fps: computeOutputFPS({ inputFPS: this.fps, resolution })
486 })),
487
488 fps: this.fps,
489 bitrate: this.bitrate,
490 ratio: this.ratio,
491 hasAudio: this.hasAudio,
492
493 segmentListSize: VIDEO_LIVE.SEGMENTS_LIST_SIZE,
494 segmentDuration: getLiveSegmentTime(this.videoLive.latencyMode),
495
496 outDirectory: this.outDirectory
497 }
498
499 return CONFIG.LIVE.TRANSCODING.ENABLED && CONFIG.LIVE.TRANSCODING.REMOTE_RUNNERS.ENABLED
500 ? new RemoteTranscodingWrapper(options)
501 : new FFmpegTranscodingWrapper(options)
502 }
34023e12
C
503
504 private getPlaylistIdFromTS (segmentPath: string) {
505 const playlistIdMatcher = /^([\d+])-/
506
507 return basename(segmentPath).match(playlistIdMatcher)[1]
508 }
509
510 private getPlaylistNameFromTS (segmentPath: string) {
511 return `${this.getPlaylistIdFromTS(segmentPath)}.m3u8`
512 }
8ebf2a5d
C
513}
514
515// ---------------------------------------------------------------------------
516
517export {
518 MuxingSession
519}