add MPEG4Video.SafeParams(), MPEG4Video.SafeSetParams() (#395)

This commit is contained in:
Alessandro Ros
2023-09-01 18:36:21 +02:00
committed by GitHub
parent 8f18a0d83b
commit 6c413c03f5
7 changed files with 45 additions and 10 deletions

2
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/bluenviron/gortsplib/v4
go 1.19 go 1.19
require ( require (
github.com/bluenviron/mediacommon v1.0.1 github.com/bluenviron/mediacommon v1.0.2-0.20230901160448-b7b4863d6096
github.com/google/uuid v1.3.1 github.com/google/uuid v1.3.1
github.com/pion/rtcp v1.2.10 github.com/pion/rtcp v1.2.10
github.com/pion/rtp v1.8.1 github.com/pion/rtp v1.8.1

4
go.sum
View File

@@ -2,8 +2,8 @@ github.com/asticode/go-astikit v0.30.0 h1:DkBkRQRIxYcknlaU7W7ksNfn4gMFsB0tqMJflx
github.com/asticode/go-astikit v0.30.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0= github.com/asticode/go-astikit v0.30.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0=
github.com/asticode/go-astits v1.13.0 h1:XOgkaadfZODnyZRR5Y0/DWkA9vrkLLPLeeOvDwfKZ1c= github.com/asticode/go-astits v1.13.0 h1:XOgkaadfZODnyZRR5Y0/DWkA9vrkLLPLeeOvDwfKZ1c=
github.com/asticode/go-astits v1.13.0/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI= github.com/asticode/go-astits v1.13.0/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI=
github.com/bluenviron/mediacommon v1.0.1 h1:+Wiy0OmNxnIqJQ9MUcvldbTtJRmdjoxeyeSdDkSCExI= github.com/bluenviron/mediacommon v1.0.2-0.20230901160448-b7b4863d6096 h1:quTSEtdDapDQilHCiVeV4Un7NcWby4hVIiJAjqf+XXk=
github.com/bluenviron/mediacommon v1.0.1/go.mod h1:/vlOVSebDwzdRtQONOKLua0fOSJg1tUDHpP+h9a0uqM= github.com/bluenviron/mediacommon v1.0.2-0.20230901160448-b7b4863d6096/go.mod h1:/vlOVSebDwzdRtQONOKLua0fOSJg1tUDHpP+h9a0uqM=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View File

@@ -5,7 +5,9 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4video"
"github.com/pion/rtp" "github.com/pion/rtp"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4video" "github.com/bluenviron/gortsplib/v4/pkg/format/rtpmpeg4video"
@@ -20,6 +22,8 @@ type MPEG4VideoES struct {
PayloadTyp uint8 PayloadTyp uint8
ProfileLevelID int ProfileLevelID int
Config []byte Config []byte
mutex sync.RWMutex
} }
func (f *MPEG4VideoES) unmarshal(ctx *unmarshalContext) error { func (f *MPEG4VideoES) unmarshal(ctx *unmarshalContext) error {
@@ -42,6 +46,11 @@ func (f *MPEG4VideoES) unmarshal(ctx *unmarshalContext) error {
if err != nil { if err != nil {
return fmt.Errorf("invalid config: %v", val) return fmt.Errorf("invalid config: %v", val)
} }
err = mpeg4video.IsValidConfig(f.Config)
if err != nil {
return fmt.Errorf("invalid config: %v", err)
}
} }
} }
@@ -72,7 +81,10 @@ func (f *MPEG4VideoES) RTPMap() string {
func (f *MPEG4VideoES) FMTP() map[string]string { func (f *MPEG4VideoES) FMTP() map[string]string {
fmtp := map[string]string{ fmtp := map[string]string{
"profile-level-id": strconv.FormatInt(int64(f.ProfileLevelID), 10), "profile-level-id": strconv.FormatInt(int64(f.ProfileLevelID), 10),
"config": strings.ToUpper(hex.EncodeToString(f.Config)), }
if f.Config != nil {
fmtp["config"] = strings.ToUpper(hex.EncodeToString(f.Config))
} }
return fmtp return fmtp
@@ -108,3 +120,17 @@ func (f *MPEG4VideoES) CreateEncoder() (*rtpmpeg4video.Encoder, error) {
return e, nil return e, nil
} }
// SafeSetParams sets the codec parameters.
func (f *MPEG4VideoES) SafeSetParams(config []byte) {
f.mutex.Lock()
defer f.mutex.Unlock()
f.Config = config
}
// SafeParams returns the codec parameters.
func (f *MPEG4VideoES) SafeParams() []byte {
f.mutex.RLock()
defer f.mutex.RUnlock()
return f.Config
}

View File

@@ -5,15 +5,13 @@ import (
"fmt" "fmt"
"github.com/pion/rtp" "github.com/pion/rtp"
"github.com/bluenviron/mediacommon/pkg/codecs/mpeg4video"
) )
// ErrMorePacketsNeeded is returned when more packets are needed. // ErrMorePacketsNeeded is returned when more packets are needed.
var ErrMorePacketsNeeded = errors.New("need more packets") var ErrMorePacketsNeeded = errors.New("need more packets")
const (
maxFrameSize = 1 * 1024 * 1024
)
func joinFragments(fragments [][]byte, size int) []byte { func joinFragments(fragments [][]byte, size int) []byte {
ret := make([]byte, size) ret := make([]byte, size)
n := 0 n := 0
@@ -49,9 +47,9 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, error) {
} }
} else { } else {
d.fragmentsSize += len(pkt.Payload) d.fragmentsSize += len(pkt.Payload)
if d.fragmentsSize > maxFrameSize { if d.fragmentsSize > mpeg4video.MaxFrameSize {
d.fragments = d.fragments[:0] // discard pending fragments d.fragments = d.fragments[:0] // discard pending fragments
return nil, fmt.Errorf("frame size (%d) is too big, maximum is %d", d.fragmentsSize, maxFrameSize) return nil, fmt.Errorf("frame size (%d) is too big, maximum is %d", d.fragmentsSize, mpeg4video.MaxFrameSize)
} }
d.fragments = append(d.fragments, pkt.Payload) d.fragments = append(d.fragments, pkt.Payload)

View File

@@ -0,0 +1,3 @@
go test fuzz v1
string(",")
string("0")

View File

@@ -0,0 +1,5 @@
go test fuzz v1
string("0")
string("\x00")
string("")
string("0\xa5")

View File

@@ -0,0 +1,3 @@
go test fuzz v1
string("0")
string("")