Skip to content

Commit

Permalink
adjust wait connected
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Totting committed Nov 22, 2023
1 parent b6fa087 commit 3e81d98
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion datachannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestRoomDataChannel(t *testing.T) {

connected := WaitConnected(ctx, []*webrtc.PeerConnection{pc1, pc2})

timeoutConnected, cancelTimeoutConnected := context.WithTimeout(ctx, 30*time.Second)
timeoutConnected, cancelTimeoutConnected := context.WithTimeout(ctx, 40*time.Second)
isConnected := false

select {
Expand Down
37 changes: 24 additions & 13 deletions testhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func CreatePeerPair(ctx context.Context, room *Room, iceServers []webrtc.ICEServ

pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
if state == webrtc.PeerConnectionStateClosed || state == webrtc.PeerConnectionStateFailed {
glog.Info("test: peer connection closed ", peerName)
glog.Info("test: peer connection ", peerName, " stated changed ", state)
if client != nil {
_ = room.StopClient(client.ID())
cancelClient()
Expand Down Expand Up @@ -576,7 +576,13 @@ func CreateDataPair(ctx context.Context, room *Room, iceServers []webrtc.ICEServ
ICEServers: iceServers,
})

pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo, webrtc.RtpTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly})
if _, err := pc.AddTransceiverFromKind(webrtc.RTPCodecTypeVideo, webrtc.RtpTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly}); err != nil {
panic(err)
}

if _, err := pc.AddTransceiverFromKind(webrtc.RTPCodecTypeAudio, webrtc.RtpTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly}); err != nil {
panic(err)
}

pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
if state == webrtc.PeerConnectionStateClosed || state == webrtc.PeerConnectionStateFailed {
Expand Down Expand Up @@ -638,17 +644,6 @@ func WaitConnected(ctx context.Context, peers []*webrtc.PeerConnection) chan boo
connectedCount := 0
ctxx, cancel := context.WithCancel(ctx)

for _, pc := range peers {
pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
if state == webrtc.PeerConnectionStateConnected {
connected <- true
} else if state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed {
waitChan <- false
cancel()
}
})
}

go func() {
defer cancel()

Expand All @@ -667,5 +662,21 @@ func WaitConnected(ctx context.Context, peers []*webrtc.PeerConnection) chan boo
}
}()

for _, pc := range peers {
if pc.ConnectionState() == webrtc.PeerConnectionStateConnected {
connected <- true
} else {
pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
if state == webrtc.PeerConnectionStateConnected {
connected <- true
} else if state == webrtc.PeerConnectionStateFailed || state == webrtc.PeerConnectionStateClosed {
waitChan <- false
cancel()
}
})
}

}

return waitChan
}

0 comments on commit 3e81d98

Please sign in to comment.