]> git.sesse.net Git - vlc/blobdiff - modules/access_filter/dump.c
Remove linux specific poll flag.
[vlc] / modules / access_filter / dump.c
index cd1664b40405474e9cee368c2f64dc429ad589b3..c4b0b5ce22710077e81d1de3f12d3a7300f708e7 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 
-#include <stdio.h>
-#include <stdlib.h>
 #include <assert.h>
 #include <time.h>
+#include <errno.h>
+
+#include <vlc_access.h>
 
-#include "vlc_access.h"
-#include "vlc_block.h"
-#include "charset.h"
-#include "vlc_keys.h"
+#include <vlc_charset.h>
+#include <vlc_keys.h>
 
 #define DEFAULT_MARGIN 32 // megabytes
 
 static int  Open (vlc_object_t *);
 static void Close (vlc_object_t *);
 
-vlc_module_begin ();
-    set_shortname (_("Dump"));
-    set_description (_("Dump"));
-    set_category (CAT_INPUT);
-    set_subcategory (SUBCAT_INPUT_ACCESS_FILTER);
-    set_capability ("access_filter", 0);
-    add_shortcut ("dump");
-    set_callbacks (Open, Close);
-
-    add_bool ("dump-force", VLC_FALSE, NULL, FORCE_TEXT,
-              FORCE_LONGTEXT, VLC_FALSE);
+vlc_module_begin ()
+    set_shortname (N_("Dump"))
+    set_description (N_("Dump"))
+    set_category (CAT_INPUT)
+    set_subcategory (SUBCAT_INPUT_ACCESS_FILTER)
+    set_capability ("access_filter", 0)
+    add_shortcut ("dump")
+    set_callbacks (Open, Close)
+
+    add_bool ("dump-force", false, NULL, FORCE_TEXT,
+              FORCE_LONGTEXT, false);
     add_integer ("dump-margin", DEFAULT_MARGIN, NULL, MARGIN_TEXT,
-                 MARGIN_LONGTEXT, VLC_FALSE);
-vlc_module_end();
+                 MARGIN_LONGTEXT, false);
+vlc_module_end ()
 
-static int Read (access_t *access, uint8_t *buffer, int len);
+static ssize_t Read (access_t *access, uint8_t *buffer, size_t len);
 static block_t *Block (access_t *access);
 static int Seek (access_t *access, int64_t offset);
 static int Control (access_t *access, int cmd, va_list ap);
@@ -74,6 +78,7 @@ struct access_sys_t
 {
     FILE *stream;
     int64_t tmp_max;
+    int64_t dumpsize;
 };
 
 /**
@@ -86,8 +91,8 @@ static int Open (vlc_object_t *obj)
 
     if (!var_CreateGetBool (access, "dump-force"))
     {
-        vlc_bool_t b;
-        if ((access2_Control (src, ACCESS_CAN_FASTSEEK, &b) == 0) && b)
+        bool b;
+        if ((access_Control (src, ACCESS_CAN_FASTSEEK, &b) == 0) && b)
         {
             msg_Dbg (obj, "dump filter useless");
             return VLC_EGENERIC;
@@ -104,20 +109,24 @@ static int Open (vlc_object_t *obj)
     access->pf_control = Control;
     access->info = src->info;
 
-    access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
-    if (p_sys == NULL)
+    access_sys_t *p_sys = access->p_sys = calloc( 1, sizeof (*p_sys) );
+    if( !p_sys )
         return VLC_ENOMEM;
-    memset (p_sys, 0, sizeof (*p_sys));
 
+# ifndef UNDER_CE
     if ((p_sys->stream = tmpfile ()) == NULL)
+# else
+    char buf[75];
+    if(GetTempFileName("\\Temp\\","vlc",0,buf) || ((p_sys->stream = fopen(buf,"wb+")) ==NULL))
+#endif
     {
-        msg_Err (access, "cannot create temporary file: %s", strerror (errno));
+        msg_Err (access, "cannot create temporary file: %m");
         free (p_sys);
         return VLC_EGENERIC;
     }
     p_sys->tmp_max = ((int64_t)var_CreateGetInteger (access, "dump-margin")) << 20;
 
-    var_AddCallback (access->p_libvlc, "key-pressed", KeyHandler, access);
+    var_AddCallback (access->p_libvlc, "key-action", KeyHandler, access);
 
     return VLC_SUCCESS;
 }
@@ -131,7 +140,7 @@ static void Close (vlc_object_t *obj)
     access_t *access = (access_t *)obj;
     access_sys_t *p_sys = access->p_sys;
 
-    var_DelCallback (access->p_libvlc, "key-pressed", KeyHandler, access);
+    var_DelCallback (access->p_libvlc, "key-action", KeyHandler, access);
 
     if (p_sys->stream != NULL)
         fclose (p_sys->stream);
@@ -144,21 +153,34 @@ static void Dump (access_t *access, const uint8_t *buffer, size_t len)
     access_sys_t *p_sys = access->p_sys;
     FILE *stream = p_sys->stream;
 
-    if ((stream == NULL) || (len == 0))
+    if ((stream == NULL) /* not dumping */
+     || (access->info.i_pos < p_sys->dumpsize) /* already known data */)
         return;
 
+    size_t needed = access->info.i_pos - p_sys->dumpsize;
+    if (len < needed)
+        return; /* gap between data and dump offset (seek too far ahead?) */
+
+    buffer += len - needed;
+    len = needed;
+
+    if (len == 0)
+        return; /* no useful data */
+
     if ((p_sys->tmp_max != -1) && (access->info.i_pos > p_sys->tmp_max))
     {
         msg_Dbg (access, "too much data - dump will not work");
         goto error;
     }
 
+    assert (len > 0);
     if (fwrite (buffer, len, 1, stream) != 1)
     {
-        msg_Err (access, "cannot write to file: %s", strerror (errno));
+        msg_Err (access, "cannot write to file: %m");
         goto error;
     }
 
+    p_sys->dumpsize += len;
     return;
 
 error:
@@ -167,7 +189,7 @@ error:
 }
 
 
-static int Read (access_t *access, uint8_t *buffer, int len)
+static ssize_t Read (access_t *access, uint8_t *buffer, size_t len)
 {
     access_t *src = access->p_source;
 
@@ -176,7 +198,6 @@ static int Read (access_t *access, uint8_t *buffer, int len)
     access->info = src->info;
 
     Dump (access, buffer, len);
-    //Trigger (access);
 
     return len;
 }
@@ -220,11 +241,7 @@ static int Seek (access_t *access, int64_t offset)
     }
 
     if (p_sys->stream != NULL)
-    {
-        msg_Dbg (access, "seeking - dump will not work");
-        fclose (p_sys->stream);
-        p_sys->stream = NULL;
-    }
+        msg_Dbg (access, "seeking - dump might not work");
 
     src->info.i_update = access->info.i_update;
     int ret = src->pf_seek (src, offset);
@@ -232,26 +249,6 @@ static int Seek (access_t *access, int64_t offset)
     return ret;
 }
 
-
-#ifndef HAVE_LOCALTIME_R
-static inline struct tm *localtime_r (const time_t *now, struct tm *res)
-{
-    struct tm *unsafe = localtime (now);
-    /*
-     * This is not thread-safe. Blame your C library.
-     * On Win32 there SHOULD be _localtime_s instead, but of course
-     * Cygwin and Mingw32 don't know about it. You're on your own if you
-     * this platform.
-     */
-    if (unsafe == NULL)
-        return NULL;
-
-    memcpy (res, unsafe, sizeof (*res));
-    return res;
-}
-#endif
-
-
 static void Trigger (access_t *access)
 {
     access_sys_t *p_sys = access->p_sys;
@@ -274,20 +271,22 @@ static void Trigger (access_t *access)
         // and there is an off-by-one in the following sprintf().
         return;
 
-    const char *home = access->p_libvlc->psz_homedir;
+    char *dir = config_GetUserDir( VLC_DOWNLOAD_DIR );
+     if( dir == NULL )
+         return;
 
     /* Hmm what about the extension?? */
-    char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
-    sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home,
+    char filename[strlen (dir) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
+    sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", dir,
              t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
+    free( dir );
 
     msg_Info (access, "dumping media to \"%s\"...", filename);
 
-    FILE *newstream = fopen (filename, "wb");
+    FILE *newstream = utf8_fopen (filename, "wb");
     if (newstream == NULL)
     {
-        msg_Err (access, "cannot create dump file \"%s\": %s", filename,
-                 strerror (errno));
+        msg_Err (access, "cannot create dump file \"%s\": %m", filename);
         return;
     }
 
@@ -303,8 +302,7 @@ static void Trigger (access_t *access)
         {
             if (ferror (oldstream))
             {
-                msg_Err (access, "cannot read temporary file: %s",
-                         strerror (errno));
+                msg_Err (access, "cannot read temporary file: %m");
                 break;
             }
 
@@ -317,7 +315,7 @@ static void Trigger (access_t *access)
 
         if (fwrite (buf, len, 1, newstream) != 1)
         {
-            msg_Err (access, "cannot write dump file: %s", strerror (errno));
+            msg_Err (access, "cannot write dump file: %m");
             break;
         }
     }
@@ -334,19 +332,11 @@ static int KeyHandler (vlc_object_t *obj, char const *varname,
 {
     access_t *access = data;
 
+    (void)varname;
     (void)oldval;
     (void)obj;
 
-    for (struct hotkey *key = access->p_libvlc->p_hotkeys;
-         key->psz_action != NULL; key++)
-    {
-        if (key->i_key == newval.i_int)
-        {
-            if (key->i_action == ACTIONID_DUMP)
-                Trigger ((access_t *)data);
-            break;
-        }
-    }
-
+    if (newval.i_int == ACTIONID_DUMP)
+        Trigger (access);
     return VLC_SUCCESS;
 }