fight with topology changes at vk dual track
This commit is contained in:
1 parent
0d787cb0f7
commit
f1ce863405
3 files changed
+84
-22
No files matched your search
+41
-14
@@ -22,6 +22,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const TopologyDirect = "DIRECT"
|
const TopologyDirect = "DIRECT"
|
||||||
|
const maxServerBounces = 5
|
||||||
|
|
||||||
type CallInfo struct {
|
type CallInfo struct {
|
||||||
CallID string
|
CallID string
|
||||||
@@ -85,6 +86,10 @@ type Bridge struct {
|
|||||||
newRelay func() Relay
|
newRelay func() Relay
|
||||||
p2p *P2PHandler
|
p2p *P2PHandler
|
||||||
screenSharing bool
|
screenSharing bool
|
||||||
|
|
||||||
|
serverBounces int
|
||||||
|
suppressScreenshare bool
|
||||||
|
bouncing bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpPost(endpoint string, form url.Values, extraHeaders map[string]string) ([]byte, error) {
|
func httpPost(endpoint string, form url.Values, extraHeaders map[string]string) ([]byte, error) {
|
||||||
@@ -355,6 +360,11 @@ func (b *Bridge) setScreenSharing(enabled bool) {
|
|||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if enabled && b.suppressScreenshare {
|
||||||
|
b.mu.Unlock()
|
||||||
|
log.Println("[vk-ws] screenshare suppressed after SERVER flap, staying single-track DIRECT")
|
||||||
|
return
|
||||||
|
}
|
||||||
b.screenSharing = enabled
|
b.screenSharing = enabled
|
||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
log.Printf("[vk-ws] peer track count change, screenshare=%v", enabled)
|
log.Printf("[vk-ws] peer track count change, screenshare=%v", enabled)
|
||||||
@@ -394,13 +404,8 @@ func (b *Bridge) handleVKMessage(raw []byte) {
|
|||||||
log.Printf("[vk-ws] Topology changed to %s", topo)
|
log.Printf("[vk-ws] Topology changed to %s", topo)
|
||||||
b.topology = topo
|
b.topology = topo
|
||||||
if topo != TopologyDirect {
|
if topo != TopologyDirect {
|
||||||
log.Printf("[vk-ws] SFU not supported, kicking %d peers", len(b.peers))
|
b.bounceForServerTopology("SERVER topology")
|
||||||
for pid := range b.peers {
|
return
|
||||||
b.vkSend("remove-participant", map[string]interface{}{
|
|
||||||
"participantId": pid,
|
|
||||||
"ban": false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case "participant-joined", "participant-added":
|
case "participant-joined", "participant-added":
|
||||||
@@ -408,13 +413,8 @@ func (b *Bridge) handleVKMessage(raw []byte) {
|
|||||||
b.peers[int64(pid)] = struct{}{}
|
b.peers[int64(pid)] = struct{}{}
|
||||||
log.Printf("[vk-ws] Participant %d joined (total: %d)", int64(pid), len(b.peers))
|
log.Printf("[vk-ws] Participant %d joined (total: %d)", int64(pid), len(b.peers))
|
||||||
if b.topology != TopologyDirect {
|
if b.topology != TopologyDirect {
|
||||||
log.Printf("[vk-ws] Kicking peer %d (SFU topology)", int64(pid))
|
b.bounceForServerTopology("participant joined under SERVER")
|
||||||
b.vkSend("remove-participant", map[string]interface{}{
|
return
|
||||||
"participantId": int64(pid),
|
|
||||||
"ban": false,
|
|
||||||
})
|
|
||||||
log.Println("[FATAL] SFU topology is not supported, exiting")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -506,6 +506,32 @@ func (b *Bridge) initRelay() {
|
|||||||
b.p2p.Init()
|
b.p2p.Init()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bridge) bounceForServerTopology(reason string) {
|
||||||
|
b.mu.Lock()
|
||||||
|
if b.bouncing {
|
||||||
|
b.mu.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
b.bouncing = true
|
||||||
|
b.serverBounces++
|
||||||
|
count := b.serverBounces
|
||||||
|
if count > maxServerBounces {
|
||||||
|
b.suppressScreenshare = true
|
||||||
|
}
|
||||||
|
suppress := b.suppressScreenshare
|
||||||
|
ws := b.vkWs
|
||||||
|
b.mu.Unlock()
|
||||||
|
|
||||||
|
if suppress {
|
||||||
|
log.Printf("[vk-ws] %s -> reconnect #%d, suppressing screenshare to settle single-track DIRECT", reason, count)
|
||||||
|
} else {
|
||||||
|
log.Printf("[vk-ws] %s -> manual reconnect #%d to recover DIRECT", reason, count)
|
||||||
|
}
|
||||||
|
if ws != nil {
|
||||||
|
ws.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bridge) readLoop() error {
|
func (b *Bridge) readLoop() error {
|
||||||
for {
|
for {
|
||||||
_, msg, err := b.vkWs.ReadMessage()
|
_, msg, err := b.vkWs.ReadMessage()
|
||||||
@@ -568,6 +594,7 @@ func (b *Bridge) run(callInfo *CallInfo, cookieStr string, cfg VKConfig) {
|
|||||||
|
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
b.screenSharing = false
|
b.screenSharing = false
|
||||||
|
b.bouncing = false
|
||||||
b.mu.Unlock()
|
b.mu.Unlock()
|
||||||
b.sendMediaSettings(false)
|
b.sendMediaSettings(false)
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import (
|
|||||||
type statusEmitter struct{}
|
type statusEmitter struct{}
|
||||||
|
|
||||||
var tunnelLostCh = make(chan struct{}, 1)
|
var tunnelLostCh = make(chan struct{}, 1)
|
||||||
|
var selfHealReconnect bool
|
||||||
|
|
||||||
func (statusEmitter) EmitStatus(status string) {
|
func (statusEmitter) EmitStatus(status string) {
|
||||||
log.Printf("[status] %s", status)
|
log.Printf("[status] %s", status)
|
||||||
@@ -45,7 +46,7 @@ func (statusEmitter) EmitStatus(status string) {
|
|||||||
if strings.HasPrefix(status, "CAPTCHA:") {
|
if strings.HasPrefix(status, "CAPTCHA:") {
|
||||||
fmt.Printf("STATUS:%s\n", status)
|
fmt.Printf("STATUS:%s\n", status)
|
||||||
}
|
}
|
||||||
if status == common.StatusTunnelLost {
|
if status == common.StatusTunnelLost && !selfHealReconnect {
|
||||||
select {
|
select {
|
||||||
case tunnelLostCh <- struct{}{}:
|
case tunnelLostCh <- struct{}{}:
|
||||||
default:
|
default:
|
||||||
@@ -236,12 +237,26 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
bridge *tunnel.RelayBridge
|
||||||
|
bridgeMu sync.Mutex
|
||||||
|
)
|
||||||
onConnected := func(t tunnel.DataTunnel) {
|
onConnected := func(t tunnel.DataTunnel) {
|
||||||
readBuf := common.VP8BufSize
|
readBuf := common.VP8BufSize
|
||||||
if _, ok := t.(*tunnel.DCTunnel); ok {
|
if _, ok := t.(*tunnel.DCTunnel); ok {
|
||||||
readBuf = common.DCBufSize
|
readBuf = common.DCBufSize
|
||||||
}
|
}
|
||||||
bridge := tunnel.NewRelayBridgeWithAuth(t, "joiner", readBuf, log.Printf, *socksUser, *socksPass)
|
bridgeMu.Lock()
|
||||||
|
defer bridgeMu.Unlock()
|
||||||
|
// Reconnect: swap the new tunnel behind the persistent SOCKS
|
||||||
|
// listener instead of binding a second one
|
||||||
|
if bridge != nil {
|
||||||
|
bridge.SwapTunnel(t)
|
||||||
|
log.Printf("[socks] tunnel swapped after reconnect")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
bridge = tunnel.NewRelayBridgeWithAuth(t, "joiner", readBuf, log.Printf, *socksUser, *socksPass)
|
||||||
|
bridge.SetPersistentListener(true)
|
||||||
bridge.MarkReady()
|
bridge.MarkReady()
|
||||||
addr := fmt.Sprintf("%s:%d", *socksHost, *socksPort)
|
addr := fmt.Sprintf("%s:%d", *socksHost, *socksPort)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -265,6 +280,7 @@ func main() {
|
|||||||
runTelemost(*link, *displayName, *vp8FPS, *vp8Batch,
|
runTelemost(*link, *displayName, *vp8FPS, *vp8Batch,
|
||||||
onConnected, addCandidate)
|
onConnected, addCandidate)
|
||||||
case "vk":
|
case "vk":
|
||||||
|
selfHealReconnect = true
|
||||||
runVK(*link, *displayName, *tunnelMode, *vp8FPS, *vp8Batch, *dualTrack,
|
runVK(*link, *displayName, *tunnelMode, *vp8FPS, *vp8Batch, *dualTrack,
|
||||||
onConnected, addCandidate)
|
onConnected, addCandidate)
|
||||||
case "dion", "dn":
|
case "dion", "dn":
|
||||||
|
|||||||
@@ -24,8 +24,11 @@ import (
|
|||||||
const (
|
const (
|
||||||
vkReconnectInitialDelay = time.Second
|
vkReconnectInitialDelay = time.Second
|
||||||
vkReconnectMaxDelay = 16 * time.Second
|
vkReconnectMaxDelay = 16 * time.Second
|
||||||
|
vkMaxReconnectAttempts = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const vkTopologyDirect = "DIRECT"
|
||||||
|
|
||||||
type vkAuthRottenError struct {
|
type vkAuthRottenError struct {
|
||||||
Code string
|
Code string
|
||||||
Msg string
|
Msg string
|
||||||
@@ -155,11 +158,16 @@ func (h *VKHeadlessJoiner) RunWithParams(jsonParams string) {
|
|||||||
if !h.waitBeforeRetry(int(h.reconnectAttempt.Load())) {
|
if !h.waitBeforeRetry(int(h.reconnectAttempt.Load())) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.reconnectAttempt.Add(1)
|
attempt := h.reconnectAttempt.Add(1)
|
||||||
if h.isClosed() {
|
if h.isClosed() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.logFn("headless: reconnect attempt #%d", h.reconnectAttempt.Load())
|
if int(attempt) > vkMaxReconnectAttempts {
|
||||||
|
h.logFn("headless: gave up after %d consecutive reconnect attempts", vkMaxReconnectAttempts)
|
||||||
|
h.Status.EmitStatusError("reconnect attempts exhausted")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.logFn("headless: reconnect attempt #%d", attempt)
|
||||||
h.Status.EmitStatus(common.StatusReconnecting)
|
h.Status.EmitStatus(common.StatusReconnecting)
|
||||||
if err := h.runOnce(); err != nil {
|
if err := h.runOnce(); err != nil {
|
||||||
var authRotten *vkAuthRottenError
|
var authRotten *vkAuthRottenError
|
||||||
@@ -517,16 +525,27 @@ func (h *VKHeadlessJoiner) handleVKMessage(raw []byte) {
|
|||||||
case "topology-changed":
|
case "topology-changed":
|
||||||
topo, _ := msg["topology"].(string)
|
topo, _ := msg["topology"].(string)
|
||||||
h.logFn("headless: topology: %s", topo)
|
h.logFn("headless: topology: %s", topo)
|
||||||
if topo == "server" {
|
if topo != "" && topo != vkTopologyDirect {
|
||||||
h.logFn("headless: ERROR: server topology not supported")
|
h.logFn("headless: %s topology -> closing WS to reconnect and recover DIRECT", topo)
|
||||||
|
h.vkMu.Lock()
|
||||||
|
ws := h.vkWs
|
||||||
|
h.vkMu.Unlock()
|
||||||
|
if ws != nil {
|
||||||
|
ws.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case "participant-joined", "participant-added":
|
case "participant-joined", "participant-added":
|
||||||
h.logFn("headless: <- %s", notif)
|
h.logFn("headless: <- %s", notif)
|
||||||
case "participant-left":
|
case "participant-left":
|
||||||
h.logFn("headless: <- %s", notif)
|
h.logFn("headless: <- %s", notif)
|
||||||
case "hungup":
|
case "hungup":
|
||||||
h.logFn("headless: ERROR: call ended (hungup)")
|
h.logFn("headless: peer hungup -> closing WS to reconnect")
|
||||||
h.Status.EmitStatusError("call ended")
|
h.vkMu.Lock()
|
||||||
|
ws := h.vkWs
|
||||||
|
h.vkMu.Unlock()
|
||||||
|
if ws != nil {
|
||||||
|
ws.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case "response":
|
case "response":
|
||||||
|
|||||||
Reference in new issue
Block a user