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
|
2019-06-07 22:55:19 +08:00
|
|
|
video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
|
|
|
|
bool render_expired_frames) {
|
|
|
|
vb->fps_counter = fps_counter;
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
vb->decoding_frame = av_frame_alloc();
|
|
|
|
if (!vb->decoding_frame) {
|
2019-03-02 22:16:55 +08:00
|
|
|
goto error_0;
|
|
|
|
}
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
vb->rendering_frame = av_frame_alloc();
|
|
|
|
if (!vb->rendering_frame) {
|
2019-03-02 22:16:55 +08:00
|
|
|
goto error_1;
|
|
|
|
}
|
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
bool ok = sc_mutex_init(&vb->mutex);
|
|
|
|
if (!ok) {
|
2019-03-02 22:16:55 +08:00
|
|
|
goto error_2;
|
|
|
|
}
|
|
|
|
|
2019-06-06 01:02:50 +08:00
|
|
|
vb->render_expired_frames = render_expired_frames;
|
|
|
|
if (render_expired_frames) {
|
2021-02-01 01:24:35 +08:00
|
|
|
ok = sc_cond_init(&vb->rendering_frame_consumed_cond);
|
|
|
|
if (!ok) {
|
|
|
|
sc_mutex_destroy(&vb->mutex);
|
2019-06-06 01:02:50 +08:00
|
|
|
goto error_2;
|
|
|
|
}
|
|
|
|
// interrupted is not used if expired frames are not rendered
|
|
|
|
// since offering a frame will never block
|
|
|
|
vb->interrupted = false;
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// there is initially no rendering frame, so consider it has already been
|
|
|
|
// consumed
|
2019-03-03 06:52:22 +08:00
|
|
|
vb->rendering_frame_consumed = true;
|
2019-03-02 22:16:55 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2019-03-02 22:16:55 +08:00
|
|
|
|
|
|
|
error_2:
|
|
|
|
av_frame_free(&vb->rendering_frame);
|
|
|
|
error_1:
|
|
|
|
av_frame_free(&vb->decoding_frame);
|
|
|
|
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) {
|
2019-06-06 01:02:50 +08:00
|
|
|
if (vb->render_expired_frames) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_destroy(&vb->rendering_frame_consumed_cond);
|
2019-06-06 01:02:50 +08:00
|
|
|
}
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_destroy(&vb->mutex);
|
2019-03-02 22:16:55 +08:00
|
|
|
av_frame_free(&vb->rendering_frame);
|
|
|
|
av_frame_free(&vb->decoding_frame);
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
video_buffer_swap_frames(struct video_buffer *vb) {
|
2021-02-01 01:56:50 +08:00
|
|
|
sc_mutex_assert(&vb->mutex);
|
2019-03-02 22:16:55 +08:00
|
|
|
AVFrame *tmp = vb->decoding_frame;
|
|
|
|
vb->decoding_frame = vb->rendering_frame;
|
|
|
|
vb->rendering_frame = tmp;
|
|
|
|
}
|
|
|
|
|
2019-03-03 07:26:48 +08:00
|
|
|
void
|
|
|
|
video_buffer_offer_decoded_frame(struct video_buffer *vb,
|
|
|
|
bool *previous_frame_skipped) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&vb->mutex);
|
2019-06-06 01:02:50 +08:00
|
|
|
if (vb->render_expired_frames) {
|
|
|
|
// wait for the current (expired) frame to be consumed
|
|
|
|
while (!vb->rendering_frame_consumed && !vb->interrupted) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_wait(&vb->rendering_frame_consumed_cond, &vb->mutex);
|
2019-06-06 01:02:50 +08:00
|
|
|
}
|
2019-06-07 22:55:19 +08:00
|
|
|
} else if (!vb->rendering_frame_consumed) {
|
|
|
|
fps_counter_add_skipped_frame(vb->fps_counter);
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
video_buffer_swap_frames(vb);
|
|
|
|
|
2019-03-03 07:26:48 +08:00
|
|
|
*previous_frame_skipped = !vb->rendering_frame_consumed;
|
2019-03-03 06:52:22 +08:00
|
|
|
vb->rendering_frame_consumed = false;
|
2019-03-02 22:16:55 +08:00
|
|
|
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&vb->mutex);
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
const AVFrame *
|
|
|
|
video_buffer_consume_rendered_frame(struct video_buffer *vb) {
|
2021-02-01 01:56:50 +08:00
|
|
|
sc_mutex_assert(&vb->mutex);
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(!vb->rendering_frame_consumed);
|
2019-03-03 06:52:22 +08:00
|
|
|
vb->rendering_frame_consumed = true;
|
2019-06-07 22:55:19 +08:00
|
|
|
fps_counter_add_rendered_frame(vb->fps_counter);
|
2019-06-06 01:02:50 +08:00
|
|
|
if (vb->render_expired_frames) {
|
|
|
|
// unblock video_buffer_offer_decoded_frame()
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_signal(&vb->rendering_frame_consumed_cond);
|
2019-06-06 01:02:50 +08:00
|
|
|
}
|
2019-03-02 22:16:55 +08:00
|
|
|
return vb->rendering_frame;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
video_buffer_interrupt(struct video_buffer *vb) {
|
2019-06-06 01:02:50 +08:00
|
|
|
if (vb->render_expired_frames) {
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_lock(&vb->mutex);
|
2019-06-06 01:02:50 +08:00
|
|
|
vb->interrupted = true;
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_mutex_unlock(&vb->mutex);
|
2019-06-06 01:02:50 +08:00
|
|
|
// wake up blocking wait
|
2021-02-01 01:24:35 +08:00
|
|
|
sc_cond_signal(&vb->rendering_frame_consumed_cond);
|
2019-06-06 01:02:50 +08:00
|
|
|
}
|
2019-03-02 22:16:55 +08:00
|
|
|
}
|