streamdeck-obs-replay/client_slobs.go

105 lines
1.9 KiB
Go

package main
import (
"meow.tf/streamdeck/obs-replay/slobs"
"strconv"
)
type SlobsClient struct {
client *slobs.Client
key 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}
}
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.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
}
})
return nil
}
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
}