]> git.sesse.net Git - nageru/blobdiff - mixer.cpp
Switch to getting bmusb from a submodule.
[nageru] / mixer.cpp
index 69eebe07905196e024ae4bf0708fc53b2629d6b7..fa2b32fbcccc54918e624033f23ce2c34be9c64c 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
 #include <thread>
 #include <vector>
 
-#include "bmusb.h"
+#include "bmusb/bmusb.h"
 #include "context.h"
 #include "h264encode.h"
 #include "pbo_frame_allocator.h"
 #include "ref_counted_gl_sync.h"
+#include "timebase.h"
 
 class QOpenGLContext;
 
@@ -95,7 +96,7 @@ Mixer::Mixer(const QSurfaceFormat &format)
        display_chain->set_dither_bits(0);  // Don't bother.
        display_chain->finalize();
 
-       h264_encoder.reset(new H264Encoder(h264_encoder_surface, WIDTH, HEIGHT, "test.mkv"));
+       h264_encoder.reset(new H264Encoder(h264_encoder_surface, WIDTH, HEIGHT, "test.mp4"));
 
        for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
                printf("Configuring card %d...\n", card_index);
@@ -150,8 +151,11 @@ Mixer::~Mixer()
        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();
+               {
+                       std::unique_lock<std::mutex> lock(bmusb_mutex);
+                       cards[card_index].should_quit = true;  // Unblock thread.
+                       cards[card_index].new_data_ready_changed.notify_all();
+               }
                cards[card_index].usb->stop_dequeue_thread();
        }
 }
@@ -164,18 +168,22 @@ void Mixer::bm_frame(int card_index, uint16_t timecode,
 
        if (video_frame.len - video_offset != 1280 * 750 * 2) {
                printf("dropping frame with wrong length (%ld)\n", video_frame.len - video_offset);
-               FILE *fp = fopen("frame.raw", "wb");
-               fwrite(video_frame.data, video_frame.len, 1, fp);
-               fclose(fp);
-               //exit(1);
                card->usb->get_video_frame_allocator()->release_frame(video_frame);
                card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
                return;
        }
+       if (audio_frame.len - audio_offset > 30000) {
+               printf("dropping frame with implausible audio length (%ld)\n", audio_frame.len - audio_offset);
+               card->usb->get_video_frame_allocator()->release_frame(video_frame);
+               card->usb->get_audio_frame_allocator()->release_frame(audio_frame);
+               return;
+       }
+
        {
                // Wait until the previous frame was consumed.
                std::unique_lock<std::mutex> lock(bmusb_mutex);
-               card->new_data_ready_changed.wait(lock, [card]{ return !card->new_data_ready; });
+               card->new_data_ready_changed.wait(lock, [card]{ return !card->new_data_ready || card->should_quit; });
+               if (card->should_quit) return;
        }
        const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)video_frame.userdata;
        GLuint pbo = userdata->pbo;
@@ -321,7 +329,7 @@ void Mixer::thread_func()
                for (int card_index = 0; card_index < NUM_CARDS; ++card_index) {
                        input_frames.push_back(bmusb_current_rendering_frame[card_index]);
                }
-               h264_encoder->end_frame(fence, move(samples_out), input_frames);
+               h264_encoder->end_frame(fence, frame * (TIMEBASE / 60), move(samples_out), input_frames);
 
                // The live frame just shows the RGBA texture we just rendered.
                // It owns rgba_tex now.