winlin

drop ts segment when duration is too small

@@ -47,6 +47,9 @@ using namespace std; @@ -47,6 +47,9 @@ using namespace std;
47 // max PES packets size to flush the video. 47 // max PES packets size to flush the video.
48 #define SRS_HLS_AUDIO_CACHE_SIZE 1024 * 1024 48 #define SRS_HLS_AUDIO_CACHE_SIZE 1024 * 1024
49 49
  50 +// drop the segment when duration of ts too small.
  51 +#define SRS_HLS_SEGMENT_MIN_DURATION_MS 100
  52 +
50 // @see: NGX_RTMP_HLS_DELAY, 53 // @see: NGX_RTMP_HLS_DELAY,
51 // 63000: 700ms, ts_tbn=90000 54 // 63000: 700ms, ts_tbn=90000
52 #define SRS_HLS_DELAY 63000 55 #define SRS_HLS_DELAY 63000
@@ -680,24 +683,39 @@ int SrsHlsMuxer::segment_close(string log_desc) @@ -680,24 +683,39 @@ int SrsHlsMuxer::segment_close(string log_desc)
680 it = std::find(segments.begin(), segments.end(), current); 683 it = std::find(segments.begin(), segments.end(), current);
681 srs_assert(it == segments.end()); 684 srs_assert(it == segments.end());
682 685
683 - // valid, add to segments.  
684 - segments.push_back(current); 686 + // valid, add to segments if segment duration is ok
  687 + if (current->duration * 1000 >= SRS_HLS_SEGMENT_MIN_DURATION_MS) {
  688 + segments.push_back(current);
685 689
686 - srs_trace("%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64"",  
687 - log_desc.c_str(), current->sequence_no, current->uri.c_str(), current->duration,  
688 - current->segment_start_dts); 690 + srs_trace("%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64"",
  691 + log_desc.c_str(), current->sequence_no, current->uri.c_str(), current->duration,
  692 + current->segment_start_dts);
689 693
690 - // close the muxer of finished segment.  
691 - srs_freep(current->muxer);  
692 - // rename from tmp to real path  
693 - std::string tmp_file = current->full_path + ".tmp";  
694 - if (rename(tmp_file.c_str(), current->full_path.c_str()) < 0) {  
695 - ret = ERROR_HLS_WRITE_FAILED;  
696 - srs_error("rename ts file failed, %s => %s. ret=%d",  
697 - tmp_file.c_str(), current->full_path.c_str(), ret);  
698 - return ret; 694 + // close the muxer of finished segment.
  695 + srs_freep(current->muxer);
  696 + // rename from tmp to real path
  697 + std::string tmp_file = current->full_path + ".tmp";
  698 + if (rename(tmp_file.c_str(), current->full_path.c_str()) < 0) {
  699 + ret = ERROR_HLS_WRITE_FAILED;
  700 + srs_error("rename ts file failed, %s => %s. ret=%d",
  701 + tmp_file.c_str(), current->full_path.c_str(), ret);
  702 + return ret;
  703 + }
  704 + current = NULL;
  705 + } else {
  706 + // reuse current segment index.
  707 + file_index--;
  708 +
  709 + srs_trace("%s drop ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64"",
  710 + log_desc.c_str(), current->sequence_no, current->uri.c_str(), current->duration,
  711 + current->segment_start_dts);
  712 +
  713 + // rename from tmp to real path
  714 + std::string tmp_file = current->full_path + ".tmp";
  715 + unlink(tmp_file.c_str());
  716 +
  717 + srs_freep(current);
699 } 718 }
700 - current = NULL;  
701 719
702 // the segments to remove 720 // the segments to remove
703 std::vector<SrsHlsSegment*> segment_to_remove; 721 std::vector<SrsHlsSegment*> segment_to_remove;
@@ -941,6 +959,9 @@ int SrsHlsCache::on_publish(SrsHlsMuxer* muxer, SrsRequest* req, int64_t segment @@ -941,6 +959,9 @@ int SrsHlsCache::on_publish(SrsHlsMuxer* muxer, SrsRequest* req, int64_t segment
941 // reset video count for new publish session. 959 // reset video count for new publish session.
942 video_count = 0; 960 video_count = 0;
943 961
  962 + // TODO: FIXME: support load exists m3u8, to continue publish stream.
  963 + // for the HLS donot requires the EXT-X-MEDIA-SEQUENCE be monotonically increase.
  964 +
944 // open muxer 965 // open muxer
945 if ((ret = muxer->update_config(app, stream, hls_path, hls_fragment, hls_window)) != ERROR_SUCCESS) { 966 if ((ret = muxer->update_config(app, stream, hls_path, hls_fragment, hls_window)) != ERROR_SUCCESS) {
946 srs_error("m3u8 muxer update config failed. ret=%d", ret); 967 srs_error("m3u8 muxer update config failed. ret=%d", ret);