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

file.c

00001 /*****************************************************************************
00002  * file.c
00003  *****************************************************************************
00004  * Copyright (C) 2001, 2002 the VideoLAN team
00005  * $Id: file.c 13454 2005-11-29 14:51:01Z dionoea $
00006  *
00007  * Authors: Laurent Aimar <[email protected]>
00008  *          Eric Petit <[email protected]>
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00023  *****************************************************************************/
00024 
00025 /*****************************************************************************
00026  * Preamble
00027  *****************************************************************************/
00028 #include <stdlib.h>
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <string.h>
00032 #include <fcntl.h>
00033 #include <errno.h>
00034 
00035 #include <vlc/vlc.h>
00036 #include <vlc/sout.h>
00037 
00038 #ifdef HAVE_UNISTD_H
00039 #   include <unistd.h>
00040 #elif defined( WIN32 ) && !defined( UNDER_CE )
00041 #   include <io.h>
00042 #endif
00043 
00044 /* For those platforms that don't use these */
00045 #ifndef S_IRGRP
00046 #   define S_IRGRP 0
00047 #endif
00048 #ifndef S_IROTH
00049 #   define S_IROTH 0
00050 #endif
00051 #ifndef STDOUT_FILENO
00052 #   define STDOUT_FILENO 1
00053 #endif
00054 #ifndef O_LARGEFILE
00055 #   define O_LARGEFILE 0
00056 #endif
00057 
00058 /*****************************************************************************
00059  * Module descriptor
00060  *****************************************************************************/
00061 static int  Open ( vlc_object_t * );
00062 static void Close( vlc_object_t * );
00063 
00064 #define SOUT_CFG_PREFIX "sout-file-"
00065 #define APPEND_TEXT N_("Append to file")
00066 #define APPEND_LONGTEXT N_( "Append to file if it exists instead " \
00067                             "of replacing it.")
00068 
00069 vlc_module_begin();
00070     set_description( _("File stream output") );
00071     set_shortname( N_("File" ));
00072     set_capability( "sout access", 50 );
00073     set_category( CAT_SOUT );
00074     set_subcategory( SUBCAT_SOUT_ACO );
00075     add_shortcut( "file" );
00076     add_shortcut( "stream" );
00077     add_bool( SOUT_CFG_PREFIX "append", 0, NULL, APPEND_TEXT,APPEND_LONGTEXT,
00078               VLC_TRUE );
00079     set_callbacks( Open, Close );
00080 vlc_module_end();
00081 
00082 
00083 /*****************************************************************************
00084  * Exported prototypes
00085  *****************************************************************************/
00086 static const char *ppsz_sout_options[] = {
00087     "append", NULL
00088 };
00089 
00090 static int Write( sout_access_out_t *, block_t * );
00091 static int Seek ( sout_access_out_t *, off_t  );
00092 static int Read ( sout_access_out_t *, block_t * );
00093 
00094 struct sout_access_out_sys_t
00095 {
00096     int i_handle;
00097 };
00098 
00099 /*****************************************************************************
00100  * Open: open the file
00101  *****************************************************************************/
00102 static int Open( vlc_object_t *p_this )
00103 {
00104     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
00105     int                 i_flags;
00106     vlc_value_t         val;
00107 
00108     sout_CfgParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
00109 
00110     if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
00111     {
00112         msg_Err( p_access, "out of memory" );
00113         return( VLC_EGENERIC );
00114     }
00115 
00116     if( !p_access->psz_name )
00117     {
00118         msg_Err( p_access, "no file name specified" );
00119         return VLC_EGENERIC;
00120     }
00121     i_flags = O_RDWR|O_CREAT|O_LARGEFILE;
00122 
00123     var_Get( p_access, SOUT_CFG_PREFIX "append", &val );
00124     if( val.b_bool )
00125     {
00126         i_flags |= O_APPEND;
00127     }
00128     else
00129     {
00130         i_flags |= O_TRUNC;
00131     }
00132     if( !strcmp( p_access->psz_name, "-" ) )
00133     {
00134 #if defined(WIN32)
00135         setmode (STDOUT_FILENO, O_BINARY);
00136 #endif
00137         p_access->p_sys->i_handle = STDOUT_FILENO;
00138         msg_Dbg( p_access, "using stdout" );
00139     }
00140     else if( ( p_access->p_sys->i_handle =
00141                open( p_access->psz_name, i_flags,
00142                      S_IWRITE | S_IREAD | S_IRGRP | S_IROTH ) ) == -1 )
00143     {
00144         msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
00145         free( p_access->p_sys );
00146         return( VLC_EGENERIC );
00147     }
00148 
00149     p_access->pf_write = Write;
00150     p_access->pf_read  = Read;
00151     p_access->pf_seek  = Seek;
00152 
00153     msg_Dbg( p_access, "file access output opened (`%s')", p_access->psz_name );
00154 
00155     /* Update pace control flag */
00156     if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
00157         p_access->p_sout->i_out_pace_nocontrol++;
00158 
00159     return VLC_SUCCESS;
00160 }
00161 
00162 /*****************************************************************************
00163  * Close: close the target
00164  *****************************************************************************/
00165 static void Close( vlc_object_t * p_this )
00166 {
00167     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
00168 
00169     if( strcmp( p_access->psz_name, "-" ) )
00170     {
00171         if( p_access->p_sys->i_handle )
00172         {
00173             close( p_access->p_sys->i_handle );
00174         }
00175     }
00176     free( p_access->p_sys );
00177 
00178     /* Update pace control flag */
00179     if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
00180         p_access->p_sout->i_out_pace_nocontrol--;
00181 
00182     msg_Dbg( p_access, "file access output closed" );
00183 }
00184 
00185 /*****************************************************************************
00186  * Read: standard read on a file descriptor.
00187  *****************************************************************************/
00188 static int Read( sout_access_out_t *p_access, block_t *p_buffer )
00189 {
00190     if( strcmp( p_access->psz_name, "-" ) )
00191     {
00192         return read( p_access->p_sys->i_handle, p_buffer->p_buffer,
00193                      p_buffer->i_buffer );
00194     }
00195 
00196     msg_Err( p_access, "cannot read while using stdout" );
00197     return VLC_EGENERIC;
00198 }
00199 
00200 /*****************************************************************************
00201  * Write: standard write on a file descriptor.
00202  *****************************************************************************/
00203 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
00204 {
00205     size_t i_write = 0;
00206 
00207     while( p_buffer )
00208     {
00209         block_t *p_next = p_buffer->p_next;;
00210 
00211         i_write += write( p_access->p_sys->i_handle,
00212                           p_buffer->p_buffer, p_buffer->i_buffer );
00213         block_Release( p_buffer );
00214 
00215         p_buffer = p_next;
00216     }
00217 
00218     return i_write;
00219 }
00220 
00221 /*****************************************************************************
00222  * Seek: seek to a specific location in a file
00223  *****************************************************************************/
00224 static int Seek( sout_access_out_t *p_access, off_t i_pos )
00225 {
00226     if( strcmp( p_access->psz_name, "-" ) )
00227     {
00228 #if defined( WIN32 ) && !defined( UNDER_CE )
00229         return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
00230 #else
00231         return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
00232 #endif
00233     }
00234     else
00235     {
00236         msg_Err( p_access, "cannot seek while using stdout" );
00237         return VLC_EGENERIC;
00238     }
00239 }

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