When a web site or app using
RTCPeerConnection
receives a new ICE candidate from the remote peer over its signaling channel, it delivers the newly-received candidate to the browser's
ICE
agent by calling
RTCPeerConnection.addIceCandidate()
.
This adds this new remote candidate to the
RTCPeerConnection
's remote description, which describes the state of the remote end of the connection.
若
candidate
parameter is missing or a value of
null
is given when calling
addIceCandidate()
, the added ICE candidate is an "end-of-candidates" indicator. The same is the case if the value of the specified object's
candidate
is either missing or an empty string (
""
), it signals that all remote candidates have been delivered.
The end-of-candidates notification is transmitted to the remote peer using a candidate with an a-line value of
end-of-candidates
.
During negotiation, your app will likely receive many candidates which you'll deliver to the ICE agent in this way, allowing it to build up a list of potential connection methods. This is covered in more detail in the articles WebRTC connectivity and Signaling and video calling .
aPromise = pc.addIceCandidate(candidate);
addIceCandidate(candidate, successCallback, failureCallback);
candidate
可选
An object conforming to the
RTCIceCandidateInit
dictionary, or an
RTCIceCandidate
object; the contents of the object should be constructed from a message received over the signaling channel, describing a newly received ICE candidate that's ready to be delivered to the local ICE agent.
若无
candidate
object is specified, or its value is
null
, an end-of-candidates signal is sent to the remote peer using the
end-of-candidates
a-line, formatted simply like this:
a=end-of-candidates
In older code and documentation, you may see a callback-based version of this function. This has been deprecated and its use is
strongly
discouraged. You should update any existing code to use the
Promise
-based version of
addIceCandidate()
instead. The parameters for this form of
addIceCandidate()
are described below, to aid in updating existing code.
successCallback
A function to be called when the ICE candidate has been successfully added. This function receives no input parameters and doesn't return a value.
failureCallback
DOMException
object describing why failure occurred.
A
Promise
which is fulfilled when the candidate has been successfully added to the remote peer's description by the ICE agent. The promise does not receive any input parameters.
When an error occurs while attempting to add the ICE candidate, the
Promise
returned by this method is rejected, returning one of the errors below as the
名称
attribute in the specified
DOMException
object passed to the rejection handler.
TypeError
sdpMid
and
sdpMLineIndex
are both
null
.
InvalidStateError
RTCPeerConnection
currently has no remote peer established (
remoteDescription
is
null
).
OperationError
sdpMid
为非
null
and doesn't match the media description ID of any media description included within the
remoteDescription
.
sdpMLineIndex
is greater than or equal to the number of media descriptions included in the remote description.
ufrag
doesn't match the
ufrag
field in any of the remote descriptions being considered.
candidate
string are invalid or could not be parsed.
This code snippet shows how to signal ICE candidates across an arbitrary signaling channel.
// This example assumes that the other peer is using a signaling channel as follows:
//
// pc.onicecandidate = event => {
// if (event.candidate) {
// signalingChannel.send(JSON.stringify({ice: event.candidate})); // "ice" is arbitrary
// } else {
// // All ICE candidates have been sent
// }
// }
signalingChannel.onmessage = receivedString => {
const message = JSON.parse(receivedString);
if (message.ice) {
// A typical value of ice here might look something like this:
//
// {candidate: "candidate:0 1 UDP 2122154243 192.168.1.9 53421 typ host", sdpMid: "0", ...}
//
// Pass the whole thing to addIceCandidate:
pc.addIceCandidate(message.ice).catch(e => {
console.log("Failure during addIceCandidate(): " + e.name);
});
} else {
// handle other things you might be signaling, like sdp
}
}
The last candidate to be signaled this way by the remote peer will be a special candidate denoting end-of-candidates. Out of interest, end-of-candidates may be manually indicated as follows:
pc.addIceCandidate({candidate:''});
However, in most cases you won't need to look for this explicitly, since the events driving the
RTCPeerConnection
will deal with it for you, sending the appropriate events.
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCPeerConnection.addIceCandidate()' in that specification. |
候选推荐 | 最初的规范。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
addIceCandidate
|
Chrome
51
|
Edge 15 |
Firefox
22
|
IE No |
Opera
43
|
Safari 11 |
WebView Android
51
|
Chrome Android
51
|
Firefox Android
44
|
Opera Android
43
|
Safari iOS Yes |
Samsung Internet Android
6.0
|
完整支持
不支持
见实现注意事项。
RTCPeerConnection
canTrickleIceCandidates
connectionState
currentLocalDescription
currentRemoteDescription
getDefaultIceServers()
iceConnectionState
iceGatheringState
localDescription
onaddstream
onconnectionstatechange
ondatachannel
onicecandidate
oniceconnectionstatechange
onicegatheringstatechange
onidentityresult
onidpassertionerror
onidpvalidationerror
onnegotiationneeded
onpeeridentity
onremovestream
onsignalingstatechange
ontrack
peerIdentity
pendingLocalDescription
pendingRemoteDescription
remoteDescription
sctp
signalingState
addIceCandidate()
addStream()
addTrack()
close()
createAnswer()
createDataChannel()
createOffer()
generateCertificate()
getConfiguration()
getIdentityAssertion()
getReceivers()
getSenders()
getStats()
getStreamById()
getTransceivers()
removeStream()
removeTrack()
restartIce()
setConfiguration()
setIdentityProvider()
setLocalDescription()
setRemoteDescription()
MediaDevices.getUserMedia()
Navigator.mediaDevices
RTCCertificate
RTCDTMFSender
RTCDTMFToneChangeEvent
RTCDataChannel
RTCDataChannelEvent
RTCDtlsTransport
RTCErrorEvent
RTCIceCandidate
RTCIceTransport
RTCPeerConnectionIceErrorEvent
RTCPeerConnectionIceEvent
RTCRtpReceiver
RTCRtpSender
RTCRtpTransceiver
RTCSctpTransport
RTCSessionDescription
RTCStatsEvent
RTCStatsReport
RTCTrackEvent