From 9bd3c5181808fdf453ca283d5e81a25faa2932e1 Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 28 Aug 2022 16:56:04 +0800 Subject: [PATCH] RTMP: Refine code for gussing stream by app. --- trunk/src/app/srs_app_rtmp_conn.cpp | 25 ++--------- trunk/src/protocol/srs_protocol_utility.cpp | 23 ++++++++++ trunk/src/protocol/srs_protocol_utility.hpp | 6 +++ trunk/src/utest/srs_utest_rtmp.cpp | 47 +++++++++++++++++++++ 4 files changed, 79 insertions(+), 22 deletions(-) diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index b940db209..eb7dd1c3a 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -459,28 +459,9 @@ srs_error_t SrsRtmpConn::stream_service_cycle() // guess stream name if (req->stream.empty()) { - srs_trace("client no stream, guess stream from app=%s and param=%s", req->app.c_str(), req->param.c_str()); - - size_t pos = std::string::npos; - std::string app = req->app; - std::string param = req->param; - - // app - if ((pos = app.find("/")) != std::string::npos) { - req->app = app.substr(0, pos); - req->stream = app.substr(pos + 1); - if ((pos = req->stream.find("?")) != std::string::npos) { - req->stream = req->stream.substr(0, pos); - } - } else { - // param - if ((pos = param.find("/")) != std::string::npos) { - req->stream = param.substr(pos + 1); - if ((pos = req->stream.find("?")) != std::string::npos) { - req->stream = req->stream.substr(0, pos); - } - } - } + string app = req->app, param = req->param; + srs_guess_stream_by_app(req->app, req->param, req->stream); + srs_trace("Guessing by app=%s, param=%s to app=%s, param=%s, stream=%s", app.c_str(), param.c_str(), req->app.c_str(), req->param.c_str(), req->stream.c_str()); } req->strip(); diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 384a24137..30e1ecf1f 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -137,6 +137,29 @@ void srs_discovery_tc_url(string tcUrl, string& schema, string& host, string& vh } } +void srs_guess_stream_by_app(string& app, string& param, string& stream) +{ + size_t pos = std::string::npos; + + // Extract stream from app, if contains slash. + if ((pos = app.find("/")) != std::string::npos) { + stream = app.substr(pos + 1); + app = app.substr(0, pos); + + if ((pos = stream.find("?")) != std::string::npos) { + param = stream.substr(pos); + stream = stream.substr(0, pos); + } + return; + } + + // Extract stream from param, if contains slash. + if ((pos = param.find("/")) != std::string::npos) { + stream = param.substr(pos + 1); + param = param.substr(0, pos); + } +} + void srs_parse_query_string(string q, map& query) { // query string flags. diff --git a/trunk/src/protocol/srs_protocol_utility.hpp b/trunk/src/protocol/srs_protocol_utility.hpp index c833d3c83..057b5d5b8 100644 --- a/trunk/src/protocol/srs_protocol_utility.hpp +++ b/trunk/src/protocol/srs_protocol_utility.hpp @@ -52,6 +52,12 @@ class ISrsProtocolReadWriter; extern void srs_discovery_tc_url(std::string tcUrl, std::string& schema, std::string& host, std::string& vhost, std::string& app, std::string& stream, int& port, std::string& param); +// Guessing stream by app and param, to make OBS happy. For example: +// rtmp://ip/live/livestream +// rtmp://ip/live/livestream?secret=xxx +// rtmp://ip/live?secret=xxx/livestream +extern void srs_guess_stream_by_app(std::string& app, std::string& param, std::string& stream); + // parse query string to map(k,v). // must format as key=value&...&keyN=valueN extern void srs_parse_query_string(std::string q, std::map& query); diff --git a/trunk/src/utest/srs_utest_rtmp.cpp b/trunk/src/utest/srs_utest_rtmp.cpp index 5d5a267d8..5e07f1506 100644 --- a/trunk/src/utest/srs_utest_rtmp.cpp +++ b/trunk/src/utest/srs_utest_rtmp.cpp @@ -3574,3 +3574,50 @@ VOID TEST(ProtocolRTMPTest, DiscoveryTcUrl) } } +VOID TEST(ProtocolRTMPTest, GuessingStream) +{ + // Stream in app without params. + if (true) { + string app = "live/livestream", param = "", stream = ""; + srs_guess_stream_by_app(app, param, stream); + EXPECT_STREQ("live", app.c_str()); + EXPECT_STREQ("livestream", stream.c_str()); + } + + // Stream in app with params. + if (true) { + string app = "live/livestream", param = "?secret=xxx", stream = ""; + srs_guess_stream_by_app(app, param, stream); + EXPECT_STREQ("live", app.c_str()); + EXPECT_STREQ("livestream", stream.c_str()); + EXPECT_STREQ("?secret=xxx", param.c_str()); + } + + // Stream in app with params. + if (true) { + string app = "live/livestream?secret=xxx", param = "", stream = ""; + srs_guess_stream_by_app(app, param, stream); + EXPECT_STREQ("live", app.c_str()); + EXPECT_STREQ("livestream", stream.c_str()); + EXPECT_STREQ("?secret=xxx", param.c_str()); + } + + // Stream in param. + if (true) { + string app = "live", param = "?secret=xxx/livestream", stream = ""; + srs_guess_stream_by_app(app, param, stream); + EXPECT_STREQ("live", app.c_str()); + EXPECT_STREQ("livestream", stream.c_str()); + EXPECT_STREQ("?secret=xxx", param.c_str()); + } + + // No stream. + if (true) { + string app = "live", param = "?secret=xxx", stream = ""; + srs_guess_stream_by_app(app, param, stream); + EXPECT_STREQ("live", app.c_str()); + EXPECT_STREQ("", stream.c_str()); + EXPECT_STREQ("?secret=xxx", param.c_str()); + } +} +