+import * as Bluebird from 'bluebird'
import * as chokidar from 'chokidar'
import { FfmpegCommand } from 'fluent-ffmpeg'
-import { copy, ensureDir, stat } from 'fs-extra'
+import { appendFile, ensureDir, readFile, stat } from 'fs-extra'
import { basename, join } from 'path'
import { isTestInstance } from '@server/helpers/core-utils'
import { getLiveMuxingCommand, getLiveTranscodingCommand } from '@server/helpers/ffmpeg-utils'
logger.info('Running RTMP server on port %d', config.rtmp.port)
this.rtmpServer = new NodeRtmpServer(config)
+ this.rtmpServer.tcpServer.on('error', err => {
+ logger.error('Cannot run RTMP server.', { err })
+ })
+
this.rtmpServer.run()
}
this.segmentsSha256.delete(videoUUID)
}
+ addSegmentToReplay (hlsVideoPath: string, segmentPath: string) {
+ const segmentName = basename(segmentPath)
+ const dest = join(hlsVideoPath, VIDEO_LIVE.REPLAY_DIRECTORY, this.buildConcatenatedName(segmentName))
+
+ return readFile(segmentPath)
+ .then(data => appendFile(dest, data))
+ .catch(err => logger.error('Cannot copy segment %s to repay directory.', segmentPath, { err }))
+ }
+
+ buildConcatenatedName (segmentOrPlaylistPath: string) {
+ const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/)
+
+ return 'concat-' + num[1] + '.ts'
+ }
+
+ private processSegments (hlsVideoPath: string, videoUUID: string, videoLive: MVideoLive, segmentPaths: string[]) {
+ Bluebird.mapSeries(segmentPaths, async previousSegment => {
+ // Add sha hash of previous segments, because ffmpeg should have finished generating them
+ await this.addSegmentSha(videoUUID, previousSegment)
+
+ if (videoLive.saveReplay) {
+ await this.addSegmentToReplay(hlsVideoPath, previousSegment)
+ }
+ }).catch(err => logger.error('Cannot process segments in %s', hlsVideoPath, { err }))
+ }
+
private getContext () {
return context
}
for (let i = 0; i < allResolutions.length; i++) {
const resolution = allResolutions[i]
- VideoFileModel.upsert({
+ const file = new VideoFileModel({
resolution,
size: -1,
extname: '.ts',
infoHash: null,
fps,
videoStreamingPlaylistId: playlist.id
- }).catch(err => {
- logger.error('Cannot create file for live streaming.', { err })
})
+
+ VideoFileModel.customUpsert(file, 'streaming-playlist', null)
+ .catch(err => logger.error('Cannot create file for live streaming.', { err }))
}
const outPath = getHLSDirectory(videoLive.Video)
const segmentsToProcessPerPlaylist: { [playlistId: string]: string[] } = {}
const playlistIdMatcher = /^([\d+])-/
- const processSegments = (segmentsToProcess: string[]) => {
- // Add sha hash of previous segments, because ffmpeg should have finished generating them
- for (const previousSegment of segmentsToProcess) {
- this.addSegmentSha(videoUUID, previousSegment)
- .catch(err => logger.error('Cannot add sha segment of video %s -> %s.', videoUUID, previousSegment, { err }))
-
- if (videoLive.saveReplay) {
- const segmentName = basename(previousSegment)
-
- copy(previousSegment, join(outPath, VIDEO_LIVE.REPLAY_DIRECTORY, segmentName))
- .catch(err => logger.error('Cannot copy segment %s to repay directory.', previousSegment, { err }))
- }
- }
- }
-
const addHandler = segmentPath => {
logger.debug('Live add handler of %s.', segmentPath)
const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
const segmentsToProcess = segmentsToProcessPerPlaylist[playlistId] || []
- processSegments(segmentsToProcess)
+ this.processSegments(outPath, videoUUID, videoLive, segmentsToProcess)
segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
.then(() => {
// Process remaining segments hash
for (const key of Object.keys(segmentsToProcessPerPlaylist)) {
- processSegments(segmentsToProcessPerPlaylist[key])
+ this.processSegments(outPath, videoUUID, videoLive, segmentsToProcessPerPlaylist[key])
}
})
.catch(err => logger.error('Cannot close watchers of %s or process remaining hash segments.', outPath, { err }))
private isDurationConstraintValid (streamingStartTime: number) {
const maxDuration = CONFIG.LIVE.MAX_DURATION
// No limit
- if (maxDuration === null) return true
+ if (maxDuration < 0) return true
const now = new Date().getTime()
const max = streamingStartTime + maxDuration
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)