Skip to content

Commit

Permalink
minimize frame skip so FPS will be calculated ASAP.
Browse files Browse the repository at this point in the history
also handled some non-proper comparison with floating point number.
until FPS calculation is ready, display '--' on screen (instead of zero)
  • Loading branch information
gilpazintel committed Jan 7, 2025
1 parent 1258860 commit 8a5c42c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
14 changes: 9 additions & 5 deletions common/rendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ namespace rs2
fps_calc()
: _counter(0),
_delta(0),
_last_timestamp(0),
_num_of_frames(0)
_last_timestamp(0.0),
_num_of_frames(0),
_skip_frames(0),//initialiy set to zero in order to minimize the time where _delta and _num_of_frames are uninitialized and the display on screen will be NA.
_last_frame_counter(0)
{}

fps_calc(const fps_calc& other)
Expand All @@ -75,10 +77,11 @@ namespace rs2
std::lock_guard<std::mutex> lock(_mtx);
if (++_counter >= _skip_frames)
{
if (_last_timestamp != 0)
if (_last_timestamp > std::numeric_limits<double>::epsilon() && frame_counter > _last_frame_counter)
{
_delta = timestamp - _last_timestamp;
_num_of_frames = frame_counter - _last_frame_counter;
_skip_frames = _skip_frames_default;// now that _delta and _num_of_frames are initialized we can recover to default value.
}

_last_frame_counter = frame_counter;
Expand All @@ -90,15 +93,16 @@ namespace rs2
double get_fps() const
{
std::lock_guard<std::mutex> lock(_mtx);
if (_delta == 0)
if (_delta < std::numeric_limits<double>::epsilon())
return 0;

return (static_cast<double>(_numerator) * _num_of_frames) / _delta;
}

private:
static const int _numerator = 1000;
static const int _skip_frames = 5;
static const int _skip_frames_default = 5;
int _skip_frames;
int _counter;
double _delta;
double _last_timestamp;
Expand Down
7 changes: 5 additions & 2 deletions common/stream-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,11 +847,14 @@ namespace rs2
stream_details.push_back(
{ "Pixel Format", rsutils::string::from() << rs2_format_to_string( profile.format() ), "" } );

double fps_tmp = fps.get_fps();
std::string fps_value = (fps_tmp > 0) ? (rsutils::string::from() << std::setprecision(2) << std::fixed << fps_tmp).str() : std::string("--");

stream_details.push_back(
{ "Hardware FPS",
rsutils::string::from() << std::setprecision( 2 ) << std::fixed << fps.get_fps(),
fps_value,
"Hardware FPS captures the number of frames per second produced by the device.\n"
"It is possible and likely that not all of these frames will make it to the application." } );
"It is possible and likely that not all of these frames will make it to the application." });

stream_details.push_back(
{ "Viewer FPS",
Expand Down

0 comments on commit 8a5c42c

Please sign in to comment.