]> git.sesse.net Git - bmusb/blobdiff - bmusb.cpp
Set libusb timeout to one second, to help shutdown if the thread is started with...
[bmusb] / bmusb.cpp
index 09473fd9f6ca4f839435bedbc1e2838cd100b35e..cd4e59c4302b5d6cdba09080cd4b36c7337e7c2a 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -878,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;
@@ -912,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<USBCardDevice> find_all_cards()
 {
        libusb_device **devices;
        ssize_t num_devices = libusb_get_device_list(nullptr, &devices);
@@ -950,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<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());
@@ -1000,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<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) {