image.png

AllocationSequence::CreateUDPPorts

image.png

UDPPort::Create

image.png

UDPPort::Init

image.png
socket绑定信号:发送数据包信号,准备发送的信号,以及candidate准备好后的信号。

BasicPortAllocatorSession::AddAllocatedPort

H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\p2p\client\basic_port_allocator.cc
image.png
是AddAllocatedPort。

  1. void BasicPortAllocatorSession::AddAllocatedPort(Port* port,
  2. AllocationSequence* seq,
  3. bool prepare_address) {
  4. RTC_DCHECK_RUN_ON(network_thread_);
  5. if (!port)
  6. return;
  7. RTC_LOG(LS_INFO) << "Adding allocated port for " << content_name();
  8. port->set_content_name(content_name());
  9. port->set_component(component());
  10. port->set_generation(generation());
  11. if (allocator_->proxy().type != rtc::PROXY_NONE)
  12. port->set_proxy(allocator_->user_agent(), allocator_->proxy());
  13. port->set_send_retransmit_count_attribute(
  14. (flags() & PORTALLOCATOR_ENABLE_STUN_RETRANSMIT_ATTRIBUTE) != 0);
  15. PortData data(port, seq);
  16. ports_.push_back(data);
  17. port->SignalCandidateReady.connect(
  18. this, &BasicPortAllocatorSession::OnCandidateReady);
  19. port->SignalCandidateError.connect(
  20. this, &BasicPortAllocatorSession::OnCandidateError);
  21. port->SignalPortComplete.connect(this,
  22. &BasicPortAllocatorSession::OnPortComplete);
  23. port->SubscribePortDestroyed(
  24. [this](PortInterface* port) { OnPortDestroyed(port); });
  25. port->SignalPortError.connect(this, &BasicPortAllocatorSession::OnPortError);
  26. RTC_LOG(LS_INFO) << port->ToString() << ": Added port to allocator";
  27. if (prepare_address)
  28. port->PrepareAddress();
  29. }

UDPPort::PrepareAddress

image.png

AsyncPacketSocket状态

image.png

UDPPort::OnLocalAddressReady

image.png

Port::AddAddress

H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\p2p\base\port.cc
image.png
这里面的foundation是给candidate排序使用的,计算公式是在ice规范里面的。

  1. void Port::AddAddress(const rtc::SocketAddress& address,
  2. const rtc::SocketAddress& base_address,
  3. const rtc::SocketAddress& related_address,
  4. const std::string& protocol,
  5. const std::string& relay_protocol,
  6. const std::string& tcptype,
  7. const std::string& type,
  8. uint32_t type_preference,
  9. uint32_t relay_preference,
  10. const std::string& url,
  11. bool is_final) {
  12. if (protocol == TCP_PROTOCOL_NAME && type == LOCAL_PORT_TYPE) {
  13. RTC_DCHECK(!tcptype.empty());
  14. }
  15. std::string foundation =
  16. ComputeFoundation(type, protocol, relay_protocol, base_address);
  17. Candidate c(component_, protocol, address, 0U, username_fragment(), password_,
  18. type, generation_, foundation, network_->id(), network_cost_);
  19. c.set_priority(
  20. c.GetPriority(type_preference, network_->preference(), relay_preference));
  21. c.set_relay_protocol(relay_protocol);
  22. c.set_tcptype(tcptype);
  23. c.set_network_name(network_->name());
  24. c.set_network_type(network_->type());
  25. c.set_url(url);
  26. c.set_related_address(related_address);
  27. bool pending = MaybeObfuscateAddress(&c, type, is_final);
  28. if (!pending) {
  29. FinishAddingAddress(c, is_final);
  30. }
  31. }

调用 FinishAddingAddress(c, is_final); 将创建好的Candidate传给应用层。