summaryrefslogtreecommitdiffstats
path: root/stream
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-04-09 19:03:23 +0200
committerwm4 <wm4@nowhere>2014-04-09 19:03:23 +0200
commite7a5124b36c872768a3af5ec8eef345902c56524 (patch)
treee8289994e0e41e7ee43a1295bf2b9fadec2d6d75 /stream
parent4c4c7bdeda910feaa75205b6bb43819a605855e2 (diff)
downloadmpv-e7a5124b36c872768a3af5ec8eef345902c56524.tar.bz2
mpv-e7a5124b36c872768a3af5ec8eef345902c56524.tar.xz
cache: move ringbuffer read into a separate function
No functional changes yet.
Diffstat (limited to 'stream')
-rw-r--r--stream/cache.c49
1 files changed, 32 insertions, 17 deletions
diff --git a/stream/cache.c b/stream/cache.c
index e69f6db0fb..42d412801f 100644
--- a/stream/cache.c
+++ b/stream/cache.c
@@ -176,6 +176,35 @@ static void cache_drop_contents(struct priv *s)
s->eof = false;
}
+// Copy at most dst_size from the cache at the given absolute file position pos.
+// Return number of bytes that could actually be read.
+// Does not advance the file position, or change anything else.
+// Can be called from anywhere, as long as the mutex is held.
+static size_t read_buffer(struct priv *s, unsigned char *dst,
+ size_t dst_size, int64_t pos)
+{
+ if (pos >= s->max_filepos || pos < s->min_filepos)
+ return 0;
+ int64_t newb = s->max_filepos - pos; // new bytes in the buffer
+
+ pos = pos - s->offset; // file pos to buffer memory pos
+ if (pos < 0) {
+ pos += s->buffer_size;
+ } else if (pos >= s->buffer_size) {
+ pos -= s->buffer_size;
+ }
+
+ if (newb > s->buffer_size - pos)
+ newb = s->buffer_size - pos; // handle wrap...
+
+ newb = MPMIN(newb, dst_size);
+
+ assert(newb >= 0 && newb <= dst_size);
+ assert(pos >= 0 && pos + newb <= s->buffer_size);
+ memcpy(&dst[0], &s->buffer[pos], newb);
+ return newb;
+}
+
// Runs in the main thread
// mutex must be held, but is sometimes temporarily dropped
static int cache_read(struct priv *s, unsigned char *buf, int size)
@@ -194,23 +223,9 @@ static int cache_read(struct priv *s, unsigned char *buf, int size)
return 0;
}
- int64_t newb = s->max_filepos - s->read_filepos; // new bytes in the buffer
-
- int64_t pos = s->read_filepos - s->offset; // file pos to buffer memory pos
- if (pos < 0)
- pos += s->buffer_size;
- else if (pos >= s->buffer_size)
- pos -= s->buffer_size;
-
- if (newb > s->buffer_size - pos)
- newb = s->buffer_size - pos; // handle wrap...
-
- newb = FFMIN(newb, size);
-
- memcpy(buf, &s->buffer[pos], newb);
-
- s->read_filepos += newb;
- return newb;
+ int readb = read_buffer(s, buf, size, s->read_filepos);
+ s->read_filepos += readb;
+ return readb;
}
// Runs in the cache thread.