codec: corrected error handling in lexers

This commit is contained in:
Saxon 2020-01-20 15:03:49 +10:30
parent fdf393566a
commit f64c986efc
3 changed files with 31 additions and 9 deletions

View File

@ -31,7 +31,6 @@ LICENSE
package h264 package h264
import ( import (
"fmt"
"io" "io"
"time" "time"
@ -68,18 +67,25 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error {
copy(buf, h264Prefix[:]) copy(buf, h264Prefix[:])
writeOut := false writeOut := false
outer:
for { for {
var b byte var b byte
var err error var err error
buf, b, err = c.ScanUntil(buf, 0x00) buf, b, err = c.ScanUntil(buf, 0x00)
if err != nil { 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++ { for n := 1; b == 0x0 && n < 4; n++ {
b, err = c.ReadByte() b, err = c.ReadByte()
if err != nil { if err != nil {
return fmt.Errorf("can't read byte: %w", err) if err != io.EOF {
return err
}
break outer
} }
buf = append(buf, b) buf = append(buf, b)
@ -101,7 +107,10 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error {
b, err = c.ReadByte() b, err = c.ReadByte()
if err != nil { if err != nil {
return fmt.Errorf("can't read byte: %w", err) if err != io.EOF {
return err
}
break outer
} }
buf = append(buf, b) 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
} }

View File

@ -77,7 +77,13 @@ func (l *Lexer) Lex(dst io.Writer, src io.Reader, delay time.Duration) error {
switch err { switch err {
case nil: // Do nothing. case nil: // Do nothing.
default: 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. // 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 // TODO: complete this
func (l *Lexer) handlePACI(d []byte) { func (l *Lexer) handlePACI(d []byte) {

View File

@ -60,10 +60,10 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error {
buf := make([]byte, 2, 4<<10) buf := make([]byte, 2, 4<<10)
n, err := r.Read(buf) n, err := r.Read(buf)
if n < 2 { if n < 2 {
return nil return io.ErrUnexpectedEOF
} }
if err != nil { if err != nil {
return fmt.Errorf("can't read source: %w", err) return err
} }
if !bytes.Equal(buf, []byte{0xff, 0xd8}) { if !bytes.Equal(buf, []byte{0xff, 0xd8}) {
return fmt.Errorf("parser: not MJPEG frame start: %#v", buf) 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 { for {
b, err := r.ReadByte() b, err := r.ReadByte()
if err != nil { 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) buf = append(buf, b)
if last == 0xff && b == 0xd9 { if last == 0xff && b == 0xd9 {