From e5a9b792ecf08ddbcf3b674de3a00f7a919d1858 Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 7 Nov 2019 15:54:34 +0100 Subject: stream: replace STREAM_CTRL_GET_SIZE with a proper entrypoint This is overlay convoluted as a stream control, and important enough to warrant "first class" functionality. --- demux/demux_raw.c | 9 ++++----- stream/stream.c | 5 +---- stream/stream.h | 4 ++-- stream/stream_cb.c | 21 ++++++++------------- stream/stream_cdda.c | 11 +++++++---- stream/stream_concat.c | 10 +++------- stream/stream_file.c | 17 +---------------- stream/stream_lavf.c | 15 +++++++-------- stream/stream_libarchive.c | 13 +++---------- stream/stream_memory.c | 10 +++------- stream/stream_smb.c | 23 +++++++---------------- 11 files changed, 46 insertions(+), 92 deletions(-) diff --git a/demux/demux_raw.c b/demux/demux_raw.c index 7469d80c5e..de657feb8c 100644 --- a/demux/demux_raw.c +++ b/demux/demux_raw.c @@ -137,8 +137,8 @@ static int generic_open(struct demuxer *demuxer) struct stream *s = demuxer->stream; struct priv *p = demuxer->priv; - int64_t end = 0; - if (stream_control(s, STREAM_CTRL_GET_SIZE, &end) == STREAM_OK) + int64_t end = stream_get_size(s); + if (end >= 0) demuxer->duration = (end / p->frame_size) / p->frame_rate; return 0; @@ -299,8 +299,7 @@ static void raw_seek(demuxer_t *demuxer, double seek_pts, int flags) { struct priv *p = demuxer->priv; stream_t *s = demuxer->stream; - int64_t end = 0; - stream_control(s, STREAM_CTRL_GET_SIZE, &end); + int64_t end = stream_get_size(s); int64_t frame_nr = seek_pts * p->frame_rate; frame_nr = frame_nr - (frame_nr % p->read_frames); int64_t pos = frame_nr * p->frame_size; @@ -308,7 +307,7 @@ static void raw_seek(demuxer_t *demuxer, double seek_pts, int flags) pos = end * seek_pts; if (pos < 0) pos = 0; - if (end && pos > end) + if (end > 0 && pos > end) pos = end; stream_seek(s, (pos / p->frame_size) * p->frame_size); } diff --git a/stream/stream.c b/stream/stream.c index 1f77a5dd1e..7fbc0d3373 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -710,10 +710,7 @@ int stream_control(stream_t *s, int cmd, void *arg) // 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; + return s->get_size ? s->get_size(s) : -1; } void free_stream(stream_t *s) diff --git a/stream/stream.h b/stream/stream.h index 081313cdc8..55db9c1314 100644 --- a/stream/stream.h +++ b/stream/stream.h @@ -48,8 +48,6 @@ #define STREAM_OK 1 enum stream_ctrl { - STREAM_CTRL_GET_SIZE = 1, - // Certain network protocols STREAM_CTRL_AVSEEK, STREAM_CTRL_HAS_AVSEEK, @@ -115,6 +113,8 @@ typedef struct stream { int (*write_buffer)(struct stream *s, void *buffer, int len); // Seek int (*seek)(struct stream *s, int64_t pos); + // Total stream size in bytes (negative if unavailable) + int64_t (*get_size)(struct stream *s); // Control int (*control)(struct stream *s, int cmd, void *arg); // Close diff --git a/stream/stream_cb.c b/stream/stream_cb.c index 5a946a6369..c6f33263a3 100644 --- a/stream/stream_cb.c +++ b/stream/stream_cb.c @@ -36,22 +36,17 @@ static int seek(stream_t *s, int64_t newpos) return p->info.seek_fn(p->info.cookie, newpos) >= 0; } -static int control(stream_t *s, int cmd, void *arg) +static int64_t get_size(stream_t *s) { struct priv *p = s->priv; - switch (cmd) { - case STREAM_CTRL_GET_SIZE: { - if (!p->info.size_fn) - break; + + if (p->info.size_fn) { int64_t size = p->info.size_fn(p->info.cookie); - if (size >= 0) { - *(int64_t *)arg = size; - return 1; - } - break; - } + if (size >= 0) + return size; } - return STREAM_UNSUPPORTED; + + return -1; } static void s_close(stream_t *s) @@ -96,7 +91,7 @@ static int open_cb(stream_t *stream) } stream->fast_skip = true; stream->fill_buffer = fill_buffer; - stream->control = control; + stream->get_size = get_size; stream->read_chunk = 64 * 1024; stream->close = s_close; diff --git a/stream/stream_cdda.c b/stream/stream_cdda.c index cc1c7646a3..158cfd0646 100644 --- a/stream/stream_cdda.c +++ b/stream/stream_cdda.c @@ -262,14 +262,16 @@ static int control(stream_t *stream, int cmd, void *arg) *(double *)arg = pos / (44100.0 * 2 * 2); return STREAM_OK; } - case STREAM_CTRL_GET_SIZE: - *(int64_t *)arg = - (p->end_sector + 1 - p->start_sector) * CDIO_CD_FRAMESIZE_RAW; - return STREAM_OK; } return STREAM_UNSUPPORTED; } +static int64_t get_size(stream_t *st) +{ + cdda_priv *p = st->priv; + return (p->end_sector + 1 - p->start_sector) * CDIO_CD_FRAMESIZE_RAW; +} + static int open_cdda(stream_t *st) { st->priv = mp_get_config_group(st, st->global, &stream_cdda_conf); @@ -383,6 +385,7 @@ static int open_cdda(stream_t *st) st->seek = seek; st->seekable = true; st->control = control; + st->get_size = get_size; st->close = close_cdda; st->streaming = true; diff --git a/stream/stream_concat.c b/stream/stream_concat.c index 339f1cf56c..f45bd6f743 100644 --- a/stream/stream_concat.c +++ b/stream/stream_concat.c @@ -73,14 +73,10 @@ static int seek(struct stream *s, int64_t newpos) return ok; } -static int control(struct stream *s, int cmd, void *arg) +static int64_t get_size(struct stream *s) { struct priv *p = s->priv; - if (cmd == STREAM_CTRL_GET_SIZE && p->size >= 0) { - *(int64_t *)arg = p->size; - return 1; - } - return STREAM_UNSUPPORTED; + return p->size; } static void s_close(struct stream *s) @@ -97,7 +93,7 @@ static int open2(struct stream *stream, struct stream_open_args *args) stream->priv = p; stream->fill_buffer = fill_buffer; - stream->control = control; + stream->get_size = get_size; stream->close = s_close; stream->seekable = true; diff --git a/stream/stream_file.c b/stream/stream_file.c index 6a46c1c2c7..5d9bfd66b3 100644 --- a/stream/stream_file.c +++ b/stream/stream_file.c @@ -132,21 +132,6 @@ static int seek(stream_t *s, int64_t newpos) return lseek(p->fd, newpos, SEEK_SET) != (off_t)-1; } -static int control(stream_t *s, int cmd, void *arg) -{ - switch (cmd) { - case STREAM_CTRL_GET_SIZE: { - int64_t size = get_size(s); - if (size >= 0) { - *(int64_t *)arg = size; - return 1; - } - break; - } - } - return STREAM_UNSUPPORTED; -} - static void s_close(stream_t *s) { struct priv *p = s->priv; @@ -343,7 +328,7 @@ static int open_f(stream_t *stream) stream->fast_skip = true; stream->fill_buffer = fill_buffer; stream->write_buffer = write_buffer; - stream->control = control; + stream->get_size = get_size; stream->read_chunk = 64 * 1024; stream->close = s_close; diff --git a/stream/stream_lavf.c b/stream/stream_lavf.c index 98c66e4f71..5359d8d7d4 100644 --- a/stream/stream_lavf.c +++ b/stream/stream_lavf.c @@ -107,6 +107,12 @@ static int seek(stream_t *s, int64_t newpos) return 1; } +static int64_t get_size(stream_t *s) +{ + AVIOContext *avio = s->priv; + return avio_size(avio); +} + static void close_f(stream_t *stream) { AVIOContext *avio = stream->priv; @@ -122,15 +128,7 @@ static void close_f(stream_t *stream) static int control(stream_t *s, int cmd, void *arg) { AVIOContext *avio = s->priv; - int64_t size; switch(cmd) { - case STREAM_CTRL_GET_SIZE: - size = avio_size(avio); - if (size >= 0) { - *(int64_t *)arg = size; - return 1; - } - break; case STREAM_CTRL_AVSEEK: { struct stream_avseek *c = arg; int64_t r = avio_seek_time(avio, c->stream_index, c->timestamp, c->flags); @@ -333,6 +331,7 @@ static int open_f(stream_t *stream) stream->seek = stream->seekable ? seek : NULL; stream->fill_buffer = fill_buffer; stream->write_buffer = write_buffer; + stream->get_size = get_size; stream->control = control; stream->close = close_f; // enable cache (should be avoided for files, but no way to detect this) diff --git a/stream/stream_libarchive.c b/stream/stream_libarchive.c index 0e136c175d..ab8fda0f34 100644 --- a/stream/stream_libarchive.c +++ b/stream/stream_libarchive.c @@ -468,17 +468,10 @@ static void archive_entry_close(stream_t *s) free_stream(p->src); } -static int archive_entry_control(stream_t *s, int cmd, void *arg) +static int64_t archive_entry_get_size(stream_t *s) { struct priv *p = s->priv; - switch (cmd) { - case STREAM_CTRL_GET_SIZE: - if (p->entry_size < 0) - break; - *(int64_t *)arg = p->entry_size; - return STREAM_OK; - } - return STREAM_UNSUPPORTED; + return p->entry_size; } static int archive_entry_open(stream_t *stream) @@ -514,7 +507,7 @@ static int archive_entry_open(stream_t *stream) stream->seekable = true; } stream->close = archive_entry_close; - stream->control = archive_entry_control; + stream->get_size = archive_entry_get_size; stream->streaming = true; return STREAM_OK; diff --git a/stream/stream_memory.c b/stream/stream_memory.c index f329da4850..5acd05dfea 100644 --- a/stream/stream_memory.c +++ b/stream/stream_memory.c @@ -38,14 +38,10 @@ static int seek(stream_t *s, int64_t newpos) return 1; } -static int control(stream_t *s, int cmd, void *arg) +static int64_t get_size(stream_t *s) { struct priv *p = s->priv; - if (cmd == STREAM_CTRL_GET_SIZE) { - *(int64_t *)arg = p->data.len; - return 1; - } - return STREAM_UNSUPPORTED; + return p->data.len; } static int open2(stream_t *stream, struct stream_open_args *args) @@ -53,7 +49,7 @@ static int open2(stream_t *stream, struct stream_open_args *args) stream->fill_buffer = fill_buffer; stream->seek = seek; stream->seekable = true; - stream->control = control; + stream->get_size = get_size; stream->read_chunk = 1024 * 1024; struct priv *p = talloc_zero(stream, struct priv); diff --git a/stream/stream_smb.c b/stream/stream_smb.c index bc1c00a8ef..a87150a838 100644 --- a/stream/stream_smb.c +++ b/stream/stream_smb.c @@ -46,22 +46,13 @@ static void smb_auth_fn(const char *server, const char *share, workgroup[wgmaxlen - 1] = '\0'; } -static int control(stream_t *s, int cmd, void *arg) { +static int64_t get_size(stream_t *s) { struct priv *p = s->priv; - switch(cmd) { - case STREAM_CTRL_GET_SIZE: { - pthread_mutex_lock(&smb_lock); - off_t size = smbc_lseek(p->fd,0,SEEK_END); - smbc_lseek(p->fd,s->pos,SEEK_SET); - pthread_mutex_unlock(&smb_lock); - if(size != (off_t)-1) { - *(int64_t *)arg = size; - return 1; - } - } - break; - } - return STREAM_UNSUPPORTED; + pthread_mutex_lock(&smb_lock); + off_t size = smbc_lseek(p->fd,0,SEEK_END); + smbc_lseek(p->fd,s->pos,SEEK_SET); + pthread_mutex_unlock(&smb_lock); + return size; } static int seek(stream_t *s,int64_t newpos) { @@ -149,7 +140,7 @@ static int open_f (stream_t *stream) stream->fill_buffer = fill_buffer; stream->write_buffer = write_buffer; stream->close = close_f; - stream->control = control; + stream->get_size = get_size; stream->read_chunk = 128 * 1024; stream->streaming = true; -- cgit v1.2.3