胡斌

don't clean up hls ts files when hls_total_m3u8_file is not empty

@@ -72,7 +72,7 @@ const char* _srs_version = "XCORE-"RTMP_SIG_SRS_SERVER; @@ -72,7 +72,7 @@ const char* _srs_version = "XCORE-"RTMP_SIG_SRS_SERVER;
72 #define SRS_CONF_DEFAULT_MAX_CONNECTIONS 1000 72 #define SRS_CONF_DEFAULT_MAX_CONNECTIONS 1000
73 #define SRS_CONF_DEFAULT_HLS_PATH "./objs/nginx/html" 73 #define SRS_CONF_DEFAULT_HLS_PATH "./objs/nginx/html"
74 #define SRS_CONF_DEFAULT_HLS_M3U8_FILE "[app]/[stream].m3u8" 74 #define SRS_CONF_DEFAULT_HLS_M3U8_FILE "[app]/[stream].m3u8"
75 -#define SRS_CONF_DEFAULT_HLS_TOTAL_M3U8_FILE "[app]/[stream]_total.m3u8" 75 +#define SRS_CONF_DEFAULT_HLS_TOTAL_M3U8_FILE ""
76 #define SRS_CONF_DEFAULT_HLS_TOTAL_FILE "[app]/[stream]_total" 76 #define SRS_CONF_DEFAULT_HLS_TOTAL_FILE "[app]/[stream]_total"
77 #define SRS_CONF_DEFAULT_HLS_TS_FILE "[app]/[stream]-[seq].ts" 77 #define SRS_CONF_DEFAULT_HLS_TS_FILE "[app]/[stream]-[seq].ts"
78 #define SRS_CONF_DEFAULT_HLS_TS_FLOOR false 78 #define SRS_CONF_DEFAULT_HLS_TS_FLOOR false
@@ -302,6 +302,7 @@ SrsHlsMuxer::SrsHlsMuxer() @@ -302,6 +302,7 @@ SrsHlsMuxer::SrsHlsMuxer()
302 should_write_file = true; 302 should_write_file = true;
303 async = new SrsAsyncCallWorker(); 303 async = new SrsAsyncCallWorker();
304 context = new SrsTsContext(); 304 context = new SrsTsContext();
  305 + save_m3u8_total = false;
305 } 306 }
306 307
307 SrsHlsMuxer::~SrsHlsMuxer() 308 SrsHlsMuxer::~SrsHlsMuxer()
@@ -415,6 +416,7 @@ int SrsHlsMuxer::update_config(SrsRequest* r, string entry_prefix, @@ -415,6 +416,7 @@ int SrsHlsMuxer::update_config(SrsRequest* r, string entry_prefix,
415 // generate the total m3u8 dir and path. 416 // generate the total m3u8 dir and path.
416 total_m3u8_url = srs_path_build_stream(total_m3u8_file, req->vhost, req->app, req->stream); 417 total_m3u8_url = srs_path_build_stream(total_m3u8_file, req->vhost, req->app, req->stream);
417 total_m3u8 = path + "/" + total_m3u8_url; 418 total_m3u8 = path + "/" + total_m3u8_url;
  419 + save_m3u8_total = !total_m3u8_url.empty();
418 420
419 // generate the m3u8 dir and path. 421 // generate the m3u8 dir and path.
420 total_url = srs_path_build_stream(total_file, req->vhost, req->app, req->stream); 422 total_url = srs_path_build_stream(total_file, req->vhost, req->app, req->stream);
@@ -761,60 +763,68 @@ int SrsHlsMuxer::segment_close(string log_desc) @@ -761,60 +763,68 @@ int SrsHlsMuxer::segment_close(string log_desc)
761 } 763 }
762 764
763 765
764 - SrsFileWriter total_m3u8_writer;  
765 - //add segment to total.m3u8  
766 - std::stringstream ss; 766 + if (save_m3u8_total) {
  767 + SrsFileWriter total_m3u8_writer;
  768 + //add segment to total.m3u8
  769 + std::stringstream ss;
767 770
768 - if(currentSeg->sequence_no == 0){  
769 - ret = total_m3u8_writer.open(total_m3u8);  
770 - if(ret != ERROR_SUCCESS){  
771 - srs_error("open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,  
772 - total_m3u8.c_str(), log_desc.c_str(), currentSeg->sequence_no, currentSeg->uri.c_str(), currentSeg->duration, 771 + if (currentSeg->sequence_no == 0) {
  772 + ret = total_m3u8_writer.open(total_m3u8);
  773 + if (ret != ERROR_SUCCESS) {
  774 + srs_error(
  775 + "open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,
  776 + total_m3u8.c_str(), log_desc.c_str(),
  777 + currentSeg->sequence_no, currentSeg->uri.c_str(),
  778 + currentSeg->duration,
773 currentSeg->segment_start_dts); 779 currentSeg->segment_start_dts);
774 - return ret;  
775 - }  
776 - //write total m3u8 header  
777 -  
778 - ss << "#EXTM3U" << SRS_CONSTS_LF  
779 - << "#EXT-X-VERSION:3" << SRS_CONSTS_LF  
780 - << "#EXT-X-ALLOW-CACHE:YES" << SRS_CONSTS_LF;  
781 - srs_verbose("write m3u8 header success.");  
782 -  
783 - }  
784 - else{  
785 - ret = total_m3u8_writer.open_append(total_m3u8);  
786 - if(ret != ERROR_SUCCESS){  
787 - srs_error("open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,  
788 - total_m3u8.c_str(), log_desc.c_str(), currentSeg->sequence_no, currentSeg->uri.c_str(), currentSeg->duration, 780 + return ret;
  781 + }
  782 + //write total m3u8 header
  783 +
  784 + ss << "#EXTM3U" << SRS_CONSTS_LF << "#EXT-X-VERSION:3"
  785 + << SRS_CONSTS_LF << "#EXT-X-ALLOW-CACHE:YES"
  786 + << SRS_CONSTS_LF;
  787 + srs_verbose("write m3u8 header success.");
  788 +
  789 + } else {
  790 + ret = total_m3u8_writer.open_append(total_m3u8);
  791 + if (ret != ERROR_SUCCESS) {
  792 + srs_error(
  793 + "open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,
  794 + total_m3u8.c_str(), log_desc.c_str(),
  795 + currentSeg->sequence_no, currentSeg->uri.c_str(),
  796 + currentSeg->duration,
789 currentSeg->segment_start_dts); 797 currentSeg->segment_start_dts);
790 - return ret;  
791 - }  
792 - }  
793 -  
794 -  
795 - if (currentSeg->is_sequence_header) {  
796 - // #EXT-X-DISCONTINUITY\n  
797 - ss << "#EXT-X-DISCONTINUITY" << SRS_CONSTS_LF;  
798 - srs_verbose("write m3u8 segment discontinuity success.");  
799 - }  
800 -  
801 - // "#EXTINF:4294967295.208,\n"  
802 - ss.precision(3);  
803 - ss.setf(std::ios::fixed, std::ios::floatfield);  
804 - ss << "#EXTINF:" << currentSeg->duration << ", no desc" << SRS_CONSTS_LF;  
805 - srs_verbose("write m3u8 segment info success.");  
806 -  
807 - // {file name}\n  
808 - ss << currentSeg->uri << SRS_CONSTS_LF;  
809 - srs_verbose("write m3u8 segment uri success.");  
810 -  
811 - // write m3u8 to writer.  
812 - std::string m3u8 = ss.str();  
813 - if ((ret = total_m3u8_writer.write((char*)m3u8.c_str(), (int)m3u8.length(), NULL)) != ERROR_SUCCESS) {  
814 - srs_error("write m3u8 failed. ret=%d", ret);  
815 - return ret;  
816 - }  
817 - srs_info("write total m3u8 %s success.", m3u8_total_file.c_str()); 798 + return ret;
  799 + }
  800 + }
  801 +
  802 + if (currentSeg->is_sequence_header) {
  803 + // #EXT-X-DISCONTINUITY\n
  804 + ss << "#EXT-X-DISCONTINUITY" << SRS_CONSTS_LF;
  805 + srs_verbose("write m3u8 segment discontinuity success.");
  806 + }
  807 +
  808 + // "#EXTINF:4294967295.208,\n"
  809 + ss.precision(3);
  810 + ss.setf(std::ios::fixed, std::ios::floatfield);
  811 + ss << "#EXTINF:" << currentSeg->duration << ", no desc"
  812 + << SRS_CONSTS_LF;
  813 + srs_verbose("write m3u8 segment info success.");
  814 +
  815 + // {file name}\n
  816 + ss << currentSeg->uri << SRS_CONSTS_LF;
  817 + srs_verbose("write m3u8 segment uri success.");
  818 +
  819 + // write m3u8 to writer.
  820 + std::string m3u8 = ss.str();
  821 + if ((ret = total_m3u8_writer.write((char*) m3u8.c_str(),
  822 + (int) m3u8.length(), NULL)) != ERROR_SUCCESS) {
  823 + srs_error("write m3u8 failed. ret=%d", ret);
  824 + return ret;
  825 + }
  826 + srs_info("write total m3u8 %s success.", m3u8_total_file.c_str());
  827 + }
818 828
819 } else { 829 } else {
820 // reuse current segment index. 830 // reuse current segment index.
@@ -1023,7 +1033,7 @@ int SrsHlsCache::on_publish(SrsHlsMuxer* muxer, SrsRequest* req, int64_t segment @@ -1023,7 +1033,7 @@ int SrsHlsCache::on_publish(SrsHlsMuxer* muxer, SrsRequest* req, int64_t segment
1023 std::string total_m3u8_file = _srs_config->get_hls_total_m3u8_file(vhost); 1033 std::string total_m3u8_file = _srs_config->get_hls_total_m3u8_file(vhost);
1024 std::string total_file = _srs_config->get_hls_total_file(vhost); 1034 std::string total_file = _srs_config->get_hls_total_file(vhost);
1025 std::string ts_file = _srs_config->get_hls_ts_file(vhost); 1035 std::string ts_file = _srs_config->get_hls_ts_file(vhost);
1026 - bool cleanup = _srs_config->get_hls_cleanup(vhost); 1036 + bool cleanup = _srs_config->get_hls_cleanup(vhost) && total_m3u8_file.empty();
1027 bool wait_keyframe = _srs_config->get_hls_wait_keyframe(vhost); 1037 bool wait_keyframe = _srs_config->get_hls_wait_keyframe(vhost);
1028 // the audio overflow, for pure audio to reap segment. 1038 // the audio overflow, for pure audio to reap segment.
1029 double hls_aof_ratio = _srs_config->get_hls_aof_ratio(vhost); 1039 double hls_aof_ratio = _srs_config->get_hls_aof_ratio(vhost);
@@ -209,6 +209,7 @@ private: @@ -209,6 +209,7 @@ private:
209 std::string total_m3u8_url; 209 std::string total_m3u8_url;
210 std::string total; 210 std::string total;
211 std::string total_url; 211 std::string total_url;
  212 + bool save_m3u8_total;
212 private: 213 private:
213 // TODO: FIXME: remove it. 214 // TODO: FIXME: remove it.
214 bool should_write_cache; 215 bool should_write_cache;