diff --git a/pkg/rtpaac/encoder.go b/pkg/rtpaac/encoder.go index c8fffbec..e48d4497 100644 --- a/pkg/rtpaac/encoder.go +++ b/pkg/rtpaac/encoder.go @@ -9,8 +9,7 @@ import ( ) const ( - rtpVersion = 0x02 - rtpPayloadMaxSize = 1460 // 1500 (mtu) - 20 (IP header) - 8 (UDP header) - 12 (RTP header) + rtpVersion = 0x02 ) func randUint32() uint32 { @@ -36,6 +35,9 @@ type Encoder struct { // initial timestamp of packets (optional). InitialTimestamp *uint32 + // maximum size of packet payloads (optional). + PayloadMaxSize int + sequenceNumber uint16 } @@ -53,6 +55,9 @@ func (e *Encoder) Init() { v := randUint32() 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 } @@ -70,7 +75,7 @@ func (e *Encoder) Encode(aus [][]byte, firstPTS time.Duration) ([]*rtp.Packet, e // split AUs into batches for _, au := range aus { - if e.lenAggregated(batch, au) <= rtpPayloadMaxSize { + if e.lenAggregated(batch, au) <= e.PayloadMaxSize { // add to existing batch batch = append(batch, au) } 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) { if len(aus) == 1 { // the AU fits into a single RTP packet - if len(aus[0]) < rtpPayloadMaxSize { + if len(aus[0]) < e.PayloadMaxSize { 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) { - packetCount := len(au) / (rtpPayloadMaxSize - 4) - lastPacketSize := len(au) % (rtpPayloadMaxSize - 4) + packetCount := len(au) / (e.PayloadMaxSize - 4) + lastPacketSize := len(au) % (e.PayloadMaxSize - 4) if lastPacketSize > 0 { packetCount++ } @@ -124,7 +129,7 @@ func (e *Encoder) writeFragmented(au []byte, pts time.Duration) ([]*rtp.Packet, encPTS := e.encodeTimestamp(pts) for i := range ret { - le := rtpPayloadMaxSize - 4 + le := e.PayloadMaxSize - 4 if i == (packetCount - 1) { le = lastPacketSize } diff --git a/pkg/rtph264/encoder.go b/pkg/rtph264/encoder.go index a88299ba..3b4d9cf6 100644 --- a/pkg/rtph264/encoder.go +++ b/pkg/rtph264/encoder.go @@ -9,9 +9,8 @@ import ( ) const ( - rtpVersion = 0x02 - rtpPayloadMaxSize = 1460 // 1500 (mtu) - 20 (IP header) - 8 (UDP header) - 12 (RTP header) - rtpClockRate = 90000 // h264 always uses 90khz + rtpVersion = 0x02 + rtpClockRate = 90000 // h264 always uses 90khz ) func randUint32() uint32 { @@ -34,6 +33,9 @@ type Encoder struct { // initial timestamp of packets (optional). InitialTimestamp *uint32 + // maximum size of packet payloads (optional). + PayloadMaxSize int + sequenceNumber uint16 } @@ -51,6 +53,9 @@ func (e *Encoder) Init() { v := randUint32() 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 } @@ -66,7 +71,7 @@ func (e *Encoder) Encode(nalus [][]byte, pts time.Duration) ([]*rtp.Packet, erro // split NALUs into batches for _, nalu := range nalus { - if e.lenAggregated(batch, nalu) <= rtpPayloadMaxSize { + if e.lenAggregated(batch, nalu) <= e.PayloadMaxSize { // add to existing batch batch = append(batch, nalu) } 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) { if len(nalus) == 1 { // 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) } @@ -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) { // use only FU-A, not FU-B, since we always use non-interleaved mode // (packetization-mode=1) - packetCount := (len(nalu) - 1) / (rtpPayloadMaxSize - 2) - lastPacketSize := (len(nalu) - 1) % (rtpPayloadMaxSize - 2) + packetCount := (len(nalu) - 1) / (e.PayloadMaxSize - 2) + lastPacketSize := (len(nalu) - 1) % (e.PayloadMaxSize - 2) if lastPacketSize > 0 { packetCount++ } @@ -151,7 +156,7 @@ func (e *Encoder) writeFragmented(nalu []byte, pts time.Duration, marker bool) ( start = 1 } end := uint8(0) - le := rtpPayloadMaxSize - 2 + le := e.PayloadMaxSize - 2 if i == (packetCount - 1) { end = 1 le = lastPacketSize