问题
I follow some flow who explain how to connect in webrtc. But I m blocked : After I get the sdpOffer, I suppose to call setRemoteDescrisption() and I have a callback of onIceCandidate. But I don't have this callback. I can show piece of my code if you need it.
Thanks for helping
回答1:
First things first, I do not know much about WebRTC on Android, but I imagine that it will be very similar to the web API. I used the standard js in my flow below.
Regarding the onicecandidate-trigger:
the onicecandidate handler is called, when you set your local description, see MDN onicecandidateevent. You need to set a local description to start the ice gathering process. One of the reasons is, that the gathered ice candidates will be added to your local description and if you have no local description to add them to, it wouldn't work.
Regarding your flow (maybe to check):
As for the process of handling the offer-answer-exchange, try doing it like this (take A and B as Peers with seperate RTCPeerConnection-Objects pcA and pcB), check if your flow differs somewhere:
- First, you should set up a handler for incoming ice candidates for A and B, like
signaller.on('ice', candidate => pc.addIceCandidate(candidate))
- Then you should register your track handlers for A and B, like
pcA.ontrack = track => ... (put it as src of your video or whatever)
- add your MediaStreamTracks to the connections
pcA.addTrack(aTrack)
(Starting with A's side...)
- A generates an offer by calling
offer = await pcA.createOffer()
- A sets the generated offer as local description
await pcA.setLocalDescription(offer)
- A sends the generated offer over the signalling channel
signaller.sendTo('B','offer', offer)
, now your ice gathering process starts - A receives generated candidates by the
onicecandidate
-handler.pcA.onicecandidate = e => signaller.sendTo('B','ice', e.candidate)
(Now we jump to B's view of the things)
- B receives the offer and sets it
signaller.on('offer', async offer => { await pcB.setRemoteDescription(offer); // and the following steps 8 to 12 follow here })
- B creates an sdp answer
answer = await pcB.createAnswer()
- B sets the answer as local description
await pcB.setLocalDescription(answer)
- Now, B's ice gathering process starts in parallel and should be handled like in 7.
- B sends the answer to A
signaller.sendTo('A','answer', answer)
(Back to A)
- A receives the answer
signaller.on('answer', async answer => await pcA.setRemoteDescription(answer); });
- Now, the call should be complete, ice candidates exchange and the signaling as ice state should be
stable
orconnected
If this works, have a look at handling glare, this is a good source.
来源:https://stackoverflow.com/questions/58429603/onicecandidate-is-never-call-after-setremotedescription-webrtc-android