forked from omriharel/deej
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsession_windows.go
198 lines (149 loc) · 4.49 KB
/
session_windows.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
package deej
import (
"errors"
"fmt"
"strings"
ole "github.com/go-ole/go-ole"
ps "github.com/mitchellh/go-ps"
wca "github.com/moutend/go-wca"
"go.uber.org/zap"
)
var errNoSuchProcess = errors.New("No such process")
var errRefreshSessions = errors.New("Trigger session refresh")
type wcaSession struct {
baseSession
pid uint32
processName string
control *wca.IAudioSessionControl2
volume *wca.ISimpleAudioVolume
eventCtx *ole.GUID
}
type masterSession struct {
baseSession
volume *wca.IAudioEndpointVolume
eventCtx *ole.GUID
stale bool // when set to true, we should refresh sessions on the next call to SetVolume
}
func newWCASession(
logger *zap.SugaredLogger,
control *wca.IAudioSessionControl2,
volume *wca.ISimpleAudioVolume,
pid uint32,
eventCtx *ole.GUID,
) (*wcaSession, error) {
s := &wcaSession{
control: control,
volume: volume,
pid: pid,
eventCtx: eventCtx,
}
// special treatment for system sounds session
if pid == 0 {
s.system = true
s.name = systemSessionName
s.humanReadableDesc = "system sounds"
} else {
// find our session's process name
process, err := ps.FindProcess(int(pid))
if err != nil {
logger.Warnw("Failed to find process name by ID", "pid", pid, "error", err)
defer s.Release()
return nil, fmt.Errorf("find process name by pid: %w", err)
}
// this PID may be invalid - this means the process has already been
// closed and we shouldn't create a session for it.
if process == nil {
logger.Debugw("Process already exited, not creating audio session", "pid", pid)
return nil, errNoSuchProcess
}
s.processName = process.Executable()
s.name = s.processName
s.humanReadableDesc = fmt.Sprintf("%s (pid %d)", s.processName, s.pid)
}
// use a self-identifying session name e.g. deej.sessions.chrome
s.logger = logger.Named(strings.TrimSuffix(s.Key(), ".exe"))
s.logger.Debugw(sessionCreationLogMessage, "session", s)
return s, nil
}
func newMasterSession(
logger *zap.SugaredLogger,
volume *wca.IAudioEndpointVolume,
eventCtx *ole.GUID,
key string,
loggerKey string,
) (*masterSession, error) {
s := &masterSession{
volume: volume,
eventCtx: eventCtx,
}
s.logger = logger.Named(loggerKey)
s.master = true
s.name = key
s.humanReadableDesc = key
s.logger.Debugw(sessionCreationLogMessage, "session", s)
return s, nil
}
func (s *wcaSession) GetVolume() float32 {
var level float32
if err := s.volume.GetMasterVolume(&level); err != nil {
s.logger.Warnw("Failed to get session volume", "error", err)
}
return level
}
func (s *wcaSession) SetVolume(v float32) error {
if err := s.volume.SetMasterVolume(v, s.eventCtx); err != nil {
s.logger.Warnw("Failed to set session volume", "error", err)
return fmt.Errorf("adjust session volume: %w", err)
}
// mitigate expired sessions by checking the state whenever we change volumes
var state uint32
if err := s.control.GetState(&state); err != nil {
s.logger.Warnw("Failed to get session state while setting volume", "error", err)
return fmt.Errorf("get session state: %w", err)
}
if state == wca.AudioSessionStateExpired {
s.logger.Warnw("Audio session expired, triggering session refresh")
return errRefreshSessions
}
s.logger.Debugw("Adjusting session volume", "to", fmt.Sprintf("%.2f", v))
return nil
}
func (s *wcaSession) Release() {
s.logger.Debug("Releasing audio session")
s.volume.Release()
s.control.Release()
}
func (s *wcaSession) String() string {
return fmt.Sprintf(sessionStringFormat, s.humanReadableDesc, s.GetVolume())
}
func (s *masterSession) GetVolume() float32 {
var level float32
if err := s.volume.GetMasterVolumeLevelScalar(&level); err != nil {
s.logger.Warnw("Failed to get session volume", "error", err)
}
return level
}
func (s *masterSession) SetVolume(v float32) error {
if s.stale {
s.logger.Warnw("Session expired because default device has changed, triggering session refresh")
return errRefreshSessions
}
if err := s.volume.SetMasterVolumeLevelScalar(v, s.eventCtx); err != nil {
s.logger.Warnw("Failed to set session volume",
"error", err,
"volume", v)
return fmt.Errorf("adjust session volume: %w", err)
}
s.logger.Debugw("Adjusting session volume", "to", fmt.Sprintf("%.2f", v))
return nil
}
func (s *masterSession) Release() {
s.logger.Debug("Releasing audio session")
s.volume.Release()
}
func (s *masterSession) String() string {
return fmt.Sprintf(sessionStringFormat, s.humanReadableDesc, s.GetVolume())
}
func (s *masterSession) markAsStale() {
s.stale = true
}