Skip to content

Commit 774570f

Browse files
committed
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 <[email protected]>
1 parent 986af12 commit 774570f

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

fakenect/fakenect.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <math.h>
3535
#include <unistd.h>
3636
#include <assert.h>
37+
#include <stdbool.h>
38+
#include <ctype.h>
3739

3840
#define GRAVITY 9.80665
3941

@@ -56,6 +58,7 @@ static void *user_video_buf = NULL;
5658
static int depth_running = 0;
5759
static int rgb_running = 0;
5860
static void *user_ptr = NULL;
61+
static bool loop_playback = true;
5962

6063
#define MAKE_RESERVED(res, fmt) (uint32_t)(((res & 0xff) << 8) | (((fmt & 0xff))))
6164
#define RESERVED_TO_RESOLUTION(reserved) (freenect_resolution)((reserved >> 8) & 0xff)
@@ -157,6 +160,14 @@ static void open_index()
157160
free(index_path);
158161
}
159162

163+
static void close_index()
164+
{
165+
fclose(index_fp);
166+
index_fp = NULL;
167+
record_prev_time = 0;
168+
playback_prev_time = 0;
169+
}
170+
160171
static char *skip_line(char *str)
161172
{
162173
char *out = strchr(str, '\n');
@@ -211,8 +222,13 @@ int freenect_process_events(freenect_context *ctx)
211222
double record_cur_time;
212223
unsigned int timestamp, data_size;
213224
char *data = NULL;
214-
if (parse_line(&type, &record_cur_time, &timestamp, &data_size, &data))
215-
return -1;
225+
if (parse_line(&type, &record_cur_time, &timestamp, &data_size, &data)) {
226+
if (loop_playback) {
227+
close_index();
228+
return 0;
229+
} else
230+
return -1;
231+
}
216232
// Sleep an amount that compensates for the original and current delays
217233
// playback_ is w.r.t. the current time
218234
// 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)
509525
exit(1);
510526
}
511527

528+
char *var = getenv("FAKENECT_LOOP");
529+
if (var) {
530+
int len = strlen(var);
531+
char tmp[len + 1];
532+
for (int i = 0; i < len; i++)
533+
tmp[i] = tolower(var[i]);
534+
tmp[len] = '\0';
535+
if (strcmp(tmp, "0") == 0 ||
536+
strcmp(tmp, "false") == 0 ||
537+
strcmp(tmp, "no") == 0 ||
538+
strcmp(tmp, "off") == 0) {
539+
loop_playback = false;
540+
}
541+
}
542+
512543
*ctx = fake_ctx;
513544

514545
read_device_info(fake_dev);

0 commit comments

Comments
 (0)