2013-06-27 15:21:46 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
|
|
|
#include "stream.h"
|
|
|
|
|
|
|
|
struct priv {
|
|
|
|
bstr data;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int fill_buffer(stream_t *s, char* buffer, int len)
|
|
|
|
{
|
|
|
|
struct priv *p = s->priv;
|
|
|
|
bstr data = p->data;
|
|
|
|
if (s->pos < 0 || s->pos > data.len)
|
|
|
|
return 0;
|
|
|
|
len = FFMIN(len, data.len - s->pos);
|
|
|
|
memcpy(buffer, data.start + s->pos, len);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int seek(stream_t *s, int64_t newpos)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int control(stream_t *s, int cmd, void *arg)
|
|
|
|
{
|
|
|
|
struct priv *p = s->priv;
|
|
|
|
switch(cmd) {
|
2014-05-24 12:04:09 +00:00
|
|
|
case STREAM_CTRL_GET_SIZE:
|
|
|
|
*(int64_t *)arg = p->data.len;
|
|
|
|
return 1;
|
2013-06-27 15:21:46 +00:00
|
|
|
case STREAM_CTRL_SET_CONTENTS: ;
|
|
|
|
bstr *data = (bstr *)arg;
|
|
|
|
talloc_free(p->data.start);
|
|
|
|
p->data = bstrdup(s, *data);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return STREAM_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
2014-05-24 12:06:13 +00:00
|
|
|
static int open_f(stream_t *stream)
|
2013-06-27 15:21:46 +00:00
|
|
|
{
|
|
|
|
stream->fill_buffer = fill_buffer;
|
|
|
|
stream->seek = seek;
|
2014-05-24 12:04:09 +00:00
|
|
|
stream->seekable = true;
|
2013-06-27 15:21:46 +00:00
|
|
|
stream->control = control;
|
|
|
|
stream->read_chunk = 1024 * 1024;
|
|
|
|
|
|
|
|
struct priv *p = talloc_zero(stream, struct priv);
|
|
|
|
stream->priv = p;
|
|
|
|
|
|
|
|
// Initial data
|
|
|
|
bstr data = bstr0(stream->url);
|
|
|
|
bstr_eatstart0(&data, "memory://");
|
|
|
|
stream_control(stream, STREAM_CTRL_SET_CONTENTS, &data);
|
|
|
|
|
|
|
|
return STREAM_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
const stream_info_t stream_info_memory = {
|
stream: fix url_options field, make protocols field not fixed length
The way the url_options field was handled was not entirely sane: it's
actually a flexible array member, so it points to garbage for streams
which do not initialize this member (it just points to the data right
after the struct, which is garbage in theory and practice). This was
not actually a problem, since the field is only used if priv_size is
set (due to how this stuff is used). But it doesn't allow setting
priv_size only, which might be useful in some cases.
Also, make the protocols array not a fixed size array. Most stream
implementations have only 1 protocol prefix, but stream_lavf.c has
over 10 (whitelists ffmpeg protocols). The high size of the fixed
size protocol array wastes space, and it is _still_ annoying to
add new prefixes to stream_lavf (have to bump the maximum length),
so make it arbitrary length.
The two changes (plus some more cosmetic changes) arte conflated into
one, because it was annoying going over all the stream implementations.
2013-08-25 20:49:27 +00:00
|
|
|
.name = "memory",
|
|
|
|
.open = open_f,
|
2014-06-10 21:56:05 +00:00
|
|
|
.protocols = (const char*const[]){ "memory", NULL },
|
2013-06-27 15:21:46 +00:00
|
|
|
};
|