Compare commits
6 Commits
android12_
...
filter.6
Author | SHA1 | Date | |
---|---|---|---|
e51e9c748f | |||
c79d3a28ca | |||
108fe5818e | |||
1ff357bd29 | |||
5f2f0ce595 | |||
816c158577 |
@ -14,17 +14,7 @@
|
||||
// set the decoded frame as ready for rendering, and notify
|
||||
static void
|
||||
push_frame(struct decoder *decoder) {
|
||||
bool previous_frame_skipped;
|
||||
video_buffer_offer_decoded_frame(decoder->video_buffer,
|
||||
&previous_frame_skipped);
|
||||
if (previous_frame_skipped) {
|
||||
// the previous EVENT_NEW_FRAME will consume this frame
|
||||
return;
|
||||
}
|
||||
static SDL_Event new_frame_event = {
|
||||
.type = EVENT_NEW_FRAME,
|
||||
};
|
||||
SDL_PushEvent(&new_frame_event);
|
||||
video_buffer_producer_offer_frame(decoder->video_buffer);
|
||||
}
|
||||
|
||||
void
|
||||
@ -66,7 +56,7 @@ decoder_push(struct decoder *decoder, const AVPacket *packet) {
|
||||
return false;
|
||||
}
|
||||
ret = avcodec_receive_frame(decoder->codec_ctx,
|
||||
decoder->video_buffer->decoding_frame);
|
||||
decoder->video_buffer->producer_frame);
|
||||
if (!ret) {
|
||||
// a frame was received
|
||||
push_frame(decoder);
|
||||
|
@ -168,7 +168,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {
|
||||
}
|
||||
|
||||
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)) {
|
||||
return;
|
||||
}
|
||||
@ -176,6 +176,6 @@ fps_counter_add_skipped_frame(struct fps_counter *counter) {
|
||||
sc_mutex_lock(&counter->mutex);
|
||||
uint32_t now = SDL_GetTicks();
|
||||
check_interval_expired(counter, now);
|
||||
++counter->nr_skipped;
|
||||
counter->nr_skipped += n;
|
||||
sc_mutex_unlock(&counter->mutex);
|
||||
}
|
||||
|
@ -54,6 +54,6 @@ void
|
||||
fps_counter_add_rendered_frame(struct fps_counter *counter);
|
||||
|
||||
void
|
||||
fps_counter_add_skipped_frame(struct fps_counter *counter);
|
||||
fps_counter_add_skipped_frames(struct fps_counter *counter, unsigned n);
|
||||
|
||||
#endif
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
||||
@ -266,6 +267,14 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
|
||||
free(local_fmt);
|
||||
}
|
||||
|
||||
static void
|
||||
video_buffer_on_frame_available(struct video_buffer *vb, void *userdata) {
|
||||
(void) vb;
|
||||
(void) userdata;
|
||||
|
||||
screen_on_frame_available(&screen);
|
||||
}
|
||||
|
||||
bool
|
||||
scrcpy(const struct scrcpy_options *options) {
|
||||
if (!server_init(&server)) {
|
||||
@ -332,8 +341,12 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
}
|
||||
fps_counter_initialized = true;
|
||||
|
||||
if (!video_buffer_init(&video_buffer, &fps_counter,
|
||||
options->render_expired_frames)) {
|
||||
static const struct video_buffer_callbacks video_buffer_cbs = {
|
||||
.on_frame_available = video_buffer_on_frame_available,
|
||||
};
|
||||
|
||||
if (!video_buffer_init(&video_buffer, options->render_expired_frames,
|
||||
&video_buffer_cbs, NULL)) {
|
||||
goto end;
|
||||
}
|
||||
video_buffer_initialized = true;
|
||||
@ -366,13 +379,6 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
|
||||
stream_init(&stream, server.video_socket, dec, rec);
|
||||
|
||||
// now we consumed the header values, the socket receives the video stream
|
||||
// start the stream
|
||||
if (!stream_start(&stream)) {
|
||||
goto end;
|
||||
}
|
||||
stream_started = true;
|
||||
|
||||
if (options->display) {
|
||||
if (options->control) {
|
||||
if (!controller_init(&controller, server.control_socket)) {
|
||||
@ -389,7 +395,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,
|
||||
@ -415,6 +421,13 @@ scrcpy(const struct scrcpy_options *options) {
|
||||
}
|
||||
}
|
||||
|
||||
// now we consumed the header values, the socket receives the video stream
|
||||
// start the stream
|
||||
if (!stream_start(&stream)) {
|
||||
goto end;
|
||||
}
|
||||
stream_started = true;
|
||||
|
||||
input_manager_init(&input_manager, options);
|
||||
|
||||
ret = event_loop(options);
|
||||
|
@ -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 *
|
||||
@ -452,7 +454,13 @@ 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);
|
||||
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);
|
||||
|
||||
struct size new_frame_size = {frame->width, frame->height};
|
||||
if (!prepare_for_frame(screen, new_frame_size)) {
|
||||
return false;
|
||||
@ -647,3 +655,15 @@ screen_hidpi_scale_coords(struct screen *screen, int32_t *x, int32_t *y) {
|
||||
*x = (int64_t) *x * dw / ww;
|
||||
*y = (int64_t) *y * dh / wh;
|
||||
}
|
||||
|
||||
void
|
||||
screen_on_frame_available(struct screen *screen) {
|
||||
(void) screen;
|
||||
|
||||
static SDL_Event new_frame_event = {
|
||||
.type = EVENT_NEW_FRAME,
|
||||
};
|
||||
|
||||
// Post the event on the UI thread
|
||||
SDL_PushEvent(&new_frame_event);
|
||||
}
|
||||
|
@ -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;
|
||||
@ -39,6 +41,7 @@ struct screen {
|
||||
|
||||
#define SCREEN_INITIALIZER { \
|
||||
.vb = NULL, \
|
||||
.fps_counter = NULL, \
|
||||
.window = NULL, \
|
||||
.renderer = NULL, \
|
||||
.texture = NULL, \
|
||||
@ -72,7 +75,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
|
||||
@ -137,4 +141,9 @@ screen_convert_drawable_to_frame_coords(struct screen *screen,
|
||||
void
|
||||
screen_hidpi_scale_coords(struct screen *screen, int32_t *x, int32_t *y);
|
||||
|
||||
// Notify the screen that a new frame is available in the video_buffer.
|
||||
// Called from a separate thread.
|
||||
void
|
||||
screen_on_frame_available(struct screen *screen);
|
||||
|
||||
#endif
|
||||
|
@ -7,12 +7,11 @@
|
||||
#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;
|
||||
|
||||
vb->decoding_frame = av_frame_alloc();
|
||||
if (!vb->decoding_frame) {
|
||||
video_buffer_init(struct video_buffer *vb, bool wait_consumer,
|
||||
const struct video_buffer_callbacks *cbs,
|
||||
void *cbs_userdata) {
|
||||
vb->producer_frame = av_frame_alloc();
|
||||
if (!vb->producer_frame) {
|
||||
goto error_0;
|
||||
}
|
||||
|
||||
@ -21,8 +20,8 @@ video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
|
||||
goto error_1;
|
||||
}
|
||||
|
||||
vb->rendering_frame = av_frame_alloc();
|
||||
if (!vb->rendering_frame) {
|
||||
vb->consumer_frame = av_frame_alloc();
|
||||
if (!vb->consumer_frame) {
|
||||
goto error_2;
|
||||
}
|
||||
|
||||
@ -31,8 +30,8 @@ video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
|
||||
goto error_3;
|
||||
}
|
||||
|
||||
vb->render_expired_frames = render_expired_frames;
|
||||
if (render_expired_frames) {
|
||||
vb->wait_consumer = wait_consumer;
|
||||
if (wait_consumer) {
|
||||
ok = sc_cond_init(&vb->pending_frame_consumed_cond);
|
||||
if (!ok) {
|
||||
sc_mutex_destroy(&vb->mutex);
|
||||
@ -47,89 +46,107 @@ video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
|
||||
// consumed
|
||||
vb->pending_frame_consumed = true;
|
||||
|
||||
vb->skipped = 0;
|
||||
|
||||
assert(cbs);
|
||||
assert(cbs->on_frame_available);
|
||||
vb->cbs = cbs;
|
||||
vb->cbs_userdata = cbs_userdata;
|
||||
|
||||
return true;
|
||||
|
||||
error_3:
|
||||
av_frame_free(&vb->rendering_frame);
|
||||
av_frame_free(&vb->consumer_frame);
|
||||
error_2:
|
||||
av_frame_free(&vb->pending_frame);
|
||||
error_1:
|
||||
av_frame_free(&vb->decoding_frame);
|
||||
av_frame_free(&vb->producer_frame);
|
||||
error_0:
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
video_buffer_destroy(struct video_buffer *vb) {
|
||||
if (vb->render_expired_frames) {
|
||||
if (vb->wait_consumer) {
|
||||
sc_cond_destroy(&vb->pending_frame_consumed_cond);
|
||||
}
|
||||
sc_mutex_destroy(&vb->mutex);
|
||||
av_frame_free(&vb->rendering_frame);
|
||||
av_frame_free(&vb->consumer_frame);
|
||||
av_frame_free(&vb->pending_frame);
|
||||
av_frame_free(&vb->decoding_frame);
|
||||
av_frame_free(&vb->producer_frame);
|
||||
}
|
||||
|
||||
static void
|
||||
video_buffer_swap_decoding_frame(struct video_buffer *vb) {
|
||||
video_buffer_swap_producer_frame(struct video_buffer *vb) {
|
||||
sc_mutex_assert(&vb->mutex);
|
||||
AVFrame *tmp = vb->decoding_frame;
|
||||
vb->decoding_frame = vb->pending_frame;
|
||||
AVFrame *tmp = vb->producer_frame;
|
||||
vb->producer_frame = vb->pending_frame;
|
||||
vb->pending_frame = tmp;
|
||||
}
|
||||
|
||||
static void
|
||||
video_buffer_swap_rendering_frame(struct video_buffer *vb) {
|
||||
video_buffer_swap_consumer_frame(struct video_buffer *vb) {
|
||||
sc_mutex_assert(&vb->mutex);
|
||||
AVFrame *tmp = vb->rendering_frame;
|
||||
vb->rendering_frame = vb->pending_frame;
|
||||
AVFrame *tmp = vb->consumer_frame;
|
||||
vb->consumer_frame = vb->pending_frame;
|
||||
vb->pending_frame = tmp;
|
||||
}
|
||||
|
||||
void
|
||||
video_buffer_offer_decoded_frame(struct video_buffer *vb,
|
||||
bool *previous_frame_skipped) {
|
||||
video_buffer_producer_offer_frame(struct video_buffer *vb) {
|
||||
sc_mutex_lock(&vb->mutex);
|
||||
if (vb->render_expired_frames) {
|
||||
if (vb->wait_consumer) {
|
||||
// wait for the current (expired) frame to be consumed
|
||||
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);
|
||||
video_buffer_swap_producer_frame(vb);
|
||||
|
||||
bool skipped = !vb->pending_frame_consumed;
|
||||
if (skipped) {
|
||||
++vb->skipped;
|
||||
}
|
||||
|
||||
*previous_frame_skipped = !vb->pending_frame_consumed;
|
||||
vb->pending_frame_consumed = false;
|
||||
|
||||
sc_mutex_unlock(&vb->mutex);
|
||||
|
||||
if (!skipped) {
|
||||
// If skipped, then the previous call will consume this frame, the
|
||||
// callback must not be called
|
||||
vb->cbs->on_frame_available(vb, vb->cbs_userdata);
|
||||
}
|
||||
}
|
||||
|
||||
const AVFrame *
|
||||
video_buffer_take_rendering_frame(struct video_buffer *vb) {
|
||||
video_buffer_consumer_take_frame(struct video_buffer *vb, unsigned *skipped) {
|
||||
sc_mutex_lock(&vb->mutex);
|
||||
assert(!vb->pending_frame_consumed);
|
||||
vb->pending_frame_consumed = true;
|
||||
|
||||
fps_counter_add_rendered_frame(vb->fps_counter);
|
||||
video_buffer_swap_consumer_frame(vb);
|
||||
|
||||
video_buffer_swap_rendering_frame(vb);
|
||||
|
||||
if (vb->render_expired_frames) {
|
||||
if (vb->wait_consumer) {
|
||||
// unblock video_buffer_offer_decoded_frame()
|
||||
sc_cond_signal(&vb->pending_frame_consumed_cond);
|
||||
}
|
||||
|
||||
if (skipped) {
|
||||
*skipped = vb->skipped;
|
||||
}
|
||||
vb->skipped = 0; // reset
|
||||
|
||||
sc_mutex_unlock(&vb->mutex);
|
||||
|
||||
// rendering_frame is only written from this thread, no need to lock
|
||||
return vb->rendering_frame;
|
||||
// consumer_frame is only written from this thread, no need to lock
|
||||
return vb->consumer_frame;
|
||||
}
|
||||
|
||||
void
|
||||
video_buffer_interrupt(struct video_buffer *vb) {
|
||||
if (vb->render_expired_frames) {
|
||||
if (vb->wait_consumer) {
|
||||
sc_mutex_lock(&vb->mutex);
|
||||
vb->interrupted = true;
|
||||
sc_mutex_unlock(&vb->mutex);
|
||||
|
@ -13,53 +13,62 @@ typedef struct AVFrame AVFrame;
|
||||
|
||||
/**
|
||||
* There are 3 frames in memory:
|
||||
* - one frame is held by the decoder (decoding_frame)
|
||||
* - one frame is held by the renderer (rendering_frame)
|
||||
* - one frame is shared between the decoder and the renderer (pending_frame)
|
||||
* - one frame is held by the producer (producer_frame)
|
||||
* - one frame is held by the consumer (consumer_frame)
|
||||
* - one frame is shared between the producer and the consumer (pending_frame)
|
||||
*
|
||||
* The decoder decodes a packet into the decoding_frame (it may takes time).
|
||||
* The producer generates a frame into the producer_frame (it may takes time).
|
||||
*
|
||||
* Once the frame is decoded, it calls video_buffer_offer_decoded_frame(),
|
||||
* which swaps the decoding and pending frames.
|
||||
* Once the frame is produced, it calls video_buffer_producer_offer_frame(),
|
||||
* which swaps the producer and pending frames.
|
||||
*
|
||||
* When the renderer is notified that a new frame is available, it calls
|
||||
* video_buffer_take_rendering_frame() to retrieve it, which swaps the pending
|
||||
* and rendering frames. The frame is valid until the next call, without
|
||||
* blocking the decoder.
|
||||
* When the consumer is notified that a new frame is available, it calls
|
||||
* video_buffer_consumer_take_frame() to retrieve it, which swaps the pending
|
||||
* and consumer frames. The frame is valid until the next call, without
|
||||
* blocking the producer.
|
||||
*/
|
||||
|
||||
struct video_buffer {
|
||||
AVFrame *decoding_frame;
|
||||
AVFrame *producer_frame;
|
||||
AVFrame *pending_frame;
|
||||
AVFrame *rendering_frame;
|
||||
AVFrame *consumer_frame;
|
||||
|
||||
sc_mutex mutex;
|
||||
bool render_expired_frames;
|
||||
bool wait_consumer; // never overwrite a pending frame if it is not consumed
|
||||
bool interrupted;
|
||||
|
||||
sc_cond pending_frame_consumed_cond;
|
||||
bool pending_frame_consumed;
|
||||
|
||||
struct fps_counter *fps_counter;
|
||||
unsigned skipped;
|
||||
|
||||
const struct video_buffer_callbacks *cbs;
|
||||
void *cbs_userdata;
|
||||
};
|
||||
|
||||
struct video_buffer_callbacks {
|
||||
// Called when a new frame can be consumed by
|
||||
// video_buffer_consumer_take_frame(vb)
|
||||
void (*on_frame_available)(struct video_buffer *vb, void *userdata);
|
||||
};
|
||||
|
||||
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 wait_consumer,
|
||||
const struct video_buffer_callbacks *cbs, void *cbs_userdata);
|
||||
|
||||
void
|
||||
video_buffer_destroy(struct video_buffer *vb);
|
||||
|
||||
// set the decoded frame as ready for rendering
|
||||
// the output flag is set to report whether the previous frame has been skipped
|
||||
// set the producer frame as ready for consuming
|
||||
void
|
||||
video_buffer_offer_decoded_frame(struct video_buffer *vb,
|
||||
bool *previous_frame_skipped);
|
||||
video_buffer_producer_offer_frame(struct video_buffer *vb);
|
||||
|
||||
// mark the rendering 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 output parameter "skipped" indicates how many produced frames have been
|
||||
// skipped
|
||||
const AVFrame *
|
||||
video_buffer_take_rendering_frame(struct video_buffer *vb);
|
||||
video_buffer_consumer_take_frame(struct video_buffer *vb, unsigned *skipped);
|
||||
|
||||
// wake up and avoid any blocking call
|
||||
void
|
||||
|
Reference in New Issue
Block a user