aboutsummaryrefslogtreecommitdiffhomepage
path: root/libmpdemux/asf_mmst_streaming.c
blob: bc337dc4adc88c70c85678ac23939d9f9a034f5d (plain)
1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// mmst implementation taken from the xine-mms plugin made by majormms (http://geocities.com/majormms/)
// 
// ported to mplayer by Abhijeet Phatak <abhijeetphatak@yahoo.com>
// date : 16 April 2002
//
// information about the mms protocol can be find at http://get.to/sdp
//


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <inttypes.h>

#include "config.h"

#ifndef HAVE_WINSOCK2
#define closesocket close
#else
#include <winsock2.h>
#endif

#ifndef USE_SETLOCALE
#undef USE_ICONV
#endif

#ifdef USE_ICONV
#include <iconv.h>
#ifdef USE_LANGINFO
#include <langinfo.h>
#endif
#include <locale.h>
#endif

#include "url.h"
#include "asf.h"

#include "stream.h"

#include "network.h"

#define BUF_SIZE 102400
#define HDR_BUF_SIZE 8192

typedef struct 
{
  uint8_t buf[BUF_SIZE];
  int     num_bytes;

} command_t;

static int seq_num;
static int num_stream_ids;
static int stream_ids[20];

static int get_data (int s, char *buf, size_t count);

static void put_32 (command_t *cmd, uint32_t value) 
{
  cmd->buf[cmd->num_bytes  ] = value % 256;
  value = value >> 8;
  cmd->buf[cmd->num_bytes+1] = value % 256 ;
  value = value >> 8;
  cmd->buf[cmd->num_bytes+2] = value % 256 ;
  value = value >> 8;
  cmd->buf[cmd->num_bytes+3] = value % 256 ;

  cmd->num_bytes += 4;
}

static uint32_t get_32 (unsigned char *cmd, int offset) 
{
  uint32_t ret;

  ret = cmd[offset] ;
  ret |= cmd[offset+1]<<8 ;
  ret |= cmd[offset+2]<<16 ;
  ret |= cmd[offset+3]<<24 ;

  return ret;
}

static void send_command (int s, int command, uint32_t switches, 
			  uint32_t extra, int length,
			  char *data) 
{
  command_t  cmd;
  int        len8;

  len8 = (length + 7) / 8;

  cmd.num_bytes = 0;

  put_32 (&cmd, 0x00000001); /* start sequence */
  put_32 (&cmd, 0xB00BFACE); /* #-)) */
  put_32 (&cmd, len8*8 + 32);
  put_32 (&cmd, 0x20534d4d); /* protocol type "MMS " */
  put_32 (&cmd, len8 + 4);
  put_32 (&cmd, seq_num);
  seq_num++;
  put_32 (&cmd, 0x0);        /* unknown */
  put_32 (&cmd, 0x0);
  put_32 (&cmd, len8+2);
  put_32 (&cmd, 0x00030000 | command); /* dir | command */
  put_32 (&cmd, switches);
  put_32 (&cmd, extra);

  memcpy (&cmd.buf[48], data, length);
  if (length & 7)
    memset(&cmd.buf[48 + length], 0, 8 - (length & 7));

  if (send (s, cmd.buf, len8*8+48, 0) != (len8*8+48)) {
    printf ("write error\n");
  }
}

#ifdef USE_ICONV
static iconv_t url_conv;
#endif

static void string_utf16(char *dest, char *src, int len) {
    int i;
#ifdef USE_ICONV
    size_t len1, len2;
    char *ip, *op;

    if (url_conv != (iconv_t)(-1))
    {
    memset(dest, 0, 1000);
    len1 = len; len2 = 1000;
    ip = src; op = dest;

    iconv(url_conv, &ip, &len1, &op, &len2);
    }
    else
    {
#endif
	for (i=0; i<len; i++) {
	    dest[i*2] = src[i];
	    dest[i*2+1] = 0;
        }
	/* trailing zeroes */
	dest[i*2] = 0;
	dest[i*2+1] = 0;
#ifdef USE_ICONV
    }
#endif
}

static void get_answer (int s) 
{
  char  data[BUF_SIZE];
  int   command = 0x1b;

  while (command == 0x1b) {
    int len;

    len = recv (s, data, BUF_SIZE, 0) ;
    if (!len) {
      printf ("\nalert! eof\n");
      return;
    }

    command = get_32 (data, 36) & 0xFFFF;

    if (command == 0x1b) 
      send_command (s, 0x1b, 0, 0, 0, data);
  }
}

static int get_data (int s, char *buf, size_t count) 
{
  ssize_t  len;
  size_t total = 0;

  while (total < count) {

    len = recv (s, &buf[total], count-total, 0);

    if (len<=0) {
      perror ("read error:");
      return 0;
    }

    total += len;

    if (len != 0) {
//      printf ("[%d/%d]", total, count);
      fflush (stdout);
    }

  }

  return 1;

}

static int get_header (int s, uint8_t *header, streaming_ctrl_t *streaming_ctrl) 
{
  unsigned char  pre_header[8];
  int            header_len;

  header_len = 0;

  while (1) {
    if (!get_data (s, pre_header, 8)) {
      printf ("pre-header read failed\n");
      return 0;
    }
    if (pre_header[4] == 0x02) {
      
      int packet_len;
      
      packet_len = (pre_header[7] << 8 | pre_header[6]) - 8;

//      printf ("asf header packet detected, len=%d\n", packet_len);

      if (packet_len < 0 || packet_len > HDR_BUF_SIZE - header_len) {
        mp_msg(MSGT_NETWORK, MSGL_FATAL, "Invalid header size, giving up\n");
        return 0;
      }

      if (!get_data (s, &header[header_len], packet_len)) {
	printf ("header data read failed\n");
	return 0;
      }

      header_len += packet_len;

      if ( (header[header_len-1] == 1) && (header[header_len-2]==1)) {
	

     if( streaming_bufferize( streaming_ctrl, header, header_len )<0 ) {
				return -1;
 	 }

     //	printf ("get header packet finished\n");

	return (header_len);

      } 

    } else {

      int32_t packet_len;
      int command;
      char data[BUF_SIZE];

      if (!get_data (s, (char*)&packet_len, 4)) {
	printf ("packet_len read failed\n");
	return 0;
      }
      
      packet_len = get_32 ((unsigned char*)&packet_len, 0) + 4;
      
//      printf ("command packet detected, len=%d\n", packet_len);

      if (packet_len < 0 || packet_len > BUF_SIZE) {
        mp_msg(MSGT_NETWORK, MSGL_FATAL,
                "Invalid rtsp packet size, giving up\n");
        return 0;
      }
      
      if (!get_data (s, data, packet_len)) {
	printf ("command data read failed\n");
	return 0;
      }
      
      command = get_32 (data, 24) & 0xFFFF;
      
//      printf ("command: %02x\n", command);
      
      if (command == 0x1b) 
	send_command (s, 0x1b, 0, 0, 0, data);
      
    }

//    printf ("get header packet succ\n");
  }
}

static int interp_header (uint8_t *header, int header_len) 
{
  int i;
  int packet_length=-1;

  /*
   * parse header
   */

  i = 30;
  while (i<header_len) {
    
    uint64_t  guid_1, guid_2, length;

    guid_2 = (uint64_t)header[i] | ((uint64_t)header[i+1]<<8) 
      | ((uint64_t)header[i+2]<<16) | ((uint64_t)header[i+3]<<24)
      | ((uint64_t)header[i+4]<<32) | ((uint64_t)header[i+5]<<40)
      | ((uint64_t)header[i+6]<<48) | ((uint64_t)header[i+7]<<56);
    i += 8;

    guid_1 = (uint64_t)header[i] | ((uint64_t)header[i+1]<<8) 
      | ((uint64_t)header[i+2]<<16) | ((uint64_t)header[i+3]<<24)
      | ((uint64_t)header[i+4]<<32) | ((uint64_t)header[i+5]<<40)
      | ((uint64_t)header[i+6]<<48) | ((uint64_t)header[i+7]<<56);
    i += 8;
    
//    printf ("guid found: %016llx%016llx\n", guid_1, guid_2);

    length = (uint64_t)header[i] | ((uint64_t)header[i+1]<<8) 
      | ((uint64_t)header[i+2]<<16) | ((uint64_t)header[i+3]<<24)
      | ((uint64_t)header[i+4]<<32) | ((uint64_t)header[i+5]<<40)
      | ((uint64_t)header[i+6]<<48) | ((uint64_t)header[i+7]<<56);

    i += 8;

    if ( (guid_1 == 0x6cce6200aa00d9a6ULL) && (guid_2 == 0x11cf668e75b22630ULL) ) {
      printf ("header object\n");
    } else if ((guid_1 == 0x6cce6200aa00d9a6ULL) && (guid_2 == 0x11cf668e75b22636ULL)) {
      printf ("data object\n");
    } else if ((guid_1 == 0x6553200cc000e48eULL) && (guid_2 == 0x11cfa9478cabdca1ULL)) {

      packet_length = get_32(header, i+92-24);

      printf ("file object, packet length = %d (%d)\n",
	      packet_length, get_32(header, i+96-24));


    } else if ((guid_1 == 0x6553200cc000e68eULL) && (guid_2 == 0x11cfa9b7b7dc0791ULL)) {

      int stream_id = header[i+48] | header[i+49] << 8;

      printf ("stream object, stream id: %d\n", stream_id);

      stream_ids[num_stream_ids] = stream_id;
      num_stream_ids++;
      
    } else {
      printf ("unknown object\n");
    }

//    printf ("length    : %lld\n", length);

    i += length-24;

  }

  return packet_length;

}


static int get_media_packet (int s, int padding, streaming_ctrl_t *stream_ctrl) {
  unsigned char  pre_header[8];
  char           data[BUF_SIZE];

  if (!get_data (s, pre_header, 8)) {
    printf ("pre-header read failed\n");
    return 0;
  }

//  for (i=0; i<8; i++)
//    printf ("pre_header[%d] = %02x (%d)\n",
//	    i, pre_header[i], pre_header[i]);

  if (pre_header[4] == 0x04) {

    int packet_len;

    packet_len = (pre_header[7] << 8 | pre_header[6]) - 8;

//    printf ("asf media packet detected, len=%d\n", packet_len);

    if (packet_len < 0 || packet_len > BUF_SIZE) {
      mp_msg(MSGT_NETWORK, MSGL_FATAL,
              "Invalid rtsp packet size, giving up\n");
      return 0;
    }
      
    if (!get_data (s, data, packet_len)) {
      printf ("media data read failed\n");
      return 0;
    }

    streaming_bufferize(stream_ctrl, data, padding);

  } else {

    int32_t packet_len;
    int command;

    if (!get_data (s, (char*)&packet_len, 4)) {
      printf ("packet_len read failed\n");
      return 0;
    }

    packet_len = get_32 ((unsigned char*)&packet_len, 0) + 4;

    if (packet_len < 0 || packet_len > BUF_SIZE) {
      mp_msg(MSGT_NETWORK, MSGL_FATAL,
              "Invalid rtsp packet size, giving up\n");
      return 0;
    }

    if (!get_data (s, data, packet_len)) {
      printf ("command data read failed\n");
      return 0;
    }

    if ( (pre_header[7] != 0xb0) || (pre_header[6] != 0x0b)
	 || (pre_header[5] != 0xfa) || (pre_header[4] != 0xce) ) {

      printf ("missing signature\n");
      return -1;
    }

    command = get_32 (data, 24) & 0xFFFF;

//    printf ("\ncommand packet detected, len=%d  cmd=0x%X\n", packet_len, command);

    if (command == 0x1b) 
      send_command (s, 0x1b, 0, 0, 0, data);
    else if (command == 0x1e) {
      printf ("everything done. Thank you for downloading a media file containing proprietary and patentend technology.\n");
      return 0;
    }
    else if (command == 0x21 ) {
	// Looks like it's new in WMS9
	// Unknown command, but ignoring it seems to work.
	return 0;
    }
    else if (command != 0x05) {
      printf ("unknown command %02x\n", command);
      return -1;
    }
  }

//  printf ("get media packet succ\n");

  return 1;
}


static int packet_length1;

int
asf_mmst_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) 
{
  int len;
  
  while( stream_ctrl->buffer_size==0 ) {
          // buffer is empty - fill it!
	  int ret = get_media_packet( fd, packet_length1, stream_ctrl);
	  if( ret<0 ) {
		  printf("get_media_packet error : %s\n",strerror(errno));
		  return -1;
	  } else if (ret==0) //EOF?
		  return ret;
  }
  
	  len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
	  if(len>size) len=size;
	  memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
	  stream_ctrl->buffer_pos += len;
	  if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
		  free( stream_ctrl->buffer );
		  stream_ctrl->buffer = NULL;
		  stream_ctrl->buffer_size = 0;
		  stream_ctrl->buffer_pos = 0;
	  }
	  return len;

}

int
asf_mmst_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) 
{
	return -1;
	// Shut up gcc warning
	fd++;
	pos++;
	streaming_ctrl=NULL;
}

int asf_mmst_streaming_start(stream_t *stream)
{
  char                 str[1024];
  char                 data[BUF_SIZE];
  uint8_t              asf_header[HDR_BUF_SIZE];
  int                  asf_header_len;
  int                  len, i, packet_length;
  char                *path, *unescpath;
  URL_t *url1 = stream->streaming_ctrl->url;
  int s = stream->fd;

  if( s>0 ) {
	  closesocket( stream->fd );
	  stream->fd = -1;
  }
  
  /* parse url */
  path = strchr(url1->file,'/') + 1;

  /* mmst filename are not url_escaped by MS MediaPlayer and are expected as
   * "plain text" by the server, so need to decode it here
   */
  unescpath=malloc(strlen(path)+1);
  if (!unescpath) {
	mp_msg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed!\n");
	return -1; 
  }
  url_unescape_string(unescpath,path);
  path=unescpath;
  

  if( url1->port==0 ) {
	url1->port=1755;
  }
  s = connect2Server( url1->hostname, url1->port, 1);
  if( s<0 ) {
	  free(path);
	  return s;
  }
  printf ("connected\n");
  
  seq_num=0;

  /*
  * Send the initial connect info including player version no. Client GUID (random) and the host address being connected to. 
  * This command is sent at the very start of protocol initiation. It sends local information to the serve 
  * cmd 1 0x01 
  * */

  /* prepare for the url encoding conversion */
#ifdef USE_ICONV
  setlocale(LC_CTYPE, "");
#ifdef USE_LANGINFO
  url_conv = iconv_open("UTF-16LE",nl_langinfo(CODESET));
#else
  url_conv = iconv_open("UTF-16LE",setlocale(LC_CTYPE, NULL));
#endif
#endif

  snprintf (str, 1023, "\034\003NSPlayer/7.0.0.1956; {33715801-BAB3-9D85-24E9-03B90328270A}; Host: %s", url1->hostname);
  string_utf16 (data, str, strlen(str));
// send_command(s, commandno ....)
  send_command (s, 1, 0, 0x0004000b, strlen(str)*2+2, data);

  len = recv (s, data, BUF_SIZE, 0) ;

  /*This sends details of the local machine IP address to a Funnel system at the server. 
  * Also, the TCP or UDP transport selection is sent.
  *
  * here 192.168.0.1 is local ip address TCP/UDP states the tronsport we r using
  * and 1037 is the  local TCP or UDP socket number
  * cmd 2 0x02
  *  */

  string_utf16 (&data[8], "\002\000\\\\192.168.0.1\\TCP\\1037", 24);
  memset (data, 0, 8);
  send_command (s, 2, 0, 0, 24*2+10, data);

  len = recv (s, data, BUF_SIZE, 0) ;

  /* This command sends file path (at server) and file name request to the server.
  * 0x5 */

  string_utf16 (&data[8], path, strlen(path));
  memset (data, 0, 8);
  send_command (s, 5, 0, 0, strlen(path)*2+10, data);
  free(path);

  get_answer (s);

  /* The ASF header chunk request. Includes ?session' variable for pre header value. 
  * After this command is sent, 
  * the server replies with 0x11 command and then the header chunk with header data follows.
  * 0x15 */

  memset (data, 0, 40);
  data[32] = 2;

  send_command (s, 0x15, 1, 0, 40, data);

  num_stream_ids = 0;
  /* get_headers(s, asf_header);  */

  asf_header_len = get_header (s, asf_header, stream->streaming_ctrl);
//  printf("---------------------------------- asf_header %d\n",asf_header);
  if (asf_header_len==0) return -1; //error reading header
  packet_length = interp_header (asf_header, asf_header_len);


  /* 
  * This command is the media stream MBR selector. Switches are always 6 bytes in length.  
  * After all switch elements, data ends with bytes [00 00] 00 20 ac 40 [02]. 
  * Where:  
  * [00 00] shows 0x61 0x00 (on the first 33 sent) or 0xff 0xff for ASF files, and with no ending data for WMV files. 
  * It is not yet understood what all this means. 
  * And the last [02] byte is probably the header ?session' value. 
  *  
  *  0x33 */

  memset (data, 0, 40);

  for (i=1; i<num_stream_ids; i++) {
    data [ (i-1) * 6 + 2 ] = 0xFF;
    data [ (i-1) * 6 + 3 ] = 0xFF;
    data [ (i-1) * 6 + 4 ] = stream_ids[i];
    data [ (i-1) * 6 + 5 ] = 0x00;
  }

  send_command (s, 0x33, num_stream_ids, 0xFFFF | stream_ids[0] << 16, (num_stream_ids-1)*6+2 , data);

  get_answer (s);

  /* Start sending file from packet xx. 
  * This command is also used for resume downloads or requesting a lost packet. 
  * Also used for seeking by sending a play point value which seeks to the media time point. 
  * Includes ?session' value in pre header and the maximum media stream time. 
  * 0x07 */

  memset (data, 0, 40);

  for (i=8; i<16; i++)
    data[i] = 0xFF;

  data[20] = 0x04;

  send_command (s, 0x07, 1, 0xFFFF | stream_ids[0] << 16, 24, data);

  stream->fd = s;
  stream->streaming_ctrl->streaming_read = asf_mmst_streaming_read;
  stream->streaming_ctrl->streaming_seek = asf_mmst_streaming_seek;
  stream->streaming_ctrl->buffering = 1;
  stream->streaming_ctrl->status = streaming_playing_e;

  packet_length1 = packet_length;
  printf("mmst packet_length = %d\n",packet_length);

#ifdef USE_ICONV
  if (url_conv != (iconv_t)(-1))
    iconv_close(url_conv);
#endif

  return 0;
}