From d6f9ac713892f604c0f9c168c349f7cc7f8acd43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 28 Apr 2020 23:42:45 +0200 Subject: [PATCH] Initial prototyping hackery. --- go.mod | 3 +++ main.go | 37 ++++++++++++++++++++++++++++++ pulsectl/pulsectl.go | 32 ++++++++++++++++++++++++++ socketops/socketops.go | 51 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 go.mod create mode 100644 main.go create mode 100644 pulsectl/pulsectl.go create mode 100644 socketops/socketops.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bc64b7d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module remote-mic + +go 1.14 diff --git a/main.go b/main.go new file mode 100644 index 0000000..8d71925 --- /dev/null +++ b/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "fmt" + "time" + "remote-mic/socketops" + "remote-mic/pulsectl" +) + + +func nop() { + fmt.Print("Listening for TCP connection on port 8080... ") + conn, err := socketops.AcceptConn(":8080") + if err != nil { + panic(err) + } + defer conn.Close() + fmt.Println("Connection accepted!") + + data := make(chan []byte, 10) + go socketops.HandleConn(conn, data) + socketops.HandleData(data) + + fmt.Println("Exiting.") +} + +func main() { + pulsectl.LoadPipeSource(pulsectl.PipeSourceConfig{ + "remote-mic", + "/dev/shm", + "s16le", + 44100, + 2, + }) + time.Sleep(10 * time.Second) + pulsectl.UnloadPipeSource() +} diff --git a/pulsectl/pulsectl.go b/pulsectl/pulsectl.go new file mode 100644 index 0000000..4f04d8b --- /dev/null +++ b/pulsectl/pulsectl.go @@ -0,0 +1,32 @@ +package pulsectl + +import ( + "os/exec" + "fmt" + "path" +) + + +type PipeSourceConfig struct { + PipeName string + PipeDir string + Encoding string + Bitrate int + Channels int +} + +func LoadPipeSource(config PipeSourceConfig) error { + cmd := exec.Command("pactl", "load-module", "module-pipe-source", + fmt.Sprintf("source_name=%s", config.PipeName), + fmt.Sprintf("file=%s", path.Join(config.PipeDir, config.PipeName)), + fmt.Sprintf("format=%s", config.Encoding), + fmt.Sprintf("rate=%d", config.Bitrate), + fmt.Sprintf("channels=%d", config.Channels), + ) + return cmd.Run() +} + +func UnloadPipeSource() error { + cmd := exec.Command("pactl", "unload-module", "module-pipe-source") + return cmd.Run() +} diff --git a/socketops/socketops.go b/socketops/socketops.go new file mode 100644 index 0000000..79f3e38 --- /dev/null +++ b/socketops/socketops.go @@ -0,0 +1,51 @@ +package socketops + +import ( + "fmt" + "net" +) + +const bufSize = 1024 + + +func AcceptConn(connstr string) (net.Conn, error) { + addr, err := net.ResolveTCPAddr("tcp", connstr) + if err != nil { + return nil, err + } + sock, err := net.ListenTCP("tcp", addr) + if err != nil { + return nil, err + } + conn, err := sock.AcceptTCP() + if err != nil { + return nil, err + } + conn.SetNoDelay(true) + return conn, nil +} + +func HandleConn(conn net.Conn, out chan<- []byte) { + buf := make([]byte, bufSize) + for { + n, err := conn.Read(buf) + if err != nil { + close(out) + break + } + if n > 0 { + out <- buf[:n] + fmt.Printf("Bytes read: %d\n", n) + } + } +} + +func HandleData(data <-chan []byte) { + for { + buf, open := <-data + if !open { + break + } + fmt.Printf("%v\n", string(buf)) + } +}