forked from OpenKinect/libfreenect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fakenect.c
421 lines (378 loc) · 11.7 KB
/
fakenect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* This file is part of the OpenKinect Project. http://www.openkinect.org
*
* Copyright (c) 2010 Brandyn White ([email protected])
*
* This code is licensed to you under the terms of the Apache License, version
* 2.0, or, at your option, the terms of the GNU General Public License,
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
* or the following URLs:
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.gnu.org/licenses/gpl-2.0.txt
*
* If you redistribute this file in source form, modified or unmodified, you
* may:
* 1) Leave this header intact and distribute it under the same terms,
* accompanying it with the APACHE20 and GPL20 files, or
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
* In all cases you must keep the copyright notice intact and include a copy
* of the CONTRIB file.
*
* Binary distributions must follow the binary distribution requirements of
* either License.
*/
#include "libfreenect.h"
#include "platform.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <assert.h>
#define GRAVITY 9.80665
// The dev and ctx are just faked with these numbers
static freenect_device *fake_dev = (freenect_device *)1234;
static freenect_context *fake_ctx = (freenect_context *)5678;
static freenect_depth_cb cur_depth_cb = NULL;
static freenect_video_cb cur_rgb_cb = NULL;
static char *input_path = NULL;
static FILE *index_fp = NULL;
static freenect_raw_tilt_state state = { 0 };
static int already_warned = 0;
static double playback_prev_time = 0.;
static double record_prev_time = 0.;
static void *depth_buffer = NULL;
static void *rgb_buffer = NULL;
static int depth_running = 0;
static int rgb_running = 0;
static void *user_ptr = NULL;
static char *one_line(FILE *fp)
{
int c;
int pos = 0;
char *out = NULL;
for (c = fgetc(fp); !(c == '\n' || c == EOF); c = fgetc(fp))
{
out = realloc(out, pos + 1);
out[pos++] = c;
}
if (out) {
out = realloc(out, pos + 1);
out[pos] = '\0';
}
return out;
}
static int get_data_size(FILE *fp)
{
int orig = ftell(fp);
fseek(fp, 0L, SEEK_END);
int out = ftell(fp);
fseek(fp, orig, SEEK_SET);
return out;
}
static int parse_line(char *type, double *cur_time, unsigned int *timestamp, unsigned int *data_size, char **data)
{
char *line = one_line(index_fp);
if (!line) {
printf("Warning: No more lines in [%s]\n", input_path);
return -1;
}
int file_path_size = strlen(input_path) + strlen(line) + 50;
char *file_path = malloc(file_path_size);
snprintf(file_path, file_path_size, "%s/%s", input_path, line);
// Open file
FILE *cur_fp = fopen(file_path, "rb");
if (!cur_fp) {
printf("Error: Cannot open file [%s]\n", file_path);
exit(1);
}
// Parse data from file name
int ret = 0;
*data_size = get_data_size(cur_fp);
sscanf(line, "%c-%lf-%u-%*s", type, cur_time, timestamp);
*data = malloc(*data_size);
if (fread(*data, *data_size, 1, cur_fp) != 1) {
printf("Error: Couldn't read entire file.\n");
ret = -1;
}
fclose(cur_fp);
free(line);
free(file_path);
return ret;
}
static void open_index()
{
input_path = getenv("FAKENECT_PATH");
if (!input_path) {
printf("Error: Environmental variable FAKENECT_PATH is not set. Set it to a path that was created using the 'record' utility.\n");
exit(1);
}
int index_path_size = strlen(input_path) + 50;
char *index_path = malloc(index_path_size);
snprintf(index_path, index_path_size, "%s/INDEX.txt", input_path);
index_fp = fopen(index_path, "rb");
if (!index_fp) {
printf("Error: Cannot open file [%s]\n", index_path);
exit(1);
}
free(index_path);
}
static char *skip_line(char *str)
{
char *out = strchr(str, '\n');
if (!out) {
printf("Error: PGM/PPM has incorrect formatting, expected a header on one line followed by a newline\n");
exit(1);
}
return out + 1;
}
int freenect_process_events(freenect_context *ctx)
{
/* This is where the magic happens. We read 1 update from the index
per call, so this needs to be called in a loop like usual. If the
index line is a Depth/RGB image the provided callback is called. If
the index line is accelerometer data, then it is used to update our
internal state. If you query for the accelerometer data you get the
last sensor reading that we have. The time delays are compensated as
best as we can to match those from the original data and current run
conditions (e.g., if it takes longer to run this code then we wait less).
*/
if (!index_fp)
open_index();
char type;
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;
// 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
if (record_prev_time != 0. && playback_prev_time != 0.)
sleep_highres((record_cur_time - record_prev_time) - (get_time() - playback_prev_time));
record_prev_time = record_cur_time;
switch (type) {
case 'd':
if (cur_depth_cb && depth_running) {
void *cur_depth = skip_line(data);
if (depth_buffer) {
memcpy(depth_buffer, cur_depth, freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT).bytes);
cur_depth = depth_buffer;
}
cur_depth_cb(fake_dev, cur_depth, timestamp);
}
break;
case 'r':
if (cur_rgb_cb && rgb_running) {
void *cur_rgb = skip_line(data);
if (rgb_buffer) {
memcpy(rgb_buffer, cur_rgb, freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB).bytes);
cur_rgb = rgb_buffer;
}
cur_rgb_cb(fake_dev, cur_rgb, timestamp);
}
break;
case 'a':
if (data_size == sizeof(state)) {
memcpy(&state, data, sizeof(state));
} else if (!already_warned) {
already_warned = 1;
printf("\n\nWarning: Accelerometer data has an unexpected"
" size [%u] instead of [%u]. The acceleration "
"and tilt data will be substituted for dummy "
"values. This data was probably made with an "
"older version of record (the upstream interface "
"changed).\n\n",
data_size, (unsigned int)sizeof state);
}
break;
}
free(data);
playback_prev_time = get_time();
return 0;
}
int freenect_process_events_timeout(freenect_context *ctx, struct timeval *timeout)
{
return freenect_process_events(ctx);
}
double freenect_get_tilt_degs(freenect_raw_tilt_state *state)
{
// NOTE: This is duped from tilt.c, this is the only function we need from there
return ((double)state->tilt_angle) / 2.;
}
freenect_raw_tilt_state* freenect_get_tilt_state(freenect_device *dev)
{
return &state;
}
freenect_tilt_status_code freenect_get_tilt_status(freenect_raw_tilt_state *state)
{
return state->tilt_status;
}
void freenect_get_mks_accel(freenect_raw_tilt_state *state, double* x, double* y, double* z)
{
//the documentation for the accelerometer (http://www.kionix.com/Product%20Sheets/KXSD9%20Product%20Brief.pdf)
//states there are 819 counts/g
*x = (double)state->accelerometer_x/FREENECT_COUNTS_PER_G*GRAVITY;
*y = (double)state->accelerometer_y/FREENECT_COUNTS_PER_G*GRAVITY;
*z = (double)state->accelerometer_z/FREENECT_COUNTS_PER_G*GRAVITY;
}
void freenect_set_depth_callback(freenect_device *dev, freenect_depth_cb cb)
{
cur_depth_cb = cb;
}
void freenect_set_video_callback(freenect_device *dev, freenect_video_cb cb)
{
cur_rgb_cb = cb;
}
int freenect_set_video_mode(freenect_device* dev, const freenect_frame_mode mode)
{
// Always say it was successful but continue to pass through the
// underlying data. Would be better to check for conflict.
return 0;
}
int freenect_set_depth_mode(freenect_device* dev, const freenect_frame_mode mode)
{
// Always say it was successful but continue to pass through the
// underlying data. Would be better to check for conflict.
return 0;
}
freenect_frame_mode freenect_find_video_mode(freenect_resolution res, freenect_video_format fmt) {
assert(FREENECT_RESOLUTION_MEDIUM == res);
assert(FREENECT_VIDEO_RGB == fmt);
// NOTE: This will leave uninitialized values if new fields are added.
// To update this line run the "record" program, look at the top output
freenect_frame_mode out = {256, 1, {0}, 921600, 640, 480, 24, 0, 30, 1};
return out;
}
int freenect_get_video_mode_count()
{
return 1;
}
freenect_frame_mode freenect_get_video_mode(int mode_num)
{
return freenect_find_video_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_VIDEO_RGB);
}
freenect_frame_mode freenect_get_current_video_mode(freenect_device *dev)
{
return freenect_get_video_mode(0);
}
freenect_frame_mode freenect_find_depth_mode(freenect_resolution res, freenect_depth_format fmt) {
assert(FREENECT_RESOLUTION_MEDIUM == res);
assert(FREENECT_DEPTH_11BIT == fmt);
// NOTE: This will leave uninitialized values if new fields are added.
// To update this line run the "record" program, look at the top output
freenect_frame_mode out = {256, 1, {0}, 614400, 640, 480, 11, 5, 30, 1};
return out;
}
int freenect_get_depth_mode_count()
{
return 1;
}
freenect_frame_mode freenect_get_depth_mode(int mode_num)
{
return freenect_find_depth_mode(FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT);
}
freenect_frame_mode freenect_get_current_depth_mode(freenect_device *dev)
{
return freenect_get_depth_mode(0);
}
int freenect_num_devices(freenect_context *ctx)
{
// Always 1 device
return 1;
}
int freenect_open_device(freenect_context *ctx, freenect_device **dev, int index)
{
// Set it to some number to allow for NULL checks
*dev = fake_dev;
return 0;
}
int freenect_open_device_by_camera_serial(freenect_context *ctx, freenect_device **dev, const char* camera_serial)
{
*dev = fake_dev;
return 0;
}
int freenect_init(freenect_context **ctx, freenect_usb_context *usb_ctx)
{
*ctx = fake_ctx;
return 0;
}
int freenect_supported_subdevices(void)
{
return FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA;
}
void freenect_select_subdevices(freenect_context *ctx, freenect_device_flags subdevs) {
// Ideally, we'd actually check for MOTOR and CAMERA and AUDIO, but for now
// we just ignore them and provide all captured data.
}
int freenect_set_depth_buffer(freenect_device *dev, void *buf)
{
depth_buffer = buf;
return 0;
}
int freenect_set_video_buffer(freenect_device *dev, void *buf)
{
rgb_buffer = buf;
return 0;
}
void freenect_set_user(freenect_device *dev, void *user)
{
user_ptr = user;
}
void *freenect_get_user(freenect_device *dev)
{
return user_ptr;
}
int freenect_start_depth(freenect_device *dev)
{
depth_running = 1;
return 0;
}
int freenect_start_video(freenect_device *dev)
{
rgb_running = 1;
return 0;
}
int freenect_stop_depth(freenect_device *dev)
{
depth_running = 0;
return 0;
}
int freenect_stop_video(freenect_device *dev)
{
rgb_running = 0;
return 0;
}
int freenect_set_video_format(freenect_device *dev, freenect_video_format fmt)
{
assert(fmt == FREENECT_VIDEO_RGB);
return 0;
}
int freenect_set_depth_format(freenect_device *dev, freenect_depth_format fmt)
{
assert(fmt == FREENECT_DEPTH_11BIT);
return 0;
}
void freenect_set_log_callback(freenect_context *ctx, freenect_log_cb cb) {}
void freenect_set_log_level(freenect_context *ctx, freenect_loglevel level) {}
int freenect_shutdown(freenect_context *ctx)
{
return 0;
}
int freenect_close_device(freenect_device *dev)
{
return 0;
}
int freenect_set_tilt_degs(freenect_device *dev, double angle)
{
return 0;
}
int freenect_set_led(freenect_device *dev, freenect_led_options option)
{
return 0;
}
int freenect_update_tilt_state(freenect_device *dev)
{
return 0;
}