Unverified 提交 6334d2ef authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo 提交者: GitHub

Fix crash when using WebRtcServer (#970)

Authored-by: 's avatarpenguinol <penguinol@163.com>
上级 a765833b
......@@ -307,10 +307,10 @@ namespace RTC
absl::flat_hash_map<uint32_t, RTC::Consumer*> mapSsrcConsumer;
absl::flat_hash_map<uint32_t, RTC::Consumer*> mapRtxSsrcConsumer;
Timer* rtcpTimer{ nullptr };
RTC::TransportCongestionControlClient* tccClient{ nullptr };
RTC::TransportCongestionControlServer* tccServer{ nullptr };
std::shared_ptr<RTC::TransportCongestionControlClient> tccClient{ nullptr };
std::shared_ptr<RTC::TransportCongestionControlServer> tccServer{ nullptr };
#ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
RTC::SenderBandwidthEstimator* senderBwe{ nullptr };
std::shared_ptr<RTC::SenderBandwidthEstimator> senderBwe{ nullptr };
#endif
// Others.
bool direct{ false }; // Whether this Transport allows PayloadChannel comm.
......
......@@ -228,20 +228,6 @@ namespace RTC
// Delete the RTCP timer.
delete this->rtcpTimer;
this->rtcpTimer = nullptr;
// Delete Transport-CC client.
delete this->tccClient;
this->tccClient = nullptr;
// Delete Transport-CC server.
delete this->tccServer;
this->tccServer = nullptr;
#ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
// Delete Sender BWE.
delete this->senderBwe;
this->senderBwe = nullptr;
#endif
}
void Transport::CloseProducersAndConsumers()
......@@ -809,7 +795,8 @@ namespace RTC
if (createTccServer)
{
this->tccServer = new RTC::TransportCongestionControlServer(this, bweType, RTC::MtuSize);
this->tccServer =
std::make_shared<RTC::TransportCongestionControlServer>(this, bweType, RTC::MtuSize);
if (this->maxIncomingBitrate != 0u)
this->tccServer->SetMaxIncomingBitrate(this->maxIncomingBitrate);
......@@ -1014,7 +1001,7 @@ namespace RTC
consumer->SetExternallyManagedBitrate();
};
this->tccClient = new RTC::TransportCongestionControlClient(
this->tccClient = std::make_shared<RTC::TransportCongestionControlClient>(
this, bweType, this->initialAvailableOutgoingBitrate, this->maxOutgoingBitrate);
if (IsConnected())
......@@ -1061,8 +1048,8 @@ namespace RTC
consumer->SetExternallyManagedBitrate();
};
this->senderBwe =
new RTC::SenderBandwidthEstimator(this, this->initialAvailableOutgoingBitrate);
this->senderBwe = std::make_shared<RTC::SenderBandwidthEstimator>(
this, this->initialAvailableOutgoingBitrate);
if (IsConnected())
this->senderBwe->TransportConnected();
......@@ -2528,7 +2515,6 @@ namespace RTC
{
this->transportWideCcSeq++;
auto* tccClient = this->tccClient;
webrtc::RtpPacketSendInfo packetInfo;
packetInfo.ssrc = packet->GetSsrc();
......@@ -2541,8 +2527,15 @@ namespace RTC
// Indicate the pacer (and prober) that a packet is to be sent.
this->tccClient->InsertPacket(packetInfo);
// When using WebRtcServer, the lifecycle of a RTC::UdpSocket maybe longer
// than WebRtcTransport so there is a chance for the send callback to be
// invoked *after* the WebRtcTransport has been closed (freed). To avoid
// invalid memory access we need to use weak_ptr. Same applies in other
// send callbacks.
std::weak_ptr<RTC::TransportCongestionControlClient> tccClientWeakPtr(this->tccClient);
#ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
auto* senderBwe = this->senderBwe;
std::weak_ptr<RTC::SenderBandwidthEstimator> senderBweWeakPtr(this->senderBwe);
RTC::SenderBandwidthEstimator::SentInfo sentInfo;
sentInfo.wideSeq = this->transportWideCcSeq;
......@@ -2550,25 +2543,41 @@ namespace RTC
sentInfo.sendingAtMs = DepLibUV::GetTimeMs();
auto* cb = new onSendCallback(
[tccClient, &packetInfo, senderBwe, &sentInfo](bool sent)
[tccClientWeakPtr, &packetInfo, senderBweWeakPtr, &sentInfo](bool sent)
{
if (sent)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
sentInfo.sentAtMs = DepLibUV::GetTimeMs();
auto senderBwe = senderBweWeakPtr.lock();
senderBwe->RtpPacketSent(sentInfo);
if (senderBwe)
{
sentInfo.sentAtMs = DepLibUV::GetTimeMs();
senderBwe->RtpPacketSent(sentInfo);
}
}
});
SendRtpPacket(consumer, packet, cb);
#else
const auto* cb = new onSendCallback(
[tccClient, &packetInfo](bool sent)
[tccClientWeakPtr, &packetInfo](bool sent)
{
if (sent)
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
{
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
}
});
SendRtpPacket(consumer, packet, cb);
......@@ -2600,7 +2609,6 @@ namespace RTC
{
this->transportWideCcSeq++;
auto* tccClient = this->tccClient;
webrtc::RtpPacketSendInfo packetInfo;
packetInfo.ssrc = packet->GetSsrc();
......@@ -2613,8 +2621,10 @@ namespace RTC
// Indicate the pacer (and prober) that a packet is to be sent.
this->tccClient->InsertPacket(packetInfo);
std::weak_ptr<RTC::TransportCongestionControlClient> tccClientWeakPtr(this->tccClient);
#ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
auto* senderBwe = this->senderBwe;
std::weak_ptr<RTC::SenderBandwidthEstimator> senderBweWeakPtr = this->senderBwe;
RTC::SenderBandwidthEstimator::SentInfo sentInfo;
sentInfo.wideSeq = this->transportWideCcSeq;
......@@ -2622,25 +2632,41 @@ namespace RTC
sentInfo.sendingAtMs = DepLibUV::GetTimeMs();
auto* cb = new onSendCallback(
[tccClient, &packetInfo, senderBwe, &sentInfo](bool sent)
[tccClientWeakPtr, &packetInfo, senderBweWeakPtr, &sentInfo](bool sent)
{
if (sent)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
sentInfo.sentAtMs = DepLibUV::GetTimeMs();
auto senderBwe = senderBweWeakPtr.lock();
senderBwe->RtpPacketSent(sentInfo);
if (senderBwe)
{
sentInfo.sentAtMs = DepLibUV::GetTimeMs();
senderBwe->RtpPacketSent(sentInfo);
}
}
});
SendRtpPacket(consumer, packet, cb);
#else
const auto* cb = new onSendCallback(
[tccClient, &packetInfo](bool sent)
[tccClientWeakPtr, &packetInfo](bool sent)
{
if (sent)
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
{
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
}
});
SendRtpPacket(consumer, packet, cb);
......@@ -2951,8 +2977,10 @@ namespace RTC
// Indicate the pacer (and prober) that a packet is to be sent.
this->tccClient->InsertPacket(packetInfo);
std::weak_ptr<RTC::TransportCongestionControlClient> tccClientWeakPtr(this->tccClient);
#ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
auto* senderBwe = this->senderBwe;
std::weak_ptr<RTC::SenderBandwidthEstimator> senderBweWeakPtr = this->senderBwe;
RTC::SenderBandwidthEstimator::SentInfo sentInfo;
sentInfo.wideSeq = this->transportWideCcSeq;
......@@ -2961,25 +2989,41 @@ namespace RTC
sentInfo.sendingAtMs = DepLibUV::GetTimeMs();
auto* cb = new onSendCallback(
[tccClient, &packetInfo, senderBwe, &sentInfo](bool sent)
[tccClientWeakPtr, &packetInfo, senderBweWeakPtr, &sentInfo](bool sent)
{
if (sent)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
sentInfo.sentAtMs = DepLibUV::GetTimeMs();
auto senderBwe = senderBweWeakPtr.lock();
senderBwe->RtpPacketSent(sentInfo);
if (senderBwe)
{
sentInfo.sentAtMs = DepLibUV::GetTimeMs();
senderBwe->RtpPacketSent(sentInfo);
}
}
});
SendRtpPacket(nullptr, packet, cb);
#else
const auto* cb = new onSendCallback(
[tccClient, &packetInfo](bool sent)
[tccClientWeakPtr, &packetInfo](bool sent)
{
if (sent)
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
{
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
}
});
SendRtpPacket(nullptr, packet, cb);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论