]> git.sesse.net Git - nageru/blobdiff - bmusb.cpp
Support arbitrary SBS zooming (ie., zooming in on the little picture, too).
[nageru] / bmusb.cpp
index 7e2a3bee3f4386e39f6b8573f3dc998086fabeae..03f8fd210120da980f66f2c4eeab8cde2eed5967 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -54,7 +54,7 @@ atomic<bool> should_quit;
 
 FrameAllocator::~FrameAllocator() {}
 
-#define NUM_QUEUED_FRAMES 8
+#define NUM_QUEUED_FRAMES 16
 class MallocFrameAllocator : public FrameAllocator {
 public:
        MallocFrameAllocator(size_t frame_size);
@@ -146,11 +146,14 @@ void dump_audio_block(uint8_t *audio_start, size_t audio_len)
        fwrite(audio_start + AUDIO_HEADER_SIZE, 1, audio_len - AUDIO_HEADER_SIZE, audiofp);
 }
 
-void BMUSBCapture::dequeue_thread()
+void BMUSBCapture::dequeue_thread_func()
 {
-       for ( ;; ) {
+       if (has_dequeue_callbacks) {
+               dequeue_init_callback();
+       }
+       while (!dequeue_thread_should_quit) {
                unique_lock<mutex> lock(queue_lock);
-               queues_not_empty.wait(lock, [this]{ return !pending_video_frames.empty() && !pending_audio_frames.empty(); });
+               queues_not_empty.wait(lock, [this]{ return dequeue_thread_should_quit || (!pending_video_frames.empty() && !pending_audio_frames.empty()); });
 
                uint16_t video_timecode = pending_video_frames.front().timecode;
                uint16_t audio_timecode = pending_audio_frames.front().timecode;
@@ -183,6 +186,9 @@ void BMUSBCapture::dequeue_thread()
                                       audio_frame.frame, AUDIO_HEADER_SIZE, audio_frame.format);
                }
        }
+       if (has_dequeue_callbacks) {
+               dequeue_cleanup_callback();
+       }
 }
 
 void BMUSBCapture::start_new_frame(const uint8_t *start)
@@ -630,7 +636,8 @@ void BMUSBCapture::configure_card()
        if (audio_frame_allocator == nullptr) {
                set_audio_frame_allocator(new MallocFrameAllocator(65536));  // FIXME: leak.
        }
-       thread(&BMUSBCapture::dequeue_thread, this).detach();
+       dequeue_thread_should_quit = false;
+       dequeue_thread = thread(&BMUSBCapture::dequeue_thread_func, this);
 
        int rc;
        struct libusb_transfer *xfr;
@@ -933,6 +940,13 @@ out:
 #endif
 }
 
+void BMUSBCapture::stop_dequeue_thread()
+{
+       dequeue_thread_should_quit = true;
+       queues_not_empty.notify_all();  
+       dequeue_thread.join();
+}
+
 void BMUSBCapture::start_bm_thread()
 {
        should_quit = false;