summaryrefslogtreecommitdiffstats
path: root/stream/stream.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2019-06-19 16:48:46 +0200
committerwm4 <wm4@nowhere>2019-09-19 20:37:05 +0200
commite40885d963f8b60d83aa5ea8104985dd20af262f (patch)
tree82c2fd8284e9a81a211dc7755f5e40e97926cd6e /stream/stream.c
parentde3ecc60cb5ac39e727d8bd1fe4f9e3499f8e672 (diff)
downloadmpv-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.c')
-rw-r--r--stream/stream.c53
1 files changed, 27 insertions, 26 deletions
diff --git a/stream/stream.c b/stream/stream.c
index b99e7419ae..61dbf8ce6c 100644
--- a/stream/stream.c
+++ b/stream/stream.c
@@ -213,10 +213,12 @@ static void stream_resize_buffer(struct stream *s, int new)
}
}
-static int open_internal(const stream_info_t *sinfo, const char *url, int flags,
- struct mp_cancel *c, struct mpv_global *global,
- struct stream **ret)
+int stream_create_instance(const stream_info_t *sinfo, const char *url, int flags,
+ struct mp_cancel *c, struct mpv_global *global,
+ void *arg, struct stream **ret)
{
+ *ret = NULL;
+
if (!sinfo->is_safe && (flags & STREAM_SAFE_ONLY))
return STREAM_UNSAFE;
if (!sinfo->is_network && (flags & STREAM_NETWORK_ONLY))
@@ -233,7 +235,11 @@ static int open_internal(const stream_info_t *sinfo, const char *url, int flags,
return STREAM_NO_MATCH;
stream_t *s = talloc_zero(NULL, stream_t);
- s->log = mp_log_new(s, global->log, sinfo->name);
+ if (flags & STREAM_SILENT) {
+ s->log = mp_null_log;
+ } else {
+ s->log = mp_log_new(s, global->log, sinfo->name);
+ }
s->info = sinfo;
s->cancel = c;
s->global = global;
@@ -242,21 +248,30 @@ static int open_internal(const stream_info_t *sinfo, const char *url, int flags,
s->is_network = sinfo->is_network;
s->mode = flags & (STREAM_READ | STREAM_WRITE);
- if (global->config) {
- int opt;
- mp_read_option_raw(global, "access-references", &m_option_type_flag, &opt);
- s->access_references = opt;
- }
+ int opt;
+ mp_read_option_raw(global, "access-references", &m_option_type_flag, &opt);
+ s->access_references = opt;
MP_VERBOSE(s, "Opening %s\n", url);
+ if (strlen(url) > INT_MAX / 8) {
+ MP_ERR(s, "URL too large.\n");
+ talloc_free(s);
+ return STREAM_ERROR;
+ }
+
if ((s->mode & STREAM_WRITE) && !sinfo->can_write) {
MP_DBG(s, "No write access implemented.\n");
talloc_free(s);
return STREAM_NO_MATCH;
}
- int r = (sinfo->open)(s);
+ int r = STREAM_UNSUPPORTED;
+ if (sinfo->open2) {
+ r = sinfo->open2(s, arg);
+ } else if (!arg) {
+ r = (sinfo->open)(s);
+ }
if (r != STREAM_OK) {
talloc_free(s);
return r;
@@ -286,13 +301,11 @@ struct stream *stream_create(const char *url, int flags,
struct stream *s = NULL;
assert(url);
- if (strlen(url) > INT_MAX / 8)
- goto done;
-
// Open stream proper
bool unsafe = false;
for (int i = 0; stream_list[i]; i++) {
- int r = open_internal(stream_list[i], url, flags, c, global, &s);
+ int r = stream_create_instance(stream_list[i], url, flags, c, global,
+ NULL, &s);
if (r == STREAM_OK)
break;
if (r == STREAM_NO_MATCH || r == STREAM_UNSUPPORTED)
@@ -617,18 +630,6 @@ void free_stream(stream_t *s)
talloc_free(s);
}
-stream_t *open_memory_stream(void *data, int len)
-{
- assert(len >= 0);
- struct mpv_global *dummy = talloc_zero(NULL, struct mpv_global);
- dummy->log = mp_null_log;
- stream_t *s = stream_open("memory://", dummy);
- MP_HANDLE_OOM(s);
- talloc_steal(s, dummy);
- stream_control(s, STREAM_CTRL_SET_CONTENTS, &(bstr){data, len});
- return s;
-}
-
static uint16_t stream_read_word_endian(stream_t *s, bool big_endian)
{
unsigned int y = stream_read_char(s);