file.c 7.33 KB
Newer Older
1 2 3
/*****************************************************************************
 * file.c
 *****************************************************************************
4
 * Copyright (C) 2001, 2002 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *          Eric Petit <titer@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29 30
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
31
#include <time.h>
32
#include <fcntl.h>
33
#include <errno.h>
34 35

#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
36 37 38
#include <vlc_sout.h>
#include <vlc_block.h>
#include <vlc_charset.h>
39
#include "vlc_strings.h"
40 41 42

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
gbazin's avatar
 
gbazin committed
43
#elif defined( WIN32 ) && !defined( UNDER_CE )
44
#   include <io.h>
gbazin's avatar
 
gbazin committed
45 46 47
#endif

/* For those platforms that don't use these */
48 49 50
#ifndef STDOUT_FILENO
#   define STDOUT_FILENO 1
#endif
51 52 53
#ifndef O_LARGEFILE
#   define O_LARGEFILE 0
#endif
54 55 56 57

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
58 59 60
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

Laurent Aimar's avatar
Laurent Aimar committed
61
#define SOUT_CFG_PREFIX "sout-file-"
zorglub's avatar
zorglub committed
62 63 64
#define APPEND_TEXT N_("Append to file")
#define APPEND_LONGTEXT N_( "Append to file if it exists instead " \
                            "of replacing it.")
Laurent Aimar's avatar
Laurent Aimar committed
65

66
vlc_module_begin();
67
    set_description( _("File stream output") );
68
    set_shortname( _("File" ));
69
    set_capability( "sout access", 50 );
zorglub's avatar
zorglub committed
70 71
    set_category( CAT_SOUT );
    set_subcategory( SUBCAT_SOUT_ACO );
72
    add_shortcut( "file" );
73
    add_shortcut( "stream" );
zorglub's avatar
zorglub committed
74 75
    add_bool( SOUT_CFG_PREFIX "append", 0, NULL, APPEND_TEXT,APPEND_LONGTEXT,
              VLC_TRUE );
76 77 78
    set_callbacks( Open, Close );
vlc_module_end();

79 80 81 82

/*****************************************************************************
 * Exported prototypes
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
83 84 85 86
static const char *ppsz_sout_options[] = {
    "append", NULL
};

87 88 89 90
static int Write( sout_access_out_t *, block_t * );
static int Seek ( sout_access_out_t *, off_t  );
static int Read ( sout_access_out_t *, block_t * );

91
struct sout_access_out_sys_t
92
{
gbazin's avatar
 
gbazin committed
93
    int i_handle;
94
};
95 96 97 98 99 100

/*****************************************************************************
 * Open: open the file
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
101
    sout_access_out_t   *p_access = (sout_access_out_t*)p_this;
102
    int                 i_flags;
Laurent Aimar's avatar
Laurent Aimar committed
103 104
    vlc_value_t         val;

105
    config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
106

107
    if( !p_access->psz_path )
108 109 110 111
    {
        msg_Err( p_access, "no file name specified" );
        return VLC_EGENERIC;
    }
Laurent Aimar's avatar
Laurent Aimar committed
112

113
    if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
114
    {
115
        return( VLC_ENOMEM );
116
    }
117 118 119 120 121 122

    i_flags = O_RDWR|O_CREAT|O_LARGEFILE;

    var_Get( p_access, SOUT_CFG_PREFIX "append", &val );
    i_flags |= val.b_bool ? O_APPEND : O_TRUNC;

123
    if( !strcmp( p_access->psz_path, "-" ) )
124
    {
dionoea's avatar
dionoea committed
125 126 127
#if defined(WIN32)
        setmode (STDOUT_FILENO, O_BINARY);
#endif
gbazin's avatar
 
gbazin committed
128
        p_access->p_sys->i_handle = STDOUT_FILENO;
129 130
        msg_Dbg( p_access, "using stdout" );
    }
131
    else
132
    {
133
        int fd;
134
        char *psz_tmp = str_format( p_access, p_access->psz_path );
135
        path_sanitize( psz_tmp );
136

137
        fd = utf8_open( psz_tmp, i_flags, 0666 );
138
        free( psz_tmp );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
139

140 141
        if( fd == -1 )
        {
142
            msg_Err( p_access, "cannot open `%s' (%s)", p_access->psz_path,
143 144 145 146 147
                     strerror( errno ) );
            free( p_access->p_sys );
            return( VLC_EGENERIC );
        }
        p_access->p_sys->i_handle = fd;
148 149
    }

gbazin's avatar
 
gbazin committed
150 151 152
    p_access->pf_write = Write;
    p_access->pf_read  = Read;
    p_access->pf_seek  = Seek;
153

154
    msg_Dbg( p_access, "file access output opened (`%s')", p_access->psz_path );
155 156 157 158 159

    /* Update pace control flag */
    if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
        p_access->p_sout->i_out_pace_nocontrol++;

160 161 162 163 164 165 166 167
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
gbazin's avatar
 
gbazin committed
168
    sout_access_out_t *p_access = (sout_access_out_t*)p_this;
169

170
    if( strcmp( p_access->psz_path, "-" ) )
171
    {
gbazin's avatar
 
gbazin committed
172
        if( p_access->p_sys->i_handle )
173
        {
gbazin's avatar
 
gbazin committed
174
            close( p_access->p_sys->i_handle );
175
        }
176
    }
177
    free( p_access->p_sys );
178

179 180 181 182
    /* Update pace control flag */
    if( p_access->psz_access && !strcmp( p_access->psz_access, "stream" ) )
        p_access->p_sout->i_out_pace_nocontrol--;

183
    msg_Dbg( p_access, "file access output closed" );
184 185 186 187 188
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
189
static int Read( sout_access_out_t *p_access, block_t *p_buffer )
gbazin's avatar
 
gbazin committed
190
{
191
    if( strcmp( p_access->psz_path, "-" ) )
gbazin's avatar
 
gbazin committed
192 193
    {
        return read( p_access->p_sys->i_handle, p_buffer->p_buffer,
194
                     p_buffer->i_buffer );
gbazin's avatar
 
gbazin committed
195
    }
196 197 198

    msg_Err( p_access, "cannot read while using stdout" );
    return VLC_EGENERIC;
gbazin's avatar
 
gbazin committed
199 200 201 202 203
}

/*****************************************************************************
 * Write: standard write on a file descriptor.
 *****************************************************************************/
204
static int Write( sout_access_out_t *p_access, block_t *p_buffer )
205 206 207
{
    size_t i_write = 0;

208
    while( p_buffer )
209
    {
210
        block_t *p_next = p_buffer->p_next;;
211

212 213 214
        i_write += write( p_access->p_sys->i_handle,
                          p_buffer->p_buffer, p_buffer->i_buffer );
        block_Release( p_buffer );
215

216 217
        p_buffer = p_next;
    }
218

219
    return i_write;
220 221 222 223 224
}

/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
225
static int Seek( sout_access_out_t *p_access, off_t i_pos )
226
{
227
    if( strcmp( p_access->psz_path, "-" ) )
228
    {
gbazin's avatar
 
gbazin committed
229 230 231 232 233
#if defined( WIN32 ) && !defined( UNDER_CE )
        return( _lseeki64( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
#else
        return( lseek( p_access->p_sys->i_handle, i_pos, SEEK_SET ) );
#endif
234 235 236 237 238 239
    }
    else
    {
        msg_Err( p_access, "cannot seek while using stdout" );
        return VLC_EGENERIC;
    }
240
}