Skip to content

Commit

Permalink
Merge pull request #4 from satoren/unique_ptr
Browse files Browse the repository at this point in the history
refactor: use unique_ptr instead of raw pointer for CompoundPacket
  • Loading branch information
nazar-pc authored Nov 21, 2021
2 parents 5833d50 + ee5e195 commit f4189c2
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 38 deletions.
15 changes: 8 additions & 7 deletions worker/include/RTC/Consumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ namespace RTC
{
this->externallyManagedBitrate = true;
}
virtual uint8_t GetBitratePriority() const = 0;
virtual uint32_t IncreaseLayer(uint32_t bitrate, bool considerLoss) = 0;
virtual void ApplyLayers() = 0;
virtual uint32_t GetDesiredBitrate() const = 0;
virtual void SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpPacket** clonedPacket) = 0;
virtual const std::vector<RTC::RtpStreamSend*>& GetRtpStreams() const = 0;
virtual RTC::RTCP::CompoundPacket* GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) = 0;
virtual uint8_t GetBitratePriority() const = 0;
virtual uint32_t IncreaseLayer(uint32_t bitrate, bool considerLoss) = 0;
virtual void ApplyLayers() = 0;
virtual uint32_t GetDesiredBitrate() const = 0;
virtual void SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpPacket** clonedPacket) = 0;
virtual const std::vector<RTC::RtpStreamSend*>& GetRtpStreams() const = 0;
virtual RTC::RTCP::CompoundPacket::UniquePtr GetRtcp(
RTC::RtpStreamSend* rtpStream, uint64_t nowMs) = 0;
virtual void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) = 0;
virtual void ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket) = 0;
virtual void ReceiveKeyFrameRequest(
Expand Down
2 changes: 1 addition & 1 deletion worker/include/RTC/PipeConsumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace RTC
void ApplyLayers() override;
uint32_t GetDesiredBitrate() const override;
void SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpPacket** clonedPacket) override;
RTC::RTCP::CompoundPacket* GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) override;
RTC::RTCP::CompoundPacket::UniquePtr GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) override;
const std::vector<RTC::RtpStreamSend*>& GetRtpStreams() const override
{
return this->rtpStreams;
Expand Down
2 changes: 1 addition & 1 deletion worker/include/RTC/Producer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ namespace RTC
ReceiveRtpPacketResult ReceiveRtpPacket(RTC::RtpPacket* packet);
void ReceiveRtcpSenderReport(RTC::RTCP::SenderReport* report);
void ReceiveRtcpXrDelaySinceLastRr(RTC::RTCP::DelaySinceLastRr::SsrcInfo* ssrcInfo);
RTC::RTCP::CompoundPacket* GetRtcp(uint64_t nowMs);
RTC::RTCP::CompoundPacket::UniquePtr GetRtcp(uint64_t nowMs);
void RequestKeyFrame(uint32_t mappedSsrc);

private:
Expand Down
18 changes: 16 additions & 2 deletions worker/include/RTC/RTCP/CompoundPacket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace RTC
class CompoundPacket
{
public:
static CompoundPacket* Create();
static void ReturnIntoPool(CompoundPacket* packet);
using UniquePtr = std::unique_ptr<CompoundPacket>;
static UniquePtr Create();

public:
const uint8_t* GetData() const
Expand Down Expand Up @@ -60,6 +60,9 @@ namespace RTC
// Use `CompoundPacket::ReturnIntoPool()` instead
~CompoundPacket() = default;

friend struct std::default_delete<RTC::RTCP::CompoundPacket>;
static void ReturnIntoPool(CompoundPacket* packet);

private:
uint8_t* header{ nullptr };
size_t size{ 0 };
Expand All @@ -71,4 +74,15 @@ namespace RTC
} // namespace RTCP
} // namespace RTC

namespace std
{
template<>
struct default_delete<RTC::RTCP::CompoundPacket>
{
void operator()(RTC::RTCP::CompoundPacket* ptr) const
{
RTC::RTCP::CompoundPacket::ReturnIntoPool(ptr);
}
};
}; // namespace std
#endif
2 changes: 1 addition & 1 deletion worker/include/RTC/SimpleConsumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace RTC
{
return this->rtpStreams;
}
RTC::RTCP::CompoundPacket* GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) override;
RTC::RTCP::CompoundPacket::UniquePtr GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) override;
void NeedWorstRemoteFractionLost(uint32_t mappedSsrc, uint8_t& worstRemoteFractionLost) override;
void ReceiveNack(RTC::RTCP::FeedbackRtpNackPacket* nackPacket) override;
void ReceiveKeyFrameRequest(RTC::RTCP::FeedbackPs::MessageType messageType, uint32_t ssrc) override;
Expand Down
2 changes: 1 addition & 1 deletion worker/include/RTC/SimulcastConsumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace RTC
void ApplyLayers() override;
uint32_t GetDesiredBitrate() const override;
void SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpPacket** clonedPacket) override;
RTC::RTCP::CompoundPacket* GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) override;
RTC::RTCP::CompoundPacket::UniquePtr GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) override;
const std::vector<RTC::RtpStreamSend*>& GetRtpStreams() const override
{
return this->rtpStreams;
Expand Down
2 changes: 1 addition & 1 deletion worker/include/RTC/SvcConsumer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace RTC
void ApplyLayers() override;
uint32_t GetDesiredBitrate() const override;
void SendRtpPacket(RTC::RtpPacket* packet, RTC::RtpPacket** clonedPacket) override;
RTC::RTCP::CompoundPacket* GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) override;
RTC::RTCP::CompoundPacket::UniquePtr GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs) override;
const std::vector<RTC::RtpStreamSend*>& GetRtpStreams() const override
{
return this->rtpStreams;
Expand Down
4 changes: 2 additions & 2 deletions worker/src/RTC/PipeConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ namespace RTC
packet->SetSequenceNumber(origSeq);
}

RTC::RTCP::CompoundPacket* PipeConsumer::GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs)
RTC::RTCP::CompoundPacket::UniquePtr PipeConsumer::GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs)
{
MS_TRACE();

Expand All @@ -304,7 +304,7 @@ namespace RTC
if (!report)
return nullptr;

auto* packet = RTC::RTCP::CompoundPacket::Create();
auto packet = RTC::RTCP::CompoundPacket::Create();

packet->AddSenderReport(report);

Expand Down
4 changes: 2 additions & 2 deletions worker/src/RTC/Producer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,14 +752,14 @@ namespace RTC
rtpStream->ReceiveRtcpXrDelaySinceLastRr(ssrcInfo);
}

RTC::RTCP::CompoundPacket* Producer::GetRtcp(uint64_t nowMs)
RTC::RTCP::CompoundPacket::UniquePtr Producer::GetRtcp(uint64_t nowMs)
{
MS_TRACE();

if (static_cast<float>((nowMs - this->lastRtcpSentTime) * 1.15) < this->maxRtcpInterval)
return nullptr;

auto* packet = RTC::RTCP::CompoundPacket::Create();
auto packet = RTC::RTCP::CompoundPacket::Create();

for (auto& kv : this->mapSsrcRtpStream)
{
Expand Down
5 changes: 2 additions & 3 deletions worker/src/RTC/RTCP/CompoundPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ namespace RTC

/* Instance methods. */

CompoundPacket* CompoundPacket::Create()
CompoundPacket::UniquePtr CompoundPacket::Create()
{
auto* packet = CompoundPacketPool.Allocate();
new (packet) CompoundPacket();

return packet;
return UniquePtr(new (packet) CompoundPacket());
}

void CompoundPacket::ReturnIntoPool(CompoundPacket* packet)
Expand Down
5 changes: 3 additions & 2 deletions worker/src/RTC/SimpleConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ namespace RTC
packet->SetSequenceNumber(origSeq);
}

RTC::RTCP::CompoundPacket* SimpleConsumer::GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs)
RTC::RTCP::CompoundPacket::UniquePtr SimpleConsumer::GetRtcp(
RTC::RtpStreamSend* rtpStream, uint64_t nowMs)
{
MS_TRACE();

Expand All @@ -330,7 +331,7 @@ namespace RTC
if (!report)
return nullptr;

auto* packet = RTC::RTCP::CompoundPacket::Create();
auto packet = RTC::RTCP::CompoundPacket::Create();

packet->AddSenderReport(report);

Expand Down
5 changes: 3 additions & 2 deletions worker/src/RTC/SimulcastConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,8 @@ namespace RTC
packet->RestorePayload();
}

RTC::RTCP::CompoundPacket* SimulcastConsumer::GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs)
RTC::RTCP::CompoundPacket::UniquePtr SimulcastConsumer::GetRtcp(
RTC::RtpStreamSend* rtpStream, uint64_t nowMs)
{
MS_TRACE();

Expand All @@ -938,7 +939,7 @@ namespace RTC
if (!report)
return nullptr;

auto* packet = RTC::RTCP::CompoundPacket::Create();
auto packet = RTC::RTCP::CompoundPacket::Create();

packet->AddSenderReport(report);

Expand Down
4 changes: 2 additions & 2 deletions worker/src/RTC/SvcConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ namespace RTC
packet->RestorePayload();
}

RTC::RTCP::CompoundPacket* SvcConsumer::GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs)
RTC::RTCP::CompoundPacket::UniquePtr SvcConsumer::GetRtcp(RTC::RtpStreamSend* rtpStream, uint64_t nowMs)
{
MS_TRACE();

Expand All @@ -668,7 +668,7 @@ namespace RTC
if (!report)
return nullptr;

auto* packet = RTC::RTCP::CompoundPacket::Create();
auto packet = RTC::RTCP::CompoundPacket::Create();

packet->AddSenderReport(report);

Expand Down
15 changes: 4 additions & 11 deletions worker/src/RTC/Transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2312,56 +2312,49 @@ namespace RTC
{
MS_TRACE();

RTC::RTCP::CompoundPacket* packet{ nullptr };
RTC::RTCP::CompoundPacket::UniquePtr packet(nullptr);

for (auto& kv : this->mapConsumers)
{
auto* consumer = kv.second;

for (const auto& rtpStream : consumer->GetRtpStreams())
{
RTC::RTCP::CompoundPacket::ReturnIntoPool(packet);
packet = consumer->GetRtcp(rtpStream, nowMs);

// Send the RTCP compound packet if there is a sender report.
if (packet != nullptr && packet->HasSenderReport())
{
packet->Serialize(RTC::RTCP::Buffer);
SendRtcpCompoundPacket(packet);
SendRtcpCompoundPacket(packet.get());
}
}
}

// Reset the Compound packet.
RTC::RTCP::CompoundPacket::ReturnIntoPool(packet);
packet = nullptr;

for (auto& kv : this->mapProducers)
{
auto* producer = kv.second;

RTC::RTCP::CompoundPacket::ReturnIntoPool(packet);
packet = producer->GetRtcp(nowMs);

// One more RR would exceed the MTU, send the compound packet now.
if (packet != nullptr && packet->GetSize() + sizeof(RTCP::ReceiverReport::Header) > RTC::MtuSize)
{
packet->Serialize(RTC::RTCP::Buffer);
SendRtcpCompoundPacket(packet);
SendRtcpCompoundPacket(packet.get());

// Reset the Compound packet.
RTC::RTCP::CompoundPacket::ReturnIntoPool(packet);
packet = nullptr;
}
}

if (packet != nullptr && packet->GetReceiverReportCount() != 0u)
{
packet->Serialize(RTC::RTCP::Buffer);
SendRtcpCompoundPacket(packet);
SendRtcpCompoundPacket(packet.get());
}

RTC::RTCP::CompoundPacket::ReturnIntoPool(packet);
}

void Transport::DistributeAvailableOutgoingBitrate()
Expand Down

0 comments on commit f4189c2

Please sign in to comment.