+ const segmentsToProcessPerPlaylist: { [playlistId: string]: string[] } = {}
+ const playlistIdMatcher = /^([\d+])-/
+
+ const processHashSegments = (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 }))
+ }
+ }
+
+ const addHandler = segmentPath => {
+ logger.debug('Live add handler of %s.', segmentPath)
+
+ const playlistId = basename(segmentPath).match(playlistIdMatcher)[0]
+
+ const segmentsToProcess = segmentsToProcessPerPlaylist[playlistId] || []
+ processHashSegments(segmentsToProcess)
+
+ segmentsToProcessPerPlaylist[playlistId] = [ segmentPath ]
+
+ // Duration constraint check
+ if (this.isDurationConstraintValid(startStreamDateTime) !== true) {
+ logger.info('Stopping session of %s: max duration exceeded.', videoUUID)
+
+ this.stopSessionOf(videoLive.videoId)
+ }
+
+ // Check user quota if the user enabled replay saving
+ if (videoLive.saveReplay === true) {
+ stat(segmentPath)
+ .then(segmentStat => {
+ currentUserLive.size += segmentStat.size
+ })
+ .then(() => this.isQuotaConstraintValid(user, videoLive))
+ .then(quotaValid => {
+ if (quotaValid !== true) {
+ logger.info('Stopping session of %s: user quota exceeded.', videoUUID)
+
+ this.stopSessionOf(videoLive.videoId)
+ }
+ })
+ .catch(err => logger.error('Cannot stat %s or check quota of %d.', segmentPath, user.id, { err }))
+ }