file out: use vlc_send()
[vlc.git] / modules / access_output / file.c
blob036a0ceda65ad5cbbe3b596a7de036959162f6fd
1 /*****************************************************************************
2 * file.c
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7 * Eric Petit <titer@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include <assert.h>
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #ifdef __OS2__
40 # include <io.h> /* setmode() */
41 #endif
43 #include <vlc_common.h>
44 #include <vlc_plugin.h>
45 #include <vlc_sout.h>
46 #include <vlc_block.h>
47 #include <vlc_fs.h>
48 #include <vlc_network.h>
49 #include <vlc_strings.h>
50 #include <vlc_dialog.h>
52 #ifndef O_LARGEFILE
53 # define O_LARGEFILE 0
54 #endif
55 #ifndef _POSIX_REALTIME_SIGNALS
56 # define _POSIX_REALTIME_SIGNALS (-1)
57 #endif
59 #define SOUT_CFG_PREFIX "sout-file-"
61 /*****************************************************************************
62 * Read: standard read on a file descriptor.
63 *****************************************************************************/
64 static ssize_t Read( sout_access_out_t *p_access, block_t *p_buffer )
66 int *fdp = p_access->p_sys, fd = *fdp;
67 ssize_t val;
70 val = read(fd, p_buffer->p_buffer, p_buffer->i_buffer);
71 while (val == -1 && errno == EINTR);
72 return val;
75 /*****************************************************************************
76 * Write: standard write on a file descriptor.
77 *****************************************************************************/
78 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
80 int *fdp = p_access->p_sys, fd = *fdp;
81 size_t i_write = 0;
83 while( p_buffer )
85 ssize_t val = write(fd, p_buffer->p_buffer, p_buffer->i_buffer);
86 if (val <= 0)
88 if (errno == EINTR)
89 continue;
90 block_ChainRelease (p_buffer);
91 msg_Err( p_access, "cannot write: %s", vlc_strerror_c(errno) );
92 return -1;
95 if ((size_t)val >= p_buffer->i_buffer)
97 block_t *p_next = p_buffer->p_next;
98 block_Release (p_buffer);
99 p_buffer = p_next;
101 else
103 p_buffer->p_buffer += val;
104 p_buffer->i_buffer -= val;
106 i_write += val;
108 return i_write;
111 static ssize_t WritePipe(sout_access_out_t *access, block_t *block)
113 int *fdp = access->p_sys, fd = *fdp;
114 ssize_t total = 0;
116 while (block != NULL)
118 if (block->i_buffer == 0)
120 block_t *next = block->p_next;
121 block_Release(block);
122 block = next;
123 continue;
126 /* TODO: vectorized I/O with writev() */
127 ssize_t val = vlc_write(fd, block->p_buffer, block->i_buffer);
128 if (val < 0)
130 if (errno == EINTR)
131 continue;
133 block_ChainRelease(block);
134 msg_Err(access, "cannot write: %s", vlc_strerror_c(errno));
135 total = -1;
136 break;
139 total += val;
141 assert((size_t)val <= block->i_buffer);
142 block->p_buffer += val;
143 block->i_buffer -= val;
146 return total;
149 #ifdef S_ISSOCK
150 static ssize_t Send(sout_access_out_t *access, block_t *block)
152 int *fdp = access->p_sys, fd = *fdp;
153 size_t total = 0;
155 while (block != NULL)
157 if (block->i_buffer == 0)
159 block_t *next = block->p_next;
160 block_Release(block);
161 block = next;
162 continue;
165 /* TODO: vectorized I/O with sendmsg() */
166 ssize_t val = vlc_send(fd, block->p_buffer, block->i_buffer, 0);
167 if (val <= 0)
168 { /* FIXME: errno is meaningless if val is zero */
169 if (errno == EINTR)
170 continue;
171 block_ChainRelease(block);
172 msg_Err(access, "cannot write: %s", vlc_strerror_c(errno));
173 return -1;
176 total += val;
178 assert((size_t)val <= block->i_buffer);
179 block->p_buffer += val;
180 block->i_buffer -= val;
182 return total;
184 #endif
186 /*****************************************************************************
187 * Seek: seek to a specific location in a file
188 *****************************************************************************/
189 static int Seek( sout_access_out_t *p_access, off_t i_pos )
191 int *fdp = p_access->p_sys, fd = *fdp;
193 return lseek(fd, i_pos, SEEK_SET);
196 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
198 switch( i_query )
200 case ACCESS_OUT_CONTROLS_PACE:
202 bool *pb = va_arg( args, bool * );
203 *pb = strcmp( p_access->psz_access, "stream" );
204 break;
207 case ACCESS_OUT_CAN_SEEK:
209 bool *pb = va_arg( args, bool * );
210 *pb = p_access->pf_seek != NULL;
211 break;
214 default:
215 return VLC_EGENERIC;
217 return VLC_SUCCESS;
220 static const char *const ppsz_sout_options[] = {
221 "append",
222 "format",
223 "overwrite",
224 #ifdef O_SYNC
225 "sync",
226 #endif
227 NULL
230 /*****************************************************************************
231 * Open: open the file
232 *****************************************************************************/
233 static int Open( vlc_object_t *p_this )
235 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
236 int fd;
237 int *fdp = vlc_obj_malloc(p_this, sizeof (*fdp));
239 if (unlikely(fdp == NULL))
240 return VLC_ENOMEM;
242 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
244 bool overwrite = var_GetBool (p_access, SOUT_CFG_PREFIX"overwrite");
245 bool append = var_GetBool( p_access, SOUT_CFG_PREFIX "append" );
247 if (!strcmp (p_access->psz_access, "fd"))
249 char *end;
251 fd = strtol (p_access->psz_path, &end, 0);
252 if (!*p_access->psz_path || *end)
254 msg_Err (p_access, "invalid file descriptor: %s",
255 p_access->psz_path);
256 return VLC_EGENERIC;
258 fd = vlc_dup (fd);
259 if (fd == -1)
261 msg_Err (p_access, "cannot use file descriptor: %s",
262 vlc_strerror_c(errno));
263 return VLC_EGENERIC;
266 else
267 if( !strcmp( p_access->psz_path, "-" ) )
269 #if defined( _WIN32 ) || defined( __OS2__ )
270 setmode (STDOUT_FILENO, O_BINARY);
271 #endif
272 fd = vlc_dup (STDOUT_FILENO);
273 if (fd == -1)
275 msg_Err (p_access, "cannot use standard output: %s",
276 vlc_strerror_c(errno));
277 return VLC_EGENERIC;
279 msg_Dbg( p_access, "using stdout" );
281 else
283 const char *path = p_access->psz_path;
284 char *buf = NULL;
286 if (var_InheritBool (p_access, SOUT_CFG_PREFIX"format"))
288 buf = vlc_strftime (path);
289 path = buf;
292 int flags = O_RDWR | O_CREAT | O_LARGEFILE;
293 if (!overwrite)
294 flags |= O_EXCL;
295 if (!append)
296 flags |= O_TRUNC;
297 #ifdef O_SYNC
298 if (var_GetBool (p_access, SOUT_CFG_PREFIX"sync"))
299 flags |= O_SYNC;
300 #endif
303 fd = vlc_open (path, flags, 0666);
304 if (fd != -1)
305 break;
306 if (fd == -1)
307 msg_Err (p_access, "cannot create %s: %s", path,
308 vlc_strerror_c(errno));
309 if (overwrite || errno != EEXIST)
310 break;
311 flags &= ~O_EXCL;
313 while (vlc_dialog_wait_question (p_access, VLC_DIALOG_QUESTION_NORMAL,
314 _("Keep existing file"),
315 _("Overwrite"), NULL, path,
316 _("The output file already exists. "
317 "If recording continues, the file will be "
318 "overridden and its content will be lost.")) == 1);
319 free (buf);
320 if (fd == -1)
321 return VLC_EGENERIC;
324 *fdp = fd;
325 p_access->p_sys = fdp;
327 struct stat st;
329 if (fstat (fd, &st))
331 msg_Err (p_access, "write error: %s", vlc_strerror_c(errno));
332 vlc_close (fd);
333 return VLC_EGENERIC;
336 p_access->pf_read = Read;
338 if (S_ISREG(st.st_mode) || S_ISBLK(st.st_mode))
340 p_access->pf_write = Write;
341 p_access->pf_seek = Seek;
343 #ifdef S_ISSOCK
344 else if (S_ISSOCK(st.st_mode))
346 p_access->pf_write = Send;
347 p_access->pf_seek = NULL;
349 #endif
350 else
352 p_access->pf_write = WritePipe;
353 p_access->pf_seek = NULL;
355 p_access->pf_control = Control;
357 msg_Dbg( p_access, "file access output opened (%s)", p_access->psz_path );
358 if (append)
359 lseek (fd, 0, SEEK_END);
361 return VLC_SUCCESS;
364 /*****************************************************************************
365 * Close: close the target
366 *****************************************************************************/
367 static void Close( vlc_object_t * p_this )
369 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
370 int *fdp = p_access->p_sys, fd = *fdp;
372 vlc_close(fd);
373 msg_Dbg( p_access, "file access output closed" );
376 #define OVERWRITE_TEXT N_("Overwrite existing file")
377 #define OVERWRITE_LONGTEXT N_( \
378 "If the file already exists, it will be overwritten.")
379 #define APPEND_TEXT N_("Append to file")
380 #define APPEND_LONGTEXT N_( "Append to file if it exists instead " \
381 "of replacing it.")
382 #define FORMAT_TEXT N_("Format time and date")
383 #define FORMAT_LONGTEXT N_("Perform ISO C time and date formatting " \
384 "on the file path")
385 #define SYNC_TEXT N_("Synchronous writing")
386 #define SYNC_LONGTEXT N_( "Open the file with synchronous writing.")
388 vlc_module_begin ()
389 set_description( N_("File stream output") )
390 set_shortname( N_("File" ))
391 set_capability( "sout access", 50 )
392 set_category( CAT_SOUT )
393 set_subcategory( SUBCAT_SOUT_ACO )
394 add_shortcut( "file", "stream", "fd" )
395 add_bool( SOUT_CFG_PREFIX "overwrite", true, OVERWRITE_TEXT,
396 OVERWRITE_LONGTEXT, true )
397 add_bool( SOUT_CFG_PREFIX "append", false, APPEND_TEXT,APPEND_LONGTEXT,
398 true )
399 add_bool( SOUT_CFG_PREFIX "format", false, FORMAT_TEXT, FORMAT_LONGTEXT,
400 true )
401 #ifdef O_SYNC
402 add_bool( SOUT_CFG_PREFIX "sync", false, SYNC_TEXT,SYNC_LONGTEXT,
403 false )
404 #endif
405 set_callbacks( Open, Close )
406 vlc_module_end ()