2023-07-31 07:34:36 -05:00
|
|
|
import { readdir, readFile } from 'fs/promises'
|
2021-06-16 08:14:41 -05:00
|
|
|
import { createServer, Server } from 'net'
|
2023-07-31 07:34:36 -05:00
|
|
|
import context from 'node-media-server/src/node_core_ctx.js'
|
|
|
|
import nodeMediaServerLogger from 'node-media-server/src/node_core_logger.js'
|
|
|
|
import NodeRtmpSession from 'node-media-server/src/node_rtmp_session.js'
|
2022-04-21 02:06:52 -05:00
|
|
|
import { join } from 'path'
|
2021-11-05 05:36:03 -05:00
|
|
|
import { createServer as createServerTLS, Server as ServerTLS } from 'tls'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { pick, wait } from '@peertube/peertube-core-utils'
|
|
|
|
import { LiveVideoError, LiveVideoErrorType, VideoState } from '@peertube/peertube-models'
|
|
|
|
import { logger, loggerTagsFactory } from '@server/helpers/logger.js'
|
|
|
|
import { CONFIG, registerConfigChangedHandler } from '@server/initializers/config.js'
|
|
|
|
import { VIDEO_LIVE, WEBSERVER } from '@server/initializers/constants.js'
|
|
|
|
import { sequelizeTypescript } from '@server/initializers/database.js'
|
|
|
|
import { RunnerJobModel } from '@server/models/runner/runner-job.js'
|
|
|
|
import { UserModel } from '@server/models/user/user.js'
|
|
|
|
import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting.js'
|
|
|
|
import { VideoLiveSessionModel } from '@server/models/video/video-live-session.js'
|
|
|
|
import { VideoLiveModel } from '@server/models/video/video-live.js'
|
|
|
|
import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist.js'
|
|
|
|
import { VideoModel } from '@server/models/video/video.js'
|
|
|
|
import { MVideo, MVideoLiveSession, MVideoLiveVideo, MVideoLiveVideoWithSetting } from '@server/types/models/index.js'
|
|
|
|
import {
|
|
|
|
ffprobePromise,
|
|
|
|
getVideoStreamBitrate,
|
|
|
|
getVideoStreamDimensionsInfo,
|
|
|
|
getVideoStreamFPS,
|
|
|
|
hasAudioStream
|
|
|
|
} from '@peertube/peertube-ffmpeg'
|
|
|
|
import { federateVideoIfNeeded } from '../activitypub/videos/index.js'
|
|
|
|
import { JobQueue } from '../job-queue/index.js'
|
|
|
|
import { getLiveReplayBaseDirectory } from '../paths.js'
|
|
|
|
import { PeerTubeSocket } from '../peertube-socket.js'
|
|
|
|
import { Hooks } from '../plugins/hooks.js'
|
|
|
|
import { computeResolutionsToTranscode } from '../transcoding/transcoding-resolutions.js'
|
|
|
|
import { LiveQuotaStore } from './live-quota-store.js'
|
|
|
|
import { cleanupAndDestroyPermanentLive, getLiveSegmentTime } from './live-utils.js'
|
|
|
|
import { MuxingSession } from './shared/index.js'
|
2021-06-16 08:14:41 -05: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>()
|
2023-04-21 07:55:10 -05:00
|
|
|
private readonly videoSessions = new Map<string, string>()
|
2021-06-16 08:14:41 -05:00
|
|
|
|
|
|
|
private rtmpServer: Server
|
2021-11-05 05:36:03 -05:00
|
|
|
private rtmpsServer: ServerTLS
|
|
|
|
|
|
|
|
private running = false
|
2021-06-16 08:14:41 -05: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 05:36:03 -05:00
|
|
|
const session = this.getContext().sessions.get(sessionId)
|
2023-05-16 02:12:50 -05:00
|
|
|
const inputLocalUrl = session.inputOriginLocalUrl + streamPath
|
|
|
|
const inputPublicUrl = session.inputOriginPublicUrl + streamPath
|
2021-11-05 05:36:03 -05:00
|
|
|
|
2023-05-16 02:12:50 -05:00
|
|
|
this.handleSession({ sessionId, inputPublicUrl, inputLocalUrl, streamKey: splittedPath[2] })
|
2021-06-16 08:14:41 -05:00
|
|
|
.catch(err => logger.error('Cannot handle sessions.', { err, ...lTags(sessionId) }))
|
|
|
|
})
|
|
|
|
|
|
|
|
events.on('donePublish', sessionId => {
|
|
|
|
logger.info('Live session ended.', { sessionId, ...lTags(sessionId) })
|
2023-05-15 08:06:14 -05:00
|
|
|
|
|
|
|
// Force session aborting, so we kill ffmpeg even if it still has data to process (slow CPU)
|
|
|
|
setTimeout(() => this.abortSession(sessionId), 2000)
|
2021-06-16 08:14:41 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
registerConfigChangedHandler(() => {
|
2021-11-05 05:36:03 -05:00
|
|
|
if (!this.running && CONFIG.LIVE.ENABLED === true) {
|
|
|
|
this.run().catch(err => logger.error('Cannot run live server.', { err }))
|
2021-06-16 08:14:41 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-05 05:36:03 -05:00
|
|
|
if (this.running && CONFIG.LIVE.ENABLED === false) {
|
2021-06-16 08:14:41 -05: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 05:36:03 -05:00
|
|
|
async run () {
|
|
|
|
this.running = true
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2021-11-05 05:36:03 -05:00
|
|
|
if (CONFIG.LIVE.RTMP.ENABLED) {
|
|
|
|
logger.info('Running RTMP server on port %d', CONFIG.LIVE.RTMP.PORT, lTags())
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2021-11-05 05:36:03 -05:00
|
|
|
this.rtmpServer = createServer(socket => {
|
|
|
|
const session = new NodeRtmpSession(config, socket)
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2023-05-16 02:12:50 -05:00
|
|
|
session.inputOriginLocalUrl = 'rtmp://127.0.0.1:' + CONFIG.LIVE.RTMP.PORT
|
|
|
|
session.inputOriginPublicUrl = WEBSERVER.RTMP_URL
|
2021-11-05 05:36:03 -05:00
|
|
|
session.run()
|
|
|
|
})
|
|
|
|
|
|
|
|
this.rtmpServer.on('error', err => {
|
|
|
|
logger.error('Cannot run RTMP server.', { err, ...lTags() })
|
|
|
|
})
|
|
|
|
|
2022-05-02 07:32:12 -05:00
|
|
|
this.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT, CONFIG.LIVE.RTMP.HOSTNAME)
|
2021-11-05 05:36:03 -05:00
|
|
|
}
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2021-11-05 05:36:03 -05: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)
|
|
|
|
|
2023-05-16 02:12:50 -05:00
|
|
|
session.inputOriginLocalUrl = 'rtmps://127.0.0.1:' + CONFIG.LIVE.RTMPS.PORT
|
|
|
|
session.inputOriginPublicUrl = WEBSERVER.RTMPS_URL
|
2021-11-05 05:36:03 -05:00
|
|
|
session.run()
|
|
|
|
})
|
|
|
|
|
|
|
|
this.rtmpsServer.on('error', err => {
|
|
|
|
logger.error('Cannot run RTMPS server.', { err, ...lTags() })
|
|
|
|
})
|
|
|
|
|
2022-05-02 07:32:12 -05:00
|
|
|
this.rtmpsServer.listen(CONFIG.LIVE.RTMPS.PORT, CONFIG.LIVE.RTMPS.HOSTNAME)
|
2021-11-05 05:36:03 -05:00
|
|
|
}
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
stop () {
|
2021-11-05 05:36:03 -05:00
|
|
|
this.running = false
|
|
|
|
|
2021-11-05 05:40:49 -05:00
|
|
|
if (this.rtmpServer) {
|
|
|
|
logger.info('Stopping RTMP server.', lTags())
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2021-11-05 05:40:49 -05: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 08:14:41 -05:00
|
|
|
|
|
|
|
// Sessions is an object
|
|
|
|
this.getContext().sessions.forEach((session: any) => {
|
|
|
|
if (session instanceof NodeRtmpSession) {
|
|
|
|
session.stop()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
isRunning () {
|
|
|
|
return !!this.rtmpServer
|
|
|
|
}
|
|
|
|
|
2023-05-22 06:44:22 -05:00
|
|
|
hasSession (sessionId: string) {
|
|
|
|
return this.getContext().sessions.has(sessionId)
|
|
|
|
}
|
|
|
|
|
2023-10-30 05:57:39 -05:00
|
|
|
stopSessionOf (options: {
|
|
|
|
videoUUID: string
|
|
|
|
error: LiveVideoErrorType | null
|
|
|
|
errorOnReplay?: boolean
|
|
|
|
}) {
|
|
|
|
const { videoUUID, error } = options
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
const sessionId = this.videoSessions.get(videoUUID)
|
|
|
|
if (!sessionId) {
|
|
|
|
logger.debug('No live session to stop for video %s', videoUUID, lTags(sessionId, videoUUID))
|
|
|
|
return
|
|
|
|
}
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
logger.info('Stopping live session of video %s', videoUUID, { error, ...lTags(sessionId, videoUUID) })
|
2022-05-03 04:38:07 -05:00
|
|
|
|
2023-10-30 05:57:39 -05:00
|
|
|
this.saveEndingSession(options)
|
2023-04-21 07:55:10 -05:00
|
|
|
.catch(err => logger.error('Cannot save ending session.', { err, ...lTags(sessionId, videoUUID) }))
|
|
|
|
|
|
|
|
this.videoSessions.delete(videoUUID)
|
2021-06-16 08:14:41 -05:00
|
|
|
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 02:40:45 -05:00
|
|
|
if (muxingSession) {
|
2021-06-17 10:08:47 -05:00
|
|
|
// Muxing session will fire and event so we correctly cleanup the session
|
2021-06-17 02:40:45 -05:00
|
|
|
muxingSession.abort()
|
|
|
|
|
|
|
|
this.muxingSessions.delete(sessionId)
|
|
|
|
}
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
|
2023-05-16 02:12:50 -05:00
|
|
|
private async handleSession (options: {
|
|
|
|
sessionId: string
|
|
|
|
inputLocalUrl: string
|
|
|
|
inputPublicUrl: string
|
|
|
|
streamKey: string
|
|
|
|
}) {
|
|
|
|
const { inputLocalUrl, inputPublicUrl, sessionId, streamKey } = options
|
|
|
|
|
2021-06-16 08:14:41 -05: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)
|
|
|
|
}
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
if (this.videoSessions.has(video.uuid)) {
|
|
|
|
logger.warn('Video %s has already a live session. Refusing stream %s.', video.uuid, streamKey, lTags(sessionId, video.uuid))
|
|
|
|
return this.abortSession(sessionId)
|
|
|
|
}
|
|
|
|
|
2022-06-24 02:34:56 -05:00
|
|
|
// Cleanup old potential live (could happen with a permanent live)
|
2021-06-16 08:14:41 -05:00
|
|
|
const oldStreamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
|
|
|
|
if (oldStreamingPlaylist) {
|
2022-05-25 07:54:16 -05:00
|
|
|
if (!videoLive.permanentLive) throw new Error('Found previous session in a non permanent live: ' + video.uuid)
|
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
await cleanupAndDestroyPermanentLive(video, oldStreamingPlaylist)
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
this.videoSessions.set(video.uuid, sessionId)
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2021-08-06 03:39:40 -05:00
|
|
|
const now = Date.now()
|
2023-05-16 02:12:50 -05:00
|
|
|
const probe = await ffprobePromise(inputLocalUrl)
|
2021-08-06 03:39:40 -05:00
|
|
|
|
2022-09-28 03:23:03 -05:00
|
|
|
const [ { resolution, ratio }, fps, bitrate, hasAudio ] = await Promise.all([
|
2023-05-16 02:12:50 -05:00
|
|
|
getVideoStreamDimensionsInfo(inputLocalUrl, probe),
|
|
|
|
getVideoStreamFPS(inputLocalUrl, probe),
|
|
|
|
getVideoStreamBitrate(inputLocalUrl, probe),
|
|
|
|
hasAudioStream(inputLocalUrl, probe)
|
2021-06-16 08:14:41 -05:00
|
|
|
])
|
|
|
|
|
2021-08-06 03:39:40 -05:00
|
|
|
logger.info(
|
|
|
|
'%s probing took %d ms (bitrate: %d, fps: %d, resolution: %d)',
|
2023-05-16 02:12:50 -05:00
|
|
|
inputLocalUrl, Date.now() - now, bitrate, fps, resolution, lTags(sessionId, video.uuid)
|
2021-08-06 03:39:40 -05:00
|
|
|
)
|
|
|
|
|
2022-08-02 09:05:44 -05:00
|
|
|
const allResolutions = await Hooks.wrapObject(
|
2022-11-07 03:25:24 -06:00
|
|
|
this.buildAllResolutionsToTranscode(resolution, hasAudio),
|
2022-08-05 06:40:56 -05:00
|
|
|
'filter:transcoding.auto.resolutions-to-transcode.result',
|
2022-08-02 09:05:44 -05:00
|
|
|
{ video }
|
|
|
|
)
|
2021-06-16 08:14:41 -05:00
|
|
|
|
|
|
|
logger.info(
|
2023-04-21 07:55:10 -05:00
|
|
|
'Handling live video of original resolution %d.', resolution,
|
2021-06-16 08:14:41 -05:00
|
|
|
{ allResolutions, ...lTags(sessionId, video.uuid) }
|
|
|
|
)
|
|
|
|
|
|
|
|
return this.runMuxingSession({
|
|
|
|
sessionId,
|
|
|
|
videoLive,
|
2022-09-28 03:23:03 -05:00
|
|
|
|
2023-05-16 02:12:50 -05:00
|
|
|
inputLocalUrl,
|
|
|
|
inputPublicUrl,
|
2021-06-16 08:14:41 -05:00
|
|
|
fps,
|
2021-08-06 03:39:40 -05:00
|
|
|
bitrate,
|
2021-08-06 06:35:25 -05:00
|
|
|
ratio,
|
2022-09-28 03:23:03 -05:00
|
|
|
allResolutions,
|
|
|
|
hasAudio
|
2021-06-16 08:14:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private async runMuxingSession (options: {
|
|
|
|
sessionId: string
|
2023-03-31 02:12:21 -05:00
|
|
|
videoLive: MVideoLiveVideoWithSetting
|
2022-09-28 03:23:03 -05:00
|
|
|
|
2023-05-16 02:12:50 -05:00
|
|
|
inputLocalUrl: string
|
|
|
|
inputPublicUrl: string
|
|
|
|
|
2021-06-16 08:14:41 -05:00
|
|
|
fps: number
|
2021-08-06 03:39:40 -05:00
|
|
|
bitrate: number
|
2021-08-06 06:35:25 -05:00
|
|
|
ratio: number
|
2021-06-16 08:14:41 -05:00
|
|
|
allResolutions: number[]
|
2022-09-28 03:23:03 -05:00
|
|
|
hasAudio: boolean
|
2021-06-16 08:14:41 -05:00
|
|
|
}) {
|
2022-09-28 03:23:03 -05:00
|
|
|
const { sessionId, videoLive } = options
|
2021-06-16 08:14:41 -05:00
|
|
|
const videoUUID = videoLive.Video.uuid
|
|
|
|
const localLTags = lTags(sessionId, videoUUID)
|
|
|
|
|
2022-05-03 04:38:07 -05:00
|
|
|
const liveSession = await this.saveStartingSession(videoLive)
|
|
|
|
|
2021-06-16 08:14:41 -05:00
|
|
|
const user = await UserModel.loadByLiveId(videoLive.id)
|
2023-05-22 08:52:59 -05:00
|
|
|
LiveQuotaStore.Instance.addNewLive(user.id, sessionId)
|
2021-06-16 08:14:41 -05:00
|
|
|
|
|
|
|
const muxingSession = new MuxingSession({
|
|
|
|
context: this.getContext(),
|
|
|
|
sessionId,
|
|
|
|
videoLive,
|
2022-09-28 03:23:03 -05:00
|
|
|
user,
|
|
|
|
|
2023-05-16 02:12:50 -05:00
|
|
|
...pick(options, [ 'inputLocalUrl', 'inputPublicUrl', 'bitrate', 'ratio', 'fps', 'allResolutions', 'hasAudio' ])
|
2021-06-16 08:14:41 -05:00
|
|
|
})
|
|
|
|
|
2022-10-04 03:03:17 -05:00
|
|
|
muxingSession.on('live-ready', () => this.publishAndFederateLive(videoLive, localLTags))
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
muxingSession.on('bad-socket-health', ({ videoUUID }) => {
|
2021-06-16 08:14:41 -05:00
|
|
|
logger.error(
|
|
|
|
'Too much data in client socket stream (ffmpeg is too slow to transcode the video).' +
|
|
|
|
' Stopping session of video %s.', videoUUID,
|
|
|
|
localLTags
|
|
|
|
)
|
|
|
|
|
2023-10-30 05:57:39 -05:00
|
|
|
this.stopSessionOf({ videoUUID, error: LiveVideoError.BAD_SOCKET_HEALTH })
|
2021-06-16 08:14:41 -05:00
|
|
|
})
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
muxingSession.on('duration-exceeded', ({ videoUUID }) => {
|
2021-06-16 08:14:41 -05:00
|
|
|
logger.info('Stopping session of %s: max duration exceeded.', videoUUID, localLTags)
|
|
|
|
|
2023-10-30 05:57:39 -05:00
|
|
|
this.stopSessionOf({ videoUUID, error: LiveVideoError.DURATION_EXCEEDED })
|
2021-06-16 08:14:41 -05:00
|
|
|
})
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
muxingSession.on('quota-exceeded', ({ videoUUID }) => {
|
2021-06-16 08:14:41 -05:00
|
|
|
logger.info('Stopping session of %s: user quota exceeded.', videoUUID, localLTags)
|
|
|
|
|
2023-10-30 05:57:39 -05:00
|
|
|
this.stopSessionOf({ videoUUID, error: LiveVideoError.QUOTA_EXCEEDED })
|
2022-05-03 04:38:07 -05:00
|
|
|
})
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
muxingSession.on('transcoding-error', ({ videoUUID }) => {
|
2023-10-30 05:57:39 -05:00
|
|
|
this.stopSessionOf({ videoUUID, error: LiveVideoError.FFMPEG_ERROR })
|
2021-06-16 08:14:41 -05:00
|
|
|
})
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
muxingSession.on('transcoding-end', ({ videoUUID }) => {
|
|
|
|
this.onMuxingFFmpegEnd(videoUUID, sessionId)
|
2021-06-16 08:14:41 -05:00
|
|
|
})
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
muxingSession.on('after-cleanup', ({ videoUUID }) => {
|
2021-06-16 08:14:41 -05:00
|
|
|
this.muxingSessions.delete(sessionId)
|
|
|
|
|
2023-05-22 08:52:59 -05:00
|
|
|
LiveQuotaStore.Instance.removeLive(user.id, sessionId)
|
2022-05-04 03:07:06 -05:00
|
|
|
|
2021-06-17 02:40:45 -05:00
|
|
|
muxingSession.destroy()
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
return this.onAfterMuxingCleanup({ videoUUID, liveSession })
|
2021-06-16 08:14:41 -05: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 })
|
2023-10-30 05:57:39 -05:00
|
|
|
|
|
|
|
this.muxingSessions.delete(sessionId)
|
|
|
|
muxingSession.destroy()
|
|
|
|
|
|
|
|
this.stopSessionOf({
|
|
|
|
videoUUID,
|
|
|
|
error: err.liveVideoErrorCode || LiveVideoError.UNKNOWN_ERROR,
|
|
|
|
errorOnReplay: true // Replay cannot be processed as muxing session failed directly
|
|
|
|
})
|
2021-06-16 08:14:41 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private async publishAndFederateLive (live: MVideoLiveVideo, localLTags: { tags: string[] }) {
|
|
|
|
const videoId = live.videoId
|
|
|
|
|
|
|
|
try {
|
2022-06-28 07:57:51 -05:00
|
|
|
const video = await VideoModel.loadFull(videoId)
|
2021-06-16 08:14:41 -05:00
|
|
|
|
|
|
|
logger.info('Will publish and federate live %s.', video.url, localLTags)
|
|
|
|
|
|
|
|
video.state = VideoState.PUBLISHED
|
2021-11-26 10:36:38 -06:00
|
|
|
video.publishedAt = new Date()
|
2021-06-16 08:14:41 -05:00
|
|
|
await video.save()
|
|
|
|
|
|
|
|
live.Video = video
|
|
|
|
|
2022-04-21 02:06:52 -05:00
|
|
|
await wait(getLiveSegmentTime(live.latencyMode) * 1000 * VIDEO_LIVE.EDGE_LIVE_DELAY_SEGMENTS_NOTIFICATION)
|
2021-06-16 08:14:41 -05:00
|
|
|
|
2022-04-21 02:06:52 -05:00
|
|
|
try {
|
|
|
|
await federateVideoIfNeeded(video, false)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot federate live video %s.', video.url, { err, ...localLTags })
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerTubeSocket.Instance.sendVideoLiveNewState(video)
|
Add Podcast RSS feeds (#5487)
* Initial test implementation of Podcast RSS
This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option.
I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort.
* Update to pfeed-podcast 1.2.2
* Initial test implementation of Podcast RSS
This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option.
I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort.
* Update to pfeed-podcast 1.2.2
* Initial test implementation of Podcast RSS
This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option.
I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort.
* Update to pfeed-podcast 1.2.2
* Add correct feed image to RSS channel
* Prefer HLS videos for podcast RSS
Remove video/stream titles, add optional height attribute to podcast RSS
* Prefix podcast RSS images with root server URL
* Add optional video query support to include captions
* Add transcripts & person images to podcast RSS feed
* Prefer webseed/webtorrent files over HLS fragmented mp4s
* Experimentally adding podcast fields to basic config page
* Add validation for new basic config fields
* Don't include "content" in podcast feed, use full description for "description"
* Initial test implementation of Podcast RSS
This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option.
I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort.
* Update to pfeed-podcast 1.2.2
* Add correct feed image to RSS channel
* Prefer HLS videos for podcast RSS
Remove video/stream titles, add optional height attribute to podcast RSS
* Prefix podcast RSS images with root server URL
* Add optional video query support to include captions
* Add transcripts & person images to podcast RSS feed
* Prefer webseed/webtorrent files over HLS fragmented mp4s
* Experimentally adding podcast fields to basic config page
* Add validation for new basic config fields
* Don't include "content" in podcast feed, use full description for "description"
* Add medium/socialInteract to podcast RSS feeds. Use HTML for description
* Change base production image to bullseye, install prosody in image
* Add liveItem and trackers to Podcast RSS feeds
Remove height from alternateEnclosure, replaced with title.
* Clear Podcast RSS feed cache when live streams start/end
* Upgrade to Node 16
* Refactor clearCacheRoute to use ApiCache
* Remove unnecessary type hint
* Update dockerfile to node 16, install python-is-python2
* Use new file paths for captions/playlists
* Fix legacy videos in RSS after migration to object storage
* Improve method of identifying non-fragmented mp4s in podcast RSS feeds
* Don't include fragmented MP4s in podcast RSS feeds
* Add experimental support for podcast:categories on the podcast RSS item
* Fix undefined category when no videos exist
Allows for empty feeds to exist (important for feeds that might only go live)
* Add support for podcast:locked -- user has to opt in to show their email
* Use comma for podcast:categories delimiter
* Make cache clearing async
* Fix merge, temporarily test with pfeed-podcast
* Syntax changes
* Add EXT_MIMETYPE constants for captions
* Update & fix tests, fix enclosure mimetypes, remove admin email
* Add test for podacst:socialInteract
* Add filters hooks for podcast customTags
* Remove showdown, updated to pfeed-podcast 6.1.2
* Add 'action:api.live-video.state.updated' hook
* Avoid assigning undefined category to podcast feeds
* Remove nvmrc
* Remove comment
* Remove unused podcast config
* Remove more unused podcast config
* Fix MChannelAccountDefault type hint missed in merge
* Remove extra line
* Re-add newline in config
* Fix lint errors for isEmailPublic
* Fix thumbnails in podcast feeds
* Requested changes based on review
* Provide podcast rss 2.0 only on video channels
* Misc cleanup for a less messy PR
* Lint fixes
* Remove pfeed-podcast
* Add peertube version to new hooks
* Don't use query include, remove TODO
* Remove film medium hack
* Clear podcast rss cache before video/channel update hooks
* Clear podcast rss cache before video uploaded/deleted hooks
* Refactor podcast feed cache clearing
* Set correct person name from video channel
* Styling
* Fix tests
---------
Co-authored-by: Chocobozzz <me@florianbigard.com>
2023-05-22 09:00:05 -05:00
|
|
|
|
|
|
|
Hooks.runAction('action:live.video.state.updated', { video })
|
2021-06-16 08:14:41 -05:00
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot save/federate live video %d.', videoId, { err, ...localLTags })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
private onMuxingFFmpegEnd (videoUUID: string, sessionId: string) {
|
2023-05-15 06:56:00 -05:00
|
|
|
// Session already cleaned up
|
|
|
|
if (!this.videoSessions.has(videoUUID)) return
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
this.videoSessions.delete(videoUUID)
|
2022-05-03 04:38:07 -05:00
|
|
|
|
2023-10-30 05:57:39 -05:00
|
|
|
this.saveEndingSession({ videoUUID, error: null })
|
2022-05-03 04:38:07 -05:00
|
|
|
.catch(err => logger.error('Cannot save ending session.', { err, ...lTags(sessionId) }))
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
|
2022-04-21 02:06:52 -05:00
|
|
|
private async onAfterMuxingCleanup (options: {
|
2023-04-21 07:55:10 -05:00
|
|
|
videoUUID: string
|
2022-05-03 04:38:07 -05:00
|
|
|
liveSession?: MVideoLiveSession
|
2022-04-21 02:06:52 -05:00
|
|
|
cleanupNow?: boolean // Default false
|
|
|
|
}) {
|
2023-04-21 07:55:10 -05:00
|
|
|
const { videoUUID, liveSession: liveSessionArg, cleanupNow = false } = options
|
|
|
|
|
|
|
|
logger.debug('Live of video %s has been cleaned up. Moving to its next state.', videoUUID, lTags(videoUUID))
|
2022-04-21 02:06:52 -05:00
|
|
|
|
2021-06-16 08:14:41 -05:00
|
|
|
try {
|
2023-04-21 07:55:10 -05:00
|
|
|
const fullVideo = await VideoModel.loadFull(videoUUID)
|
2021-06-16 08:14:41 -05:00
|
|
|
if (!fullVideo) return
|
|
|
|
|
|
|
|
const live = await VideoLiveModel.loadByVideoId(fullVideo.id)
|
|
|
|
|
2022-07-12 09:30:59 -05:00
|
|
|
const liveSession = liveSessionArg ?? await VideoLiveSessionModel.findLatestSessionOf(fullVideo.id)
|
2022-05-03 04:38:07 -05:00
|
|
|
|
|
|
|
// On server restart during a live
|
|
|
|
if (!liveSession.endDate) {
|
|
|
|
liveSession.endDate = new Date()
|
|
|
|
await liveSession.save()
|
|
|
|
}
|
|
|
|
|
2022-08-08 08:48:17 -05:00
|
|
|
JobQueue.Instance.createJobAsync({
|
2022-04-21 02:06:52 -05:00
|
|
|
type: 'video-live-ending',
|
|
|
|
payload: {
|
|
|
|
videoId: fullVideo.id,
|
2022-05-03 04:38:07 -05:00
|
|
|
|
2022-04-21 02:06:52 -05:00
|
|
|
replayDirectory: live.saveReplay
|
|
|
|
? await this.findReplayDirectory(fullVideo)
|
|
|
|
: undefined,
|
2022-05-03 04:38:07 -05:00
|
|
|
|
|
|
|
liveSessionId: liveSession.id,
|
2022-06-16 06:39:57 -05:00
|
|
|
streamingPlaylistId: fullVideo.getHLSPlaylist()?.id,
|
2022-05-03 04:38:07 -05:00
|
|
|
|
2022-04-21 02:06:52 -05:00
|
|
|
publishedAt: fullVideo.publishedAt.toISOString()
|
2022-08-08 08:48:17 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
delay: cleanupNow
|
|
|
|
? 0
|
|
|
|
: VIDEO_LIVE.CLEANUP_DELAY
|
|
|
|
})
|
2022-04-21 02:06:52 -05:00
|
|
|
|
|
|
|
fullVideo.state = live.permanentLive
|
|
|
|
? VideoState.WAITING_FOR_LIVE
|
|
|
|
: VideoState.LIVE_ENDED
|
2021-06-16 08:14:41 -05:00
|
|
|
|
|
|
|
await fullVideo.save()
|
|
|
|
|
|
|
|
PeerTubeSocket.Instance.sendVideoLiveNewState(fullVideo)
|
|
|
|
|
|
|
|
await federateVideoIfNeeded(fullVideo, false)
|
Add Podcast RSS feeds (#5487)
* Initial test implementation of Podcast RSS
This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option.
I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort.
* Update to pfeed-podcast 1.2.2
* Initial test implementation of Podcast RSS
This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option.
I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort.
* Update to pfeed-podcast 1.2.2
* Initial test implementation of Podcast RSS
This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option.
I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort.
* Update to pfeed-podcast 1.2.2
* Add correct feed image to RSS channel
* Prefer HLS videos for podcast RSS
Remove video/stream titles, add optional height attribute to podcast RSS
* Prefix podcast RSS images with root server URL
* Add optional video query support to include captions
* Add transcripts & person images to podcast RSS feed
* Prefer webseed/webtorrent files over HLS fragmented mp4s
* Experimentally adding podcast fields to basic config page
* Add validation for new basic config fields
* Don't include "content" in podcast feed, use full description for "description"
* Initial test implementation of Podcast RSS
This is a pretty simple implementation to add support for The Podcast Namespace in RSS -- instead of affecting the existing RSS implementation, this adds a new UI option.
I attempted to retain compatibility with the rest of the RSS feed implementation as much as possible and have created a temporary fork of the "pfeed" library to support this effort.
* Update to pfeed-podcast 1.2.2
* Add correct feed image to RSS channel
* Prefer HLS videos for podcast RSS
Remove video/stream titles, add optional height attribute to podcast RSS
* Prefix podcast RSS images with root server URL
* Add optional video query support to include captions
* Add transcripts & person images to podcast RSS feed
* Prefer webseed/webtorrent files over HLS fragmented mp4s
* Experimentally adding podcast fields to basic config page
* Add validation for new basic config fields
* Don't include "content" in podcast feed, use full description for "description"
* Add medium/socialInteract to podcast RSS feeds. Use HTML for description
* Change base production image to bullseye, install prosody in image
* Add liveItem and trackers to Podcast RSS feeds
Remove height from alternateEnclosure, replaced with title.
* Clear Podcast RSS feed cache when live streams start/end
* Upgrade to Node 16
* Refactor clearCacheRoute to use ApiCache
* Remove unnecessary type hint
* Update dockerfile to node 16, install python-is-python2
* Use new file paths for captions/playlists
* Fix legacy videos in RSS after migration to object storage
* Improve method of identifying non-fragmented mp4s in podcast RSS feeds
* Don't include fragmented MP4s in podcast RSS feeds
* Add experimental support for podcast:categories on the podcast RSS item
* Fix undefined category when no videos exist
Allows for empty feeds to exist (important for feeds that might only go live)
* Add support for podcast:locked -- user has to opt in to show their email
* Use comma for podcast:categories delimiter
* Make cache clearing async
* Fix merge, temporarily test with pfeed-podcast
* Syntax changes
* Add EXT_MIMETYPE constants for captions
* Update & fix tests, fix enclosure mimetypes, remove admin email
* Add test for podacst:socialInteract
* Add filters hooks for podcast customTags
* Remove showdown, updated to pfeed-podcast 6.1.2
* Add 'action:api.live-video.state.updated' hook
* Avoid assigning undefined category to podcast feeds
* Remove nvmrc
* Remove comment
* Remove unused podcast config
* Remove more unused podcast config
* Fix MChannelAccountDefault type hint missed in merge
* Remove extra line
* Re-add newline in config
* Fix lint errors for isEmailPublic
* Fix thumbnails in podcast feeds
* Requested changes based on review
* Provide podcast rss 2.0 only on video channels
* Misc cleanup for a less messy PR
* Lint fixes
* Remove pfeed-podcast
* Add peertube version to new hooks
* Don't use query include, remove TODO
* Remove film medium hack
* Clear podcast rss cache before video/channel update hooks
* Clear podcast rss cache before video uploaded/deleted hooks
* Refactor podcast feed cache clearing
* Set correct person name from video channel
* Styling
* Fix tests
---------
Co-authored-by: Chocobozzz <me@florianbigard.com>
2023-05-22 09:00:05 -05:00
|
|
|
|
|
|
|
Hooks.runAction('action:live.video.state.updated', { video: fullVideo })
|
2021-06-16 08:14:41 -05:00
|
|
|
} catch (err) {
|
2023-04-21 07:55:10 -05:00
|
|
|
logger.error('Cannot save/federate new video state of live streaming of video %s.', videoUUID, { err, ...lTags(videoUUID) })
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async handleBrokenLives () {
|
2023-04-21 07:55:10 -05:00
|
|
|
await RunnerJobModel.cancelAllJobs({ type: 'live-rtmp-hls-transcoding' })
|
|
|
|
|
2021-06-16 08:14:41 -05:00
|
|
|
const videoUUIDs = await VideoModel.listPublishedLiveUUIDs()
|
|
|
|
|
|
|
|
for (const uuid of videoUUIDs) {
|
2023-04-21 07:55:10 -05:00
|
|
|
await this.onAfterMuxingCleanup({ videoUUID: uuid, cleanupNow: true })
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 02:06:52 -05: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])
|
|
|
|
}
|
|
|
|
|
2022-11-07 03:25:24 -06:00
|
|
|
private buildAllResolutionsToTranscode (originResolution: number, hasAudio: boolean) {
|
2022-08-05 08:05:20 -05:00
|
|
|
const includeInput = CONFIG.LIVE.TRANSCODING.ALWAYS_TRANSCODE_ORIGINAL_RESOLUTION
|
2022-08-05 03:36:19 -05:00
|
|
|
|
2021-06-16 08:14:41 -05:00
|
|
|
const resolutionsEnabled = CONFIG.LIVE.TRANSCODING.ENABLED
|
2022-11-07 03:25:24 -06:00
|
|
|
? computeResolutionsToTranscode({ input: originResolution, type: 'live', includeInput, strictLower: false, hasAudio })
|
2021-06-16 08:14:41 -05:00
|
|
|
: []
|
|
|
|
|
2022-08-05 03:36:19 -05:00
|
|
|
if (resolutionsEnabled.length === 0) {
|
|
|
|
return [ originResolution ]
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolutionsEnabled
|
2021-06-16 08:14:41 -05:00
|
|
|
}
|
|
|
|
|
2023-03-31 02:12:21 -05:00
|
|
|
private async saveStartingSession (videoLive: MVideoLiveVideoWithSetting) {
|
|
|
|
const replaySettings = videoLive.saveReplay
|
|
|
|
? new VideoLiveReplaySettingModel({
|
|
|
|
privacy: videoLive.ReplaySetting.privacy
|
|
|
|
})
|
|
|
|
: null
|
2022-05-03 04:38:07 -05:00
|
|
|
|
2023-03-31 02:12:21 -05:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
if (videoLive.saveReplay) {
|
|
|
|
await replaySettings.save({ transaction: t })
|
|
|
|
}
|
|
|
|
|
|
|
|
return VideoLiveSessionModel.create({
|
|
|
|
startDate: new Date(),
|
|
|
|
liveVideoId: videoLive.videoId,
|
|
|
|
saveReplay: videoLive.saveReplay,
|
|
|
|
replaySettingId: videoLive.saveReplay ? replaySettings.id : null,
|
|
|
|
endingProcessed: false
|
|
|
|
}, { transaction: t })
|
|
|
|
})
|
2022-05-03 04:38:07 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 05:57:39 -05:00
|
|
|
private async saveEndingSession (options: {
|
|
|
|
videoUUID: string
|
|
|
|
error: LiveVideoErrorType | null
|
|
|
|
errorOnReplay?: boolean
|
|
|
|
}) {
|
|
|
|
const { videoUUID, error, errorOnReplay } = options
|
|
|
|
|
2023-04-21 07:55:10 -05:00
|
|
|
const liveSession = await VideoLiveSessionModel.findCurrentSessionOf(videoUUID)
|
2022-07-18 08:49:15 -05:00
|
|
|
if (!liveSession) return
|
|
|
|
|
2022-05-03 04:38:07 -05:00
|
|
|
liveSession.endDate = new Date()
|
|
|
|
liveSession.error = error
|
|
|
|
|
2023-10-30 05:57:39 -05:00
|
|
|
if (errorOnReplay === true) {
|
|
|
|
liveSession.endingProcessed = true
|
|
|
|
}
|
|
|
|
|
2022-05-03 04:38:07 -05:00
|
|
|
return liveSession.save()
|
|
|
|
}
|
|
|
|
|
2021-06-16 08:14:41 -05:00
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
LiveManager
|
|
|
|
}
|