Skip to content

Commit

Permalink
Re-use transceivers. (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
boks1971 authored Nov 14, 2024
1 parent 9e20a7d commit 24c29a8
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions localparticipant.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,19 @@ func (p *LocalParticipant) PublishTrack(track webrtc.TrackLocal, opts *TrackPubl
return nil, err
}

// add transceivers
transceiver, err := publisher.PeerConnection().AddTransceiverFromTrack(track, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
})
// add transceivers - re-use if possible, AddTrack will try to re-use.
// NOTE: `AddTrack` technically cannot re-use transceiver if it was ever
// used to send media, i. e. if it was ever in a `sendrecv` or `sendonly`
// direction. But, pion does not enforce that based on browser behaviour
// observed in practice.
sender, err := publisher.PeerConnection().AddTrack(track)
if err != nil {
return nil, err
}

// LocalTrack will consume rtcp packets so we don't need to consume again
_, isSampleTrack := track.(*LocalTrack)
pub.setSender(transceiver.Sender(), !isSampleTrack)
pub.setSender(sender, !isSampleTrack)

publisher.Negotiate()

Expand Down Expand Up @@ -215,13 +217,24 @@ func (p *LocalParticipant) PublishSimulcastTrack(tracks []*LocalTrack, opts *Tra
var sender *webrtc.RTPSender
for idx, st := range tracksCopy {
if idx == 0 {
transceiver, err = publishPC.AddTransceiverFromTrack(st, webrtc.RTPTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
})
// add transceivers - re-use if possible, AddTrack will try to re-use.
// NOTE: `AddTrack` technically cannot re-use transceiver if it was ever
// used to send media, i. e. if it was ever in a `sendrecv` or `sendonly`
// direction. But, pion does not enforce that based on browser behaviour
// observed in practice.
sender, err = publishPC.AddTrack(st)
if err != nil {
return nil, err
}
sender = transceiver.Sender()

// as there is no way to get transceiver from sender, search
for _, tr := range publishPC.GetTransceivers() {
if tr.Sender() == sender {
transceiver = tr
break
}
}

pub.setSender(sender, false)
} else {
if err = sender.AddEncoding(st); err != nil {
Expand Down

0 comments on commit 24c29a8

Please sign in to comment.