Skip to content

Commit

Permalink
Fix from roefer (https://github.com/roefer) to github issue 22 (https…
Browse files Browse the repository at this point in the history
…://github.com/OpenKinect/libfreenect/issues/#issue/22)

Fixes issues with iso transfers in libusb, updates libusb patch
Changes Packet/transfer numbers
OS X now running at full 30fps

Signed-off-by: Kyle Machulis <[email protected]>
  • Loading branch information
qdot authored and marcan committed Nov 16, 2010
1 parent 0015f13 commit 562a7b2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
6 changes: 5 additions & 1 deletion README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ root. Deal with it.

==== OS X

NOTE: AS OF 2010-11-16, WE HAVE UPDATED THIS PATCH. IF YOU HAVE
ALREADY PATCHED, PLEASE REPATCH AND REINSTALL LIBUSB TO GET OS X
WORKING AT FULL 30FPS.

You will need to pull the libusb-1.0 repo head and patch using the
files in platform/osx/. Just go to the root directory of the cloned
libusb-1.0 repo and run

patch -p0 < [path_to_OpenKinectRepo]/platform/osx/libusb-osx-kinect.diff
patch -p1 < [path_to_OpenKinectRepo]/platform/osx/libusb-osx-kinect.diff

Recompile libusb and put it wherever CMake will look (/usr/local/lib,
/usr/lib, etc...). If you're using a package manager like fink,
Expand Down
23 changes: 16 additions & 7 deletions c/lib/usb_libusb10.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@
#include <libusb.h>

#if defined(__APPLE__)
/* OS X likes things on powers of 2 boundaries, and needs WAY larger
packets/xfers to deal with what it's getting. Feel free to play
with these numbers, but keep them in multiples of 8, otherwise you
may get weirdness only seen with USB Prober on level 4 or
higher. */
#define PKTS_PER_XFER 512
#define NUM_XFERS 64
/*
From Github Issue 22 by Roefer -
https://github.com/OpenKinect/libfreenect/issues/#issue/22
The current implementation still does not reach 30 Hz on MacOS. This
is due to bad scheduling of USB transfers in libusb (Ed Note: libusb
1.0.8). A fix can be found at
http://www.informatik.uni-bremen.de/~roefer/libusb/libusb-osx-kinect.diff
(Ed Note: patch applies to libusb repo at 7da756e09fd)
In camera.c, I use PKTS_PER_XFER = 128, NUM_XFERS = 4. There are a
few rules: PKTS_PER_XFER * NUM_XFERS <= 1000, PKTS_PER_XFER % 8 == 0.
*/
#define PKTS_PER_XFER 128
#define NUM_XFERS 4
#define DEPTH_PKTBUF 2048
#define RGB_PKTBUF 2048
#else
Expand Down
49 changes: 37 additions & 12 deletions platform/osx/libusb-osx-kinect.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- libusb/libusbi.h
+++ libusb/libusbi.h
--- a/libusb/libusbi.h
+++ b/libusb/libusbi.h
@@ -288,13 +288,21 @@ struct libusb_device_handle {
unsigned char os_priv[0];
};
Expand All @@ -24,8 +24,10 @@
/* in-memory transfer layout:
*
* 1. struct usbi_transfer
--- libusb/os/darwin_usb.c
+++ libusb/os/darwin_usb.c
diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c
index 646c938..3900241 100644
--- a/libusb/os/darwin_usb.c
+++ b/libusb/os/darwin_usb.c
@@ -1137,6 +1137,8 @@ static int submit_bulk_transfer(struct usbi_transfer *itransfer) {
ret = (*(cInterface->interface))->WritePipeAsync(cInterface->interface, pipeRef, transfer->buffer,
transfer->length, darwin_async_io_callback, itransfer);
Expand All @@ -35,18 +37,29 @@
if (is_read)
ret = (*(cInterface->interface))->ReadPipeAsyncTO(cInterface->interface, pipeRef, transfer->buffer,
transfer->length, transfer->timeout, transfer->timeout,
@@ -1200,7 +1202,9 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
}
@@ -1198,9 +1200,11 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {

return darwin_to_libusb (kresult);
}
-
/* schedule for a frame a little in the future */
- frame += 2;
+ //printf("scheduling for frame += 2\n");
+ //theo modded += 2 was too short
+ frame += 64;
+ frame += 4;
+
+ if(cInterface->frames[transfer->endpoint] && frame < cInterface->frames[transfer->endpoint])
+ frame = cInterface->frames[transfer->endpoint];

/* submit the request */
if (is_read)
@@ -1243,6 +1247,8 @@ static int submit_control_transfer(struct usbi_transfer *itransfer) {
@@ -1211,6 +1215,7 @@ static int submit_iso_transfer(struct usbi_transfer *itransfer) {
kresult = (*(cInterface->interface))->WriteIsochPipeAsync(cInterface->interface, pipeRef, transfer->buffer, frame,
transfer->num_iso_packets, tpriv->isoc_framelist, darwin_async_io_callback,
itransfer);
+ cInterface->frames[transfer->endpoint] = frame + transfer->num_iso_packets / 8;

if (kresult != kIOReturnSuccess) {
usbi_err (TRANSFER_CTX (transfer), "isochronous transfer failed (dir: %s): %s", is_read ? "In" : "Out",
@@ -1243,6 +1248,8 @@ static int submit_control_transfer(struct usbi_transfer *itransfer) {
tpriv->req.pData = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
tpriv->req.completionTimeout = transfer->timeout;
tpriv->req.noDataTimeout = transfer->timeout;
Expand All @@ -55,7 +68,7 @@

/* all transfers in libusb-1.0 are async */
kresult = (*(dpriv->device))->DeviceRequestAsyncTO(dpriv->device, &(tpriv->req), darwin_async_io_callback, itransfer);
@@ -1358,6 +1364,9 @@ static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0)
@@ -1358,6 +1365,9 @@ static void darwin_async_io_callback (void *refcon, IOReturn result, void *arg0)
}

static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_t result) {
Expand All @@ -65,11 +78,23 @@
switch (result) {
case kIOReturnUnderrun:
case kIOReturnSuccess:
@@ -1372,6 +1381,7 @@ static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_
@@ -1372,6 +1382,7 @@ static int darwin_transfer_status (struct usbi_transfer *itransfer, kern_return_
return LIBUSB_TRANSFER_OVERFLOW;
case kIOUSBTransactionTimeout:
usbi_err (ITRANSFER_CTX (itransfer), "transfer error: timed out");
+ itransfer->flags |= USBI_TRANSFER_TIMED_OUT;
return LIBUSB_TRANSFER_TIMED_OUT;
default:
usbi_err (ITRANSFER_CTX (itransfer), "transfer error: %s (value = 0x%08x)", darwin_error_str (result), result);
diff --git a/libusb/os/darwin_usb.h b/libusb/os/darwin_usb.h
index a71d464..e3bdad8 100644
--- a/libusb/os/darwin_usb.h
+++ b/libusb/os/darwin_usb.h
@@ -139,6 +139,7 @@ struct darwin_device_handle_priv {
usb_interface_t **interface;
uint8_t num_endpoints;
CFRunLoopSourceRef cfSource;
+ UInt64 frames[256];
uint8_t endpoint_addrs[USB_MAXENDPOINTS];
} interfaces[USB_MAXINTERFACES];
};

0 comments on commit 562a7b2

Please sign in to comment.