-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathvideo.go
More file actions
199 lines (159 loc) · 5.06 KB
/
video.go
File metadata and controls
199 lines (159 loc) · 5.06 KB
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
package kvm
import (
"context"
"fmt"
"time"
"github.com/jetkvm/kvm/internal/native"
"github.com/jetkvm/kvm/internal/sync"
)
var (
lastVideoState native.VideoState
videoSleepModeCtx context.Context
videoSleepModeCancel context.CancelFunc
)
const (
defaultVideoSleepModeDuration = 1 * time.Minute
)
func triggerVideoStateUpdate() {
go func() {
writeJSONRPCEvent("videoInputState", lastVideoState, currentSession)
}()
// Publish video state to MQTT
if mqttManager != nil {
mqttManager.publishVideoState()
}
nativeLogger.Info().Interface("state", lastVideoState).Msg("video state updated")
}
func rpcGetVideoState() (native.VideoState, error) {
notifyFailsafeMode(currentSession)
return lastVideoState, nil
}
var (
hostDisplayAdvertiseLock = sync.Mutex{}
hostDisplayAdvertised bool
)
func configuredVideoEDID() string {
if config.EdidString == "" || isInternalDisabledEDID(config.EdidString) {
return native.DefaultEDID
}
return config.EdidString
}
func isInternalDisabledEDID(edid string) bool {
return edid == native.DisabledEDID
}
func isHostDisplayAdvertised() bool {
hostDisplayAdvertiseLock.Lock()
defer hostDisplayAdvertiseLock.Unlock()
return hostDisplayAdvertised
}
func shouldAdvertiseHostDisplayLocked() bool {
return !config.HideDisplayWhenIdle || getActiveSessions() > 0
}
func setHostDisplayAdvertised(enabled bool, reason string, force bool) error {
hostDisplayAdvertiseLock.Lock()
defer hostDisplayAdvertiseLock.Unlock()
return setHostDisplayAdvertisedLocked(enabled, reason, force)
}
func setHostDisplayAdvertisedLocked(enabled bool, reason string, force bool) error {
if !force && enabled == hostDisplayAdvertised {
return nil
}
edid := native.DisabledEDID
if enabled {
edid = configuredVideoEDID()
}
if err := nativeInstance.VideoSetEDID(edid); err != nil {
nativeLogger.Warn().Err(err).Bool("advertised", enabled).Str("reason", reason).Msg("failed to update host display advertisement")
return err
}
hostDisplayAdvertised = enabled
nativeLogger.Info().Bool("advertised", enabled).Str("reason", reason).Msg("host display advertisement updated")
return nil
}
// applyHostDisplayAdvertisement reconciles the advertised state without rewriting
// EDID when the host display is already in the desired state.
func applyHostDisplayAdvertisement(reason string) error {
return updateHostDisplayAdvertisement(reason, false)
}
// reapplyHostDisplayAdvertisement rewrites EDID even if the advertised
// boolean is unchanged; use it when native state or configured EDID changed.
func reapplyHostDisplayAdvertisement(reason string) error {
return updateHostDisplayAdvertisement(reason, true)
}
func updateHostDisplayAdvertisement(reason string, force bool) error {
hostDisplayAdvertiseLock.Lock()
defer hostDisplayAdvertiseLock.Unlock()
return setHostDisplayAdvertisedLocked(shouldAdvertiseHostDisplayLocked(), reason, force)
}
type rpcVideoSleepModeResponse struct {
Supported bool `json:"supported"`
Enabled bool `json:"enabled"`
Duration int `json:"duration"`
}
func rpcGetVideoSleepMode() rpcVideoSleepModeResponse {
sleepMode, _ := nativeInstance.VideoGetSleepMode()
return rpcVideoSleepModeResponse{
Supported: nativeInstance.VideoSleepModeSupported(),
Enabled: sleepMode,
Duration: config.VideoSleepAfterSec,
}
}
func rpcSetVideoSleepMode(duration int) error {
if duration < 0 {
duration = -1 // disable
}
config.VideoSleepAfterSec = duration
if err := SaveConfig(); err != nil {
return fmt.Errorf("failed to save config: %w", err)
}
// we won't restart the ticker here,
// as the session can't be inactive when this function is called
return nil
}
func stopVideoSleepModeTicker() {
nativeLogger.Trace().Msg("stopping HDMI sleep mode ticker")
if videoSleepModeCancel != nil {
nativeLogger.Trace().Msg("canceling HDMI sleep mode ticker context")
videoSleepModeCancel()
videoSleepModeCancel = nil
videoSleepModeCtx = nil
}
}
func startVideoSleepModeTicker() {
if !nativeInstance.VideoSleepModeSupported() {
return
}
var duration time.Duration
if config.VideoSleepAfterSec == 0 {
duration = defaultVideoSleepModeDuration
} else if config.VideoSleepAfterSec > 0 {
duration = time.Duration(config.VideoSleepAfterSec) * time.Second
} else {
stopVideoSleepModeTicker()
return
}
// Stop any existing timer and goroutine
stopVideoSleepModeTicker()
// Create new context for this ticker
videoSleepModeCtx, videoSleepModeCancel = context.WithCancel(context.Background())
go doVideoSleepModeTicker(videoSleepModeCtx, duration)
}
func doVideoSleepModeTicker(ctx context.Context, duration time.Duration) {
timer := time.NewTimer(duration)
defer timer.Stop()
nativeLogger.Trace().Msg("HDMI sleep mode ticker started")
for {
select {
case <-timer.C:
if getActiveSessions() > 0 {
nativeLogger.Warn().Msg("not going to enter HDMI sleep mode because there are active sessions")
continue
}
nativeLogger.Trace().Msg("entering HDMI sleep mode")
_ = nativeInstance.VideoSetSleepMode(true)
case <-ctx.Done():
nativeLogger.Trace().Msg("HDMI sleep mode ticker stopped")
return
}
}
}