[WIP] Cleanup: PR Cleanup

This commit is contained in:
Alex P 2025-09-20 00:21:09 +03:00
parent 1dbc6c9d06
commit 432303e228
3 changed files with 49 additions and 56 deletions

View File

@ -13,7 +13,7 @@ import (
var audioControlService *audio.AudioControlService var audioControlService *audio.AudioControlService
func initAudioControlService() { func ensureAudioControlService() *audio.AudioControlService {
if audioControlService == nil { if audioControlService == nil {
sessionProvider := &SessionProviderImpl{} sessionProvider := &SessionProviderImpl{}
audioControlService = audio.NewAudioControlService(sessionProvider, logger) audioControlService = audio.NewAudioControlService(sessionProvider, logger)
@ -31,50 +31,44 @@ func initAudioControlService() {
return nil return nil
}) })
} }
return audioControlService
} }
// --- Global Convenience Functions for Audio Control --- // --- Global Convenience Functions for Audio Control ---
// StopAudioOutputAndRemoveTracks is a global helper to stop audio output subprocess and remove WebRTC tracks // MuteAudioOutput is a global helper to mute audio output
func StopAudioOutputAndRemoveTracks() error { func MuteAudioOutput() error {
initAudioControlService() return ensureAudioControlService().MuteAudio(true)
return audioControlService.MuteAudio(true)
} }
// StartAudioOutputAndAddTracks is a global helper to start audio output subprocess and add WebRTC tracks // UnmuteAudioOutput is a global helper to unmute audio output
func StartAudioOutputAndAddTracks() error { func UnmuteAudioOutput() error {
initAudioControlService() return ensureAudioControlService().MuteAudio(false)
return audioControlService.MuteAudio(false)
} }
// StopMicrophoneAndRemoveTracks is a global helper to stop microphone subprocess and remove WebRTC tracks // StopMicrophone is a global helper to stop microphone subprocess
func StopMicrophoneAndRemoveTracks() error { func StopMicrophone() error {
initAudioControlService() return ensureAudioControlService().StopMicrophone()
return audioControlService.StopMicrophone()
} }
// StartMicrophoneAndAddTracks is a global helper to start microphone subprocess and add WebRTC tracks // StartMicrophone is a global helper to start microphone subprocess
func StartMicrophoneAndAddTracks() error { func StartMicrophone() error {
initAudioControlService() return ensureAudioControlService().StartMicrophone()
return audioControlService.StartMicrophone()
} }
// IsAudioOutputActive is a global helper to check if audio output subprocess is running // IsAudioOutputActive is a global helper to check if audio output subprocess is running
func IsAudioOutputActive() bool { func IsAudioOutputActive() bool {
initAudioControlService() return ensureAudioControlService().IsAudioOutputActive()
return audioControlService.IsAudioOutputActive()
} }
// IsMicrophoneActive is a global helper to check if microphone subprocess is running // IsMicrophoneActive is a global helper to check if microphone subprocess is running
func IsMicrophoneActive() bool { func IsMicrophoneActive() bool {
initAudioControlService() return ensureAudioControlService().IsMicrophoneActive()
return audioControlService.IsMicrophoneActive()
} }
// ResetMicrophone is a global helper to reset the microphone // ResetMicrophone is a global helper to reset the microphone
func ResetMicrophone() error { func ResetMicrophone() error {
initAudioControlService() return ensureAudioControlService().ResetMicrophone()
return audioControlService.ResetMicrophone()
} }
// GetCurrentSessionAudioTrack returns the current session's audio track for audio relay // GetCurrentSessionAudioTrack returns the current session's audio track for audio relay
@ -118,20 +112,20 @@ func ReplaceCurrentSessionAudioTrack(newTrack *webrtc.TrackLocalStaticSample) er
// SetAudioQuality is a global helper to set audio output quality // SetAudioQuality is a global helper to set audio output quality
func SetAudioQuality(quality audio.AudioQuality) error { func SetAudioQuality(quality audio.AudioQuality) error {
initAudioControlService() ensureAudioControlService()
audioControlService.SetAudioQuality(quality) audioControlService.SetAudioQuality(quality)
return nil return nil
} }
// GetAudioQualityPresets is a global helper to get available audio quality presets // GetAudioQualityPresets is a global helper to get available audio quality presets
func GetAudioQualityPresets() map[audio.AudioQuality]audio.AudioConfig { func GetAudioQualityPresets() map[audio.AudioQuality]audio.AudioConfig {
initAudioControlService() ensureAudioControlService()
return audioControlService.GetAudioQualityPresets() return audioControlService.GetAudioQualityPresets()
} }
// GetCurrentAudioQuality is a global helper to get current audio quality configuration // GetCurrentAudioQuality is a global helper to get current audio quality configuration
func GetCurrentAudioQuality() audio.AudioConfig { func GetCurrentAudioQuality() audio.AudioConfig {
initAudioControlService() ensureAudioControlService()
return audioControlService.GetCurrentAudioQuality() return audioControlService.GetCurrentAudioQuality()
} }
@ -148,9 +142,9 @@ func handleAudioMute(c *gin.Context) {
var err error var err error
if req.Muted { if req.Muted {
err = StopAudioOutputAndRemoveTracks() err = MuteAudioOutput()
} else { } else {
err = StartAudioOutputAndAddTracks() err = UnmuteAudioOutput()
} }
if err != nil { if err != nil {
@ -166,7 +160,7 @@ func handleAudioMute(c *gin.Context) {
// handleMicrophoneStart handles POST /microphone/start requests // handleMicrophoneStart handles POST /microphone/start requests
func handleMicrophoneStart(c *gin.Context) { func handleMicrophoneStart(c *gin.Context) {
err := StartMicrophoneAndAddTracks() err := StartMicrophone()
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
@ -177,7 +171,7 @@ func handleMicrophoneStart(c *gin.Context) {
// handleMicrophoneStop handles POST /microphone/stop requests // handleMicrophoneStop handles POST /microphone/stop requests
func handleMicrophoneStop(c *gin.Context) { func handleMicrophoneStop(c *gin.Context) {
err := StopMicrophoneAndRemoveTracks() err := StopMicrophone()
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return return
@ -199,9 +193,9 @@ func handleMicrophoneMute(c *gin.Context) {
var err error var err error
if req.Muted { if req.Muted {
err = StopMicrophoneAndRemoveTracks() err = StopMicrophone()
} else { } else {
err = StartMicrophoneAndAddTracks() err = StartMicrophone()
} }
if err != nil { if err != nil {
@ -225,19 +219,19 @@ func handleMicrophoneReset(c *gin.Context) {
// handleSubscribeAudioEvents handles WebSocket audio event subscription // handleSubscribeAudioEvents handles WebSocket audio event subscription
func handleSubscribeAudioEvents(connectionID string, wsCon *websocket.Conn, runCtx context.Context, l *zerolog.Logger) { func handleSubscribeAudioEvents(connectionID string, wsCon *websocket.Conn, runCtx context.Context, l *zerolog.Logger) {
initAudioControlService() ensureAudioControlService()
audioControlService.SubscribeToAudioEvents(connectionID, wsCon, runCtx, l) audioControlService.SubscribeToAudioEvents(connectionID, wsCon, runCtx, l)
} }
// handleUnsubscribeAudioEvents handles WebSocket audio event unsubscription // handleUnsubscribeAudioEvents handles WebSocket audio event unsubscription
func handleUnsubscribeAudioEvents(connectionID string, l *zerolog.Logger) { func handleUnsubscribeAudioEvents(connectionID string, l *zerolog.Logger) {
initAudioControlService() ensureAudioControlService()
audioControlService.UnsubscribeFromAudioEvents(connectionID, l) audioControlService.UnsubscribeFromAudioEvents(connectionID, l)
} }
// handleAudioStatus handles GET requests for audio status // handleAudioStatus handles GET requests for audio status
func handleAudioStatus(c *gin.Context) { func handleAudioStatus(c *gin.Context) {
initAudioControlService() ensureAudioControlService()
status := audioControlService.GetAudioStatus() status := audioControlService.GetAudioStatus()
c.JSON(200, status) c.JSON(200, status)

View File

@ -11,7 +11,7 @@ import (
func main() { func main() {
versionPtr := flag.Bool("version", false, "print version and exit") versionPtr := flag.Bool("version", false, "print version and exit")
versionJsonPtr := flag.Bool("version-json", false, "print version as json and exit") versionJsonPtr := flag.Bool("version-json", false, "print version as json and exit")
audioServerPtr := flag.Bool("audio-output-server", false, "Run as audio server subprocess") audioOutputServerPtr := flag.Bool("audio-output-server", false, "Run as audio server subprocess")
audioInputServerPtr := flag.Bool("audio-input-server", false, "Run as audio input server subprocess") audioInputServerPtr := flag.Bool("audio-input-server", false, "Run as audio input server subprocess")
flag.Parse() flag.Parse()
@ -26,5 +26,5 @@ func main() {
return return
} }
kvm.Main(*audioServerPtr, *audioInputServerPtr) kvm.Main(*audioOutputServerPtr, *audioInputServerPtr)
} }

View File

@ -4,36 +4,35 @@ import (
"sync" "sync"
) )
var audioMuteState struct { // AudioState holds all audio-related state with a single mutex
muted bool type AudioState struct {
mu sync.RWMutex mu sync.RWMutex
audioMuted bool
microphoneMuted bool
} }
var microphoneMuteState struct { var globalAudioState = &AudioState{}
muted bool
mu sync.RWMutex
}
func SetAudioMuted(muted bool) { func SetAudioMuted(muted bool) {
audioMuteState.mu.Lock() globalAudioState.mu.Lock()
defer audioMuteState.mu.Unlock() defer globalAudioState.mu.Unlock()
audioMuteState.muted = muted globalAudioState.audioMuted = muted
} }
func IsAudioMuted() bool { func IsAudioMuted() bool {
audioMuteState.mu.RLock() globalAudioState.mu.RLock()
defer audioMuteState.mu.RUnlock() defer globalAudioState.mu.RUnlock()
return audioMuteState.muted return globalAudioState.audioMuted
} }
func SetMicrophoneMuted(muted bool) { func SetMicrophoneMuted(muted bool) {
microphoneMuteState.mu.Lock() globalAudioState.mu.Lock()
defer microphoneMuteState.mu.Unlock() defer globalAudioState.mu.Unlock()
microphoneMuteState.muted = muted globalAudioState.microphoneMuted = muted
} }
func IsMicrophoneMuted() bool { func IsMicrophoneMuted() bool {
microphoneMuteState.mu.RLock() globalAudioState.mu.RLock()
defer microphoneMuteState.mu.RUnlock() defer globalAudioState.mu.RUnlock()
return microphoneMuteState.muted return globalAudioState.microphoneMuted
} }