From 06e7a20b5fd03b2d37bf6a2c0b534a79cac6c43f Mon Sep 17 00:00:00 2001 From: runner365 Date: Thu, 23 Jan 2020 17:19:06 +0800 Subject: [PATCH] update streamid decode --- trunk/src/srt/srt_conn.cpp | 87 ++++++++++++++++++++++++++++++++++++ trunk/src/srt/srt_conn.hpp | 63 ++------------------------ trunk/src/srt/srt_handle.cpp | 7 ++- 3 files changed, 96 insertions(+), 61 deletions(-) diff --git a/trunk/src/srt/srt_conn.cpp b/trunk/src/srt/srt_conn.cpp index ab4d0584ab..2df80b011f 100644 --- a/trunk/src/srt/srt_conn.cpp +++ b/trunk/src/srt/srt_conn.cpp @@ -3,6 +3,93 @@ #include "stringex.hpp" #include +bool is_streamid_valid(const std::string& streamid) { + int mode = ERR_SRT_MODE; + std::string url_subpash; + + bool ret = get_streamid_info(streamid, mode, url_subpash); + if (!ret) { + return ret; + } + + if ((mode != PULL_SRT_MODE) && (mode != PUSH_SRT_MODE)) { + return false; + } + + if (url_subpash.empty()) { + return false; + } + + std::vector info_vec; + string_split(url_subpash, "/", info_vec); + if (info_vec.size() < 2) { + return false; + } + + return true; +} + +bool get_key_value(const std::string& info, std::string& key, std::string& value) { + size_t pos = info.find("="); + + if (pos == info.npos) { + return false; + } + + key = info.substr(0, pos); + value = info.substr(pos+1); + + if (key.empty() || value.empty()) { + return false; + } + return true; +} + +//eg. streamid=#!::h:live/livestream,m:publish +bool get_streamid_info(const std::string& streamid, int& mode, std::string& url_subpash) { + std::vector info_vec; + std::string real_streamid; + + size_t pos = streamid.find("#!::h"); + if (pos != 0) { + return false; + } + real_streamid = streamid.substr(4); + + string_split(real_streamid, ",", info_vec); + if (info_vec.size() < 2) { + return false; + } + + for (int index = 0; index < info_vec.size(); index++) { + std::string key; + std::string value; + + bool ret = get_key_value(info_vec[index], key, value); + if (!ret) { + continue; + } + + if (key == "h") { + url_subpash = value;//eg. h=live/stream + } else if (key == "m") { + std::string mode_str = string_lower(value);//m=publish or m=request + if (mode_str == "publish") { + mode = PUSH_SRT_MODE; + } else if (mode_str == "request") { + mode = PULL_SRT_MODE; + } else { + mode = ERR_SRT_MODE; + return false; + } + } else {//not suport + continue; + } + } + + return true; +} + srt_conn::srt_conn(SRTSOCKET conn_fd, const std::string& streamid):_conn_fd(conn_fd), _streamid(streamid) { get_streamid_info(streamid, _mode, _url_subpath); diff --git a/trunk/src/srt/srt_conn.hpp b/trunk/src/srt/srt_conn.hpp index 9c46d05bda..adc79eaf47 100644 --- a/trunk/src/srt/srt_conn.hpp +++ b/trunk/src/srt/srt_conn.hpp @@ -15,66 +15,9 @@ #define PULL_SRT_MODE 0x01 #define PUSH_SRT_MODE 0x02 -inline bool is_streamid_valid(const std::string& streamid) { - bool ret = false; - if (streamid.empty()) { - return ret; - } - //check whether subpath is valid, eg. live/test or srs.srt.com.cn/live/test - std::vector ret_vec; - string_split(streamid, "/", ret_vec); - if (ret_vec.size() < 2) { - return ret; - } - - //check whether mode is valid, eg. live/test?m=push or live/test?m=pull - size_t pos = streamid.find("?"); - if (pos == streamid.npos) { - return ret; - } - - std::string mode = streamid.substr(pos+1); - string_split(mode, "=", ret_vec); - if (ret_vec.size() != 2) { - return ret; - } - //it must be m=push or m=pull - std::string mode_oper = string_lower(ret_vec[1]); - if ((ret_vec[0] != "m") || ((mode_oper != "push") && (mode_oper != "pull"))) { - return ret; - } - ret = true; - return ret; -} - -inline void get_streamid_info(const std::string& streamid, int& mode, std::string& url_subpash) { - std::string real_streamip; - - size_t pos = streamid.find("?"); - if (pos == std::string::npos) { - mode = ERR_SRT_MODE; - url_subpash = streamid; - } else { - std::string mode_str = streamid.substr(pos+1); - - url_subpash = streamid.substr(0, pos); - - size_t mode_pos = mode_str.find("m="); - if (mode_pos == std::string::npos) { - mode = PULL_SRT_MODE; - } else { - mode_str = mode_str.substr(mode_pos+2); - mode_str = string_lower(mode_str); - if (mode_str == "push") { - mode = PUSH_SRT_MODE; - } else if(mode_str == "pull"){ - mode = PULL_SRT_MODE; - } else { - mode = ERR_SRT_MODE; - } - } - } -} +bool is_streamid_valid(const std::string& streamid); +bool get_key_value(const std::string& info, std::string& key, std::string& value); +bool get_streamid_info(const std::string& streamid, int& mode, std::string& url_subpash); class srt_conn { public: diff --git a/trunk/src/srt/srt_handle.cpp b/trunk/src/srt/srt_handle.cpp index 226013923c..ea25b2c831 100644 --- a/trunk/src/srt/srt_handle.cpp +++ b/trunk/src/srt/srt_handle.cpp @@ -421,7 +421,12 @@ void srt_handle::handle_srt_socket(SRT_SOCKSTATUS status, SRTSOCKET conn_fd) } return; } - get_streamid_info(conn_ptr->get_streamid(), mode, subpath); + bool ret = get_streamid_info(conn_ptr->get_streamid(), mode, subpath); + if (!ret) { + conn_ptr->close(); + conn_ptr = nullptr; + return; + } if (mode == PUSH_SRT_MODE) { switch (status)