From: Steinar H. Gunderson Date: Wed, 27 Jul 2016 19:13:41 +0000 (+0200) Subject: Add a function to get the number of USB cards. X-Git-Tag: 0.5~4 X-Git-Url: https://git.sesse.net/?p=bmusb;a=commitdiff_plain;h=28c6df233b806a580281eb1ae242fa108af3eb09 Add a function to get the number of USB cards. --- diff --git a/bmusb.cpp b/bmusb.cpp index 09473fd..fd323a6 100644 --- 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 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 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 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) { diff --git a/bmusb/bmusb.h b/bmusb/bmusb.h index 21cbc40..22a6655 100644 --- a/bmusb/bmusb.h +++ b/bmusb/bmusb.h @@ -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 get_available_video_modes() const override; uint32_t get_current_video_mode() const override; void set_video_mode(uint32_t video_mode_id) override;