Don't fail to display UI when pa connection fails

Additionaly we now re-establish the connection if
it fails while we are already running.
pull/56/head 0.6.0-beta
lawl 4 years ago
parent c07c021e49
commit 4243d5036d

@ -6,6 +6,6 @@ require (
gioui.org v0.0.0-20200630184602-223f8fd40ae4 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/aarzilli/nucular v0.0.0-20200615134801-81910c722bba
github.com/lawl/pulseaudio v0.0.0-20200721170903-af3730493af9
github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
)

@ -17,10 +17,8 @@ github.com/golang/freetype v0.0.0-20161208064710-d9be45aaf745 h1:0d9whnMsm0iklqv
github.com/golang/freetype v0.0.0-20161208064710-d9be45aaf745/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad h1:eMxs9EL0PvIGS9TTtxg4R+JxuPGav82J8rA+GFnY7po=
github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/lawl/pulseaudio v0.0.0-20200721164730-42f022e1b3ee h1:1ehdFhVi5cpSq8FAiSZC+Pe00NmAMT8bSN0axLtr7pE=
github.com/lawl/pulseaudio v0.0.0-20200721164730-42f022e1b3ee/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
github.com/lawl/pulseaudio v0.0.0-20200721170903-af3730493af9 h1:HGt/8C24W2BdSObJZYc8V6C8a7jDxjb9xefVHvjuK3o=
github.com/lawl/pulseaudio v0.0.0-20200721170903-af3730493af9/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0 h1:JrvOwrr1teFiqsp0EQxgEPJsm0pet+YLTL+HdYmnMx0=
github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529 h1:iMGN4xG0cnqj3t+zOM8wUB0BiPKHEwSxEZCvzcbZuvk=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=

@ -8,6 +8,7 @@ import (
"log"
"os"
"syscall"
"time"
"github.com/aarzilli/nucular/font"
@ -66,42 +67,7 @@ func main() {
go updateCheck(&ui)
}
paClient, err := pulseaudio.NewClient()
defer paClient.Close()
ui.paClient = paClient
if err != nil {
log.Fatalf("Couldn't create pulseaudio client: %v\n", err)
}
go updateNoiseSupressorLoaded(paClient, &ui.noiseSupressorState)
sources, err := paClient.Sources()
if err != nil {
log.Fatalf("Couldn't fetch sources from pulseaudio\n")
}
inputs := make([]input, 0)
for i := range sources {
if sources[i].Name == "nui_mic_remap" {
continue
}
log.Printf("Input %s, %+v\n", sources[i].Name, sources[i])
var inp input
inp.ID = sources[i].Name
inp.Name = sources[i].PropList["device.description"]
inp.isMonitor = (sources[i].MonitorSourceIndex != 0xffffffff)
//PA_SOURCE_DYNAMIC_LATENCY = 0x0040U
inp.dynamicLatency = sources[i].Flags&uint32(0x0040) != 0
inputs = append(inputs, inp)
}
ui.inputList = inputs
go paConnectionWatchdog(&ui)
wnd := nucular.NewMasterWindowSize(0, "NoiseTorch", image.Point{550, 300}, func(w *nucular.Window) {
updatefn(w, &ui)
@ -131,3 +97,48 @@ func removeLib(file string) {
}
log.Printf("Deleted temp librnnoise: %s\n", file)
}
func paConnectionWatchdog(ui *uistate) {
for {
if ui.paClient.Connected() {
continue
}
paClient, err := pulseaudio.NewClient()
if err != nil {
log.Printf("Couldn't create pulseaudio client: %v\n", err)
}
ui.paClient = paClient
go updateNoiseSupressorLoaded(paClient, &ui.noiseSupressorState)
sources, err := ui.paClient.Sources()
if err != nil {
log.Printf("Couldn't fetch sources from pulseaudio\n")
}
inputs := make([]input, 0)
for i := range sources {
if sources[i].Name == "nui_mic_remap" {
continue
}
log.Printf("Input %s, %+v\n", sources[i].Name, sources[i])
var inp input
inp.ID = sources[i].Name
inp.Name = sources[i].PropList["device.description"]
inp.isMonitor = (sources[i].MonitorSourceIndex != 0xffffffff)
//PA_SOURCE_DYNAMIC_LATENCY = 0x0040U
inp.dynamicLatency = sources[i].Flags&uint32(0x0040) != 0
inputs = append(inputs, inp)
}
ui.inputList = inputs
time.Sleep(500 * time.Millisecond)
}
}

@ -26,6 +26,11 @@ func updateNoiseSupressorLoaded(c *pulseaudio.Client, b *int) {
for {
*b = supressorState(c)
if !c.Connected() {
break
}
<-upd
}
}

10
ui.go

@ -50,6 +50,11 @@ func updatefn(w *nucular.Window, ui *uistate) {
return
}
if !ui.paClient.Connected() {
connectScreen(w, ui)
return
}
w.MenubarBegin()
w.Row(10).Dynamic(2)
@ -260,3 +265,8 @@ func versionScreen(w *nucular.Window, ui *uistate) {
ui.versionScreen = false
}
}
func connectScreen(w *nucular.Window, ui *uistate) {
w.Row(50).Dynamic(1)
w.Label("Connecting to pulseaudio...", "CB")
}

@ -49,6 +49,7 @@ type Client struct {
clientIndex int
packets chan packet
updates chan struct{}
connected bool
}
// NewClient establishes a connection to the PulseAudio server.
@ -67,9 +68,10 @@ func NewClient(addressArr ...string) (*Client, error) {
}
c := &Client{
conn: conn,
packets: make(chan packet),
updates: make(chan struct{}, 1),
conn: conn,
packets: make(chan packet),
updates: make(chan struct{}, 1),
connected: true,
}
go c.processPackets()
@ -205,7 +207,10 @@ loop:
}
}
}
// end of packet processing loop, e.g. disconnected
c.connected = false
for _, p := range pending {
p.responseChan <- packetResponse{
buff: nil,
err: fmt.Errorf("PulseAudio client was closed"),
@ -330,6 +335,11 @@ func exists(path string) bool {
return false
}
// Connected returns a bool specifying if the connection to pulse is alive
func (c *Client) Connected() bool {
return c != nil && c.connected
}
// RuntimePath resolves a file in the pulse runtime path
// E.g. pass "native" to get the address for pulse' native socket
// Original implementation: https://github.com/pulseaudio/pulseaudio/blob/6c58c69bb6b937c1e758410d3114fc3bc0606fbe/src/pulsecore/core-util.c

@ -62,7 +62,7 @@ github.com/golang/freetype/truetype
# github.com/hashicorp/golang-lru v0.0.0-20160813221303-0a025b7e63ad
github.com/hashicorp/golang-lru
github.com/hashicorp/golang-lru/simplelru
# github.com/lawl/pulseaudio v0.0.0-20200721170903-af3730493af9
# github.com/lawl/pulseaudio v0.0.0-20200802093727-ab0735955fd0
## explicit
github.com/lawl/pulseaudio
# golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899

Loading…
Cancel
Save