github.com/pion/webrtc/v3@v3.2.24/pkg/media/samplebuilder/sampleSequenceLocation.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  // Package samplebuilder provides functionality to reconstruct media frames from RTP packets.
     5  package samplebuilder
     6  
     7  type sampleSequenceLocation struct {
     8  	// head is the first packet in a sequence
     9  	head uint16
    10  	// tail is always set to one after the final sequence number,
    11  	// so if head == tail then the sequence is empty
    12  	tail uint16
    13  }
    14  
    15  func (l sampleSequenceLocation) empty() bool {
    16  	return l.head == l.tail
    17  }
    18  
    19  func (l sampleSequenceLocation) hasData() bool {
    20  	return l.head != l.tail
    21  }
    22  
    23  func (l sampleSequenceLocation) count() uint16 {
    24  	return seqnumDistance(l.head, l.tail)
    25  }
    26  
    27  const (
    28  	slCompareVoid = iota
    29  	slCompareBefore
    30  	slCompareInside
    31  	slCompareAfter
    32  )
    33  
    34  func (l sampleSequenceLocation) compare(pos uint16) int {
    35  	if l.head == l.tail {
    36  		return slCompareVoid
    37  	}
    38  
    39  	if l.head < l.tail {
    40  		if l.head <= pos && pos < l.tail {
    41  			return slCompareInside
    42  		}
    43  	} else {
    44  		if l.head <= pos || pos < l.tail {
    45  			return slCompareInside
    46  		}
    47  	}
    48  
    49  	if l.head-pos <= pos-l.tail {
    50  		return slCompareBefore
    51  	}
    52  	return slCompareAfter
    53  }