1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: file.c,v 1.9 2003/09/07 20:08:31 fenrir Exp $
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>
36 #include <vlc/input.h>
41 #elif defined( WIN32 ) && !defined( UNDER_CE )
45 /* For those platforms that don't use these */
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close ( vlc_object_t * );
59 static int Write( sout_access_out_t *, sout_buffer_t * );
60 static int Seek ( sout_access_out_t *, off_t );
62 /*****************************************************************************
64 *****************************************************************************/
66 set_description( _("File stream ouput") );
67 set_capability( "sout access", 50 );
68 add_shortcut( "file" );
69 set_callbacks( Open, Close );
72 struct sout_access_out_sys_t
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open( vlc_object_t *p_this )
83 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
86 if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
88 msg_Err( p_access, "out of memory" );
89 return( VLC_EGENERIC );
92 if( !p_access->psz_name )
94 msg_Err( p_access, "no file name specified" );
97 i_flags = O_WRONLY|O_CREAT;
98 if( sout_cfg_find_value( p_access->p_cfg, "append" ) )
106 if( !strcmp( p_access->psz_name, "-" ) )
108 p_access->p_sys->i_handle = STDOUT_FILENO;
109 msg_Dbg( p_access, "using stdout" );
111 else if( ( p_access->p_sys->i_handle =
112 open( p_access->psz_name, i_flags,
113 S_IWRITE | S_IREAD | S_IRGRP | S_IROTH ) ) == -1 )
115 msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
116 free( p_access->p_sys );
117 return( VLC_EGENERIC );
120 p_access->pf_write = Write;
121 p_access->pf_seek = Seek;
123 msg_Info( p_access, "Open: name:`%s'", p_access->psz_name );
127 /*****************************************************************************
128 * Close: close the target
129 *****************************************************************************/
130 static void Close( vlc_object_t * p_this )
132 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
134 if( strcmp( p_access->psz_name, "-" ) )
136 if( p_access->p_sys->i_handle )
138 close( p_access->p_sys->i_handle );
141 free( p_access->p_sys );
143 msg_Info( p_access, "Close" );
146 /*****************************************************************************
147 * Read: standard read on a file descriptor.
148 *****************************************************************************/
149 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
155 sout_buffer_t *p_next;
157 i_write += write( p_access->p_sys->i_handle, p_buffer->p_buffer,
159 p_next = p_buffer->p_next;
160 sout_BufferDelete( p_access->p_sout, p_buffer );
168 /*****************************************************************************
169 * Seek: seek to a specific location in a file
170 *****************************************************************************/
171 static int Seek( sout_access_out_t *p_access, off_t i_pos )
173 msg_Dbg( p_access, "Seek: pos:"I64Fd, (int64_t)i_pos );
175 if( strcmp( p_access->psz_name, "-" ) )
177 #if defined( WIN32 ) && !defined( UNDER_CE )
178 return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
180 return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
185 msg_Err( p_access, "cannot seek while using stdout" );