diff --git a/codec/h264/lex.go b/codec/h264/lex.go index 1f169afe..3d9fb8a4 100644 --- a/codec/h264/lex.go +++ b/codec/h264/lex.go @@ -31,7 +31,6 @@ LICENSE package h264 import ( - "fmt" "io" "time" @@ -68,18 +67,25 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error { copy(buf, h264Prefix[:]) writeOut := false +outer: for { var b byte var err error buf, b, err = c.ScanUntil(buf, 0x00) if err != nil { - return fmt.Errorf("can't scan until: %w", err) + if err != io.EOF { + return err + } + break } for n := 1; b == 0x0 && n < 4; n++ { b, err = c.ReadByte() if err != nil { - return fmt.Errorf("can't read byte: %w", err) + if err != io.EOF { + return err + } + break outer } buf = append(buf, b) @@ -101,7 +107,10 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error { b, err = c.ReadByte() if err != nil { - return fmt.Errorf("can't read byte: %w", err) + if err != io.EOF { + return err + } + break outer } buf = append(buf, b) @@ -119,4 +128,8 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error { } } } + if len(buf) == len(h264Prefix) { + return io.EOF + } + return io.ErrUnexpectedEOF } diff --git a/codec/h265/lex.go b/codec/h265/lex.go index 6534e571..2fb41a9d 100644 --- a/codec/h265/lex.go +++ b/codec/h265/lex.go @@ -77,7 +77,13 @@ func (l *Lexer) Lex(dst io.Writer, src io.Reader, delay time.Duration) error { switch err { case nil: // Do nothing. default: - return fmt.Errorf("source read error: %w\n", err) + if err == io.EOF { + if l.buf.Len() == 0 { + return io.EOF + } + return io.ErrUnexpectedEOF + } + return err } // Get payload from RTP packet. @@ -179,7 +185,7 @@ func (l *Lexer) handleFragmentation(d []byte) { } } -// handlePACI will handl PACI packets +// handlePACI will handle PACI packets // // TODO: complete this func (l *Lexer) handlePACI(d []byte) { diff --git a/codec/mjpeg/lex.go b/codec/mjpeg/lex.go index d8b671b2..aea4ce9f 100644 --- a/codec/mjpeg/lex.go +++ b/codec/mjpeg/lex.go @@ -60,10 +60,10 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error { buf := make([]byte, 2, 4<<10) n, err := r.Read(buf) if n < 2 { - return nil + return io.ErrUnexpectedEOF } if err != nil { - return fmt.Errorf("can't read source: %w", err) + return err } if !bytes.Equal(buf, []byte{0xff, 0xd8}) { return fmt.Errorf("parser: not MJPEG frame start: %#v", buf) @@ -72,7 +72,10 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error { for { b, err := r.ReadByte() if err != nil { - return fmt.Errorf("can't read byte: %w", err) + if err == io.EOF { + return io.ErrUnexpectedEOF + } + return err } buf = append(buf, b) if last == 0xff && b == 0xd9 {