mirror of https://github.com/jetkvm/kvm.git
Improvement: automatically resume audio when the audio usb gadget is re-enabled from settings
This commit is contained in:
parent
2afe2ca539
commit
f24443e072
|
@ -3,6 +3,7 @@ package audio
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -168,15 +169,22 @@ func (r *AudioRelay) relayLoop() {
|
||||||
// forwardToWebRTC forwards a frame to the WebRTC audio track
|
// forwardToWebRTC forwards a frame to the WebRTC audio track
|
||||||
func (r *AudioRelay) forwardToWebRTC(frame []byte) error {
|
func (r *AudioRelay) forwardToWebRTC(frame []byte) error {
|
||||||
r.mutex.RLock()
|
r.mutex.RLock()
|
||||||
|
defer r.mutex.RUnlock()
|
||||||
|
|
||||||
audioTrack := r.audioTrack
|
audioTrack := r.audioTrack
|
||||||
config := r.config
|
config := r.config
|
||||||
muted := r.muted
|
muted := r.muted
|
||||||
r.mutex.RUnlock()
|
|
||||||
|
|
||||||
|
// Comprehensive nil check for audioTrack to prevent panic
|
||||||
if audioTrack == nil {
|
if audioTrack == nil {
|
||||||
return nil // No audio track available
|
return nil // No audio track available
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if interface contains nil pointer using reflection
|
||||||
|
if reflect.ValueOf(audioTrack).IsNil() {
|
||||||
|
return nil // Audio track interface contains nil pointer
|
||||||
|
}
|
||||||
|
|
||||||
// Prepare sample data
|
// Prepare sample data
|
||||||
var sampleData []byte
|
var sampleData []byte
|
||||||
if muted {
|
if muted {
|
||||||
|
@ -186,7 +194,7 @@ func (r *AudioRelay) forwardToWebRTC(frame []byte) error {
|
||||||
sampleData = frame
|
sampleData = frame
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write sample to WebRTC track
|
// Write sample to WebRTC track while holding the read lock
|
||||||
return audioTrack.WriteSample(media.Sample{
|
return audioTrack.WriteSample(media.Sample{
|
||||||
Data: sampleData,
|
Data: sampleData,
|
||||||
Duration: config.FrameSize,
|
Duration: config.FrameSize,
|
||||||
|
|
15
main.go
15
main.go
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
"github.com/gwatts/rootcerts"
|
"github.com/gwatts/rootcerts"
|
||||||
"github.com/jetkvm/kvm/internal/audio"
|
"github.com/jetkvm/kvm/internal/audio"
|
||||||
|
"github.com/pion/webrtc/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -46,9 +47,17 @@ func startAudioSubprocess() error {
|
||||||
func(pid int) {
|
func(pid int) {
|
||||||
logger.Info().Int("pid", pid).Msg("audio server process started")
|
logger.Info().Int("pid", pid).Msg("audio server process started")
|
||||||
|
|
||||||
// Start audio relay system for main process without a track initially
|
// Start audio relay system for main process
|
||||||
// The track will be updated when a WebRTC session is created
|
// If there's an active WebRTC session, use its audio track
|
||||||
if err := audio.StartAudioRelay(nil); err != nil {
|
var audioTrack *webrtc.TrackLocalStaticSample
|
||||||
|
if currentSession != nil && currentSession.AudioTrack != nil {
|
||||||
|
audioTrack = currentSession.AudioTrack
|
||||||
|
logger.Info().Msg("restarting audio relay with existing WebRTC audio track")
|
||||||
|
} else {
|
||||||
|
logger.Info().Msg("starting audio relay without WebRTC track (will be updated when session is created)")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := audio.StartAudioRelay(audioTrack); err != nil {
|
||||||
logger.Error().Err(err).Msg("failed to start audio relay")
|
logger.Error().Err(err).Msg("failed to start audio relay")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue