rtp*: support decoding negative timestamps

This commit is contained in:
aler9
2021-03-09 08:36:39 +01:00
parent a66c1a8bdf
commit 7be302bd03
3 changed files with 31 additions and 4 deletions

View File

@@ -21,6 +21,10 @@ func NewDecoder(clockRate int) *Decoder {
} }
} }
func (d *Decoder) decodeTimestamp(ts uint32) time.Duration {
return (time.Duration(ts) - time.Duration(d.initialTs)) * time.Second / d.clockRate
}
// Decode decodes an AU from an RTP/AAC packet. // Decode decodes an AU from an RTP/AAC packet.
func (d *Decoder) Decode(byts []byte) (*AUAndTimestamp, error) { func (d *Decoder) Decode(byts []byte) (*AUAndTimestamp, error) {
pkt := rtp.Packet{} pkt := rtp.Packet{}
@@ -40,6 +44,6 @@ func (d *Decoder) Decode(byts []byte) (*AUAndTimestamp, error) {
return &AUAndTimestamp{ return &AUAndTimestamp{
AU: pkt.Payload[4:], AU: pkt.Payload[4:],
Timestamp: time.Duration(pkt.Timestamp-d.initialTs) * time.Second / d.clockRate, Timestamp: d.decodeTimestamp(pkt.Timestamp),
}, nil }, nil
} }

View File

@@ -50,6 +50,10 @@ func NewDecoder() *Decoder {
return &Decoder{} return &Decoder{}
} }
func (d *Decoder) decodeTimestamp(ts uint32) time.Duration {
return (time.Duration(ts) - time.Duration(d.initialTs)) * time.Second / rtpClockRate
}
// Decode decodes NALUs from RTP/H264 packets. // Decode decodes NALUs from RTP/H264 packets.
// It can return: // It can return:
// * no NALUs and ErrMorePacketsNeeded // * no NALUs and ErrMorePacketsNeeded
@@ -82,7 +86,7 @@ func (d *Decoder) Decode(byts []byte) ([]*NALUAndTimestamp, error) {
NALUTypeReserved23: NALUTypeReserved23:
return []*NALUAndTimestamp{{ return []*NALUAndTimestamp{{
NALU: pkt.Payload, NALU: pkt.Payload,
Timestamp: time.Duration(pkt.Timestamp-d.initialTs) * time.Second / rtpClockRate, Timestamp: d.decodeTimestamp(pkt.Timestamp),
}}, nil }}, nil
case NALUTypeStapA: case NALUTypeStapA:
@@ -108,7 +112,7 @@ func (d *Decoder) Decode(byts []byte) ([]*NALUAndTimestamp, error) {
ret = append(ret, &NALUAndTimestamp{ ret = append(ret, &NALUAndTimestamp{
NALU: pkt.Payload[:size], NALU: pkt.Payload[:size],
Timestamp: time.Duration(pkt.Timestamp-d.initialTs) * time.Second / rtpClockRate, Timestamp: d.decodeTimestamp(pkt.Timestamp),
}) })
pkt.Payload = pkt.Payload[size:] pkt.Payload = pkt.Payload[size:]
} }
@@ -166,7 +170,7 @@ func (d *Decoder) Decode(byts []byte) ([]*NALUAndTimestamp, error) {
d.state = decoderStateInitial d.state = decoderStateInitial
return []*NALUAndTimestamp{{ return []*NALUAndTimestamp{{
NALU: d.fragmentedBuf, NALU: d.fragmentedBuf,
Timestamp: time.Duration(pkt.Timestamp-d.initialTs) * time.Second / rtpClockRate, Timestamp: d.decodeTimestamp(pkt.Timestamp),
}}, nil }}, nil
} }
} }

View File

@@ -55,6 +55,25 @@ var cases = []struct {
), ),
}, },
}, },
{
"negative timestamp",
&NALUAndTimestamp{
Timestamp: -20 * time.Millisecond,
NALU: mergeBytes(
[]byte{0x05},
bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8),
),
},
[][]byte{
mergeBytes(
[]byte{
0x80, 0xe0, 0x44, 0xed, 0x88, 0x77, 0x5f, 0x4d,
0x9d, 0xbb, 0x78, 0x12, 0x05,
},
bytes.Repeat([]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8),
),
},
},
{ {
"fragmented", "fragmented",
&NALUAndTimestamp{ &NALUAndTimestamp{