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>
38 static int OpenGzip (vlc_object_t *);
39 static int OpenBzip2 (vlc_object_t *);
40 static void Close (vlc_object_t *);
43 set_description (N_("Decompression"));
44 set_category (CAT_INPUT);
45 set_subcategory (SUBCAT_INPUT_DEMUX);
46 set_capability ("demux", 20);
47 set_callbacks (OpenBzip2, Close);
48 /* TODO: shortnames */
52 set_callbacks (OpenGzip, Close);
55 static int Demux (demux_t *);
56 static int Control (demux_t *, int i_query, va_list args);
63 int write_fd, read_fd;
66 static void cloexec (int fd)
68 int flags = fcntl (fd, F_GETFD);
69 fcntl (fd, F_SETFD, O_CLOEXEC | (flags != -1) ? flags : 0);
72 extern char **environ;
74 static void cleanup_fd (void *data)
76 close ((intptr_t)data);
79 static void *Thread (void *data)
81 demux_t *demux = data;
82 demux_sys_t *p_sys = demux->p_sys;
83 int fd = p_sys->write_fd;
86 vlc_cleanup_push (cleanup_fd, (void *)(intptr_t)fd);
91 /* TODO: mmap() + vmsplice() */
93 unsigned char buf[65536];
94 int canc = vlc_savecancel ();
95 ssize_t len = stream_Read (demux->s, buf, sizeof (buf));
97 vlc_restorecancel (canc);
102 for (ssize_t i = 0, j = 0; i < len; i += j)
104 j = write (fd, buf + i, len - i);
107 msg_Err (demux, "cannot write data (%m)");
115 vlc_cleanup_run (); /* close(fd) */
120 #define MIN_BLOCK (1 << 10)
121 #define MAX_BLOCK (1 << 20)
123 * Read data, decompress it, and forward
124 * @return -1 in case of error, 0 in case of EOF, 1 otherwise.
126 static int Demux (demux_t *demux)
128 demux_sys_t *p_sys = demux->p_sys;
129 int length, fd = p_sys->read_fd;
132 if (ioctl (fd, TIOCINQ, &length) == 0)
134 if (length > MAX_BLOCK)
137 if (length < MIN_BLOCK)
144 block_t *block = block_Alloc (length);
148 length = net_Read (demux, fd, NULL, block->p_buffer, length, false);
151 block->i_buffer = length;
152 stream_DemuxSend (p_sys->out, block);
157 /*****************************************************************************
159 *****************************************************************************/
160 static int Control (demux_t *demux, int query, va_list args)
162 /*demux_sys_t *p_sys = demux->p_sys;*/
164 (void)query; (void)args;
169 * Pipe data through an external executable.
170 * @param demux the demux object.
171 * @param path path to the executable.
173 static int Open (demux_t *demux, const char *path)
175 demux_sys_t *p_sys = demux->p_sys = malloc (sizeof (*p_sys));
179 demux->pf_demux = Demux;
180 demux->pf_control = Control;
182 /* I am not a big fan of the pyramid style, but I cannot think of anything
183 * better here. There are too many failure cases. */
184 int ret = VLC_EGENERIC;
186 if (pipe (comp) == 0)
189 p_sys->write_fd = comp[1];
192 if (pipe (uncomp) == 0)
195 p_sys->read_fd = uncomp[0];
197 posix_spawn_file_actions_t actions;
198 if (posix_spawn_file_actions_init (&actions) == 0)
200 char *const argv[] = { (char *)path, NULL };
202 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
203 && !posix_spawn_file_actions_addclose (&actions, comp[0])
204 && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
205 && !posix_spawn_file_actions_addclose (&actions, comp[1])
206 && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
209 p_sys->out = stream_DemuxNew (demux, "", demux->out);
210 if (p_sys->out != NULL)
212 if (vlc_clone (&p_sys->thread, Thread, demux,
213 VLC_THREAD_PRIORITY_INPUT) == 0)
216 stream_DemuxDelete (p_sys->out);
219 msg_Err (demux, "Cannot create demux");
222 msg_Err (demux, "Cannot execute %s", path);
223 posix_spawn_file_actions_destroy (&actions);
225 if (ret != VLC_SUCCESS)
231 if (ret != VLC_SUCCESS)
240 * Releases allocate resources.
242 static void Close (vlc_object_t *obj)
244 demux_t *demux = (demux_t *)obj;
245 demux_sys_t *p_sys = demux->p_sys;
248 vlc_cancel (p_sys->thread);
249 stream_DemuxDelete (p_sys->out);
250 close (p_sys->read_fd);
251 vlc_join (p_sys->thread, NULL);
253 msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
254 while (waitpid (p_sys->pid, &status, 0) == -1);
255 msg_Dbg (obj, "exit status %d", status);
262 * Detects gzip file format
264 static int OpenGzip (vlc_object_t *obj)
266 demux_t *demux = (demux_t *)obj;
267 stream_t *stream = demux->s;
270 if (stream_Peek (stream, &peek, 3) < 3)
273 if (memcmp (peek, "\x1f\x8b\x08", 3))
276 msg_Dbg (obj, "detected gzip compressed stream");
277 return Open (demux, "zcat");
282 * Detects bzip2 file format
284 static int OpenBzip2 (vlc_object_t *obj)
286 demux_t *demux = (demux_t *)obj;
287 stream_t *stream = demux->s;
290 /* (Try to) parse the bzip2 header */
291 if (stream_Peek (stream, &peek, 10) < 10)
294 if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
295 || memcmp (peek, "\x31\x41\x59\x26\x53\x59", 6))
298 msg_Dbg (obj, "detected bzip2 compressed stream");
299 return Open (demux, "bzcat");