From 013265a6ea7a89c00d634a699980a01f39b33911 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 16:19:52 -0400 Subject: [PATCH 1/7] test: Expect RTMPS for verified ingest servers, with a plain RTMP fallback Built-in services whose ingest servers accept RTMPS should connect over it, and the frontend should be able to fall back to plain RTMP when the encrypted connection fails. - test_rtmps_upgrade loads the real rtmp-services module. A built-in service on a verified host connects to rtmps://:443 with the same path, and reports the plain URL through the module's rtmp_services_rtmps_fallback command. rtmp_services_set_plain_rtmp switches it back to the plain URL. Unlisted hosts, other services, other ports, custom servers and rtmps:// servers stay as they are. - test_rtmps_fallback_dialog runs the "Proceed anyway" dialog on Qt's offscreen platform. It accepts on its own after the three-second countdown, its progress bar follows the countdown, a click accepts at once, Cancel stops the countdown, and a fallback is written to stderr and the OBS log. The upgrade list (rtmps-upgrade.c.h) and the dialog are no-op stubs here, so five upgrade cases and three dialog cases fail. Refs #2205. --- .../frontend-dialogs-obsrtmpsfallback.pch.hpp | 18 + frontend/build/frontend-widgets.pch.hpp | 1 + ...frontend.dialogs-obsrtmpsfallback+.hpp.cpp | 5 + frontend/build/source-sets.json | 2 + frontend/dialogs/OBSRtmpsFallback.cpp.hpp | 30 ++ frontend/dialogs/OBSRtmpsFallback.hpp | 40 ++ plugins/rtmp-services/CMakeLists.txt | 1 + .../build/rtmp-services.core+.h.c | 1 + .../rtmp-services/build/rtmp-services.pch.h | 1 + .../service-specific/rtmps-upgrade.c.h | 9 + .../service-specific/rtmps-upgrade.h | 14 + test/cmocka/test_rtmps_fallback_dialog.cpp | 189 ++++++++++ test/cmocka/test_rtmps_upgrade.c | 341 ++++++++++++++++++ .../tests.d/test_rtmps_fallback_dialog.cmake | 24 ++ test/cmocka/tests.d/test_rtmps_upgrade.cmake | 21 ++ 15 files changed, 697 insertions(+) create mode 100644 frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp create mode 100644 frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp create mode 100644 frontend/dialogs/OBSRtmpsFallback.cpp.hpp create mode 100644 frontend/dialogs/OBSRtmpsFallback.hpp create mode 100644 plugins/rtmp-services/service-specific/rtmps-upgrade.c.h create mode 100644 plugins/rtmp-services/service-specific/rtmps-upgrade.h create mode 100644 test/cmocka/test_rtmps_fallback_dialog.cpp create mode 100644 test/cmocka/test_rtmps_upgrade.c create mode 100644 test/cmocka/tests.d/test_rtmps_fallback_dialog.cmake create mode 100644 test/cmocka/tests.d/test_rtmps_upgrade.cmake diff --git a/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp b/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp new file mode 100644 index 0000000000..08704559cd --- /dev/null +++ b/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp @@ -0,0 +1,18 @@ +/* Precompiled header (BUILD.md) for the frontend unity groups + * frontend.dialogs-obsrtmpsfallback. C++. Every implementation file in them includes this header and + * nothing else: the includes and macro setup below are theirs, in group and + * file order. */ + +#pragma once + +/* dialogs/OBSRtmpsFallback.cpp */ +#include "frontend/dialogs/OBSRtmpsFallback.hpp" + +#include +#include +#include +#include +#include + +#include +#include diff --git a/frontend/build/frontend-widgets.pch.hpp b/frontend/build/frontend-widgets.pch.hpp index ac0a024618..d71503f677 100644 --- a/frontend/build/frontend-widgets.pch.hpp +++ b/frontend/build/frontend-widgets.pch.hpp @@ -151,6 +151,7 @@ #endif #include #include +#include #include #if defined(_WIN32) || defined(WHATSNEW_ENABLED) #include diff --git a/frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp b/frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp new file mode 100644 index 0000000000..6d75748832 --- /dev/null +++ b/frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp @@ -0,0 +1,5 @@ +/* Unity group (BUILD.md): OBSRtmpsFallback.cpp on its own (a C test builds it alone). */ + +#include "frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp" + +#include "frontend/dialogs/OBSRtmpsFallback.cpp.hpp" diff --git a/frontend/build/source-sets.json b/frontend/build/source-sets.json index bd96883561..b03030152a 100644 --- a/frontend/build/source-sets.json +++ b/frontend/build/source-sets.json @@ -238,6 +238,8 @@ "dialogs/OBSFallbackWarning.hpp", "dialogs/OBSLogViewer.hpp", "dialogs/OBSMissingFiles.hpp", + "build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp", + "dialogs/OBSRtmpsFallback.hpp", "build/frontend.dialogs-2+.hpp.cpp", "dialogs/OBSRemux.hpp", "dialogs/OBSWhatsNew.hpp" diff --git a/frontend/dialogs/OBSRtmpsFallback.cpp.hpp b/frontend/dialogs/OBSRtmpsFallback.cpp.hpp new file mode 100644 index 0000000000..c2f6c9eb61 --- /dev/null +++ b/frontend/dialogs/OBSRtmpsFallback.cpp.hpp @@ -0,0 +1,30 @@ +#include "frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp" + +OBSRtmpsFallback::OBSRtmpsFallback(QWidget *parent, const Texts &texts) : QDialog(parent) +{ + setWindowTitle(texts.title); + + QVBoxLayout *layout = new QVBoxLayout(this); + QLabel *label = new QLabel(texts.message, this); + label->setWordWrap(true); + layout->addWidget(label); + + progress = new QProgressBar(this); + progress->setRange(0, CountdownMs); + progress->setTextVisible(false); + layout->addWidget(progress); + + QDialogButtonBox *buttons = new QDialogButtonBox(this); + proceedButton = buttons->addButton(texts.proceed, QDialogButtonBox::AcceptRole); + buttons->addButton(texts.cancel, QDialogButtonBox::RejectRole); + connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); + layout->addWidget(buttons); +} + +void OBSRtmpsFallback::Tick() {} + +void OBSRtmpsFallback::LogFallback(const char *plain_url) +{ + (void)plain_url; +} diff --git a/frontend/dialogs/OBSRtmpsFallback.hpp b/frontend/dialogs/OBSRtmpsFallback.hpp new file mode 100644 index 0000000000..5bef399d05 --- /dev/null +++ b/frontend/dialogs/OBSRtmpsFallback.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include +#include +#include +#include + +class QProgressBar; +class QPushButton; + +/* Shown when a stream that rtmp-services upgraded to RTMPS fails to connect. + * "Proceed anyway" connects to the same server over plain RTMP; it fills a + * progress bar for CountdownMs and then accepts on its own, so a stream started + * with nobody at the screen still goes live. Cancel stops the start. */ +class OBSRtmpsFallback : public QDialog { + Q_OBJECT + +public: + static constexpr int CountdownMs = 3000; + + struct Texts { + QString title; + QString message; + QString proceed; + QString cancel; + }; + + OBSRtmpsFallback(QWidget *parent, const Texts &texts); + + /* Writes the fallback to stderr and the OBS log. */ + static void LogFallback(const char *plain_url); + +private: + QProgressBar *progress = nullptr; + QPushButton *proceedButton = nullptr; + QTimer tick; + QElapsedTimer elapsed; + + void Tick(); +}; diff --git a/plugins/rtmp-services/CMakeLists.txt b/plugins/rtmp-services/CMakeLists.txt index 48ceb0303b..99e16909b1 100644 --- a/plugins/rtmp-services/CMakeLists.txt +++ b/plugins/rtmp-services/CMakeLists.txt @@ -28,6 +28,7 @@ target_sources( service-specific/amazon-ivs.h service-specific/dacast.h service-specific/nimotv.h + service-specific/rtmps-upgrade.h service-specific/service-ingest.h service-specific/showroom.h service-specific/twitch.h diff --git a/plugins/rtmp-services/build/rtmp-services.core+.h.c b/plugins/rtmp-services/build/rtmp-services.core+.h.c index d42c2287ba..4fc18f2c82 100644 --- a/plugins/rtmp-services/build/rtmp-services.core+.h.c +++ b/plugins/rtmp-services/build/rtmp-services.core+.h.c @@ -8,6 +8,7 @@ #include "plugins/rtmp-services/service-specific/amazon-ivs.c.h" #include "plugins/rtmp-services/service-specific/dacast.c.h" #include "plugins/rtmp-services/service-specific/nimotv.c.h" +#include "plugins/rtmp-services/service-specific/rtmps-upgrade.c.h" #include "plugins/rtmp-services/service-specific/service-ingest.c.h" #include "plugins/rtmp-services/service-specific/showroom.c.h" #include "plugins/rtmp-services/service-specific/twitch.c.h" diff --git a/plugins/rtmp-services/build/rtmp-services.pch.h b/plugins/rtmp-services/build/rtmp-services.pch.h index 01fe428b7f..82e9243ab7 100644 --- a/plugins/rtmp-services/build/rtmp-services.pch.h +++ b/plugins/rtmp-services/build/rtmp-services.pch.h @@ -18,6 +18,7 @@ #include "plugins/rtmp-services/service-specific/showroom.h" #include "plugins/rtmp-services/service-specific/dacast.h" #include "plugins/rtmp-services/service-specific/amazon-ivs.h" +#include "plugins/rtmp-services/service-specific/rtmps-upgrade.h" /* rtmp-services-main.c */ #include diff --git a/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h b/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h new file mode 100644 index 0000000000..9066865dac --- /dev/null +++ b/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h @@ -0,0 +1,9 @@ +#include "plugins/rtmp-services/build/rtmp-services.pch.h" + +bool rtmps_upgrade_url(const char *service, const char *url, struct dstr *out) +{ + (void)service; + (void)url; + (void)out; + return false; +} diff --git a/plugins/rtmp-services/service-specific/rtmps-upgrade.h b/plugins/rtmp-services/service-specific/rtmps-upgrade.h new file mode 100644 index 0000000000..69378017a2 --- /dev/null +++ b/plugins/rtmp-services/service-specific/rtmps-upgrade.h @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + +/* Built-in services whose plain RTMP ingest servers also accept RTMPS on port + * 443. rtmp-services connects to these over RTMPS and keeps the plain URL so + * the frontend can offer it if the encrypted connection fails. + * + * Writes the RTMPS form of url (same host and path, port 443) to out and + * returns true when url is a plain rtmp:// URL on the default port and its host + * is in the list for service. Returns false and leaves out untouched + * otherwise. */ +bool rtmps_upgrade_url(const char *service, const char *url, struct dstr *out); diff --git a/test/cmocka/test_rtmps_fallback_dialog.cpp b/test/cmocka/test_rtmps_fallback_dialog.cpp new file mode 100644 index 0000000000..0dc00d3373 --- /dev/null +++ b/test/cmocka/test_rtmps_fallback_dialog.cpp @@ -0,0 +1,189 @@ +/* The dialog the frontend shows when a stream that rtmp-services upgraded to + * RTMPS fails to connect (obs-rust/obs-studio#2205). "Proceed anyway" fills a + * progress bar for three seconds and then accepts on its own, so a stream + * started with nobody at the screen still goes live; Cancel stops the start. + * Each fallback is written to stderr and the OBS log. Runs the real dialog on + * Qt's offscreen platform. */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +extern "C" { +#include +} + +#include + +#include "OBSRtmpsFallback.hpp" + +static const OBSRtmpsFallback::Texts texts = {"Title", "Message", "Proceed anyway", "Cancel"}; + +/* Runs the event loop until the dialog has a result or limit_ms passes, and + * returns how long it took. */ +static qint64 wait_for_result(OBSRtmpsFallback &dlg, int limit_ms) +{ + QElapsedTimer t; + t.start(); + while (dlg.isVisible() && t.elapsed() < limit_ms) { + QApplication::processEvents(QEventLoop::AllEvents, 20); + QThread::msleep(5); + } + return t.elapsed(); +} + +static QPushButton *button(OBSRtmpsFallback &dlg, QDialogButtonBox::ButtonRole role) +{ + QDialogButtonBox *box = dlg.findChild(); + if (!box) { + return nullptr; + } + for (QAbstractButton *b : box->buttons()) { + if (box->buttonRole(b) == role) { + return qobject_cast(b); + } + } + return nullptr; +} + +/* With no click, the dialog accepts on its own once the countdown ends. */ +static void countdown_proceeds_after_three_seconds(void **state) +{ + (void)state; + OBSRtmpsFallback dlg(nullptr, texts); + dlg.show(); + + qint64 took = wait_for_result(dlg, 6000); + + assert_false(dlg.isVisible()); + assert_int_equal(dlg.result(), QDialog::Accepted); + assert_true(took >= OBSRtmpsFallback::CountdownMs - 200); + assert_true(took <= OBSRtmpsFallback::CountdownMs + 1500); +} + +/* The progress bar shows how much of the countdown has passed. */ +static void progress_bar_follows_countdown(void **state) +{ + (void)state; + OBSRtmpsFallback dlg(nullptr, texts); + dlg.show(); + + wait_for_result(dlg, OBSRtmpsFallback::CountdownMs / 2); + + QProgressBar *bar = dlg.findChild(); + assert_non_null(bar); + int pct = bar->value() * 100 / bar->maximum(); + assert_true(pct >= 30); + assert_true(pct <= 75); + dlg.reject(); +} + +/* Clicking "Proceed anyway" accepts right away. */ +static void proceed_click_accepts_now(void **state) +{ + (void)state; + OBSRtmpsFallback dlg(nullptr, texts); + dlg.show(); + + QPushButton *proceed = button(dlg, QDialogButtonBox::AcceptRole); + assert_non_null(proceed); + assert_string_equal(proceed->text().toUtf8().constData(), "Proceed anyway"); + proceed->click(); + + assert_true(wait_for_result(dlg, 500) < 500); + assert_int_equal(dlg.result(), QDialog::Accepted); +} + +/* Cancel rejects, and the countdown does not accept afterwards. */ +static void cancel_stops_the_countdown(void **state) +{ + (void)state; + OBSRtmpsFallback dlg(nullptr, texts); + dlg.show(); + + QPushButton *cancel = button(dlg, QDialogButtonBox::RejectRole); + assert_non_null(cancel); + cancel->click(); + wait_for_result(dlg, 500); + assert_int_equal(dlg.result(), QDialog::Rejected); + + QElapsedTimer t; + t.start(); + while (t.elapsed() < OBSRtmpsFallback::CountdownMs + 500) { + QApplication::processEvents(QEventLoop::AllEvents, 20); + QThread::msleep(5); + } + assert_int_equal(dlg.result(), QDialog::Rejected); + assert_false(dlg.isVisible()); +} + +static char logged[1024]; + +static void capture_log(int level, const char *format, va_list args, void *param) +{ + (void)level; + (void)param; + char line[512]; + vsnprintf(line, sizeof(line), format, args); + strncat(logged, line, sizeof(logged) - strlen(logged) - 1); + strncat(logged, "\n", sizeof(logged) - strlen(logged) - 1); +} + +/* A fallback is written to stderr and to the OBS log, naming the server. */ +static void fallback_is_logged_to_stderr_and_log(void **state) +{ + (void)state; + char path[] = "test_rtmps_fallback_stderr_XXXXXX"; + int fd = mkstemp(path); + assert_true(fd >= 0); + + fflush(stderr); + int saved = dup(fileno(stderr)); + dup2(fd, fileno(stderr)); + + logged[0] = '\0'; + base_set_log_handler(capture_log, nullptr); + OBSRtmpsFallback::LogFallback("rtmp://ingest.example/app"); + base_set_log_handler(nullptr, nullptr); + + fflush(stderr); + dup2(saved, fileno(stderr)); + close(saved); + + char err[1024] = {0}; + lseek(fd, 0, SEEK_SET); + ssize_t n = read(fd, err, sizeof(err) - 1); + close(fd); + unlink(path); + + assert_true(n > 0); + assert_non_null(strstr(err, "RTMPS")); + assert_non_null(strstr(err, "rtmp://ingest.example/app")); + assert_non_null(strstr(logged, "RTMPS")); + assert_non_null(strstr(logged, "rtmp://ingest.example/app")); +} + +int main(int argc, char **argv) +{ + qputenv("QT_QPA_PLATFORM", "offscreen"); + QApplication app(argc, argv); + + const struct CMUnitTest tests[] = { + cmocka_unit_test(countdown_proceeds_after_three_seconds), + cmocka_unit_test(progress_bar_follows_countdown), + cmocka_unit_test(proceed_click_accepts_now), + cmocka_unit_test(cancel_stops_the_countdown), + cmocka_unit_test(fallback_is_logged_to_stderr_and_log), + }; + return cmocka_run_group_tests(tests, nullptr, nullptr); +} diff --git a/test/cmocka/test_rtmps_upgrade.c b/test/cmocka/test_rtmps_upgrade.c new file mode 100644 index 0000000000..ff51f563aa --- /dev/null +++ b/test/cmocka/test_rtmps_upgrade.c @@ -0,0 +1,341 @@ +/* rtmp-services connects to built-in services over RTMPS when their ingest + * server is known to accept it (obs-rust/obs-studio#2205). The service still + * stores the plain rtmp:// server the user picked; the upgrade happens when the + * output asks for the URL, and the service keeps the plain URL so the frontend + * can offer it if the encrypted connection fails. + * + * The cases load the real rtmp-services module with a services.json written + * by the test. They need obs_startup(), which needs a display on Linux, so + * they skip without one (Tier 2 runs ctest under xvfb-run). */ + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#ifdef RTMP_SERVICES_MODULE_PATH +#include "rtmp-format-ver.h" +#endif + +/* An ingest server that accepts RTMPS on port 443 and is on the list. */ +#define LISTED_PLAIN "rtmp://live.twitch.tv/app" +#define LISTED_RTMPS "rtmps://live.twitch.tv:443/app" + +static bool module_up; + +static void quiet_log(int level, const char *format, va_list args, void *param) +{ + (void)level; + (void)format; + (void)args; + (void)param; +} + +static obs_service_t *make_service(const char *id, const char *service, const char *server) +{ + obs_data_t *settings = obs_data_create(); + if (service) + obs_data_set_string(settings, "service", service); + obs_data_set_string(settings, "server", server); + obs_data_set_string(settings, "key", "test"); + obs_service_t *s = obs_service_create(id, "test service", settings, NULL); + obs_data_release(settings); + return s; +} + +static const char *url_of(obs_service_t *s) +{ + return obs_service_get_connect_info(s, OBS_SERVICE_CONNECT_INFO_SERVER_URL); +} + +/* The plain URL the service would fall back to, or "" when it did not + * upgrade the URL (or the module has no fallback command). */ +static void fallback_of(obs_service_t *s, char *buf, size_t size) +{ + calldata_t cd = {0}; + const char *plain = NULL; + + buf[0] = '\0'; + calldata_set_ptr(&cd, "service", s); + if (proc_handler_call(obs_get_proc_handler(), "rtmp_services_rtmps_fallback", &cd) && + calldata_get_string(&cd, "plain_url", &plain) && plain) + snprintf(buf, size, "%s", plain); + calldata_free(&cd); +} + +static bool set_plain_rtmp(obs_service_t *s, bool enabled) +{ + calldata_t cd = {0}; + calldata_set_ptr(&cd, "service", s); + calldata_set_bool(&cd, "enabled", enabled); + bool ok = proc_handler_call(obs_get_proc_handler(), "rtmp_services_set_plain_rtmp", &cd); + calldata_free(&cd); + return ok; +} + +#define SKIP_WITHOUT_MODULE() \ + do { \ + if (!module_up) { \ + print_message("rtmp-services module not loaded, skipping\n"); \ + skip(); \ + } \ + } while (0) + +/* A built-in service on a listed host connects over RTMPS on port 443, with + * the same host and app path. */ +static void module_listed_server_connects_over_rtmps(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Twitch", LISTED_PLAIN); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_RTMPS); + obs_service_release(s); +} + +/* The service keeps the plain URL so the frontend can offer it. */ +static void module_listed_server_reports_plain_fallback(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + char plain[256]; + obs_service_t *s = make_service("rtmp_common", "Twitch", LISTED_PLAIN); + assert_non_null(s); + fallback_of(s, plain, sizeof(plain)); + assert_string_equal(plain, LISTED_PLAIN); + obs_service_release(s); +} + +/* After the user accepts the fallback, the service hands out the plain URL + * until the frontend clears it for the next start. */ +static void module_plain_rtmp_after_fallback(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Twitch", LISTED_PLAIN); + assert_non_null(s); + assert_true(set_plain_rtmp(s, true)); + assert_string_equal(url_of(s), LISTED_PLAIN); + assert_true(set_plain_rtmp(s, false)); + assert_string_equal(url_of(s), LISTED_RTMPS); + obs_service_release(s); +} + +/* A host that is not on the list stays on plain RTMP and has no fallback. */ +static void module_unlisted_host_stays_plain(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + char plain[256]; + obs_service_t *s = make_service("rtmp_common", "Twitch", "rtmp://127.0.0.1/app"); + assert_non_null(s); + assert_string_equal(url_of(s), "rtmp://127.0.0.1/app"); + fallback_of(s, plain, sizeof(plain)); + assert_string_equal(plain, ""); + obs_service_release(s); +} + +/* The list is per service: a listed host under another service name stays + * plain. */ +static void module_unlisted_service_stays_plain(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Test Service", LISTED_PLAIN); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_PLAIN); + obs_service_release(s); +} + +/* Only the default RTMP port is mapped to 443. */ +static void module_other_port_stays_plain(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Twitch", "rtmp://live.twitch.tv:1936/app"); + assert_non_null(s); + assert_string_equal(url_of(s), "rtmp://live.twitch.tv:1936/app"); + obs_service_release(s); + + s = make_service("rtmp_common", "Twitch", "rtmp://live.twitch.tv:1935/app"); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_RTMPS); + obs_service_release(s); +} + +/* The host match ignores case, and the query string is kept. */ +static void module_host_case_and_query(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + obs_service_t *s = make_service("rtmp_common", "Twitch", "rtmp://LIVE.Twitch.TV/app?x=1"); + assert_non_null(s); + assert_string_equal(url_of(s), "rtmps://LIVE.Twitch.TV:443/app?x=1"); + obs_service_release(s); +} + +/* A server the user typed into a custom service is never changed. */ +static void module_custom_server_unchanged(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + char plain[256]; + obs_service_t *s = make_service("rtmp_custom", NULL, LISTED_PLAIN); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_PLAIN); + fallback_of(s, plain, sizeof(plain)); + assert_string_equal(plain, ""); + obs_service_release(s); +} + +/* A server that is already rtmps:// is used as is, with no fallback. */ +static void module_rtmps_server_has_no_fallback(void **state) +{ + (void)state; + SKIP_WITHOUT_MODULE(); + + char plain[256]; + obs_service_t *s = make_service("rtmp_common", "Twitch", LISTED_RTMPS); + assert_non_null(s); + assert_string_equal(url_of(s), LISTED_RTMPS); + fallback_of(s, plain, sizeof(plain)); + assert_string_equal(plain, ""); + obs_service_release(s); +} + +#ifdef RTMP_SERVICES_MODULE_PATH +/* rtmp-services only upgrades when an output handles RTMPS; this stands in + * for obs-outputs' RTMP output and is never started. */ +static const char *output_name(void *type_data) +{ + (void)type_data; + return "RTMP stand-in"; +} + +static void *output_create(obs_data_t *settings, obs_output_t *output) +{ + (void)settings; + return output; +} + +static void output_destroy(void *data) +{ + (void)data; +} + +static bool output_start(void *data) +{ + (void)data; + return false; +} + +static void output_stop(void *data, uint64_t ts) +{ + (void)data; + (void)ts; +} + +static void output_encoded_packet(void *data, struct encoder_packet *packet) +{ + (void)data; + (void)packet; +} +#endif + +static int setup(void) +{ +#ifdef RTMP_SERVICES_MODULE_PATH + base_set_log_handler(quiet_log, NULL); + + char json[1024]; + snprintf(json, sizeof(json), + "{\"format_version\": %d, \"services\": [" + "{\"name\": \"Twitch\", \"common\": true, \"servers\": [" + "{\"name\": \"Default\", \"url\": \"" LISTED_PLAIN "\"}," + "{\"name\": \"Local\", \"url\": \"rtmp://127.0.0.1/app\"}]}," + "{\"name\": \"Test Service\", \"common\": true, \"servers\": [" + "{\"name\": \"Default\", \"url\": \"" LISTED_PLAIN "\"}]}]}", + RTMP_SERVICES_FORMAT_VERSION); + + if (os_mkdirs(SERVICES_DATA_DIR) == MKDIR_ERROR || + !os_quick_write_utf8_file(SERVICES_DATA_DIR "/services.json", json, strlen(json), false)) { + fprintf(stderr, "could not write " SERVICES_DATA_DIR "/services.json\n"); + return -1; + } + + /* The module config path is under a regular file, so rtmp-services + * has no config directory: it neither reads a cached services.json + * nor starts its service update download. */ + if (!obs_startup("en-US", SERVICES_DATA_DIR "/services.json", NULL)) { + fprintf(stderr, "cases skipped: obs_startup failed (no display?)\n"); + return 0; + } + + struct obs_output_info info = { + .id = "test_rtmp_output", + .flags = OBS_OUTPUT_AV | OBS_OUTPUT_ENCODED | OBS_OUTPUT_SERVICE, + .get_name = output_name, + .create = output_create, + .destroy = output_destroy, + .start = output_start, + .stop = output_stop, + .encoded_packet = output_encoded_packet, + .encoded_video_codecs = "h264", + .encoded_audio_codecs = "aac", + .protocols = "RTMP;RTMPS", + }; + obs_register_output(&info); + + obs_module_t *module = NULL; + if (obs_open_module(&module, RTMP_SERVICES_MODULE_PATH, SERVICES_DATA_DIR) != MODULE_SUCCESS || + !obs_init_module(module)) { + fprintf(stderr, "could not load rtmp-services\n"); + return -1; + } + + module_up = true; +#else + (void)quiet_log; + fprintf(stderr, "cases skipped: rtmp-services is not a loadable module here\n"); +#endif + return 0; +} + +int main(void) +{ + if (setup() != 0) + return 1; + + const struct CMUnitTest tests[] = { + cmocka_unit_test(module_listed_server_connects_over_rtmps), + cmocka_unit_test(module_listed_server_reports_plain_fallback), + cmocka_unit_test(module_plain_rtmp_after_fallback), + cmocka_unit_test(module_unlisted_host_stays_plain), + cmocka_unit_test(module_unlisted_service_stays_plain), + cmocka_unit_test(module_other_port_stays_plain), + cmocka_unit_test(module_host_case_and_query), + cmocka_unit_test(module_custom_server_unchanged), + cmocka_unit_test(module_rtmps_server_has_no_fallback), + }; + int failed = cmocka_run_group_tests(tests, NULL, NULL); + + if (module_up) + obs_shutdown(); + return failed; +} diff --git a/test/cmocka/tests.d/test_rtmps_fallback_dialog.cmake b/test/cmocka/tests.d/test_rtmps_fallback_dialog.cmake new file mode 100644 index 0000000000..54fb270b78 --- /dev/null +++ b/test/cmocka/tests.d/test_rtmps_fallback_dialog.cmake @@ -0,0 +1,24 @@ +# The "Proceed anyway" dialog the frontend shows when an RTMPS stream fails to +# connect (obs-rust/obs-studio#2205): a three-second countdown that accepts on +# its own, Cancel, and the stderr/log line. Builds the real dialog on Qt's +# offscreen platform, so it needs Qt Widgets but no display and no frontend +# build. +find_package(Qt6 QUIET COMPONENTS Widgets) +if(TARGET Qt6::Widgets AND NOT OS_WINDOWS) + add_executable( + test_rtmps_fallback_dialog + test_rtmps_fallback_dialog.cpp + "${CMAKE_SOURCE_DIR}/frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp" + "${CMAKE_SOURCE_DIR}/frontend/dialogs/OBSRtmpsFallback.hpp" + ) + set_target_properties(test_rtmps_fallback_dialog PROPERTIES AUTOMOC ON) + target_include_directories( + test_rtmps_fallback_dialog + PRIVATE ${CMOCKA_INCLUDE_DIR} "${CMAKE_SOURCE_DIR}/frontend/dialogs" + ) + target_link_libraries(test_rtmps_fallback_dialog PRIVATE OBS::libobs Qt6::Widgets ${CMOCKA_LIBRARIES}) + add_dependencies(cmocka-tests test_rtmps_fallback_dialog) + add_test(test_rtmps_fallback_dialog ${CMAKE_CURRENT_BINARY_DIR}/test_rtmps_fallback_dialog) + set_tests_properties(test_rtmps_fallback_dialog PROPERTIES TIMEOUT 60) + cmocka_qt_runtime(test_rtmps_fallback_dialog Qt6::Core) +endif() diff --git a/test/cmocka/tests.d/test_rtmps_upgrade.cmake b/test/cmocka/tests.d/test_rtmps_upgrade.cmake new file mode 100644 index 0000000000..04e5a9a636 --- /dev/null +++ b/test/cmocka/tests.d/test_rtmps_upgrade.cmake @@ -0,0 +1,21 @@ +# Built-in services connect over RTMPS when their ingest server is known to +# accept it, and keep the plain URL for the frontend's fallback +# (obs-rust/obs-studio#2205). The cases load the real rtmp-services module, so +# they only run on Linux, and obs_startup needs a display there (run under +# xvfb-run, as Tier 2 does). +add_executable(test_rtmps_upgrade test_rtmps_upgrade.c) +target_include_directories(test_rtmps_upgrade PRIVATE ${CMOCKA_INCLUDE_DIR}) +target_link_libraries(test_rtmps_upgrade PRIVATE OBS::libobs ${CMOCKA_LIBRARIES}) +if(OS_LINUX AND TARGET rtmp-services) + target_compile_definitions( + test_rtmps_upgrade + PRIVATE + RTMP_SERVICES_MODULE_PATH="$" + SERVICES_DATA_DIR="${CMAKE_CURRENT_BINARY_DIR}/test_rtmps_upgrade_data" + ) + target_include_directories(test_rtmps_upgrade PRIVATE "${CMAKE_SOURCE_DIR}/plugins/rtmp-services") + add_dependencies(test_rtmps_upgrade rtmp-services) +endif() +add_dependencies(cmocka-tests test_rtmps_upgrade) +add_test(NAME test_rtmps_upgrade COMMAND test_rtmps_upgrade) +set_tests_properties(test_rtmps_upgrade PROPERTIES TIMEOUT 60) From 0af397983e4c17ffd4880bf95e80d6ffc3dd3f5f Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sat, 24 Oct 2026 16:35:07 -0400 Subject: [PATCH 2/7] rtmp-services: Stream over RTMPS where the ingest server accepts it Built-in services whose ingest servers accept RTMPS on port 443 now connect over it. tools/rtmps-endpoint-check.py checked every server of every built-in RTMP service and Twitch's ingest list without an account: TLS with a valid certificate, the RTMP handshake and connect, and a made-up stream key rejected the same way as over plain RTMP. All servers of Twitch, Vimeo, niconico, BoxCast, Bitmovin, Eventials and EventLive.pro passed; rtmps-upgrade.c.h lists their hosts. The list is in code because services.json is replaced at runtime by the services update feed. rtmp_common_url() hands out rtmps://:443/ for a listed host on the default port and keeps the plain URL. The stored server is unchanged, so existing profiles switch too. Custom servers, other ports, rtmps:// servers and unlisted hosts stay as they are. When an upgraded stream fails to connect, the frontend shows OBSRtmpsFallback: "Proceed anyway" counts down three seconds and then restarts the stream over plain RTMP unless the user cancels, and the fallback goes to stderr and the log. With the window hidden the countdown runs with a tray notice. The next start tries RTMPS again; multitrack video keeps its own URLs. The frontend reaches the service through two global proc handler commands rtmp-services registers (rtmp_services_rtmps_fallback, rtmp_services_set_plain_rtmp), since obs_service_get_proc_handler() is not exported. Fixes #2205. --- docs/features.md | 9 + .../frontend-dialogs-obsrtmpsfallback.pch.hpp | 2 + frontend/data/locale/en-US.ini | 4 + frontend/dialogs/OBSRtmpsFallback.cpp.hpp | 31 +- frontend/dialogs/OBSRtmpsFallback.hpp | 3 + frontend/widgets/OBSBasic.hpp | 9 + frontend/widgets/OBSBasic_Streaming.cpp.hpp | 72 ++++ plugins/rtmp-services/rtmp-common.c.h | 76 +++- plugins/rtmp-services/rtmp-services-main.c.h | 2 + .../service-specific/rtmps-upgrade.c.h | 124 ++++++- tools/rtmps-endpoint-check.py | 330 ++++++++++++++++++ 11 files changed, 654 insertions(+), 8 deletions(-) create mode 100755 tools/rtmps-endpoint-check.py diff --git a/docs/features.md b/docs/features.md index 09df27eb38..c5353c0616 100644 --- a/docs/features.md +++ b/docs/features.md @@ -242,6 +242,15 @@ not get a line. 2026-10-11. Issue [#19](https://github.com/obs-rust/obs-studio/issues/19), PRs [#162](https://github.com/obs-rust/obs-studio/pull/162), [#181](https://github.com/obs-rust/obs-studio/pull/181). +- **Encrypted streaming where the service supports it.** Twitch, Vimeo, + niconico, BoxCast, Bitmovin, Eventials and EventLive.pro stream over RTMPS + (port 443) instead of plain RTMP, on every server checked to accept it; + existing profiles switch too, with no setting to change. If the encrypted + connection fails, a "Proceed anyway" dialog counts down three seconds and + then streams to the same server over plain RTMP, unless you cancel; the + fallback is written to the log and to stderr. Custom servers are + unchanged. *All*, pending merge. Issue + [#2205](https://github.com/obs-rust/obs-studio/issues/2205). ### Sources and filters diff --git a/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp b/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp index 08704559cd..58df2d9be6 100644 --- a/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp +++ b/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp @@ -14,5 +14,7 @@ #include #include +#include + #include #include diff --git a/frontend/data/locale/en-US.ini b/frontend/data/locale/en-US.ini index dc10b3fa52..989c6b8444 100644 --- a/frontend/data/locale/en-US.ini +++ b/frontend/data/locale/en-US.ini @@ -446,6 +446,10 @@ Output.ConnectFail.InvalidStream="Could not access the specified channel or stre Output.ConnectFail.HdrDisabled="HDR output is currently disabled for this output." Output.ConnectFail.Error="An unexpected error occurred when trying to connect to the server. More information in the log file." Output.ConnectFail.Disconnected="Disconnected from server." +Output.RtmpsFallback.Title="Encrypted connection failed" +Output.RtmpsFallback.Text="OBS could not connect to the server over an encrypted connection (RTMPS).\n\nStream to %1 without encryption instead?" +Output.RtmpsFallback.Proceed="Proceed anyway" +Output.RtmpsFallback.Tray="Encrypted connection failed; streaming without encryption." # output streaming-related messages Output.StreamEncodeError.Title="Encoding error" diff --git a/frontend/dialogs/OBSRtmpsFallback.cpp.hpp b/frontend/dialogs/OBSRtmpsFallback.cpp.hpp index c2f6c9eb61..b933ad808a 100644 --- a/frontend/dialogs/OBSRtmpsFallback.cpp.hpp +++ b/frontend/dialogs/OBSRtmpsFallback.cpp.hpp @@ -20,11 +20,38 @@ OBSRtmpsFallback::OBSRtmpsFallback(QWidget *parent, const Texts &texts) : QDialo connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); layout->addWidget(buttons); + + tick.setInterval(50); + connect(&tick, &QTimer::timeout, this, &OBSRtmpsFallback::Tick); +} + +void OBSRtmpsFallback::showEvent(QShowEvent *event) +{ + QDialog::showEvent(event); + elapsed.start(); + progress->setValue(0); + tick.start(); } -void OBSRtmpsFallback::Tick() {} +void OBSRtmpsFallback::Tick() +{ + /* Cancel or a click closed the dialog: the countdown ends with it. */ + if (!isVisible()) { + tick.stop(); + return; + } + + qint64 ms = elapsed.elapsed(); + progress->setValue((int)std::min(ms, CountdownMs)); + if (ms >= CountdownMs) { + tick.stop(); + accept(); + } +} void OBSRtmpsFallback::LogFallback(const char *plain_url) { - (void)plain_url; + fprintf(stderr, "RTMPS connection failed; streaming without encryption to %s\n", plain_url); + fflush(stderr); + blog(LOG_WARNING, "RTMPS connection failed; streaming without encryption to %s", plain_url); } diff --git a/frontend/dialogs/OBSRtmpsFallback.hpp b/frontend/dialogs/OBSRtmpsFallback.hpp index 5bef399d05..77cad2a072 100644 --- a/frontend/dialogs/OBSRtmpsFallback.hpp +++ b/frontend/dialogs/OBSRtmpsFallback.hpp @@ -37,4 +37,7 @@ class OBSRtmpsFallback : public QDialog { QElapsedTimer elapsed; void Tick(); + +protected: + void showEvent(QShowEvent *event) override; }; diff --git a/frontend/widgets/OBSBasic.hpp b/frontend/widgets/OBSBasic.hpp index b8440528c7..0264727482 100644 --- a/frontend/widgets/OBSBasic.hpp +++ b/frontend/widgets/OBSBasic.hpp @@ -1400,6 +1400,15 @@ private slots: bool streamingStopping = false; bool streamingStarting = false; + /* RTMPS fallback (OBSRtmpsFallback): whether the next start keeps + * plain RTMP after "Proceed anyway", and whether the stream being + * started takes its URLs from multitrack video instead of the service. */ + bool rtmpsFallbackRestart = false; + bool streamUsesMultitrack = false; + std::string RtmpsFallbackUrl(); + void SetPlainRtmp(bool enabled); + void OfferRtmpsFallback(const std::string &plainUrl); + public slots: void DisplayStreamStartError(); void StartStreaming(); diff --git a/frontend/widgets/OBSBasic_Streaming.cpp.hpp b/frontend/widgets/OBSBasic_Streaming.cpp.hpp index d3fd8ffdfb..30217a03d4 100644 --- a/frontend/widgets/OBSBasic_Streaming.cpp.hpp +++ b/frontend/widgets/OBSBasic_Streaming.cpp.hpp @@ -69,6 +69,11 @@ void OBSBasic::StartStreaming() } } + /* A start the user asks for tries RTMPS again; only the restart after + * "Proceed anyway" keeps plain RTMP. */ + SetPlainRtmp(rtmpsFallbackRestart); + rtmpsFallbackRestart = false; + emit StreamingPreparing(); if (sysTrayStream) { @@ -92,6 +97,8 @@ void OBSBasic::StartStreaming() sysTrayStream->setText("Basic.Main.Connecting"); } + streamUsesMultitrack = outputHandler->multitrackVideoActive; + if (!outputHandler->StartStreaming(service)) { DisplayStreamStartError(); return; @@ -335,6 +342,13 @@ void OBSBasic::StreamingStop(int code, QString last_error) errorDescription = Str("Output.ConnectFail.Disconnected"); } + /* A service connection that rtmp-services upgraded to RTMPS can retry + * over plain RTMP; multitrack video brings its own URLs. */ + std::string rtmpsPlainUrl; + if (code == OBS_OUTPUT_CONNECT_FAILED && !streamUsesMultitrack) { + rtmpsPlainUrl = RtmpsFallbackUrl(); + } + if (use_last_error && !last_error.isEmpty()) { dstr_printf(errorMessage, "%s\n\n%s", errorDescription, QT_TO_UTF8(last_error)); } else { @@ -368,6 +382,9 @@ void OBSBasic::StreamingStop(int code, QString last_error) : QTStr("Output.StreamEncodeError.Msg.LastError").arg(last_error); OBSMessageBox::information(this, QTStr("Output.StreamEncodeError.Title"), msg); + } else if (!rtmpsPlainUrl.empty()) { + OfferRtmpsFallback(rtmpsPlainUrl); + } else if (code != OBS_OUTPUT_SUCCESS && isVisible()) { OBSMessageBox::information(this, QTStr("Output.ConnectFail.Title"), QT_UTF8(errorMessage)); @@ -384,6 +401,61 @@ void OBSBasic::StreamingStop(int code, QString last_error) } } +std::string OBSBasic::RtmpsFallbackUrl() +{ + calldata_t cd = {0}; + const char *plain = nullptr; + std::string url; + + calldata_set_ptr(&cd, "service", GetService()); + if (proc_handler_call(obs_get_proc_handler(), "rtmp_services_rtmps_fallback", &cd) && + calldata_get_string(&cd, "plain_url", &plain) && plain) { + url = plain; + } + calldata_free(&cd); + return url; +} + +void OBSBasic::SetPlainRtmp(bool enabled) +{ + calldata_t cd = {0}; + calldata_set_ptr(&cd, "service", GetService()); + calldata_set_bool(&cd, "enabled", enabled); + proc_handler_call(obs_get_proc_handler(), "rtmp_services_set_plain_rtmp", &cd); + calldata_free(&cd); +} + +/* The RTMPS connection failed: "Proceed anyway" restarts the stream over + * plain RTMP after a three-second countdown unless the user cancels. With the + * window hidden there is nobody to ask, so the countdown runs on its own. */ +void OBSBasic::OfferRtmpsFallback(const std::string &plainUrl) +{ + auto proceed = [this, plainUrl]() { + OBSRtmpsFallback::LogFallback(plainUrl.c_str()); + rtmpsFallbackRestart = true; + QMetaObject::invokeMethod(this, &OBSBasic::StartStreaming, Qt::QueuedConnection); + }; + + if (!isVisible()) { + SysTrayNotify(QTStr("Output.RtmpsFallback.Tray"), QSystemTrayIcon::Warning); + QTimer::singleShot(OBSRtmpsFallback::CountdownMs, this, proceed); + return; + } + + OBSRtmpsFallback::Texts texts = { + QTStr("Output.RtmpsFallback.Title"), + QTStr("Output.RtmpsFallback.Text").arg(QString::fromStdString(plainUrl)), + QTStr("Output.RtmpsFallback.Proceed"), + QTStr("Cancel"), + }; + OBSRtmpsFallback dialog(this, texts); + if (dialog.exec() == QDialog::Accepted) { + proceed(); + } else { + blog(LOG_INFO, "RTMPS fallback cancelled; the stream was not started"); + } +} + void OBSBasic::StreamActionTriggered() { if (outputHandler->StreamingActive()) { diff --git a/plugins/rtmp-services/rtmp-common.c.h b/plugins/rtmp-services/rtmp-common.c.h index 90fdb71359..63ed68fd60 100644 --- a/plugins/rtmp-services/rtmp-common.c.h +++ b/plugins/rtmp-services/rtmp-common.c.h @@ -14,6 +14,14 @@ struct rtmp_common { char **audio_codecs; bool supports_additional_audio_track; + + /* The RTMPS URL handed out instead of a plain one (rtmps-upgrade.h), + * the plain URL it replaced (NULL when nothing was upgraded), and + * whether the frontend asked for the plain URL after a failed RTMPS + * connection. */ + struct dstr rtmps_url; + char *rtmps_fallback; + bool plain_rtmp; }; static const char *rtmp_common_getname(void *unused) @@ -192,6 +200,8 @@ static void rtmp_common_destroy(void *data) bfree(service->protocol); bfree(service->server); bfree(service->key); + dstr_free(&service->rtmps_url); + bfree(service->rtmps_fallback); bfree(service); } @@ -489,7 +499,8 @@ static void fill_servers(obs_property_t *servers_prop, json_t *service, const ch return; } - /* Assumption: Twitch should be RTMP only, so no RTMPS check */ + /* Twitch lists its plain RTMP servers; rtmp_common_url() connects to + * the ones in rtmps-upgrade.c.h over RTMPS. */ if (strcmp(name, "Twitch") == 0) { if (fill_twitch_servers(servers_prop)) return; @@ -810,7 +821,7 @@ static void rtmp_common_apply_settings(void *data, obs_data_t *video_settings, o } } -static const char *rtmp_common_url(void *data) +static const char *rtmp_common_plain_url(void *data) { struct rtmp_common *service = data; @@ -869,6 +880,67 @@ static const char *rtmp_common_url(void *data) return service->server; } +/* Hands out the RTMPS form of a plain URL on a host known to accept it, unless + * the frontend asked for plain RTMP after the encrypted connection failed. */ +static const char *rtmp_common_url(void *data) +{ + struct rtmp_common *service = data; + const char *url = rtmp_common_plain_url(data); + struct dstr rtmps = {0}; + + bfree(service->rtmps_fallback); + service->rtmps_fallback = NULL; + + if (service->plain_rtmp || !obs_is_output_protocol_registered("RTMPS") || + !rtmps_upgrade_url(service->service, url, &rtmps)) { + dstr_free(&rtmps); + return url; + } + + /* The output may still hold the last URL; only replace it when it + * changed. */ + if (dstr_cmp(&service->rtmps_url, rtmps.array) != 0) + dstr_move(&service->rtmps_url, &rtmps); + dstr_free(&rtmps); + + service->rtmps_fallback = bstrdup(url); + return service->rtmps_url.array; +} + +static void rtmp_common_rtmps_fallback_proc(void *param, calldata_t *cd) +{ + UNUSED_PARAMETER(param); + obs_service_t *service = calldata_ptr(cd, "service"); + + if (!service || strcmp(obs_service_get_id(service), "rtmp_common") != 0) + return; + + struct rtmp_common *data = obs_obj_get_data(service); + rtmp_common_url(data); + calldata_set_string(cd, "plain_url", data->rtmps_fallback ? data->rtmps_fallback : ""); +} + +static void rtmp_common_set_plain_rtmp_proc(void *param, calldata_t *cd) +{ + UNUSED_PARAMETER(param); + obs_service_t *service = calldata_ptr(cd, "service"); + + if (!service || strcmp(obs_service_get_id(service), "rtmp_common") != 0) + return; + + struct rtmp_common *data = obs_obj_get_data(service); + data->plain_rtmp = calldata_bool(cd, "enabled"); +} + +void rtmp_common_register_procs(void) +{ + proc_handler_t *ph = obs_get_proc_handler(); + proc_handler_add(ph, "void rtmp_services_rtmps_fallback(in ptr service, out string plain_url)", + rtmp_common_rtmps_fallback_proc, NULL); + proc_handler_add(ph, "void rtmp_services_set_plain_rtmp(in ptr service, in bool enabled)", + rtmp_common_set_plain_rtmp_proc, NULL); +} + static const char *rtmp_common_key(void *data) { struct rtmp_common *service = data; diff --git a/plugins/rtmp-services/rtmp-services-main.c.h b/plugins/rtmp-services/rtmp-services-main.c.h index b407037203..bc1c9a3c2b 100644 --- a/plugins/rtmp-services/rtmp-services-main.c.h +++ b/plugins/rtmp-services/rtmp-services-main.c.h @@ -14,6 +14,7 @@ static const char *RTMP_SERVICES_URL = (const char *)SERVICES_URL; extern struct obs_service_info rtmp_common_service; extern struct obs_service_info rtmp_custom_service; +extern void rtmp_common_register_procs(void); static update_info_t *update_info = NULL; static struct dstr module_name = {0}; @@ -93,6 +94,7 @@ bool obs_module_load(void) proc_handler_t *ph = obs_get_proc_handler(); proc_handler_add(ph, "void twitch_ingests_refresh(int seconds)", refresh_callback, NULL); proc_handler_add(ph, "void amazon_ivs_ingests_refresh(int seconds)", amazon_ivs_refresh_callback, NULL); + rtmp_common_register_procs(); #if defined(ENABLE_SERVICE_UPDATES) char *local_dir = obs_module_file(""); diff --git a/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h b/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h index 9066865dac..5065886999 100644 --- a/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h +++ b/plugins/rtmp-services/service-specific/rtmps-upgrade.c.h @@ -1,9 +1,125 @@ #include "plugins/rtmp-services/build/rtmp-services.pch.h" -bool rtmps_upgrade_url(const char *service, const char *url, struct dstr *out) +/* Ingest hosts that answered on port 443 with a valid certificate for the + * host name, completed the RTMP handshake and connect over TLS, and rejected a + * publish with a made-up stream key the same way as over plain RTMP. Checked + * on 2026-10-24 for every server of every built-in RTMP service and every + * server in Twitch's ingest list (obs-rust/obs-studio#2205) with + * tools/rtmps-endpoint-check.py. A host that is not here stays on plain RTMP. */ +static const struct { + const char *service; + const char *host; +} rtmps_hosts[] = { + {"Bitmovin", "live-input.bitmovin.com"}, + {"BoxCast", "rtmp.boxcast.com"}, + {"Eventials", "transmission.eventials.com"}, + {"EventLive.pro", "go.eventlive.pro"}, + {"niconico (ニコニコ生放送)", "liveorigin.dlive.nicovideo.jp"}, + {"Twitch", "apn10.contribute.live-video.net"}, + {"Twitch", "apn20.contribute.live-video.net"}, + {"Twitch", "aps10.contribute.live-video.net"}, + {"Twitch", "aps20.contribute.live-video.net"}, + {"Twitch", "aps30.contribute.live-video.net"}, + {"Twitch", "euc10.contribute.live-video.net"}, + {"Twitch", "eun10.contribute.live-video.net"}, + {"Twitch", "euw10.contribute.live-video.net"}, + {"Twitch", "euw30.contribute.live-video.net"}, + {"Twitch", "ingest.global-contribute.live-video.net"}, + {"Twitch", "live-ams.twitch.tv"}, + {"Twitch", "live-arn.twitch.tv"}, + {"Twitch", "live-atl.twitch.tv"}, + {"Twitch", "live-ber.twitch.tv"}, + {"Twitch", "live-cdg.twitch.tv"}, + {"Twitch", "live-cph.twitch.tv"}, + {"Twitch", "live-den.twitch.tv"}, + {"Twitch", "live-dfw.twitch.tv"}, + {"Twitch", "live-eze.twitch.tv"}, + {"Twitch", "live-fra.twitch.tv"}, + {"Twitch", "live-hel.twitch.tv"}, + {"Twitch", "live-hkg.twitch.tv"}, + {"Twitch", "live-hou.twitch.tv"}, + {"Twitch", "live-iad.twitch.tv"}, + {"Twitch", "live-jfk.twitch.tv"}, + {"Twitch", "live-lax.twitch.tv"}, + {"Twitch", "live-lhr.twitch.tv"}, + {"Twitch", "live-lim.twitch.tv"}, + {"Twitch", "live-lis.twitch.tv"}, + {"Twitch", "live-mad.twitch.tv"}, + {"Twitch", "live-mde.twitch.tv"}, + {"Twitch", "live-mia.twitch.tv"}, + {"Twitch", "live-mil.twitch.tv"}, + {"Twitch", "live-mrs.twitch.tv"}, + {"Twitch", "live-ord.twitch.tv"}, + {"Twitch", "live-osl.twitch.tv"}, + {"Twitch", "live-pdx.twitch.tv"}, + {"Twitch", "live-phx.twitch.tv"}, + {"Twitch", "live-prg.twitch.tv"}, + {"Twitch", "live-qro.twitch.tv"}, + {"Twitch", "live-rio.twitch.tv"}, + {"Twitch", "live-sao.twitch.tv"}, + {"Twitch", "live-scl.twitch.tv"}, + {"Twitch", "live-sea.twitch.tv"}, + {"Twitch", "live-sel.twitch.tv"}, + {"Twitch", "live-sfo.twitch.tv"}, + {"Twitch", "live-sin.twitch.tv"}, + {"Twitch", "live-sjc.twitch.tv"}, + {"Twitch", "live-slc.twitch.tv"}, + {"Twitch", "live-syd.twitch.tv"}, + {"Twitch", "live-tpe.twitch.tv"}, + {"Twitch", "live-tyo.twitch.tv"}, + {"Twitch", "live-vie.twitch.tv"}, + {"Twitch", "live-waw.twitch.tv"}, + {"Twitch", "live-ymq.twitch.tv"}, + {"Twitch", "live-yto.twitch.tv"}, + {"Twitch", "live.twitch.tv"}, + {"Twitch", "sae10.contribute.live-video.net"}, + {"Twitch", "use10.contribute.live-video.net"}, + {"Twitch", "use20.contribute.live-video.net"}, + {"Twitch", "usw20.contribute.live-video.net"}, + {"Vimeo", "rtmp.cloud.vimeo.com"}, +}; + +#define UPGRADE_FROM_SCHEME "rtmp://" +#define UPGRADE_FROM_PORT "1935" + +static bool rtmps_host_listed(const char *service, const char *host, size_t host_len) { - (void)service; - (void)url; - (void)out; + for (size_t i = 0; i < sizeof(rtmps_hosts) / sizeof(rtmps_hosts[0]); i++) { + if (strcmp(rtmps_hosts[i].service, service) == 0 && strlen(rtmps_hosts[i].host) == host_len && + astrcmpi_n(rtmps_hosts[i].host, host, host_len) == 0) + return true; + } return false; } + +bool rtmps_upgrade_url(const char *service, const char *url, struct dstr *out) +{ + if (!service || !url || astrcmpi_n(url, UPGRADE_FROM_SCHEME, sizeof(UPGRADE_FROM_SCHEME) - 1) != 0) + return false; + + const char *host = url + sizeof(UPGRADE_FROM_SCHEME) - 1; + size_t host_len = strcspn(host, ":/?"); + const char *rest = host + host_len; + + /* Only the default RTMP port maps to RTMPS on 443. */ + if (*rest == ':') { + const char *port = rest + 1; + size_t port_len = strcspn(port, "/?"); + if (port_len != sizeof(UPGRADE_FROM_PORT) - 1 || + strncmp(port, UPGRADE_FROM_PORT, sizeof(UPGRADE_FROM_PORT) - 1) != 0) + return false; + rest = port + port_len; + } + + if (!host_len || !rtmps_host_listed(service, host, host_len)) + return false; + + dstr_copy(out, "rtmps://"); + dstr_ncat(out, host, host_len); + dstr_cat(out, ":443"); + dstr_cat(out, rest); + return true; +} + +#undef UPGRADE_FROM_SCHEME +#undef UPGRADE_FROM_PORT diff --git a/tools/rtmps-endpoint-check.py b/tools/rtmps-endpoint-check.py new file mode 100755 index 0000000000..672a444e00 --- /dev/null +++ b/tools/rtmps-endpoint-check.py @@ -0,0 +1,330 @@ +#!/usr/bin/env -S uv run --script +# /// script +# requires-python = ">=3.10" +# /// +"""Check whether RTMP ingest servers also work over RTMPS, without an account. + +plugins/rtmp-services/service-specific/rtmps-upgrade.c.h lists the hosts that +pass. Rerun this when services.json or Twitch's ingest list changes, and add +a host to that list only when every server of its service passes. + +For each server URL this runs, over plain RTMP and over RTMPS (same host and +app path, port 443 unless the URL says otherwise): + DNS + TCP, TLS with certificate/hostname verification (RTMPS only), + RTMP handshake, connect(app), then publish with a fake key. +No media is ever sent. A server "matches" when RTMPS gets as far as plain RTMP +and the fake-key publish is answered the same way. + +Usage: + uv run tools/rtmps-endpoint-check.py plugins/rtmp-services/data/services.json \ + --twitch-feed --twitch-list > results.jsonl + +Each output line is one server: its verdict (pass, fail, check, untestable) +and how far plain RTMP and RTMPS got. +""" +import argparse, json, os, socket, ssl, struct, sys, time +from urllib.parse import urlsplit + +FAKE_KEY = "rtmps-endpoint-check-invalid-key" +TIMEOUT = 8 + + +# --- AMF0 ------------------------------------------------------------------- +def amf_enc(v): + if v is None: + return b"\x05" + if isinstance(v, bool): + return b"\x01" + (b"\x01" if v else b"\x00") + if isinstance(v, (int, float)): + return b"\x00" + struct.pack(">d", float(v)) + if isinstance(v, str): + b = v.encode() + return b"\x02" + struct.pack(">H", len(b)) + b + if isinstance(v, dict): + out = b"\x03" + for k, x in v.items(): + kb = k.encode() + out += struct.pack(">H", len(kb)) + kb + amf_enc(x) + return out + b"\x00\x00\x09" + raise TypeError(v) + + +def amf_dec(b, i=0): + t = b[i] + i += 1 + if t == 0x00: + return struct.unpack(">d", b[i:i + 8])[0], i + 8 + if t == 0x01: + return bool(b[i]), i + 1 + if t == 0x02: + n = struct.unpack(">H", b[i:i + 2])[0] + return b[i + 2:i + 2 + n].decode("utf-8", "replace"), i + 2 + n + if t in (0x03, 0x08): + if t == 0x08: + i += 4 + o = {} + while True: + n = struct.unpack(">H", b[i:i + 2])[0] + if n == 0 and b[i + 2] == 0x09: + return o, i + 3 + k = b[i + 2:i + 2 + n].decode("utf-8", "replace") + o[k], i = amf_dec(b, i + 2 + n) + if t in (0x05, 0x06): + return None, i + raise ValueError("amf type %d" % t) + + +def amf_all(b): + out, i = [], 0 + while i < len(b): + try: + v, i = amf_dec(b, i) + except Exception: + break + out.append(v) + return out + + +# --- RTMP chunk stream ------------------------------------------------------ +class Conn: + def __init__(self, sock): + self.s = sock + self.buf = b"" + self.in_chunk = 128 + self.prev = {} + + def recv_exact(self, n): + while len(self.buf) < n: + d = self.s.recv(65536) + if not d: + raise ConnectionError("closed by server") + self.buf += d + out, self.buf = self.buf[:n], self.buf[n:] + return out + + def send_msg(self, csid, mtype, payload, msid=0): + hdr = bytes([csid & 0x3F]) + b"\x00\x00\x00" + struct.pack(">I", len(payload))[1:] + bytes([mtype]) + struct.pack("> 6, b0 & 0x3F + if csid == 0: + csid = 64 + self.recv_exact(1)[0] + elif csid == 1: + x = self.recv_exact(2) + csid = 64 + x[0] + x[1] * 256 + p = dict(self.prev.get(csid, {"ts": 0, "len": 0, "type": 0, "msid": 0})) + if fmt <= 2: + ts = int.from_bytes(self.recv_exact(3), "big") + if fmt <= 1: + p["len"] = int.from_bytes(self.recv_exact(3), "big") + p["type"] = self.recv_exact(1)[0] + if fmt == 0: + p["msid"] = struct.unpack("I", have[:4])[0] & 0x7FFFFFFF + continue + return p["type"], have + + +def status_of(vals): + for v in vals: + if isinstance(v, dict) and "code" in v: + return v.get("code"), v.get("level"), str(v.get("description", ""))[:160] + return None, None, None + + +def attempt(host, port, app, tcurl, use_tls): # noqa: C901 + r = {"host": host, "port": port, "tls": use_tls, "stage": "dns"} + try: + socket.getaddrinfo(host, port) + r["stage"] = "tcp" + raw = socket.create_connection((host, port), timeout=TIMEOUT) + raw.settimeout(TIMEOUT) + s = raw + if use_tls: + r["stage"] = "tls" + s = ssl.create_default_context().wrap_socket(raw, server_hostname=host) + r["tls_version"] = s.version() + c = Conn(s) + r["stage"] = "handshake" + c1 = struct.pack(">I", 0) + b"\x00" * 4 + os.urandom(1528) + s.sendall(b"\x03" + c1) + s0 = c.recv_exact(1) + if s0 != b"\x03": + raise ValueError("S0=%s (not RTMP)" % s0.hex()) + s1 = c.recv_exact(1536) + c.recv_exact(1536) + s.sendall(s1) + r["stage"] = "connect" + c.invoke("connect", 1, {"app": app, "type": "nonprivate", "flashVer": "FMLE/3.0 (compatible; FMSc/1.0)", "tcUrl": tcurl}) + while True: + t, body = c.read_msg() + if t == 20: + vals = amf_all(body) + if vals and vals[0] in ("_result", "_error") and vals[1] == 1: + code, _, desc = status_of(vals) + r["connect"] = code or vals[0] + if vals[0] == "_error": + r["connect_desc"] = desc + return r + break + r["stage"] = "publish" + c.invoke("releaseStream", 2, None, FAKE_KEY) + c.invoke("FCPublish", 3, None, FAKE_KEY) + c.invoke("createStream", 4, None) + msid = None + deadline = time.time() + TIMEOUT + while time.time() < deadline: + t, body = c.read_msg() + if t != 20: + continue + vals = amf_all(body) + if not vals: + continue + if vals[0] == "_result" and vals[1] == 4 and msid is None: + msid = int(vals[3]) if len(vals) > 3 and isinstance(vals[3], float) else 1 + c.invoke("publish", 5, None, FAKE_KEY, "live", csid=4, msid=msid) + continue + if vals[0] == "_error" and vals[1] == 4: + r["publish"] = "createStream:_error" + return r + if vals[0] in ("onStatus", "onFCPublish"): + code, level, desc = status_of(vals) + if code and (vals[0] == "onStatus" or level == "error"): + r["publish"] = code + r["publish_desc"] = desc + return r + r["publish"] = "timeout" + except ConnectionError as e: + r["error"] = str(e) + if r["stage"] == "publish": + r["publish"] = "closed" + except ssl.SSLCertVerificationError as e: + r["error"] = "cert: " + (e.verify_message or str(e)) + except Exception as e: + r["error"] = "%s: %s" % (type(e).__name__, e) + finally: + try: + s.close() + except Exception: + pass + return r + + +def split(url): + u = urlsplit(url) + path = u.path.lstrip("/") + app = path.rstrip("/") + if u.query: + app += "?" + u.query + return u.hostname, u.port, app + + +def outcome(r): + if "publish" in r: + return "publish:" + r["publish"] + if "connect" in r: + return "connect:" + r["connect"] + return "fail@" + r["stage"] + + +def check(service, url): + host, port, app = split(url) + is_tls = url.lower().startswith("rtmps://") + plain_port = 1935 if is_tls else (port or 1935) + tls_port = port if is_tls and port else 443 + plain = attempt(host, plain_port, app, "rtmp://%s:%d/%s" % (host, plain_port, app), False) + time.sleep(1) + tls = attempt(host, tls_port, app, "rtmps://%s:%d/%s" % (host, tls_port, app), True) + po, to = outcome(plain), outcome(tls) + if to.startswith("publish:") and to != "publish:NetStream.Publish.Start" and (po == to or po.startswith("fail@")): + verdict = "pass" + elif to == "publish:NetStream.Publish.Start": + verdict = "check" # fake key accepted; can't tell what a real key does + elif po.startswith("fail@") and to.startswith("fail@"): + verdict = "untestable" + elif po.startswith("publish:") and to.startswith("publish:"): + verdict = "check" # both answer publish, but differently + else: + verdict = "fail" + return {"service": service, "url": url, "verdict": verdict, "plain": po, "rtmps": to, "plain_detail": plain, "rtmps_detail": tls} + + +def main(): # noqa: C901 + ap = argparse.ArgumentParser() + ap.add_argument("services") + ap.add_argument("--twitch-feed", action="store_true") + ap.add_argument("--extra", nargs="*", default=[], help="NAME=URL") + ap.add_argument("--only", nargs="*") + ap.add_argument("--jobs", type=int, default=8, help="services checked in parallel") + ap.add_argument("--twitch-list", action="store_true", help="with --twitch-feed, also check the Twitch servers in services.json") + a = ap.parse_args() + d = json.load(open(a.services)) + jobs = [] + for s in d["services"]: + if a.only and s["name"] not in a.only: + continue + if s["name"] == "Twitch" and a.twitch_feed and not a.twitch_list: + continue + for x in s.get("servers", []): + if x["url"].lower().startswith(("rtmp://", "rtmps://")): + jobs.append((s["name"], x["url"])) + if a.twitch_feed: + import urllib.request + feed = json.load(urllib.request.urlopen("https://ingest.twitch.tv/ingests", timeout=15)) + for i in feed["ingests"]: + jobs.append(("Twitch", i["url_template_secure"].replace("/{stream_key}", ""))) + # The built-in fallback when the list can't be loaded (twitch.c.h). + jobs.append(("Twitch", "rtmp://live.twitch.tv/app")) + for e in a.extra: + n, u = e.split("=", 1) + jobs.append((n, u)) + seen, by_service = set(), {} + for name, url in jobs: + key = (name, url.lower().replace("rtmps://", "rtmp://").split("?")[0].rstrip("/")) + if key in seen: + continue + seen.add(key) + by_service.setdefault(name, []).append(url) + + import threading + from concurrent.futures import ThreadPoolExecutor + lock = threading.Lock() + + def run_service(name): + # Each worker takes one provider and checks its servers in sequence. + for url in by_service[name]: + r = check(name, url) + with lock: + print(json.dumps(r, ensure_ascii=False), flush=True) + time.sleep(1) + + with ThreadPoolExecutor(max_workers=a.jobs) as ex: + list(ex.map(run_service, by_service)) + + +if __name__ == "__main__": + main() From b7cc6c040db9c6a9d4f9fc3958482bf8cc5e84b9 Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 25 Oct 2026 16:21:55 -0400 Subject: [PATCH 3/7] test: Stop an RTMPS start whose certificate does not verify The frontend should retry a failed RTMPS start over plain RTMP only for a connection failure: a timeout, a refused or reset connection, or a handshake that fails for another reason. A server certificate that does not verify, also one issued for another host name, should stop the start (docs/fallback-warning.md rule 7). test_rtmps_cert_no_fallback builds the real rtmp-stream.c and librtmp and connects to a TLS listener on the loopback interface. The listener presents a certificate the test makes at run time for another host, once trusted (only the host name is wrong) and once untrusted. Both must be reported through the RTMP output's rtmp_tls_cert_unverified proc. A listener that closes the connection during the handshake, and a port with nothing listening, must not be. The output has no such proc yet, so all four cases fail. Refs #2205. --- test/cmocka/build/test-graph.json | 67 ++++ test/cmocka/test_rtmps_cert_no_fallback.c | 352 ++++++++++++++++++++++ 2 files changed, 419 insertions(+) create mode 100644 test/cmocka/test_rtmps_cert_no_fallback.c diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index c172f09641..0124f840ef 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -2614,6 +2614,66 @@ } ] }, + "test_rtmps_cert_no_fallback": { + "sources": [ + "test/cmocka/test_rtmps_cert_no_fallback.c", + { + "path": "plugins/obs-outputs/rtmp-hevc.c.h", + "requires": "ENABLE_HEVC" + }, + "plugins/obs-outputs/flv-mux.c.h", + "plugins/obs-outputs/net-if.c.h", + "plugins/obs-outputs/net-if-host-posix.c.h", + "plugins/obs-outputs/rtmp-stream-host-posix.c.h", + "plugins/obs-outputs/rtmp-av1.c.h", + "plugins/obs-outputs/librtmp/amf.c", + "plugins/obs-outputs/librtmp/cencode.c", + "plugins/obs-outputs/librtmp/hashswf.c", + "plugins/obs-outputs/librtmp/log.c", + "plugins/obs-outputs/librtmp/md5.c", + "plugins/obs-outputs/librtmp/parseurl.c", + "plugins/obs-outputs/librtmp/rtmp.c" + ], + "requires": { + "all": [ + "HAVE_OBS_OUTPUTS", + { + "not": "OS_WINDOWS" + } + ] + }, + "includes": [ + "plugins/obs-outputs", + "shared/opts-parser", + "shared/bpm" + ], + "libraries": [ + "OBS::libobs", + "OBS::happy-eyeballs", + "MbedTLS::mbedtls", + "ZLIB::ZLIB", + "jansson::jansson", + "OBS::native-test-cmocka", + { + "name": "OBS::native-test-foundation", + "requires": "OS_MACOS" + }, + { + "name": "OBS::native-test-security", + "requires": "OS_MACOS" + } + ], + "definitions": [ + "USE_MBEDTLS", + "CRYPTO" + ], + "compile_options": [ + { + "name": "-Wno-comma", + "requires": "C_COMPILER_CLANG" + } + ] + }, "test_scene_audio_action_steps": { "sources": [ "test/cmocka/test_scene_audio_action_steps.c", @@ -5100,6 +5160,13 @@ ], "timeout_seconds": 30 }, + "test_rtmps_cert_no_fallback": { + "target": "test_rtmps_cert_no_fallback", + "command": [ + "${target}" + ], + "timeout_seconds": 60 + }, "test_scene_audio_action_steps": { "target": "test_scene_audio_action_steps", "command": [ diff --git a/test/cmocka/test_rtmps_cert_no_fallback.c b/test/cmocka/test_rtmps_cert_no_fallback.c new file mode 100644 index 0000000000..80a0d0fbd1 --- /dev/null +++ b/test/cmocka/test_rtmps_cert_no_fallback.c @@ -0,0 +1,352 @@ +/* An RTMPS stream whose server certificate does not verify must not fall + * back to plain RTMP (obs-rust/obs-studio#2205, docs/fallback-warning.md + * rule 7). The frontend retries a failed RTMPS start over RTMP only for a + * connection failure: timeout, refusal, reset, or a handshake that fails for + * another reason. A certificate that does not verify, also one issued for + * another host name, stops the start; the RTMP output reports it through its + * "rtmp_tls_cert_unverified" proc. + * + * The test builds the real rtmp-stream.c and librtmp into this executable and + * connects to a TLS listener on the loopback interface. The listener presents + * a certificate the test makes at run time, issued for another host. With + * that certificate trusted, only the host name is wrong; without it, the + * certificate is also untrusted. A listener that closes the connection during + * the handshake, and a port with no listener, are connection failures that + * still fall back. The output is a small fake, so no display or encoder is + * needed. */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "librtmp/rtmp.h" + +/* Route the libobs calls rtmp-stream.c makes on its output to the fakes + * below, and RTMP_Connect through a wrapper that can add a trusted + * certificate. */ +#define obs_output_get_name fake_output_get_name +#define obs_output_set_last_error fake_output_set_last_error +#define obs_output_end_data_capture fake_output_end_data_capture +#define obs_output_get_proc_handler fake_output_get_proc_handler +static int test_rtmp_connect(RTMP *r, RTMPPacket *cp); +#define RTMP_Connect test_rtmp_connect + +#include "rtmp-stream.c" + +#undef RTMP_Connect + +const char *obs_module_text(const char *lookup) +{ + return lookup; +} + +/* ------------------------------------------------------------------------- */ +/* fakes */ + +static int fake_output; /* only its address is used */ +static proc_handler_t *output_procs; + +const char *fake_output_get_name(const obs_output_t *output) +{ + (void)output; + return "cert test"; +} + +void fake_output_set_last_error(obs_output_t *output, const char *message) +{ + (void)output; + (void)message; +} + +void fake_output_end_data_capture(obs_output_t *output) +{ + (void)output; +} + +proc_handler_t *fake_output_get_proc_handler(const obs_output_t *output) +{ + (void)output; + return output_procs; +} + +/* A certificate the client trusts in addition to the system's, or NULL. */ +static mbedtls_x509_crt *trusted_extra; + +static int test_rtmp_connect(RTMP *r, RTMPPacket *cp) +{ + if (trusted_extra && r->RTMP_TLS_ctx) + mbedtls_ssl_conf_ca_chain(&r->RTMP_TLS_ctx->conf, trusted_extra, NULL); + return RTMP_Connect(r, cp); +} + +/* ------------------------------------------------------------------------- */ +/* the server's certificate */ + +static mbedtls_entropy_context entropy; +static mbedtls_ctr_drbg_context drbg; +static mbedtls_pk_context server_key; +static mbedtls_x509_crt server_cert; + +/* A self-signed certificate for host, with its key. */ +static void make_cert(const char *host) +{ + char name[128]; + unsigned char pem[4096]; + unsigned char serial[] = {0x01}; + mbedtls_x509write_cert w; + + mbedtls_pk_init(&server_key); + assert_int_equal(mbedtls_pk_setup(&server_key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)), 0); + assert_int_equal(mbedtls_ecp_gen_key(MBEDTLS_ECP_DP_SECP256R1, mbedtls_pk_ec(server_key), + mbedtls_ctr_drbg_random, &drbg), + 0); + + snprintf(name, sizeof(name), "CN=%s", host); + mbedtls_x509write_crt_init(&w); + mbedtls_x509write_crt_set_version(&w, MBEDTLS_X509_CRT_VERSION_3); + mbedtls_x509write_crt_set_md_alg(&w, MBEDTLS_MD_SHA256); + mbedtls_x509write_crt_set_subject_key(&w, &server_key); + mbedtls_x509write_crt_set_issuer_key(&w, &server_key); + assert_int_equal(mbedtls_x509write_crt_set_subject_name(&w, name), 0); + assert_int_equal(mbedtls_x509write_crt_set_issuer_name(&w, name), 0); + assert_int_equal(mbedtls_x509write_crt_set_serial_raw(&w, serial, sizeof(serial)), 0); + assert_int_equal(mbedtls_x509write_crt_set_validity(&w, "20200101000000", "20991231235959"), 0); + assert_int_equal(mbedtls_x509write_crt_set_basic_constraints(&w, 1, -1), 0); + assert_int_equal(mbedtls_x509write_crt_pem(&w, pem, sizeof(pem), mbedtls_ctr_drbg_random, &drbg), 0); + mbedtls_x509write_crt_free(&w); + + mbedtls_x509_crt_init(&server_cert); + assert_int_equal(mbedtls_x509_crt_parse(&server_cert, pem, strlen((char *)pem) + 1), 0); +} + +/* ------------------------------------------------------------------------- */ +/* the listener */ + +enum listener { + LISTENER_TLS, /* completes TCP and presents server_cert */ + LISTENER_RESET, /* completes TCP and closes during the handshake */ +}; + +struct server { + enum listener kind; + int fd; + int port; + pthread_t thread; +}; + +static void *serve(void *param) +{ + struct server *s = param; + int conn = accept(s->fd, NULL, NULL); + if (conn < 0) + return NULL; + + if (s->kind == LISTENER_TLS) { + mbedtls_ssl_config conf; + mbedtls_ssl_context ssl; + mbedtls_net_context net = {.fd = conn}; + + mbedtls_ssl_config_init(&conf); + mbedtls_ssl_init(&ssl); + mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM, + MBEDTLS_SSL_PRESET_DEFAULT); + mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); + mbedtls_ssl_conf_rng(&conf, mbedtls_ctr_drbg_random, &drbg); + mbedtls_ssl_conf_own_cert(&conf, &server_cert, &server_key); + if (mbedtls_ssl_setup(&ssl, &conf) == 0) { + mbedtls_ssl_set_bio(&ssl, &net, mbedtls_net_send, mbedtls_net_recv, NULL); + /* Fails once the client rejects the certificate. */ + mbedtls_ssl_handshake(&ssl); + } + mbedtls_ssl_free(&ssl); + mbedtls_ssl_config_free(&conf); + } else { + char hello[16]; + (void)recv(conn, hello, sizeof(hello), 0); + } + close(conn); + return NULL; +} + +static void start_server(struct server *s, enum listener kind) +{ + struct sockaddr_in addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK)}; + socklen_t len = sizeof(addr); + + s->kind = kind; + s->fd = socket(AF_INET, SOCK_STREAM, 0); + assert_true(s->fd >= 0); + assert_int_equal(bind(s->fd, (struct sockaddr *)&addr, sizeof(addr)), 0); + assert_int_equal(listen(s->fd, 1), 0); + assert_int_equal(getsockname(s->fd, (struct sockaddr *)&addr, &len), 0); + s->port = ntohs(addr.sin_port); + assert_int_equal(pthread_create(&s->thread, NULL, serve, s), 0); +} + +static void stop_server(struct server *s) +{ + shutdown(s->fd, SHUT_RDWR); + pthread_join(s->thread, NULL); + close(s->fd); +} + +/* A port on the loopback interface with nothing listening. */ +static int closed_port(void) +{ + struct sockaddr_in addr = {.sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK)}; + socklen_t len = sizeof(addr); + int fd = socket(AF_INET, SOCK_STREAM, 0); + assert_true(fd >= 0); + assert_int_equal(bind(fd, (struct sockaddr *)&addr, sizeof(addr)), 0); + assert_int_equal(getsockname(fd, (struct sockaddr *)&addr, &len), 0); + close(fd); + return ntohs(addr.sin_port); +} + +/* ------------------------------------------------------------------------- */ +/* connecting */ + +struct result { + int code; + bool proc_found; + bool unverified; +}; + +/* Connects the RTMP output to rtmps://127.0.0.1:port/live and asks it, as + * the frontend does, whether the certificate failed to verify. */ +static struct result connect_to(int port) +{ + struct result r = {0}; + calldata_t cd = {0}; + + output_procs = proc_handler_create(); + struct rtmp_stream *stream = rtmp_stream_create(NULL, (obs_output_t *)&fake_output); + assert_non_null(stream); + dstr_printf(&stream->path, "rtmps://127.0.0.1:%d/live", port); + dstr_copy(&stream->key, "key"); + + r.code = try_connect(stream); + + r.proc_found = proc_handler_call(output_procs, "rtmp_tls_cert_unverified", &cd); + r.unverified = r.proc_found && calldata_bool(&cd, "unverified"); + calldata_free(&cd); + + rtmp_stream_destroy(stream); + proc_handler_destroy(output_procs); + output_procs = NULL; + return r; +} + +/* The certificate is trusted but issued for another host: the host name does + * not match, so the start stops and does not fall back. */ +static void cert_for_other_host_does_not_fall_back(void **state) +{ + (void)state; + struct server s; + trusted_extra = &server_cert; + start_server(&s, LISTENER_TLS); + + struct result r = connect_to(s.port); + + stop_server(&s); + trusted_extra = NULL; + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_true(r.unverified); +} + +/* The certificate is not trusted (and for another host): no fallback. */ +static void untrusted_cert_does_not_fall_back(void **state) +{ + (void)state; + struct server s; + start_server(&s, LISTENER_TLS); + + struct result r = connect_to(s.port); + + stop_server(&s); + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_true(r.unverified); +} + +/* The server closes the connection during the handshake: a connection + * failure, so the fallback stays open. */ +static void reset_handshake_still_falls_back(void **state) +{ + (void)state; + struct server s; + start_server(&s, LISTENER_RESET); + + struct result r = connect_to(s.port); + + stop_server(&s); + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_false(r.unverified); +} + +/* Nothing listens: a refused connection, so the fallback stays open. */ +static void refused_connection_still_falls_back(void **state) +{ + (void)state; + struct result r = connect_to(closed_port()); + + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_false(r.unverified); +} + +static int setup(void **state) +{ + (void)state; + const char *pers = "rtmps-cert-test"; + mbedtls_entropy_init(&entropy); + mbedtls_ctr_drbg_init(&drbg); + assert_int_equal(mbedtls_ctr_drbg_seed(&drbg, mbedtls_entropy_func, &entropy, (const unsigned char *)pers, + strlen(pers)), + 0); + make_cert("other.example"); + return 0; +} + +static int teardown(void **state) +{ + (void)state; + mbedtls_x509_crt_free(&server_cert); + mbedtls_pk_free(&server_key); + mbedtls_ctr_drbg_free(&drbg); + mbedtls_entropy_free(&entropy); + return 0; +} + +int main(void) +{ + signal(SIGPIPE, SIG_IGN); + + const struct CMUnitTest tests[] = { + cmocka_unit_test(cert_for_other_host_does_not_fall_back), + cmocka_unit_test(untrusted_cert_does_not_fall_back), + cmocka_unit_test(reset_handshake_still_falls_back), + cmocka_unit_test(refused_connection_still_falls_back), + }; + return cmocka_run_group_tests(tests, setup, teardown); +} From b24bc944ae8181c88abc284412ad75bdcc5e8fbb Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 25 Oct 2026 16:21:55 -0400 Subject: [PATCH 4/7] frontend: Use the shared fallback warning for RTMPS The RTMPS fallback now raises the shared fallback warning (obs_frontend_warn_fallback, #2218) instead of its own OBSRtmpsFallback dialog, which is removed with its unity group, precompiled header and strings. utility/RtmpsFallback.hpp raises it with Cancel, the standard wording (title "Streaming unencrypted (RTMP)", the message in the order what failed, what OBS does instead, what the user can do) and the plain URL, and passes the answer on: the countdown or "Accept anyway" restarts the stream over plain RTMP, Cancel stops the start. The warning logs the fallback at error level and to stderr. Two choices keep the first draft's behaviour until they are decided, as constants in RtmpsFallback.hpp. The shared warning asks once per id in a session, so the id names the server (IdPerServer). With the main window hidden, OBS still shows a tray notice and waits three seconds before it restarts (CountdownWhenHidden). The fallback now covers connection failures only. The RTMP output records whether its last connect failed because the server's certificate did not verify (MBEDTLS_ERR_X509_CERT_VERIFY_FAILED, also for a host-name mismatch) and reports it through the output proc rtmp_tls_cert_unverified. Then the frontend shows the connection error and logs the reason instead of offering plain RTMP. test_rtmps_fallback_warning replaces test_rtmps_fallback_dialog: the warning is raised with the right id, title, message and Cancel, its answer is passed on, only a connection failure of a service stream falls back, and the strings follow the standard wording. Refs #2205. --- docs/features.md | 9 +- .../frontend-dialogs-obsrtmpsfallback.pch.hpp | 20 -- frontend/build/frontend-widgets.pch.hpp | 2 +- ...frontend.dialogs-obsrtmpsfallback+.hpp.cpp | 5 - frontend/build/source-sets.json | 3 +- frontend/data/locale/en-US.ini | 7 +- frontend/dialogs/OBSRtmpsFallback.cpp.hpp | 57 ------ frontend/dialogs/OBSRtmpsFallback.hpp | 43 ---- frontend/utility/RtmpsFallback.hpp | 83 ++++++++ frontend/widgets/OBSBasic.hpp | 5 +- frontend/widgets/OBSBasic_Streaming.cpp.hpp | 69 ++++--- plugins/obs-outputs/rtmp-stream.c.h | 24 +++ plugins/obs-outputs/rtmp-stream.h | 4 + test/cmocka/test_rtmps_fallback_dialog.cpp | 189 ------------------ test/cmocka/test_rtmps_fallback_warning.cpp | 171 ++++++++++++++++ .../tests.d/test_rtmps_fallback_dialog.cmake | 24 --- .../tests.d/test_rtmps_fallback_warning.cmake | 26 +++ 17 files changed, 366 insertions(+), 375 deletions(-) delete mode 100644 frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp delete mode 100644 frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp delete mode 100644 frontend/dialogs/OBSRtmpsFallback.cpp.hpp delete mode 100644 frontend/dialogs/OBSRtmpsFallback.hpp create mode 100644 frontend/utility/RtmpsFallback.hpp delete mode 100644 test/cmocka/test_rtmps_fallback_dialog.cpp create mode 100644 test/cmocka/test_rtmps_fallback_warning.cpp delete mode 100644 test/cmocka/tests.d/test_rtmps_fallback_dialog.cmake create mode 100644 test/cmocka/tests.d/test_rtmps_fallback_warning.cmake diff --git a/docs/features.md b/docs/features.md index c5353c0616..26e5a198c8 100644 --- a/docs/features.md +++ b/docs/features.md @@ -246,10 +246,11 @@ not get a line. niconico, BoxCast, Bitmovin, Eventials and EventLive.pro stream over RTMPS (port 443) instead of plain RTMP, on every server checked to accept it; existing profiles switch too, with no setting to change. If the encrypted - connection fails, a "Proceed anyway" dialog counts down three seconds and - then streams to the same server over plain RTMP, unless you cancel; the - fallback is written to the log and to stderr. Custom servers are - unchanged. *All*, pending merge. Issue + connection cannot be made, the shared fallback warning ("Streaming + unencrypted (RTMP)") counts down three seconds and then streams to the same + server over plain RTMP, unless you cancel; the fallback is written to the + log and to stderr. A server certificate that does not verify stops the + start instead. Custom servers are unchanged. *All*, pending merge. Issue [#2205](https://github.com/obs-rust/obs-studio/issues/2205). ### Sources and filters diff --git a/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp b/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp deleted file mode 100644 index 58df2d9be6..0000000000 --- a/frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp +++ /dev/null @@ -1,20 +0,0 @@ -/* Precompiled header (BUILD.md) for the frontend unity groups - * frontend.dialogs-obsrtmpsfallback. C++. Every implementation file in them includes this header and - * nothing else: the includes and macro setup below are theirs, in group and - * file order. */ - -#pragma once - -/* dialogs/OBSRtmpsFallback.cpp */ -#include "frontend/dialogs/OBSRtmpsFallback.hpp" - -#include -#include -#include -#include -#include - -#include - -#include -#include diff --git a/frontend/build/frontend-widgets.pch.hpp b/frontend/build/frontend-widgets.pch.hpp index d71503f677..7b98bb0ebf 100644 --- a/frontend/build/frontend-widgets.pch.hpp +++ b/frontend/build/frontend-widgets.pch.hpp @@ -57,6 +57,7 @@ #include #include #include +#include #include #include #if defined(_WIN32) || defined(WHATSNEW_ENABLED) @@ -151,7 +152,6 @@ #endif #include #include -#include #include #if defined(_WIN32) || defined(WHATSNEW_ENABLED) #include diff --git a/frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp b/frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp deleted file mode 100644 index 6d75748832..0000000000 --- a/frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp +++ /dev/null @@ -1,5 +0,0 @@ -/* Unity group (BUILD.md): OBSRtmpsFallback.cpp on its own (a C test builds it alone). */ - -#include "frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp" - -#include "frontend/dialogs/OBSRtmpsFallback.cpp.hpp" diff --git a/frontend/build/source-sets.json b/frontend/build/source-sets.json index b03030152a..1a6d224aad 100644 --- a/frontend/build/source-sets.json +++ b/frontend/build/source-sets.json @@ -238,8 +238,6 @@ "dialogs/OBSFallbackWarning.hpp", "dialogs/OBSLogViewer.hpp", "dialogs/OBSMissingFiles.hpp", - "build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp", - "dialogs/OBSRtmpsFallback.hpp", "build/frontend.dialogs-2+.hpp.cpp", "dialogs/OBSRemux.hpp", "dialogs/OBSWhatsNew.hpp" @@ -405,6 +403,7 @@ "utility/RemuxQueueModel.hpp", "utility/RemuxWorker.hpp", "utility/ResizeSignaler.hpp", + "utility/RtmpsFallback.hpp", "build/frontend.utility-3+.hpp.cpp", "utility/SceneRenameDelegate.hpp", "build/frontend.utility-screenshotobj+.hpp.cpp", diff --git a/frontend/data/locale/en-US.ini b/frontend/data/locale/en-US.ini index 989c6b8444..e551ac8d6c 100644 --- a/frontend/data/locale/en-US.ini +++ b/frontend/data/locale/en-US.ini @@ -446,10 +446,9 @@ Output.ConnectFail.InvalidStream="Could not access the specified channel or stre Output.ConnectFail.HdrDisabled="HDR output is currently disabled for this output." Output.ConnectFail.Error="An unexpected error occurred when trying to connect to the server. More information in the log file." Output.ConnectFail.Disconnected="Disconnected from server." -Output.RtmpsFallback.Title="Encrypted connection failed" -Output.RtmpsFallback.Text="OBS could not connect to the server over an encrypted connection (RTMPS).\n\nStream to %1 without encryption instead?" -Output.RtmpsFallback.Proceed="Proceed anyway" -Output.RtmpsFallback.Tray="Encrypted connection failed; streaming without encryption." +Output.RtmpsFallback.Title="Streaming unencrypted (RTMP)" +Output.RtmpsFallback.Message="OBS could not make an encrypted connection (RTMPS) to the server.\n\nOBS streams to %1 unencrypted instead. To stop the stream from starting, select Cancel; to stream encrypted, check that your network or firewall allows connections on port 443." +Output.RtmpsFallback.Tray="Streaming unencrypted (RTMP): the encrypted connection failed." # output streaming-related messages Output.StreamEncodeError.Title="Encoding error" diff --git a/frontend/dialogs/OBSRtmpsFallback.cpp.hpp b/frontend/dialogs/OBSRtmpsFallback.cpp.hpp deleted file mode 100644 index b933ad808a..0000000000 --- a/frontend/dialogs/OBSRtmpsFallback.cpp.hpp +++ /dev/null @@ -1,57 +0,0 @@ -#include "frontend/build/frontend-dialogs-obsrtmpsfallback.pch.hpp" - -OBSRtmpsFallback::OBSRtmpsFallback(QWidget *parent, const Texts &texts) : QDialog(parent) -{ - setWindowTitle(texts.title); - - QVBoxLayout *layout = new QVBoxLayout(this); - QLabel *label = new QLabel(texts.message, this); - label->setWordWrap(true); - layout->addWidget(label); - - progress = new QProgressBar(this); - progress->setRange(0, CountdownMs); - progress->setTextVisible(false); - layout->addWidget(progress); - - QDialogButtonBox *buttons = new QDialogButtonBox(this); - proceedButton = buttons->addButton(texts.proceed, QDialogButtonBox::AcceptRole); - buttons->addButton(texts.cancel, QDialogButtonBox::RejectRole); - connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept); - connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject); - layout->addWidget(buttons); - - tick.setInterval(50); - connect(&tick, &QTimer::timeout, this, &OBSRtmpsFallback::Tick); -} - -void OBSRtmpsFallback::showEvent(QShowEvent *event) -{ - QDialog::showEvent(event); - elapsed.start(); - progress->setValue(0); - tick.start(); -} - -void OBSRtmpsFallback::Tick() -{ - /* Cancel or a click closed the dialog: the countdown ends with it. */ - if (!isVisible()) { - tick.stop(); - return; - } - - qint64 ms = elapsed.elapsed(); - progress->setValue((int)std::min(ms, CountdownMs)); - if (ms >= CountdownMs) { - tick.stop(); - accept(); - } -} - -void OBSRtmpsFallback::LogFallback(const char *plain_url) -{ - fprintf(stderr, "RTMPS connection failed; streaming without encryption to %s\n", plain_url); - fflush(stderr); - blog(LOG_WARNING, "RTMPS connection failed; streaming without encryption to %s", plain_url); -} diff --git a/frontend/dialogs/OBSRtmpsFallback.hpp b/frontend/dialogs/OBSRtmpsFallback.hpp deleted file mode 100644 index 77cad2a072..0000000000 --- a/frontend/dialogs/OBSRtmpsFallback.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -class QProgressBar; -class QPushButton; - -/* Shown when a stream that rtmp-services upgraded to RTMPS fails to connect. - * "Proceed anyway" connects to the same server over plain RTMP; it fills a - * progress bar for CountdownMs and then accepts on its own, so a stream started - * with nobody at the screen still goes live. Cancel stops the start. */ -class OBSRtmpsFallback : public QDialog { - Q_OBJECT - -public: - static constexpr int CountdownMs = 3000; - - struct Texts { - QString title; - QString message; - QString proceed; - QString cancel; - }; - - OBSRtmpsFallback(QWidget *parent, const Texts &texts); - - /* Writes the fallback to stderr and the OBS log. */ - static void LogFallback(const char *plain_url); - -private: - QProgressBar *progress = nullptr; - QPushButton *proceedButton = nullptr; - QTimer tick; - QElapsedTimer elapsed; - - void Tick(); - -protected: - void showEvent(QShowEvent *event) override; -}; diff --git a/frontend/utility/RtmpsFallback.hpp b/frontend/utility/RtmpsFallback.hpp new file mode 100644 index 0000000000..50b8960b04 --- /dev/null +++ b/frontend/utility/RtmpsFallback.hpp @@ -0,0 +1,83 @@ +#pragma once + +#include +#include + +#include +#include + +#include +#include +#include + +/* The RTMPS fallback (obs-rust/obs-studio#2205): a stream that rtmp-services + * upgraded to RTMPS fails to connect, and OBS offers to stream to the same + * server over plain RTMP. The warning is the shared fallback warning + * (obs_frontend_warn_fallback, docs/fallback-warning.md): logged at error + * level and on stderr, a dialog with a three-second countdown that accepts, + * and Cancel, which stops the start. */ +namespace RtmpsFallback { + +/* Two choices are open; both keep the behaviour of the first draft of #2219 + * until they are decided. + * + * (a) How often to ask. The shared warning shows its dialog once per id in an + * OBS session. true: the id names the server, so each server asks once + * per session (closest to asking on every failed start without an API + * change; a second failure to the same server only logs and continues + * over RTMP). false: one id for every server, the helper's default. */ +inline constexpr bool IdPerServer = true; + +/* (b) The main window hidden (OBS in the tray). The shared warning then logs + * and accepts at once. true: OBS also shows a tray notice and waits + * HiddenCountdownMs before it restarts over RTMP. false: it restarts at + * once. */ +inline constexpr bool CountdownWhenHidden = true; +inline constexpr int HiddenCountdownMs = 3000; + +inline constexpr const char *BaseId = "rtmp.unencrypted"; + +/* The fallback warning's id for a stream to plainUrl. */ +inline std::string Id(const std::string &plainUrl) +{ + if (!IdPerServer || plainUrl.empty()) { + return BaseId; + } + return std::string(BaseId) + ":" + plainUrl; +} + +/* Whether a stream that stopped with code may retry over plain RTMP: only a + * connection failure of a service stream (multitrack video brings its own + * URLs), and never when the server's certificate did not verify. That is no + * network trouble, and plain RTMP would send the stream key unencrypted to a + * server that could not prove who it is. */ +inline bool Allowed(int code, bool multitrack, bool certUnverified) +{ + return code == OBS_OUTPUT_CONNECT_FAILED && !multitrack && !certUnverified; +} + +using Answer = std::function; + +/* Raises the shared warning for a fallback to plainUrl. message is translated + * and has %1 for the URL. answer is called once, on the UI thread: true for + * the countdown or "Accept anyway" (restart over plain RTMP), false for + * Cancel or a closed dialog. */ +inline void Warn(const QString &title, const QString &message, const std::string &plainUrl, Answer answer) +{ + std::string id = Id(plainUrl); + QByteArray t = title.toUtf8(); + QByteArray m = message.arg(QString::fromStdString(plainUrl)).toUtf8(); + auto *held = new Answer(std::move(answer)); + obs_frontend_warn_fallback( + id.c_str(), t.constData(), m.constData(), true, + [](void *param, bool accepted) { + Answer *a = static_cast(param); + if (*a) { + (*a)(accepted); + } + delete a; + }, + held); +} + +} // namespace RtmpsFallback diff --git a/frontend/widgets/OBSBasic.hpp b/frontend/widgets/OBSBasic.hpp index 0264727482..5bf8c46e32 100644 --- a/frontend/widgets/OBSBasic.hpp +++ b/frontend/widgets/OBSBasic.hpp @@ -1400,12 +1400,13 @@ private slots: bool streamingStopping = false; bool streamingStarting = false; - /* RTMPS fallback (OBSRtmpsFallback): whether the next start keeps - * plain RTMP after "Proceed anyway", and whether the stream being + /* RTMPS fallback (utility/RtmpsFallback.hpp): whether the next start + * keeps plain RTMP after "Accept anyway", and whether the stream being * started takes its URLs from multitrack video instead of the service. */ bool rtmpsFallbackRestart = false; bool streamUsesMultitrack = false; std::string RtmpsFallbackUrl(); + bool StreamTlsCertUnverified(); void SetPlainRtmp(bool enabled); void OfferRtmpsFallback(const std::string &plainUrl); diff --git a/frontend/widgets/OBSBasic_Streaming.cpp.hpp b/frontend/widgets/OBSBasic_Streaming.cpp.hpp index 30217a03d4..05f9189508 100644 --- a/frontend/widgets/OBSBasic_Streaming.cpp.hpp +++ b/frontend/widgets/OBSBasic_Streaming.cpp.hpp @@ -346,7 +346,13 @@ void OBSBasic::StreamingStop(int code, QString last_error) * over plain RTMP; multitrack video brings its own URLs. */ std::string rtmpsPlainUrl; if (code == OBS_OUTPUT_CONNECT_FAILED && !streamUsesMultitrack) { - rtmpsPlainUrl = RtmpsFallbackUrl(); + bool certUnverified = StreamTlsCertUnverified(); + if (RtmpsFallback::Allowed(code, streamUsesMultitrack, certUnverified)) { + rtmpsPlainUrl = RtmpsFallbackUrl(); + } else if (certUnverified && !RtmpsFallbackUrl().empty()) { + blog(LOG_ERROR, "RTMPS: the server's certificate did not verify; " + "the stream is not started and does not fall back to RTMP"); + } } if (use_last_error && !last_error.isEmpty()) { @@ -425,35 +431,50 @@ void OBSBasic::SetPlainRtmp(bool enabled) calldata_free(&cd); } -/* The RTMPS connection failed: "Proceed anyway" restarts the stream over - * plain RTMP after a three-second countdown unless the user cancels. With the - * window hidden there is nobody to ask, so the countdown runs on its own. */ +/* Whether the stream output's last connect failed because the server's TLS + * certificate did not verify (obs-outputs' rtmp output reports it). */ +bool OBSBasic::StreamTlsCertUnverified() +{ + obs_output_t *output = outputHandler ? outputHandler->streamOutput.Get() : nullptr; + if (!output) { + return false; + } + + calldata_t cd = {0}; + bool unverified = false; + if (proc_handler_call(obs_output_get_proc_handler(output), "rtmp_tls_cert_unverified", &cd)) { + unverified = calldata_bool(&cd, "unverified"); + } + calldata_free(&cd); + return unverified; +} + +/* The RTMPS connection failed: the shared fallback warning counts down three + * seconds and then OBS restarts the stream over plain RTMP, unless the user + * cancels. The warning logs the fallback. */ void OBSBasic::OfferRtmpsFallback(const std::string &plainUrl) { - auto proceed = [this, plainUrl]() { - OBSRtmpsFallback::LogFallback(plainUrl.c_str()); + auto restart = [this]() { + if (isClosing() || outputHandler->StreamingActive()) { + return; + } rtmpsFallbackRestart = true; QMetaObject::invokeMethod(this, &OBSBasic::StartStreaming, Qt::QueuedConnection); }; - if (!isVisible()) { - SysTrayNotify(QTStr("Output.RtmpsFallback.Tray"), QSystemTrayIcon::Warning); - QTimer::singleShot(OBSRtmpsFallback::CountdownMs, this, proceed); - return; - } - - OBSRtmpsFallback::Texts texts = { - QTStr("Output.RtmpsFallback.Title"), - QTStr("Output.RtmpsFallback.Text").arg(QString::fromStdString(plainUrl)), - QTStr("Output.RtmpsFallback.Proceed"), - QTStr("Cancel"), - }; - OBSRtmpsFallback dialog(this, texts); - if (dialog.exec() == QDialog::Accepted) { - proceed(); - } else { - blog(LOG_INFO, "RTMPS fallback cancelled; the stream was not started"); - } + RtmpsFallback::Warn(QTStr("Output.RtmpsFallback.Title"), QTStr("Output.RtmpsFallback.Message"), plainUrl, + [this, restart](bool accepted) { + if (!accepted) { + blog(LOG_INFO, "RTMPS fallback cancelled; the stream was not started"); + return; + } + if (RtmpsFallback::CountdownWhenHidden && !isVisible()) { + SysTrayNotify(QTStr("Output.RtmpsFallback.Tray"), QSystemTrayIcon::Warning); + QTimer::singleShot(RtmpsFallback::HiddenCountdownMs, this, restart); + return; + } + restart(); + }); } void OBSBasic::StreamActionTriggered() diff --git a/plugins/obs-outputs/rtmp-stream.c.h b/plugins/obs-outputs/rtmp-stream.c.h index e85383f1e1..dd94d2c40c 100644 --- a/plugins/obs-outputs/rtmp-stream.c.h +++ b/plugins/obs-outputs/rtmp-stream.c.h @@ -140,6 +140,23 @@ static void rtmp_stream_destroy(void *data) bfree(stream); } +/* The TLS handshake failed because the server's certificate did not verify: + * not trusted, expired, or issued for another host name (mbedTLS reports a + * host-name mismatch the same way). Unlike a timeout, refusal or reset, this + * is no network trouble, so the frontend does not retry over plain RTMP + * (obs-rust/obs-studio#2205). */ +static bool rtmp_tls_cert_unverified(int last_error_code) +{ + return last_error_code == -0x2700; /* MBEDTLS_ERR_X509_CERT_VERIFY_FAILED */ +} + +/* Output proc: whether the last connect failed on certificate verification. */ +static void rtmp_stream_tls_cert_unverified_proc(void *data, calldata_t *cd) +{ + struct rtmp_stream *stream = data; + calldata_set_bool(cd, "unverified", stream->tls_cert_unverified); +} + static void *rtmp_stream_create(obs_data_t *settings, obs_output_t *output) { struct rtmp_stream *stream = bzalloc(sizeof(struct rtmp_stream)); @@ -181,6 +198,9 @@ static void *rtmp_stream_create(obs_data_t *settings, obs_output_t *output) goto fail; } + proc_handler_add(obs_output_get_proc_handler(output), "void rtmp_tls_cert_unverified(out bool unverified)", + rtmp_stream_tls_cert_unverified_proc, stream); + UNUSED_PARAMETER(settings); return stream; @@ -992,7 +1012,11 @@ static int try_connect(struct rtmp_stream *stream) rtmp_host_log_interface_type(stream); + stream->tls_cert_unverified = false; if (!RTMP_Connect(&stream->rtmp, NULL)) { + stream->tls_cert_unverified = rtmp_tls_cert_unverified(stream->rtmp.last_error_code); + if (stream->tls_cert_unverified) + warn("The server's TLS certificate did not verify; the stream is not started"); set_output_error(stream); return OBS_OUTPUT_CONNECT_FAILED; } diff --git a/plugins/obs-outputs/rtmp-stream.h b/plugins/obs-outputs/rtmp-stream.h index cd82ed39e0..626170adda 100644 --- a/plugins/obs-outputs/rtmp-stream.h +++ b/plugins/obs-outputs/rtmp-stream.h @@ -125,6 +125,10 @@ struct rtmp_stream { RTMP rtmp; bool new_socket_loop; + + /* The last connect failed because the server's TLS certificate did + * not verify (rtmp_tls_cert_unverified). */ + bool tls_cert_unverified; bool low_latency_mode; bool disable_send_window_optimization; bool socket_thread_active; diff --git a/test/cmocka/test_rtmps_fallback_dialog.cpp b/test/cmocka/test_rtmps_fallback_dialog.cpp deleted file mode 100644 index 0dc00d3373..0000000000 --- a/test/cmocka/test_rtmps_fallback_dialog.cpp +++ /dev/null @@ -1,189 +0,0 @@ -/* The dialog the frontend shows when a stream that rtmp-services upgraded to - * RTMPS fails to connect (obs-rust/obs-studio#2205). "Proceed anyway" fills a - * progress bar for three seconds and then accepts on its own, so a stream - * started with nobody at the screen still goes live; Cancel stops the start. - * Each fallback is written to stderr and the OBS log. Runs the real dialog on - * Qt's offscreen platform. */ - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -extern "C" { -#include -} - -#include - -#include "OBSRtmpsFallback.hpp" - -static const OBSRtmpsFallback::Texts texts = {"Title", "Message", "Proceed anyway", "Cancel"}; - -/* Runs the event loop until the dialog has a result or limit_ms passes, and - * returns how long it took. */ -static qint64 wait_for_result(OBSRtmpsFallback &dlg, int limit_ms) -{ - QElapsedTimer t; - t.start(); - while (dlg.isVisible() && t.elapsed() < limit_ms) { - QApplication::processEvents(QEventLoop::AllEvents, 20); - QThread::msleep(5); - } - return t.elapsed(); -} - -static QPushButton *button(OBSRtmpsFallback &dlg, QDialogButtonBox::ButtonRole role) -{ - QDialogButtonBox *box = dlg.findChild(); - if (!box) { - return nullptr; - } - for (QAbstractButton *b : box->buttons()) { - if (box->buttonRole(b) == role) { - return qobject_cast(b); - } - } - return nullptr; -} - -/* With no click, the dialog accepts on its own once the countdown ends. */ -static void countdown_proceeds_after_three_seconds(void **state) -{ - (void)state; - OBSRtmpsFallback dlg(nullptr, texts); - dlg.show(); - - qint64 took = wait_for_result(dlg, 6000); - - assert_false(dlg.isVisible()); - assert_int_equal(dlg.result(), QDialog::Accepted); - assert_true(took >= OBSRtmpsFallback::CountdownMs - 200); - assert_true(took <= OBSRtmpsFallback::CountdownMs + 1500); -} - -/* The progress bar shows how much of the countdown has passed. */ -static void progress_bar_follows_countdown(void **state) -{ - (void)state; - OBSRtmpsFallback dlg(nullptr, texts); - dlg.show(); - - wait_for_result(dlg, OBSRtmpsFallback::CountdownMs / 2); - - QProgressBar *bar = dlg.findChild(); - assert_non_null(bar); - int pct = bar->value() * 100 / bar->maximum(); - assert_true(pct >= 30); - assert_true(pct <= 75); - dlg.reject(); -} - -/* Clicking "Proceed anyway" accepts right away. */ -static void proceed_click_accepts_now(void **state) -{ - (void)state; - OBSRtmpsFallback dlg(nullptr, texts); - dlg.show(); - - QPushButton *proceed = button(dlg, QDialogButtonBox::AcceptRole); - assert_non_null(proceed); - assert_string_equal(proceed->text().toUtf8().constData(), "Proceed anyway"); - proceed->click(); - - assert_true(wait_for_result(dlg, 500) < 500); - assert_int_equal(dlg.result(), QDialog::Accepted); -} - -/* Cancel rejects, and the countdown does not accept afterwards. */ -static void cancel_stops_the_countdown(void **state) -{ - (void)state; - OBSRtmpsFallback dlg(nullptr, texts); - dlg.show(); - - QPushButton *cancel = button(dlg, QDialogButtonBox::RejectRole); - assert_non_null(cancel); - cancel->click(); - wait_for_result(dlg, 500); - assert_int_equal(dlg.result(), QDialog::Rejected); - - QElapsedTimer t; - t.start(); - while (t.elapsed() < OBSRtmpsFallback::CountdownMs + 500) { - QApplication::processEvents(QEventLoop::AllEvents, 20); - QThread::msleep(5); - } - assert_int_equal(dlg.result(), QDialog::Rejected); - assert_false(dlg.isVisible()); -} - -static char logged[1024]; - -static void capture_log(int level, const char *format, va_list args, void *param) -{ - (void)level; - (void)param; - char line[512]; - vsnprintf(line, sizeof(line), format, args); - strncat(logged, line, sizeof(logged) - strlen(logged) - 1); - strncat(logged, "\n", sizeof(logged) - strlen(logged) - 1); -} - -/* A fallback is written to stderr and to the OBS log, naming the server. */ -static void fallback_is_logged_to_stderr_and_log(void **state) -{ - (void)state; - char path[] = "test_rtmps_fallback_stderr_XXXXXX"; - int fd = mkstemp(path); - assert_true(fd >= 0); - - fflush(stderr); - int saved = dup(fileno(stderr)); - dup2(fd, fileno(stderr)); - - logged[0] = '\0'; - base_set_log_handler(capture_log, nullptr); - OBSRtmpsFallback::LogFallback("rtmp://ingest.example/app"); - base_set_log_handler(nullptr, nullptr); - - fflush(stderr); - dup2(saved, fileno(stderr)); - close(saved); - - char err[1024] = {0}; - lseek(fd, 0, SEEK_SET); - ssize_t n = read(fd, err, sizeof(err) - 1); - close(fd); - unlink(path); - - assert_true(n > 0); - assert_non_null(strstr(err, "RTMPS")); - assert_non_null(strstr(err, "rtmp://ingest.example/app")); - assert_non_null(strstr(logged, "RTMPS")); - assert_non_null(strstr(logged, "rtmp://ingest.example/app")); -} - -int main(int argc, char **argv) -{ - qputenv("QT_QPA_PLATFORM", "offscreen"); - QApplication app(argc, argv); - - const struct CMUnitTest tests[] = { - cmocka_unit_test(countdown_proceeds_after_three_seconds), - cmocka_unit_test(progress_bar_follows_countdown), - cmocka_unit_test(proceed_click_accepts_now), - cmocka_unit_test(cancel_stops_the_countdown), - cmocka_unit_test(fallback_is_logged_to_stderr_and_log), - }; - return cmocka_run_group_tests(tests, nullptr, nullptr); -} diff --git a/test/cmocka/test_rtmps_fallback_warning.cpp b/test/cmocka/test_rtmps_fallback_warning.cpp new file mode 100644 index 0000000000..c3d195a4b9 --- /dev/null +++ b/test/cmocka/test_rtmps_fallback_warning.cpp @@ -0,0 +1,171 @@ +/* The RTMPS fallback (obs-rust/obs-studio#2205) through the shared fallback + * warning (obs_frontend_warn_fallback, docs/fallback-warning.md). When a + * stream that rtmp-services upgraded to RTMPS fails to connect, the frontend + * raises the warning with Cancel, its standard wording and the plain RTMP URL, + * and restarts over RTMP only when the answer is to continue. A certificate + * that did not verify never falls back. obs_frontend_warn_fallback is a fake + * here that records its arguments. */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +extern "C" { +#include +} + +#include "RtmpsFallback.hpp" + +static struct { + int calls; + std::string id; + std::string title; + std::string message; + bool can_cancel; + obs_frontend_fallback_cb cb; + void *param; +} raised; + +extern "C" void obs_frontend_warn_fallback(const char *id, const char *title, const char *message, bool can_cancel, + obs_frontend_fallback_cb cb, void *param) +{ + raised.calls++; + raised.id = id; + raised.title = title; + raised.message = message; + raised.can_cancel = can_cancel; + raised.cb = cb; + raised.param = param; +} + +static int reset(void **state) +{ + (void)state; + raised = {}; + return 0; +} + +static const char *url = "rtmp://ingest.example/app"; + +/* The warning has Cancel, the fallback's id, the title, and the message with + * the plain URL in it. Nothing is answered before the warning answers. */ +static void raises_shared_warning_with_cancel(void **state) +{ + (void)state; + int answers = 0; + RtmpsFallback::Warn("Streaming unencrypted (RTMP)", "OBS streams to %1 unencrypted.", url, + [&](bool) { answers++; }); + + assert_int_equal(raised.calls, 1); + assert_true(raised.can_cancel); + assert_string_equal(raised.id.c_str(), "rtmp.unencrypted:rtmp://ingest.example/app"); + assert_string_equal(raised.title.c_str(), "Streaming unencrypted (RTMP)"); + assert_string_equal(raised.message.c_str(), "OBS streams to rtmp://ingest.example/app unencrypted."); + assert_non_null(raised.cb); + assert_int_equal(answers, 0); + + raised.cb(raised.param, true); + assert_int_equal(answers, 1); +} + +/* "Accept anyway" or the countdown: the answer is to continue over RTMP. */ +static void accept_passes_true(void **state) +{ + (void)state; + int answer = -1; + RtmpsFallback::Warn("T", "M %1", url, [&](bool accepted) { answer = accepted; }); + raised.cb(raised.param, true); + assert_int_equal(answer, 1); +} + +/* Cancel or a closed dialog: the start stops. */ +static void cancel_passes_false(void **state) +{ + (void)state; + int answer = -1; + RtmpsFallback::Warn("T", "M %1", url, [&](bool accepted) { answer = accepted; }); + raised.cb(raised.param, false); + assert_int_equal(answer, 0); +} + +/* Open question (a): with IdPerServer, each server has its own id, so each + * server's first failure in a session shows the dialog. */ +static void id_names_the_server(void **state) +{ + (void)state; + if (RtmpsFallback::IdPerServer) { + assert_string_not_equal(RtmpsFallback::Id("rtmp://a.example/app").c_str(), + RtmpsFallback::Id("rtmp://b.example/app").c_str()); + } else { + assert_string_equal(RtmpsFallback::Id("rtmp://a.example/app").c_str(), "rtmp.unencrypted"); + } + assert_string_equal(RtmpsFallback::Id("").c_str(), "rtmp.unencrypted"); + assert_int_equal(strncmp(RtmpsFallback::Id(url).c_str(), "rtmp.unencrypted", 16), 0); +} + +/* Only a connection failure of a service stream falls back; a certificate + * that did not verify, multitrack video and other stop codes do not. */ +static void falls_back_only_on_connection_failure(void **state) +{ + (void)state; + assert_true(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, false)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, true)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, true, false)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_BAD_PATH, false, false)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_INVALID_STREAM, false, false)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_DISCONNECTED, false, false)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_SUCCESS, false, false)); +} + +static std::string locale_value(const std::string &text, const char *key) +{ + std::istringstream in(text); + std::string line, prefix = std::string(key) + "=\""; + while (std::getline(in, line)) { + if (line.compare(0, prefix.size(), prefix) == 0 && line.size() > prefix.size()) { + return line.substr(prefix.size(), line.size() - prefix.size() - 1); + } + } + return ""; +} + +/* The strings follow docs/fallback-warning.md: the title "Streaming + * unencrypted (RTMP)", the term "unencrypted", and a message that names the + * URL. The buttons come from the shared warning, so the fallback has none. */ +static void wording_follows_the_standard(void **state) +{ + (void)state; + std::ifstream f(FRONTEND_LOCALE); + assert_true(f.good()); + std::stringstream ss; + ss << f.rdbuf(); + std::string text = ss.str(); + + assert_string_equal(locale_value(text, "Output.RtmpsFallback.Title").c_str(), "Streaming unencrypted (RTMP)"); + std::string message = locale_value(text, "Output.RtmpsFallback.Message"); + assert_non_null(strstr(message.c_str(), "unencrypted")); + assert_non_null(strstr(message.c_str(), "%1")); + assert_null(strstr(message.c_str(), "insecure")); + assert_null(strstr(message.c_str(), "unsafe")); + assert_string_equal(locale_value(text, "Output.RtmpsFallback.Proceed").c_str(), ""); + assert_string_equal(locale_value(text, "FallbackWarning.AcceptAnyway").c_str(), "Accept anyway"); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup(raises_shared_warning_with_cancel, reset), + cmocka_unit_test_setup(accept_passes_true, reset), + cmocka_unit_test_setup(cancel_passes_false, reset), + cmocka_unit_test_setup(id_names_the_server, reset), + cmocka_unit_test_setup(falls_back_only_on_connection_failure, reset), + cmocka_unit_test_setup(wording_follows_the_standard, reset), + }; + return cmocka_run_group_tests(tests, nullptr, nullptr); +} diff --git a/test/cmocka/tests.d/test_rtmps_fallback_dialog.cmake b/test/cmocka/tests.d/test_rtmps_fallback_dialog.cmake deleted file mode 100644 index 54fb270b78..0000000000 --- a/test/cmocka/tests.d/test_rtmps_fallback_dialog.cmake +++ /dev/null @@ -1,24 +0,0 @@ -# The "Proceed anyway" dialog the frontend shows when an RTMPS stream fails to -# connect (obs-rust/obs-studio#2205): a three-second countdown that accepts on -# its own, Cancel, and the stderr/log line. Builds the real dialog on Qt's -# offscreen platform, so it needs Qt Widgets but no display and no frontend -# build. -find_package(Qt6 QUIET COMPONENTS Widgets) -if(TARGET Qt6::Widgets AND NOT OS_WINDOWS) - add_executable( - test_rtmps_fallback_dialog - test_rtmps_fallback_dialog.cpp - "${CMAKE_SOURCE_DIR}/frontend/build/frontend.dialogs-obsrtmpsfallback+.hpp.cpp" - "${CMAKE_SOURCE_DIR}/frontend/dialogs/OBSRtmpsFallback.hpp" - ) - set_target_properties(test_rtmps_fallback_dialog PROPERTIES AUTOMOC ON) - target_include_directories( - test_rtmps_fallback_dialog - PRIVATE ${CMOCKA_INCLUDE_DIR} "${CMAKE_SOURCE_DIR}/frontend/dialogs" - ) - target_link_libraries(test_rtmps_fallback_dialog PRIVATE OBS::libobs Qt6::Widgets ${CMOCKA_LIBRARIES}) - add_dependencies(cmocka-tests test_rtmps_fallback_dialog) - add_test(test_rtmps_fallback_dialog ${CMAKE_CURRENT_BINARY_DIR}/test_rtmps_fallback_dialog) - set_tests_properties(test_rtmps_fallback_dialog PROPERTIES TIMEOUT 60) - cmocka_qt_runtime(test_rtmps_fallback_dialog Qt6::Core) -endif() diff --git a/test/cmocka/tests.d/test_rtmps_fallback_warning.cmake b/test/cmocka/tests.d/test_rtmps_fallback_warning.cmake new file mode 100644 index 0000000000..e03f4d32df --- /dev/null +++ b/test/cmocka/tests.d/test_rtmps_fallback_warning.cmake @@ -0,0 +1,26 @@ +# The RTMPS fallback (obs-rust/obs-studio#2205) raises the shared fallback +# warning (obs_frontend_warn_fallback, #2218) with the right id, wording and +# Cancel, passes its answer on, and never falls back when the server's +# certificate did not verify. The warning itself is a fake here; its dialog is +# tested in test_fallback_warning_dialog. Needs Qt Core only. +find_package(Qt6 QUIET COMPONENTS Core) +if(TARGET Qt6::Core AND NOT OS_WINDOWS) + add_executable( + test_rtmps_fallback_warning + test_rtmps_fallback_warning.cpp + "${CMAKE_SOURCE_DIR}/frontend/utility/RtmpsFallback.hpp" + ) + target_compile_definitions( + test_rtmps_fallback_warning + PRIVATE FRONTEND_LOCALE="${CMAKE_SOURCE_DIR}/frontend/data/locale/en-US.ini" + ) + target_include_directories( + test_rtmps_fallback_warning + PRIVATE ${CMOCKA_INCLUDE_DIR} "${CMAKE_SOURCE_DIR}/frontend/api" "${CMAKE_SOURCE_DIR}/frontend/utility" + ) + target_link_libraries(test_rtmps_fallback_warning PRIVATE OBS::libobs Qt6::Core ${CMOCKA_LIBRARIES}) + add_dependencies(cmocka-tests test_rtmps_fallback_warning) + add_test(test_rtmps_fallback_warning ${CMAKE_CURRENT_BINARY_DIR}/test_rtmps_fallback_warning) + set_tests_properties(test_rtmps_fallback_warning PROPERTIES TIMEOUT 30) + cmocka_qt_runtime(test_rtmps_fallback_warning Qt6::Core) +endif() From 6c4f0f6b7665ffde1650f80374cc48101baedb1c Mon Sep 17 00:00:00 2001 From: Ryan Doupe Date: Sun, 25 Oct 2026 16:48:25 -0400 Subject: [PATCH 5/7] frontend: Say when RTMPS failed for lack of trusted root certificates When the system has no trusted root certificates, no RTMPS connection can be verified. The fallback to plain RTMP stays, but its warning now says why, in the standard order: OBS could not find the system's trusted certificates, so it could not make a secure connection; it streams unencrypted over RTMP instead; to stream encrypted, install the system's CA certificates package, or select Cancel. Other connection failures keep the normal message. The warning has its own id, rtmp.unencrypted.nocerts:, so it shows once per server in a session even after an ordinary failure was shown. The RTMP output's rtmp_tls_cert_unverified proc becomes rtmp_tls_failure(out string failure): "cert_unverified", "no_root_certs", or "" for anything else. "no_root_certs" is a handshake that failed certificate verification (or found no CA chain) while RTMP_TLS_LoadCerts loaded no certificates. A certificate that does not verify against loaded roots still stops the start. test_rtmps_cert_no_fallback checks an empty and a missing trust store (both "no_root_certs"), and a reset handshake with an empty store (an ordinary failure). test_rtmps_fallback_warning checks the causes, the id and the message of the no-certificates warning, and its wording. Refs #2205. --- docs/features.md | 5 +- frontend/data/locale/en-US.ini | 1 + frontend/utility/RtmpsFallback.hpp | 68 +++++++++---- frontend/widgets/OBSBasic.hpp | 4 +- frontend/widgets/OBSBasic_Streaming.cpp.hpp | 39 ++++--- plugins/obs-outputs/rtmp-stream.c.h | 61 ++++++++--- plugins/obs-outputs/rtmp-stream.h | 13 ++- test/cmocka/test_rtmps_cert_no_fallback.c | 106 +++++++++++++++++--- test/cmocka/test_rtmps_fallback_warning.cpp | 84 +++++++++++++--- 9 files changed, 298 insertions(+), 83 deletions(-) diff --git a/docs/features.md b/docs/features.md index 26e5a198c8..687fdf663a 100644 --- a/docs/features.md +++ b/docs/features.md @@ -249,8 +249,9 @@ not get a line. connection cannot be made, the shared fallback warning ("Streaming unencrypted (RTMP)") counts down three seconds and then streams to the same server over plain RTMP, unless you cancel; the fallback is written to the - log and to stderr. A server certificate that does not verify stops the - start instead. Custom servers are unchanged. *All*, pending merge. Issue + log and to stderr. Without trusted root certificates on the system, the + warning says so and suggests installing the system's CA certificates. A + server certificate that does not verify stops the start instead. Custom servers are unchanged. *All*, pending merge. Issue [#2205](https://github.com/obs-rust/obs-studio/issues/2205). ### Sources and filters diff --git a/frontend/data/locale/en-US.ini b/frontend/data/locale/en-US.ini index e551ac8d6c..4dbc1c0c70 100644 --- a/frontend/data/locale/en-US.ini +++ b/frontend/data/locale/en-US.ini @@ -448,6 +448,7 @@ Output.ConnectFail.Error="An unexpected error occurred when trying to connect to Output.ConnectFail.Disconnected="Disconnected from server." Output.RtmpsFallback.Title="Streaming unencrypted (RTMP)" Output.RtmpsFallback.Message="OBS could not make an encrypted connection (RTMPS) to the server.\n\nOBS streams to %1 unencrypted instead. To stop the stream from starting, select Cancel; to stream encrypted, check that your network or firewall allows connections on port 443." +Output.RtmpsFallback.NoRootCerts="OBS could not find your system's trusted certificates, so it could not make a secure connection (RTMPS) to the server.\n\nOBS streams to %1 unencrypted over RTMP instead. To stream encrypted, install your system's CA certificates package and start the stream again; to stop the stream from starting, select Cancel." Output.RtmpsFallback.Tray="Streaming unencrypted (RTMP): the encrypted connection failed." # output streaming-related messages diff --git a/frontend/utility/RtmpsFallback.hpp b/frontend/utility/RtmpsFallback.hpp index 50b8960b04..dee411ce66 100644 --- a/frontend/utility/RtmpsFallback.hpp +++ b/frontend/utility/RtmpsFallback.hpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -18,32 +19,59 @@ * and Cancel, which stops the start. */ namespace RtmpsFallback { -/* Two choices are open; both keep the behaviour of the first draft of #2219 - * until they are decided. - * - * (a) How often to ask. The shared warning shows its dialog once per id in an - * OBS session. true: the id names the server, so each server asks once - * per session (closest to asking on every failed start without an API - * change; a second failure to the same server only logs and continues - * over RTMP). false: one id for every server, the helper's default. */ +/* How often to ask: the shared warning shows its dialog once per id in an + * OBS session, and the id names the server, so each server asks once per + * session. A second failure to the same server only logs and continues over + * RTMP. */ inline constexpr bool IdPerServer = true; -/* (b) The main window hidden (OBS in the tray). The shared warning then logs - * and accepts at once. true: OBS also shows a tray notice and waits - * HiddenCountdownMs before it restarts over RTMP. false: it restarts at - * once. */ +/* With the main window hidden (OBS in the tray) the shared warning logs and + * accepts at once; OBS then shows a tray notice and waits HiddenCountdownMs + * before it restarts over RTMP. */ inline constexpr bool CountdownWhenHidden = true; inline constexpr int HiddenCountdownMs = 3000; +/* Why the RTMPS connection failed, as the RTMP output's rtmp_tls_failure proc + * reports it. */ +enum class Cause { + Connection, /* timeout, refused, reset, other handshake failure */ + CertUnverified, /* "cert_unverified": the server's certificate */ + NoRootCerts, /* "no_root_certs": the system has no trusted roots */ +}; + +inline Cause ParseCause(const char *failure) +{ + if (!failure) { + return Cause::Connection; + } + if (strcmp(failure, "cert_unverified") == 0) { + return Cause::CertUnverified; + } + if (strcmp(failure, "no_root_certs") == 0) { + return Cause::NoRootCerts; + } + return Cause::Connection; +} + inline constexpr const char *BaseId = "rtmp.unencrypted"; +inline constexpr const char *NoRootCertsId = "rtmp.unencrypted.nocerts"; -/* The fallback warning's id for a stream to plainUrl. */ -inline std::string Id(const std::string &plainUrl) +/* The fallback warning's id for a stream to plainUrl. The missing root + * certificates have their own id, so that warning is shown once per server + * even when an ordinary failure was shown first. */ +inline std::string Id(const std::string &plainUrl, Cause cause = Cause::Connection) { + std::string base = cause == Cause::NoRootCerts ? NoRootCertsId : BaseId; if (!IdPerServer || plainUrl.empty()) { - return BaseId; + return base; } - return std::string(BaseId) + ":" + plainUrl; + return base + ":" + plainUrl; +} + +/* The locale key of the warning's message (%1 is the plain RTMP URL). */ +inline const char *MessageKey(Cause cause) +{ + return cause == Cause::NoRootCerts ? "Output.RtmpsFallback.NoRootCerts" : "Output.RtmpsFallback.Message"; } /* Whether a stream that stopped with code may retry over plain RTMP: only a @@ -51,9 +79,9 @@ inline std::string Id(const std::string &plainUrl) * URLs), and never when the server's certificate did not verify. That is no * network trouble, and plain RTMP would send the stream key unencrypted to a * server that could not prove who it is. */ -inline bool Allowed(int code, bool multitrack, bool certUnverified) +inline bool Allowed(int code, bool multitrack, Cause cause) { - return code == OBS_OUTPUT_CONNECT_FAILED && !multitrack && !certUnverified; + return code == OBS_OUTPUT_CONNECT_FAILED && !multitrack && cause != Cause::CertUnverified; } using Answer = std::function; @@ -62,9 +90,9 @@ using Answer = std::function; * and has %1 for the URL. answer is called once, on the UI thread: true for * the countdown or "Accept anyway" (restart over plain RTMP), false for * Cancel or a closed dialog. */ -inline void Warn(const QString &title, const QString &message, const std::string &plainUrl, Answer answer) +inline void Warn(const QString &title, const QString &message, const std::string &plainUrl, Cause cause, Answer answer) { - std::string id = Id(plainUrl); + std::string id = Id(plainUrl, cause); QByteArray t = title.toUtf8(); QByteArray m = message.arg(QString::fromStdString(plainUrl)).toUtf8(); auto *held = new Answer(std::move(answer)); diff --git a/frontend/widgets/OBSBasic.hpp b/frontend/widgets/OBSBasic.hpp index 5bf8c46e32..6e5f0a0aaf 100644 --- a/frontend/widgets/OBSBasic.hpp +++ b/frontend/widgets/OBSBasic.hpp @@ -1406,9 +1406,9 @@ private slots: bool rtmpsFallbackRestart = false; bool streamUsesMultitrack = false; std::string RtmpsFallbackUrl(); - bool StreamTlsCertUnverified(); + std::string StreamTlsFailure(); void SetPlainRtmp(bool enabled); - void OfferRtmpsFallback(const std::string &plainUrl); + void OfferRtmpsFallback(const std::string &plainUrl, const std::string &tlsFailure); public slots: void DisplayStreamStartError(); diff --git a/frontend/widgets/OBSBasic_Streaming.cpp.hpp b/frontend/widgets/OBSBasic_Streaming.cpp.hpp index 05f9189508..749ecd7009 100644 --- a/frontend/widgets/OBSBasic_Streaming.cpp.hpp +++ b/frontend/widgets/OBSBasic_Streaming.cpp.hpp @@ -345,11 +345,13 @@ void OBSBasic::StreamingStop(int code, QString last_error) /* A service connection that rtmp-services upgraded to RTMPS can retry * over plain RTMP; multitrack video brings its own URLs. */ std::string rtmpsPlainUrl; + std::string rtmpsTlsFailure; if (code == OBS_OUTPUT_CONNECT_FAILED && !streamUsesMultitrack) { - bool certUnverified = StreamTlsCertUnverified(); - if (RtmpsFallback::Allowed(code, streamUsesMultitrack, certUnverified)) { + rtmpsTlsFailure = StreamTlsFailure(); + RtmpsFallback::Cause cause = RtmpsFallback::ParseCause(rtmpsTlsFailure.c_str()); + if (RtmpsFallback::Allowed(code, streamUsesMultitrack, cause)) { rtmpsPlainUrl = RtmpsFallbackUrl(); - } else if (certUnverified && !RtmpsFallbackUrl().empty()) { + } else if (cause == RtmpsFallback::Cause::CertUnverified && !RtmpsFallbackUrl().empty()) { blog(LOG_ERROR, "RTMPS: the server's certificate did not verify; " "the stream is not started and does not fall back to RTMP"); } @@ -389,7 +391,7 @@ void OBSBasic::StreamingStop(int code, QString last_error) OBSMessageBox::information(this, QTStr("Output.StreamEncodeError.Title"), msg); } else if (!rtmpsPlainUrl.empty()) { - OfferRtmpsFallback(rtmpsPlainUrl); + OfferRtmpsFallback(rtmpsPlainUrl, rtmpsTlsFailure); } else if (code != OBS_OUTPUT_SUCCESS && isVisible()) { OBSMessageBox::information(this, QTStr("Output.ConnectFail.Title"), QT_UTF8(errorMessage)); @@ -431,29 +433,34 @@ void OBSBasic::SetPlainRtmp(bool enabled) calldata_free(&cd); } -/* Whether the stream output's last connect failed because the server's TLS - * certificate did not verify (obs-outputs' rtmp output reports it). */ -bool OBSBasic::StreamTlsCertUnverified() +/* Why the stream output's last connect failed its TLS handshake, as + * obs-outputs' rtmp output reports it: "cert_unverified", "no_root_certs", + * or empty for anything else (also for outputs without the proc). */ +std::string OBSBasic::StreamTlsFailure() { obs_output_t *output = outputHandler ? outputHandler->streamOutput.Get() : nullptr; if (!output) { - return false; + return ""; } calldata_t cd = {0}; - bool unverified = false; - if (proc_handler_call(obs_output_get_proc_handler(output), "rtmp_tls_cert_unverified", &cd)) { - unverified = calldata_bool(&cd, "unverified"); + const char *failure = nullptr; + std::string result; + if (proc_handler_call(obs_output_get_proc_handler(output), "rtmp_tls_failure", &cd) && + calldata_get_string(&cd, "failure", &failure) && failure) { + result = failure; } calldata_free(&cd); - return unverified; + return result; } /* The RTMPS connection failed: the shared fallback warning counts down three * seconds and then OBS restarts the stream over plain RTMP, unless the user - * cancels. The warning logs the fallback. */ -void OBSBasic::OfferRtmpsFallback(const std::string &plainUrl) + * cancels. The warning logs the fallback. Without trusted root certificates + * its message says so. */ +void OBSBasic::OfferRtmpsFallback(const std::string &plainUrl, const std::string &tlsFailure) { + RtmpsFallback::Cause cause = RtmpsFallback::ParseCause(tlsFailure.c_str()); auto restart = [this]() { if (isClosing() || outputHandler->StreamingActive()) { return; @@ -462,8 +469,8 @@ void OBSBasic::OfferRtmpsFallback(const std::string &plainUrl) QMetaObject::invokeMethod(this, &OBSBasic::StartStreaming, Qt::QueuedConnection); }; - RtmpsFallback::Warn(QTStr("Output.RtmpsFallback.Title"), QTStr("Output.RtmpsFallback.Message"), plainUrl, - [this, restart](bool accepted) { + RtmpsFallback::Warn(QTStr("Output.RtmpsFallback.Title"), QTStr(RtmpsFallback::MessageKey(cause)), plainUrl, + cause, [this, restart](bool accepted) { if (!accepted) { blog(LOG_INFO, "RTMPS fallback cancelled; the stream was not started"); return; diff --git a/plugins/obs-outputs/rtmp-stream.c.h b/plugins/obs-outputs/rtmp-stream.c.h index dd94d2c40c..8e166dea38 100644 --- a/plugins/obs-outputs/rtmp-stream.c.h +++ b/plugins/obs-outputs/rtmp-stream.c.h @@ -140,21 +140,50 @@ static void rtmp_stream_destroy(void *data) bfree(stream); } -/* The TLS handshake failed because the server's certificate did not verify: - * not trusted, expired, or issued for another host name (mbedTLS reports a - * host-name mismatch the same way). Unlike a timeout, refusal or reset, this - * is no network trouble, so the frontend does not retry over plain RTMP - * (obs-rust/obs-studio#2205). */ -static bool rtmp_tls_cert_unverified(int last_error_code) +/* Why the TLS handshake of a failed connect failed (obs-rust/obs-studio#2205). + * + * The server's certificate did not verify: not trusted, expired, or issued + * for another host name (mbedTLS reports a host-name mismatch the same way). + * Unlike a timeout, refusal or reset this is no network trouble, so the + * frontend does not retry over plain RTMP. + * + * The system has no trusted root certificates (RTMP_TLS_LoadCerts loaded + * none, so nothing can verify): the frontend still falls back, and its + * warning names the cause. + * + * Any other failure, also one before the certificate is checked, is a + * connection failure. */ +static enum rtmp_tls_failure rtmp_tls_failure(RTMP *r) { - return last_error_code == -0x2700; /* MBEDTLS_ERR_X509_CERT_VERIFY_FAILED */ + int code = r->last_error_code; + bool verify_failed = code == -0x2700; /* MBEDTLS_ERR_X509_CERT_VERIFY_FAILED */ + bool no_ca_chain = code == -0x7680; /* MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED */ + if (!verify_failed && !no_ca_chain) + return RTMP_TLS_FAILURE_NONE; + + bool has_roots = r->RTMP_TLS_ctx && r->RTMP_TLS_ctx->cacert && r->RTMP_TLS_ctx->cacert->raw.len > 0; + if (!has_roots) + return RTMP_TLS_FAILURE_NO_ROOT_CERTS; + return verify_failed ? RTMP_TLS_FAILURE_CERT_UNVERIFIED : RTMP_TLS_FAILURE_NONE; } -/* Output proc: whether the last connect failed on certificate verification. */ -static void rtmp_stream_tls_cert_unverified_proc(void *data, calldata_t *cd) +/* Output proc: why the last connect's TLS handshake failed, as + * "cert_unverified", "no_root_certs", or "" for anything else. */ +static void rtmp_stream_tls_failure_proc(void *data, calldata_t *cd) { struct rtmp_stream *stream = data; - calldata_set_bool(cd, "unverified", stream->tls_cert_unverified); + const char *failure = ""; + switch (stream->tls_failure) { + case RTMP_TLS_FAILURE_CERT_UNVERIFIED: + failure = "cert_unverified"; + break; + case RTMP_TLS_FAILURE_NO_ROOT_CERTS: + failure = "no_root_certs"; + break; + case RTMP_TLS_FAILURE_NONE: + break; + } + calldata_set_string(cd, "failure", failure); } static void *rtmp_stream_create(obs_data_t *settings, obs_output_t *output) @@ -198,8 +227,8 @@ static void *rtmp_stream_create(obs_data_t *settings, obs_output_t *output) goto fail; } - proc_handler_add(obs_output_get_proc_handler(output), "void rtmp_tls_cert_unverified(out bool unverified)", - rtmp_stream_tls_cert_unverified_proc, stream); + proc_handler_add(obs_output_get_proc_handler(output), "void rtmp_tls_failure(out string failure)", + rtmp_stream_tls_failure_proc, stream); UNUSED_PARAMETER(settings); return stream; @@ -1012,11 +1041,13 @@ static int try_connect(struct rtmp_stream *stream) rtmp_host_log_interface_type(stream); - stream->tls_cert_unverified = false; + stream->tls_failure = RTMP_TLS_FAILURE_NONE; if (!RTMP_Connect(&stream->rtmp, NULL)) { - stream->tls_cert_unverified = rtmp_tls_cert_unverified(stream->rtmp.last_error_code); - if (stream->tls_cert_unverified) + stream->tls_failure = rtmp_tls_failure(&stream->rtmp); + if (stream->tls_failure == RTMP_TLS_FAILURE_CERT_UNVERIFIED) warn("The server's TLS certificate did not verify; the stream is not started"); + else if (stream->tls_failure == RTMP_TLS_FAILURE_NO_ROOT_CERTS) + warn("No trusted root certificates were found, so the TLS connection could not be verified"); set_output_error(stream); return OBS_OUTPUT_CONNECT_FAILED; } diff --git a/plugins/obs-outputs/rtmp-stream.h b/plugins/obs-outputs/rtmp-stream.h index 626170adda..a0ee0b0aff 100644 --- a/plugins/obs-outputs/rtmp-stream.h +++ b/plugins/obs-outputs/rtmp-stream.h @@ -53,6 +53,14 @@ struct dbr_interpolation_point { long bitrates[MAX_OUTPUT_VIDEO_ENCODERS]; }; +/* Why an RTMPS connect failed, for the frontend's RTMP fallback + * (obs-rust/obs-studio#2205). */ +enum rtmp_tls_failure { + RTMP_TLS_FAILURE_NONE, /* none, or a connection failure */ + RTMP_TLS_FAILURE_CERT_UNVERIFIED, /* the server's certificate */ + RTMP_TLS_FAILURE_NO_ROOT_CERTS, /* no trusted root certificates */ +}; + struct rtmp_stream { obs_output_t *output; @@ -126,9 +134,8 @@ struct rtmp_stream { bool new_socket_loop; - /* The last connect failed because the server's TLS certificate did - * not verify (rtmp_tls_cert_unverified). */ - bool tls_cert_unverified; + /* Why the last connect's TLS handshake failed (rtmp_tls_failure). */ + enum rtmp_tls_failure tls_failure; bool low_latency_mode; bool disable_send_window_optimization; bool socket_thread_active; diff --git a/test/cmocka/test_rtmps_cert_no_fallback.c b/test/cmocka/test_rtmps_cert_no_fallback.c index 80a0d0fbd1..dde5548750 100644 --- a/test/cmocka/test_rtmps_cert_no_fallback.c +++ b/test/cmocka/test_rtmps_cert_no_fallback.c @@ -3,8 +3,10 @@ * rule 7). The frontend retries a failed RTMPS start over RTMP only for a * connection failure: timeout, refusal, reset, or a handshake that fails for * another reason. A certificate that does not verify, also one issued for - * another host name, stops the start; the RTMP output reports it through its - * "rtmp_tls_cert_unverified" proc. + * another host name, stops the start. A system without trusted root + * certificates still falls back, with a warning that names that cause. The + * RTMP output reports the cause through its "rtmp_tls_failure" proc: + * "cert_unverified", "no_root_certs", or "" for any other failure. * * The test builds the real rtmp-stream.c and librtmp into this executable and * connects to a TLS listener on the loopback interface. The listener presents @@ -12,7 +14,9 @@ * that certificate trusted, only the host name is wrong; without it, the * certificate is also untrusted. A listener that closes the connection during * the handshake, and a port with no listener, are connection failures that - * still fall back. The output is a small fake, so no display or encoder is + * still fall back. An empty trust store (the system's certificate directory + * has no certificates) and a missing one (it could not be read) are the + * no-root-certificates case. The output is a small fake, so no display or encoder is * needed. */ #include @@ -25,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -90,10 +95,31 @@ proc_handler_t *fake_output_get_proc_handler(const obs_output_t *output) /* A certificate the client trusts in addition to the system's, or NULL. */ static mbedtls_x509_crt *trusted_extra; +/* The client's trust store. */ +enum store { + STORE_SYSTEM, /* the system's root certificates */ + STORE_EMPTY, /* the certificate directory parsed, with nothing in it */ + STORE_MISSING, /* the certificates could not be loaded at all */ +}; +static enum store store = STORE_SYSTEM; + static int test_rtmp_connect(RTMP *r, RTMPPacket *cp) { - if (trusted_extra && r->RTMP_TLS_ctx) - mbedtls_ssl_conf_ca_chain(&r->RTMP_TLS_ctx->conf, trusted_extra, NULL); + tls_ctx *tls = r->RTMP_TLS_ctx; + if (tls && store == STORE_EMPTY && tls->cacert) { + mbedtls_x509_crt_free(tls->cacert); + mbedtls_x509_crt_init(tls->cacert); + } else if (tls && store == STORE_MISSING) { + /* What RTMP_TLS_LoadCerts leaves when it fails. */ + if (tls->cacert) { + mbedtls_x509_crt_free(tls->cacert); + free(tls->cacert); + tls->cacert = NULL; + } + mbedtls_ssl_conf_ca_chain(&tls->conf, NULL, NULL); + } + if (trusted_extra && tls) + mbedtls_ssl_conf_ca_chain(&tls->conf, trusted_extra, NULL); return RTMP_Connect(r, cp); } @@ -227,7 +253,7 @@ static int closed_port(void) struct result { int code; bool proc_found; - bool unverified; + char failure[32]; }; /* Connects the RTMP output to rtmps://127.0.0.1:port/live and asks it, as @@ -245,8 +271,9 @@ static struct result connect_to(int port) r.code = try_connect(stream); - r.proc_found = proc_handler_call(output_procs, "rtmp_tls_cert_unverified", &cd); - r.unverified = r.proc_found && calldata_bool(&cd, "unverified"); + r.proc_found = proc_handler_call(output_procs, "rtmp_tls_failure", &cd); + const char *failure = r.proc_found ? calldata_string(&cd, "failure") : NULL; + snprintf(r.failure, sizeof(r.failure), "%s", failure ? failure : "(none)"); calldata_free(&cd); rtmp_stream_destroy(stream); @@ -270,7 +297,7 @@ static void cert_for_other_host_does_not_fall_back(void **state) trusted_extra = NULL; assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); assert_true(r.proc_found); - assert_true(r.unverified); + assert_string_equal(r.failure, "cert_unverified"); } /* The certificate is not trusted (and for another host): no fallback. */ @@ -285,7 +312,7 @@ static void untrusted_cert_does_not_fall_back(void **state) stop_server(&s); assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); assert_true(r.proc_found); - assert_true(r.unverified); + assert_string_equal(r.failure, "cert_unverified"); } /* The server closes the connection during the handshake: a connection @@ -301,7 +328,7 @@ static void reset_handshake_still_falls_back(void **state) stop_server(&s); assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); assert_true(r.proc_found); - assert_false(r.unverified); + assert_string_equal(r.failure, ""); } /* Nothing listens: a refused connection, so the fallback stays open. */ @@ -312,7 +339,59 @@ static void refused_connection_still_falls_back(void **state) assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); assert_true(r.proc_found); - assert_false(r.unverified); + assert_string_equal(r.failure, ""); +} + +/* The system has no trusted root certificates: the start falls back, and the + * output names the cause so that the warning can say so. */ +static void empty_trust_store_reports_no_root_certs(void **state) +{ + (void)state; + struct server s; + store = STORE_EMPTY; + start_server(&s, LISTENER_TLS); + + struct result r = connect_to(s.port); + + stop_server(&s); + store = STORE_SYSTEM; + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, "no_root_certs"); +} + +static void missing_trust_store_reports_no_root_certs(void **state) +{ + (void)state; + struct server s; + store = STORE_MISSING; + start_server(&s, LISTENER_TLS); + + struct result r = connect_to(s.port); + + stop_server(&s); + store = STORE_SYSTEM; + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, "no_root_certs"); +} + +/* Without root certificates, a failure before the certificate is checked is + * still an ordinary connection failure. */ +static void empty_trust_store_reset_is_ordinary(void **state) +{ + (void)state; + struct server s; + store = STORE_EMPTY; + start_server(&s, LISTENER_RESET); + + struct result r = connect_to(s.port); + + stop_server(&s); + store = STORE_SYSTEM; + assert_int_equal(r.code, OBS_OUTPUT_CONNECT_FAILED); + assert_true(r.proc_found); + assert_string_equal(r.failure, ""); } static int setup(void **state) @@ -347,6 +426,9 @@ int main(void) cmocka_unit_test(untrusted_cert_does_not_fall_back), cmocka_unit_test(reset_handshake_still_falls_back), cmocka_unit_test(refused_connection_still_falls_back), + cmocka_unit_test(empty_trust_store_reports_no_root_certs), + cmocka_unit_test(missing_trust_store_reports_no_root_certs), + cmocka_unit_test(empty_trust_store_reset_is_ordinary), }; return cmocka_run_group_tests(tests, setup, teardown); } diff --git a/test/cmocka/test_rtmps_fallback_warning.cpp b/test/cmocka/test_rtmps_fallback_warning.cpp index c3d195a4b9..b747e6a10e 100644 --- a/test/cmocka/test_rtmps_fallback_warning.cpp +++ b/test/cmocka/test_rtmps_fallback_warning.cpp @@ -3,7 +3,8 @@ * stream that rtmp-services upgraded to RTMPS fails to connect, the frontend * raises the warning with Cancel, its standard wording and the plain RTMP URL, * and restarts over RTMP only when the answer is to continue. A certificate - * that did not verify never falls back. obs_frontend_warn_fallback is a fake + * that did not verify never falls back; a system without trusted root + * certificates falls back with a message that says so. obs_frontend_warn_fallback is a fake * here that records its arguments. */ #include @@ -60,7 +61,7 @@ static void raises_shared_warning_with_cancel(void **state) (void)state; int answers = 0; RtmpsFallback::Warn("Streaming unencrypted (RTMP)", "OBS streams to %1 unencrypted.", url, - [&](bool) { answers++; }); + RtmpsFallback::Cause::Connection, [&](bool) { answers++; }); assert_int_equal(raised.calls, 1); assert_true(raised.can_cancel); @@ -79,7 +80,8 @@ static void accept_passes_true(void **state) { (void)state; int answer = -1; - RtmpsFallback::Warn("T", "M %1", url, [&](bool accepted) { answer = accepted; }); + RtmpsFallback::Warn("T", "M %1", url, RtmpsFallback::Cause::Connection, + [&](bool accepted) { answer = accepted; }); raised.cb(raised.param, true); assert_int_equal(answer, 1); } @@ -89,13 +91,14 @@ static void cancel_passes_false(void **state) { (void)state; int answer = -1; - RtmpsFallback::Warn("T", "M %1", url, [&](bool accepted) { answer = accepted; }); + RtmpsFallback::Warn("T", "M %1", url, RtmpsFallback::Cause::Connection, + [&](bool accepted) { answer = accepted; }); raised.cb(raised.param, false); assert_int_equal(answer, 0); } -/* Open question (a): with IdPerServer, each server has its own id, so each - * server's first failure in a session shows the dialog. */ +/* Each server has its own id, so each server's first failure in a session + * shows the dialog. */ static void id_names_the_server(void **state) { (void)state; @@ -114,13 +117,51 @@ static void id_names_the_server(void **state) static void falls_back_only_on_connection_failure(void **state) { (void)state; - assert_true(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, false)); - assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, true)); - assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, true, false)); - assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_BAD_PATH, false, false)); - assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_INVALID_STREAM, false, false)); - assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_DISCONNECTED, false, false)); - assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_SUCCESS, false, false)); + using RtmpsFallback::Cause; + assert_true(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, Cause::Connection)); + assert_true(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, Cause::NoRootCerts)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, false, Cause::CertUnverified)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_CONNECT_FAILED, true, Cause::Connection)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_BAD_PATH, false, Cause::Connection)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_INVALID_STREAM, false, Cause::Connection)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_DISCONNECTED, false, Cause::Connection)); + assert_false(RtmpsFallback::Allowed(OBS_OUTPUT_SUCCESS, false, Cause::Connection)); +} + +/* The RTMP output's rtmp_tls_failure strings map to the causes; anything + * else, or no answer, is an ordinary connection failure. */ +static void parses_the_outputs_cause(void **state) +{ + (void)state; + using RtmpsFallback::Cause; + assert_true(RtmpsFallback::ParseCause("cert_unverified") == Cause::CertUnverified); + assert_true(RtmpsFallback::ParseCause("no_root_certs") == Cause::NoRootCerts); + assert_true(RtmpsFallback::ParseCause("") == Cause::Connection); + assert_true(RtmpsFallback::ParseCause("other") == Cause::Connection); + assert_true(RtmpsFallback::ParseCause(nullptr) == Cause::Connection); +} + +/* No trusted root certificates: the fallback is offered with its own id and + * the message that names the cause. Other failures keep the normal message. */ +static void no_root_certs_warning_says_why(void **state) +{ + (void)state; + using RtmpsFallback::Cause; + assert_string_equal(RtmpsFallback::MessageKey(Cause::NoRootCerts), "Output.RtmpsFallback.NoRootCerts"); + assert_string_equal(RtmpsFallback::MessageKey(Cause::Connection), "Output.RtmpsFallback.Message"); + + int answer = -1; + RtmpsFallback::Warn("T", "No certificates; streaming to %1.", url, Cause::NoRootCerts, + [&](bool accepted) { answer = accepted; }); + assert_int_equal(raised.calls, 1); + assert_true(raised.can_cancel); + assert_string_equal(raised.id.c_str(), "rtmp.unencrypted.nocerts:rtmp://ingest.example/app"); + assert_string_equal(raised.message.c_str(), "No certificates; streaming to rtmp://ingest.example/app."); + raised.cb(raised.param, true); + assert_int_equal(answer, 1); + + assert_string_not_equal(RtmpsFallback::Id(url, Cause::NoRootCerts).c_str(), + RtmpsFallback::Id(url, Cause::Connection).c_str()); } static std::string locale_value(const std::string &text, const char *key) @@ -154,6 +195,21 @@ static void wording_follows_the_standard(void **state) assert_null(strstr(message.c_str(), "insecure")); assert_null(strstr(message.c_str(), "unsafe")); assert_string_equal(locale_value(text, "Output.RtmpsFallback.Proceed").c_str(), ""); + + /* No root certificates: what failed (the trusted certificates), what OBS + * does instead (unencrypted over RTMP), and what the user can do. */ + std::string nocerts = locale_value(text, "Output.RtmpsFallback.NoRootCerts"); + assert_false(nocerts.empty()); + assert_true(nocerts != message); + assert_non_null(strstr(nocerts.c_str(), "trusted certificates")); + assert_non_null(strstr(nocerts.c_str(), "unencrypted")); + assert_non_null(strstr(nocerts.c_str(), "CA certificates package")); + assert_non_null(strstr(nocerts.c_str(), "Cancel")); + assert_non_null(strstr(nocerts.c_str(), "%1")); + const char *what = strstr(nocerts.c_str(), "trusted certificates"); + const char *instead = strstr(nocerts.c_str(), "unencrypted"); + const char *todo = strstr(nocerts.c_str(), "CA certificates package"); + assert_true(what < instead && instead < todo); assert_string_equal(locale_value(text, "FallbackWarning.AcceptAnyway").c_str(), "Accept anyway"); } @@ -165,6 +221,8 @@ int main(void) cmocka_unit_test_setup(cancel_passes_false, reset), cmocka_unit_test_setup(id_names_the_server, reset), cmocka_unit_test_setup(falls_back_only_on_connection_failure, reset), + cmocka_unit_test_setup(parses_the_outputs_cause, reset), + cmocka_unit_test_setup(no_root_certs_warning_says_why, reset), cmocka_unit_test_setup(wording_follows_the_standard, reset), }; return cmocka_run_group_tests(tests, nullptr, nullptr); From a7d61ff2e95eda4ed2419670546cd22829dbd474 Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Sun, 1 Nov 2026 23:28:40 -0800 Subject: [PATCH 6/7] rtmp-services: Declare rtmp_common_register_procs before its definition The unity group compiles rtmp-common.c.h before rtmp-services-main.c.h, so the extern declaration in main came after the definition and GCC failed the merge-queue build with -Werror=missing-prototypes (#2219). Co-Authored-By: Claude Opus 5.5 --- plugins/rtmp-services/service-specific/rtmps-upgrade.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/rtmp-services/service-specific/rtmps-upgrade.h b/plugins/rtmp-services/service-specific/rtmps-upgrade.h index 69378017a2..063f948d28 100644 --- a/plugins/rtmp-services/service-specific/rtmps-upgrade.h +++ b/plugins/rtmp-services/service-specific/rtmps-upgrade.h @@ -12,3 +12,8 @@ * is in the list for service. Returns false and leaves out untouched * otherwise. */ bool rtmps_upgrade_url(const char *service, const char *url, struct dstr *out); + +/* Registers the rtmp_services_rtmps_fallback and rtmp_services_set_plain_rtmp + * procedures on the global proc handler. Defined in rtmp-common.c, called from + * obs_module_load. */ +void rtmp_common_register_procs(void); From 3908f582d1897b1504990f430c9c53a3aa37a9b0 Mon Sep 17 00:00:00 2001 From: Zach Vorhies Date: Mon, 2 Nov 2026 00:41:02 -0800 Subject: [PATCH 7/7] obs-outputs: Free the RTMP stream key copy on reconnect and destroy RTMP_AddStream copies the key, but RTMP_Close frees it for a publishing connection only after a redirect, and RTMP_Init forgets it. A failed RTMPS start (test_rtmps_cert_no_fallback) leaked it under LeakSanitizer. Co-Authored-By: Claude Opus 5.5 --- plugins/obs-outputs/rtmp-stream.c.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/plugins/obs-outputs/rtmp-stream.c.h b/plugins/obs-outputs/rtmp-stream.c.h index 8e166dea38..f127dca52e 100644 --- a/plugins/obs-outputs/rtmp-stream.c.h +++ b/plugins/obs-outputs/rtmp-stream.c.h @@ -89,6 +89,19 @@ static inline bool disconnected(struct rtmp_stream *stream) return os_atomic_load_bool(&stream->disconnected); } +/* RTMP_AddStream copies the stream key; RTMP_Close frees that copy only for + * a reading connection or after a server redirect (RTMP_PUB_CLEAN), and + * RTMP_Init forgets it. Free it before either drops it. */ +static void free_rtmp_streams(RTMP *r) +{ + for (int i = 0; i < r->Link.nStreams; i++) { + free(r->Link.streams[i].playpath.av_val); + r->Link.streams[i].playpath.av_val = NULL; + } + r->Link.nStreams = 0; + r->Link.curStreamIdx = 0; +} + static void rtmp_stream_destroy(void *data) { struct rtmp_stream *stream = data; @@ -110,6 +123,7 @@ static void rtmp_stream_destroy(void *data) } } + free_rtmp_streams(&stream->rtmp); RTMP_TLS_Free(&stream->rtmp); free_packets(stream); dstr_free(&stream->path); @@ -1000,7 +1014,8 @@ static int try_connect(struct rtmp_stream *stream) info("Connecting to RTMP URL %s...", stream->path.array); - // free any existing RTMP TLS context + // free any existing RTMP TLS context and stream keys + free_rtmp_streams(&stream->rtmp); RTMP_TLS_Free(&stream->rtmp); RTMP_Init(&stream->rtmp);