Able to control stream volume

This commit is contained in:
nemunaire 2023-11-15 11:31:48 +01:00
commit d2090bee67
5 changed files with 190 additions and 42 deletions

View file

@ -11,9 +11,12 @@ import (
)
type InputState struct {
Name string `json:"name"`
Active bool `json:"active"`
Controlable bool `json:"controlable"`
Name string `json:"name"`
Active bool `json:"active"`
Controlable bool `json:"controlable"`
Streams map[string]string `json:"streams,omitempty"`
Mixable bool `json:"mixable"`
Mixer map[string]*inputs.InputMixer `json:"mixer,omitempty"`
}
func declareInputsRoutes(cfg *config.Config, router *gin.RouterGroup) {
@ -21,12 +24,21 @@ func declareInputsRoutes(cfg *config.Config, router *gin.RouterGroup) {
ret := map[string]*InputState{}
for k, inp := range inputs.SoundInputs {
var mixer map[string]*inputs.InputMixer
_, controlable := inp.(inputs.ControlableInput)
im, mixable := inp.(inputs.MixableInput)
if mixable {
mixer, _ = im.GetMixers()
}
ret[k] = &InputState{
Name: inp.GetName(),
Active: inp.IsActive(),
Controlable: controlable,
Streams: inp.CurrentlyPlaying(),
Mixable: mixable,
Mixer: mixer,
}
}
@ -37,25 +49,36 @@ func declareInputsRoutes(cfg *config.Config, router *gin.RouterGroup) {
inputsRoutes.Use(InputHandler)
inputsRoutes.GET("", func(c *gin.Context) {
src := c.MustGet("input").(inputs.SoundInput)
inp := c.MustGet("input").(inputs.SoundInput)
var mixer map[string]*inputs.InputMixer
_, controlable := inp.(inputs.ControlableInput)
im, mixable := inp.(inputs.MixableInput)
if mixable {
mixer, _ = im.GetMixers()
}
c.JSON(http.StatusOK, &InputState{
Name: src.GetName(),
Active: src.IsActive(),
Name: inp.GetName(),
Active: inp.IsActive(),
Controlable: controlable,
Streams: inp.CurrentlyPlaying(),
Mixable: mixable,
Mixer: mixer,
})
})
inputsRoutes.GET("/settings", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("input"))
})
inputsRoutes.GET("/streams", func(c *gin.Context) {
src := c.MustGet("input").(inputs.SoundInput)
inp := c.MustGet("input").(inputs.SoundInput)
if !src.IsActive() {
if !inp.IsActive() {
c.AbortWithStatusJSON(http.StatusNotAcceptable, gin.H{"errmsg": "Input not active"})
return
}
c.JSON(http.StatusOK, src.CurrentlyPlaying())
c.JSON(http.StatusOK, inp.CurrentlyPlaying())
})
streamRoutes := inputsRoutes.Group("/streams/:stream")
@ -74,18 +97,40 @@ func declareInputsRoutes(cfg *config.Config, router *gin.RouterGroup) {
return
}
c.JSON(http.StatusOK, true)
})
streamRoutes.POST("/volume", func(c *gin.Context) {
input, ok := c.MustGet("input").(inputs.MixableInput)
if !ok {
c.AbortWithStatusJSON(http.StatusMethodNotAllowed, gin.H{"errmsg": "The source doesn't support that"})
return
}
var mixer inputs.InputMixer
err := c.BindJSON(&mixer)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
err = input.SetMixer(c.MustGet("streamid").(string), &mixer)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to pause the input: %s", err.Error())})
return
}
c.JSON(http.StatusOK, true)
})
}
func InputHandler(c *gin.Context) {
src, ok := inputs.SoundInputs[c.Param("input")]
inp, ok := inputs.SoundInputs[c.Param("input")]
if !ok {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": fmt.Sprintf("Input not found: %s", c.Param("input"))})
return
}
c.Set("input", src)
c.Set("input", inp)
c.Next()
}