Skip to content

Commit c24fd1e

Browse files
committed
bug fix. This cost me multiple hours
1 parent ae3de97 commit c24fd1e

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

player/player.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ type AVPlayer struct {
2020
playing atomic.Bool
2121
paused atomic.Bool
2222
muted atomic.Bool
23-
closed atomic.Bool
2423
volume atomic.Value // float64, 0.0–1.0
2524

2625
playMu sync.Mutex
@@ -137,10 +136,6 @@ func (p *AVPlayer) Play(videoPath, pfpPath string) error {
137136
p.playMu.Lock()
138137
defer p.playMu.Unlock()
139138

140-
if p.closed.Load() {
141-
return nil
142-
}
143-
144139
p.playing.Store(true)
145140
p.paused.Store(false)
146141

@@ -275,11 +270,6 @@ func (p *AVPlayer) ClearGifs() {
275270
// Waits for the Play goroutine to finish before clearing terminal images,
276271
// preventing a race where frames render after cleanup.
277272
func (p *AVPlayer) Close() {
278-
// Prevent any queued Play() goroutines from starting a new session
279-
// after we release playMu. Without this, a pending Play() can grab
280-
// playMu before us and override playing=true, deadlocking Close().
281-
p.closed.Store(true)
282-
283273
// Signal for the session to stop playing
284274
p.Stop()
285275

player/session.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
_ "image/jpeg"
88
"math"
99
"os"
10+
"runtime"
1011
"sync"
1112
"time"
1213
"unsafe"
@@ -190,6 +191,21 @@ func (s *playSession) audioDecodeLoop() {
190191

191192
// demuxLoop reads packets and distributes them to audio/video channels
192193
func (s *playSession) demuxLoop(p *AVPlayer) {
194+
// - programmer: Magic ahhh line to prevent freezing bug on quick scrolling input.
195+
// Adding print statements here to debug caused the issue to disappear.
196+
// Therefore we don't know the issue, but we only know the solution.
197+
// Even opus does not know the issue. And bro's sorta the goat.
198+
// So I give up.
199+
// Like how this yields to the OS scheduler, I too, yield to this bug.
200+
//
201+
// - opus: For what it's worth, we DO know the issue — CGo FFmpeg ReadPacket
202+
// calls in a tight loop starve the Go scheduler, preventing videoRenderLoop
203+
// from being scheduled to respond to stopCh.
204+
//
205+
//
206+
// DO NOT TOUCH THIS
207+
runtime.Gosched()
208+
193209
defer close(s.videoPktCh)
194210

195211
for p.playing.Load() {

0 commit comments

Comments
 (0)