Use extended types for sequence number and time stamp in RTPStats. (#1999)
Reinstating this PR - https://github.com/livekit/livekit/pull/1548 Will use extended types in other places too over time.
This commit is contained in:
+24
-14
@@ -49,14 +49,14 @@ func (w *WrapAround[T, ET]) Seed(from *WrapAround[T, ET]) {
|
||||
w.cycles = from.cycles
|
||||
}
|
||||
|
||||
type wrapAroundUpdateResult[ET extendedNumber] struct {
|
||||
type WrapAroundUpdateResult[ET extendedNumber] struct {
|
||||
IsRestart bool
|
||||
PreExtendedStart ET // valid only if IsRestart = true
|
||||
PreExtendedHighest ET
|
||||
ExtendedVal ET
|
||||
}
|
||||
|
||||
func (w *WrapAround[T, ET]) Update(val T) (result wrapAroundUpdateResult[ET]) {
|
||||
func (w *WrapAround[T, ET]) Update(val T) (result WrapAroundUpdateResult[ET]) {
|
||||
if !w.initialized {
|
||||
result.PreExtendedHighest = ET(val) - 1
|
||||
result.ExtendedVal = ET(val)
|
||||
@@ -82,10 +82,17 @@ func (w *WrapAround[T, ET]) Update(val T) (result wrapAroundUpdateResult[ET]) {
|
||||
}
|
||||
w.highest = val
|
||||
|
||||
result.ExtendedVal = ET(w.cycles)*w.fullRange + ET(val)
|
||||
result.ExtendedVal = w.getExtendedHighest(w.cycles, val)
|
||||
return
|
||||
}
|
||||
|
||||
func (w *WrapAround[T, ET]) RollbackRestart(ev ET) {
|
||||
if w.isWrapBack(w.start, T(ev)) {
|
||||
w.cycles--
|
||||
}
|
||||
w.start = T(ev)
|
||||
}
|
||||
|
||||
func (w *WrapAround[T, ET]) ResetHighest(val T) {
|
||||
w.highest = val
|
||||
}
|
||||
@@ -103,14 +110,10 @@ func (w *WrapAround[T, ET]) GetHighest() T {
|
||||
}
|
||||
|
||||
func (w *WrapAround[T, ET]) GetExtendedHighest() ET {
|
||||
return ET(w.cycles)*w.fullRange + ET(w.highest)
|
||||
return w.getExtendedHighest(w.cycles, w.highest)
|
||||
}
|
||||
|
||||
func (w *WrapAround[T, ET]) maybeAdjustStart(val T) (isRestart bool, preExtendedStart ET, extendedVal ET) {
|
||||
isWrapBack := func() bool {
|
||||
return ET(w.highest) < (w.fullRange>>1) && ET(val) >= (w.fullRange>>1)
|
||||
}
|
||||
|
||||
// re-adjust start if necessary. The conditions are
|
||||
// 1. Not seen more than half the range yet
|
||||
// 1. wrap back compared to start and not completed a half cycle, sequences like (10, 65530) in uint16 space
|
||||
@@ -118,10 +121,10 @@ func (w *WrapAround[T, ET]) maybeAdjustStart(val T) (isRestart bool, preExtended
|
||||
cycles := w.cycles
|
||||
totalNum := w.GetExtendedHighest() - w.GetExtendedStart() + 1
|
||||
if totalNum > (w.fullRange >> 1) {
|
||||
if isWrapBack() {
|
||||
if w.isWrapBack(val, w.highest) {
|
||||
cycles--
|
||||
}
|
||||
extendedVal = ET(cycles)*w.fullRange + ET(val)
|
||||
extendedVal = w.getExtendedHighest(cycles, val)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -130,17 +133,24 @@ func (w *WrapAround[T, ET]) maybeAdjustStart(val T) (isRestart bool, preExtended
|
||||
isRestart = true
|
||||
preExtendedStart = w.GetExtendedStart()
|
||||
|
||||
if val > w.highest {
|
||||
// wrap around
|
||||
if w.isWrapBack(val, w.highest) {
|
||||
w.cycles = 1
|
||||
cycles = 0
|
||||
}
|
||||
w.start = val
|
||||
} else {
|
||||
if isWrapBack() {
|
||||
if w.isWrapBack(val, w.highest) {
|
||||
cycles--
|
||||
}
|
||||
}
|
||||
extendedVal = ET(cycles)*w.fullRange + ET(val)
|
||||
extendedVal = w.getExtendedHighest(cycles, val)
|
||||
return
|
||||
}
|
||||
|
||||
func (w *WrapAround[T, ET]) isWrapBack(earlier T, later T) bool {
|
||||
return ET(later) < (w.fullRange>>1) && ET(earlier) >= (w.fullRange>>1)
|
||||
}
|
||||
|
||||
func (w *WrapAround[T, ET]) getExtendedHighest(cycles int, val T) ET {
|
||||
return ET(cycles)*w.fullRange + ET(val)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
input uint16
|
||||
updated wrapAroundUpdateResult[uint32]
|
||||
updated WrapAroundUpdateResult[uint32]
|
||||
start uint16
|
||||
extendedStart uint32
|
||||
highest uint16
|
||||
@@ -35,7 +35,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "initialize",
|
||||
input: 10,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: 9,
|
||||
@@ -50,7 +50,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "reset start no wrap around",
|
||||
input: 8,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: true,
|
||||
PreExtendedStart: 10,
|
||||
PreExtendedHighest: 10,
|
||||
@@ -65,7 +65,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "reset start wrap around",
|
||||
input: (1 << 16) - 6,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: true,
|
||||
PreExtendedStart: 8,
|
||||
PreExtendedHighest: 10,
|
||||
@@ -80,7 +80,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "reset start again",
|
||||
input: (1 << 16) - 12,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: true,
|
||||
PreExtendedStart: (1 << 16) - 6,
|
||||
PreExtendedHighest: (1 << 16) + 10,
|
||||
@@ -95,7 +95,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "out of order - no restart",
|
||||
input: (1 << 16) - 3,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 16) + 10,
|
||||
@@ -110,7 +110,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "duplicate",
|
||||
input: 10,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 16) + 10,
|
||||
@@ -125,7 +125,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "big in-order jump",
|
||||
input: (1 << 15) - 10,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 16) + 10,
|
||||
@@ -140,7 +140,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "out-of-order after half range",
|
||||
input: (1 << 15) - 11,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 16) + (1 << 15) - 10,
|
||||
@@ -155,7 +155,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "wrap back out-of-order after half range",
|
||||
input: (1 << 16) - 1,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 16) + (1 << 15) - 10,
|
||||
@@ -170,7 +170,7 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
{
|
||||
name: "in-order",
|
||||
input: (1 << 15) + 3,
|
||||
updated: wrapAroundUpdateResult[uint32]{
|
||||
updated: WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 16) + (1 << 15) - 10,
|
||||
@@ -194,12 +194,72 @@ func TestWrapAroundUint16(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapAroundUint16RollbackRestart(t *testing.T) {
|
||||
w := NewWrapAround[uint16, uint32]()
|
||||
|
||||
// initialize
|
||||
w.Update(23)
|
||||
require.Equal(t, uint16(23), w.GetStart())
|
||||
require.Equal(t, uint32(23), w.GetExtendedStart())
|
||||
require.Equal(t, uint16(23), w.GetHighest())
|
||||
require.Equal(t, uint32(23), w.GetExtendedHighest())
|
||||
|
||||
// an in-order update
|
||||
w.Update(25)
|
||||
require.Equal(t, uint16(23), w.GetStart())
|
||||
require.Equal(t, uint32(23), w.GetExtendedStart())
|
||||
require.Equal(t, uint16(25), w.GetHighest())
|
||||
require.Equal(t, uint32(25), w.GetExtendedHighest())
|
||||
|
||||
// force restart without wrap
|
||||
res := w.Update(12)
|
||||
expectedResult := WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: true,
|
||||
PreExtendedStart: 23,
|
||||
PreExtendedHighest: 25,
|
||||
ExtendedVal: 12,
|
||||
}
|
||||
require.Equal(t, expectedResult, res)
|
||||
require.Equal(t, uint16(12), w.GetStart())
|
||||
require.Equal(t, uint32(12), w.GetExtendedStart())
|
||||
require.Equal(t, uint16(25), w.GetHighest())
|
||||
require.Equal(t, uint32(25), w.GetExtendedHighest())
|
||||
|
||||
// roll back restart
|
||||
w.RollbackRestart(res.PreExtendedStart)
|
||||
require.Equal(t, uint16(23), w.GetStart())
|
||||
require.Equal(t, uint32(23), w.GetExtendedStart())
|
||||
require.Equal(t, uint16(25), w.GetHighest())
|
||||
require.Equal(t, uint32(25), w.GetExtendedHighest())
|
||||
|
||||
// force restart with wrap
|
||||
res = w.Update(65533)
|
||||
expectedResult = WrapAroundUpdateResult[uint32]{
|
||||
IsRestart: true,
|
||||
PreExtendedStart: 23,
|
||||
PreExtendedHighest: 25,
|
||||
ExtendedVal: 65533,
|
||||
}
|
||||
require.Equal(t, expectedResult, res)
|
||||
require.Equal(t, uint16(65533), w.GetStart())
|
||||
require.Equal(t, uint32(65533), w.GetExtendedStart())
|
||||
require.Equal(t, uint16(25), w.GetHighest())
|
||||
require.Equal(t, uint32(65536+25), w.GetExtendedHighest())
|
||||
|
||||
// roll back restart
|
||||
w.RollbackRestart(res.PreExtendedStart)
|
||||
require.Equal(t, uint16(23), w.GetStart())
|
||||
require.Equal(t, uint32(23), w.GetExtendedStart())
|
||||
require.Equal(t, uint16(25), w.GetHighest())
|
||||
require.Equal(t, uint32(25), w.GetExtendedHighest())
|
||||
}
|
||||
|
||||
func TestWrapAroundUint32(t *testing.T) {
|
||||
w := NewWrapAround[uint32, uint64]()
|
||||
testCases := []struct {
|
||||
name string
|
||||
input uint32
|
||||
updated wrapAroundUpdateResult[uint64]
|
||||
updated WrapAroundUpdateResult[uint64]
|
||||
start uint32
|
||||
extendedStart uint64
|
||||
highest uint32
|
||||
@@ -209,7 +269,7 @@ func TestWrapAroundUint32(t *testing.T) {
|
||||
{
|
||||
name: "initialize",
|
||||
input: 10,
|
||||
updated: wrapAroundUpdateResult[uint64]{
|
||||
updated: WrapAroundUpdateResult[uint64]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: 9,
|
||||
@@ -224,7 +284,7 @@ func TestWrapAroundUint32(t *testing.T) {
|
||||
{
|
||||
name: "reset start no wrap around",
|
||||
input: 8,
|
||||
updated: wrapAroundUpdateResult[uint64]{
|
||||
updated: WrapAroundUpdateResult[uint64]{
|
||||
IsRestart: true,
|
||||
PreExtendedStart: 10,
|
||||
PreExtendedHighest: 10,
|
||||
@@ -239,7 +299,7 @@ func TestWrapAroundUint32(t *testing.T) {
|
||||
{
|
||||
name: "reset start wrap around",
|
||||
input: (1 << 32) - 6,
|
||||
updated: wrapAroundUpdateResult[uint64]{
|
||||
updated: WrapAroundUpdateResult[uint64]{
|
||||
IsRestart: true,
|
||||
PreExtendedStart: 8,
|
||||
PreExtendedHighest: 10,
|
||||
@@ -254,7 +314,7 @@ func TestWrapAroundUint32(t *testing.T) {
|
||||
{
|
||||
name: "reset start again",
|
||||
input: (1 << 32) - 12,
|
||||
updated: wrapAroundUpdateResult[uint64]{
|
||||
updated: WrapAroundUpdateResult[uint64]{
|
||||
IsRestart: true,
|
||||
PreExtendedStart: (1 << 32) - 6,
|
||||
PreExtendedHighest: (1 << 32) + 10,
|
||||
@@ -269,7 +329,7 @@ func TestWrapAroundUint32(t *testing.T) {
|
||||
{
|
||||
name: "duplicate",
|
||||
input: 10,
|
||||
updated: wrapAroundUpdateResult[uint64]{
|
||||
updated: WrapAroundUpdateResult[uint64]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 32) + 10,
|
||||
@@ -284,7 +344,7 @@ func TestWrapAroundUint32(t *testing.T) {
|
||||
{
|
||||
name: "big in-order jump",
|
||||
input: 1 << 31,
|
||||
updated: wrapAroundUpdateResult[uint64]{
|
||||
updated: WrapAroundUpdateResult[uint64]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 32) + 10,
|
||||
@@ -299,7 +359,7 @@ func TestWrapAroundUint32(t *testing.T) {
|
||||
{
|
||||
name: "out-of-order after half range",
|
||||
input: (1 << 31) - 1,
|
||||
updated: wrapAroundUpdateResult[uint64]{
|
||||
updated: WrapAroundUpdateResult[uint64]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 32) + (1 << 31),
|
||||
@@ -314,7 +374,7 @@ func TestWrapAroundUint32(t *testing.T) {
|
||||
{
|
||||
name: "in-order",
|
||||
input: (1 << 31) + 3,
|
||||
updated: wrapAroundUpdateResult[uint64]{
|
||||
updated: WrapAroundUpdateResult[uint64]{
|
||||
IsRestart: false,
|
||||
PreExtendedStart: 0,
|
||||
PreExtendedHighest: (1 << 32) + (1 << 31),
|
||||
|
||||
Reference in New Issue
Block a user