]> git.sesse.net Git - vlc/blobdiff - modules/access_filter/record.c
Include vlc_plugin.h as needed
[vlc] / modules / access_filter / record.c
index 81d6021b94cad788d1ab860bff4c0111f0a27522..38d6cd26d08e697337d982f9e68cd773eb1bb285 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * record.c
  *****************************************************************************
- * Copyright (C) 2005 VideoLAN (Centrale Réseaux) and its contributors
- * $Id: demux.c 7546 2004-04-29 13:53:29Z gbazin $
+ * Copyright (C) 2005-2006 the VideoLAN team
+ * $Id$
  *
  * Author: Laurent Aimar <fenrir@via.ecp.fr>
  *
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
-#include <vlc/vout.h>
+#include <vlc_plugin.h>
+
+#include <vlc_input.h>
+#include <vlc_access.h>
 
 #include "vlc_keys.h"
-#include <osd.h>
+#include <vlc_osd.h>
+#include <vlc_charset.h>
 #include <errno.h>
 #include <time.h>
 
@@ -41,7 +47,7 @@
 
 #define RECORD_PATH_TXT N_("Record directory")
 #define RECORD_PATH_LONGTXT N_( \
-    "Allows you to specify the directory where the record will be stored" )
+    "Directory where the record will be stored." )
 
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
@@ -55,7 +61,8 @@ vlc_module_begin();
     add_shortcut( "record" );
 
     add_directory( "record-path", NULL, NULL,
-                   RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
+                   RECORD_PATH_TXT, RECORD_PATH_LONGTXT, true );
+        change_unsafe();
 
     set_callbacks( Open, Close );
 
@@ -66,7 +73,7 @@ vlc_module_end();
  *****************************************************************************/
 
 static block_t *Block  ( access_t * );
-static int      Read   ( access_t *, uint8_t *, int );
+static ssize_t  Read   ( access_t *, uint8_t *, size_t );
 static int      Control( access_t *, int i_query, va_list args );
 static int      Seek   ( access_t *, int64_t );
 
@@ -77,18 +84,36 @@ static int EventKey( vlc_object_t *, char const *,
 
 struct access_sys_t
 {
-    vlc_bool_t b_dump;
+    bool b_dump;
 
     char *psz_path;
-    char *psz_ext;
+    const char *psz_ext;
     char *psz_file;
     int64_t i_size;
     FILE *f;
 
     vout_thread_t *p_vout;
     int            i_vout_chan;
+
+    int i_update_sav;
 };
 
+static inline void PreUpdateFlags( access_t *p_access )
+{
+    access_t *p_src = p_access->p_source;
+    /* backport flags turned off 0 */
+    p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
+}
+
+static inline void PostUpdateFlags( access_t *p_access )
+{
+    access_t *p_src = p_access->p_source;
+    /* */
+    p_access->info = p_src->info;
+    p_access->p_sys->i_update_sav = p_access->info.i_update;
+}
+
+
 /*****************************************************************************
  * Open:
  *****************************************************************************/
@@ -116,9 +141,10 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_size = 0;
     p_sys->psz_file = NULL;
     p_sys->psz_ext = "dat";
-    p_sys->b_dump = VLC_FALSE;
+    p_sys->b_dump = false;
     p_sys->p_vout = NULL;
     p_sys->i_vout_chan = -1;
+    p_sys->i_update_sav = p_access->info.i_update;
 
     if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
         !strncasecmp( p_src->psz_access, "udp", 3 ) )
@@ -128,14 +154,14 @@ static int Open( vlc_object_t *p_this )
     if( *psz == '\0' )
     {
         free( psz );
-        if( p_access->p_vlc->psz_homedir )
-            psz = strdup( p_access->p_vlc->psz_homedir );
+        if( p_access->p_libvlc->psz_homedir ) /* XXX: This should never happen */
+            psz = strdup( p_access->p_libvlc->psz_homedir );
     }
     p_sys->psz_path = psz;
     msg_Dbg( p_access, "Record access filter path %s", psz );
 
     /* catch all key event */
-    var_AddCallback( p_access->p_vlc, "key-pressed", EventKey, p_access );
+    var_AddCallback( p_access->p_libvlc, "key-action", EventKey, p_access );
 
     return VLC_SUCCESS;
 }
@@ -148,7 +174,7 @@ static void Close( vlc_object_t *p_this )
     access_t     *p_access = (access_t*)p_this;
     access_sys_t *p_sys = p_access->p_sys;
 
-    var_DelCallback( p_access->p_vlc, "key-pressed", EventKey, p_access );
+    var_DelCallback( p_access->p_libvlc, "key-action", EventKey, p_access );
 
     if( p_sys->f )
     {
@@ -168,13 +194,16 @@ static block_t *Block( access_t *p_access )
     access_t     *p_src = p_access->p_source;
     block_t      *p_block;
 
+    /* */
+    PreUpdateFlags( p_access );
+
     /* */
     p_block = p_src->pf_block( p_src );
     if( p_block && p_block->i_buffer )
         Dump( p_access, p_block->p_buffer, p_block->i_buffer );
 
     /* */
-    p_access->info = p_src->info;
+    PostUpdateFlags( p_access );
 
     return p_block;
 }
@@ -182,18 +211,22 @@ static block_t *Block( access_t *p_access )
 /*****************************************************************************
  *
  *****************************************************************************/
-static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
+static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
 {
     access_t     *p_src = p_access->p_source;
     int i_ret;
 
+    /* */
+    PreUpdateFlags( p_access );
+
+    /* */
     i_ret = p_src->pf_read( p_src, p_buffer, i_len );
 
     if( i_ret > 0 )
         Dump( p_access, p_buffer, i_ret );
 
     /* */
-    p_access->info = p_src->info;
+    PostUpdateFlags( p_access );
 
     return i_ret;
 }
@@ -206,10 +239,14 @@ static int Control( access_t *p_access, int i_query, va_list args )
     access_t     *p_src = p_access->p_source;
     int i_ret;
 
+    /* */
+    PreUpdateFlags( p_access );
+
+    /* */
     i_ret = p_src->pf_control( p_src, i_query, args );
 
     /* */
-    p_access->info = p_src->info;
+    PostUpdateFlags( p_access );
 
     return i_ret;
 }
@@ -222,10 +259,14 @@ static int Seek( access_t *p_access, int64_t i_pos )
     access_t     *p_src = p_access->p_source;
     int i_ret;
 
+    /* */
+    PreUpdateFlags( p_access );
+
+    /* */
     i_ret = p_src->pf_seek( p_src, i_pos );
 
     /* */
-    p_access->info = p_src->info;
+    PostUpdateFlags( p_access );
 
     return i_ret;
 }
@@ -239,23 +280,15 @@ static int EventKey( vlc_object_t *p_this, char const *psz_var,
     access_t     *p_access = p_data;
     access_sys_t *p_sys = p_access->p_sys;
 
-    struct hotkey *p_hotkeys = p_access->p_vlc->p_hotkeys;
-    int i_action = -1, i;
-
-    for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
-    {
-        if( p_hotkeys[i].i_key == newval.i_int )
-        {
-            i_action = p_hotkeys[i].i_action;
-        }
-    }
+    (void)psz_var;
+    (void)oldval;
 
-    if( i_action == ACTIONID_RECORD )
+    if( newval.i_int == ACTIONID_RECORD )
     {
         if( p_sys->b_dump )
-            p_sys->b_dump = VLC_FALSE;
+            p_sys->b_dump = false;
         else
-            p_sys->b_dump = VLC_TRUE;
+            p_sys->b_dump = true;
     }
 
     return VLC_SUCCESS;
@@ -264,12 +297,13 @@ static int EventKey( vlc_object_t *p_this, char const *psz_var,
 /*****************************************************************************
  *
  *****************************************************************************/
-static void Notify( access_t *p_access, vlc_bool_t b_dump )
+static void Notify( access_t *p_access, bool b_dump )
 {
     access_sys_t *p_sys = p_access->p_sys;
     vout_thread_t *p_vout;
 
     p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+    if( !p_vout ) return;
 
     if( p_vout != p_sys->p_vout )
     {
@@ -282,9 +316,9 @@ static void Notify( access_t *p_access, vlc_bool_t b_dump )
     if( p_sys->i_vout_chan != -1 )
     {
         if( b_dump )
-            vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording" );
+            vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
         else
-            vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording done" );
+            vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
     }
     vlc_object_release( p_vout );
 }
@@ -302,10 +336,10 @@ static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
     {
         if( p_sys->f )
         {
-            msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
+            msg_Dbg( p_access, "dumped %"PRId64" kb (%s)",
                      p_sys->i_size/1024, p_sys->psz_file );
 
-            Notify( p_access, VLC_FALSE );
+            Notify( p_access, false );
 
             fclose( p_sys->f );
             p_sys->f = NULL;
@@ -322,66 +356,95 @@ static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
     if( !p_sys->f )
     {
         input_thread_t *p_input;
-        char *psz_name = NULL;
+        char *psz_name = NULL, *psz;
         time_t t = time(NULL);
         struct tm l;
 
-#ifdef HAVE_LOCALTIME_R
         if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
-#else
-        /* Grrr */
-        {
-            struct tm *p_l = localtime( &t );
-            if( p_l ) l = *p_l;
-            else memset( &l, 0, sizeof(l) );
-        }
-#endif
 
         p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
         if( p_input )
         {
-            vlc_mutex_lock( &p_input->input.p_item->lock );
-            if( p_input->input.p_item->psz_name )
+            input_item_t * p_item = input_GetItem( p_input );
+            vlc_mutex_lock( &p_item->lock );
+            if( p_item->psz_name )
             {
-                char *p = strrchr( p_input->input.p_item->psz_name, '/' );
+                char *p = strrchr( p_item->psz_name, '/' );
                 if( p == NULL )
-                    p = strrchr( p_input->input.p_item->psz_name, '\\' );
+                    p = strrchr( p_item->psz_name, '\\' );
 
                 if( p == NULL )
-                    psz_name = strdup( p_input->input.p_item->psz_name );
+                    psz_name = strdup( p_item->psz_name );
                 else if( p[1] != '\0' )
                     psz_name = strdup( &p[1] );
             }
-            vlc_mutex_unlock( &p_input->input.p_item->lock );
+            vlc_mutex_unlock( &p_item->lock );
 
             vlc_object_release( p_input );
         }
 
-        if( psz_name == NULL )
-            psz_name = strdup( "Unknown" );
+        if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
+                      ( psz_name != NULL ) ? psz_name : "Unknown",
+                      l.tm_mday, l.tm_mon+1, l.tm_year+1900,
+                      l.tm_hour, l.tm_min, l.tm_sec,
+                      p_sys->psz_ext ) == -1 )
+            p_sys->psz_file = NULL;
 
-        asprintf( &p_sys->psz_file, "%s/%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
-                  p_sys->psz_path, psz_name,
-                  l.tm_mday, l.tm_mon+1, l.tm_year+1900,
-                  l.tm_hour, l.tm_min, l.tm_sec,
-                  p_sys->psz_ext );
+        free( psz_name );
+        if( p_sys->psz_file == NULL )
+        {
+            p_sys->b_dump = false;
+            return;
+        }
+
+        /* Remove all forbidden characters (except (back)slashes) */
+        for( psz = p_sys->psz_file; *psz; psz++ )
+        {
+            unsigned char c = (unsigned char)*psz;
+
+            /* Even if many OS accept non printable characters, we remove
+             * them to avoid confusing users */
+            if( ( c < 32 ) || ( c == 127 ) )
+                *psz = '_';
+#if defined (WIN32) || defined (UNDER_CE)
+            /* Windows has a lot of forbidden characters, even if it has
+             * fewer than DOS. */
+            if( strchr( "\"*:<>?|", c ) != NULL )
+                *psz = '_';
+#endif
+        }
+
+        psz_name = p_sys->psz_file;
 
+#if defined (WIN32) || defined (UNDER_CE)
+#define DIR_SEP "\\"
+#else
+#define DIR_SEP "/"
+#endif
+        if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
+                      p_sys->psz_path, psz_name ) == -1 )
+            p_sys->psz_file = NULL;
         free( psz_name );
+        if( p_sys->psz_file == NULL )
+        {
+            p_sys->b_dump = false;
+            return;
+        }
 
         msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
 
-        p_sys->f = fopen( p_sys->psz_file, "wb" );
+        p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
         if( p_sys->f == NULL )
         {
-            msg_Err( p_access, "cannot open file '%s' (%s)",
-                     p_sys->psz_file, strerror(errno) );
+            msg_Err( p_access, "cannot open file '%s' (%m)",
+                     p_sys->psz_file );
             free( p_sys->psz_file );
             p_sys->psz_file = NULL;
-            p_sys->b_dump = VLC_FALSE;
+            p_sys->b_dump = false;
             return;
         }
 
-        Notify( p_access, VLC_TRUE );
+        Notify( p_access, true );
 
         p_sys->i_size = 0;
     }