From: Steinar H. Gunderson Date: Sun, 11 Nov 2018 17:29:45 +0000 (+0100) Subject: Only bother doing MJPEG encoding if there are any connected clients that want the... X-Git-Tag: 1.8.0~49 X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=0c95058b25adce6f634279bc7feb04158242e63e Only bother doing MJPEG encoding if there are any connected clients that want the stream. --- diff --git a/nageru/mixer.cpp b/nageru/mixer.cpp index 953fd81..8793eda 100644 --- a/nageru/mixer.cpp +++ b/nageru/mixer.cpp @@ -1071,12 +1071,16 @@ void Mixer::thread_func() new_frame->upload_func = nullptr; } - // There are situations where we could possibly want to - // include FFmpeg inputs (CEF inputs are unlikely), - // but they're not necessarily in 4:2:2 Y'CbCr, so it would - // require more functionality the the JPEG encoder. - if (card_index < num_cards) { - mjpeg_encoder->upload_frame(pts_int, card_index, new_frame->frame, new_frame->video_format, new_frame->y_offset, new_frame->cbcr_offset); + // Only bother doing MJPEG encoding if there are any connected clients + // that want the stream. + if (httpd.get_num_connected_multicam_clients() > 0) { + // There are situations where we could possibly want to + // include FFmpeg inputs (CEF inputs are unlikely), + // but they're not necessarily in 4:2:2 Y'CbCr, so it would + // require more functionality the the JPEG encoder. + if (card_index < num_cards) { + mjpeg_encoder->upload_frame(pts_int, card_index, new_frame->frame, new_frame->video_format, new_frame->y_offset, new_frame->cbcr_offset); + } } } diff --git a/shared/httpd.cpp b/shared/httpd.cpp index a2668f5..782d18b 100644 --- a/shared/httpd.cpp +++ b/shared/httpd.cpp @@ -26,6 +26,7 @@ using namespace std; HTTPD::HTTPD() { global_metrics.add("num_connected_clients", &metric_num_connected_clients, Metrics::TYPE_GAUGE); + global_metrics.add("num_connected_multicam_clients", &metric_num_connected_multicam_clients, Metrics::TYPE_GAUGE); } HTTPD::~HTTPD() @@ -136,6 +137,9 @@ int HTTPD::answer_to_connection(MHD_Connection *connection, streams.insert(stream); } ++metric_num_connected_clients; + if (stream_type == HTTPD::StreamType::MULTICAM_STREAM) { + ++metric_num_connected_multicam_clients; + } *con_cls = stream; // Does not strictly have to be equal to MUX_BUFFER_SIZE. @@ -156,6 +160,9 @@ void HTTPD::free_stream(void *cls) { HTTPD::Stream *stream = (HTTPD::Stream *)cls; HTTPD *httpd = stream->get_parent(); + if (stream->get_stream_type() == HTTPD::StreamType::MULTICAM_STREAM) { + --httpd->metric_num_connected_multicam_clients; + } { unique_lock lock(httpd->streams_mutex); delete stream; diff --git a/shared/httpd.h b/shared/httpd.h index 8c3c810..2a62859 100644 --- a/shared/httpd.h +++ b/shared/httpd.h @@ -59,6 +59,9 @@ public: { return metric_num_connected_clients.load(); } + int64_t get_num_connected_multicam_clients() const { + return metric_num_connected_multicam_clients.load(); + } private: static int answer_to_connection_thunk(void *cls, MHD_Connection *connection, @@ -119,7 +122,8 @@ private: std::string header[NUM_STREAM_TYPES]; // Metrics. - std::atomic metric_num_connected_clients{ 0 }; + std::atomic metric_num_connected_clients{0}; + std::atomic metric_num_connected_multicam_clients{0}; }; #endif // !defined(_HTTPD_H)