前言


代码分析
SdpOfferAnswerHandler::DoSetLocalDescription
->
JsepTransportController::MaybeStartGathering
SdpOfferAnswerHandler::DoSetLocalDescription {***// MaybeStartGathering needs to be called after informing the observer so that// we don't signal any candidates before signaling that SetLocalDescription// completed.transport_controller()->MaybeStartGathering();}
JsepTransportController::MaybeStartGathering
void JsepTransportController::MaybeStartGathering() {if (!network_thread_->IsCurrent()) {network_thread_->Invoke<void>(RTC_FROM_HERE,[&] { MaybeStartGathering(); });return;}for (auto& dtls : GetDtlsTransports()) {dtls->ice_transport()->MaybeStartGathering();}}
P2PTransportChannel::MaybeStartGathering
void P2PTransportChannel::MaybeStartGathering() {RTC_DCHECK_RUN_ON(network_thread_);// 首先会判断ufrag和pwd是否为空if (ice_parameters_.ufrag.empty() || ice_parameters_.pwd.empty()) {RTC_LOG(LS_ERROR)<< "Cannot gather candidates because ICE parameters are empty"" ufrag: "<< ice_parameters_.ufrag << " pwd: " << ice_parameters_.pwd;return;}// Start gathering if we never started before, or if an ICE restart occurred.// 新启动一个收集。或者重启收集if (allocator_sessions_.empty() ||IceCredentialsChanged(allocator_sessions_.back()->ice_ufrag(),allocator_sessions_.back()->ice_pwd(),ice_parameters_.ufrag, ice_parameters_.pwd)) {***// Time for a new allocator.// 获取一个pooled_session ,连接池std::unique_ptr<PortAllocatorSession> pooled_session =allocator_->TakePooledSession(transport_name(), component(),ice_parameters_.ufrag,ice_parameters_.pwd);if (pooled_session) {AddAllocatorSession(std::move(pooled_session));PortAllocatorSession* raw_pooled_session =allocator_sessions_.back().get();// Process the pooled session's existing candidates/ports, if they exist.OnCandidatesReady(raw_pooled_session,raw_pooled_session->ReadyCandidates());for (PortInterface* port : allocator_sessions_.back()->ReadyPorts()) {OnPortReady(raw_pooled_session, port);}if (allocator_sessions_.back()->CandidatesAllocationDone()) {OnCandidatesAllocationDone(raw_pooled_session);}} else {// 添加一个收集器AddAllocatorSession(allocator_->CreateSession(transport_name(), component(), ice_parameters_.ufrag,ice_parameters_.pwd));allocator_sessions_.back()->StartGettingPorts();}}}
BasicPortAllocatorSession::StartGettingPorts

void BasicPortAllocatorSession::StartGettingPorts() {RTC_DCHECK_RUN_ON(network_thread_);state_ = SessionState::GATHERING;if (!socket_factory_) {owned_socket_factory_.reset(new rtc::BasicPacketSocketFactory(network_thread_));socket_factory_ = owned_socket_factory_.get();}// 向网络线程发送一个任务,根据参数,此时当前类的OnMessage会收到MSG_CONFIG_STARTnetwork_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_START);***}
OnMessage处理线程消息
void BasicPortAllocatorSession::OnMessage(rtc::Message* message) {switch (message->message_id) {case MSG_CONFIG_START:GetPortConfigurations();break;case MSG_CONFIG_READY:OnConfigReady(static_cast<PortConfiguration*>(message->pdata));break;case MSG_ALLOCATE:OnAllocate();break;case MSG_SEQUENCEOBJECTS_CREATED:OnAllocationSequenceObjectsCreated();break;case MSG_CONFIG_STOP:OnConfigStop();break;default:RTC_NOTREACHED();}}
BasicPortAllocatorSession::GetPortConfiguration
void BasicPortAllocatorSession::GetPortConfigurations() {RTC_DCHECK_RUN_ON(network_thread_);PortConfiguration* config =new PortConfiguration(allocator_->stun_servers(), username(), password());for (const RelayServerConfig& turn_server : allocator_->turn_servers()) {config->AddRelay(turn_server);}ConfigReady(config);}void BasicPortAllocatorSession::ConfigReady(PortConfiguration* config) {RTC_DCHECK_RUN_ON(network_thread_);// 向网络线程发送MSG_CONFIG_READY信息network_thread_->Post(RTC_FROM_HERE, this, MSG_CONFIG_READY, config);}void BasicPortAllocatorSession::OnMessage(rtc::Message* message) {switch (message->message_id) {***case MSG_CONFIG_READY:OnConfigReady(static_cast<PortConfiguration*>(message->pdata));break;***}}// Adds a configuration to the list.void BasicPortAllocatorSession::OnConfigReady(PortConfiguration* config) {RTC_DCHECK_RUN_ON(network_thread_);if (config) {configs_.push_back(config);}AllocatePorts();}
