diff options
author | xegineering <me@xegineering.eu> | 2024-11-28 22:44:28 +0100 |
---|---|---|
committer | xegineering <me@xegineering.eu> | 2024-11-28 22:44:28 +0100 |
commit | 99031596efbcf004188217d2a9e53ac5ce33b511 (patch) | |
tree | 34425218468652238956029371ba3ad123f7a389 /soundbox | |
parent | bb84332ef7ca114f0c608bbea7f11451f010a831 (diff) | |
download | soundbox-go-99031596efbcf004188217d2a9e53ac5ce33b511.tar soundbox-go-99031596efbcf004188217d2a9e53ac5ce33b511.tar.zst soundbox-go-99031596efbcf004188217d2a9e53ac5ce33b511.zip |
Replace custom code by io package functions
This makes use of two functions from this package:
- io.Copy()
- io.MultiWriter()
`io.Copy()` is used to move the data from whatever reader is provided.
`io.Multiwriter()` solves the issue that we need to stream to multiple
network connections at the same time (one for each soundbox).
Diffstat (limited to 'soundbox')
-rw-r--r-- | soundbox/streaming.go | 27 | ||||
-rw-r--r-- | soundbox/url.go | 2 |
2 files changed, 9 insertions, 20 deletions
diff --git a/soundbox/streaming.go b/soundbox/streaming.go index 2686f47..1a2d89e 100644 --- a/soundbox/streaming.go +++ b/soundbox/streaming.go @@ -2,7 +2,6 @@ package soundbox import ( "context" - "errors" "io" "net" "time" @@ -27,24 +26,14 @@ func streamContext(ctx context.Context, r io.Reader, targets []net.HardwareAddr) } }() - for { - buffer := make([]byte, bufferSize) - i, readErr := r.Read(buffer) - for _, conn := range conns { - conn.SetDeadline(time.Now().Add(writeTimeout)) - _, writeErr := conn.Write(buffer[:i]) - if writeErr != nil { - return writeErr - } - } - if readErr != nil { - if errors.Is(readErr, io.EOF) { - break - } else { - return readErr - } - } + writers := make([]io.Writer, 0) + for _, conn := range conns { + writers = append(writers, conn) } - return nil + mw := io.MultiWriter(writers...) + + _, err := io.Copy(mw, r) + + return err } diff --git a/soundbox/url.go b/soundbox/url.go index 6fed442..b24b05d 100644 --- a/soundbox/url.go +++ b/soundbox/url.go @@ -33,7 +33,7 @@ func StreamURLContext(ctx context.Context, url string, targets []net.HardwareAdd return err } - streamContext(ctx, stdout, targets) + _ = streamContext(ctx, stdout, targets) return cmd.Wait() } |