]> git.sesse.net Git - bmusb/commitdiff
Add support for callbacks in the dequeue thread.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 12 Oct 2015 19:23:55 +0000 (21:23 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 12 Oct 2015 19:23:55 +0000 (21:23 +0200)
The typical use for this would be setting up and tearing down an
OpenGL context or similar. You can fake it easily enough for
startup, but it's much worse for shutdown (which is coming soon).

bmusb.cpp
bmusb.h

index 16da5f01e467409188f4cb3ab92335631802d401..b2de56f2b2cee588bc4a1c01252ffa2154045a48 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -146,6 +146,9 @@ void dump_audio_block(uint8_t *audio_start, size_t audio_len)
 
 void BMUSBCapture::dequeue_thread()
 {
+       if (has_dequeue_callbacks) {
+               dequeue_init_callback();
+       }
        for ( ;; ) {
                unique_lock<mutex> lock(queue_lock);
                queues_not_empty.wait(lock, [this]{ return !pending_video_frames.empty() && !pending_audio_frames.empty(); });
@@ -181,6 +184,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)
diff --git a/bmusb.h b/bmusb.h
index 301350f53be858fac1885f0821920bdbac600e2c..c7a5224d4ad31a41f5846123dc363a8be4c3fd46 100644 (file)
--- a/bmusb.h
+++ b/bmusb.h
@@ -95,6 +95,14 @@ class BMUSBCapture {
                frame_callback = callback;
        }
 
+       // Needs to be run before configure_card().
+       void set_dequeue_thread_callbacks(std::function<void()> init, std::function<void()> cleanup)
+       {
+               dequeue_init_callback = init;
+               dequeue_cleanup_callback = cleanup;
+               has_dequeue_callbacks = true;
+       }
+
        void configure_card();
        void start_bm_capture();
 
@@ -129,6 +137,10 @@ class BMUSBCapture {
        FrameAllocator *audio_frame_allocator = nullptr;
        frame_callback_t frame_callback = nullptr;
 
+       bool has_dequeue_callbacks = false;
+       std::function<void()> dequeue_init_callback = nullptr;
+       std::function<void()> dequeue_cleanup_callback = nullptr;
+
        int current_register = 0;
 
        static constexpr int NUM_BMUSB_REGISTERS = 60;