Active high/low res only when needed

This commit is contained in:
Kamil Trzcinski 2022-10-25 16:55:33 +02:00
parent 6bdb33ff3a
commit 57fc761986
2 changed files with 45 additions and 15 deletions

View File

@ -106,13 +106,11 @@ public:
afterGetting(this); // we're preceded by a net read; no infinite recursion afterGetting(this); // we're preceded by a net read; no infinite recursion
} }
private:
Boolean fHaveStartedReading; Boolean fHaveStartedReading;
Boolean fHadKeyFrame; Boolean fHadKeyFrame;
Boolean fRequestedKeyFrame; Boolean fRequestedKeyFrame;
Boolean fLowResMode; Boolean fLowResMode;
public:
DynamicH264Stream *pNextStream; DynamicH264Stream *pNextStream;
}; };
@ -135,7 +133,6 @@ public:
return H264VideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic); return H264VideoRTPSink::createNew(envir(), rtpGroupsock, rtpPayloadTypeIfDynamic);
} }
private:
Boolean fLowResMode; Boolean fLowResMode;
}; };
@ -207,7 +204,15 @@ static void *rtsp_server_thread(void *opaque)
static bool rtsp_h264_needs_buffer(buffer_lock_t *buf_lock) static bool rtsp_h264_needs_buffer(buffer_lock_t *buf_lock)
{ {
return rtsp_streams != NULL; bool needsBuffer = false;
pthread_mutex_lock(&rtsp_lock);
for (DynamicH264Stream *stream = rtsp_streams; stream; stream = stream->pNextStream) {
if (!stream->fLowResMode)
needsBuffer = true;
}
pthread_mutex_unlock(&rtsp_lock);
return needsBuffer;
} }
static void rtsp_h264_capture(buffer_lock_t *buf_lock, buffer_t *buf) static void rtsp_h264_capture(buffer_lock_t *buf_lock, buffer_t *buf)
@ -223,6 +228,19 @@ static void rtsp_h264_capture(buffer_lock_t *buf_lock, buffer_t *buf)
pthread_mutex_unlock(&rtsp_lock); pthread_mutex_unlock(&rtsp_lock);
} }
static bool rtsp_h264_low_res_needs_buffer(buffer_lock_t *buf_lock)
{
bool needsBuffer = false;
pthread_mutex_lock(&rtsp_lock);
for (DynamicH264Stream *stream = rtsp_streams; stream; stream = stream->pNextStream) {
if (stream->fLowResMode)
needsBuffer = true;
}
pthread_mutex_unlock(&rtsp_lock);
return needsBuffer;
}
static void rtsp_h264_low_res_capture(buffer_lock_t *buf_lock, buffer_t *buf) static void rtsp_h264_low_res_capture(buffer_lock_t *buf_lock, buffer_t *buf)
{ {
pthread_mutex_lock(&rtsp_lock); pthread_mutex_lock(&rtsp_lock);
@ -264,7 +282,7 @@ extern "C" int rtsp_server(rtsp_options_t *options)
buffer_lock_register_check_streaming(&http_h264, rtsp_h264_needs_buffer); buffer_lock_register_check_streaming(&http_h264, rtsp_h264_needs_buffer);
buffer_lock_register_notify_buffer(&http_h264, rtsp_h264_capture); buffer_lock_register_notify_buffer(&http_h264, rtsp_h264_capture);
buffer_lock_register_check_streaming(&http_h264_lowres, rtsp_h264_needs_buffer); buffer_lock_register_check_streaming(&http_h264_lowres, rtsp_h264_low_res_needs_buffer);
buffer_lock_register_notify_buffer(&http_h264_lowres, rtsp_h264_low_res_capture); buffer_lock_register_notify_buffer(&http_h264_lowres, rtsp_h264_low_res_capture);
pthread_create(&rtsp_thread, NULL, rtsp_server_thread, env); pthread_create(&rtsp_thread, NULL, rtsp_server_thread, env);

View File

@ -91,12 +91,14 @@ public:
free(name); free(name);
} }
bool wantsFrame() const bool wantsFrame(bool low_res) const
{ {
if (!pc || !video) if (!pc || !video)
return false; return false;
if (pc->state() != rtc::PeerConnection::State::Connected) if (pc->state() != rtc::PeerConnection::State::Connected)
return false; return false;
if (use_low_res != low_res)
return false;
return video->wantsFrame(); return video->wantsFrame();
} }
@ -230,7 +232,9 @@ static bool webrtc_h264_needs_buffer(buffer_lock_t *buf_lock)
{ {
std::unique_lock lk(webrtc_clients_lock); std::unique_lock lk(webrtc_clients_lock);
for (auto client : webrtc_clients) { for (auto client : webrtc_clients) {
if (client->wantsFrame()) if (client->wantsFrame(false))
return true;
if (!http_h264_lowres.buf_list && client->wantsFrame(true))
return true; return true;
} }
@ -241,21 +245,29 @@ static void webrtc_h264_capture(buffer_lock_t *buf_lock, buffer_t *buf)
{ {
std::unique_lock lk(webrtc_clients_lock); std::unique_lock lk(webrtc_clients_lock);
for (auto client : webrtc_clients) { for (auto client : webrtc_clients) {
if (client->wantsFrame()) { if (client->wantsFrame(false))
client->pushFrame(buf, false); client->pushFrame(buf, false);
if (!http_h264_lowres.buf_list && client->wantsFrame(true))
if (!http_h264_lowres.buf_list) {
client->pushFrame(buf, true); client->pushFrame(buf, true);
} }
} }
static bool webrtc_h264_low_res_needs_buffer(buffer_lock_t *buf_lock)
{
std::unique_lock lk(webrtc_clients_lock);
for (auto client : webrtc_clients) {
if (client->wantsFrame(true))
return true;
} }
return false;
} }
static void webrtc_h264_low_res_capture(buffer_lock_t *buf_lock, buffer_t *buf) static void webrtc_h264_low_res_capture(buffer_lock_t *buf_lock, buffer_t *buf)
{ {
std::unique_lock lk(webrtc_clients_lock); std::unique_lock lk(webrtc_clients_lock);
for (auto client : webrtc_clients) { for (auto client : webrtc_clients) {
if (client->wantsFrame()) { if (client->wantsFrame(true)) {
client->pushFrame(buf, true); client->pushFrame(buf, true);
} }
} }
@ -408,7 +420,7 @@ extern "C" void webrtc_server()
{ {
buffer_lock_register_check_streaming(&http_h264, webrtc_h264_needs_buffer); buffer_lock_register_check_streaming(&http_h264, webrtc_h264_needs_buffer);
buffer_lock_register_notify_buffer(&http_h264, webrtc_h264_capture); buffer_lock_register_notify_buffer(&http_h264, webrtc_h264_capture);
buffer_lock_register_check_streaming(&http_h264_lowres, webrtc_h264_needs_buffer); buffer_lock_register_check_streaming(&http_h264_lowres, webrtc_h264_low_res_needs_buffer);
buffer_lock_register_notify_buffer(&http_h264_lowres, webrtc_h264_low_res_capture); buffer_lock_register_notify_buffer(&http_h264_lowres, webrtc_h264_low_res_capture);
} }