summaryrefslogtreecommitdiffstats
path: root/stream
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2015-08-18 00:10:54 +0200
committerwm4 <wm4@nowhere>2015-08-18 00:10:54 +0200
commitcf2fa9d3e5bd6b8ca3e1637080b665e896ea60a0 (patch)
tree1de6ea60ee9f38ad5b7b66d7d15ee7a806a1520b /stream
parentbf5eac8dd3f142acd0d506407f1ae853ada3c5bc (diff)
downloadmpv-cf2fa9d3e5bd6b8ca3e1637080b665e896ea60a0.tar.bz2
mpv-cf2fa9d3e5bd6b8ca3e1637080b665e896ea60a0.tar.xz
stream: provide a stream_get_size() convenience function
And use it everywhere, instead of retrieving the size manually. Slight simplification.
Diffstat (limited to 'stream')
-rw-r--r--stream/cache.c6
-rw-r--r--stream/cache_file.c3
-rw-r--r--stream/stream.c15
-rw-r--r--stream/stream.h1
-rw-r--r--stream/stream_libarchive.c3
5 files changed, 17 insertions, 11 deletions
diff --git a/stream/cache.c b/stream/cache.c
index b4d7ad3664..43b7eba329 100644
--- a/stream/cache.c
+++ b/stream/cache.c
@@ -355,7 +355,8 @@ static void update_cached_controls(struct priv *s)
s->stream_metadata = talloc_steal(s, tags);
}
s->stream_size = s->eof_pos;
- if (stream_control(s->stream, STREAM_CTRL_GET_SIZE, &i64) == STREAM_OK)
+ i64 = stream_get_size(s->stream);
+ if (i64 >= 0)
s->stream_size = i64;
s->has_avseek = stream_control(s->stream, STREAM_CTRL_HAS_AVSEEK, NULL) > 0;
}
@@ -625,8 +626,7 @@ int stream_cache_init(stream_t *cache, stream_t *stream,
int64_t cache_size = opts->size * 1024ULL;
- int64_t file_size = -1;
- stream_control(stream, STREAM_CTRL_GET_SIZE, &file_size);
+ int64_t file_size = stream_get_size(stream);
if (file_size >= 0)
cache_size = MPMIN(cache_size, file_size);
diff --git a/stream/cache_file.c b/stream/cache_file.c
index 94b3f4bda2..901b3f6f3b 100644
--- a/stream/cache_file.c
+++ b/stream/cache_file.c
@@ -66,8 +66,7 @@ static int fill_buffer(stream_t *s, char *buffer, int max_len)
}
// Size of file changes -> invalidate last block
if (s->pos >= p->size - BLOCK_SIZE) {
- int64_t new_size = -1;
- stream_control(s, STREAM_CTRL_GET_SIZE, &new_size);
+ int64_t new_size = stream_get_size(s);
if (p->size >= 0 && new_size != p->size)
set_bit(p, BLOCK_ALIGN(p->size), 0);
p->size = MPMIN(p->max_size, new_size);
diff --git a/stream/stream.c b/stream/stream.c
index 48ae58d639..4f8ee119fb 100644
--- a/stream/stream.c
+++ b/stream/stream.c
@@ -468,8 +468,7 @@ static int stream_read_unbuffered(stream_t *s, void *buf, int len)
// just in case this is an error e.g. due to network
// timeout reset and retry
// do not retry if this looks like proper eof
- int64_t size = -1;
- stream_control(s, STREAM_CTRL_GET_SIZE, &size);
+ int64_t size = stream_get_size(s);
if (!s->eof && s->pos != size && stream_reconnect(s)) {
s->eof = 1; // make sure EOF is set to ensure no endless recursion
return stream_read_unbuffered(s, buf, orig_len);
@@ -702,6 +701,15 @@ int stream_control(stream_t *s, int cmd, void *arg)
return s->control ? s->control(s, cmd, arg) : STREAM_UNSUPPORTED;
}
+// Return the current size of the stream, or a negative value if unknown.
+int64_t stream_get_size(stream_t *s)
+{
+ int64_t size = -1;
+ if (stream_control(s, STREAM_CTRL_GET_SIZE, &size) != STREAM_OK)
+ size = -1;
+ return size;
+}
+
void free_stream(stream_t *s)
{
if (!s)
@@ -908,8 +916,7 @@ struct bstr stream_read_complete(struct stream *s, void *talloc_ctx,
int total_read = 0;
int padding = 1;
char *buf = NULL;
- int64_t size = 0;
- stream_control(s, STREAM_CTRL_GET_SIZE, &size);
+ int64_t size = stream_get_size(s);
if (size > max_size)
return (struct bstr){NULL, 0};
if (size > 0)
diff --git a/stream/stream.h b/stream/stream.h
index b9ddc8c72f..28a6ba6bc9 100644
--- a/stream/stream.h
+++ b/stream/stream.h
@@ -251,6 +251,7 @@ int stream_read(stream_t *s, char *mem, int total);
int stream_read_partial(stream_t *s, char *buf, int buf_size);
struct bstr stream_peek(stream_t *s, int len);
void stream_drop_buffers(stream_t *s);
+int64_t stream_get_size(stream_t *s);
struct mpv_global;
diff --git a/stream/stream_libarchive.c b/stream/stream_libarchive.c
index 4dbb7bde08..475febc726 100644
--- a/stream/stream_libarchive.c
+++ b/stream/stream_libarchive.c
@@ -42,8 +42,7 @@ static ssize_t seek_cb(struct archive *arch, void *priv,
offset += mpa->src->pos;
break;
case SEEK_END: ;
- int64_t size = -1;
- stream_control(mpa->src, STREAM_CTRL_GET_SIZE, &size);
+ int64_t size = stream_get_size(mpa->src);
if (size < 0)
return -1;
offset += size;