Skip to content

Commit

Permalink
Worker: const correctness (versatica#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmillan authored Jan 27, 2023
1 parent 8287299 commit bfaf883
Show file tree
Hide file tree
Showing 25 changed files with 117 additions and 111 deletions.
1 change: 1 addition & 0 deletions worker/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakStringLiterals: false
ColumnLimit: 100
QualifierAlignment: Left
CommentPragmas: 'NOLINT'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 2
Expand Down
1 change: 1 addition & 0 deletions worker/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Checks: '*,-llvmlibc-*,-boost-use-to-string,-cert-*,-clang-analyzer-osx.*,-clang
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
User: mediasoup
FormatStyle: file
CheckOptions:
- key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader
value: ''
Expand Down
28 changes: 14 additions & 14 deletions worker/src/RTC/ActiveSpeakerObserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace RTC

inline int64_t BinomialCoefficient(int32_t n, int32_t r)
{
int32_t m = n - r;
const int32_t m = n - r;

if (r < m)
{
Expand Down Expand Up @@ -67,16 +67,16 @@ namespace RTC
inline bool ComputeBigs(
const std::vector<uint8_t>& littles, std::vector<uint8_t>& bigs, uint8_t threashold)
{
uint32_t littleLen = littles.size();
uint32_t bigLen = bigs.size();
uint32_t littleLenPerBig = littleLen / bigLen;
uint32_t littleLen = littles.size();
uint32_t bigLen = bigs.size();
const uint32_t littleLenPerBig = littleLen / bigLen;
bool changed{ false };

for (uint32_t b = 0u, l = 0u; b < bigLen; ++b)
{
uint8_t sum{ 0u };

for (uint32_t lEnd = l + littleLenPerBig; l < lEnd; ++l)
for (const uint32_t lEnd = l + littleLenPerBig; l < lEnd; ++l)
{
if (littles[l] > threashold)
{
Expand Down Expand Up @@ -261,7 +261,7 @@ namespace RTC
{
MS_TRACE();

uint64_t now = DepLibUV::GetTimeMs();
const uint64_t now = DepLibUV::GetTimeMs();

if (now - this->lastLevelIdleTime >= LevelIdleTimeout)
{
Expand Down Expand Up @@ -452,7 +452,7 @@ namespace RTC
{
if (this->lastLevelChangeTime <= now)
{
uint64_t elapsed = now - this->lastLevelChangeTime;
const uint64_t elapsed = now - this->lastLevelChangeTime;

this->lastLevelChangeTime = now;

Expand Down Expand Up @@ -498,25 +498,25 @@ namespace RTC
{
MS_TRACE();

int8_t minLevel = this->minLevel + SubunitLengthN1;
bool changed = false;
const int8_t minLevel = this->minLevel + SubunitLengthN1;
bool changed = false;

for (uint32_t i = 0; i < ImmediateBuffLen; ++i)
{
// this->levels is a circular buffer where new samples are written in the
// next vector index. this->immediates is a buffer where the most recent
// value is always in index 0.
size_t levelIndex = this->nextLevelIndex >= (i + 1)
? this->nextLevelIndex - i - 1
: this->nextLevelIndex + LevelsBuffLen - i - 1;
uint8_t level = this->levels[levelIndex];
const size_t levelIndex = this->nextLevelIndex >= (i + 1)
? this->nextLevelIndex - i - 1
: this->nextLevelIndex + LevelsBuffLen - i - 1;
uint8_t level = this->levels[levelIndex];

if (level < minLevel)
{
level = MinLevel;
}

uint8_t immediate = (level / SubunitLengthN1);
const uint8_t immediate = (level / SubunitLengthN1);

if (this->immediates[i] != immediate)
{
Expand Down
4 changes: 2 additions & 2 deletions worker/src/RTC/Consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ namespace RTC
return;
}

bool wasActive = IsActive();
const bool wasActive = IsActive();

this->paused = true;

Expand Down Expand Up @@ -410,7 +410,7 @@ namespace RTC
if (this->producerPaused)
return;

bool wasActive = IsActive();
const bool wasActive = IsActive();

this->producerPaused = true;

Expand Down
6 changes: 3 additions & 3 deletions worker/src/RTC/DtlsTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ namespace RTC
localRole == Role::CLIENT || localRole == Role::SERVER,
"local DTLS role must be 'client' or 'server'");

Role previousLocalRole = this->localRole;
const Role previousLocalRole = this->localRole;

if (localRole == previousLocalRole)
{
Expand Down Expand Up @@ -860,7 +860,7 @@ namespace RTC
MS_TRACE();

int err;
bool wasHandshakeDone = this->handshakeDone;
const bool wasHandshakeDone = this->handshakeDone;

err = SSL_get_error(this->ssl, returnCode);

Expand Down Expand Up @@ -1342,7 +1342,7 @@ namespace RTC
{
MS_TRACE();

int w = where & -SSL_ST_MASK;
const int w = where & -SSL_ST_MASK;
const char* role;

if ((w & SSL_ST_CONNECT) != 0)
Expand Down
8 changes: 4 additions & 4 deletions worker/src/RTC/NackGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ namespace RTC
{
MS_TRACE();

uint16_t seq = packet->GetSequenceNumber();
bool isKeyFrame = packet->IsKeyFrame();
uint16_t seq = packet->GetSequenceNumber();
const bool isKeyFrame = packet->IsKeyFrame();

if (!this->started)
{
Expand Down Expand Up @@ -156,7 +156,7 @@ namespace RTC
// If the nack list is too large, remove packets from the nack list until
// the latest first packet of a keyframe. If the list is still too large,
// clear it and request a keyframe.
uint16_t numNewNacks = seqEnd - seqStart;
const uint16_t numNewNacks = seqEnd - seqStart;

if (static_cast<uint16_t>(this->nackList.size()) + numNewNacks > MaxNackPackets)
{
Expand Down Expand Up @@ -228,7 +228,7 @@ namespace RTC
{
MS_TRACE();

uint64_t nowMs = DepLibUV::GetTimeMs();
const uint64_t nowMs = DepLibUV::GetTimeMs();
std::vector<uint16_t> nackBatch;

auto it = this->nackList.begin();
Expand Down
6 changes: 3 additions & 3 deletions worker/src/RTC/PortManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ namespace RTC
return;
}

size_t portIdx = static_cast<size_t>(port) - Settings::configuration.rtcMinPort;
const size_t portIdx = static_cast<size_t>(port) - Settings::configuration.rtcMinPort;

switch (transport)
{
Expand Down Expand Up @@ -564,7 +564,7 @@ namespace RTC
}

// Otherwise add an entry in the map and return it.
uint16_t numPorts =
const uint16_t numPorts =
Settings::configuration.rtcMaxPort - Settings::configuration.rtcMinPort + 1;

// Emplace a new vector filled with numPorts false values, meaning that
Expand All @@ -591,7 +591,7 @@ namespace RTC
}

// Otherwise add an entry in the map and return it.
uint16_t numPorts =
const uint16_t numPorts =
Settings::configuration.rtcMaxPort - Settings::configuration.rtcMinPort + 1;

// Emplace a new vector filled with numPorts false values, meaning that
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 @@ -1351,7 +1351,7 @@ namespace RTC
extenLen = 3u;

// NOTE: Add value 0. The sending Transport will update it.
uint32_t absSendTime{ 0u };
const uint32_t absSendTime{ 0u };

Utils::Byte::Set3Bytes(bufferPtr, 0, absSendTime);

Expand All @@ -1367,7 +1367,7 @@ namespace RTC
extenLen = 2u;

// NOTE: Add value 0. The sending Transport will update it.
uint16_t wideSeqNumber{ 0u };
const uint16_t wideSeqNumber{ 0u };

Utils::Byte::Set2Bytes(bufferPtr, 0, wideSeqNumber);

Expand Down
4 changes: 2 additions & 2 deletions worker/src/RTC/RateCalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace RTC

RemoveOldData(nowMs);

float scale = this->scale / this->windowSizeMs;
const float scale = this->scale / this->windowSizeMs;

this->lastTime = nowMs;
this->lastRate = static_cast<uint32_t>(std::trunc(this->totalCount * scale + 0.5f));
Expand All @@ -85,7 +85,7 @@ namespace RTC
if (this->newestItemIndex < 0 || this->oldestItemIndex < 0)
return;

uint64_t newOldestTime = nowMs - this->windowSizeMs;
const uint64_t newOldestTime = nowMs - this->windowSizeMs;

// Oldest item already removed.
if (newOldestTime < this->oldestItemStartTime)
Expand Down
10 changes: 5 additions & 5 deletions worker/src/RTC/RtpPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ namespace RTC

auto paddedExtensionsTotalSize =
static_cast<size_t>(Utils::Byte::PadTo4Bytes(static_cast<uint16_t>(extensionsTotalSize)));
size_t padding = paddedExtensionsTotalSize - extensionsTotalSize;
const size_t padding = paddedExtensionsTotalSize - extensionsTotalSize;

extensionsTotalSize = paddedExtensionsTotalSize;

Expand Down Expand Up @@ -876,8 +876,8 @@ namespace RTC
// One-Byte extensions cannot have length 0.
while (ptr < extensionEnd)
{
uint8_t id = (*ptr & 0xF0) >> 4;
size_t len = static_cast<size_t>(*ptr & 0x0F) + 1;
const uint8_t id = (*ptr & 0xF0) >> 4;
const size_t len = static_cast<size_t>(*ptr & 0x0F) + 1;

// id=15 in One-Byte extensions means "stop parsing here".
if (id == 15u)
Expand Down Expand Up @@ -929,8 +929,8 @@ namespace RTC
// Two-Byte extensions can have length 0.
while (ptr + 1 < extensionEnd)
{
uint8_t id = *ptr;
uint8_t len = *(ptr + 1);
const uint8_t id = *ptr;
const uint8_t len = *(ptr + 1);

// Valid extension id.
if (id != 0u)
Expand Down
8 changes: 4 additions & 4 deletions worker/src/RTC/RtpStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace RTC
{
MS_TRACE();

uint64_t nowMs = DepLibUV::GetTimeMs();
const uint64_t nowMs = DepLibUV::GetTimeMs();

jsonObject["timestamp"] = nowMs;
jsonObject["ssrc"] = this->params.ssrc;
Expand Down Expand Up @@ -113,7 +113,7 @@ namespace RTC
{
MS_TRACE();

uint16_t seq = packet->GetSequenceNumber();
const uint16_t seq = packet->GetSequenceNumber();

// If this is the first packet seen, initialize stuff.
if (!this->started)
Expand Down Expand Up @@ -174,8 +174,8 @@ namespace RTC
{
MS_TRACE();

uint16_t seq = packet->GetSequenceNumber();
uint16_t udelta = seq - this->maxSeq;
const uint16_t seq = packet->GetSequenceNumber();
const uint16_t udelta = seq - this->maxSeq;

// If the new packet sequence number is greater than the max seen but not
// "so much bigger", accept it.
Expand Down
16 changes: 8 additions & 8 deletions worker/src/RTC/RtpStreamRecv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ namespace RTC
{
MS_TRACE();

uint64_t nowMs = DepLibUV::GetTimeMs();
const uint64_t nowMs = DepLibUV::GetTimeMs();

RTC::RtpStream::FillJsonStats(jsonObject);

Expand Down Expand Up @@ -429,7 +429,7 @@ namespace RTC

report->SetSsrc(GetSsrc());

uint32_t prevPacketsLost = this->packetsLost;
const uint32_t prevPacketsLost = this->packetsLost;

// Calculate Packets Expected and Lost.
auto expected = GetExpectedPackets();
Expand All @@ -448,7 +448,7 @@ namespace RTC

this->receivedPrior = this->mediaTransmissionCounter.GetPacketCount();

int32_t lostInterval = expectedInterval - receivedInterval;
const int32_t lostInterval = expectedInterval - receivedInterval;

if (expectedInterval == 0 || lostInterval <= 0)
this->fractionLost = 0;
Expand All @@ -466,7 +466,7 @@ namespace RTC
else
{
// Recalculate packetsLost.
uint32_t newLostInterval = (worstRemoteFractionLost * expectedInterval) >> 8;
const uint32_t newLostInterval = (worstRemoteFractionLost * expectedInterval) >> 8;

this->reportedPacketLost += newLostInterval;

Expand Down Expand Up @@ -545,16 +545,16 @@ namespace RTC
/* Calculate RTT. */

// Get the NTP representation of the current timestamp.
uint64_t nowMs = DepLibUV::GetTimeMs();
auto ntp = Utils::Time::TimeMs2Ntp(nowMs);
const uint64_t nowMs = DepLibUV::GetTimeMs();
auto ntp = Utils::Time::TimeMs2Ntp(nowMs);

// Get the compact NTP representation of the current timestamp.
uint32_t compactNtp = (ntp.seconds & 0x0000FFFF) << 16;

compactNtp |= (ntp.fractions & 0xFFFF0000) >> 16;

uint32_t lastRr = ssrcInfo->GetLastReceiverReport();
uint32_t dlrr = ssrcInfo->GetDelaySinceLastReceiverReport();
const uint32_t lastRr = ssrcInfo->GetLastReceiverReport();
const uint32_t dlrr = ssrcInfo->GetDelaySinceLastReceiverReport();

// RTT in 1/2^16 second fractions.
uint32_t rtt{ 0 };
Expand Down
Loading

0 comments on commit bfaf883

Please sign in to comment.