Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions internal/mux/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ func (p *Player) Seek(offset int64, whence int) (int64, error) {
}

func (p *playerImpl) Seek(offset int64, whence int) (int64, error) {
// Check if the source implements io.Seeker.
s, ok := p.src.(io.Seeker)
if !ok {
return 0, errors.New("mux: the source must implement io.Seeker")
}

defer p.mux.signal()

p.m.Lock()
Expand All @@ -387,11 +393,6 @@ func (p *playerImpl) Seek(offset int64, whence int) (int64, error) {
p.reportSourceErrorIfDrainedImpl()
}

// Check if the source implements io.Seeker.
s, ok := p.src.(io.Seeker)
if !ok {
return 0, errors.New("mux: the source must implement io.Seeker")
}
return s.Seek(offset, whence)
}

Expand Down
32 changes: 32 additions & 0 deletions internal/mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,3 +1003,35 @@ func TestPartialSampleAtEOFStopsPlayer(t *testing.T) {
t.Errorf("IsRegistered after EOF: got %v; want false", got)
}
}

func TestSeekNonSeekerPreservesBuffer(t *testing.T) {
for _, playing := range []bool{false, true} {
t.Run(fmt.Sprintf("playing=%t", playing), func(t *testing.T) {
src := []byte{0, 64, 128, 192, 255}
m := mux.New(48000, 1, mux.FormatUnsignedInt8)
p := newPlayer(t, m, bytes.NewBuffer(src))
p.Play()
waitForBufferedSize(t, p, len(src))
if !playing {
p.PauseAndStopReading()
}
if _, err := p.Seek(0, io.SeekStart); err == nil {
t.Fatal("Seek on a non-seekable source succeeded")
}
if got := p.BufferedSize(); got != len(src) {
t.Fatalf("BufferedSize after failed Seek: got %d; want %d", got, len(src))
}
if got := p.IsPlaying(); got != playing {
t.Fatalf("IsPlaying: got %t; want %t", got, playing)
}
p.Play()
buf := make([]float32, len(src))
m.ReadFloat32s(buf)
for i, got := range buf {
if want := float32(src[i])/128 - 1; got != want {
t.Errorf("sample %d: got %v; want %v", i, got, want)
}
}
})
}
}
Loading