summaryrefslogtreecommitdiffstats
path: root/demux
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2015-08-17 00:55:26 +0200
committerwm4 <wm4@nowhere>2015-08-17 00:55:26 +0200
commit2b280f4522a288429253b396b778d60ed018312c (patch)
treeec76722ed42b8323fee962fa333d5e116784c2fb /demux
parent00b60710cf5a290f1882472d51577f72795a3335 (diff)
downloadmpv-2b280f4522a288429253b396b778d60ed018312c.tar.bz2
mpv-2b280f4522a288429253b396b778d60ed018312c.tar.xz
stream: libarchive wrapper for reading compressed archives
This works similar to the existing .rar support, but uses libarchive. libarchive supports a number of formats, including zip and (most of) rar. Unfortunately, seeking does not work too well. Most libarchive readers do not support seeking, so it's emulated by skipping data until the target position. On backwards seek, the file is reopened. This works fine on a local machine (and if the file is not too large), but will perform not so well over network connection. This is disabled by default for now. One reason is that we try libarchive on every file we open, before trying libavformat, and I'm not sure if I trust libarchive that much yet. Another reason is that this breaks multivolume rar support. While libarchive supports seeking in rar, and (probably) supports multivolume archive, our support of libarchive (probably) does not. I don't care about multivolume rar, but vocal users do.
Diffstat (limited to 'demux')
-rw-r--r--demux/demux.c4
-rw-r--r--demux/demux_libarchive.c88
2 files changed, 92 insertions, 0 deletions
diff --git a/demux/demux.c b/demux/demux.c
index 4c4a399c9f..7d66c56745 100644
--- a/demux/demux.c
+++ b/demux/demux.c
@@ -53,6 +53,7 @@ extern const demuxer_desc_t demuxer_desc_subreader;
extern const demuxer_desc_t demuxer_desc_playlist;
extern const demuxer_desc_t demuxer_desc_disc;
extern const demuxer_desc_t demuxer_desc_rar;
+extern const demuxer_desc_t demuxer_desc_libarchive;
/* Please do not add any new demuxers here. If you want to implement a new
* demuxer, add it to libavformat, except for wrappers around external
@@ -71,6 +72,9 @@ const demuxer_desc_t *const demuxer_list[] = {
&demuxer_desc_libass,
#endif
&demuxer_desc_matroska,
+#if HAVE_LIBARCHIVE
+ &demuxer_desc_libarchive,
+#endif
&demuxer_desc_rar,
&demuxer_desc_lavf,
&demuxer_desc_mf,
diff --git a/demux/demux_libarchive.c b/demux/demux_libarchive.c
new file mode 100644
index 0000000000..b95f37228c
--- /dev/null
+++ b/demux/demux_libarchive.c
@@ -0,0 +1,88 @@
+/*
+ * This file is part of mpv.
+ *
+ * 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.
+ *
+ * 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <archive.h>
+#include <archive_entry.h>
+
+#include "common/common.h"
+#include "common/playlist.h"
+#include "stream/stream.h"
+#include "demux.h"
+
+#include "stream/stream_libarchive.h"
+
+static int cmp_filename(const void *a, const void *b)
+{
+ return strcmp(*(char **)a, *(char **)b);
+}
+
+static int open_file(struct demuxer *demuxer, enum demux_check check)
+{
+ struct mp_archive *mpa = mp_archive_new(demuxer->log, demuxer->stream);
+ if (!mpa)
+ return -1;
+
+ struct playlist *pl = talloc_zero(demuxer, struct playlist);
+ demuxer->playlist = pl;
+
+ // make it load archive://
+ pl->disable_safety = true;
+
+ char *prefix = mp_url_escape(mpa, demuxer->stream->url, "~|");
+
+ char **files = NULL;
+ int num_files = 0;
+
+ for (;;) {
+ struct archive_entry *entry;
+ int r = archive_read_next_header(mpa->arch, &entry);
+ if (r == ARCHIVE_EOF)
+ break;
+ if (r < ARCHIVE_OK)
+ MP_ERR(demuxer, "libarchive: %s\n", archive_error_string(mpa->arch));
+ if (r < ARCHIVE_WARN)
+ break;
+ if (archive_entry_filetype(entry) != AE_IFREG)
+ continue;
+ const char *fn = archive_entry_pathname(entry);
+ // Some archives may have no filenames.
+ if (!fn)
+ fn = talloc_asprintf(mpa, "mpv_unknown#%d\n", num_files);
+ // stream_libarchive.c does the real work
+ char *f = talloc_asprintf(mpa, "archive://%s|%s", prefix, fn);
+ MP_TARRAY_APPEND(mpa, files, num_files, f);
+ }
+
+ if (files)
+ qsort(files, num_files, sizeof(files[0]), cmp_filename);
+
+ for (int n = 0; n < num_files; n++)
+ playlist_add_file(pl, files[n]);
+
+ demuxer->filetype = "archive";
+ demuxer->fully_read = true;
+
+ mp_archive_free(mpa);
+
+ return 0;
+}
+
+const struct demuxer_desc demuxer_desc_libarchive = {
+ .name = "libarchive",
+ .desc = "libarchive wrapper",
+ .open = open_file,
+};