Skip to content

Commit

Permalink
fix(c_sync): handle error codes to close #627
Browse files Browse the repository at this point in the history
Signed-off-by: Benn Snyder <benn.snyder@gmail.com>
  • Loading branch information
piedar committed Feb 11, 2021
1 parent d913755 commit 4d6b941
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions wrappers/c_sync/libfreenect_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,18 @@ static void *init(void *unused)
return NULL;
}

static void init_thread(void)
static int init_thread(void)
{
thread_running = 1;
freenect_init(&ctx, 0);
int ret = freenect_init(&ctx, 0);
if (ret != 0) return ret;
// We claim both the motor and the camera, because we can't know in advance
// which devices the caller will want, and the c_sync interface doesn't
// support audio, so there's no reason to claim the device needlessly.
freenect_select_subdevices(ctx, (freenect_device_flags)(FREENECT_DEVICE_MOTOR | FREENECT_DEVICE_CAMERA));
pthread_create(&thread, NULL, init, NULL);
ret = pthread_create(&thread, NULL, init, NULL);
if (ret != 0) return ret;
return 0;
}

static int change_video_format(sync_kinect_t *kinect, freenect_resolution res, freenect_video_format fmt)
Expand Down Expand Up @@ -279,8 +282,10 @@ static int setup_kinect(int index, int res, int fmt, int is_depth)
pending_runloop_tasks_inc();
pthread_mutex_lock(&runloop_lock);
int thread_running_prev = thread_running;
if (!thread_running)
init_thread();
if (!thread_running) {
int ret = init_thread();
if (ret != 0) return ret;
}
if (!kinects[index]) {
kinects[index] = alloc_kinect(index);
}
Expand Down Expand Up @@ -334,15 +339,15 @@ static int sync_get(void **data, uint32_t *timestamp, buffer_ring_t *buf)
}


/*
/*
Use this to make sure the runloop is locked and no one is in it. Then you can
call arbitrary functions from libfreenect.h in a safe way. If the kinect with
this index has not been initialized yet, then it will try to set it up. If
this index has not been initialized yet, then it will try to set it up. If
this function is successful, then you can access kinects[index]. Don't forget
to unlock the runloop when you're done.
Returns 0 if successful, nonzero if kinect[index] is unvailable
*/
*/
static int runloop_enter(int index)
{
if (index < 0 || index >= MAX_KINECTS) {
Expand All @@ -352,7 +357,7 @@ static int runloop_enter(int index)
if (!thread_running || !kinects[index])
if (setup_kinect(index, FREENECT_RESOLUTION_MEDIUM, FREENECT_DEPTH_11BIT, 1))
return -1;

pending_runloop_tasks_inc();
pthread_mutex_lock(&runloop_lock);
return 0;
Expand Down Expand Up @@ -407,7 +412,7 @@ int freenect_sync_get_tilt_state(freenect_raw_tilt_state **state, int index)
{
if (runloop_enter(index)) return -1;
freenect_update_tilt_state(kinects[index]->dev);
*state = freenect_get_tilt_state(kinects[index]->dev);
*state = freenect_get_tilt_state(kinects[index]->dev);
runloop_exit();
return 0;
}
Expand Down

0 comments on commit 4d6b941

Please sign in to comment.