X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=bmusb.cpp;h=cd4e59c4302b5d6cdba09080cd4b36c7337e7c2a;hb=d51fd416b5c2784b03ae50bda59cd01a881bf7df;hp=c4758f5eaa7a0c9c5bb8fe1e74b81c17ee255c14;hpb=8503a5c99e244f38d38659e9d43b5834b97a6d1b;p=bmusb diff --git a/bmusb.cpp b/bmusb.cpp index c4758f5..cd4e59c 100644 --- a/bmusb.cpp +++ b/bmusb.cpp @@ -21,7 +21,7 @@ #if HAS_MULTIVERSIONING #include #endif -#include "bmusb.h" +#include "bmusb/bmusb.h" #include #include @@ -51,6 +51,7 @@ using namespace std::placeholders; namespace bmusb { card_connected_callback_t BMUSBCapture::card_connected_callback = nullptr; +bool BMUSBCapture::hotplug_existing_devices = false; namespace { @@ -877,12 +878,15 @@ void BMUSBCapture::usb_thread_func() printf("couldn't set realtime priority for USB thread: %s\n", strerror(errno)); } while (!should_quit) { - int rc = libusb_handle_events(nullptr); + timeval sec { 1, 0 }; + int rc = libusb_handle_events_timeout(nullptr, &sec); if (rc != LIBUSB_SUCCESS) break; } } +namespace { + struct USBCardDevice { uint16_t product; uint8_t bus, port; @@ -911,7 +915,7 @@ string get_card_description(int id, uint8_t bus, uint8_t port, uint16_t product) return buf; } -libusb_device_handle *open_card(int card_index, string *description) +vector find_all_cards() { libusb_device **devices; ssize_t num_devices = libusb_get_device_list(nullptr, &devices); @@ -949,6 +953,13 @@ libusb_device_handle *open_card(int card_index, string *description) return a.port < b.port; }); + return found_cards; +} + +libusb_device_handle *open_card(int card_index, string *description) +{ + vector found_cards = find_all_cards(); + for (size_t i = 0; i < found_cards.size(); ++i) { string tmp_description = get_card_description(i, found_cards[i].bus, found_cards[i].port, found_cards[i].product); fprintf(stderr, "%s\n", tmp_description.c_str()); @@ -999,6 +1010,24 @@ libusb_device_handle *open_card(unsigned card_index, libusb_device *dev, string return devh; } +} // namespace + +unsigned BMUSBCapture::num_cards() +{ + int rc = libusb_init(nullptr); + if (rc < 0) { + fprintf(stderr, "Error initializing libusb: %s\n", libusb_error_name(rc)); + exit(1); + } + + vector found_cards = find_all_cards(); + unsigned ret = found_cards.size(); + for (size_t i = 0; i < found_cards.size(); ++i) { + libusb_unref_device(found_cards[i].device); + } + return ret; +} + void BMUSBCapture::configure_card() { if (video_frame_allocator == nullptr) { @@ -1346,7 +1375,7 @@ void BMUSBCapture::start_bm_thread() // coming back with errors, so only care about devices joining. if (card_connected_callback != nullptr) { if (libusb_hotplug_register_callback( - nullptr, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, LIBUSB_HOTPLUG_NO_FLAGS, + nullptr, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, hotplug_existing_devices ? LIBUSB_HOTPLUG_ENUMERATE : LIBUSB_HOTPLUG_NO_FLAGS, USB_VENDOR_BLACKMAGIC, LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, &BMUSBCapture::cb_hotplug, nullptr, nullptr) < 0) { fprintf(stderr, "libusb_hotplug_register_callback() failed\n");