AlexxIT/go2rtc · error

ErrRead

ErrRead

Error message

amf: read error

What it means

amf.ErrRead ("amf: read error", pkg/flv/amf/amf.go:25) is the sentinel returned by the AMF0 reader (ReadItem, ReadByte, ReadNumber, ReadString, ReadEcmaArray) when a read would go past the end of the buffer (a.pos >= len(a.buf)) or an item cannot be parsed. It indicates truncated or malformed AMF data, typically a corrupt FLV tag payload.

Solutions

  1. Check for truncation upstream: ensure the FLV tag payload was fully read before AMF decoding.
  2. Validate the source stream/file integrity; resync or reconnect if the producer truncated the tag.
  3. Wrap decodes with errors.Is(err, amf.ErrRead) and skip the malformed tag instead of aborting the whole stream.
  4. Log the raw buffer and type marker; an unimplemented AMF0 type requires extending the reader.
  5. If reading a file, verify it isn't a partial download/corrupt recording.

Example fix

// before
items, err := amf.NewReader(tag.Payload).ReadItems()
if err != nil {
    return err // whole pipeline dies on one bad tag
}
// after
items, err := amf.NewReader(tag.Payload).ReadItems()
if errors.Is(err, amf.ErrRead) {
    log.Warn("skipping malformed AMF tag")
    return nil
}
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the payload is long enough to hold at least one AMF item
if len(tag.Payload) < 2 {
    return nil // nothing decodable
}

Type guard

func isAMFReadError(err error) bool {
    return errors.Is(err, amf.ErrRead)
}

Try / catch

items, err := amf.NewReader(payload).ReadItems()
if errors.Is(err, amf.ErrRead) {
    log.Warn("malformed AMF data; skipping tag")
    return nil
}

Prevention

When it happens

Trigger: Any AMF decode call (ReadItems/ReadItem/ReadByte/ReadNumber/ReadString/ReadEcmaArray) on a buffer shorter than the encoded item requires, or a malformed item (e.g. ReadItem at amf.go:73 hitting an unknown/invalid type marker and falling through to return ErrRead).

Common situations: Truncated FLV tag due to a dropped connection mid-tag; corrupt recording file; parsing an FLV tag payload that isn't AMF (e.g. feeding metadata into the wrong decoder); producer emitting a non-standard AMF type marker the reader doesn't implement.

Related errors


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/1c54e7c4308d5898. Report an issue: GitHub.

Appendix: source

Thrown at pkg/flv/amf/amf.go:25

)

const (
	TypeNumber byte = iota
	TypeBoolean
	TypeString
	TypeObject
	TypeNull      = 5
	TypeEcmaArray = 8
	TypeObjectEnd = 9
)

// AMF spec: http://download.macromedia.com/pub/labs/amf/amf0_spec_121207.pdf
type AMF struct {
	buf []byte
	pos int
}

var ErrRead = errors.New("amf: read error")

func NewReader(b []byte) *AMF {
	return &AMF{buf: b}
}

func (a *AMF) ReadItems() ([]any, error) {
	var items []any
	for a.pos < len(a.buf) {
		v, err := a.ReadItem()
		if err != nil {
			return nil, err
		}
		items = append(items, v)
	}
	return items, nil
}

func (a *AMF) ReadItem() (any, error) {

View on GitHub (pinned to c245815e75)