1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Eric Petit <titer@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <sys/types.h>
40 #elif defined( WIN32 ) && !defined( UNDER_CE )
44 /* For those platforms that don't use these */
52 # define STDOUT_FILENO 1
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
62 set_description( _("File stream ouput") );
63 set_capability( "sout access", 50 );
64 add_shortcut( "file" );
65 add_shortcut( "stream" );
66 set_callbacks( Open, Close );
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Write( sout_access_out_t *, block_t * );
74 static int Seek ( sout_access_out_t *, off_t );
75 static int Read ( sout_access_out_t *, block_t * );
77 struct sout_access_out_sys_t
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
87 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
90 if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
92 msg_Err( p_access, "out of memory" );
93 return( VLC_EGENERIC );
96 if( !p_access->psz_name )
98 msg_Err( p_access, "no file name specified" );
101 i_flags = O_RDWR|O_CREAT;
102 if( sout_cfg_find_value( p_access->p_cfg, "append" ) )
110 if( !strcmp( p_access->psz_name, "-" ) )
112 p_access->p_sys->i_handle = STDOUT_FILENO;
113 msg_Dbg( p_access, "using stdout" );
115 else if( ( p_access->p_sys->i_handle =
116 open( p_access->psz_name, i_flags,
117 S_IWRITE | S_IREAD | S_IRGRP | S_IROTH ) ) == -1 )
119 msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
120 free( p_access->p_sys );
121 return( VLC_EGENERIC );
124 p_access->pf_write = Write;
125 p_access->pf_read = Read;
126 p_access->pf_seek = Seek;
128 msg_Dbg( p_access, "file access output opened (`%s')", p_access->psz_name );
130 /* Update pace control flag */
131 if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
132 p_access->p_sout->i_out_pace_nocontrol++;
137 /*****************************************************************************
138 * Close: close the target
139 *****************************************************************************/
140 static void Close( vlc_object_t * p_this )
142 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
144 if( strcmp( p_access->psz_name, "-" ) )
146 if( p_access->p_sys->i_handle )
148 close( p_access->p_sys->i_handle );
151 free( p_access->p_sys );
153 /* Update pace control flag */
154 if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
155 p_access->p_sout->i_out_pace_nocontrol--;
157 msg_Dbg( p_access, "file access output closed" );
160 /*****************************************************************************
161 * Read: standard read on a file descriptor.
162 *****************************************************************************/
163 static int Read( sout_access_out_t *p_access, block_t *p_buffer )
165 if( strcmp( p_access->psz_name, "-" ) )
167 return read( p_access->p_sys->i_handle, p_buffer->p_buffer,
168 p_buffer->i_buffer );
171 msg_Err( p_access, "cannot read while using stdout" );
175 /*****************************************************************************
176 * Write: standard write on a file descriptor.
177 *****************************************************************************/
178 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
184 block_t *p_next = p_buffer->p_next;;
186 i_write += write( p_access->p_sys->i_handle,
187 p_buffer->p_buffer, p_buffer->i_buffer );
188 block_Release( p_buffer );
196 /*****************************************************************************
197 * Seek: seek to a specific location in a file
198 *****************************************************************************/
199 static int Seek( sout_access_out_t *p_access, off_t i_pos )
201 if( strcmp( p_access->psz_name, "-" ) )
203 #if defined( WIN32 ) && !defined( UNDER_CE )
204 return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
206 return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
211 msg_Err( p_access, "cannot seek while using stdout" );