[fix] rtmp: 解析Amf0TypeMarkerUndefined和Amf0TypeMarkerUnsupported

pull/286/head
q191201771 2 years ago
parent 2cc8bf6ac3
commit 597f9d1e4b

@ -32,17 +32,17 @@ const (
Amf0TypeMarkerString = uint8(0x02)
Amf0TypeMarkerObject = uint8(0x03)
Amf0TypeMarkerNull = uint8(0x05)
Amf0TypeMarkerUndefined = uint8(0x06)
Amf0TypeMarkerEcmaArray = uint8(0x08)
Amf0TypeMarkerObjectEnd = uint8(0x09) // end for both Object and Array
Amf0TypeMarkerStrictArray = uint8(0x0a)
Amf0TypeMarkerLongString = uint8(0x0c)
Amf0TypeMarkerUnsupported = uint8(0x0d)
// 还没用到的类型
//Amf0TypeMarkerMovieclip = uint8(0x04)
//Amf0TypeMarkerUndefined = uint8(0x06)
//Amf0TypeMarkerReference = uint8(0x07)
//Amf0TypeMarkerData = uint8(0x0b)
//Amf0TypeMarkerUnsupported = uint8(0x0d)
//Amf0TypeMarkerRecordset = uint8(0x0e)
//Amf0TypeMarkerXmlDocument = uint8(0x0f)
//Amf0TypeMarkerTypedObject = uint8(0x10)
@ -271,6 +271,18 @@ func (amf0) ReadNull(b []byte) (int, error) {
return 1, nil
}
func (amf0) ReadUndefinedOrUnsupported(b []byte) (int, error) {
if len(b) < 1 {
return 0, nazaerrors.Wrap(base.ErrAmfTooShort)
}
return 1, nil
}
// ReadObject
//
// @return ObjectPairArray: ...
// @return int: 读取时从 b 消耗的字节大小
// @return error: ...
func (amf0) ReadObject(b []byte) (ObjectPairArray, int, error) {
if len(b) < 1 {
return nil, 0, nazaerrors.Wrap(base.ErrAmfTooShort)
@ -425,8 +437,14 @@ func (amf0) read(b []byte, index int, k string, ops ObjectPairArray) (ObjectPair
}
ops = append(ops, ObjectPair{k, v})
index += l
case Amf0TypeMarkerUndefined, Amf0TypeMarkerUnsupported:
l, err := Amf0.ReadUndefinedOrUnsupported(b[index:])
if err != nil {
return nil, 0, err
}
index += l
default:
Log.Errorf("unknown type. vt=%d, hex=%s", vt, hex.Dump(nazabytes.Prefix(b, 4096)))
Log.Errorf("unknown type. vt=%d, hex=%s, %s", vt, hex.Dump(nazabytes.Prefix(b, 4096)), hex.Dump(nazabytes.Prefix(b[index:], 4096)))
return ops, index, base.NewErrAmfInvalidType(vt)
}

@ -361,6 +361,19 @@ func TestAmf0Corner(t *testing.T) {
_ = Amf0.WriteObject(mw, objs)
}
func TestAmf0_ReadObject_case1(t *testing.T) {
// 易推流App iOS端
golden := "030008666c61736856657202001f464d4c452f332e302028636f6d70617469626c653b20464d53632f312e3029000c6361706162696c697469657300406de00000000000000b766964656f436f6465637300406000000000000000036170700200046c697665000673776655726c05000466706164010000077061676555726c05000d766964656f46756e6374696f6e003ff0000000000000000e6f626a656374456e636f64696e67060005746355726c02001a72746d703a2f2f3139322e3136382e33342e3135392f6c697665000b617564696f436f64656373004090000000000000000009"
b, _ := hex.DecodeString(golden)
opa, i, err := Amf0.ReadObject(b)
assert.Equal(t, nil, err)
assert.Equal(t, 231, i)
assert.Equal(t, "666c6173685665723a20464d4c452f332e302028636f6d70617469626c653b20464d53632f312e30290a6361706162696c69746965733a203233390a766964656f436f646563733a203132380a6170703a206c6976650a667061643a2066616c73650a766964656f46756e6374696f6e3a20310a746355726c3a2072746d703a2f2f3139322e3136382e33342e3135392f6c6976650a617564696f436f646563733a20313032340a", hex.EncodeToString([]byte(opa.DebugString())))
//nazalog.Debugf("%+v", opa)
}
func BenchmarkAmf0_ReadObject(b *testing.B) {
out := &bytes.Buffer{}
objs := []ObjectPair{

@ -11,7 +11,6 @@ package rtmp
import (
"encoding/hex"
"fmt"
"github.com/q191201771/naza/pkg/nazabytes"
"github.com/q191201771/lal/pkg/base"

Loading…
Cancel
Save