]> git.sesse.net Git - bmusb/commitdiff
Add a function to get the number of USB cards.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 27 Jul 2016 19:13:41 +0000 (21:13 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 27 Jul 2016 19:13:41 +0000 (21:13 +0200)
bmusb.cpp
bmusb/bmusb.h

index 09473fd9f6ca4f839435bedbc1e2838cd100b35e..fd323a66ffc42bc8a025ba4e6353f7e7a6c37acd 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -884,6 +884,8 @@ void BMUSBCapture::usb_thread_func()
        }
 }
 
+namespace {
+
 struct USBCardDevice {
        uint16_t product;
        uint8_t bus, port;
@@ -912,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);
@@ -950,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());
@@ -1000,6 +1009,18 @@ libusb_device_handle *open_card(unsigned card_index, libusb_device *dev, string
        return devh;
 }
 
+}  // namespace
+
+int BMUSBCapture::num_cards()
+{
+       vector<USBCardDevice> found_cards = find_all_cards();
+       int 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) {
index 21cbc408da0f1190db14cd6d45d94ddfab8ace1a..22a6655b21da12792d9a4069bf415b7bfdb521ec 100644 (file)
@@ -175,6 +175,10 @@ class BMUSBCapture : public CaptureInterface {
 
        ~BMUSBCapture() {}
 
+       // Note: Cards could be unplugged and replugged between this call and
+       // actually opening the card (in configure_card()).
+       static int num_cards();
+
        std::map<uint32_t, VideoMode> get_available_video_modes() const override;
        uint32_t get_current_video_mode() const override;
        void set_video_mode(uint32_t video_mode_id) override;