Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

record.c

00001 /*****************************************************************************
00002  * record.c
00003  *****************************************************************************
00004  * Copyright (C) 2005 the VideoLAN team
00005  * $Id: demux.c 7546 2004-04-29 13:53:29Z gbazin $
00006  *
00007  * Author: Laurent Aimar <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>
00028 
00029 #include <vlc/vlc.h>
00030 #include <vlc/input.h>
00031 #include <vlc/vout.h>
00032 
00033 #include "vlc_keys.h"
00034 #include <vlc_osd.h>
00035 #include <errno.h>
00036 #include <time.h>
00037 
00038 /*****************************************************************************
00039  * Module descriptor
00040  *****************************************************************************/
00041 
00042 #define RECORD_PATH_TXT N_("Record directory")
00043 #define RECORD_PATH_LONGTXT N_( \
00044     "Allows you to specify the directory where the record will be stored" )
00045 
00046 static int  Open ( vlc_object_t * );
00047 static void Close( vlc_object_t * );
00048 
00049 vlc_module_begin();
00050     set_shortname( _("Record") );
00051     set_description( _("Record") );
00052     set_category( CAT_INPUT );
00053     set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
00054     set_capability( "access_filter", 0 );
00055     add_shortcut( "record" );
00056 
00057     add_directory( "record-path", NULL, NULL,
00058                    RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
00059 
00060     set_callbacks( Open, Close );
00061 
00062 vlc_module_end();
00063 
00064 /*****************************************************************************
00065  * Local prototypes
00066  *****************************************************************************/
00067 
00068 static block_t *Block  ( access_t * );
00069 static int      Read   ( access_t *, uint8_t *, int );
00070 static int      Control( access_t *, int i_query, va_list args );
00071 static int      Seek   ( access_t *, int64_t );
00072 
00073 static void Dump( access_t *, uint8_t *, int );
00074 
00075 static int EventKey( vlc_object_t *, char const *,
00076                      vlc_value_t, vlc_value_t, void * );
00077 
00078 struct access_sys_t
00079 {
00080     vlc_bool_t b_dump;
00081 
00082     char *psz_path;
00083     char *psz_ext;
00084     char *psz_file;
00085     int64_t i_size;
00086     FILE *f;
00087 
00088     vout_thread_t *p_vout;
00089     int            i_vout_chan;
00090 
00091     int i_update_sav;
00092 };
00093 
00094 static inline void PreUpdateFlags( access_t *p_access )
00095 {
00096     access_t *p_src = p_access->p_source;
00097     /* backport flags turned off 0 */
00098     p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
00099 }
00100 
00101 static inline void PostUpdateFlags( access_t *p_access )
00102 {
00103     access_t *p_src = p_access->p_source;
00104     /* */
00105     p_access->info = p_src->info;
00106     p_access->p_sys->i_update_sav = p_access->info.i_update;
00107 }
00108 
00109 
00110 /*****************************************************************************
00111  * Open:
00112  *****************************************************************************/
00113 static int Open( vlc_object_t *p_this )
00114 {
00115     access_t *p_access = (access_t*)p_this;
00116     access_t *p_src = p_access->p_source;
00117     access_sys_t *p_sys;
00118     char *psz;
00119 
00120     /* */
00121     p_access->pf_read  = p_src->pf_read  ? Read : NULL;
00122     p_access->pf_block = p_src->pf_block ? Block : NULL;
00123     p_access->pf_seek  = p_src->pf_seek  ? Seek : NULL;
00124     p_access->pf_control = Control;
00125 
00126     /* */
00127     p_access->info = p_src->info;
00128 
00129     /* */
00130     p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
00131 
00132     /* */
00133     p_sys->f = NULL;
00134     p_sys->i_size = 0;
00135     p_sys->psz_file = NULL;
00136     p_sys->psz_ext = "dat";
00137     p_sys->b_dump = VLC_FALSE;
00138     p_sys->p_vout = NULL;
00139     p_sys->i_vout_chan = -1;
00140     p_sys->i_update_sav = p_access->info.i_update;
00141 
00142     if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
00143         !strncasecmp( p_src->psz_access, "udp", 3 ) )
00144         p_sys->psz_ext = "ts";
00145 
00146     psz = var_CreateGetString( p_access, "record-path" );
00147     if( *psz == '\0' )
00148     {
00149         free( psz );
00150         if( p_access->p_vlc->psz_homedir )
00151             psz = strdup( p_access->p_vlc->psz_homedir );
00152     }
00153     p_sys->psz_path = psz;
00154     msg_Dbg( p_access, "Record access filter path %s", psz );
00155 
00156     /* catch all key event */
00157     var_AddCallback( p_access->p_vlc, "key-pressed", EventKey, p_access );
00158 
00159     return VLC_SUCCESS;
00160 }
00161 
00162 /*****************************************************************************
00163  * Close:
00164  *****************************************************************************/
00165 static void Close( vlc_object_t *p_this )
00166 {
00167     access_t     *p_access = (access_t*)p_this;
00168     access_sys_t *p_sys = p_access->p_sys;
00169 
00170     var_DelCallback( p_access->p_vlc, "key-pressed", EventKey, p_access );
00171 
00172     if( p_sys->f )
00173     {
00174         fclose( p_sys->f );
00175         free( p_sys->psz_file );
00176     }
00177 
00178     free( p_sys->psz_path );
00179     free( p_sys );
00180 }
00181 
00182 /*****************************************************************************
00183  *
00184  *****************************************************************************/
00185 static block_t *Block( access_t *p_access )
00186 {
00187     access_t     *p_src = p_access->p_source;
00188     block_t      *p_block;
00189 
00190     /* */
00191     PreUpdateFlags( p_access );
00192 
00193     /* */
00194     p_block = p_src->pf_block( p_src );
00195     if( p_block && p_block->i_buffer )
00196         Dump( p_access, p_block->p_buffer, p_block->i_buffer );
00197 
00198     /* */
00199     PostUpdateFlags( p_access );
00200 
00201     return p_block;
00202 }
00203 
00204 /*****************************************************************************
00205  *
00206  *****************************************************************************/
00207 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
00208 {
00209     access_t     *p_src = p_access->p_source;
00210     int i_ret;
00211 
00212     /* */
00213     PreUpdateFlags( p_access );
00214 
00215     /* */
00216     i_ret = p_src->pf_read( p_src, p_buffer, i_len );
00217 
00218     if( i_ret > 0 )
00219         Dump( p_access, p_buffer, i_ret );
00220 
00221     /* */
00222     PostUpdateFlags( p_access );
00223 
00224     return i_ret;
00225 }
00226 
00227 /*****************************************************************************
00228  *
00229  *****************************************************************************/
00230 static int Control( access_t *p_access, int i_query, va_list args )
00231 {
00232     access_t     *p_src = p_access->p_source;
00233     int i_ret;
00234 
00235     /* */
00236     PreUpdateFlags( p_access );
00237 
00238     /* */
00239     i_ret = p_src->pf_control( p_src, i_query, args );
00240 
00241     /* */
00242     PostUpdateFlags( p_access );
00243 
00244     return i_ret;
00245 }
00246 
00247 /*****************************************************************************
00248  *
00249  *****************************************************************************/
00250 static int Seek( access_t *p_access, int64_t i_pos )
00251 {
00252     access_t     *p_src = p_access->p_source;
00253     int i_ret;
00254 
00255     /* */
00256     PreUpdateFlags( p_access );
00257 
00258     /* */
00259     i_ret = p_src->pf_seek( p_src, i_pos );
00260 
00261     /* */
00262     PostUpdateFlags( p_access );
00263 
00264     return i_ret;
00265 }
00266 
00267 /*****************************************************************************
00268  *
00269  *****************************************************************************/
00270 static int EventKey( vlc_object_t *p_this, char const *psz_var,
00271                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
00272 {
00273     access_t     *p_access = p_data;
00274     access_sys_t *p_sys = p_access->p_sys;
00275 
00276     struct hotkey *p_hotkeys = p_access->p_vlc->p_hotkeys;
00277     int i_action = -1, i;
00278 
00279     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
00280     {
00281         if( p_hotkeys[i].i_key == newval.i_int )
00282         {
00283             i_action = p_hotkeys[i].i_action;
00284         }
00285     }
00286 
00287     if( i_action == ACTIONID_RECORD )
00288     {
00289         if( p_sys->b_dump )
00290             p_sys->b_dump = VLC_FALSE;
00291         else
00292             p_sys->b_dump = VLC_TRUE;
00293     }
00294 
00295     return VLC_SUCCESS;
00296 }
00297 
00298 /*****************************************************************************
00299  *
00300  *****************************************************************************/
00301 static void Notify( access_t *p_access, vlc_bool_t b_dump )
00302 {
00303     access_sys_t *p_sys = p_access->p_sys;
00304     vout_thread_t *p_vout;
00305 
00306     p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
00307 
00308     if( p_vout != p_sys->p_vout )
00309     {
00310         p_sys->p_vout = p_vout;
00311         if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
00312                          &p_sys->i_vout_chan  ) )
00313             p_sys->i_vout_chan = -1;
00314     }
00315 
00316     if( p_sys->i_vout_chan != -1 )
00317     {
00318         if( b_dump )
00319             vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording" );
00320         else
00321             vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording done" );
00322     }
00323     vlc_object_release( p_vout );
00324 }
00325 
00326 /*****************************************************************************
00327  *
00328  *****************************************************************************/
00329 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
00330 {
00331     access_sys_t *p_sys = p_access->p_sys;
00332     int i_write;
00333 
00334     /* */
00335     if( !p_sys->b_dump )
00336     {
00337         if( p_sys->f )
00338         {
00339             msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
00340                      p_sys->i_size/1024, p_sys->psz_file );
00341 
00342             Notify( p_access, VLC_FALSE );
00343 
00344             fclose( p_sys->f );
00345             p_sys->f = NULL;
00346 
00347             free( p_sys->psz_file );
00348             p_sys->psz_file = NULL;
00349 
00350             p_sys->i_size = 0;
00351         }
00352         return;
00353     }
00354 
00355     /* */
00356     if( !p_sys->f )
00357     {
00358         input_thread_t *p_input;
00359         char *psz_name = NULL;
00360         time_t t = time(NULL);
00361         struct tm l;
00362 
00363 #ifdef HAVE_LOCALTIME_R
00364         if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
00365 #else
00366         /* Grrr */
00367         {
00368             struct tm *p_l = localtime( &t );
00369             if( p_l ) l = *p_l;
00370             else memset( &l, 0, sizeof(l) );
00371         }
00372 #endif
00373 
00374         p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
00375         if( p_input )
00376         {
00377             vlc_mutex_lock( &p_input->input.p_item->lock );
00378             if( p_input->input.p_item->psz_name )
00379             {
00380                 char *p = strrchr( p_input->input.p_item->psz_name, '/' );
00381                 if( p == NULL )
00382                     p = strrchr( p_input->input.p_item->psz_name, '\\' );
00383 
00384                 if( p == NULL )
00385                     psz_name = strdup( p_input->input.p_item->psz_name );
00386                 else if( p[1] != '\0' )
00387                     psz_name = strdup( &p[1] );
00388             }
00389             vlc_mutex_unlock( &p_input->input.p_item->lock );
00390 
00391             vlc_object_release( p_input );
00392         }
00393 
00394         if( psz_name == NULL )
00395             psz_name = strdup( "Unknown" );
00396 
00397         asprintf( &p_sys->psz_file, "%s/%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
00398                   p_sys->psz_path, psz_name,
00399                   l.tm_mday, l.tm_mon+1, l.tm_year+1900,
00400                   l.tm_hour, l.tm_min, l.tm_sec,
00401                   p_sys->psz_ext );
00402 
00403         free( psz_name );
00404 
00405         msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
00406 
00407         p_sys->f = fopen( p_sys->psz_file, "wb" );
00408         if( p_sys->f == NULL )
00409         {
00410             msg_Err( p_access, "cannot open file '%s' (%s)",
00411                      p_sys->psz_file, strerror(errno) );
00412             free( p_sys->psz_file );
00413             p_sys->psz_file = NULL;
00414             p_sys->b_dump = VLC_FALSE;
00415             return;
00416         }
00417 
00418         Notify( p_access, VLC_TRUE );
00419 
00420         p_sys->i_size = 0;
00421     }
00422 
00423     /* */
00424     if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
00425         p_sys->i_size += i_write;
00426 }
00427 

Generated on Tue Dec 20 10:14:27 2005 for vlc-0.8.4a by  doxygen 1.4.2