Skip to content

Commit

Permalink
Add ability to match orphan audio devices - fixes #501
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 May 31, 2017
1 parent c29570e commit 6943a00
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions src/usb_libusb10.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* either License.
*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -36,7 +37,6 @@
# define sleep(x) Sleep((x)*1000)
#endif


FN_INTERNAL int fnusb_num_devices(freenect_context *ctx)
{
libusb_device **devs; // pointer to pointer of device, used to retrieve a list of devices
Expand Down Expand Up @@ -78,16 +78,59 @@ FN_INTERNAL int fnusb_is_pid_k4w_audio(int pid)
return (pid == PID_K4W_AUDIO || pid == PID_K4W_AUDIO_ALT_1 || pid == PID_K4W_AUDIO_ALT_2);
}

FN_INTERNAL libusb_device * fnusb_find_connected_audio_device(libusb_device * camera, libusb_device ** deviceList, int cnt)
FN_INTERNAL libusb_device * fnusb_find_orphan_audio(libusb_device * camera, libusb_device ** deviceList, int count)
{
if (cnt <= 0) return NULL;
if (count <= 0) return NULL;

int cameraBusNo = libusb_get_bus_number(camera);
const int cameraBusNo = libusb_get_bus_number(camera);
if (cameraBusNo < 0) return NULL;

const size_t max_busses = 4096;
assert(cameraBusNo <= max_busses);
int numAudioByBus[max_busses] = { 0 };
libusb_device* devicesByBus_Unique[max_busses] = { NULL };

int i = 0;
for (i = 0; i < count; i++)
{
struct libusb_device_descriptor desc;
int res = libusb_get_device_descriptor (deviceList[i], &desc);
if (res < 0)
{
continue;
}

if (desc.idVendor == VID_MICROSOFT)
{
// make sure its some type of Kinect audio device
if ((desc.idProduct == PID_NUI_AUDIO || fnusb_is_pid_k4w_audio(desc.idProduct)))
{
const int audioBusNo = libusb_get_bus_number(deviceList[i]);
if (audioBusNo == cameraBusNo)
{
if (++(numAudioByBus[cameraBusNo]) == 1) {
devicesByBus_Unique[cameraBusNo] = deviceList[i];
} else {
devicesByBus_Unique[cameraBusNo] = NULL; // no duplicates!
}
}
}
}
}

return devicesByBus_Unique[cameraBusNo];
}

FN_INTERNAL libusb_device * fnusb_find_connected_audio_device(libusb_device * camera, libusb_device ** deviceList, int count)
{
if (count <= 0) return NULL;

const int cameraBusNo = libusb_get_bus_number(camera);
if (cameraBusNo < 0) return NULL;
libusb_device * cameraParent = libusb_get_parent(camera);

int i = 0;
for (i = 0; i < cnt; i++)
for (i = 0; i < count; i++)
{
struct libusb_device_descriptor desc;
int res = libusb_get_device_descriptor (deviceList[i], &desc);
Expand All @@ -101,12 +144,12 @@ FN_INTERNAL libusb_device * fnusb_find_connected_audio_device(libusb_device * ca
// make sure its some type of Kinect audio device
if ((desc.idProduct == PID_NUI_AUDIO || fnusb_is_pid_k4w_audio(desc.idProduct)))
{
int audioBusNo = libusb_get_bus_number(deviceList[i]);
const int audioBusNo = libusb_get_bus_number(deviceList[i]);
if (audioBusNo == cameraBusNo)
{
// we have a match!
// let's double check
libusb_device * audioParent = libusb_get_parent(deviceList[i]);
const libusb_device * audioParent = libusb_get_parent(deviceList[i]);
if (cameraParent == audioParent)
{
return deviceList[i];
Expand All @@ -116,7 +159,7 @@ FN_INTERNAL libusb_device * fnusb_find_connected_audio_device(libusb_device * ca
}
}

return NULL;
return fnusb_find_orphan_audio(camera, deviceList, count);
}

FN_INTERNAL int fnusb_list_device_attributes(freenect_context *ctx, struct freenect_device_attributes** attribute_list)
Expand Down

0 comments on commit 6943a00

Please sign in to comment.