X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Flive%2Flive-manager.ts;h=33e49acc1b9a9c4d94573f9c88db3d3a5c599c7a;hb=2e9c7877eb3a3c5d64cc5c3383f0a7c0b51f5481;hp=d7199cc89a2949dd688c8bed0d45a77830711263;hpb=8ebf2a5d5d126e6ef9b89109124adf2a5e9e293d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/live/live-manager.ts b/server/lib/live/live-manager.ts index d7199cc89..33e49acc1 100644 --- a/server/lib/live/live-manager.ts +++ b/server/lib/live/live-manager.ts @@ -1,27 +1,35 @@ +import { readFile } from 'fs-extra' import { createServer, Server } from 'net' -import { isTestInstance } from '@server/helpers/core-utils' -import { computeResolutionsToTranscode, getVideoFileFPS, getVideoFileResolution } from '@server/helpers/ffprobe-utils' +import { createServer as createServerTLS, Server as ServerTLS } from 'tls' +import { + computeLowerResolutionsToTranscode, + ffprobePromise, + getVideoFileBitrate, + getVideoFileFPS, + getVideoFileResolution +} from '@server/helpers/ffprobe-utils' import { logger, loggerTagsFactory } from '@server/helpers/logger' import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config' -import { P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE, VIEW_LIFETIME, WEBSERVER } from '@server/initializers/constants' +import { P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE } from '@server/initializers/constants' import { UserModel } from '@server/models/user/user' import { VideoModel } from '@server/models/video/video' import { VideoLiveModel } from '@server/models/video/video-live' import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist' -import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo, MVideoLiveVideo } from '@server/types/models' +import { MStreamingPlaylistVideo, MVideo, MVideoLiveVideo } from '@server/types/models' import { VideoState, VideoStreamingPlaylistType } from '@shared/models' import { federateVideoIfNeeded } from '../activitypub/videos' import { JobQueue } from '../job-queue' +import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename } from '../paths' import { PeerTubeSocket } from '../peertube-socket' import { LiveQuotaStore } from './live-quota-store' import { LiveSegmentShaStore } from './live-segment-sha-store' import { cleanupLive } from './live-utils' import { MuxingSession } from './shared' -const NodeRtmpSession = require('node-media-server/node_rtmp_session') -const context = require('node-media-server/node_core_ctx') -const nodeMediaServerLogger = require('node-media-server/node_core_logger') +const NodeRtmpSession = require('node-media-server/src/node_rtmp_session') +const context = require('node-media-server/src/node_core_ctx') +const nodeMediaServerLogger = require('node-media-server/src/node_core_logger') // Disable node media server logs nodeMediaServerLogger.setLogType(0) @@ -33,9 +41,6 @@ const config = { gop_cache: VIDEO_LIVE.RTMP.GOP_CACHE, ping: VIDEO_LIVE.RTMP.PING, ping_timeout: VIDEO_LIVE.RTMP.PING_TIMEOUT - }, - transcoding: { - ffmpeg: 'ffmpeg' } } @@ -47,10 +52,11 @@ class LiveManager { private readonly muxingSessions = new Map() private readonly videoSessions = new Map() - // Values are Date().getTime() - private readonly watchersPerVideo = new Map() private rtmpServer: Server + private rtmpsServer: ServerTLS + + private running = false private constructor () { } @@ -66,7 +72,9 @@ class LiveManager { return this.abortSession(sessionId) } - this.handleSession(sessionId, streamPath, splittedPath[2]) + const session = this.getContext().sessions.get(sessionId) + + this.handleSession(sessionId, session.inputOriginUrl + streamPath, splittedPath[2]) .catch(err => logger.error('Cannot handle sessions.', { err, ...lTags(sessionId) })) }) @@ -75,12 +83,12 @@ class LiveManager { }) registerConfigChangedHandler(() => { - if (!this.rtmpServer && CONFIG.LIVE.ENABLED === true) { - this.run() + if (!this.running && CONFIG.LIVE.ENABLED === true) { + this.run().catch(err => logger.error('Cannot run live server.', { err })) return } - if (this.rtmpServer && CONFIG.LIVE.ENABLED === false) { + if (this.running && CONFIG.LIVE.ENABLED === false) { this.stop() } }) @@ -88,31 +96,68 @@ class LiveManager { // Cleanup broken lives, that were terminated by a server restart for example this.handleBrokenLives() .catch(err => logger.error('Cannot handle broken lives.', { err, ...lTags() })) - - setInterval(() => this.updateLiveViews(), VIEW_LIFETIME.LIVE) } - run () { - logger.info('Running RTMP server on port %d', config.rtmp.port, lTags()) + async run () { + this.running = true - this.rtmpServer = createServer(socket => { - const session = new NodeRtmpSession(config, socket) + if (CONFIG.LIVE.RTMP.ENABLED) { + logger.info('Running RTMP server on port %d', CONFIG.LIVE.RTMP.PORT, lTags()) - session.run() - }) + this.rtmpServer = createServer(socket => { + const session = new NodeRtmpSession(config, socket) - this.rtmpServer.on('error', err => { - logger.error('Cannot run RTMP server.', { err, ...lTags() }) - }) + session.inputOriginUrl = 'rtmp://127.0.0.1:' + CONFIG.LIVE.RTMP.PORT + session.run() + }) + + this.rtmpServer.on('error', err => { + logger.error('Cannot run RTMP server.', { err, ...lTags() }) + }) + + this.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT) + } + + if (CONFIG.LIVE.RTMPS.ENABLED) { + logger.info('Running RTMPS server on port %d', CONFIG.LIVE.RTMPS.PORT, lTags()) + + const [ key, cert ] = await Promise.all([ + readFile(CONFIG.LIVE.RTMPS.KEY_FILE), + readFile(CONFIG.LIVE.RTMPS.CERT_FILE) + ]) + const serverOptions = { key, cert } + + this.rtmpsServer = createServerTLS(serverOptions, socket => { + const session = new NodeRtmpSession(config, socket) - this.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT) + session.inputOriginUrl = 'rtmps://127.0.0.1:' + CONFIG.LIVE.RTMPS.PORT + session.run() + }) + + this.rtmpsServer.on('error', err => { + logger.error('Cannot run RTMPS server.', { err, ...lTags() }) + }) + + this.rtmpsServer.listen(CONFIG.LIVE.RTMPS.PORT) + } } stop () { - logger.info('Stopping RTMP server.', lTags()) + this.running = false + + if (this.rtmpServer) { + logger.info('Stopping RTMP server.', lTags()) - this.rtmpServer.close() - this.rtmpServer = undefined + this.rtmpServer.close() + this.rtmpServer = undefined + } + + if (this.rtmpsServer) { + logger.info('Stopping RTMPS server.', lTags()) + + this.rtmpsServer.close() + this.rtmpsServer = undefined + } // Sessions is an object this.getContext().sessions.forEach((session: any) => { @@ -134,19 +179,6 @@ class LiveManager { this.abortSession(sessionId) } - addViewTo (videoId: number) { - if (this.videoSessions.has(videoId) === false) return - - let watchers = this.watchersPerVideo.get(videoId) - - if (!watchers) { - watchers = [] - this.watchersPerVideo.set(videoId, watchers) - } - - watchers.push(new Date().getTime()) - } - private getContext () { return context } @@ -159,10 +191,15 @@ class LiveManager { } const muxingSession = this.muxingSessions.get(sessionId) - if (muxingSession) muxingSession.abort() + if (muxingSession) { + // Muxing session will fire and event so we correctly cleanup the session + muxingSession.abort() + + this.muxingSessions.delete(sessionId) + } } - private async handleSession (sessionId: string, streamPath: string, streamKey: string) { + private async handleSession (sessionId: string, inputUrl: string, streamKey: string) { const videoLive = await VideoLiveModel.loadByStreamKey(streamKey) if (!videoLive) { logger.warn('Unknown live video with stream key %s.', streamKey, lTags(sessionId)) @@ -185,17 +222,24 @@ class LiveManager { this.videoSessions.set(video.id, sessionId) - const rtmpUrl = 'rtmp://127.0.0.1:' + config.rtmp.port + streamPath + const now = Date.now() + const probe = await ffprobePromise(inputUrl) - const [ { videoFileResolution }, fps ] = await Promise.all([ - getVideoFileResolution(rtmpUrl), - getVideoFileFPS(rtmpUrl) + const [ { resolution, ratio }, fps, bitrate ] = await Promise.all([ + getVideoFileResolution(inputUrl, probe), + getVideoFileFPS(inputUrl, probe), + getVideoFileBitrate(inputUrl, probe) ]) - const allResolutions = this.buildAllResolutionsToTranscode(videoFileResolution) + logger.info( + '%s probing took %d ms (bitrate: %d, fps: %d, resolution: %d)', + inputUrl, Date.now() - now, bitrate, fps, resolution, lTags(sessionId, video.uuid) + ) + + const allResolutions = this.buildAllResolutionsToTranscode(resolution) logger.info( - 'Will mux/transcode live video of original resolution %d.', videoFileResolution, + 'Will mux/transcode live video of original resolution %d.', resolution, { allResolutions, ...lTags(sessionId, video.uuid) } ) @@ -205,8 +249,10 @@ class LiveManager { sessionId, videoLive, streamingPlaylist, - rtmpUrl, + inputUrl, fps, + bitrate, + ratio, allResolutions }) } @@ -215,11 +261,13 @@ class LiveManager { sessionId: string videoLive: MVideoLiveVideo streamingPlaylist: MStreamingPlaylistVideo - rtmpUrl: string + inputUrl: string fps: number + bitrate: number + ratio: number allResolutions: number[] }) { - const { sessionId, videoLive, streamingPlaylist, allResolutions, fps, rtmpUrl } = options + const { sessionId, videoLive, streamingPlaylist, allResolutions, fps, bitrate, ratio, inputUrl } = options const videoUUID = videoLive.Video.uuid const localLTags = lTags(sessionId, videoUUID) @@ -232,7 +280,9 @@ class LiveManager { sessionId, videoLive, streamingPlaylist, - rtmpUrl, + inputUrl, + bitrate, + ratio, fps, allResolutions }) @@ -269,6 +319,8 @@ class LiveManager { muxingSession.on('after-cleanup', ({ videoId }) => { this.muxingSessions.delete(sessionId) + muxingSession.destroy() + return this.onAfterMuxingCleanup(videoId) .catch(err => logger.error('Error in end transmuxing.', { err, ...localLTags })) }) @@ -291,6 +343,7 @@ class LiveManager { logger.info('Will publish and federate live %s.', video.url, localLTags) video.state = VideoState.PUBLISHED + video.publishedAt = new Date() await video.save() live.Video = video @@ -307,7 +360,6 @@ class LiveManager { } private onMuxingFFmpegEnd (videoId: number) { - this.watchersPerVideo.delete(videoId) this.videoSessions.delete(videoId) } @@ -341,34 +393,6 @@ class LiveManager { } } - private async updateLiveViews () { - if (!this.isRunning()) return - - if (!isTestInstance()) logger.info('Updating live video views.', lTags()) - - for (const videoId of this.watchersPerVideo.keys()) { - const notBefore = new Date().getTime() - VIEW_LIFETIME.LIVE - - const watchers = this.watchersPerVideo.get(videoId) - - const numWatchers = watchers.length - - const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) - video.views = numWatchers - await video.save() - - await federateVideoIfNeeded(video, false) - - PeerTubeSocket.Instance.sendVideoViewsUpdate(video) - - // Only keep not expired watchers - const newWatchers = watchers.filter(w => w > notBefore) - this.watchersPerVideo.set(videoId, newWatchers) - - logger.debug('New live video views for %s is %d.', video.url, numWatchers, lTags()) - } - } - private async handleBrokenLives () { const videoUUIDs = await VideoModel.listPublishedLiveUUIDs() @@ -379,25 +403,24 @@ class LiveManager { private buildAllResolutionsToTranscode (originResolution: number) { const resolutionsEnabled = CONFIG.LIVE.TRANSCODING.ENABLED - ? computeResolutionsToTranscode(originResolution, 'live') + ? computeLowerResolutionsToTranscode(originResolution, 'live') : [] return resolutionsEnabled.concat([ originResolution ]) } - private async createLivePlaylist (video: MVideo, allResolutions: number[]) { - const playlistUrl = WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsMasterPlaylistStaticPath(video.uuid) - const [ videoStreamingPlaylist ] = await VideoStreamingPlaylistModel.upsert({ - videoId: video.id, - playlistUrl, - segmentsSha256Url: WEBSERVER.URL + VideoStreamingPlaylistModel.getHlsSha256SegmentsStaticPath(video.uuid, video.isLive), - p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrl, allResolutions), - p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, + private async createLivePlaylist (video: MVideo, allResolutions: number[]): Promise { + const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(video) + + playlist.playlistFilename = generateHLSMasterPlaylistFilename(true) + playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(true) + + playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION + playlist.type = VideoStreamingPlaylistType.HLS - type: VideoStreamingPlaylistType.HLS - }, { returning: true }) as [ MStreamingPlaylist, boolean ] + playlist.assignP2PMediaLoaderInfoHashes(video, allResolutions) - return Object.assign(videoStreamingPlaylist, { Video: video }) + return playlist.save() } static get Instance () {