]> git.sesse.net Git - nageru/blobdiff - nageru/mixer.h
Fix a Clang 19 warning.
[nageru] / nageru / mixer.h
index 3ad74f8dd7a878b2f18c9f490104ff96830ca80c..c42d8f1a44c4f0a104201c2c8d631db317f73d33 100644 (file)
@@ -14,6 +14,7 @@
 #include <chrono>
 #include <condition_variable>
 #include <cstddef>
+#include <deque>
 #include <functional>
 #include <map>
 #include <memory>
 #include <queue>
 #include <string>
 #include <thread>
+#include <utility>
 #include <vector>
 
 #include <movit/effect.h>
-#include <movit/image_format.h>
 
 #include "audio_mixer.h"
+#include "card_type.h"
 #include "bmusb/bmusb.h"
-#include "defs.h"
 #include "ffmpeg_capture.h"
 #include "shared/httpd.h"
 #include "input_state.h"
 #include "libusb.h"
 #include "pbo_frame_allocator.h"
+#include "queue_length_policy.h"
 #include "ref_counted_frame.h"
 #include "shared/ref_counted_gl_sync.h"
+#include "srt_metrics.h"
 #include "theme.h"
+#include "shared/shared_defs.h"
 #include "shared/timebase.h"
 #include "video_encoder.h"
 #include "ycbcr_interpretation.h"
@@ -57,102 +61,6 @@ class ResourcePool;
 class YCbCrInput;
 }  // namespace movit
 
-// A class to estimate the future jitter. Used in QueueLengthPolicy (see below).
-//
-// There are many ways to estimate jitter; I've tested a few ones (and also
-// some algorithms that don't explicitly model jitter) with different
-// parameters on some real-life data in experiments/queue_drop_policy.cpp.
-// This is one based on simple order statistics where I've added some margin in
-// the number of starvation events; I believe that about one every hour would
-// probably be acceptable, but this one typically goes lower than that, at the
-// cost of 2–3 ms extra latency. (If the queue is hard-limited to one frame, it's
-// possible to get ~10 ms further down, but this would mean framedrops every
-// second or so.) The general strategy is: Take the 99.9-percentile jitter over
-// last 5000 frames, multiply by two, and that's our worst-case jitter
-// estimate. The fact that we're not using the max value means that we could
-// actually even throw away very late frames immediately, which means we only
-// get one user-visible event instead of seeing something both when the frame
-// arrives late (duplicate frame) and then again when we drop.
-class JitterHistory {
-private:
-       static constexpr size_t history_length = 5000;
-       static constexpr double percentile = 0.999;
-       static constexpr double multiplier = 2.0;
-
-public:
-       void register_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
-       void unregister_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
-
-       void clear() {
-               history.clear();
-               orders.clear();
-       }
-       void frame_arrived(std::chrono::steady_clock::time_point now, int64_t frame_duration, size_t dropped_frames);
-       std::chrono::steady_clock::time_point get_expected_next_frame() const { return expected_timestamp; }
-       double estimate_max_jitter() const;
-
-private:
-       // A simple O(k) based algorithm for getting the k-th largest or
-       // smallest element from our window; we simply keep the multiset
-       // ordered (insertions and deletions are O(n) as always) and then
-       // iterate from one of the sides. If we had larger values of k,
-       // we could go for a more complicated setup with two sets or heaps
-       // (one increasing and one decreasing) that we keep balanced around
-       // the point, or it is possible to reimplement std::set with
-       // counts in each node. However, since k=5, we don't need this.
-       std::multiset<double> orders;
-       std::deque<std::multiset<double>::iterator> history;
-
-       std::chrono::steady_clock::time_point expected_timestamp = std::chrono::steady_clock::time_point::min();
-
-       // Metrics. There are no direct summaries for jitter, since we already have latency summaries.
-       std::atomic<int64_t> metric_input_underestimated_jitter_frames{0};
-       std::atomic<double> metric_input_estimated_max_jitter_seconds{0.0 / 0.0};
-};
-
-// For any card that's not the master (where we pick out the frames as they
-// come, as fast as we can process), there's going to be a queue. The question
-// is when we should drop frames from that queue (apart from the obvious
-// dropping if the 16-frame queue should become full), especially given that
-// the frame rate could be lower or higher than the master (either subtly or
-// dramatically). We have two (conflicting) demands:
-//
-//   1. We want to avoid starving the queue.
-//   2. We don't want to add more delay than is needed.
-//
-// Our general strategy is to drop as many frames as we can (helping for #2)
-// that we think is safe for #1 given jitter. To this end, we measure the
-// deviation from the expected arrival time for all cards, and use that for
-// continuous jitter estimation.
-//
-// We then drop everything from the queue that we're sure we won't need to
-// serve the output in the time before the next frame arrives. Typically,
-// this means the queue will contain 0 or 1 frames, although more is also
-// possible if the jitter is very high.
-class QueueLengthPolicy {
-public:
-       QueueLengthPolicy() {}
-
-       void register_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
-       void unregister_metrics(const std::vector<std::pair<std::string, std::string>> &labels);
-
-       // Call after picking out a frame, so 0 means starvation.
-       // Note that the policy has no memory; everything is given in as parameters.
-       void update_policy(std::chrono::steady_clock::time_point now,
-                          std::chrono::steady_clock::time_point expected_next_frame,
-                          int64_t input_frame_duration,
-                          int64_t master_frame_duration,
-                          double max_input_card_jitter_seconds,
-                          double max_master_card_jitter_seconds);
-       unsigned get_safe_queue_length() const { return safe_queue_length; }
-
-private:
-       unsigned safe_queue_length = 0;  // Can never go below zero.
-
-       // Metrics.
-       std::atomic<int64_t> metric_input_queue_safe_length_frames{1};
-};
-
 class Mixer {
 public:
        // The surface format is used for offscreen destinations for OpenGL contexts we need.
@@ -421,6 +329,10 @@ public:
                desired_output_card_index = card_index;
        }
 
+       bool get_output_card_is_master() const {
+               return output_card_is_master;
+       }
+
        std::map<uint32_t, bmusb::VideoMode> get_available_output_video_modes() const;
 
        uint32_t get_output_video_mode() const {
@@ -491,9 +403,10 @@ private:
        std::unique_ptr<movit::ResourcePool> resource_pool;
        std::unique_ptr<Theme> theme;
        std::atomic<unsigned> audio_source_channel{0};
-       std::atomic<int> master_clock_channel{0};  // Gets overridden by <output_card_index> if set.
+       std::atomic<int> master_clock_channel{0};  // Gets overridden by <output_card_index> if output_card_is_master == true.
        int output_card_index = -1;  // -1 for none.
        uint32_t output_video_mode = -1;
+       bool output_card_is_master = false;  // Only relevant if output_card_index != -1.
 
        // The mechanics of changing the output card and modes are so intricately connected
        // with the work the mixer thread is doing. Thus, we don't change it directly,
@@ -565,9 +478,6 @@ private:
 
                std::unique_ptr<PBOFrameAllocator> frame_allocator;
 
-               // Stuff for the OpenGL context (for texture uploading).
-               QSurface *surface = nullptr;
-
                struct NewFrame {
                        RefCountedFrame frame;
                        int64_t length;  // In TIMEBASE units.
@@ -611,52 +521,9 @@ private:
                std::atomic<int64_t> metric_input_frame_rate_den{-1};
                std::atomic<int64_t> metric_input_sample_rate_hz{-1};
 
-               // SRT metrics.
-               std::atomic<double> metric_srt_uptime_seconds{0.0 / 0.0};
-               std::atomic<double> metric_srt_send_duration_seconds{0.0 / 0.0};
-               std::atomic<int64_t> metric_srt_sent_bytes{-1};
-               std::atomic<int64_t> metric_srt_received_bytes{-1};
-               std::atomic<int64_t> metric_srt_sent_packets_normal{-1};
-               std::atomic<int64_t> metric_srt_received_packets_normal{-1};
-               std::atomic<int64_t> metric_srt_sent_packets_lost{-1};
-               std::atomic<int64_t> metric_srt_received_packets_lost{-1};
-               std::atomic<int64_t> metric_srt_sent_packets_retransmitted{-1};
-               std::atomic<int64_t> metric_srt_sent_bytes_retransmitted{-1};
-               std::atomic<int64_t> metric_srt_sent_packets_ack{-1};
-               std::atomic<int64_t> metric_srt_received_packets_ack{-1};
-               std::atomic<int64_t> metric_srt_sent_packets_nak{-1};
-               std::atomic<int64_t> metric_srt_received_packets_nak{-1};
-               std::atomic<int64_t> metric_srt_sent_packets_dropped{-1};
-               std::atomic<int64_t> metric_srt_received_packets_dropped{-1};
-               std::atomic<int64_t> metric_srt_sent_bytes_dropped{-1};
-               std::atomic<int64_t> metric_srt_received_bytes_dropped{-1};
-               std::atomic<int64_t> metric_srt_received_packets_undecryptable{-1};
-               std::atomic<int64_t> metric_srt_received_bytes_undecryptable{-1};
-
-               std::atomic<int64_t> metric_srt_filter_received_extra_packets{-1};
-               std::atomic<int64_t> metric_srt_filter_received_rebuilt_packets{-1};
-               std::atomic<int64_t> metric_srt_filter_received_lost_packets{-1};
-
-               std::atomic<double> metric_srt_packet_sending_period_seconds{0.0 / 0.0};
-               std::atomic<int64_t> metric_srt_flow_window_packets{-1};
-               std::atomic<int64_t> metric_srt_congestion_window_packets{-1};
-               std::atomic<int64_t> metric_srt_flight_size_packets{-1};
-               std::atomic<double> metric_srt_rtt_seconds{0.0 / 0.0};
-               std::atomic<double> metric_srt_estimated_bandwidth_bits_per_second{0.0 / 0.0};
-               std::atomic<double> metric_srt_bandwidth_ceiling_bits_per_second{0.0 / 0.0};
-               std::atomic<int64_t> metric_srt_send_buffer_available_bytes{-1};
-               std::atomic<int64_t> metric_srt_receive_buffer_available_bytes{-1};
-               std::atomic<int64_t> metric_srt_mss_bytes{-1};
-               std::atomic<int64_t> metric_srt_sender_unacked_packets{-1};
-               std::atomic<int64_t> metric_srt_sender_unacked_bytes{-1};
-               std::atomic<double> metric_srt_sender_unacked_timespan_seconds{0.0 / 0.0};
-               std::atomic<double> metric_srt_sender_delivery_delay_seconds{0.0 / 0.0};
-               std::atomic<int64_t> metric_srt_receiver_unacked_packets{-1};
-               std::atomic<int64_t> metric_srt_receiver_unacked_bytes{-1};
-               std::atomic<double> metric_srt_receiver_unacked_timespan_seconds{0.0 / 0.0};
-               std::atomic<double> metric_srt_receiver_delivery_delay_seconds{0.0 / 0.0};
-               std::atomic<int64_t> metric_srt_filter_sent_packets{-1};
-
+#ifdef HAVE_SRT
+               SRTMetrics srt_metrics;
+#endif
        };
        JitterHistory output_jitter_history;
        CaptureCard cards[MAX_VIDEO_CARDS];  // Protected by <card_mutex>.
@@ -673,10 +540,6 @@ private:
        };
        OutputFrameInfo get_one_frame_from_each_card(unsigned master_card_index, bool master_card_is_output, CaptureCard::NewFrame new_frames[MAX_VIDEO_CARDS], bool has_new_frame[MAX_VIDEO_CARDS], std::vector<int32_t> raw_audio[MAX_VIDEO_CARDS]);
 
-#ifdef HAVE_SRT
-       void update_srt_stats(int srt_sock, Mixer::CaptureCard *card);
-#endif
-
        std::string description_for_card(unsigned card_index);
        static bool is_srt_card(const CaptureCard *card);