65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"meow.tf/streamdeck/obs-replay/obsws"
|
|
)
|
|
|
|
type OBSWSClient struct {
|
|
client *obsws.Client
|
|
key string
|
|
}
|
|
|
|
func NewOBSWSClient(key, host string, port int, password string) *OBSWSClient {
|
|
obsc := &obsws.Client{Host: host, Port: port, Password: password}
|
|
|
|
return &OBSWSClient{client: obsc, key: key}
|
|
}
|
|
|
|
func (c *OBSWSClient) Connect() error {
|
|
err := c.client.Connect()
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
c.client.AddEventHandler("StreamStatus", streamStatusUpdate(c.key))
|
|
c.client.AddEventHandler("ReplayStarted", func(obsws.Event) { loopContextState(c.key, 1) })
|
|
c.client.AddEventHandler("ReplayStopped", func(obsws.Event) { loopContextState(c.key, 0) })
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *OBSWSClient) Disconnect() error {
|
|
return c.client.Disconnect()
|
|
}
|
|
|
|
func (c *OBSWSClient) Connected() bool {
|
|
return c.client.Connected()
|
|
}
|
|
|
|
func (c *OBSWSClient) ToggleReplay() error {
|
|
req := obsws.NewStartStopReplayBufferRequest()
|
|
|
|
return req.Send(c.client)
|
|
}
|
|
|
|
func (c *OBSWSClient) SaveReplay() error {
|
|
req := obsws.NewSaveReplayBufferRequest()
|
|
|
|
return req.Send(c.client)
|
|
}
|
|
|
|
func streamStatusUpdate(key string) func(obsws.Event) {
|
|
return func(e obsws.Event) {
|
|
evt := e.(obsws.StreamStatusEvent)
|
|
|
|
state := 0
|
|
|
|
if evt.Replay {
|
|
state = 1
|
|
}
|
|
|
|
loopContextState(key, state)
|
|
}
|
|
}
|