dlna: add PrepareForConnection action
[vlc.git] / modules / access_output / file.c
blobb1d563370c394e426ac593c6bf01e8c6391451e0
1 /*****************************************************************************
2 * file.c
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VLC authors and VideoLAN
5 * $Id$
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Preamble
27 *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include <assert.h>
34 #include <signal.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 #include <unistd.h>
40 #ifdef __OS2__
41 # include <io.h> /* setmode() */
42 #endif
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
46 #include <vlc_sout.h>
47 #include <vlc_block.h>
48 #include <vlc_fs.h>
49 #include <vlc_network.h>
50 #include <vlc_strings.h>
51 #include <vlc_dialog.h>
53 #ifndef O_LARGEFILE
54 # define O_LARGEFILE 0
55 #endif
56 #ifndef _POSIX_REALTIME_SIGNALS
57 # define _POSIX_REALTIME_SIGNALS (-1)
58 #endif
60 #define SOUT_CFG_PREFIX "sout-file-"
62 /*****************************************************************************
63 * Read: standard read on a file descriptor.
64 *****************************************************************************/
65 static ssize_t Read( sout_access_out_t *p_access, block_t *p_buffer )
67 int *fdp = p_access->p_sys, fd = *fdp;
68 ssize_t val;
71 val = read(fd, p_buffer->p_buffer, p_buffer->i_buffer);
72 while (val == -1 && errno == EINTR);
73 return val;
76 /*****************************************************************************
77 * Write: standard write on a file descriptor.
78 *****************************************************************************/
79 static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer )
81 int *fdp = p_access->p_sys, fd = *fdp;
82 size_t i_write = 0;
84 while( p_buffer )
86 ssize_t val = write(fd, p_buffer->p_buffer, p_buffer->i_buffer);
87 if (val <= 0)
89 if (errno == EINTR)
90 continue;
91 block_ChainRelease (p_buffer);
92 msg_Err( p_access, "cannot write: %s", vlc_strerror_c(errno) );
93 return -1;
96 if ((size_t)val >= p_buffer->i_buffer)
98 block_t *p_next = p_buffer->p_next;
99 block_Release (p_buffer);
100 p_buffer = p_next;
102 else
104 p_buffer->p_buffer += val;
105 p_buffer->i_buffer -= val;
107 i_write += val;
109 return i_write;
112 static ssize_t WritePipe(sout_access_out_t *access, block_t *block)
114 int *fdp = access->p_sys, fd = *fdp;
115 ssize_t total = 0;
117 while (block != NULL)
119 if (block->i_buffer == 0)
121 block_t *next = block->p_next;
122 block_Release(block);
123 block = next;
124 continue;
127 /* TODO: vectorized I/O with writev() */
128 ssize_t val = vlc_write(fd, block->p_buffer, block->i_buffer);
129 if (val < 0)
131 if (errno == EINTR)
132 continue;
134 block_ChainRelease(block);
135 msg_Err(access, "cannot write: %s", vlc_strerror_c(errno));
136 total = -1;
137 break;
140 total += val;
142 assert((size_t)val <= block->i_buffer);
143 block->p_buffer += val;
144 block->i_buffer -= val;
147 return total;
150 #ifdef S_ISSOCK
151 static ssize_t Send(sout_access_out_t *access, block_t *block)
153 int *fdp = access->p_sys, fd = *fdp;
154 size_t total = 0;
156 while (block != NULL)
158 if (block->i_buffer == 0)
160 block_t *next = block->p_next;
161 block_Release(block);
162 block = next;
163 continue;
166 /* TODO: vectorized I/O with sendmsg() */
167 ssize_t val = send(fd, block->p_buffer, block->i_buffer, MSG_NOSIGNAL);
168 if (val <= 0)
169 { /* FIXME: errno is meaningless if val is zero */
170 if (errno == EINTR)
171 continue;
172 block_ChainRelease(block);
173 msg_Err(access, "cannot write: %s", vlc_strerror_c(errno));
174 return -1;
177 total += val;
179 assert((size_t)val <= block->i_buffer);
180 block->p_buffer += val;
181 block->i_buffer -= val;
183 return total;
185 #endif
187 /*****************************************************************************
188 * Seek: seek to a specific location in a file
189 *****************************************************************************/
190 static int Seek( sout_access_out_t *p_access, off_t i_pos )
192 int *fdp = p_access->p_sys, fd = *fdp;
194 return lseek(fd, i_pos, SEEK_SET);
197 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
199 switch( i_query )
201 case ACCESS_OUT_CONTROLS_PACE:
203 bool *pb = va_arg( args, bool * );
204 *pb = strcmp( p_access->psz_access, "stream" );
205 break;
208 case ACCESS_OUT_CAN_SEEK:
210 bool *pb = va_arg( args, bool * );
211 *pb = p_access->pf_seek != NULL;
212 break;
215 default:
216 return VLC_EGENERIC;
218 return VLC_SUCCESS;
221 static const char *const ppsz_sout_options[] = {
222 "append",
223 "format",
224 "overwrite",
225 #ifdef O_SYNC
226 "sync",
227 #endif
228 NULL
231 /*****************************************************************************
232 * Open: open the file
233 *****************************************************************************/
234 static int Open( vlc_object_t *p_this )
236 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
237 int fd;
238 int *fdp = vlc_obj_malloc(p_this, sizeof (*fdp));
240 if (unlikely(fdp == NULL))
241 return VLC_ENOMEM;
243 config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
245 bool overwrite = var_GetBool (p_access, SOUT_CFG_PREFIX"overwrite");
246 bool append = var_GetBool( p_access, SOUT_CFG_PREFIX "append" );
248 if (!strcmp (p_access->psz_access, "fd"))
250 char *end;
252 fd = strtol (p_access->psz_path, &end, 0);
253 if (!*p_access->psz_path || *end)
255 msg_Err (p_access, "invalid file descriptor: %s",
256 p_access->psz_path);
257 return VLC_EGENERIC;
259 fd = vlc_dup (fd);
260 if (fd == -1)
262 msg_Err (p_access, "cannot use file descriptor: %s",
263 vlc_strerror_c(errno));
264 return VLC_EGENERIC;
267 else
268 if( !strcmp( p_access->psz_path, "-" ) )
270 #if defined( _WIN32 ) || defined( __OS2__ )
271 setmode (STDOUT_FILENO, O_BINARY);
272 #endif
273 fd = vlc_dup (STDOUT_FILENO);
274 if (fd == -1)
276 msg_Err (p_access, "cannot use standard output: %s",
277 vlc_strerror_c(errno));
278 return VLC_EGENERIC;
280 msg_Dbg( p_access, "using stdout" );
282 else
284 const char *path = p_access->psz_path;
285 char *buf = NULL;
287 if (var_InheritBool (p_access, SOUT_CFG_PREFIX"format"))
289 buf = vlc_strftime (path);
290 path = buf;
293 int flags = O_RDWR | O_CREAT | O_LARGEFILE;
294 if (!overwrite)
295 flags |= O_EXCL;
296 if (!append)
297 flags |= O_TRUNC;
298 #ifdef O_SYNC
299 if (var_GetBool (p_access, SOUT_CFG_PREFIX"sync"))
300 flags |= O_SYNC;
301 #endif
304 fd = vlc_open (path, flags, 0666);
305 if (fd != -1)
306 break;
307 if (fd == -1)
308 msg_Err (p_access, "cannot create %s: %s", path,
309 vlc_strerror_c(errno));
310 if (overwrite || errno != EEXIST)
311 break;
312 flags &= ~O_EXCL;
314 while (vlc_dialog_wait_question (p_access, VLC_DIALOG_QUESTION_NORMAL,
315 _("Keep existing file"),
316 _("Overwrite"), NULL, path,
317 _("The output file already exists. "
318 "If recording continues, the file will be "
319 "overridden and its content will be lost.")) == 1);
320 free (buf);
321 if (fd == -1)
322 return VLC_EGENERIC;
325 *fdp = fd;
326 p_access->p_sys = fdp;
328 struct stat st;
330 if (fstat (fd, &st))
332 msg_Err (p_access, "write error: %s", vlc_strerror_c(errno));
333 vlc_close (fd);
334 return VLC_EGENERIC;
337 p_access->pf_read = Read;
339 if (S_ISREG(st.st_mode) || S_ISBLK(st.st_mode))
341 p_access->pf_write = Write;
342 p_access->pf_seek = Seek;
344 #ifdef S_ISSOCK
345 else if (S_ISSOCK(st.st_mode))
347 p_access->pf_write = Send;
348 p_access->pf_seek = NULL;
350 #endif
351 else
353 p_access->pf_write = WritePipe;
354 p_access->pf_seek = NULL;
356 p_access->pf_control = Control;
358 msg_Dbg( p_access, "file access output opened (%s)", p_access->psz_path );
359 if (append)
360 lseek (fd, 0, SEEK_END);
362 return VLC_SUCCESS;
365 /*****************************************************************************
366 * Close: close the target
367 *****************************************************************************/
368 static void Close( vlc_object_t * p_this )
370 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
371 int *fdp = p_access->p_sys, fd = *fdp;
373 vlc_close(fd);
374 msg_Dbg( p_access, "file access output closed" );
377 #define OVERWRITE_TEXT N_("Overwrite existing file")
378 #define OVERWRITE_LONGTEXT N_( \
379 "If the file already exists, it will be overwritten.")
380 #define APPEND_TEXT N_("Append to file")
381 #define APPEND_LONGTEXT N_( "Append to file if it exists instead " \
382 "of replacing it.")
383 #define FORMAT_TEXT N_("Format time and date")
384 #define FORMAT_LONGTEXT N_("Perform ISO C time and date formatting " \
385 "on the file path")
386 #define SYNC_TEXT N_("Synchronous writing")
387 #define SYNC_LONGTEXT N_( "Open the file with synchronous writing.")
389 vlc_module_begin ()
390 set_description( N_("File stream output") )
391 set_shortname( N_("File" ))
392 set_capability( "sout access", 50 )
393 set_category( CAT_SOUT )
394 set_subcategory( SUBCAT_SOUT_ACO )
395 add_shortcut( "file", "stream", "fd" )
396 add_bool( SOUT_CFG_PREFIX "overwrite", true, OVERWRITE_TEXT,
397 OVERWRITE_LONGTEXT, true )
398 add_bool( SOUT_CFG_PREFIX "append", false, APPEND_TEXT,APPEND_LONGTEXT,
399 true )
400 add_bool( SOUT_CFG_PREFIX "format", false, FORMAT_TEXT, FORMAT_LONGTEXT,
401 true )
402 #ifdef O_SYNC
403 add_bool( SOUT_CFG_PREFIX "sync", false, SYNC_TEXT,SYNC_LONGTEXT,
404 false )
405 #endif
406 set_callbacks( Open, Close )
407 vlc_module_end ()