mirror of https://github.com/jetkvm/kvm.git
73 lines
2.0 KiB
Go
73 lines
2.0 KiB
Go
package audio
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"sync/atomic"
|
|
"unsafe"
|
|
)
|
|
|
|
var (
|
|
// Global audio output supervisor instance
|
|
globalOutputSupervisor unsafe.Pointer // *AudioServerSupervisor
|
|
)
|
|
|
|
// isAudioServerProcess detects if we're running as the audio server subprocess
|
|
func isAudioServerProcess() bool {
|
|
for _, arg := range os.Args {
|
|
if strings.Contains(arg, "--audio-server") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// StartAudioStreaming launches the audio stream.
|
|
// In audio server subprocess: uses CGO-based audio streaming
|
|
// In main process: this should not be called (use StartAudioRelay instead)
|
|
func StartAudioStreaming(send func([]byte)) error {
|
|
if isAudioServerProcess() {
|
|
// Audio server subprocess: use CGO audio processing
|
|
return StartAudioOutputStreaming(send)
|
|
} else {
|
|
// Main process: should use relay system instead
|
|
// This is kept for backward compatibility but not recommended
|
|
return StartAudioOutputStreaming(send)
|
|
}
|
|
}
|
|
|
|
// StopAudioStreaming stops the audio stream.
|
|
func StopAudioStreaming() {
|
|
if isAudioServerProcess() {
|
|
// Audio server subprocess: stop CGO audio processing
|
|
StopAudioOutputStreaming()
|
|
} else {
|
|
// Main process: stop relay if running
|
|
StopAudioRelay()
|
|
}
|
|
}
|
|
|
|
// StartNonBlockingAudioStreaming is an alias for backward compatibility
|
|
func StartNonBlockingAudioStreaming(send func([]byte)) error {
|
|
return StartAudioOutputStreaming(send)
|
|
}
|
|
|
|
// StopNonBlockingAudioStreaming is an alias for backward compatibility
|
|
func StopNonBlockingAudioStreaming() {
|
|
StopAudioOutputStreaming()
|
|
}
|
|
|
|
// SetAudioOutputSupervisor sets the global audio output supervisor
|
|
func SetAudioOutputSupervisor(supervisor *AudioServerSupervisor) {
|
|
atomic.StorePointer(&globalOutputSupervisor, unsafe.Pointer(supervisor))
|
|
}
|
|
|
|
// GetAudioOutputSupervisor returns the global audio output supervisor
|
|
func GetAudioOutputSupervisor() *AudioServerSupervisor {
|
|
ptr := atomic.LoadPointer(&globalOutputSupervisor)
|
|
if ptr == nil {
|
|
return nil
|
|
}
|
|
return (*AudioServerSupervisor)(ptr)
|
|
}
|