stream-poc2/handlersPreset.go
2025-10-09 22:38:02 +02:00

57 lines
1.4 KiB
Go

package main
import (
"log"
"net/http"
"stream-poc2/ffmpeg"
"sync/atomic"
"github.com/gin-gonic/gin"
)
// The reason why preset is configured separately is that there is no way of representing it properly in the
// HLS master/variant playlist which would confuse players, because there would be two identical looking playlist
// (hls.js solves this by omitting one for example)
// But that's not the only reason. I believe this must be a server-side setting, that the administrators of the server control
// So for example, when the server is under high load, they can tune it to "fast" to ease the load, and when there is less demand they can tune it to "slow" for better quality.
var currentPreset atomic.Pointer[string]
func init() {
profileDefault := ""
currentPreset.Store(&profileDefault)
}
func getCurrentPreset() string {
preset := currentPreset.Load()
if preset != nil {
return *preset
}
return ""
}
func setPresetHandler(ctx *gin.Context) {
var newPreset string
err := ctx.BindJSON(&newPreset)
if err != nil {
ctx.Status(400)
return
}
if newPreset == "" || ffmpeg.IsNVENCPresetValid(newPreset) {
log.Println("Update global ffmpeg NVENC preset to ", newPreset)
currentPreset.Store(&newPreset)
ctx.Status(http.StatusOK)
} else {
ctx.Status(400)
}
}
func getPresetHandler(ctx *gin.Context) {
preset := currentPreset.Load()
if preset != nil {
ctx.JSON(http.StatusOK, *preset)
} else {
ctx.JSON(http.StatusOK, "")
}
}