1 /*****************************************************************************
2 * decomp.c : Decompression module for vlc
3 *****************************************************************************
4 * Copyright © 2008 Rémi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_demux.h>
28 #include <vlc_stream.h>
29 #include <vlc_network.h>
34 #include <sys/ioctl.h>
37 # include <sys/mman.h>
42 static int OpenGzip (vlc_object_t *);
43 static int OpenBzip2 (vlc_object_t *);
44 static void Close (vlc_object_t *);
47 set_description (N_("Decompression"));
48 set_category (CAT_INPUT);
49 set_subcategory (SUBCAT_INPUT_DEMUX);
50 set_capability ("demux", 20);
51 set_callbacks (OpenBzip2, Close);
52 /* TODO: shortnames */
56 set_callbacks (OpenGzip, Close);
59 static int Demux (demux_t *);
60 static int Control (demux_t *, int i_query, va_list args);
67 int write_fd, read_fd;
70 static void cloexec (int fd)
72 int flags = fcntl (fd, F_GETFD);
73 fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
76 extern char **environ;
78 static const size_t bufsize = 65536;
79 static void cleanup_mmap (void *addr)
81 munmap (addr, bufsize);
85 static void *Thread (void *data)
87 demux_t *demux = data;
88 demux_sys_t *p_sys = demux->p_sys;
89 int fd = p_sys->write_fd;
95 int canc = vlc_savecancel ();
97 unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
98 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
99 vlc_cleanup_push (cleanup_mmap, buf);
101 unsigned char buf[bufsize];
104 len = stream_Read (demux->s, buf, bufsize);
105 vlc_restorecancel (canc);
110 for (ssize_t i = 0, j = 0; i < len; i += j)
112 struct iovec iov[1] = { { buf + i, len - i, } };
115 j = vmsplice (fd, iov, 1, SPLICE_F_GIFT);
117 j = writev (fd, iov, 1);
123 msg_Err (demux, "cannot write data (%m)");
129 vlc_cleanup_run (); /* munmap (buf, bufsize) */
134 msg_Dbg (demux, "compressed stream at EOF");
139 #define MIN_BLOCK (1 << 10)
140 #define MAX_BLOCK (1 << 20)
142 * Read data, decompress it, and forward
143 * @return -1 in case of error, 0 in case of EOF, 1 otherwise.
145 static int Demux (demux_t *demux)
147 demux_sys_t *p_sys = demux->p_sys;
148 int length, fd = p_sys->read_fd;
151 if (ioctl (fd, TIOCINQ, &length) == 0)
153 if (length > MAX_BLOCK)
156 if (length < MIN_BLOCK)
163 block_t *block = block_Alloc (length);
167 length = net_Read (demux, fd, NULL, block->p_buffer, length, false);
170 block->i_buffer = length;
171 stream_DemuxSend (p_sys->out, block);
176 /*****************************************************************************
178 *****************************************************************************/
179 static int Control (demux_t *demux, int query, va_list args)
181 /*demux_sys_t *p_sys = demux->p_sys;*/
183 (void)query; (void)args;
188 * Pipe data through an external executable.
189 * @param demux the demux object.
190 * @param path path to the executable.
192 static int Open (demux_t *demux, const char *path)
194 demux_sys_t *p_sys = demux->p_sys = malloc (sizeof (*p_sys));
198 demux->pf_demux = Demux;
199 demux->pf_control = Control;
201 /* I am not a big fan of the pyramid style, but I cannot think of anything
202 * better here. There are too many failure cases. */
203 int ret = VLC_EGENERIC;
205 if (pipe (comp) == 0)
208 p_sys->write_fd = comp[1];
211 if (pipe (uncomp) == 0)
214 p_sys->read_fd = uncomp[0];
216 posix_spawn_file_actions_t actions;
217 if (posix_spawn_file_actions_init (&actions) == 0)
219 char *const argv[] = { (char *)path, NULL };
221 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
222 && !posix_spawn_file_actions_addclose (&actions, comp[0])
223 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
224 && !posix_spawn_file_actions_addclose (&actions, uncomp[1])
225 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
228 p_sys->out = stream_DemuxNew (demux, "", demux->out);
229 if (p_sys->out != NULL)
231 if (vlc_clone (&p_sys->thread, Thread, demux,
232 VLC_THREAD_PRIORITY_INPUT) == 0)
235 stream_DemuxDelete (p_sys->out);
238 msg_Err (demux, "Cannot create demux");
241 msg_Err (demux, "Cannot execute %s", path);
242 posix_spawn_file_actions_destroy (&actions);
244 if (ret != VLC_SUCCESS)
250 if (ret != VLC_SUCCESS)
259 * Releases allocate resources.
261 static void Close (vlc_object_t *obj)
263 demux_t *demux = (demux_t *)obj;
264 demux_sys_t *p_sys = demux->p_sys;
267 vlc_cancel (p_sys->thread);
268 stream_DemuxDelete (p_sys->out);
269 close (p_sys->read_fd);
270 vlc_join (p_sys->thread, NULL);
271 close (p_sys->write_fd);
273 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
274 while (waitpid (p_sys->pid, &status, 0) == -1);
275 msg_Dbg (obj, "exit status %d", status);
282 * Detects gzip file format
284 static int OpenGzip (vlc_object_t *obj)
286 demux_t *demux = (demux_t *)obj;
287 stream_t *stream = demux->s;
290 if (stream_Peek (stream, &peek, 3) < 3)
293 if (memcmp (peek, "\x1f\x8b\x08", 3))
296 msg_Dbg (obj, "detected gzip compressed stream");
297 return Open (demux, "zcat");
302 * Detects bzip2 file format
304 static int OpenBzip2 (vlc_object_t *obj)
306 demux_t *demux = (demux_t *)obj;
307 stream_t *stream = demux->s;
310 /* (Try to) parse the bzip2 header */
311 if (stream_Peek (stream, &peek, 10) < 10)
314 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
315 || memcmp (peek, "\x31\x41\x59\x26\x53\x59", 6))
318 msg_Dbg (obj, "detected bzip2 compressed stream");
319 return Open (demux, "bzcat");