mediaSoup源码分析-ICE流程

 

其实很简单,直接放源码吧

inline void WebRtcTransport::OnStunDataReceived(
	  RTC::TransportTuple* tuple, const uint8_t* data, size_t len)
{
	MS_TRACE();

	RTC::StunPacket* packet = RTC::StunPacket::Parse(data, len);

	if (packet == nullptr)
	{
		MS_WARN_DEV("ignoring wrong STUN packet received");

		return;
	}

	// Pass it to the IceServer.
	this->iceServer->ProcessStunPacket(packet, tuple);

	delete packet;
}
// Create a success response.
RTC::StunPacket* response = packet->CreateSuccessResponse();

// Add XOR-MAPPED-ADDRESS.
response->SetXorMappedAddress(tuple->GetRemoteAddress());

// Authenticate the response.
if (this->oldPassword.empty())
	response->Authenticate(this->password);
else
	response->Authenticate(this->oldPassword);

// Send back.
response->Serialize(StunSerializeBuffer);
this->listener->OnIceServerSendStunPacket(this, response, tuple);

void IceServer::HandleTuple(RTC::TransportTuple* tuple, bool hasUseCandidate)
{
	MS_TRACE();

	switch (this->state)
	{
		case IceState::NEW:
		{
			// There should be no tuples.
			MS_ASSERT(
				 this->tuples.empty(), "state is 'new' but there are %zu tuples", this->tuples.size());

			// There shouldn't be a selected tuple.
			MS_ASSERT(this->selectedTuple == nullptr, "state is 'new' but there is selected tuple");

			if (!hasUseCandidate)
			{
				MS_DEBUG_TAG(ice, "transition from state 'new' to 'connected'");

				// Store the tuple.
				auto storedTuple = AddTuple(tuple);

				// Mark it as selected tuple.
				SetSelectedTuple(storedTuple);
				// Update state.
				this->state = IceState::CONNECTED;
				// Notify the listener.
				this->listener->OnIceServerConnected(this);
			}
			else
			{
				MS_DEBUG_TAG(ice, "transition from state 'new' to 'completed'");

				// Store the tuple.
				auto storedTuple = AddTuple(tuple);

				// Mark it as selected tuple.
				SetSelectedTuple(storedTuple);
				// Update state.
				this->state = IceState::COMPLETED;
				// Notify the listener.
				this->listener->OnIceServerCompleted(this);
			}

				break;
		}

		case IceState::DISCONNECTED:
		{
			// There should be no tuples.
			MS_ASSERT(
				  this->tuples.empty(),
				  "state is 'disconnected' but there are %zu tuples",
				  this->tuples.size());

			// There shouldn't be a selected tuple.
			MS_ASSERT(
				  this->selectedTuple == nullptr, "state is 'disconnected' but there is selected tuple");

			if (!hasUseCandidate)
			{
				MS_DEBUG_TAG(ice, "transition from state 'disconnected' to 'connected'");

				// Store the tuple.
				auto storedTuple = AddTuple(tuple);

				// Mark it as selected tuple.
				SetSelectedTuple(storedTuple);
				// Update state.
				this->state = IceState::CONNECTED;
				// Notify the listener.
				this->listener->OnIceServerConnected(this);
			}
			else
			{
				MS_DEBUG_TAG(ice, "transition from state 'disconnected' to 'completed'");

				// Store the tuple.
				auto storedTuple = AddTuple(tuple);

				// Mark it as selected tuple.
				SetSelectedTuple(storedTuple);
				// Update state.
				this->state = IceState::COMPLETED;
				// Notify the listener.
				this->listener->OnIceServerCompleted(this);
			}

			break;
		}

		case IceState::CONNECTED:
			{
				// There should be some tuples.
				MS_ASSERT(!this->tuples.empty(), "state is 'connected' but there are no tuples");

				// There should be a selected tuple.
				MS_ASSERT(
				  this->selectedTuple != nullptr, "state is 'connected' but there is not selected tuple");

				if (!hasUseCandidate)
				{
					// If a new tuple store it.
					if (HasTuple(tuple) == nullptr)
						AddTuple(tuple);
				}
				else
				{
					MS_DEBUG_TAG(ice, "transition from state 'connected' to 'completed'");

					auto storedTuple = HasTuple(tuple);

					// If a new tuple store it.
					if (storedTuple == nullptr)
						storedTuple = AddTuple(tuple);

					// Mark it as selected tuple.
					SetSelectedTuple(storedTuple);
					// Update state.
					this->state = IceState::COMPLETED;
					// Notify the listener.
					this->listener->OnIceServerCompleted(this);
				}

				break;
		}

		case IceState::COMPLETED:
		{
				// There should be some tuples.
				MS_ASSERT(!this->tuples.empty(), "state is 'completed' but there are no tuples");

				// There should be a selected tuple.
				MS_ASSERT(
				  this->selectedTuple != nullptr, "state is 'completed' but there is not selected tuple");

				if (!hasUseCandidate)
				{
					// If a new tuple store it.
					if (HasTuple(tuple) == nullptr)
						AddTuple(tuple);
				}
				else
				{
					auto storedTuple = HasTuple(tuple);

					// If a new tuple store it.
					if (storedTuple == nullptr)
						storedTuple = AddTuple(tuple);

					// Mark it as selected tuple.
					SetSelectedTuple(storedTuple);
				}

				break;
		}
	}
}

最后通知交互完成


	inline void WebRtcTransport::OnIceServerConnected(const RTC::IceServer* /*iceServer*/)
	{
		MS_TRACE();

		MS_DEBUG_TAG(ice, "ICE connected");

		// Notify the Node WebRtcTransport.
		json data = json::object();

		data["iceState"] = "connected";

		Channel::Notifier::Emit(this->id, "icestatechange", data);

		// If ready, run the DTLS handler.
		MayRunDtlsTransport();
	}

	inline void WebRtcTransport::OnIceServerCompleted(const RTC::IceServer* /*iceServer*/)
	{
		MS_TRACE();

		MS_DEBUG_TAG(ice, "ICE completed");

		// Notify the Node WebRtcTransport.
		json data = json::object();

		data["iceState"] = "completed";

		Channel::Notifier::Emit(this->id, "icestatechange", data);

		// If ready, run the DTLS handler.
		MayRunDtlsTransport();
	}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前进的蜗牛啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值