1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: file.c,v 1.6 2003/06/11 21:41:56 gbazin 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 )
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
55 static int Write( sout_access_out_t *, sout_buffer_t * );
56 static int Seek ( sout_access_out_t *, off_t );
58 /*****************************************************************************
60 *****************************************************************************/
62 set_description( _("File stream ouput") );
63 set_capability( "sout access", 50 );
64 add_shortcut( "file" );
65 set_callbacks( Open, Close );
68 struct sout_access_out_sys_t
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
79 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
81 if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
83 msg_Err( p_access, "out of memory" );
84 return( VLC_EGENERIC );
87 if( !p_access->psz_name )
89 msg_Err( p_access, "no file name specified" );
92 if( !strcmp( p_access->psz_name, "-" ) )
94 p_access->p_sys->i_handle = STDOUT_FILENO;
95 msg_Dbg( p_access, "using stdout" );
97 else if( ( p_access->p_sys->i_handle =
98 open( p_access->psz_name, O_WRONLY|O_CREAT|O_TRUNC,
99 S_IREAD | S_IRGRP | S_IROTH ) ) == -1 )
101 msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
102 free( p_access->p_sys );
103 return( VLC_EGENERIC );
106 p_access->pf_write = Write;
107 p_access->pf_seek = Seek;
109 msg_Info( p_access, "Open: name:`%s'", p_access->psz_name );
113 /*****************************************************************************
114 * Close: close the target
115 *****************************************************************************/
116 static void Close( vlc_object_t * p_this )
118 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
120 if( strcmp( p_access->psz_name, "-" ) )
122 if( p_access->p_sys->i_handle )
124 close( p_access->p_sys->i_handle );
127 free( p_access->p_sys );
129 msg_Info( p_access, "Close" );
132 /*****************************************************************************
133 * Read: standard read on a file descriptor.
134 *****************************************************************************/
135 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
141 sout_buffer_t *p_next;
143 i_write += write( p_access->p_sys->i_handle, p_buffer->p_buffer,
145 p_next = p_buffer->p_next;
146 sout_BufferDelete( p_access->p_sout, p_buffer );
154 /*****************************************************************************
155 * Seek: seek to a specific location in a file
156 *****************************************************************************/
157 static int Seek( sout_access_out_t *p_access, off_t i_pos )
159 msg_Dbg( p_access, "Seek: pos:"I64Fd, (int64_t)i_pos );
161 if( strcmp( p_access->psz_name, "-" ) )
163 #if defined( WIN32 ) && !defined( UNDER_CE )
164 return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
166 return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
171 msg_Err( p_access, "cannot seek while using stdout" );