2013-08-25 18:40:21 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2013-08-25 18:40:21 +00:00
|
|
|
*
|
|
|
|
* 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
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-08-25 18:40:21 +00:00
|
|
|
*
|
Relicense some non-MPlayer source files to LGPL 2.1 or later
This covers source files which were added in mplayer2 and mpv times
only, and where all code is covered by LGPL relicensing agreements.
There are probably more files to which this applies, but I'm being
conservative here.
A file named ao_sdl.c exists in MPlayer too, but the mpv one is a
complete rewrite, and was added some time after the original ao_sdl.c
was removed. The same applies to vo_sdl.c, for which the SDL2 API is
radically different in addition (MPlayer supports SDL 1.2 only).
common.c contains only code written by me. But common.h is a strange
case: although it originally was named mp_common.h and exists in MPlayer
too, by now it contains only definitions written by uau and me. The
exceptions are the CONTROL_ defines - thus not changing the license of
common.h yet.
codec_tags.c contained once large tables generated from MPlayer's
codecs.conf, but all of these tables were removed.
From demux_playlist.c I'm removing a code fragment from someone who was
not asked; this probably could be done later (see commit 15dccc37).
misc.c is a bit complicated to reason about (it was split off mplayer.c
and thus contains random functions out of this file), but actually all
functions have been added post-MPlayer. Except get_relative_time(),
which was written by uau, but looks similar to 3 different versions of
something similar in each of the Unix/win32/OSX timer source files. I'm
not sure what that means in regards to copyright, so I've just moved it
into another still-GPL source file for now.
screenshot.c once had some minor parts of MPlayer's vf_screenshot.c, but
they're all gone.
2016-01-19 17:36:06 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-08-25 18:40:21 +00:00
|
|
|
*/
|
|
|
|
|
2015-04-17 20:56:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-07-10 06:28:03 +00:00
|
|
|
|
2019-10-26 14:44:05 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/options.h"
|
2023-02-11 20:08:38 +00:00
|
|
|
#include "options/m_config.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
|
|
|
#include "common/playlist.h"
|
2023-10-02 00:33:05 +00:00
|
|
|
#include "misc/charset_conv.h"
|
2018-05-17 18:58:49 +00:00
|
|
|
#include "misc/thread_tools.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/path.h"
|
2013-08-25 18:40:21 +00:00
|
|
|
#include "stream/stream.h"
|
2015-04-17 20:56:19 +00:00
|
|
|
#include "osdep/io.h"
|
2018-07-27 03:01:35 +00:00
|
|
|
#include "misc/natural_sort.h"
|
2013-08-25 18:40:21 +00:00
|
|
|
#include "demux.h"
|
|
|
|
|
|
|
|
#define PROBE_SIZE (8 * 1024)
|
|
|
|
|
2023-02-11 20:08:38 +00:00
|
|
|
enum dir_mode {
|
2023-09-19 19:16:23 +00:00
|
|
|
DIR_AUTO,
|
2023-02-11 20:08:38 +00:00
|
|
|
DIR_LAZY,
|
2023-08-29 06:19:19 +00:00
|
|
|
DIR_RECURSIVE,
|
2023-02-11 20:08:38 +00:00
|
|
|
DIR_IGNORE,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct demux_playlist_opts
|
|
|
|
struct demux_playlist_opts {
|
|
|
|
int dir_mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct m_sub_options demux_playlist_conf = {
|
|
|
|
.opts = (const struct m_option[]) {
|
|
|
|
{"directory-mode", OPT_CHOICE(dir_mode,
|
2023-09-19 19:16:23 +00:00
|
|
|
{"auto", DIR_AUTO},
|
2023-02-11 20:08:38 +00:00
|
|
|
{"lazy", DIR_LAZY},
|
2023-08-29 06:19:19 +00:00
|
|
|
{"recursive", DIR_RECURSIVE},
|
2023-02-11 20:08:38 +00:00
|
|
|
{"ignore", DIR_IGNORE})},
|
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct demux_playlist_opts),
|
|
|
|
.defaults = &(const struct demux_playlist_opts){
|
2023-09-19 19:16:23 +00:00
|
|
|
.dir_mode = DIR_AUTO,
|
2023-02-11 20:08:38 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2014-10-30 21:24:25 +00:00
|
|
|
static bool check_mimetype(struct stream *s, const char *const *list)
|
|
|
|
{
|
|
|
|
if (s->mime_type) {
|
|
|
|
for (int n = 0; list && list[n]; n++) {
|
2015-06-20 14:36:22 +00:00
|
|
|
if (strcasecmp(s->mime_type, list[n]) == 0)
|
2014-10-30 21:24:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-25 18:40:21 +00:00
|
|
|
struct pl_parser {
|
2023-09-19 19:16:23 +00:00
|
|
|
struct mpv_global *global;
|
2014-01-19 19:54:00 +00:00
|
|
|
struct mp_log *log;
|
2013-08-25 18:40:21 +00:00
|
|
|
struct stream *s;
|
2021-09-05 22:29:27 +00:00
|
|
|
char buffer[2 * 1024 * 1024];
|
2013-08-25 18:40:21 +00:00
|
|
|
int utf16;
|
|
|
|
struct playlist *pl;
|
2014-01-19 19:13:51 +00:00
|
|
|
bool error;
|
2013-08-25 18:40:21 +00:00
|
|
|
bool probing;
|
2014-01-19 19:54:00 +00:00
|
|
|
bool force;
|
2015-04-17 20:56:19 +00:00
|
|
|
bool add_base;
|
2023-10-02 00:33:05 +00:00
|
|
|
bool line_allocated;
|
2014-12-05 22:50:56 +00:00
|
|
|
enum demux_check check_level;
|
|
|
|
struct stream *real_stream;
|
2015-08-04 15:04:56 +00:00
|
|
|
char *format;
|
2023-10-02 00:33:05 +00:00
|
|
|
char *codepage;
|
2023-02-11 20:08:38 +00:00
|
|
|
struct demux_playlist_opts *opts;
|
2013-08-25 18:40:21 +00:00
|
|
|
};
|
|
|
|
|
2019-10-26 14:44:05 +00:00
|
|
|
|
|
|
|
static uint16_t stream_read_word_endian(stream_t *s, bool big_endian)
|
|
|
|
{
|
|
|
|
unsigned int y = stream_read_char(s);
|
|
|
|
y = (y << 8) | stream_read_char(s);
|
|
|
|
if (!big_endian)
|
|
|
|
y = ((y >> 8) & 0xFF) | (y << 8);
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read characters until the next '\n' (including), or until the buffer in s is
|
|
|
|
// exhausted.
|
|
|
|
static int read_characters(stream_t *s, uint8_t *dst, int dstsize, int utf16)
|
|
|
|
{
|
|
|
|
if (utf16 == 1 || utf16 == 2) {
|
|
|
|
uint8_t *cur = dst;
|
|
|
|
while (1) {
|
|
|
|
if ((cur - dst) + 8 >= dstsize) // PUT_UTF8 writes max. 8 bytes
|
|
|
|
return -1; // line too long
|
|
|
|
uint32_t c;
|
|
|
|
uint8_t tmp;
|
|
|
|
GET_UTF16(c, stream_read_word_endian(s, utf16 == 2), return -1;)
|
|
|
|
if (s->eof)
|
|
|
|
break; // legitimate EOF; ignore the case of partial reads
|
|
|
|
PUT_UTF8(c, tmp, *cur++ = tmp;)
|
|
|
|
if (c == '\n')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return cur - dst;
|
|
|
|
} else {
|
stream: turn into a ring buffer, make size configurable
In some corner cases (see #6802), it can be beneficial to use a larger
stream buffer size. Use this as argument to rewrite everything for no
reason.
Turn stream.c itself into a ring buffer, with configurable size. The
latter would have been easily achievable with minimal changes, and the
ring buffer is the hard part. There is no reason to have a ring buffer
at all, except possibly if ffmpeg don't fix their awful mp4 demuxer, and
some subtle issues with demux_mkv.c wanting to seek back by small
offsets (the latter was handled with small stream_peek() calls, which
are unneeded now).
In addition, this turns small forward seeks into reads (where data is
simply skipped). Before this commit, only stream_skip() did this (which
also mean that stream_skip() simply calls stream_seek() now).
Replace all stream_peek() calls with something else (usually
stream_read_peek()). The function was a problem, because it returned a
pointer to the internal buffer, which is now a ring buffer with
wrapping. The new function just copies the data into a buffer, and in
some cases requires callers to dynamically allocate memory. (The most
common case, demux_lavf.c, required a separate buffer allocation anyway
due to FFmpeg "idiosyncrasies".) This is the bulk of the demuxer_*
changes.
I'm not happy with this. There still isn't a good reason why there
should be a ring buffer, that is complex, and most of the time just
wastes half of the available memory. Maybe another rewrite soon.
It also contains bugs; you're an alpha tester now.
2019-11-06 20:36:02 +00:00
|
|
|
uint8_t buf[1024];
|
|
|
|
int buf_len = stream_read_peek(s, buf, sizeof(buf));
|
|
|
|
uint8_t *end = memchr(buf, '\n', buf_len);
|
|
|
|
int len = end ? end - buf + 1 : buf_len;
|
2019-10-26 14:44:05 +00:00
|
|
|
if (len > dstsize)
|
|
|
|
return -1; // line too long
|
stream: turn into a ring buffer, make size configurable
In some corner cases (see #6802), it can be beneficial to use a larger
stream buffer size. Use this as argument to rewrite everything for no
reason.
Turn stream.c itself into a ring buffer, with configurable size. The
latter would have been easily achievable with minimal changes, and the
ring buffer is the hard part. There is no reason to have a ring buffer
at all, except possibly if ffmpeg don't fix their awful mp4 demuxer, and
some subtle issues with demux_mkv.c wanting to seek back by small
offsets (the latter was handled with small stream_peek() calls, which
are unneeded now).
In addition, this turns small forward seeks into reads (where data is
simply skipped). Before this commit, only stream_skip() did this (which
also mean that stream_skip() simply calls stream_seek() now).
Replace all stream_peek() calls with something else (usually
stream_read_peek()). The function was a problem, because it returned a
pointer to the internal buffer, which is now a ring buffer with
wrapping. The new function just copies the data into a buffer, and in
some cases requires callers to dynamically allocate memory. (The most
common case, demux_lavf.c, required a separate buffer allocation anyway
due to FFmpeg "idiosyncrasies".) This is the bulk of the demuxer_*
changes.
I'm not happy with this. There still isn't a good reason why there
should be a ring buffer, that is complex, and most of the time just
wastes half of the available memory. Maybe another rewrite soon.
It also contains bugs; you're an alpha tester now.
2019-11-06 20:36:02 +00:00
|
|
|
memcpy(dst, buf, len);
|
2019-11-15 11:10:01 +00:00
|
|
|
stream_seek_skip(s, stream_tell(s) + len);
|
2019-10-26 14:44:05 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// On error, or if the line is larger than max-1, return NULL and unset s->eof.
|
|
|
|
// On EOF, return NULL, and s->eof will be set.
|
|
|
|
// Otherwise, return the line (including \n or \r\n at the end of the line).
|
|
|
|
// If the return value is non-NULL, it's always the same as mem.
|
|
|
|
// utf16: 0: UTF8 or 8 bit legacy, 1: UTF16-LE, 2: UTF16-BE
|
|
|
|
static char *read_line(stream_t *s, char *mem, int max, int utf16)
|
|
|
|
{
|
|
|
|
if (max < 1)
|
|
|
|
return NULL;
|
|
|
|
int read = 0;
|
|
|
|
while (1) {
|
|
|
|
// Reserve 1 byte of ptr for terminating \0.
|
|
|
|
int l = read_characters(s, &mem[read], max - read - 1, utf16);
|
|
|
|
if (l < 0 || memchr(&mem[read], '\0', l)) {
|
|
|
|
MP_WARN(s, "error reading line\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
read += l;
|
|
|
|
if (l == 0 || (read > 0 && mem[read - 1] == '\n'))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mem[read] = '\0';
|
2019-11-15 11:10:01 +00:00
|
|
|
if (!stream_read_peek(s, &(char){0}, 1) && read == 0) // legitimate EOF
|
2019-10-26 14:44:05 +00:00
|
|
|
return NULL;
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2013-08-25 18:40:21 +00:00
|
|
|
static char *pl_get_line0(struct pl_parser *p)
|
|
|
|
{
|
2019-10-26 14:44:05 +00:00
|
|
|
char *res = read_line(p->s, p->buffer, sizeof(p->buffer), p->utf16);
|
2013-08-25 18:40:21 +00:00
|
|
|
if (res) {
|
|
|
|
int len = strlen(res);
|
|
|
|
if (len > 0 && res[len - 1] == '\n')
|
|
|
|
res[len - 1] = '\0';
|
2014-01-19 19:13:51 +00:00
|
|
|
} else {
|
|
|
|
p->error |= !p->s->eof;
|
2013-08-25 18:40:21 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bstr pl_get_line(struct pl_parser *p)
|
|
|
|
{
|
2023-10-02 00:33:05 +00:00
|
|
|
bstr line = bstr_strip(bstr0(pl_get_line0(p)));
|
|
|
|
const char *charset = mp_charset_guess(p, p->log, line, p->codepage, 0);
|
|
|
|
if (charset && !mp_charset_is_utf8(charset)) {
|
|
|
|
bstr utf8 = mp_iconv_to_utf8(p->log, line, charset, 0);
|
|
|
|
if (utf8.start && utf8.start != line.start) {
|
|
|
|
line = utf8;
|
|
|
|
p->line_allocated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper in case mp_iconv_to_utf8 allocates memory
|
|
|
|
static void pl_free_line(struct pl_parser *p, bstr line)
|
|
|
|
{
|
|
|
|
if (p->line_allocated) {
|
|
|
|
talloc_free(line.start);
|
|
|
|
p->line_allocated = false;
|
|
|
|
}
|
2013-08-25 18:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pl_add(struct pl_parser *p, bstr entry)
|
|
|
|
{
|
|
|
|
char *s = bstrto0(NULL, entry);
|
2024-02-04 18:58:09 +00:00
|
|
|
playlist_append_file(p->pl, s);
|
2013-08-25 18:40:21 +00:00
|
|
|
talloc_free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool pl_eof(struct pl_parser *p)
|
|
|
|
{
|
2014-01-19 19:13:51 +00:00
|
|
|
return p->error || p->s->eof;
|
2013-08-25 18:40:21 +00:00
|
|
|
}
|
|
|
|
|
2014-12-05 22:50:56 +00:00
|
|
|
static bool maybe_text(bstr d)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < d.len; n++) {
|
|
|
|
unsigned char c = d.start[n];
|
|
|
|
if (c < 32 && c != '\n' && c != '\r' && c != '\t')
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-25 18:40:21 +00:00
|
|
|
static int parse_m3u(struct pl_parser *p)
|
|
|
|
{
|
2023-10-02 00:33:05 +00:00
|
|
|
bstr line = pl_get_line(p);
|
2014-12-05 22:50:56 +00:00
|
|
|
if (p->probing && !bstr_equals0(line, "#EXTM3U")) {
|
|
|
|
// Last resort: if the file extension is m3u, it might be headerless.
|
|
|
|
if (p->check_level == DEMUX_CHECK_UNSAFE) {
|
|
|
|
char *ext = mp_splitext(p->real_stream->url, NULL);
|
stream: turn into a ring buffer, make size configurable
In some corner cases (see #6802), it can be beneficial to use a larger
stream buffer size. Use this as argument to rewrite everything for no
reason.
Turn stream.c itself into a ring buffer, with configurable size. The
latter would have been easily achievable with minimal changes, and the
ring buffer is the hard part. There is no reason to have a ring buffer
at all, except possibly if ffmpeg don't fix their awful mp4 demuxer, and
some subtle issues with demux_mkv.c wanting to seek back by small
offsets (the latter was handled with small stream_peek() calls, which
are unneeded now).
In addition, this turns small forward seeks into reads (where data is
simply skipped). Before this commit, only stream_skip() did this (which
also mean that stream_skip() simply calls stream_seek() now).
Replace all stream_peek() calls with something else (usually
stream_read_peek()). The function was a problem, because it returned a
pointer to the internal buffer, which is now a ring buffer with
wrapping. The new function just copies the data into a buffer, and in
some cases requires callers to dynamically allocate memory. (The most
common case, demux_lavf.c, required a separate buffer allocation anyway
due to FFmpeg "idiosyncrasies".) This is the bulk of the demuxer_*
changes.
I'm not happy with this. There still isn't a good reason why there
should be a ring buffer, that is complex, and most of the time just
wastes half of the available memory. Maybe another rewrite soon.
It also contains bugs; you're an alpha tester now.
2019-11-06 20:36:02 +00:00
|
|
|
char probe[PROBE_SIZE];
|
|
|
|
int len = stream_read_peek(p->real_stream, probe, sizeof(probe));
|
|
|
|
bstr data = {probe, len};
|
2023-08-15 02:22:15 +00:00
|
|
|
if (ext && data.len >= 2 && maybe_text(data)) {
|
2016-05-17 16:17:46 +00:00
|
|
|
const char *exts[] = {"m3u", "m3u8", NULL};
|
|
|
|
for (int n = 0; exts[n]; n++) {
|
|
|
|
if (strcasecmp(ext, exts[n]) == 0)
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 22:50:56 +00:00
|
|
|
}
|
2023-10-02 00:33:05 +00:00
|
|
|
pl_free_line(p, line);
|
2013-08-25 18:40:21 +00:00
|
|
|
return -1;
|
2014-12-05 22:50:56 +00:00
|
|
|
}
|
2016-05-17 16:17:46 +00:00
|
|
|
|
2014-12-05 22:50:56 +00:00
|
|
|
ok:
|
2023-10-02 00:33:05 +00:00
|
|
|
if (p->probing) {
|
|
|
|
pl_free_line(p, line);
|
2013-08-25 18:40:21 +00:00
|
|
|
return 0;
|
2023-10-02 00:33:05 +00:00
|
|
|
}
|
2016-05-17 16:17:46 +00:00
|
|
|
|
2015-07-10 19:22:35 +00:00
|
|
|
char *title = NULL;
|
2014-11-30 18:30:22 +00:00
|
|
|
while (line.len || !pl_eof(p)) {
|
2023-10-02 00:33:05 +00:00
|
|
|
bstr line_dup = line;
|
|
|
|
if (bstr_eatstart0(&line_dup, "#EXTINF:")) {
|
2015-07-10 19:22:35 +00:00
|
|
|
bstr duration, btitle;
|
2023-10-02 00:33:05 +00:00
|
|
|
if (bstr_split_tok(line_dup, ",", &duration, &btitle) && btitle.len) {
|
2015-07-10 19:22:35 +00:00
|
|
|
talloc_free(title);
|
|
|
|
title = bstrto0(NULL, btitle);
|
|
|
|
}
|
2023-10-02 00:33:05 +00:00
|
|
|
} else if (bstr_startswith0(line_dup, "#EXT-X-")) {
|
2015-08-04 15:04:56 +00:00
|
|
|
p->format = "hls";
|
2023-10-02 00:33:05 +00:00
|
|
|
} else if (line_dup.len > 0 && !bstr_startswith0(line_dup, "#")) {
|
|
|
|
char *fn = bstrto0(NULL, line_dup);
|
2015-07-10 19:22:35 +00:00
|
|
|
struct playlist_entry *e = playlist_entry_new(fn);
|
|
|
|
talloc_free(fn);
|
|
|
|
e->title = talloc_steal(e, title);
|
|
|
|
title = NULL;
|
2024-02-04 18:58:09 +00:00
|
|
|
playlist_insert_at(p->pl, e, NULL);
|
2015-07-10 19:22:35 +00:00
|
|
|
}
|
2023-10-02 00:33:05 +00:00
|
|
|
pl_free_line(p, line);
|
|
|
|
line = pl_get_line(p);
|
2013-08-25 18:40:21 +00:00
|
|
|
}
|
2023-10-02 00:33:05 +00:00
|
|
|
pl_free_line(p, line);
|
2015-07-10 19:22:35 +00:00
|
|
|
talloc_free(title);
|
2013-08-25 18:40:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-26 21:34:07 +00:00
|
|
|
static int parse_ref_init(struct pl_parser *p)
|
|
|
|
{
|
2023-10-02 00:33:05 +00:00
|
|
|
bstr line = pl_get_line(p);
|
|
|
|
if (!bstr_equals0(line, "[Reference]")) {
|
|
|
|
pl_free_line(p, line);
|
2013-08-26 21:34:07 +00:00
|
|
|
return -1;
|
2023-10-02 00:33:05 +00:00
|
|
|
}
|
2023-11-12 19:18:34 +00:00
|
|
|
pl_free_line(p, line);
|
2014-10-30 21:24:25 +00:00
|
|
|
|
|
|
|
// ASF http streaming redirection - this is needed because ffmpeg http://
|
|
|
|
// and mmsh:// can not automatically switch automatically between each
|
|
|
|
// others. Both protocols use http - MMSH requires special http headers
|
|
|
|
// to "activate" it, and will in other cases return this playlist.
|
|
|
|
static const char *const mmsh_types[] = {"audio/x-ms-wax",
|
|
|
|
"audio/x-ms-wma", "video/x-ms-asf", "video/x-ms-afs", "video/x-ms-wmv",
|
|
|
|
"video/x-ms-wma", "application/x-mms-framed",
|
|
|
|
"application/vnd.ms.wms-hdr.asfv1", NULL};
|
|
|
|
bstr burl = bstr0(p->s->url);
|
|
|
|
if (bstr_eatstart0(&burl, "http://") && check_mimetype(p->s, mmsh_types)) {
|
2014-11-30 18:30:22 +00:00
|
|
|
MP_INFO(p, "Redirecting to mmsh://\n");
|
2024-02-04 18:58:09 +00:00
|
|
|
playlist_append_file(p->pl, talloc_asprintf(p, "mmsh://%.*s", BSTR_P(burl)));
|
2014-10-30 21:24:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-08-26 21:34:07 +00:00
|
|
|
while (!pl_eof(p)) {
|
2023-10-02 00:33:05 +00:00
|
|
|
line = pl_get_line(p);
|
2023-11-12 19:18:34 +00:00
|
|
|
bstr value;
|
|
|
|
if (bstr_case_startswith(line, bstr0("Ref"))) {
|
|
|
|
bstr_split_tok(line, "=", &(bstr){0}, &value);
|
|
|
|
if (value.len)
|
|
|
|
pl_add(p, value);
|
2013-08-26 21:34:07 +00:00
|
|
|
}
|
2023-10-02 00:33:05 +00:00
|
|
|
pl_free_line(p, line);
|
2013-08-26 21:34:07 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-12 14:51:48 +00:00
|
|
|
static int parse_ini_thing(struct pl_parser *p, const char *header,
|
|
|
|
const char *entry)
|
2013-11-04 22:55:41 +00:00
|
|
|
{
|
|
|
|
bstr line = {0};
|
|
|
|
while (!line.len && !pl_eof(p))
|
2023-10-02 00:33:05 +00:00
|
|
|
line = pl_get_line(p);
|
|
|
|
if (bstrcasecmp0(line, header) != 0) {
|
|
|
|
pl_free_line(p, line);
|
2013-11-04 22:55:41 +00:00
|
|
|
return -1;
|
2023-10-02 00:33:05 +00:00
|
|
|
}
|
|
|
|
if (p->probing) {
|
|
|
|
pl_free_line(p, line);
|
2013-11-04 22:55:41 +00:00
|
|
|
return 0;
|
2023-10-02 00:33:05 +00:00
|
|
|
}
|
2023-11-12 19:18:34 +00:00
|
|
|
pl_free_line(p, line);
|
2013-11-04 22:55:41 +00:00
|
|
|
while (!pl_eof(p)) {
|
2023-10-02 00:33:05 +00:00
|
|
|
line = pl_get_line(p);
|
2013-11-04 22:55:41 +00:00
|
|
|
bstr key, value;
|
2023-11-12 19:18:34 +00:00
|
|
|
if (bstr_split_tok(line, "=", &key, &value) &&
|
2017-11-12 14:51:48 +00:00
|
|
|
bstr_case_startswith(key, bstr0(entry)))
|
2013-11-04 22:55:41 +00:00
|
|
|
{
|
2014-12-30 12:24:43 +00:00
|
|
|
value = bstr_strip(value);
|
|
|
|
if (bstr_startswith0(value, "\"") && bstr_endswith0(value, "\""))
|
|
|
|
value = bstr_splice(value, 1, -1);
|
2013-11-04 22:55:41 +00:00
|
|
|
pl_add(p, value);
|
|
|
|
}
|
2023-10-02 00:33:05 +00:00
|
|
|
pl_free_line(p, line);
|
2013-11-04 22:55:41 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-12 14:51:48 +00:00
|
|
|
static int parse_pls(struct pl_parser *p)
|
|
|
|
{
|
|
|
|
return parse_ini_thing(p, "[playlist]", "File");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_url(struct pl_parser *p)
|
|
|
|
{
|
|
|
|
return parse_ini_thing(p, "[InternetShortcut]", "URL");
|
|
|
|
}
|
|
|
|
|
2014-01-19 19:54:00 +00:00
|
|
|
static int parse_txt(struct pl_parser *p)
|
|
|
|
{
|
|
|
|
if (!p->force)
|
|
|
|
return -1;
|
|
|
|
if (p->probing)
|
|
|
|
return 0;
|
|
|
|
MP_WARN(p, "Reading plaintext playlist.\n");
|
|
|
|
while (!pl_eof(p)) {
|
2023-10-02 00:33:05 +00:00
|
|
|
bstr line = pl_get_line(p);
|
2014-01-19 19:54:00 +00:00
|
|
|
if (line.len == 0)
|
|
|
|
continue;
|
|
|
|
pl_add(p, line);
|
2023-10-02 00:33:05 +00:00
|
|
|
pl_free_line(p, line);
|
2014-01-19 19:54:00 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-18 20:08:18 +00:00
|
|
|
#define MAX_DIR_STACK 20
|
|
|
|
|
|
|
|
static bool same_st(struct stat *st1, struct stat *st2)
|
|
|
|
{
|
2017-10-23 10:01:45 +00:00
|
|
|
return st1->st_dev == st2->st_dev && st1->st_ino == st2->st_ino;
|
2016-04-18 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 22:16:16 +00:00
|
|
|
struct pl_dir_entry {
|
|
|
|
char *path;
|
|
|
|
char *name;
|
|
|
|
struct stat st;
|
|
|
|
bool is_dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cmp_dir_entry(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
struct pl_dir_entry *a_entry = (struct pl_dir_entry*) a;
|
|
|
|
struct pl_dir_entry *b_entry = (struct pl_dir_entry*) b;
|
|
|
|
if (a_entry->is_dir == b_entry->is_dir) {
|
|
|
|
return mp_natural_sort_cmp(a_entry->name, b_entry->name);
|
|
|
|
} else {
|
|
|
|
return a_entry->is_dir ? 1 : -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 20:08:18 +00:00
|
|
|
// Return true if this was a readable directory.
|
|
|
|
static bool scan_dir(struct pl_parser *p, char *path,
|
2023-02-11 22:16:16 +00:00
|
|
|
struct stat *dir_stack, int num_dir_stack)
|
2016-04-18 20:08:18 +00:00
|
|
|
{
|
|
|
|
if (strlen(path) >= 8192 || num_dir_stack == MAX_DIR_STACK)
|
|
|
|
return false; // things like mount bind loops
|
|
|
|
|
|
|
|
DIR *dp = opendir(path);
|
|
|
|
if (!dp) {
|
|
|
|
MP_ERR(p, "Could not read directory.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-11 22:16:16 +00:00
|
|
|
struct pl_dir_entry *dir_entries = NULL;
|
|
|
|
int num_dir_entries = 0;
|
|
|
|
int path_len = strlen(path);
|
2023-02-11 20:08:38 +00:00
|
|
|
int dir_mode = p->opts->dir_mode;
|
2023-02-11 22:16:16 +00:00
|
|
|
|
2016-04-18 20:08:18 +00:00
|
|
|
struct dirent *ep;
|
|
|
|
while ((ep = readdir(dp))) {
|
|
|
|
if (ep->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (mp_cancel_test(p->s->cancel))
|
|
|
|
break;
|
|
|
|
|
|
|
|
char *file = mp_path_join(p, path, ep->d_name);
|
|
|
|
|
|
|
|
struct stat st;
|
2023-02-11 22:16:16 +00:00
|
|
|
if (stat(file, &st) == 0 && S_ISDIR(st.st_mode)) {
|
2023-02-11 20:08:38 +00:00
|
|
|
if (dir_mode != DIR_IGNORE) {
|
|
|
|
for (int n = 0; n < num_dir_stack; n++) {
|
|
|
|
if (same_st(&dir_stack[n], &st)) {
|
|
|
|
MP_VERBOSE(p, "Skip recursive entry: %s\n", file);
|
|
|
|
goto skip;
|
|
|
|
}
|
2016-04-18 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 22:16:16 +00:00
|
|
|
struct pl_dir_entry d = {file, &file[path_len], st, true};
|
|
|
|
MP_TARRAY_APPEND(p, dir_entries, num_dir_entries, d);
|
2023-02-11 20:08:38 +00:00
|
|
|
}
|
2016-04-18 20:08:18 +00:00
|
|
|
} else {
|
2023-02-11 22:16:16 +00:00
|
|
|
struct pl_dir_entry f = {file, &file[path_len], .is_dir = false};
|
|
|
|
MP_TARRAY_APPEND(p, dir_entries, num_dir_entries, f);
|
2016-04-18 20:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
skip: ;
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
|
2023-02-11 22:16:16 +00:00
|
|
|
if (dir_entries)
|
|
|
|
qsort(dir_entries, num_dir_entries, sizeof(dir_entries[0]), cmp_dir_entry);
|
|
|
|
|
|
|
|
for (int n = 0; n < num_dir_entries; n++) {
|
|
|
|
if (dir_mode == DIR_RECURSIVE && dir_entries[n].is_dir) {
|
|
|
|
dir_stack[num_dir_stack] = dir_entries[n].st;
|
|
|
|
char *file = dir_entries[n].path;
|
|
|
|
scan_dir(p, file, dir_stack, num_dir_stack + 1);
|
|
|
|
}
|
|
|
|
else {
|
2024-02-04 18:58:09 +00:00
|
|
|
playlist_append_file(p->pl, dir_entries[n].path);
|
2023-02-11 22:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-04-17 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_dir(struct pl_parser *p)
|
|
|
|
{
|
2017-02-02 17:24:27 +00:00
|
|
|
if (!p->real_stream->is_directory)
|
2015-04-17 20:56:19 +00:00
|
|
|
return -1;
|
|
|
|
if (p->probing)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
char *path = mp_file_get_path(p, bstr0(p->real_stream->url));
|
2016-12-04 22:15:31 +00:00
|
|
|
if (!path)
|
|
|
|
return -1;
|
2015-04-17 20:56:19 +00:00
|
|
|
|
2016-04-18 20:08:18 +00:00
|
|
|
struct stat dir_stack[MAX_DIR_STACK];
|
2015-04-17 20:56:19 +00:00
|
|
|
|
2023-09-19 19:16:23 +00:00
|
|
|
if (p->opts->dir_mode == DIR_AUTO) {
|
|
|
|
struct MPOpts *opts = mp_get_config_group(NULL, p->global, &mp_opt_root);
|
|
|
|
p->opts->dir_mode = opts->shuffle ? DIR_RECURSIVE : DIR_LAZY;
|
|
|
|
talloc_free(opts);
|
|
|
|
}
|
|
|
|
|
2023-02-11 22:16:16 +00:00
|
|
|
scan_dir(p, path, dir_stack, 0);
|
2015-04-17 20:56:19 +00:00
|
|
|
|
|
|
|
p->add_base = false;
|
|
|
|
|
2023-02-11 22:16:16 +00:00
|
|
|
return p->pl->num_entries > 0 ? 0 : -1;
|
2015-04-17 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 21:24:25 +00:00
|
|
|
#define MIME_TYPES(...) \
|
|
|
|
.mime_types = (const char*const[]){__VA_ARGS__, NULL}
|
|
|
|
|
2013-08-25 18:40:21 +00:00
|
|
|
struct pl_format {
|
|
|
|
const char *name;
|
|
|
|
int (*parse)(struct pl_parser *p);
|
2014-06-10 21:56:05 +00:00
|
|
|
const char *const *mime_types;
|
2013-08-25 18:40:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct pl_format formats[] = {
|
2015-04-17 20:56:19 +00:00
|
|
|
{"directory", parse_dir},
|
2014-05-06 18:09:55 +00:00
|
|
|
{"m3u", parse_m3u,
|
|
|
|
MIME_TYPES("audio/mpegurl", "audio/x-mpegurl", "application/x-mpegurl")},
|
2013-08-26 21:34:07 +00:00
|
|
|
{"ini", parse_ref_init},
|
2014-05-06 18:09:55 +00:00
|
|
|
{"pls", parse_pls,
|
|
|
|
MIME_TYPES("audio/x-scpls")},
|
2017-11-12 14:51:48 +00:00
|
|
|
{"url", parse_url},
|
2014-01-19 19:54:00 +00:00
|
|
|
{"txt", parse_txt},
|
2013-08-25 18:40:21 +00:00
|
|
|
};
|
|
|
|
|
2014-01-19 19:54:00 +00:00
|
|
|
static const struct pl_format *probe_pl(struct pl_parser *p)
|
2013-08-25 18:40:21 +00:00
|
|
|
{
|
|
|
|
int64_t start = stream_tell(p->s);
|
|
|
|
for (int n = 0; n < MP_ARRAY_SIZE(formats); n++) {
|
|
|
|
const struct pl_format *fmt = &formats[n];
|
|
|
|
stream_seek(p->s, start);
|
2014-05-06 18:09:46 +00:00
|
|
|
if (check_mimetype(p->s, fmt->mime_types)) {
|
|
|
|
MP_VERBOSE(p, "forcing format by mime-type.\n");
|
|
|
|
p->force = true;
|
|
|
|
return fmt;
|
|
|
|
}
|
2013-08-25 18:40:21 +00:00
|
|
|
if (fmt->parse(p) >= 0)
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_file(struct demuxer *demuxer, enum demux_check check)
|
|
|
|
{
|
2016-12-04 22:15:31 +00:00
|
|
|
if (!demuxer->access_references)
|
|
|
|
return -1;
|
|
|
|
|
2013-08-25 18:40:21 +00:00
|
|
|
bool force = check < DEMUX_CHECK_UNSAFE || check == DEMUX_CHECK_REQUEST;
|
|
|
|
|
|
|
|
struct pl_parser *p = talloc_zero(NULL, struct pl_parser);
|
2023-09-19 19:16:23 +00:00
|
|
|
p->global = demuxer->global;
|
2014-01-19 19:54:00 +00:00
|
|
|
p->log = demuxer->log;
|
2013-08-25 18:40:21 +00:00
|
|
|
p->pl = talloc_zero(p, struct playlist);
|
2014-12-05 22:50:56 +00:00
|
|
|
p->real_stream = demuxer->stream;
|
2015-04-17 20:56:19 +00:00
|
|
|
p->add_base = true;
|
2013-08-25 18:40:21 +00:00
|
|
|
|
2023-10-02 00:33:05 +00:00
|
|
|
struct demux_opts *opts = mp_get_config_group(p, p->global, &demux_conf);
|
|
|
|
p->codepage = opts->meta_cp;
|
|
|
|
|
stream: turn into a ring buffer, make size configurable
In some corner cases (see #6802), it can be beneficial to use a larger
stream buffer size. Use this as argument to rewrite everything for no
reason.
Turn stream.c itself into a ring buffer, with configurable size. The
latter would have been easily achievable with minimal changes, and the
ring buffer is the hard part. There is no reason to have a ring buffer
at all, except possibly if ffmpeg don't fix their awful mp4 demuxer, and
some subtle issues with demux_mkv.c wanting to seek back by small
offsets (the latter was handled with small stream_peek() calls, which
are unneeded now).
In addition, this turns small forward seeks into reads (where data is
simply skipped). Before this commit, only stream_skip() did this (which
also mean that stream_skip() simply calls stream_seek() now).
Replace all stream_peek() calls with something else (usually
stream_read_peek()). The function was a problem, because it returned a
pointer to the internal buffer, which is now a ring buffer with
wrapping. The new function just copies the data into a buffer, and in
some cases requires callers to dynamically allocate memory. (The most
common case, demux_lavf.c, required a separate buffer allocation anyway
due to FFmpeg "idiosyncrasies".) This is the bulk of the demuxer_*
changes.
I'm not happy with this. There still isn't a good reason why there
should be a ring buffer, that is complex, and most of the time just
wastes half of the available memory. Maybe another rewrite soon.
It also contains bugs; you're an alpha tester now.
2019-11-06 20:36:02 +00:00
|
|
|
char probe[PROBE_SIZE];
|
|
|
|
int probe_len = stream_read_peek(p->real_stream, probe, sizeof(probe));
|
|
|
|
p->s = stream_memory_open(demuxer->global, probe, probe_len);
|
2014-05-06 18:09:46 +00:00
|
|
|
p->s->mime_type = demuxer->stream->mime_type;
|
2013-08-25 18:40:21 +00:00
|
|
|
p->utf16 = stream_skip_bom(p->s);
|
2014-01-19 19:54:00 +00:00
|
|
|
p->force = force;
|
2014-12-05 22:50:56 +00:00
|
|
|
p->check_level = check;
|
2013-08-25 18:40:21 +00:00
|
|
|
p->probing = true;
|
2014-01-19 19:54:00 +00:00
|
|
|
const struct pl_format *fmt = probe_pl(p);
|
2013-08-25 18:40:21 +00:00
|
|
|
free_stream(p->s);
|
|
|
|
playlist_clear(p->pl);
|
|
|
|
if (!fmt) {
|
|
|
|
talloc_free(p);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->probing = false;
|
2014-01-19 19:13:51 +00:00
|
|
|
p->error = false;
|
2013-08-25 18:40:21 +00:00
|
|
|
p->s = demuxer->stream;
|
|
|
|
p->utf16 = stream_skip_bom(p->s);
|
2023-02-11 20:08:38 +00:00
|
|
|
p->opts = mp_get_config_group(demuxer, demuxer->global, &demux_playlist_conf);
|
2014-01-19 19:13:51 +00:00
|
|
|
bool ok = fmt->parse(p) >= 0 && !p->error;
|
2024-06-26 15:32:02 +00:00
|
|
|
if (p->add_base) {
|
|
|
|
bstr proto = mp_split_proto(bstr0(demuxer->filename), NULL);
|
|
|
|
// Don't add base path to self-expanding protocols
|
|
|
|
if (bstrcasecmp0(proto, "memory") && bstrcasecmp0(proto, "lavf") &&
|
|
|
|
bstrcasecmp0(proto, "hex"))
|
|
|
|
{
|
|
|
|
playlist_add_base_path(p->pl, mp_dirname(demuxer->filename));
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 20:12:02 +00:00
|
|
|
playlist_set_stream_flags(p->pl, demuxer->stream_origin);
|
2013-08-25 18:40:21 +00:00
|
|
|
demuxer->playlist = talloc_steal(demuxer, p->pl);
|
2015-08-04 15:04:56 +00:00
|
|
|
demuxer->filetype = p->format ? p->format : fmt->name;
|
2015-02-18 20:10:43 +00:00
|
|
|
demuxer->fully_read = true;
|
2013-08-25 18:40:21 +00:00
|
|
|
talloc_free(p);
|
demux: change hack for closing subtitle files early
Subtitles (and a few other file types, like playlists) are not streamed,
but fully read on opening. This means keeping the file handle or network
socket open is a waste of resources and could cause other weird
behavior. This is why there's a hack to close them after opening.
Change this hack to make the demuxer itself do this, which is less
weird. (Until recently, demuxer->stream ownership was more complex,
which is why it was done this way.)
There is some evil shit due to a huge ownership/lifetime mess of various
objects. Especially EDL (the currently only nested demuxer case)
requires being careful about mp_cancel and passing down stream pointers.
As one defensive programming measure, stop accessing the "stream"
variable in open_given_type(), even where it would still work. This
includes removing a redundant line of code, and removing the peak call,
which should not be needed anymore, as the remaining demuxers do this
mostly correctly.
2018-09-07 21:02:36 +00:00
|
|
|
if (ok)
|
|
|
|
demux_close_stream(demuxer);
|
2013-08-25 18:40:21 +00:00
|
|
|
return ok ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
2023-02-11 20:08:38 +00:00
|
|
|
const demuxer_desc_t demuxer_desc_playlist = {
|
2013-08-25 18:40:21 +00:00
|
|
|
.name = "playlist",
|
|
|
|
.desc = "Playlist file",
|
|
|
|
.open = open_file,
|
|
|
|
};
|