]> git.sesse.net Git - bmusb/blobdiff - bmusb.cpp
Fix int -> unsigned.
[bmusb] / bmusb.cpp
index ec3eac2918313e36258649357f7f27385f42a111..7c2108203f7904d76bdb9de9a49c8c1f3964ae3f 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -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 {
 
@@ -883,6 +884,8 @@ void BMUSBCapture::usb_thread_func()
        }
 }
 
+namespace {
+
 struct USBCardDevice {
        uint16_t product;
        uint8_t bus, port;
@@ -911,7 +914,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<USBCardDevice> find_all_cards()
 {
        libusb_device **devices;
        ssize_t num_devices = libusb_get_device_list(nullptr, &devices);
@@ -949,6 +952,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<USBCardDevice> 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 +1009,18 @@ libusb_device_handle *open_card(unsigned card_index, libusb_device *dev, string
        return devh;
 }
 
+}  // namespace
+
+unsigned BMUSBCapture::num_cards()
+{
+       vector<USBCardDevice> 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 +1368,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");