]> git.sesse.net Git - vlc/blob - modules/access_output/file.c
* toolbox: use the "Output_Dir" property when generating the msvc project files so...
[vlc] / modules / access_output / file.c
1 /*****************************************************************************
2  * file.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: file.c,v 1.10 2003/12/04 12:33:43 gbazin Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Eric Petit <titer@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 #include <vlc/vlc.h>
36 #include <vlc/input.h>
37 #include <vlc/sout.h>
38
39 #ifdef HAVE_UNISTD_H
40 #   include <unistd.h>
41 #elif defined( WIN32 ) && !defined( UNDER_CE )
42 #   include <io.h>
43 #endif
44
45 /* For those platforms that don't use these */
46 #ifndef S_IRGRP
47 #   define S_IRGRP 0
48 #endif
49 #ifndef S_IROTH
50 #   define S_IROTH 0
51 #endif
52 #ifndef STDOUT_FILENO
53 #   define STDOUT_FILENO 1
54 #endif
55
56 /*****************************************************************************
57  * Exported prototypes
58  *****************************************************************************/
59 static int     Open   ( vlc_object_t * );
60 static void    Close  ( vlc_object_t * );
61
62 static int     Write( sout_access_out_t *, sout_buffer_t * );
63 static int     Seek ( sout_access_out_t *, off_t  );
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 vlc_module_begin();
69     set_description( _("File stream ouput") );
70     set_capability( "sout access", 50 );
71     add_shortcut( "file" );
72     set_callbacks( Open, Close );
73 vlc_module_end();
74
75 struct sout_access_out_sys_t
76 {
77     int i_handle;
78
79 };
80
81 /*****************************************************************************
82  * Open: open the file
83  *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
85 {
86     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
87     int                 i_flags;
88
89     if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
90     {
91         msg_Err( p_access, "out of memory" );
92         return( VLC_EGENERIC );
93     }
94
95     if( !p_access->psz_name )
96     {
97         msg_Err( p_access, "no file name specified" );
98         return VLC_EGENERIC;
99     }
100     i_flags = O_WRONLY|O_CREAT;
101     if( sout_cfg_find_value( p_access->p_cfg, "append" ) )
102     {
103         i_flags |= O_APPEND;
104     }
105     else
106     {
107         i_flags |= O_TRUNC;
108     }
109     if( !strcmp( p_access->psz_name, "-" ) )
110     {
111         p_access->p_sys->i_handle = STDOUT_FILENO;
112         msg_Dbg( p_access, "using stdout" );
113     }
114     else if( ( p_access->p_sys->i_handle =
115                open( p_access->psz_name, i_flags,
116                      S_IWRITE | S_IREAD | S_IRGRP | S_IROTH ) ) == -1 )
117     {
118         msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
119         free( p_access->p_sys );
120         return( VLC_EGENERIC );
121     }
122
123     p_access->pf_write        = Write;
124     p_access->pf_seek         = Seek;
125
126     msg_Info( p_access, "Open: name:`%s'", p_access->psz_name );
127     return VLC_SUCCESS;
128 }
129
130 /*****************************************************************************
131  * Close: close the target
132  *****************************************************************************/
133 static void Close( vlc_object_t * p_this )
134 {
135     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
136
137     if( strcmp( p_access->psz_name, "-" ) )
138     {
139         if( p_access->p_sys->i_handle )
140         {
141             close( p_access->p_sys->i_handle );
142         }
143     }
144     free( p_access->p_sys );
145
146     msg_Info( p_access, "Close" );
147 }
148
149 /*****************************************************************************
150  * Read: standard read on a file descriptor.
151  *****************************************************************************/
152 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
153 {
154     size_t i_write = 0;
155
156     do
157     {
158         sout_buffer_t *p_next;
159
160         i_write += write( p_access->p_sys->i_handle, p_buffer->p_buffer,
161                           p_buffer->i_size );
162         p_next = p_buffer->p_next;
163         sout_BufferDelete( p_access->p_sout, p_buffer );
164         p_buffer = p_next;
165
166     } while( p_buffer );
167
168     return( i_write );
169 }
170
171 /*****************************************************************************
172  * Seek: seek to a specific location in a file
173  *****************************************************************************/
174 static int Seek( sout_access_out_t *p_access, off_t i_pos )
175 {
176     msg_Dbg( p_access, "Seek: pos:"I64Fd, (int64_t)i_pos );
177
178     if( strcmp( p_access->psz_name, "-" ) )
179     {
180 #if defined( WIN32 ) && !defined( UNDER_CE )
181         return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
182 #else
183         return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
184 #endif
185     }
186     else
187     {
188         msg_Err( p_access, "cannot seek while using stdout" );
189         return VLC_EGENERIC;
190     }
191 }