]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/live/live-manager.ts
Merge branch 'release/3.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / live / live-manager.ts
1
2 import { createServer, Server } from 'net'
3 import { isTestInstance } from '@server/helpers/core-utils'
4 import { computeResolutionsToTranscode, getVideoFileFPS, getVideoFileResolution } from '@server/helpers/ffprobe-utils'
5 import { logger, loggerTagsFactory } from '@server/helpers/logger'
6 import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config'
7 import { P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE, VIEW_LIFETIME } from '@server/initializers/constants'
8 import { UserModel } from '@server/models/user/user'
9 import { VideoModel } from '@server/models/video/video'
10 import { VideoLiveModel } from '@server/models/video/video-live'
11 import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
12 import { MStreamingPlaylistVideo, MVideo, MVideoLiveVideo } from '@server/types/models'
13 import { VideoState, VideoStreamingPlaylistType } from '@shared/models'
14 import { federateVideoIfNeeded } from '../activitypub/videos'
15 import { JobQueue } from '../job-queue'
16 import { PeerTubeSocket } from '../peertube-socket'
17 import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename } from '../video-paths'
18 import { LiveQuotaStore } from './live-quota-store'
19 import { LiveSegmentShaStore } from './live-segment-sha-store'
20 import { cleanupLive } from './live-utils'
21 import { MuxingSession } from './shared'
22
23 const NodeRtmpSession = require('node-media-server/src/node_rtmp_session')
24 const context = require('node-media-server/src/node_core_ctx')
25 const nodeMediaServerLogger = require('node-media-server/src/node_core_logger')
26
27 // Disable node media server logs
28 nodeMediaServerLogger.setLogType(0)
29
30 const config = {
31 rtmp: {
32 port: CONFIG.LIVE.RTMP.PORT,
33 chunk_size: VIDEO_LIVE.RTMP.CHUNK_SIZE,
34 gop_cache: VIDEO_LIVE.RTMP.GOP_CACHE,
35 ping: VIDEO_LIVE.RTMP.PING,
36 ping_timeout: VIDEO_LIVE.RTMP.PING_TIMEOUT
37 },
38 transcoding: {
39 ffmpeg: 'ffmpeg'
40 }
41 }
42
43 const lTags = loggerTagsFactory('live')
44
45 class LiveManager {
46
47 private static instance: LiveManager
48
49 private readonly muxingSessions = new Map<string, MuxingSession>()
50 private readonly videoSessions = new Map<number, string>()
51 // Values are Date().getTime()
52 private readonly watchersPerVideo = new Map<number, number[]>()
53
54 private rtmpServer: Server
55
56 private constructor () {
57 }
58
59 init () {
60 const events = this.getContext().nodeEvent
61 events.on('postPublish', (sessionId: string, streamPath: string) => {
62 logger.debug('RTMP received stream', { id: sessionId, streamPath, ...lTags(sessionId) })
63
64 const splittedPath = streamPath.split('/')
65 if (splittedPath.length !== 3 || splittedPath[1] !== VIDEO_LIVE.RTMP.BASE_PATH) {
66 logger.warn('Live path is incorrect.', { streamPath, ...lTags(sessionId) })
67 return this.abortSession(sessionId)
68 }
69
70 this.handleSession(sessionId, streamPath, splittedPath[2])
71 .catch(err => logger.error('Cannot handle sessions.', { err, ...lTags(sessionId) }))
72 })
73
74 events.on('donePublish', sessionId => {
75 logger.info('Live session ended.', { sessionId, ...lTags(sessionId) })
76 })
77
78 registerConfigChangedHandler(() => {
79 if (!this.rtmpServer && CONFIG.LIVE.ENABLED === true) {
80 this.run()
81 return
82 }
83
84 if (this.rtmpServer && CONFIG.LIVE.ENABLED === false) {
85 this.stop()
86 }
87 })
88
89 // Cleanup broken lives, that were terminated by a server restart for example
90 this.handleBrokenLives()
91 .catch(err => logger.error('Cannot handle broken lives.', { err, ...lTags() }))
92
93 setInterval(() => this.updateLiveViews(), VIEW_LIFETIME.LIVE)
94 }
95
96 run () {
97 logger.info('Running RTMP server on port %d', config.rtmp.port, lTags())
98
99 this.rtmpServer = createServer(socket => {
100 const session = new NodeRtmpSession(config, socket)
101
102 session.run()
103 })
104
105 this.rtmpServer.on('error', err => {
106 logger.error('Cannot run RTMP server.', { err, ...lTags() })
107 })
108
109 this.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT)
110 }
111
112 stop () {
113 logger.info('Stopping RTMP server.', lTags())
114
115 this.rtmpServer.close()
116 this.rtmpServer = undefined
117
118 // Sessions is an object
119 this.getContext().sessions.forEach((session: any) => {
120 if (session instanceof NodeRtmpSession) {
121 session.stop()
122 }
123 })
124 }
125
126 isRunning () {
127 return !!this.rtmpServer
128 }
129
130 stopSessionOf (videoId: number) {
131 const sessionId = this.videoSessions.get(videoId)
132 if (!sessionId) return
133
134 this.videoSessions.delete(videoId)
135 this.abortSession(sessionId)
136 }
137
138 addViewTo (videoId: number) {
139 if (this.videoSessions.has(videoId) === false) return
140
141 let watchers = this.watchersPerVideo.get(videoId)
142
143 if (!watchers) {
144 watchers = []
145 this.watchersPerVideo.set(videoId, watchers)
146 }
147
148 watchers.push(new Date().getTime())
149 }
150
151 private getContext () {
152 return context
153 }
154
155 private abortSession (sessionId: string) {
156 const session = this.getContext().sessions.get(sessionId)
157 if (session) {
158 session.stop()
159 this.getContext().sessions.delete(sessionId)
160 }
161
162 const muxingSession = this.muxingSessions.get(sessionId)
163 if (muxingSession) {
164 // Muxing session will fire and event so we correctly cleanup the session
165 muxingSession.abort()
166
167 this.muxingSessions.delete(sessionId)
168 }
169 }
170
171 private async handleSession (sessionId: string, streamPath: string, streamKey: string) {
172 const videoLive = await VideoLiveModel.loadByStreamKey(streamKey)
173 if (!videoLive) {
174 logger.warn('Unknown live video with stream key %s.', streamKey, lTags(sessionId))
175 return this.abortSession(sessionId)
176 }
177
178 const video = videoLive.Video
179 if (video.isBlacklisted()) {
180 logger.warn('Video is blacklisted. Refusing stream %s.', streamKey, lTags(sessionId, video.uuid))
181 return this.abortSession(sessionId)
182 }
183
184 // Cleanup old potential live files (could happen with a permanent live)
185 LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
186
187 const oldStreamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
188 if (oldStreamingPlaylist) {
189 await cleanupLive(video, oldStreamingPlaylist)
190 }
191
192 this.videoSessions.set(video.id, sessionId)
193
194 const rtmpUrl = 'rtmp://127.0.0.1:' + config.rtmp.port + streamPath
195
196 const [ { videoFileResolution }, fps ] = await Promise.all([
197 getVideoFileResolution(rtmpUrl),
198 getVideoFileFPS(rtmpUrl)
199 ])
200
201 const allResolutions = this.buildAllResolutionsToTranscode(videoFileResolution)
202
203 logger.info(
204 'Will mux/transcode live video of original resolution %d.', videoFileResolution,
205 { allResolutions, ...lTags(sessionId, video.uuid) }
206 )
207
208 const streamingPlaylist = await this.createLivePlaylist(video, allResolutions)
209
210 return this.runMuxingSession({
211 sessionId,
212 videoLive,
213 streamingPlaylist,
214 rtmpUrl,
215 fps,
216 allResolutions
217 })
218 }
219
220 private async runMuxingSession (options: {
221 sessionId: string
222 videoLive: MVideoLiveVideo
223 streamingPlaylist: MStreamingPlaylistVideo
224 rtmpUrl: string
225 fps: number
226 allResolutions: number[]
227 }) {
228 const { sessionId, videoLive, streamingPlaylist, allResolutions, fps, rtmpUrl } = options
229 const videoUUID = videoLive.Video.uuid
230 const localLTags = lTags(sessionId, videoUUID)
231
232 const user = await UserModel.loadByLiveId(videoLive.id)
233 LiveQuotaStore.Instance.addNewLive(user.id, videoLive.id)
234
235 const muxingSession = new MuxingSession({
236 context: this.getContext(),
237 user,
238 sessionId,
239 videoLive,
240 streamingPlaylist,
241 rtmpUrl,
242 fps,
243 allResolutions
244 })
245
246 muxingSession.on('master-playlist-created', () => this.publishAndFederateLive(videoLive, localLTags))
247
248 muxingSession.on('bad-socket-health', ({ videoId }) => {
249 logger.error(
250 'Too much data in client socket stream (ffmpeg is too slow to transcode the video).' +
251 ' Stopping session of video %s.', videoUUID,
252 localLTags
253 )
254
255 this.stopSessionOf(videoId)
256 })
257
258 muxingSession.on('duration-exceeded', ({ videoId }) => {
259 logger.info('Stopping session of %s: max duration exceeded.', videoUUID, localLTags)
260
261 this.stopSessionOf(videoId)
262 })
263
264 muxingSession.on('quota-exceeded', ({ videoId }) => {
265 logger.info('Stopping session of %s: user quota exceeded.', videoUUID, localLTags)
266
267 this.stopSessionOf(videoId)
268 })
269
270 muxingSession.on('ffmpeg-error', ({ sessionId }) => this.abortSession(sessionId))
271 muxingSession.on('ffmpeg-end', ({ videoId }) => {
272 this.onMuxingFFmpegEnd(videoId)
273 })
274
275 muxingSession.on('after-cleanup', ({ videoId }) => {
276 this.muxingSessions.delete(sessionId)
277
278 muxingSession.destroy()
279
280 return this.onAfterMuxingCleanup(videoId)
281 .catch(err => logger.error('Error in end transmuxing.', { err, ...localLTags }))
282 })
283
284 this.muxingSessions.set(sessionId, muxingSession)
285
286 muxingSession.runMuxing()
287 .catch(err => {
288 logger.error('Cannot run muxing.', { err, ...localLTags })
289 this.abortSession(sessionId)
290 })
291 }
292
293 private async publishAndFederateLive (live: MVideoLiveVideo, localLTags: { tags: string[] }) {
294 const videoId = live.videoId
295
296 try {
297 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
298
299 logger.info('Will publish and federate live %s.', video.url, localLTags)
300
301 video.state = VideoState.PUBLISHED
302 await video.save()
303
304 live.Video = video
305
306 setTimeout(() => {
307 federateVideoIfNeeded(video, false)
308 .catch(err => logger.error('Cannot federate live video %s.', video.url, { err, ...localLTags }))
309
310 PeerTubeSocket.Instance.sendVideoLiveNewState(video)
311 }, VIDEO_LIVE.SEGMENT_TIME_SECONDS * 1000 * VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION)
312 } catch (err) {
313 logger.error('Cannot save/federate live video %d.', videoId, { err, ...localLTags })
314 }
315 }
316
317 private onMuxingFFmpegEnd (videoId: number) {
318 this.watchersPerVideo.delete(videoId)
319 this.videoSessions.delete(videoId)
320 }
321
322 private async onAfterMuxingCleanup (videoUUID: string, cleanupNow = false) {
323 try {
324 const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoUUID)
325 if (!fullVideo) return
326
327 const live = await VideoLiveModel.loadByVideoId(fullVideo.id)
328
329 if (!live.permanentLive) {
330 JobQueue.Instance.createJob({
331 type: 'video-live-ending',
332 payload: {
333 videoId: fullVideo.id
334 }
335 }, { delay: cleanupNow ? 0 : VIDEO_LIVE.CLEANUP_DELAY })
336
337 fullVideo.state = VideoState.LIVE_ENDED
338 } else {
339 fullVideo.state = VideoState.WAITING_FOR_LIVE
340 }
341
342 await fullVideo.save()
343
344 PeerTubeSocket.Instance.sendVideoLiveNewState(fullVideo)
345
346 await federateVideoIfNeeded(fullVideo, false)
347 } catch (err) {
348 logger.error('Cannot save/federate new video state of live streaming of video %d.', videoUUID, { err, ...lTags(videoUUID) })
349 }
350 }
351
352 private async updateLiveViews () {
353 if (!this.isRunning()) return
354
355 if (!isTestInstance()) logger.info('Updating live video views.', lTags())
356
357 for (const videoId of this.watchersPerVideo.keys()) {
358 const notBefore = new Date().getTime() - VIEW_LIFETIME.LIVE
359
360 const watchers = this.watchersPerVideo.get(videoId)
361
362 const numWatchers = watchers.length
363
364 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
365 video.views = numWatchers
366 await video.save()
367
368 await federateVideoIfNeeded(video, false)
369
370 PeerTubeSocket.Instance.sendVideoViewsUpdate(video)
371
372 // Only keep not expired watchers
373 const newWatchers = watchers.filter(w => w > notBefore)
374 this.watchersPerVideo.set(videoId, newWatchers)
375
376 logger.debug('New live video views for %s is %d.', video.url, numWatchers, lTags())
377 }
378 }
379
380 private async handleBrokenLives () {
381 const videoUUIDs = await VideoModel.listPublishedLiveUUIDs()
382
383 for (const uuid of videoUUIDs) {
384 await this.onAfterMuxingCleanup(uuid, true)
385 }
386 }
387
388 private buildAllResolutionsToTranscode (originResolution: number) {
389 const resolutionsEnabled = CONFIG.LIVE.TRANSCODING.ENABLED
390 ? computeResolutionsToTranscode(originResolution, 'live')
391 : []
392
393 return resolutionsEnabled.concat([ originResolution ])
394 }
395
396 private async createLivePlaylist (video: MVideo, allResolutions: number[]): Promise<MStreamingPlaylistVideo> {
397 const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(video)
398
399 playlist.playlistFilename = generateHLSMasterPlaylistFilename(true)
400 playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(true)
401
402 playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
403 playlist.type = VideoStreamingPlaylistType.HLS
404
405 playlist.assignP2PMediaLoaderInfoHashes(video, allResolutions)
406
407 return playlist.save()
408 }
409
410 static get Instance () {
411 return this.instance || (this.instance = new this())
412 }
413 }
414
415 // ---------------------------------------------------------------------------
416
417 export {
418 LiveManager
419 }