summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxegineering <me@xegineering.eu>2024-12-15 12:21:24 +0100
committerxegineering <me@xegineering.eu>2024-12-15 12:36:59 +0100
commitde2694f182790dd311265e378498d586f9b3ae66 (patch)
tree5d9a796f8b15ae6b4da65b24b7338a10de43db22
parent2184e703ba3c3fd79e792a78a70e76daa2aa1062 (diff)
downloadsoundbox-go-de2694f182790dd311265e378498d586f9b3ae66.tar
soundbox-go-de2694f182790dd311265e378498d586f9b3ae66.tar.zst
soundbox-go-de2694f182790dd311265e378498d586f9b3ae66.zip
pipewire: Make sure only one instance is running
The init / deinit of the PipeWire C library is not well handled for multiple instances. Furthermore the PipeWire audio streaming relies on a channel which is a global variable. Both issues have to be fixed before multiple PipeWire capture instances are allowed.
-rw-r--r--soundbox/pipewire.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/soundbox/pipewire.go b/soundbox/pipewire.go
index bb672f8..0443766 100644
--- a/soundbox/pipewire.go
+++ b/soundbox/pipewire.go
@@ -8,10 +8,12 @@ import "C"
import (
"context"
+ "fmt"
"io"
"log"
"net"
"os/exec"
+ "sync"
"unsafe"
)
@@ -21,6 +23,10 @@ type pwCapture struct {
var pwAudio chan []byte
+// lock makes sure there is always just one PipeWire capture device since
+// multiple are not yet implemented.
+var lock sync.Mutex
+
func newPWCapture(ctx context.Context) pwCapture {
pwc := pwCapture{}
@@ -74,6 +80,12 @@ func s16leDropSilence(input []byte) []byte {
}
func StreamPipewireContext(ctx context.Context, targets []net.HardwareAddr) error {
+ success := lock.TryLock()
+ if !success {
+ return fmt.Errorf("Could not acquire lock")
+ }
+ defer lock.Unlock()
+
cmd := exec.CommandContext(
ctx,
"ffmpeg",