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