]> git.sesse.net Git - vlc/blob - modules/access_output/file.c
Added stream output. (common work with titer).
[vlc] / modules / access_output / file.c
1 /*****************************************************************************
2  * file.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 VideoLAN
5  * $Id: file.c,v 1.1 2002/12/14 21:32:41 fenrir 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( _MSC_VER ) && defined( _WIN32 ) && !defined( UNDER_CE )
42 #   include <io.h>
43 #endif
44
45 /*****************************************************************************
46  * Exported prototypes
47  *****************************************************************************/
48 static int     Open   ( vlc_object_t * );
49 static void    Close  ( vlc_object_t * );
50
51 static int     Write( sout_instance_t *, sout_buffer_t * );
52 static int     Seek( sout_instance_t *, off_t  );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin();
58     set_description( _("File stream ouput") );
59     set_capability( "sout access", 50 );
60     add_shortcut( "file" );
61     set_callbacks( Open, Close );
62 vlc_module_end();
63
64 typedef struct sout_access_data_s
65 {
66     FILE *p_file;
67
68 } sout_access_data_t;
69
70 /*****************************************************************************
71  * Open: open the file
72  *****************************************************************************/
73 static int Open( vlc_object_t *p_this )
74 {
75     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
76     sout_access_data_t  *p_access;
77     char *              psz_name = p_sout->psz_name;
78
79     p_access = malloc( sizeof( sout_access_data_t ) );
80
81     if( !( p_access->p_file = fopen( psz_name, "wb" ) ) )
82     {
83         msg_Err( p_sout, "cannot open `%s'", psz_name );
84         free( p_access );
85         return( -1 );
86     }
87
88     p_sout->i_method        = SOUT_METHOD_FILE;
89     p_sout->p_access_data   = p_access;
90     p_sout->pf_write        = Write;
91     p_sout->pf_seek         = Seek;
92
93     msg_Info( p_sout, "Open: name:`%s'", psz_name );
94     return VLC_SUCCESS;
95 }
96
97 /*****************************************************************************
98  * Close: close the target
99  *****************************************************************************/
100 static void Close( vlc_object_t * p_this )
101 {
102     sout_instance_t     *p_sout = (sout_instance_t*)p_this;
103     sout_access_data_t  *p_access = (sout_access_data_t*)p_sout->p_access_data;
104
105     if( p_access->p_file )
106     {
107         fclose( p_access->p_file );
108     }
109
110     msg_Info( p_sout, "Close" );
111 }
112
113 /*****************************************************************************
114  * Read: standard read on a file descriptor.
115  *****************************************************************************/
116 static int Write( sout_instance_t *p_sout, sout_buffer_t *p_buffer )
117 {
118     sout_access_data_t  *p_access = (sout_access_data_t*)p_sout->p_access_data;
119     size_t i_write = 0;
120
121     do
122     {
123         sout_buffer_t *p_next;
124
125         i_write += fwrite( p_buffer->p_buffer, 1, p_buffer->i_size,
126                            p_access->p_file );
127         p_next = p_buffer->p_next;
128         sout_BufferDelete( p_sout, p_buffer );
129         p_buffer = p_next;
130
131     } while( p_buffer );
132
133     msg_Dbg( p_sout, "Write: len:%d", (uint32_t)i_write );
134
135     return( i_write );
136 }
137
138 /*****************************************************************************
139  * Seek: seek to a specific location in a file
140  *****************************************************************************/
141 static int Seek( sout_instance_t *p_sout, off_t i_pos )
142 {
143
144     sout_access_data_t  *p_access = (sout_access_data_t*)p_sout->p_access_data;
145
146     msg_Dbg( p_sout, "Seek: pos:%lld", (int64_t)i_pos );
147     return( fseek( p_access->p_file, i_pos, SEEK_SET ) );
148 }
149
150
151