+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)
gop_cache: VIDEO_LIVE.RTMP.GOP_CACHE,
ping: VIDEO_LIVE.RTMP.PING,
ping_timeout: VIDEO_LIVE.RTMP.PING_TIMEOUT
- },
- transcoding: {
- ffmpeg: 'ffmpeg'
}
}
private readonly muxingSessions = new Map<string, MuxingSession>()
private readonly videoSessions = new Map<number, string>()
- // Values are Date().getTime()
- private readonly watchersPerVideo = new Map<number, number[]>()
private rtmpServer: Server
+ private rtmpsServer: ServerTLS
+
+ private running = false
private constructor () {
}
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) }))
})
})
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()
}
})
// 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.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT)
+ this.rtmpsServer = createServerTLS(serverOptions, socket => {
+ const session = new NodeRtmpSession(config, socket)
+
+ 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
- this.rtmpServer.close()
- this.rtmpServer = undefined
+ if (this.rtmpServer) {
+ logger.info('Stopping RTMP server.', lTags())
+
+ 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) => {
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
}
}
}
- 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))
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) }
)
sessionId,
videoLive,
streamingPlaylist,
- rtmpUrl,
+ inputUrl,
fps,
+ bitrate,
+ ratio,
allResolutions
})
}
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)
sessionId,
videoLive,
streamingPlaylist,
- rtmpUrl,
+ inputUrl,
+ bitrate,
+ ratio,
fps,
allResolutions
})
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
}
private onMuxingFFmpegEnd (videoId: number) {
- this.watchersPerVideo.delete(videoId)
this.videoSessions.delete(videoId)
}
}
}
- 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()
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<MStreamingPlaylistVideo> {
+ 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 () {