add dual track support for wbstream's vp8
This commit is contained in:
1 parent
62da620ec0
commit
5b98351e58
36 files changed
+636
-85
No files matched your search
@@ -0,0 +1,173 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type MultiTrackTunnel struct {
|
||||
tunnels []*VP8DataTunnel
|
||||
|
||||
mu sync.Mutex
|
||||
onData func([]byte)
|
||||
onClose func()
|
||||
isClosed bool
|
||||
fps int
|
||||
batch int
|
||||
}
|
||||
|
||||
func NewMultiTrackTunnel(tunnels []*VP8DataTunnel) *MultiTrackTunnel {
|
||||
m := &MultiTrackTunnel{tunnels: tunnels}
|
||||
for i, tun := range tunnels {
|
||||
// Only the cam (index 0) carries the cascade-on-close semantics;
|
||||
// screenshare tracks close independently during partial shrink.
|
||||
m.wireSubTunnel(tun, i == 0)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) wireSubTunnel(tun *VP8DataTunnel, isCamera bool) {
|
||||
tun.SetOnData(func(data []byte) {
|
||||
m.mu.Lock()
|
||||
handler := m.onData
|
||||
m.mu.Unlock()
|
||||
if handler != nil {
|
||||
handler(data)
|
||||
}
|
||||
})
|
||||
if !isCamera {
|
||||
// Screenshare close is a partial shrink; it must not cascade-Stop
|
||||
// the cam writer or notify the parent that the whole tunnel died.
|
||||
return
|
||||
}
|
||||
tun.SetOnClose(func() {
|
||||
m.mu.Lock()
|
||||
if m.isClosed {
|
||||
m.mu.Unlock()
|
||||
return
|
||||
}
|
||||
m.isClosed = true
|
||||
closeHandler := m.onClose
|
||||
subTunnels := m.tunnels
|
||||
m.mu.Unlock()
|
||||
|
||||
for _, t := range subTunnels {
|
||||
t.Stop()
|
||||
}
|
||||
if closeHandler != nil {
|
||||
closeHandler()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) AddSubTunnel(tun *VP8DataTunnel) {
|
||||
m.mu.Lock()
|
||||
if m.isClosed {
|
||||
m.mu.Unlock()
|
||||
tun.Stop()
|
||||
return
|
||||
}
|
||||
m.tunnels = append(m.tunnels, tun)
|
||||
fps := m.fps
|
||||
batch := m.batch
|
||||
m.mu.Unlock()
|
||||
m.wireSubTunnel(tun, false)
|
||||
if fps > 0 && batch > 0 {
|
||||
tun.Start(fps, batch)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) RemoveLastSubTunnel() *VP8DataTunnel {
|
||||
m.mu.Lock()
|
||||
if len(m.tunnels) <= 1 {
|
||||
m.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
last := m.tunnels[len(m.tunnels)-1]
|
||||
m.tunnels = m.tunnels[:len(m.tunnels)-1]
|
||||
m.mu.Unlock()
|
||||
last.Stop()
|
||||
return last
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) SubTunnelCount() int {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return len(m.tunnels)
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) SendData(data []byte) {
|
||||
m.mu.Lock()
|
||||
tunnels := m.tunnels
|
||||
m.mu.Unlock()
|
||||
if len(tunnels) == 0 {
|
||||
return
|
||||
}
|
||||
var connID uint32
|
||||
if len(data) >= 8 {
|
||||
connID = binary.BigEndian.Uint32(data[4:8])
|
||||
}
|
||||
idx := connID % uint32(len(tunnels))
|
||||
tunnels[idx].SendData(data)
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) SetOnData(fn func([]byte)) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.onData = fn
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) SetOnClose(fn func()) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.onClose = fn
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) Reconfigure(fps, batch int) {
|
||||
m.mu.Lock()
|
||||
m.fps = fps
|
||||
m.batch = batch
|
||||
tunnels := m.tunnels
|
||||
m.mu.Unlock()
|
||||
for _, tun := range tunnels {
|
||||
tun.Reconfigure(fps, batch)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) Start(fps, batch int) {
|
||||
m.mu.Lock()
|
||||
m.fps = fps
|
||||
m.batch = batch
|
||||
tunnels := m.tunnels
|
||||
m.mu.Unlock()
|
||||
for _, tun := range tunnels {
|
||||
tun.Start(fps, batch)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) Stop() {
|
||||
m.mu.Lock()
|
||||
if m.isClosed {
|
||||
m.mu.Unlock()
|
||||
return
|
||||
}
|
||||
m.isClosed = true
|
||||
tunnels := m.tunnels
|
||||
m.mu.Unlock()
|
||||
for _, tun := range tunnels {
|
||||
tun.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MultiTrackTunnel) HandleFrame(frame []byte) {
|
||||
m.mu.Lock()
|
||||
var first *VP8DataTunnel
|
||||
if len(m.tunnels) > 0 {
|
||||
first = m.tunnels[0]
|
||||
}
|
||||
m.mu.Unlock()
|
||||
if first != nil {
|
||||
first.HandleFrame(frame)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ const (
|
||||
MsgUDP byte = 0x06
|
||||
MsgUDPReply byte = 0x07
|
||||
MsgConfig byte = 0x08
|
||||
MsgConfigAck byte = 0x09
|
||||
)
|
||||
|
||||
const ControlConnID uint32 = 0
|
||||
@@ -22,32 +23,43 @@ type DataTunnel interface {
|
||||
Reconfigure(fps, batch int)
|
||||
}
|
||||
|
||||
func EncodeVP8Config(fps, batch int) []byte {
|
||||
func EncodeVP8Config(fps, batch, trackCount int) []byte {
|
||||
if fps < 1 {
|
||||
fps = 1
|
||||
}
|
||||
if batch < 1 {
|
||||
batch = 1
|
||||
}
|
||||
if trackCount < 1 {
|
||||
trackCount = 1
|
||||
}
|
||||
if fps > 0xFFFF {
|
||||
fps = 0xFFFF
|
||||
}
|
||||
if batch > 0xFFFF {
|
||||
batch = 0xFFFF
|
||||
}
|
||||
var payload [4]byte
|
||||
if trackCount > 0xFFFF {
|
||||
trackCount = 0xFFFF
|
||||
}
|
||||
var payload [6]byte
|
||||
binary.BigEndian.PutUint16(payload[0:2], uint16(fps))
|
||||
binary.BigEndian.PutUint16(payload[2:4], uint16(batch))
|
||||
binary.BigEndian.PutUint16(payload[4:6], uint16(trackCount))
|
||||
return EncodeFrame(ControlConnID, MsgConfig, payload[:])
|
||||
}
|
||||
|
||||
func DecodeVP8Config(payload []byte) (fps, batch int, ok bool) {
|
||||
func DecodeVP8Config(payload []byte) (fps, batch, trackCount int, ok bool) {
|
||||
if len(payload) < 4 {
|
||||
return 0, 0, false
|
||||
return 0, 0, 0, false
|
||||
}
|
||||
fps = int(binary.BigEndian.Uint16(payload[0:2]))
|
||||
batch = int(binary.BigEndian.Uint16(payload[2:4]))
|
||||
return fps, batch, true
|
||||
trackCount = 1
|
||||
if len(payload) >= 6 {
|
||||
trackCount = int(binary.BigEndian.Uint16(payload[4:6]))
|
||||
}
|
||||
return fps, batch, trackCount, true
|
||||
}
|
||||
|
||||
func EncodeFrame(connID uint32, msgType byte, payload []byte) []byte {
|
||||
|
||||
@@ -37,6 +37,24 @@ type RelayBridge struct {
|
||||
listenerMu sync.Mutex
|
||||
listener net.Listener
|
||||
closed atomic.Bool
|
||||
|
||||
onPeerConfigMu sync.Mutex
|
||||
onPeerConfig func(fps, batch, trackCount int)
|
||||
|
||||
onConfigAckMu sync.Mutex
|
||||
onConfigAck func()
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) SetOnPeerConfig(fn func(fps, batch, trackCount int)) {
|
||||
rb.onPeerConfigMu.Lock()
|
||||
rb.onPeerConfig = fn
|
||||
rb.onPeerConfigMu.Unlock()
|
||||
}
|
||||
|
||||
func (rb *RelayBridge) SetOnConfigAck(fn func()) {
|
||||
rb.onConfigAckMu.Lock()
|
||||
rb.onConfigAck = fn
|
||||
rb.onConfigAckMu.Unlock()
|
||||
}
|
||||
|
||||
func NewRelayBridgeWithAuth(tunnel DataTunnel, mode string, readBuf int, logFn func(string, ...any), socksUser, socksPass string) *RelayBridge {
|
||||
@@ -147,13 +165,31 @@ func (rb *RelayBridge) send(connID uint32, msgType byte, payload []byte) {
|
||||
func (rb *RelayBridge) handleTunnelData(data []byte) {
|
||||
DecodeFrames(data, func(connID uint32, msgType byte, payload []byte) {
|
||||
if connID == ControlConnID && msgType == MsgConfig {
|
||||
fps, batch, ok := DecodeVP8Config(payload)
|
||||
fps, batch, trackCount, ok := DecodeVP8Config(payload)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if rb.mode == "creator" {
|
||||
rb.logFn("relay: peer requested vp8 pacing fps=%d batch=%d", fps, batch)
|
||||
rb.logFn("relay: peer requested vp8 pacing fps=%d batch=%d trackCount=%d", fps, batch, trackCount)
|
||||
rb.currentTunnel().Reconfigure(fps, batch)
|
||||
rb.send(ControlConnID, MsgConfigAck, nil)
|
||||
rb.onPeerConfigMu.Lock()
|
||||
cb := rb.onPeerConfig
|
||||
rb.onPeerConfigMu.Unlock()
|
||||
if cb != nil {
|
||||
cb(fps, batch, trackCount)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
if connID == ControlConnID && msgType == MsgConfigAck {
|
||||
if rb.mode == "joiner" {
|
||||
rb.onConfigAckMu.Lock()
|
||||
cb := rb.onConfigAck
|
||||
rb.onConfigAckMu.Unlock()
|
||||
if cb != nil {
|
||||
cb()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user