bring kcp to tm, fix disconnections
This commit is contained in:
1 parent
515bb65cbf
commit
5f4d0636fa
10 files changed
+195
-11
No files matched your search
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -51,6 +51,7 @@ func main() {
|
||||
resources := flag.String("resources", "default", "resource mode: moderate, default, unlimited")
|
||||
vp8FPS := flag.Int("vp8-fps", 24, "VP8 frame rate")
|
||||
vp8Batch := flag.Int("vp8-batch", 30, "VP8 batch multiplier")
|
||||
reliable := flag.Bool("reliable", false, "wrap the video tunnel with KCP reliability")
|
||||
debugFlag := flag.Bool("debug", false, "verbose debug logging")
|
||||
flag.Parse()
|
||||
common.Debug = *debugFlag
|
||||
@@ -104,11 +105,13 @@ func main() {
|
||||
DisplayName string `json:"displayName"`
|
||||
VP8FPS int `json:"vp8Fps"`
|
||||
VP8Batch int `json:"vp8Batch"`
|
||||
Reliable bool `json:"reliable"`
|
||||
}{
|
||||
JoinLink: strings.TrimSpace(*tmLink),
|
||||
DisplayName: *displayName,
|
||||
VP8FPS: *vp8FPS,
|
||||
VP8Batch: *vp8Batch,
|
||||
Reliable: *reliable,
|
||||
})
|
||||
|
||||
go inner.RunWithParams(string(params))
|
||||
|
||||
@@ -496,7 +496,7 @@ func (b *Bridge) handleMessage(raw []byte) {
|
||||
}
|
||||
b.mu.Unlock()
|
||||
if needRebind {
|
||||
go b.forceReconnect("slot binding killed")
|
||||
log.Printf("[bind] slot kill/vanish observed - ignoring (tunnel data path is independent of slot binding)")
|
||||
}
|
||||
b.ack(uid)
|
||||
return
|
||||
@@ -709,7 +709,7 @@ func (b *Bridge) initRelay() {
|
||||
relay.OnPubReady = func() {
|
||||
log.Printf("[relay] pub PC connected")
|
||||
}
|
||||
relay.OnConnected = func(tun *tunnel.VP8DataTunnel) {
|
||||
relay.OnConnected = func(tun tunnel.DataTunnel) {
|
||||
if b.activeBridge != nil {
|
||||
b.activeBridge.Reset()
|
||||
}
|
||||
|
||||
@@ -24,13 +24,14 @@ type SFURelay struct {
|
||||
sampleTrack *webrtc.TrackLocalStaticSample
|
||||
tun *tunnel.VP8DataTunnel
|
||||
obf *tunnel.TunnelObfuscator
|
||||
OnConnected func(*tunnel.VP8DataTunnel)
|
||||
OnConnected func(tunnel.DataTunnel)
|
||||
OnPubReady func()
|
||||
OnPeerRestart func()
|
||||
OnPubICE func(*webrtc.ICECandidate)
|
||||
OnSubICE func(*webrtc.ICECandidate)
|
||||
|
||||
readBufSize int
|
||||
tunFired bool
|
||||
}
|
||||
|
||||
func (r *SFURelay) SetObfuscator(o *tunnel.TunnelObfuscator) { r.obf = o }
|
||||
@@ -59,7 +60,10 @@ func (r *SFURelay) Init(iceServers []webrtc.ICEServer) error {
|
||||
"audio", "tunnel-audio",
|
||||
)
|
||||
pubPC.AddTransceiverFromTrack(audioTrack, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly})
|
||||
pubPC.AddTransceiverFromTrack(sampleTrack, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly})
|
||||
videoTr, _ := pubPC.AddTransceiverFromTrack(sampleTrack, webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly})
|
||||
if videoTr != nil {
|
||||
go tunnel.DrainSenderRTCP(videoTr.Sender())
|
||||
}
|
||||
|
||||
pubPC.OnICECandidate(func(cand *webrtc.ICECandidate) {
|
||||
if cand == nil || r.OnPubICE == nil {
|
||||
@@ -75,9 +79,9 @@ func (r *SFURelay) Init(iceServers []webrtc.ICEServer) error {
|
||||
log.Println("[relay] starting VP8 publish tunnel on pub PC connected")
|
||||
r.tun = tunnel.NewVP8DataTunnel(r.sampleTrack, r.obf, log.Printf)
|
||||
r.tun.Start(0, 0)
|
||||
if r.OnConnected != nil {
|
||||
r.OnConnected(r.tun)
|
||||
}
|
||||
r.tunFired = false
|
||||
mt := tunnel.NewMultiTrackTunnel([]*tunnel.VP8DataTunnel{r.tun})
|
||||
mt.SetOnData(func(payload []byte) { r.activate(mt, payload) })
|
||||
}
|
||||
if r.OnPubReady != nil {
|
||||
r.OnPubReady()
|
||||
@@ -112,6 +116,35 @@ func (r *SFURelay) Init(iceServers []webrtc.ICEServer) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SFURelay) activate(mt *tunnel.MultiTrackTunnel, payload []byte) {
|
||||
r.mu.Lock()
|
||||
if r.tunFired {
|
||||
r.mu.Unlock()
|
||||
return
|
||||
}
|
||||
r.tunFired = true
|
||||
r.mu.Unlock()
|
||||
|
||||
var delivered tunnel.DataTunnel = mt
|
||||
useKCP := false
|
||||
if !tunnel.LooksLikeRelayFrame(payload) {
|
||||
delivered = tunnel.NewMultiTrackKCPTunnel(mt, log.Printf)
|
||||
useKCP = true
|
||||
log.Println("[relay] per-track kcp reliability active over video tunnel")
|
||||
}
|
||||
log.Printf("[relay] auto-detected active tunnel: %T", delivered)
|
||||
if r.OnConnected != nil {
|
||||
r.OnConnected(delivered)
|
||||
}
|
||||
if useKCP {
|
||||
if kcptun, ok := delivered.(*tunnel.MultiTrackKCPTunnel); ok {
|
||||
kcptun.InjectSegment(payload)
|
||||
}
|
||||
} else {
|
||||
mt.DeliverData(payload)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SFURelay) CreatePubOffer() (webrtc.SessionDescription, error) {
|
||||
offer, err := r.pubPC.CreateOffer(nil)
|
||||
if err != nil {
|
||||
|
||||
Executable
+116
@@ -0,0 +1,116 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -u
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
CREATOR="$ROOT/headless/telemost/headless-telemost-creator"
|
||||
JOINER="$ROOT/headless/telemost-joiner/headless-telemost-joiner"
|
||||
|
||||
RATES="${RATES:-0 62500 125000 250000 625000}"
|
||||
PHASE_SECS="${PHASE_SECS:-100}"
|
||||
SOCKS_PORT="${SOCKS_PORT:-11080}"
|
||||
SINK_PORT="${SINK_PORT:-19099}"
|
||||
SETTLE_TIMEOUT="${SETTLE_TIMEOUT:-60}"
|
||||
|
||||
if [ $# -lt 2 ]; then
|
||||
echo "Usage: $0 <cookies.json> <telemost-join-link>" >&2
|
||||
exit 2
|
||||
fi
|
||||
COOKIES="$1"
|
||||
TM_LINK="$2"
|
||||
|
||||
[ -x "$CREATOR" ] || { echo "FAIL: $CREATOR not built (run ./build-headless.sh)" >&2; exit 2; }
|
||||
[ -x "$JOINER" ] || { echo "FAIL: $JOINER not built (run ./build-headless.sh)" >&2; exit 2; }
|
||||
[ -f "$COOKIES" ] || { echo "FAIL: cookies not found: $COOKIES" >&2; exit 2; }
|
||||
command -v ncat >/dev/null 2>&1 || { echo "FAIL: ncat not found (brew install nmap)" >&2; exit 2; }
|
||||
|
||||
CREATOR_LOG=$(mktemp -t tm-reap-creator.XXXXXX.log)
|
||||
JOINER_LOG=$(mktemp -t tm-reap-joiner.XXXXXX.log)
|
||||
C_PID=""; J_PID=""; SINK_PID=""
|
||||
|
||||
cleanup() {
|
||||
[ -n "$SINK_PID" ] && kill "$SINK_PID" 2>/dev/null
|
||||
[ -n "$J_PID" ] && kill "$J_PID" 2>/dev/null
|
||||
[ -n "$C_PID" ] && kill "$C_PID" 2>/dev/null
|
||||
wait 2>/dev/null
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
echo "room: $TM_LINK"
|
||||
echo "rates: $RATES phase: ${PHASE_SECS}s socks: $SOCKS_PORT sink: $SINK_PORT"
|
||||
|
||||
echo "=== creator joins named room ==="
|
||||
"$CREATOR" -cookies "$COOKIES" -tm-link "$TM_LINK" > "$CREATOR_LOG" 2>&1 &
|
||||
C_PID=$!
|
||||
|
||||
echo "=== joiner joins named room, exposes SOCKS5 ==="
|
||||
"$JOINER" -tm-link "$TM_LINK" -socks-port "$SOCKS_PORT" > "$JOINER_LOG" 2>&1 &
|
||||
J_PID=$!
|
||||
|
||||
waited=0
|
||||
while [ "$waited" -lt "$SETTLE_TIMEOUT" ]; do
|
||||
grep -q "TUNNEL CONNECTED" "$JOINER_LOG" && break
|
||||
kill -0 "$C_PID" 2>/dev/null || { echo "FAIL: creator died" >&2; tail -20 "$CREATOR_LOG" >&2; exit 1; }
|
||||
kill -0 "$J_PID" 2>/dev/null || { echo "FAIL: joiner died" >&2; tail -20 "$JOINER_LOG" >&2; exit 1; }
|
||||
sleep 1; waited=$((waited + 1))
|
||||
done
|
||||
grep -q "TUNNEL CONNECTED" "$JOINER_LOG" || { echo "FAIL: no TUNNEL CONNECTED in ${SETTLE_TIMEOUT}s" >&2; tail -25 "$JOINER_LOG" >&2; exit 1; }
|
||||
echo "tunnel up."
|
||||
|
||||
# discard sink reached through the tunnel egress; creator and joiner share this host's loopback
|
||||
ncat -k -l --recv-only 127.0.0.1 "$SINK_PORT" > /dev/null 2>&1 &
|
||||
SINK_PID=$!
|
||||
sleep 1
|
||||
|
||||
# emit ~rate bytes/sec into stdout for dur seconds, in 10 chunks/sec (coarse token bucket, no pv dep)
|
||||
pump_rate() {
|
||||
rate=$1; dur=$2
|
||||
[ "$rate" -le 0 ] && { sleep "$dur"; return; }
|
||||
chunk=$((rate / 10)); [ "$chunk" -lt 1 ] && chunk=1
|
||||
end=$(( $(date +%s) + dur ))
|
||||
while [ "$(date +%s)" -lt "$end" ]; do
|
||||
dd if=/dev/zero bs="$chunk" count=1 2>/dev/null
|
||||
sleep 0.1
|
||||
done
|
||||
}
|
||||
|
||||
# count reap markers currently in both logs
|
||||
reap_count() {
|
||||
c1=$(grep -cE "\[bind\] UNBOUND|forcing reconnect: slot binding killed" "$JOINER_LOG" 2>/dev/null || echo 0)
|
||||
c2=$(grep -cE "\[bind\] UNBOUND|forcing reconnect: slot binding killed" "$CREATOR_LOG" 2>/dev/null || echo 0)
|
||||
echo $((c1 + c2))
|
||||
}
|
||||
|
||||
echo ""
|
||||
echo "=== reap sweep ==="
|
||||
printf "%-10s %-14s %-12s\n" "rate_Bps" "first_reap_s" "reaps_in_phase"
|
||||
RESULTS=""
|
||||
for RATE in $RATES; do
|
||||
base=$(reap_count)
|
||||
start=$(date +%s)
|
||||
# drive the load in the background so we can poll for the reap during the phase
|
||||
( pump_rate "$RATE" "$PHASE_SECS" | ncat --proxy 127.0.0.1:"$SOCKS_PORT" --proxy-type socks5 127.0.0.1 "$SINK_PORT" ) >/dev/null 2>&1 &
|
||||
LOAD_PID=$!
|
||||
first_reap="-"
|
||||
while kill -0 "$LOAD_PID" 2>/dev/null; do
|
||||
if [ "$first_reap" = "-" ] && [ "$(reap_count)" -gt "$base" ]; then
|
||||
first_reap=$(( $(date +%s) - start ))
|
||||
fi
|
||||
sleep 1
|
||||
done
|
||||
wait "$LOAD_PID" 2>/dev/null
|
||||
# one final check after the phase ends
|
||||
if [ "$first_reap" = "-" ] && [ "$(reap_count)" -gt "$base" ]; then
|
||||
first_reap=$(( $(date +%s) - start ))
|
||||
fi
|
||||
total=$(( $(reap_count) - base ))
|
||||
printf "%-10s %-14s %-12s\n" "$RATE" "$first_reap" "$total"
|
||||
RESULTS="$RESULTS$RATE=$first_reap,${total} "
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "--- joiner reap lines ---"
|
||||
grep -nE "\[bind\] (BOUND|UNBOUND)|forcing reconnect" "$JOINER_LOG" | tail -20
|
||||
echo ""
|
||||
echo "summary (rate_Bps=first_reap_s,reaps): $RESULTS"
|
||||
echo "logs: creator=$CREATOR_LOG joiner=$JOINER_LOG"
|
||||
@@ -63,6 +63,7 @@ type TelemostHeadlessJoiner struct {
|
||||
obf *tunnel.TunnelObfuscator
|
||||
vp8FPS int
|
||||
vp8Batch int
|
||||
reliable bool
|
||||
|
||||
httpClient *http.Client
|
||||
instanceID string
|
||||
@@ -109,6 +110,7 @@ func (j *TelemostHeadlessJoiner) RunWithParams(jsonParams string) {
|
||||
DisplayName string `json:"displayName"`
|
||||
VP8FPS int `json:"vp8Fps"`
|
||||
VP8Batch int `json:"vp8Batch"`
|
||||
Reliable bool `json:"reliable"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(jsonParams), ¶ms); err != nil {
|
||||
j.logFn("telemost-joiner: failed to parse params: %v", err)
|
||||
@@ -129,6 +131,7 @@ func (j *TelemostHeadlessJoiner) RunWithParams(jsonParams string) {
|
||||
j.obf = obf
|
||||
j.vp8FPS = params.VP8FPS
|
||||
j.vp8Batch = params.VP8Batch
|
||||
j.reliable = params.Reliable
|
||||
j.logFn("telemost-joiner: link=%s name=%s vp8Fps=%d vp8Batch=%d localEpoch=0x%08x",
|
||||
j.joinLink, j.displayName, params.VP8FPS, params.VP8Batch, obf.LocalEpoch())
|
||||
|
||||
@@ -433,9 +436,10 @@ func (j *TelemostHeadlessJoiner) initPC() {
|
||||
|
||||
subPC.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
||||
j.logFn("telemost-joiner: sub PC state: %s", state.String())
|
||||
if state == webrtc.PeerConnectionStateFailed {
|
||||
if state == webrtc.PeerConnectionStateFailed && !j.isClosed() {
|
||||
j.logFn("telemost-joiner: ERROR: subscriber connection failed")
|
||||
j.Status.EmitStatusError("subscriber connection failed")
|
||||
go j.forceReconnect("subscriber connection failed")
|
||||
}
|
||||
})
|
||||
|
||||
@@ -466,6 +470,11 @@ func (j *TelemostHeadlessJoiner) initPC() {
|
||||
|
||||
pubPC.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
|
||||
j.logFn("telemost-joiner: pub PC state: %s", state.String())
|
||||
if state == webrtc.PeerConnectionStateFailed && !j.isClosed() {
|
||||
j.logFn("telemost-joiner: ERROR: publisher connection failed")
|
||||
j.Status.EmitStatusError("publisher connection failed")
|
||||
go j.forceReconnect("publisher connection failed")
|
||||
}
|
||||
if state == webrtc.PeerConnectionStateConnected && j.vp8tunnel == nil {
|
||||
j.reconnectAttempt.Store(0)
|
||||
j.logFn("telemost-joiner: === VP8 TUNNEL CONNECTED ===")
|
||||
@@ -473,14 +482,20 @@ func (j *TelemostHeadlessJoiner) initPC() {
|
||||
j.vp8tunnel = tunnel.NewVP8DataTunnel(j.sampleTrack, j.obf, j.logFn)
|
||||
vp8tun := j.vp8tunnel
|
||||
vp8tun.Start(j.vp8FPS, j.vp8Batch)
|
||||
var active tunnel.DataTunnel = vp8tun
|
||||
if j.reliable {
|
||||
mt := tunnel.NewMultiTrackTunnel([]*tunnel.VP8DataTunnel{vp8tun})
|
||||
active = tunnel.NewMultiTrackKCPTunnel(mt, j.logFn)
|
||||
j.logFn("telemost-joiner: per-track kcp reliability active over video tunnel")
|
||||
}
|
||||
if !j.configAck.acknowledged() {
|
||||
acked, cancel := j.configAck.arm()
|
||||
go sendVP8ConfigUntilAcked(acked, cancel, j.stopCh, vp8tun,
|
||||
go sendVP8ConfigUntilAcked(acked, cancel, j.stopCh, active,
|
||||
vp8tun.FPS(), vp8tun.Batch(), 1, j.logFn, "telemost-joiner")
|
||||
j.logFn("telemost-joiner: pushed vp8 config to creator fps=%d batch=%d", vp8tun.FPS(), vp8tun.Batch())
|
||||
}
|
||||
if j.OnConnected != nil {
|
||||
j.OnConnected(j.vp8tunnel)
|
||||
j.OnConnected(active)
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -801,7 +816,7 @@ func (j *TelemostHeadlessJoiner) handleMessage(raw []byte) {
|
||||
}
|
||||
j.boundMu.Unlock()
|
||||
if needRebind {
|
||||
go j.forceReconnect("slot binding killed")
|
||||
j.logFn("telemost-joiner: slot kill/vanish observed - ignoring (tunnel data path is independent of slot binding)")
|
||||
}
|
||||
j.ack(uid)
|
||||
return
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/pion/rtp/codecs"
|
||||
"github.com/pion/webrtc/v4"
|
||||
"whitelist-bypass/relay/common"
|
||||
"whitelist-bypass/relay/tunnel"
|
||||
)
|
||||
|
||||
type SignalingMessage struct {
|
||||
@@ -156,6 +157,7 @@ func AddTunnelTracks(pc *webrtc.PeerConnection, logFn func(string, ...any), pref
|
||||
logFn("%s: AddTrack audio: sender=%v err=%v", prefix, audioSender != nil, audioErr)
|
||||
logFn("%s: AddTrack video: sender=%v err=%v", prefix, videoSender != nil, videoErr)
|
||||
logFn("%s: senders count: %d", prefix, len(pc.GetSenders()))
|
||||
go tunnel.DrainSenderRTCP(videoSender)
|
||||
return sampleTrack
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package tunnel
|
||||
|
||||
import "github.com/pion/webrtc/v4"
|
||||
|
||||
func DrainSenderRTCP(sender *webrtc.RTPSender) {
|
||||
if sender == nil {
|
||||
return
|
||||
}
|
||||
buf := make([]byte, 1500)
|
||||
for {
|
||||
if _, _, err := sender.Read(buf); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user