Skip to content

Commit

Permalink
Improved PTS calculation algo of WebRTC Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
getroot committed Jan 27, 2023
1 parent 4cffc03 commit b44fd38
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/projects/modules/rtp_rtcp/lip_sync_clock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,55 @@ std::optional<uint64_t> LipSyncClock::CalcPTS(uint32_t id, uint32_t rtp_timestam
// UpdateSenderReportTime(id, msw, lsw, rtp_timestamp);
}

uint32_t delta = 0;
if (clock->_last_rtp_timestamp == 0)
{
clock->_extended_rtp_timestamp = rtp_timestamp;
}
else
{
if (rtp_timestamp > clock->_last_rtp_timestamp)
{
delta = rtp_timestamp - clock->_last_rtp_timestamp;
}
else
{
delta = clock->_last_rtp_timestamp - rtp_timestamp;
if (delta > 0x80000000)
{
// wrap around
delta = 0xFFFFFFFF - clock->_last_rtp_timestamp + rtp_timestamp + 1;
}
else
{
// reordering or duplicate or error
delta = 0;
logtw("RTP timestamp is not monotonic: %u -> %u", clock->_last_rtp_timestamp, rtp_timestamp);
}
}

clock->_extended_rtp_timestamp += delta;
}

logtd("Calc PTS : id(%u) last_rtp_timestamp(%u) rtp_timestamp(%u) delta(%u) extended_rtp_timestamp(%llu)", id, clock->_last_rtp_timestamp, rtp_timestamp, delta, clock->_extended_rtp_timestamp);

clock->_last_rtp_timestamp = rtp_timestamp;

std::shared_lock<std::shared_mutex> lock(clock->_clock_lock);
// The timestamp difference can be negative.
return clock->_pts + ((int64_t)rtp_timestamp - (int64_t)clock->_rtcp_timestamp);
auto pts = clock->_pts + ((int64_t)clock->_extended_rtp_timestamp - (int64_t)clock->_rtcp_timestamp);

// This is to make pts start at zero.
if (_first_pts == true)
{
// pts in 100/10000000
_adjust_pts_us = (double)pts * clock->_timebase * 100000.0;
_first_pts = false;
}

pts = pts - (int64_t)(_adjust_pts_us / clock->_timebase / 100000.0);

return pts;
}

bool LipSyncClock::UpdateSenderReportTime(uint32_t id, uint32_t ntp_msw, uint32_t ntp_lsw, uint32_t rtcp_timestamp)
Expand Down
5 changes: 5 additions & 0 deletions src/projects/modules/rtp_rtcp/lip_sync_clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class LipSyncClock
bool _updated = false;
double _timebase = 0;
uint32_t _rtcp_timestamp = 0;
uint32_t _last_rtp_timestamp = 0;
uint64_t _extended_rtp_timestamp = 0;
uint64_t _pts = 0; // converted NTP timestamp to timebase timestamp
};

Expand All @@ -30,4 +32,7 @@ class LipSyncClock
bool _enabled = false;

std::shared_ptr<Clock> GetClock(uint32_t id);

bool _first_pts = true;
uint64_t _adjust_pts_us = 0;
};

2 comments on commit b44fd38

@basisbit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@getroot Does this have any effect on playback of content with b-frames maybe?

@getroot
Copy link
Member Author

@getroot getroot commented on b44fd38 Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@basisbit No, we can support b-frames after browsers support them. This is just a way to improve the quality of webrtc providers (not publishers).

Please sign in to comment.