]> git.sesse.net Git - vlc/blob - modules/stream_filter/decomp.c
vlc_plugin: allow overriding module meta-infos
[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     /* Thread data */
75     int          write_fd;
76
77     /* Shared data */
78     vlc_cond_t   wait;
79     vlc_mutex_t  lock;
80     bool         paused;
81
82     /* Caller data */
83     vlc_thread_t thread;
84     pid_t        pid;
85
86     uint64_t     offset;
87     block_t      *peeked;
88
89     int          read_fd;
90     bool         can_pace;
91     bool         can_pause;
92     int64_t      pts_delay;
93 };
94
95 extern char **environ;
96
97 static const size_t bufsize = 65536;
98 #ifdef HAVE_VMSPLICE
99 static void cleanup_mmap (void *addr)
100 {
101     munmap (addr, bufsize);
102 }
103 #endif
104
105 static void *Thread (void *data)
106 {
107     stream_t *stream = data;
108     stream_sys_t *p_sys = stream->p_sys;
109 #ifdef HAVE_VMSPLICE
110     const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
111 #endif
112     int fd = p_sys->write_fd;
113     bool error = false;
114
115     do
116     {
117         ssize_t len;
118         int canc = vlc_savecancel ();
119 #ifdef HAVE_VMSPLICE
120         unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
121                                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
122         if (unlikely(buf == MAP_FAILED))
123             break;
124         vlc_cleanup_push (cleanup_mmap, buf);
125 #else
126         unsigned char *buf = malloc (bufsize);
127         if (unlikely(buf == NULL))
128             break;
129         vlc_cleanup_push (free, buf);
130 #endif
131
132         vlc_mutex_lock (&p_sys->lock);
133         while (p_sys->paused) /* practically always false, but... */
134             vlc_cond_wait (&p_sys->wait, &p_sys->lock);
135         len = stream_Read (stream->p_source, buf, bufsize);
136         vlc_mutex_unlock (&p_sys->lock);
137
138         vlc_restorecancel (canc);
139         error = len <= 0;
140
141         for (ssize_t i = 0, j; i < len; i += j)
142         {
143 #ifdef HAVE_VMSPLICE
144             if ((len - i) <= page_mask) /* incomplete last page */
145                 j = write (fd, buf + i, len - i);
146             else
147             {
148                 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
149                 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
150             }
151             if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
152 #endif
153             j = write (fd, buf + i, len - i);
154             if (j <= 0)
155             {
156                 if (j == 0)
157                     errno = EPIPE;
158                 msg_Err (stream, "cannot write data: %s",
159                          vlc_strerror_c(errno));
160                 error = true;
161                 break;
162             }
163         }
164         vlc_cleanup_run (); /* free (buf) */
165     }
166     while (!error);
167
168     msg_Dbg (stream, "compressed stream at EOF");
169     /* Let child process know about EOF */
170     p_sys->write_fd = -1;
171     close (fd);
172     return NULL;
173 }
174
175
176 static int Peek (stream_t *, const uint8_t **, unsigned int);
177
178 #define MIN_BLOCK (1 << 10)
179 #define MAX_BLOCK (1 << 20)
180 /**
181  * Reads decompressed from the decompression program
182  * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
183  */
184 static int Read (stream_t *stream, void *buf, unsigned int buflen)
185 {
186     stream_sys_t *sys = stream->p_sys;
187     unsigned ret = 0;
188
189     if (buf == NULL) /* caller skips data, get big enough peek buffer */
190         buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
191
192     block_t *peeked = sys->peeked;
193     if (peeked != NULL)
194     {   /* dequeue peeked data */
195         size_t length = peeked->i_buffer;
196         if (length > buflen)
197             length = buflen;
198
199         if (buf != NULL)
200         {
201             memcpy (buf, peeked->p_buffer, length);
202             buf = ((char *)buf) + length;
203         }
204         buflen -= length;
205         peeked->p_buffer += length;
206         peeked->i_buffer -= length;
207
208         if (peeked->i_buffer == 0)
209         {
210             block_Release (peeked);
211             sys->peeked = NULL;
212         }
213
214         sys->offset += length;
215         ret += length;
216     }
217     assert ((buf != NULL) || (buflen == 0));
218
219     ssize_t val = net_Read (stream, sys->read_fd, NULL, buf, buflen, false);
220     if (val > 0)
221     {
222         sys->offset += val;
223         ret += val;
224     }
225     return ret;
226 }
227
228 /**
229  *
230  */
231 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
232 {
233     stream_sys_t *sys = stream->p_sys;
234     block_t *peeked = sys->peeked;
235     size_t curlen;
236
237     if (peeked != NULL)
238     {
239         curlen = peeked->i_buffer;
240         if (curlen < len)
241            peeked = block_Realloc (peeked, 0, len);
242     }
243     else
244     {
245         curlen = 0;
246         peeked = block_Alloc (len);
247     }
248
249     sys->peeked = peeked;
250     if (unlikely(peeked == NULL))
251         return 0;
252
253     while (curlen < len)
254     {
255         ssize_t val;
256
257         val = net_Read (stream, sys->read_fd, NULL,
258                         peeked->p_buffer + curlen, len - curlen, false);
259         if (val <= 0)
260             break;
261         curlen += val;
262         peeked->i_buffer = curlen;
263     }
264     *pbuf = peeked->p_buffer;
265     return curlen;
266 }
267
268 /**
269  *
270  */
271 static int Control (stream_t *stream, int query, va_list args)
272 {
273     stream_sys_t *p_sys = stream->p_sys;
274
275     switch (query)
276     {
277         case STREAM_CAN_SEEK:
278         case STREAM_CAN_FASTSEEK:
279             *(va_arg (args, bool *)) = false;
280             break;
281         case STREAM_CAN_PAUSE:
282              *(va_arg (args, bool *)) = p_sys->can_pause;
283             break;
284         case STREAM_CAN_CONTROL_PACE:
285             *(va_arg (args, bool *)) = p_sys->can_pace;
286             break;
287         case STREAM_GET_POSITION:
288             *(va_arg (args, uint64_t *)) = p_sys->offset;
289             break;
290         case STREAM_GET_SIZE:
291             *(va_arg (args, uint64_t *)) = 0;
292             break;
293         case STREAM_GET_PTS_DELAY:
294             *va_arg (args, int64_t *) = p_sys->pts_delay;
295             break;
296         case STREAM_SET_PAUSE_STATE:
297         {
298             bool paused = va_arg (args, unsigned);
299
300             vlc_mutex_lock (&p_sys->lock);
301             stream_Control (stream->p_source, STREAM_SET_PAUSE_STATE, paused);
302             p_sys->paused = paused;
303             vlc_cond_signal (&p_sys->wait);
304             vlc_mutex_unlock (&p_sys->lock);
305             break;
306         }
307         default:
308             return VLC_EGENERIC;
309     }
310     return VLC_SUCCESS;
311 }
312
313 /**
314  * Pipe data through an external executable.
315  * @param stream the stream filter object.
316  * @param path path to the executable.
317  */
318 static int Open (stream_t *stream, const char *path)
319 {
320     stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
321     if (p_sys == NULL)
322         return VLC_ENOMEM;
323
324     stream->pf_read = Read;
325     stream->pf_peek = Peek;
326     stream->pf_control = Control;
327
328     vlc_cond_init (&p_sys->wait);
329     vlc_mutex_init (&p_sys->lock);
330     p_sys->paused = false;
331     p_sys->pid = -1;
332     p_sys->offset = 0;
333     p_sys->peeked = NULL;
334     stream_Control (stream->p_source, STREAM_CAN_PAUSE, &p_sys->can_pause);
335     stream_Control (stream->p_source, STREAM_CAN_CONTROL_PACE,
336                     &p_sys->can_pace);
337     stream_Control (stream->p_source, STREAM_GET_PTS_DELAY, &p_sys->pts_delay);
338
339     /* I am not a big fan of the pyramid style, but I cannot think of anything
340      * better here. There are too many failure cases. */
341     int ret = VLC_EGENERIC;
342     int comp[2];
343
344     /* We use two pipes rather than one stream socket pair, so that we can
345      * use vmsplice() on Linux. */
346     if (vlc_pipe (comp) == 0)
347     {
348         p_sys->write_fd = comp[1];
349
350         int uncomp[2];
351         if (vlc_pipe (uncomp) == 0)
352         {
353             p_sys->read_fd = uncomp[0];
354
355 #if (_POSIX_SPAWN >= 0)
356             posix_spawn_file_actions_t actions;
357             if (posix_spawn_file_actions_init (&actions) == 0)
358             {
359                 char *const argv[] = { (char *)path, NULL };
360
361                 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
362                  && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
363                  && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
364                                    environ))
365                 {
366                     if (vlc_clone (&p_sys->thread, Thread, stream,
367                                    VLC_THREAD_PRIORITY_INPUT) == 0)
368                         ret = VLC_SUCCESS;
369                 }
370                 else
371                 {
372                     msg_Err (stream, "cannot execute %s", path);
373                     p_sys->pid = -1;
374                 }
375                 posix_spawn_file_actions_destroy (&actions);
376             }
377 #else /* _POSIX_SPAWN */
378             switch (p_sys->pid = fork ())
379             {
380                 case -1:
381                     msg_Err (stream, "cannot fork: %s", vlc_strerror_c(errno));
382                     break;
383                 case 0:
384                     dup2 (comp[0], 0);
385                     dup2 (uncomp[1], 1);
386                     execlp (path, path, (char *)NULL);
387                     exit (1); /* if we get, execlp() failed! */
388                 default:
389                     if (vlc_clone (&p_sys->thread, Thread, stream,
390                                    VLC_THREAD_PRIORITY_INPUT) == 0)
391                         ret = VLC_SUCCESS;
392             }
393 #endif /* _POSIX_SPAWN < 0 */
394             close (uncomp[1]);
395             if (ret != VLC_SUCCESS)
396                 close (uncomp[0]);
397         }
398         close (comp[0]);
399         if (ret != VLC_SUCCESS)
400             close (comp[1]);
401     }
402
403     if (ret == VLC_SUCCESS)
404         return VLC_SUCCESS;
405
406     if (p_sys->pid != -1)
407         while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
408     vlc_mutex_destroy (&p_sys->lock);
409     vlc_cond_destroy (&p_sys->wait);
410     free (p_sys);
411     return ret;
412 }
413
414
415 /**
416  * Releases allocate resources.
417  */
418 static void Close (vlc_object_t *obj)
419 {
420     stream_t *stream = (stream_t *)obj;
421     stream_sys_t *p_sys = stream->p_sys;
422     int status;
423
424     vlc_cancel (p_sys->thread);
425     close (p_sys->read_fd);
426     vlc_join (p_sys->thread, NULL);
427     if (p_sys->write_fd != -1)
428         /* Killed before EOF? */
429         close (p_sys->write_fd);
430
431     msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
432     while (waitpid (p_sys->pid, &status, 0) == -1);
433     msg_Dbg (obj, "exit status %d", status);
434
435     if (p_sys->peeked)
436         block_Release (p_sys->peeked);
437     vlc_mutex_destroy (&p_sys->lock);
438     vlc_cond_destroy (&p_sys->wait);
439     free (p_sys);
440 }
441
442
443 /**
444  * Detects gzip file format
445  */
446 static int OpenGzip (vlc_object_t *obj)
447 {
448     stream_t      *stream = (stream_t *)obj;
449     const uint8_t *peek;
450
451     if (stream_Peek (stream->p_source, &peek, 3) < 3)
452         return VLC_EGENERIC;
453
454     if (memcmp (peek, "\x1f\x8b\x08", 3))
455         return VLC_EGENERIC;
456
457     msg_Dbg (obj, "detected gzip compressed stream");
458     return Open (stream, "zcat");
459 }
460
461
462 /**
463  * Detects bzip2 file format
464  */
465 static int OpenBzip2 (vlc_object_t *obj)
466 {
467     stream_t      *stream = (stream_t *)obj;
468     const uint8_t *peek;
469
470     /* (Try to) parse the bzip2 header */
471     if (stream_Peek (stream->p_source, &peek, 10) < 10)
472         return VLC_EGENERIC;
473
474     if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
475      || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
476         return VLC_EGENERIC;
477
478     msg_Dbg (obj, "detected bzip2 compressed stream");
479     return Open (stream, "bzcat");
480 }
481
482 /**
483  * Detects xz file format
484  */
485 static int OpenXZ (vlc_object_t *obj)
486 {
487     stream_t      *stream = (stream_t *)obj;
488     const uint8_t *peek;
489
490     /* (Try to) parse the xz stream header */
491     if (stream_Peek (stream->p_source, &peek, 8) < 8)
492         return VLC_EGENERIC;
493
494     if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
495         return VLC_EGENERIC;
496
497     msg_Dbg (obj, "detected xz compressed stream");
498     return Open (stream, "xzcat");
499 }