Fix viewing fps display by keeping track of last update time, last frame count and actually calculate it based on frames sent over a period of time.

This commit is contained in:
Isaac Connor 2021-08-30 17:55:32 -04:00
parent 8199554217
commit 381fa0d08d
3 changed files with 72 additions and 62 deletions

View File

@ -265,7 +265,14 @@ void MonitorStream::processCommand(const CmdMsg *msg) {
status_data.forced = false;
status_data.buffer_level = 0;
} else {
status_data.fps = monitor->GetFPS();
FPSeconds elapsed = now - last_fps_update;
if (elapsed.count()) {
actual_fps = (frame_count - last_frame_count) / elapsed.count();
last_frame_count = frame_count;
last_fps_update = now;
}
status_data.fps = actual_fps;
status_data.capture_fps = monitor->get_capture_fps();
status_data.analysis_fps = monitor->get_analysis_fps();
status_data.state = monitor->shared_data->state;
@ -558,10 +565,9 @@ void MonitorStream::runStream() {
}
} else {
Debug(2, "Not using playback_buffer");
} // end if connkey & playback_buffer
} // end if connkey && playback_buffer
while (!zm_terminate) {
bool got_command = false;
if (feof(stdout)) {
Debug(2, "feof stdout");
break;
@ -576,6 +582,7 @@ void MonitorStream::runStream() {
now = std::chrono::system_clock::now();
bool was_paused = paused;
bool got_command = false; // commands like zoom should output a frame even if paused
if (connkey) {
while (checkCommandQueue() && !zm_terminate) {
// Loop in here until all commands are processed.
@ -597,7 +604,6 @@ void MonitorStream::runStream() {
paused_timestamp = SystemTimePoint(zm::chrono::duration_cast<Microseconds>(monitor->shared_timestamps[index]));
}
} else if (paused_image) {
Debug(1, "Clearing paused_image");
delete paused_image;
paused_image = nullptr;
}
@ -606,15 +612,12 @@ void MonitorStream::runStream() {
if (temp_read_index == temp_write_index) {
// Go back to live viewing
Debug(1, "Exceeded temporary streaming buffer");
// Clear paused flag
paused = false;
// Clear delayed_play flag
delayed = false;
replay_rate = ZM_RATE_BASE;
} else {
if (!paused) {
int temp_index = MOD_ADD(temp_read_index, 0, temp_image_buffer_count);
// Debug( 3, "tri: %d, ti: %d", temp_read_index, temp_index );
SwapImage *swap_image = &temp_image_buffer[temp_index];
if (!swap_image->valid) {
@ -628,7 +631,7 @@ void MonitorStream::runStream() {
// If the next frame is due
if (actual_delta_time > expected_delta_time) {
// Debug( 2, "eDT: %.3lf, aDT: %.3f", expected_delta_time, actual_delta_time );
if (temp_index % frame_mod == 0) {
if ((temp_index % frame_mod) == 0) {
Debug(2, "Sending delayed frame %d", temp_index);
// Send the next frame
if (!sendFrame(temp_image_buffer[temp_index].file_name, temp_image_buffer[temp_index].timestamp)) {
@ -646,7 +649,10 @@ void MonitorStream::runStream() {
SwapImage *swap_image = &temp_image_buffer[temp_read_index];
// Send the next frame
if (!sendFrame(temp_image_buffer[temp_read_index].file_name, temp_image_buffer[temp_read_index].timestamp)) {
if (!sendFrame(
temp_image_buffer[temp_read_index].file_name,
temp_image_buffer[temp_read_index].timestamp)
) {
zm_terminate = true;
}
@ -682,7 +688,7 @@ void MonitorStream::runStream() {
if (last_read_index != monitor->shared_data->last_write_index) {
// have a new image to send
int index = monitor->shared_data->last_write_index % monitor->image_buffer_count; // % shouldn't be neccessary
int index = monitor->shared_data->last_write_index % monitor->image_buffer_count;
if ((frame_mod == 1) || ((frame_count%frame_mod) == 0)) {
if (!paused && !delayed) {
last_read_index = monitor->shared_data->last_write_index;
@ -700,8 +706,6 @@ void MonitorStream::runStream() {
zm_terminate = true;
break;
}
//frame_sent = true;
//
if (frame_count == 0) {
// Chrome will not display the first frame until it receives another.
// Firefox is fine. So just send the first frame twice.
@ -801,7 +805,7 @@ void MonitorStream::runStream() {
Warning("no last_frame_sent. Shouldn't happen. frame_mod was (%d) frame_count (%d)",
frame_mod, frame_count);
}
} // end while
} // end while ! zm_terminate
if (buffered_playback) {
Debug(1, "Cleaning swap files from %s", swap_path.c_str());

View File

@ -40,7 +40,6 @@ class MonitorStream : public StreamBase {
Microseconds ttl;
int playback_buffer;
bool delayed;
int frame_count;
protected:
bool checkSwapPath(const char *path, bool create_path);
@ -61,8 +60,8 @@ class MonitorStream : public StreamBase {
temp_write_index(0),
ttl(0),
playback_buffer(0),
delayed(false),
frame_count(0) {}
delayed(false)
{}
void setStreamBuffer(int p_playback_buffer) {
playback_buffer = p_playback_buffer;

View File

@ -102,7 +102,6 @@ protected:
int last_scale;
int zoom;
int last_zoom;
double maxfps;
int bitrate;
unsigned short last_x, last_y;
unsigned short x, y;
@ -122,8 +121,14 @@ protected:
SystemTimePoint now;
SystemTimePoint last_comm_update;
double base_fps;
double effective_fps;
double maxfps;
double base_fps; // Should be capturing fps, hence a rough target
double effective_fps; // Target fps after taking max_fps into account
double actual_fps; // sliding calculated actual streaming fps achieved
SystemTimePoint last_fps_update;
int frame_count; // Count of frames sent
int last_frame_count; // Used in calculating actual_fps from frame_count - last_frame_count
int frame_mod;
SystemTimePoint last_frame_sent;
@ -152,7 +157,6 @@ public:
last_scale(DEFAULT_SCALE),
zoom(DEFAULT_ZOOM),
last_zoom(DEFAULT_ZOOM),
maxfps(DEFAULT_MAXFPS),
bitrate(DEFAULT_BITRATE),
last_x(0),
last_y(0),
@ -164,7 +168,14 @@ public:
sd(-1),
lock_fd(0),
paused(false),
step(0)
step(0),
maxfps(DEFAULT_MAXFPS),
base_fps(0.0),
effective_fps(0.0),
actual_fps(0.0),
frame_count(0),
last_frame_count(0),
frame_mod(1)
{
memset(&loc_sock_path, 0, sizeof(loc_sock_path));
memset(&loc_addr, 0, sizeof(loc_addr));
@ -172,10 +183,6 @@ public:
memset(&rem_addr, 0, sizeof(rem_addr));
memset(&sock_path_lock, 0, sizeof(sock_path_lock));
base_fps = 0.0;
effective_fps = 0.0;
frame_mod = 1;
vid_stream = nullptr;
msg = { 0, { 0 } };
}