mmsh.c 22.5 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * mmsh.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
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>

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

32 33
#include "vlc_playlist.h"

34 35 36 37 38 39 40
#include "network.h"
#include "asf.h"
#include "buffer.h"

#include "mms.h"
#include "mmsh.h"

41 42 43 44 45
/* TODO:
 *  - http_proxy
 *  - authentication
 */

46 47 48
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
49 50
int  E_(MMSHOpen)  ( access_t * );
void E_(MMSHClose) ( access_t * );
Laurent Aimar's avatar
Laurent Aimar committed
51

52 53 54 55
static int  Read( access_t *, uint8_t *, int );
static int  ReadRedirect( access_t *, uint8_t *, int );
static int  Seek( access_t *, int64_t );
static int  Control( access_t *, int, va_list );
Laurent Aimar's avatar
Laurent Aimar committed
56

57 58 59 60
static int  Describe( access_t  *, char **ppsz_location );
static int  Start( access_t *, int64_t );
static void Stop( access_t * );
static int  GetPacket( access_t *, chunk_t * );
61 62 63 64

/****************************************************************************
 * Open: connect to ftp server and ask for file
 ****************************************************************************/
65
int E_(MMSHOpen)( access_t *p_access )
66 67
{
    access_sys_t    *p_sys;
68
    char            *psz_location = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
69

70 71
    /* init p_sys */

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    /* Set up p_access */
    p_access->pf_read = Read;
    p_access->pf_block = NULL;
    p_access->pf_control = Control;
    p_access->pf_seek = Seek;
    p_access->info.i_update = 0;
    p_access->info.i_size = 0;
    p_access->info.i_pos = 0;
    p_access->info.b_eof = VLC_FALSE;
    p_access->info.i_title = 0;
    p_access->info.i_seekpoint = 0;
    p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
    memset( p_sys, 0, sizeof( access_sys_t ) );
    p_sys->i_proto= MMS_PROTO_HTTP;
    p_sys->fd     = -1;
87
    p_sys->i_start= 0;
88 89

    /* open a tcp connection */
90 91
    vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
    if( p_sys->url.psz_host == NULL || *p_sys->url.psz_host == '\0' )
92
    {
93
        msg_Err( p_access, "invalid host" );
94 95 96
        vlc_UrlClean( &p_sys->url );
        free( p_sys );
        return VLC_EGENERIC;
97
    }
98 99
    if( p_sys->url.i_port <= 0 )
        p_sys->url.i_port = 80;
100

101
    if( Describe( p_access, &psz_location ) )
102
    {
103 104 105
        vlc_UrlClean( &p_sys->url );
        free( p_sys );
        return VLC_EGENERIC;
106
    }
107 108
    /* Handle redirection */
    if( psz_location && *psz_location )
109
    {
110
        playlist_t * p_playlist = vlc_object_find( p_access, VLC_OBJECT_PLAYLIST, FIND_PARENT );
111

112
        msg_Dbg( p_access, "redirection to %s", psz_location );
113 114

        if( !p_playlist )
115
        {
116
            msg_Err( p_access, "redirection failed: can't find playlist" );
117 118
            free( psz_location );
            return VLC_EGENERIC;
119
        }
120 121 122 123 124 125
        p_playlist->pp_items[p_playlist->i_index]->b_autodeletion = VLC_TRUE;
        playlist_Add( p_playlist, psz_location, psz_location,
                      PLAYLIST_INSERT | PLAYLIST_GO,
                      p_playlist->i_index + 1 );
        vlc_object_release( p_playlist );

126 127
        free( psz_location );

128
        p_access->pf_read = ReadRedirect;
129 130 131
        return VLC_SUCCESS;
    }

132
    /* Start playing */
133
    if( Start( p_access, 0 ) )
134
    {
135
        msg_Err( p_access, "cannot start stream" );
136 137 138 139
        free( p_sys->p_header );
        vlc_UrlClean( &p_sys->url );
        free( p_sys );
        return VLC_EGENERIC;
140 141
    }

142
    if( !p_sys->b_broadcast )
143
    {
144
        p_access->info.i_size = p_sys->asfh.i_file_size;
145 146
    }

Laurent Aimar's avatar
Laurent Aimar committed
147
    return VLC_SUCCESS;
148 149 150 151 152
}

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
153 154 155 156 157 158 159 160 161 162 163 164
void E_( MMSHClose )( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;

    Stop( p_access );
    free( p_sys );
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
165
{
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    access_sys_t *p_sys = p_access->p_sys;
    vlc_bool_t   *pb_bool;
    int          *pi_int;
    int64_t      *pi_64;

    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = !p_sys->b_broadcast;
            break;
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = VLC_FALSE;
            break;
184

185 186 187 188 189
        /* */
        case ACCESS_GET_MTU:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = 3 * p_sys->asfh.i_min_data_packet_size;
            break;
190

191 192
        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
193
            *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * I64C(1000);
194
            break;
195

196 197 198 199 200
        /* */
        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
201
        case ACCESS_SET_PRIVATE_ID_STATE:
202
            return VLC_EGENERIC;
203

204
        default:
205
            msg_Warn( p_access, "unimplemented query in control" );
206 207 208 209
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
210 211 212 213 214
}

/*****************************************************************************
 * Seek: try to go at the right place
 *****************************************************************************/
215
static int Seek( access_t *p_access, int64_t i_pos )
216
{
217
    access_sys_t *p_sys = p_access->p_sys;
218 219 220 221
    chunk_t      ck;
    off_t        i_offset;
    off_t        i_packet;

222 223
    msg_Dbg( p_access, "seeking to "I64Fd, i_pos );

224 225 226
    i_packet = ( i_pos - p_sys->i_header ) / p_sys->asfh.i_min_data_packet_size;
    i_offset = ( i_pos - p_sys->i_header ) % p_sys->asfh.i_min_data_packet_size;

227 228
    Stop( p_access );
    Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
229

230
    while( !p_access->b_die )
231
    {
232
        if( GetPacket( p_access, &ck ) )
233 234 235 236 237
            break;

        /* skip headers */
        if( ck.i_type != 0x4824 )
            break;
238 239

        msg_Warn( p_access, "skipping header" );
240 241
    }

242 243
    p_access->info.i_pos = i_pos;
    p_access->info.b_eof = VLC_FALSE;
244 245
    p_sys->i_packet_used += i_offset;

246
    return VLC_SUCCESS;
247 248
}

249 250 251
/*****************************************************************************
 * Read:
 *****************************************************************************/
252
static int ReadRedirect( access_t *p_access, uint8_t *p, int i_len )
253 254 255 256
{
    return 0;
}

257 258 259
/*****************************************************************************
 * Read:
 *****************************************************************************/
260
static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
261
{
262
    access_sys_t *p_sys = p_access->p_sys;
263 264 265
    size_t       i_copy;
    size_t       i_data = 0;

266 267 268
    if( p_access->info.b_eof )
        return 0;

269 270
    while( i_data < i_len )
    {
271
        if( p_access->info.i_pos < p_sys->i_start + p_sys->i_header )
Laurent Aimar's avatar
Laurent Aimar committed
272
        {
273
            int i_offset = p_access->info.i_pos - p_sys->i_start;
274 275
            i_copy = __MIN( p_sys->i_header - i_offset, i_len - i_data );
            memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
Laurent Aimar's avatar
Laurent Aimar committed
276 277

            i_data += i_copy;
278
            p_access->info.i_pos += i_copy;
Laurent Aimar's avatar
Laurent Aimar committed
279 280
        }
        else if( p_sys->i_packet_used < p_sys->i_packet_length )
281
        {
282 283
            i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
                            i_len - i_data );
284 285 286 287 288 289 290

            memcpy( &p_buffer[i_data],
                    &p_sys->p_packet[p_sys->i_packet_used],
                    i_copy );

            i_data += i_copy;
            p_sys->i_packet_used += i_copy;
291
            p_access->info.i_pos += i_copy;
292
        }
293
        else if( p_sys->i_packet_length > 0 &&
294 295
                 (int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size )
        {
296 297
            i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
                            i_len - i_data );
298 299 300 301 302

            memset( &p_buffer[i_data], 0, i_copy );

            i_data += i_copy;
            p_sys->i_packet_used += i_copy;
303
            p_access->info.i_pos += i_copy;
304 305 306 307
        }
        else
        {
            chunk_t ck;
308
            if( GetPacket( p_access, &ck ) )
309
            {
310 311 312 313
                if( ck.i_type == 0x4524 && ck.i_sequence != 0 && p_sys->b_broadcast )
                {
                    char *psz_location = NULL;

314
                    p_sys->i_start = p_access->info.i_pos;
315

316 317
                    msg_Dbg( p_access, "stoping the stream" );
                    Stop( p_access );
318

319 320
                    msg_Dbg( p_access, "describe the stream" );
                    if( Describe( p_access, &psz_location ) )
321
                    {
322 323 324
                        msg_Err( p_access, "describe failed" );
                        p_access->info.b_eof = VLC_TRUE;
                        return 0;
325
                    }
326
                    if( Start( p_access, 0 ) )
327
                    {
328 329 330
                        msg_Err( p_access, "Start failed" );
                        p_access->info.b_eof = VLC_TRUE;
                        return 0;
331 332 333 334
                    }
                }
                else
                {
335 336
                    p_access->info.b_eof = VLC_TRUE;
                    return 0;
337
                }
338
            }
339
            if( ck.i_type != 0x4424 )
Laurent Aimar's avatar
Laurent Aimar committed
340 341 342 343
            {
                p_sys->i_packet_used = 0;
                p_sys->i_packet_length = 0;
            }
344 345 346 347 348 349
        }
    }

    return( i_data );
}

350 351 352
/*****************************************************************************
 * Describe:
 *****************************************************************************/
353
static int Describe( access_t  *p_access, char **ppsz_location )
354
{
355
    access_sys_t *p_sys = p_access->p_sys;
356 357 358 359 360 361 362 363 364 365 366 367 368
    char         *psz_location = NULL;
    char         *psz;
    int          i_code;

    /* Reinit context */
    p_sys->b_broadcast = VLC_TRUE;
    p_sys->i_request_context = 1;
    p_sys->i_packet_sequence = 0;
    p_sys->i_packet_used = 0;
    p_sys->i_packet_length = 0;
    p_sys->p_packet = NULL;
    E_( GenerateGuid )( &p_sys->guid );

369
    if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
370 371
                                            p_sys->url.i_port ) ) < 0 )
    {
372
        msg_Err( p_access, "cannot connect to%s:%d", p_sys->url.psz_host, p_sys->url.i_port );
373 374 375 376
        goto error;
    }

    /* send first request */
377
    net_Printf( VLC_OBJECT(p_access), p_sys->fd,
378 379 380 381 382 383 384 385 386 387 388 389
                "GET %s HTTP/1.0\r\n"
                "Accept: */*\r\n"
                "User-Agent: NSPlayer/4.1.0.3856\r\n"
                "Host: %s:%d\r\n"
                "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=0:0,request-context=%d,max-duration=0\r\n"
                "Pragma: xClientGUID={"GUID_FMT"}\r\n"
                "Connection: Close\r\n",
                ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
                p_sys->url.psz_host, p_sys->url.i_port,
                p_sys->i_request_context++,
                GUID_PRINT( p_sys->guid ) );

390
    if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
391
    {
392
        msg_Err( p_access, "failed to send request" );
393 394 395 396
        goto error;
    }

    /* Receive the http header */
397
    if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
398
    {
399
        msg_Err( p_access, "failed to read answer" );
400 401 402 403
        goto error;
    }
    if( strncmp( psz, "HTTP/1.", 7 ) )
    {
404
        msg_Err( p_access, "invalid HTTP reply '%s'", psz );
405 406 407 408 409 410
        free( psz );
        goto error;
    }
    i_code = atoi( &psz[9] );
    if( i_code >= 400 )
    {
411
        msg_Err( p_access, "error: %s", psz );
412 413 414 415
        free( psz );
        goto error;
    }

416
    msg_Dbg( p_access, "HTTP reply '%s'", psz );
417 418 419
    free( psz );
    for( ;; )
    {
420
        char *psz = net_Gets( p_access, p_sys->fd );
421 422 423 424
        char *p;

        if( psz == NULL )
        {
425
            msg_Err( p_access, "failed to read answer" );
426 427 428 429 430 431 432 433 434 435 436
            goto error;
        }

        if( *psz == '\0' )
        {
            free( psz );
            break;
        }

        if( ( p = strchr( psz, ':' ) ) == NULL )
        {
437
            msg_Err( p_access, "malformed header line: %s", psz );
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
            free( psz );
            goto error;
        }
        *p++ = '\0';
        while( *p == ' ' ) p++;

        /* FIXME FIXME test Content-Type to see if it's a plain stream or an
         * asx FIXME */
        if( !strcasecmp( psz, "Pragma" ) )
        {
            if( strstr( p, "features" ) )
            {
                /* FIXME, it is a bit badly done here ..... */
                if( strstr( p, "broadcast" ) )
                {
453
                    msg_Dbg( p_access, "stream type = broadcast" );
454 455 456 457
                    p_sys->b_broadcast = VLC_TRUE;
                }
                else if( strstr( p, "seekable" ) )
                {
458
                    msg_Dbg( p_access, "stream type = seekable" );
459 460 461 462
                    p_sys->b_broadcast = VLC_FALSE;
                }
                else
                {
463
                    msg_Warn( p_access, "unknow stream types (%s)", p );
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
                    p_sys->b_broadcast = VLC_FALSE;
                }
            }
        }
        else if( !strcasecmp( psz, "Location" ) )
        {
            psz_location = strdup( p );
        }

        free( psz );
    }

    /* Handle the redirection */
    if( ( i_code == 301 || i_code == 302 ||
          i_code == 303 || i_code == 307 ) &&
        psz_location && *psz_location )
    {
481
        msg_Dbg( p_access, "redirection to %s", psz_location );
482 483 484 485 486 487 488 489 490 491 492 493
        net_Close( p_sys->fd ); p_sys->fd = -1;

        *ppsz_location = psz_location;
        return VLC_SUCCESS;
    }

    /* Read the asf header */
    p_sys->i_header = 0;
    p_sys->p_header = NULL;
    for( ;; )
    {
        chunk_t ck;
494
        if( GetPacket( p_access, &ck ) ||
495 496 497 498 499 500 501 502 503 504 505 506 507
            ck.i_type != 0x4824 )
        {
            break;
        }

        if( ck.i_data > 0 )
        {
            p_sys->i_header += ck.i_data;
            p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header );
            memcpy( &p_sys->p_header[p_sys->i_header - ck.i_data],
                    ck.p_data, ck.i_data );
        }
    }
508
    msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
509 510
    if( p_sys->i_header <= 0 )
    {
511
        msg_Err( p_access, "header size == 0" );
512 513 514 515 516 517 518 519 520 521 522 523
        goto error;
    }
    /* close this connection */
    net_Close( p_sys->fd ); p_sys->fd = -1;

    /* *** parse header and get stream and their id *** */
    /* get all streams properties,
     *
     * TODO : stream bitrates properties(optional)
     *        and bitrate mutual exclusion(optional) */
    E_( asf_HeaderParse )( &p_sys->asfh,
                           p_sys->p_header, p_sys->i_header );
524
    msg_Dbg( p_access, "packet count=%lld packet size=%d",
525 526 527 528
             p_sys->asfh.i_data_packets_count,
             p_sys->asfh.i_min_data_packet_size );

    E_( asf_StreamSelect)( &p_sys->asfh,
529 530 531 532
                           var_CreateGetInteger( p_access, "mms-maxbitrate" ),
                           var_CreateGetInteger( p_access, "mms-all" ),
                           var_CreateGetInteger( p_access, "audio" ),
                           var_CreateGetInteger( p_access, "video" ) );
533 534 535 536 537 538 539 540 541 542 543 544

    return VLC_SUCCESS;

error:
    if( p_sys->fd > 0 )
    {
        net_Close( p_sys->fd  );
        p_sys->fd = -1;
    }
    return VLC_EGENERIC;
}

Laurent Aimar's avatar
Laurent Aimar committed
545 546 547
/*****************************************************************************
 *
 *****************************************************************************/
548
static int Start( access_t *p_access, off_t i_pos )
549
{
550
    access_sys_t *p_sys = p_access->p_sys;
551 552 553
    int  i_streams = 0;
    int  i;
    char *psz;
554

555
    msg_Dbg( p_access, "starting stream" );
556

557
    if( ( p_sys->fd = net_OpenTCP( p_access, p_sys->url.psz_host,
558
                                            p_sys->url.i_port ) ) < 0 )
559 560
    {
        /* should not occur */
561
        msg_Err( p_access, "cannot connect to the server" );
562 563
        return VLC_EGENERIC;
    }
564 565 566 567 568 569 570 571 572 573 574

    for( i = 1; i < 128; i++ )
    {
        if( p_sys->asfh.stream[i].i_selected )
        {
            i_streams++;
        }
    }

    if( i_streams <= 0 )
    {
575
        msg_Err( p_access, "no stream selected" );
576 577
        return VLC_EGENERIC;
    }
578
    net_Printf( VLC_OBJECT(p_access), p_sys->fd,
579 580 581 582 583 584
                "GET %s HTTP/1.0\r\n"
                "Accept: */*\r\n"
                "User-Agent: NSPlayer/4.1.0.3856\r\n"
                "Host: %s:%d\r\n",
                ( p_sys->url.psz_path == NULL || *p_sys->url.psz_path == '\0' ) ? "/" : p_sys->url.psz_path,
                p_sys->url.psz_host, p_sys->url.i_port );
585 586
    if( p_sys->b_broadcast )
    {
587
        net_Printf( VLC_OBJECT(p_access), p_sys->fd,
588 589
                    "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
                    p_sys->i_request_context++ );
590 591 592
    }
    else
    {
593
        net_Printf( VLC_OBJECT(p_access), p_sys->fd,
594 595 596 597
                    "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=0\r\n",
                    (uint32_t)((i_pos >> 32)&0xffffffff),
                    (uint32_t)(i_pos&0xffffffff),
                    p_sys->i_request_context++ );
598
    }
599
    net_Printf( VLC_OBJECT(p_access), p_sys->fd,
600 601 602 603 604 605
                "Pragma: xPlayStrm=1\r\n"
                "Pragma: xClientGUID={"GUID_FMT"}\r\n"
                "Pragma: stream-switch-count=%d\r\n"
                "Pragma: stream-switch-entry=",
                GUID_PRINT( p_sys->guid ),
                i_streams);
606

607
    for( i = 1; i < 128; i++ )
608
    {
609
        if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
610
        {
611 612
            int i_select = 2;
            if( p_sys->asfh.stream[i].i_selected )
613
            {
614
                i_select = 0;
615 616
            }

617
            net_Printf( VLC_OBJECT(p_access), p_sys->fd,
618
                        "ffff:%d:%d ", i, i_select );
619 620
        }
    }
621 622
    net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" );
    net_Printf( VLC_OBJECT(p_access), p_sys->fd, "Connection: Close\r\n" );
623

624
    if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, "\r\n" ) < 0 )
625
    {
626
        msg_Err( p_access, "failed to send request" );
627 628 629
        return VLC_EGENERIC;
    }

630
    if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd ) ) == NULL )
631
    {
632
        msg_Err( p_access, "cannot read data" );
633 634
        return VLC_EGENERIC;
    }
635
    if( atoi( &psz[9] ) >= 400 )
636
    {
637
        msg_Err( p_access, "error: %s", psz );
638 639
        free( psz );
        return VLC_EGENERIC;
640
    }
641
    msg_Dbg( p_access, "HTTP reply '%s'", psz );
642 643 644 645
    free( psz );

    /* FIXME check HTTP code */
    for( ;; )
646
    {
647
        char *psz = net_Gets( p_access, p_sys->fd );
648 649
        if( psz == NULL )
        {
650
            msg_Err( p_access, "cannot read data" );
651 652 653 654 655 656 657
            return VLC_EGENERIC;
        }
        if( *psz == '\0' )
        {
            free( psz );
            break;
        }
658
        msg_Dbg( p_access, "%s", psz );
659
        free( psz );
660 661
    }

Laurent Aimar's avatar
Laurent Aimar committed
662 663 664
    p_sys->i_packet_used   = 0;
    p_sys->i_packet_length = 0;

665 666 667
    return VLC_SUCCESS;
}

Laurent Aimar's avatar
Laurent Aimar committed
668 669 670
/*****************************************************************************
 *
 *****************************************************************************/
671
static void Stop( access_t *p_access )
672
{
673
    access_sys_t *p_sys = p_access->p_sys;
674

675
    msg_Dbg( p_access, "closing stream" );
676 677 678 679 680
    if( p_sys->fd > 0 )
    {
        net_Close( p_sys->fd );
        p_sys->fd = -1;
    }
681 682
}

Laurent Aimar's avatar
Laurent Aimar committed
683 684 685
/*****************************************************************************
 *
 *****************************************************************************/
686
static int GetPacket( access_t * p_access, chunk_t *p_ck )
687
{
688
    access_sys_t *p_sys = p_access->p_sys;
689

690 691 692
    /* chunk_t */
    memset( p_ck, 0, sizeof( chunk_t ) );

693
    /* Read the chunk header */
694
    if( net_Read( p_access, p_sys->fd, p_sys->buffer, 12, VLC_TRUE ) < 12 )
695
    {
696
        /* msg_Err( p_access, "cannot read data" ); */
Laurent Aimar's avatar
Laurent Aimar committed
697
        return VLC_EGENERIC;
698 699
    }

700 701 702 703 704 705 706
    p_ck->i_type      = GetWLE( p_sys->buffer);
    p_ck->i_size      = GetWLE( p_sys->buffer + 2);
    p_ck->i_sequence  = GetDWLE( p_sys->buffer + 4);
    p_ck->i_unknown   = GetWLE( p_sys->buffer + 8);
    p_ck->i_size2     = GetWLE( p_sys->buffer + 10);
    p_ck->p_data      = p_sys->buffer + 12;
    p_ck->i_data      = p_ck->i_size2 - 8;
707

Laurent Aimar's avatar
Laurent Aimar committed
708
    if( p_ck->i_type == 0x4524 )   // Transfer complete
709
    {
710 711
        if( p_ck->i_sequence == 0 )
        {
712
            msg_Warn( p_access, "EOF" );
713 714 715 716
            return VLC_EGENERIC;
        }
        else
        {
717
            msg_Warn( p_access, "Next stream follow but not supported" );
718 719
            return VLC_EGENERIC;
        }
720
    }
Laurent Aimar's avatar
Laurent Aimar committed
721
    else if( p_ck->i_type != 0x4824 && p_ck->i_type != 0x4424 )
722
    {
723
        msg_Err( p_access, "invalid chunk FATAL (0x%x)", p_ck->i_type );
Laurent Aimar's avatar
Laurent Aimar committed
724
        return VLC_EGENERIC;
725 726
    }

727
    if( p_ck->i_data > 0 &&
728
        net_Read( p_access, p_sys->fd, &p_sys->buffer[12], p_ck->i_data, VLC_TRUE ) < p_ck->i_data )
729
    {
730
        msg_Err( p_access, "cannot read data" );
731
        return VLC_EGENERIC;
732 733
    }

Laurent Aimar's avatar
Laurent Aimar committed
734 735
    if( p_sys->i_packet_sequence != 0 &&
        p_ck->i_sequence != p_sys->i_packet_sequence )
736
    {
737
        msg_Warn( p_access, "packet lost ? (%d != %d)", p_ck->i_sequence, p_sys->i_packet_sequence );
738 739
    }

Laurent Aimar's avatar
Laurent Aimar committed
740 741 742 743
    p_sys->i_packet_sequence = p_ck->i_sequence + 1;
    p_sys->i_packet_used   = 0;
    p_sys->i_packet_length = p_ck->i_data;
    p_sys->p_packet        = p_ck->p_data;
744

Laurent Aimar's avatar
Laurent Aimar committed
745
    return VLC_SUCCESS;
746
}