From dfaf0700aefbcede0d058254f1f0a456ad46de03 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 30 Sep 2015 20:27:06 +0200 Subject: [PATCH] Support multiple cards at the same time (although currently, they have to be of different models). --- bmusb.cpp | 46 ++++++++++++++++++++++++++++------------------ bmusb.h | 19 ++++++++++++++----- main.cpp | 2 ++ 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/bmusb.cpp b/bmusb.cpp index 96ffa58..c03b228 100644 --- a/bmusb.cpp +++ b/bmusb.cpp @@ -45,6 +45,9 @@ using namespace std::placeholders; FILE *audiofp; +thread usb_thread; +atomic should_quit; + FrameAllocator::~FrameAllocator() {} #define NUM_QUEUED_FRAMES 8 @@ -574,7 +577,7 @@ void BMUSBCapture::usb_thread_func() } } -void BMUSBCapture::start_bm_capture() +void BMUSBCapture::configure_card() { if (video_frame_allocator == nullptr) { set_video_frame_allocator(new MallocFrameAllocator(FRAME_SIZE)); // FIXME: leak. @@ -586,7 +589,6 @@ void BMUSBCapture::start_bm_capture() int rc; struct libusb_transfer *xfr; - vector iso_xfrs; rc = libusb_init(nullptr); if (rc < 0) { @@ -594,7 +596,9 @@ void BMUSBCapture::start_bm_capture() exit(1); } - struct libusb_device_handle *devh = libusb_open_device_with_vid_pid(nullptr, 0x1edb, 0xbd3b); + //struct libusb_device_handle *devh = libusb_open_device_with_vid_pid(nullptr, 0x1edb, 0xbd3b); + //struct libusb_device_handle *devh = libusb_open_device_with_vid_pid(nullptr, 0x1edb, 0xbd4f); + struct libusb_device_handle *devh = libusb_open_device_with_vid_pid(nullptr, vid, pid); if (!devh) { fprintf(stderr, "Error finding USB device\n"); exit(1); @@ -852,24 +856,24 @@ void BMUSBCapture::start_bm_capture() iso_xfrs.push_back(xfr); } } +} - { - int i = 0; - for (libusb_transfer *xfr : iso_xfrs) { - rc = libusb_submit_transfer(xfr); - ++i; - if (rc < 0) { - //printf("num_bytes=%d\n", num_bytes); - fprintf(stderr, "Error submitting iso to endpoint 0x%02x, number %d: %s\n", - xfr->endpoint, i, libusb_error_name(rc)); - exit(1); - } +void BMUSBCapture::start_bm_capture() +{ + printf("starting capture\n"); + int i = 0; + for (libusb_transfer *xfr : iso_xfrs) { + printf("submitting transfer...\n"); + int rc = libusb_submit_transfer(xfr); + ++i; + if (rc < 0) { + //printf("num_bytes=%d\n", num_bytes); + fprintf(stderr, "Error submitting iso to endpoint 0x%02x, number %d: %s\n", + xfr->endpoint, i, libusb_error_name(rc)); + exit(1); } } - should_quit = false; - usb_thread = thread(&BMUSBCapture::usb_thread_func, this); - #if 0 libusb_release_interface(devh, 0); @@ -881,7 +885,13 @@ out: #endif } -void BMUSBCapture::stop_bm_capture() +void BMUSBCapture::start_bm_thread() +{ + should_quit = false; + usb_thread = thread(&BMUSBCapture::usb_thread_func); +} + +void BMUSBCapture::stop_bm_thread() { should_quit = true; usb_thread.join(); diff --git a/bmusb.h b/bmusb.h index e0531c2..9b74730 100644 --- a/bmusb.h +++ b/bmusb.h @@ -8,6 +8,7 @@ #include #include #include +#include // An interface for frame allocators; if you do not specify one // (using set_video_frame_allocator), a default one that pre-allocates @@ -60,6 +61,11 @@ typedef std::function *q); void dequeue_thread(); - void usb_thread_func(); + static void usb_thread_func(); static void cb_xfr(struct libusb_transfer *xfr); FrameAllocator::Frame current_video_frame; @@ -114,9 +123,6 @@ class BMUSBCapture { std::deque pending_video_frames; std::deque pending_audio_frames; - std::thread usb_thread; - std::atomic should_quit; - FrameAllocator *video_frame_allocator = nullptr; FrameAllocator *audio_frame_allocator = nullptr; frame_callback_t frame_callback = nullptr; @@ -125,6 +131,9 @@ class BMUSBCapture { static constexpr int NUM_BMUSB_REGISTERS = 60; uint8_t register_file[NUM_BMUSB_REGISTERS]; + + int vid, pid; + std::vector iso_xfrs; }; #endif diff --git a/main.cpp b/main.cpp index df70a65..7506962 100644 --- a/main.cpp +++ b/main.cpp @@ -40,6 +40,8 @@ int main(int argc, char **argv) { usb = new BMUSBCapture; usb->set_frame_callback(check_frame_stability); + usb->configure_card(); + BMUSBCapture::start_bm_thread(); usb->start_bm_capture(); sleep(1000000); } -- 2.39.2