diff options
author | xegineering <me@xegineering.eu> | 2024-11-17 12:45:35 +0100 |
---|---|---|
committer | xegineering <me@xegineering.eu> | 2024-11-17 12:48:00 +0100 |
commit | d4cf5ed8e38d1e5d0b8110e8959e002c216f073f (patch) | |
tree | 4565ee3c50e6de74b6ce65c615ac71dcf8378a7f /soundbox/stream.go | |
parent | a95013aa0813d1644aa15780f360fa4dad2223a8 (diff) | |
download | soundbox-go-d4cf5ed8e38d1e5d0b8110e8959e002c216f073f.tar soundbox-go-d4cf5ed8e38d1e5d0b8110e8959e002c216f073f.tar.zst soundbox-go-d4cf5ed8e38d1e5d0b8110e8959e002c216f073f.zip |
Fix streaming only via first interface candidate
Diffstat (limited to 'soundbox/stream.go')
-rw-r--r-- | soundbox/stream.go | 100 |
1 files changed, 0 insertions, 100 deletions
diff --git a/soundbox/stream.go b/soundbox/stream.go deleted file mode 100644 index 910523d..0000000 --- a/soundbox/stream.go +++ /dev/null @@ -1,100 +0,0 @@ -package soundbox - -import ( - "context" - "errors" - "fmt" - "io" - "net" - "os/exec" - "time" -) - -// streamingPort is the default network port a soundbox is listening to for -// incoming audio stream data. -const streamingPort = 5316 - -const bufferSize = 20 - -const writeTimeout = 1 * time.Second - -// StreamURLContext streams audio from a given URL to one or multiple soundbox -// devices. The devices are referenced via their MAC addresses given by the -// targets argument. The ctx argument is passed to cancel the streaming. -func StreamURLContext(ctx context.Context, url string, targets []net.HardwareAddr) error { - iface, err := getInterface() - if err != nil { - return err - } - - ips := make([]net.IP, 0) - for _, target := range targets { - ip, err := toLinkLocal(target) - if err != nil { - return err - } - ips = append(ips, ip) - } - - conns := make([]net.Conn, 0) - for _, ip := range ips { - var d net.Dialer - conn, err := d.DialContext( - ctx, - "tcp6", - fmt.Sprintf("[%s%%%s]:%d", ip, iface.Name, streamingPort), - ) - if err != nil { - return err - } - conns = append(conns, conn) - } - defer func() { - for _, conn := range conns { - conn.Close() - } - }() - - cmd := exec.CommandContext( - ctx, - "ffmpeg", - "-re", - "-i", - url, - "-acodec", - "flac", - "-f", - "ogg", - "-", - ) - stdout, err := cmd.StdoutPipe() - if err != nil { - return err - } - - err = cmd.Start() - if err != nil { - return err - } - - for { - buffer := make([]byte, bufferSize) - i, err := stdout.Read(buffer) - if err != nil { - if errors.Is(err, io.EOF) { - break - } else { - return err - } - } - for _, conn := range conns { - conn.SetDeadline(time.Now().Add(writeTimeout)) - _, err = conn.Write(buffer[:i]) - if err != nil { - return err - } - } - } - - return cmd.Wait() -} |