From cd682ae4be1e227bd07a3e15bda96c95b88513cd Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 30 Mar 2015 14:08:17 +0800 Subject: [PATCH] update donations. fix hls path bug. --- DONATIONS.txt | 1 + trunk/src/app/srs_app_hls.cpp | 79 ++++++++++--------------- trunk/src/app/srs_app_hls.hpp | 3 +- trunk/src/kernel/srs_kernel_utility.cpp | 5 ++ trunk/src/kernel/srs_kernel_utility.hpp | 2 + 5 files changed, 41 insertions(+), 49 deletions(-) diff --git a/DONATIONS.txt b/DONATIONS.txt index 0803f82d6..a28d0164b 100644 --- a/DONATIONS.txt +++ b/DONATIONS.txt @@ -13,6 +13,7 @@ RMB 500-999 * [2015-xx-xx xx:xx] xxx RMB 100-499 +* [2015-03-30 13:34] 扶凯 * [2015-03-29 11-07] 姚伟斌 * [2015-03-14 20:21] 万伟 * [2015-03-11 09:44] 叶发养 diff --git a/trunk/src/app/srs_app_hls.cpp b/trunk/src/app/srs_app_hls.cpp index c166f1540..8a2e7336d 100644 --- a/trunk/src/app/srs_app_hls.cpp +++ b/trunk/src/app/srs_app_hls.cpp @@ -215,11 +215,20 @@ int SrsHlsMuxer::update_config(SrsRequest* r, string entry_prefix, hls_entry_prefix = entry_prefix; hls_path = path; - hls_m3u8_file = m3u8_file; hls_ts_file = ts_file; hls_fragment = fragment; hls_aof_ratio = aof_ratio; hls_window = window; + + // generate the m3u8 dir and path. + m3u8 = path + "/" + m3u8_file; + m3u8 = srs_path_build_stream(m3u8, req->vhost, req->app, req->stream); + + m3u8_dir = m3u8; + size_t pos = string::npos; + if ((pos = m3u8_dir.rfind("/")) != string::npos) { + m3u8_dir = m3u8_dir.substr(0, pos); + } // we always keep the target duration increasing. int max_td = srs_max(target_duration, (int)(fragment * _srs_config->get_hls_td_ratio(r->vhost))); @@ -251,12 +260,6 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts) return ret; } - // TODO: create all parents dirs. - // create dir for app. - if (should_write_file && (ret = create_dir(current->full_path)) != ERROR_SUCCESS) { - return ret; - } - // when segment open, the current segment must be NULL. srs_assert(!current); @@ -304,16 +307,30 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts) ss << current->sequence_no; ts_file = srs_string_replace(ts_file, "[seq]", ss.str()); } - - // replace variables current->full_path = hls_path + "/" + ts_file; + // the ts url, relative or absolute url. + std::string ts_url = current->full_path; + if (srs_string_starts_with(ts_url, m3u8_dir)) { + ts_url = ts_url.substr(m3u8_dir.length()); + } + while (srs_string_starts_with(ts_url, "/")) { + ts_url = ts_url.substr(1); + } current->uri += hls_entry_prefix; if (!hls_entry_prefix.empty() && !srs_string_ends_with(hls_entry_prefix, "/")) { current->uri += "/"; } - current->uri += ts_file; + current->uri += ts_url; + + // create dir recursively for hls. + if (should_write_file && (ret = srs_create_dir_recursively(m3u8_dir)) != ERROR_SUCCESS) { + srs_error("create app dir %s failed. ret=%d", m3u8_dir.c_str(), ret); + return ret; + } + srs_info("create app dir %s ok", m3u8_dir.c_str()); + // open temp ts file. std::string tmp_file = current->full_path + ".tmp"; if ((ret = current->muxer->open(tmp_file.c_str())) != ERROR_SUCCESS) { srs_error("open hls muxer failed. ret=%d", ret); @@ -527,23 +544,16 @@ int SrsHlsMuxer::refresh_m3u8() { int ret = ERROR_SUCCESS; - std::string m3u8_file = hls_path; - m3u8_file += "/"; - m3u8_file += hls_m3u8_file; - m3u8_file = srs_path_build_stream(m3u8_file, req->vhost, req->app, req->stream); - - m3u8 = m3u8_file; - m3u8_file += ".temp"; - - if ((ret = _refresh_m3u8(m3u8_file)) == ERROR_SUCCESS) { - if (should_write_file && rename(m3u8_file.c_str(), m3u8.c_str()) < 0) { + std::string temp_m3u8 = m3u8 + ".temp"; + if ((ret = _refresh_m3u8(temp_m3u8)) == ERROR_SUCCESS) { + if (should_write_file && rename(temp_m3u8.c_str(), m3u8.c_str()) < 0) { ret = ERROR_HLS_WRITE_FAILED; - srs_error("rename m3u8 file failed. %s => %s, ret=%d", m3u8_file.c_str(), m3u8.c_str(), ret); + srs_error("rename m3u8 file failed. %s => %s, ret=%d", temp_m3u8.c_str(), m3u8.c_str(), ret); } } // remove the temp file. - unlink(m3u8_file.c_str()); + unlink(temp_m3u8.c_str()); return ret; } @@ -634,31 +644,6 @@ int SrsHlsMuxer::_refresh_m3u8(string m3u8_file) return ret; } -int SrsHlsMuxer::create_dir(string filepath) -{ - int ret = ERROR_SUCCESS; - - if (!should_write_file) { - return ret; - } - - std::string app_dir = filepath; - size_t pos = string::npos; - if ((pos = app_dir.rfind("/")) != string::npos) { - app_dir = app_dir.substr(0, pos); - } - - // TODO: cleanup the dir when startup. - - if ((ret = srs_create_dir_recursively(app_dir)) != ERROR_SUCCESS) { - srs_error("create app dir %s failed. ret=%d", app_dir.c_str(), ret); - return ret; - } - srs_info("create app dir %s ok", app_dir.c_str()); - - return ret; -} - SrsHlsCache::SrsHlsCache() { cache = new SrsTsCache(); diff --git a/trunk/src/app/srs_app_hls.hpp b/trunk/src/app/srs_app_hls.hpp index 1b590d184..916b0cc2b 100644 --- a/trunk/src/app/srs_app_hls.hpp +++ b/trunk/src/app/srs_app_hls.hpp @@ -169,8 +169,8 @@ private: private: std::string hls_entry_prefix; std::string hls_path; - std::string hls_m3u8_file; std::string hls_ts_file; + std::string m3u8_dir; double hls_aof_ratio; int hls_fragment; int hls_window; @@ -244,7 +244,6 @@ public: private: virtual int refresh_m3u8(); virtual int _refresh_m3u8(std::string m3u8_file); - virtual int create_dir(std::string filepath); }; /** diff --git a/trunk/src/kernel/srs_kernel_utility.cpp b/trunk/src/kernel/srs_kernel_utility.cpp index 05a674252..82061b60c 100644 --- a/trunk/src/kernel/srs_kernel_utility.cpp +++ b/trunk/src/kernel/srs_kernel_utility.cpp @@ -226,6 +226,11 @@ bool srs_string_ends_with(string str, string flag) return str.rfind(flag) == str.length() - flag.length(); } +bool srs_string_starts_with(string str, string flag) +{ + return str.find(flag) == 0; +} + int srs_do_create_dir_recursively(string dir) { int ret = ERROR_SUCCESS; diff --git a/trunk/src/kernel/srs_kernel_utility.hpp b/trunk/src/kernel/srs_kernel_utility.hpp index d5a514878..5269703fe 100644 --- a/trunk/src/kernel/srs_kernel_utility.hpp +++ b/trunk/src/kernel/srs_kernel_utility.hpp @@ -60,6 +60,8 @@ extern std::string srs_string_trim_start(std::string str, std::string trim_chars extern std::string srs_string_remove(std::string str, std::string remove_chars); // whether string end with extern bool srs_string_ends_with(std::string str, std::string flag); +// whether string starts with +extern bool srs_string_starts_with(std::string str, std::string flag); // create dir recursively extern int srs_create_dir_recursively(std::string dir);