diff options
author | wm4 <wm4@nowhere> | 2019-11-06 21:36:02 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2019-11-06 21:36:02 +0100 |
commit | f37f4de8496556afaa024e39e2efb433eb1680d4 (patch) | |
tree | 1ade8205598a3142d1fcbe5f32f461266ce81b76 /demux/demux_playlist.c | |
parent | abb089431d0467c3609207d0b27359ef08a6c16d (diff) | |
download | mpv-f37f4de8496556afaa024e39e2efb433eb1680d4.tar.bz2 mpv-f37f4de8496556afaa024e39e2efb433eb1680d4.tar.xz |
stream: turn into a ring buffer, make size configurable
In some corner cases (see #6802), it can be beneficial to use a larger
stream buffer size. Use this as argument to rewrite everything for no
reason.
Turn stream.c itself into a ring buffer, with configurable size. The
latter would have been easily achievable with minimal changes, and the
ring buffer is the hard part. There is no reason to have a ring buffer
at all, except possibly if ffmpeg don't fix their awful mp4 demuxer, and
some subtle issues with demux_mkv.c wanting to seek back by small
offsets (the latter was handled with small stream_peek() calls, which
are unneeded now).
In addition, this turns small forward seeks into reads (where data is
simply skipped). Before this commit, only stream_skip() did this (which
also mean that stream_skip() simply calls stream_seek() now).
Replace all stream_peek() calls with something else (usually
stream_read_peek()). The function was a problem, because it returned a
pointer to the internal buffer, which is now a ring buffer with
wrapping. The new function just copies the data into a buffer, and in
some cases requires callers to dynamically allocate memory. (The most
common case, demux_lavf.c, required a separate buffer allocation anyway
due to FFmpeg "idiosyncrasies".) This is the bulk of the demuxer_*
changes.
I'm not happy with this. There still isn't a good reason why there
should be a ring buffer, that is complex, and most of the time just
wastes half of the available memory. Maybe another rewrite soon.
It also contains bugs; you're an alpha tester now.
Diffstat (limited to 'demux/demux_playlist.c')
-rw-r--r-- | demux/demux_playlist.c | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/demux/demux_playlist.c b/demux/demux_playlist.c index 95c89a18e2..a7650b2b69 100644 --- a/demux/demux_playlist.c +++ b/demux/demux_playlist.c @@ -92,12 +92,13 @@ static int read_characters(stream_t *s, uint8_t *dst, int dstsize, int utf16) } return cur - dst; } else { - bstr buf = stream_peek_buffer(s); - uint8_t *end = memchr(buf.start, '\n', buf.len); - int len = end ? end - buf.start + 1 : buf.len; + uint8_t buf[1024]; + int buf_len = stream_read_peek(s, buf, sizeof(buf)); + uint8_t *end = memchr(buf, '\n', buf_len); + int len = end ? end - buf + 1 : buf_len; if (len > dstsize) return -1; // line too long - memcpy(dst, buf.start, len); + memcpy(dst, buf, len); stream_skip(s, len); return len; } @@ -178,7 +179,9 @@ static int parse_m3u(struct pl_parser *p) // Last resort: if the file extension is m3u, it might be headerless. if (p->check_level == DEMUX_CHECK_UNSAFE) { char *ext = mp_splitext(p->real_stream->url, NULL); - bstr data = stream_peek(p->real_stream, PROBE_SIZE); + char probe[PROBE_SIZE]; + int len = stream_read_peek(p->real_stream, probe, sizeof(probe)); + bstr data = {probe, len}; if (ext && data.len > 10 && maybe_text(data)) { const char *exts[] = {"m3u", "m3u8", NULL}; for (int n = 0; exts[n]; n++) { @@ -437,8 +440,9 @@ static int open_file(struct demuxer *demuxer, enum demux_check check) p->real_stream = demuxer->stream; p->add_base = true; - bstr probe_buf = stream_peek(demuxer->stream, PROBE_SIZE); - p->s = stream_memory_open(demuxer->global, probe_buf.start, probe_buf.len); + char probe[PROBE_SIZE]; + int probe_len = stream_read_peek(p->real_stream, probe, sizeof(probe)); + p->s = stream_memory_open(demuxer->global, probe, probe_len); p->s->mime_type = demuxer->stream->mime_type; p->utf16 = stream_skip_bom(p->s); p->force = force; |