Fix oto v3 audio player integration

This commit is contained in:
Stu Leak 2026-01-06 21:46:01 -05:00
parent 68738cf1a5
commit 0af9d7790e

View File

@ -146,7 +146,6 @@ func (p *UnifiedPlayer) Load(path string, offset time.Duration) error {
// Initialize audio context for playback
sampleRate := 48000
channels := 2
bytesPerSample := 2 // 16-bit = 2 bytes
ctx, ready, err := oto.NewContext(&oto.NewContextOptions{
SampleRate: sampleRate,
@ -612,47 +611,26 @@ func (p *UnifiedPlayer) startVideoProcess() error {
// readAudioStream reads and processes audio from the audio pipe
func (p *UnifiedPlayer) readAudioStream() {
buffer := make([]byte, 4096) // 85ms chunks
if p.audioContext == nil || p.audioPipeReader == nil {
return
}
p.mu.Lock()
if p.audioPlayer == nil {
p.audioPlayer = p.audioContext.NewPlayer(p.audioPipeReader)
p.audioPlayer.Play()
}
p.mu.Unlock()
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-p.ctx.Done():
logging.Debug(logging.CatPlayer, "Audio reading goroutine stopped")
return
default:
// Read from audio pipe
n, err := p.audioPipeReader.Read(buffer)
if err != nil && err.Error() != "EOF" {
logging.Error(logging.CatPlayer, "Audio read error: %v", err)
continue
}
if n == 0 {
continue
}
// Initialize audio player if needed
if p.audioPlayer == nil && p.audioContext != nil {
player, err := p.audioContext.NewPlayer(p.audioPipeReader)
if err != nil {
logging.Error(logging.CatPlayer, "Failed to create audio player: %v", err)
return
}
p.audioPlayer = player
}
// Write audio data to player buffer
if p.audioPlayer != nil {
p.audioPlayer.Write(buffer[:n])
}
// Buffer for sync monitoring (keep small to avoid memory issues)
if len(p.audioBuffer) > 32768 { // Max 1 second at 48kHz
p.audioBuffer = p.audioBuffer[len(p.audioBuffer)-16384:] // Keep half
}
// Simple audio sync timing
case <-ticker.C:
p.updateAVSync()
}
}