You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is the example code. This is a func to get all candidates, using STUN: stun.qq.com
funcgetCandidates() ([]*webrtc.ICECandidate, error) {
pendingCandidates:=make([]*webrtc.ICECandidate, 0)
// Prepare the configurationconfig:= webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.qq.com"},
},
},
BundlePolicy: webrtc.BundlePolicyMaxBundle,
}
// Create a new RTCPeerConnectionpeerConnection, err:=webrtc.NewPeerConnection(config)
iferr!=nil {
panic(err)
}
// When an ICE candidate is available send to the other Pion instance// the other Pion instance will add this candidate by calling AddICECandidatedoneCandidates:=falsepeerConnection.OnICECandidate(func(c*webrtc.ICECandidate) {
fmt.Printf("OnICECandidate %p\n", c)
ifc==nil {
doneCandidates=truereturn
}
pendingCandidates=append(pendingCandidates, c)
})
// Create an offer to send to the other processoffer, err:=peerConnection.CreateOffer(nil)
iferr!=nil {
panic(err)
}
// Sets the LocalDescription, and starts our UDP listeners// Note: this will start the gathering of ICE candidatesiferr=peerConnection.SetLocalDescription(offer); err!=nil {
panic(err)
}
// Send our offer to the HTTP server listening in the other processpayload, err:=json.Marshal(offer)
iferr!=nil {
panic(err)
}
fmt.Print(string(payload)) // nolint:noctx//peerConnection.Close()// Block to wait ICE detection donefor {
ifdoneCandidates {
break
}
time.Sleep(time.Duration(10) *time.Millisecond)
}
returnpendingCandidates, nil}
What did you expect?
I want to detect the local ip(Linux eth0) and external ip address(router WAN) correctly, but I found 2 issues that maybe improve:
When there is packet loss in UDP WriteTo() (in file util.go, func stunRequest()), the srflx detection will fail. A simple retransmition strategy will work. I just modified the code for 4 retries, it worked perfectly.
Some stun servers are still using with RFC3489, no XORMappedAddress, such as stun.qq.com. I just add some code in util.go: getXORMappedAddr() to check the missing Attribution error and fall back to RFC3489 to get MappedAddress. It works with stun.qq.com.
The stun detection of pion/webrtc has a odd state, sometimes it will run srflx detection twice, for example, my demo code will do it twice. After fixed the 2nd MappedAddress, it seems been fixed. I don't know why.
This is the patched file util.go: getXORMappedAddr(), I don't submit a pull request, the patch is for your reference. It's working.
Environment.
What did you do?
This is the example code. This is a func to get all candidates, using STUN:
stun.qq.com
What did you expect?
I want to detect the local ip(Linux eth0) and external ip address(router WAN) correctly, but I found 2 issues that maybe improve:
WriteTo()
(in fileutil.go
,func stunRequest()
), the srflx detection will fail. A simple retransmition strategy will work. I just modified the code for 4 retries, it worked perfectly.This is the patched file util.go:
getXORMappedAddr()
, I don't submit a pull request, the patch is for your reference. It's working.The text was updated successfully, but these errors were encountered: