Move fps counter out of video buffer

In order to make video buffer more generic, move out its specific
responsibility to count the fps between the decoder and the renderer.
This commit is contained in:
Romain Vimont 2021-02-19 20:56:09 +01:00
parent 218636dc10
commit cb197ee3a2
9 changed files with 27 additions and 23 deletions

View file

@ -18,6 +18,7 @@ push_frame(struct decoder *decoder) {
video_buffer_offer_decoded_frame(decoder->video_buffer,
&previous_frame_skipped);
if (previous_frame_skipped) {
fps_counter_add_skipped_frame(decoder->fps_counter);
// the previous EVENT_NEW_FRAME will consume this frame
return;
}
@ -28,8 +29,10 @@ push_frame(struct decoder *decoder) {
}
void
decoder_init(struct decoder *decoder, struct video_buffer *vb) {
decoder_init(struct decoder *decoder, struct video_buffer *vb,
struct fps_counter *fps_counter) {
decoder->video_buffer = vb;
decoder->fps_counter = fps_counter;
}
bool

View file

@ -10,11 +10,14 @@ struct video_buffer;
struct decoder {
struct video_buffer *video_buffer;
struct fps_counter *fps_counter;
AVCodecContext *codec_ctx;
};
void
decoder_init(struct decoder *decoder, struct video_buffer *vb);
decoder_init(struct decoder *decoder, struct video_buffer *vb,
struct fps_counter *fps_counter);
bool
decoder_open(struct decoder *decoder, const AVCodec *codec);

View file

@ -480,9 +480,7 @@ input_manager_process_key(struct input_manager *im,
return;
case SDLK_i:
if (!shift && !repeat && down) {
struct fps_counter *fps_counter =
im->video_buffer->fps_counter;
switch_fps_counter_state(fps_counter);
switch_fps_counter_state(im->fps_counter);
}
return;
case SDLK_n:

View file

@ -16,6 +16,7 @@
struct input_manager {
struct controller *controller;
struct video_buffer *video_buffer;
struct fps_counter *fps_counter;
struct screen *screen;
// SDL reports repeated events as a boolean, but Android expects the actual

View file

@ -42,6 +42,7 @@ static struct file_handler file_handler;
static struct input_manager input_manager = {
.controller = &controller,
.video_buffer = &video_buffer,
.fps_counter = &fps_counter,
.screen = &screen,
.repeat = 0,
@ -332,8 +333,7 @@ scrcpy(const struct scrcpy_options *options) {
}
fps_counter_initialized = true;
if (!video_buffer_init(&video_buffer, &fps_counter,
options->render_expired_frames)) {
if (!video_buffer_init(&video_buffer, options->render_expired_frames)) {
goto end;
}
video_buffer_initialized = true;
@ -346,7 +346,7 @@ scrcpy(const struct scrcpy_options *options) {
file_handler_initialized = true;
}
decoder_init(&decoder, &video_buffer);
decoder_init(&decoder, &video_buffer, &fps_counter);
dec = &decoder;
}
@ -389,7 +389,7 @@ scrcpy(const struct scrcpy_options *options) {
const char *window_title =
options->window_title ? options->window_title : device_name;
screen_init(&screen, &video_buffer);
screen_init(&screen, &video_buffer, &fps_counter);
if (!screen_init_rendering(&screen, window_title, frame_size,
options->always_on_top, options->window_x,

View file

@ -192,9 +192,11 @@ screen_update_content_rect(struct screen *screen) {
}
void
screen_init(struct screen *screen, struct video_buffer *vb) {
screen_init(struct screen *screen, struct video_buffer *vb,
struct fps_counter *fps_counter) {
*screen = (struct screen) SCREEN_INITIALIZER;
screen->vb = vb;
screen->fps_counter = fps_counter;
}
static inline SDL_Texture *
@ -450,6 +452,9 @@ update_texture(struct screen *screen, const AVFrame *frame) {
static bool
screen_update_frame(struct screen *screen) {
const AVFrame *frame = video_buffer_take_rendering_frame(screen->vb);
fps_counter_add_rendered_frame(screen->fps_counter);
struct size new_frame_size = {frame->width, frame->height};
if (!prepare_for_frame(screen, new_frame_size)) {
return false;

View file

@ -14,6 +14,8 @@ struct video_buffer;
struct screen {
struct video_buffer *vb;
struct fps_counter *fps_counter;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
@ -38,6 +40,7 @@ struct screen {
#define SCREEN_INITIALIZER { \
.vb = NULL, \
.fps_counter = NULL, \
.window = NULL, \
.renderer = NULL, \
.texture = NULL, \
@ -70,7 +73,8 @@ struct screen {
// initialize default values
void
screen_init(struct screen *screen, struct video_buffer *vb);
screen_init(struct screen *screen, struct video_buffer *vb,
struct fps_counter *fps_counter);
// initialize screen, create window, renderer and texture (window is hidden)
// window_x and window_y accept SC_WINDOW_POSITION_UNDEFINED

View file

@ -7,10 +7,7 @@
#include "util/log.h"
bool
video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
bool render_expired_frames) {
vb->fps_counter = fps_counter;
video_buffer_init(struct video_buffer *vb, bool render_expired_frames) {
vb->decoding_frame = av_frame_alloc();
if (!vb->decoding_frame) {
goto error_0;
@ -95,8 +92,6 @@ video_buffer_offer_decoded_frame(struct video_buffer *vb,
while (!vb->pending_frame_consumed && !vb->interrupted) {
sc_cond_wait(&vb->pending_frame_consumed_cond, &vb->mutex);
}
} else if (!vb->pending_frame_consumed) {
fps_counter_add_skipped_frame(vb->fps_counter);
}
video_buffer_swap_decoding_frame(vb);
@ -113,8 +108,6 @@ video_buffer_take_rendering_frame(struct video_buffer *vb) {
assert(!vb->pending_frame_consumed);
vb->pending_frame_consumed = true;
fps_counter_add_rendered_frame(vb->fps_counter);
video_buffer_swap_rendering_frame(vb);
if (vb->render_expired_frames) {

View file

@ -39,13 +39,10 @@ struct video_buffer {
sc_cond pending_frame_consumed_cond;
bool pending_frame_consumed;
struct fps_counter *fps_counter;
};
bool
video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
bool render_expired_frames);
video_buffer_init(struct video_buffer *vb, bool render_expired_frames);
void
video_buffer_destroy(struct video_buffer *vb);