Wrap SDL thread functions into scrcpy-specific API
The goal is to expose a consistent API for system tools, and paves the way to make the "core" independant of SDL in the future.
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
#include "video_buffer.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <SDL2/SDL_mutex.h>
|
||||
#include <libavutil/avutil.h>
|
||||
#include <libavformat/avformat.h>
|
||||
|
||||
#include "util/lock.h"
|
||||
#include "util/log.h"
|
||||
|
||||
bool
|
||||
@@ -13,22 +11,26 @@ video_buffer_init(struct video_buffer *vb, struct fps_counter *fps_counter,
|
||||
bool render_expired_frames) {
|
||||
vb->fps_counter = fps_counter;
|
||||
|
||||
if (!(vb->decoding_frame = av_frame_alloc())) {
|
||||
vb->decoding_frame = av_frame_alloc();
|
||||
if (!vb->decoding_frame) {
|
||||
goto error_0;
|
||||
}
|
||||
|
||||
if (!(vb->rendering_frame = av_frame_alloc())) {
|
||||
vb->rendering_frame = av_frame_alloc();
|
||||
if (!vb->rendering_frame) {
|
||||
goto error_1;
|
||||
}
|
||||
|
||||
if (!(vb->mutex = SDL_CreateMutex())) {
|
||||
bool ok = sc_mutex_init(&vb->mutex);
|
||||
if (!ok) {
|
||||
goto error_2;
|
||||
}
|
||||
|
||||
vb->render_expired_frames = render_expired_frames;
|
||||
if (render_expired_frames) {
|
||||
if (!(vb->rendering_frame_consumed_cond = SDL_CreateCond())) {
|
||||
SDL_DestroyMutex(vb->mutex);
|
||||
ok = sc_cond_init(&vb->rendering_frame_consumed_cond);
|
||||
if (!ok) {
|
||||
sc_mutex_destroy(&vb->mutex);
|
||||
goto error_2;
|
||||
}
|
||||
// interrupted is not used if expired frames are not rendered
|
||||
@@ -53,9 +55,9 @@ error_0:
|
||||
void
|
||||
video_buffer_destroy(struct video_buffer *vb) {
|
||||
if (vb->render_expired_frames) {
|
||||
SDL_DestroyCond(vb->rendering_frame_consumed_cond);
|
||||
sc_cond_destroy(&vb->rendering_frame_consumed_cond);
|
||||
}
|
||||
SDL_DestroyMutex(vb->mutex);
|
||||
sc_mutex_destroy(&vb->mutex);
|
||||
av_frame_free(&vb->rendering_frame);
|
||||
av_frame_free(&vb->decoding_frame);
|
||||
}
|
||||
@@ -70,11 +72,11 @@ video_buffer_swap_frames(struct video_buffer *vb) {
|
||||
void
|
||||
video_buffer_offer_decoded_frame(struct video_buffer *vb,
|
||||
bool *previous_frame_skipped) {
|
||||
mutex_lock(vb->mutex);
|
||||
sc_mutex_lock(&vb->mutex);
|
||||
if (vb->render_expired_frames) {
|
||||
// wait for the current (expired) frame to be consumed
|
||||
while (!vb->rendering_frame_consumed && !vb->interrupted) {
|
||||
cond_wait(vb->rendering_frame_consumed_cond, vb->mutex);
|
||||
sc_cond_wait(&vb->rendering_frame_consumed_cond, &vb->mutex);
|
||||
}
|
||||
} else if (!vb->rendering_frame_consumed) {
|
||||
fps_counter_add_skipped_frame(vb->fps_counter);
|
||||
@@ -85,7 +87,7 @@ video_buffer_offer_decoded_frame(struct video_buffer *vb,
|
||||
*previous_frame_skipped = !vb->rendering_frame_consumed;
|
||||
vb->rendering_frame_consumed = false;
|
||||
|
||||
mutex_unlock(vb->mutex);
|
||||
sc_mutex_unlock(&vb->mutex);
|
||||
}
|
||||
|
||||
const AVFrame *
|
||||
@@ -95,7 +97,7 @@ video_buffer_consume_rendered_frame(struct video_buffer *vb) {
|
||||
fps_counter_add_rendered_frame(vb->fps_counter);
|
||||
if (vb->render_expired_frames) {
|
||||
// unblock video_buffer_offer_decoded_frame()
|
||||
cond_signal(vb->rendering_frame_consumed_cond);
|
||||
sc_cond_signal(&vb->rendering_frame_consumed_cond);
|
||||
}
|
||||
return vb->rendering_frame;
|
||||
}
|
||||
@@ -103,10 +105,10 @@ video_buffer_consume_rendered_frame(struct video_buffer *vb) {
|
||||
void
|
||||
video_buffer_interrupt(struct video_buffer *vb) {
|
||||
if (vb->render_expired_frames) {
|
||||
mutex_lock(vb->mutex);
|
||||
sc_mutex_lock(&vb->mutex);
|
||||
vb->interrupted = true;
|
||||
mutex_unlock(vb->mutex);
|
||||
sc_mutex_unlock(&vb->mutex);
|
||||
// wake up blocking wait
|
||||
cond_signal(vb->rendering_frame_consumed_cond);
|
||||
sc_cond_signal(&vb->rendering_frame_consumed_cond);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user