X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=bmusb.cpp;h=cd4e59c4302b5d6cdba09080cd4b36c7337e7c2a;hb=d51fd416b5c2784b03ae50bda59cd01a881bf7df;hp=d62cef90d3db567c4af368847649762b0c34bd6c;hpb=20a789f2e76ca239e3bfeb165d6ddefd9c2022f2;p=bmusb diff --git a/bmusb.cpp b/bmusb.cpp index d62cef9..cd4e59c 100644 --- a/bmusb.cpp +++ b/bmusb.cpp @@ -1,4 +1,4 @@ -// Intensity Shuttle USB3 prototype capture driver, v0.3 +// Intensity Shuttle USB3 capture driver, v0.4 // Can download 8-bit and 10-bit UYVY/v210 frames from HDMI, quite stable // (can do captures for hours at a time with no drops), except during startup // 576p60/720p60/1080i60 works, 1080p60 does not work (firmware limitation) @@ -21,7 +21,7 @@ #if HAS_MULTIVERSIONING #include #endif -#include "bmusb.h" +#include "bmusb/bmusb.h" #include #include @@ -48,7 +48,10 @@ using namespace std::placeholders; #define FRAME_SIZE (8 << 20) // 8 MB. #define USB_VIDEO_TRANSFER_SIZE (128 << 10) // 128 kB. +namespace bmusb { + card_connected_callback_t BMUSBCapture::card_connected_callback = nullptr; +bool BMUSBCapture::hotplug_existing_devices = false; namespace { @@ -875,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; @@ -909,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); @@ -947,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()); @@ -997,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) { @@ -1344,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"); @@ -1426,3 +1457,5 @@ void BMUSBCapture::update_capture_mode() exit(1); } } + +} // namespace bmusb