mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-03 22:58:51 +00:00
Expose skipped frames to the consumer
A skipped frame is detected when the producer offers a frame while the current pending frame has not been consumed. However, the producer (in practice the decoder) is not interested in the fact that a frame has been skipped, only the consumer (the renderer) is. Therefore, expose the skipped count in consumer_take_frame() instead of a flag in the producer_offer_frame(). This allows to manage the skipped and rendered frames count at the same place, and remove fps_counter from decoder.
This commit is contained in:
parent
b1c7c71160
commit
6627459cc5
8 changed files with 31 additions and 26 deletions
|
@ -14,19 +14,12 @@
|
||||||
// set the decoded frame as ready for rendering, and notify
|
// set the decoded frame as ready for rendering, and notify
|
||||||
static void
|
static void
|
||||||
push_frame(struct decoder *decoder) {
|
push_frame(struct decoder *decoder) {
|
||||||
bool previous_frame_skipped;
|
video_buffer_producer_offer_frame(decoder->video_buffer);
|
||||||
video_buffer_producer_offer_frame(decoder->video_buffer,
|
|
||||||
&previous_frame_skipped);
|
|
||||||
if (previous_frame_skipped) {
|
|
||||||
fps_counter_add_skipped_frame(decoder->fps_counter);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -10,14 +10,11 @@ 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);
|
||||||
|
|
|
@ -168,7 +168,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_add_skipped_frame(struct fps_counter *counter) {
|
fps_counter_add_skipped_frames(struct fps_counter *counter, unsigned n) {
|
||||||
if (!is_started(counter)) {
|
if (!is_started(counter)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -176,6 +176,6 @@ fps_counter_add_skipped_frame(struct fps_counter *counter) {
|
||||||
sc_mutex_lock(&counter->mutex);
|
sc_mutex_lock(&counter->mutex);
|
||||||
uint32_t now = SDL_GetTicks();
|
uint32_t now = SDL_GetTicks();
|
||||||
check_interval_expired(counter, now);
|
check_interval_expired(counter, now);
|
||||||
++counter->nr_skipped;
|
counter->nr_skipped += n;
|
||||||
sc_mutex_unlock(&counter->mutex);
|
sc_mutex_unlock(&counter->mutex);
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,6 @@ void
|
||||||
fps_counter_add_rendered_frame(struct fps_counter *counter);
|
fps_counter_add_rendered_frame(struct fps_counter *counter);
|
||||||
|
|
||||||
void
|
void
|
||||||
fps_counter_add_skipped_frame(struct fps_counter *counter);
|
fps_counter_add_skipped_frames(struct fps_counter *counter, unsigned n);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -364,7 +364,7 @@ scrcpy(const struct scrcpy_options *options) {
|
||||||
file_handler_initialized = true;
|
file_handler_initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
decoder_init(&decoder, &video_buffer, &fps_counter);
|
decoder_init(&decoder, &video_buffer);
|
||||||
dec = &decoder;
|
dec = &decoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -453,8 +453,11 @@ 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_consumer_take_frame(screen->vb);
|
unsigned skipped;
|
||||||
|
const AVFrame *frame =
|
||||||
|
video_buffer_consumer_take_frame(screen->vb, &skipped);
|
||||||
|
|
||||||
|
fps_counter_add_skipped_frames(screen->fps_counter, skipped);
|
||||||
fps_counter_add_rendered_frame(screen->fps_counter);
|
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};
|
||||||
|
|
|
@ -45,6 +45,8 @@ video_buffer_init(struct video_buffer *vb, bool wait_consumer,
|
||||||
// there is initially no frame, so consider it has already been consumed
|
// there is initially no frame, so consider it has already been consumed
|
||||||
vb->pending_frame_consumed = true;
|
vb->pending_frame_consumed = true;
|
||||||
|
|
||||||
|
vb->skipped = 0;
|
||||||
|
|
||||||
assert(cbs);
|
assert(cbs);
|
||||||
assert(cbs->on_frame_available);
|
assert(cbs->on_frame_available);
|
||||||
vb->cbs = cbs;
|
vb->cbs = cbs;
|
||||||
|
@ -90,8 +92,7 @@ video_buffer_swap_consumer_frame(struct video_buffer *vb) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
video_buffer_producer_offer_frame(struct video_buffer *vb,
|
video_buffer_producer_offer_frame(struct video_buffer *vb) {
|
||||||
bool *previous_frame_skipped) {
|
|
||||||
sc_mutex_lock(&vb->mutex);
|
sc_mutex_lock(&vb->mutex);
|
||||||
if (vb->wait_consumer) {
|
if (vb->wait_consumer) {
|
||||||
// wait for the current (expired) frame to be consumed
|
// wait for the current (expired) frame to be consumed
|
||||||
|
@ -103,7 +104,10 @@ video_buffer_producer_offer_frame(struct video_buffer *vb,
|
||||||
video_buffer_swap_producer_frame(vb);
|
video_buffer_swap_producer_frame(vb);
|
||||||
|
|
||||||
bool skipped = !vb->pending_frame_consumed;
|
bool skipped = !vb->pending_frame_consumed;
|
||||||
*previous_frame_skipped = skipped;
|
if (skipped) {
|
||||||
|
++vb->skipped;
|
||||||
|
}
|
||||||
|
|
||||||
vb->pending_frame_consumed = false;
|
vb->pending_frame_consumed = false;
|
||||||
|
|
||||||
sc_mutex_unlock(&vb->mutex);
|
sc_mutex_unlock(&vb->mutex);
|
||||||
|
@ -116,7 +120,7 @@ video_buffer_producer_offer_frame(struct video_buffer *vb,
|
||||||
}
|
}
|
||||||
|
|
||||||
const AVFrame *
|
const AVFrame *
|
||||||
video_buffer_consumer_take_frame(struct video_buffer *vb) {
|
video_buffer_consumer_take_frame(struct video_buffer *vb, unsigned *skipped) {
|
||||||
sc_mutex_lock(&vb->mutex);
|
sc_mutex_lock(&vb->mutex);
|
||||||
assert(!vb->pending_frame_consumed);
|
assert(!vb->pending_frame_consumed);
|
||||||
vb->pending_frame_consumed = true;
|
vb->pending_frame_consumed = true;
|
||||||
|
@ -127,6 +131,12 @@ video_buffer_consumer_take_frame(struct video_buffer *vb) {
|
||||||
// unblock video_buffer_offer_decoded_frame()
|
// unblock video_buffer_offer_decoded_frame()
|
||||||
sc_cond_signal(&vb->pending_frame_consumed_cond);
|
sc_cond_signal(&vb->pending_frame_consumed_cond);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (skipped) {
|
||||||
|
*skipped = vb->skipped;
|
||||||
|
}
|
||||||
|
vb->skipped = 0; // reset
|
||||||
|
|
||||||
sc_mutex_unlock(&vb->mutex);
|
sc_mutex_unlock(&vb->mutex);
|
||||||
|
|
||||||
// consumer_frame is only written from this thread, no need to lock
|
// consumer_frame is only written from this thread, no need to lock
|
||||||
|
|
|
@ -40,6 +40,8 @@ struct video_buffer {
|
||||||
sc_cond pending_frame_consumed_cond;
|
sc_cond pending_frame_consumed_cond;
|
||||||
bool pending_frame_consumed;
|
bool pending_frame_consumed;
|
||||||
|
|
||||||
|
unsigned skipped;
|
||||||
|
|
||||||
const struct video_buffer_callbacks *cbs;
|
const struct video_buffer_callbacks *cbs;
|
||||||
void *cbs_userdata;
|
void *cbs_userdata;
|
||||||
};
|
};
|
||||||
|
@ -58,15 +60,15 @@ void
|
||||||
video_buffer_destroy(struct video_buffer *vb);
|
video_buffer_destroy(struct video_buffer *vb);
|
||||||
|
|
||||||
// set the producer frame as ready for consuming
|
// set the producer frame as ready for consuming
|
||||||
// the output flag is set to report whether the previous frame has been skipped
|
|
||||||
void
|
void
|
||||||
video_buffer_producer_offer_frame(struct video_buffer *vb,
|
video_buffer_producer_offer_frame(struct video_buffer *vb);
|
||||||
bool *previous_frame_skipped);
|
|
||||||
|
|
||||||
// mark the consumer frame as consumed and return it
|
// mark the consumer frame as consumed and return it
|
||||||
// the frame is valid until the next call to this function
|
// the frame is valid until the next call to this function
|
||||||
|
// the output parameter "skipped" indicates how many produced frames have been
|
||||||
|
// skipped
|
||||||
const AVFrame *
|
const AVFrame *
|
||||||
video_buffer_consumer_take_frame(struct video_buffer *vb);
|
video_buffer_consumer_take_frame(struct video_buffer *vb, unsigned *skipped);
|
||||||
|
|
||||||
// wake up and avoid any blocking call
|
// wake up and avoid any blocking call
|
||||||
void
|
void
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue