]> git.sesse.net Git - nageru/commitdiff
Add metrics for the SRT output (basically same as on inputs).
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 6 Aug 2023 22:52:08 +0000 (00:52 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 6 Aug 2023 22:52:08 +0000 (00:52 +0200)
nageru/video_encoder.cpp
nageru/video_encoder.h

index 29f31050e084a4e9527407cbdfc6823c0a7330d4..4d6a403d0051cfec8d3a8c6c5ad638b3983c600c 100644 (file)
@@ -209,6 +209,7 @@ bool VideoEncoder::begin_frame(int64_t pts, int64_t duration, movit::YCbCrLumaCo
 
 RefCountedGLsync VideoEncoder::end_frame()
 {
+       want_srt_metric_update = true;
        lock_guard<mutex> lock(qs_mu);
        return quicksync_encoder->end_frame();
 }
@@ -256,6 +257,8 @@ void VideoEncoder::open_output_streams()
                if (is_srt) {
                        srt_mux.reset(mux);
                        srt_mux_metrics.init({{ "destination", "srt" }});
+                       srt_metrics.init({{ "cardtype", "output" }});
+                       global_metrics.add("srt_num_connection_attempts", {{ "cardtype", "output" }}, &metric_srt_num_connection_attempts);
                } else {
                        http_mux.reset(mux);
                        http_mux_metrics.init({{ "destination", "http" }});
@@ -373,6 +376,7 @@ int VideoEncoder::connect_to_srt()
                        // Die immediately.
                        return sock;
                }
+               ++metric_srt_num_connection_attempts;
                if (srt_connect(sock, cur->ai_addr, cur->ai_addrlen) < 0) {
                        fprintf(stderr, "srt_connect(%s): %s\n", print_addrinfo(cur).c_str(), srt_getlasterror_str());
                        srt_close(sock);
@@ -390,6 +394,9 @@ int VideoEncoder::connect_to_srt()
 
 int VideoEncoder::write_srt_packet(uint8_t *buf, int buf_size)
 {
+       if (want_srt_metric_update.exchange(false) && srt_sock != -1) {
+               srt_metrics.update_srt_stats(srt_sock);
+       }
        while (buf_size > 0) {
                if (srt_sock == -1) {
                        srt_sock = connect_to_srt();
@@ -397,12 +404,14 @@ int VideoEncoder::write_srt_packet(uint8_t *buf, int buf_size)
                                usleep(100000);
                                continue;
                        }
+                       srt_metrics.update_srt_stats(srt_sock);
                }
                int to_send = min(buf_size, SRT_LIVE_DEF_PLSIZE);
                int ret = srt_send(srt_sock, (char *)buf, to_send);
                if (ret < 0)  {
                        fprintf(stderr, "srt_send(): %s\n", srt_getlasterror_str());
                        srt_close(srt_sock);
+                       srt_metrics.metric_srt_uptime_seconds = 0.0 / 0.0;
                        srt_sock = connect_to_srt();
                        continue;
                }
index 86badf2a9a189fa708789045273f576336225e5e..91d2c756471a90eac5dbdc3b6d3e60982580fc59 100644 (file)
@@ -14,6 +14,7 @@
 #include <mutex>
 #include <string>
 #include <vector>
+#include <chrono>
 
 extern "C" {
 #include <libavformat/avformat.h>
@@ -24,6 +25,7 @@ extern "C" {
 
 #include "shared/mux.h"
 #include "shared/ref_counted_gl_sync.h"
+#include "srt_metrics.h"
 
 class AudioEncoder;
 class AV1Encoder;
@@ -112,6 +114,9 @@ private:
        std::string http_mux_header;
        MuxMetrics http_mux_metrics;
        MuxMetrics srt_mux_metrics;
+       SRTMetrics srt_metrics;
+       std::atomic<int64_t> metric_srt_num_connection_attempts{0};
+       std::atomic<bool> want_srt_metric_update{true};  // Is nominally set every frame. Some racing is OK (this is mainly a rate-limiter).
 
        std::atomic<int> quicksync_encoders_in_shutdown{0};
        std::atomic<int> overriding_bitrate{0};