]> git.sesse.net Git - nageru/commitdiff
Yet more quitting fixes. Still not quite there.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 8 Oct 2015 18:52:28 +0000 (20:52 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 8 Oct 2015 18:52:28 +0000 (20:52 +0200)
bmusb.cpp
bmusb.h
mixer.cpp
mixer.h

index 80f575fa9765ca1f2610781062977161ff3b3a66..636fea51e5ce8f4e2108fa5d9cc5d8fdaae4d777 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -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;
diff --git a/bmusb.h b/bmusb.h
index 301350f53be858fac1885f0821920bdbac600e2c..96a2257f8549279dc0000c7b31dfb2df8709e62a 100644 (file)
--- a/bmusb.h
+++ b/bmusb.h
@@ -95,8 +95,17 @@ 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();
+       void stop_dequeue_thread();
 
        static void start_bm_thread();
        static void stop_bm_thread();
@@ -112,7 +121,7 @@ class BMUSBCapture {
        void start_new_frame(const uint8_t *start);
 
        void queue_frame(uint16_t format, uint16_t timecode, FrameAllocator::Frame frame, std::deque<QueuedFrame> *q);
-       void dequeue_thread();
+       void dequeue_thread_func();
 
        static void usb_thread_func();
        static void cb_xfr(struct libusb_transfer *xfr);
@@ -129,6 +138,12 @@ class BMUSBCapture {
        FrameAllocator *audio_frame_allocator = nullptr;
        frame_callback_t frame_callback = nullptr;
 
+       std::thread dequeue_thread;
+       std::atomic<bool> dequeue_thread_should_quit;
+       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;
index 8c7a25bceff483eeb4eff9c846440510fda49950..e8616137afe169add9954a4508fc71a679e9568d 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
@@ -79,24 +79,28 @@ Mixer::Mixer(const QSurfaceFormat &format)
 
        h264_encoder.reset(new H264Encoder(h264_encoder_surface, WIDTH, HEIGHT, "test.mp4"));
 
-       printf("Configuring first card...\n");
-       cards[0].usb = new BMUSBCapture(0x1edb, 0xbd3b);  // 0xbd4f
-       cards[0].usb->set_frame_callback(std::bind(&Mixer::bm_frame, this, 0, _1, _2, _3, _4, _5, _6, _7));
-       cards[0].frame_allocator.reset(new PBOFrameAllocator(1280 * 750 * 2 + 44, 1280, 720));
-       cards[0].usb->set_video_frame_allocator(cards[0].frame_allocator.get());
-       cards[0].usb->configure_card();
-       cards[0].surface = create_surface(format);
-#if NUM_CARDS == 2
-       cards[1].surface = create_surface(format);
-#endif
-
-       if (NUM_CARDS == 2) {
-               printf("Configuring second card...\n");
-               cards[1].usb = new BMUSBCapture(0x1edb, 0xbd4f);
-               cards[1].usb->set_frame_callback(std::bind(&Mixer::bm_frame, this, 1, _1, _2, _3, _4, _5, _6, _7));
-               cards[1].frame_allocator.reset(new PBOFrameAllocator(1280 * 750 * 2 + 44, 1280, 720));
-               cards[1].usb->set_video_frame_allocator(cards[1].frame_allocator.get());
-               cards[1].usb->configure_card();
+       for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
+               printf("Configuring card %d...\n", card_index);
+               CaptureCard *card = &cards[card_index];
+               card->usb = new BMUSBCapture(0x1edb, card_index == 0 ? 0xbd3b : 0xbd4f);
+               card->usb->set_frame_callback(std::bind(&Mixer::bm_frame, this, card_index, _1, _2, _3, _4, _5, _6, _7));
+               card->frame_allocator.reset(new PBOFrameAllocator(1280 * 750 * 2 + 44, 1280, 720));
+               card->usb->set_video_frame_allocator(card->frame_allocator.get());
+               card->surface = create_surface(format);
+               card->usb->set_dequeue_thread_callbacks(
+                       [card]{
+                               eglBindAPI(EGL_OPENGL_API);
+                               card->context = create_context();
+                               if (!make_current(card->context, card->surface)) {
+                                       printf("failed to create bmusb context\n");
+                                       exit(1);
+                               }
+                               printf("inited!\n");
+                       },
+                       [this]{
+                               resource_pool->clean_context();
+                       });
+               card->usb->configure_card();
        }
 
        BMUSBCapture::start_bm_thread();
@@ -125,6 +129,12 @@ Mixer::~Mixer()
 {
        resource_pool->release_glsl_program(cbcr_program_num);
        BMUSBCapture::stop_bm_thread();
+
+       for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
+               cards[card_index].new_data_ready = false;  // Unblock thread.
+               cards[card_index].new_data_ready_changed.notify_all();
+               cards[card_index].usb->stop_dequeue_thread();
+       }
 }
 
 void Mixer::bm_frame(int card_index, uint16_t timecode,
@@ -132,16 +142,6 @@ void Mixer::bm_frame(int card_index, uint16_t timecode,
                     FrameAllocator::Frame audio_frame, size_t audio_offset, uint16_t audio_format)
 {
        CaptureCard *card = &cards[card_index];
-       if (!card->thread_initialized) {
-               printf("initializing context for bmusb thread %d\n", card_index);
-               eglBindAPI(EGL_OPENGL_API);
-               card->context = create_context();
-               if (!make_current(card->context, card->surface)) {
-                       printf("failed to create bmusb context\n");
-                       exit(1);
-               }
-               card->thread_initialized = true;
-       }       
 
        if (video_frame.len - video_offset != 1280 * 750 * 2) {
                printf("dropping frame with wrong length (%ld)\n", video_frame.len - video_offset);
@@ -331,6 +331,8 @@ void Mixer::thread_func()
                }
                check_error();
        }
+
+       resource_pool->clean_context();
 }
 
 void Mixer::subsample_chroma(GLuint src_tex, GLuint dst_tex)
diff --git a/mixer.h b/mixer.h
index de95eef1765cbb1f07feae2a4a5ce2693ee6e3e6..594cb041ee1ece399abcb581aaf433677e81f7ab 100644 (file)
--- a/mixer.h
+++ b/mixer.h
@@ -100,8 +100,7 @@ private:
                BMUSBCapture *usb;
                std::unique_ptr<PBOFrameAllocator> frame_allocator;
 
-               // Threading stuff
-               bool thread_initialized = false;
+               // Stuff for the OpenGL context (for texture uploading).
                QSurface *surface;
                QOpenGLContext *context;