Skip to content

Commit 2ca9019

Browse files
ofTheoqdot
authored andcommitted
Bug fixes (claim_interface call, buffer resizing) to get libfreenect C code working in OS X
Patch to libusb-1.0 (repo head 7da756e09fd97efad2b35b5cee0e2b2550aac2cb) for getting libfreenect working in OS X Integrated by Kyle Machulis <kyle@nonpolynomial.com>
1 parent 545f8cb commit 2ca9019

File tree

3 files changed

+117
-11
lines changed

3 files changed

+117
-11
lines changed

c/examples/glview.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ This code is licensed to you under the terms of the GNU GPL, version 2 or versio
1111

1212
#include <stdio.h>
1313
#include <string.h>
14-
#include <math.h>
1514
#include <libusb.h>
1615
#include "libfreenect.h"
1716

1817
#include <pthread.h>
1918

19+
#if defined(__APPLE__)
20+
#include <GLUT/glut.h>
21+
#include <OpenGL/gl.h>
22+
#include <OpenGL/glu.h>
23+
#else
2024
#include <GL/glut.h>
2125
#include <GL/gl.h>
2226
#include <GL/glu.h>
27+
#endif
28+
29+
#include <math.h>
2330

2431
pthread_t gl_thread;
2532
volatile int die = 0;
@@ -37,11 +44,11 @@ uint8_t gl_depth_back[640*480*4];
3744
uint8_t gl_rgb_front[640*480*4];
3845
uint8_t gl_rgb_back[640*480*4];
3946

40-
int gl_depth_tex;
41-
int gl_rgb_tex;
47+
GLuint gl_depth_tex;
48+
GLuint gl_rgb_tex;
4249

43-
pthread_cond_t gl_frame_cond = PTHREAD_COND_INITIALIZER;
4450

51+
pthread_cond_t gl_frame_cond = PTHREAD_COND_INITIALIZER;
4552
int got_frames = 0;
4653

4754
void DrawGLScene()
@@ -86,7 +93,6 @@ void DrawGLScene()
8693
glEnd();
8794

8895
glutSwapBuffers();
89-
printf("Frame %d\n", fcnt++);
9096
}
9197

9298
void keyPressed(unsigned char key, int x, int y)
@@ -211,6 +217,7 @@ void depthimg(uint16_t *buf, int width, int height)
211217

212218
void rgbimg(uint8_t *buf, int width, int height)
213219
{
220+
214221
int i;
215222

216223
pthread_mutex_lock(&gl_backbuf_mutex);
@@ -236,23 +243,35 @@ int main(int argc, char **argv)
236243

237244
g_argc = argc;
238245
g_argv = argv;
246+
247+
res = pthread_create(&gl_thread, NULL, gl_threadfunc, NULL);
248+
239249

240250
libusb_init(NULL);
251+
//libusb_set_debug(0, 3);
241252

242253
dev = libusb_open_device_with_vid_pid(NULL, 0x45e, 0x2ae);
243254
if (!dev) {
244255
printf("Could not open device\n");
245256
return 1;
246257
}
247-
248258
res = pthread_create(&gl_thread, NULL, gl_threadfunc, NULL);
249259
if (res) {
250260
printf("pthread_create failed\n");
251261
return 1;
252262
}
253-
263+
264+
libusb_claim_interface(dev, 0);
265+
266+
//gl_threadfunc(&none);
267+
268+
printf("device is %i\n", libusb_get_device_address(libusb_get_device(dev)));
269+
254270
cams_init(dev, depthimg, rgbimg);
255-
256-
while(!die && libusb_handle_events(NULL) == 0);
271+
272+
while(!die && libusb_handle_events(NULL) == 0 );
273+
274+
printf("-- done!\n");
275+
257276
pthread_exit(NULL);
258277
}

c/lib/cameras.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@ This code is licensed to you under the terms of the GNU GPL, version 2 or versio
1414
#include <libusb.h>
1515
#include "libfreenect.h"
1616
#include "cameras.h"
17-
17+
#include <unistd.h>
18+
19+
#if defined(__APPLE__)
20+
#define DEPTH_LEN 2048
21+
#define RGB_LEN 2048
22+
#define PKTS_PER_XFER 256
23+
#define NUM_XFERS 8
24+
#else
1825
#define DEPTH_LEN 1760
1926
#define RGB_LEN 1920
20-
2127
#define PKTS_PER_XFER 16
2228
#define NUM_XFERS 32
29+
#endif
2330

2431
static struct libusb_transfer *depth_xfers[NUM_XFERS];
2532
static struct libusb_transfer *rgb_xfers[NUM_XFERS];
@@ -48,7 +55,12 @@ uint16_t depth_frame[640*480];
4855
int depth_pos = 0;
4956

5057
uint8_t rgb_buf[2*307200];
58+
#if defined(__APPLE__)
59+
uint8_t rgb_frame[640*480*4];
60+
#else
5161
uint8_t rgb_frame[640*480*3];
62+
#endif
63+
5264
int rgb_pos = 0;
5365

5466
extern const struct caminit inits[];
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
--- libusb/libusbi.h
2+
+++ libusb/libusbi.h
3+
@@ -288,13 +288,21 @@ struct libusb_device_handle {
4+
unsigned char os_priv[0];
5+
};
6+
7+
-#define USBI_TRANSFER_TIMED_OUT (1<<0)
8+
-
9+
enum {
10+
USBI_CLOCK_MONOTONIC,
11+
USBI_CLOCK_REALTIME
12+
};
13+
14+
+/* flags for struct usbi_transfer:
15+
+ * USBI_TRANSFER_TIMED_OUT
16+
+ * USBI_TRANSFER_OS_HANDLES_TIMEOUT - set in the backend submit_transfer if the os will handle the timeout
17+
+ */
18+
+enum {
19+
+ USBI_TRANSFER_TIMED_OUT = (1 << 0),
20+
+ USBI_TRANSFER_OS_HANDLES_TIMEOUT = (1 << 1)
21+
+};
22+
+
23+
+
24+
/* in-memory transfer layout:
25+
*
26+
* 1. struct usbi_transfer
27+
--- libusb/os/darwin_usb.c
28+
+++ libusb/os/darwin_usb.c
29+
@@ -1137,6 +1137,8 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
30+
ret = (*(cInterface->interface))->WritePipeAsync(cInterface->interface, pipeRef, transfer->buffer,
31+
transfer->length, darwin_async_io_callback, itransfer);
32+
} else {
33+
+ itransfer->flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
34+
+
35+
if (is_read)
36+
ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
37+
transfer->length, transfer->timeout, transfer->timeout,
38+
@@ -1200,7 +1202,9 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
39+
}
40+
41+
/* schedule for a frame a little in the future */
42+
- frame += 2;
43+
+ //printf("scheduling for frame += 2\n");
44+
+ //theo modded += 2 was too short
45+
+ frame += 64;
46+
47+
/* submit the request */
48+
if (is_read)
49+
@@ -1243,6 +1247,8 @@ static int submit_control_transfer(struct usbi_transfer *itransfer) {
50+
tpriv->req.pData = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
51+
tpriv->req.completionTimeout = transfer->timeout;
52+
tpriv->req.noDataTimeout = transfer->timeout;
53+
+
54+
+ itransfer->flags |= USBI_TRANSFER_OS_HANDLES_TIMEOUT;
55+
56+
/* all transfers in libusb-1.0 are async */
57+
kresult = (*(dpriv->device))->DeviceRequestAsyncTO(dpriv->device, &(tpriv->req), darwin_async_io_callback, itransfer);
58+
@@ -1358,6 +1364,9 @@ static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0)
59+
}
60+
61+
static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_t result) {
62+
+ if (itransfer->flags & USBI_TRANSFER_TIMED_OUT)
63+
+ result = kIOUSBTransactionTimeout;
64+
+
65+
switch (result) {
66+
case kIOReturnUnderrun:
67+
case kIOReturnSuccess:
68+
@@ -1372,6 +1381,7 @@ static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_
69+
return LIBUSB_TRANSFER_OVERFLOW;
70+
case kIOUSBTransactionTimeout:
71+
usbi_err (ITRANSFER_CTX (itransfer), "transfer error: timed out");
72+
+ itransfer->flags |= USBI_TRANSFER_TIMED_OUT;
73+
return LIBUSB_TRANSFER_TIMED_OUT;
74+
default:
75+
usbi_err (ITRANSFER_CTX (itransfer), "transfer error: %s (value = 0x%08x)", darwin_error_str (result), result);

0 commit comments

Comments
 (0)