]>
Commit | Line | Data |
---|---|---|
4ec52d04 | 1 | import { readdir, readFile } from 'fs-extra' |
8ebf2a5d | 2 | import { createServer, Server } from 'net' |
4ec52d04 | 3 | import { join } from 'path' |
df1db951 | 4 | import { createServer as createServerTLS, Server as ServerTLS } from 'tls' |
8ebf2a5d C |
5 | import { logger, loggerTagsFactory } from '@server/helpers/logger' |
6 | import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config' | |
28705705 | 7 | import { VIDEO_LIVE, WEBSERVER } from '@server/initializers/constants' |
0c9668f7 | 8 | import { sequelizeTypescript } from '@server/initializers/database' |
5a05c145 | 9 | import { RunnerJobModel } from '@server/models/runner/runner-job' |
8ebf2a5d C |
10 | import { UserModel } from '@server/models/user/user' |
11 | import { VideoModel } from '@server/models/video/video' | |
12 | import { VideoLiveModel } from '@server/models/video/video-live' | |
0c9668f7 | 13 | import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting' |
26e3e98f | 14 | import { VideoLiveSessionModel } from '@server/models/video/video-live-session' |
8ebf2a5d | 15 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' |
05a60d85 | 16 | import { MVideo, MVideoLiveSession, MVideoLiveVideo, MVideoLiveVideoWithSetting } from '@server/types/models' |
1ce4256a | 17 | import { pick, wait } from '@shared/core-utils' |
0c9668f7 | 18 | import { ffprobePromise, getVideoStreamBitrate, getVideoStreamDimensionsInfo, getVideoStreamFPS, hasAudioStream } from '@shared/ffmpeg' |
afb371d9 | 19 | import { LiveVideoError, VideoState } from '@shared/models' |
8ebf2a5d C |
20 | import { federateVideoIfNeeded } from '../activitypub/videos' |
21 | import { JobQueue } from '../job-queue' | |
afb371d9 | 22 | import { getLiveReplayBaseDirectory } from '../paths' |
df1db951 | 23 | import { PeerTubeSocket } from '../peertube-socket' |
84cae54e | 24 | import { Hooks } from '../plugins/hooks' |
0c9668f7 | 25 | import { computeResolutionsToTranscode } from '../transcoding/transcoding-resolutions' |
8ebf2a5d | 26 | import { LiveQuotaStore } from './live-quota-store' |
0c9668f7 | 27 | import { cleanupAndDestroyPermanentLive, getLiveSegmentTime } from './live-utils' |
8ebf2a5d C |
28 | import { MuxingSession } from './shared' |
29 | ||
7a397c7f C |
30 | const NodeRtmpSession = require('node-media-server/src/node_rtmp_session') |
31 | const context = require('node-media-server/src/node_core_ctx') | |
32 | const nodeMediaServerLogger = require('node-media-server/src/node_core_logger') | |
8ebf2a5d C |
33 | |
34 | // Disable node media server logs | |
35 | nodeMediaServerLogger.setLogType(0) | |
36 | ||
37 | const 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 | |
8ebf2a5d C |
44 | } |
45 | } | |
46 | ||
47 | const lTags = loggerTagsFactory('live') | |
48 | ||
49 | class LiveManager { | |
50 | ||
51 | private static instance: LiveManager | |
52 | ||
53 | private readonly muxingSessions = new Map<string, MuxingSession>() | |
0c9668f7 | 54 | private readonly videoSessions = new Map<string, string>() |
8ebf2a5d C |
55 | |
56 | private rtmpServer: Server | |
df1db951 C |
57 | private rtmpsServer: ServerTLS |
58 | ||
59 | private running = false | |
8ebf2a5d C |
60 | |
61 | private constructor () { | |
62 | } | |
63 | ||
64 | init () { | |
65 | const events = this.getContext().nodeEvent | |
66 | events.on('postPublish', (sessionId: string, streamPath: string) => { | |
67 | logger.debug('RTMP received stream', { id: sessionId, streamPath, ...lTags(sessionId) }) | |
68 | ||
69 | const splittedPath = streamPath.split('/') | |
70 | if (splittedPath.length !== 3 || splittedPath[1] !== VIDEO_LIVE.RTMP.BASE_PATH) { | |
71 | logger.warn('Live path is incorrect.', { streamPath, ...lTags(sessionId) }) | |
72 | return this.abortSession(sessionId) | |
73 | } | |
74 | ||
df1db951 | 75 | const session = this.getContext().sessions.get(sessionId) |
28705705 C |
76 | const inputLocalUrl = session.inputOriginLocalUrl + streamPath |
77 | const inputPublicUrl = session.inputOriginPublicUrl + streamPath | |
df1db951 | 78 | |
28705705 | 79 | this.handleSession({ sessionId, inputPublicUrl, inputLocalUrl, streamKey: splittedPath[2] }) |
8ebf2a5d C |
80 | .catch(err => logger.error('Cannot handle sessions.', { err, ...lTags(sessionId) })) |
81 | }) | |
82 | ||
83 | events.on('donePublish', sessionId => { | |
84 | logger.info('Live session ended.', { sessionId, ...lTags(sessionId) }) | |
5a05c145 C |
85 | |
86 | // Force session aborting, so we kill ffmpeg even if it still has data to process (slow CPU) | |
87 | setTimeout(() => this.abortSession(sessionId), 2000) | |
8ebf2a5d C |
88 | }) |
89 | ||
90 | registerConfigChangedHandler(() => { | |
df1db951 C |
91 | if (!this.running && CONFIG.LIVE.ENABLED === true) { |
92 | this.run().catch(err => logger.error('Cannot run live server.', { err })) | |
8ebf2a5d C |
93 | return |
94 | } | |
95 | ||
df1db951 | 96 | if (this.running && CONFIG.LIVE.ENABLED === false) { |
8ebf2a5d C |
97 | this.stop() |
98 | } | |
99 | }) | |
100 | ||
101 | // Cleanup broken lives, that were terminated by a server restart for example | |
102 | this.handleBrokenLives() | |
103 | .catch(err => logger.error('Cannot handle broken lives.', { err, ...lTags() })) | |
8ebf2a5d C |
104 | } |
105 | ||
df1db951 C |
106 | async run () { |
107 | this.running = true | |
8ebf2a5d | 108 | |
df1db951 C |
109 | if (CONFIG.LIVE.RTMP.ENABLED) { |
110 | logger.info('Running RTMP server on port %d', CONFIG.LIVE.RTMP.PORT, lTags()) | |
8ebf2a5d | 111 | |
df1db951 C |
112 | this.rtmpServer = createServer(socket => { |
113 | const session = new NodeRtmpSession(config, socket) | |
8ebf2a5d | 114 | |
28705705 C |
115 | session.inputOriginLocalUrl = 'rtmp://127.0.0.1:' + CONFIG.LIVE.RTMP.PORT |
116 | session.inputOriginPublicUrl = WEBSERVER.RTMP_URL | |
df1db951 C |
117 | session.run() |
118 | }) | |
119 | ||
120 | this.rtmpServer.on('error', err => { | |
121 | logger.error('Cannot run RTMP server.', { err, ...lTags() }) | |
122 | }) | |
123 | ||
cfbe6be5 | 124 | this.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT, CONFIG.LIVE.RTMP.HOSTNAME) |
df1db951 | 125 | } |
8ebf2a5d | 126 | |
df1db951 C |
127 | if (CONFIG.LIVE.RTMPS.ENABLED) { |
128 | logger.info('Running RTMPS server on port %d', CONFIG.LIVE.RTMPS.PORT, lTags()) | |
129 | ||
130 | const [ key, cert ] = await Promise.all([ | |
131 | readFile(CONFIG.LIVE.RTMPS.KEY_FILE), | |
132 | readFile(CONFIG.LIVE.RTMPS.CERT_FILE) | |
133 | ]) | |
134 | const serverOptions = { key, cert } | |
135 | ||
136 | this.rtmpsServer = createServerTLS(serverOptions, socket => { | |
137 | const session = new NodeRtmpSession(config, socket) | |
138 | ||
28705705 C |
139 | session.inputOriginLocalUrl = 'rtmps://127.0.0.1:' + CONFIG.LIVE.RTMPS.PORT |
140 | session.inputOriginPublicUrl = WEBSERVER.RTMPS_URL | |
df1db951 C |
141 | session.run() |
142 | }) | |
143 | ||
144 | this.rtmpsServer.on('error', err => { | |
145 | logger.error('Cannot run RTMPS server.', { err, ...lTags() }) | |
146 | }) | |
147 | ||
cfbe6be5 | 148 | this.rtmpsServer.listen(CONFIG.LIVE.RTMPS.PORT, CONFIG.LIVE.RTMPS.HOSTNAME) |
df1db951 | 149 | } |
8ebf2a5d C |
150 | } |
151 | ||
152 | stop () { | |
df1db951 C |
153 | this.running = false |
154 | ||
5037e0e4 C |
155 | if (this.rtmpServer) { |
156 | logger.info('Stopping RTMP server.', lTags()) | |
8ebf2a5d | 157 | |
5037e0e4 C |
158 | this.rtmpServer.close() |
159 | this.rtmpServer = undefined | |
160 | } | |
161 | ||
162 | if (this.rtmpsServer) { | |
163 | logger.info('Stopping RTMPS server.', lTags()) | |
164 | ||
165 | this.rtmpsServer.close() | |
166 | this.rtmpsServer = undefined | |
167 | } | |
8ebf2a5d C |
168 | |
169 | // Sessions is an object | |
170 | this.getContext().sessions.forEach((session: any) => { | |
171 | if (session instanceof NodeRtmpSession) { | |
172 | session.stop() | |
173 | } | |
174 | }) | |
175 | } | |
176 | ||
177 | isRunning () { | |
178 | return !!this.rtmpServer | |
179 | } | |
180 | ||
17ecdf61 C |
181 | hasSession (sessionId: string) { |
182 | return this.getContext().sessions.has(sessionId) | |
183 | } | |
184 | ||
0c9668f7 C |
185 | stopSessionOf (videoUUID: string, error: LiveVideoError | null) { |
186 | const sessionId = this.videoSessions.get(videoUUID) | |
187 | if (!sessionId) { | |
188 | logger.debug('No live session to stop for video %s', videoUUID, lTags(sessionId, videoUUID)) | |
189 | return | |
190 | } | |
8ebf2a5d | 191 | |
0c9668f7 | 192 | logger.info('Stopping live session of video %s', videoUUID, { error, ...lTags(sessionId, videoUUID) }) |
26e3e98f | 193 | |
0c9668f7 C |
194 | this.saveEndingSession(videoUUID, error) |
195 | .catch(err => logger.error('Cannot save ending session.', { err, ...lTags(sessionId, videoUUID) })) | |
196 | ||
197 | this.videoSessions.delete(videoUUID) | |
8ebf2a5d C |
198 | this.abortSession(sessionId) |
199 | } | |
200 | ||
8ebf2a5d C |
201 | private getContext () { |
202 | return context | |
203 | } | |
204 | ||
205 | private abortSession (sessionId: string) { | |
206 | const session = this.getContext().sessions.get(sessionId) | |
207 | if (session) { | |
208 | session.stop() | |
209 | this.getContext().sessions.delete(sessionId) | |
210 | } | |
211 | ||
212 | const muxingSession = this.muxingSessions.get(sessionId) | |
609a4442 | 213 | if (muxingSession) { |
e466544f | 214 | // Muxing session will fire and event so we correctly cleanup the session |
609a4442 | 215 | muxingSession.abort() |
609a4442 C |
216 | |
217 | this.muxingSessions.delete(sessionId) | |
218 | } | |
8ebf2a5d C |
219 | } |
220 | ||
28705705 C |
221 | private async handleSession (options: { |
222 | sessionId: string | |
223 | inputLocalUrl: string | |
224 | inputPublicUrl: string | |
225 | streamKey: string | |
226 | }) { | |
227 | const { inputLocalUrl, inputPublicUrl, sessionId, streamKey } = options | |
228 | ||
8ebf2a5d C |
229 | const videoLive = await VideoLiveModel.loadByStreamKey(streamKey) |
230 | if (!videoLive) { | |
231 | logger.warn('Unknown live video with stream key %s.', streamKey, lTags(sessionId)) | |
232 | return this.abortSession(sessionId) | |
233 | } | |
234 | ||
235 | const video = videoLive.Video | |
236 | if (video.isBlacklisted()) { | |
237 | logger.warn('Video is blacklisted. Refusing stream %s.', streamKey, lTags(sessionId, video.uuid)) | |
238 | return this.abortSession(sessionId) | |
239 | } | |
240 | ||
0c9668f7 C |
241 | if (this.videoSessions.has(video.uuid)) { |
242 | logger.warn('Video %s has already a live session. Refusing stream %s.', video.uuid, streamKey, lTags(sessionId, video.uuid)) | |
243 | return this.abortSession(sessionId) | |
244 | } | |
245 | ||
92083e42 | 246 | // Cleanup old potential live (could happen with a permanent live) |
8ebf2a5d C |
247 | const oldStreamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) |
248 | if (oldStreamingPlaylist) { | |
5333788c C |
249 | if (!videoLive.permanentLive) throw new Error('Found previous session in a non permanent live: ' + video.uuid) |
250 | ||
cfd57d2c | 251 | await cleanupAndDestroyPermanentLive(video, oldStreamingPlaylist) |
8ebf2a5d C |
252 | } |
253 | ||
0c9668f7 | 254 | this.videoSessions.set(video.uuid, sessionId) |
8ebf2a5d | 255 | |
c826f34a | 256 | const now = Date.now() |
28705705 | 257 | const probe = await ffprobePromise(inputLocalUrl) |
c826f34a | 258 | |
1ce4256a | 259 | const [ { resolution, ratio }, fps, bitrate, hasAudio ] = await Promise.all([ |
28705705 C |
260 | getVideoStreamDimensionsInfo(inputLocalUrl, probe), |
261 | getVideoStreamFPS(inputLocalUrl, probe), | |
262 | getVideoStreamBitrate(inputLocalUrl, probe), | |
263 | hasAudioStream(inputLocalUrl, probe) | |
8ebf2a5d C |
264 | ]) |
265 | ||
c826f34a C |
266 | logger.info( |
267 | '%s probing took %d ms (bitrate: %d, fps: %d, resolution: %d)', | |
28705705 | 268 | inputLocalUrl, Date.now() - now, bitrate, fps, resolution, lTags(sessionId, video.uuid) |
c826f34a C |
269 | ) |
270 | ||
ebb9e53a | 271 | const allResolutions = await Hooks.wrapObject( |
a32bf8cd | 272 | this.buildAllResolutionsToTranscode(resolution, hasAudio), |
64fd6158 | 273 | 'filter:transcoding.auto.resolutions-to-transcode.result', |
ebb9e53a C |
274 | { video } |
275 | ) | |
8ebf2a5d C |
276 | |
277 | logger.info( | |
0c9668f7 | 278 | 'Handling live video of original resolution %d.', resolution, |
8ebf2a5d C |
279 | { allResolutions, ...lTags(sessionId, video.uuid) } |
280 | ) | |
281 | ||
8ebf2a5d C |
282 | return this.runMuxingSession({ |
283 | sessionId, | |
284 | videoLive, | |
1ce4256a | 285 | |
28705705 C |
286 | inputLocalUrl, |
287 | inputPublicUrl, | |
8ebf2a5d | 288 | fps, |
c826f34a | 289 | bitrate, |
679c12e6 | 290 | ratio, |
1ce4256a C |
291 | allResolutions, |
292 | hasAudio | |
8ebf2a5d C |
293 | }) |
294 | } | |
295 | ||
296 | private async runMuxingSession (options: { | |
297 | sessionId: string | |
05a60d85 | 298 | videoLive: MVideoLiveVideoWithSetting |
1ce4256a | 299 | |
28705705 C |
300 | inputLocalUrl: string |
301 | inputPublicUrl: string | |
302 | ||
8ebf2a5d | 303 | fps: number |
c826f34a | 304 | bitrate: number |
679c12e6 | 305 | ratio: number |
8ebf2a5d | 306 | allResolutions: number[] |
1ce4256a | 307 | hasAudio: boolean |
8ebf2a5d | 308 | }) { |
1ce4256a | 309 | const { sessionId, videoLive } = options |
8ebf2a5d C |
310 | const videoUUID = videoLive.Video.uuid |
311 | const localLTags = lTags(sessionId, videoUUID) | |
312 | ||
26e3e98f C |
313 | const liveSession = await this.saveStartingSession(videoLive) |
314 | ||
8ebf2a5d | 315 | const user = await UserModel.loadByLiveId(videoLive.id) |
3f0ceab0 | 316 | LiveQuotaStore.Instance.addNewLive(user.id, sessionId) |
8ebf2a5d C |
317 | |
318 | const muxingSession = new MuxingSession({ | |
319 | context: this.getContext(), | |
8ebf2a5d C |
320 | sessionId, |
321 | videoLive, | |
1ce4256a C |
322 | user, |
323 | ||
28705705 | 324 | ...pick(options, [ 'inputLocalUrl', 'inputPublicUrl', 'bitrate', 'ratio', 'fps', 'allResolutions', 'hasAudio' ]) |
8ebf2a5d C |
325 | }) |
326 | ||
cfd57d2c | 327 | muxingSession.on('live-ready', () => this.publishAndFederateLive(videoLive, localLTags)) |
8ebf2a5d | 328 | |
0c9668f7 | 329 | muxingSession.on('bad-socket-health', ({ videoUUID }) => { |
8ebf2a5d C |
330 | logger.error( |
331 | 'Too much data in client socket stream (ffmpeg is too slow to transcode the video).' + | |
332 | ' Stopping session of video %s.', videoUUID, | |
333 | localLTags | |
334 | ) | |
335 | ||
0c9668f7 | 336 | this.stopSessionOf(videoUUID, LiveVideoError.BAD_SOCKET_HEALTH) |
8ebf2a5d C |
337 | }) |
338 | ||
0c9668f7 | 339 | muxingSession.on('duration-exceeded', ({ videoUUID }) => { |
8ebf2a5d C |
340 | logger.info('Stopping session of %s: max duration exceeded.', videoUUID, localLTags) |
341 | ||
0c9668f7 | 342 | this.stopSessionOf(videoUUID, LiveVideoError.DURATION_EXCEEDED) |
8ebf2a5d C |
343 | }) |
344 | ||
0c9668f7 | 345 | muxingSession.on('quota-exceeded', ({ videoUUID }) => { |
8ebf2a5d C |
346 | logger.info('Stopping session of %s: user quota exceeded.', videoUUID, localLTags) |
347 | ||
0c9668f7 | 348 | this.stopSessionOf(videoUUID, LiveVideoError.QUOTA_EXCEEDED) |
26e3e98f C |
349 | }) |
350 | ||
0c9668f7 C |
351 | muxingSession.on('transcoding-error', ({ videoUUID }) => { |
352 | this.stopSessionOf(videoUUID, LiveVideoError.FFMPEG_ERROR) | |
8ebf2a5d C |
353 | }) |
354 | ||
0c9668f7 C |
355 | muxingSession.on('transcoding-end', ({ videoUUID }) => { |
356 | this.onMuxingFFmpegEnd(videoUUID, sessionId) | |
8ebf2a5d C |
357 | }) |
358 | ||
0c9668f7 | 359 | muxingSession.on('after-cleanup', ({ videoUUID }) => { |
8ebf2a5d C |
360 | this.muxingSessions.delete(sessionId) |
361 | ||
3f0ceab0 | 362 | LiveQuotaStore.Instance.removeLive(user.id, sessionId) |
9a82ce24 | 363 | |
609a4442 C |
364 | muxingSession.destroy() |
365 | ||
0c9668f7 | 366 | return this.onAfterMuxingCleanup({ videoUUID, liveSession }) |
8ebf2a5d C |
367 | .catch(err => logger.error('Error in end transmuxing.', { err, ...localLTags })) |
368 | }) | |
369 | ||
370 | this.muxingSessions.set(sessionId, muxingSession) | |
371 | ||
372 | muxingSession.runMuxing() | |
373 | .catch(err => { | |
374 | logger.error('Cannot run muxing.', { err, ...localLTags }) | |
375 | this.abortSession(sessionId) | |
376 | }) | |
377 | } | |
378 | ||
379 | private async publishAndFederateLive (live: MVideoLiveVideo, localLTags: { tags: string[] }) { | |
380 | const videoId = live.videoId | |
381 | ||
382 | try { | |
4fae2b1f | 383 | const video = await VideoModel.loadFull(videoId) |
8ebf2a5d C |
384 | |
385 | logger.info('Will publish and federate live %s.', video.url, localLTags) | |
386 | ||
387 | video.state = VideoState.PUBLISHED | |
7137377d | 388 | video.publishedAt = new Date() |
8ebf2a5d C |
389 | await video.save() |
390 | ||
391 | live.Video = video | |
392 | ||
4ec52d04 | 393 | await wait(getLiveSegmentTime(live.latencyMode) * 1000 * VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION) |
8ebf2a5d | 394 | |
4ec52d04 C |
395 | try { |
396 | await federateVideoIfNeeded(video, false) | |
397 | } catch (err) { | |
398 | logger.error('Cannot federate live video %s.', video.url, { err, ...localLTags }) | |
399 | } | |
400 | ||
401 | PeerTubeSocket.Instance.sendVideoLiveNewState(video) | |
cb0eda56 AG |
402 | |
403 | Hooks.runAction('action:live.video.state.updated', { video }) | |
8ebf2a5d C |
404 | } catch (err) { |
405 | logger.error('Cannot save/federate live video %d.', videoId, { err, ...localLTags }) | |
406 | } | |
407 | } | |
408 | ||
0c9668f7 | 409 | private onMuxingFFmpegEnd (videoUUID: string, sessionId: string) { |
c08a7f16 C |
410 | // Session already cleaned up |
411 | if (!this.videoSessions.has(videoUUID)) return | |
412 | ||
0c9668f7 | 413 | this.videoSessions.delete(videoUUID) |
26e3e98f | 414 | |
0c9668f7 | 415 | this.saveEndingSession(videoUUID, null) |
26e3e98f | 416 | .catch(err => logger.error('Cannot save ending session.', { err, ...lTags(sessionId) })) |
8ebf2a5d C |
417 | } |
418 | ||
4ec52d04 | 419 | private async onAfterMuxingCleanup (options: { |
0c9668f7 | 420 | videoUUID: string |
26e3e98f | 421 | liveSession?: MVideoLiveSession |
4ec52d04 C |
422 | cleanupNow?: boolean // Default false |
423 | }) { | |
0c9668f7 C |
424 | const { videoUUID, liveSession: liveSessionArg, cleanupNow = false } = options |
425 | ||
426 | logger.debug('Live of video %s has been cleaned up. Moving to its next state.', videoUUID, lTags(videoUUID)) | |
4ec52d04 | 427 | |
8ebf2a5d | 428 | try { |
0c9668f7 | 429 | const fullVideo = await VideoModel.loadFull(videoUUID) |
8ebf2a5d C |
430 | if (!fullVideo) return |
431 | ||
432 | const live = await VideoLiveModel.loadByVideoId(fullVideo.id) | |
433 | ||
46f7cd68 | 434 | const liveSession = liveSessionArg ?? await VideoLiveSessionModel.findLatestSessionOf(fullVideo.id) |
26e3e98f C |
435 | |
436 | // On server restart during a live | |
437 | if (!liveSession.endDate) { | |
438 | liveSession.endDate = new Date() | |
439 | await liveSession.save() | |
440 | } | |
441 | ||
bd911b54 | 442 | JobQueue.Instance.createJobAsync({ |
4ec52d04 C |
443 | type: 'video-live-ending', |
444 | payload: { | |
445 | videoId: fullVideo.id, | |
26e3e98f | 446 | |
4ec52d04 C |
447 | replayDirectory: live.saveReplay |
448 | ? await this.findReplayDirectory(fullVideo) | |
449 | : undefined, | |
26e3e98f C |
450 | |
451 | liveSessionId: liveSession.id, | |
cdd83816 | 452 | streamingPlaylistId: fullVideo.getHLSPlaylist()?.id, |
26e3e98f | 453 | |
4ec52d04 | 454 | publishedAt: fullVideo.publishedAt.toISOString() |
bd911b54 C |
455 | }, |
456 | ||
457 | delay: cleanupNow | |
458 | ? 0 | |
459 | : VIDEO_LIVE.CLEANUP_DELAY | |
460 | }) | |
4ec52d04 C |
461 | |
462 | fullVideo.state = live.permanentLive | |
463 | ? VideoState.WAITING_FOR_LIVE | |
464 | : VideoState.LIVE_ENDED | |
8ebf2a5d C |
465 | |
466 | await fullVideo.save() | |
467 | ||
468 | PeerTubeSocket.Instance.sendVideoLiveNewState(fullVideo) | |
469 | ||
470 | await federateVideoIfNeeded(fullVideo, false) | |
cb0eda56 AG |
471 | |
472 | Hooks.runAction('action:live.video.state.updated', { video: fullVideo }) | |
8ebf2a5d | 473 | } catch (err) { |
0c9668f7 | 474 | logger.error('Cannot save/federate new video state of live streaming of video %s.', videoUUID, { err, ...lTags(videoUUID) }) |
8ebf2a5d C |
475 | } |
476 | } | |
477 | ||
8ebf2a5d | 478 | private async handleBrokenLives () { |
0c9668f7 C |
479 | await RunnerJobModel.cancelAllJobs({ type: 'live-rtmp-hls-transcoding' }) |
480 | ||
8ebf2a5d C |
481 | const videoUUIDs = await VideoModel.listPublishedLiveUUIDs() |
482 | ||
483 | for (const uuid of videoUUIDs) { | |
0c9668f7 | 484 | await this.onAfterMuxingCleanup({ videoUUID: uuid, cleanupNow: true }) |
8ebf2a5d C |
485 | } |
486 | } | |
487 | ||
4ec52d04 C |
488 | private async findReplayDirectory (video: MVideo) { |
489 | const directory = getLiveReplayBaseDirectory(video) | |
490 | const files = await readdir(directory) | |
491 | ||
492 | if (files.length === 0) return undefined | |
493 | ||
494 | return join(directory, files.sort().reverse()[0]) | |
495 | } | |
496 | ||
a32bf8cd | 497 | private buildAllResolutionsToTranscode (originResolution: number, hasAudio: boolean) { |
5e2afe42 | 498 | const includeInput = CONFIG.LIVE.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION |
84cae54e | 499 | |
8ebf2a5d | 500 | const resolutionsEnabled = CONFIG.LIVE.TRANSCODING.ENABLED |
a32bf8cd | 501 | ? computeResolutionsToTranscode({ input: originResolution, type: 'live', includeInput, strictLower: false, hasAudio }) |
8ebf2a5d C |
502 | : [] |
503 | ||
84cae54e C |
504 | if (resolutionsEnabled.length === 0) { |
505 | return [ originResolution ] | |
506 | } | |
507 | ||
508 | return resolutionsEnabled | |
8ebf2a5d C |
509 | } |
510 | ||
05a60d85 W |
511 | private async saveStartingSession (videoLive: MVideoLiveVideoWithSetting) { |
512 | const replaySettings = videoLive.saveReplay | |
513 | ? new VideoLiveReplaySettingModel({ | |
514 | privacy: videoLive.ReplaySetting.privacy | |
515 | }) | |
516 | : null | |
26e3e98f | 517 | |
05a60d85 W |
518 | return sequelizeTypescript.transaction(async t => { |
519 | if (videoLive.saveReplay) { | |
520 | await replaySettings.save({ transaction: t }) | |
521 | } | |
522 | ||
523 | return VideoLiveSessionModel.create({ | |
524 | startDate: new Date(), | |
525 | liveVideoId: videoLive.videoId, | |
526 | saveReplay: videoLive.saveReplay, | |
527 | replaySettingId: videoLive.saveReplay ? replaySettings.id : null, | |
528 | endingProcessed: false | |
529 | }, { transaction: t }) | |
530 | }) | |
26e3e98f C |
531 | } |
532 | ||
0c9668f7 C |
533 | private async saveEndingSession (videoUUID: string, error: LiveVideoError | null) { |
534 | const liveSession = await VideoLiveSessionModel.findCurrentSessionOf(videoUUID) | |
0755cb89 C |
535 | if (!liveSession) return |
536 | ||
26e3e98f C |
537 | liveSession.endDate = new Date() |
538 | liveSession.error = error | |
539 | ||
540 | return liveSession.save() | |
541 | } | |
542 | ||
8ebf2a5d C |
543 | static get Instance () { |
544 | return this.instance || (this.instance = new this()) | |
545 | } | |
546 | } | |
547 | ||
548 | // --------------------------------------------------------------------------- | |
549 | ||
550 | export { | |
551 | LiveManager | |
552 | } |