]> git.sesse.net Git - vlc/blob - modules/stream_filter/decomp.c
decomp: disable on DARWIN, because 10.4 does not support posix_spawn_file_actions_adddup2
[vlc] / modules / stream_filter / 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_stream.h>
28 #include <vlc_network.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <spawn.h>
32 #include <sys/wait.h>
33 #include <sys/ioctl.h>
34 #if defined (__linux__) && defined (HAVE_VMSPLICE)
35 # include <sys/uio.h>
36 # include <sys/mman.h>
37 #else
38 # undef HAVE_VMSPLICE
39 #endif
40
41 static int  OpenGzip (vlc_object_t *);
42 static int  OpenBzip2 (vlc_object_t *);
43 static void Close (vlc_object_t *);
44
45 vlc_module_begin ()
46     set_description (N_("Decompression"))
47     set_category (CAT_INPUT)
48     set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
49     set_capability ("stream_filter", 20)
50     set_callbacks (OpenBzip2, Close)
51     /* TODO: access shortnames for stream_UrlNew() */
52
53     add_submodule ()
54     set_callbacks (OpenGzip, Close)
55 vlc_module_end ()
56
57 struct stream_sys_t
58 {
59     block_t      *peeked;
60     uint64_t     offset;
61     vlc_thread_t thread;
62     pid_t        pid;
63     int          write_fd, read_fd;
64 };
65
66 static void cloexec (int fd)
67 {
68     int flags = fcntl (fd, F_GETFD);
69     fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
70 }
71
72 extern char **environ;
73
74 static const size_t bufsize = 65536;
75 #ifdef HAVE_VMSPLICE
76 static void cleanup_mmap (void *addr)
77 {
78     munmap (addr, bufsize);
79 }
80 #endif
81
82 static void *Thread (void *data)
83 {
84     stream_t *stream = data;
85     stream_sys_t *p_sys = stream->p_sys;
86 #ifdef HAVE_VMSPLICE
87     ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
88 #endif
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 HAVE_VMSPLICE
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 (stream->p_source, buf, bufsize);
105         vlc_restorecancel (canc);
106         error = len <= 0;
107
108         for (ssize_t i = 0, j; i < len; i += j)
109         {
110 #ifdef HAVE_VMSPLICE
111             if ((len - i) <= page_mask) /* incomplete last page */
112                 j = write (fd, buf + i, len - i);
113             else
114             {
115                 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
116                 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
117             }
118 #else
119             j = write (fd, buf + i, len - i);
120 #endif
121             if (j <= 0)
122             {
123                 if (j == 0)
124                     errno = EPIPE;
125                 msg_Err (stream, "cannot write data (%m)");
126                 error = true;
127                 break;
128             }
129         }
130 #ifdef HAVE_VMSPLICE
131         vlc_cleanup_run (); /* munmap (buf, bufsize) */
132 #endif
133     }
134     while (!error);
135
136     msg_Dbg (stream, "compressed stream at EOF");
137     return NULL;
138 }
139
140
141 #define MIN_BLOCK (1 << 10)
142 #define MAX_BLOCK (1 << 20)
143 /**
144  * Reads decompressed from the decompression program
145  * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
146  */
147 static int Read (stream_t *stream, void *buf, unsigned int buflen)
148 {
149     stream_sys_t *p_sys = stream->p_sys;
150     block_t *peeked;
151     size_t bonus = 0;
152     ssize_t length;
153
154     if ((peeked = p_sys->peeked) != NULL)
155     {
156         bonus = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
157         memcpy (buf, peeked->p_buffer, bonus);
158         peeked->p_buffer += bonus;
159         peeked->i_buffer -= bonus;
160         if (peeked->i_buffer == 0)
161         {
162             block_Release (peeked);
163             p_sys->peeked = NULL;
164         }
165     }
166
167     length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
168     if (length < 0)
169         return 0;
170     length += bonus;
171     p_sys->offset += length;
172     return length;
173 }
174
175 /**
176  *
177  */
178 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
179 {
180     stream_sys_t *p_sys = stream->p_sys;
181     block_t *peeked = p_sys->peeked;
182     size_t curlen = 0;
183     int fd = p_sys->read_fd;
184
185     if (peeked == NULL)
186         peeked = block_Alloc (len);
187     else if ((curlen = peeked->i_buffer) < len)
188         peeked = block_Realloc (peeked, 0, len);
189
190     if ((p_sys->peeked = peeked) == NULL)
191         return 0;
192
193     if (curlen < len)
194     {
195         ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
196                                 len - curlen, true);
197         if (val >= 0)
198         {
199             curlen += val;
200             peeked->i_buffer = curlen;
201         }
202     }
203     *pbuf = peeked->p_buffer;
204     return curlen;
205 }
206
207 /**
208  *
209  */
210 static int Control (stream_t *stream, int query, va_list args)
211 {
212     stream_sys_t *p_sys = stream->p_sys;
213
214     switch (query)
215     {
216         case STREAM_CAN_SEEK:
217         case STREAM_CAN_FASTSEEK:
218             *(va_arg (args, bool *)) = false;
219             break;
220         case STREAM_GET_POSITION:
221             *(va_arg (args, int64_t *)) = p_sys->offset;
222             break;
223         case STREAM_GET_SIZE:
224             *(va_arg (args, int64_t *)) = 0;
225             break;
226         case STREAM_GET_MTU:
227             *(va_arg (args, int *)) = 0;
228             break;
229         default:
230             return VLC_EGENERIC;
231     }
232     return VLC_SUCCESS;
233 }
234
235 /**
236  * Pipe data through an external executable.
237  * @param stream the stream filter object.
238  * @param path path to the executable.
239  */
240 static int Open (stream_t *stream, const char *path)
241 {
242     stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
243     if (p_sys == NULL)
244         return VLC_ENOMEM;
245
246     stream->pf_read = Read;
247     stream->pf_peek = Peek;
248     stream->pf_control = Control;
249     p_sys->peeked = NULL;
250     p_sys->offset = 0;
251     p_sys->pid = -1;
252
253     /* I am not a big fan of the pyramid style, but I cannot think of anything
254      * better here. There are too many failure cases. */
255     int ret = VLC_EGENERIC;
256     int comp[2];
257
258     if (pipe (comp) == 0)
259     {
260         cloexec (comp[1]);
261         p_sys->write_fd = comp[1];
262
263         int uncomp[2];
264         if (pipe (uncomp) == 0)
265         {
266             cloexec (uncomp[0]);
267             p_sys->read_fd = uncomp[0];
268
269             posix_spawn_file_actions_t actions;
270             if (posix_spawn_file_actions_init (&actions) == 0)
271             {
272                 char *const argv[] = { (char *)path, NULL };
273
274                 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
275                  && !posix_spawn_file_actions_addclose (&actions, comp[0])
276                  && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
277                  && !posix_spawn_file_actions_addclose (&actions, uncomp[1])
278                  && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
279                                    environ))
280                 {
281                     if (vlc_clone (&p_sys->thread, Thread, stream,
282                                    VLC_THREAD_PRIORITY_INPUT) == 0)
283                         ret = VLC_SUCCESS;
284                 }
285                 else
286                 {
287                     msg_Err (stream, "Cannot execute %s", path);
288                     p_sys->pid = -1;
289                 }
290                 posix_spawn_file_actions_destroy (&actions);
291             }
292             close (uncomp[1]);
293             if (ret != VLC_SUCCESS)
294                 close (uncomp[0]);
295         }
296         close (comp[0]);
297         if (ret != VLC_SUCCESS)
298         {
299             close (comp[1]);
300             if (p_sys->pid != -1)
301                 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
302         }
303     }
304     return ret;
305 }
306
307
308 /**
309  * Releases allocate resources.
310  */
311 static void Close (vlc_object_t *obj)
312 {
313     stream_t *stream = (stream_t *)obj;
314     stream_sys_t *p_sys = stream->p_sys;
315     int status;
316
317     vlc_cancel (p_sys->thread);
318     close (p_sys->read_fd);
319     vlc_join (p_sys->thread, NULL);
320     close (p_sys->write_fd);
321
322     msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
323     while (waitpid (p_sys->pid, &status, 0) == -1);
324     msg_Dbg (obj, "exit status %d", status);
325
326     if (p_sys->peeked)
327         block_Release (p_sys->peeked);
328     free (p_sys);
329 }
330
331
332 /**
333  * Detects gzip file format
334  */
335 static int OpenGzip (vlc_object_t *obj)
336 {
337     stream_t      *stream = (stream_t *)obj;
338     const uint8_t *peek;
339
340     if (stream_Peek (stream->p_source, &peek, 3) < 3)
341         return VLC_EGENERIC;
342
343     if (memcmp (peek, "\x1f\x8b\x08", 3))
344         return VLC_EGENERIC;
345
346     msg_Dbg (obj, "detected gzip compressed stream");
347     return Open (stream, "zcat");
348 }
349
350
351 /**
352  * Detects bzip2 file format
353  */
354 static int OpenBzip2 (vlc_object_t *obj)
355 {
356     stream_t      *stream = (stream_t *)obj;
357     const uint8_t *peek;
358
359     /* (Try to) parse the bzip2 header */
360     if (stream_Peek (stream->p_source, &peek, 10) < 10)
361         return VLC_EGENERIC;
362
363     if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
364      || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
365         return VLC_EGENERIC;
366
367     msg_Dbg (obj, "detected bzip2 compressed stream");
368     return Open (stream, "bzcat");
369 }
370