Skip to content

Commit c9f4aea

Browse files
committed
Fix: golangci-lint errors
1 parent 3dc196b commit c9f4aea

19 files changed

+421
-123
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
version: "2"
2+
run:
3+
build-tags:
4+
- nolint
25
linters:
36
enable:
47
- forbidigo

cloud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func handleSessionRequest(
454454
// Check if we have an existing session and handle renegotiation
455455
if currentSession != nil {
456456
scopedLogger.Info().Msg("handling renegotiation for existing session")
457-
457+
458458
// Handle renegotiation with existing session
459459
sd, err = currentSession.ExchangeOffer(req.Sd)
460460
if err != nil {

internal/audio/cgo_audio.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build !nolint
2+
13
package audio
24

35
import (
@@ -54,7 +56,7 @@ int jetkvm_audio_read_encode(void *opus_buf) {
5456
short pcm_buffer[1920]; // max 2ch*960
5557
unsigned char *out = (unsigned char*)opus_buf;
5658
int pcm_rc = snd_pcm_readi(pcm_handle, pcm_buffer, frame_size);
57-
59+
5860
// Handle ALSA errors with recovery
5961
if (pcm_rc < 0) {
6062
if (pcm_rc == -EPIPE) {
@@ -70,12 +72,12 @@ int jetkvm_audio_read_encode(void *opus_buf) {
7072
return -1;
7173
}
7274
}
73-
75+
7476
// If we got fewer frames than expected, pad with silence
7577
if (pcm_rc < frame_size) {
7678
memset(&pcm_buffer[pcm_rc * channels], 0, (frame_size - pcm_rc) * channels * sizeof(short));
7779
}
78-
80+
7981
int nb_bytes = opus_encode(encoder, pcm_buffer, frame_size, out, max_packet_size);
8082
return nb_bytes;
8183
}
@@ -85,15 +87,15 @@ int jetkvm_audio_playback_init() {
8587
int err;
8688
snd_pcm_hw_params_t *params;
8789
if (pcm_playback_handle) return 0;
88-
90+
8991
// Try to open the USB gadget audio device for playback
9092
// This should correspond to the capture endpoint of the USB gadget
9193
if (snd_pcm_open(&pcm_playback_handle, "hw:1,0", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
9294
// Fallback to default device if hw:1,0 doesn't work for playback
9395
if (snd_pcm_open(&pcm_playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0)
9496
return -1;
9597
}
96-
98+
9799
snd_pcm_hw_params_malloc(&params);
98100
snd_pcm_hw_params_any(pcm_playback_handle, params);
99101
snd_pcm_hw_params_set_access(pcm_playback_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
@@ -104,23 +106,23 @@ int jetkvm_audio_playback_init() {
104106
snd_pcm_hw_params(pcm_playback_handle, params);
105107
snd_pcm_hw_params_free(params);
106108
snd_pcm_prepare(pcm_playback_handle);
107-
109+
108110
// Initialize Opus decoder
109111
decoder = opus_decoder_create(sample_rate, channels, &err);
110112
if (!decoder) return -2;
111-
113+
112114
return 0;
113115
}
114116
115117
// Decode Opus and write PCM to playback device
116118
int jetkvm_audio_decode_write(void *opus_buf, int opus_size) {
117119
short pcm_buffer[1920]; // max 2ch*960
118120
unsigned char *in = (unsigned char*)opus_buf;
119-
121+
120122
// Decode Opus to PCM
121123
int pcm_frames = opus_decode(decoder, in, opus_size, pcm_buffer, frame_size, 0);
122124
if (pcm_frames < 0) return -1;
123-
125+
124126
// Write PCM to playback device
125127
int pcm_rc = snd_pcm_writei(pcm_playback_handle, pcm_buffer, pcm_frames);
126128
if (pcm_rc < 0) {
@@ -131,7 +133,7 @@ int jetkvm_audio_decode_write(void *opus_buf, int opus_size) {
131133
}
132134
if (pcm_rc < 0) return -2;
133135
}
134-
136+
135137
return pcm_frames;
136138
}
137139
@@ -148,8 +150,6 @@ void jetkvm_audio_close() {
148150
*/
149151
import "C"
150152

151-
152-
153153
// Go wrappers for initializing, starting, stopping, and controlling audio
154154
func cgoAudioInit() error {
155155
ret := C.jetkvm_audio_init()
@@ -179,8 +179,6 @@ func cgoAudioReadEncode(buf []byte) (int, error) {
179179
return int(n), nil
180180
}
181181

182-
183-
184182
// Go wrappers for audio playback (microphone input)
185183
func cgoAudioPlaybackInit() error {
186184
ret := C.jetkvm_audio_playback_init()
@@ -206,8 +204,6 @@ func cgoAudioDecodeWrite(buf []byte) (int, error) {
206204
return int(n), nil
207205
}
208206

209-
210-
211207
// Wrapper functions for non-blocking audio manager
212208
func CGOAudioInit() error {
213209
return cgoAudioInit()

internal/audio/cgo_audio_stub.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,30 @@ func cgoAudioPlaybackClose() {
2828

2929
func cgoAudioDecodeWrite(buf []byte) (int, error) {
3030
return 0, errors.New("audio not available in lint mode")
31-
}
31+
}
32+
33+
// Uppercase wrapper functions (called by nonblocking_audio.go)
34+
35+
func CGOAudioInit() error {
36+
return cgoAudioInit()
37+
}
38+
39+
func CGOAudioClose() {
40+
cgoAudioClose()
41+
}
42+
43+
func CGOAudioReadEncode(buf []byte) (int, error) {
44+
return cgoAudioReadEncode(buf)
45+
}
46+
47+
func CGOAudioPlaybackInit() error {
48+
return cgoAudioPlaybackInit()
49+
}
50+
51+
func CGOAudioPlaybackClose() {
52+
cgoAudioPlaybackClose()
53+
}
54+
55+
func CGOAudioDecodeWrite(buf []byte) (int, error) {
56+
return cgoAudioDecodeWrite(buf)
57+
}

internal/audio/input.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// AudioInputMetrics holds metrics for microphone input
1212
// Note: int64 fields must be 64-bit aligned for atomic operations on ARM
1313
type AudioInputMetrics struct {
14-
FramesSent int64 // Must be first for alignment
14+
FramesSent int64 // Must be first for alignment
1515
FramesDropped int64
1616
BytesProcessed int64
1717
ConnectionDrops int64
@@ -22,8 +22,8 @@ type AudioInputMetrics struct {
2222
// AudioInputManager manages microphone input stream from WebRTC to USB gadget
2323
type AudioInputManager struct {
2424
// metrics MUST be first for ARM32 alignment (contains int64 fields)
25-
metrics AudioInputMetrics
26-
25+
metrics AudioInputMetrics
26+
2727
inputBuffer chan []byte
2828
logger zerolog.Logger
2929
running int32
@@ -44,7 +44,7 @@ func (aim *AudioInputManager) Start() error {
4444
}
4545

4646
aim.logger.Info().Msg("Starting audio input manager")
47-
47+
4848
// Start the non-blocking audio input stream
4949
err := StartNonBlockingAudioInput(aim.inputBuffer)
5050
if err != nil {
@@ -62,11 +62,11 @@ func (aim *AudioInputManager) Stop() {
6262
}
6363

6464
aim.logger.Info().Msg("Stopping audio input manager")
65-
65+
6666
// Stop the non-blocking audio input stream
6767
// Note: This is handled by the global non-blocking audio manager
6868
// Individual input streams are managed centrally
69-
69+
7070
// Drain the input buffer
7171
go func() {
7272
for {
@@ -115,4 +115,4 @@ func (aim *AudioInputManager) GetMetrics() AudioInputMetrics {
115115
// IsRunning returns whether the audio input manager is running
116116
func (aim *AudioInputManager) IsRunning() bool {
117117
return atomic.LoadInt32(&aim.running) == 1
118-
}
118+
}

internal/audio/nonblocking_api.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ func StartNonBlockingAudioStreaming(send func([]byte)) error {
1414
managerMutex.Lock()
1515
defer managerMutex.Unlock()
1616

17-
if globalNonBlockingManager != nil && globalNonBlockingManager.IsRunning() {
18-
return ErrAudioAlreadyRunning
17+
if globalNonBlockingManager != nil && globalNonBlockingManager.IsOutputRunning() {
18+
return nil // Already running, this is not an error
19+
}
20+
21+
if globalNonBlockingManager == nil {
22+
globalNonBlockingManager = NewNonBlockingAudioManager()
1923
}
2024

21-
globalNonBlockingManager = NewNonBlockingAudioManager()
2225
return globalNonBlockingManager.StartAudioOutput(send)
2326
}
2427

@@ -31,6 +34,11 @@ func StartNonBlockingAudioInput(receiveChan <-chan []byte) error {
3134
globalNonBlockingManager = NewNonBlockingAudioManager()
3235
}
3336

37+
// Check if input is already running to avoid unnecessary operations
38+
if globalNonBlockingManager.IsInputRunning() {
39+
return nil // Already running, this is not an error
40+
}
41+
3442
return globalNonBlockingManager.StartAudioInput(receiveChan)
3543
}
3644

@@ -45,6 +53,16 @@ func StopNonBlockingAudioStreaming() {
4553
}
4654
}
4755

56+
// StopNonBlockingAudioInput stops only the audio input without affecting output
57+
func StopNonBlockingAudioInput() {
58+
managerMutex.Lock()
59+
defer managerMutex.Unlock()
60+
61+
if globalNonBlockingManager != nil && globalNonBlockingManager.IsInputRunning() {
62+
globalNonBlockingManager.StopAudioInput()
63+
}
64+
}
65+
4866
// GetNonBlockingAudioStats returns statistics from the non-blocking audio system
4967
func GetNonBlockingAudioStats() NonBlockingAudioStats {
5068
managerMutex.Lock()
@@ -62,4 +80,12 @@ func IsNonBlockingAudioRunning() bool {
6280
defer managerMutex.Unlock()
6381

6482
return globalNonBlockingManager != nil && globalNonBlockingManager.IsRunning()
65-
}
83+
}
84+
85+
// IsNonBlockingAudioInputRunning returns true if the non-blocking audio input is running
86+
func IsNonBlockingAudioInputRunning() bool {
87+
managerMutex.Lock()
88+
defer managerMutex.Unlock()
89+
90+
return globalNonBlockingManager != nil && globalNonBlockingManager.IsInputRunning()
91+
}

internal/audio/nonblocking_audio.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ type NonBlockingAudioManager struct {
2323
logger *zerolog.Logger
2424

2525
// Audio output (capture from device, send to WebRTC)
26-
outputSendFunc func([]byte)
27-
outputWorkChan chan audioWorkItem
26+
outputSendFunc func([]byte)
27+
outputWorkChan chan audioWorkItem
2828
outputResultChan chan audioResult
2929

30-
// Audio input (receive from WebRTC, playback to device)
30+
// Audio input (receive from WebRTC, playback to device)
3131
inputReceiveChan <-chan []byte
32-
inputWorkChan chan audioWorkItem
33-
inputResultChan chan audioResult
32+
inputWorkChan chan audioWorkItem
33+
inputResultChan chan audioResult
3434

3535
// Worker threads and flags - int32 fields grouped together
3636
outputRunning int32
@@ -69,7 +69,7 @@ type NonBlockingAudioStats struct {
6969
InputFramesDropped int64
7070
WorkerErrors int64
7171
// time.Time is int64 internally, so it's also aligned
72-
LastProcessTime time.Time
72+
LastProcessTime time.Time
7373
}
7474

7575
// NewNonBlockingAudioManager creates a new non-blocking audio manager
@@ -81,8 +81,8 @@ func NewNonBlockingAudioManager() *NonBlockingAudioManager {
8181
ctx: ctx,
8282
cancel: cancel,
8383
logger: &logger,
84-
outputWorkChan: make(chan audioWorkItem, 10), // Buffer for work items
85-
outputResultChan: make(chan audioResult, 10), // Buffer for results
84+
outputWorkChan: make(chan audioWorkItem, 10), // Buffer for work items
85+
outputResultChan: make(chan audioResult, 10), // Buffer for results
8686
inputWorkChan: make(chan audioWorkItem, 10),
8787
inputResultChan: make(chan audioResult, 10),
8888
}
@@ -327,7 +327,7 @@ func (nam *NonBlockingAudioManager) inputCoordinatorThread() {
327327
return
328328

329329
case frame := <-nam.inputReceiveChan:
330-
if frame == nil || len(frame) == 0 {
330+
if len(frame) == 0 {
331331
continue
332332
}
333333

@@ -397,6 +397,16 @@ func (nam *NonBlockingAudioManager) Stop() {
397397
nam.logger.Info().Msg("non-blocking audio manager stopped")
398398
}
399399

400+
// StopAudioInput stops only the audio input operations
401+
func (nam *NonBlockingAudioManager) StopAudioInput() {
402+
nam.logger.Info().Msg("stopping audio input")
403+
404+
// Stop only the input coordinator
405+
atomic.StoreInt32(&nam.inputRunning, 0)
406+
407+
nam.logger.Info().Msg("audio input stopped")
408+
}
409+
400410
// GetStats returns current statistics
401411
func (nam *NonBlockingAudioManager) GetStats() NonBlockingAudioStats {
402412
return NonBlockingAudioStats{
@@ -412,4 +422,14 @@ func (nam *NonBlockingAudioManager) GetStats() NonBlockingAudioStats {
412422
// IsRunning returns true if any audio operations are running
413423
func (nam *NonBlockingAudioManager) IsRunning() bool {
414424
return atomic.LoadInt32(&nam.outputRunning) == 1 || atomic.LoadInt32(&nam.inputRunning) == 1
415-
}
425+
}
426+
427+
// IsInputRunning returns true if audio input is running
428+
func (nam *NonBlockingAudioManager) IsInputRunning() bool {
429+
return atomic.LoadInt32(&nam.inputRunning) == 1
430+
}
431+
432+
// IsOutputRunning returns true if audio output is running
433+
func (nam *NonBlockingAudioManager) IsOutputRunning() bool {
434+
return atomic.LoadInt32(&nam.outputRunning) == 1
435+
}

jsonrpc.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121

2222
// Mouse event processing with single worker
2323
var (
24-
mouseEventChan = make(chan mouseEventData, 100) // Buffered channel for mouse events
25-
mouseWorkerOnce sync.Once
24+
mouseEventChan = make(chan mouseEventData, 100) // Buffered channel for mouse events
25+
mouseWorkerOnce sync.Once
2626
)
2727

2828
type mouseEventData struct {
@@ -35,15 +35,15 @@ func startMouseWorker() {
3535
go func() {
3636
ticker := time.NewTicker(16 * time.Millisecond) // ~60 FPS
3737
defer ticker.Stop()
38-
38+
3939
var latestMouseEvent *mouseEventData
40-
40+
4141
for {
4242
select {
4343
case event := <-mouseEventChan:
4444
// Always keep the latest mouse event
4545
latestMouseEvent = &event
46-
46+
4747
case <-ticker.C:
4848
// Process the latest mouse event at regular intervals
4949
if latestMouseEvent != nil {
@@ -68,7 +68,7 @@ func onRPCMessageThrottled(message webrtc.DataChannelMessage, session *Session)
6868
if isMouseEvent(request.Method) {
6969
// Start the mouse worker if not already started
7070
mouseWorkerOnce.Do(startMouseWorker)
71-
71+
7272
// Send to mouse worker (non-blocking)
7373
select {
7474
case mouseEventChan <- mouseEventData{message: message, session: session}:

0 commit comments

Comments
 (0)