tcp.c 6.5 KB
Newer Older
1
/*****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
2
 * tcp.c: TCP input module
3
 *****************************************************************************
4
 * Copyright (C) 2003-2004 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34
#include <vlc_access.h>
35

Clément Stenac's avatar
Clément Stenac committed
36
#include <vlc_network.h>
37 38 39 40

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
41
#define CACHING_TEXT N_("Caching value in ms")
42
#define CACHING_LONGTEXT N_( \
43
    "Caching value for TCP streams. This " \
Clément Stenac's avatar
Clément Stenac committed
44
    "value should be set in milliseconds." )
45 46 47 48 49

static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

vlc_module_begin();
50 51
    set_shortname( N_("TCP") );
    set_description( N_("TCP input") );
Clément Stenac's avatar
Clément Stenac committed
52 53
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
Gildas Bazin's avatar
 
Gildas Bazin committed
54 55

    add_integer( "tcp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
56
                 CACHING_LONGTEXT, true );
Gildas Bazin's avatar
 
Gildas Bazin committed
57

58
    set_capability( "access", 0 );
59 60 61 62 63 64 65 66 67
    add_shortcut( "tcp" );
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
struct access_sys_t
{
Laurent Aimar's avatar
Laurent Aimar committed
68
    int        fd;
69 70
};

Laurent Aimar's avatar
Laurent Aimar committed
71

72
static ssize_t Read( access_t *, uint8_t *, size_t );
Laurent Aimar's avatar
Laurent Aimar committed
73
static int Control( access_t *, int, va_list );
74 75 76 77 78 79

/*****************************************************************************
 * Open: open the socket
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
Laurent Aimar's avatar
Laurent Aimar committed
80 81
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys;
82

Laurent Aimar's avatar
Laurent Aimar committed
83 84
    char         *psz_dup = strdup(p_access->psz_path);
    char         *psz_parser = psz_dup;
85 86

    /* Parse server:port */
87
    if( *psz_parser == '[' )
88
    {
89 90 91
        psz_parser = strchr( psz_parser, ']' );
        if( psz_parser == NULL )
            psz_parser = psz_dup;
92
    }
93
    psz_parser = strchr( psz_parser, ':' );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
94

95
    if( psz_parser == NULL )
96
    {
97
        msg_Err( p_access, "missing port number : %s", psz_dup );
98 99 100 101
        free( psz_dup );
        return VLC_EGENERIC;
    }

102
    *psz_parser++ = '\0';
103

104
    /* Init p_access */
105 106 107 108
    access_InitFields( p_access ); \
    ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL ); \
    MALLOC_ERR( p_access->p_sys, access_sys_t ); \
    p_sys = p_access->p_sys; memset( p_sys, 0, sizeof( access_sys_t ) );
109

110
    p_sys->fd = net_ConnectTCP( p_access, psz_dup, atoi( psz_parser ) );
111
    free( psz_dup );
Laurent Aimar's avatar
Laurent Aimar committed
112 113

    if( p_sys->fd < 0 )
114
    {
Laurent Aimar's avatar
Laurent Aimar committed
115
        free( p_sys );
116 117 118 119
        return VLC_EGENERIC;
    }

    /* Update default_pts to a suitable value for udp access */
Laurent Aimar's avatar
Laurent Aimar committed
120
    var_Create( p_access, "tcp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
121 122 123 124 125 126 127 128 129

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
Laurent Aimar's avatar
Laurent Aimar committed
130 131
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys = p_access->p_sys;
132

Laurent Aimar's avatar
Laurent Aimar committed
133
    net_Close( p_sys->fd );
134 135 136 137 138 139
    free( p_sys );
}

/*****************************************************************************
 * Read: read on a file descriptor, checking b_die periodically
 *****************************************************************************/
140
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
Laurent Aimar's avatar
Laurent Aimar committed
141 142 143 144
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_read;

145
    if( p_access->info.b_eof )
Laurent Aimar's avatar
Laurent Aimar committed
146 147
        return 0;

148
    i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len,
149
                       false );
Laurent Aimar's avatar
Laurent Aimar committed
150
    if( i_read == 0 )
151
        p_access->info.b_eof = true;
Laurent Aimar's avatar
Laurent Aimar committed
152
    else if( i_read > 0 )
153
        p_access->info.i_pos += i_read;
Laurent Aimar's avatar
Laurent Aimar committed
154 155 156 157 158 159 160 161

    return i_read;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
162
{
163
    bool   *pb_bool;
Laurent Aimar's avatar
Laurent Aimar committed
164 165
    int          *pi_int;
    int64_t      *pi_64;
166

Laurent Aimar's avatar
Laurent Aimar committed
167 168 169 170 171
    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
172 173
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = false;
Laurent Aimar's avatar
Laurent Aimar committed
174 175
            break;
        case ACCESS_CAN_PAUSE:
176 177
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;    /* FIXME */
Laurent Aimar's avatar
Laurent Aimar committed
178 179
            break;
        case ACCESS_CAN_CONTROL_PACE:
180 181
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;    /* FIXME */
Laurent Aimar's avatar
Laurent Aimar committed
182 183 184 185 186 187 188 189 190 191
            break;

        /* */
        case ACCESS_GET_MTU:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = 0;
            break;

        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
192
            *pi_64 = (int64_t)var_GetInteger( p_access, "tcp-caching" ) * INT64_C(1000);
Laurent Aimar's avatar
Laurent Aimar committed
193 194 195 196 197 198 199
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
            /* Nothing to do */
            break;

200 201 202
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
203
        case ACCESS_SET_PRIVATE_ID_STATE:
204
        case ACCESS_GET_CONTENT_TYPE:
205 206
            return VLC_EGENERIC;

Laurent Aimar's avatar
Laurent Aimar committed
207
        default:
208
            msg_Warn( p_access, "unimplemented query in control" );
Laurent Aimar's avatar
Laurent Aimar committed
209 210 211 212
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
213
}