From c8466c36bd3982d314c7243acb2b8b718126f2b5 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 1 Sep 2015 21:27:04 +0800 Subject: [PATCH] for #319, raw api support update the global RTMP chunk_size. --- trunk/src/app/srs_app_config.cpp | 23 ++++++++++++++++++++++ trunk/src/app/srs_app_config.hpp | 4 ++++ trunk/src/app/srs_app_http_api.cpp | 31 +++++++++++++++++++++--------- trunk/src/app/srs_app_utility.cpp | 12 ++++++++++++ trunk/src/app/srs_app_utility.hpp | 7 +++++++ 5 files changed, 68 insertions(+), 9 deletions(-) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 04aace1b1..461fe6f6b 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -2270,6 +2270,29 @@ int SrsConfig::raw_set_pid(string pid, bool& applied) return ret; } +int SrsConfig::raw_set_chunk_size(string chunk_size, bool& applied) +{ + int ret = ERROR_SUCCESS; + + applied = false; + + + SrsConfDirective* conf = root->get_or_create("chunk_size"); + + if (conf->arg0() == chunk_size) { + return ret; + } + + conf->args.clear(); + conf->args.push_back(chunk_size); + + // directly supported reload for chunk_size change. + + applied = true; + + return ret; +} + int SrsConfig::do_reload_listen() { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index 8a6308135..8b716f6b1 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -341,6 +341,10 @@ public: * raw set the global pid. */ virtual int raw_set_pid(std::string pid, bool& applied); + /** + * raw set the global chunk size. + */ + virtual int raw_set_chunk_size(std::string chunk_size, bool& applied); private: virtual int do_reload_listen(); virtual int do_reload_pid(); diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index 953958c46..741d966fc 100755 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -986,8 +986,9 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) // @param value the updated value for scope. // possible updates: // @param scope @param value value-description - // global.listen 1935,1936 the port list. - // global.pid ./objs/srs.pid the pid file of srs. + // listen 1935,1936 the port list. + // pid ./objs/srs.pid the pid file of srs. + // chunk_size 60000 the global RTMP chunk_size. if (rpc == "update") { if (!allow_update) { ret = ERROR_SYSTEM_CONFIG_RAW_DISABLED; @@ -997,14 +998,14 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) std::string scope = r->query_get("scope"); std::string value = r->query_get("value"); - if (scope.empty() || (scope != "global.listen" && scope != "global.pid")) { + if (scope.empty() || (scope != "listen" && scope != "pid" && scope != "chunk_size")) { ret = ERROR_SYSTEM_CONFIG_RAW_NOT_ALLOWED; srs_error("raw api query invalid scope=%s. ret=%d", scope.c_str(), ret); return srs_api_response_code(w, r, ret); } bool applied = false; - if (scope == "global.listen") { + if (scope == "listen") { vector eps = srs_string_split(value, ","); bool invalid = eps.empty(); @@ -1018,15 +1019,15 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) } if (invalid) { ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS; - srs_error("raw api update check global.listen=%s failed. ret=%d", value.c_str(), ret); + srs_error("raw api update check listen=%s failed. ret=%d", value.c_str(), ret); return srs_api_response_code(w, r, ret); } if ((ret = _srs_config->raw_set_listen(eps, applied)) != ERROR_SUCCESS) { - srs_error("raw api update global.listen=%s failed. ret=%d", value.c_str(), ret); + srs_error("raw api update listen=%s failed. ret=%d", value.c_str(), ret); return srs_api_response_code(w, r, ret); } - } else if (scope == "global.pid") { + } else if (scope == "pid") { bool invalid = value.empty(); if (!invalid) { invalid = !srs_string_starts_with(value, "./") @@ -1038,12 +1039,24 @@ int SrsGoApiRaw::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) } if (invalid) { ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS; - srs_error("raw api update check global.pid=%s failed. ret=%d", value.c_str(), ret); + srs_error("raw api update check pid=%s failed. ret=%d", value.c_str(), ret); return srs_api_response_code(w, r, ret); } if ((ret = _srs_config->raw_set_pid(value, applied)) != ERROR_SUCCESS) { - srs_error("raw api update global.pid=%s failed. ret=%d", value.c_str(), ret); + srs_error("raw api update pid=%s failed. ret=%d", value.c_str(), ret); + return srs_api_response_code(w, r, ret); + } + } else if (scope == "chunk_size") { + int csv = ::atoi(value.c_str()); + if (csv < 128 || csv > 65535 || !srs_is_digit_number(value)) { + ret = ERROR_SYSTEM_CONFIG_RAW_PARAMS; + srs_error("raw api update check chunk_size=%s/%d failed. ret=%d", value.c_str(), csv, ret); + return srs_api_response_code(w, r, ret); + } + + if ((ret = _srs_config->raw_set_chunk_size(value, applied)) != ERROR_SUCCESS) { + srs_error("raw api update chunk_size=%s/%d failed. ret=%d", value.c_str(), csv, ret); return srs_api_response_code(w, r, ret); } } diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index fdd49de0b..3fc18b092 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -35,6 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #endif #include #include +#include #include using namespace std; @@ -1355,6 +1356,17 @@ string srs_get_peer_ip(int fd) return ip; } +bool srs_is_digit_number(const string& str) +{ + if (str.empty()) { + return false; + } + + int v = ::atoi(str.c_str()); + int powv = (int)pow(10, str.length() - 1); + return v / powv >= 1 && v / powv <= 9; +} + void srs_api_dump_summaries(SrsAmf0Object* obj) { SrsRusage* r = srs_get_system_rusage(); diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index adf0ae365..c37e099b4 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -669,6 +669,13 @@ extern int srs_get_local_port(int fd); // where peer ip is the client public ip which connected to server. extern std::string srs_get_peer_ip(int fd); +// whether string is digit number +// is_digit("1234567890") === true +// is_digit("0123456789") === false +// is_digit("1234567890a") === false +// is_digit("a1234567890") === false +extern bool srs_is_digit_number(const std::string& str); + // dump summaries for /api/v1/summaries. extern void srs_api_dump_summaries(SrsAmf0Object* obj);