forked from ElTheLedge/Audio-Over-IP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
494 lines (423 loc) · 11.4 KB
/
Copy pathapp.go
File metadata and controls
494 lines (423 loc) · 11.4 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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package main
import (
"context"
_ "embed"
"os/exec"
"runtime"
"sync"
"AuOvIP/pcmresample"
"AuOvIP/wcatools"
"github.com/getlantern/systray"
wRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
)
//go:embed build/windows/icon.ico
var trayIcon []byte
// App struct
type App struct {
ctx context.Context
trayOnce sync.Once
quitMutex sync.Mutex
allowQuit bool
serverToggleItem *systray.MenuItem
serverStatusChan chan bool
serverListChan chan []Server
serverMenuItems map[string]*systray.MenuItem
serverMenuMu sync.Mutex
}
func (a *App) GetServerConfig() ServerConfig {
return serverConfig
}
func (a *App) SaveServerConfig(cfg ServerConfig) error {
configMu.Lock()
serverConfig = cfg
configMu.Unlock()
wRuntime.EventsEmit(a.ctx, "updateServerPagePortDisplay", cfg.Port)
return SaveServerConfig()
}
func (a *App) GetClientConfig() ClientConfig {
return clientConfig
}
func (a *App) SaveClientConfig(cfg ClientConfig) error {
configMu.Lock()
clientConfig = cfg
configMu.Unlock()
return SaveClientConfig()
}
func (a *App) GetLastActiveMode() string {
configMu.Lock()
defer configMu.Unlock()
return appSettings.LastActiveMode
}
func (a *App) SaveLastActiveMode(mode string) error {
configMu.Lock()
appSettings.LastActiveMode = mode
configMu.Unlock()
return SaveAppSettings()
}
func (a *App) GetAppSettings() AppSettings {
configMu.Lock()
defer configMu.Unlock()
return appSettings
}
func (a *App) SaveAppSettings(settings AppSettings) error {
configMu.Lock()
appSettings = settings
configMu.Unlock()
return SaveAppSettings()
}
func (a *App) GetConfigDir() string {
dir, _ := GetConfigDir()
return dir
}
func (a *App) OpenConfigDir() error {
dir, err := GetConfigDir()
if err != nil {
return err
}
return exec.Command("explorer", dir).Start()
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
serverStatusChan: make(chan bool, 10),
serverListChan: make(chan []Server, 10),
serverMenuItems: make(map[string]*systray.MenuItem),
}
}
// startup is called at application startup
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.initTray()
_ = LoadConfig()
InitDeviceMonitor()
for _, s := range ListServers() {
go func(id string) {
_ = ReconnectServer(id)
}(s.ID)
}
// Auto-start broadcasting if enabled
configMu.Lock()
autoStart := appSettings.AutoStartBroadcasting
configMu.Unlock()
if autoStart {
a.EnableServer()
}
}
// domReady is called after front-end resources have been loaded
func (a App) domReady(ctx context.Context) {
//Start connection status frontend updater
go connStatusUpdater()
go serverStatsUpdater()
go clientBandwidthUpdater()
}
// beforeClose is called when the application is about to quit,
// either by clicking the window close button or calling runtime.Quit.
// Returning true will cause the application to continue, false will continue shutdown as normal.
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
if runtime.GOOS != "windows" {
return false
}
if a.shouldAllowQuit() {
return false
}
wRuntime.WindowHide(a.ctx)
return true
}
// shutdown is called at application termination
func (a *App) shutdown(ctx context.Context) {
if runtime.GOOS == "windows" {
systray.Quit()
}
}
func (a *App) shouldAllowQuit() bool {
a.quitMutex.Lock()
defer a.quitMutex.Unlock()
return a.allowQuit
}
func (a *App) setAllowQuit(allow bool) {
a.quitMutex.Lock()
a.allowQuit = allow
a.quitMutex.Unlock()
}
func (a *App) initTray() {
if runtime.GOOS != "windows" {
return
}
a.trayOnce.Do(func() {
go systray.Run(a.onTrayReady, func() {})
})
}
type serverMenuData struct {
server Server
menuItem *systray.MenuItem
refreshItem *systray.MenuItem
connectItem *systray.MenuItem
disconnectItem *systray.MenuItem
reconnectItem *systray.MenuItem
}
func (a *App) onTrayReady() {
systray.SetIcon(trayIcon)
systray.SetTitle("Audio Over IP")
systray.SetTooltip("Audio Over IP")
// Create "Inbound Connections" section at the top
inboundSection := systray.AddMenuItem("Inbound Connections", "List of connected servers")
// Track server menu items - servers will be added as submenus under Inbound Connections
serverMenus := make(map[string]*serverMenuData)
// Initial server list population
go func() {
servers := a.GetServerList()
if len(servers) > 0 {
select {
case a.serverListChan <- servers:
default:
}
}
}()
systray.AddSeparator()
showItem := systray.AddMenuItem("Show", "Show Audio Over IP")
hideItem := systray.AddMenuItem("Hide", "Hide Audio Over IP")
systray.AddSeparator()
// Add server toggle menu item
serverLabel := "Enable Server Mode"
if serverIsOn {
serverLabel = "Disable Server Mode"
}
a.serverToggleItem = systray.AddMenuItem(serverLabel, "Toggle server mode on/off")
systray.AddSeparator()
quitItem := systray.AddMenuItem("Quit", "Quit Audio Over IP")
for {
select {
case <-showItem.ClickedCh:
wRuntime.WindowShow(a.ctx)
wRuntime.WindowUnminimise(a.ctx)
case <-hideItem.ClickedCh:
wRuntime.WindowHide(a.ctx)
case <-a.serverToggleItem.ClickedCh:
// Toggle server mode
if serverIsOn {
a.DisableServer()
} else {
a.EnableServer()
}
case enabled := <-a.serverStatusChan:
// Update menu item label when server status changes
if enabled {
a.serverToggleItem.SetTitle("Disable Server Mode")
} else {
a.serverToggleItem.SetTitle("Enable Server Mode")
}
case servers := <-a.serverListChan:
// Update server menus
a.updateServerMenus(inboundSection, servers, serverMenus)
case <-quitItem.ClickedCh:
a.setAllowQuit(true)
systray.Quit()
wRuntime.Quit(a.ctx)
}
}
}
func (a *App) updateServerMenus(inboundSection *systray.MenuItem, servers []Server, serverMenus map[string]*serverMenuData) {
// Remove servers that no longer exist
for id, menu := range serverMenus {
found := false
for _, s := range servers {
if s.ID == id {
found = true
break
}
}
if !found {
menu.menuItem.Hide()
delete(serverMenus, id)
}
}
// Add or update existing servers
for _, server := range servers {
if menu, exists := serverMenus[server.ID]; exists {
// Update existing menu item title
title := server.Hostname
if title == "" {
title = server.Addr
}
if server.Online {
title += " ✓"
} else {
title += " ✗"
}
menu.menuItem.SetTitle(title)
menu.server = server
// Disable refresh if connected or connecting
isConnected := server.Status == "Connected" || server.Status == "Connecting..."
if isConnected {
menu.refreshItem.Disable()
} else {
menu.refreshItem.Enable()
}
} else {
// Create new menu item as submenu under Inbound Connections
title := server.Hostname
if title == "" {
title = server.Addr
}
if server.Online {
title += " ✓"
} else {
title += " ✗"
}
menuItem := inboundSection.AddSubMenuItem(title, "Inbound connection: "+server.Addr)
refreshItem := menuItem.AddSubMenuItem("Refresh", "Refresh connection info")
connectItem := menuItem.AddSubMenuItem("Connect", "Connect to this server")
disconnectItem := menuItem.AddSubMenuItem("Disconnect", "Disconnect from this server")
reconnectItem := menuItem.AddSubMenuItem("Reconnect", "Reconnect to this server")
menu := &serverMenuData{
server: server,
menuItem: menuItem,
refreshItem: refreshItem,
connectItem: connectItem,
disconnectItem: disconnectItem,
reconnectItem: reconnectItem,
}
serverMenus[server.ID] = menu
// Disable refresh if connected or connecting
isConnected := server.Status == "Connected" || server.Status == "Connecting..."
if isConnected {
menu.refreshItem.Disable()
}
// Start goroutine to handle clicks for this server
go a.handleServerMenuClicks(menu)
}
}
}
func (a *App) handleServerMenuClicks(menu *serverMenuData) {
for {
select {
case <-menu.refreshItem.ClickedCh:
go a.CheckServerConnection(menu.server.ID)
case <-menu.connectItem.ClickedCh:
go a.ConnectToAudioServer(menu.server.ID)
case <-menu.disconnectItem.ClickedCh:
go a.DisconnetFromAudioServer(menu.server.ID)
case <-menu.reconnectItem.ClickedCh:
go func() {
a.DisconnetFromAudioServer(menu.server.ID)
a.ConnectToAudioServer(menu.server.ID)
}()
}
}
}
func (a *App) notifyTrayServerListChanged(servers []Server) {
select {
case a.serverListChan <- servers:
default:
}
}
func (a *App) QuitApp() {
wRuntime.Quit(a.ctx)
}
func (a *App) MinimiseApp() {
wRuntime.WindowMinimise(a.ctx)
}
// Server management exposed to frontend
func (a *App) AddServer(ipAddress string, port string) error {
_, err := AddServer(ipAddress, port)
if err == nil {
servers := ListServers()
wRuntime.EventsEmit(a.ctx, "serversUpdated", servers)
a.notifyTrayServerListChanged(servers)
}
return err
}
func (a *App) CheckServerConnection(id string) error {
return ReconnectServer(id)
}
func (a *App) RemoveServer(id string) {
RemoveServer(id)
servers := ListServers()
wRuntime.EventsEmit(a.ctx, "serversUpdated", servers)
a.notifyTrayServerListChanged(servers)
}
func (a *App) ReorderServers(newOrder []string) {
ReorderServers(newOrder)
}
func (a *App) GetServerList() []Server {
return ListServers()
}
func (a *App) UpdateServerSettings(id string, opts pcmresample.Options) {
err := UpdateServerSettings(id, opts)
if err != nil {
clientLogger.Errorf("%s", err.Error())
}
wRuntime.EventsEmit(a.ctx, "serversUpdated", ListServers())
}
func (a *App) ConnectToAudioServer(id string) {
// start connect in background (launches a go routine)
err := ConnectServer(id)
if err != nil {
clientLogger.Errorf("%s", err.Error())
}
}
func (a *App) DisconnetFromAudioServer(id string) {
err := DisconnectServer(id)
if err != nil {
clientLogger.Errorf("%s", err.Error())
}
}
var serverIsOn = false
func (a *App) GetServerEnabled() bool {
return serverIsOn
}
func (a *App) GetConnectedClients() []ConnectedClient {
return GetConnectedClients()
}
func (a *App) EnableServer() {
serverIsOn = true
wRuntime.EventsEmit(a.ctx, "updateServerStatus", serverIsOn)
// Notify systray to update menu label
select {
case a.serverStatusChan <- true:
default:
}
// Start server in a separate goroutine to avoid blocking
go startServer(serverConfig.Port)
}
func (a *App) DisableServer() {
stopServer()
serverIsOn = false
wRuntime.EventsEmit(a.ctx, "updateServerStatus", serverIsOn)
// Notify systray to update menu label
select {
case a.serverStatusChan <- false:
default:
}
}
func (a *App) ListAudioDevices() ([]wcatools.AudioConfig, error) {
return wcatools.ListDevices()
}
func (a *App) AddPlaybackDevice(serverID, deviceID string) error {
err := AddPlaybackDevice(serverID, deviceID)
if err == nil {
wRuntime.EventsEmit(a.ctx, "serversUpdated", ListServers())
}
return err
}
func (a *App) RemovePlaybackDevice(serverID, deviceID string) error {
err := RemovePlaybackDevice(serverID, deviceID)
if err == nil {
wRuntime.EventsEmit(a.ctx, "serversUpdated", ListServers())
}
return err
}
func (a *App) UpdatePlaybackDevice(serverID, deviceID string, enabled bool, volume float32) error {
err := UpdatePlaybackDevice(serverID, deviceID, enabled, volume)
if err == nil {
wRuntime.EventsEmit(a.ctx, "serversUpdated", ListServers())
}
return err
}
func (a *App) SetServerRemoteDevice(serverID, deviceID string) error {
err := SetServerRemoteDevice(serverID, deviceID)
if err == nil {
wRuntime.EventsEmit(a.ctx, "serversUpdated", ListServers())
}
return err
}