From 597f9d1e4b025e2162ecbd64ccc4a47ec87b4867 Mon Sep 17 00:00:00 2001 From: q191201771 <191201771@qq.com> Date: Mon, 17 Apr 2023 20:18:38 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20rtmp:=20=E8=A7=A3=E6=9E=90Amf0TypeMarke?= =?UTF-8?q?rUndefined=E5=92=8CAmf0TypeMarkerUnsupported?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/rtmp/amf0.go | 24 +++++++++++++++++++++--- pkg/rtmp/amf0_test.go | 13 +++++++++++++ pkg/rtmp/stream.go | 1 - 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/pkg/rtmp/amf0.go b/pkg/rtmp/amf0.go index a4cfe64..34434b5 100644 --- a/pkg/rtmp/amf0.go +++ b/pkg/rtmp/amf0.go @@ -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) } diff --git a/pkg/rtmp/amf0_test.go b/pkg/rtmp/amf0_test.go index 98318da..2bb712c 100644 --- a/pkg/rtmp/amf0_test.go +++ b/pkg/rtmp/amf0_test.go @@ -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{ diff --git a/pkg/rtmp/stream.go b/pkg/rtmp/stream.go index 9c4f7c4..f38c319 100644 --- a/pkg/rtmp/stream.go +++ b/pkg/rtmp/stream.go @@ -11,7 +11,6 @@ package rtmp import ( "encoding/hex" "fmt" - "github.com/q191201771/naza/pkg/nazabytes" "github.com/q191201771/lal/pkg/base"