2022-04-21 09:06:52 +02:00
|
|
|
import { readdir, readFile } from 'fs-extra'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { createServer, Server } from 'net'
|
2022-04-21 09:06:52 +02:00
|
|
|
import { join } from 'path'
|
2021-11-05 11:36:03 +01:00
|
|
|
import { createServer as createServerTLS, Server as ServerTLS } from 'tls'
|
2021-08-06 10:39:40 +02:00
|
|
|
import {
|
2022-08-05 10:36:19 +02:00
|
|
|
computeResolutionsToTranscode,
|
2021-08-06 10:39:40 +02:00
|
|
|
ffprobePromise,
|
2022-03-04 13:40:02 +01:00
|
|
|
getLiveSegmentTime,
|
2022-02-11 10:51:33 +01:00
|
|
|
getVideoStreamBitrate,
|
2022-03-04 13:40:02 +01:00
|
|
|
getVideoStreamDimensionsInfo,
|
2022-09-28 10:23:03 +02:00
|
|
|
getVideoStreamFPS,
|
|
|
|
hasAudioStream
|
2022-02-11 10:51:33 +01:00
|
|
|
} from '@server/helpers/ffmpeg'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger'
|
|
|
|
import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config'
|
2021-11-09 10:11:20 +01:00
|
|
|
import { P2P_MEDIA_LOADER_PEER_VERSION, VIDEO_LIVE } from '@server/initializers/constants'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { UserModel } from '@server/models/user/user'
|
|
|
|
import { VideoModel } from '@server/models/video/video'
|
|
|
|
import { VideoLiveModel } from '@server/models/video/video-live'
|
2022-05-03 11:38:07 +02:00
|
|
|
import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
|
2022-05-03 11:38:07 +02:00
|
|
|
import { MStreamingPlaylistVideo, MVideo, MVideoLiveSession, MVideoLiveVideo } from '@server/types/models'
|
2022-09-28 10:23:03 +02:00
|
|
|
import { pick, wait } from '@shared/core-utils'
|
2022-05-03 11:38:07 +02:00
|
|
|
import { LiveVideoError, VideoState, VideoStreamingPlaylistType } from '@shared/models'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { federateVideoIfNeeded } from '../activitypub/videos'
|
|
|
|
import { JobQueue } from '../job-queue'
|
2022-04-21 09:06:52 +02:00
|
|
|
import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } from '../paths'
|
2021-11-05 11:36:03 +01:00
|
|
|
import { PeerTubeSocket } from '../peertube-socket'
|
2022-08-05 10:36:19 +02:00
|
|
|
import { Hooks } from '../plugins/hooks'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { LiveQuotaStore } from './live-quota-store'
|
2022-05-25 14:54:16 +02:00
|
|
|
import { cleanupPermanentLive } from './live-utils'
|
2021-06-16 15:14:41 +02:00
|
|
|
import { MuxingSession } from './shared'
|
|
|
|
|
2021-07-22 11:58:44 +02:00
|
|
|
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')
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
// Disable node media server logs
|
|
|
|
nodeMediaServerLogger.setLogType(0)
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
rtmp: {
|
|
|
|
port: CONFIG.LIVE.RTMP.PORT,
|
|
|
|
chunk_size: VIDEO_LIVE.RTMP.CHUNK_SIZE,
|
|
|
|
gop_cache: VIDEO_LIVE.RTMP.GOP_CACHE,
|
|
|
|
ping: VIDEO_LIVE.RTMP.PING,
|
|
|
|
ping_timeout: VIDEO_LIVE.RTMP.PING_TIMEOUT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const lTags = loggerTagsFactory('live')
|
|
|
|
|
|
|
|
class LiveManager {
|
|
|
|
|
|
|
|
private static instance: LiveManager
|
|
|
|
|
|
|
|
private readonly muxingSessions = new Map<string, MuxingSession>()
|
|
|
|
private readonly videoSessions = new Map<number, string>()
|
|
|
|
|
|
|
|
private rtmpServer: Server
|
2021-11-05 11:36:03 +01:00
|
|
|
private rtmpsServer: ServerTLS
|
|
|
|
|
|
|
|
private running = false
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
private constructor () {
|
|
|
|
}
|
|
|
|
|
|
|
|
init () {
|
|
|
|
const events = this.getContext().nodeEvent
|
|
|
|
events.on('postPublish', (sessionId: string, streamPath: string) => {
|
|
|
|
logger.debug('RTMP received stream', { id: sessionId, streamPath, ...lTags(sessionId) })
|
|
|
|
|
|
|
|
const splittedPath = streamPath.split('/')
|
|
|
|
if (splittedPath.length !== 3 || splittedPath[1] !== VIDEO_LIVE.RTMP.BASE_PATH) {
|
|
|
|
logger.warn('Live path is incorrect.', { streamPath, ...lTags(sessionId) })
|
|
|
|
return this.abortSession(sessionId)
|
|
|
|
}
|
|
|
|
|
2021-11-05 11:36:03 +01:00
|
|
|
const session = this.getContext().sessions.get(sessionId)
|
|
|
|
|
|
|
|
this.handleSession(sessionId, session.inputOriginUrl + streamPath, splittedPath[2])
|
2021-06-16 15:14:41 +02:00
|
|
|
.catch(err => logger.error('Cannot handle sessions.', { err, ...lTags(sessionId) }))
|
|
|
|
})
|
|
|
|
|
|
|
|
events.on('donePublish', sessionId => {
|
|
|
|
logger.info('Live session ended.', { sessionId, ...lTags(sessionId) })
|
|
|
|
})
|
|
|
|
|
|
|
|
registerConfigChangedHandler(() => {
|
2021-11-05 11:36:03 +01:00
|
|
|
if (!this.running && CONFIG.LIVE.ENABLED === true) {
|
|
|
|
this.run().catch(err => logger.error('Cannot run live server.', { err }))
|
2021-06-16 15:14:41 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-05 11:36:03 +01:00
|
|
|
if (this.running && CONFIG.LIVE.ENABLED === false) {
|
2021-06-16 15:14:41 +02:00
|
|
|
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() }))
|
|
|
|
}
|
|
|
|
|
2021-11-05 11:36:03 +01:00
|
|
|
async run () {
|
|
|
|
this.running = true
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2021-11-05 11:36:03 +01:00
|
|
|
if (CONFIG.LIVE.RTMP.ENABLED) {
|
|
|
|
logger.info('Running RTMP server on port %d', CONFIG.LIVE.RTMP.PORT, lTags())
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2021-11-05 11:36:03 +01:00
|
|
|
this.rtmpServer = createServer(socket => {
|
|
|
|
const session = new NodeRtmpSession(config, socket)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2021-11-05 11:36:03 +01:00
|
|
|
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() })
|
|
|
|
})
|
|
|
|
|
2022-05-02 14:32:12 +02:00
|
|
|
this.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT, CONFIG.LIVE.RTMP.HOSTNAME)
|
2021-11-05 11:36:03 +01:00
|
|
|
}
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2021-11-05 11:36:03 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
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() })
|
|
|
|
})
|
|
|
|
|
2022-05-02 14:32:12 +02:00
|
|
|
this.rtmpsServer.listen(CONFIG.LIVE.RTMPS.PORT, CONFIG.LIVE.RTMPS.HOSTNAME)
|
2021-11-05 11:36:03 +01:00
|
|
|
}
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stop () {
|
2021-11-05 11:36:03 +01:00
|
|
|
this.running = false
|
|
|
|
|
2021-11-05 11:40:49 +01:00
|
|
|
if (this.rtmpServer) {
|
|
|
|
logger.info('Stopping RTMP server.', lTags())
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2021-11-05 11:40:49 +01:00
|
|
|
this.rtmpServer.close()
|
|
|
|
this.rtmpServer = undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.rtmpsServer) {
|
|
|
|
logger.info('Stopping RTMPS server.', lTags())
|
|
|
|
|
|
|
|
this.rtmpsServer.close()
|
|
|
|
this.rtmpsServer = undefined
|
|
|
|
}
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
// Sessions is an object
|
|
|
|
this.getContext().sessions.forEach((session: any) => {
|
|
|
|
if (session instanceof NodeRtmpSession) {
|
|
|
|
session.stop()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
isRunning () {
|
|
|
|
return !!this.rtmpServer
|
|
|
|
}
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
stopSessionOf (videoId: number, error: LiveVideoError | null) {
|
2021-06-16 15:14:41 +02:00
|
|
|
const sessionId = this.videoSessions.get(videoId)
|
|
|
|
if (!sessionId) return
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
this.saveEndingSession(videoId, error)
|
|
|
|
.catch(err => logger.error('Cannot save ending session.', { err, ...lTags(sessionId) }))
|
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
this.videoSessions.delete(videoId)
|
|
|
|
this.abortSession(sessionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
private getContext () {
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
|
|
|
|
private abortSession (sessionId: string) {
|
|
|
|
const session = this.getContext().sessions.get(sessionId)
|
|
|
|
if (session) {
|
|
|
|
session.stop()
|
|
|
|
this.getContext().sessions.delete(sessionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
const muxingSession = this.muxingSessions.get(sessionId)
|
2021-06-17 09:40:45 +02:00
|
|
|
if (muxingSession) {
|
2021-06-17 17:08:47 +02:00
|
|
|
// Muxing session will fire and event so we correctly cleanup the session
|
2021-06-17 09:40:45 +02:00
|
|
|
muxingSession.abort()
|
|
|
|
|
|
|
|
this.muxingSessions.delete(sessionId)
|
|
|
|
}
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
2021-11-05 11:36:03 +01:00
|
|
|
private async handleSession (sessionId: string, inputUrl: string, streamKey: string) {
|
2021-06-16 15:14:41 +02:00
|
|
|
const videoLive = await VideoLiveModel.loadByStreamKey(streamKey)
|
|
|
|
if (!videoLive) {
|
|
|
|
logger.warn('Unknown live video with stream key %s.', streamKey, lTags(sessionId))
|
|
|
|
return this.abortSession(sessionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
const video = videoLive.Video
|
|
|
|
if (video.isBlacklisted()) {
|
|
|
|
logger.warn('Video is blacklisted. Refusing stream %s.', streamKey, lTags(sessionId, video.uuid))
|
|
|
|
return this.abortSession(sessionId)
|
|
|
|
}
|
|
|
|
|
2022-06-24 09:34:56 +02:00
|
|
|
// Cleanup old potential live (could happen with a permanent live)
|
2021-06-16 15:14:41 +02:00
|
|
|
const oldStreamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
|
|
|
|
if (oldStreamingPlaylist) {
|
2022-05-25 14:54:16 +02:00
|
|
|
if (!videoLive.permanentLive) throw new Error('Found previous session in a non permanent live: ' + video.uuid)
|
|
|
|
|
|
|
|
await cleanupPermanentLive(video, oldStreamingPlaylist)
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.videoSessions.set(video.id, sessionId)
|
|
|
|
|
2021-08-06 10:39:40 +02:00
|
|
|
const now = Date.now()
|
2021-11-05 11:36:03 +01:00
|
|
|
const probe = await ffprobePromise(inputUrl)
|
2021-08-06 10:39:40 +02:00
|
|
|
|
2022-09-28 10:23:03 +02:00
|
|
|
const [ { resolution, ratio }, fps, bitrate, hasAudio ] = await Promise.all([
|
2022-02-11 10:51:33 +01:00
|
|
|
getVideoStreamDimensionsInfo(inputUrl, probe),
|
|
|
|
getVideoStreamFPS(inputUrl, probe),
|
2022-09-28 10:23:03 +02:00
|
|
|
getVideoStreamBitrate(inputUrl, probe),
|
|
|
|
hasAudioStream(inputUrl, probe)
|
2021-06-16 15:14:41 +02:00
|
|
|
])
|
|
|
|
|
2021-08-06 10:39:40 +02:00
|
|
|
logger.info(
|
|
|
|
'%s probing took %d ms (bitrate: %d, fps: %d, resolution: %d)',
|
2021-11-05 11:36:03 +01:00
|
|
|
inputUrl, Date.now() - now, bitrate, fps, resolution, lTags(sessionId, video.uuid)
|
2021-08-06 10:39:40 +02:00
|
|
|
)
|
|
|
|
|
2022-08-02 16:05:44 +02:00
|
|
|
const allResolutions = await Hooks.wrapObject(
|
|
|
|
this.buildAllResolutionsToTranscode(resolution),
|
2022-08-05 13:40:56 +02:00
|
|
|
'filter:transcoding.auto.resolutions-to-transcode.result',
|
2022-08-02 16:05:44 +02:00
|
|
|
{ video }
|
|
|
|
)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
logger.info(
|
2021-08-06 13:35:25 +02:00
|
|
|
'Will mux/transcode live video of original resolution %d.', resolution,
|
2021-06-16 15:14:41 +02:00
|
|
|
{ allResolutions, ...lTags(sessionId, video.uuid) }
|
|
|
|
)
|
|
|
|
|
|
|
|
const streamingPlaylist = await this.createLivePlaylist(video, allResolutions)
|
|
|
|
|
|
|
|
return this.runMuxingSession({
|
|
|
|
sessionId,
|
|
|
|
videoLive,
|
2022-09-28 10:23:03 +02:00
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
streamingPlaylist,
|
2021-11-05 11:36:03 +01:00
|
|
|
inputUrl,
|
2021-06-16 15:14:41 +02:00
|
|
|
fps,
|
2021-08-06 10:39:40 +02:00
|
|
|
bitrate,
|
2021-08-06 13:35:25 +02:00
|
|
|
ratio,
|
2022-09-28 10:23:03 +02:00
|
|
|
allResolutions,
|
|
|
|
hasAudio
|
2021-06-16 15:14:41 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private async runMuxingSession (options: {
|
|
|
|
sessionId: string
|
|
|
|
videoLive: MVideoLiveVideo
|
2022-09-28 10:23:03 +02:00
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
streamingPlaylist: MStreamingPlaylistVideo
|
2021-11-05 11:36:03 +01:00
|
|
|
inputUrl: string
|
2021-06-16 15:14:41 +02:00
|
|
|
fps: number
|
2021-08-06 10:39:40 +02:00
|
|
|
bitrate: number
|
2021-08-06 13:35:25 +02:00
|
|
|
ratio: number
|
2021-06-16 15:14:41 +02:00
|
|
|
allResolutions: number[]
|
2022-09-28 10:23:03 +02:00
|
|
|
hasAudio: boolean
|
2021-06-16 15:14:41 +02:00
|
|
|
}) {
|
2022-09-28 10:23:03 +02:00
|
|
|
const { sessionId, videoLive } = options
|
2021-06-16 15:14:41 +02:00
|
|
|
const videoUUID = videoLive.Video.uuid
|
|
|
|
const localLTags = lTags(sessionId, videoUUID)
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
const liveSession = await this.saveStartingSession(videoLive)
|
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
const user = await UserModel.loadByLiveId(videoLive.id)
|
|
|
|
LiveQuotaStore.Instance.addNewLive(user.id, videoLive.id)
|
|
|
|
|
|
|
|
const muxingSession = new MuxingSession({
|
|
|
|
context: this.getContext(),
|
|
|
|
sessionId,
|
|
|
|
videoLive,
|
2022-09-28 10:23:03 +02:00
|
|
|
user,
|
|
|
|
|
|
|
|
...pick(options, [ 'streamingPlaylist', 'inputUrl', 'bitrate', 'ratio', 'fps', 'allResolutions', 'hasAudio' ])
|
2021-06-16 15:14:41 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
muxingSession.on('master-playlist-created', () => this.publishAndFederateLive(videoLive, localLTags))
|
|
|
|
|
|
|
|
muxingSession.on('bad-socket-health', ({ videoId }) => {
|
|
|
|
logger.error(
|
|
|
|
'Too much data in client socket stream (ffmpeg is too slow to transcode the video).' +
|
|
|
|
' Stopping session of video %s.', videoUUID,
|
|
|
|
localLTags
|
|
|
|
)
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
this.stopSessionOf(videoId, LiveVideoError.BAD_SOCKET_HEALTH)
|
2021-06-16 15:14:41 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
muxingSession.on('duration-exceeded', ({ videoId }) => {
|
|
|
|
logger.info('Stopping session of %s: max duration exceeded.', videoUUID, localLTags)
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
this.stopSessionOf(videoId, LiveVideoError.DURATION_EXCEEDED)
|
2021-06-16 15:14:41 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
muxingSession.on('quota-exceeded', ({ videoId }) => {
|
|
|
|
logger.info('Stopping session of %s: user quota exceeded.', videoUUID, localLTags)
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
this.stopSessionOf(videoId, LiveVideoError.QUOTA_EXCEEDED)
|
|
|
|
})
|
|
|
|
|
|
|
|
muxingSession.on('ffmpeg-error', ({ videoId }) => {
|
|
|
|
this.stopSessionOf(videoId, LiveVideoError.FFMPEG_ERROR)
|
2021-06-16 15:14:41 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
muxingSession.on('ffmpeg-end', ({ videoId }) => {
|
2022-05-03 11:38:07 +02:00
|
|
|
this.onMuxingFFmpegEnd(videoId, sessionId)
|
2021-06-16 15:14:41 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
muxingSession.on('after-cleanup', ({ videoId }) => {
|
|
|
|
this.muxingSessions.delete(sessionId)
|
|
|
|
|
2022-05-04 10:07:06 +02:00
|
|
|
LiveQuotaStore.Instance.removeLive(user.id, videoLive.id)
|
|
|
|
|
2021-06-17 09:40:45 +02:00
|
|
|
muxingSession.destroy()
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
return this.onAfterMuxingCleanup({ videoId, liveSession })
|
2021-06-16 15:14:41 +02:00
|
|
|
.catch(err => logger.error('Error in end transmuxing.', { err, ...localLTags }))
|
|
|
|
})
|
|
|
|
|
|
|
|
this.muxingSessions.set(sessionId, muxingSession)
|
|
|
|
|
|
|
|
muxingSession.runMuxing()
|
|
|
|
.catch(err => {
|
|
|
|
logger.error('Cannot run muxing.', { err, ...localLTags })
|
|
|
|
this.abortSession(sessionId)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private async publishAndFederateLive (live: MVideoLiveVideo, localLTags: { tags: string[] }) {
|
|
|
|
const videoId = live.videoId
|
|
|
|
|
|
|
|
try {
|
2022-06-28 14:57:51 +02:00
|
|
|
const video = await VideoModel.loadFull(videoId)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
logger.info('Will publish and federate live %s.', video.url, localLTags)
|
|
|
|
|
|
|
|
video.state = VideoState.PUBLISHED
|
2021-11-26 17:36:38 +01:00
|
|
|
video.publishedAt = new Date()
|
2021-06-16 15:14:41 +02:00
|
|
|
await video.save()
|
|
|
|
|
|
|
|
live.Video = video
|
|
|
|
|
2022-04-21 09:06:52 +02:00
|
|
|
await wait(getLiveSegmentTime(live.latencyMode) * 1000 * VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2022-04-21 09:06:52 +02:00
|
|
|
try {
|
|
|
|
await federateVideoIfNeeded(video, false)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot federate live video %s.', video.url, { err, ...localLTags })
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerTubeSocket.Instance.sendVideoLiveNewState(video)
|
2021-06-16 15:14:41 +02:00
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot save/federate live video %d.', videoId, { err, ...localLTags })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
private onMuxingFFmpegEnd (videoId: number, sessionId: string) {
|
2021-06-16 15:14:41 +02:00
|
|
|
this.videoSessions.delete(videoId)
|
2022-05-03 11:38:07 +02:00
|
|
|
|
|
|
|
this.saveEndingSession(videoId, null)
|
|
|
|
.catch(err => logger.error('Cannot save ending session.', { err, ...lTags(sessionId) }))
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 09:06:52 +02:00
|
|
|
private async onAfterMuxingCleanup (options: {
|
|
|
|
videoId: number | string
|
2022-05-03 11:38:07 +02:00
|
|
|
liveSession?: MVideoLiveSession
|
2022-04-21 09:06:52 +02:00
|
|
|
cleanupNow?: boolean // Default false
|
|
|
|
}) {
|
2022-05-03 11:38:07 +02:00
|
|
|
const { videoId, liveSession: liveSessionArg, cleanupNow = false } = options
|
2022-04-21 09:06:52 +02:00
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
try {
|
2022-06-28 14:57:51 +02:00
|
|
|
const fullVideo = await VideoModel.loadFull(videoId)
|
2021-06-16 15:14:41 +02:00
|
|
|
if (!fullVideo) return
|
|
|
|
|
|
|
|
const live = await VideoLiveModel.loadByVideoId(fullVideo.id)
|
|
|
|
|
2022-07-12 16:30:59 +02:00
|
|
|
const liveSession = liveSessionArg ?? await VideoLiveSessionModel.findLatestSessionOf(fullVideo.id)
|
2022-05-03 11:38:07 +02:00
|
|
|
|
|
|
|
// On server restart during a live
|
|
|
|
if (!liveSession.endDate) {
|
|
|
|
liveSession.endDate = new Date()
|
|
|
|
await liveSession.save()
|
|
|
|
}
|
|
|
|
|
2022-08-08 15:48:17 +02:00
|
|
|
JobQueue.Instance.createJobAsync({
|
2022-04-21 09:06:52 +02:00
|
|
|
type: 'video-live-ending',
|
|
|
|
payload: {
|
|
|
|
videoId: fullVideo.id,
|
2022-05-03 11:38:07 +02:00
|
|
|
|
2022-04-21 09:06:52 +02:00
|
|
|
replayDirectory: live.saveReplay
|
|
|
|
? await this.findReplayDirectory(fullVideo)
|
|
|
|
: undefined,
|
2022-05-03 11:38:07 +02:00
|
|
|
|
|
|
|
liveSessionId: liveSession.id,
|
2022-06-16 13:39:57 +02:00
|
|
|
streamingPlaylistId: fullVideo.getHLSPlaylist()?.id,
|
2022-05-03 11:38:07 +02:00
|
|
|
|
2022-04-21 09:06:52 +02:00
|
|
|
publishedAt: fullVideo.publishedAt.toISOString()
|
2022-08-08 15:48:17 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
delay: cleanupNow
|
|
|
|
? 0
|
|
|
|
: VIDEO_LIVE.CLEANUP_DELAY
|
|
|
|
})
|
2022-04-21 09:06:52 +02:00
|
|
|
|
|
|
|
fullVideo.state = live.permanentLive
|
|
|
|
? VideoState.WAITING_FOR_LIVE
|
|
|
|
: VideoState.LIVE_ENDED
|
2021-06-16 15:14:41 +02:00
|
|
|
|
|
|
|
await fullVideo.save()
|
|
|
|
|
|
|
|
PeerTubeSocket.Instance.sendVideoLiveNewState(fullVideo)
|
|
|
|
|
|
|
|
await federateVideoIfNeeded(fullVideo, false)
|
|
|
|
} catch (err) {
|
2022-04-21 09:06:52 +02:00
|
|
|
logger.error('Cannot save/federate new video state of live streaming of video %d.', videoId, { err, ...lTags(videoId + '') })
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async handleBrokenLives () {
|
|
|
|
const videoUUIDs = await VideoModel.listPublishedLiveUUIDs()
|
|
|
|
|
|
|
|
for (const uuid of videoUUIDs) {
|
2022-04-21 09:06:52 +02:00
|
|
|
await this.onAfterMuxingCleanup({ videoId: uuid, cleanupNow: true })
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 09:06:52 +02:00
|
|
|
private async findReplayDirectory (video: MVideo) {
|
|
|
|
const directory = getLiveReplayBaseDirectory(video)
|
|
|
|
const files = await readdir(directory)
|
|
|
|
|
|
|
|
if (files.length === 0) return undefined
|
|
|
|
|
|
|
|
return join(directory, files.sort().reverse()[0])
|
|
|
|
}
|
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
private buildAllResolutionsToTranscode (originResolution: number) {
|
2022-08-05 15:05:20 +02:00
|
|
|
const includeInput = CONFIG.LIVE.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
|
2022-08-05 10:36:19 +02:00
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
const resolutionsEnabled = CONFIG.LIVE.TRANSCODING.ENABLED
|
2022-08-05 15:05:20 +02:00
|
|
|
? computeResolutionsToTranscode({ input: originResolution, type: 'live', includeInput, strictLower: false })
|
2021-06-16 15:14:41 +02:00
|
|
|
: []
|
|
|
|
|
2022-08-05 10:36:19 +02:00
|
|
|
if (resolutionsEnabled.length === 0) {
|
|
|
|
return [ originResolution ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolutionsEnabled
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 11:20:00 +02:00
|
|
|
private async createLivePlaylist (video: MVideo, allResolutions: number[]): Promise<MStreamingPlaylistVideo> {
|
|
|
|
const playlist = await VideoStreamingPlaylistModel.loadOrGenerate(video)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2021-07-23 11:20:00 +02:00
|
|
|
playlist.playlistFilename = generateHLSMasterPlaylistFilename(true)
|
|
|
|
playlist.segmentsSha256Filename = generateHlsSha256SegmentsFilename(true)
|
2021-06-16 15:14:41 +02:00
|
|
|
|
2021-07-23 11:20:00 +02:00
|
|
|
playlist.p2pMediaLoaderPeerVersion = P2P_MEDIA_LOADER_PEER_VERSION
|
|
|
|
playlist.type = VideoStreamingPlaylistType.HLS
|
|
|
|
|
|
|
|
playlist.assignP2PMediaLoaderInfoHashes(video, allResolutions)
|
|
|
|
|
|
|
|
return playlist.save()
|
2021-06-16 15:14:41 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
private saveStartingSession (videoLive: MVideoLiveVideo) {
|
|
|
|
const liveSession = new VideoLiveSessionModel({
|
|
|
|
startDate: new Date(),
|
2022-07-22 15:22:21 +02:00
|
|
|
liveVideoId: videoLive.videoId,
|
|
|
|
saveReplay: videoLive.saveReplay,
|
|
|
|
endingProcessed: false
|
2022-05-03 11:38:07 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return liveSession.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
private async saveEndingSession (videoId: number, error: LiveVideoError | null) {
|
|
|
|
const liveSession = await VideoLiveSessionModel.findCurrentSessionOf(videoId)
|
2022-07-18 15:49:15 +02:00
|
|
|
if (!liveSession) return
|
|
|
|
|
2022-05-03 11:38:07 +02:00
|
|
|
liveSession.endDate = new Date()
|
|
|
|
liveSession.error = error
|
|
|
|
|
|
|
|
return liveSession.save()
|
|
|
|
}
|
|
|
|
|
2021-06-16 15:14:41 +02:00
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
LiveManager
|
|
|
|
}
|