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

View File

@ -11,7 +11,7 @@ import (
func main() {
versionPtr := flag.Bool("version", false, "print version 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")
flag.Parse()
@ -26,5 +26,5 @@ func main() {
return
}
kvm.Main(*audioServerPtr, *audioInputServerPtr)
kvm.Main(*audioOutputServerPtr, *audioInputServerPtr)
}

View File

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