]> git.sesse.net Git - vlc/blob - modules/access_output/file.c
* src/stream_output/stream_output.c, include/stream_output.h: new sout_AccessOutRead...
[vlc] / modules / access_output / file.c
1 /*****************************************************************************
2  * file.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: file.c,v 1.11 2004/01/23 17:56:14 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 static int     Read ( sout_access_out_t *, sout_buffer_t * );
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 vlc_module_begin();
70     set_description( _("File stream ouput") );
71     set_capability( "sout access", 50 );
72     add_shortcut( "file" );
73     set_callbacks( Open, Close );
74 vlc_module_end();
75
76 struct sout_access_out_sys_t
77 {
78     int i_handle;
79
80 };
81
82 /*****************************************************************************
83  * Open: open the file
84  *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
86 {
87     sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
88     int                 i_flags;
89
90     if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
91     {
92         msg_Err( p_access, "out of memory" );
93         return( VLC_EGENERIC );
94     }
95
96     if( !p_access->psz_name )
97     {
98         msg_Err( p_access, "no file name specified" );
99         return VLC_EGENERIC;
100     }
101     i_flags = O_RDWR|O_CREAT;
102     if( sout_cfg_find_value( p_access->p_cfg, "append" ) )
103     {
104         i_flags |= O_APPEND;
105     }
106     else
107     {
108         i_flags |= O_TRUNC;
109     }
110     if( !strcmp( p_access->psz_name, "-" ) )
111     {
112         p_access->p_sys->i_handle = STDOUT_FILENO;
113         msg_Dbg( p_access, "using stdout" );
114     }
115     else if( ( p_access->p_sys->i_handle =
116                open( p_access->psz_name, i_flags,
117                      S_IWRITE | S_IREAD | S_IRGRP | S_IROTH ) ) == -1 )
118     {
119         msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
120         free( p_access->p_sys );
121         return( VLC_EGENERIC );
122     }
123
124     p_access->pf_write = Write;
125     p_access->pf_read  = Read;
126     p_access->pf_seek  = Seek;
127
128     msg_Info( p_access, "Open: name:`%s'", p_access->psz_name );
129     return VLC_SUCCESS;
130 }
131
132 /*****************************************************************************
133  * Close: close the target
134  *****************************************************************************/
135 static void Close( vlc_object_t * p_this )
136 {
137     sout_access_out_t *p_access = (sout_access_out_t*)p_this;
138
139     if( strcmp( p_access->psz_name, "-" ) )
140     {
141         if( p_access->p_sys->i_handle )
142         {
143             close( p_access->p_sys->i_handle );
144         }
145     }
146     free( p_access->p_sys );
147
148     msg_Info( p_access, "Close" );
149 }
150
151 /*****************************************************************************
152  * Read: standard read on a file descriptor.
153  *****************************************************************************/
154 static int Read( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
155 {
156     if( strcmp( p_access->psz_name, "-" ) )
157     {
158         return read( p_access->p_sys->i_handle, p_buffer->p_buffer,
159                      p_buffer->i_size );
160     }
161     else
162     {
163         msg_Err( p_access, "cannot seek while using stdout" );
164         return VLC_EGENERIC;
165     }
166 }
167
168 /*****************************************************************************
169  * Write: standard write on a file descriptor.
170  *****************************************************************************/
171 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
172 {
173     size_t i_write = 0;
174
175     do
176     {
177         sout_buffer_t *p_next;
178
179         i_write += write( p_access->p_sys->i_handle, p_buffer->p_buffer,
180                           p_buffer->i_size );
181         p_next = p_buffer->p_next;
182         sout_BufferDelete( p_access->p_sout, p_buffer );
183         p_buffer = p_next;
184
185     } while( p_buffer );
186
187     return( i_write );
188 }
189
190 /*****************************************************************************
191  * Seek: seek to a specific location in a file
192  *****************************************************************************/
193 static int Seek( sout_access_out_t *p_access, off_t i_pos )
194 {
195     //msg_Dbg( p_access, "Seek: pos:"I64Fd, (int64_t)i_pos );
196
197     if( strcmp( p_access->psz_name, "-" ) )
198     {
199 #if defined( WIN32 ) && !defined( UNDER_CE )
200         return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
201 #else
202         return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
203 #endif
204     }
205     else
206     {
207         msg_Err( p_access, "cannot seek while using stdout" );
208         return VLC_EGENERIC;
209     }
210 }