diff --git a/src/Common/Parser.cpp b/src/Common/Parser.cpp index 9c073f2c..012a3306 100644 --- a/src/Common/Parser.cpp +++ b/src/Common/Parser.cpp @@ -20,6 +20,30 @@ using namespace toolkit; namespace mediakit { +string findrSubString(const char *buf, const char *start, const char end, size_t buf_size) { + if (buf_size <= 0) { + buf_size = strlen(buf); + } + auto msg_start = buf; + auto msg_end = buf + buf_size; + size_t len = 0; + if (start != NULL) { + len = strlen(start); + msg_start = strstr(buf, start); + } + if (msg_start == NULL) { + return ""; + } + msg_start += len; + if (end != NULL) { + msg_end = strrchr(msg_start, end); + if (msg_end == NULL) { + return ""; + } + } + return string(msg_start, msg_end); +} + string findSubString(const char *buf, const char *start, const char *end, size_t buf_size) { if (buf_size <= 0) { buf_size = strlen(buf); diff --git a/src/Common/Parser.h b/src/Common/Parser.h index 3741f4bc..5c10cae7 100644 --- a/src/Common/Parser.h +++ b/src/Common/Parser.h @@ -18,6 +18,7 @@ namespace mediakit { // 从字符串中提取子字符串 +std::string findrSubString(const char *buf, const char *start, const char end, size_t buf_size = 0); std::string findSubString(const char *buf, const char *start, const char *end, size_t buf_size = 0); // 把url解析为主机地址和端口号,兼容ipv4/ipv6/dns void splitUrl(const std::string &url, std::string &host, uint16_t &port); diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index f401cf34..27e00ae7 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -65,12 +65,25 @@ void RtmpPusher::onPublishResult_l(const SockException &ex, bool handshake_done) void RtmpPusher::publish(const string &url) { teardown(); - string host_url = findSubString(url.data(), "://", "/"); - _app = findSubString(url.data(), (host_url + "/").data(), "/"); - _stream_id = findSubString(url.data(), (host_url + "/" + _app + "/").data(), NULL); - _tc_url = string("rtmp://") + host_url + "/" + _app; + string _head ; + // rtmps rt_url head should be rtmps + const char *rtmp_head = "rtmp://"; + const char *rtmps_head = "rtmps://"; + if(0 == strncasecmp(url.c_str(), rtmp_head, strlen(rtmp_head))) + { + _head = rtmp_head; + } + if(0 == strncasecmp(url.c_str(), rtmps_head, strlen(rtmps_head))) + { + _head = rtmps_head; + } - if (!_app.size() || !_stream_id.size()) { + string host_url = findSubString(url.data(), "://", "/"); + _app = findrSubString(url.data(), (host_url + "/").data(), '/'); + _stream_id = findSubString(url.data(), (host_url + "/" + _app + "/").data(), NULL); + _tc_url = _head + host_url + "/" + _app; + + if (!_app.size() || !_stream_id.size() || !_head.size()) { onPublishResult_l(SockException(Err_other, "rtmp url非法"), false); return; }