]> git.sesse.net Git - vlc/blob - modules/stream_filter/decomp.c
rotate: use atomic variable instead of spin lock
[vlc] / modules / stream_filter / decomp.c
1 /*****************************************************************************
2  * decomp.c : Decompression module for vlc
3  *****************************************************************************
4  * Copyright © 2008-2009 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * 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 Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * 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_stream.h>
28 #include <vlc_network.h>
29 #include <vlc_fs.h>
30 #include <assert.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #ifndef _POSIX_SPAWN
34 # define _POSIX_SPAWN (-1)
35 #endif
36 #include <fcntl.h>
37 #if (_POSIX_SPAWN >= 0)
38 # include <spawn.h>
39 #endif
40 #include <sys/wait.h>
41 #include <sys/ioctl.h>
42 #if defined (__linux__) && defined (HAVE_VMSPLICE)
43 # include <sys/uio.h>
44 # include <sys/mman.h>
45 #else
46 # undef HAVE_VMSPLICE
47 #endif
48
49 static int  OpenGzip (vlc_object_t *);
50 static int  OpenBzip2 (vlc_object_t *);
51 static int  OpenXZ (vlc_object_t *);
52 static void Close (vlc_object_t *);
53
54 vlc_module_begin ()
55     set_category (CAT_INPUT)
56     set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
57     set_capability ("stream_filter", 20)
58
59     set_description (N_("LZMA decompression"))
60     set_callbacks (OpenXZ, Close)
61
62     add_submodule ()
63     set_description (N_("Burrows-Wheeler decompression"))
64     set_callbacks (OpenBzip2, Close)
65     /* TODO: access shortnames for stream_UrlNew() */
66
67     add_submodule ()
68     set_description (N_("gzip decompression"))
69     set_callbacks (OpenGzip, Close)
70 vlc_module_end ()
71
72 struct stream_sys_t
73 {
74     block_t      *peeked;
75     uint64_t     offset;
76     vlc_thread_t thread;
77     pid_t        pid;
78     int          write_fd, read_fd;
79 };
80
81 extern char **environ;
82
83 static const size_t bufsize = 65536;
84 #ifdef HAVE_VMSPLICE
85 static void cleanup_mmap (void *addr)
86 {
87     munmap (addr, bufsize);
88 }
89 #endif
90
91 static void *Thread (void *data)
92 {
93     stream_t *stream = data;
94     stream_sys_t *p_sys = stream->p_sys;
95 #ifdef HAVE_VMSPLICE
96     const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
97 #endif
98     int fd = p_sys->write_fd;
99     bool error = false;
100
101     do
102     {
103         ssize_t len;
104         int canc = vlc_savecancel ();
105 #ifdef HAVE_VMSPLICE
106         unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
107                                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
108         if (unlikely(buf == MAP_FAILED))
109             break;
110         vlc_cleanup_push (cleanup_mmap, buf);
111 #else
112         unsigned char *buf = malloc (bufsize);
113         if (unlikely(buf == NULL))
114             break;
115         vlc_cleanup_push (free, buf);
116 #endif
117
118         len = stream_Read (stream->p_source, buf, bufsize);
119         vlc_restorecancel (canc);
120         error = len <= 0;
121
122         for (ssize_t i = 0, j; i < len; i += j)
123         {
124 #ifdef HAVE_VMSPLICE
125             if ((len - i) <= page_mask) /* incomplete last page */
126                 j = write (fd, buf + i, len - i);
127             else
128             {
129                 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
130                 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
131             }
132             if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
133 #endif
134             j = write (fd, buf + i, len - i);
135             if (j <= 0)
136             {
137                 if (j == 0)
138                     errno = EPIPE;
139                 msg_Err (stream, "cannot write data (%m)");
140                 error = true;
141                 break;
142             }
143         }
144         vlc_cleanup_run (); /* free (buf) */
145     }
146     while (!error);
147
148     msg_Dbg (stream, "compressed stream at EOF");
149     /* Let child process know about EOF */
150     p_sys->write_fd = -1;
151     close (fd);
152     return NULL;
153 }
154
155
156 static int Peek (stream_t *, const uint8_t **, unsigned int);
157
158 #define MIN_BLOCK (1 << 10)
159 #define MAX_BLOCK (1 << 20)
160 /**
161  * Reads decompressed from the decompression program
162  * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
163  */
164 static int Read (stream_t *stream, void *buf, unsigned int buflen)
165 {
166     stream_sys_t *p_sys = stream->p_sys;
167     block_t *peeked;
168     ssize_t length;
169
170     if (buf == NULL) /* caller skips data, get big enough peek buffer */
171         buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
172
173     if ((peeked = p_sys->peeked) != NULL)
174     {   /* dequeue peeked data */
175         length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
176         if (buf != NULL)
177         {
178             memcpy (buf, peeked->p_buffer, length);
179             buf = ((char *)buf) + length;
180         }
181         buflen -= length;
182         peeked->p_buffer += length;
183         peeked->i_buffer -= length;
184         if (peeked->i_buffer == 0)
185         {
186             block_Release (peeked);
187             p_sys->peeked = NULL;
188         }
189         p_sys->offset += length;
190
191         if (buflen > 0)
192             length += Read (stream, ((char *)buf) + length, buflen - length);
193         return length;
194     }
195     assert ((buf != NULL) || (buflen == 0));
196
197     length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
198     if (length < 0)
199         return 0;
200     p_sys->offset += length;
201     return length;
202 }
203
204 /**
205  *
206  */
207 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
208 {
209     stream_sys_t *p_sys = stream->p_sys;
210     block_t *peeked = p_sys->peeked;
211     size_t curlen = 0;
212     int fd = p_sys->read_fd;
213
214     if (peeked == NULL)
215         peeked = block_Alloc (len);
216     else if ((curlen = peeked->i_buffer) < len)
217         peeked = block_Realloc (peeked, 0, len);
218
219     if ((p_sys->peeked = peeked) == NULL)
220         return 0;
221
222     if (curlen < len)
223     {
224         ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
225                                 len - curlen, true);
226         if (val >= 0)
227         {
228             curlen += val;
229             peeked->i_buffer = curlen;
230         }
231     }
232     *pbuf = peeked->p_buffer;
233     return curlen;
234 }
235
236 /**
237  *
238  */
239 static int Control (stream_t *stream, int query, va_list args)
240 {
241     stream_sys_t *p_sys = stream->p_sys;
242
243     switch (query)
244     {
245         case STREAM_CAN_SEEK:
246         case STREAM_CAN_FASTSEEK:
247             *(va_arg (args, bool *)) = false;
248             break;
249         case STREAM_GET_POSITION:
250             *(va_arg (args, uint64_t *)) = p_sys->offset;
251             break;
252         case STREAM_GET_SIZE:
253             *(va_arg (args, uint64_t *)) = 0;
254             break;
255         default:
256             return VLC_EGENERIC;
257     }
258     return VLC_SUCCESS;
259 }
260
261 /**
262  * Pipe data through an external executable.
263  * @param stream the stream filter object.
264  * @param path path to the executable.
265  */
266 static int Open (stream_t *stream, const char *path)
267 {
268     stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
269     if (p_sys == NULL)
270         return VLC_ENOMEM;
271
272     stream->pf_read = Read;
273     stream->pf_peek = Peek;
274     stream->pf_control = Control;
275     p_sys->peeked = NULL;
276     p_sys->offset = 0;
277     p_sys->pid = -1;
278
279     /* I am not a big fan of the pyramid style, but I cannot think of anything
280      * better here. There are too many failure cases. */
281     int ret = VLC_EGENERIC;
282     int comp[2];
283
284     /* We use two pipes rather than one stream socket pair, so that we can
285      * use vmsplice() on Linux. */
286     if (vlc_pipe (comp) == 0)
287     {
288         p_sys->write_fd = comp[1];
289
290         int uncomp[2];
291         if (vlc_pipe (uncomp) == 0)
292         {
293             p_sys->read_fd = uncomp[0];
294
295 #if (_POSIX_SPAWN >= 0)
296             posix_spawn_file_actions_t actions;
297             if (posix_spawn_file_actions_init (&actions) == 0)
298             {
299                 char *const argv[] = { (char *)path, NULL };
300
301                 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
302                  && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
303                  && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
304                                    environ))
305                 {
306                     if (vlc_clone (&p_sys->thread, Thread, stream,
307                                    VLC_THREAD_PRIORITY_INPUT) == 0)
308                         ret = VLC_SUCCESS;
309                 }
310                 else
311                 {
312                     msg_Err (stream, "Cannot execute %s", path);
313                     p_sys->pid = -1;
314                 }
315                 posix_spawn_file_actions_destroy (&actions);
316             }
317 #else /* _POSIX_SPAWN */
318             switch (p_sys->pid = fork ())
319             {
320                 case -1:
321                     msg_Err (stream, "Cannot fork (%m)");
322                     break;
323                 case 0:
324                     dup2 (comp[0], 0);
325                     dup2 (uncomp[1], 1);
326                     execlp (path, path, (char *)NULL);
327                     exit (1); /* if we get, execlp() failed! */
328                 default:
329                     if (vlc_clone (&p_sys->thread, Thread, stream,
330                                    VLC_THREAD_PRIORITY_INPUT) == 0)
331                         ret = VLC_SUCCESS;
332             }
333 #endif /* _POSIX_SPAWN < 0 */
334             close (uncomp[1]);
335             if (ret != VLC_SUCCESS)
336                 close (uncomp[0]);
337         }
338         close (comp[0]);
339         if (ret != VLC_SUCCESS)
340         {
341             close (comp[1]);
342             if (p_sys->pid != -1)
343                 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
344         }
345     }
346     return ret;
347 }
348
349
350 /**
351  * Releases allocate resources.
352  */
353 static void Close (vlc_object_t *obj)
354 {
355     stream_t *stream = (stream_t *)obj;
356     stream_sys_t *p_sys = stream->p_sys;
357     int status;
358
359     vlc_cancel (p_sys->thread);
360     close (p_sys->read_fd);
361     vlc_join (p_sys->thread, NULL);
362     if (p_sys->write_fd != -1)
363         /* Killed before EOF? */
364         close (p_sys->write_fd);
365
366     msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
367     while (waitpid (p_sys->pid, &status, 0) == -1);
368     msg_Dbg (obj, "exit status %d", status);
369
370     if (p_sys->peeked)
371         block_Release (p_sys->peeked);
372     free (p_sys);
373 }
374
375
376 /**
377  * Detects gzip file format
378  */
379 static int OpenGzip (vlc_object_t *obj)
380 {
381     stream_t      *stream = (stream_t *)obj;
382     const uint8_t *peek;
383
384     if (stream_Peek (stream->p_source, &peek, 3) < 3)
385         return VLC_EGENERIC;
386
387     if (memcmp (peek, "\x1f\x8b\x08", 3))
388         return VLC_EGENERIC;
389
390     msg_Dbg (obj, "detected gzip compressed stream");
391     return Open (stream, "zcat");
392 }
393
394
395 /**
396  * Detects bzip2 file format
397  */
398 static int OpenBzip2 (vlc_object_t *obj)
399 {
400     stream_t      *stream = (stream_t *)obj;
401     const uint8_t *peek;
402
403     /* (Try to) parse the bzip2 header */
404     if (stream_Peek (stream->p_source, &peek, 10) < 10)
405         return VLC_EGENERIC;
406
407     if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
408      || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
409         return VLC_EGENERIC;
410
411     msg_Dbg (obj, "detected bzip2 compressed stream");
412     return Open (stream, "bzcat");
413 }
414
415 /**
416  * Detects xz file format
417  */
418 static int OpenXZ (vlc_object_t *obj)
419 {
420     stream_t      *stream = (stream_t *)obj;
421     const uint8_t *peek;
422
423     /* (Try to) parse the xz stream header */
424     if (stream_Peek (stream->p_source, &peek, 8) < 8)
425         return VLC_EGENERIC;
426
427     if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
428         return VLC_EGENERIC;
429
430     msg_Dbg (obj, "detected xz compressed stream");
431     return Open (stream, "xzcat");
432 }