2019-03-02 22:16:55 +08:00
|
|
|
#ifndef VIDEO_BUFFER_H
|
|
|
|
#define VIDEO_BUFFER_H
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2021-01-09 02:24:51 +08:00
|
|
|
#include "common.h"
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
#include <stdbool.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2018-08-15 23:01:54 +08:00
|
|
|
#include "fps_counter.h"
|
2021-02-01 01:24:35 +08:00
|
|
|
#include "util/thread.h"
|
2018-02-07 19:25:52 +08:00
|
|
|
|
2017-12-12 22:12:07 +08:00
|
|
|
// forward declarations
|
|
|
|
typedef struct AVFrame AVFrame;
|
|
|
|
|
2021-02-01 16:38:11 +08:00
|
|
|
/**
|
2021-04-11 21:01:05 +08:00
|
|
|
* A video buffer holds 1 pending frame, which is the last frame received from
|
|
|
|
* the producer (typically, the decoder).
|
2021-02-01 16:38:11 +08:00
|
|
|
*
|
2021-04-11 21:01:05 +08:00
|
|
|
* If a pending frame has not been consumed when the producer pushes a new
|
|
|
|
* frame, then it is lost. The intent is to always provide access to the very
|
|
|
|
* last frame to minimize latency.
|
2021-02-01 16:38:11 +08:00
|
|
|
*
|
2021-04-11 21:01:05 +08:00
|
|
|
* The producer and the consumer typically do not live in the same thread.
|
|
|
|
* That's the reason why the callback on_frame_available() does not provide the
|
|
|
|
* frame as parameter: the consumer might post an event to its own thread to
|
|
|
|
* retrieve the pending frame from there, and that frame may have changed since
|
|
|
|
* the callback if producer pushed a new one in between.
|
2021-02-01 16:38:11 +08:00
|
|
|
*/
|
|
|
|
|
2019-03-02 22:16:55 +08:00
|
|
|
struct video_buffer {
|
2021-02-01 16:38:11 +08:00
|
|
|
AVFrame *pending_frame;
|
2021-04-11 21:01:05 +08:00
|
|
|
AVFrame *tmp_frame; // To preserve the pending frame on error
|
2021-02-01 16:38:11 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex mutex;
|
2021-02-01 16:38:11 +08:00
|
|
|
|
|
|
|
bool pending_frame_consumed;
|
2021-02-20 05:02:36 +08:00
|
|
|
|
|
|
|
const struct video_buffer_callbacks *cbs;
|
|
|
|
void *cbs_userdata;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct video_buffer_callbacks {
|
2021-04-11 21:01:05 +08:00
|
|
|
// Called when a new frame can be consumed.
|
2021-02-20 05:02:36 +08:00
|
|
|
// This callback is mandatory (it must not be NULL).
|
|
|
|
void (*on_frame_available)(struct video_buffer *vb, void *userdata);
|
2021-02-24 02:59:43 +08:00
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
// Called when a pending frame has been overwritten by the producer.
|
2021-02-24 02:59:43 +08:00
|
|
|
// This callback is optional (it may be NULL).
|
|
|
|
void (*on_frame_skipped)(struct video_buffer *vb, void *userdata);
|
2017-12-12 22:12:07 +08:00
|
|
|
};
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2021-04-11 21:01:05 +08:00
|
|
|
video_buffer_init(struct video_buffer *vb);
|
2019-03-03 03:09:56 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
video_buffer_destroy(struct video_buffer *vb);
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2021-02-20 05:02:36 +08:00
|
|
|
void
|
|
|
|
video_buffer_set_consumer_callbacks(struct video_buffer *vb,
|
|
|
|
const struct video_buffer_callbacks *cbs,
|
|
|
|
void *cbs_userdata);
|
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
bool
|
|
|
|
video_buffer_push(struct video_buffer *vb, const AVFrame *frame);
|
2018-02-09 02:23:24 +08:00
|
|
|
|
2021-04-11 21:01:05 +08:00
|
|
|
void
|
|
|
|
video_buffer_consume(struct video_buffer *vb, AVFrame *dst);
|
2017-12-12 22:12:07 +08:00
|
|
|
|
|
|
|
#endif
|