1
0
mirror of https://github.com/mpv-player/mpv synced 2024-12-18 04:45:33 +00:00

stream: provide a stream_get_size() convenience function

And use it everywhere, instead of retrieving the size manually. Slight
simplification.
This commit is contained in:
wm4 2015-08-18 00:10:54 +02:00
parent bf5eac8dd3
commit cf2fa9d3e5
9 changed files with 27 additions and 30 deletions

View File

@ -1300,7 +1300,6 @@ static void update_cache(struct demux_internal *in)
// Don't lock while querying the stream.
double time_length = -1;
struct mp_tags *stream_metadata = NULL;
int64_t stream_size = -1;
int64_t stream_cache_size = -1;
int64_t stream_cache_fill = -1;
int stream_cache_idle = -1;
@ -1310,8 +1309,8 @@ static void update_cache(struct demux_internal *in)
&time_length);
}
int64_t stream_size = stream_get_size(stream);
stream_control(stream, STREAM_CTRL_GET_METADATA, &stream_metadata);
stream_control(stream, STREAM_CTRL_GET_SIZE, &stream_size);
stream_control(stream, STREAM_CTRL_GET_CACHE_SIZE, &stream_cache_size);
stream_control(stream, STREAM_CTRL_GET_CACHE_FILL, &stream_cache_fill);
stream_control(stream, STREAM_CTRL_GET_CACHE_IDLE, &stream_cache_idle);

View File

@ -204,8 +204,8 @@ static int64_t mp_seek(void *opaque, int64_t pos, int whence)
int64_t current_pos;
MP_TRACE(demuxer, "mp_seek(%p, %"PRId64", %d)\n", stream, pos, whence);
if (whence == SEEK_END || whence == AVSEEK_SIZE) {
int64_t end;
if (stream_control(stream, STREAM_CTRL_GET_SIZE, &end) != STREAM_OK)
int64_t end = stream_get_size(stream);
if (end < 0)
return -1;
if (whence == AVSEEK_SIZE)
return end;
@ -917,8 +917,7 @@ static void demux_seek_lavf(demuxer_t *demuxer, double rel_seek_secs, int flags)
if (flags & SEEK_FACTOR) {
struct stream *s = demuxer->stream;
int64_t end = 0;
stream_control(s, STREAM_CTRL_GET_SIZE, &end);
int64_t end = stream_get_size(s);
if (end > 0 && demuxer->ts_resets_possible &&
!(priv->avif_flags & AVFMT_NO_BYTE_SEEK))
{

View File

@ -1768,9 +1768,7 @@ static int read_mkv_segment_header(demuxer_t *demuxer, int64_t *segment_end)
MP_VERBOSE(demuxer, " (skipping)\n");
if (*segment_end <= 0)
break;
int64_t end = 0;
stream_control(s, STREAM_CTRL_GET_SIZE, &end);
if (*segment_end >= end)
if (*segment_end >= stream_get_size(s))
return 0;
if (!stream_seek(s, *segment_end)) {
MP_WARN(demuxer, "Failed to seek in file\n");
@ -1834,8 +1832,7 @@ static int demux_mkv_open(demuxer_t *demuxer, enum demux_check check)
return -1;
}
int64_t end = 0;
stream_control(s, STREAM_CTRL_GET_SIZE, &end);
int64_t end = stream_get_size(s);
// Read headers that come after the first cluster (i.e. require seeking).
// Note: reading might increase ->num_headers.
@ -2800,9 +2797,7 @@ static void demux_mkv_seek(demuxer_t *demuxer, double rel_seek_secs, int flags)
read_deferred_cues(demuxer);
int64_t size = 0;
stream_control(s, STREAM_CTRL_GET_SIZE, &size);
int64_t size = stream_get_size(s);
int64_t target_filepos = size * MPCLAMP(rel_seek_secs, 0, 1);
mkv_index_t *index = NULL;
@ -2824,7 +2819,7 @@ static void demux_mkv_seek(demuxer_t *demuxer, double rel_seek_secs, int flags)
stream_seek(s, index->filepos);
mkv_d->skip_to_timecode = index->timecode * mkv_d->tc_scale;
} else {
stream_seek(s, target_filepos);
stream_seek(s, MPMAX(target_filepos, 0));
if (ebml_resync_cluster(mp_null_log, s) < 0) {
// Assume EOF
mkv_d->cluster_end = size;
@ -2877,8 +2872,7 @@ static void probe_last_timestamp(struct demuxer *demuxer)
return;
} else {
// No index -> just try to find a random cluster towards file end.
int64_t size = 0;
stream_control(demuxer->stream, STREAM_CTRL_GET_SIZE, &size);
int64_t size = stream_get_size(demuxer->stream);
stream_seek(demuxer->stream, MPMAX(size - 10 * 1024 * 1024, 0));
if (ebml_resync_cluster(mp_null_log, demuxer->stream) < 0)
stream_seek(demuxer->stream, old_pos); // full scan otherwise

View File

@ -208,8 +208,7 @@ int stream_dump(struct MPContext *mpctx, const char *source_filename)
if (!stream)
return -1;
int64_t size = 0;
stream_control(stream, STREAM_CTRL_GET_SIZE, &size);
int64_t size = stream_get_size(stream);
stream_set_capture_file(stream, opts->stream_dump);

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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;

View File

@ -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;