优化函数命名风格

FindField改名为findSubString
This commit is contained in:
xia-chu 2023-06-10 12:28:49 +08:00 committed by 夏楚
parent 31944a92ad
commit 64b8079ac1
17 changed files with 51 additions and 50 deletions

View File

@ -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)) { if (atoll(tm.data()) + expire_sec < time(NULL)) {
//截图已经过期,改名,以便再次请求时,可以返回老截图 //截图已经过期,改名,以便再次请求时,可以返回老截图
rename(path.data(), new_snap.data()); rename(path.data(), new_snap.data());

View File

@ -19,11 +19,12 @@ using namespace toolkit;
namespace mediakit { 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) { if (buf_size <= 0) {
buf_size = strlen(buf); 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; size_t len = 0;
if (start != NULL) { if (start != NULL) {
len = strlen(start); len = strlen(start);
@ -253,12 +254,12 @@ std::string Parser::mergeUrl(const string &base_url, const string &path) {
} }
void RtspUrl::parse(const string &strUrl) { 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; bool is_ssl = strcasecmp(schema.data(), "rtsps") == 0;
// 查找"://"与"/"之间的字符串,用于提取用户名密码 // 查找"://"与"/"之间的字符串,用于提取用户名密码
auto middle_url = FindField(strUrl.data(), "://", "/"); auto middle_url = findSubString(strUrl.data(), "://", "/");
if (middle_url.empty()) { if (middle_url.empty()) {
middle_url = FindField(strUrl.data(), "://", nullptr); middle_url = findSubString(strUrl.data(), "://", nullptr);
} }
auto pos = middle_url.rfind('@'); auto pos = middle_url.rfind('@');
if (pos == string::npos) { if (pos == string::npos) {
@ -273,15 +274,15 @@ void RtspUrl::parse(const string &strUrl) {
if (user_pwd.find(":") == string::npos) { if (user_pwd.find(":") == string::npos) {
return setup(is_ssl, url, user_pwd, ""); return setup(is_ssl, url, user_pwd, "");
} }
auto user = FindField(user_pwd.data(), nullptr, ":"); auto user = findSubString(user_pwd.data(), nullptr, ":");
auto pwd = FindField(user_pwd.data(), ":", nullptr); auto pwd = findSubString(user_pwd.data(), ":", nullptr);
return setup(is_ssl, url, user, pwd); return setup(is_ssl, url, user, pwd);
} }
void RtspUrl::setup(bool is_ssl, const string &url, const string &user, const string &passwd) { 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()) { if (ip.empty()) {
ip = split(FindField(url.data(), "://", NULL), "?")[0]; ip = split(findSubString(url.data(), "://", NULL), "?")[0];
} }
uint16_t port = is_ssl ? 322 : 554; uint16_t port = is_ssl ? 322 : 554;
splitUrl(ip, ip, port); splitUrl(ip, ip, port);

View File

@ -18,7 +18,7 @@
namespace mediakit { 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 // 把url解析为主机地址和端口号,兼容ipv4/ipv6/dns
void splitUrl(const std::string &url, std::string &host, uint16_t &port); void splitUrl(const std::string &url, std::string &host, uint16_t &port);

View File

@ -45,9 +45,9 @@ Track::Ptr Factory::getTrackBySdp(const SdpTrack::Ptr &track) {
case CodecOpus : return std::make_shared<OpusTrack>(); case CodecOpus : return std::make_shared<OpusTrack>();
case CodecAAC : { 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()) { 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()) { if (aac_cfg_str.empty()) {
//如果sdp中获取不到aac config信息那么在rtp也无法获取那么忽略该Track //如果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== //a=fmtp:96 packetization-mode=1;profile-level-id=42C01F;sprop-parameter-sets=Z0LAH9oBQBboQAAAAwBAAAAPI8YMqA==,aM48gA==
auto map = Parser::parseArgs(track->_fmtp, ";", "="); auto map = Parser::parseArgs(track->_fmtp, ";", "=");
auto sps_pps = map["sprop-parameter-sets"]; auto sps_pps = map["sprop-parameter-sets"];
string base64_SPS = FindField(sps_pps.data(), NULL, ","); string base64_SPS = findSubString(sps_pps.data(), NULL, ",");
string base64_PPS = FindField(sps_pps.data(), ",", NULL); string base64_PPS = findSubString(sps_pps.data(), ",", NULL);
auto sps = decodeBase64(base64_SPS); auto sps = decodeBase64(base64_SPS);
auto pps = decodeBase64(base64_PPS); auto pps = decodeBase64(base64_PPS);
if (sps.empty() || pps.empty()) { if (sps.empty() || pps.empty()) {

View File

@ -21,7 +21,7 @@ namespace mediakit {
void HttpClient::sendRequest(const string &url) { void HttpClient::sendRequest(const string &url) {
clearResponse(); clearResponse();
_url = url; _url = url;
auto protocol = FindField(url.data(), NULL, "://"); auto protocol = findSubString(url.data(), NULL, "://");
uint16_t port; uint16_t port;
bool is_https; bool is_https;
if (strcasecmp(protocol.data(), "http") == 0) { if (strcasecmp(protocol.data(), "http") == 0) {
@ -35,11 +35,11 @@ void HttpClient::sendRequest(const string &url) {
throw std::invalid_argument(strErr); throw std::invalid_argument(strErr);
} }
auto host = FindField(url.data(), "://", "/"); auto host = findSubString(url.data(), "://", "/");
if (host.empty()) { 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()) { if (_path.empty()) {
_path = "/"; _path = "/";
} }
@ -361,8 +361,8 @@ void HttpClient::checkCookie(HttpClient::HttpHeader &headers) {
int index = 0; int index = 0;
auto arg_vec = split(it_set_cookie->second, ";"); auto arg_vec = split(it_set_cookie->second, ";");
for (string &key_val : arg_vec) { for (string &key_val : arg_vec) {
auto key = FindField(key_val.data(), NULL, "="); auto key = findSubString(key_val.data(), NULL, "=");
auto val = FindField(key_val.data(), "=", NULL); auto val = findSubString(key_val.data(), "=", NULL);
if (index++ == 0) { if (index++ == 0) {
cookie->setKeyVal(key, val); cookie->setKeyVal(key, val);

View File

@ -158,9 +158,9 @@ HttpServerCookie::Ptr HttpCookieManager::getCookie(const string &cookie_name, co
if (it == http_header.end()) { if (it == http_header.end()) {
return nullptr; return nullptr;
} }
auto cookie = FindField(it->second.data(), (cookie_name + "=").data(), ";"); auto cookie = findSubString(it->second.data(), (cookie_name + "=").data(), ";");
if (cookie.empty()) { if (cookie.empty()) {
cookie = FindField(it->second.data(), (cookie_name + "=").data(), nullptr); cookie = findSubString(it->second.data(), (cookie_name + "=").data(), nullptr);
} }
if (cookie.empty()) { if (cookie.empty()) {
return nullptr; return nullptr;

View File

@ -600,8 +600,8 @@ void HttpResponseInvokerImp::responseFile(const StrCaseMap &requestHeader,
if (!strRange.empty()) { if (!strRange.empty()) {
//分节下载 //分节下载
code = 206; code = 206;
auto iRangeStart = atoll(FindField(strRange.data(), "bytes=", "-").data()); auto iRangeStart = atoll(findSubString(strRange.data(), "bytes=", "-").data());
auto iRangeEnd = atoll(FindField(strRange.data(), "-", nullptr).data()); auto iRangeEnd = atoll(findSubString(strRange.data(), "-", nullptr).data());
auto fileSize = fileBody->remainSize(); auto fileSize = fileBody->remainSize();
if (iRangeEnd == 0) { if (iRangeEnd == 0) {
iRangeEnd = fileSize - 1; iRangeEnd = fileSize - 1;

View File

@ -28,7 +28,7 @@ PlayerBase::Ptr PlayerBase::createPlayer(const EventPoller::Ptr &poller, const s
ptr->teardown(); ptr->teardown();
}; };
string url = url_in; string url = url_in;
string prefix = FindField(url.data(), NULL, "://"); string prefix = findSubString(url.data(), NULL, "://");
auto pos = url.find('?'); auto pos = url.find('?');
if (pos != string::npos) { if (pos != string::npos) {
//去除?后面的字符串 //去除?后面的字符串

View File

@ -26,7 +26,7 @@ PusherBase::Ptr PusherBase::createPusher(const EventPoller::Ptr &poller,
}); });
ptr->teardown(); ptr->teardown();
}; };
std::string prefix = FindField(url.data(), NULL, "://"); std::string prefix = findSubString(url.data(), NULL, "://");
if (strcasecmp("rtsps",prefix.data()) == 0) { if (strcasecmp("rtsps",prefix.data()) == 0) {
return PusherBase::Ptr(new TcpClientWithSSL<RtspPusherImp>(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), releasePusher); return PusherBase::Ptr(new TcpClientWithSSL<RtspPusherImp>(poller, std::dynamic_pointer_cast<RtspMediaSource>(src)), releasePusher);

View File

@ -52,14 +52,14 @@ void RtmpPlayer::teardown() {
void RtmpPlayer::play(const string &url) { void RtmpPlayer::play(const string &url) {
teardown(); teardown();
string host_url = FindField(url.data(), "://", "/"); string host_url = findSubString(url.data(), "://", "/");
{ {
auto pos = url.find_last_of('/'); auto pos = url.find_last_of('/');
if (pos != string::npos) { if (pos != string::npos) {
_stream_id = url.substr(pos + 1); _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; _tc_url = string("rtmp://") + host_url + "/" + _app;
if (!_app.size() || !_stream_id.size()) { if (!_app.size() || !_stream_id.size()) {

View File

@ -65,9 +65,9 @@ void RtmpPusher::onPublishResult_l(const SockException &ex, bool handshake_done)
void RtmpPusher::publish(const string &url) { void RtmpPusher::publish(const string &url) {
teardown(); teardown();
string host_url = FindField(url.data(), "://", "/"); string host_url = findSubString(url.data(), "://", "/");
_app = FindField(url.data(), (host_url + "/").data(), "/"); _app = findSubString(url.data(), (host_url + "/").data(), "/");
_stream_id = FindField(url.data(), (host_url + "/" + _app + "/").data(), NULL); _stream_id = findSubString(url.data(), (host_url + "/" + _app + "/").data(), NULL);
_tc_url = string("rtmp://") + host_url + "/" + _app; _tc_url = string("rtmp://") + host_url + "/" + _app;
if (!_app.size() || !_stream_id.size()) { if (!_app.size() || !_stream_id.size()) {

View File

@ -180,11 +180,11 @@ void SdpParser::load(const string &sdp) {
break; break;
} }
case 'a': { case 'a': {
string attr = FindField(opt_val.data(), nullptr, ":"); string attr = findSubString(opt_val.data(), nullptr, ":");
if (attr.empty()) { if (attr.empty()) {
track->_attr.emplace(opt_val, ""); track->_attr.emplace(opt_val, "");
} else { } else {
track->_attr.emplace(attr, FindField(opt_val.data(), ":", nullptr)); track->_attr.emplace(attr, findSubString(opt_val.data(), ":", nullptr));
} }
break; break;
} }
@ -245,7 +245,7 @@ void SdpParser::load(const string &sdp) {
it = track._attr.erase(it); it = track._attr.erase(it);
continue; continue;
} }
track._fmtp = FindField(fmtp.data(), " ", nullptr); track._fmtp = findSubString(fmtp.data(), " ", nullptr);
++it; ++it;
} }

View File

@ -273,7 +273,7 @@ void RtspPlayer::handleResSETUP(const Parser &parser, unsigned int track_idx) {
if (track_idx == 0) { if (track_idx == 0) {
_session_id = parser["Session"]; _session_id = parser["Session"];
_session_id.append(";"); _session_id.append(";");
_session_id = FindField(_session_id.data(), nullptr, ";"); _session_id = findSubString(_session_id.data(), nullptr, ";");
} }
auto strTransport = parser["Transport"]; auto strTransport = parser["Transport"];
@ -469,7 +469,7 @@ void RtspPlayer::handleResPAUSE(const Parser &parser, int type) {
// 修正时间轴 // 修正时间轴
auto strRange = parser["Range"]; auto strRange = parser["Range"];
if (strRange.size()) { if (strRange.size()) {
auto strStart = FindField(strRange.data(), "npt=", "-"); auto strStart = findSubString(strRange.data(), "npt=", "-");
if (strStart == "now") { if (strStart == "now") {
strStart = "0"; strStart = "0";
} }

View File

@ -291,13 +291,13 @@ void RtspPusher::handleResSetup(const Parser &parser, unsigned int track_idx) {
if (track_idx == 0) { if (track_idx == 0) {
_session_id = parser["Session"]; _session_id = parser["Session"];
_session_id.append(";"); _session_id.append(";");
_session_id = FindField(_session_id.data(), nullptr, ";"); _session_id = findSubString(_session_id.data(), nullptr, ";");
} }
auto transport = parser["Transport"]; auto transport = parser["Transport"];
if (transport.find("TCP") != string::npos || transport.find("interleaved") != string::npos) { if (transport.find("TCP") != string::npos || transport.find("interleaved") != string::npos) {
_rtp_type = Rtsp::RTP_TCP; _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()); _track_vec[track_idx]->_interleaved = atoi(interleaved.data());
} else if (transport.find("multicast") != string::npos) { } else if (transport.find("multicast") != string::npos) {
throw std::runtime_error("SETUP rtsp pusher can not support multicast!"); 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; _rtp_type = Rtsp::RTP_UDP;
createUdpSockIfNecessary(track_idx); createUdpSockIfNecessary(track_idx);
const char *strPos = "server_port="; const char *strPos = "server_port=";
auto port_str = FindField((transport + ";").data(), strPos, ";"); auto port_str = findSubString((transport + ";").data(), strPos, ";");
uint16_t rtp_port = atoi(FindField(port_str.data(), NULL, "-").data()); uint16_t rtp_port = atoi(findSubString(port_str.data(), NULL, "-").data());
uint16_t rtcp_port = atoi(FindField(port_str.data(), "-", NULL).data()); uint16_t rtcp_port = atoi(findSubString(port_str.data(), "-", NULL).data());
auto &rtp_sock = _rtp_sock[track_idx]; auto &rtp_sock = _rtp_sock[track_idx];
auto &rtcp_sock = _rtcp_sock[track_idx]; auto &rtcp_sock = _rtcp_sock[track_idx];

View File

@ -595,8 +595,8 @@ void RtspSession::onAuthUser(const string &realm,const string &authorization){
return; return;
} }
//请求中包含认证信息 //请求中包含认证信息
auto authType = FindField(authorization.data(),NULL," "); auto authType = findSubString(authorization.data(), NULL, " ");
auto authStr = FindField(authorization.data()," ",NULL); auto authStr = findSubString(authorization.data(), " ", NULL);
if(authType.empty() || authStr.empty()){ if(authType.empty() || authStr.empty()){
//认证信息格式不合法回复401 Unauthorized //认证信息格式不合法回复401 Unauthorized
onAuthFailed(realm,"can not find auth type or auth string"); 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; _rtcp_socks[trackIdx] = pr.second;
//设置客户端内网端口信息 //设置客户端内网端口信息
string strClientPort = FindField(parser["Transport"].data(), "client_port=", NULL); string strClientPort = findSubString(parser["Transport"].data(), "client_port=", NULL);
uint16_t ui16RtpPort = atoi(FindField(strClientPort.data(), NULL, "-").data()); uint16_t ui16RtpPort = atoi(findSubString(strClientPort.data(), NULL, "-").data());
uint16_t ui16RtcpPort = atoi(FindField(strClientPort.data(), "-", NULL).data()); uint16_t ui16RtcpPort = atoi(findSubString(strClientPort.data(), "-", NULL).data());
auto peerAddr = SockUtil::make_sockaddr(get_peer_ip().data(), ui16RtpPort); auto peerAddr = SockUtil::make_sockaddr(get_peer_ip().data(), ui16RtpPort);
//设置rtp发送目标地址 //设置rtp发送目标地址
@ -785,7 +785,7 @@ void RtspSession::handleReq_Play(const Parser &parser) {
if (!strRange.empty()) { if (!strRange.empty()) {
//这是seek操作 //这是seek操作
res_header.emplace("Range", strRange); res_header.emplace("Range", strRange);
auto strStart = FindField(strRange.data(), "npt=", "-"); auto strStart = findSubString(strRange.data(), "npt=", "-");
if (strStart == "now") { if (strStart == "now") {
strStart = "0"; strStart = "0";
} }

View File

@ -124,7 +124,7 @@ int main(int argc, char *argv[]) {
auto delay_ms = cmd_main["delay"].as<int>(); auto delay_ms = cmd_main["delay"].as<int>();
auto pusher_count = cmd_main["count"].as<int>(); auto pusher_count = cmd_main["count"].as<int>();
auto merge_ms = cmd_main["merge"].as<int>(); auto merge_ms = cmd_main["merge"].as<int>();
auto schema = FindField(out_url.data(), nullptr, "://"); auto schema = findSubString(out_url.data(), nullptr, "://");
if (schema != RTSP_SCHEMA && schema != RTMP_SCHEMA) { if (schema != RTSP_SCHEMA && schema != RTMP_SCHEMA) {
cout << "推流协议只支持rtsp或rtmp" << endl; cout << "推流协议只支持rtsp或rtmp" << endl;
return -1; return -1;

View File

@ -114,7 +114,7 @@ int domain(const string &filePath, const string &pushUrl) {
auto poller = EventPollerPool::Instance().getPoller(); auto poller = EventPollerPool::Instance().getPoller();
//vhost/app/stream可以随便自己填现在不限制app应用名了 //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; static semaphore sem;
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 signal(SIGINT, [](int) { sem.post(); });// 设置退出信号