@@ -37,6 +37,7 @@ typedef struct buffer_ring {
37
37
uint32_t timestamp ;
38
38
int valid ; // True if middle buffer is valid
39
39
int fmt ;
40
+ int res ;
40
41
} buffer_ring_t ;
41
42
42
43
typedef struct sync_kinect {
@@ -56,8 +57,6 @@ static pthread_mutex_t runloop_lock = PTHREAD_MUTEX_INITIALIZER;
56
57
static int pending_runloop_tasks = 0 ;
57
58
static pthread_mutex_t pending_runloop_tasks_lock = PTHREAD_MUTEX_INITIALIZER ;
58
59
static pthread_cond_t pending_runloop_tasks_cond = PTHREAD_COND_INITIALIZER ;
59
- static freenect_resolution m_video_resolution = FREENECT_RESOLUTION_MEDIUM ;
60
- static freenect_resolution m_depth_resolution = FREENECT_RESOLUTION_MEDIUM ;
61
60
62
61
/* Locking Convention
63
62
Rules:
@@ -69,7 +68,7 @@ static freenect_resolution m_depth_resolution = FREENECT_RESOLUTION_MEDIUM;
69
68
- runloop_lock, buffer_ring_t.lock (NOTE: You may only have one)
70
69
*/
71
70
72
- static int alloc_buffer_ring_video (freenect_video_format fmt , buffer_ring_t * buf )
71
+ static int alloc_buffer_ring_video (freenect_resolution res , freenect_video_format fmt , buffer_ring_t * buf )
73
72
{
74
73
int sz , i ;
75
74
switch (fmt ) {
@@ -78,7 +77,7 @@ static int alloc_buffer_ring_video(freenect_video_format fmt, buffer_ring_t *buf
78
77
case FREENECT_VIDEO_IR_8BIT :
79
78
case FREENECT_VIDEO_IR_10BIT :
80
79
case FREENECT_VIDEO_IR_10BIT_PACKED :
81
- sz = freenect_find_video_mode (m_video_resolution , fmt ).bytes ;
80
+ sz = freenect_find_video_mode (res , fmt ).bytes ;
82
81
break ;
83
82
default :
84
83
printf ("Invalid video format %d\n" , fmt );
@@ -89,10 +88,11 @@ static int alloc_buffer_ring_video(freenect_video_format fmt, buffer_ring_t *buf
89
88
buf -> timestamp = 0 ;
90
89
buf -> valid = 0 ;
91
90
buf -> fmt = fmt ;
91
+ buf -> res = res ;
92
92
return 0 ;
93
93
}
94
94
95
- static int alloc_buffer_ring_depth (freenect_depth_format fmt , buffer_ring_t * buf )
95
+ static int alloc_buffer_ring_depth (freenect_resolution res , freenect_depth_format fmt , buffer_ring_t * buf )
96
96
{
97
97
int sz , i ;
98
98
switch (fmt ) {
@@ -102,7 +102,7 @@ static int alloc_buffer_ring_depth(freenect_depth_format fmt, buffer_ring_t *buf
102
102
case FREENECT_DEPTH_10BIT_PACKED :
103
103
case FREENECT_DEPTH_REGISTERED :
104
104
case FREENECT_DEPTH_MM :
105
- sz = freenect_find_depth_mode (m_depth_resolution , fmt ).bytes ;
105
+ sz = freenect_find_depth_mode (res , fmt ).bytes ;
106
106
break ;
107
107
default :
108
108
printf ("Invalid depth format %d\n" , fmt );
@@ -113,6 +113,7 @@ static int alloc_buffer_ring_depth(freenect_depth_format fmt, buffer_ring_t *buf
113
113
buf -> timestamp = 0 ;
114
114
buf -> valid = 0 ;
115
115
buf -> fmt = fmt ;
116
+ buf -> res = res ;
116
117
return 0 ;
117
118
}
118
119
@@ -126,6 +127,7 @@ static void free_buffer_ring(buffer_ring_t *buf)
126
127
buf -> timestamp = 0 ;
127
128
buf -> valid = 0 ;
128
129
buf -> fmt = -1 ;
130
+ buf -> res = -1 ;
129
131
}
130
132
131
133
static void producer_cb_inner (freenect_device * dev , void * data , uint32_t timestamp , buffer_ring_t * buf , set_buffer_t set_buffer )
@@ -219,29 +221,27 @@ static void init_thread(void)
219
221
pthread_create (& thread , NULL , init , NULL );
220
222
}
221
223
222
- static int change_video_format (sync_kinect_t * kinect , freenect_video_format fmt , freenect_resolution requested_resolution )
224
+ static int change_video_format (sync_kinect_t * kinect , freenect_resolution res , freenect_video_format fmt )
223
225
{
224
226
freenect_stop_video (kinect -> dev );
225
227
free_buffer_ring (& kinect -> video );
226
- if (alloc_buffer_ring_video (fmt , & kinect -> video ))
228
+ if (alloc_buffer_ring_video (res , fmt , & kinect -> video ))
227
229
return -1 ;
228
- freenect_set_video_mode (kinect -> dev , freenect_find_video_mode (requested_resolution , fmt ));
230
+ freenect_set_video_mode (kinect -> dev , freenect_find_video_mode (res , fmt ));
229
231
freenect_set_video_buffer (kinect -> dev , kinect -> video .bufs [2 ]);
230
232
freenect_start_video (kinect -> dev );
231
- m_video_resolution = requested_resolution ;
232
233
return 0 ;
233
234
}
234
235
235
- static int change_depth_format (sync_kinect_t * kinect , freenect_depth_format fmt , freenect_resolution requested_resolution )
236
+ static int change_depth_format (sync_kinect_t * kinect , freenect_resolution res , freenect_depth_format fmt )
236
237
{
237
238
freenect_stop_depth (kinect -> dev );
238
239
free_buffer_ring (& kinect -> depth );
239
- if (alloc_buffer_ring_depth (fmt , & kinect -> depth ))
240
+ if (alloc_buffer_ring_depth (res , fmt , & kinect -> depth ))
240
241
return -1 ;
241
- freenect_set_depth_mode (kinect -> dev , freenect_find_depth_mode (requested_resolution , fmt ));
242
+ freenect_set_depth_mode (kinect -> dev , freenect_find_depth_mode (res , fmt ));
242
243
freenect_set_depth_buffer (kinect -> dev , kinect -> depth .bufs [2 ]);
243
244
freenect_start_depth (kinect -> dev );
244
- m_depth_resolution = requested_resolution ;
245
245
return 0 ;
246
246
}
247
247
@@ -258,7 +258,9 @@ static sync_kinect_t *alloc_kinect(int index)
258
258
kinect -> depth .bufs [i ] = NULL ;
259
259
}
260
260
kinect -> video .fmt = -1 ;
261
+ kinect -> video .res = -1 ;
261
262
kinect -> depth .fmt = -1 ;
263
+ kinect -> depth .res = -1 ;
262
264
freenect_set_video_callback (kinect -> dev , video_producer_cb );
263
265
freenect_set_depth_callback (kinect -> dev , depth_producer_cb );
264
266
pthread_mutex_init (& kinect -> video .lock , NULL );
@@ -268,7 +270,7 @@ static sync_kinect_t *alloc_kinect(int index)
268
270
return kinect ;
269
271
}
270
272
271
- static int setup_kinect (int index , int fmt , int is_depth )
273
+ static int setup_kinect (int index , int res , int fmt , int is_depth )
272
274
{
273
275
pending_runloop_tasks_inc ();
274
276
pthread_mutex_lock (& runloop_lock );
@@ -299,12 +301,12 @@ static int setup_kinect(int index, int fmt, int is_depth)
299
301
else
300
302
buf = & kinects [index ]-> video ;
301
303
pthread_mutex_lock (& buf -> lock );
302
- if (buf -> fmt != fmt ) {
304
+ if (( buf -> fmt != fmt ) || ( buf -> res != res ) ) {
303
305
if (is_depth )
304
- change_depth_format (kinects [index ], (freenect_depth_format ) fmt , m_video_resolution );
306
+ change_depth_format (kinects [index ], (freenect_resolution ) res , ( freenect_depth_format ) fmt );
305
307
else
306
- change_video_format (kinects [index ], (freenect_video_format ) fmt , m_video_resolution );
307
- }
308
+ change_video_format (kinects [index ], (freenect_resolution ) res , ( freenect_video_format ) fmt );
309
+ }
308
310
pthread_mutex_unlock (& buf -> lock );
309
311
pthread_mutex_unlock (& runloop_lock );
310
312
pending_runloop_tasks_dec ();
@@ -343,7 +345,7 @@ static int runloop_enter(int index)
343
345
return -1 ;
344
346
}
345
347
if (!thread_running || !kinects [index ])
346
- if (setup_kinect (index , FREENECT_DEPTH_11BIT , 1 ))
348
+ if (setup_kinect (index , FREENECT_RESOLUTION_MEDIUM , FREENECT_DEPTH_11BIT , 1 ))
347
349
return -1 ;
348
350
349
351
pending_runloop_tasks_inc ();
@@ -357,27 +359,31 @@ static void runloop_exit()
357
359
pending_runloop_tasks_dec ();
358
360
}
359
361
360
- int freenect_sync_get_video (void * * video , uint32_t * timestamp , int index , freenect_video_format fmt )
362
+ int freenect_sync_get_video (void * * video , uint32_t * timestamp , int index ,
363
+ freenect_resolution res , freenect_video_format fmt )
361
364
{
362
365
if (index < 0 || index >= MAX_KINECTS ) {
363
366
printf ("Error: Invalid index [%d]\n" , index );
364
367
return -1 ;
365
368
}
366
- if (!thread_running || !kinects [index ] || kinects [index ]-> video .fmt != fmt )
367
- if (setup_kinect (index , fmt , 0 ))
369
+ if (!thread_running || !kinects [index ] || kinects [index ]-> video .fmt != fmt
370
+ || kinects [index ]-> video .res != res )
371
+ if (setup_kinect (index , res , fmt , 0 ))
368
372
return -1 ;
369
373
sync_get (video , timestamp , & kinects [index ]-> video );
370
374
return 0 ;
371
375
}
372
376
373
- int freenect_sync_get_depth (void * * depth , uint32_t * timestamp , int index , freenect_depth_format fmt )
377
+ int freenect_sync_get_depth (void * * depth , uint32_t * timestamp , int index ,
378
+ freenect_resolution res , freenect_depth_format fmt )
374
379
{
375
380
if (index < 0 || index >= MAX_KINECTS ) {
376
381
printf ("Error: Invalid index [%d]\n" , index );
377
382
return -1 ;
378
383
}
379
- if (!thread_running || !kinects [index ] || kinects [index ]-> depth .fmt != fmt )
380
- if (setup_kinect (index , fmt , 1 ))
384
+ if (!thread_running || !kinects [index ] || kinects [index ]-> depth .fmt != fmt
385
+ || kinects [index ]-> depth .res != res )
386
+ if (setup_kinect (index , res , fmt , 1 ))
381
387
return -1 ;
382
388
sync_get (depth , timestamp , & kinects [index ]-> depth );
383
389
return 0 ;
0 commit comments