X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Fstream_filter%2Fdecomp.c;h=5a12e87f203f6492eb5ad6b9f1bfa5583e086c1e;hb=1e68102044c971fe1985d8873b38f3af42a63813;hp=f083fc7f9842df47bfca017ec26abdab7a63ec46;hpb=c2e0af90cfa124f43e79df2ad7be602e7314d083;p=vlc diff --git a/modules/stream_filter/decomp.c b/modules/stream_filter/decomp.c index f083fc7f98..5a12e87f20 100644 --- a/modules/stream_filter/decomp.c +++ b/modules/stream_filter/decomp.c @@ -1,21 +1,21 @@ /***************************************************************************** * decomp.c : Decompression module for vlc ***************************************************************************** - * Copyright © 2008 Rémi Denis-Courmont + * Copyright © 2008-2009 Rémi Denis-Courmont * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by + * This program 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. * * This program 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. + * 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 General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. *****************************************************************************/ #ifdef HAVE_CONFIG_H @@ -26,9 +26,17 @@ #include #include #include +#include +#include #include +#include +#ifndef _POSIX_SPAWN +# define _POSIX_SPAWN (-1) +#endif #include -#include +#if (_POSIX_SPAWN >= 0) +# include +#endif #include #include #if defined (__linux__) && defined (HAVE_VMSPLICE) @@ -40,34 +48,48 @@ static int OpenGzip (vlc_object_t *); static int OpenBzip2 (vlc_object_t *); +static int OpenXZ (vlc_object_t *); static void Close (vlc_object_t *); vlc_module_begin () - set_description (N_("Decompression")) set_category (CAT_INPUT) set_subcategory (SUBCAT_INPUT_STREAM_FILTER) set_capability ("stream_filter", 20) + + set_description (N_("LZMA decompression")) + set_callbacks (OpenXZ, Close) + + add_submodule () + set_description (N_("Burrows-Wheeler decompression")) set_callbacks (OpenBzip2, Close) /* TODO: access shortnames for stream_UrlNew() */ add_submodule () + set_description (N_("gzip decompression")) set_callbacks (OpenGzip, Close) vlc_module_end () struct stream_sys_t { - block_t *peeked; - uint64_t offset; + /* Thread data */ + int write_fd; + + /* Shared data */ + vlc_cond_t wait; + vlc_mutex_t lock; + bool paused; + + /* Caller data */ vlc_thread_t thread; pid_t pid; - int write_fd, read_fd; -}; -static void cloexec (int fd) -{ - int flags = fcntl (fd, F_GETFD); - fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0)); -} + uint64_t offset; + block_t *peeked; + + int read_fd; + bool can_pace; + bool can_pause; +}; extern char **environ; @@ -84,7 +106,7 @@ static void *Thread (void *data) stream_t *stream = data; stream_sys_t *p_sys = stream->p_sys; #ifdef HAVE_VMSPLICE - ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1; + const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1; #endif int fd = p_sys->write_fd; bool error = false; @@ -96,12 +118,22 @@ static void *Thread (void *data) #ifdef HAVE_VMSPLICE unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); + if (unlikely(buf == MAP_FAILED)) + break; vlc_cleanup_push (cleanup_mmap, buf); #else - unsigned char buf[bufsize]; + unsigned char *buf = malloc (bufsize); + if (unlikely(buf == NULL)) + break; + vlc_cleanup_push (free, buf); #endif + vlc_mutex_lock (&p_sys->lock); + while (p_sys->paused) /* practically always false, but... */ + vlc_cond_wait (&p_sys->wait, &p_sys->lock); len = stream_Read (stream->p_source, buf, bufsize); + vlc_mutex_unlock (&p_sys->lock); + vlc_restorecancel (canc); error = len <= 0; @@ -115,9 +147,9 @@ static void *Thread (void *data) struct iovec iov = { buf + i, (len - i) & ~page_mask, }; j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT); } -#else - j = write (fd, buf + i, len - i); + if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */ #endif + j = write (fd, buf + i, len - i); if (j <= 0) { if (j == 0) @@ -127,17 +159,20 @@ static void *Thread (void *data) break; } } -#ifdef HAVE_VMSPLICE - vlc_cleanup_run (); /* munmap (buf, bufsize) */ -#endif + vlc_cleanup_run (); /* free (buf) */ } while (!error); msg_Dbg (stream, "compressed stream at EOF"); + /* Let child process know about EOF */ + p_sys->write_fd = -1; + close (fd); return NULL; } +static int Peek (stream_t *, const uint8_t **, unsigned int); + #define MIN_BLOCK (1 << 10) #define MAX_BLOCK (1 << 20) /** @@ -148,26 +183,38 @@ static int Read (stream_t *stream, void *buf, unsigned int buflen) { stream_sys_t *p_sys = stream->p_sys; block_t *peeked; - size_t bonus = 0; ssize_t length; + if (buf == NULL) /* caller skips data, get big enough peek buffer */ + buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen); + if ((peeked = p_sys->peeked) != NULL) - { - bonus = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen; - memcpy (buf, peeked->p_buffer, bonus); - peeked->p_buffer += bonus; - peeked->i_buffer -= bonus; + { /* dequeue peeked data */ + length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen; + if (buf != NULL) + { + memcpy (buf, peeked->p_buffer, length); + buf = ((char *)buf) + length; + } + buflen -= length; + peeked->p_buffer += length; + peeked->i_buffer -= length; if (peeked->i_buffer == 0) { block_Release (peeked); p_sys->peeked = NULL; } + p_sys->offset += length; + + if (buflen > 0) + length += Read (stream, ((char *)buf) + length, buflen - length); + return length; } + assert ((buf != NULL) || (buflen == 0)); length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false); if (length < 0) return 0; - length += bonus; p_sys->offset += length; return length; } @@ -217,15 +264,29 @@ static int Control (stream_t *stream, int query, va_list args) case STREAM_CAN_FASTSEEK: *(va_arg (args, bool *)) = false; break; + case STREAM_CAN_PAUSE: + *(va_arg (args, bool *)) = p_sys->can_pause; + break; + case STREAM_CAN_CONTROL_PACE: + *(va_arg (args, bool *)) = p_sys->can_pace; + break; case STREAM_GET_POSITION: - *(va_arg (args, int64_t *)) = p_sys->offset; + *(va_arg (args, uint64_t *)) = p_sys->offset; break; case STREAM_GET_SIZE: - *(va_arg (args, int64_t *)) = 0; + *(va_arg (args, uint64_t *)) = 0; break; - case STREAM_GET_MTU: - *(va_arg (args, int *)) = 0; + case STREAM_SET_PAUSE_STATE: + { + bool paused = va_arg (args, unsigned); + + vlc_mutex_lock (&p_sys->lock); + stream_Control (stream->p_source, STREAM_SET_PAUSE_STATE, paused); + p_sys->paused = paused; + vlc_cond_signal (&p_sys->wait); + vlc_mutex_unlock (&p_sys->lock); break; + } default: return VLC_EGENERIC; } @@ -246,9 +307,16 @@ static int Open (stream_t *stream, const char *path) stream->pf_read = Read; stream->pf_peek = Peek; stream->pf_control = Control; - p_sys->peeked = NULL; - p_sys->offset = 0; + + vlc_cond_init (&p_sys->wait); + vlc_mutex_init (&p_sys->lock); + p_sys->paused = false; p_sys->pid = -1; + p_sys->offset = 0; + p_sys->peeked = NULL; + stream_Control (stream->p_source, STREAM_CAN_PAUSE, &p_sys->can_pause); + stream_Control (stream->p_source, STREAM_CAN_CONTROL_PACE, + &p_sys->can_pace); /* I am not a big fan of the pyramid style, but I cannot think of anything * better here. There are too many failure cases. */ @@ -257,26 +325,23 @@ static int Open (stream_t *stream, const char *path) /* We use two pipes rather than one stream socket pair, so that we can * use vmsplice() on Linux. */ - if (pipe (comp) == 0) + if (vlc_pipe (comp) == 0) { - cloexec (comp[1]); p_sys->write_fd = comp[1]; int uncomp[2]; - if (pipe (uncomp) == 0) + if (vlc_pipe (uncomp) == 0) { - cloexec (uncomp[0]); p_sys->read_fd = uncomp[0]; +#if (_POSIX_SPAWN >= 0) posix_spawn_file_actions_t actions; if (posix_spawn_file_actions_init (&actions) == 0) { char *const argv[] = { (char *)path, NULL }; if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0) - && !posix_spawn_file_actions_addclose (&actions, comp[0]) && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1) - && !posix_spawn_file_actions_addclose (&actions, uncomp[1]) && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv, environ)) { @@ -291,18 +356,40 @@ static int Open (stream_t *stream, const char *path) } posix_spawn_file_actions_destroy (&actions); } +#else /* _POSIX_SPAWN */ + switch (p_sys->pid = fork ()) + { + case -1: + msg_Err (stream, "Cannot fork (%m)"); + break; + case 0: + dup2 (comp[0], 0); + dup2 (uncomp[1], 1); + execlp (path, path, (char *)NULL); + exit (1); /* if we get, execlp() failed! */ + default: + if (vlc_clone (&p_sys->thread, Thread, stream, + VLC_THREAD_PRIORITY_INPUT) == 0) + ret = VLC_SUCCESS; + } +#endif /* _POSIX_SPAWN < 0 */ close (uncomp[1]); if (ret != VLC_SUCCESS) close (uncomp[0]); } close (comp[0]); if (ret != VLC_SUCCESS) - { close (comp[1]); - if (p_sys->pid != -1) - while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1); - } } + + if (ret == VLC_SUCCESS) + return VLC_SUCCESS; + + if (p_sys->pid != -1) + while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1); + vlc_mutex_destroy (&p_sys->lock); + vlc_cond_destroy (&p_sys->wait); + free (p_sys); return ret; } @@ -319,7 +406,9 @@ static void Close (vlc_object_t *obj) vlc_cancel (p_sys->thread); close (p_sys->read_fd); vlc_join (p_sys->thread, NULL); - close (p_sys->write_fd); + if (p_sys->write_fd != -1) + /* Killed before EOF? */ + close (p_sys->write_fd); msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid); while (waitpid (p_sys->pid, &status, 0) == -1); @@ -327,6 +416,8 @@ static void Close (vlc_object_t *obj) if (p_sys->peeked) block_Release (p_sys->peeked); + vlc_mutex_destroy (&p_sys->lock); + vlc_cond_destroy (&p_sys->wait); free (p_sys); } @@ -370,3 +461,21 @@ static int OpenBzip2 (vlc_object_t *obj) return Open (stream, "bzcat"); } +/** + * Detects xz file format + */ +static int OpenXZ (vlc_object_t *obj) +{ + stream_t *stream = (stream_t *)obj; + const uint8_t *peek; + + /* (Try to) parse the xz stream header */ + if (stream_Peek (stream->p_source, &peek, 8) < 8) + return VLC_EGENERIC; + + if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6)) + return VLC_EGENERIC; + + msg_Dbg (obj, "detected xz compressed stream"); + return Open (stream, "xzcat"); +}