胡斌

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;
#define SRS_CONF_DEFAULT_MAX_CONNECTIONS 1000
#define SRS_CONF_DEFAULT_HLS_PATH "./objs/nginx/html"
#define SRS_CONF_DEFAULT_HLS_M3U8_FILE "[app]/[stream].m3u8"
#define SRS_CONF_DEFAULT_HLS_TOTAL_M3U8_FILE "[app]/[stream]_total.m3u8"
#define SRS_CONF_DEFAULT_HLS_TOTAL_M3U8_FILE ""
#define SRS_CONF_DEFAULT_HLS_TOTAL_FILE "[app]/[stream]_total"
#define SRS_CONF_DEFAULT_HLS_TS_FILE "[app]/[stream]-[seq].ts"
#define SRS_CONF_DEFAULT_HLS_TS_FLOOR false
... ...
... ... @@ -302,6 +302,7 @@ SrsHlsMuxer::SrsHlsMuxer()
should_write_file = true;
async = new SrsAsyncCallWorker();
context = new SrsTsContext();
save_m3u8_total = false;
}
SrsHlsMuxer::~SrsHlsMuxer()
... ... @@ -415,6 +416,7 @@ int SrsHlsMuxer::update_config(SrsRequest* r, string entry_prefix,
// generate the total m3u8 dir and path.
total_m3u8_url = srs_path_build_stream(total_m3u8_file, req->vhost, req->app, req->stream);
total_m3u8 = path + "/" + total_m3u8_url;
save_m3u8_total = !total_m3u8_url.empty();
// generate the m3u8 dir and path.
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)
}
SrsFileWriter total_m3u8_writer;
//add segment to total.m3u8
std::stringstream ss;
if (save_m3u8_total) {
SrsFileWriter total_m3u8_writer;
//add segment to total.m3u8
std::stringstream ss;
if(currentSeg->sequence_no == 0){
ret = total_m3u8_writer.open(total_m3u8);
if(ret != ERROR_SUCCESS){
srs_error("open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,
total_m3u8.c_str(), log_desc.c_str(), currentSeg->sequence_no, currentSeg->uri.c_str(), currentSeg->duration,
if (currentSeg->sequence_no == 0) {
ret = total_m3u8_writer.open(total_m3u8);
if (ret != ERROR_SUCCESS) {
srs_error(
"open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,
total_m3u8.c_str(), log_desc.c_str(),
currentSeg->sequence_no, currentSeg->uri.c_str(),
currentSeg->duration,
currentSeg->segment_start_dts);
return ret;
}
//write total m3u8 header
ss << "#EXTM3U" << SRS_CONSTS_LF
<< "#EXT-X-VERSION:3" << SRS_CONSTS_LF
<< "#EXT-X-ALLOW-CACHE:YES" << SRS_CONSTS_LF;
srs_verbose("write m3u8 header success.");
}
else{
ret = total_m3u8_writer.open_append(total_m3u8);
if(ret != ERROR_SUCCESS){
srs_error("open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,
total_m3u8.c_str(), log_desc.c_str(), currentSeg->sequence_no, currentSeg->uri.c_str(), currentSeg->duration,
return ret;
}
//write total m3u8 header
ss << "#EXTM3U" << SRS_CONSTS_LF << "#EXT-X-VERSION:3"
<< SRS_CONSTS_LF << "#EXT-X-ALLOW-CACHE:YES"
<< SRS_CONSTS_LF;
srs_verbose("write m3u8 header success.");
} else {
ret = total_m3u8_writer.open_append(total_m3u8);
if (ret != ERROR_SUCCESS) {
srs_error(
"open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64,
total_m3u8.c_str(), log_desc.c_str(),
currentSeg->sequence_no, currentSeg->uri.c_str(),
currentSeg->duration,
currentSeg->segment_start_dts);
return ret;
}
}
if (currentSeg->is_sequence_header) {
// #EXT-X-DISCONTINUITY\n
ss << "#EXT-X-DISCONTINUITY" << SRS_CONSTS_LF;
srs_verbose("write m3u8 segment discontinuity success.");
}
// "#EXTINF:4294967295.208,\n"
ss.precision(3);
ss.setf(std::ios::fixed, std::ios::floatfield);
ss << "#EXTINF:" << currentSeg->duration << ", no desc" << SRS_CONSTS_LF;
srs_verbose("write m3u8 segment info success.");
// {file name}\n
ss << currentSeg->uri << SRS_CONSTS_LF;
srs_verbose("write m3u8 segment uri success.");
// write m3u8 to writer.
std::string m3u8 = ss.str();
if ((ret = total_m3u8_writer.write((char*)m3u8.c_str(), (int)m3u8.length(), NULL)) != ERROR_SUCCESS) {
srs_error("write m3u8 failed. ret=%d", ret);
return ret;
}
srs_info("write total m3u8 %s success.", m3u8_total_file.c_str());
return ret;
}
}
if (currentSeg->is_sequence_header) {
// #EXT-X-DISCONTINUITY\n
ss << "#EXT-X-DISCONTINUITY" << SRS_CONSTS_LF;
srs_verbose("write m3u8 segment discontinuity success.");
}
// "#EXTINF:4294967295.208,\n"
ss.precision(3);
ss.setf(std::ios::fixed, std::ios::floatfield);
ss << "#EXTINF:" << currentSeg->duration << ", no desc"
<< SRS_CONSTS_LF;
srs_verbose("write m3u8 segment info success.");
// {file name}\n
ss << currentSeg->uri << SRS_CONSTS_LF;
srs_verbose("write m3u8 segment uri success.");
// write m3u8 to writer.
std::string m3u8 = ss.str();
if ((ret = total_m3u8_writer.write((char*) m3u8.c_str(),
(int) m3u8.length(), NULL)) != ERROR_SUCCESS) {
srs_error("write m3u8 failed. ret=%d", ret);
return ret;
}
srs_info("write total m3u8 %s success.", m3u8_total_file.c_str());
}
} else {
// reuse current segment index.
... ... @@ -1023,7 +1033,7 @@ int SrsHlsCache::on_publish(SrsHlsMuxer* muxer, SrsRequest* req, int64_t segment
std::string total_m3u8_file = _srs_config->get_hls_total_m3u8_file(vhost);
std::string total_file = _srs_config->get_hls_total_file(vhost);
std::string ts_file = _srs_config->get_hls_ts_file(vhost);
bool cleanup = _srs_config->get_hls_cleanup(vhost);
bool cleanup = _srs_config->get_hls_cleanup(vhost) && total_m3u8_file.empty();
bool wait_keyframe = _srs_config->get_hls_wait_keyframe(vhost);
// the audio overflow, for pure audio to reap segment.
double hls_aof_ratio = _srs_config->get_hls_aof_ratio(vhost);
... ...
... ... @@ -209,6 +209,7 @@ private:
std::string total_m3u8_url;
std::string total;
std::string total_url;
bool save_m3u8_total;
private:
// TODO: FIXME: remove it.
bool should_write_cache;
... ...