create new subscriber datatrack after migration (#600)

* use negotiated data channel in migration case

* create subscriber data track after migration

* fix participant state update

* add participant leave log

* update protocol
This commit is contained in:
cnderrauber
2022-04-07 15:43:24 +08:00
committed by GitHub
parent 93a2730128
commit f74144846b
8 changed files with 100 additions and 91 deletions
+72 -30
View File
@@ -216,28 +216,11 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
if p.SubscriberAsPrimary() {
primaryPC = p.subscriber.pc
secondaryPC = p.publisher.pc
ordered := true
negotiated := p.params.Migration
// also create data channels for subs, this is for legacy clients that do not use subscriber
// as primary channel
p.reliableDCSub, err = primaryPC.CreateDataChannel(ReliableDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
Negotiated: &negotiated,
})
if err != nil {
return nil, err
if !params.Migration {
if err := p.createDataChannelForSubscriberAsPrimary(); err != nil {
return nil, err
}
}
p.reliableDCSub.OnOpen(p.incActiveCounter)
retransmits := uint16(0)
p.lossyDCSub, err = primaryPC.CreateDataChannel(LossyDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
MaxRetransmits: &retransmits,
Negotiated: &negotiated,
})
if err != nil {
return nil, err
}
p.lossyDCSub.OnOpen(p.incActiveCounter)
} else {
p.activeCounter.Add(2)
}
@@ -255,6 +238,59 @@ func NewParticipant(params ParticipantParams) (*ParticipantImpl, error) {
return p, nil
}
func (p *ParticipantImpl) createDataChannelForSubscriberAsPrimary() error {
primaryPC := p.subscriber.pc
ordered := true
// also create data channels for subs, this is for legacy clients that do not use subscriber
// as primary channel
var (
reliableID, lossyID uint16
reliableIDPtr, lossyIDPtr *uint16
)
// for old version migration clients, they don't send subscriber data channel info
// so we need to create data channels with default ID and don't negotiate as client already has
// data channels with default ID.
// for new version migration clients, we create data channels with new ID and negotiate with client
for _, dc := range p.pendingDataChannels {
if dc.Target == livekit.SignalTarget_SUBSCRIBER {
if dc.Label == ReliableDataChannel {
// pion use step 2 for auto generated ID, so we need to add 4 to avoid conflict
reliableID = uint16(dc.Id) + 4
reliableIDPtr = &reliableID
} else if dc.Label == LossyDataChannel {
lossyID = uint16(dc.Id) + 4
lossyIDPtr = &lossyID
}
}
}
var err error
negotiated := p.params.Migration && reliableIDPtr == nil
p.reliableDCSub, err = primaryPC.CreateDataChannel(ReliableDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
ID: reliableIDPtr,
Negotiated: &negotiated,
})
if err != nil {
return err
}
p.reliableDCSub.OnOpen(p.incActiveCounter)
retransmits := uint16(0)
negotiated = p.params.Migration && lossyIDPtr == nil
p.lossyDCSub, err = primaryPC.CreateDataChannel(LossyDataChannel, &webrtc.DataChannelInit{
Ordered: &ordered,
MaxRetransmits: &retransmits,
ID: lossyIDPtr,
Negotiated: &negotiated,
})
if err != nil {
return err
}
p.lossyDCSub.OnOpen(p.incActiveCounter)
return nil
}
func (p *ParticipantImpl) GetLogger() logger.Logger {
return p.params.Logger
}
@@ -486,14 +522,20 @@ func (p *ParticipantImpl) AddTrack(req *livekit.AddTrackRequest) {
})
}
func (p *ParticipantImpl) SetMigrateInfo(mediaTracks []*livekit.TrackPublishedResponse, dataChannels []*livekit.DataChannelInfo) {
func (p *ParticipantImpl) SetMigrateInfo(previousAnswer *webrtc.SessionDescription, mediaTracks []*livekit.TrackPublishedResponse, dataChannels []*livekit.DataChannelInfo) {
p.pendingTracksLock.Lock()
defer p.pendingTracksLock.Unlock()
for _, t := range mediaTracks {
p.pendingTracks[t.GetCid()] = &pendingTrackInfo{t.GetTrack(), true}
}
p.pendingDataChannels = dataChannels
if p.SubscriberAsPrimary() {
if err := p.createDataChannelForSubscriberAsPrimary(); err != nil {
p.params.Logger.Errorw("create data channel failed", err)
}
}
p.pendingTracksLock.Unlock()
p.subscriber.SetPreviousAnswer(previousAnswer)
}
// HandleAnswer handles a client answer response, with subscriber PC, server initiates the
@@ -536,6 +578,7 @@ func (p *ParticipantImpl) Close(sendLeave bool) error {
return nil
}
p.params.Logger.Infow("closing participant", "sendLeave", sendLeave)
// send leave message
if sendLeave {
_ = p.writeMessage(&livekit.SignalResponse{
@@ -589,10 +632,6 @@ func (p *ParticipantImpl) Negotiate() {
}
}
func (p *ParticipantImpl) SetPreviousAnswer(previous *webrtc.SessionDescription) {
p.subscriber.SetPreviousAnswer(previous)
}
func (p *ParticipantImpl) SetMigrateState(s types.MigrateState) {
p.lock.Lock()
preState := p.MigrateState()
@@ -609,7 +648,7 @@ func (p *ParticipantImpl) SetMigrateState(s types.MigrateState) {
}
p.lock.Unlock()
if s == types.MigrateStateComplete {
p.handlePendingDataChannels()
p.handlePendingPublisherDataChannels()
}
if pendingOffer != nil {
_, err := p.HandleOffer(*pendingOffer)
@@ -1555,7 +1594,7 @@ func (p *ParticipantImpl) DebugInfo() map[string]interface{} {
return info
}
func (p *ParticipantImpl) handlePendingDataChannels() {
func (p *ParticipantImpl) handlePendingPublisherDataChannels() {
ordered := true
negotiated := true
for _, ci := range p.pendingDataChannels {
@@ -1563,6 +1602,9 @@ func (p *ParticipantImpl) handlePendingDataChannels() {
dc *webrtc.DataChannel
err error
)
if ci.Target == livekit.SignalTarget_SUBSCRIBER {
continue
}
if ci.Label == LossyDataChannel && p.lossyDC == nil {
retransmits := uint16(0)
id := uint16(ci.GetId())