add dc backpressure to all platforms
This commit is contained in:
1 parent
a96e29c2e5
commit
4ad375bf00
12 files changed
+145
-46
No files matched your search
@@ -29,6 +29,7 @@ type MediaParams struct {
|
||||
Reliable bool
|
||||
DualTrack bool
|
||||
ReadBuf int
|
||||
MaxDCBuf uint64
|
||||
LogFn func(string, ...any)
|
||||
}
|
||||
|
||||
@@ -253,7 +254,11 @@ func (s *MediaSession) maybeStartDCTunnel() {
|
||||
if readBuf == 0 {
|
||||
readBuf = common.DCBufSize
|
||||
}
|
||||
dctun := rtc.NewChunkedDCTunnelFromRaw(readWrapped, writeWrapped, s.obf, readBuf, s.p.LogFn)
|
||||
maxDCBuf := s.p.MaxDCBuf
|
||||
if maxDCBuf == 0 {
|
||||
maxDCBuf = common.MaxDCBufSize
|
||||
}
|
||||
dctun := rtc.NewChunkedDCTunnelFromRaw(readWrapped, writeWrapped, pubDC, s.obf, readBuf, maxDCBuf, s.p.LogFn)
|
||||
s.mu.Lock()
|
||||
s.dctun = dctun
|
||||
s.mu.Unlock()
|
||||
|
||||
@@ -34,6 +34,7 @@ type SessionConfig struct {
|
||||
VP8FPS int
|
||||
VP8Batch int
|
||||
ReadBuf int
|
||||
MaxDCBuf uint64
|
||||
ScreenShare bool
|
||||
IsJoiner bool
|
||||
Reliable bool
|
||||
@@ -286,7 +287,11 @@ func (s *Session) maybeStartDCTunnel() {
|
||||
if readBuf == 0 {
|
||||
readBuf = common.DCBufSize
|
||||
}
|
||||
dcTunnel := rtc.NewChunkedDCTunnelFromRaw(wrapped, wrapped, s.cfg.Obfuscator, readBuf, s.cfg.LogFn)
|
||||
maxDCBuf := s.cfg.MaxDCBuf
|
||||
if maxDCBuf == 0 {
|
||||
maxDCBuf = common.MaxDCBufSize
|
||||
}
|
||||
dcTunnel := rtc.NewChunkedDCTunnelFromRaw(wrapped, wrapped, reliableChannel, s.cfg.Obfuscator, readBuf, maxDCBuf, s.cfg.LogFn)
|
||||
s.mu.Lock()
|
||||
s.dctun = dcTunnel
|
||||
s.mu.Unlock()
|
||||
|
||||
@@ -25,8 +25,9 @@ const (
|
||||
RTPBufSize = 65536
|
||||
// VP8BufSize fits one RTP packet: 1200 MTU - 1 VP8 descriptor - 64 tunnel wrapper - 9 protocol frame
|
||||
// (tunnel wrapper = 20 vp8 keepalive header + 4 epoch + 24 XChaCha20 nonce + 16 Poly1305 tag)
|
||||
VP8BufSize = 1126
|
||||
DCBufSize = 32768
|
||||
VP8BufSize = 1126
|
||||
DCBufSize = 32768
|
||||
MaxDCBufSize = 1 << 20
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
@@ -624,7 +624,7 @@ func (h *VKHeadlessJoiner) initPC() {
|
||||
h.logFn("vk-joiner: === DC TUNNEL CONNECTED ===")
|
||||
h.Status.EmitStatus(common.StatusTunnelConnected)
|
||||
if h.OnConnected != nil {
|
||||
h.OnConnected(rtc.NewDCTunnel(dc, h.obf, common.RTPBufSize, h.logFn))
|
||||
h.OnConnected(rtc.NewDCTunnel(dc, h.obf, common.RTPBufSize, common.MaxDCBufSize, h.logFn))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kulikov0/headless-client/webrtc"
|
||||
)
|
||||
|
||||
const dcFlowRecheckInterval = 100 * time.Millisecond
|
||||
|
||||
type DCFlowGate struct {
|
||||
dc *webrtc.DataChannel
|
||||
highWater uint64
|
||||
lowWater uint64
|
||||
mu sync.Mutex
|
||||
lowSignal chan struct{}
|
||||
}
|
||||
|
||||
func NewDCFlowGate(dc *webrtc.DataChannel, maxDCBuf uint64) *DCFlowGate {
|
||||
gate := &DCFlowGate{
|
||||
dc: dc,
|
||||
highWater: maxDCBuf,
|
||||
lowWater: maxDCBuf / 2,
|
||||
lowSignal: make(chan struct{}, 1),
|
||||
}
|
||||
dc.SetBufferedAmountLowThreshold(gate.lowWater)
|
||||
dc.OnBufferedAmountLow(func() {
|
||||
select {
|
||||
case gate.lowSignal <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
})
|
||||
return gate
|
||||
}
|
||||
|
||||
func (g *DCFlowGate) Acquire() {
|
||||
g.mu.Lock()
|
||||
if g.dc.BufferedAmount() <= g.highWater {
|
||||
return
|
||||
}
|
||||
for g.dc.BufferedAmount() > g.lowWater && g.dc.ReadyState() == webrtc.DataChannelStateOpen {
|
||||
select {
|
||||
case <-g.lowSignal:
|
||||
case <-time.After(dcFlowRecheckInterval):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (g *DCFlowGate) Release() {
|
||||
g.mu.Unlock()
|
||||
}
|
||||
@@ -35,6 +35,7 @@ type DCTunnel struct {
|
||||
obf *tunnel.TunnelObfuscator
|
||||
chunked bool
|
||||
readBuf int
|
||||
flowGate *DCFlowGate
|
||||
|
||||
recvBufs sync.Map
|
||||
sendMsgID uint32
|
||||
@@ -45,8 +46,8 @@ type DCTunnel struct {
|
||||
sendMsgs atomic.Uint64
|
||||
}
|
||||
|
||||
func NewDCTunnel(dc *webrtc.DataChannel, obf *tunnel.TunnelObfuscator, readBuf int, logFn func(string, ...any)) *DCTunnel {
|
||||
t := &DCTunnel{dc: dc, obf: obf, readBuf: readBuf, logFn: logFn}
|
||||
func NewDCTunnel(dc *webrtc.DataChannel, obf *tunnel.TunnelObfuscator, readBuf int, maxDCBuf uint64, logFn func(string, ...any)) *DCTunnel {
|
||||
t := &DCTunnel{dc: dc, obf: obf, readBuf: readBuf, flowGate: NewDCFlowGate(dc, maxDCBuf), logFn: logFn}
|
||||
|
||||
raw, err := dc.Detach()
|
||||
if err != nil {
|
||||
@@ -69,25 +70,25 @@ func NewDCTunnel(dc *webrtc.DataChannel, obf *tunnel.TunnelObfuscator, readBuf i
|
||||
return t
|
||||
}
|
||||
|
||||
func NewDCTunnelFromRaw(dc *webrtc.DataChannel, raw datachannel.ReadWriteCloser, obf *tunnel.TunnelObfuscator, readBuf int, logFn func(string, ...any)) *DCTunnel {
|
||||
t := &DCTunnel{dc: dc, raw: raw, obf: obf, readBuf: readBuf, logFn: logFn}
|
||||
func NewDCTunnelFromRaw(dc *webrtc.DataChannel, raw datachannel.ReadWriteCloser, obf *tunnel.TunnelObfuscator, readBuf int, maxDCBuf uint64, logFn func(string, ...any)) *DCTunnel {
|
||||
t := &DCTunnel{dc: dc, raw: raw, obf: obf, readBuf: readBuf, flowGate: NewDCFlowGate(dc, maxDCBuf), logFn: logFn}
|
||||
go t.readLoop()
|
||||
return t
|
||||
}
|
||||
|
||||
func NewChunkedDCTunnel(readRaw datachannel.ReadWriteCloser, writeDC *webrtc.DataChannel, obf *tunnel.TunnelObfuscator, readBuf int, logFn func(string, ...any)) *DCTunnel {
|
||||
func NewChunkedDCTunnel(readRaw datachannel.ReadWriteCloser, writeDC *webrtc.DataChannel, obf *tunnel.TunnelObfuscator, readBuf int, maxDCBuf uint64, logFn func(string, ...any)) *DCTunnel {
|
||||
writeRaw, err := writeDC.Detach()
|
||||
if err != nil {
|
||||
logFn("dctunnel: write DC detach failed: %v", err)
|
||||
return nil
|
||||
}
|
||||
t := &DCTunnel{raw: readRaw, writeRaw: writeRaw, obf: obf, readBuf: readBuf, logFn: logFn, chunked: true}
|
||||
t := &DCTunnel{raw: readRaw, writeRaw: writeRaw, obf: obf, readBuf: readBuf, flowGate: NewDCFlowGate(writeDC, maxDCBuf), logFn: logFn, chunked: true}
|
||||
go t.readLoop()
|
||||
return t
|
||||
}
|
||||
|
||||
func NewChunkedDCTunnelFromRaw(readRaw, writeRaw datachannel.ReadWriteCloser, obf *tunnel.TunnelObfuscator, readBuf int, logFn func(string, ...any)) *DCTunnel {
|
||||
t := &DCTunnel{raw: readRaw, writeRaw: writeRaw, obf: obf, readBuf: readBuf, logFn: logFn, chunked: true}
|
||||
func NewChunkedDCTunnelFromRaw(readRaw, writeRaw datachannel.ReadWriteCloser, writeDC *webrtc.DataChannel, obf *tunnel.TunnelObfuscator, readBuf int, maxDCBuf uint64, logFn func(string, ...any)) *DCTunnel {
|
||||
t := &DCTunnel{raw: readRaw, writeRaw: writeRaw, obf: obf, readBuf: readBuf, flowGate: NewDCFlowGate(writeDC, maxDCBuf), logFn: logFn, chunked: true}
|
||||
go t.readLoop()
|
||||
return t
|
||||
}
|
||||
@@ -242,6 +243,10 @@ func (t *DCTunnel) SendData(data []byte) {
|
||||
return
|
||||
}
|
||||
}
|
||||
if msgType == tunnel.MsgData {
|
||||
t.flowGate.Acquire()
|
||||
defer t.flowGate.Release()
|
||||
}
|
||||
if t.chunked {
|
||||
t.sendChunked(wire)
|
||||
} else {
|
||||
|
||||
@@ -46,6 +46,7 @@ type SessionConfig struct {
|
||||
RoomID string
|
||||
AccessToken string
|
||||
ReadBuf int
|
||||
MaxDCBuf uint64
|
||||
ScreenShare bool // when true, publish a second VP8 track as ScreenShare and shard outbound across both
|
||||
IsJoiner bool // when true, run the configPingPong loop; only the joiner sends VP8 config to the peer
|
||||
Reliable bool
|
||||
@@ -336,7 +337,11 @@ func (s *Session) maybeStartDCTunnel() {
|
||||
if readBuf == 0 {
|
||||
readBuf = common.DCBufSize
|
||||
}
|
||||
dctun := rtc.NewChunkedDCTunnelFromRaw(readWrapped, writeWrapped, s.cfg.Obfuscator, readBuf, s.cfg.LogFn)
|
||||
maxDCBuf := s.cfg.MaxDCBuf
|
||||
if maxDCBuf == 0 {
|
||||
maxDCBuf = common.MaxDCBufSize
|
||||
}
|
||||
dctun := rtc.NewChunkedDCTunnelFromRaw(readWrapped, writeWrapped, pubDC, s.cfg.Obfuscator, readBuf, maxDCBuf, s.cfg.LogFn)
|
||||
s.mu.Lock()
|
||||
s.dctun = dctun
|
||||
s.mu.Unlock()
|
||||
|
||||
Reference in new issue
Block a user