提交 e306bffb 编写于 作者: R Romain Vimont

Use a callback to notify a new decoded frame

Make the decoder independant of the SDL event mechanism.
上级 2512c532
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <libavutil/time.h> #include <libavutil/time.h>
#include <SDL2/SDL_events.h>
#include <unistd.h> #include <unistd.h>
#include "events.h" #include "events.h"
...@@ -18,18 +17,22 @@ push_frame(struct decoder *decoder) { ...@@ -18,18 +17,22 @@ 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) {
// the previous EVENT_NEW_FRAME will consume this frame // the previous callback will consume this frame
return; return;
} }
static SDL_Event new_frame_event = {
.type = EVENT_NEW_FRAME, decoder->cbs->on_new_frame(decoder, decoder->cbs_userdata);
};
SDL_PushEvent(&new_frame_event);
} }
void void
decoder_init(struct decoder *decoder, struct video_buffer *vb) { decoder_init(struct decoder *decoder, struct video_buffer *vb,
const struct decoder_callbacks *cbs, void *cbs_userdata) {
decoder->video_buffer = vb; decoder->video_buffer = vb;
assert(cbs);
assert(cbs->on_new_frame);
decoder->cbs = cbs;
decoder->cbs_userdata = cbs_userdata;
} }
bool bool
......
...@@ -11,10 +11,20 @@ struct video_buffer; ...@@ -11,10 +11,20 @@ struct video_buffer;
struct decoder { struct decoder {
struct video_buffer *video_buffer; struct video_buffer *video_buffer;
AVCodecContext *codec_ctx; AVCodecContext *codec_ctx;
const struct decoder_callbacks *cbs;
void *cbs_userdata;
};
struct decoder_callbacks {
// Called when a new frame can be consumed by
// video_buffer_take_rendering_frame(decoder->video_buffer).
void (*on_new_frame)(struct decoder *decoder, void *userdata);
}; };
void void
decoder_init(struct decoder *decoder, struct video_buffer *vb); decoder_init(struct decoder *decoder, struct video_buffer *vb,
const struct decoder_callbacks *cbs, void *cbs_userdata);
bool bool
decoder_open(struct decoder *decoder, const AVCodec *codec); decoder_open(struct decoder *decoder, const AVCodec *codec);
......
...@@ -306,6 +306,17 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) { ...@@ -306,6 +306,17 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
free(local_fmt); free(local_fmt);
} }
static void
decoder_on_new_frame(struct decoder *decoder, void *userdata) {
(void) decoder;
(void) userdata;
static SDL_Event new_frame_event = {
.type = EVENT_NEW_FRAME,
};
SDL_PushEvent(&new_frame_event);
}
bool bool
scrcpy(const struct scrcpy_options *options) { scrcpy(const struct scrcpy_options *options) {
if (!server_init(&server)) { if (!server_init(&server)) {
...@@ -386,7 +397,11 @@ scrcpy(const struct scrcpy_options *options) { ...@@ -386,7 +397,11 @@ scrcpy(const struct scrcpy_options *options) {
file_handler_initialized = true; file_handler_initialized = true;
} }
decoder_init(&decoder, &video_buffer); static const struct decoder_callbacks decoder_cbs = {
.on_new_frame = decoder_on_new_frame,
};
decoder_init(&decoder, &video_buffer, &decoder_cbs, NULL);
dec = &decoder; dec = &decoder;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册