]> git.sesse.net Git - nageru/blobdiff - mixer.cpp
Add an option to control the mapping of streams to export to MJPEG (or turn it off...
[nageru] / mixer.cpp
index 5cee30005faee349e8023396b9cd5f98d5072316..2f156fa8a1f11ee9b8f22ade874a3ddfd114acaa 100644 (file)
--- a/mixer.cpp
+++ b/mixer.cpp
 #include "flags.h"
 #include "input_mapping.h"
 #include "metrics.h"
+#include "mjpeg_encoder.h"
 #include "pbo_frame_allocator.h"
 #include "ref_counted_gl_sync.h"
 #include "resampling_queue.h"
 #include "timebase.h"
 #include "timecode_renderer.h"
 #include "v210_converter.h"
+#include "va_display_with_cleanup.h"
 #include "video_encoder.h"
 
 #undef Status
@@ -356,6 +358,9 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
        display_chain->finalize();
 
        video_encoder.reset(new VideoEncoder(resource_pool.get(), h264_encoder_surface, global_flags.va_display, global_flags.width, global_flags.height, &httpd, global_disk_space_estimator));
+       if (!global_flags.card_to_mjpeg_stream_export.empty()) {
+               mjpeg_encoder.reset(new MJPEGEncoder(&httpd, global_flags.va_display));
+       }
 
        // Must be instantiated after VideoEncoder has initialized global_flags.use_zerocopy.
        theme.reset(new Theme(global_flags.theme_filename, global_flags.theme_dirs, resource_pool.get(), num_cards));
@@ -499,14 +504,13 @@ Mixer::Mixer(const QSurfaceFormat &format, unsigned num_cards)
 
 Mixer::~Mixer()
 {
+       if (mjpeg_encoder != nullptr) {
+               mjpeg_encoder->stop();
+       }
+       httpd.stop();
        BMUSBCapture::stop_bm_thread();
 
        for (unsigned card_index = 0; card_index < num_cards + num_video_inputs + num_html_inputs; ++card_index) {
-               {
-                       unique_lock<mutex> lock(card_mutex);
-                       cards[card_index].should_quit = true;  // Unblock thread.
-                       cards[card_index].new_frames_changed.notify_all();
-               }
                cards[card_index].capture->stop_dequeue_thread();
                if (cards[card_index].output) {
                        cards[card_index].output->end_output();
@@ -689,18 +693,22 @@ int unwrap_timecode(uint16_t current_wrapped, int last)
        }
 }
 
+DeviceSpec card_index_to_device(unsigned card_index, unsigned num_cards)
+{
+       if (card_index >= num_cards) {
+               return DeviceSpec{InputSourceType::FFMPEG_VIDEO_INPUT, card_index - num_cards};
+       } else {
+               return DeviceSpec{InputSourceType::CAPTURE_CARD, card_index};
+       }
+}
+
 }  // namespace
 
 void Mixer::bm_frame(unsigned card_index, uint16_t timecode,
                      FrameAllocator::Frame video_frame, size_t video_offset, VideoFormat video_format,
                     FrameAllocator::Frame audio_frame, size_t audio_offset, AudioFormat audio_format)
 {
-       DeviceSpec device;
-       if (card_index >= num_cards) {
-               device = DeviceSpec{InputSourceType::FFMPEG_VIDEO_INPUT, card_index - num_cards};
-       } else {
-               device = DeviceSpec{InputSourceType::CAPTURE_CARD, card_index};
-       }
+       DeviceSpec device = card_index_to_device(card_index, num_cards);
        CaptureCard *card = &cards[card_index];
 
        ++card->metric_input_received_frames;
@@ -954,6 +962,9 @@ void Mixer::bm_frame(unsigned card_index, uint16_t timecode,
                        new_frame.upload_func = upload_func;
                        new_frame.dropped_frames = dropped_frames;
                        new_frame.received_timestamp = video_frame.received_timestamp;  // Ignore the audio timestamp.
+                       new_frame.video_format = video_format;
+                       new_frame.y_offset = y_offset;
+                       new_frame.cbcr_offset = cbcr_offset;
                        card->new_frames.push_back(move(new_frame));
                        card->jitter_history.frame_arrived(video_frame.received_timestamp, frame_length, dropped_frames);
                        card->may_have_dropped_last_frame = false;
@@ -992,7 +1003,7 @@ void Mixer::thread_func()
                }
        }
 
-       BasicStats basic_stats(/*verbose=*/true);
+       BasicStats basic_stats(/*verbose=*/true, /*use_opengl=*/true);
        int stats_dropped_frames = 0;
 
        while (!should_quit) {
@@ -1028,6 +1039,7 @@ void Mixer::thread_func()
                handle_hotplugged_cards();
 
                for (unsigned card_index = 0; card_index < num_cards + num_video_inputs + num_html_inputs; ++card_index) {
+                       DeviceSpec device = card_index_to_device(card_index, num_cards);
                        if (card_index == master_card_index || !has_new_frame[card_index]) {
                                continue;
                        }
@@ -1035,8 +1047,8 @@ void Mixer::thread_func()
                                ++new_frames[card_index].dropped_frames;
                        }
                        if (new_frames[card_index].dropped_frames > 0) {
-                               printf("Card %u dropped %d frames before this\n",
-                                       card_index, int(new_frames[card_index].dropped_frames));
+                               printf("%s dropped %d frames before this\n",
+                                       spec_to_string(device).c_str(), int(new_frames[card_index].dropped_frames));
                        }
                }
 
@@ -1062,11 +1074,24 @@ void Mixer::thread_func()
                                new_frame->upload_func();
                                new_frame->upload_func = nullptr;
                        }
+
+                       // Only bother doing MJPEG encoding if there are any connected clients
+                       // that want the stream.
+                       if (httpd.get_num_connected_multicam_clients() > 0) {
+                               auto stream_it = global_flags.card_to_mjpeg_stream_export.find(card_index);
+                               if (stream_it != global_flags.card_to_mjpeg_stream_export.end()) {
+                                       mjpeg_encoder->upload_frame(pts_int, stream_it->second, new_frame->frame, new_frame->video_format, new_frame->y_offset, new_frame->cbcr_offset);
+                               }
+                       }
                }
 
                int64_t frame_duration = output_frame_info.frame_duration;
                render_one_frame(frame_duration);
-               ++frame_num;
+               {
+                       lock_guard<mutex> lock(frame_num_mutex);
+                       ++frame_num;
+               }
+               frame_num_updated.notify_all();
                pts_int += frame_duration;
 
                basic_stats.update(frame_num, stats_dropped_frames);
@@ -1598,6 +1623,14 @@ void Mixer::set_ffmpeg_filename(unsigned card_index, const string &filename) {
        ((FFmpegCapture *)(cards[card_index].capture.get()))->change_filename(filename);
 }
 
+void Mixer::wait_for_next_frame()
+{
+       unique_lock<mutex> lock(frame_num_mutex);
+       unsigned old_frame_num = frame_num;
+       frame_num_updated.wait_for(lock, seconds(1),  // Timeout is just in case.
+               [old_frame_num, this]{ return this->frame_num > old_frame_num; });
+}
+
 Mixer::OutputChannel::~OutputChannel()
 {
        if (has_current_frame) {