1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: file.c,v 1.5 2003/04/29 15:40: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 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close ( vlc_object_t * );
51 static int Write( sout_access_out_t *, sout_buffer_t * );
52 static int Seek ( sout_access_out_t *, off_t );
54 /*****************************************************************************
56 *****************************************************************************/
58 set_description( _("File stream ouput") );
59 set_capability( "sout access", 50 );
60 add_shortcut( "file" );
61 set_callbacks( Open, Close );
64 struct sout_access_out_sys_t
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open( vlc_object_t *p_this )
75 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
77 if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
79 msg_Err( p_access, "out of memory" );
80 return( VLC_EGENERIC );
83 if( !p_access->psz_name )
85 msg_Err( p_access, "no file name specified" );
88 if( !strcmp( p_access->psz_name, "-" ) )
90 p_access->p_sys->p_file = stdout;
91 msg_Dbg( p_access, "using stdout" );
93 else if( !( p_access->p_sys->p_file = fopen( p_access->psz_name, "wb" ) ) )
95 msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
96 free( p_access->p_sys );
97 return( VLC_EGENERIC );
100 p_access->pf_write = Write;
101 p_access->pf_seek = Seek;
103 msg_Info( p_access, "Open: name:`%s'", p_access->psz_name );
107 /*****************************************************************************
108 * Close: close the target
109 *****************************************************************************/
110 static void Close( vlc_object_t * p_this )
112 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
114 if( strcmp( p_access->psz_name, "-" ) )
116 if( p_access->p_sys->p_file )
118 fclose( p_access->p_sys->p_file );
121 free( p_access->p_sys );
123 msg_Info( p_access, "Close" );
126 /*****************************************************************************
127 * Read: standard read on a file descriptor.
128 *****************************************************************************/
129 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
135 sout_buffer_t *p_next;
137 i_write += fwrite( p_buffer->p_buffer, 1, p_buffer->i_size,
138 p_access->p_sys->p_file );
139 p_next = p_buffer->p_next;
140 sout_BufferDelete( p_access->p_sout, p_buffer );
148 /*****************************************************************************
149 * Seek: seek to a specific location in a file
150 *****************************************************************************/
151 static int Seek( sout_access_out_t *p_access, off_t i_pos )
154 msg_Dbg( p_access, "Seek: pos:"I64Fd, (int64_t)i_pos );
155 if( strcmp( p_access->psz_name, "-" ) )
157 return( fseek( p_access->p_sys->p_file, i_pos, SEEK_SET ) );
161 msg_Err( p_access, "cannot seek while using stdout" );