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

View file

@ -10,11 +10,14 @@ struct video_buffer;
struct decoder { struct decoder {
struct video_buffer *video_buffer; struct video_buffer *video_buffer;
struct fps_counter *fps_counter;
AVCodecContext *codec_ctx; AVCodecContext *codec_ctx;
}; };
void 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 bool
decoder_open(struct decoder *decoder, const AVCodec *codec); decoder_open(struct decoder *decoder, const AVCodec *codec);

View file

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

View file

@ -16,6 +16,7 @@
struct input_manager { struct input_manager {
struct controller *controller; struct controller *controller;
struct video_buffer *video_buffer; struct video_buffer *video_buffer;
struct fps_counter *fps_counter;
struct screen *screen; struct screen *screen;
// SDL reports repeated events as a boolean, but Android expects the actual // 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 = { static struct input_manager input_manager = {
.controller = &controller, .controller = &controller,
.video_buffer = &video_buffer, .video_buffer = &video_buffer,
.fps_counter = &fps_counter,
.screen = &screen, .screen = &screen,
.repeat = 0, .repeat = 0,
@ -332,8 +333,7 @@ scrcpy(const struct scrcpy_options *options) {
} }
fps_counter_initialized = true; fps_counter_initialized = true;
if (!video_buffer_init(&video_buffer, &fps_counter, if (!video_buffer_init(&video_buffer, options->render_expired_frames)) {
options->render_expired_frames)) {
goto end; goto end;
} }
video_buffer_initialized = true; video_buffer_initialized = true;
@ -346,7 +346,7 @@ scrcpy(const struct scrcpy_options *options) {
file_handler_initialized = true; file_handler_initialized = true;
} }
decoder_init(&decoder, &video_buffer); decoder_init(&decoder, &video_buffer, &fps_counter);
dec = &decoder; dec = &decoder;
} }
@ -389,7 +389,7 @@ scrcpy(const struct scrcpy_options *options) {
const char *window_title = const char *window_title =
options->window_title ? options->window_title : device_name; 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, if (!screen_init_rendering(&screen, window_title, frame_size,
options->always_on_top, options->window_x, options->always_on_top, options->window_x,

View file

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

View file

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

View file

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

View file

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