mirror of
https://github.com/mpv-player/mpv
synced 2024-11-14 22:48:35 +01:00
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.
This commit is contained in:
parent
de3ecc60cb
commit
e40885d963
@ -2968,7 +2968,7 @@ void demux_close_stream(struct demuxer *demuxer)
|
|||||||
|
|
||||||
MP_VERBOSE(demuxer, "demuxer read all data; closing stream\n");
|
MP_VERBOSE(demuxer, "demuxer read all data; closing stream\n");
|
||||||
free_stream(demuxer->stream);
|
free_stream(demuxer->stream);
|
||||||
demuxer->stream = open_memory_stream(NULL, 0); // dummy
|
demuxer->stream = stream_memory_open(demuxer->global, NULL, 0); // dummy
|
||||||
demuxer->stream->cancel = demuxer->cancel;
|
demuxer->stream->cancel = demuxer->cancel;
|
||||||
in->d_user->stream = demuxer->stream;
|
in->d_user->stream = demuxer->stream;
|
||||||
}
|
}
|
||||||
|
@ -396,7 +396,7 @@ static void convert_charset(struct demuxer *demuxer)
|
|||||||
data = conv;
|
data = conv;
|
||||||
}
|
}
|
||||||
if (data.start) {
|
if (data.start) {
|
||||||
priv->stream = open_memory_stream(data.start, data.len);
|
priv->stream = stream_memory_open(demuxer->global, data.start, data.len);
|
||||||
priv->own_stream = true;
|
priv->own_stream = true;
|
||||||
}
|
}
|
||||||
talloc_free(alloc);
|
talloc_free(alloc);
|
||||||
|
@ -45,7 +45,8 @@ static int open_file(struct demuxer *demuxer, enum demux_check check)
|
|||||||
bstr probe = stream_peek(demuxer->stream, probe_size);
|
bstr probe = stream_peek(demuxer->stream, probe_size);
|
||||||
if (probe.len == 0)
|
if (probe.len == 0)
|
||||||
return -1;
|
return -1;
|
||||||
struct stream *probe_stream = open_memory_stream(probe.start, probe.len);
|
struct stream *probe_stream =
|
||||||
|
stream_memory_open(demuxer->global, probe.start, probe.len);
|
||||||
struct mp_archive *mpa = mp_archive_new(mp_null_log, probe_stream, flags);
|
struct mp_archive *mpa = mp_archive_new(mp_null_log, probe_stream, flags);
|
||||||
bool ok = !!mpa;
|
bool ok = !!mpa;
|
||||||
free_stream(probe_stream);
|
free_stream(probe_stream);
|
||||||
|
@ -366,7 +366,7 @@ static int open_file(struct demuxer *demuxer, enum demux_check check)
|
|||||||
p->add_base = true;
|
p->add_base = true;
|
||||||
|
|
||||||
bstr probe_buf = stream_peek(demuxer->stream, PROBE_SIZE);
|
bstr probe_buf = stream_peek(demuxer->stream, PROBE_SIZE);
|
||||||
p->s = open_memory_stream(probe_buf.start, probe_buf.len);
|
p->s = stream_memory_open(demuxer->global, probe_buf.start, probe_buf.len);
|
||||||
p->s->mime_type = demuxer->stream->mime_type;
|
p->s->mime_type = demuxer->stream->mime_type;
|
||||||
p->utf16 = stream_skip_bom(p->s);
|
p->utf16 = stream_skip_bom(p->s);
|
||||||
p->force = force;
|
p->force = force;
|
||||||
|
@ -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,
|
int stream_create_instance(const stream_info_t *sinfo, const char *url, int flags,
|
||||||
struct mp_cancel *c, struct mpv_global *global,
|
struct mp_cancel *c, struct mpv_global *global,
|
||||||
struct stream **ret)
|
void *arg, struct stream **ret)
|
||||||
{
|
{
|
||||||
|
*ret = NULL;
|
||||||
|
|
||||||
if (!sinfo->is_safe && (flags & STREAM_SAFE_ONLY))
|
if (!sinfo->is_safe && (flags & STREAM_SAFE_ONLY))
|
||||||
return STREAM_UNSAFE;
|
return STREAM_UNSAFE;
|
||||||
if (!sinfo->is_network && (flags & STREAM_NETWORK_ONLY))
|
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;
|
return STREAM_NO_MATCH;
|
||||||
|
|
||||||
stream_t *s = talloc_zero(NULL, stream_t);
|
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->info = sinfo;
|
||||||
s->cancel = c;
|
s->cancel = c;
|
||||||
s->global = global;
|
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->is_network = sinfo->is_network;
|
||||||
s->mode = flags & (STREAM_READ | STREAM_WRITE);
|
s->mode = flags & (STREAM_READ | STREAM_WRITE);
|
||||||
|
|
||||||
if (global->config) {
|
int opt;
|
||||||
int opt;
|
mp_read_option_raw(global, "access-references", &m_option_type_flag, &opt);
|
||||||
mp_read_option_raw(global, "access-references", &m_option_type_flag, &opt);
|
s->access_references = opt;
|
||||||
s->access_references = opt;
|
|
||||||
}
|
|
||||||
|
|
||||||
MP_VERBOSE(s, "Opening %s\n", url);
|
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) {
|
if ((s->mode & STREAM_WRITE) && !sinfo->can_write) {
|
||||||
MP_DBG(s, "No write access implemented.\n");
|
MP_DBG(s, "No write access implemented.\n");
|
||||||
talloc_free(s);
|
talloc_free(s);
|
||||||
return STREAM_NO_MATCH;
|
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) {
|
if (r != STREAM_OK) {
|
||||||
talloc_free(s);
|
talloc_free(s);
|
||||||
return r;
|
return r;
|
||||||
@ -286,13 +301,11 @@ struct stream *stream_create(const char *url, int flags,
|
|||||||
struct stream *s = NULL;
|
struct stream *s = NULL;
|
||||||
assert(url);
|
assert(url);
|
||||||
|
|
||||||
if (strlen(url) > INT_MAX / 8)
|
|
||||||
goto done;
|
|
||||||
|
|
||||||
// Open stream proper
|
// Open stream proper
|
||||||
bool unsafe = false;
|
bool unsafe = false;
|
||||||
for (int i = 0; stream_list[i]; i++) {
|
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)
|
if (r == STREAM_OK)
|
||||||
break;
|
break;
|
||||||
if (r == STREAM_NO_MATCH || r == STREAM_UNSUPPORTED)
|
if (r == STREAM_NO_MATCH || r == STREAM_UNSUPPORTED)
|
||||||
@ -617,18 +630,6 @@ void free_stream(stream_t *s)
|
|||||||
talloc_free(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)
|
static uint16_t stream_read_word_endian(stream_t *s, bool big_endian)
|
||||||
{
|
{
|
||||||
unsigned int y = stream_read_char(s);
|
unsigned int y = stream_read_char(s);
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
// flags for stream_open_ext (this includes STREAM_READ and STREAM_WRITE)
|
// flags for stream_open_ext (this includes STREAM_READ and STREAM_WRITE)
|
||||||
#define STREAM_SAFE_ONLY 4
|
#define STREAM_SAFE_ONLY 4
|
||||||
#define STREAM_NETWORK_ONLY 8
|
#define STREAM_NETWORK_ONLY 8
|
||||||
|
#define STREAM_SILENT 16
|
||||||
|
|
||||||
#define STREAM_UNSAFE -3
|
#define STREAM_UNSAFE -3
|
||||||
#define STREAM_NO_MATCH -2
|
#define STREAM_NO_MATCH -2
|
||||||
@ -47,9 +48,6 @@
|
|||||||
enum stream_ctrl {
|
enum stream_ctrl {
|
||||||
STREAM_CTRL_GET_SIZE = 1,
|
STREAM_CTRL_GET_SIZE = 1,
|
||||||
|
|
||||||
// stream_memory.c
|
|
||||||
STREAM_CTRL_SET_CONTENTS,
|
|
||||||
|
|
||||||
// Certain network protocols
|
// Certain network protocols
|
||||||
STREAM_CTRL_AVSEEK,
|
STREAM_CTRL_AVSEEK,
|
||||||
STREAM_CTRL_HAS_AVSEEK,
|
STREAM_CTRL_HAS_AVSEEK,
|
||||||
@ -72,6 +70,8 @@ typedef struct stream_info_st {
|
|||||||
const char *name;
|
const char *name;
|
||||||
// opts is set from ->opts
|
// opts is set from ->opts
|
||||||
int (*open)(struct stream *st);
|
int (*open)(struct stream *st);
|
||||||
|
// Alternative to open(). Only either open() or open2() can be set.
|
||||||
|
int (*open2)(struct stream *st, void *arg);
|
||||||
const char *const *protocols;
|
const char *const *protocols;
|
||||||
bool can_write; // correctly checks for READ/WRITE modes
|
bool can_write; // correctly checks for READ/WRITE modes
|
||||||
bool is_safe; // opening is no security issue, even with remote provided URLs
|
bool is_safe; // opening is no security issue, even with remote provided URLs
|
||||||
@ -165,15 +165,20 @@ struct bstr stream_read_file(const char *filename, void *talloc_ctx,
|
|||||||
struct mpv_global *global, int max_size);
|
struct mpv_global *global, int max_size);
|
||||||
int stream_control(stream_t *s, int cmd, void *arg);
|
int stream_control(stream_t *s, int cmd, void *arg);
|
||||||
void free_stream(stream_t *s);
|
void free_stream(stream_t *s);
|
||||||
|
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);
|
||||||
struct stream *stream_create(const char *url, int flags,
|
struct stream *stream_create(const char *url, int flags,
|
||||||
struct mp_cancel *c, struct mpv_global *global);
|
struct mp_cancel *c, struct mpv_global *global);
|
||||||
struct stream *stream_open(const char *filename, struct mpv_global *global);
|
struct stream *stream_open(const char *filename, struct mpv_global *global);
|
||||||
stream_t *open_output_stream(const char *filename, struct mpv_global *global);
|
stream_t *open_output_stream(const char *filename, struct mpv_global *global);
|
||||||
stream_t *open_memory_stream(void *data, int len);
|
|
||||||
|
|
||||||
void mp_url_unescape_inplace(char *buf);
|
void mp_url_unescape_inplace(char *buf);
|
||||||
char *mp_url_escape(void *talloc_ctx, const char *s, const char *ok);
|
char *mp_url_escape(void *talloc_ctx, const char *s, const char *ok);
|
||||||
|
|
||||||
|
// stream_memory.c
|
||||||
|
struct stream *stream_memory_open(struct mpv_global *global, void *data, int len);
|
||||||
|
|
||||||
// stream_file.c
|
// stream_file.c
|
||||||
char *mp_file_url_to_filename(void *talloc_ctx, bstr url);
|
char *mp_file_url_to_filename(void *talloc_ctx, bstr url);
|
||||||
char *mp_file_get_path(void *talloc_ctx, bstr url);
|
char *mp_file_get_path(void *talloc_ctx, bstr url);
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
#include <libavutil/common.h>
|
#include <libavutil/common.h>
|
||||||
|
|
||||||
|
#include "common/common.h"
|
||||||
#include "stream.h"
|
#include "stream.h"
|
||||||
|
|
||||||
struct priv {
|
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)
|
static int control(stream_t *s, int cmd, void *arg)
|
||||||
{
|
{
|
||||||
struct priv *p = s->priv;
|
struct priv *p = s->priv;
|
||||||
switch(cmd) {
|
if (cmd == STREAM_CTRL_GET_SIZE) {
|
||||||
case STREAM_CTRL_GET_SIZE:
|
|
||||||
*(int64_t *)arg = p->data.len;
|
*(int64_t *)arg = p->data.len;
|
||||||
return 1;
|
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;
|
return STREAM_UNSUPPORTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int open_f(stream_t *stream)
|
static int open2(stream_t *stream, void *arg)
|
||||||
{
|
{
|
||||||
stream->fill_buffer = fill_buffer;
|
stream->fill_buffer = fill_buffer;
|
||||||
stream->seek = seek;
|
stream->seek = seek;
|
||||||
@ -71,7 +66,11 @@ static int open_f(stream_t *stream)
|
|||||||
bool use_hex = bstr_eatstart0(&data, "hex://");
|
bool use_hex = bstr_eatstart0(&data, "hex://");
|
||||||
if (!use_hex)
|
if (!use_hex)
|
||||||
bstr_eatstart0(&data, "memory://");
|
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)) {
|
if (use_hex && !bstr_decode_hex(stream, p->data, &p->data)) {
|
||||||
MP_FATAL(stream, "Invalid data.\n");
|
MP_FATAL(stream, "Invalid data.\n");
|
||||||
@ -83,6 +82,18 @@ static int open_f(stream_t *stream)
|
|||||||
|
|
||||||
const stream_info_t stream_info_memory = {
|
const stream_info_t stream_info_memory = {
|
||||||
.name = "memory",
|
.name = "memory",
|
||||||
.open = open_f,
|
.open2 = open2,
|
||||||
.protocols = (const char*const[]){ "memory", "hex", NULL },
|
.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;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user