Skip to content

Commit

Permalink
Fix jitter calculation in RtpStreamRecv (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc authored Jul 12, 2023
1 parent dfbd618 commit 5d31b53
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
### Next

* `Worker`: Add `Transport::Destroying()` protected method ([PR #1114](https://github.com/versatica/mediasoup/pull/1114)).
* `RtpStreamRecv`: Fix jitter calculation ([PR #1117](https://github.com/versatica/mediasoup/pull/1117), thanks to @penguinol).


### 3.12.5
Expand Down
4 changes: 2 additions & 2 deletions worker/include/RTC/RTCP/ReceiverReport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ namespace RTC
{
return uint32_t{ ntohl(this->header->jitter) };
}
void SetJitter(uint32_t jitter)
void SetJitter(float jitter)
{
this->header->jitter = uint32_t{ htonl(jitter) };
this->header->jitter = uint32_t{ htonl(static_cast<uint32_t>(jitter)) };
}
uint32_t GetLastSenderReport() const
{
Expand Down
5 changes: 3 additions & 2 deletions worker/include/RTC/RtpStreamRecv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ namespace RTC
uint64_t lastSrReceived{ 0u };
// Relative transit time for prev packet.
int32_t transit{ 0u };
// Jitter in ms.
uint32_t jitter{ 0u };
// Jitter in RTP timestamp units. As per spec it's kept as floating value
// although it's exposed as integer in the stats.
float jitter{ 0 };
uint8_t firSeqNumber{ 0u };
uint32_t reportedPacketLost{ 0u };
std::unique_ptr<RTC::NackGenerator> nackGenerator;
Expand Down
11 changes: 5 additions & 6 deletions worker/src/RTC/RtpStreamRecv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ namespace RTC
RTC::RtpStream::FillJsonStats(jsonObject);

jsonObject["type"] = "inbound-rtp";
jsonObject["jitter"] = this->jitter;
jsonObject["jitter"] = static_cast<uint32_t>(this->jitter);
jsonObject["packetCount"] = this->transmissionCounter.GetPacketCount();
jsonObject["byteCount"] = this->transmissionCounter.GetBytes();
jsonObject["bitrate"] = this->transmissionCounter.GetBitrate(nowMs);
Expand Down Expand Up @@ -692,15 +692,14 @@ namespace RTC
{
MS_TRACE();

if (this->params.clockRate == 0u)
if (GetClockRate() == 0u)
{
return;
}

// NOTE: Based on https://github.com/versatica/mediasoup/issues/1018.
auto transit = static_cast<int>(
DepLibUV::GetTimeMs() - (static_cast<uint64_t>(rtpTimestamp) * 1000 / this->params.clockRate));
int d = transit - this->transit;
auto transit = static_cast<int>((DepLibUV::GetTimeMs() * GetClockRate() / 1000) - rtpTimestamp);
int d = transit - this->transit;

// First transit calculation, save and return.
if (this->transit == 0)
Expand All @@ -717,7 +716,7 @@ namespace RTC
d = -d;
}

this->jitter += (1. / 16.) * (static_cast<double>(d) - this->jitter);
this->jitter += (1. / 16.) * (static_cast<float>(d) - this->jitter);
}

void RtpStreamRecv::UpdateScore()
Expand Down

0 comments on commit 5d31b53

Please sign in to comment.