-
Notifications
You must be signed in to change notification settings - Fork 15
jitter: Add automatic buffer reset on large RTP discontinuities #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
youngSSS
wants to merge
3
commits into
livekit:main
Choose a base branch
from
youngSSS:fixJitter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| package jitter | ||
|
|
||
| import ( | ||
| "math" | ||
| "sync" | ||
| "time" | ||
|
|
||
|
|
@@ -43,6 +44,7 @@ type Buffer struct { | |
|
|
||
| initialized bool | ||
| prevSN uint16 | ||
| prevTS uint32 | ||
| head *packet | ||
| tail *packet | ||
|
|
||
|
|
@@ -51,6 +53,9 @@ type Buffer struct { | |
|
|
||
| pool *packet | ||
| size int | ||
|
|
||
| maxSequenceJump *uint16 | ||
| maxTimestampJump *uint32 | ||
| } | ||
|
|
||
| type Option func(*Buffer) | ||
|
|
@@ -112,6 +117,18 @@ func WithPacketLossHandler(handler func()) Option { | |
| } | ||
| } | ||
|
|
||
| func WithMaxSequenceJump(max uint16) Option { | ||
| return func(b *Buffer) { | ||
| b.maxSequenceJump = &max | ||
| } | ||
| } | ||
|
|
||
| func WithMaxTimestampJump(max uint32) Option { | ||
| return func(b *Buffer) { | ||
| b.maxTimestampJump = &max | ||
| } | ||
| } | ||
|
|
||
| func (b *Buffer) WithLogger(logger logger.Logger) *Buffer { | ||
| b.logger = logger | ||
| return b | ||
|
|
@@ -187,6 +204,86 @@ func (b *Buffer) Close() { | |
| b.closed.Break() | ||
| } | ||
|
|
||
| func (b *Buffer) isLargeTimestampJump(current, prev uint32) bool { | ||
| if b.maxTimestampJump == nil || !b.initialized { | ||
| return false | ||
| } | ||
|
|
||
| maxJump := uint32(*b.maxTimestampJump) | ||
|
|
||
| cur := int64(current) | ||
| prv := int64(prev) | ||
|
|
||
| forwardDiff := cur - prv | ||
| if forwardDiff < 0 { | ||
| forwardDiff += int64(math.MaxUint32) + 1 | ||
| } | ||
|
|
||
| backwardDiff := prv - cur | ||
| if backwardDiff < 0 { | ||
| backwardDiff += int64(math.MaxUint32) + 1 | ||
| } | ||
|
|
||
| return min(backwardDiff, forwardDiff) > int64(maxJump) | ||
| } | ||
|
|
||
| func (b *Buffer) isLargeSequenceJump(current, prev uint16) bool { | ||
| if b.maxSequenceJump == nil || !b.initialized { | ||
| return false | ||
| } | ||
|
|
||
| maxJump := int32(*b.maxSequenceJump) | ||
|
|
||
| cur := int32(current) | ||
| prv := int32(prev) | ||
|
|
||
| forwardDiff := cur - prv | ||
| if forwardDiff < 0 { | ||
| forwardDiff += int32(math.MaxUint16) + 1 | ||
| } | ||
|
|
||
| backwardDiff := prv - cur | ||
| if backwardDiff < 0 { | ||
| backwardDiff += int32(math.MaxUint16) + 1 | ||
| } | ||
|
|
||
| return min(backwardDiff, forwardDiff) > maxJump | ||
| } | ||
|
|
||
| func (b *Buffer) reset() { | ||
| b.logger.Infow("resetting jitter buffer due to RTP discontinuity") | ||
|
|
||
| dropped := 0 | ||
| for b.head != nil { | ||
| next := b.head.next | ||
| if !b.head.extPacket.Padding { | ||
| dropped++ | ||
| } | ||
| b.free(b.head) | ||
| b.head = next | ||
| } | ||
| b.tail = nil | ||
|
|
||
| if dropped > 0 { | ||
| b.stats.PacketsDropped += uint64(dropped) | ||
| if b.onPacketLoss != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about this again - if there was a huge gap in seen timestamps or sequence numbers it might be good to call onPacketLoss callback regardless of whether we actually had and dropped packets. It might be more robust to move it outside of if dropped > 0 block. |
||
| b.onPacketLoss() | ||
| } | ||
| } | ||
|
|
||
| b.initialized = false | ||
| b.prevSN = 0 | ||
| b.prevTS = 0 | ||
|
|
||
| if !b.timer.Stop() { | ||
| select { | ||
| case <-b.timer.C: | ||
| default: | ||
| } | ||
| } | ||
| b.timer.Reset(b.latency) | ||
| } | ||
|
|
||
| // push adds a packet to the buffer | ||
| func (b *Buffer) push(pkt *rtp.Packet, receivedAt time.Time) { | ||
| b.stats.PacketsPushed++ | ||
|
|
@@ -197,8 +294,19 @@ func (b *Buffer) push(pkt *rtp.Packet, receivedAt time.Time) { | |
| } | ||
| } | ||
|
|
||
| if b.isLargeTimestampJump(pkt.Timestamp, b.prevTS) || | ||
| b.isLargeSequenceJump(pkt.SequenceNumber, b.prevSN) { | ||
| b.logger.Infow("large RTP discontinuity detected", | ||
| "current_ts", pkt.Timestamp, | ||
| "prev_ts", b.prevTS, | ||
| "current_sn", pkt.SequenceNumber, | ||
| "prev_sn", b.prevSN, | ||
| ) | ||
| b.reset() | ||
| } | ||
|
|
||
| if b.initialized && before(pkt.SequenceNumber, b.prevSN) { | ||
| // packet expired | ||
| // packet expired (not after discontinuity reset) | ||
| if !pkt.Padding { | ||
| b.stats.PacketsDropped++ | ||
| if b.onPacketLoss != nil { | ||
|
|
@@ -354,6 +462,7 @@ func (b *Buffer) popSample() []ExtPacket { | |
| func (b *Buffer) popHead() *packet { | ||
| c := b.head | ||
| b.prevSN = c.extPacket.SequenceNumber | ||
| b.prevTS = c.extPacket.Timestamp | ||
| b.head = c.next | ||
| if b.head == nil { | ||
| b.tail = nil | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point packets are dropped - might be good to update PacketsDropped stats field and invoke packet loss callback which API consumer might use to hook up e.g sending packet loss indicator.