rtpaac, rtph264: add parameter PayloadMaxSize (#106)

This commit is contained in:
aler9
2022-03-20 11:33:37 +01:00
parent e7aca99c73
commit 47f88c63ba
2 changed files with 25 additions and 15 deletions

View File

@@ -10,7 +10,6 @@ import (
const ( const (
rtpVersion = 0x02 rtpVersion = 0x02
rtpPayloadMaxSize = 1460 // 1500 (mtu) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
) )
func randUint32() uint32 { func randUint32() uint32 {
@@ -36,6 +35,9 @@ type Encoder struct {
// initial timestamp of packets (optional). // initial timestamp of packets (optional).
InitialTimestamp *uint32 InitialTimestamp *uint32
// maximum size of packet payloads (optional).
PayloadMaxSize int
sequenceNumber uint16 sequenceNumber uint16
} }
@@ -53,6 +55,9 @@ func (e *Encoder) Init() {
v := randUint32() v := randUint32()
e.InitialTimestamp = &v e.InitialTimestamp = &v
} }
if e.PayloadMaxSize == 0 {
e.PayloadMaxSize = 1460 // 1500 (UDP MTU) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
}
e.sequenceNumber = *e.InitialSequenceNumber e.sequenceNumber = *e.InitialSequenceNumber
} }
@@ -70,7 +75,7 @@ func (e *Encoder) Encode(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, e
// split AUs into batches // split AUs into batches
for _, au := range aus { for _, au := range aus {
if e.lenAggregated(batch, au) <= rtpPayloadMaxSize { if e.lenAggregated(batch, au) <= e.PayloadMaxSize {
// add to existing batch // add to existing batch
batch = append(batch, au) batch = append(batch, au)
} else { } else {
@@ -102,7 +107,7 @@ func (e *Encoder) Encode(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, e
func (e *Encoder) writeBatch(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, error) { func (e *Encoder) writeBatch(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, error) {
if len(aus) == 1 { if len(aus) == 1 {
// the AU fits into a single RTP packet // the AU fits into a single RTP packet
if len(aus[0]) < rtpPayloadMaxSize { if len(aus[0]) < e.PayloadMaxSize {
return e.writeAggregated(aus, firstPTS) return e.writeAggregated(aus, firstPTS)
} }
@@ -114,8 +119,8 @@ func (e *Encoder) writeBatch(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packe
} }
func (e *Encoder) writeFragmented(au []byte, pts time.Duration) ([]*rtp.Packet, error) { func (e *Encoder) writeFragmented(au []byte, pts time.Duration) ([]*rtp.Packet, error) {
packetCount := len(au) / (rtpPayloadMaxSize - 4) packetCount := len(au) / (e.PayloadMaxSize - 4)
lastPacketSize := len(au) % (rtpPayloadMaxSize - 4) lastPacketSize := len(au) % (e.PayloadMaxSize - 4)
if lastPacketSize > 0 { if lastPacketSize > 0 {
packetCount++ packetCount++
} }
@@ -124,7 +129,7 @@ func (e *Encoder) writeFragmented(au []byte, pts time.Duration) ([]*rtp.Packet,
encPTS := e.encodeTimestamp(pts) encPTS := e.encodeTimestamp(pts)
for i := range ret { for i := range ret {
le := rtpPayloadMaxSize - 4 le := e.PayloadMaxSize - 4
if i == (packetCount - 1) { if i == (packetCount - 1) {
le = lastPacketSize le = lastPacketSize
} }

View File

@@ -10,7 +10,6 @@ import (
const ( const (
rtpVersion = 0x02 rtpVersion = 0x02
rtpPayloadMaxSize = 1460 // 1500 (mtu) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
rtpClockRate = 90000 // h264 always uses 90khz rtpClockRate = 90000 // h264 always uses 90khz
) )
@@ -34,6 +33,9 @@ type Encoder struct {
// initial timestamp of packets (optional). // initial timestamp of packets (optional).
InitialTimestamp *uint32 InitialTimestamp *uint32
// maximum size of packet payloads (optional).
PayloadMaxSize int
sequenceNumber uint16 sequenceNumber uint16
} }
@@ -51,6 +53,9 @@ func (e *Encoder) Init() {
v := randUint32() v := randUint32()
e.InitialTimestamp = &v e.InitialTimestamp = &v
} }
if e.PayloadMaxSize == 0 {
e.PayloadMaxSize = 1460 // 1500 (UDP MTU) - 20 (IP header) - 8 (UDP header) - 12 (RTP header)
}
e.sequenceNumber = *e.InitialSequenceNumber e.sequenceNumber = *e.InitialSequenceNumber
} }
@@ -66,7 +71,7 @@ func (e *Encoder) Encode(nalus [][]byte, pts time.Duration) ([]*rtp.Packet, erro
// split NALUs into batches // split NALUs into batches
for _, nalu := range nalus { for _, nalu := range nalus {
if e.lenAggregated(batch, nalu) <= rtpPayloadMaxSize { if e.lenAggregated(batch, nalu) <= e.PayloadMaxSize {
// add to existing batch // add to existing batch
batch = append(batch, nalu) batch = append(batch, nalu)
} else { } else {
@@ -98,7 +103,7 @@ func (e *Encoder) Encode(nalus [][]byte, pts time.Duration) ([]*rtp.Packet, erro
func (e *Encoder) writeBatch(nalus [][]byte, pts time.Duration, marker bool) ([]*rtp.Packet, error) { func (e *Encoder) writeBatch(nalus [][]byte, pts time.Duration, marker bool) ([]*rtp.Packet, error) {
if len(nalus) == 1 { if len(nalus) == 1 {
// the NALU fits into a single RTP packet // the NALU fits into a single RTP packet
if len(nalus[0]) < rtpPayloadMaxSize { if len(nalus[0]) < e.PayloadMaxSize {
return e.writeSingle(nalus[0], pts, marker) return e.writeSingle(nalus[0], pts, marker)
} }
@@ -130,8 +135,8 @@ func (e *Encoder) writeSingle(nalu []byte, pts time.Duration, marker bool) ([]*r
func (e *Encoder) writeFragmented(nalu []byte, pts time.Duration, marker bool) ([]*rtp.Packet, error) { func (e *Encoder) writeFragmented(nalu []byte, pts time.Duration, marker bool) ([]*rtp.Packet, error) {
// use only FU-A, not FU-B, since we always use non-interleaved mode // use only FU-A, not FU-B, since we always use non-interleaved mode
// (packetization-mode=1) // (packetization-mode=1)
packetCount := (len(nalu) - 1) / (rtpPayloadMaxSize - 2) packetCount := (len(nalu) - 1) / (e.PayloadMaxSize - 2)
lastPacketSize := (len(nalu) - 1) % (rtpPayloadMaxSize - 2) lastPacketSize := (len(nalu) - 1) % (e.PayloadMaxSize - 2)
if lastPacketSize > 0 { if lastPacketSize > 0 {
packetCount++ packetCount++
} }
@@ -151,7 +156,7 @@ func (e *Encoder) writeFragmented(nalu []byte, pts time.Duration, marker bool) (
start = 1 start = 1
} }
end := uint8(0) end := uint8(0)
le := rtpPayloadMaxSize - 2 le := e.PayloadMaxSize - 2
if i == (packetCount - 1) { if i == (packetCount - 1) {
end = 1 end = 1
le = lastPacketSize le = lastPacketSize