From e40885d963f8b60d83aa5ea8104985dd20af262f Mon Sep 17 00:00:00 2001 From: wm4 Date: Wed, 19 Jun 2019 16:48:46 +0200 Subject: 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. --- stream/stream_memory.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'stream/stream_memory.c') 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 +#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; +} -- cgit v1.2.3