package tunnel import ( "sync" "sync/atomic" "time" "github.com/pion/webrtc/v4" "github.com/pion/webrtc/v4/pkg/media" ) const ( defaultVP8FPS = 24 defaultVP8Batch = 30 keepaliveIdlePeriod = 100 * time.Millisecond sendQueueDepth = 128 ) type VP8DataTunnel struct { track *webrtc.TrackLocalStaticSample logFn func(string, ...any) obf *TunnelObfuscator stopCh chan struct{} sendQueue chan []byte cfgChan chan struct{} stopOnce sync.Once running atomic.Bool cfgMu sync.Mutex fps int batch int sentFrames atomic.Uint64 recvFrames atomic.Uint64 keepaliveFrames atomic.Uint64 OnData func([]byte) OnClose func() OnPeerRestart func() } func (t *VP8DataTunnel) SetOnData(fn func([]byte)) { t.OnData = fn } func (t *VP8DataTunnel) SetOnClose(fn func()) { t.OnClose = fn } func (t *VP8DataTunnel) SetOnPeerRestart(fn func()) { t.OnPeerRestart = fn } func NewVP8DataTunnel(track *webrtc.TrackLocalStaticSample, obf *TunnelObfuscator, logFn func(string, ...any)) *VP8DataTunnel { return NewVP8DataTunnelWithQueue(track, obf, logFn, sendQueueDepth) } func NewVP8DataTunnelWithQueue(track *webrtc.TrackLocalStaticSample, obf *TunnelObfuscator, logFn func(string, ...any), queueDepth int) *VP8DataTunnel { if queueDepth < sendQueueDepth { queueDepth = sendQueueDepth } return &VP8DataTunnel{ track: track, obf: obf, logFn: logFn, stopCh: make(chan struct{}), sendQueue: make(chan []byte, queueDepth), cfgChan: make(chan struct{}, 1), fps: defaultVP8FPS, batch: defaultVP8Batch, } } func (t *VP8DataTunnel) Reconfigure(fps, batch int) { if fps <= 0 && batch <= 0 { return } t.cfgMu.Lock() changed := false if fps > 0 && t.fps != fps { t.fps = fps changed = true } if batch > 0 && t.batch != batch { t.batch = batch changed = true } newFPS, newBatch := t.fps, t.batch t.cfgMu.Unlock() if !changed { return } t.logFn("vp8tunnel: reconfigure fps=%d batch=%d", newFPS, newBatch) select { case t.cfgChan <- struct{}{}: default: } } func (t *VP8DataTunnel) FPS() int { t.cfgMu.Lock() defer t.cfgMu.Unlock() return t.fps } func (t *VP8DataTunnel) Batch() int { t.cfgMu.Lock() defer t.cfgMu.Unlock() return t.batch } func (t *VP8DataTunnel) SendData(data []byte) { if len(data) == 0 { return } select { case t.sendQueue <- data: case <-t.stopCh: } } func (t *VP8DataTunnel) TrySendData(data []byte) bool { if len(data) == 0 { return true } select { case t.sendQueue <- data: return true case <-t.stopCh: return false default: return false } } func (t *VP8DataTunnel) Start(fps, batch int) { t.cfgMu.Lock() if fps > 0 { t.fps = fps } if batch > 0 { t.batch = batch } t.cfgMu.Unlock() if !t.running.CompareAndSwap(false, true) { return } go t.writerLoop() } func (t *VP8DataTunnel) Stop() { if !t.running.CompareAndSwap(true, false) { return } t.stopOnce.Do(func() { close(t.stopCh) }) if t.OnClose != nil { t.OnClose() } } func (t *VP8DataTunnel) currentIntervals() (sampleInterval time.Duration, keepaliveEvery, fps, batch int) { t.cfgMu.Lock() fps = t.fps batch = t.batch t.cfgMu.Unlock() frameInterval := time.Second / time.Duration(fps) sampleInterval = frameInterval if batch > 1 { sampleInterval = frameInterval / time.Duration(batch) } if sampleInterval <= 0 { sampleInterval = time.Millisecond } keepaliveEvery = int(keepaliveIdlePeriod / sampleInterval) if keepaliveEvery < 1 { keepaliveEvery = 1 } return } func (t *VP8DataTunnel) writerLoop() { for { sampleInterval, keepaliveEvery, fps, batch := t.currentIntervals() t.logFn("vp8tunnel: writer (re)started fps=%d batch=%d sampleInterval=%s keepaliveEvery=%d", fps, batch, sampleInterval, keepaliveEvery) ticker := time.NewTicker(sampleInterval) idleTicks := 0 reconfigure := false for !reconfigure { select { case <-t.stopCh: ticker.Stop() return case <-t.cfgChan: reconfigure = true case <-ticker.C: var sample []byte isKeepalive := false select { case data := <-t.sendQueue: sample = t.obf.EncodeData(data) idleTicks = 0 default: idleTicks++ if idleTicks < keepaliveEvery { continue } idleTicks = 0 sample = t.obf.EncodeKeepalive() isKeepalive = true } if sample == nil { continue } if err := t.track.WriteSample(media.Sample{Data: sample, Duration: sampleInterval}); err != nil { t.logFn("vp8tunnel: WriteSample error: %v", err) continue } n := t.sentFrames.Add(1) if isKeepalive { t.keepaliveFrames.Add(1) } if n <= 5 || n%500 == 0 { keepalives := t.keepaliveFrames.Load() t.logFn("vp8tunnel: sent frame #%d size=%d data=%d keepalive=%d", n, len(sample), n-keepalives, keepalives) } } } ticker.Stop() } } func (t *VP8DataTunnel) HandleFrame(frame []byte) { res := t.obf.Decode(frame) if !res.HasFrame { return } if res.SelfEcho { return } if res.PeerRestart { t.logFn("vp8tunnel: peer restart detected, new epoch=0x%08x", res.PeerEpoch) if t.OnPeerRestart != nil { t.OnPeerRestart() } } if res.Keepalive || len(res.Payload) == 0 { return } n := t.recvFrames.Add(1) if n <= 5 || n%500 == 0 { t.logFn("vp8tunnel: recv frame #%d size=%d", n, len(res.Payload)) } if t.OnData != nil { t.OnData(res.Payload) } }