Skip to content

Commit

Permalink
Reuse passive created sendonly transceiver
Browse files Browse the repository at this point in the history
When peerconnection create a transceiver by remote's recvonly
offer, the transceiver will have nil sender but can't be reused
since its currentDirection is set to sendonly. This pr change
the currentDirection to inactive so it can be reused by AddTrack.
  • Loading branch information
cnderrauber authored and jeremija committed Jan 31, 2023
1 parent 5b41ed6 commit 3c802f7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
14 changes: 8 additions & 6 deletions peerconnection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,16 +1270,18 @@ func setRTPTransceiverCurrentDirection(answer *SessionDescription, currentTransc
case RTPTransceiverDirectionSendonly:
direction = RTPTransceiverDirectionRecvonly
case RTPTransceiverDirectionRecvonly:
// Pion will answer recvonly with a offer recvonly transceiver, so we should
// not change the direction to sendonly if we are the offerer, otherwise this
// tranceiver can't be reuse for AddTrack
if t.Direction() != RTPTransceiverDirectionRecvonly {
direction = RTPTransceiverDirectionSendonly
}
direction = RTPTransceiverDirectionSendonly
default:
}
}

// If a transceiver is created by applying a remote description that has recvonly transceiver,
// it will have no sender. In this case, the transceiver's current direction is set to inactive so
// that the transceiver can be reused by next AddTrack.
if direction == RTPTransceiverDirectionSendonly && t.Sender() == nil {
direction = RTPTransceiverDirectionInactive
}

t.setCurrentDirection(direction)
}
return nil
Expand Down
52 changes: 52 additions & 0 deletions peerconnection_renegotiation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,3 +1233,55 @@ func TestPeerConnection_Renegotiation_MidConflict(t *testing.T) {
assert.NoError(t, offerPC.Close())
assert.NoError(t, answerPC.Close())
}

func TestPeerConnection_Regegotiation_AnswerAddsTrack(t *testing.T) {
lim := test.TimeOut(time.Second * 30)
defer lim.Stop()

report := test.CheckRoutines(t)
defer report()

pcOffer, pcAnswer, err := newPair()
if err != nil {
t.Fatal(err)
}

tracksCh := make(chan *TrackRemote)
pcOffer.OnTrack(func(track *TrackRemote, r *RTPReceiver) {
tracksCh <- track
for {
if _, _, readErr := track.ReadRTP(); errors.Is(readErr, io.EOF) {
return
}
}
})

vp8Track, err := NewTrackLocalStaticSample(RTPCodecCapability{MimeType: MimeTypeVP8}, "foo", "bar")
assert.NoError(t, err)
assert.NoError(t, signalPair(pcOffer, pcAnswer))

_, err = pcOffer.AddTransceiverFromKind(RTPCodecTypeVideo, RtpTransceiverInit{
Direction: RTPTransceiverDirectionRecvonly,
})
assert.NoError(t, err)
assert.NoError(t, signalPair(pcOffer, pcAnswer))

_, err = pcAnswer.AddTransceiverFromKind(RTPCodecTypeVideo, RtpTransceiverInit{
Direction: RTPTransceiverDirectionSendonly,
})
assert.NoError(t, err)

assert.NoError(t, err)
_, err = pcAnswer.AddTrack(vp8Track)
assert.NoError(t, err)
assert.NoError(t, signalPair(pcOffer, pcAnswer))

ctx, cancel := context.WithCancel(context.Background())

go sendVideoUntilDone(ctx.Done(), t, []*TrackLocalStaticSample{vp8Track})

<-tracksCh
cancel()

closePairNow(t, pcOffer, pcAnswer)
}

0 comments on commit 3c802f7

Please sign in to comment.