2019-03-02 22:16:55 +08:00
|
|
|
#include "video_buffer.h"
|
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
#include <assert.h>
|
2019-03-02 22:16:55 +08:00
|
|
|
#include <libavutil/avutil.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/log.h"
|
2019-03-02 22:16:55 +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) {
|
2021-02-20 04:16:57 +08:00
|
|
|
vb->producer_frame = av_frame_alloc();
|
|
|
|
if (!vb->producer_frame) {
|
2019-03-02 22:16:55 +08:00
|
|
|
goto error_0;
|
|
|
|
}
|
|
|
|
|
2021-02-01 16:38:11 +08:00
|
|
|
vb->pending_frame = av_frame_alloc();
|
|
|
|
if (!vb->pending_frame) {
|
|
|
|
goto error_1;
|
|
|
|
}
|
|
|
|
|
2021-02-20 04:16:57 +08:00
|
|
|
vb->consumer_frame = av_frame_alloc();
|
|
|
|
if (!vb->consumer_frame) {
|
2021-02-01 16:38:11 +08:00
|
|
|
goto error_2;
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
bool ok = sc_mutex_init(&vb->mutex);
|
|
|
|
if (!ok) {
|
2021-02-01 16:38:11 +08:00
|
|
|
goto error_3;
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-20 04:16:57 +08:00
|
|
|
// there is initially no frame, so consider it has already been consumed
|
2021-02-01 16:38:11 +08:00
|
|
|
vb->pending_frame_consumed = true;
|
2019-03-02 22:16:55 +08:00
|
|
|
|
2021-02-20 05:02:36 +08:00
|
|
|
// The callbacks must be set by the consumer via
|
|
|
|
// video_buffer_set_consumer_callbacks()
|
|
|
|
vb->cbs = NULL;
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2019-03-02 22:16:55 +08:00
|
|
|
|
2021-02-01 16:38:11 +08:00
|
|
|
error_3:
|
2021-02-20 04:16:57 +08:00
|
|
|
av_frame_free(&vb->consumer_frame);
|
2021-02-01 16:38:11 +08:00
|
|
|
error_2:
|
|
|
|
av_frame_free(&vb->pending_frame);
|
2019-03-02 22:16:55 +08:00
|
|
|
error_1:
|
2021-02-20 04:16:57 +08:00
|
|
|
av_frame_free(&vb->producer_frame);
|
2019-03-02 22:16:55 +08:00
|
|
|
error_0:
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
video_buffer_destroy(struct video_buffer *vb) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_destroy(&vb->mutex);
|
2021-02-20 04:16:57 +08:00
|
|
|
av_frame_free(&vb->consumer_frame);
|
2021-02-01 16:38:11 +08:00
|
|
|
av_frame_free(&vb->pending_frame);
|
2021-02-20 04:16:57 +08:00
|
|
|
av_frame_free(&vb->producer_frame);
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-22 00:22:25 +08:00
|
|
|
static inline void
|
|
|
|
swap_frames(AVFrame **lhs, AVFrame **rhs) {
|
|
|
|
AVFrame *tmp = *lhs;
|
|
|
|
*lhs = *rhs;
|
|
|
|
*rhs = tmp;
|
2019-03-02 22:16:55 +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) {
|
|
|
|
assert(!vb->cbs); // must be set only once
|
|
|
|
assert(cbs);
|
|
|
|
assert(cbs->on_frame_available);
|
|
|
|
vb->cbs = cbs;
|
|
|
|
vb->cbs_userdata = cbs_userdata;
|
|
|
|
}
|
|
|
|
|
2019-03-03 07:26:48 +08:00
|
|
|
void
|
2021-02-24 02:59:43 +08:00
|
|
|
video_buffer_producer_offer_frame(struct video_buffer *vb) {
|
2021-02-20 05:02:36 +08:00
|
|
|
assert(vb->cbs);
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&vb->mutex);
|
2019-03-02 22:16:55 +08:00
|
|
|
|
2021-02-22 00:29:45 +08:00
|
|
|
av_frame_unref(vb->pending_frame);
|
2021-02-22 00:22:25 +08:00
|
|
|
swap_frames(&vb->producer_frame, &vb->pending_frame);
|
2019-03-02 22:16:55 +08:00
|
|
|
|
2021-02-20 05:02:36 +08:00
|
|
|
bool skipped = !vb->pending_frame_consumed;
|
2021-02-01 16:38:11 +08:00
|
|
|
vb->pending_frame_consumed = false;
|
2019-03-02 22:16:55 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&vb->mutex);
|
2021-02-20 05:02:36 +08:00
|
|
|
|
2021-02-24 02:59:43 +08:00
|
|
|
if (skipped) {
|
|
|
|
if (vb->cbs->on_frame_skipped)
|
|
|
|
vb->cbs->on_frame_skipped(vb, vb->cbs_userdata);
|
|
|
|
} else {
|
2021-02-20 05:02:36 +08:00
|
|
|
vb->cbs->on_frame_available(vb, vb->cbs_userdata);
|
|
|
|
}
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
const AVFrame *
|
2021-02-20 04:16:57 +08:00
|
|
|
video_buffer_consumer_take_frame(struct video_buffer *vb) {
|
2021-02-01 16:38:11 +08:00
|
|
|
sc_mutex_lock(&vb->mutex);
|
|
|
|
assert(!vb->pending_frame_consumed);
|
|
|
|
vb->pending_frame_consumed = true;
|
|
|
|
|
2021-02-22 00:22:25 +08:00
|
|
|
swap_frames(&vb->consumer_frame, &vb->pending_frame);
|
2021-02-22 00:29:45 +08:00
|
|
|
av_frame_unref(vb->pending_frame);
|
2021-02-01 16:38:11 +08:00
|
|
|
|
|
|
|
sc_mutex_unlock(&vb->mutex);
|
|
|
|
|
2021-02-20 04:16:57 +08:00
|
|
|
// consumer_frame is only written from this thread, no need to lock
|
|
|
|
return vb->consumer_frame;
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|