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
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
}

View File

@ -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) {

View File

@ -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 {