From f443a74649174b2f9347c158e30f8ac7aa3e958a Mon Sep 17 00:00:00 2001
From: Chocobozzz <me@florianbigard.com>
Date: Fri, 4 Mar 2022 13:40:02 +0100
Subject: Add latency setting support

---
 server/helpers/ffmpeg/ffmpeg-live.ts | 39 +++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)

(limited to 'server/helpers/ffmpeg/ffmpeg-live.ts')

diff --git a/server/helpers/ffmpeg/ffmpeg-live.ts b/server/helpers/ffmpeg/ffmpeg-live.ts
index ff571626c..fd20971eb 100644
--- a/server/helpers/ffmpeg/ffmpeg-live.ts
+++ b/server/helpers/ffmpeg/ffmpeg-live.ts
@@ -1,7 +1,7 @@
 import { FfmpegCommand, FilterSpecification } from 'fluent-ffmpeg'
 import { join } from 'path'
 import { VIDEO_LIVE } from '@server/initializers/constants'
-import { AvailableEncoders } from '@shared/models'
+import { AvailableEncoders, LiveVideoLatencyMode } from '@shared/models'
 import { logger, loggerTagsFactory } from '../logger'
 import { buildStreamSuffix, getFFmpeg, getScaleFilter, StreamType } from './ffmpeg-commons'
 import { getEncoderBuilderResult } from './ffmpeg-encoders'
@@ -15,6 +15,7 @@ async function getLiveTranscodingCommand (options: {
 
   outPath: string
   masterPlaylistName: string
+  latencyMode: LiveVideoLatencyMode
 
   resolutions: number[]
 
@@ -26,7 +27,7 @@ async function getLiveTranscodingCommand (options: {
   availableEncoders: AvailableEncoders
   profile: string
 }) {
-  const { inputUrl, outPath, resolutions, fps, bitrate, availableEncoders, profile, masterPlaylistName, ratio } = options
+  const { inputUrl, outPath, resolutions, fps, bitrate, availableEncoders, profile, masterPlaylistName, ratio, latencyMode } = options
 
   const command = getFFmpeg(inputUrl, 'live')
 
@@ -120,14 +121,21 @@ async function getLiveTranscodingCommand (options: {
 
   command.complexFilter(complexFilter)
 
-  addDefaultLiveHLSParams(command, outPath, masterPlaylistName)
+  addDefaultLiveHLSParams({ command, outPath, masterPlaylistName, latencyMode })
 
   command.outputOption('-var_stream_map', varStreamMap.join(' '))
 
   return command
 }
 
-function getLiveMuxingCommand (inputUrl: string, outPath: string, masterPlaylistName: string) {
+function getLiveMuxingCommand (options: {
+  inputUrl: string
+  outPath: string
+  masterPlaylistName: string
+  latencyMode: LiveVideoLatencyMode
+}) {
+  const { inputUrl, outPath, masterPlaylistName, latencyMode } = options
+
   const command = getFFmpeg(inputUrl, 'live')
 
   command.outputOption('-c:v copy')
@@ -135,22 +143,39 @@ function getLiveMuxingCommand (inputUrl: string, outPath: string, masterPlaylist
   command.outputOption('-map 0:a?')
   command.outputOption('-map 0:v?')
 
-  addDefaultLiveHLSParams(command, outPath, masterPlaylistName)
+  addDefaultLiveHLSParams({ command, outPath, masterPlaylistName, latencyMode })
 
   return command
 }
 
+function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) {
+  if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) {
+    return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY
+  }
+
+  return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY
+}
+
 // ---------------------------------------------------------------------------
 
 export {
+  getLiveSegmentTime,
+
   getLiveTranscodingCommand,
   getLiveMuxingCommand
 }
 
 // ---------------------------------------------------------------------------
 
-function addDefaultLiveHLSParams (command: FfmpegCommand, outPath: string, masterPlaylistName: string) {
-  command.outputOption('-hls_time ' + VIDEO_LIVE.SEGMENT_TIME_SECONDS)
+function addDefaultLiveHLSParams (options: {
+  command: FfmpegCommand
+  outPath: string
+  masterPlaylistName: string
+  latencyMode: LiveVideoLatencyMode
+}) {
+  const { command, outPath, masterPlaylistName, latencyMode } = options
+
+  command.outputOption('-hls_time ' + getLiveSegmentTime(latencyMode))
   command.outputOption('-hls_list_size ' + VIDEO_LIVE.SEGMENTS_LIST_SIZE)
   command.outputOption('-hls_flags delete_segments+independent_segments')
   command.outputOption(`-hls_segment_filename ${join(outPath, '%v-%06d.ts')}`)
-- 
cgit v1.2.3