]> git.sesse.net Git - nageru/blobdiff - decklink_output.cpp
Support audio-only FFmpeg inputs. Somewhat wonky, though.
[nageru] / decklink_output.cpp
index 6295f7623c770b29325ab40a57e7cf72fc2968a1..a3d220b37c98bb96834415af5a277c47424b83cb 100644 (file)
@@ -2,6 +2,9 @@
 #include <movit/util.h>
 #include <movit/resource_pool.h>  // Must be above the Xlib includes.
 #include <pthread.h>
+#include <unistd.h>
+
+#include <mutex>
 
 #include <epoxy/egl.h>
 
@@ -9,8 +12,8 @@
 #include "decklink_output.h"
 #include "decklink_util.h"
 #include "flags.h"
+#include "metrics.h"
 #include "print_latency.h"
-#include "resource_pool.h"
 #include "timebase.h"
 #include "v210_converter.h"
 
@@ -21,7 +24,7 @@ using namespace std::chrono;
 namespace {
 
 // This class can be deleted during regular use, so make all the metrics static.
-bool metrics_inited = false;
+once_flag decklink_metrics_inited;
 LatencyHistogram latency_histogram;
 atomic<int64_t> metric_decklink_output_width_pixels{-1};
 atomic<int64_t> metric_decklink_output_height_pixels{-1};
@@ -43,6 +46,8 @@ atomic<int64_t> metric_decklink_output_completed_frames_unknown{0};
 
 atomic<int64_t> metric_decklink_output_scheduled_samples{0};
 
+Summary metric_decklink_output_margin_seconds;
+
 }  // namespace
 
 DeckLinkOutput::DeckLinkOutput(ResourcePool *resource_pool, QSurface *surface, unsigned width, unsigned height, unsigned card_index)
@@ -50,7 +55,7 @@ DeckLinkOutput::DeckLinkOutput(ResourcePool *resource_pool, QSurface *surface, u
 {
        chroma_subsampler.reset(new ChromaSubsampler(resource_pool));
 
-       if (!metrics_inited) {
+       call_once(decklink_metrics_inited, [](){
                latency_histogram.init("decklink_output");
                global_metrics.add("decklink_output_width_pixels", &metric_decklink_output_width_pixels, Metrics::TYPE_GAUGE);
                global_metrics.add("decklink_output_height_pixels", &metric_decklink_output_height_pixels, Metrics::TYPE_GAUGE);
@@ -71,22 +76,23 @@ DeckLinkOutput::DeckLinkOutput(ResourcePool *resource_pool, QSurface *surface, u
                global_metrics.add("decklink_output_completed_frames", {{ "status", "unknown" }}, &metric_decklink_output_completed_frames_unknown);
 
                global_metrics.add("decklink_output_scheduled_samples", &metric_decklink_output_scheduled_samples);
-
-               metrics_inited = true;
-       }
+               vector<double> quantiles{0.01, 0.1, 0.25, 0.5, 0.75, 0.9, 0.99};
+               metric_decklink_output_margin_seconds.init(quantiles, 60.0);
+               global_metrics.add("decklink_output_margin_seconds", &metric_decklink_output_margin_seconds);
+       });
 }
 
-void DeckLinkOutput::set_device(IDeckLink *decklink)
+bool DeckLinkOutput::set_device(IDeckLink *decklink)
 {
        if (decklink->QueryInterface(IID_IDeckLinkOutput, (void**)&output) != S_OK) {
-               fprintf(stderr, "Card %u has no outputs\n", card_index);
-               exit(1);
+               fprintf(stderr, "Warning: Card %u has no outputs\n", card_index);
+               return false;
        }
 
        IDeckLinkDisplayModeIterator *mode_it;
        if (output->GetDisplayModeIterator(&mode_it) != S_OK) {
-               fprintf(stderr, "Failed to enumerate output display modes for card %u\n", card_index);
-               exit(1);
+               fprintf(stderr, "Warning: Failed to enumerate output display modes for card %u\n", card_index);
+               return false;
        }
 
        video_modes.clear();
@@ -112,6 +118,7 @@ void DeckLinkOutput::set_device(IDeckLink *decklink)
        // if they exist. We're not very likely to need analog outputs, so we don't need a way
        // to change beyond that.
        video_connection = pick_default_video_connection(decklink, BMDDeckLinkVideoOutputConnections, card_index);
+       return true;
 }
 
 void DeckLinkOutput::start_output(uint32_t mode, int64_t base_pts)
@@ -263,7 +270,7 @@ void DeckLinkOutput::send_frame(GLuint y_tex, GLuint cbcr_tex, YCbCrLumaCoeffici
                last_frame_had_mode_mismatch = false;
        }
 
-       unique_ptr<Frame> frame = move(get_frame());
+       unique_ptr<Frame> frame = get_frame();
        if (global_flags.ten_bit_output) {
                chroma_subsampler->create_v210(y_tex, cbcr_tex, width, height, frame->uyvy_tex);
        } else {
@@ -373,6 +380,9 @@ void DeckLinkOutput::wait_for_frame(int64_t pts, int *dropped_frames, int64_t *f
        *frame_timestamp = steady_clock::now() +
                nanoseconds((target_time - stream_frame_time) * 1000000000 / TIMEBASE);
 
+       metric_decklink_output_margin_seconds.count_event(
+               (target_time - stream_frame_time) / double(TIMEBASE));
+
        // If we're ahead of time, wait for the frame to (approximately) start.
        if (stream_frame_time < target_time) {
                should_quit.sleep_until(*frame_timestamp);
@@ -515,7 +525,7 @@ unique_ptr<DeckLinkOutput::Frame> DeckLinkOutput::get_frame()
        check_error();
        glBindBuffer(GL_PIXEL_PACK_BUFFER, frame->pbo);
        check_error();
-       glBufferStorage(GL_PIXEL_PACK_BUFFER, stride * height, NULL, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
+       glBufferStorage(GL_PIXEL_PACK_BUFFER, stride * height, nullptr, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
        check_error();
        frame->uyvy_ptr = (uint8_t *)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, stride * height, GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT);
        check_error();
@@ -544,7 +554,15 @@ void DeckLinkOutput::present_thread_func()
                        ++metric_decklink_output_inflight_frames;
                }
 
-               glClientWaitSync(frame->fence.get(), /*flags=*/0, GL_TIMEOUT_IGNORED);
+               for ( ;; ) {
+                       int err = glClientWaitSync(frame->fence.get(), /*flags=*/0, 0);
+                       if (err == GL_TIMEOUT_EXPIRED) {
+                               // NVIDIA likes to busy-wait; yield instead.
+                               this_thread::sleep_for(milliseconds(1));
+                       } else {
+                               break;
+                       }
+               }
                check_error();
                frame->fence.reset();