Skip to content

Commit

Permalink
fakenect: replay index in an endless loop
Browse files Browse the repository at this point in the history
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 <robert@impossible.com>
  • Loading branch information
rib committed Nov 28, 2017
1 parent 986af12 commit 774570f
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions fakenect/fakenect.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <math.h>
#include <unistd.h>
#include <assert.h>
#include <stdbool.h>
#include <ctype.h>

#define GRAVITY 9.80665

Expand All @@ -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)
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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, &timestamp, &data_size, &data))
return -1;
if (parse_line(&type, &record_cur_time, &timestamp, &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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 774570f

Please sign in to comment.