diff options
author | wm4 <wm4@nowhere> | 2019-06-19 16:48:46 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2019-09-19 20:37:05 +0200 |
commit | e40885d963f8b60d83aa5ea8104985dd20af262f (patch) | |
tree | 82c2fd8284e9a81a211dc7755f5e40e97926cd6e /stream/stream_memory.c | |
parent | de3ecc60cb5ac39e727d8bd1fe4f9e3499f8e672 (diff) | |
download | mpv-e40885d963f8b60d83aa5ea8104985dd20af262f.tar.bz2 mpv-e40885d963f8b60d83aa5ea8104985dd20af262f.tar.xz |
stream: create memory streams in more straightforward way
Instead of having to rely on the protocol matching, make a function that
creates a stream from a stream_info_t directly. Instead of going through
a weird indirection with STREAM_CTRL, add a direct argument for non-text
arguments to the open callback. Instead of creating a weird dummy
mpv_global, just pass an existing one from all callers. (The latter one
is just an artifact from the past, where mpv_global wasn't available
everywhere.)
Actually I just wanted a function that creates a stream without any of
that bullshit. This goal was slightly missed, since you still need this
heavy "constructor" just to setup a shitty struct with some shitty
callbacks.
Diffstat (limited to 'stream/stream_memory.c')
-rw-r--r-- | stream/stream_memory.c | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/stream/stream_memory.c b/stream/stream_memory.c index 8df043201e..107fa80d19 100644 --- a/stream/stream_memory.c +++ b/stream/stream_memory.c @@ -17,6 +17,7 @@ #include <libavutil/common.h> +#include "common/common.h" #include "stream.h" struct priv { @@ -42,20 +43,14 @@ static int seek(stream_t *s, int64_t newpos) static int control(stream_t *s, int cmd, void *arg) { struct priv *p = s->priv; - switch(cmd) { - case STREAM_CTRL_GET_SIZE: + if (cmd == STREAM_CTRL_GET_SIZE) { *(int64_t *)arg = p->data.len; return 1; - case STREAM_CTRL_SET_CONTENTS: ; - bstr *data = (bstr *)arg; - talloc_free(p->data.start); - p->data = bstrdup(s, *data); - return 1; } return STREAM_UNSUPPORTED; } -static int open_f(stream_t *stream) +static int open2(stream_t *stream, void *arg) { stream->fill_buffer = fill_buffer; stream->seek = seek; @@ -71,7 +66,11 @@ static int open_f(stream_t *stream) bool use_hex = bstr_eatstart0(&data, "hex://"); if (!use_hex) bstr_eatstart0(&data, "memory://"); - stream_control(stream, STREAM_CTRL_SET_CONTENTS, &data); + + if (arg) + data = *(bstr *)arg; + + p->data = bstrdup(stream, data); if (use_hex && !bstr_decode_hex(stream, p->data, &p->data)) { MP_FATAL(stream, "Invalid data.\n"); @@ -83,6 +82,18 @@ static int open_f(stream_t *stream) const stream_info_t stream_info_memory = { .name = "memory", - .open = open_f, + .open2 = open2, .protocols = (const char*const[]){ "memory", "hex", NULL }, }; + +struct stream *stream_memory_open(struct mpv_global *global, void *data, int len) +{ + assert(len >= 0); + + struct stream *s = NULL; + stream_create_instance(&stream_info_memory, "memory://", + STREAM_READ | STREAM_SILENT, NULL, global, + &(bstr){data, len}, &s); + MP_HANDLE_OOM(s); + return s; +} |