2019-03-02 15:16:55 +01:00
|
|
|
#ifndef VIDEO_BUFFER_H
|
|
|
|
#define VIDEO_BUFFER_H
|
2017-12-12 15:12:07 +01:00
|
|
|
|
2021-01-08 19:24:51 +01:00
|
|
|
#include "common.h"
|
|
|
|
|
2019-03-02 23:52:22 +01:00
|
|
|
#include <stdbool.h>
|
2017-12-12 15:12:07 +01:00
|
|
|
|
2018-08-15 17:01:54 +02:00
|
|
|
#include "fps_counter.h"
|
2021-01-31 18:24:35 +01:00
|
|
|
#include "util/thread.h"
|
2018-02-07 12:25:52 +01:00
|
|
|
|
2017-12-12 15:12:07 +01:00
|
|
|
// forward declarations
|
|
|
|
typedef struct AVFrame AVFrame;
|
|
|
|
|
2021-02-01 09:38:11 +01:00
|
|
|
/**
|
|
|
|
* There are 3 frames in memory:
|
2021-02-19 21:16:57 +01:00
|
|
|
* - 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)
|
2021-02-01 09:38:11 +01:00
|
|
|
*
|
2021-02-19 21:16:57 +01:00
|
|
|
* The producer generates a frame into the producer_frame (it may takes time).
|
2021-02-01 09:38:11 +01:00
|
|
|
*
|
2021-02-19 21:16:57 +01:00
|
|
|
* Once the frame is produced, it calls video_buffer_producer_offer_frame(),
|
|
|
|
* which swaps the producer and pending frames.
|
2021-02-01 09:38:11 +01:00
|
|
|
*
|
2021-02-19 21:16:57 +01:00
|
|
|
* 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.
|
2021-02-01 09:38:11 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-02 15:16:55 +01:00
|
|
|
struct video_buffer {
|
2021-02-19 21:16:57 +01:00
|
|
|
AVFrame *producer_frame;
|
2021-02-01 09:38:11 +01:00
|
|
|
AVFrame *pending_frame;
|
2021-02-19 21:16:57 +01:00
|
|
|
AVFrame *consumer_frame;
|
2021-02-01 09:38:11 +01:00
|
|
|
|
2021-01-31 18:24:35 +01:00
|
|
|
sc_mutex mutex;
|
2021-02-01 09:38:11 +01:00
|
|
|
|
|
|
|
bool pending_frame_consumed;
|
2021-02-19 22:02:36 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
// This callback is mandatory (it must not be NULL).
|
|
|
|
void (*on_frame_available)(struct video_buffer *vb, void *userdata);
|
2021-02-23 19:59:43 +01:00
|
|
|
|
|
|
|
// Called when a pending frame has been overwritten by the producer
|
|
|
|
// This callback is optional (it may be NULL).
|
|
|
|
void (*on_frame_skipped)(struct video_buffer *vb, void *userdata);
|
2017-12-12 15:12:07 +01:00
|
|
|
};
|
|
|
|
|
2019-03-02 23:52:22 +01:00
|
|
|
bool
|
2021-04-11 15:01:05 +02:00
|
|
|
video_buffer_init(struct video_buffer *vb);
|
2019-03-02 20:09:56 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
video_buffer_destroy(struct video_buffer *vb);
|
2017-12-12 15:12:07 +01:00
|
|
|
|
2021-02-19 22:02:36 +01:00
|
|
|
void
|
|
|
|
video_buffer_set_consumer_callbacks(struct video_buffer *vb,
|
|
|
|
const struct video_buffer_callbacks *cbs,
|
|
|
|
void *cbs_userdata);
|
|
|
|
|
2021-02-19 21:16:57 +01:00
|
|
|
// set the producer frame as ready for consuming
|
2019-03-03 00:26:48 +01:00
|
|
|
void
|
2021-02-23 19:59:43 +01:00
|
|
|
video_buffer_producer_offer_frame(struct video_buffer *vb);
|
2018-02-08 19:23:24 +01:00
|
|
|
|
2021-02-19 21:16:57 +01:00
|
|
|
// mark the consumer frame as consumed and return it
|
2021-02-01 09:38:11 +01:00
|
|
|
// the frame is valid until the next call to this function
|
2019-03-02 20:09:56 +01:00
|
|
|
const AVFrame *
|
2021-02-19 21:16:57 +01:00
|
|
|
video_buffer_consumer_take_frame(struct video_buffer *vb);
|
2017-12-12 15:12:07 +01:00
|
|
|
|
|
|
|
#endif
|