From b0de1ac36c7b16ff4f2763421b86ebedeba2044b Mon Sep 17 00:00:00 2001 From: wm4 Date: Fri, 10 Nov 2017 12:32:40 +0100 Subject: demux: limit number of seek ranges to a static maximum Limit the number of cached ranges to MAX_SEEK_RANGES, which is the same number of maximally exported seek ranges. It makes no sense to keep them, as the user won't see them anyway. Remove the smallest ones to enforce the limit if the number grows too high. --- demux/demux.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'demux') diff --git a/demux/demux.c b/demux/demux.c index 68e39ca4d1..0075df8b72 100644 --- a/demux/demux.c +++ b/demux/demux.c @@ -506,17 +506,32 @@ static void clear_cached_range(struct demux_internal *in, update_seek_ranges(range); } +// Remove ranges with no data (except in->current_range). Also remove excessive +// ranges. static void free_empty_cached_ranges(struct demux_internal *in) { assert(in->current_range && in->num_ranges > 0); assert(in->current_range == in->ranges[in->num_ranges - 1]); - for (int n = in->num_ranges - 2; n >= 0; n--) { - struct demux_cached_range *range = in->ranges[n]; - if (range->seek_start == MP_NOPTS_VALUE) { - clear_cached_range(in, range); - MP_TARRAY_REMOVE_AT(in->ranges, in->num_ranges, n); + while (1) { + struct demux_cached_range *worst = NULL; + + for (int n = in->num_ranges - 2; n >= 0; n--) { + struct demux_cached_range *range = in->ranges[n]; + if (range->seek_start == MP_NOPTS_VALUE) { + clear_cached_range(in, range); + MP_TARRAY_REMOVE_AT(in->ranges, in->num_ranges, n); + } else { + if (!worst || (range->seek_end - range->seek_start < + worst->seek_end - worst->seek_start)) + worst = range; + } } + + if (in->num_ranges <= MAX_SEEK_RANGES) + break; + + clear_cached_range(in, worst); } } -- cgit v1.2.3