Skip to content

Commit 71adf8b

Browse files
committed
fix stop stream detection
1 parent f82b793 commit 71adf8b

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

obs-studio-server/source/nodeobs_autoconfig.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -617,8 +617,7 @@ void autoConfig::TestBandwidthThreadV2(void)
617617
ul.unlock();
618618
std::this_thread::sleep_for(std::chrono::milliseconds(250));
619619
}
620-
dataWaitMs = (int)std::chrono::duration_cast<std::chrono::milliseconds>(
621-
std::chrono::steady_clock::now() - dataStart).count();
620+
dataWaitMs = (int)std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - dataStart).count();
622621
}
623622

624623
if (!gotError) {
@@ -1732,11 +1731,25 @@ static void applyResults()
17321731
v.fps_den = (uint32_t)(runContext.idealFPSDen ? runContext.idealFPSDen : 1);
17331732
v.output_width = ((uint32_t)runContext.idealResolutionCX) & 0xFFFFFFFC;
17341733
v.output_height = ((uint32_t)runContext.idealResolutionCY) & 0xFFFFFFFE;
1734+
1735+
blog(LOG_INFO, "applyResults: ctx=%p current=%ux%u@%u/%u requested=%ux%u@%u/%u", video, video->output_width,
1736+
video->output_height, video->fps_num, video->fps_den, v.output_width, v.output_height, v.fps_num, v.fps_den);
1737+
1738+
// Skip the libobs call when nothing changes — the common case where
1739+
// autoconfig picks the resolution/FPS the canvas is already running at.
1740+
// obs_set_video_info would otherwise return OBS_VIDEO_CURRENTLY_ACTIVE
1741+
// for any active video context (e.g. running preview).
1742+
if (video->fps_num == v.fps_num && video->fps_den == v.fps_den && video->output_width == v.output_width &&
1743+
video->output_height == v.output_height) {
1744+
blog(LOG_INFO, "applyResults: ctx=%p no change, skipping obs_set_video_info", video);
1745+
continue;
1746+
}
1747+
17351748
int ret = obs_set_video_info(video, &v);
1736-
std::lock_guard<std::mutex> lock(eventsMutex);
1737-
events.push(AutoConfigInfo("applied", ret == OBS_VIDEO_SUCCESS ? "video" : ("video_failed_ret_" + std::to_string(ret)), (double)ret));
1749+
blog(ret == OBS_VIDEO_SUCCESS ? LOG_INFO : LOG_WARNING, "applyResults: ctx=%p obs_set_video_info returned %d", video, ret);
17381750
if (ret != OBS_VIDEO_SUCCESS) {
1739-
blog(LOG_WARNING, "applyResults: obs_set_video_info returned %d", ret);
1751+
std::lock_guard<std::mutex> lock(eventsMutex);
1752+
events.push(AutoConfigInfo("error", "video_failed_ret_" + std::to_string(ret), 0));
17401753
}
17411754
}
17421755

@@ -1759,8 +1772,7 @@ static void applyResults()
17591772
obs_data_set_string(settings, "server", targetServer.c_str());
17601773
obs_service_update(st.streaming->service, settings);
17611774
obs_data_release(settings);
1762-
std::lock_guard<std::mutex> lock(eventsMutex);
1763-
events.push(AutoConfigInfo("applied", "service", 0));
1775+
blog(LOG_INFO, "applyResults: target %llu: applied service server '%s'", st.id, targetServer.c_str());
17641776
}
17651777

17661778
// Streaming bitrate — three caps applied in order:
@@ -1794,8 +1806,7 @@ static void applyResults()
17941806
obs_data_set_int(encSettings, "bitrate", (long long)finalBitrate);
17951807
obs_encoder_update(st.streaming->videoEncoder, encSettings);
17961808
obs_data_release(encSettings);
1797-
std::lock_guard<std::mutex> lock(eventsMutex);
1798-
events.push(AutoConfigInfo("applied", "video_encoder", 0));
1809+
blog(LOG_INFO, "applyResults: target %llu: applied video encoder bitrate %llu", st.id, finalBitrate);
17991810
}
18001811

18011812
// Encoder type mismatch log (per-target)
@@ -1818,8 +1829,7 @@ static void applyResults()
18181829
obs_data_set_int(encSettings, "bitrate", (long long)runContext.idealBitrate);
18191830
obs_encoder_update(recording->videoEncoder, encSettings);
18201831
obs_data_release(encSettings);
1821-
std::lock_guard<std::mutex> lock(eventsMutex);
1822-
events.push(AutoConfigInfo("applied", "recording_video_encoder", 0));
1832+
blog(LOG_INFO, "applyResults: applied recording video encoder bitrate %llu", runContext.idealBitrate);
18231833
}
18241834
});
18251835
}

obs-studio-server/source/osn-streaming.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "osn-encoders.hpp"
2525
//os_gettime_ns
2626
#include <util/platform.h>
27+
#include <chrono>
28+
#include <thread>
2729

2830
osn::Streaming::~Streaming()
2931
{
@@ -79,8 +81,18 @@ void osn::Streaming::testBandwidth(bool &gotError)
7981

8082
void osn::Streaming::CleanTestMode()
8183
{
82-
if (GetOutput() && obs_output_active(GetOutput())) {
83-
obs_output_stop(GetOutput());
84+
if (GetOutput()) {
85+
if (obs_output_active(GetOutput())) {
86+
obs_output_stop(GetOutput());
87+
}
88+
// obs_output_stop is asynchronous: the output stays active until its
89+
// internal stop thread finishes flushing. Block here so callers
90+
// (autoconfig SaveSettings -> applyResults -> obs_set_video_info)
91+
// don't race with OBS_VIDEO_CURRENTLY_ACTIVE (-4).
92+
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(3);
93+
while (obs_output_active(GetOutput()) && std::chrono::steady_clock::now() < deadline) {
94+
std::this_thread::sleep_for(std::chrono::milliseconds(20));
95+
}
8496
}
8597

8698
if (service && originalServiceSettings) {

0 commit comments

Comments
 (0)