]> git.sesse.net Git - nageru/blobdiff - nageru/mixer.h
Export SRT statistics as Prometheus metrics.
[nageru] / nageru / mixer.h
index 3ed6c5f5c9baac664fc48d6a31f40cc60498155a..af07f8896665044291a281918af5bbd51961cd0a 100644 (file)
@@ -23,6 +23,7 @@
 #include <thread>
 #include <vector>
 
+#include <movit/effect.h>
 #include <movit/image_format.h>
 
 #include "audio_mixer.h"
@@ -42,6 +43,7 @@
 class ALSAOutput;
 class ChromaSubsampler;
 class DeckLinkOutput;
+class MJPEGEncoder;
 class QSurface;
 class QSurfaceFormat;
 class TimecodeRenderer;
@@ -249,14 +251,14 @@ public:
                return theme->get_channel_color(channel);
        }
 
-       int get_channel_signal(unsigned channel) const
+       int map_channel_to_signal(unsigned channel) const
        {
-               return theme->get_channel_signal(channel);
+               return theme->map_channel_to_signal(channel);
        }
 
-       int map_signal(unsigned channel)
+       int map_signal_to_card(int signal)
        {
-               return theme->map_signal(channel);
+               return theme->map_signal_to_card(signal);
        }
 
        unsigned get_master_clock() const
@@ -287,6 +289,11 @@ public:
                theme->set_wb(channel, r, g, b);
        }
 
+       std::string format_status_line(const std::string &disk_space_left_text, double file_length_seconds)
+       {
+               return theme->format_status_line(disk_space_left_text, file_length_seconds);
+       }
+
        // Note: You can also get this through the global variable global_audio_mixer.
        AudioMixer *get_audio_mixer() { return audio_mixer.get(); }
        const AudioMixer *get_audio_mixer() const { return audio_mixer.get(); }
@@ -324,6 +331,12 @@ public:
 
        bool card_is_ffmpeg(unsigned card_index) const {
                assert(card_index < num_cards + num_video_inputs);
+               if (card_index < num_cards) {
+                       // SRT inputs are more like regular inputs than FFmpeg inputs,
+                       // so show them as such. (This allows the user to right-click
+                       // to select a different input.)
+                       return false;
+               }
                return cards[card_index].type == CardType::FFMPEG_INPUT;
        }
 
@@ -412,7 +425,7 @@ public:
                return httpd.get_num_connected_clients();
        }
 
-       std::vector<Theme::MenuEntry> get_theme_menu() { return theme->get_theme_menu(); }
+       Theme::MenuEntry *get_theme_menu() { return theme->get_theme_menu(); }
 
        void theme_menu_entry_clicked(int lua_ref) { return theme->theme_menu_entry_clicked(lua_ref); }
 
@@ -432,7 +445,7 @@ private:
                FFMPEG_INPUT,
                CEF_INPUT,
        };
-       void configure_card(unsigned card_index, bmusb::CaptureInterface *capture, CardType card_type, DeckLinkOutput *output);
+       void configure_card(unsigned card_index, bmusb::CaptureInterface *capture, CardType card_type, DeckLinkOutput *output, bool is_srt_card = false);
        void set_output_card_internal(int card_index);  // Should only be called from the mixer thread.
        void bm_frame(unsigned card_index, uint16_t timecode,
                bmusb::FrameAllocator::Frame video_frame, size_t video_offset, bmusb::VideoFormat video_format,
@@ -447,6 +460,9 @@ private:
        void render_one_frame(int64_t duration);
        void audio_thread_func();
        void release_display_frame(DisplayFrame *frame);
+#ifdef HAVE_SRT
+       void start_srt();
+#endif
        double pts() { return double(pts_int) / TIMEBASE; }
        void trim_queue(CaptureCard *card, size_t safe_queue_length);
        std::pair<std::string, std::string> get_channels_json();
@@ -455,7 +471,7 @@ private:
        HTTPD httpd;
        unsigned num_cards, num_video_inputs, num_html_inputs = 0;
 
-       QSurface *mixer_surface, *h264_encoder_surface, *decklink_output_surface;
+       QSurface *mixer_surface, *h264_encoder_surface, *decklink_output_surface, *image_update_surface;
        std::unique_ptr<movit::ResourcePool> resource_pool;
        std::unique_ptr<Theme> theme;
        std::atomic<unsigned> audio_source_channel{0};
@@ -475,6 +491,7 @@ private:
        std::unique_ptr<ChromaSubsampler> chroma_subsampler;
        std::unique_ptr<v210Converter> v210_converter;
        std::unique_ptr<VideoEncoder> video_encoder;
+       std::unique_ptr<MJPEGEncoder> mjpeg_encoder;
 
        std::unique_ptr<TimecodeRenderer> timecode_renderer;
        std::atomic<bool> display_timecode_in_stream{false};
@@ -493,11 +510,19 @@ private:
        // frame rate is integer, will always stay zero.
        unsigned fractional_samples = 0;
 
+       // Monotonic counter that lets us know which slot was last turned into
+       // a fake capture. Used for SRT re-plugging.
+       unsigned fake_capture_counter = 0;
+
        mutable std::mutex card_mutex;
        bool has_bmusb_thread = false;
        struct CaptureCard {
                std::unique_ptr<bmusb::CaptureInterface> capture;
                bool is_fake_capture;
+               // If is_fake_capture is true, contains a monotonic timer value for when
+               // it was last changed. Otherwise undefined. Used for SRT re-plugging.
+               int fake_capture_counter;
+               std::string last_srt_stream_id = "<default, matches nothing>";  // Used for SRT re-plugging.
                CardType type;
                std::unique_ptr<DeckLinkOutput> output;
 
@@ -530,12 +555,20 @@ private:
                        std::function<void()> upload_func;  // Needs to be called to actually upload the texture to OpenGL.
                        unsigned dropped_frames = 0;  // Number of dropped frames before this one.
                        std::chrono::steady_clock::time_point received_timestamp = std::chrono::steady_clock::time_point::min();
+                       movit::RGBTriplet neutral_color{1.0f, 1.0f, 1.0f};
+
+                       // Used for MJPEG encoding. (upload_func packs everything it needs
+                       // into the functor, but would otherwise also use these.)
+                       // width=0 or height=0 means a broken frame, ie., do not upload.
+                       bmusb::VideoFormat video_format;
+                       size_t y_offset, cbcr_offset;
                };
                std::deque<NewFrame> new_frames;
                std::condition_variable new_frames_changed;  // Set whenever new_frames is changed.
-
                QueueLengthPolicy queue_length_policy;  // Refers to the "new_frames" queue.
 
+               std::vector<int32_t> new_raw_audio;
+
                int last_timecode = -1;  // Unwrapped.
 
                JitterHistory jitter_history;
@@ -557,10 +590,58 @@ private:
                std::atomic<int64_t> metric_input_frame_rate_nom{-1};
                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};
+
        };
        JitterHistory output_jitter_history;
        CaptureCard cards[MAX_VIDEO_CARDS];  // Protected by <card_mutex>.
        YCbCrInterpretation ycbcr_interpretation[MAX_VIDEO_CARDS];  // Protected by <card_mutex>.
+       movit::RGBTriplet last_received_neutral_color[MAX_VIDEO_CARDS];  // Used by the mixer thread only. Constructor-initialiezd.
        std::unique_ptr<AudioMixer> audio_mixer;  // Same as global_audio_mixer (see audio_mixer.h).
        bool input_card_is_master_clock(unsigned card_index, unsigned master_card_index) const;
        struct OutputFrameInfo {
@@ -570,7 +651,11 @@ private:
                bool is_preroll;
                std::chrono::steady_clock::time_point frame_timestamp;
        };
-       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]);
+       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
 
        InputState input_state;
 
@@ -578,6 +663,9 @@ private:
        // Protected by its own mutex.
        std::mutex hotplug_mutex;
        std::vector<libusb_device *> hotplugged_cards;
+#ifdef HAVE_SRT
+       std::vector<int> hotplugged_srt_cards;
+#endif
 
        class OutputChannel {
        public:
@@ -610,6 +698,9 @@ private:
 
        std::thread mixer_thread;
        std::thread audio_thread;
+#ifdef HAVE_SRT
+       std::thread srt_thread;
+#endif
        std::atomic<bool> should_quit{false};
        std::atomic<bool> should_cut{false};