Skip to content

Commit

Permalink
Statistics improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
XITRIX committed Jan 4, 2025
1 parent 25f9f3d commit f9adf14
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@
"text_encoding": "cpp",
"cfenv": "cpp",
"typeindex": "cpp"
}
},
"cmake.configureSettings": { "PLATFORM_SWITCH": "ON" }
}
66 changes: 42 additions & 24 deletions app/src/streaming/ffmpeg/FFmpegVideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,21 +262,24 @@ int FFmpegVideoDecoder::submit_decode_unit(PDECODE_UNIT decode_unit) {
if (decode_unit->fullLength < DECODER_BUFFER_SIZE) {
PLENTRY entry = decode_unit->bufferList;

if (m_video_decode_stats_progress.measurement_start_timestamp == 0) {
m_video_decode_stats_progress.measurement_start_timestamp = LiGetMillis();
}

if (!m_last_frame) {
m_video_decode_stats.measurement_start_timestamp = LiGetMillis();
m_last_frame = decode_unit->frameNumber;
} else {
// Any frame number greater than m_LastFrameNumber + 1 represents a
// dropped frame
m_video_decode_stats.network_dropped_frames +=
m_video_decode_stats_progress.network_dropped_frames +=
decode_unit->frameNumber - (m_last_frame + 1);
m_video_decode_stats.total_frames +=
m_video_decode_stats_progress.total_frames +=
decode_unit->frameNumber - (m_last_frame + 1);
m_last_frame = decode_unit->frameNumber;
}

m_video_decode_stats.received_frames++;
m_video_decode_stats.total_frames++;
m_video_decode_stats_progress.received_frames++;
m_video_decode_stats_progress.total_frames++;

int length = 0;
while (entry != NULL) {
Expand All @@ -289,7 +292,7 @@ int FFmpegVideoDecoder::submit_decode_unit(PDECODE_UNIT decode_unit) {
entry = entry->next;
}

m_video_decode_stats.total_reassembly_time +=
m_video_decode_stats_progress.total_reassembly_time +=
LiGetMillis() - decode_unit->receiveTimeMs;

m_frames_in++;
Expand All @@ -302,14 +305,42 @@ int FFmpegVideoDecoder::submit_decode_unit(PDECODE_UNIT decode_unit) {

if (decode(m_ffmpeg_buffer, length) == 0) {
m_frames_out++;
m_video_decode_stats.total_decode_time +=
LiGetMillis() - before_decode;

auto decodeTime = LiGetMillis() - before_decode;
m_video_decode_stats_progress.total_decode_time += decodeTime;

// Also count the frame-to-frame delay if the decoder is delaying
// frames until a subsequent frame is submitted.
m_video_decode_stats.total_decode_time +=
m_video_decode_stats_progress.total_decode_time +=
(m_frames_in - m_frames_out) * (1000 / m_stream_fps);
m_video_decode_stats.decoded_frames++;
m_video_decode_stats_progress.decoded_frames++;

const int time_interval = 200;
timeCount += decodeTime;
if (timeCount >= time_interval) {
// brls::Logger::debug("FPS: {}", frames / 5.0f);
m_video_decode_stats_cache = m_video_decode_stats_progress;
m_video_decode_stats_progress = {};

// Preserve dropped frames count
m_video_decode_stats_progress.network_dropped_frames = m_video_decode_stats_cache.network_dropped_frames;

uint64_t now = LiGetMillis();
m_video_decode_stats_cache.total_fps =
(float)m_video_decode_stats_cache.total_frames /
((float)(now - m_video_decode_stats_cache.measurement_start_timestamp) /
1000);
m_video_decode_stats_cache.received_fps =
(float)m_video_decode_stats_cache.received_frames /
((float)(now - m_video_decode_stats_cache.measurement_start_timestamp) /
1000);
m_video_decode_stats_cache.decoded_fps =
(float)m_video_decode_stats_cache.decoded_frames /
((float)(now - m_video_decode_stats_cache.measurement_start_timestamp) /
1000);

timeCount -= time_interval;
}

m_frame = get_frame(true);
if (m_frame != NULL)
Expand Down Expand Up @@ -410,18 +441,5 @@ AVFrame* FFmpegVideoDecoder::get_frame(bool native_frame) {
}

VideoDecodeStats* FFmpegVideoDecoder::video_decode_stats() {
uint64_t now = LiGetMillis();
m_video_decode_stats.total_fps =
(float)m_video_decode_stats.total_frames /
((float)(now - m_video_decode_stats.measurement_start_timestamp) /
1000);
m_video_decode_stats.received_fps =
(float)m_video_decode_stats.received_frames /
((float)(now - m_video_decode_stats.measurement_start_timestamp) /
1000);
m_video_decode_stats.decoded_fps =
(float)m_video_decode_stats.decoded_frames /
((float)(now - m_video_decode_stats.measurement_start_timestamp) /
1000);
return (VideoDecodeStats*)&m_video_decode_stats;
return (VideoDecodeStats*)&m_video_decode_stats_cache;
}
4 changes: 3 additions & 1 deletion app/src/streaming/ffmpeg/FFmpegVideoDecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class FFmpegVideoDecoder : public IFFmpegVideoDecoder {
int m_current_frame = 0, m_next_frame = 0;
uint32_t m_last_frame = 0;

VideoDecodeStats m_video_decode_stats = {};
VideoDecodeStats m_video_decode_stats_progress = {};
VideoDecodeStats m_video_decode_stats_cache = {};
uint64_t timeCount = 0;

char* m_ffmpeg_buffer = nullptr;
AVFrame* m_frame = nullptr;
Expand Down
33 changes: 23 additions & 10 deletions app/src/streaming/video/OpenGL/GLVideoRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ void GLVideoRenderer::checkAndUpdateScale(int width, int height,

void GLVideoRenderer::draw(NVGcontext* vg, int width, int height,
AVFrame* frame, int imageFormat) {
if (!m_video_render_stats.rendered_frames) {
m_video_render_stats.measurement_start_timestamp = LiGetMillis();
if (!m_video_render_stats_progress.rendered_frames) {
m_video_render_stats_progress.measurement_start_timestamp = LiGetMillis();
}

uint64_t before_render = LiGetMillis();
Expand Down Expand Up @@ -320,20 +320,33 @@ void GLVideoRenderer::draw(NVGcontext* vg, int width, int height,

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

m_video_render_stats.total_render_time += LiGetMillis() - before_render;
m_video_render_stats.rendered_frames++;
auto render_time = LiGetMillis() - before_render;
timeCount += render_time;

m_video_render_stats_progress.total_render_time += render_time;
m_video_render_stats_progress.rendered_frames++;

const int time_interval = 200;
if (timeCount >= time_interval) {
// brls::Logger::debug("FPS: {}", frames / 5.0f);
m_video_render_stats_cache = m_video_render_stats_progress;
m_video_render_stats_progress = {};

m_video_render_stats_cache.rendered_fps =
(float)m_video_render_stats_cache.rendered_frames /
((float)(LiGetMillis() -
m_video_render_stats_cache.measurement_start_timestamp) /
1000);

timeCount -= time_interval;
}

// auto code = glGetError();
// brls::Logger::error("OpenGL error: {}\n", code);
}

VideoRenderStats* GLVideoRenderer::video_render_stats() {
m_video_render_stats.rendered_fps =
(float)m_video_render_stats.rendered_frames /
((float)(LiGetMillis() -
m_video_render_stats.measurement_start_timestamp) /
1000);
return (VideoRenderStats*)&m_video_render_stats;
return (VideoRenderStats*)&m_video_render_stats_cache;
}

#endif // USE_GL_RENDERER
4 changes: 3 additions & 1 deletion app/src/streaming/video/OpenGL/GLVideoRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class GLVideoRenderer : public IVideoRenderer {
int textureWidth[PLANES_NUM_MAX];
int textureHeight[PLANES_NUM_MAX];
float borderColor[PLANES_NUM_MAX] = {0.0f, 0.5f, 0.5f};
VideoRenderStats m_video_render_stats = {};
VideoRenderStats m_video_render_stats_progress = {};
VideoRenderStats m_video_render_stats_cache = {};
uint64_t timeCount = 0;

int currentFrameTypePlanesNum = 0;
const int (*currentPlanes)[5];
Expand Down
6 changes: 3 additions & 3 deletions app/src/streaming_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ void StreamingView::draw(NVGcontext* vg, float x, float y, float width,

offset += sprintf(
&output[offset],
"Frames dropped by your network connection: %.2f%% (Total: %u)\n"
"Frames dropped by your network connection: %u\n"
"Average receive time: %.2f ms\n"
"Average decoding time: %.2f ms\n"
"Average rendering time: %.2f ms\n",
(float)stats->video_decode_stats.network_dropped_frames /
stats->video_decode_stats.total_frames * 100,
// (float)stats->video_decode_stats.network_dropped_frames / // removed % of dropped frames
// stats->video_decode_stats.total_frames * 100,
stats->video_decode_stats.network_dropped_frames,
(float)stats->video_decode_stats.total_reassembly_time /
stats->video_decode_stats.received_frames,
Expand Down

0 comments on commit f9adf14

Please sign in to comment.