winlin

fix the hls crash bug

@@ -714,6 +714,9 @@ int SrsHls::reopen() @@ -714,6 +714,9 @@ int SrsHls::reopen()
714 current->duration = (stream_dts - current->segment_start_dts) / 90000.0; 714 current->duration = (stream_dts - current->segment_start_dts) / 90000.0;
715 segments.push_back(current); 715 segments.push_back(current);
716 716
  717 + srs_trace("reap ts segment, sequence_no=%d, uri=%s, duration=%.2f, start=%"PRId64"",
  718 + current->sequence_no, current->uri.c_str(), current->duration, current->segment_start_dts);
  719 +
717 // close the muxer of finished segment. 720 // close the muxer of finished segment.
718 srs_freep(current->muxer); 721 srs_freep(current->muxer);
719 current = NULL; 722 current = NULL;
@@ -726,19 +729,20 @@ int SrsHls::reopen() @@ -726,19 +729,20 @@ int SrsHls::reopen()
726 std::vector<SrsM3u8Segment*>::reverse_iterator it; 729 std::vector<SrsM3u8Segment*>::reverse_iterator it;
727 for (it = segments.rbegin(); it != segments.rend(); ++it) { 730 for (it = segments.rbegin(); it != segments.rend(); ++it) {
728 SrsM3u8Segment* segment = *it; 731 SrsM3u8Segment* segment = *it;
729 - duration += segment->duration;  
730 732
  733 + // once find the overflow segment, clear all segments before it.
731 if ((int)duration > hls_window) { 734 if ((int)duration > hls_window) {
732 segment_to_remove.push_back(segment); 735 segment_to_remove.push_back(segment);
  736 + continue;
733 } 737 }
  738 +
  739 + duration += segment->duration;
734 } 740 }
735 if (!segment_to_remove.empty()) { 741 if (!segment_to_remove.empty()) {
736 segments.erase(segments.begin(), segments.begin() + segment_to_remove.size()); 742 segments.erase(segments.begin(), segments.begin() + segment_to_remove.size());
737 743
738 // refresh the m3u8, donot contains the removed ts 744 // refresh the m3u8, donot contains the removed ts
739 - if ((ret = refresh_m3u8()) != ERROR_SUCCESS) {  
740 - return ret;  
741 - } 745 + ret = refresh_m3u8();
742 } 746 }
743 747
744 // remove the ts file. 748 // remove the ts file.
@@ -747,6 +751,12 @@ int SrsHls::reopen() @@ -747,6 +751,12 @@ int SrsHls::reopen()
747 unlink(segment->full_path.c_str()); 751 unlink(segment->full_path.c_str());
748 srs_freep(segment); 752 srs_freep(segment);
749 } 753 }
  754 +
  755 + // check ret of refresh m3u8
  756 + if (ret != ERROR_SUCCESS) {
  757 + srs_error("refresh m3u8 failed. ret=%d", ret);
  758 + return ret;
  759 + }
750 } 760 }
751 // new segment. 761 // new segment.
752 current = new SrsM3u8Segment(); 762 current = new SrsM3u8Segment();