]> git.sesse.net Git - bmusb/blobdiff - bmusb.cpp
Add support for persistent USB device memory.
[bmusb] / bmusb.cpp
index 3aac81e5e34e5de7459585630635eab8a460d25a..ed7a1f5caa42874909bf13804210322ee34942b5 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -1028,7 +1028,7 @@ void BMUSBCapture::configure_card()
        // set up isochronous transfers for audio and video
        for (int e = 3; e <= 4; ++e) {
                //int num_transfers = (e == 3) ? 6 : 6;
-               int num_transfers = 10;
+               int num_transfers = 6;
                for (int i = 0; i < num_transfers; ++i) {
                        size_t buf_size;
                        int num_iso_pack, size;
@@ -1044,8 +1044,23 @@ void BMUSBCapture::configure_card()
                                num_iso_pack = 80;
                                buf_size = num_iso_pack * size;
                        }
-                       assert(size_t(num_iso_pack * size) <= buf_size);
-                       uint8_t *buf = new uint8_t[buf_size];
+                       int num_bytes = num_iso_pack * size;
+                       assert(size_t(num_bytes) <= buf_size);
+#if LIBUSB_API_VERSION >= 0x01000105
+                       uint8_t *buf = libusb_dev_mem_alloc(devh, num_bytes);
+#else
+                       uint8_t *buf = nullptr;
+#endif
+                       if (buf == nullptr) {
+                               fprintf(stderr, "Failed to allocate persistent DMA memory ");
+#if LIBUSB_API_VERSION >= 0x01000105
+                               fprintf(stderr, "(probably too old kernel; use 4.6.0 or newer).\n");
+#else
+                               fprintf(stderr, "(compiled against too old libusb-1.0).\n");
+#endif
+                               fprintf(stderr, "Will go slower, and likely fail due to memory fragmentation after a few hours.\n");
+                               buf = new uint8_t[num_bytes];
+                       }
 
                        xfr = libusb_alloc_transfer(num_iso_pack);
                        if (!xfr) {