]> git.sesse.net Git - vlc/blob - modules/stream_filter/decomp.c
Smooth: remove tautology
[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 *p_sys = stream->p_sys;
187     block_t *peeked;
188     ssize_t length;
189
190     if (buf == NULL) /* caller skips data, get big enough peek buffer */
191         buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
192
193     if ((peeked = p_sys->peeked) != NULL)
194     {   /* dequeue peeked data */
195         length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
196         if (buf != NULL)
197         {
198             memcpy (buf, peeked->p_buffer, length);
199             buf = ((char *)buf) + length;
200         }
201         buflen -= length;
202         peeked->p_buffer += length;
203         peeked->i_buffer -= length;
204         if (peeked->i_buffer == 0)
205         {
206             block_Release (peeked);
207             p_sys->peeked = NULL;
208         }
209         p_sys->offset += length;
210
211         if (buflen > 0)
212             length += Read (stream, ((char *)buf) + length, buflen - length);
213         return length;
214     }
215     assert ((buf != NULL) || (buflen == 0));
216
217     length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
218     if (length < 0)
219         return 0;
220     p_sys->offset += length;
221     return length;
222 }
223
224 /**
225  *
226  */
227 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
228 {
229     stream_sys_t *p_sys = stream->p_sys;
230     block_t *peeked = p_sys->peeked;
231     size_t curlen = 0;
232     int fd = p_sys->read_fd;
233
234     if (peeked == NULL)
235         peeked = block_Alloc (len);
236     else if ((curlen = peeked->i_buffer) < len)
237         peeked = block_Realloc (peeked, 0, len);
238
239     if ((p_sys->peeked = peeked) == NULL)
240         return 0;
241
242     while (curlen < len)
243     {
244         ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
245                                 len - curlen, false);
246         if (val <= 0)
247             break;
248         curlen += val;
249         peeked->i_buffer = curlen;
250     }
251     *pbuf = peeked->p_buffer;
252     return curlen;
253 }
254
255 /**
256  *
257  */
258 static int Control (stream_t *stream, int query, va_list args)
259 {
260     stream_sys_t *p_sys = stream->p_sys;
261
262     switch (query)
263     {
264         case STREAM_CAN_SEEK:
265         case STREAM_CAN_FASTSEEK:
266             *(va_arg (args, bool *)) = false;
267             break;
268         case STREAM_CAN_PAUSE:
269              *(va_arg (args, bool *)) = p_sys->can_pause;
270             break;
271         case STREAM_CAN_CONTROL_PACE:
272             *(va_arg (args, bool *)) = p_sys->can_pace;
273             break;
274         case STREAM_GET_POSITION:
275             *(va_arg (args, uint64_t *)) = p_sys->offset;
276             break;
277         case STREAM_GET_SIZE:
278             *(va_arg (args, uint64_t *)) = 0;
279             break;
280         case STREAM_GET_PTS_DELAY:
281             *va_arg (args, int64_t *) = p_sys->pts_delay;
282             break;
283         case STREAM_SET_PAUSE_STATE:
284         {
285             bool paused = va_arg (args, unsigned);
286
287             vlc_mutex_lock (&p_sys->lock);
288             stream_Control (stream->p_source, STREAM_SET_PAUSE_STATE, paused);
289             p_sys->paused = paused;
290             vlc_cond_signal (&p_sys->wait);
291             vlc_mutex_unlock (&p_sys->lock);
292             break;
293         }
294         default:
295             return VLC_EGENERIC;
296     }
297     return VLC_SUCCESS;
298 }
299
300 /**
301  * Pipe data through an external executable.
302  * @param stream the stream filter object.
303  * @param path path to the executable.
304  */
305 static int Open (stream_t *stream, const char *path)
306 {
307     stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
308     if (p_sys == NULL)
309         return VLC_ENOMEM;
310
311     stream->pf_read = Read;
312     stream->pf_peek = Peek;
313     stream->pf_control = Control;
314
315     vlc_cond_init (&p_sys->wait);
316     vlc_mutex_init (&p_sys->lock);
317     p_sys->paused = false;
318     p_sys->pid = -1;
319     p_sys->offset = 0;
320     p_sys->peeked = NULL;
321     stream_Control (stream->p_source, STREAM_CAN_PAUSE, &p_sys->can_pause);
322     stream_Control (stream->p_source, STREAM_CAN_CONTROL_PACE,
323                     &p_sys->can_pace);
324     stream_Control (stream->p_source, STREAM_GET_PTS_DELAY, &p_sys->pts_delay);
325
326     /* I am not a big fan of the pyramid style, but I cannot think of anything
327      * better here. There are too many failure cases. */
328     int ret = VLC_EGENERIC;
329     int comp[2];
330
331     /* We use two pipes rather than one stream socket pair, so that we can
332      * use vmsplice() on Linux. */
333     if (vlc_pipe (comp) == 0)
334     {
335         p_sys->write_fd = comp[1];
336
337         int uncomp[2];
338         if (vlc_pipe (uncomp) == 0)
339         {
340             p_sys->read_fd = uncomp[0];
341
342 #if (_POSIX_SPAWN >= 0)
343             posix_spawn_file_actions_t actions;
344             if (posix_spawn_file_actions_init (&actions) == 0)
345             {
346                 char *const argv[] = { (char *)path, NULL };
347
348                 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
349                  && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
350                  && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
351                                    environ))
352                 {
353                     if (vlc_clone (&p_sys->thread, Thread, stream,
354                                    VLC_THREAD_PRIORITY_INPUT) == 0)
355                         ret = VLC_SUCCESS;
356                 }
357                 else
358                 {
359                     msg_Err (stream, "cannot execute %s", path);
360                     p_sys->pid = -1;
361                 }
362                 posix_spawn_file_actions_destroy (&actions);
363             }
364 #else /* _POSIX_SPAWN */
365             switch (p_sys->pid = fork ())
366             {
367                 case -1:
368                     msg_Err (stream, "cannot fork: %s", vlc_strerror_c(errno));
369                     break;
370                 case 0:
371                     dup2 (comp[0], 0);
372                     dup2 (uncomp[1], 1);
373                     execlp (path, path, (char *)NULL);
374                     exit (1); /* if we get, execlp() failed! */
375                 default:
376                     if (vlc_clone (&p_sys->thread, Thread, stream,
377                                    VLC_THREAD_PRIORITY_INPUT) == 0)
378                         ret = VLC_SUCCESS;
379             }
380 #endif /* _POSIX_SPAWN < 0 */
381             close (uncomp[1]);
382             if (ret != VLC_SUCCESS)
383                 close (uncomp[0]);
384         }
385         close (comp[0]);
386         if (ret != VLC_SUCCESS)
387             close (comp[1]);
388     }
389
390     if (ret == VLC_SUCCESS)
391         return VLC_SUCCESS;
392
393     if (p_sys->pid != -1)
394         while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
395     vlc_mutex_destroy (&p_sys->lock);
396     vlc_cond_destroy (&p_sys->wait);
397     free (p_sys);
398     return ret;
399 }
400
401
402 /**
403  * Releases allocate resources.
404  */
405 static void Close (vlc_object_t *obj)
406 {
407     stream_t *stream = (stream_t *)obj;
408     stream_sys_t *p_sys = stream->p_sys;
409     int status;
410
411     vlc_cancel (p_sys->thread);
412     close (p_sys->read_fd);
413     vlc_join (p_sys->thread, NULL);
414     if (p_sys->write_fd != -1)
415         /* Killed before EOF? */
416         close (p_sys->write_fd);
417
418     msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
419     while (waitpid (p_sys->pid, &status, 0) == -1);
420     msg_Dbg (obj, "exit status %d", status);
421
422     if (p_sys->peeked)
423         block_Release (p_sys->peeked);
424     vlc_mutex_destroy (&p_sys->lock);
425     vlc_cond_destroy (&p_sys->wait);
426     free (p_sys);
427 }
428
429
430 /**
431  * Detects gzip file format
432  */
433 static int OpenGzip (vlc_object_t *obj)
434 {
435     stream_t      *stream = (stream_t *)obj;
436     const uint8_t *peek;
437
438     if (stream_Peek (stream->p_source, &peek, 3) < 3)
439         return VLC_EGENERIC;
440
441     if (memcmp (peek, "\x1f\x8b\x08", 3))
442         return VLC_EGENERIC;
443
444     msg_Dbg (obj, "detected gzip compressed stream");
445     return Open (stream, "zcat");
446 }
447
448
449 /**
450  * Detects bzip2 file format
451  */
452 static int OpenBzip2 (vlc_object_t *obj)
453 {
454     stream_t      *stream = (stream_t *)obj;
455     const uint8_t *peek;
456
457     /* (Try to) parse the bzip2 header */
458     if (stream_Peek (stream->p_source, &peek, 10) < 10)
459         return VLC_EGENERIC;
460
461     if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
462      || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
463         return VLC_EGENERIC;
464
465     msg_Dbg (obj, "detected bzip2 compressed stream");
466     return Open (stream, "bzcat");
467 }
468
469 /**
470  * Detects xz file format
471  */
472 static int OpenXZ (vlc_object_t *obj)
473 {
474     stream_t      *stream = (stream_t *)obj;
475     const uint8_t *peek;
476
477     /* (Try to) parse the xz stream header */
478     if (stream_Peek (stream->p_source, &peek, 8) < 8)
479         return VLC_EGENERIC;
480
481     if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
482         return VLC_EGENERIC;
483
484     msg_Dbg (obj, "detected xz compressed stream");
485     return Open (stream, "xzcat");
486 }