]> git.sesse.net Git - bmusb/commitdiff
Support multiple cards at the same time (although currently, they have to be of diffe...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 30 Sep 2015 18:27:06 +0000 (20:27 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 30 Sep 2015 18:27:06 +0000 (20:27 +0200)
bmusb.cpp
bmusb.h
main.cpp

index 96ffa58fa21f12e168893fbbb487f2d63f8b412d..c03b228c0d5d98fef78c56b256c96490a48ebf45 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -45,6 +45,9 @@ using namespace std::placeholders;
 
 FILE *audiofp;
 
+thread usb_thread;
+atomic<bool> 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<libusb_transfer *> 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 e0531c2c3f552543b81b48a225b396e4b7974c67..9b74730cb056721dfa7038fbc1010d0a61e221fb 100644 (file)
--- a/bmusb.h
+++ b/bmusb.h
@@ -8,6 +8,7 @@
 #include <functional>
 #include <mutex>
 #include <thread>
+#include <vector>
 
 // 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<void(uint16_t timecode,
 // The actual capturing class, representing capture from a single card.
 class BMUSBCapture {
  public:
+       BMUSBCapture(int vid = 0x1edb, int pid = 0xbd3b)
+               : vid(vid), pid(pid)
+       {
+       }
+
        // Does not take ownership.
        void set_video_frame_allocator(FrameAllocator *allocator)
        {
@@ -87,8 +93,11 @@ class BMUSBCapture {
                frame_callback = callback;
        }
 
+       void configure_card();
        void start_bm_capture();
-       void stop_bm_capture();
+
+       static void start_bm_thread();
+       static void stop_bm_thread();
 
  private:
        struct QueuedFrame {
@@ -103,7 +112,7 @@ class BMUSBCapture {
        void queue_frame(uint16_t format, uint16_t timecode, FrameAllocator::Frame frame, std::deque<QueuedFrame> *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<QueuedFrame> pending_video_frames;
        std::deque<QueuedFrame> pending_audio_frames;
 
-       std::thread usb_thread;
-       std::atomic<bool> 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<libusb_transfer *> iso_xfrs;
 };
 
 #endif
index df70a65991333bf946b7cae1826ccbaae2d1e7bf..7506962329d26e2e6e334db48c350ba09e3e0370 100644 (file)
--- 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);
 }