]> git.sesse.net Git - vlc/blob - modules/demux/decomp.c
Remove most stray semi-colons in module descriptions
[vlc] / modules / demux / decomp.c
1 /*****************************************************************************
2  * decomp.c : Decompression module for vlc
3  *****************************************************************************
4  * Copyright © 2008 Rémi Denis-Courmont
5  *
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.
10  *
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.
15  *
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  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
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>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <spawn.h>
33 #include <sys/wait.h>
34 #include <sys/ioctl.h>
35 #ifdef __linux__
36 # include <sys/uio.h>
37 # include <sys/mman.h>
38 #endif
39
40 #include <assert.h>
41
42 static int  OpenGzip (vlc_object_t *);
43 static int  OpenBzip2 (vlc_object_t *);
44 static void Close (vlc_object_t *);
45
46 vlc_module_begin ()
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 */
53     /* --demux support */
54
55     add_submodule ()
56     set_callbacks (OpenGzip, Close)
57 vlc_module_end ()
58
59 static int Demux   (demux_t *);
60 static int Control (demux_t *, int i_query, va_list args);
61
62 struct demux_sys_t
63 {
64     stream_t    *out;
65     vlc_thread_t thread;
66     pid_t        pid;
67     int          write_fd, read_fd;
68 };
69
70 static void cloexec (int fd)
71 {
72     int flags = fcntl (fd, F_GETFD);
73     fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
74 }
75
76 extern char **environ;
77
78 static const size_t bufsize = 65536;
79 static void cleanup_mmap (void *addr)
80 {
81     munmap (addr, bufsize);
82 }
83
84
85 static void *Thread (void *data)
86 {
87     demux_t *demux = data;
88     demux_sys_t *p_sys = demux->p_sys;
89     int fd = p_sys->write_fd;
90     bool error = false;
91
92     do
93     {
94         ssize_t len;
95         int canc = vlc_savecancel ();
96 #ifdef __linux__
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);
100 #else
101         unsigned char buf[bufsize];
102 #endif
103
104         len = stream_Read (demux->s, buf, bufsize);
105         vlc_restorecancel (canc);
106
107         if (len <= 0)
108             break;
109
110         for (ssize_t i = 0, j = 0; i < len; i += j)
111         {
112             struct iovec iov[1] = { { buf + i, len - i, } };
113
114 #ifdef __linux__
115             j = vmsplice (fd, iov, 1, SPLICE_F_GIFT);
116 #else
117             j = writev (fd, iov, 1);
118 #endif
119             if (j <= 0)
120             {
121                 if (j == 0)
122                     errno = EPIPE;
123                 msg_Err (demux, "cannot write data (%m)");
124                 error = true;
125                 break;
126             }
127         }
128 #ifdef __linux__
129         vlc_cleanup_run (); /* munmap (buf, bufsize) */
130 #endif
131     }
132     while (!error);
133
134     msg_Dbg (demux, "compressed stream at EOF");
135     return NULL;
136 }
137
138
139 #define MIN_BLOCK (1 << 10)
140 #define MAX_BLOCK (1 << 20)
141 /**
142  * Read data, decompress it, and forward
143  * @return -1 in case of error, 0 in case of EOF, 1 otherwise.
144  */
145 static int Demux (demux_t *demux)
146 {
147     demux_sys_t *p_sys = demux->p_sys;
148     int length, fd = p_sys->read_fd;
149
150 #ifdef TIOCINQ
151     if (ioctl (fd, TIOCINQ, &length) == 0)
152     {
153         if (length > MAX_BLOCK)
154             length = MAX_BLOCK;
155         else
156         if (length < MIN_BLOCK)
157             length = MIN_BLOCK;
158     }
159     else
160 #endif
161         length = MIN_BLOCK;
162
163     block_t *block = block_Alloc (length);
164     if (block == NULL)
165         return VLC_ENOMEM;
166
167     length = net_Read (demux, fd, NULL, block->p_buffer, length, false);
168     if (length <= 0)
169         return 0;
170     block->i_buffer = length;
171     stream_DemuxSend (p_sys->out, block);
172     return 1;
173 }
174
175
176 /*****************************************************************************
177  * Control:
178  *****************************************************************************/
179 static int Control (demux_t *demux, int query, va_list args)
180 {
181     /*demux_sys_t *p_sys = demux->p_sys;*/
182     (void)demux;
183     (void)query; (void)args;
184     return VLC_EGENERIC;
185 }
186
187 /**
188  * Pipe data through an external executable.
189  * @param demux the demux object.
190  * @param path path to the executable.
191  */
192 static int Open (demux_t *demux, const char *path)
193 {
194     demux_sys_t *p_sys = demux->p_sys = malloc (sizeof (*p_sys));
195     if (p_sys == NULL)
196         return VLC_ENOMEM;
197
198     demux->pf_demux = Demux;
199     demux->pf_control = Control;
200
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;
204     int comp[2];
205     if (pipe (comp) == 0)
206     {
207         cloexec (comp[1]);
208         p_sys->write_fd = comp[1];
209
210         int uncomp[2];
211         if (pipe (uncomp) == 0)
212         {
213             cloexec (uncomp[0]);
214             p_sys->read_fd = uncomp[0];
215
216             posix_spawn_file_actions_t actions;
217             if (posix_spawn_file_actions_init (&actions) == 0)
218             {
219                 char *const argv[] = { (char *)path, NULL };
220
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,
226                                    environ))
227                 {
228                     p_sys->out = stream_DemuxNew (demux, "", demux->out);
229                     if (p_sys->out != NULL)
230                     {
231                         if (vlc_clone (&p_sys->thread, Thread, demux,
232                                        VLC_THREAD_PRIORITY_INPUT) == 0)
233                             ret = VLC_SUCCESS;
234                         else
235                             stream_DemuxDelete (p_sys->out);
236                     }
237                     else
238                         msg_Err (demux, "Cannot create demux");
239                 }
240                 else
241                     msg_Err (demux, "Cannot execute %s", path);
242                 posix_spawn_file_actions_destroy (&actions);
243             }
244             if (ret != VLC_SUCCESS)
245             {
246                 close (comp[1]);
247                 close (comp[0]);
248             }
249         }
250         if (ret != VLC_SUCCESS)
251             close (uncomp[0]);
252         close (uncomp[1]);
253     }
254     return ret;
255 }
256
257
258 /**
259  * Releases allocate resources.
260  */
261 static void Close (vlc_object_t *obj)
262 {
263     demux_t *demux = (demux_t *)obj;
264     demux_sys_t *p_sys = demux->p_sys;
265     int status;
266
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);
272
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);
276
277     free (p_sys);
278 }
279
280
281 /**
282  * Detects gzip file format
283  */
284 static int OpenGzip (vlc_object_t *obj)
285 {
286     demux_t       *demux = (demux_t *)obj;
287     stream_t      *stream = demux->s;
288     const uint8_t *peek;
289
290     if (stream_Peek (stream, &peek, 3) < 3)
291         return VLC_EGENERIC;
292
293     if (memcmp (peek, "\x1f\x8b\x08", 3))
294         return VLC_EGENERIC;
295
296     msg_Dbg (obj, "detected gzip compressed stream");
297     return Open (demux, "zcat");
298 }
299
300
301 /**
302  * Detects bzip2 file format
303  */
304 static int OpenBzip2 (vlc_object_t *obj)
305 {
306     demux_t       *demux = (demux_t *)obj;
307     stream_t      *stream = demux->s;
308     const uint8_t *peek;
309
310     /* (Try to) parse the bzip2 header */
311     if (stream_Peek (stream, &peek, 10) < 10)
312         return VLC_EGENERIC;
313
314     if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
315      || memcmp (peek, "\x31\x41\x59\x26\x53\x59", 6))
316         return VLC_EGENERIC;
317
318     msg_Dbg (obj, "detected bzip2 compressed stream");
319     return Open (demux, "bzcat");
320 }
321