mirror of
https://github.com/pion/webrtc.git
synced 2025-09-27 03:25:58 +08:00
Refactored samplebuilder logic
Many corner cases would cause samplebuilder to fail and return invalid results. This refactoring is more reliable in all cases. Fixed bug in H264 writer by reusing the packet object in H264 writer.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
package samplebuilder
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/pion/rtp"
|
||||
@@ -10,8 +11,10 @@ import (
|
||||
|
||||
// SampleBuilder buffers packets until media frames are complete.
|
||||
type SampleBuilder struct {
|
||||
maxLate uint16 // how many packets to wait until we get a valid Sample
|
||||
buffer [65536]*rtp.Packet
|
||||
maxLate uint16 // how many packets to wait until we get a valid Sample
|
||||
maxLateTimestamp uint32 // max timestamp between old and new timestamps before dropping packets
|
||||
buffer [math.MaxUint16 + 1]*rtp.Packet
|
||||
preparedSamples [math.MaxUint16 + 1]*media.Sample
|
||||
|
||||
// Interface that allows us to take RTP packets to samples
|
||||
depacketizer rtp.Depacketizer
|
||||
@@ -19,22 +22,24 @@ type SampleBuilder struct {
|
||||
// sampleRate allows us to compute duration of media.SamplecA
|
||||
sampleRate uint32
|
||||
|
||||
// Last seqnum that has been added to buffer
|
||||
lastPush uint16
|
||||
|
||||
// Last seqnum that has been successfully popped
|
||||
// isContiguous is false when we start or when we have a gap
|
||||
// that is older then maxLate
|
||||
isContiguous bool
|
||||
lastPopSeq uint16
|
||||
lastPopTimestamp uint32
|
||||
|
||||
// Interface that checks whether the packet is the first fragment of the frame or not
|
||||
partitionHeadChecker rtp.PartitionHeadChecker
|
||||
|
||||
// the handler to be called when the builder is about to remove the
|
||||
// reference to some packet.
|
||||
packetReleaseHandler func(*rtp.Packet)
|
||||
|
||||
// filled contains the head/tail of the packets inserted into the buffer
|
||||
filled sampleSequenceLocation
|
||||
|
||||
// active contains the active head/tail of the timestamp being actively processed
|
||||
active sampleSequenceLocation
|
||||
|
||||
// prepared contains the samples that have been processed to date
|
||||
prepared sampleSequenceLocation
|
||||
|
||||
// number of packets forced to be dropped
|
||||
droppedPackets uint16
|
||||
}
|
||||
|
||||
// New constructs a new SampleBuilder.
|
||||
@@ -51,6 +56,51 @@ func New(maxLate uint16, depacketizer rtp.Depacketizer, sampleRate uint32, opts
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SampleBuilder) tooOld(location sampleSequenceLocation) bool {
|
||||
if s.maxLateTimestamp == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
var foundHead *rtp.Packet
|
||||
var foundTail *rtp.Packet
|
||||
|
||||
for i := location.head; i != location.tail; i++ {
|
||||
if packet := s.buffer[i]; packet != nil {
|
||||
foundHead = packet
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if foundHead == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := location.tail - 1; i != location.head; i-- {
|
||||
if packet := s.buffer[i]; packet != nil {
|
||||
foundTail = packet
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if foundTail == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return timestampDistance(foundHead.Timestamp, foundTail.Timestamp) > s.maxLateTimestamp
|
||||
}
|
||||
|
||||
// fetchTimestamp returns the timestamp associated with a given sample location
|
||||
func (s *SampleBuilder) fetchTimestamp(location sampleSequenceLocation) (timestamp uint32, hasData bool) {
|
||||
if location.empty() {
|
||||
return 0, false
|
||||
}
|
||||
packet := s.buffer[location.head]
|
||||
if packet == nil {
|
||||
return 0, false
|
||||
}
|
||||
return packet.Timestamp, true
|
||||
}
|
||||
|
||||
func (s *SampleBuilder) releasePacket(i uint16) {
|
||||
var p *rtp.Packet
|
||||
p, s.buffer[i] = s.buffer[i], nil
|
||||
@@ -59,6 +109,43 @@ func (s *SampleBuilder) releasePacket(i uint16) {
|
||||
}
|
||||
}
|
||||
|
||||
// purgeConsumedBuffers clears all buffers that have already been consumed by
|
||||
// popping.
|
||||
func (s *SampleBuilder) purgeConsumedBuffers() {
|
||||
for s.active.compare(s.filled.head) == slCompareBefore && s.filled.hasData() {
|
||||
s.releasePacket(s.filled.head)
|
||||
s.filled.head++
|
||||
}
|
||||
}
|
||||
|
||||
// purgeBuffers flushes all buffers that are already consumed or those buffers
|
||||
// that are too late to consume.
|
||||
func (s *SampleBuilder) purgeBuffers() {
|
||||
s.purgeConsumedBuffers()
|
||||
|
||||
for (s.tooOld(s.filled) || (s.filled.count() > s.maxLate)) && s.filled.hasData() {
|
||||
if s.active.empty() {
|
||||
// refill the active based on the filled packets
|
||||
s.active = s.filled
|
||||
}
|
||||
|
||||
if s.active.hasData() && (s.active.head == s.filled.head) {
|
||||
// attempt to force the active packet to be consumed even though
|
||||
// outstanding data may be pending arrival
|
||||
if s.buildSample(true) != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// could not build the sample so drop it
|
||||
s.active.head++
|
||||
s.droppedPackets++
|
||||
}
|
||||
|
||||
s.releasePacket(s.filled.head)
|
||||
s.filled.head++
|
||||
}
|
||||
}
|
||||
|
||||
// Push adds an RTP Packet to s's buffer.
|
||||
//
|
||||
// Push does not copy the input. If you wish to reuse
|
||||
@@ -66,58 +153,139 @@ func (s *SampleBuilder) releasePacket(i uint16) {
|
||||
func (s *SampleBuilder) Push(p *rtp.Packet) {
|
||||
s.buffer[p.SequenceNumber] = p
|
||||
|
||||
// Remove outdated references if SequenceNumber is increased.
|
||||
if int16(p.SequenceNumber-s.lastPush) > 0 {
|
||||
for i := s.lastPush; i != p.SequenceNumber+1; i++ {
|
||||
s.releasePacket(i - s.maxLate)
|
||||
}
|
||||
switch s.filled.compare(p.SequenceNumber) {
|
||||
case slCompareVoid:
|
||||
s.filled.head = p.SequenceNumber
|
||||
s.filled.tail = p.SequenceNumber + 1
|
||||
case slCompareBefore:
|
||||
s.filled.head = p.SequenceNumber
|
||||
case slCompareAfter:
|
||||
s.filled.tail = p.SequenceNumber + 1
|
||||
case slCompareInside:
|
||||
break
|
||||
}
|
||||
|
||||
s.lastPush = p.SequenceNumber
|
||||
s.purgeBuffers()
|
||||
}
|
||||
|
||||
const secondToNanoseconds = 1000000000
|
||||
|
||||
// We have a valid collection of RTP Packets
|
||||
// walk forwards building a sample if everything looks good clear and update buffer+values
|
||||
func (s *SampleBuilder) buildSample(firstBuffer uint16) (*media.Sample, uint32) {
|
||||
// buildSample creates a sample from a valid collection of RTP Packets by
|
||||
// walking forwards building a sample if everything looks good clear and
|
||||
// update buffer+values
|
||||
func (s *SampleBuilder) buildSample(purgingBuffers bool) *media.Sample {
|
||||
if s.active.empty() {
|
||||
s.active = s.filled
|
||||
}
|
||||
|
||||
if s.active.empty() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.filled.compare(s.active.tail) == slCompareInside {
|
||||
s.active.tail = s.filled.tail
|
||||
}
|
||||
|
||||
var consume sampleSequenceLocation
|
||||
|
||||
for i := s.active.head; s.buffer[i] != nil && i < s.active.tail; i++ {
|
||||
if s.depacketizer.IsDetectedFinalPacketInSequence(s.buffer[i].Marker) {
|
||||
consume.head = s.active.head
|
||||
consume.tail = i + 1
|
||||
break
|
||||
}
|
||||
headTimestamp, hasData := s.fetchTimestamp(s.active)
|
||||
if hasData && s.buffer[i].Timestamp != headTimestamp {
|
||||
consume.head = s.active.head
|
||||
consume.tail = i
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if consume.empty() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !purgingBuffers && s.buffer[consume.tail] == nil {
|
||||
// wait for the next packet after this set of packets to arrive
|
||||
// to ensure at least one post sample timestamp is known
|
||||
// (unless we have to release right now)
|
||||
return nil
|
||||
}
|
||||
|
||||
sampleTimestamp, _ := s.fetchTimestamp(s.active)
|
||||
afterTimestamp := sampleTimestamp
|
||||
|
||||
// scan for any packet after the current and use that time stamp as the diff point
|
||||
for i := consume.tail; i < s.active.tail; i++ {
|
||||
if s.buffer[i] != nil {
|
||||
afterTimestamp = s.buffer[i].Timestamp
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// the head set of packets is now fully consumed
|
||||
s.active.head = consume.tail
|
||||
|
||||
// prior to decoding all the packets, check if this packet
|
||||
// would end being disposed anyway
|
||||
if s.partitionHeadChecker != nil {
|
||||
if !s.partitionHeadChecker.IsPartitionHead(s.buffer[consume.head].Payload) {
|
||||
s.droppedPackets += consume.count()
|
||||
s.purgeConsumedBuffers()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// merge all the buffers into a sample
|
||||
data := []byte{}
|
||||
|
||||
for i := firstBuffer; s.buffer[i] != nil; i++ {
|
||||
if s.buffer[i].Timestamp != s.buffer[firstBuffer].Timestamp {
|
||||
lastTimeStamp := s.lastPopTimestamp
|
||||
if !s.isContiguous {
|
||||
if s.buffer[firstBuffer-1] != nil {
|
||||
lastTimeStamp = s.buffer[firstBuffer-1].Timestamp
|
||||
} else {
|
||||
// If PartitionHeadChecker detects that the first packet is a head,
|
||||
// the duration of the packet is not guessable
|
||||
lastTimeStamp = s.buffer[firstBuffer].Timestamp
|
||||
}
|
||||
}
|
||||
|
||||
samples := s.buffer[i-1].Timestamp - lastTimeStamp
|
||||
s.lastPopSeq = i - 1
|
||||
s.isContiguous = true
|
||||
s.lastPopTimestamp = s.buffer[i-1].Timestamp
|
||||
for j := firstBuffer; j < i; j++ {
|
||||
s.releasePacket(j)
|
||||
}
|
||||
|
||||
return &media.Sample{Data: data, Duration: time.Duration((float64(samples)/float64(s.sampleRate))*secondToNanoseconds) * time.Nanosecond}, s.lastPopTimestamp
|
||||
}
|
||||
|
||||
p, err := s.depacketizer.Unmarshal(s.buffer[i].Payload)
|
||||
for ; consume.head != consume.tail; consume.head++ {
|
||||
p, err := s.depacketizer.Unmarshal(s.buffer[consume.head].Payload)
|
||||
if err != nil {
|
||||
return nil, 0
|
||||
return nil
|
||||
}
|
||||
|
||||
data = append(data, p...)
|
||||
}
|
||||
return nil, 0
|
||||
samples := afterTimestamp - sampleTimestamp
|
||||
|
||||
sample := &media.Sample{
|
||||
Data: data,
|
||||
Duration: time.Duration((float64(samples)/float64(s.sampleRate))*secondToNanoseconds) * time.Nanosecond,
|
||||
PacketTimestamp: sampleTimestamp,
|
||||
PrevDroppedPackets: s.droppedPackets,
|
||||
}
|
||||
|
||||
s.droppedPackets = 0
|
||||
|
||||
s.preparedSamples[s.prepared.tail] = sample
|
||||
s.prepared.tail++
|
||||
|
||||
s.purgeConsumedBuffers()
|
||||
|
||||
return sample
|
||||
}
|
||||
|
||||
// Distance between two seqnums
|
||||
// Pop compiles pushed RTP packets into media samples and then
|
||||
// returns the next valid sample (or nil if no sample is compiled).
|
||||
func (s *SampleBuilder) Pop() *media.Sample {
|
||||
_ = s.buildSample(false)
|
||||
if s.prepared.empty() {
|
||||
return nil
|
||||
}
|
||||
var result *media.Sample
|
||||
result, s.preparedSamples[s.prepared.head] = s.preparedSamples[s.prepared.head], nil
|
||||
s.prepared.head++
|
||||
return result
|
||||
}
|
||||
|
||||
// PopWithTimestamp compiles pushed RTP packets into media samples and then
|
||||
// returns the next valid sample with its associated RTP timestamp (or nil, 0 if
|
||||
// no sample is compiled).
|
||||
func (s *SampleBuilder) PopWithTimestamp() (*media.Sample, uint32) {
|
||||
sample := s.Pop()
|
||||
return sample, sample.PacketTimestamp
|
||||
}
|
||||
|
||||
// seqnumDistance computes the distance between two sequence numbers
|
||||
func seqnumDistance(x, y uint16) uint16 {
|
||||
diff := int16(x - y)
|
||||
if diff < 0 {
|
||||
@@ -127,53 +295,14 @@ func seqnumDistance(x, y uint16) uint16 {
|
||||
return uint16(diff)
|
||||
}
|
||||
|
||||
// Pop scans s's buffer for a valid sample.
|
||||
// It returns nil if no valid samples have been found.
|
||||
func (s *SampleBuilder) Pop() *media.Sample {
|
||||
sample, _ := s.PopWithTimestamp()
|
||||
return sample
|
||||
}
|
||||
|
||||
// PopWithTimestamp scans s's buffer for a valid sample and its RTP timestamp.
|
||||
// It returns nil, 0 when no valid samples have been found.
|
||||
func (s *SampleBuilder) PopWithTimestamp() (*media.Sample, uint32) {
|
||||
var i uint16
|
||||
if !s.isContiguous {
|
||||
i = s.lastPush - s.maxLate
|
||||
} else {
|
||||
if seqnumDistance(s.lastPopSeq, s.lastPush) > s.maxLate {
|
||||
i = s.lastPush - s.maxLate
|
||||
s.isContiguous = false
|
||||
} else {
|
||||
i = s.lastPopSeq + 1
|
||||
}
|
||||
// timestampDistance computes the distance between two timestamps
|
||||
func timestampDistance(x, y uint32) uint32 {
|
||||
diff := int32(x - y)
|
||||
if diff < 0 {
|
||||
return uint32(-diff)
|
||||
}
|
||||
|
||||
for ; i != s.lastPush; i++ {
|
||||
curr := s.buffer[i]
|
||||
if curr == nil {
|
||||
continue // we haven't hit a buffer yet, keep moving
|
||||
}
|
||||
|
||||
if !s.isContiguous {
|
||||
if s.partitionHeadChecker == nil {
|
||||
if s.buffer[i-1] == nil {
|
||||
continue // We have never popped a buffer, so we can't assert that the first RTP packet we encounter is valid
|
||||
} else if s.buffer[i-1].Timestamp == curr.Timestamp {
|
||||
continue // We have the same timestamps, so it is data that spans multiple RTP packets
|
||||
}
|
||||
} else {
|
||||
if !s.partitionHeadChecker.IsPartitionHead(curr.Payload) {
|
||||
continue
|
||||
}
|
||||
// We can start using this frame as it is a head of frame partition
|
||||
}
|
||||
}
|
||||
|
||||
// Initial validity checks have passed, walk forward
|
||||
return s.buildSample(i)
|
||||
}
|
||||
return nil, 0
|
||||
return uint32(diff)
|
||||
}
|
||||
|
||||
// An Option configures a SampleBuilder.
|
||||
@@ -194,3 +323,12 @@ func WithPacketReleaseHandler(h func(*rtp.Packet)) Option {
|
||||
o.packetReleaseHandler = h
|
||||
}
|
||||
}
|
||||
|
||||
// WithMaxTimeDelay ensures that packets that are too old in the buffer get
|
||||
// purged based on time rather than building up an extraordinarily long delay.
|
||||
func WithMaxTimeDelay(maxLateDuration time.Duration) Option {
|
||||
return func(o *SampleBuilder) {
|
||||
totalMillis := maxLateDuration.Milliseconds()
|
||||
o.maxLateTimestamp = uint32(int64(o.sampleRate) * totalMillis / 1000)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user