streamdeck-obs-replay/client_slobs.go

117 lines
2.1 KiB
Go

package main
import (
"meow.tf/streamdeck/obs-replay/slobs"
"strconv"
)
type SlobsClient struct {
client *slobs.Client
key string
password string
recording bool
}
func NewSlobsClient(key, host string, port int, password string) *SlobsClient {
slobsc := slobs.NewClient(host + ":" + strconv.Itoa(port))
return &SlobsClient{client: slobsc, key: key, password: password}
}
const (
Running = "running"
Saving = "saving"
Stopping = "stopping"
Offline = "offline"
)
func (c *SlobsClient) Connect() error {
err := c.client.Connect()
if err != nil {
return err
}
c.client.Auth(c.password, func(err error) {
if err != nil {
// TODO alert that it failed to startup?
return
}
c.requestInitialData()
})
return nil
}
func (c *SlobsClient) requestInitialData() {
c.client.SendRPC("StreamingService", "getModel", func(e *slobs.RPCResponse) {
state := &slobs.IStreamingState{}
e.DecodeTo(&state)
switch state.ReplayBufferStatus {
case Saving:
fallthrough
case Running:
loopContextState(c.key, 1)
c.recording = true
case Offline:
loopContextState(c.key, 0)
c.recording = false
}
})
c.client.Subscribe("StreamingService", "replayBufferStatusChange", func(e *slobs.ResourceEvent) {
var status string
e.DecodeTo(&status)
switch status {
case Saving:
return
case Running:
loopContextState(c.key, 1)
c.recording = true
case Offline:
loopContextState(c.key, 0)
c.recording = false
}
})
}
func (c *SlobsClient) Disconnect() error {
return c.client.Disconnect()
}
func (c *SlobsClient) Connected() bool {
return c.client.Connected()
}
func (c *SlobsClient) ToggleReplay() error {
ret := make(chan struct{}, 1)
handler := func(res *slobs.RPCResponse) {
close(ret)
}
if c.recording {
c.client.SendRPC("StreamingService", "stopReplayBuffer", handler)
} else {
c.client.SendRPC("StreamingService", "startReplayBuffer", handler)
}
<-ret
return nil
}
func (c *SlobsClient) SaveReplay() error {
ret := make(chan struct{}, 1)
c.client.SendRPC("StreamingService", "saveReplay", func(res *slobs.RPCResponse) {
close(ret)
})
<-ret
return nil
}