file.c 7.03 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * file.c
 *****************************************************************************
 * Copyright (C) 2001, 2002 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
33
#include <errno.h>
34 35 36 37 38 39

#include <vlc/vlc.h>
#include <vlc/sout.h>

#ifdef HAVE_UNISTD_H
#   include <unistd.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
40
#elif defined( WIN32 ) && !defined( UNDER_CE )
41
#   include <io.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
42 43 44 45
#endif

/* For those platforms that don't use these */
#ifndef S_IRGRP
Gildas Bazin's avatar
 
Gildas Bazin committed
46
#   define S_IRGRP 0
Gildas Bazin's avatar
 
Gildas Bazin committed
47 48
#endif
#ifndef S_IROTH
Gildas Bazin's avatar
 
Gildas Bazin committed
49
#   define S_IROTH 0
50
#endif
51 52 53
#ifndef STDOUT_FILENO
#   define STDOUT_FILENO 1
#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-"
Clément Stenac's avatar
Clément Stenac 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 69
    set_capability( "sout access", 50 );
    add_shortcut( "file" );
70
    add_shortcut( "stream" );
Clément Stenac's avatar
Clément Stenac committed
71 72
    add_bool( SOUT_CFG_PREFIX "append", 0, NULL, APPEND_TEXT,APPEND_LONGTEXT,
              VLC_TRUE );
73 74 75
    set_callbacks( Open, Close );
vlc_module_end();

76 77 78 79

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

84 85 86 87
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 * );

88
struct sout_access_out_sys_t
89
{
Gildas Bazin's avatar
 
Gildas Bazin committed
90
    int i_handle;
91
};
92 93 94 95 96 97

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

    sout_ParseCfg( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
103

104 105 106 107 108
    if( !( p_access->p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
    {
        msg_Err( p_access, "out of memory" );
        return( VLC_EGENERIC );
    }
109

110 111 112 113 114
    if( !p_access->psz_name )
    {
        msg_Err( p_access, "no file name specified" );
        return VLC_EGENERIC;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
115
    i_flags = O_RDWR|O_CREAT;
Laurent Aimar's avatar
Laurent Aimar committed
116 117 118

    var_Get( p_access, SOUT_CFG_PREFIX "append", &val );
    if( val.b_bool )
119 120 121 122 123 124 125
    {
        i_flags |= O_APPEND;
    }
    else
    {
        i_flags |= O_TRUNC;
    }
126 127
    if( !strcmp( p_access->psz_name, "-" ) )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
128
        p_access->p_sys->i_handle = STDOUT_FILENO;
129 130
        msg_Dbg( p_access, "using stdout" );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
131
    else if( ( p_access->p_sys->i_handle =
132
               open( p_access->psz_name, i_flags,
Gildas Bazin's avatar
 
Gildas Bazin committed
133
                     S_IWRITE | S_IREAD | S_IRGRP | S_IROTH ) ) == -1 )
134
    {
135 136 137
        msg_Err( p_access, "cannot open `%s'", p_access->psz_name );
        free( p_access->p_sys );
        return( VLC_EGENERIC );
138 139
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
140 141 142
    p_access->pf_write = Write;
    p_access->pf_read  = Read;
    p_access->pf_seek  = Seek;
143

144
    msg_Dbg( p_access, "file access output opened (`%s')", p_access->psz_name );
145 146 147 148 149

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

150 151 152 153 154 155 156 157
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
158
    sout_access_out_t *p_access = (sout_access_out_t*)p_this;
159

160
    if( strcmp( p_access->psz_name, "-" ) )
161
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
162
        if( p_access->p_sys->i_handle )
163
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
164
            close( p_access->p_sys->i_handle );
165
        }
166
    }
167
    free( p_access->p_sys );
168

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

173
    msg_Dbg( p_access, "file access output closed" );
174 175 176 177 178
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
179
static int Read( sout_access_out_t *p_access, block_t *p_buffer )
Gildas Bazin's avatar
 
Gildas Bazin committed
180 181 182 183
{
    if( strcmp( p_access->psz_name, "-" ) )
    {
        return read( p_access->p_sys->i_handle, p_buffer->p_buffer,
184
                     p_buffer->i_buffer );
Gildas Bazin's avatar
 
Gildas Bazin committed
185
    }
186 187 188

    msg_Err( p_access, "cannot read while using stdout" );
    return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
189 190 191 192 193
}

/*****************************************************************************
 * Write: standard write on a file descriptor.
 *****************************************************************************/
194
static int Write( sout_access_out_t *p_access, block_t *p_buffer )
195 196 197
{
    size_t i_write = 0;

198
    while( p_buffer )
199
    {
200
        block_t *p_next = p_buffer->p_next;;
201

202 203 204
        i_write += write( p_access->p_sys->i_handle,
                          p_buffer->p_buffer, p_buffer->i_buffer );
        block_Release( p_buffer );
205

206 207
        p_buffer = p_next;
    }
208

209
    return i_write;
210 211 212 213 214
}

/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
215
static int Seek( sout_access_out_t *p_access, off_t i_pos )
216
{
217 218
    if( strcmp( p_access->psz_name, "-" ) )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
219 220 221 222 223
#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
224 225 226 227 228 229
    }
    else
    {
        msg_Err( p_access, "cannot seek while using stdout" );
        return VLC_EGENERIC;
    }
230
}