From a1d91fcba108a59c440f0675dd5106422cf3ff7e Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 3 Oct 2024 18:16:08 +0200 Subject: Switch from local playback to soundbox streaming This commit replaces the local playback of the received audio content by forwarding it to one or multiple soundbox devices. For this purpose the soundbox-go[1] library is used. The target devices cannot be selected via the GUI. Thus all devices are specified in the ~/.config/soundbox/config.json file. The format has to be looked up based on the code. Further documentation will follow. [1]: https://xengineering.eu/git/soundbox-go --- main.go | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 5701c72..94b94c7 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,8 @@ import ( "context" "image/color" "log" + "net" "os" - "os/exec" "sync" "gioui.org/app" @@ -15,6 +15,8 @@ import ( "gioui.org/unit" "gioui.org/widget" "gioui.org/widget/material" + + "xengineering.eu/soundbox" ) func main() { @@ -93,7 +95,7 @@ func (ui *Ui) HandleInputs(gtx layout.Context) { ui.State.PlayerCancel() } else { ui.State.PlayerContext, ui.State.PlayerCancel = context.WithCancel(context.Background()) - go mpv(ui.State.PlayerContext, ui.State.UrlEditor.Text(), ui) + go play(ui.State.PlayerContext, ui.State.UrlEditor.Text(), ui) } } } @@ -129,7 +131,7 @@ func (ui *Ui) Layout(gtx layout.Context) layout.Dimensions { }) } -func mpv(ctx context.Context, url string, ui *Ui) { +func play(ctx context.Context, url string, ui *Ui) { setPlayingState := func(isPlaying bool) { ui.State.Lock() defer ui.Window.Invalidate() @@ -144,14 +146,24 @@ func mpv(ctx context.Context, url string, ui *Ui) { } setPlayingState(true) + defer setPlayingState(false) - cmd := exec.CommandContext( - ctx, - "mpv", - "--no-video", - url, - ) - _ = cmd.Run() + config, err := loadConfig() + if err != nil { + log.Println(err) + } - setPlayingState(false) + var devices []net.HardwareAddr + for _, entry := range config.Soundboxes { + mac, err := net.ParseMAC(entry.Mac) + if err != nil { + log.Printf("Failed to parse MAC: %v", err) + } + devices = append(devices, mac) + } + + err = soundbox.StreamURLContext(ctx, url, devices) + if err != nil { + log.Println(err) + } } -- cgit v1.2.3-70-g09d2