]> git.sesse.net Git - vlc/blobdiff - modules/stream_filter/decomp.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / stream_filter / decomp.c
index da86731ecea2bd1091e0d0fe5bc8bb13e002def3..ec81167af85f61842d26267c2f46e52349297bbe 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * decomp.c : Decompression module for vlc
  *****************************************************************************
- * Copyright © 2008 Rémi Denis-Courmont
+ * Copyright © 2008-2009 Rémi Denis-Courmont
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
 #include <vlc_plugin.h>
 #include <vlc_stream.h>
 #include <vlc_network.h>
+#include <assert.h>
 #include <unistd.h>
+#include <errno.h>
 #ifndef _POSIX_SPAWN
 # define _POSIX_SPAWN (-1)
 #endif
 #include <fcntl.h>
-#include <spawn.h>
+#if (_POSIX_SPAWN >= 0)
+# include <spawn.h>
+#endif
 #include <sys/wait.h>
 #include <sys/ioctl.h>
 #if defined (__linux__) && defined (HAVE_VMSPLICE)
@@ -43,6 +47,7 @@
 
 static int  OpenGzip (vlc_object_t *);
 static int  OpenBzip2 (vlc_object_t *);
+static int  OpenXZ (vlc_object_t *);
 static void Close (vlc_object_t *);
 
 vlc_module_begin ()
@@ -50,6 +55,9 @@ vlc_module_begin ()
     set_category (CAT_INPUT)
     set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
     set_capability ("stream_filter", 20)
+    set_callbacks (OpenXZ, Close)
+
+    add_submodule ()
     set_callbacks (OpenBzip2, Close)
     /* TODO: access shortnames for stream_UrlNew() */
 
@@ -87,7 +95,7 @@ static void *Thread (void *data)
     stream_t *stream = data;
     stream_sys_t *p_sys = stream->p_sys;
 #ifdef HAVE_VMSPLICE
-    ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
+    const ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
 #endif
     int fd = p_sys->write_fd;
     bool error = false;
@@ -99,9 +107,14 @@ static void *Thread (void *data)
 #ifdef HAVE_VMSPLICE
         unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
                                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+        if (unlikely(buf == MAP_FAILED))
+            break;
         vlc_cleanup_push (cleanup_mmap, buf);
 #else
-        unsigned char buf[bufsize];
+        unsigned char *buf = malloc (bufsize);
+        if (unlikely(buf == NULL))
+            break;
+        vlc_cleanup_push (free, buf);
 #endif
 
         len = stream_Read (stream->p_source, buf, bufsize);
@@ -118,9 +131,9 @@ static void *Thread (void *data)
                 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
                 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
             }
-#else
-            j = write (fd, buf + i, len - i);
+            if (j == -1 && errno == ENOSYS) /* vmsplice() not supported */
 #endif
+            j = write (fd, buf + i, len - i);
             if (j <= 0)
             {
                 if (j == 0)
@@ -130,17 +143,20 @@ static void *Thread (void *data)
                 break;
             }
         }
-#ifdef HAVE_VMSPLICE
-        vlc_cleanup_run (); /* munmap (buf, bufsize) */
-#endif
+        vlc_cleanup_run (); /* free (buf) */
     }
     while (!error);
 
     msg_Dbg (stream, "compressed stream at EOF");
+    /* Let child process know about EOF */
+    p_sys->write_fd = -1;
+    close (fd);
     return NULL;
 }
 
 
+static int Peek (stream_t *, const uint8_t **, unsigned int);
+
 #define MIN_BLOCK (1 << 10)
 #define MAX_BLOCK (1 << 20)
 /**
@@ -151,26 +167,38 @@ static int Read (stream_t *stream, void *buf, unsigned int buflen)
 {
     stream_sys_t *p_sys = stream->p_sys;
     block_t *peeked;
-    size_t bonus = 0;
     ssize_t length;
 
+    if (buf == NULL) /* caller skips data, get big enough peek buffer */
+        buflen = Peek (stream, &(const uint8_t *){ NULL }, buflen);
+
     if ((peeked = p_sys->peeked) != NULL)
-    {
-        bonus = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
-        memcpy (buf, peeked->p_buffer, bonus);
-        peeked->p_buffer += bonus;
-        peeked->i_buffer -= bonus;
+    {   /* dequeue peeked data */
+        length = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
+        if (buf != NULL)
+        {
+            memcpy (buf, peeked->p_buffer, length);
+            buf = ((char *)buf) + length;
+        }
+        buflen -= length;
+        peeked->p_buffer += length;
+        peeked->i_buffer -= length;
         if (peeked->i_buffer == 0)
         {
             block_Release (peeked);
             p_sys->peeked = NULL;
         }
+        p_sys->offset += length;
+
+        if (buflen > 0)
+            length += Read (stream, ((char *)buf) + length, buflen - length);
+        return length;
     }
+    assert ((buf != NULL) || (buflen == 0));
 
     length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
     if (length < 0)
         return 0;
-    length += bonus;
     p_sys->offset += length;
     return length;
 }
@@ -221,13 +249,10 @@ static int Control (stream_t *stream, int query, va_list args)
             *(va_arg (args, bool *)) = false;
             break;
         case STREAM_GET_POSITION:
-            *(va_arg (args, int64_t *)) = p_sys->offset;
+            *(va_arg (args, uint64_t *)) = p_sys->offset;
             break;
         case STREAM_GET_SIZE:
-            *(va_arg (args, int64_t *)) = 0;
-            break;
-        case STREAM_GET_MTU:
-            *(va_arg (args, int *)) = 0;
+            *(va_arg (args, uint64_t *)) = 0;
             break;
         default:
             return VLC_EGENERIC;
@@ -342,7 +367,9 @@ static void Close (vlc_object_t *obj)
     vlc_cancel (p_sys->thread);
     close (p_sys->read_fd);
     vlc_join (p_sys->thread, NULL);
-    close (p_sys->write_fd);
+    if (p_sys->write_fd != -1)
+        /* Killed before EOF? */
+        close (p_sys->write_fd);
 
     msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
     while (waitpid (p_sys->pid, &status, 0) == -1);
@@ -393,3 +420,21 @@ static int OpenBzip2 (vlc_object_t *obj)
     return Open (stream, "bzcat");
 }
 
+/**
+ * Detects xz file format
+ */
+static int OpenXZ (vlc_object_t *obj)
+{
+    stream_t      *stream = (stream_t *)obj;
+    const uint8_t *peek;
+
+    /* (Try to) parse the xz stream header */
+    if (stream_Peek (stream->p_source, &peek, 8) < 8)
+        return VLC_EGENERIC;
+
+    if (memcmp (peek, "\xfd\x37\x7a\x58\x5a", 6))
+        return VLC_EGENERIC;
+
+    msg_Dbg (obj, "detected xz compressed stream");
+    return Open (stream, "xzcat");
+}