tcp.c 6.43 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 28
 *****************************************************************************/

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

#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
29
#include <vlc_access.h>
30

Clément Stenac's avatar
Clément Stenac committed
31
#include <vlc_network.h>
32 33 34 35

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

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

vlc_module_begin();
45
    set_shortname( _("TCP") );
46
    set_description( _("TCP input") );
47 48
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
Gildas Bazin's avatar
 
Gildas Bazin committed
49 50 51 52

    add_integer( "tcp-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
                 CACHING_LONGTEXT, VLC_TRUE );

53
    set_capability( "access2", 0 );
54 55 56 57 58 59 60 61 62
    add_shortcut( "tcp" );
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
struct access_sys_t
{
63
    int        fd;
64 65
};

66 67 68

static int Read( access_t *, uint8_t *, int );
static int Control( access_t *, int, va_list );
69 70 71 72 73 74

/*****************************************************************************
 * Open: open the socket
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
75 76
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys;
77

78 79
    char         *psz_dup = strdup(p_access->psz_path);
    char         *psz_parser = psz_dup;
80 81

    /* Parse server:port */
82
    if( *psz_parser == '[' )
83
    {
84 85 86
        psz_parser = strchr( psz_parser, ']' );
        if( psz_parser == NULL )
            psz_parser = psz_dup;
87
    }
88
    psz_parser = strchr( psz_parser, ':' );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
89

90
    if( psz_parser == NULL )
91
    {
92
        msg_Err( p_access, "missing port number : %s", psz_dup );
93 94 95 96
        free( psz_dup );
        return VLC_EGENERIC;
    }

97
    *psz_parser++ = '\0';
98

99
    /* Init p_access */
100 101 102 103
    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 ) );
104

105
    p_sys->fd = net_ConnectTCP( p_access, psz_dup, atoi( psz_parser ) );
106
    free( psz_dup );
Laurent Aimar's avatar
Laurent Aimar committed
107 108

    if( p_sys->fd < 0 )
109
    {
Laurent Aimar's avatar
Laurent Aimar committed
110
        free( p_sys );
111 112 113 114
        return VLC_EGENERIC;
    }

    /* Update default_pts to a suitable value for udp access */
115
    var_Create( p_access, "tcp-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
116 117 118 119 120 121 122 123 124

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
125 126
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys = p_access->p_sys;
127

Laurent Aimar's avatar
Laurent Aimar committed
128
    net_Close( p_sys->fd );
129 130 131 132 133 134
    free( p_sys );
}

/*****************************************************************************
 * Read: read on a file descriptor, checking b_die periodically
 *****************************************************************************/
135 136 137 138 139
static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_read;

140
    if( p_access->info.b_eof )
141 142
        return 0;

143 144
    i_read = net_Read( p_access, p_sys->fd, NULL, p_buffer, i_len,
                       VLC_FALSE );
145
    if( i_read == 0 )
146
        p_access->info.b_eof = VLC_TRUE;
147
    else if( i_read > 0 )
148
        p_access->info.i_pos += i_read;
149 150 151 152 153 154 155 156

    return i_read;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
157
{
158 159 160
    vlc_bool_t   *pb_bool;
    int          *pi_int;
    int64_t      *pi_64;
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = VLC_FALSE;
            break;
        case ACCESS_CAN_PAUSE:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = VLC_TRUE;    /* FIXME */
            break;
        case ACCESS_CAN_CONTROL_PACE:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = VLC_TRUE;    /* FIXME */
            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 * );
187
            *pi_64 = (int64_t)var_GetInteger( p_access, "tcp-caching" ) * I64C(1000);
188 189 190 191 192 193 194
            break;

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

195 196 197
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
198
        case ACCESS_SET_PRIVATE_ID_STATE:
199 200
            return VLC_EGENERIC;

201
        default:
202
            msg_Warn( p_access, "unimplemented query in control" );
203 204 205 206
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
207
}