1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <sys/types.h>
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
41 #include <vlc_block.h>
43 #include <vlc_strings.h>
44 #include <vlc_dialog.h>
46 #if defined( _WIN32 ) || defined( __OS2__ )
55 # define O_LARGEFILE 0
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
64 #define SOUT_CFG_PREFIX "sout-file-"
65 #define OVERWRITE_TEXT N_("Overwrite existing file")
66 #define OVERWRITE_LONGTEXT N_( \
67 "If the file already exists, it will be overwritten.")
68 #define APPEND_TEXT N_("Append to file")
69 #define APPEND_LONGTEXT N_( "Append to file if it exists instead " \
71 #define FORMAT_TEXT N_("Format time and date")
72 #define FORMAT_LONGTEXT N_("Perform ISO C time and date formatting " \
74 #define SYNC_TEXT N_("Synchronous writing")
75 #define SYNC_LONGTEXT N_( "Open the file with synchronous writing.")
78 set_description( N_("File stream output") )
79 set_shortname( N_("File" ))
80 set_capability( "sout access", 50 )
81 set_category( CAT_SOUT )
82 set_subcategory( SUBCAT_SOUT_ACO )
83 add_shortcut( "file", "stream", "fd" )
84 add_bool( SOUT_CFG_PREFIX "overwrite", true, OVERWRITE_TEXT,
85 OVERWRITE_LONGTEXT, true )
86 add_bool( SOUT_CFG_PREFIX "append", false, APPEND_TEXT,APPEND_LONGTEXT,
88 add_bool( SOUT_CFG_PREFIX "format", false, FORMAT_TEXT, FORMAT_LONGTEXT,
91 add_bool( SOUT_CFG_PREFIX "sync", false, SYNC_TEXT,SYNC_LONGTEXT,
94 set_callbacks( Open, Close )
98 /*****************************************************************************
100 *****************************************************************************/
101 static const char *const ppsz_sout_options[] = {
111 static ssize_t Write( sout_access_out_t *, block_t * );
112 static int Seek ( sout_access_out_t *, off_t );
113 static ssize_t Read ( sout_access_out_t *, block_t * );
114 static int Control( sout_access_out_t *, int, va_list );
116 /*****************************************************************************
117 * Open: open the file
118 *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
121 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
124 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
126 if( !p_access->psz_path )
128 msg_Err( p_access, "no file name specified" );
132 bool overwrite = var_GetBool (p_access, SOUT_CFG_PREFIX"overwrite");
133 bool append = var_GetBool( p_access, SOUT_CFG_PREFIX "append" );
135 if (!strcmp (p_access->psz_access, "fd"))
139 fd = strtol (p_access->psz_path, &end, 0);
140 if (!*p_access->psz_path || *end)
142 msg_Err (p_access, "invalid file descriptor: %s",
149 msg_Err (p_access, "cannot use file descriptor: %m");
154 if( !strcmp( p_access->psz_path, "-" ) )
156 #if defined( _WIN32 ) || defined( __OS2__ )
157 setmode (STDOUT_FILENO, O_BINARY);
159 fd = vlc_dup (STDOUT_FILENO);
162 msg_Err (p_access, "cannot use standard output: %m");
165 msg_Dbg( p_access, "using stdout" );
169 const char *path = p_access->psz_path;
172 if (var_InheritBool (p_access, SOUT_CFG_PREFIX"format"))
174 buf = str_format_time (path);
179 int flags = O_RDWR | O_CREAT | O_LARGEFILE;
185 if (var_GetBool (p_access, SOUT_CFG_PREFIX"sync"))
190 fd = vlc_open (path, flags, 0666);
194 msg_Err (p_access, "cannot create %s: %m", path);
195 if (overwrite || errno != EEXIST)
199 while (dialog_Question (p_access, path,
200 _("The output file already exists. "
201 "If recording continues, the file will be "
202 "overridden and its content will be lost."),
203 _("Keep existing file"),
204 _("Overwrite"), NULL) == 2);
210 p_access->pf_write = Write;
211 p_access->pf_read = Read;
212 p_access->pf_seek = Seek;
213 p_access->pf_control = Control;
214 p_access->p_sys = (void *)(intptr_t)fd;
216 msg_Dbg( p_access, "file access output opened (%s)", p_access->psz_path );
218 lseek (fd, 0, SEEK_END);
223 /*****************************************************************************
224 * Close: close the target
225 *****************************************************************************/
226 static void Close( vlc_object_t * p_this )
228 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
230 close( (intptr_t)p_access->p_sys );
232 msg_Dbg( p_access, "file access output closed" );
235 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
239 case ACCESS_OUT_CONTROLS_PACE:
241 bool *pb = va_arg( args, bool * );
242 *pb = strcmp( p_access->psz_access, "stream" );
252 /*****************************************************************************
253 * Read: standard read on a file descriptor.
254 *****************************************************************************/
255 static ssize_t Read( sout_access_out_t *p_access, block_t *p_buffer )
260 val = read( (intptr_t)p_access->p_sys, p_buffer->p_buffer,
261 p_buffer->i_buffer );
262 while (val == -1 && errno == EINTR);
266 /*****************************************************************************
267 * Write: standard write on a file descriptor.
268 *****************************************************************************/
269 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
275 ssize_t val = write ((intptr_t)p_access->p_sys,
276 p_buffer->p_buffer, p_buffer->i_buffer);
281 block_ChainRelease (p_buffer);
282 msg_Err( p_access, "cannot write: %m" );
286 if ((size_t)val >= p_buffer->i_buffer)
288 block_t *p_next = p_buffer->p_next;
289 block_Release (p_buffer);
294 p_buffer->p_buffer += val;
295 p_buffer->i_buffer -= val;
302 /*****************************************************************************
303 * Seek: seek to a specific location in a file
304 *****************************************************************************/
305 static int Seek( sout_access_out_t *p_access, off_t i_pos )
307 return lseek( (intptr_t)p_access->p_sys, i_pos, SEEK_SET );