Skip to content

Commit

Permalink
Register response receiver before sending request. (#557)
Browse files Browse the repository at this point in the history
Just in case very low latency links get the response before registering.
  • Loading branch information
boks1971 authored Nov 13, 2024
1 parent 01419b0 commit 9fe0702
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions localparticipant.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (p *LocalParticipant) PublishTrack(track webrtc.TrackLocal, opts *TrackPubl
return nil, ErrNoPeerConnection
}

pubChan := make(chan *livekit.TrackPublishedResponse, 1)
p.engine.RegisterTrackPublishedListener(track.ID(), pubChan)
defer p.engine.UnregisterTrackPublishedListener(track.ID())

pub := NewLocalTrackPublication(kind, track, *opts, p.engine.client)
pub.onMuteChanged = p.onTrackMuted

Expand Down Expand Up @@ -107,19 +111,14 @@ func (p *LocalParticipant) PublishTrack(track webrtc.TrackLocal, opts *TrackPubl

publisher.Negotiate()

pubChan := make(chan *livekit.TrackPublishedResponse, 1)
p.engine.RegisterTrackPublishedListener(track.ID(), pubChan)

var pubRes *livekit.TrackPublishedResponse
select {
case pubRes = <-pubChan:
break
case <-time.After(trackPublishTimeout):
p.engine.UnregisterTrackPublishedListener(track.ID())
return nil, ErrTrackPublishTimeout
}

p.engine.UnregisterTrackPublishedListener(track.ID())
pub.updateInfo(pubRes.Track)
p.addPublication(pub)

Expand Down Expand Up @@ -163,6 +162,10 @@ func (p *LocalParticipant) PublishSimulcastTrack(tracks []*LocalTrack, opts *Tra

mainTrack := tracksCopy[len(tracksCopy)-1]

pubChan := make(chan *livekit.TrackPublishedResponse, 1)
p.engine.RegisterTrackPublishedListener(mainTrack.ID(), pubChan)
defer p.engine.UnregisterTrackPublishedListener(mainTrack.ID())

pub := NewLocalTrackPublication(KindFromRTPType(mainTrack.Kind()), nil, *opts, p.engine.client)
pub.onMuteChanged = p.onTrackMuted

Expand Down Expand Up @@ -193,19 +196,14 @@ func (p *LocalParticipant) PublishSimulcastTrack(tracks []*LocalTrack, opts *Tra
return nil, err
}

pubChan := make(chan *livekit.TrackPublishedResponse, 1)
p.engine.RegisterTrackPublishedListener(mainTrack.ID(), pubChan)

var pubRes *livekit.TrackPublishedResponse
select {
case pubRes = <-pubChan:
break
case <-time.After(trackPublishTimeout):
p.engine.UnregisterTrackPublishedListener(mainTrack.ID())
return nil, ErrTrackPublishTimeout
}

p.engine.UnregisterTrackPublishedListener(mainTrack.ID())
publisher, ok := p.engine.Publisher()
if !ok {
return nil, ErrNoPeerConnection
Expand Down

0 comments on commit 9fe0702

Please sign in to comment.