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 ...@@ -307,10 +307,10 @@ namespace RTC
absl::flat_hash_map<uint32_t, RTC::Consumer*> mapSsrcConsumer; absl::flat_hash_map<uint32_t, RTC::Consumer*> mapSsrcConsumer;
absl::flat_hash_map<uint32_t, RTC::Consumer*> mapRtxSsrcConsumer; absl::flat_hash_map<uint32_t, RTC::Consumer*> mapRtxSsrcConsumer;
Timer* rtcpTimer{ nullptr }; Timer* rtcpTimer{ nullptr };
RTC::TransportCongestionControlClient* tccClient{ nullptr }; std::shared_ptr<RTC::TransportCongestionControlClient> tccClient{ nullptr };
RTC::TransportCongestionControlServer* tccServer{ nullptr }; std::shared_ptr<RTC::TransportCongestionControlServer> tccServer{ nullptr };
#ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR #ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
RTC::SenderBandwidthEstimator* senderBwe{ nullptr }; std::shared_ptr<RTC::SenderBandwidthEstimator> senderBwe{ nullptr };
#endif #endif
// Others. // Others.
bool direct{ false }; // Whether this Transport allows PayloadChannel comm. bool direct{ false }; // Whether this Transport allows PayloadChannel comm.
......
...@@ -228,20 +228,6 @@ namespace RTC ...@@ -228,20 +228,6 @@ namespace RTC
// Delete the RTCP timer. // Delete the RTCP timer.
delete this->rtcpTimer; delete this->rtcpTimer;
this->rtcpTimer = nullptr; 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() void Transport::CloseProducersAndConsumers()
...@@ -809,7 +795,8 @@ namespace RTC ...@@ -809,7 +795,8 @@ namespace RTC
if (createTccServer) 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) if (this->maxIncomingBitrate != 0u)
this->tccServer->SetMaxIncomingBitrate(this->maxIncomingBitrate); this->tccServer->SetMaxIncomingBitrate(this->maxIncomingBitrate);
...@@ -1014,7 +1001,7 @@ namespace RTC ...@@ -1014,7 +1001,7 @@ namespace RTC
consumer->SetExternallyManagedBitrate(); consumer->SetExternallyManagedBitrate();
}; };
this->tccClient = new RTC::TransportCongestionControlClient( this->tccClient = std::make_shared<RTC::TransportCongestionControlClient>(
this, bweType, this->initialAvailableOutgoingBitrate, this->maxOutgoingBitrate); this, bweType, this->initialAvailableOutgoingBitrate, this->maxOutgoingBitrate);
if (IsConnected()) if (IsConnected())
...@@ -1061,8 +1048,8 @@ namespace RTC ...@@ -1061,8 +1048,8 @@ namespace RTC
consumer->SetExternallyManagedBitrate(); consumer->SetExternallyManagedBitrate();
}; };
this->senderBwe = this->senderBwe = std::make_shared<RTC::SenderBandwidthEstimator>(
new RTC::SenderBandwidthEstimator(this, this->initialAvailableOutgoingBitrate); this, this->initialAvailableOutgoingBitrate);
if (IsConnected()) if (IsConnected())
this->senderBwe->TransportConnected(); this->senderBwe->TransportConnected();
...@@ -2528,7 +2515,6 @@ namespace RTC ...@@ -2528,7 +2515,6 @@ namespace RTC
{ {
this->transportWideCcSeq++; this->transportWideCcSeq++;
auto* tccClient = this->tccClient;
webrtc::RtpPacketSendInfo packetInfo; webrtc::RtpPacketSendInfo packetInfo;
packetInfo.ssrc = packet->GetSsrc(); packetInfo.ssrc = packet->GetSsrc();
...@@ -2541,8 +2527,15 @@ namespace RTC ...@@ -2541,8 +2527,15 @@ namespace RTC
// Indicate the pacer (and prober) that a packet is to be sent. // Indicate the pacer (and prober) that a packet is to be sent.
this->tccClient->InsertPacket(packetInfo); 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 #ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
auto* senderBwe = this->senderBwe; std::weak_ptr<RTC::SenderBandwidthEstimator> senderBweWeakPtr(this->senderBwe);
RTC::SenderBandwidthEstimator::SentInfo sentInfo; RTC::SenderBandwidthEstimator::SentInfo sentInfo;
sentInfo.wideSeq = this->transportWideCcSeq; sentInfo.wideSeq = this->transportWideCcSeq;
...@@ -2550,25 +2543,41 @@ namespace RTC ...@@ -2550,25 +2543,41 @@ namespace RTC
sentInfo.sendingAtMs = DepLibUV::GetTimeMs(); sentInfo.sendingAtMs = DepLibUV::GetTimeMs();
auto* cb = new onSendCallback( auto* cb = new onSendCallback(
[tccClient, &packetInfo, senderBwe, &sentInfo](bool sent) [tccClientWeakPtr, &packetInfo, senderBweWeakPtr, &sentInfo](bool sent)
{ {
if (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); SendRtpPacket(consumer, packet, cb);
#else #else
const auto* cb = new onSendCallback( const auto* cb = new onSendCallback(
[tccClient, &packetInfo](bool sent) [tccClientWeakPtr, &packetInfo](bool sent)
{ {
if (sent) if (sent)
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64()); {
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
}
}); });
SendRtpPacket(consumer, packet, cb); SendRtpPacket(consumer, packet, cb);
...@@ -2600,7 +2609,6 @@ namespace RTC ...@@ -2600,7 +2609,6 @@ namespace RTC
{ {
this->transportWideCcSeq++; this->transportWideCcSeq++;
auto* tccClient = this->tccClient;
webrtc::RtpPacketSendInfo packetInfo; webrtc::RtpPacketSendInfo packetInfo;
packetInfo.ssrc = packet->GetSsrc(); packetInfo.ssrc = packet->GetSsrc();
...@@ -2613,8 +2621,10 @@ namespace RTC ...@@ -2613,8 +2621,10 @@ namespace RTC
// Indicate the pacer (and prober) that a packet is to be sent. // Indicate the pacer (and prober) that a packet is to be sent.
this->tccClient->InsertPacket(packetInfo); this->tccClient->InsertPacket(packetInfo);
std::weak_ptr<RTC::TransportCongestionControlClient> tccClientWeakPtr(this->tccClient);
#ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR #ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
auto* senderBwe = this->senderBwe; std::weak_ptr<RTC::SenderBandwidthEstimator> senderBweWeakPtr = this->senderBwe;
RTC::SenderBandwidthEstimator::SentInfo sentInfo; RTC::SenderBandwidthEstimator::SentInfo sentInfo;
sentInfo.wideSeq = this->transportWideCcSeq; sentInfo.wideSeq = this->transportWideCcSeq;
...@@ -2622,25 +2632,41 @@ namespace RTC ...@@ -2622,25 +2632,41 @@ namespace RTC
sentInfo.sendingAtMs = DepLibUV::GetTimeMs(); sentInfo.sendingAtMs = DepLibUV::GetTimeMs();
auto* cb = new onSendCallback( auto* cb = new onSendCallback(
[tccClient, &packetInfo, senderBwe, &sentInfo](bool sent) [tccClientWeakPtr, &packetInfo, senderBweWeakPtr, &sentInfo](bool sent)
{ {
if (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); SendRtpPacket(consumer, packet, cb);
#else #else
const auto* cb = new onSendCallback( const auto* cb = new onSendCallback(
[tccClient, &packetInfo](bool sent) [tccClientWeakPtr, &packetInfo](bool sent)
{ {
if (sent) if (sent)
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64()); {
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
}
}); });
SendRtpPacket(consumer, packet, cb); SendRtpPacket(consumer, packet, cb);
...@@ -2951,8 +2977,10 @@ namespace RTC ...@@ -2951,8 +2977,10 @@ namespace RTC
// Indicate the pacer (and prober) that a packet is to be sent. // Indicate the pacer (and prober) that a packet is to be sent.
this->tccClient->InsertPacket(packetInfo); this->tccClient->InsertPacket(packetInfo);
std::weak_ptr<RTC::TransportCongestionControlClient> tccClientWeakPtr(this->tccClient);
#ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR #ifdef ENABLE_RTC_SENDER_BANDWIDTH_ESTIMATOR
auto* senderBwe = this->senderBwe; std::weak_ptr<RTC::SenderBandwidthEstimator> senderBweWeakPtr = this->senderBwe;
RTC::SenderBandwidthEstimator::SentInfo sentInfo; RTC::SenderBandwidthEstimator::SentInfo sentInfo;
sentInfo.wideSeq = this->transportWideCcSeq; sentInfo.wideSeq = this->transportWideCcSeq;
...@@ -2961,25 +2989,41 @@ namespace RTC ...@@ -2961,25 +2989,41 @@ namespace RTC
sentInfo.sendingAtMs = DepLibUV::GetTimeMs(); sentInfo.sendingAtMs = DepLibUV::GetTimeMs();
auto* cb = new onSendCallback( auto* cb = new onSendCallback(
[tccClient, &packetInfo, senderBwe, &sentInfo](bool sent) [tccClientWeakPtr, &packetInfo, senderBweWeakPtr, &sentInfo](bool sent)
{ {
if (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); SendRtpPacket(nullptr, packet, cb);
#else #else
const auto* cb = new onSendCallback( const auto* cb = new onSendCallback(
[tccClient, &packetInfo](bool sent) [tccClientWeakPtr, &packetInfo](bool sent)
{ {
if (sent) if (sent)
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64()); {
auto tccClient = tccClientWeakPtr.lock();
if (tccClient)
{
tccClient->PacketSent(packetInfo, DepLibUV::GetTimeMsInt64());
}
}
}); });
SendRtpPacket(nullptr, packet, cb); SendRtpPacket(nullptr, packet, cb);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论