import { UserModel } from '@server/models/user/user'
import { VideoModel } from '@server/models/video/video'
import { VideoLiveModel } from '@server/models/video/video-live'
+import { VideoLiveSessionModel } from '@server/models/video/video-live-session'
import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
-import { MStreamingPlaylistVideo, MVideo, MVideoLiveVideo } from '@server/types/models'
+import { MStreamingPlaylistVideo, MVideo, MVideoLiveSession, MVideoLiveVideo } from '@server/types/models'
import { wait } from '@shared/core-utils'
-import { VideoState, VideoStreamingPlaylistType } from '@shared/models'
+import { LiveVideoError, VideoState, VideoStreamingPlaylistType } from '@shared/models'
import { federateVideoIfNeeded } from '../activitypub/videos'
import { JobQueue } from '../job-queue'
import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getLiveReplayBaseDirectory } 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 { cleanupPermanentLive } from './live-utils'
import { MuxingSession } from './shared'
const NodeRtmpSession = require('node-media-server/src/node_rtmp_session')
logger.error('Cannot run RTMP server.', { err, ...lTags() })
})
- this.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT)
+ this.rtmpServer.listen(CONFIG.LIVE.RTMP.PORT, CONFIG.LIVE.RTMP.HOSTNAME)
}
if (CONFIG.LIVE.RTMPS.ENABLED) {
logger.error('Cannot run RTMPS server.', { err, ...lTags() })
})
- this.rtmpsServer.listen(CONFIG.LIVE.RTMPS.PORT)
+ this.rtmpsServer.listen(CONFIG.LIVE.RTMPS.PORT, CONFIG.LIVE.RTMPS.HOSTNAME)
}
}
return !!this.rtmpServer
}
- stopSessionOf (videoId: number) {
+ stopSessionOf (videoId: number, error: LiveVideoError | null) {
const sessionId = this.videoSessions.get(videoId)
if (!sessionId) return
+ this.saveEndingSession(videoId, error)
+ .catch(err => logger.error('Cannot save ending session.', { err, ...lTags(sessionId) }))
+
this.videoSessions.delete(videoId)
this.abortSession(sessionId)
}
return this.abortSession(sessionId)
}
- // Cleanup old potential live files (could happen with a permanent live)
- LiveSegmentShaStore.Instance.cleanupShaSegments(video.uuid)
-
+ // Cleanup old potential live (could happen with a permanent live)
const oldStreamingPlaylist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
if (oldStreamingPlaylist) {
- await cleanupLive(video, oldStreamingPlaylist)
+ if (!videoLive.permanentLive) throw new Error('Found previous session in a non permanent live: ' + video.uuid)
+
+ await cleanupPermanentLive(video, oldStreamingPlaylist)
}
this.videoSessions.set(video.id, sessionId)
const videoUUID = videoLive.Video.uuid
const localLTags = lTags(sessionId, videoUUID)
+ const liveSession = await this.saveStartingSession(videoLive)
+
const user = await UserModel.loadByLiveId(videoLive.id)
LiveQuotaStore.Instance.addNewLive(user.id, videoLive.id)
localLTags
)
- this.stopSessionOf(videoId)
+ this.stopSessionOf(videoId, LiveVideoError.BAD_SOCKET_HEALTH)
})
muxingSession.on('duration-exceeded', ({ videoId }) => {
logger.info('Stopping session of %s: max duration exceeded.', videoUUID, localLTags)
- this.stopSessionOf(videoId)
+ this.stopSessionOf(videoId, LiveVideoError.DURATION_EXCEEDED)
})
muxingSession.on('quota-exceeded', ({ videoId }) => {
logger.info('Stopping session of %s: user quota exceeded.', videoUUID, localLTags)
- this.stopSessionOf(videoId)
+ this.stopSessionOf(videoId, LiveVideoError.QUOTA_EXCEEDED)
+ })
+
+ muxingSession.on('ffmpeg-error', ({ videoId }) => {
+ this.stopSessionOf(videoId, LiveVideoError.FFMPEG_ERROR)
})
- muxingSession.on('ffmpeg-error', ({ sessionId }) => this.abortSession(sessionId))
muxingSession.on('ffmpeg-end', ({ videoId }) => {
- this.onMuxingFFmpegEnd(videoId)
+ this.onMuxingFFmpegEnd(videoId, sessionId)
})
muxingSession.on('after-cleanup', ({ videoId }) => {
this.muxingSessions.delete(sessionId)
+ LiveQuotaStore.Instance.removeLive(user.id, videoLive.id)
+
muxingSession.destroy()
- return this.onAfterMuxingCleanup({ videoId })
+ return this.onAfterMuxingCleanup({ videoId, liveSession })
.catch(err => logger.error('Error in end transmuxing.', { err, ...localLTags }))
})
const videoId = live.videoId
try {
- const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
+ const video = await VideoModel.loadFull(videoId)
logger.info('Will publish and federate live %s.', video.url, localLTags)
}
}
- private onMuxingFFmpegEnd (videoId: number) {
+ private onMuxingFFmpegEnd (videoId: number, sessionId: string) {
this.videoSessions.delete(videoId)
+
+ this.saveEndingSession(videoId, null)
+ .catch(err => logger.error('Cannot save ending session.', { err, ...lTags(sessionId) }))
}
private async onAfterMuxingCleanup (options: {
videoId: number | string
+ liveSession?: MVideoLiveSession
cleanupNow?: boolean // Default false
}) {
- const { videoId, cleanupNow = false } = options
+ const { videoId, liveSession: liveSessionArg, cleanupNow = false } = options
try {
- const fullVideo = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
+ const fullVideo = await VideoModel.loadFull(videoId)
if (!fullVideo) return
const live = await VideoLiveModel.loadByVideoId(fullVideo.id)
+ const liveSession = liveSessionArg ?? await VideoLiveSessionModel.findLatestSessionOf(fullVideo.id)
+
+ // On server restart during a live
+ if (!liveSession.endDate) {
+ liveSession.endDate = new Date()
+ await liveSession.save()
+ }
+
JobQueue.Instance.createJob({
type: 'video-live-ending',
payload: {
videoId: fullVideo.id,
+
replayDirectory: live.saveReplay
? await this.findReplayDirectory(fullVideo)
: undefined,
+
+ liveSessionId: liveSession.id,
+ streamingPlaylistId: fullVideo.getHLSPlaylist()?.id,
+
publishedAt: fullVideo.publishedAt.toISOString()
}
}, { delay: cleanupNow ? 0 : VIDEO_LIVE.CLEANUP_DELAY })
return playlist.save()
}
+ private saveStartingSession (videoLive: MVideoLiveVideo) {
+ const liveSession = new VideoLiveSessionModel({
+ startDate: new Date(),
+ liveVideoId: videoLive.videoId
+ })
+
+ return liveSession.save()
+ }
+
+ private async saveEndingSession (videoId: number, error: LiveVideoError | null) {
+ const liveSession = await VideoLiveSessionModel.findCurrentSessionOf(videoId)
+ if (!liveSession) return
+
+ liveSession.endDate = new Date()
+ liveSession.error = error
+
+ return liveSession.save()
+ }
+
static get Instance () {
return this.instance || (this.instance = new this())
}