H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\p2p\client\basic_port_allocator.cc
AllocationSequence的作用就是用于分配UDPPort和RelayPort,里面创建了Candidate。

AllocationSequence类image.png

AllocationSequence创建时机

image.png
BasicPortAllocatorSession::DoAllocate
image.png

AllocationSequence::Init

image.png

CreateUdpSocket

H:\webrtc-20210315\webrtc-20210315\webrtc\webrtc-checkout\src\p2p\base\basic_packet_socket_factory.cc
AsyncPacketSocket* CreateUdpSocket(const SocketAddress& local_address, uint16_t min_port, uint16_t max_port) override;
image.png

CreateAsyncSocket

AsyncSocket* PhysicalSocketServer::CreateAsyncSocket(int family, int type)
image.png

SocketDispatcher::Create

image.png

PhysicalSocket::Create

image.png

SocketDispatcher::Initialize

  1. bool SocketDispatcher::Initialize() {
  2. RTC_DCHECK(s_ != INVALID_SOCKET);
  3. // Must be a non-blocking
  4. #if defined(WEBRTC_WIN)
  5. u_long argp = 1;
  6. ioctlsocket(s_, FIONBIO, &argp);
  7. #elif defined(WEBRTC_POSIX)
  8. fcntl(s_, F_SETFL, fcntl(s_, F_GETFL, 0) | O_NONBLOCK);
  9. #endif
  10. #if defined(WEBRTC_IOS)
  11. // iOS may kill sockets when the app is moved to the background
  12. // (specifically, if the app doesn't use the "voip" UIBackgroundMode). When
  13. // we attempt to write to such a socket, SIGPIPE will be raised, which by
  14. // default will terminate the process, which we don't want. By specifying
  15. // this socket option, SIGPIPE will be disabled for the socket.
  16. int value = 1;
  17. ::setsockopt(s_, SOL_SOCKET, SO_NOSIGPIPE, &value, sizeof(value));
  18. #endif
  19. ss_->Add(this);
  20. return true;
  21. }

创建Socket的调用栈

image.png

小结

image.png
1、收集candidate
2、BasicPortAllocatorSession::DoAllocate
3、看创建socket的调用堆栈