Initial prototyping hackery.

This commit is contained in:
Kristóf Tóth 2020-04-28 23:42:45 +02:00
commit d6f9ac7138
4 changed files with 123 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module remote-mic
go 1.14

37
main.go Normal file
View File

@ -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()
}

32
pulsectl/pulsectl.go Normal file
View File

@ -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()
}

51
socketops/socketops.go Normal file
View File

@ -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))
}
}