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.
This commit is contained in:
wm4 2019-11-07 15:54:34 +01:00
parent ca75fedaf4
commit e5a9b792ec
11 changed files with 46 additions and 92 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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