winlin

update donations. fix hls path bug.

@@ -13,6 +13,7 @@ RMB 500-999 @@ -13,6 +13,7 @@ RMB 500-999
13 * [2015-xx-xx xx:xx] xxx 13 * [2015-xx-xx xx:xx] xxx
14 14
15 RMB 100-499 15 RMB 100-499
  16 +* [2015-03-30 13:34] 扶凯
16 * [2015-03-29 11-07] 姚伟斌 17 * [2015-03-29 11-07] 姚伟斌
17 * [2015-03-14 20:21] 万伟 18 * [2015-03-14 20:21] 万伟
18 * [2015-03-11 09:44] 叶发养 19 * [2015-03-11 09:44] 叶发养
@@ -215,11 +215,20 @@ int SrsHlsMuxer::update_config(SrsRequest* r, string entry_prefix, @@ -215,11 +215,20 @@ int SrsHlsMuxer::update_config(SrsRequest* r, string entry_prefix,
215 215
216 hls_entry_prefix = entry_prefix; 216 hls_entry_prefix = entry_prefix;
217 hls_path = path; 217 hls_path = path;
218 - hls_m3u8_file = m3u8_file;  
219 hls_ts_file = ts_file; 218 hls_ts_file = ts_file;
220 hls_fragment = fragment; 219 hls_fragment = fragment;
221 hls_aof_ratio = aof_ratio; 220 hls_aof_ratio = aof_ratio;
222 hls_window = window; 221 hls_window = window;
  222 +
  223 + // generate the m3u8 dir and path.
  224 + m3u8 = path + "/" + m3u8_file;
  225 + m3u8 = srs_path_build_stream(m3u8, req->vhost, req->app, req->stream);
  226 +
  227 + m3u8_dir = m3u8;
  228 + size_t pos = string::npos;
  229 + if ((pos = m3u8_dir.rfind("/")) != string::npos) {
  230 + m3u8_dir = m3u8_dir.substr(0, pos);
  231 + }
223 232
224 // we always keep the target duration increasing. 233 // we always keep the target duration increasing.
225 int max_td = srs_max(target_duration, (int)(fragment * _srs_config->get_hls_td_ratio(r->vhost))); 234 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) @@ -251,12 +260,6 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts)
251 return ret; 260 return ret;
252 } 261 }
253 262
254 - // TODO: create all parents dirs.  
255 - // create dir for app.  
256 - if (should_write_file && (ret = create_dir(current->full_path)) != ERROR_SUCCESS) {  
257 - return ret;  
258 - }  
259 -  
260 // when segment open, the current segment must be NULL. 263 // when segment open, the current segment must be NULL.
261 srs_assert(!current); 264 srs_assert(!current);
262 265
@@ -304,16 +307,30 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts) @@ -304,16 +307,30 @@ int SrsHlsMuxer::segment_open(int64_t segment_start_dts)
304 ss << current->sequence_no; 307 ss << current->sequence_no;
305 ts_file = srs_string_replace(ts_file, "[seq]", ss.str()); 308 ts_file = srs_string_replace(ts_file, "[seq]", ss.str());
306 } 309 }
307 -  
308 - // replace variables  
309 current->full_path = hls_path + "/" + ts_file; 310 current->full_path = hls_path + "/" + ts_file;
310 311
  312 + // the ts url, relative or absolute url.
  313 + std::string ts_url = current->full_path;
  314 + if (srs_string_starts_with(ts_url, m3u8_dir)) {
  315 + ts_url = ts_url.substr(m3u8_dir.length());
  316 + }
  317 + while (srs_string_starts_with(ts_url, "/")) {
  318 + ts_url = ts_url.substr(1);
  319 + }
311 current->uri += hls_entry_prefix; 320 current->uri += hls_entry_prefix;
312 if (!hls_entry_prefix.empty() && !srs_string_ends_with(hls_entry_prefix, "/")) { 321 if (!hls_entry_prefix.empty() && !srs_string_ends_with(hls_entry_prefix, "/")) {
313 current->uri += "/"; 322 current->uri += "/";
314 } 323 }
315 - current->uri += ts_file; 324 + current->uri += ts_url;
  325 +
  326 + // create dir recursively for hls.
  327 + if (should_write_file && (ret = srs_create_dir_recursively(m3u8_dir)) != ERROR_SUCCESS) {
  328 + srs_error("create app dir %s failed. ret=%d", m3u8_dir.c_str(), ret);
  329 + return ret;
  330 + }
  331 + srs_info("create app dir %s ok", m3u8_dir.c_str());
316 332
  333 + // open temp ts file.
317 std::string tmp_file = current->full_path + ".tmp"; 334 std::string tmp_file = current->full_path + ".tmp";
318 if ((ret = current->muxer->open(tmp_file.c_str())) != ERROR_SUCCESS) { 335 if ((ret = current->muxer->open(tmp_file.c_str())) != ERROR_SUCCESS) {
319 srs_error("open hls muxer failed. ret=%d", ret); 336 srs_error("open hls muxer failed. ret=%d", ret);
@@ -527,23 +544,16 @@ int SrsHlsMuxer::refresh_m3u8() @@ -527,23 +544,16 @@ int SrsHlsMuxer::refresh_m3u8()
527 { 544 {
528 int ret = ERROR_SUCCESS; 545 int ret = ERROR_SUCCESS;
529 546
530 - std::string m3u8_file = hls_path;  
531 - m3u8_file += "/";  
532 - m3u8_file += hls_m3u8_file;  
533 - m3u8_file = srs_path_build_stream(m3u8_file, req->vhost, req->app, req->stream);  
534 -  
535 - m3u8 = m3u8_file;  
536 - m3u8_file += ".temp";  
537 -  
538 - if ((ret = _refresh_m3u8(m3u8_file)) == ERROR_SUCCESS) {  
539 - if (should_write_file && rename(m3u8_file.c_str(), m3u8.c_str()) < 0) { 547 + std::string temp_m3u8 = m3u8 + ".temp";
  548 + if ((ret = _refresh_m3u8(temp_m3u8)) == ERROR_SUCCESS) {
  549 + if (should_write_file && rename(temp_m3u8.c_str(), m3u8.c_str()) < 0) {
540 ret = ERROR_HLS_WRITE_FAILED; 550 ret = ERROR_HLS_WRITE_FAILED;
541 - srs_error("rename m3u8 file failed. %s => %s, ret=%d", m3u8_file.c_str(), m3u8.c_str(), ret); 551 + srs_error("rename m3u8 file failed. %s => %s, ret=%d", temp_m3u8.c_str(), m3u8.c_str(), ret);
542 } 552 }
543 } 553 }
544 554
545 // remove the temp file. 555 // remove the temp file.
546 - unlink(m3u8_file.c_str()); 556 + unlink(temp_m3u8.c_str());
547 557
548 return ret; 558 return ret;
549 } 559 }
@@ -634,31 +644,6 @@ int SrsHlsMuxer::_refresh_m3u8(string m3u8_file) @@ -634,31 +644,6 @@ int SrsHlsMuxer::_refresh_m3u8(string m3u8_file)
634 return ret; 644 return ret;
635 } 645 }
636 646
637 -int SrsHlsMuxer::create_dir(string filepath)  
638 -{  
639 - int ret = ERROR_SUCCESS;  
640 -  
641 - if (!should_write_file) {  
642 - return ret;  
643 - }  
644 -  
645 - std::string app_dir = filepath;  
646 - size_t pos = string::npos;  
647 - if ((pos = app_dir.rfind("/")) != string::npos) {  
648 - app_dir = app_dir.substr(0, pos);  
649 - }  
650 -  
651 - // TODO: cleanup the dir when startup.  
652 -  
653 - if ((ret = srs_create_dir_recursively(app_dir)) != ERROR_SUCCESS) {  
654 - srs_error("create app dir %s failed. ret=%d", app_dir.c_str(), ret);  
655 - return ret;  
656 - }  
657 - srs_info("create app dir %s ok", app_dir.c_str());  
658 -  
659 - return ret;  
660 -}  
661 -  
662 SrsHlsCache::SrsHlsCache() 647 SrsHlsCache::SrsHlsCache()
663 { 648 {
664 cache = new SrsTsCache(); 649 cache = new SrsTsCache();
@@ -169,8 +169,8 @@ private: @@ -169,8 +169,8 @@ private:
169 private: 169 private:
170 std::string hls_entry_prefix; 170 std::string hls_entry_prefix;
171 std::string hls_path; 171 std::string hls_path;
172 - std::string hls_m3u8_file;  
173 std::string hls_ts_file; 172 std::string hls_ts_file;
  173 + std::string m3u8_dir;
174 double hls_aof_ratio; 174 double hls_aof_ratio;
175 int hls_fragment; 175 int hls_fragment;
176 int hls_window; 176 int hls_window;
@@ -244,7 +244,6 @@ public: @@ -244,7 +244,6 @@ public:
244 private: 244 private:
245 virtual int refresh_m3u8(); 245 virtual int refresh_m3u8();
246 virtual int _refresh_m3u8(std::string m3u8_file); 246 virtual int _refresh_m3u8(std::string m3u8_file);
247 - virtual int create_dir(std::string filepath);  
248 }; 247 };
249 248
250 /** 249 /**
@@ -226,6 +226,11 @@ bool srs_string_ends_with(string str, string flag) @@ -226,6 +226,11 @@ bool srs_string_ends_with(string str, string flag)
226 return str.rfind(flag) == str.length() - flag.length(); 226 return str.rfind(flag) == str.length() - flag.length();
227 } 227 }
228 228
  229 +bool srs_string_starts_with(string str, string flag)
  230 +{
  231 + return str.find(flag) == 0;
  232 +}
  233 +
229 int srs_do_create_dir_recursively(string dir) 234 int srs_do_create_dir_recursively(string dir)
230 { 235 {
231 int ret = ERROR_SUCCESS; 236 int ret = ERROR_SUCCESS;
@@ -60,6 +60,8 @@ extern std::string srs_string_trim_start(std::string str, std::string trim_chars @@ -60,6 +60,8 @@ extern std::string srs_string_trim_start(std::string str, std::string trim_chars
60 extern std::string srs_string_remove(std::string str, std::string remove_chars); 60 extern std::string srs_string_remove(std::string str, std::string remove_chars);
61 // whether string end with 61 // whether string end with
62 extern bool srs_string_ends_with(std::string str, std::string flag); 62 extern bool srs_string_ends_with(std::string str, std::string flag);
  63 +// whether string starts with
  64 +extern bool srs_string_starts_with(std::string str, std::string flag);
63 65
64 // create dir recursively 66 // create dir recursively
65 extern int srs_create_dir_recursively(std::string dir); 67 extern int srs_create_dir_recursively(std::string dir);