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