codec/h264&h265/lex.go: returning errors even if io.EOF, and not bothering to return what's currently in buffer if error

This commit is contained in:
Saxon 2020-01-17 16:02:51 +10:30
parent 3a743ea917
commit a26777b697
2 changed files with 5 additions and 21 deletions

View File

@ -31,6 +31,7 @@ LICENSE
package h264 package h264
import ( import (
"fmt"
"io" "io"
"time" "time"
@ -66,25 +67,19 @@ func Lex(dst io.Writer, src io.Reader, delay time.Duration) error {
buf := make([]byte, len(h264Prefix), bufSize) buf := make([]byte, len(h264Prefix), bufSize)
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 {
if err != io.EOF { return fmt.Errorf("can't scan until: %w", err)
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 {
if err != io.EOF { return fmt.Errorf("can't read byte: %w", err)
return err
}
break outer
} }
buf = append(buf, b) buf = append(buf, b)
@ -106,10 +101,7 @@ outer:
b, err = c.ReadByte() b, err = c.ReadByte()
if err != nil { if err != nil {
if err != io.EOF { return fmt.Errorf("can't read byte: %w", err)
return err
}
break outer
} }
buf = append(buf, b) buf = append(buf, b)
@ -127,10 +119,4 @@ outer:
} }
} }
} }
if len(buf) == len(h264Prefix) {
return nil
}
<-tick
_, err := dst.Write(buf)
return err
} }

View File

@ -76,8 +76,6 @@ func (l *Lexer) Lex(dst io.Writer, src io.Reader, delay time.Duration) error {
n, err := src.Read(buf) n, err := src.Read(buf)
switch err { switch err {
case nil: // Do nothing. case nil: // Do nothing.
case io.EOF:
return nil
default: default:
return fmt.Errorf("source read error: %w\n", err) return fmt.Errorf("source read error: %w\n", err)
} }