mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 23:26:54 +08:00
rtp*: support decoding negative timestamps
This commit is contained in:
@@ -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
|
||||||
}
|
}
|
||||||
|
@@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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{
|
||||||
|
Reference in New Issue
Block a user