]> git.sesse.net Git - vlc/blob - modules/access_output/file.c
macosx: add trademark claim to about panel
[vlc] / modules / access_output / file.c
1 /*****************************************************************************
2  * file.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <sys/types.h>
34 #include <time.h>
35 #include <fcntl.h>
36 #include <errno.h>
37
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_sout.h>
41 #include <vlc_block.h>
42 #include <vlc_fs.h>
43 #include <vlc_strings.h>
44 #include <vlc_dialog.h>
45
46 #if defined( WIN32 ) || defined( __OS2__ )
47 #   include <io.h>
48 #endif
49
50 #ifndef WIN32
51 #   include <unistd.h>
52 #endif
53
54 #ifndef O_LARGEFILE
55 #   define O_LARGEFILE 0
56 #endif
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 static int  Open ( vlc_object_t * );
62 static void Close( vlc_object_t * );
63
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 " \
70                             "of replacing it.")
71 #define FORMAT_TEXT N_("Format time and date")
72 #define FORMAT_LONGTEXT N_("Perform ISO C time and date formatting " \
73     "on the file path")
74 #define SYNC_TEXT N_("Synchronous writing")
75 #define SYNC_LONGTEXT N_( "Open the file with synchronous writing.")
76
77 vlc_module_begin ()
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,
87               true )
88     add_bool( SOUT_CFG_PREFIX "format", false, FORMAT_TEXT, FORMAT_LONGTEXT,
89               true )
90 #ifdef O_SYNC
91     add_bool( SOUT_CFG_PREFIX "sync", false, SYNC_TEXT,SYNC_LONGTEXT,
92               false )
93 #endif
94     set_callbacks( Open, Close )
95 vlc_module_end ()
96
97
98 /*****************************************************************************
99  * Exported prototypes
100  *****************************************************************************/
101 static const char *const ppsz_sout_options[] = {
102     "append",
103     "format",
104     "overwrite",
105 #ifdef O_SYNC
106     "sync",
107 #endif
108     NULL
109 };
110
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 );
115
116 /*****************************************************************************
117  * Open: open the file
118  *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
120 {
121     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
122     int                 fd;
123
124     config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
125
126     if( !p_access->psz_path )
127     {
128         msg_Err( p_access, "no file name specified" );
129         return VLC_EGENERIC;
130     }
131
132     bool overwrite = var_GetBool (p_access, SOUT_CFG_PREFIX"overwrite");
133     bool append = var_GetBool( p_access, SOUT_CFG_PREFIX "append" );
134
135     if (!strcmp (p_access->psz_access, "fd"))
136     {
137         char *end;
138
139         fd = strtol (p_access->psz_path, &end, 0);
140         if (!*p_access->psz_path || *end)
141         {
142             msg_Err (p_access, "invalid file descriptor: %s",
143                      p_access->psz_path);
144             return VLC_EGENERIC;
145         }
146         fd = vlc_dup (fd);
147         if (fd == -1)
148         {
149             msg_Err (p_access, "cannot use file descriptor: %m");
150             return VLC_EGENERIC;
151         }
152     }
153     else
154     if( !strcmp( p_access->psz_path, "-" ) )
155     {
156 #if defined( WIN32 ) || defined( __OS2__ )
157         setmode (STDOUT_FILENO, O_BINARY);
158 #endif
159         fd = vlc_dup (STDOUT_FILENO);
160         if (fd == -1)
161         {
162             msg_Err (p_access, "cannot use standard output: %m");
163             return VLC_EGENERIC;
164         }
165         msg_Dbg( p_access, "using stdout" );
166     }
167     else
168     {
169         const char *path = p_access->psz_path;
170         char *buf = NULL;
171
172         if (var_InheritBool (p_access, SOUT_CFG_PREFIX"format"))
173         {
174             buf = str_format_time (path);
175             path_sanitize (buf);
176             path = buf;
177         }
178
179         int flags = O_RDWR | O_CREAT | O_LARGEFILE;
180         if (!overwrite)
181             flags |= O_EXCL;
182         if (!append)
183             flags |= O_TRUNC;
184 #ifdef O_SYNC
185         if (var_GetBool (p_access, SOUT_CFG_PREFIX"sync"))
186             flags |= O_SYNC;
187 #endif
188         do
189         {
190             fd = vlc_open (path, flags, 0666);
191             if (fd != -1)
192                 break;
193             if (fd == -1)
194                 msg_Err (p_access, "cannot create %s: %m", path);
195             if (overwrite || errno != EEXIST)
196                 break;
197             flags &= ~O_EXCL;
198         }
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);
205         free (buf);
206         if (fd == -1)
207             return VLC_EGENERIC;
208     }
209
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;
215
216     msg_Dbg( p_access, "file access output opened (%s)", p_access->psz_path );
217     if (append)
218         lseek (fd, 0, SEEK_END);
219
220     return VLC_SUCCESS;
221 }
222
223 /*****************************************************************************
224  * Close: close the target
225  *****************************************************************************/
226 static void Close( vlc_object_t * p_this )
227 {
228     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
229
230     close( (intptr_t)p_access->p_sys );
231
232     msg_Dbg( p_access, "file access output closed" );
233 }
234
235 static int Control( sout_access_out_t *p_access, int i_query, va_list args )
236 {
237     switch( i_query )
238     {
239         case ACCESS_OUT_CONTROLS_PACE:
240         {
241             bool *pb = va_arg( args, bool * );
242             *pb = strcmp( p_access->psz_access, "stream" );
243             break;
244         }
245
246         default:
247             return VLC_EGENERIC;
248     }
249     return VLC_SUCCESS;
250 }
251
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 )
256 {
257     ssize_t val;
258
259     do
260         val = read( (intptr_t)p_access->p_sys, p_buffer->p_buffer,
261                     p_buffer->i_buffer );
262     while (val == -1 && errno == EINTR);
263     return val;
264 }
265
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 )
270 {
271     size_t i_write = 0;
272
273     while( p_buffer )
274     {
275         ssize_t val = write ((intptr_t)p_access->p_sys,
276                              p_buffer->p_buffer, p_buffer->i_buffer);
277         if (val <= 0)
278         {
279             if (errno == EINTR)
280                 continue;
281             block_ChainRelease (p_buffer);
282             msg_Err( p_access, "cannot write: %m" );
283             return -1;
284         }
285
286         if ((size_t)val >= p_buffer->i_buffer)
287         {
288             block_t *p_next = p_buffer->p_next;
289             block_Release (p_buffer);
290             p_buffer = p_next;
291         }
292         else
293         {
294             p_buffer->p_buffer += val;
295             p_buffer->i_buffer -= val;
296         }
297         i_write += val;
298     }
299     return i_write;
300 }
301
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 )
306 {
307     return lseek( (intptr_t)p_access->p_sys, i_pos, SEEK_SET );
308 }