Skip to content

Commit

Permalink
Fixed long loading when seeking near the end of the video
Browse files Browse the repository at this point in the history
  • Loading branch information
windows-server-2003 committed Nov 27, 2021
1 parent de9359a commit 36b9879
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions source/network/network_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,17 @@ Result_with_string NetworkDecoder::seek(s64 microseconds) {

clear_buffer();

s64 min_ts = std::max<s64>(0, microseconds - 1000000);
s64 max_ts = microseconds + 500000;

if (video_audio_seperate) {
int ffmpeg_result = avformat_seek_file(format_context[VIDEO], -1, microseconds - 1000000, microseconds, microseconds + 1000000, AVSEEK_FLAG_FRAME); // AVSEEK_FLAG_FRAME <- ?
if(ffmpeg_result < 0) {
int ffmpeg_result = avformat_seek_file(format_context[VIDEO], -1, min_ts, microseconds, max_ts, AVSEEK_FLAG_FRAME); // AVSEEK_FLAG_FRAME <- ?
for (int i = 2; i <= 3 && ffmpeg_result < 0; i++) { // retry with wider range backward
min_ts = std::max<s64>(0, microseconds - i * 1000000);
ffmpeg_result = avformat_seek_file(format_context[VIDEO], -1, min_ts, microseconds, max_ts, AVSEEK_FLAG_FRAME);
if (ffmpeg_result >= 0) Util_log_save("seek", "succeeded at " + std::to_string(ffmpeg_result));
}
if (ffmpeg_result < 0) {
result.code = DEF_ERR_FFMPEG_RETURNED_NOT_SUCCESS;
result.string = DEF_ERR_FFMPEG_RETURNED_NOT_SUCCESS_STR;
result.error_description = "avformat_seek_file() for video failed " + std::to_string(ffmpeg_result);
Expand Down Expand Up @@ -794,7 +802,12 @@ Result_with_string NetworkDecoder::seek(s64 microseconds) {
if (result.code != 0) return result;
return result;
} else {
int ffmpeg_result = avformat_seek_file(format_context[BOTH], -1, microseconds - 1000000, microseconds, microseconds + 1000000, AVSEEK_FLAG_FRAME); // AVSEEK_FLAG_FRAME <- ?
int ffmpeg_result = avformat_seek_file(format_context[BOTH], -1, min_ts, microseconds, max_ts, AVSEEK_FLAG_FRAME); // AVSEEK_FLAG_FRAME <- ?
for (int i = 2; i <= 3 && ffmpeg_result < 0; i++) { // retry with wider range backward
min_ts = std::max<s64>(0, microseconds - i * 1000000);
ffmpeg_result = avformat_seek_file(format_context[BOTH], -1, min_ts, microseconds, max_ts, AVSEEK_FLAG_FRAME);
if (ffmpeg_result >= 0) Util_log_save("seek", "succeeded at " + std::to_string(ffmpeg_result));
}
if(ffmpeg_result < 0) {
result.code = DEF_ERR_FFMPEG_RETURNED_NOT_SUCCESS;
result.string = DEF_ERR_FFMPEG_RETURNED_NOT_SUCCESS_STR;
Expand Down
2 changes: 1 addition & 1 deletion source/network/network_decoder_multiple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Result_with_string NetworkMultipleDecoder::seek(s64 microseconds) {
decoder.clear_buffer();
decoder.change_ffmpeg_data(fragments[(int) seq_using], adjust_timestamp ? seq_using * fragment_len : 0);
// trying to seek to a point too close to the end somehow causes ffmpeg to read the entire stream again ?
microseconds = std::max(0.0, std::min((double) microseconds, (get_duration() - 2) * 1000000));
microseconds = std::max(0.0, std::min((double) microseconds, (get_duration() - 1) * 1000000));
result = decoder.seek(microseconds);
}
return result;
Expand Down

0 comments on commit 36b9879

Please sign in to comment.