00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
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
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
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
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
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
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
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
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
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
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 }