Get to the point of connecting publisher PC and using it for async signalling (#3822)

* starting signalling DC work

* WIP

* plumbing data channel

* add datachannel message sink file

* mage generate

* clean up
This commit is contained in:
Raja Subramanian
2025-08-01 18:50:28 +05:30
committed by GitHub
parent 5e483e7554
commit db4bc127e8
14 changed files with 359 additions and 60 deletions
+44 -6
View File
@@ -62,8 +62,9 @@ import (
)
const (
LossyDataChannel = "_lossy"
ReliableDataChannel = "_reliable"
LossyDataChannel = "_lossy"
ReliableDataChannel = "_reliable"
SignallingDataChannel = "_signalling"
fastNegotiationFrequency = 10 * time.Millisecond
negotiationFrequency = 150 * time.Millisecond
@@ -204,6 +205,7 @@ type PCTransport struct {
lossyDC *datachannel.DataChannelWriter[*webrtc.DataChannel]
lossyDCOpened bool
unlabeledDataChannels []*datachannel.DataChannelWriter[*webrtc.DataChannel]
signallingDataChannel *datachannel.DataChannelWriter[*webrtc.DataChannel]
iceStartedAt time.Time
iceConnectedAt time.Time
@@ -553,7 +555,8 @@ func (t *PCTransport) createPeerConnection() (cc.BandwidthEstimator, error) {
}
t.pc = pc
if !t.params.UseOneShotSignallingMode && !t.params.SynchronousLocalCandidatesMode {
// SIGNALLING-V2-TODO: have to support both sync and async candidates, so has to be a check at function level
if !t.params.UseOneShotSignallingMode /* SIGNALLING-V2-TODO && !t.params.SynchronousLocalCandidatesMode */ {
// one shot signalling mode gathers all candidates and sends in answer
t.pc.OnICEGatheringStateChange(t.onICEGatheringStateChange)
t.pc.OnICECandidate(t.onICECandidateTrickle)
@@ -806,6 +809,7 @@ func (t *PCTransport) onDataChannel(dc *webrtc.DataChannel) {
t.params.Logger.Debugw(dc.Label() + " data channel open")
var kind livekit.DataPacket_Kind
var isUnlabeled bool
var isSignalling bool
switch dc.Label() {
case ReliableDataChannel:
kind = livekit.DataPacket_RELIABLE
@@ -813,6 +817,10 @@ func (t *PCTransport) onDataChannel(dc *webrtc.DataChannel) {
case LossyDataChannel:
kind = livekit.DataPacket_LOSSY
case SignallingDataChannel:
t.params.Logger.Infow("signalling datachannel added", "label", dc.Label())
isSignalling = true
default:
t.params.Logger.Infow("unlabeled datachannel added", "label", dc.Label())
isUnlabeled = true
@@ -833,6 +841,13 @@ func (t *PCTransport) onDataChannel(dc *webrtc.DataChannel) {
)
t.lock.Unlock()
case isSignalling:
t.lock.Lock()
signallingDataChannel := datachannel.NewDataChannelWriter(dc, rawDC, 0)
t.signallingDataChannel = signallingDataChannel
t.lock.Unlock()
t.params.Handler.OnDataChannelOpenSignalling(signallingDataChannel)
case kind == livekit.DataPacket_RELIABLE:
t.lock.Lock()
if t.reliableDC != nil {
@@ -864,9 +879,14 @@ func (t *PCTransport) onDataChannel(dc *webrtc.DataChannel) {
return
}
if isUnlabeled {
switch {
case isUnlabeled:
t.params.Handler.OnDataMessageUnlabeled(buffer[:n])
} else {
case isSignalling:
t.params.Handler.OnDataMessageSignalling(buffer[:n])
default:
t.params.Handler.OnDataMessage(kind, buffer[:n])
}
}
@@ -874,6 +894,18 @@ func (t *PCTransport) onDataChannel(dc *webrtc.DataChannel) {
t.maybeNotifyFullyEstablished()
})
dc.OnClose(func() {
t.params.Logger.Debugw(dc.Label() + " data channel close")
switch dc.Label() {
case SignallingDataChannel:
t.lock.RLock()
signallingDataChannel := t.signallingDataChannel
t.lock.RUnlock()
t.params.Handler.OnDataChannelCloseSignalling(signallingDataChannel)
}
})
}
func (t *PCTransport) maybeNotifyFullyEstablished() {
@@ -1298,8 +1330,10 @@ func (t *PCTransport) clearConnTimer() {
}
}
// SIGNALLING-V2-TODO: this needs both sync and async support when not in one shot mode,
// cannot use the state `SynchronousLocalCandidatesMode`, needs a flag at function level
func (t *PCTransport) HandleRemoteDescription(sd webrtc.SessionDescription, remoteId uint32) error {
if t.params.UseOneShotSignallingMode || t.params.SynchronousLocalCandidatesMode {
if t.params.UseOneShotSignallingMode /* SIGNALLING-V2-TODO || t.params.SynchronousLocalCandidatesMode */ {
if sd.Type == webrtc.SDPTypeOffer {
remoteOfferId := t.remoteOfferId.Load()
if remoteOfferId != 0 && remoteOfferId != t.localAnswerId.Load() {
@@ -1366,6 +1400,8 @@ func (t *PCTransport) HandleRemoteDescription(sd webrtc.SessionDescription, remo
return nil
}
// SIGNALLING-V2-TODO: use a flag at function level for sync vs async rather
// then state `SynchronousLocalCandidatesMode`
func (t *PCTransport) GetAnswer() (webrtc.SessionDescription, uint32, error) {
if !t.params.UseOneShotSignallingMode && !t.params.SynchronousLocalCandidatesMode {
return webrtc.SessionDescription{}, 0, ErrNotSynchronousLocalCandidatesMode
@@ -1417,6 +1453,8 @@ func (t *PCTransport) GetAnswer() (webrtc.SessionDescription, uint32, error) {
return *cld, answerId, nil
}
// SIGNALLING-V2-TODO: use a flag at function level for sync vs async rather
// then state `SynchronousLocalCandidatesMode`
func (t *PCTransport) GetOffer() (webrtc.SessionDescription, uint32, error) {
if !t.params.SynchronousLocalCandidatesMode {
return webrtc.SessionDescription{}, 0, ErrNotSynchronousLocalCandidatesMode