mmsh.c 28.8 KB
Newer Older
1 2 3
/*****************************************************************************
 * mmsh.c:
 *****************************************************************************
4
 * Copyright (C) 2001, 2002 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>
Clément Stenac's avatar
Clément Stenac committed
33
#include <vlc_access.h>
34
#include "vlc_playlist.h"
35
#include "vlc_strings.h"
36

Clément Stenac's avatar
Clément Stenac committed
37
#include <vlc_network.h>
38
#include "vlc_url.h"
39 40 41 42 43 44
#include "asf.h"
#include "buffer.h"

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

45 46 47 48
/* TODO:
 *  - authentication
 */

49 50 51
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
52 53
int  MMSHOpen  ( access_t * );
void MMSHClose ( access_t * );
Laurent Aimar's avatar
Laurent Aimar committed
54

55 56
static ssize_t Read( access_t *, uint8_t *, size_t );
static ssize_t ReadRedirect( access_t *, uint8_t *, size_t );
57 58
static int  Seek( access_t *, int64_t );
static int  Control( access_t *, int, va_list );
Laurent Aimar's avatar
Laurent Aimar committed
59

60 61 62
static int  Describe( access_t  *, char **ppsz_location );
static int  Start( access_t *, int64_t );
static void Stop( access_t * );
63

64
static int  GetPacket( access_t *, chunk_t * );
65 66 67 68 69 70 71
static void GetHeader( access_t *p_access );

static int Restart( access_t * );
static int Reset( access_t * );

//#define MMSH_USER_AGENT "NSPlayer/4.1.0.3856"
#define MMSH_USER_AGENT "NSPlayer/7.10.0.3059"
72 73 74 75

/****************************************************************************
 * Open: connect to ftp server and ask for file
 ****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
76
int MMSHOpen( access_t *p_access )
77 78
{
    access_sys_t    *p_sys;
79
    char            *psz_location = NULL;
80
    char            *psz_proxy;
Laurent Aimar's avatar
Laurent Aimar committed
81

82 83
    /* init p_sys */

84 85 86 87 88 89 90 91
    /* 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;
92
    p_access->info.b_eof = false;
93 94
    p_access->info.i_title = 0;
    p_access->info.i_seekpoint = 0;
95

96
    p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
97 98 99
    if( !p_sys )
        return VLC_ENOMEM;

100 101 102
    memset( p_sys, 0, sizeof( access_sys_t ) );
    p_sys->i_proto= MMS_PROTO_HTTP;
    p_sys->fd     = -1;
103
    p_sys->i_start= 0;
104

105
    /* Handle proxy */
106
    p_sys->b_proxy = false;
107 108 109 110 111
    memset( &p_sys->proxy, 0, sizeof(p_sys->proxy) );

    /* Check proxy */
    /* TODO reuse instead http-proxy from http access ? */
    psz_proxy = var_CreateGetString( p_access, "mmsh-proxy" );
112 113 114 115 116 117 118 119 120 121 122 123 124 125
    if( !*psz_proxy )
    {
        char *psz_http_proxy = config_GetPsz( p_access, "http-proxy" );
        if( psz_http_proxy && *psz_http_proxy )
        {
            free( psz_proxy );
            psz_proxy = psz_http_proxy;
            var_SetString( p_access, "mmsh-proxy", psz_proxy );
        }
        else
        {
            free( psz_http_proxy );
        }
    }
126

127 128
    if( *psz_proxy )
    {
129
        p_sys->b_proxy = true;
130 131 132 133 134 135 136 137
        vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
    }
#ifdef HAVE_GETENV
    else
    {
        char *psz_proxy = getenv( "http_proxy" );
        if( psz_proxy && *psz_proxy )
        {
138
            p_sys->b_proxy = true;
139 140 141 142 143 144 145 146
            vlc_UrlParse( &p_sys->proxy, psz_proxy, 0 );
        }
    }
#endif
    free( psz_proxy );

    if( p_sys->b_proxy )
    {
147 148
        if( ( p_sys->proxy.psz_host == NULL ) ||
            ( *p_sys->proxy.psz_host == '\0' ) )
149 150 151 152 153 154
        {
            msg_Warn( p_access, "invalid proxy host" );
            vlc_UrlClean( &p_sys->proxy );
            free( p_sys );
            return VLC_EGENERIC;
        }
155

156 157 158 159 160 161
        if( p_sys->proxy.i_port <= 0 )
            p_sys->proxy.i_port = 80;
        msg_Dbg( p_access, "Using http proxy %s:%d",
                 p_sys->proxy.psz_host, p_sys->proxy.i_port );
    }

162
    /* open a tcp connection */
163
    vlc_UrlParse( &p_sys->url, p_access->psz_path, 0 );
164 165
    if( ( p_sys->url.psz_host == NULL ) ||
        ( *p_sys->url.psz_host == '\0' ) )
166
    {
167
        msg_Err( p_access, "invalid host" );
Laurent Aimar's avatar
Laurent Aimar committed
168
        vlc_UrlClean( &p_sys->proxy );
169 170 171
        vlc_UrlClean( &p_sys->url );
        free( p_sys );
        return VLC_EGENERIC;
172
    }
173 174
    if( p_sys->url.i_port <= 0 )
        p_sys->url.i_port = 80;
175

176
    if( Describe( p_access, &psz_location ) )
177
    {
Laurent Aimar's avatar
Laurent Aimar committed
178
        vlc_UrlClean( &p_sys->proxy );
179 180 181
        vlc_UrlClean( &p_sys->url );
        free( p_sys );
        return VLC_EGENERIC;
182
    }
183 184
    /* Handle redirection */
    if( psz_location && *psz_location )
185
    {
186
        playlist_t * p_playlist = pl_Yield( p_access );
187
        msg_Dbg( p_access, "redirection to %s", psz_location );
188

189
        /** \bug we do not autodelete here */
190
        playlist_Add( p_playlist, psz_location, psz_location,
191 192
                      PLAYLIST_INSERT | PLAYLIST_GO, PLAYLIST_END, true,
                      false );
193 194
        vlc_object_release( p_playlist );

195 196
        free( psz_location );

197
        p_access->pf_read = ReadRedirect;
198 199 200
        return VLC_SUCCESS;
    }

201
    /* Start playing */
202
    if( Start( p_access, 0 ) )
203
    {
204
        msg_Err( p_access, "cannot start stream" );
205
        free( p_sys->p_header );
Laurent Aimar's avatar
Laurent Aimar committed
206
        vlc_UrlClean( &p_sys->proxy );
207 208 209
        vlc_UrlClean( &p_sys->url );
        free( p_sys );
        return VLC_EGENERIC;
210 211
    }

212
    if( !p_sys->b_broadcast )
213
    {
214
        p_access->info.i_size = p_sys->asfh.i_file_size;
215 216
    }

Laurent Aimar's avatar
Laurent Aimar committed
217
    return VLC_SUCCESS;
218 219 220 221 222
}

/*****************************************************************************
 * Close: free unused data structures
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
223
void  MMSHClose ( access_t *p_access )
224 225 226 227
{
    access_sys_t *p_sys = p_access->p_sys;

    Stop( p_access );
Laurent Aimar's avatar
Laurent Aimar committed
228

Rémi Duraffort's avatar
Rémi Duraffort committed
229
    free( p_sys->p_header  );
Laurent Aimar's avatar
Laurent Aimar committed
230 231 232

    vlc_UrlClean( &p_sys->proxy );
    vlc_UrlClean( &p_sys->url );
233 234 235 236 237 238 239
    free( p_sys );
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
240
{
241
    access_sys_t *p_sys = p_access->p_sys;
242
    bool   *pb_bool;
243 244
    int          *pi_int;
    int64_t      *pi_64;
245
    int          i_int;
246 247 248 249 250

    switch( i_query )
    {
        /* */
        case ACCESS_CAN_SEEK:
251
            pb_bool = (bool*)va_arg( args, bool* );
252 253
            *pb_bool = !p_sys->b_broadcast;
            break;
254

255 256
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
257 258
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = false;
259 260
            break;

261
        case ACCESS_CAN_CONTROL_PACE:
262 263
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;
264
            break;
265

266 267 268 269 270
        /* */
        case ACCESS_GET_MTU:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = 3 * p_sys->asfh.i_min_data_packet_size;
            break;
271

272 273
        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
274
            *pi_64 = (int64_t)var_GetInteger( p_access, "mms-caching" ) * INT64_C(1000);
275
            break;
276

277 278
        case ACCESS_GET_PRIVATE_ID_STATE:
            i_int = (int)va_arg( args, int );
279
            pb_bool = (bool *)va_arg( args, bool * );
280

281
            if( (i_int < 0) || (i_int > 127) )
282
                return VLC_EGENERIC;
283
            *pb_bool =  p_sys->asfh.stream[i_int].i_selected ? true : false;
284 285
            break;

286 287 288 289 290
        /* */
        case ACCESS_SET_PAUSE_STATE:
        case ACCESS_GET_TITLE_INFO:
        case ACCESS_SET_TITLE:
        case ACCESS_SET_SEEKPOINT:
291
        case ACCESS_SET_PRIVATE_ID_STATE:
292
        case ACCESS_GET_CONTENT_TYPE:
293
            return VLC_EGENERIC;
294

295
        default:
296
            msg_Warn( p_access, "unimplemented query in control" );
297 298 299 300
            return VLC_EGENERIC;

    }
    return VLC_SUCCESS;
301 302 303 304 305
}

/*****************************************************************************
 * Seek: try to go at the right place
 *****************************************************************************/
306
static int Seek( access_t *p_access, int64_t i_pos )
307
{
308
    access_sys_t *p_sys = p_access->p_sys;
309 310 311 312
    chunk_t      ck;
    off_t        i_offset;
    off_t        i_packet;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
313
    msg_Dbg( p_access, "seeking to %"PRId64, i_pos );
314

315 316 317
    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;

318 319
    Stop( p_access );
    Start( p_access, i_packet * p_sys->asfh.i_min_data_packet_size );
320

321
    while( !p_access->b_die )
322
    {
323
        msg_Warn( p_access, "GetPacket 1" );
324
        if( GetPacket( p_access, &ck ) )
325 326 327 328 329
            break;

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

        msg_Warn( p_access, "skipping header" );
332 333
    }

334
    p_access->info.i_pos = i_pos;
335
    p_access->info.b_eof = false;
336 337
    p_sys->i_packet_used += i_offset;

338
    return VLC_SUCCESS;
339 340
}

341 342 343
/*****************************************************************************
 * Read:
 *****************************************************************************/
344
static ssize_t ReadRedirect( access_t *p_access, uint8_t *p, size_t i_len )
345
{
346
    return 0;
347 348
}

349 350 351
/*****************************************************************************
 * Read:
 *****************************************************************************/
352
static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
353
{
354
    access_sys_t *p_sys = p_access->p_sys;
355 356 357
    size_t       i_copy;
    size_t       i_data = 0;

358
    if( p_access->info.b_eof )
359
        return 0;
360

361
    while( i_data < (size_t) i_len )
362
    {
363
        if( p_access->info.i_pos < (p_sys->i_start + p_sys->i_header) )
Laurent Aimar's avatar
Laurent Aimar committed
364
        {
365
            int i_offset = p_access->info.i_pos - p_sys->i_start;
366 367
            i_copy = __MIN( p_sys->i_header - i_offset,
                            (int)((size_t)i_len - i_data) );
368
            memcpy( &p_buffer[i_data], &p_sys->p_header[i_offset], i_copy );
Laurent Aimar's avatar
Laurent Aimar committed
369 370

            i_data += i_copy;
371
            p_access->info.i_pos += i_copy;
Laurent Aimar's avatar
Laurent Aimar committed
372 373
        }
        else if( p_sys->i_packet_used < p_sys->i_packet_length )
374
        {
375 376
            i_copy = __MIN( p_sys->i_packet_length - p_sys->i_packet_used,
                            i_len - i_data );
377 378 379 380 381 382
            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;
383
            p_access->info.i_pos += i_copy;
384
        }
385 386
        else if( (p_sys->i_packet_length > 0) &&
                 ((int)p_sys->i_packet_used < p_sys->asfh.i_min_data_packet_size) )
387
        {
388 389
            i_copy = __MIN( p_sys->asfh.i_min_data_packet_size - p_sys->i_packet_used,
                            i_len - i_data );
390 391 392 393
            memset( &p_buffer[i_data], 0, i_copy );

            i_data += i_copy;
            p_sys->i_packet_used += i_copy;
394
            p_access->info.i_pos += i_copy;
395 396 397 398
        }
        else
        {
            chunk_t ck;
399
        msg_Warn( p_access, "GetPacket 2" );
400
            if( GetPacket( p_access, &ck ) )
401
            {
402 403
                int i_ret = -1;
                if( p_sys->b_broadcast )
404
                {
405
                    if( (ck.i_type == 0x4524) && (ck.i_sequence != 0) )
406 407 408
                        i_ret = Restart( p_access );
                    else if( ck.i_type == 0x4324 )
                        i_ret = Reset( p_access );
409
                }
410
                if( i_ret )
411
                {
412
                    p_access->info.b_eof = true;
413
                    return 0;
414
                }
415
            }
416
            if( ck.i_type != 0x4424 )
Laurent Aimar's avatar
Laurent Aimar committed
417 418 419 420
            {
                p_sys->i_packet_used = 0;
                p_sys->i_packet_length = 0;
            }
421 422 423 424 425 426
        }
    }

    return( i_data );
}

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/* */
static int Restart( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    char *psz_location = NULL;

    msg_Dbg( p_access, "Restart the stream" );
    p_sys->i_start = p_access->info.i_pos;

    /* */
    msg_Dbg( p_access, "stoping the stream" );
    Stop( p_access );

    /* */
    msg_Dbg( p_access, "describe the stream" );
    if( Describe( p_access, &psz_location ) )
    {
        msg_Err( p_access, "describe failed" );
445
        return VLC_EGENERIC;
446 447 448 449 450
    }
    /* */
    if( Start( p_access, 0 ) )
    {
        msg_Err( p_access, "Start failed" );
451
        return VLC_EGENERIC;
452
    }
453
    return VLC_SUCCESS;
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
}
static int Reset( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    asf_header_t old_asfh = p_sys->asfh;
    int i;

    msg_Dbg( p_access, "Reset the stream" );
    p_sys->i_start = p_access->info.i_pos;

    /* */
    p_sys->i_packet_sequence = 0;
    p_sys->i_packet_used = 0;
    p_sys->i_packet_length = 0;
    p_sys->p_packet = NULL;

    /* Get the next header FIXME memory loss ? */
    GetHeader( p_access );
    if( p_sys->i_header <= 0 )
473
        return VLC_EGENERIC;
474

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
475
     asf_HeaderParse ( &p_sys->asfh,
476
                           p_sys->p_header, p_sys->i_header );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
477
    msg_Dbg( p_access, "packet count=%"PRId64" packet size=%d",
478 479 480
             p_sys->asfh.i_data_packets_count,
             p_sys->asfh.i_min_data_packet_size );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
481
     asf_StreamSelect( &p_sys->asfh,
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
                           var_CreateGetInteger( p_access, "mms-maxbitrate" ),
                           var_CreateGetInteger( p_access, "mms-all" ),
                           var_CreateGetInteger( p_access, "audio" ),
                           var_CreateGetInteger( p_access, "video" ) );

    /* Check we have comptible asfh */
    for( i = 1; i < 128; i++ )
    {
        asf_stream_t *p_old = &old_asfh.stream[i];
        asf_stream_t *p_new = &p_sys->asfh.stream[i];

        if( p_old->i_cat != p_new->i_cat || p_old->i_selected != p_new->i_selected )
            break;
    }
    if( i < 128 )
    {
        msg_Warn( p_access, "incompatible asf header, restart" );
        return Restart( p_access );
    }

    /* */
    p_sys->i_packet_used = 0;
    p_sys->i_packet_length = 0;
505
    return VLC_SUCCESS;
506
}
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525

static int OpenConnection( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    vlc_url_t    srv = p_sys->b_proxy ? p_sys->proxy : p_sys->url;

    if( ( p_sys->fd = net_ConnectTCP( p_access,
                                      srv.psz_host, srv.i_port ) ) < 0 )
    {
        msg_Err( p_access, "cannot connect to %s:%d",
                 srv.psz_host, srv.i_port );
        return VLC_EGENERIC;
    }

    if( p_sys->b_proxy )
    {
        net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
                    "GET http://%s:%d%s HTTP/1.0\r\n",
                    p_sys->url.psz_host, p_sys->url.i_port,
526 527 528
                    ( (p_sys->url.psz_path == NULL) ||
                      (*p_sys->url.psz_path == '\0') ) ?
                         "/" : p_sys->url.psz_path );
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

        /* Proxy Authentication */
        if( p_sys->proxy.psz_username && *p_sys->proxy.psz_username )
        {
            char *buf;
            char *b64;

            asprintf( &buf, "%s:%s", p_sys->proxy.psz_username,
                       p_sys->proxy.psz_password ? p_sys->proxy.psz_password : "" );

            b64 = vlc_b64_encode( buf );
            free( buf );

            net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
                        "Proxy-Authorization: Basic %s\r\n", b64 );
            free( b64 );
        }
    }
    else
    {
        net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
                    "GET %s HTTP/1.0\r\n"
                    "Host: %s:%d\r\n",
552 553 554
                    ( (p_sys->url.psz_path == NULL) ||
                      (*p_sys->url.psz_path == '\0') ) ?
                            "/" : p_sys->url.psz_path,
555 556 557 558 559
                    p_sys->url.psz_host, p_sys->url.i_port );
    }
    return VLC_SUCCESS;
}

560 561 562
/*****************************************************************************
 * Describe:
 *****************************************************************************/
563
static int Describe( access_t  *p_access, char **ppsz_location )
564
{
565
    access_sys_t *p_sys = p_access->p_sys;
566 567 568 569 570
    char         *psz_location = NULL;
    char         *psz;
    int          i_code;

    /* Reinit context */
571
    p_sys->b_broadcast = true;
572 573 574 575 576
    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;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
577
     GenerateGuid ( &p_sys->guid );
578

579 580
    if( OpenConnection( p_access ) )
        return VLC_EGENERIC;
581

582
    net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
583
                "Accept: */*\r\n"
584
                "User-Agent: "MMSH_USER_AGENT"\r\n"
585 586 587 588 589 590
                "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->i_request_context++,
                GUID_PRINT( p_sys->guid ) );

591
    if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
592
    {
593
        msg_Err( p_access, "failed to send request" );
594 595 596 597
        goto error;
    }

    /* Receive the http header */
598
    if( ( psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL ) ) == NULL )
599
    {
600
        msg_Err( p_access, "failed to read answer" );
601 602
        goto error;
    }
603

604 605
    if( strncmp( psz, "HTTP/1.", 7 ) )
    {
606
        msg_Err( p_access, "invalid HTTP reply '%s'", psz );
607 608 609 610 611 612
        free( psz );
        goto error;
    }
    i_code = atoi( &psz[9] );
    if( i_code >= 400 )
    {
613
        msg_Err( p_access, "error: %s", psz );
614 615 616 617
        free( psz );
        goto error;
    }

618
    msg_Dbg( p_access, "HTTP reply '%s'", psz );
619 620 621
    free( psz );
    for( ;; )
    {
622
        char *psz = net_Gets( p_access, p_sys->fd, NULL );
623 624 625 626
        char *p;

        if( psz == NULL )
        {
627
            msg_Err( p_access, "failed to read answer" );
628 629 630 631 632 633 634 635 636 637 638
            goto error;
        }

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

        if( ( p = strchr( psz, ':' ) ) == NULL )
        {
639
            msg_Err( p_access, "malformed header line: %s", psz );
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
            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" ) )
                {
655
                    msg_Dbg( p_access, "stream type = broadcast" );
656
                    p_sys->b_broadcast = true;
657 658 659
                }
                else if( strstr( p, "seekable" ) )
                {
660
                    msg_Dbg( p_access, "stream type = seekable" );
661
                    p_sys->b_broadcast = false;
662 663 664
                }
                else
                {
665
                    msg_Warn( p_access, "unknow stream types (%s)", p );
666
                    p_sys->b_broadcast = false;
667 668 669 670 671 672 673 674 675 676 677 678
                }
            }
        }
        else if( !strcasecmp( psz, "Location" ) )
        {
            psz_location = strdup( p );
        }

        free( psz );
    }

    /* Handle the redirection */
679 680
    if( ( (i_code == 301) || (i_code == 302) ||
          (i_code == 303) || (i_code == 307) ) &&
681 682
        psz_location && *psz_location )
    {
683
        msg_Dbg( p_access, "redirection to %s", psz_location );
684 685 686 687 688 689 690
        net_Close( p_sys->fd ); p_sys->fd = -1;

        *ppsz_location = psz_location;
        return VLC_SUCCESS;
    }

    /* Read the asf header */
691
    GetHeader( p_access );
692 693
    if( p_sys->i_header <= 0 )
    {
694
        msg_Err( p_access, "header size == 0" );
695 696 697
        goto error;
    }
    /* close this connection */
698 699
    net_Close( p_sys->fd );
    p_sys->fd = -1;
700 701 702 703 704 705

    /* *** parse header and get stream and their id *** */
    /* get all streams properties,
     *
     * TODO : stream bitrates properties(optional)
     *        and bitrate mutual exclusion(optional) */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
706
     asf_HeaderParse ( &p_sys->asfh,
707
                           p_sys->p_header, p_sys->i_header );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
708
    msg_Dbg( p_access, "packet count=%"PRId64" packet size=%d",
709 710 711
             p_sys->asfh.i_data_packets_count,
             p_sys->asfh.i_min_data_packet_size );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
712
     asf_StreamSelect( &p_sys->asfh,
713 714 715 716
                           var_CreateGetInteger( p_access, "mms-maxbitrate" ),
                           var_CreateGetInteger( p_access, "mms-all" ),
                           var_CreateGetInteger( p_access, "audio" ),
                           var_CreateGetInteger( p_access, "video" ) );
717 718 719 720 721 722 723 724 725 726
    return VLC_SUCCESS;

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

728 729 730 731 732 733
static void GetHeader( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;

    /* Read the asf header */
    p_sys->i_header = 0;
Rémi Duraffort's avatar
Rémi Duraffort committed
734
    free( p_sys->p_header  );
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
    p_sys->p_header = NULL;
    for( ;; )
    {
        chunk_t ck;
        if( GetPacket( p_access, &ck ) || 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 );
        }
    }
    msg_Dbg( p_access, "complete header size=%d", p_sys->i_header );
}

753

Laurent Aimar's avatar
Laurent Aimar committed
754
/*****************************************************************************
755
 * Start stream
756
 ****************************************************************************/
757
static int Start( access_t *p_access, int64_t i_pos )
758
{
759
    access_sys_t *p_sys = p_access->p_sys;
760
    int  i_streams = 0;
761
    int  i_streams_selected = 0;
762
    int  i;
763
    char *psz = NULL;
764

765
    msg_Dbg( p_access, "starting stream" );
766 767 768

    for( i = 1; i < 128; i++ )
    {
769 770 771
        if( p_sys->asfh.stream[i].i_cat == ASF_STREAM_UNKNOWN )
            continue;
        i_streams++;
772
        if( p_sys->asfh.stream[i].i_selected )
773
            i_streams_selected++;
774
    }
775
    if( i_streams_selected <= 0 )
776
    {
777
        msg_Err( p_access, "no stream selected" );
778 779
        return VLC_EGENERIC;
    }
780

781 782
    if( OpenConnection( p_access ) )
        return VLC_EGENERIC;
783

784
    net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
785
                "Accept: */*\r\n"
786
                "User-Agent: "MMSH_USER_AGENT"\r\n" );
787 788
    if( p_sys->b_broadcast )
    {
789
        net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
790 791
                    "Pragma: no-cache,rate=1.000000,request-context=%d\r\n",
                    p_sys->i_request_context++ );
792 793 794
    }
    else
    {
795
        net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
796 797 798 799
                    "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++ );
800
    }
801
    net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
802 803 804 805 806 807
                "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);
808

809
    for( i = 1; i < 128; i++ )
810
    {
811
        if( p_sys->asfh.stream[i].i_cat != ASF_STREAM_UNKNOWN )
812
        {
813 814
            int i_select = 2;
            if( p_sys->asfh.stream[i].i_selected )
815
            {
816
                i_select = 0;
817
            }
818
            net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
819
                        "ffff:%d:%d ", i, i_select );
820 821
        }
    }
822 823 824
    net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" );
    net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL,
                "Connection: Close\r\n" );
825

826
    if( net_Printf( VLC_OBJECT(p_access), p_sys->fd, NULL, "\r\n" ) < 0 )
827
    {
828
        msg_Err( p_access, "failed to send request" );
829 830 831
        return VLC_EGENERIC;
    }

832 833
    psz = net_Gets( VLC_OBJECT(p_access), p_sys->fd, NULL );
    if( psz == NULL )
834
    {
835
        msg_Err( p_access, "cannot read data 0" );
836 837
        return VLC_EGENERIC;
    }
838

839
    if( atoi( &psz[9] ) >= 400 )
840
    {
841
        msg_Err( p_access, "error: %s", psz );
842 843
        free( psz );
        return VLC_EGENERIC;
844
    }
845
    msg_Dbg( p_access, "HTTP reply '%s'", psz );
846 847 848 849
    free( psz );

    /* FIXME check HTTP code */
    for( ;; )
850
    {
851
        char *psz = net_Gets( p_access, p_sys->fd, NULL );
852 853
        if( psz == NULL )
        {
854
            msg_Err( p_access, "cannot read data 1" );
855 856 857 858 859 860 861
            return VLC_EGENERIC;
        }
        if( *psz == '\0' )
        {
            free( psz );
            break;
        }
862
        msg_Dbg( p_access, "%s", psz );
863
        free( psz );
864 865
    }

Laurent Aimar's avatar
Laurent Aimar committed
866 867 868
    p_sys->i_packet_used   = 0;
    p_sys->i_packet_length = 0;

869 870 871
    return VLC_SUCCESS;
}

Laurent Aimar's avatar
Laurent Aimar committed
872
/*****************************************************************************
873
 * closing stream
Laurent Aimar's avatar
Laurent Aimar committed
874
 *****************************************************************************/
875
static void Stop( access_t *p_access )
876
{
877
    access_sys_t *p_sys = p_access->p_sys;
878

879
    msg_Dbg( p_access, "closing stream" );
880 881 882 883 884
    if( p_sys->fd > 0 )
    {
        net_Close( p_sys->fd );
        p_sys->fd = -1;
    }
885 886
}

Laurent Aimar's avatar
Laurent Aimar committed
887
/*****************************************************************************
888
 * get packet
Laurent Aimar's avatar
Laurent Aimar committed
889
 *****************************************************************************/
890
static int GetPacket( access_t * p_access, chunk_t *p_ck )
891
{
892
    access_sys_t *p_sys = p_access->p_sys;
893
    int restsize;
894

895 896 897
    /* chunk_t */
    memset( p_ck, 0, sizeof( chunk_t ) );

898
    /* Read the chunk header */
899 900 901 902 903
    /* Some headers are short, like 0x4324. Reading 12 bytes will cause us
     * to lose synchronization with the stream. Just read to the length
     * (4 bytes), decode and then read up to 8 additional bytes to get the
     * entire header.
     */
904
    if( net_Read( p_access, p_sys->fd, NULL, p_sys->buffer, 4, true ) < 4 )
905 906
    {
       msg_Err( p_access, "cannot read data 2" );
907
       return VLC_EGENERIC;
908
    }
909

910 911
    p_ck->i_type = GetWLE( p_sys->buffer);
    p_ck->i_size = GetWLE( p_sys->buffer + 2);
912