Skip to content

Commit

Permalink
Worker tests: use unique pointers when possible
Browse files Browse the repository at this point in the history
(WIP)

### Details

- Avoid having allocated pointers outside `unique_ptr` pointers.
- So caution with all those mediasoup `Parse()` static methods that return an allocated pointer.
- Working on `TestXr.cpp`.
- `TestRtpStreamSend.cpp`: Use `unique_ptr` instead of `shared_ptr`.
  • Loading branch information
ibc committed Jul 1, 2024
1 parent 9d9e358 commit d92022e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 30 deletions.
37 changes: 19 additions & 18 deletions worker/test/src/RTC/RTCP/TestXr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SCENARIO("RTCP XR parsing", "[parser][rtcp][xr]")

SECTION("parse XR packet")
{
auto* packet = ExtendedReportPacket::Parse(buffer, sizeof(buffer));
std::unique_ptr<ExtendedReportPacket> packet(ExtendedReportPacket::Parse(buffer, sizeof(buffer)));

REQUIRE(packet);
// Despite total buffer size is 40 bytes, our GetSize() method doesn't not
Expand Down Expand Up @@ -113,18 +113,15 @@ SCENARIO("RTCP XR parsing", "[parser][rtcp][xr]")

packet->Serialize(serialized);

auto* packet2 = ExtendedReportPacket::Parse(serialized, serializedBufferLength);
std::unique_ptr<ExtendedReportPacket> packet2(
ExtendedReportPacket::Parse(serialized, serializedBufferLength));

REQUIRE(packet2->GetType() == Type::XR);
REQUIRE(packet2->GetCount() == 0);
REQUIRE(packet2->GetSize() == 36);

REQUIRE(std::memcmp(clonedBuffer, serialized, serializedBufferLength) == 0);

delete packet2;
}

delete packet;
}
}

Expand All @@ -133,7 +130,7 @@ SCENARIO("RTCP XrDelaySinceLastRt parsing", "[parser][rtcp][xr-dlrr]")
SECTION("create RRT")
{
// Create local report and check content.
auto* report1 = new ReceiverReferenceTime();
std::unique_ptr<ReceiverReferenceTime> report1(new ReceiverReferenceTime());

report1->SetNtpSec(11111111);
report1->SetNtpFrac(22222222);
Expand All @@ -148,7 +145,8 @@ SCENARIO("RTCP XrDelaySinceLastRt parsing", "[parser][rtcp][xr-dlrr]")
report1->Serialize(bufferReport1);

// Create a new report out of the external buffer.
auto report2 = ReceiverReferenceTime::Parse(bufferReport1, report1->GetSize());
std::unique_ptr<ReceiverReferenceTime> report2(
ReceiverReferenceTime::Parse(bufferReport1, report1->GetSize()));

REQUIRE(report1->GetType() == report2->GetType());
REQUIRE(report1->GetNtpSec() == report2->GetNtpSec());
Expand All @@ -158,8 +156,8 @@ SCENARIO("RTCP XrDelaySinceLastRt parsing", "[parser][rtcp][xr-dlrr]")
std::unique_ptr<ExtendedReportPacket> packet1(new ExtendedReportPacket());

packet1->SetSsrc(2222);
packet1->AddReport(report1);
packet1->AddReport(report2);
packet1->AddReport(report1.get());
packet1->AddReport(report2.get());

REQUIRE(packet1->GetType() == Type::XR);
REQUIRE(packet1->GetCount() == 0);
Expand All @@ -179,7 +177,8 @@ SCENARIO("RTCP XrDelaySinceLastRt parsing", "[parser][rtcp][xr-dlrr]")
packet1->Serialize(bufferPacket1);

// Create a new packet out of the external buffer.
auto packet2 = ExtendedReportPacket::Parse(bufferPacket1, packet1->GetSize());
std::unique_ptr<ExtendedReportPacket> packet2(
ExtendedReportPacket::Parse(bufferPacket1, packet1->GetSize()));

REQUIRE(packet2->GetType() == packet1->GetType());
REQUIRE(packet2->GetCount() == packet1->GetCount());
Expand All @@ -194,8 +193,8 @@ SCENARIO("RTCP XrDelaySinceLastRt parsing", "[parser][rtcp][xr-dlrr]")
SECTION("create DLRR")
{
// Create local report and check content.
auto* report1 = new DelaySinceLastRr();
auto* ssrcInfo1 = new DelaySinceLastRr::SsrcInfo();
std::unique_ptr<DelaySinceLastRr> report1(new DelaySinceLastRr());
std::unique_ptr<DelaySinceLastRr::SsrcInfo> ssrcInfo1(new DelaySinceLastRr::SsrcInfo());

ssrcInfo1->SetSsrc(1234);
ssrcInfo1->SetLastReceiverReport(11111111);
Expand All @@ -206,15 +205,16 @@ SCENARIO("RTCP XrDelaySinceLastRt parsing", "[parser][rtcp][xr-dlrr]")
REQUIRE(ssrcInfo1->GetDelaySinceLastReceiverReport() == 22222222);
REQUIRE(ssrcInfo1->GetSize() == sizeof(DelaySinceLastRr::SsrcInfo::Body));

report1->AddSsrcInfo(ssrcInfo1);
report1->AddSsrcInfo(ssrcInfo1.get());

// Serialize the report into an external buffer.
uint8_t bufferReport1[256]{ 0 };

report1->Serialize(bufferReport1);

// Create a new report out of the external buffer.
auto report2 = DelaySinceLastRr::Parse(bufferReport1, report1->GetSize());
std::unique_ptr<DelaySinceLastRr> report2(
DelaySinceLastRr::Parse(bufferReport1, report1->GetSize()));

REQUIRE(report1->GetType() == report2->GetType());

Expand All @@ -231,8 +231,8 @@ SCENARIO("RTCP XrDelaySinceLastRt parsing", "[parser][rtcp][xr-dlrr]")
std::unique_ptr<ExtendedReportPacket> packet1(new ExtendedReportPacket());

packet1->SetSsrc(2222);
packet1->AddReport(report1);
packet1->AddReport(report2);
packet1->AddReport(report1.get());
packet1->AddReport(report2.get());

REQUIRE(packet1->GetType() == Type::XR);
REQUIRE(packet1->GetCount() == 0);
Expand All @@ -252,7 +252,8 @@ SCENARIO("RTCP XrDelaySinceLastRt parsing", "[parser][rtcp][xr-dlrr]")
packet1->Serialize(bufferPacket1);

// Create a new packet out of the external buffer.
auto packet2 = ExtendedReportPacket::Parse(bufferPacket1, packet1->GetSize());
std::unique_ptr<ExtendedReportPacket> packet2(
ExtendedReportPacket::Parse(bufferPacket1, packet1->GetSize()));

REQUIRE(packet2->GetType() == packet1->GetType());
REQUIRE(packet2->GetCount() == packet1->GetCount());
Expand Down
22 changes: 10 additions & 12 deletions worker/test/src/RTC/TestRtpStreamSend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ SCENARIO("NACK and RTP packets retransmission", "[rtp][rtcp][nack]")
params.mimeType.type = RTC::RtpCodecMimeType::Type::VIDEO;

std::string mid;
auto* stream = new RtpStreamSend(&testRtpStreamListener, params, mid);
std::unique_ptr<RtpStreamSend> sharedStream1(
new RtpStreamSend(&testRtpStreamListener, params, mid));

size_t iterations = 10000000;

Expand All @@ -508,36 +509,33 @@ SCENARIO("NACK and RTP packets retransmission", "[rtp][rtcp][nack]")
auto* packet = RtpPacket::Parse(rtpBuffer1, 1500);
packet->SetSsrc(1111);

std::shared_ptr<RtpPacket> sharedPacket(packet);
std::unique_ptr<RtpPacket> sharedPacket(packet);

stream->ReceivePacket(packet, sharedPacket);
sharedStream1->ReceivePacket(packet, sharedPacket);
}

std::chrono::duration<double> dur = std::chrono::system_clock::now() - start;
std::cout << "nullptr && initialized shared_ptr: \t" << dur.count() << " seconds" << std::endl;

delete stream;
std::cout << "nullptr && initialized unique_ptr: \t" << dur.count() << " seconds" << std::endl;

params.mimeType.type = RTC::RtpCodecMimeType::Type::AUDIO;
stream = new RtpStreamSend(&testRtpStreamListener, params, mid);
std::unique_ptr<RtpStreamSend> sharedStream2(
new RtpStreamSend(&testRtpStreamListener, params, mid));

start = std::chrono::system_clock::now();

for (size_t i = 0; i < iterations; i++)
{
std::shared_ptr<RtpPacket> sharedPacket;
std::unique_ptr<RtpPacket> sharedPacket;

// Create packet.
auto* packet = RtpPacket::Parse(rtpBuffer1, 1500);
packet->SetSsrc(1111);

stream->ReceivePacket(packet, sharedPacket);
sharedStream2->ReceivePacket(packet, sharedPacket);
}

dur = std::chrono::system_clock::now() - start;
std::cout << "raw && empty shared_ptr duration: \t" << dur.count() << " seconds" << std::endl;

delete stream;
std::cout << "raw && empty unique_ptr duration: \t" << dur.count() << " seconds" << std::endl;
}
#endif
}

0 comments on commit d92022e

Please sign in to comment.