fix: Address linting errors in audio code

- Check SetReadDeadline error in IPC client
- Explicitly ignore Kill() error (process may be dead)
- Remove init() function and rely on explicit ExtractEmbeddedBinaries() call
This commit is contained in:
Alex P 2025-10-06 22:12:20 +03:00
parent 7872ddc8fc
commit 141e2f9099
4 changed files with 31 additions and 39 deletions

View File

@ -15,10 +15,10 @@ var audioOutputBinary []byte
var audioInputBinary []byte var audioInputBinary []byte
const ( const (
audioBinDir = "/userdata/jetkvm/bin" audioBinDir = "/userdata/jetkvm/bin"
audioOutputBinPath = audioBinDir + "/jetkvm_audio_output" audioOutputBinPath = audioBinDir + "/jetkvm_audio_output"
audioInputBinPath = audioBinDir + "/jetkvm_audio_input" audioInputBinPath = audioBinDir + "/jetkvm_audio_input"
binaryFileMode = 0755 // rwxr-xr-x binaryFileMode = 0755 // rwxr-xr-x
) )
// ExtractEmbeddedBinaries extracts the embedded C audio binaries to disk // ExtractEmbeddedBinaries extracts the embedded C audio binaries to disk
@ -78,13 +78,3 @@ func GetAudioOutputBinaryPath() string {
func GetAudioInputBinaryPath() string { func GetAudioInputBinaryPath() string {
return audioInputBinPath return audioInputBinPath
} }
// init ensures binaries are extracted when package is imported
func init() {
// Extract binaries on package initialization
// This ensures binaries are available before supervisors start
if err := ExtractEmbeddedBinaries(); err != nil {
// Log error but don't panic - let caller handle initialization failure
fmt.Fprintf(os.Stderr, "Warning: Failed to extract embedded audio binaries: %v\n", err)
}
}

View File

@ -22,15 +22,15 @@ var writeBufferPool = sync.Pool{
// IPC Protocol constants (matches C implementation in ipc_protocol.h) // IPC Protocol constants (matches C implementation in ipc_protocol.h)
const ( const (
ipcMagicOutput = 0x4A4B4F55 // "JKOU" - Output (device → browser) ipcMagicOutput = 0x4A4B4F55 // "JKOU" - Output (device → browser)
ipcMagicInput = 0x4A4B4D49 // "JKMI" - Input (browser → device) ipcMagicInput = 0x4A4B4D49 // "JKMI" - Input (browser → device)
ipcHeaderSize = 9 // Reduced from 17 (removed 8-byte timestamp) ipcHeaderSize = 9 // Reduced from 17 (removed 8-byte timestamp)
ipcMaxFrameSize = 1024 // 128kbps @ 20ms = ~600 bytes worst case with VBR+FEC ipcMaxFrameSize = 1024 // 128kbps @ 20ms = ~600 bytes worst case with VBR+FEC
ipcMsgTypeOpus = 0 ipcMsgTypeOpus = 0
ipcMsgTypeConfig = 1 ipcMsgTypeConfig = 1
ipcMsgTypeStop = 3 ipcMsgTypeStop = 3
connectTimeout = 5 * time.Second connectTimeout = 5 * time.Second
readTimeout = 2 * time.Second readTimeout = 2 * time.Second
) )
// IPCClient manages Unix socket communication with audio subprocess // IPCClient manages Unix socket communication with audio subprocess
@ -109,7 +109,9 @@ func (c *IPCClient) ReadMessage() (uint8, []byte, error) {
} }
// Set read deadline // Set read deadline
c.conn.SetReadDeadline(time.Now().Add(readTimeout)) if err := c.conn.SetReadDeadline(time.Now().Add(readTimeout)); err != nil {
return 0, nil, fmt.Errorf("failed to set read deadline: %w", err)
}
// Read 9-byte header // Read 9-byte header
var header [ipcHeaderSize]byte var header [ipcHeaderSize]byte

View File

@ -21,12 +21,12 @@ type Supervisor struct {
socketPath string socketPath string
env []string env []string
cmd *exec.Cmd cmd *exec.Cmd
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
running atomic.Bool running atomic.Bool
done chan struct{} // Closed when supervision loop exits done chan struct{} // Closed when supervision loop exits
logger zerolog.Logger logger zerolog.Logger
// Restart state // Restart state
restartCount uint8 restartCount uint8
@ -81,7 +81,7 @@ func (s *Supervisor) Stop() {
// Kill process if running // Kill process if running
if s.cmd != nil && s.cmd.Process != nil { if s.cmd != nil && s.cmd.Process != nil {
s.cmd.Process.Kill() _ = s.cmd.Process.Kill() // Ignore error, process may already be dead
} }
// Wait for supervision loop to exit // Wait for supervision loop to exit

View File

@ -66,14 +66,14 @@ var defaultGadgetConfig = map[string]gadgetConfigItem{
path: []string{"functions", "uac1.usb0"}, path: []string{"functions", "uac1.usb0"},
configPath: []string{"uac1.usb0"}, configPath: []string{"uac1.usb0"},
attrs: gadgetAttributes{ attrs: gadgetAttributes{
"p_chmask": "3", // Playback: stereo (2 channels) "p_chmask": "3", // Playback: stereo (2 channels)
"p_srate": "48000", // Playback: 48kHz sample rate "p_srate": "48000", // Playback: 48kHz sample rate
"p_ssize": "2", // Playback: 16-bit (2 bytes) "p_ssize": "2", // Playback: 16-bit (2 bytes)
"p_volume_present": "0", // Playback: no volume control "p_volume_present": "0", // Playback: no volume control
"c_chmask": "3", // Capture: stereo (2 channels) "c_chmask": "3", // Capture: stereo (2 channels)
"c_srate": "48000", // Capture: 48kHz sample rate "c_srate": "48000", // Capture: 48kHz sample rate
"c_ssize": "2", // Capture: 16-bit (2 bytes) "c_ssize": "2", // Capture: 16-bit (2 bytes)
"c_volume_present": "0", // Capture: no volume control "c_volume_present": "0", // Capture: no volume control
}, },
}, },
} }