xref: /aosp_15_r20/external/webrtc/p2p/g3doc/ice.md (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker# ICE
2*d9f75844SAndroid Build Coastguard Worker
3*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.owner = 'jonaso' %?>
4*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.reviewed = '2021-04-12' %?>
5*d9f75844SAndroid Build Coastguard Worker
6*d9f75844SAndroid Build Coastguard Worker## Overview
7*d9f75844SAndroid Build Coastguard Worker
8*d9f75844SAndroid Build Coastguard WorkerICE ([link](https://developer.mozilla.org/en-US/docs/Glossary/ICE)) provides
9*d9f75844SAndroid Build Coastguard Workerunreliable packet transport between two clients (p2p) or between a client and a
10*d9f75844SAndroid Build Coastguard Workerserver.
11*d9f75844SAndroid Build Coastguard Worker
12*d9f75844SAndroid Build Coastguard WorkerThis documentation provides an overview of how ICE is implemented, i.e how the
13*d9f75844SAndroid Build Coastguard Workerfollowing classes interact.
14*d9f75844SAndroid Build Coastguard Worker
15*d9f75844SAndroid Build Coastguard Worker*   [`cricket::IceTransportInternal`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/ice_transport_internal.h;l=225;drc=8cb97062880b0e0a78f9d578370a01aced81a13f) -
16*d9f75844SAndroid Build Coastguard Worker    is the interface that does ICE (manage ports, candidates, connections to
17*d9f75844SAndroid Build Coastguard Worker    send/receive packets). The interface is implemented by
18*d9f75844SAndroid Build Coastguard Worker    [`cricket::P2PTransportChannel`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/p2p_transport_channel.h;l=103;drc=0ccfbd2de7bc3b237a0f8c30f48666c97b9e5523).
19*d9f75844SAndroid Build Coastguard Worker
20*d9f75844SAndroid Build Coastguard Worker*   [`cricket::PortInterface`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/port_interface.h;l=47;drc=c3a486c41e682cce943f2b20fe987c9421d4b631)
21*d9f75844SAndroid Build Coastguard Worker    Represents a local communication mechanism that can be used to create
22*d9f75844SAndroid Build Coastguard Worker    connections to similar mechanisms of the other client. There are 4
23*d9f75844SAndroid Build Coastguard Worker    implementations of `cricket::PortInterface`
24*d9f75844SAndroid Build Coastguard Worker    [`cricket::UDPPort`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/stun_port.h;l=33;drc=a4d873786f10eedd72de25ad0d94ad7c53c1f68a),
25*d9f75844SAndroid Build Coastguard Worker    [`cricket::StunPort`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/stun_port.h;l=265;drc=a4d873786f10eedd72de25ad0d94ad7c53c1f68a),
26*d9f75844SAndroid Build Coastguard Worker    [`cricket::TcpPort`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/tcp_port.h;l=33;drc=7a284e1614a38286477ed2334ecbdde78e87b79c)
27*d9f75844SAndroid Build Coastguard Worker    and
28*d9f75844SAndroid Build Coastguard Worker    [`cricket::TurnPort`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/turn_port.h;l=44;drc=ffb7603b6025fbd6e79f360d293ab49092bded54).
29*d9f75844SAndroid Build Coastguard Worker    The ports share lots of functionality in a base class,
30*d9f75844SAndroid Build Coastguard Worker    [`cricket::Port`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/port.h;l=187;drc=3ba7beba29c4e542c4a9bffcc5a47d5e911865be).
31*d9f75844SAndroid Build Coastguard Worker
32*d9f75844SAndroid Build Coastguard Worker*   [`cricket::Candidate`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/candidate.h;l=30;drc=10542f21c8e4e2d60b136fab45338f2b1e132dde)
33*d9f75844SAndroid Build Coastguard Worker    represents an address discovered by a `cricket::Port`. A candidate can be
34*d9f75844SAndroid Build Coastguard Worker    local (i.e discovered by a local port) or remote. Remote candidates are
35*d9f75844SAndroid Build Coastguard Worker    transported using signaling, i.e outside of webrtc. There are 4 types of
36*d9f75844SAndroid Build Coastguard Worker    candidates: `local`, `stun`, `prflx` or `relay`
37*d9f75844SAndroid Build Coastguard Worker    ([standard](https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidateType))
38*d9f75844SAndroid Build Coastguard Worker
39*d9f75844SAndroid Build Coastguard Worker*   [`cricket::Connection`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/connection.h)
40*d9f75844SAndroid Build Coastguard Worker    provides the management of a `cricket::CandidatePair`, i.e for sending data
41*d9f75844SAndroid Build Coastguard Worker    between two candidates. It sends STUN Binding requests (aka STUN pings) to
42*d9f75844SAndroid Build Coastguard Worker    verify that packets can traverse back and forth and keep connections alive
43*d9f75844SAndroid Build Coastguard Worker    (both that NAT binding is kept, and that the remote peer still wants the
44*d9f75844SAndroid Build Coastguard Worker    connection to remain open).
45*d9f75844SAndroid Build Coastguard Worker
46*d9f75844SAndroid Build Coastguard Worker*   `cricket::P2PTransportChannel` uses an
47*d9f75844SAndroid Build Coastguard Worker    [`cricket::PortAllocator`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/port_allocator.h;l=335;drc=9438fb3fff97c803d1ead34c0e4f223db168526f)
48*d9f75844SAndroid Build Coastguard Worker    to create ports and discover local candidates. The `cricket::PortAllocator`
49*d9f75844SAndroid Build Coastguard Worker    is implemented by
50*d9f75844SAndroid Build Coastguard Worker    [`cricket::BasicPortAllocator`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/client/basic_port_allocator.h;l=29;drc=e27f3dea8293884701283a54f90f8a429ea99505).
51*d9f75844SAndroid Build Coastguard Worker
52*d9f75844SAndroid Build Coastguard Worker*   `cricket::P2PTransportChannel` uses an
53*d9f75844SAndroid Build Coastguard Worker    [`cricket::IceControllerInterface`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/ice_controller_interface.h;l=73;drc=9438fb3fff97c803d1ead34c0e4f223db168526f)
54*d9f75844SAndroid Build Coastguard Worker    to manage a set of connections. The `cricket::IceControllerInterface`
55*d9f75844SAndroid Build Coastguard Worker    decides which `cricket::Connection` to send data on.
56*d9f75844SAndroid Build Coastguard Worker
57*d9f75844SAndroid Build Coastguard Worker## Connection establishment
58*d9f75844SAndroid Build Coastguard Worker
59*d9f75844SAndroid Build Coastguard WorkerThis section describes a normal sequence of interactions to establish ice state
60*d9f75844SAndroid Build Coastguard Workercompleted
61*d9f75844SAndroid Build Coastguard Worker[ link ](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/ice_transport_internal.h;l=208;drc=9438fb3fff97c803d1ead34c0e4f223db168526f)
62*d9f75844SAndroid Build Coastguard Worker([ standard ](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/iceConnectionState))
63*d9f75844SAndroid Build Coastguard Worker
64*d9f75844SAndroid Build Coastguard WorkerAll of these steps are invoked by interactions with `PeerConnection`.
65*d9f75844SAndroid Build Coastguard Worker
66*d9f75844SAndroid Build Coastguard Worker1.  [`P2PTransportChannel::MaybeStartGathering`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/p2p_transport_channel.cc;l=864;drc=0ccfbd2de7bc3b237a0f8c30f48666c97b9e5523)
67*d9f75844SAndroid Build Coastguard Worker    This function is invoked as part of `PeerConnection::SetLocalDescription`.
68*d9f75844SAndroid Build Coastguard Worker    `P2PTransportChannel` will use the `cricket::PortAllocator` to create a
69*d9f75844SAndroid Build Coastguard Worker    `cricket::PortAllocatorSession`. The `cricket::PortAllocatorSession` will
70*d9f75844SAndroid Build Coastguard Worker    create local ports as configured, and the ports will start gathering
71*d9f75844SAndroid Build Coastguard Worker    candidates.
72*d9f75844SAndroid Build Coastguard Worker
73*d9f75844SAndroid Build Coastguard Worker2.  [`IceTransportInternal::SignalCandidateGathered`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/ice_transport_internal.h;l=293;drc=8cb97062880b0e0a78f9d578370a01aced81a13f)
74*d9f75844SAndroid Build Coastguard Worker    When a port finds a local candidate, it will be added to a list on
75*d9f75844SAndroid Build Coastguard Worker    `cricket::P2PTransportChannel` and signaled to application using
76*d9f75844SAndroid Build Coastguard Worker    `IceTransportInternal::SignalCandidateGathered`. A p2p application can then
77*d9f75844SAndroid Build Coastguard Worker    send them to peer using favorite transport mechanism whereas a client-server
78*d9f75844SAndroid Build Coastguard Worker    application will do nothing.
79*d9f75844SAndroid Build Coastguard Worker
80*d9f75844SAndroid Build Coastguard Worker3.  [`P2PTransportChannel::AddRemoteCandidate`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/p2p_transport_channel.cc;l=1233;drc=0ccfbd2de7bc3b237a0f8c30f48666c97b9e5523)
81*d9f75844SAndroid Build Coastguard Worker    When the application get a remote candidate, it can add it using
82*d9f75844SAndroid Build Coastguard Worker    `PeerConnection::AddRemoteCandidate` (after
83*d9f75844SAndroid Build Coastguard Worker    `PeerConnection::SetRemoteDescription` has been called!), this will trickle
84*d9f75844SAndroid Build Coastguard Worker    down to `P2PTransportChannel::AddRemoteCandidate`. `P2PTransportChannel`
85*d9f75844SAndroid Build Coastguard Worker    will combine the remote candidate with all compatible local candidates to
86*d9f75844SAndroid Build Coastguard Worker    form new `cricket::Connection`(s). Candidates are compatible if it is
87*d9f75844SAndroid Build Coastguard Worker    possible to send/receive data (e.g ipv4 can only send to ipv4, tcp can only
88*d9f75844SAndroid Build Coastguard Worker    connect to tcp etc...) The newly formed `cricket::Connection`(s) will be
89*d9f75844SAndroid Build Coastguard Worker    added to the `cricket::IceController` that will decide which
90*d9f75844SAndroid Build Coastguard Worker    `cricket::Connection` to send STUN ping on.
91*d9f75844SAndroid Build Coastguard Worker
92*d9f75844SAndroid Build Coastguard Worker4.  [`P2PTransportChannel::SignalCandidatePairChanged`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/ice_transport_internal.h;l=310;drc=8cb97062880b0e0a78f9d578370a01aced81a13f)
93*d9f75844SAndroid Build Coastguard Worker    When a remote connection replies to a STUN ping, `cricket::IceController`
94*d9f75844SAndroid Build Coastguard Worker    will instruct `P2PTransportChannel` to use the connection. This is signalled
95*d9f75844SAndroid Build Coastguard Worker    up the stack using `P2PTransportChannel::SignalCandidatePairChanged`. Note
96*d9f75844SAndroid Build Coastguard Worker    that `cricket::IceController` will continue to send STUN pings on the
97*d9f75844SAndroid Build Coastguard Worker    selected connection, as well as other connections.
98*d9f75844SAndroid Build Coastguard Worker
99*d9f75844SAndroid Build Coastguard Worker5.  [`P2PTransportChannel::SignalIceTransportStateChanged`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/ice_transport_internal.h;l=323;drc=8cb97062880b0e0a78f9d578370a01aced81a13f)
100*d9f75844SAndroid Build Coastguard Worker    The initial selection of a connection makes `P2PTransportChannel` signal up
101*d9f75844SAndroid Build Coastguard Worker    stack that state has changed, which may make [`cricket::DtlsTransportInternal`](https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/p2p/base/dtls_transport_internal.h;l=63;drc=653bab6790ac92c513b7cf4cd3ad59039c589a95)
102*d9f75844SAndroid Build Coastguard Worker    initiate a DTLS handshake (depending on the DTLS role).
103