mirror of
https://github.com/livekit/livekit.git
synced 2026-04-03 21:25:40 +00:00
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.
35 lines
477 B
Go
35 lines
477 B
Go
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()
|
|
}
|