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