]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/live-manager.ts
Try to fix live freeze
[github/Chocobozzz/PeerTube.git] / server / lib / live-manager.ts
CommitLineData
c6c0fa6c 1
c6c0fa6c
C
2import * as chokidar from 'chokidar'
3import { FfmpegCommand } from 'fluent-ffmpeg'
937581b8
C
4import { copy, ensureDir, stat } from 'fs-extra'
5import { basename, join } from 'path'
c655c9ef 6import { isTestInstance } from '@server/helpers/core-utils'
9252a33d 7import { getLiveMuxingCommand, getLiveTranscodingCommand } from '@server/helpers/ffmpeg-utils'
daf6e480 8import { computeResolutionsToTranscode, getVideoFileFPS, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
c6c0fa6c
C
9import { logger } from '@server/helpers/logger'
10import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config'
e4bf7856 11import { MEMOIZE_TTL, P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE, VIEW_LIFETIME, WEBSERVER } from '@server/initializers/constants'
fb719404 12import { UserModel } from '@server/models/account/user'
a5cf76af 13import { VideoModel } from '@server/models/video/video'
c6c0fa6c
C
14import { VideoFileModel } from '@server/models/video/video-file'
15import { VideoLiveModel } from '@server/models/video/video-live'
16import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
b5b68755 17import { MStreamingPlaylist, MUserId, MVideoLive, MVideoLiveVideo } from '@server/types/models'
c6c0fa6c 18import { VideoState, VideoStreamingPlaylistType } from '@shared/models'
a5cf76af 19import { federateVideoIfNeeded } from './activitypub/videos'
c6c0fa6c 20import { buildSha256Segment } from './hls'
a5cf76af 21import { JobQueue } from './job-queue'
bb4ba6d9 22import { cleanupLive } from './job-queue/handlers/video-live-ending'
a5cf76af 23import { PeerTubeSocket } from './peertube-socket'
fb719404 24import { isAbleToUploadVideo } from './user'
c6c0fa6c 25import { getHLSDirectory } from './video-paths'
5a547f69 26import { availableEncoders } from './video-transcoding-profiles'
c6c0fa6c 27
fb719404 28import memoizee = require('memoizee')
6cddd97d 29
c6c0fa6c
C
30const NodeRtmpServer = require('node-media-server/node_rtmp_server')
31const context = require('node-media-server/node_core_ctx')
32const nodeMediaServerLogger = require('node-media-server/node_core_logger')
33
34// Disable node media server logs
35nodeMediaServerLogger.setLogType(0)
36
37const config = {
38 rtmp: {
39 port: CONFIG.LIVE.RTMP.PORT,
40 chunk_size: VIDEO_LIVE.RTMP.CHUNK_SIZE,
41 gop_cache: VIDEO_LIVE.RTMP.GOP_CACHE,
42 ping: VIDEO_LIVE.RTMP.PING,
43 ping_timeout: VIDEO_LIVE.RTMP.PING_TIMEOUT
44 },
45 transcoding: {
46 ffmpeg: 'ffmpeg'
47 }
48}
49
c6c0fa6c
C
50class LiveManager {
51
52 private static instance: LiveManager
53
54 private readonly transSessions = new Map<string, FfmpegCommand>()
a5cf76af 55 private readonly videoSessions = new Map<number, string>()
e4bf7856
C
56 // Values are Date().getTime()
57 private readonly watchersPerVideo = new Map<number, number[]>()
c6c0fa6c 58 private readonly segmentsSha256 = new Map<string, Map<string, string>>()
fb719404
C
59 private readonly livesPerUser = new Map<number, { liveId: number, videoId: number, size: number }[]>()
60
61 private readonly isAbleToUploadVideoWithCache = memoizee((userId: number) => {
62 return isAbleToUploadVideo(userId, 1000)
63 }, { maxAge: MEMOIZE_TTL.LIVE_ABLE_TO_UPLOAD })
c6c0fa6c 64
c6c0fa6c
C
65 private rtmpServer: any
66
67 private constructor () {
68 }
69
70 init () {
a5cf76af
C
71 const events = this.getContext().nodeEvent
72 events.on('postPublish', (sessionId: string, streamPath: string) => {
c6c0fa6c
C
73 logger.debug('RTMP received stream', { id: sessionId, streamPath })
74
75 const splittedPath = streamPath.split('/')
76 if (splittedPath.length !== 3 || splittedPath[1] !== VIDEO_LIVE.RTMP.BASE_PATH) {
77 logger.warn('Live path is incorrect.', { streamPath })
78 return this.abortSession(sessionId)
79 }
80
81 this.handleSession(sessionId, streamPath, splittedPath[2])
82 .catch(err => logger.error('Cannot handle sessions.', { err }))
83 })
84
a5cf76af 85 events.on('donePublish', sessionId => {
284ef529 86 logger.info('Live session ended.', { sessionId })
c6c0fa6c
C
87 })
88
c6c0fa6c
C
89 registerConfigChangedHandler(() => {
90 if (!this.rtmpServer && CONFIG.LIVE.ENABLED === true) {
91 this.run()
92 return
93 }
94
95 if (this.rtmpServer && CONFIG.LIVE.ENABLED === false) {
96 this.stop()
97 }
98 })
e4bf7856 99
5c0904fc
C
100 // Cleanup broken lives, that were terminated by a server restart for example
101 this.handleBrokenLives()
102 .catch(err => logger.error('Cannot handle broken lives.', { err }))
103
e4bf7856 104 setInterval(() => this.updateLiveViews(), VIEW_LIFETIME.LIVE)
c6c0fa6c
C
105 }
106
107 run () {
3cabf353 108 logger.info('Running RTMP server on port %d', config.rtmp.port)
c6c0fa6c
C
109
110 this.rtmpServer = new NodeRtmpServer(config)
111 this.rtmpServer.run()
112 }
113
114 stop () {
115 logger.info('Stopping RTMP server.')
116
117 this.rtmpServer.stop()
118 this.rtmpServer = undefined
119 }
120
e4bf7856
C
121 isRunning () {
122 return !!this.rtmpServer
123 }
124
c6c0fa6c
C
125 getSegmentsSha256 (videoUUID: string) {
126 return this.segmentsSha256.get(videoUUID)
127 }
128
a5cf76af
C
129 stopSessionOf (videoId: number) {
130 const sessionId = this.videoSessions.get(videoId)
131 if (!sessionId) return
132
97969c4e 133 this.videoSessions.delete(videoId)
a5cf76af 134 this.abortSession(sessionId)
a5cf76af
C
135 }
136
68e70a74
C
137 getLiveQuotaUsedByUser (userId: number) {
138 const currentLives = this.livesPerUser.get(userId)
139 if (!currentLives) return 0
140
141 return currentLives.reduce((sum, obj) => sum + obj.size, 0)
142 }
143
e4bf7856
C
144 addViewTo (videoId: number) {
145 if (this.videoSessions.has(videoId) === false) return
146
147 let watchers = this.watchersPerVideo.get(videoId)
148
149 if (!watchers) {
150 watchers = []
151 this.watchersPerVideo.set(videoId, watchers)
152 }
153
154 watchers.push(new Date().getTime())
155 }
156
bb4ba6d9
C
157 cleanupShaSegments (videoUUID: string) {
158 this.segmentsSha256.delete(videoUUID)
159 }
160
c6c0fa6c
C
161 private getContext () {
162 return context
163 }
164
165 private abortSession (id: string) {
166 const session = this.getContext().sessions.get(id)
284ef529
C
167 if (session) {
168 session.stop()
169 this.getContext().sessions.delete(id)
170 }
c6c0fa6c
C
171
172 const transSession = this.transSessions.get(id)
284ef529
C
173 if (transSession) {
174 transSession.kill('SIGINT')
175 this.transSessions.delete(id)
176 }
c6c0fa6c
C
177 }
178
179 private async handleSession (sessionId: string, streamPath: string, streamKey: string) {
180 const videoLive = await VideoLiveModel.loadByStreamKey(streamKey)
181 if (!videoLive) {
182 logger.warn('Unknown live video with stream key %s.', streamKey)
183 return this.abortSession(sessionId)
184 }
185
186 const video = videoLive.Video
a5cf76af
C
187 if (video.isBlacklisted()) {
188 logger.warn('Video is blacklisted. Refusing stream %s.', streamKey)
189 return this.abortSession(sessionId)
190 }
191
bb4ba6d9
C
192 // Cleanup old potential live files (could happen with a permanent live)
193 this.cleanupShaSegments(video.uuid)
194
195 const oldStreamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
196 if (oldStreamingPlaylist) {
197 await cleanupLive(video, oldStreamingPlaylist)
198 }
199
a5cf76af
C
200 this.videoSessions.set(video.id, sessionId)
201
c6c0fa6c
C
202 const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid)
203
204 const session = this.getContext().sessions.get(sessionId)
68e70a74
C
205 const rtmpUrl = 'rtmp://127.0.0.1:' + config.rtmp.port + streamPath
206
207 const [ resolutionResult, fps ] = await Promise.all([
208 getVideoFileResolution(rtmpUrl),
209 getVideoFileFPS(rtmpUrl)
210 ])
211
c6c0fa6c 212 const resolutionsEnabled = CONFIG.LIVE.TRANSCODING.ENABLED
68e70a74 213 ? computeResolutionsToTranscode(resolutionResult.videoFileResolution, 'live')
c6c0fa6c
C
214 : []
215
6297bae0
C
216 const allResolutions = resolutionsEnabled.concat([ session.videoHeight ])
217
218 logger.info('Will mux/transcode live video of original resolution %d.', session.videoHeight, { allResolutions })
c6c0fa6c
C
219
220 const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({
221 videoId: video.id,
222 playlistUrl,
223 segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive),
6297bae0 224 p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, allResolutions),
c6c0fa6c
C
225 p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION,
226
227 type: VideoStreamingPlaylistType.HLS
228 }, { returning: true }) as [ MStreamingPlaylist, boolean ]
229
c6c0fa6c
C
230 return this.runMuxing({
231 sessionId,
232 videoLive,
233 playlist: videoStreamingPlaylist,
68e70a74
C
234 rtmpUrl,
235 fps,
6297bae0 236 allResolutions
c6c0fa6c
C
237 })
238 }
239
240 private async runMuxing (options: {
241 sessionId: string
242 videoLive: MVideoLiveVideo
243 playlist: MStreamingPlaylist
68e70a74
C
244 rtmpUrl: string
245 fps: number
6297bae0 246 allResolutions: number[]
c6c0fa6c 247 }) {
6297bae0 248 const { sessionId, videoLive, playlist, allResolutions, fps, rtmpUrl } = options
fb719404 249 const startStreamDateTime = new Date().getTime()
c6c0fa6c 250
fb719404
C
251 const user = await UserModel.loadByLiveId(videoLive.id)
252 if (!this.livesPerUser.has(user.id)) {
253 this.livesPerUser.set(user.id, [])
254 }
255
256 const currentUserLive = { liveId: videoLive.id, videoId: videoLive.videoId, size: 0 }
257 const livesOfUser = this.livesPerUser.get(user.id)
258 livesOfUser.push(currentUserLive)
259
c6c0fa6c
C
260 for (let i = 0; i < allResolutions.length; i++) {
261 const resolution = allResolutions[i]
262
263 VideoFileModel.upsert({
264 resolution,
265 size: -1,
266 extname: '.ts',
267 infoHash: null,
bd54ad19 268 fps,
c6c0fa6c
C
269 videoStreamingPlaylistId: playlist.id
270 }).catch(err => {
271 logger.error('Cannot create file for live streaming.', { err })
272 })
273 }
274
275 const outPath = getHLSDirectory(videoLive.Video)
276 await ensureDir(outPath)
277
937581b8
C
278 const replayDirectory = join(outPath, VIDEO_LIVE.REPLAY_DIRECTORY)
279
280 if (videoLive.saveReplay === true) {
281 await ensureDir(replayDirectory)
282 }
283
68e70a74 284 const videoUUID = videoLive.Video.uuid
fb719404 285
c6c0fa6c 286 const ffmpegExec = CONFIG.LIVE.TRANSCODING.ENABLED
5a547f69
C
287 ? await getLiveTranscodingCommand({
288 rtmpUrl,
289 outPath,
33ff70ba 290 resolutions: allResolutions,
5a547f69 291 fps,
5a547f69
C
292 availableEncoders,
293 profile: 'default'
294 })
937581b8 295 : getLiveMuxingCommand(rtmpUrl, outPath)
c6c0fa6c 296
68e70a74 297 logger.info('Running live muxing/transcoding for %s.', videoUUID)
c6c0fa6c
C
298 this.transSessions.set(sessionId, ffmpegExec)
299
a5cf76af
C
300 const tsWatcher = chokidar.watch(outPath + '/*.ts')
301
786b855a
C
302 const segmentsToProcessPerPlaylist: { [playlistId: string]: string[] } = {}
303 const playlistIdMatcher = /^([\d+])-/
fb719404 304
937581b8 305 const processSegments = (segmentsToProcess: string[]) => {
210856a7
C
306 // Add sha hash of previous segments, because ffmpeg should have finished generating them
307 for (const previousSegment of segmentsToProcess) {
308 this.addSegmentSha(videoUUID, previousSegment)
309 .catch(err => logger.error('Cannot add sha segment of video %s -> %s.', videoUUID, previousSegment, { err }))
937581b8
C
310
311 if (videoLive.saveReplay) {
312 const segmentName = basename(previousSegment)
313
314 copy(previousSegment, join(outPath, VIDEO_LIVE.REPLAY_DIRECTORY, segmentName))
315 .catch(err => logger.error('Cannot copy segment %s to repay directory.', previousSegment, { err }))
316 }
210856a7 317 }
6bff8ce2
C
318 }
319
320 const addHandler = segmentPath => {
321 logger.debug('Live add handler of %s.', segmentPath)
322
323 const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
324
325 const segmentsToProcess = segmentsToProcessPerPlaylist[playlistId] || []
937581b8 326 processSegments(segmentsToProcess)
210856a7 327
786b855a 328 segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
fb719404 329
210856a7 330 // Duration constraint check
fb719404 331 if (this.isDurationConstraintValid(startStreamDateTime) !== true) {
97969c4e
C
332 logger.info('Stopping session of %s: max duration exceeded.', videoUUID)
333
fb719404
C
334 this.stopSessionOf(videoLive.videoId)
335 }
336
97969c4e 337 // Check user quota if the user enabled replay saving
fb719404
C
338 if (videoLive.saveReplay === true) {
339 stat(segmentPath)
340 .then(segmentStat => {
341 currentUserLive.size += segmentStat.size
342 })
343 .then(() => this.isQuotaConstraintValid(user, videoLive))
344 .then(quotaValid => {
345 if (quotaValid !== true) {
97969c4e
C
346 logger.info('Stopping session of %s: user quota exceeded.', videoUUID)
347
fb719404
C
348 this.stopSessionOf(videoLive.videoId)
349 }
350 })
351 .catch(err => logger.error('Cannot stat %s or check quota of %d.', segmentPath, user.id, { err }))
352 }
a5cf76af
C
353 }
354
210856a7 355 const deleteHandler = segmentPath => this.removeSegmentSha(videoUUID, segmentPath)
a5cf76af 356
fb719404 357 tsWatcher.on('add', p => addHandler(p))
a5cf76af
C
358 tsWatcher.on('unlink', p => deleteHandler(p))
359
360 const masterWatcher = chokidar.watch(outPath + '/master.m3u8')
361 masterWatcher.on('add', async () => {
362 try {
363 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoLive.videoId)
364
365 video.state = VideoState.PUBLISHED
366 await video.save()
367 videoLive.Video = video
368
501af82d
C
369 setTimeout(() => {
370 federateVideoIfNeeded(video, false)
371 .catch(err => logger.error('Cannot federate live video %s.', video.url, { err }))
372
373 PeerTubeSocket.Instance.sendVideoLiveNewState(video)
374 }, VIDEO_LIVE.SEGMENT_TIME_SECONDS * 1000 * VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION)
a5cf76af 375
a5cf76af 376 } catch (err) {
501af82d 377 logger.error('Cannot save/federate live video %d.', videoLive.videoId, { err })
a5cf76af
C
378 } finally {
379 masterWatcher.close()
380 .catch(err => logger.error('Cannot close master watcher of %s.', outPath, { err }))
381 }
382 })
383
c6c0fa6c 384 const onFFmpegEnded = () => {
68e70a74 385 logger.info('RTMP transmuxing for video %s ended. Scheduling cleanup', rtmpUrl)
c6c0fa6c 386
284ef529 387 this.transSessions.delete(sessionId)
bb4ba6d9 388
e4bf7856 389 this.watchersPerVideo.delete(videoLive.videoId)
bb4ba6d9
C
390 this.videoSessions.delete(videoLive.videoId)
391
392 const newLivesPerUser = this.livesPerUser.get(user.id)
393 .filter(o => o.liveId !== videoLive.id)
394 this.livesPerUser.set(user.id, newLivesPerUser)
284ef529 395
6bff8ce2
C
396 setTimeout(() => {
397 // Wait latest segments generation, and close watchers
398
399 Promise.all([ tsWatcher.close(), masterWatcher.close() ])
8c666c44
C
400 .then(() => {
401 // Process remaining segments hash
402 for (const key of Object.keys(segmentsToProcessPerPlaylist)) {
403 processSegments(segmentsToProcessPerPlaylist[key])
404 }
405 })
406 .catch(err => logger.error('Cannot close watchers of %s or process remaining hash segments.', outPath, { err }))
6bff8ce2
C
407
408 this.onEndTransmuxing(videoLive.Video.id)
409 .catch(err => logger.error('Error in closed transmuxing.', { err }))
410 }, 1000)
c6c0fa6c
C
411 }
412
413 ffmpegExec.on('error', (err, stdout, stderr) => {
414 onFFmpegEnded()
415
416 // Don't care that we killed the ffmpeg process
97969c4e 417 if (err?.message?.includes('Exiting normally')) return
c6c0fa6c
C
418
419 logger.error('Live transcoding error.', { err, stdout, stderr })
77e9f859
C
420
421 this.abortSession(sessionId)
c6c0fa6c
C
422 })
423
424 ffmpegExec.on('end', () => onFFmpegEnded())
9252a33d
C
425
426 ffmpegExec.run()
c6c0fa6c
C
427 }
428
fb719404 429 private async onEndTransmuxing (videoId: number, cleanupNow = false) {
a5cf76af
C
430 try {
431 const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
432 if (!fullVideo) return
c6c0fa6c 433
bb4ba6d9
C
434 const live = await VideoLiveModel.loadByVideoId(videoId)
435
436 if (!live.permanentLive) {
437 JobQueue.Instance.createJob({
438 type: 'video-live-ending',
439 payload: {
440 videoId: fullVideo.id
441 }
442 }, { delay: cleanupNow ? 0 : VIDEO_LIVE.CLEANUP_DELAY })
443
444 fullVideo.state = VideoState.LIVE_ENDED
445 } else {
446 fullVideo.state = VideoState.WAITING_FOR_LIVE
447 }
c6c0fa6c 448
a5cf76af 449 await fullVideo.save()
c6c0fa6c 450
a5cf76af 451 PeerTubeSocket.Instance.sendVideoLiveNewState(fullVideo)
c6c0fa6c 452
a5cf76af
C
453 await federateVideoIfNeeded(fullVideo, false)
454 } catch (err) {
455 logger.error('Cannot save/federate new video state of live streaming.', { err })
456 }
c6c0fa6c
C
457 }
458
210856a7
C
459 private async addSegmentSha (videoUUID: string, segmentPath: string) {
460 const segmentName = basename(segmentPath)
461 logger.debug('Adding live sha segment %s.', segmentPath)
c6c0fa6c 462
210856a7 463 const shaResult = await buildSha256Segment(segmentPath)
c6c0fa6c 464
210856a7
C
465 if (!this.segmentsSha256.has(videoUUID)) {
466 this.segmentsSha256.set(videoUUID, new Map())
c6c0fa6c
C
467 }
468
210856a7 469 const filesMap = this.segmentsSha256.get(videoUUID)
c6c0fa6c
C
470 filesMap.set(segmentName, shaResult)
471 }
472
210856a7
C
473 private removeSegmentSha (videoUUID: string, segmentPath: string) {
474 const segmentName = basename(segmentPath)
c6c0fa6c 475
210856a7 476 logger.debug('Removing live sha segment %s.', segmentPath)
c6c0fa6c 477
210856a7 478 const filesMap = this.segmentsSha256.get(videoUUID)
c6c0fa6c 479 if (!filesMap) {
210856a7 480 logger.warn('Unknown files map to remove sha for %s.', videoUUID)
c6c0fa6c
C
481 return
482 }
483
484 if (!filesMap.has(segmentName)) {
210856a7 485 logger.warn('Unknown segment in files map for video %s and segment %s.', videoUUID, segmentPath)
c6c0fa6c
C
486 return
487 }
488
489 filesMap.delete(segmentName)
490 }
491
fb719404
C
492 private isDurationConstraintValid (streamingStartTime: number) {
493 const maxDuration = CONFIG.LIVE.MAX_DURATION
494 // No limit
495 if (maxDuration === null) return true
496
497 const now = new Date().getTime()
498 const max = streamingStartTime + maxDuration
499
500 return now <= max
501 }
502
503 private async isQuotaConstraintValid (user: MUserId, live: MVideoLive) {
504 if (live.saveReplay !== true) return true
505
506 return this.isAbleToUploadVideoWithCache(user.id)
507 }
508
e4bf7856
C
509 private async updateLiveViews () {
510 if (!this.isRunning()) return
511
c655c9ef 512 if (!isTestInstance()) logger.info('Updating live video views.')
e4bf7856
C
513
514 for (const videoId of this.watchersPerVideo.keys()) {
515 const notBefore = new Date().getTime() - VIEW_LIFETIME.LIVE
516
517 const watchers = this.watchersPerVideo.get(videoId)
518
519 const numWatchers = watchers.length
520
521 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
522 video.views = numWatchers
523 await video.save()
524
525 await federateVideoIfNeeded(video, false)
526
527 // Only keep not expired watchers
528 const newWatchers = watchers.filter(w => w > notBefore)
529 this.watchersPerVideo.set(videoId, newWatchers)
530
531 logger.debug('New live video views for %s is %d.', video.url, numWatchers)
532 }
533 }
534
5c0904fc
C
535 private async handleBrokenLives () {
536 const videoIds = await VideoModel.listPublishedLiveIds()
537
538 for (const id of videoIds) {
539 await this.onEndTransmuxing(id, true)
540 }
541 }
542
c6c0fa6c
C
543 static get Instance () {
544 return this.instance || (this.instance = new this())
545 }
546}
547
548// ---------------------------------------------------------------------------
549
550export {
551 LiveManager
552}