add dc backpressure to all platforms

This commit is contained in:
kulikov0 committed 2026-11-07 11:41:31 +01:00
1 parent a96e29c2e5
commit 4ad375bf00
12 files changed
+145 -46

No files matched your search

+18 -8
View File
@@ -26,6 +26,7 @@ func main() {
roomFlag := flag.String("room", "", "conference link https://portal/video/CODE or alias to rejoin as host (empty = create new room)")
resources := flag.String("resources", "default", "resource mode: moderate, default, unlimited, custom")
customReadBuf := flag.Int("read-buf", 0, "DC read buffer size in bytes, used with -resources custom")
customMaxDCBuf := flag.Int("max-dc-buf", 0, "max DC buffer size in bytes, used with -resources custom")
writeFile := flag.String("write-file", "", "path to file where the active guest link is appended")
upstreamSocks := flag.String("upstream-socks", "", "route tunneled egress through this SOCKS5 proxy (host:port), e.g. a local VPN client")
upstreamUser := flag.String("upstream-user", "", "upstream SOCKS5 username")
@@ -39,22 +40,30 @@ func main() {
common.AllowLoopbackDst = *allowLoopback
var readBuf int
var maxDCBuf uint64
var memLimit int64
switch *resources {
case "moderate":
readBuf = 16384
maxDCBuf = 1 << 20
memLimit = 64 << 20
case "default":
readBuf = common.DCBufSize
maxDCBuf = common.MaxDCBufSize
memLimit = 128 << 20
case "unlimited":
readBuf = common.RTPBufSize
maxDCBuf = 8 << 20
memLimit = 256 << 20
case "custom":
readBuf = *customReadBuf
if readBuf == 0 {
readBuf = common.RTPBufSize
}
maxDCBuf = uint64(*customMaxDCBuf)
if maxDCBuf == 0 {
maxDCBuf = 8 << 20
}
memLimit = 256 << 20
default:
log.Fatalf("[config] unknown resources mode: %s (use moderate, default, unlimited, custom)", *resources)
@@ -62,7 +71,7 @@ func main() {
if memLimit > 0 {
debug.SetMemoryLimit(memLimit)
}
log.Printf("[config] resources=%s read-buf=%d", *resources, readBuf)
log.Printf("[config] resources=%s read-buf=%d max-dc-buf=%d", *resources, readBuf, maxDCBuf)
if *cookiesPath == "" {
log.Fatalf("[FATAL] --cookies is required")
@@ -190,13 +199,14 @@ func main() {
}
ms, err := bitrix.NewMediaSession(bitrix.MediaParams{
Signal: sig,
Alias: alias,
Mode: bitrix.TunnelModeAuto,
FPS: 24,
Batch: 30,
ReadBuf: readBuf,
LogFn: log.Printf,
Signal: sig,
Alias: alias,
Mode: bitrix.TunnelModeAuto,
FPS: 24,
Batch: 30,
ReadBuf: readBuf,
MaxDCBuf: maxDCBuf,
LogFn: log.Printf,
})
if err != nil {
log.Fatalf("[FATAL] media session: %v", err)
+11 -1
View File
@@ -22,6 +22,7 @@ func main() {
roomFlag := flag.String("room", "", "Blink chat id, blink://<chat id> or group invite link to rejoin (empty = create new room)")
resources := flag.String("resources", "default", "resource mode: default, moderate, unlimited, custom")
customReadBuf := flag.Int("read-buf", 0, "DC read buffer size in bytes, used with -resources custom")
customMaxDCBuf := flag.Int("max-dc-buf", 0, "max DC buffer size in bytes, used with -resources custom")
customMemLimit := flag.Int64("mem-limit", 0, "memory limit in bytes, used with -resources custom")
writeFile := flag.String("write-file", "", "path to file where the join link is appended")
upstreamSocks := flag.String("upstream-socks", "", "route tunneled egress through this SOCKS5 proxy (host:port), e.g. a local VPN client")
@@ -36,22 +37,30 @@ func main() {
common.AllowLoopbackDst = *allowLoopback
var readBuf int
var maxDCBuf uint64
var memLimit int64
switch *resources {
case "moderate":
readBuf = 16384
maxDCBuf = 1 << 20
memLimit = 64 << 20
case "default":
readBuf = common.DCBufSize
maxDCBuf = common.MaxDCBufSize
memLimit = 128 << 20
case "unlimited":
readBuf = common.RTPBufSize
maxDCBuf = 8 << 20
memLimit = 256 << 20
case "custom":
readBuf = *customReadBuf
if readBuf == 0 {
readBuf = common.RTPBufSize
}
maxDCBuf = uint64(*customMaxDCBuf)
if maxDCBuf == 0 {
maxDCBuf = 8 << 20
}
memLimit = *customMemLimit
if memLimit == 0 {
memLimit = 256 << 20
@@ -62,7 +71,7 @@ func main() {
if memLimit > 0 {
debug.SetMemoryLimit(memLimit)
}
log.Printf("[config] resources=%s read-buf=%d mem-limit=%d", *resources, readBuf, memLimit)
log.Printf("[config] resources=%s read-buf=%d max-dc-buf=%d mem-limit=%d", *resources, readBuf, maxDCBuf, memLimit)
if *cookiesPath == "" {
log.Fatalf("[auth] --cookies is required")
@@ -128,6 +137,7 @@ func main() {
Obfuscator: obfuscator,
LogFn: log.Printf,
ReadBuf: readBuf,
MaxDCBuf: maxDCBuf,
})
session.OnConnected = func(dataTunnel tunnel.DataTunnel) {
if activeBridge != nil {
+9 -9
View File
@@ -658,16 +658,16 @@ func main() {
switch *resources {
case "moderate":
readBuf = 16384
maxDCBuf = 1 * 1024 * 1024
memLimit = 64 * 1024 * 1024
maxDCBuf = 1 << 20
memLimit = 64 << 20
case "default":
readBuf = 32768
maxDCBuf = 4 * 1024 * 1024
memLimit = 128 * 1024 * 1024
readBuf = common.DCBufSize
maxDCBuf = common.MaxDCBufSize
memLimit = 128 << 20
case "unlimited":
readBuf = common.RTPBufSize
maxDCBuf = 8 * 1024 * 1024
memLimit = 256 * 1024 * 1024
maxDCBuf = 8 << 20
memLimit = 256 << 20
case "custom":
readBuf = *customReadBuf
if readBuf == 0 {
@@ -675,11 +675,11 @@ func main() {
}
maxDCBuf = uint64(*customMaxDCBuf)
if maxDCBuf == 0 {
maxDCBuf = 8 * 1024 * 1024
maxDCBuf = 8 << 20
}
memLimit = *customMemLimit
if memLimit == 0 {
memLimit = 256 * 1024 * 1024
memLimit = 256 << 20
}
default:
log.Fatalf("[config] unknown resources mode: %s (use moderate, default, unlimited, custom)", *resources)
+9 -13
View File
@@ -31,9 +31,10 @@ type TunnelRelay struct {
externalICE func(*webrtc.ICECandidate)
externalCSC func(webrtc.PeerConnectionState)
dc *webrtc.DataChannel
dcMu sync.Mutex
conns sync.Map
dc *webrtc.DataChannel
dcMu sync.Mutex
dcFlowGate *rtc.DCFlowGate
conns sync.Map
sampleTrack *webrtc.TrackLocalStaticSample
tun *rtc.VP8DataTunnel
@@ -85,6 +86,7 @@ func (u *TunnelRelay) Init(iceServers []webrtc.ICEServer) error {
log.Printf("[relay] warning: could not create tunnel DC: %v", err)
} else {
u.dc = dc
u.dcFlowGate = rtc.NewDCFlowGate(dc, u.maxDCBuf)
dc.OnOpen(func() {
log.Printf("[relay] tunnel DC open (readyState=%v)", dc.ReadyState())
})
@@ -336,6 +338,10 @@ func (u *TunnelRelay) handleDCMessage(data []byte) {
}
func (u *TunnelRelay) sendDCFrame(connID uint32, mt byte, payload []byte) {
if mt == tunnel.MsgData && u.dcFlowGate != nil {
u.dcFlowGate.Acquire()
defer u.dcFlowGate.Release()
}
u.dcMu.Lock()
defer u.dcMu.Unlock()
if u.dc == nil {
@@ -393,16 +399,6 @@ func (u *TunnelRelay) connectTCP(connID uint32, addr string) {
buf := make([]byte, bufSz)
sent := 0
for {
if u.maxDCBuf > 0 {
u.dcMu.Lock()
dc := u.dc
u.dcMu.Unlock()
if dc != nil {
for dc.BufferedAmount() > u.maxDCBuf {
time.Sleep(5 * time.Millisecond)
}
}
}
n, err := conn.Read(buf)
if n > 0 {
u.sendDCFrame(connID, tunnel.MsgData, buf[:n])
+11 -1
View File
@@ -21,6 +21,7 @@ func main() {
displayName := flag.String("name", "Headless", "display name in the room")
resources := flag.String("resources", "default", "resource mode: default, moderate, unlimited, custom")
customReadBuf := flag.Int("read-buf", 0, "DC read buffer size in bytes, used with -resources custom")
customMaxDCBuf := flag.Int("max-dc-buf", 0, "max DC buffer size in bytes, used with -resources custom")
customMemLimit := flag.Int64("mem-limit", 0, "memory limit in bytes, used with -resources custom")
writeFile := flag.String("write-file", "", "path to file where active room id is appended")
upstreamSocks := flag.String("upstream-socks", "", "route tunneled egress through this SOCKS5 proxy (host:port), e.g. a local VPN client")
@@ -35,22 +36,30 @@ func main() {
common.AllowLoopbackDst = *allowLoopback
var readBuf int
var maxDCBuf uint64
var memLimit int64
switch *resources {
case "moderate":
readBuf = 16384
maxDCBuf = 1 << 20
memLimit = 64 << 20
case "default":
readBuf = common.DCBufSize
maxDCBuf = common.MaxDCBufSize
memLimit = 128 << 20
case "unlimited":
readBuf = common.RTPBufSize
maxDCBuf = 8 << 20
memLimit = 256 << 20
case "custom":
readBuf = *customReadBuf
if readBuf == 0 {
readBuf = common.RTPBufSize
}
maxDCBuf = uint64(*customMaxDCBuf)
if maxDCBuf == 0 {
maxDCBuf = 8 << 20
}
memLimit = *customMemLimit
if memLimit == 0 {
memLimit = 256 << 20
@@ -61,7 +70,7 @@ func main() {
if memLimit > 0 {
debug.SetMemoryLimit(memLimit)
}
log.Printf("[config] resources=%s read-buf=%d mem-limit=%d", *resources, readBuf, memLimit)
log.Printf("[config] resources=%s read-buf=%d max-dc-buf=%d mem-limit=%d", *resources, readBuf, maxDCBuf, memLimit)
if *cookiesPath == "" {
log.Fatalf("[auth] --cookies is required")
@@ -108,6 +117,7 @@ func main() {
RoomID: roomID,
AccessToken: access,
ReadBuf: readBuf,
MaxDCBuf: maxDCBuf,
})
sess.OnConnected = func(tun tunnel.DataTunnel) {
if activeBridge != nil {