Skip to content

Commit 63e1254

Browse files
committed
LibMedia: Reduce the amount of WAV data loaded before playback starts
Apparently FFmpeg probes WAV files for 32 packets before returning the stream info. With the default of 19200 bytes per packet, this could end up waiting for up to 5 seconds of data to be downloaded. Setting the max_size option only affects WAV and W64 in our build of libavformat. No other formats we care about will be affected.
1 parent db71822 commit 63e1254

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

Libraries/LibMedia/FFmpeg/FFmpegDemuxer.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ static DecoderErrorOr<void> initialize_format_context(AVFormatContext*& format_c
4545
return DecoderError::with_description(DecoderErrorCategory::Memory, "Failed to allocate format context"sv);
4646
format_context->pb = &io_context;
4747
format_context->flags |= AVFMT_FLAG_FAST_SEEK;
48-
if (avformat_open_input(&format_context, nullptr, nullptr, nullptr) < 0)
48+
49+
AVDictionary* options = nullptr;
50+
ScopeGuard free_options = [&] { av_dict_free(&options); };
51+
52+
// Reduce the maximum packet size for the WAV demuxer, so that playback begins sooner.
53+
av_dict_set(&options, "max_size", "4096", 0);
54+
55+
auto open_result = avformat_open_input(&format_context, nullptr, nullptr, &options);
56+
if (open_result < 0)
4957
return DecoderError::with_description(DecoderErrorCategory::Corrupted, "Failed to open input for format parsing"sv);
5058

5159
// Read stream info; doing this is required for headerless formats like MPEG

0 commit comments

Comments
 (0)