From 64b8079ac10b17ca8012a2b3fecdfaba71d89269 Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sat, 10 Jun 2023 12:28:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=87=BD=E6=95=B0=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FindField改名为findSubString --- server/WebApi.cpp | 2 +- src/Common/Parser.cpp | 19 ++++++++++--------- src/Common/Parser.h | 2 +- src/Extension/Factory.cpp | 8 ++++---- src/Http/HttpClient.cpp | 12 ++++++------ src/Http/HttpCookieManager.cpp | 4 ++-- src/Http/HttpFileManager.cpp | 4 ++-- src/Player/PlayerBase.cpp | 2 +- src/Pusher/PusherBase.cpp | 2 +- src/Rtmp/RtmpPlayer.cpp | 4 ++-- src/Rtmp/RtmpPusher.cpp | 6 +++--- src/Rtsp/Rtsp.cpp | 6 +++--- src/Rtsp/RtspPlayer.cpp | 4 ++-- src/Rtsp/RtspPusher.cpp | 10 +++++----- src/Rtsp/RtspSession.cpp | 12 ++++++------ tests/test_bench_push.cpp | 2 +- tests/test_pusherMp4.cpp | 2 +- 17 files changed, 51 insertions(+), 50 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 64eec188..88e4d4ea 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1563,7 +1563,7 @@ void installWebApi() { } //找到截图 - auto tm = FindField(path.data() + scan_path.size(), nullptr, ".jpeg"); + auto tm = findSubString(path.data() + scan_path.size(), nullptr, ".jpeg"); if (atoll(tm.data()) + expire_sec < time(NULL)) { //截图已经过期,改名,以便再次请求时,可以返回老截图 rename(path.data(), new_snap.data()); diff --git a/src/Common/Parser.cpp b/src/Common/Parser.cpp index b47a0013..a7be567c 100644 --- a/src/Common/Parser.cpp +++ b/src/Common/Parser.cpp @@ -19,11 +19,12 @@ using namespace toolkit; namespace mediakit { -string FindField(const char *buf, const char *start, const char *end, size_t buf_size) { +string findSubString(const char *buf, const char *start, const char *end, size_t buf_size) { if (buf_size <= 0) { buf_size = strlen(buf); } - const char *msg_start = buf, *msg_end = buf + buf_size; + auto msg_start = buf; + auto msg_end = buf + buf_size; size_t len = 0; if (start != NULL) { len = strlen(start); @@ -253,12 +254,12 @@ std::string Parser::mergeUrl(const string &base_url, const string &path) { } void RtspUrl::parse(const string &strUrl) { - auto schema = FindField(strUrl.data(), nullptr, "://"); + auto schema = findSubString(strUrl.data(), nullptr, "://"); bool is_ssl = strcasecmp(schema.data(), "rtsps") == 0; // 查找"://"与"/"之间的字符串,用于提取用户名密码 - auto middle_url = FindField(strUrl.data(), "://", "/"); + auto middle_url = findSubString(strUrl.data(), "://", "/"); if (middle_url.empty()) { - middle_url = FindField(strUrl.data(), "://", nullptr); + middle_url = findSubString(strUrl.data(), "://", nullptr); } auto pos = middle_url.rfind('@'); if (pos == string::npos) { @@ -273,15 +274,15 @@ void RtspUrl::parse(const string &strUrl) { if (user_pwd.find(":") == string::npos) { return setup(is_ssl, url, user_pwd, ""); } - auto user = FindField(user_pwd.data(), nullptr, ":"); - auto pwd = FindField(user_pwd.data(), ":", nullptr); + auto user = findSubString(user_pwd.data(), nullptr, ":"); + auto pwd = findSubString(user_pwd.data(), ":", nullptr); return setup(is_ssl, url, user, pwd); } void RtspUrl::setup(bool is_ssl, const string &url, const string &user, const string &passwd) { - auto ip = FindField(url.data(), "://", "/"); + auto ip = findSubString(url.data(), "://", "/"); if (ip.empty()) { - ip = split(FindField(url.data(), "://", NULL), "?")[0]; + ip = split(findSubString(url.data(), "://", NULL), "?")[0]; } uint16_t port = is_ssl ? 322 : 554; splitUrl(ip, ip, port); diff --git a/src/Common/Parser.h b/src/Common/Parser.h index 3d81cea3..3741f4bc 100644 --- a/src/Common/Parser.h +++ b/src/Common/Parser.h @@ -18,7 +18,7 @@ namespace mediakit { // 从字符串中提取子字符串 -std::string FindField(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/Extension/Factory.cpp b/src/Extension/Factory.cpp index c85f920c..4e0b795c 100644 --- a/src/Extension/Factory.cpp +++ b/src/Extension/Factory.cpp @@ -45,9 +45,9 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) { case CodecOpus : return std::make_shared(); case CodecAAC : { - string aac_cfg_str = FindField(track->_fmtp.data(), "config=", ";"); + string aac_cfg_str = findSubString(track->_fmtp.data(), "config=", ";"); if (aac_cfg_str.empty()) { - aac_cfg_str = FindField(track->_fmtp.data(), "config=", nullptr); + aac_cfg_str = findSubString(track->_fmtp.data(), "config=", nullptr); } if (aac_cfg_str.empty()) { //如果sdp中获取不到aac config信息,那么在rtp也无法获取,那么忽略该Track @@ -67,8 +67,8 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) { //a=fmtp:96 packetization-mode=1;profile-level-id=42C01F;sprop-parameter-sets=Z0LAH9oBQBboQAAAAwBAAAAPI8YMqA==,aM48gA== auto map = Parser::parseArgs(track->_fmtp, ";", "="); auto sps_pps = map["sprop-parameter-sets"]; - string base64_SPS = FindField(sps_pps.data(), NULL, ","); - string base64_PPS = FindField(sps_pps.data(), ",", NULL); + string base64_SPS = findSubString(sps_pps.data(), NULL, ","); + string base64_PPS = findSubString(sps_pps.data(), ",", NULL); auto sps = decodeBase64(base64_SPS); auto pps = decodeBase64(base64_PPS); if (sps.empty() || pps.empty()) { diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 99581ea9..328872e6 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -21,7 +21,7 @@ namespace mediakit { void HttpClient::sendRequest(const string &url) { clearResponse(); _url = url; - auto protocol = FindField(url.data(), NULL, "://"); + auto protocol = findSubString(url.data(), NULL, "://"); uint16_t port; bool is_https; if (strcasecmp(protocol.data(), "http") == 0) { @@ -35,11 +35,11 @@ void HttpClient::sendRequest(const string &url) { throw std::invalid_argument(strErr); } - auto host = FindField(url.data(), "://", "/"); + auto host = findSubString(url.data(), "://", "/"); if (host.empty()) { - host = FindField(url.data(), "://", NULL); + host = findSubString(url.data(), "://", NULL); } - _path = FindField(url.data(), host.data(), NULL); + _path = findSubString(url.data(), host.data(), NULL); if (_path.empty()) { _path = "/"; } @@ -361,8 +361,8 @@ void HttpClient::checkCookie(HttpClient::HttpHeader &headers) { int index = 0; auto arg_vec = split(it_set_cookie->second, ";"); for (string &key_val : arg_vec) { - auto key = FindField(key_val.data(), NULL, "="); - auto val = FindField(key_val.data(), "=", NULL); + auto key = findSubString(key_val.data(), NULL, "="); + auto val = findSubString(key_val.data(), "=", NULL); if (index++ == 0) { cookie->setKeyVal(key, val); diff --git a/src/Http/HttpCookieManager.cpp b/src/Http/HttpCookieManager.cpp index 2ce0f188..b846a3b9 100644 --- a/src/Http/HttpCookieManager.cpp +++ b/src/Http/HttpCookieManager.cpp @@ -158,9 +158,9 @@ HttpServerCookie::Ptr HttpCookieManager::getCookie(const string &cookie_name, co if (it == http_header.end()) { return nullptr; } - auto cookie = FindField(it->second.data(), (cookie_name + "=").data(), ";"); + auto cookie = findSubString(it->second.data(), (cookie_name + "=").data(), ";"); if (cookie.empty()) { - cookie = FindField(it->second.data(), (cookie_name + "=").data(), nullptr); + cookie = findSubString(it->second.data(), (cookie_name + "=").data(), nullptr); } if (cookie.empty()) { return nullptr; diff --git a/src/Http/HttpFileManager.cpp b/src/Http/HttpFileManager.cpp index 9a22e64f..af032597 100644 --- a/src/Http/HttpFileManager.cpp +++ b/src/Http/HttpFileManager.cpp @@ -600,8 +600,8 @@ void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader, if (!strRange.empty()) { //分节下载 code = 206; - auto iRangeStart = atoll(FindField(strRange.data(), "bytes=", "-").data()); - auto iRangeEnd = atoll(FindField(strRange.data(), "-", nullptr).data()); + auto iRangeStart = atoll(findSubString(strRange.data(), "bytes=", "-").data()); + auto iRangeEnd = atoll(findSubString(strRange.data(), "-", nullptr).data()); auto fileSize = fileBody->remainSize(); if (iRangeEnd == 0) { iRangeEnd = fileSize - 1; diff --git a/src/Player/PlayerBase.cpp b/src/Player/PlayerBase.cpp index 31d8e051..cb00a0da 100644 --- a/src/Player/PlayerBase.cpp +++ b/src/Player/PlayerBase.cpp @@ -28,7 +28,7 @@ PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller, const s ptr->teardown(); }; string url = url_in; - string prefix = FindField(url.data(), NULL, "://"); + string prefix = findSubString(url.data(), NULL, "://"); auto pos = url.find('?'); if (pos != string::npos) { //去除?后面的字符串 diff --git a/src/Pusher/PusherBase.cpp b/src/Pusher/PusherBase.cpp index fa0d68e8..eddbf7b8 100644 --- a/src/Pusher/PusherBase.cpp +++ b/src/Pusher/PusherBase.cpp @@ -26,7 +26,7 @@ PusherBase::Ptr PusherBase::createPusher(const EventPoller::Ptr &poller, }); ptr->teardown(); }; - std::string prefix = FindField(url.data(), NULL, "://"); + std::string prefix = findSubString(url.data(), NULL, "://"); if (strcasecmp("rtsps",prefix.data()) == 0) { return PusherBase::Ptr(new TcpClientWithSSL(poller, std::dynamic_pointer_cast(src)), releasePusher); diff --git a/src/Rtmp/RtmpPlayer.cpp b/src/Rtmp/RtmpPlayer.cpp index 71e3ecb9..1c2b335c 100644 --- a/src/Rtmp/RtmpPlayer.cpp +++ b/src/Rtmp/RtmpPlayer.cpp @@ -52,14 +52,14 @@ void RtmpPlayer::teardown() { void RtmpPlayer::play(const string &url) { teardown(); - string host_url = FindField(url.data(), "://", "/"); + string host_url = findSubString(url.data(), "://", "/"); { auto pos = url.find_last_of('/'); if (pos != string::npos) { _stream_id = url.substr(pos + 1); } } - _app = FindField(url.data(), (host_url + "/").data(), ("/" + _stream_id).data()); + _app = findSubString(url.data(), (host_url + "/").data(), ("/" + _stream_id).data()); _tc_url = string("rtmp://") + host_url + "/" + _app; if (!_app.size() || !_stream_id.size()) { diff --git a/src/Rtmp/RtmpPusher.cpp b/src/Rtmp/RtmpPusher.cpp index 9379d015..e8c55f70 100644 --- a/src/Rtmp/RtmpPusher.cpp +++ b/src/Rtmp/RtmpPusher.cpp @@ -65,9 +65,9 @@ void RtmpPusher::onPublishResult_l(const SockException &ex, bool handshake_done) void RtmpPusher::publish(const string &url) { teardown(); - string host_url = FindField(url.data(), "://", "/"); - _app = FindField(url.data(), (host_url + "/").data(), "/"); - _stream_id = FindField(url.data(), (host_url + "/" + _app + "/").data(), NULL); + 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; if (!_app.size() || !_stream_id.size()) { diff --git a/src/Rtsp/Rtsp.cpp b/src/Rtsp/Rtsp.cpp index b80a88f7..2e3618da 100644 --- a/src/Rtsp/Rtsp.cpp +++ b/src/Rtsp/Rtsp.cpp @@ -180,11 +180,11 @@ void SdpParser::load(const string &sdp) { break; } case 'a': { - string attr = FindField(opt_val.data(), nullptr, ":"); + string attr = findSubString(opt_val.data(), nullptr, ":"); if (attr.empty()) { track->_attr.emplace(opt_val, ""); } else { - track->_attr.emplace(attr, FindField(opt_val.data(), ":", nullptr)); + track->_attr.emplace(attr, findSubString(opt_val.data(), ":", nullptr)); } break; } @@ -245,7 +245,7 @@ void SdpParser::load(const string &sdp) { it = track._attr.erase(it); continue; } - track._fmtp = FindField(fmtp.data(), " ", nullptr); + track._fmtp = findSubString(fmtp.data(), " ", nullptr); ++it; } diff --git a/src/Rtsp/RtspPlayer.cpp b/src/Rtsp/RtspPlayer.cpp index a58880d3..0dafa03a 100644 --- a/src/Rtsp/RtspPlayer.cpp +++ b/src/Rtsp/RtspPlayer.cpp @@ -273,7 +273,7 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) { if (track_idx == 0) { _session_id = parser["Session"]; _session_id.append(";"); - _session_id = FindField(_session_id.data(), nullptr, ";"); + _session_id = findSubString(_session_id.data(), nullptr, ";"); } auto strTransport = parser["Transport"]; @@ -469,7 +469,7 @@ void RtspPlayer::handleResPAUSE(const Parser &parser, int type) { // 修正时间轴 auto strRange = parser["Range"]; if (strRange.size()) { - auto strStart = FindField(strRange.data(), "npt=", "-"); + auto strStart = findSubString(strRange.data(), "npt=", "-"); if (strStart == "now") { strStart = "0"; } diff --git a/src/Rtsp/RtspPusher.cpp b/src/Rtsp/RtspPusher.cpp index e0855b19..4748fb15 100644 --- a/src/Rtsp/RtspPusher.cpp +++ b/src/Rtsp/RtspPusher.cpp @@ -291,13 +291,13 @@ void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) { if (track_idx == 0) { _session_id = parser["Session"]; _session_id.append(";"); - _session_id = FindField(_session_id.data(), nullptr, ";"); + _session_id = findSubString(_session_id.data(), nullptr, ";"); } auto transport = parser["Transport"]; if (transport.find("TCP") != string::npos || transport.find("interleaved") != string::npos) { _rtp_type = Rtsp::RTP_TCP; - string interleaved = FindField(FindField((transport + ";").data(), "interleaved=", ";").data(), NULL, "-"); + string interleaved = findSubString(findSubString((transport + ";").data(), "interleaved=", ";").data(), NULL, "-"); _track_vec[track_idx]->_interleaved = atoi(interleaved.data()); } else if (transport.find("multicast") != string::npos) { throw std::runtime_error("SETUP rtsp pusher can not support multicast!"); @@ -305,9 +305,9 @@ void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) { _rtp_type = Rtsp::RTP_UDP; createUdpSockIfNecessary(track_idx); const char *strPos = "server_port="; - auto port_str = FindField((transport + ";").data(), strPos, ";"); - uint16_t rtp_port = atoi(FindField(port_str.data(), NULL, "-").data()); - uint16_t rtcp_port = atoi(FindField(port_str.data(), "-", NULL).data()); + auto port_str = findSubString((transport + ";").data(), strPos, ";"); + uint16_t rtp_port = atoi(findSubString(port_str.data(), NULL, "-").data()); + uint16_t rtcp_port = atoi(findSubString(port_str.data(), "-", NULL).data()); auto &rtp_sock = _rtp_sock[track_idx]; auto &rtcp_sock = _rtcp_sock[track_idx]; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index 33c59283..15db5aa8 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -595,8 +595,8 @@ void RtspSession::onAuthUser(const string &realm,const string &authorization){ return; } //请求中包含认证信息 - auto authType = FindField(authorization.data(),NULL," "); - auto authStr = FindField(authorization.data()," ",NULL); + auto authType = findSubString(authorization.data(), NULL, " "); + auto authStr = findSubString(authorization.data(), " ", NULL); if(authType.empty() || authStr.empty()){ //认证信息格式不合法,回复401 Unauthorized onAuthFailed(realm,"can not find auth type or auth string"); @@ -690,9 +690,9 @@ void RtspSession::handleReq_Setup(const Parser &parser) { _rtcp_socks[trackIdx] = pr.second; //设置客户端内网端口信息 - string strClientPort = FindField(parser["Transport"].data(), "client_port=", NULL); - uint16_t ui16RtpPort = atoi(FindField(strClientPort.data(), NULL, "-").data()); - uint16_t ui16RtcpPort = atoi(FindField(strClientPort.data(), "-", NULL).data()); + string strClientPort = findSubString(parser["Transport"].data(), "client_port=", NULL); + uint16_t ui16RtpPort = atoi(findSubString(strClientPort.data(), NULL, "-").data()); + uint16_t ui16RtcpPort = atoi(findSubString(strClientPort.data(), "-", NULL).data()); auto peerAddr = SockUtil::make_sockaddr(get_peer_ip().data(), ui16RtpPort); //设置rtp发送目标地址 @@ -785,7 +785,7 @@ void RtspSession::handleReq_Play(const Parser &parser) { if (!strRange.empty()) { //这是seek操作 res_header.emplace("Range", strRange); - auto strStart = FindField(strRange.data(), "npt=", "-"); + auto strStart = findSubString(strRange.data(), "npt=", "-"); if (strStart == "now") { strStart = "0"; } diff --git a/tests/test_bench_push.cpp b/tests/test_bench_push.cpp index d0929a8e..b34f8b99 100644 --- a/tests/test_bench_push.cpp +++ b/tests/test_bench_push.cpp @@ -124,7 +124,7 @@ int main(int argc, char *argv[]) { auto delay_ms = cmd_main["delay"].as(); auto pusher_count = cmd_main["count"].as(); auto merge_ms = cmd_main["merge"].as(); - auto schema = FindField(out_url.data(), nullptr, "://"); + auto schema = findSubString(out_url.data(), nullptr, "://"); if (schema != RTSP_SCHEMA && schema != RTMP_SCHEMA) { cout << "推流协议只支持rtsp或rtmp!" << endl; return -1; diff --git a/tests/test_pusherMp4.cpp b/tests/test_pusherMp4.cpp index ef338344..c28845d4 100644 --- a/tests/test_pusherMp4.cpp +++ b/tests/test_pusherMp4.cpp @@ -114,7 +114,7 @@ int domain(const string &filePath, const string &pushUrl) { auto poller = EventPollerPool::Instance().getPoller(); //vhost/app/stream可以随便自己填,现在不限制app应用名了 - createPusher(poller, FindField(pushUrl.data(), nullptr, "://").substr(0, 4), DEFAULT_VHOST, "live", "stream", filePath, pushUrl); + createPusher(poller, findSubString(pushUrl.data(), nullptr, "://").substr(0, 4), DEFAULT_VHOST, "live", "stream", filePath, pushUrl); //设置退出信号处理函数 static semaphore sem; signal(SIGINT, [](int) { sem.post(); });// 设置退出信号