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,7 +683,8 @@ int SrsHlsMuxer::segment_close(string log_desc) @@ -680,7 +683,8 @@ 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. 686 + // valid, add to segments if segment duration is ok
  687 + if (current->duration * 1000 >= SRS_HLS_SEGMENT_MIN_DURATION_MS) {
684 segments.push_back(current); 688 segments.push_back(current);
685 689
686 srs_trace("%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64"", 690 srs_trace("%s reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64"",
@@ -698,6 +702,20 @@ int SrsHlsMuxer::segment_close(string log_desc) @@ -698,6 +702,20 @@ int SrsHlsMuxer::segment_close(string log_desc)
698 return ret; 702 return ret;
699 } 703 }
700 current = NULL; 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);
  718 + }
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);