audio: Bug fix: there could be chunks after 'data' chunk

This commit is contained in:
Hajime Hoshi 2016-09-27 00:37:40 +09:00
parent e22561c292
commit ffbe450c3f
2 changed files with 16 additions and 5 deletions

View File

@ -70,8 +70,7 @@ func (p *players) Read(b []byte) (int, error) {
closed := []*Player{}
l := len(b)
for p := range p.players {
err := p.readToBuffer(l)
if err == io.EOF {
if err := p.readToBuffer(l); err == io.EOF {
closed = append(closed, p)
} else if err != nil {
return 0, err

View File

@ -28,19 +28,30 @@ type Stream struct {
src audio.ReadSeekCloser
headerSize int64
dataSize int64
remaining int64
}
// Read is implementation of io.Reader's Read.
func (s *Stream) Read(p []byte) (int, error) {
return s.src.Read(p)
if s.remaining <= 0 {
return 0, io.EOF
}
if s.remaining < int64(len(p)) {
p = p[0:s.remaining]
}
n, err := s.src.Read(p)
s.remaining -= int64(n)
return n, err
}
// Seek is implementation of io.Seeker's Seek.
func (s *Stream) Seek(offset int64, whence int) (int64, error) {
if whence == 0 {
if whence == io.SeekStart {
offset += s.headerSize
}
return s.src.Seek(offset, whence)
n, err := s.src.Seek(offset, whence)
s.remaining = s.dataSize - (n - s.headerSize)
return n, err
}
// Read is implementation of io.Closer's Close.
@ -139,6 +150,7 @@ chunks:
src: src,
headerSize: headerSize,
dataSize: dataSize,
remaining: dataSize,
}
return s, nil
}