add VP8/VP9 limits

This commit is contained in:
aler9
2023-08-06 20:54:04 +02:00
committed by Alessandro Ros
parent 155feeb4e5
commit e6e9e84909
4 changed files with 25 additions and 15 deletions

2
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/bluenviron/gortsplib/v3
go 1.18 go 1.18
require ( require (
github.com/bluenviron/mediacommon v0.7.1-0.20230806181841-a2766dec314f github.com/bluenviron/mediacommon v0.7.1-0.20230806185229-f060a1e5295b
github.com/google/uuid v1.3.0 github.com/google/uuid v1.3.0
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.12.0 h1:BiefTgVEyPgEB8nT6J+Sys/uxE4H/a04SW/aedpOpPc= github.com/asticode/go-astits v1.12.0 h1:BiefTgVEyPgEB8nT6J+Sys/uxE4H/a04SW/aedpOpPc=
github.com/asticode/go-astits v1.12.0/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI= github.com/asticode/go-astits v1.12.0/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI=
github.com/bluenviron/mediacommon v0.7.1-0.20230806181841-a2766dec314f h1:hVo5b6WSVT0+p43GpYv0e4cLtmpkhJp3kD6Ef83jzUM= github.com/bluenviron/mediacommon v0.7.1-0.20230806185229-f060a1e5295b h1:53WQf0Kam0/Rj4bnTWhpo6n6cCrRE84tjf9xrwq8mWo=
github.com/bluenviron/mediacommon v0.7.1-0.20230806181841-a2766dec314f/go.mod h1:LR4w8cpvzo2ZcmBwXcentvBj7ZlyF9g9xP4dDbt8uJw= github.com/bluenviron/mediacommon v0.7.1-0.20230806185229-f060a1e5295b/go.mod h1:LR4w8cpvzo2ZcmBwXcentvBj7ZlyF9g9xP4dDbt8uJw=
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,6 +5,7 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/bluenviron/mediacommon/pkg/codecs/vp8"
"github.com/pion/rtp" "github.com/pion/rtp"
"github.com/pion/rtp/codecs" "github.com/pion/rtp/codecs"
@@ -35,6 +36,7 @@ func joinFragments(fragments [][]byte, size int) []byte {
type Decoder struct { type Decoder struct {
timeDecoder *rtptime.Decoder timeDecoder *rtptime.Decoder
firstPacketReceived bool firstPacketReceived bool
fragmentsSize int
fragments [][]byte fragments [][]byte
} }
@@ -65,6 +67,7 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, time.Duration, error) {
d.firstPacketReceived = true d.firstPacketReceived = true
if !pkt.Marker { if !pkt.Marker {
d.fragmentsSize = len(vpkt.Payload)
d.fragments = append(d.fragments, vpkt.Payload) d.fragments = append(d.fragments, vpkt.Payload)
return nil, 0, ErrMorePacketsNeeded return nil, 0, ErrMorePacketsNeeded
} }
@@ -79,18 +82,20 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, time.Duration, error) {
return nil, 0, fmt.Errorf("received a non-starting fragment") return nil, 0, fmt.Errorf("received a non-starting fragment")
} }
d.fragmentsSize += len(vpkt.Payload)
if d.fragmentsSize > vp8.MaxFrameSize {
d.fragments = d.fragments[:0] // discard pending fragments
return nil, 0, fmt.Errorf("frame size (%d) is too big, maximum is %d", d.fragmentsSize, vp8.MaxFrameSize)
}
d.fragments = append(d.fragments, vpkt.Payload) d.fragments = append(d.fragments, vpkt.Payload)
if !pkt.Marker { if !pkt.Marker {
return nil, 0, ErrMorePacketsNeeded return nil, 0, ErrMorePacketsNeeded
} }
n := 0 frame = joinFragments(d.fragments, d.fragmentsSize)
for _, frag := range d.fragments {
n += len(frag)
}
frame = joinFragments(d.fragments, n)
d.fragments = d.fragments[:0] d.fragments = d.fragments[:0]
} }

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/bluenviron/mediacommon/pkg/codecs/vp9"
"github.com/pion/rtp" "github.com/pion/rtp"
"github.com/pion/rtp/codecs" "github.com/pion/rtp/codecs"
@@ -35,6 +36,7 @@ func joinFragments(fragments [][]byte, size int) []byte {
type Decoder struct { type Decoder struct {
timeDecoder *rtptime.Decoder timeDecoder *rtptime.Decoder
firstPacketReceived bool firstPacketReceived bool
fragmentsSize int
fragments [][]byte fragments [][]byte
} }
@@ -60,6 +62,7 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, time.Duration, error) {
d.firstPacketReceived = true d.firstPacketReceived = true
if !vpkt.E { if !vpkt.E {
d.fragmentsSize = len(vpkt.Payload)
d.fragments = append(d.fragments, vpkt.Payload) d.fragments = append(d.fragments, vpkt.Payload)
return nil, 0, ErrMorePacketsNeeded return nil, 0, ErrMorePacketsNeeded
} }
@@ -74,18 +77,20 @@ func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, time.Duration, error) {
return nil, 0, fmt.Errorf("received a non-starting fragment") return nil, 0, fmt.Errorf("received a non-starting fragment")
} }
d.fragmentsSize += len(vpkt.Payload)
if d.fragmentsSize > vp9.MaxFrameSize {
d.fragments = d.fragments[:0] // discard pending fragments
return nil, 0, fmt.Errorf("frame size (%d) is too big, maximum is %d", d.fragmentsSize, vp9.MaxFrameSize)
}
d.fragments = append(d.fragments, vpkt.Payload) d.fragments = append(d.fragments, vpkt.Payload)
if !vpkt.E { if !vpkt.E {
return nil, 0, ErrMorePacketsNeeded return nil, 0, ErrMorePacketsNeeded
} }
n := 0 frame = joinFragments(d.fragments, d.fragmentsSize)
for _, frag := range d.fragments {
n += len(frag)
}
frame = joinFragments(d.fragments, n)
d.fragments = d.fragments[:0] d.fragments = d.fragments[:0]
} }