access.c 11.1 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * access.c: Real rtsp input
 *****************************************************************************
 * Copyright (C) 2005 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Gildas Bazin <gbazin@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
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
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

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

Clément Stenac's avatar
Clément Stenac committed
36
#include <vlc_network.h>
37 38 39 40 41 42 43 44 45 46 47
#include "rtsp.h"
#include "real.h"

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

#define CACHING_TEXT N_("Caching value (ms)")
#define CACHING_LONGTEXT N_( \
48
    "Caching value for RTSP streams. This " \
Clément Stenac's avatar
Clément Stenac committed
49
    "value should be set in milliseconds." )
50 51

vlc_module_begin();
52 53
    set_description( N_("Real RTSP") );
    set_shortname( N_("Real RTSP") );
54 55 56
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
    add_integer( "realrtsp-caching", 3000, NULL,
57
                 CACHING_TEXT, CACHING_LONGTEXT, true );
58
    set_capability( "access", 10 );
59 60 61
    set_callbacks( Open, Close );
    add_shortcut( "realrtsp" );
    add_shortcut( "rtsp" );
62
    add_shortcut( "pnm" );
63 64 65 66 67 68 69 70 71 72 73 74
vlc_module_end();


/*****************************************************************************
 * Exported prototypes
 *****************************************************************************/
static block_t *BlockRead( access_t * );
static int     Seek( access_t *, int64_t );
static int     Control( access_t *, int, va_list );

struct access_sys_t
{
75 76
    bool b_seekable;
    bool b_pace_control;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    rtsp_client_t *p_rtsp;

    int fd;

    block_t *p_header;
};

/*****************************************************************************
 * Network wrappers
 *****************************************************************************/
static int RtspConnect( void *p_userdata, char *psz_server, int i_port )
{
    access_t *p_access = (access_t *)p_userdata;
    access_sys_t *p_sys = p_access->p_sys;

    /* Open connection */
94
    p_sys->fd = net_ConnectTCP( p_access, psz_server, i_port );
95 96 97
    if( p_sys->fd < 0 )
    {
        msg_Err( p_access, "cannot connect to %s:%d", psz_server, i_port );
98
        intf_UserFatal( p_access, false, _("Connection failed"),
99
                        _("VLC could not connect to \"%s:%d\"."), psz_server, i_port );
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}

static int RtspDisconnect( void *p_userdata )
{
    access_t *p_access = (access_t *)p_userdata;
    access_sys_t *p_sys = p_access->p_sys;

    net_Close( p_sys->fd );
    return VLC_SUCCESS;
}

static int RtspRead( void *p_userdata, uint8_t *p_buffer, int i_buffer )
{
    access_t *p_access = (access_t *)p_userdata;
    access_sys_t *p_sys = p_access->p_sys;

120
    return net_Read( p_access, p_sys->fd, 0, p_buffer, i_buffer, true );
121 122 123 124 125 126 127 128 129 130 131 132 133 134
}

static int RtspReadLine( void *p_userdata, uint8_t *p_buffer, int i_buffer )
{
    access_t *p_access = (access_t *)p_userdata;
    access_sys_t *p_sys = p_access->p_sys;

    char *psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, 0 );

    //fprintf(stderr, "ReadLine: %s\n", psz);

    if( psz ) strncpy( (char *)p_buffer, psz, i_buffer );
    else *p_buffer = 0;

135
    free( psz );
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    return 0;
}

static int RtspWrite( void *p_userdata, uint8_t *p_buffer, int i_buffer )
{
    access_t *p_access = (access_t *)p_userdata;
    access_sys_t *p_sys = p_access->p_sys;

    //fprintf(stderr, "Write: %s", p_buffer);

    net_Printf( VLC_OBJECT(p_access), p_sys->fd, 0, "%s", p_buffer );

    return 0;
}

/*****************************************************************************
 * Open: open the rtsp connection
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    access_t *p_access = (access_t *)p_this;
    access_sys_t *p_sys;
    char *psz_server = 0;
    int i_result;

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
161
    if( !p_access->psz_access || (
162
        strncmp( p_access->psz_access, "rtsp", 4 ) &&
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
163 164 165 166 167 168
        strncmp( p_access->psz_access, "pnm", 3 )  &&
        strncmp( p_access->psz_access, "realrtsp", 8 ) ))
    {
            return VLC_EGENERIC;
    }

169 170 171 172 173 174 175
    p_access->pf_read = NULL;
    p_access->pf_block = BlockRead;
    p_access->pf_seek = Seek;
    p_access->pf_control = Control;
    p_access->info.i_update = 0;
    p_access->info.i_size = 0;
    p_access->info.i_pos = 0;
176
    p_access->info.b_eof = false;
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    p_access->info.i_title = 0;
    p_access->info.i_seekpoint = 0;
    p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
    p_sys->p_rtsp = malloc( sizeof( rtsp_client_t) );

    p_sys->p_header = 0;
    p_sys->p_rtsp->p_userdata = p_access;
    p_sys->p_rtsp->pf_connect = RtspConnect;
    p_sys->p_rtsp->pf_disconnect = RtspDisconnect;
    p_sys->p_rtsp->pf_read = RtspRead;
    p_sys->p_rtsp->pf_read_line = RtspReadLine;
    p_sys->p_rtsp->pf_write = RtspWrite;

    i_result = rtsp_connect( p_sys->p_rtsp, p_access->psz_path, 0 );
    if( i_result )
    {
        msg_Dbg( p_access, "could not connect to: %s", p_access->psz_path );
        free( p_sys->p_rtsp );
        p_sys->p_rtsp = 0;
        goto error;
    }

    msg_Dbg( p_access, "rtsp connected" );

    /* looking for server type */
    if( rtsp_search_answers( p_sys->p_rtsp, "Server" ) )
        psz_server = strdup( rtsp_search_answers( p_sys->p_rtsp, "Server" ) );
    else
    {
        if( rtsp_search_answers( p_sys->p_rtsp, "RealChallenge1" ) )
            psz_server = strdup("Real");
        else
            psz_server = strdup("unknown");
    }

    if( strstr( psz_server, "Real" ) || strstr( psz_server, "Helix" ) )
    {
        uint32_t bandwidth = 10485800;
        rmff_header_t *h;

        msg_Dbg( p_access, "found a real/helix rtsp server" );

        if( !(h = real_setup_and_get_header( p_sys->p_rtsp, bandwidth )) )
        {
            /* Check if we got a redirect */
            if( rtsp_search_answers( p_sys->p_rtsp, "Location" ) )
            {
                msg_Dbg( p_access, "redirect: %s",
                         rtsp_search_answers(p_sys->p_rtsp, "Location") );
                msg_Warn( p_access, "redirect not supported" );
                goto error;
            }


            msg_Err( p_access, "rtsp session can not be established" );
232
            intf_UserFatal( p_access, false, _("Session failed"),
233
                    _("The requested RTSP session could not be established.") );
234 235 236 237 238
            goto error;
        }

        p_sys->p_header = block_New( p_access, 4096 );
        p_sys->p_header->i_buffer =
239
            rmff_dump_header( h, (char *)p_sys->p_header->p_buffer, 1024 );
240
        rmff_free_header( h );
241 242 243
    }
    else
    {
244
        msg_Warn( p_access, "only real/helix rtsp servers supported for now" );
245 246 247 248 249 250 251
        goto error;
    }

    /* Update default_pts to a suitable value for file access */
    var_Create( p_access, "realrtsp-caching",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);

252
    free( psz_server );
253 254 255
    return VLC_SUCCESS;

 error:
256
    free( psz_server );
257 258 259 260 261 262 263 264 265 266 267 268 269
    Close( p_this );
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Close: close the target
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
    access_t     *p_access = (access_t*)p_this;
    access_sys_t *p_sys = p_access->p_sys;

    if( p_sys->p_rtsp ) rtsp_close( p_sys->p_rtsp );
270
    free( p_sys->p_rtsp );
271 272 273 274 275 276 277 278 279 280
    free( p_sys );
}

/*****************************************************************************
 * Read: standard read on a file descriptor.
 *****************************************************************************/
static block_t *BlockRead( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    block_t *p_block;
281 282
    rmff_pheader_t pheader;
    int i_size;
283 284 285 286 287 288 289 290

    if( p_sys->p_header )
    {
        p_block = p_sys->p_header;
        p_sys->p_header = 0;
        return p_block;
    }

291 292 293 294 295
    i_size = real_get_rdt_chunk_header( p_access->p_sys->p_rtsp, &pheader );
    if( i_size <= 0 ) return 0;

    p_block = block_New( p_access, i_size );
    p_block->i_buffer = real_get_rdt_chunk( p_access->p_sys->p_rtsp, &pheader,
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
                                            &p_block->p_buffer );

    return p_block;
}

/*****************************************************************************
 * Seek: seek to a specific location in a file
 *****************************************************************************/
static int Seek( access_t *p_access, int64_t i_pos )
{
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
314
    bool   *pb_bool;
315 316 317 318 319 320 321 322
    int          *pi_int;
    int64_t      *pi_64;

    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
323 324
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = false;//p_sys->b_seekable;
325 326 327
            break;

        case ACCESS_CAN_PAUSE:
328 329
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = false;
330 331 332
            break;

        case ACCESS_CAN_CONTROL_PACE:
333 334
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;//p_sys->b_pace_control;
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
            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 * );
            *pi_64 = var_GetInteger( p_access, "realrtsp-caching" ) * 1000;
            break;

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

        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
        case ACCESS_SET_PRIVATE_ID_STATE:
        case ACCESS_GET_META:
358
        case ACCESS_GET_CONTENT_TYPE:
359 360 361 362 363 364 365 366 367
            return VLC_EGENERIC;

        default:
            msg_Warn( p_access, "unimplemented query in control" );
            return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}