1
1
mirror of https://github.com/wader/fq.git synced 2024-12-26 06:51:35 +03:00
fq/pkg/bitio/limitreader.go
Mattias Wadman 7b27e506f1 mp4,bitio: Fix broken pssh decoding and add proper reader cloning to bitio
PSSH decoding probably got broken in some refactoring. system id is 16 bytes,
also scalar mapper should use cloned reader to not affect reader which in the case
broke reading system id bytes.

Add test with playread and widevine pssh boxes

Related to #282
2022-05-28 13:42:38 +02:00

36 lines
714 B
Go

package bitio
import (
"io"
)
// LimitReader is a bitio.Reader that reads a limited amount of bits from a bitio.Reader.
// Similar to bytes.LimitedReader.
type LimitReader struct {
r Reader
n int64
}
// NewLimitReader returns a new bitio.LimitReader.
func NewLimitReader(r Reader, n int64) *LimitReader { return &LimitReader{r, n} }
func (l *LimitReader) ReadBits(p []byte, nBits int64) (n int64, err error) {
if l.n <= 0 {
return 0, io.EOF
}
if nBits > l.n {
nBits = l.n
}
n, err = l.r.ReadBits(p, nBits)
l.n -= n
return n, err
}
func (l *LimitReader) CloneReader() (Reader, error) {
rc, err := CloneReader(l.r)
if err != nil {
return nil, err
}
return &LimitReader{r: rc, n: l.n}, nil
}