Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-use transceivers. #559

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading