WebRTC candidate

I. Introduction

        Both sides of WebRTC audio and video communication need to know the communication address of the peer to communicate. WebRTC uses ICE to establish a communication connection with the communication peer. A key step in ICE is to collect candidate information. The local end needs to send its own host candidate , NAT-mapped candidates and relay candidates and other information are sent to the peer, and the peer also needs to send candidate information to the local. The two parties perform media connectivity detection, and communication can only proceed after the detection is successful.

2. candidate

        Candidates are also called candidates, which contain a network address information, including host candidates (host candidate), server reflection candidates (srflx candidate), relay candidates (relay candidate), etc. The meanings of various candidates are as follows.

        The host candidate (Local Address) is the IP address and port used locally. For example, through ifconfig/ipconfig, the IP address of the WLAN network card is 192.168.0.105, and port 51417 is ready to be used.

        Server Reflexive Address is the IP address and port used after NAT mapping.

        The relay candidate (Relayed Address) is the IP address and port opened by the TURN server. The TURN server is used to forward the data packet through the TURN server when the peer NAT traversal fails and the P2P communication cannot be established. The TURN server is generally deployed in On a machine with a public IP.

candidate 格式为:a=candidate:{foundation} {component} {protocol} {priority} {ip} {port} typ {type} generation {generation} ufrag {username} network_id {id} network_cost {cost}

Take the following candidate as an example to illustrate the meaning it represents:

a=candidate:1221703924 1 udp 2122260223 192.168.0.105 51417 typ host generation 0 ufrag Q8Wv network-id 1 network-cost 10

typ host means the local candidate, the IP used is 192.168.0.105, the port is 51417, the UDP protocol is used, the priority is 2122260223, the generation means the algebra, the initial value is 0, if the candidate is updated, the generation value will increase and replace the old one candidate.

        The WebRTC example provides a tool that can collect candidates . Through this tool, we can collect Local Address and Server Reflexive Address. If you build a TURN server and add the TURN service address to ICE Servers, you can also collect Relayed Address.

3. WebRTC collection candidate source code analysis

        When the local SDP is generated locally through createOffer, setLocalDescription will be called to set it in the local description information, and candidates will be collected in setLocalDescription.

        As follows, when PeerConnection::DoSetLocalDescription completes ApplyLocalDescription, it will execute transport_controller_->MaybeStartGathering() to start candidate collection.

void PeerConnection::DoSetLocalDescription(
    std::unique_ptr<SessionDescriptionInterface> desc,
    rtc::scoped_refptr<SetSessionDescriptionObserver> observer) {
  // 省略...(详见src/pc/peer_connection.cc)
  error = ApplyLocalDescription(std::move(desc));

  // 省略...

  // MaybeStartGathering needs to be called after posting
  // MSG_SET_SESSIONDESCRIPTION_SUCCESS, so that we don't signal any candidates
  // before signaling that SetLocalDescription completed.
  transport_controller_->MaybeStartGathering();

  // 省略...
}

        If the current thread is not a network thread, send the task of MaybeStartGathering to the network thread, otherwise execute the logic of dtls->ice_transport()->MaybeStartGathering().

        The logic of MaybeStartGathering is as follows. If the current state is the state of not collecting candidates or preparing to restart ICE, enter the state of collecting, and add a session to allocator_sessions_ (the session is a PortAllocatorSession object), and then call the session’s StartGettingPorts.

 

        The driving process after calling StartGettingPorts is: GetPortConfiguarations -> OnConfigReady -> OnAllocate -> OnAllocationSequenceObjectsCreated.

 

 

        GetPortConfigurations first creates the PortConfiguration object config, and then adds stun_servers, turn_servers address information to the config object.

 

        OnConfigReady is to stuff the PortConfiguration object into configs_.

 

 

        DoAllocate is called in OnAllocate, and the most important logic in DoAllocate is to generate an AllocationSequence object for each Network (if you are not familiar with Network objects, you can read this blog first to understand the process of WebRTC collecting network card information).

        After the AllocationSequence object is created using Network, PortConfiguration and sequence_flags in DoAllocate, the Init initialization will be performed for the AllocationSequence object. 

        AllocationSequence::Init calls BasicPacketSocketFactory::CreateUdpSocket to create a socket, which is bound to the IP address corresponding to the Network and an available port selected from the range [min_port, max_port]. The calling process of socket creation is as follows.

         After AllocationSequence::Init is executed, it will call AllocationSequence::Start. This function mainly sends MSG_ALLOCATION_PHASE message to the network thread to drive the creation of UDPPort, TcpPort, StunPort, RelayPort and other objects.

        We take CreateUDPPorts as an example to illustrate how host candidates are collected. For collecting server reflection address candidates, please read this blog , and for collecting relay address candidates, please read this blog .

        The logic of CreateUDPPorts is as follows. First, create a UDPPort object, and then add UDPPort through BasicPortAllocatorSession::AddAllocatedPort for processing.

        BasicPortAllocatorSession::AddAllocatedPort is mainly for port content_name, component and other attributes, and associates some event callback processing functions, and then executes PrepareAddress.

        PrepareAddress calls OnLocalAddressReady, OnLocalAddressReady calls AddAddress to create a candidate.

 

        So far, the local candidates are created. For the collection of server reflection address candidates, please read this blog . For the collection of relay address candidates, please read this blog . After the candidate information is created, the network thread will send MSG_SEQUENCEOBJECTS_CREATED message, Callback OnAllocationSequenceObjectsCreated Candidate creation is complete.

4. References

RFC5245

RFC8445

RFC5766

Guess you like

Origin blog.csdn.net/weixin_38102771/article/details/123193845