1.erase the last "#EXT-X-ENDLIST" int the total.m3u8 if stream is republished
2.erase the title "no desc" in m3u8 to reduce size because the title is optional
正在显示
4 个修改的文件
包含
51 行增加
和
4 行删除
| @@ -303,6 +303,7 @@ SrsHlsMuxer::SrsHlsMuxer() | @@ -303,6 +303,7 @@ SrsHlsMuxer::SrsHlsMuxer() | ||
| 303 | async = new SrsAsyncCallWorker(); | 303 | async = new SrsAsyncCallWorker(); |
| 304 | context = new SrsTsContext(); | 304 | context = new SrsTsContext(); |
| 305 | save_m3u8_total = false; | 305 | save_m3u8_total = false; |
| 306 | + m3u8_total_endlist_saved = false; | ||
| 306 | total_duraion = 0.0; | 307 | total_duraion = 0.0; |
| 307 | } | 308 | } |
| 308 | 309 | ||
| @@ -791,7 +792,7 @@ int SrsHlsMuxer::segment_close(string log_desc) | @@ -791,7 +792,7 @@ int SrsHlsMuxer::segment_close(string log_desc) | ||
| 791 | srs_verbose("write m3u8 header success."); | 792 | srs_verbose("write m3u8 header success."); |
| 792 | 793 | ||
| 793 | } else { | 794 | } else { |
| 794 | - ret = total_m3u8_writer.open_append(total_m3u8); | 795 | + ret = total_m3u8_writer.open_write(total_m3u8); |
| 795 | if (ret != ERROR_SUCCESS) { | 796 | if (ret != ERROR_SUCCESS) { |
| 796 | srs_error( | 797 | srs_error( |
| 797 | "open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64, | 798 | "open total file %s error:%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64, |
| @@ -803,7 +804,12 @@ int SrsHlsMuxer::segment_close(string log_desc) | @@ -803,7 +804,12 @@ int SrsHlsMuxer::segment_close(string log_desc) | ||
| 803 | } | 804 | } |
| 804 | } | 805 | } |
| 805 | 806 | ||
| 807 | + bool erase_last_endlist = false; | ||
| 808 | + | ||
| 806 | if (currentSeg->is_sequence_header) { | 809 | if (currentSeg->is_sequence_header) { |
| 810 | + if( m3u8_total_endlist_saved ){ | ||
| 811 | + erase_last_endlist = true; | ||
| 812 | + } | ||
| 807 | // #EXT-X-DISCONTINUITY\n | 813 | // #EXT-X-DISCONTINUITY\n |
| 808 | ss << "#EXT-X-DISCONTINUITY" << SRS_CONSTS_LF; | 814 | ss << "#EXT-X-DISCONTINUITY" << SRS_CONSTS_LF; |
| 809 | srs_verbose("write m3u8 segment discontinuity success."); | 815 | srs_verbose("write m3u8 segment discontinuity success."); |
| @@ -812,7 +818,7 @@ int SrsHlsMuxer::segment_close(string log_desc) | @@ -812,7 +818,7 @@ int SrsHlsMuxer::segment_close(string log_desc) | ||
| 812 | // "#EXTINF:4294967295.208,\n" | 818 | // "#EXTINF:4294967295.208,\n" |
| 813 | ss.precision(3); | 819 | ss.precision(3); |
| 814 | ss.setf(std::ios::fixed, std::ios::floatfield); | 820 | ss.setf(std::ios::fixed, std::ios::floatfield); |
| 815 | - ss << "#EXTINF:" << currentSeg->duration << ", no desc" | 821 | + ss << "#EXTINF:" << currentSeg->duration << ","//", no desc" //the #EXTINF:<duration>,<title>,title is optional,so remove it to save the space |
| 816 | << SRS_CONSTS_LF; | 822 | << SRS_CONSTS_LF; |
| 817 | srs_verbose("write m3u8 segment info success."); | 823 | srs_verbose("write m3u8 segment info success."); |
| 818 | 824 | ||
| @@ -821,7 +827,16 @@ int SrsHlsMuxer::segment_close(string log_desc) | @@ -821,7 +827,16 @@ int SrsHlsMuxer::segment_close(string log_desc) | ||
| 821 | srs_verbose("write m3u8 segment uri success."); | 827 | srs_verbose("write m3u8 segment uri success."); |
| 822 | 828 | ||
| 823 | if(log_desc == "unpublish"){ | 829 | if(log_desc == "unpublish"){ |
| 824 | - ss << "#EXT-X-ENDLIST" << SRS_CONSTS_LF; | 830 | + ss << "#EXT-X-ENDLIST" << SRS_CONSTS_LF; |
| 831 | + m3u8_total_endlist_saved = true; | ||
| 832 | + } | ||
| 833 | + | ||
| 834 | + if(erase_last_endlist){ | ||
| 835 | + int64_t cur_pos = total_m3u8_writer.tellg(); | ||
| 836 | + | ||
| 837 | + cur_pos -= sizeof("#EXT-X-ENDLIST");// sizeof("#EXT-X-ENDLIST") = strlen("#EXT-X-ENDLIST") + 1 = strlen("#EXT-X-ENDLIST") + strlen("\n") | ||
| 838 | + | ||
| 839 | + total_m3u8_writer.lseek(cur_pos); | ||
| 825 | } | 840 | } |
| 826 | 841 | ||
| 827 | // write m3u8 to writer. | 842 | // write m3u8 to writer. |
| @@ -1019,7 +1034,7 @@ int SrsHlsMuxer::_refresh_m3u8(string m3u8_file) | @@ -1019,7 +1034,7 @@ int SrsHlsMuxer::_refresh_m3u8(string m3u8_file) | ||
| 1019 | // "#EXTINF:4294967295.208,\n" | 1034 | // "#EXTINF:4294967295.208,\n" |
| 1020 | ss.precision(3); | 1035 | ss.precision(3); |
| 1021 | ss.setf(std::ios::fixed, std::ios::floatfield); | 1036 | ss.setf(std::ios::fixed, std::ios::floatfield); |
| 1022 | - ss << "#EXTINF:" << segment->duration << ", no desc" << SRS_CONSTS_LF; | 1037 | + ss << "#EXTINF:" << segment->duration << ","/*", no desc"*/ << SRS_CONSTS_LF;//#EXTINF:<duration>,<title>,title is optional,so remove it to save the space |
| 1023 | srs_verbose("write m3u8 segment info success."); | 1038 | srs_verbose("write m3u8 segment info success."); |
| 1024 | 1039 | ||
| 1025 | // {file name}\n | 1040 | // {file name}\n |
| @@ -210,6 +210,7 @@ private: | @@ -210,6 +210,7 @@ private: | ||
| 210 | std::string total; | 210 | std::string total; |
| 211 | std::string total_url; | 211 | std::string total_url; |
| 212 | bool save_m3u8_total; | 212 | bool save_m3u8_total; |
| 213 | + bool m3u8_total_endlist_saved; | ||
| 213 | //total duration for all segments | 214 | //total duration for all segments |
| 214 | double total_duraion; | 215 | double total_duraion; |
| 215 | private: | 216 | private: |
| @@ -94,6 +94,32 @@ int SrsFileWriter::open_append(string p) | @@ -94,6 +94,32 @@ int SrsFileWriter::open_append(string p) | ||
| 94 | return ret; | 94 | return ret; |
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | +int SrsFileWriter::open_write(string p) | ||
| 98 | +{ | ||
| 99 | + int ret = ERROR_SUCCESS; | ||
| 100 | + | ||
| 101 | + if (fd > 0) { | ||
| 102 | + ret = ERROR_SYSTEM_FILE_ALREADY_OPENED; | ||
| 103 | + srs_error("file %s already opened. ret=%d", path.c_str(), ret); | ||
| 104 | + return ret; | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + int flags = O_CREAT|O_WRONLY; | ||
| 108 | + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; | ||
| 109 | + | ||
| 110 | + if ((fd = ::open(p.c_str(), flags, mode)) < 0) { | ||
| 111 | + ret = ERROR_SYSTEM_FILE_OPENE; | ||
| 112 | + srs_error("open file %s failed. ret=%d", p.c_str(), ret); | ||
| 113 | + return ret; | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + ::lseek(fd, 0l, SEEK_END); | ||
| 117 | + | ||
| 118 | + path = p; | ||
| 119 | + | ||
| 120 | + return ret; | ||
| 121 | +} | ||
| 122 | + | ||
| 97 | void SrsFileWriter::close() | 123 | void SrsFileWriter::close() |
| 98 | { | 124 | { |
| 99 | int ret = ERROR_SUCCESS; | 125 | int ret = ERROR_SUCCESS; |
| @@ -59,6 +59,11 @@ public: | @@ -59,6 +59,11 @@ public: | ||
| 59 | */ | 59 | */ |
| 60 | virtual int open_append(std::string p); | 60 | virtual int open_append(std::string p); |
| 61 | /** | 61 | /** |
| 62 | + * open file writer, seek to file end. | ||
| 63 | + * @param p a string indicates the path of file to open. | ||
| 64 | + */ | ||
| 65 | + virtual int open_write(std::string p); | ||
| 66 | + /** | ||
| 62 | * close current writer. | 67 | * close current writer. |
| 63 | * @remark user can reopen again. | 68 | * @remark user can reopen again. |
| 64 | */ | 69 | */ |
-
请 注册 或 登录 后发表评论