Browse Source

liquidsoap can understand if a stream is "up"

(provided the liq script is complex enough!)
boyska 6 years ago
parent
commit
7ce86918bc
3 changed files with 57 additions and 14 deletions
  1. 5 6
      cmd/direttoforo/main.go
  2. 12 0
      contrib/liquidsoap/http.liq
  3. 40 8
      liquidsoap/telnet.go

+ 5 - 6
cmd/direttoforo/main.go

@@ -78,21 +78,20 @@ func main() {
 				continue
 			}
 			changed := false
-			for name, enabled := range outs {
+			for name, newstream := range outs {
 				if stream, exists := state.Streams[name]; exists {
-					if stream.State != enabled {
-						stream.State = enabled
+					if newstream != stream {
+						state.Streams[name] = newstream
 						changed = true
 					}
-					state.Streams[name] = stream
 				} else {
-					state.Streams[name] = liquidsoap.Stream{State: enabled}
+					state.Streams[name] = newstream
 					changed = true
 				}
 			}
 			t.Close()
-			fmt.Println(changed, "state=", state)
 			if changed {
+				fmt.Println("state=", state)
 				netUI.Update()
 			}
 		}

+ 12 - 0
contrib/liquidsoap/http.liq

@@ -5,6 +5,16 @@
 set('log.file', false)
 set('log.stdout', true)
 
+rec_up = interactive.bool("rec_up", false)
+icecast_up = interactive.bool("icecast_up", false)
+def var_ok(varname) =
+    fun () ->    ignore(server.execute("var.set #{varname} = true"))
+end
+def var_ko(varname) =
+    fun () -> ignore(server.execute("var.set #{varname} = false"))
+end
+
+
 audioin = in()
 audioin = rewrite_metadata([("artist", "Direttoforo")], audioin)
 audioin = server.rms(id="rms", audioin)
@@ -12,6 +22,8 @@ audioin = server.rms(id="rms", audioin)
 f = output.file(
         id="rec",
     %vorbis(quality=0.2, samplerate=44100, channels=2),
+on_start=var_ok("rec_up"),
+on_stop=var_ko("rec_up"),
     "rec/%Y/%m/rec-%Y-%m-%d_%H-%M-%S.ogg",
     audioin
 )

+ 40 - 8
liquidsoap/telnet.go

@@ -16,7 +16,9 @@ type Client struct {
 
 // Stream represents a liquidsoap stream. It contains information about the status and the nature of it
 type Stream struct {
-	State bool
+	Enabled bool `json:"enabled"`
+	Up bool `json:"up"`
+	Type string `json:"type"`
 }
 
 // NewTelnet returns a liquidsoap.Client created using telnet on the given parameters
@@ -56,8 +58,8 @@ func (c *Client) Command(cmdline string) (string, error) {
 
 // Outputs will return a map of outputs that liquidsoap is handling.
 // An output is set to true if it is enabled
-func (c *Client) Outputs() (outputs map[string]bool, err error) {
-	outputs = make(map[string]bool)
+func (c *Client) Outputs() (outputs map[string]Stream, err error) {
+	outputs = make(map[string]Stream)
 	var cmdout string
 	cmdout, err = c.Command("list")
 	if err != nil {
@@ -71,12 +73,17 @@ func (c *Client) Outputs() (outputs map[string]bool, err error) {
 		}
 		if strings.Index(parts[1], "output.") == 0 {
 			name := strings.TrimSpace(parts[0])
-			var enabled bool
-			enabled, err = c.GetOutput(name)
-			if err != nil {
-				return
+			var stream Stream
+			stream.Type = parts[1][7:]
+			enabled, err := c.GetOutput(name)
+			if err == nil {
+			stream.Enabled = enabled
 			}
-			outputs[name] = enabled
+			up, err := c.GetOutputUp(name)
+			if err == nil {
+				stream.Up = up
+			}
+			outputs[name] = stream
 		}
 	}
 
@@ -92,6 +99,31 @@ func (c *Client) GetOutput(name string) (bool, error) {
 	return strings.TrimSpace(cmdout) == "on", nil
 }
 
+// GetOutput checks whether an output is up
+//
+// Please note that this relies on a custom convention proposed by direttoforo, where to every stream "x"
+// there is an interactive boolean variable "x_up" that has its state
+func (c *Client) GetOutputUp(name string) (bool, error) {
+	return c.GetVar(name + "_up")
+}
+
+// GetVar reads an interactive variable
+func (c *Client) GetVar(name string) (bool, error) {
+	cmdout, err := c.Command("var.get " + name)
+	if err != nil {
+		return false, err
+	}
+	cmdout = strings.TrimSpace(cmdout)
+	switch cmdout {
+	case "true":
+		return true, nil
+	case "false":
+		return false, nil
+	default:
+		return false, fmt.Errorf("Variable %s not set", name)
+	}
+}
+
 // SetOutput enables or disables an output
 func (c *Client) SetOutput(name string, enabled bool) error {
 	action := "stop"