feat: video auto reconnect
Clang Lint CI / lint-no-video (push) Successful in 56s
Clang Build CI / run-no-video (push) Successful in 59s
Clang Build CI / run-video (push) Successful in 1m15s
Clang Build CI / build-release (push) Successful in 2m23s
Clang Lint CI / lint-video (push) Successful in 2m14s
Clang Lint CI / lint-no-video (push) Successful in 56s
Clang Build CI / run-no-video (push) Successful in 59s
Clang Build CI / run-video (push) Successful in 1m15s
Clang Build CI / build-release (push) Successful in 2m23s
Clang Lint CI / lint-video (push) Successful in 2m14s
This commit is contained in:
+121
-99
@@ -1,5 +1,6 @@
|
||||
#include <log.h>
|
||||
#include <math.h>
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -14,7 +15,6 @@
|
||||
#include "midi.h"
|
||||
#include "project.h"
|
||||
#include "shaders.h"
|
||||
#include "shared.h"
|
||||
#include "state.h"
|
||||
#include "tempo.h"
|
||||
#include "timer.h"
|
||||
@@ -22,15 +22,18 @@
|
||||
#include "window.h"
|
||||
|
||||
static Parameters init_params;
|
||||
static SharedContext *context;
|
||||
static Context context;
|
||||
static ShaderProgram program;
|
||||
static Window *window_output;
|
||||
static Window *window_monitor;
|
||||
static Timer timer;
|
||||
static MidiDevice midi;
|
||||
static bool trace_midi;
|
||||
static Project project;
|
||||
|
||||
#ifdef VIDEO_IN
|
||||
static VideoCaptureArray video_captures;
|
||||
#endif /* VIDEO_IN */
|
||||
|
||||
static void compute_fps() {
|
||||
double fps;
|
||||
char title[STR_LEN];
|
||||
@@ -52,103 +55,108 @@ static void compute_fps() {
|
||||
window_update_title(window_monitor, title);
|
||||
}
|
||||
|
||||
context->fps = (unsigned int)round(fps);
|
||||
context.fps = (unsigned int)round(fps);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_context() {
|
||||
context = shared_init_context("/" PACKAGE "_context");
|
||||
context.stop = false;
|
||||
|
||||
context->stop = false;
|
||||
|
||||
state_init(context, &project.state_config, init_params.demo,
|
||||
state_init(&context, &project.state_config, init_params.demo,
|
||||
init_params.auto_random, init_params.auto_random_cycle,
|
||||
init_params.base_tempo, init_params.load_state);
|
||||
|
||||
memset(context->input_resolutions, 0, sizeof(context->input_resolutions));
|
||||
#ifdef VIDEO_IN
|
||||
memset(context.input_resolutions, 0, sizeof(context.input_resolutions));
|
||||
memset(context.input_formats, 0, sizeof(context.input_formats));
|
||||
memset(context.input_fps, 0, sizeof(context.input_fps));
|
||||
memset(context.input_swap, 0, sizeof(context.input_swap));
|
||||
#endif /* VIDEO_IN */
|
||||
}
|
||||
|
||||
static void free_context() { shared_close_context(context); }
|
||||
|
||||
static void reload_shader(unsigned int i) {
|
||||
shaders_update(&program, &project.fragment_shaders[i][0], i, &project);
|
||||
}
|
||||
|
||||
#ifdef VIDEO_IN
|
||||
|
||||
static void init_inputs() {
|
||||
context->inputs.length = init_params.video_in.length;
|
||||
video_captures.length = init_params.video_in.length;
|
||||
|
||||
for (unsigned int i = 0; i < init_params.video_in.length; i++) {
|
||||
video_init(&context->inputs.values[i], init_params.video_in.values[i],
|
||||
video_init(&video_captures.values[i], init_params.video_in.values[i],
|
||||
init_params.video_size);
|
||||
|
||||
if (!context->inputs.values[i].error) {
|
||||
context->input_resolutions[i][0] = context->inputs.values[i].width;
|
||||
context->input_resolutions[i][1] = context->inputs.values[i].height;
|
||||
context->inputs.values[i].needs_reload = false;
|
||||
context->inputs.values[i].disconnected = false;
|
||||
if (!video_captures.values[i].error) {
|
||||
context.input_resolutions[i][0] = video_captures.values[i].width;
|
||||
context.input_resolutions[i][1] = video_captures.values[i].height;
|
||||
context.input_formats[i] = video_captures.values[i].pixelformat;
|
||||
video_captures.values[i].needs_reload = false;
|
||||
video_captures.values[i].disconnected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool reconnect_video_captures() {
|
||||
for (unsigned int i = 0; i < init_params.video_in.length; i++) {
|
||||
if (context->inputs.values[i].disconnected) {
|
||||
video_init(&context->inputs.values[i], init_params.video_in.values[i],
|
||||
init_params.video_size);
|
||||
static void start_video_background_read(VideoCapture *video_capture,
|
||||
Context *context, int input_index,
|
||||
bool trace_fps) {
|
||||
pthread_t thread;
|
||||
struct VideoBackgroundReadArgs *process_args =
|
||||
(struct VideoBackgroundReadArgs *)malloc(
|
||||
sizeof(struct VideoBackgroundReadArgs));
|
||||
process_args->capture = video_capture;
|
||||
process_args->context = context;
|
||||
process_args->input_index = input_index;
|
||||
process_args->trace_fps = trace_fps;
|
||||
pthread_create(&thread, NULL, video_background_read, (void *)process_args);
|
||||
pthread_detach(thread);
|
||||
}
|
||||
|
||||
if (!context->inputs.values[i].error) {
|
||||
context->input_resolutions[i][0] = context->inputs.values[i].width;
|
||||
context->input_resolutions[i][1] = context->inputs.values[i].height;
|
||||
static void *
|
||||
background_reconnect_video_captures(__attribute__((unused)) void *args) {
|
||||
log_info("background video capture reconnect started");
|
||||
while (!context.stop) {
|
||||
sleep(1);
|
||||
for (unsigned int i = 0; i < init_params.video_in.length; i++) {
|
||||
if (video_captures.values[i].disconnected) {
|
||||
video_free(&video_captures.values[i]);
|
||||
video_init(&video_captures.values[i], init_params.video_in.values[i],
|
||||
init_params.video_size);
|
||||
|
||||
if (!video_background_read(&context->inputs.values[i], context, i,
|
||||
init_params.trace_fps)) {
|
||||
return false;
|
||||
if (!video_captures.values[i].error) {
|
||||
context.input_resolutions[i][0] = video_captures.values[i].width;
|
||||
context.input_resolutions[i][1] = video_captures.values[i].height;
|
||||
context.input_formats[i] = video_captures.values[i].pixelformat;
|
||||
video_captures.values[i].needs_reload = true;
|
||||
video_captures.values[i].disconnected = false;
|
||||
start_video_background_read(&video_captures.values[i], &context, i,
|
||||
init_params.trace_fps);
|
||||
}
|
||||
|
||||
context->inputs.values[i].needs_reload = true;
|
||||
context->inputs.values[i].disconnected = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
static bool start_video_captures() {
|
||||
pid_t pid;
|
||||
for (unsigned int i = 0; i < context->inputs.length; i++) {
|
||||
if (!context->inputs.values[i].error &&
|
||||
!video_background_read(&context->inputs.values[i], context, i,
|
||||
init_params.trace_fps)) {
|
||||
return false;
|
||||
static void start_video_captures() {
|
||||
pthread_t thread;
|
||||
for (unsigned int i = 0; i < video_captures.length; i++) {
|
||||
if (!video_captures.values[i].error) {
|
||||
start_video_background_read(&video_captures.values[i], &context, i,
|
||||
init_params.trace_fps);
|
||||
}
|
||||
}
|
||||
if (!init_params.video_reconnect) {
|
||||
return true;
|
||||
if (init_params.video_reconnect) {
|
||||
pthread_create(&thread, NULL, background_reconnect_video_captures, NULL);
|
||||
pthread_detach(thread);
|
||||
}
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
log_error("Could not create subprocess");
|
||||
return false;
|
||||
}
|
||||
if (pid == 0) {
|
||||
return true;
|
||||
}
|
||||
log_info("background reconnect acquisition started (pid: %d)", pid);
|
||||
while (!context->stop) {
|
||||
sleep(1);
|
||||
if (!reconnect_video_captures()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static void free_video_captures() {
|
||||
for (unsigned int i = 0; i < context->inputs.length; i++) {
|
||||
for (unsigned int i = 0; i < video_captures.length; i++) {
|
||||
shaders_free_input(&program, i);
|
||||
|
||||
video_free(&context->inputs.values[i]);
|
||||
video_free(&video_captures.values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +165,7 @@ static void free_video_captures() {
|
||||
static void error_callback(int error, const char *description) {
|
||||
log_error("[GLFW] %d: %s", error, description);
|
||||
window_terminate();
|
||||
context->stop = true;
|
||||
context.stop = true;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
@@ -173,13 +181,37 @@ static void key_callback(Window *window, int key,
|
||||
log_info("[ESC] Closing...");
|
||||
window_close(window);
|
||||
} else if (event > 0) {
|
||||
state_key_event(context, &project.state_config, event, &midi);
|
||||
state_key_event(&context, &project.state_config, event, &midi);
|
||||
}
|
||||
}
|
||||
|
||||
static void midi_callback(unsigned char code, unsigned char value) {
|
||||
state_midi_event(context, &project.state_config, &midi, code, value,
|
||||
trace_midi);
|
||||
state_midi_event(&context, &project.state_config, &midi, code, value,
|
||||
init_params.trace_midi);
|
||||
}
|
||||
|
||||
static void start_state_background_write() {
|
||||
pthread_t thread;
|
||||
struct StateBackgroundWriteArgs *process_args =
|
||||
(struct StateBackgroundWriteArgs *)malloc(
|
||||
sizeof(struct StateBackgroundWriteArgs));
|
||||
process_args->context = &context;
|
||||
process_args->state_config = project.state_config;
|
||||
process_args->midi = &midi;
|
||||
pthread_create(&thread, NULL, state_background_write, process_args);
|
||||
pthread_detach(thread);
|
||||
}
|
||||
|
||||
static void start_midi_background_listen() {
|
||||
pthread_t thread;
|
||||
struct MidiBackgroundListenArgs *process_args =
|
||||
(struct MidiBackgroundListenArgs *)malloc(
|
||||
sizeof(struct MidiBackgroundListenArgs));
|
||||
process_args->device = &midi;
|
||||
process_args->context = &context;
|
||||
process_args->event_callback = midi_callback;
|
||||
pthread_create(&thread, NULL, midi_background_listen, process_args);
|
||||
pthread_detach(thread);
|
||||
}
|
||||
|
||||
static bool init(const Parameters *params) {
|
||||
@@ -196,38 +228,30 @@ static bool init(const Parameters *params) {
|
||||
#ifdef VIDEO_IN
|
||||
init_inputs();
|
||||
|
||||
if (!start_video_captures()) {
|
||||
return false;
|
||||
}
|
||||
start_video_captures();
|
||||
#endif /* VIDEO_IN */
|
||||
|
||||
midi_open(&midi, config_file_get_str(&project.config, "MIDI_HW", "hw"));
|
||||
|
||||
if (midi.error) {
|
||||
context->demo = true;
|
||||
context.demo = true;
|
||||
} else {
|
||||
trace_midi = params->trace_midi;
|
||||
|
||||
if (!midi_background_listen(&midi, context, midi_callback)) {
|
||||
return false;
|
||||
}
|
||||
start_midi_background_listen();
|
||||
}
|
||||
|
||||
if (!state_background_write(context, &project.state_config, &midi)) {
|
||||
return false;
|
||||
}
|
||||
start_state_background_write();
|
||||
|
||||
window_startup(error_callback);
|
||||
|
||||
context->tex_resolution[1] = params->internal_size;
|
||||
context.tex_resolution[1] = params->internal_size;
|
||||
|
||||
if (params->output) {
|
||||
window_output = window_init(PACKAGE " " VERSION, params->output_screen,
|
||||
params->windowed, NULL, key_callback);
|
||||
|
||||
window_use(window_output, context);
|
||||
window_use(window_output, &context);
|
||||
|
||||
shaders_init(&program, &project, context, false);
|
||||
shaders_init(&program, &project, &context, false);
|
||||
} else {
|
||||
window_output = NULL;
|
||||
}
|
||||
@@ -237,19 +261,19 @@ static bool init(const Parameters *params) {
|
||||
window_init(PACKAGE " " VERSION " (monitor)", params->monitor_screen,
|
||||
params->windowed, window_output, key_callback);
|
||||
|
||||
window_use(window_monitor, context);
|
||||
window_use(window_monitor, &context);
|
||||
|
||||
shaders_init(&program, &project, context, window_output != NULL);
|
||||
shaders_init(&program, &project, &context, window_output != NULL);
|
||||
} else {
|
||||
window_monitor = NULL;
|
||||
}
|
||||
|
||||
#ifdef VIDEO_IN
|
||||
shaders_link_inputs(&program, &project, &context->inputs);
|
||||
shaders_link_inputs(&program, &project, &video_captures);
|
||||
#endif /* VIDEO_IN */
|
||||
|
||||
if (program.error) {
|
||||
context->stop = true;
|
||||
context.stop = true;
|
||||
window_terminate();
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -273,10 +297,10 @@ static bool loop() {
|
||||
|
||||
#ifdef VIDEO_IN
|
||||
if (init_params.video_reconnect) {
|
||||
for (unsigned int i = 0; i < context->inputs.length; i++) {
|
||||
if (context->inputs.values[i].needs_reload) {
|
||||
shaders_relink_input(&program, &project, &context->inputs, i);
|
||||
context->inputs.values[i].needs_reload = false;
|
||||
for (unsigned int i = 0; i < video_captures.length; i++) {
|
||||
if (video_captures.values[i].needs_reload) {
|
||||
shaders_relink_input(&program, &project, &video_captures, i);
|
||||
video_captures.values[i].needs_reload = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -284,21 +308,21 @@ static bool loop() {
|
||||
|
||||
compute_fps();
|
||||
|
||||
context->time = window_get_time();
|
||||
context->tempo_total = (float)tempo_total(&context->tempo);
|
||||
context.time = window_get_time();
|
||||
context.tempo_total = (float)tempo_total(&context.tempo);
|
||||
|
||||
if (window_output != NULL) {
|
||||
window_use(window_output, context);
|
||||
window_use(window_output, &context);
|
||||
|
||||
shaders_compute(&program, context, false, false);
|
||||
shaders_compute(&program, &context, false, false);
|
||||
|
||||
window_refresh(window_output);
|
||||
}
|
||||
|
||||
if (window_monitor != NULL) {
|
||||
window_use(window_monitor, context);
|
||||
window_use(window_monitor, &context);
|
||||
|
||||
shaders_compute(&program, context, true, window_output != NULL);
|
||||
shaders_compute(&program, &context, true, window_output != NULL);
|
||||
|
||||
window_refresh(window_monitor);
|
||||
}
|
||||
@@ -309,22 +333,22 @@ static bool loop() {
|
||||
}
|
||||
|
||||
static void shutdown() {
|
||||
context->stop = true;
|
||||
context.stop = true;
|
||||
|
||||
if (init_params.save_state) {
|
||||
state_save(context, &project.state_config);
|
||||
state_save(&context, &project.state_config);
|
||||
}
|
||||
|
||||
shaders_free(&program);
|
||||
|
||||
if (window_output != NULL) {
|
||||
window_use(window_output, context);
|
||||
window_use(window_output, &context);
|
||||
|
||||
shaders_free_window(&program, false);
|
||||
}
|
||||
|
||||
if (window_monitor != NULL) {
|
||||
window_use(window_monitor, context);
|
||||
window_use(window_monitor, &context);
|
||||
|
||||
shaders_free_window(&program, init_params.output);
|
||||
}
|
||||
@@ -333,8 +357,6 @@ static void shutdown() {
|
||||
free_video_captures();
|
||||
#endif /* VIDEO_IN */
|
||||
|
||||
free_context();
|
||||
|
||||
project_free(&project);
|
||||
|
||||
window_terminate();
|
||||
|
||||
Reference in New Issue
Block a user