Use difference debounce interval in negotiation (#3078)
Transport will send offer immediately if last negotiation is before debounce interval in #1929, it will cost two negotiation for a/v tracks if a pubisher publishes two tracks at same time like screenshare or enable mic/camera. This change use a small debounce interval in this case to avoid this issue.
This commit is contained in:
+12
-6
@@ -22,7 +22,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bep/debounce"
|
|
||||||
"github.com/pion/dtls/v2/pkg/crypto/elliptic"
|
"github.com/pion/dtls/v2/pkg/crypto/elliptic"
|
||||||
"github.com/pion/ice/v2"
|
"github.com/pion/ice/v2"
|
||||||
"github.com/pion/interceptor"
|
"github.com/pion/interceptor"
|
||||||
@@ -60,6 +59,7 @@ const (
|
|||||||
LossyDataChannel = "_lossy"
|
LossyDataChannel = "_lossy"
|
||||||
ReliableDataChannel = "_reliable"
|
ReliableDataChannel = "_reliable"
|
||||||
|
|
||||||
|
fastNegotiationFrequency = 10 * time.Millisecond
|
||||||
negotiationFrequency = 150 * time.Millisecond
|
negotiationFrequency = 150 * time.Millisecond
|
||||||
negotiationFailedTimeout = 15 * time.Second
|
negotiationFailedTimeout = 15 * time.Second
|
||||||
dtlsRetransmissionInterval = 100 * time.Millisecond
|
dtlsRetransmissionInterval = 100 * time.Millisecond
|
||||||
@@ -190,7 +190,7 @@ type PCTransport struct {
|
|||||||
resetShortConnOnICERestart atomic.Bool
|
resetShortConnOnICERestart atomic.Bool
|
||||||
signalingRTT atomic.Uint32 // milliseconds
|
signalingRTT atomic.Uint32 // milliseconds
|
||||||
|
|
||||||
debouncedNegotiate func(func())
|
debouncedNegotiate *sfuutils.Debouncer
|
||||||
debouncePending bool
|
debouncePending bool
|
||||||
lastNegotiate time.Time
|
lastNegotiate time.Time
|
||||||
|
|
||||||
@@ -415,7 +415,7 @@ func NewPCTransport(params TransportParams) (*PCTransport, error) {
|
|||||||
}
|
}
|
||||||
t := &PCTransport{
|
t := &PCTransport{
|
||||||
params: params,
|
params: params,
|
||||||
debouncedNegotiate: debounce.New(negotiationFrequency),
|
debouncedNegotiate: sfuutils.NewDebouncer(negotiationFrequency),
|
||||||
negotiationState: transport.NegotiationStateNone,
|
negotiationState: transport.NegotiationStateNone,
|
||||||
eventsQueue: utils.NewTypedOpsQueue[event](utils.OpsQueueParams{
|
eventsQueue: utils.NewTypedOpsQueue[event](utils.OpsQueueParams{
|
||||||
Name: "transport",
|
Name: "transport",
|
||||||
@@ -1001,8 +1001,8 @@ func (t *PCTransport) Negotiate(force bool) {
|
|||||||
|
|
||||||
var postEvent bool
|
var postEvent bool
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
if force || (!t.debouncePending && time.Since(t.lastNegotiate) > negotiationFrequency) {
|
if force {
|
||||||
t.debouncedNegotiate(func() {
|
t.debouncedNegotiate.Add(func() {
|
||||||
// no op to cancel pending negotiation
|
// no op to cancel pending negotiation
|
||||||
})
|
})
|
||||||
t.debouncePending = false
|
t.debouncePending = false
|
||||||
@@ -1011,7 +1011,13 @@ func (t *PCTransport) Negotiate(force bool) {
|
|||||||
postEvent = true
|
postEvent = true
|
||||||
} else {
|
} else {
|
||||||
if !t.debouncePending {
|
if !t.debouncePending {
|
||||||
t.debouncedNegotiate(func() {
|
if time.Since(t.lastNegotiate) > negotiationFrequency {
|
||||||
|
t.debouncedNegotiate.SetDuration(fastNegotiationFrequency)
|
||||||
|
} else {
|
||||||
|
t.debouncedNegotiate.SetDuration(negotiationFrequency)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.debouncedNegotiate.Add(func() {
|
||||||
t.lock.Lock()
|
t.lock.Lock()
|
||||||
t.debouncePending = false
|
t.debouncePending = false
|
||||||
t.updateLastNeogitateLocked()
|
t.updateLastNeogitateLocked()
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewDebouncer(after time.Duration) *Debouncer {
|
||||||
|
return &Debouncer{
|
||||||
|
after: after,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Debouncer struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
after time.Duration
|
||||||
|
timer *time.Timer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Debouncer) Add(f func()) {
|
||||||
|
d.mu.Lock()
|
||||||
|
defer d.mu.Unlock()
|
||||||
|
|
||||||
|
if d.timer != nil {
|
||||||
|
d.timer.Stop()
|
||||||
|
}
|
||||||
|
d.timer = time.AfterFunc(d.after, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Debouncer) SetDuration(after time.Duration) {
|
||||||
|
d.mu.Lock()
|
||||||
|
d.after = after
|
||||||
|
d.mu.Unlock()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user