From 774570fe520426b4caf86c07a6f5b5b7d65ef905 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Tue, 7 Nov 2017 16:05:20 +0000 Subject: [PATCH] fakenect: replay index in an endless loop When the last line has been handled then the index gets closed and any 'prev' state is reset so that the next call to freenect_process_events will re-open the index and start again from the first frame. Since the Kinect camera doesn't normally spontaneously stop this seems like a more natural behaviour. In case there is some use case that would prefer to avoid looping over frames then the previous behaviour can be got by setting FAKENECT_LOOP=0 (or some case variant of 'false', 'off' or 'no') Signed-off-by: Robert Bragg --- fakenect/fakenect.c | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/fakenect/fakenect.c b/fakenect/fakenect.c index c6c1d916..91f38ad1 100644 --- a/fakenect/fakenect.c +++ b/fakenect/fakenect.c @@ -34,6 +34,8 @@ #include #include #include +#include +#include #define GRAVITY 9.80665 @@ -56,6 +58,7 @@ static void *user_video_buf = NULL; static int depth_running = 0; static int rgb_running = 0; static void *user_ptr = NULL; +static bool loop_playback = true; #define MAKE_RESERVED(res, fmt) (uint32_t)(((res & 0xff) << 8) | (((fmt & 0xff)))) #define RESERVED_TO_RESOLUTION(reserved) (freenect_resolution)((reserved >> 8) & 0xff) @@ -157,6 +160,14 @@ static void open_index() free(index_path); } +static void close_index() +{ + fclose(index_fp); + index_fp = NULL; + record_prev_time = 0; + playback_prev_time = 0; +} + static char *skip_line(char *str) { char *out = strchr(str, '\n'); @@ -211,8 +222,13 @@ int freenect_process_events(freenect_context *ctx) double record_cur_time; unsigned int timestamp, data_size; char *data = NULL; - if (parse_line(&type, &record_cur_time, ×tamp, &data_size, &data)) - return -1; + if (parse_line(&type, &record_cur_time, ×tamp, &data_size, &data)) { + if (loop_playback) { + close_index(); + return 0; + } else + return -1; + } // Sleep an amount that compensates for the original and current delays // playback_ is w.r.t. the current time // record_ is w.r.t. the original time period during the recording @@ -509,6 +525,21 @@ int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx) exit(1); } + char *var = getenv("FAKENECT_LOOP"); + if (var) { + int len = strlen(var); + char tmp[len + 1]; + for (int i = 0; i < len; i++) + tmp[i] = tolower(var[i]); + tmp[len] = '\0'; + if (strcmp(tmp, "0") == 0 || + strcmp(tmp, "false") == 0 || + strcmp(tmp, "no") == 0 || + strcmp(tmp, "off") == 0) { + loop_playback = false; + } + } + *ctx = fake_ctx; read_device_info(fake_dev);