-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obs.go
323 lines (270 loc) · 8.66 KB
/
obs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package addons
import (
"image/color"
"strings"
"github.com/andreykaipov/goobs"
"github.com/andreykaipov/goobs/api/events"
"github.com/andreykaipov/goobs/api/requests/inputs"
"github.com/andreykaipov/goobs/api/requests/scenes"
"github.com/derickr/streamdeck-goui/actionhandlers"
"github.com/magicmonkey/go-streamdeck"
"github.com/magicmonkey/go-streamdeck/buttons"
sddecorators "github.com/magicmonkey/go-streamdeck/decorators"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)
type Obs struct {
SD *streamdeck.StreamDeck
obs_client *goobs.Client
recordButtonIndex int
streamButtonIndex int
toggleMuteIndex int
muteOnImage string
muteOffImage string
muteSource string
}
var obs_current_scene string
type ObsScene struct {
Name string `mapstructure:"name"`
Image string `mapstructure:"image"`
ButtonId int
}
func (scene *ObsScene) SetButtonId(id int) {
scene.ButtonId = id
}
var buttons_obs map[string]*ObsScene // scene name and image name
func (o *Obs) ClearButtons() {
o.recordButtonIndex = -1
o.streamButtonIndex = -1
o.toggleMuteIndex = -1
}
func (o *Obs) SetRecordButton(index int, image string, unmuteSource string) {
if o.obs_client == nil {
return
}
oaction := &actionhandlers.OBSRecordAction{Client: o.obs_client, Source: unmuteSource}
recordButton, err := buttons.NewImageFileButton(image)
if err == nil {
recordButton.SetActionHandler(oaction)
o.SD.AddButton(index, recordButton)
} else {
recordButton := buttons.NewTextButton("Record")
recordButton.SetActionHandler(oaction)
o.SD.AddButton(index, recordButton)
}
o.recordButtonIndex = index
}
func (o *Obs) SetStreamButton(index int, image string, unmuteSource string) {
if o.obs_client == nil {
return
}
oaction := &actionhandlers.OBSStreamAction{Client: o.obs_client, Source: unmuteSource}
streamButton, err := buttons.NewImageFileButton(image)
if err == nil {
streamButton.SetActionHandler(oaction)
o.SD.AddButton(index, streamButton)
} else {
streamButton := buttons.NewTextButton("Stream")
streamButton.SetActionHandler(oaction)
o.SD.AddButton(index, streamButton)
}
o.streamButtonIndex = index
}
func (o *Obs) getMuteCurrentStatus(source string) bool {
log.Info().Msg("Fetching current mute status")
result, err := o.obs_client.Inputs.GetInputMute(&inputs.GetInputMuteParams{InputName: source})
if err != nil {
log.Warn().Err(err).Msg("OBS stream action error")
}
return result.InputMuted
}
func (o *Obs) SetToggleMuteButton(index int, source string, imageOn string, imageOff string) {
if o.obs_client == nil {
return
}
oaction := &actionhandlers.OBSToggleMuteAction{Client: o.obs_client, Source: source}
toggleMuteButton, err := buttons.NewImageFileButton(imageOn)
if err == nil {
toggleMuteButton.SetActionHandler(oaction)
o.SD.AddButton(index, toggleMuteButton)
} else {
toggleMuteButton := buttons.NewTextButton("Mute/Unmute")
toggleMuteButton.SetActionHandler(oaction)
o.SD.AddButton(index, toggleMuteButton)
}
o.toggleMuteIndex = index
o.muteOnImage = imageOn
o.muteOffImage = imageOff
o.muteSource = source
o.updateMuteButton(o.getMuteCurrentStatus(source))
}
func (o *Obs) ConnectOBS() {
log.Debug().Msg("Connecting to OBS...")
log.Info().Msgf("%#v\n", viper.Get("obs.host"))
tmpObsClient, err := goobs.New(
viper.GetString("obs.host"),
goobs.WithPassword(viper.GetString("obs.password")),
)
o.obs_client = tmpObsClient
if err != nil {
log.Warn().Err(err).Msg("Cannot connect to OBS")
}
o.ClearButtons()
}
func (o *Obs) ObsEventHandlers() {
if o.obs_client == nil {
return
}
log.Debug().Msg("Setting up handlers...")
go o.obs_client.Listen(func(event interface{}) {
switch e := event.(type) {
case *events.CurrentProgramSceneChanged:
// Make sure to assert the actual event type.
scene := e.SceneName
lScene := strings.ToLower(scene)
log.Info().Msg("Old scene: " + obs_current_scene)
// undecorate the old
if oldb, ok := buttons_obs[obs_current_scene]; ok {
log.Info().Int("button", oldb.ButtonId).Msg("Clear original button decoration")
o.SD.UnsetDecorator(oldb.ButtonId)
}
// decorate the new
log.Info().Msg("New scene: " + scene)
if eventb, ok := buttons_obs[lScene]; ok {
log.Info().Int("button", eventb.ButtonId).Msg("Highlight new scene button")
decorator2 := sddecorators.NewBorder(4, color.RGBA{255, 255, 0, 255})
o.SD.SetDecorator(eventb.ButtonId, decorator2)
}
obs_current_scene = lScene
case *events.RecordStateChanged:
if o.recordButtonIndex >= 0 {
if e.OutputActive {
decorator2 := sddecorators.NewBorder(8, color.RGBA{255, 0, 0, 255})
log.Info().Msg("Recording Started")
o.SD.SetDecorator(o.recordButtonIndex, decorator2)
} else {
log.Info().Msg("Recording Stopped")
o.SD.UnsetDecorator(o.recordButtonIndex)
}
}
case *events.StreamStateChanged:
if o.streamButtonIndex >= 0 {
if e.OutputActive {
decorator2 := sddecorators.NewBorder(8, color.RGBA{0, 0, 255, 255})
log.Info().Msg("Stream Started")
o.SD.SetDecorator(o.streamButtonIndex, decorator2)
} else {
log.Info().Msg("Stream Stopped")
o.SD.UnsetDecorator(o.streamButtonIndex)
}
}
case *events.InputMuteStateChanged:
if o.toggleMuteIndex < 0 {
return
}
o.updateMuteButton(e.InputMuted)
}
})
}
func (o *Obs) updateMuteButton(muted bool) {
var text string
var image string
var decorator *sddecorators.Border
if muted {
image = o.muteOffImage
decorator = sddecorators.NewBorder(8, color.RGBA{255, 0, 0, 255})
text = " Muted "
} else {
image = o.muteOnImage
decorator = sddecorators.NewBorder(8, color.RGBA{0, 255, 0, 255})
text = " Unmuted "
}
log.Info().Msg(text)
oaction := &actionhandlers.OBSToggleMuteAction{Client: o.obs_client, Source: o.muteSource}
toggleMuteButton, err := buttons.NewImageFileButton(image)
if err == nil {
toggleMuteButton.SetActionHandler(oaction)
o.SD.AddButton(o.toggleMuteIndex, toggleMuteButton)
} else {
toggleMuteButton := buttons.NewTextButton(text)
toggleMuteButton.SetActionHandler(oaction)
o.SD.SetDecorator(o.toggleMuteIndex, decorator)
o.SD.AddButton(o.toggleMuteIndex, toggleMuteButton)
}
}
func (o *Obs) Buttons(maxScenes int, offset int) {
o.ConnectOBS()
if o.obs_client == nil {
return
}
o.ObsEventHandlers()
// OBS Scenes to Buttons
buttons_obs = make(map[string]*ObsScene)
viper.UnmarshalKey("obs_scenes", &buttons_obs)
// offset for what number button to start at
image_path := viper.GetString("buttons.images")
var image string
// what scenes do we have? (max 8 for the top row of buttons)
scenes, err := o.obs_client.Scenes.GetSceneList(&scenes.GetSceneListParams{})
if err != nil {
log.Warn().Err(err)
}
// what is the current schene?
obs_current_scene = strings.ToLower(scenes.CurrentProgramSceneName)
// make buttons for these scenes
log.Debug().Msgf("Max Scenes: %d", maxScenes)
for j := len(scenes.Scenes) - 1; j >= 0; j-- {
scene := scenes.Scenes[j]
i := len(scenes.Scenes) - 1 - j
log.Debug().Msg("Scene: " + scene.SceneName)
image = ""
sceneName := scene.SceneName
lSceneName := strings.ToLower(sceneName)
// only need a few scenes
if i >= maxScenes {
continue
}
oaction := &actionhandlers.OBSSceneAction{Scene: scene.SceneName, Client: o.obs_client}
if s, ok := buttons_obs[lSceneName]; ok {
if s.Image != "" {
image = image_path + "/" + s.Image
}
} else {
// there wasn't an entry in the buttons for this scene so add one
buttons_obs[lSceneName] = &ObsScene{}
}
if image != "" {
// try to make an image button
obutton, err := buttons.NewImageFileButton(image)
if err == nil {
obutton.SetActionHandler(oaction)
o.SD.AddButton(i+offset, obutton)
// store which button we just set
buttons_obs[lSceneName].SetButtonId(i + offset)
} else {
// something went wrong with the image, use a default one
image = image_path + "/play.jpg"
obutton, err := buttons.NewImageFileButton(image)
if err == nil {
obutton.SetActionHandler(oaction)
o.SD.AddButton(i+offset, obutton)
// store which button we just set
buttons_obs[lSceneName].SetButtonId(i + offset)
}
}
} else {
// use a text button
oopbutton := buttons.NewTextButton(scene.SceneName)
oopbutton.SetActionHandler(oaction)
o.SD.AddButton(i+offset, oopbutton)
// store which button we just set
buttons_obs[lSceneName].SetButtonId(i + offset)
}
}
// highlight the active scene
if eventb, ok := buttons_obs[obs_current_scene]; ok {
decorator2 := sddecorators.NewBorder(5, color.RGBA{255, 255, 0, 255})
log.Info().Int("button", eventb.ButtonId).Msg("Highlight current scene")
o.SD.SetDecorator(eventb.ButtonId, decorator2)
}
}