Mercurial > mplayer.hg
annotate libmpdemux/asf_mmst_streaming.c @ 7932:5cac037fcdd3
10l
author | arpi |
---|---|
date | Sun, 27 Oct 2002 03:30:59 +0000 |
parents | 5bb4d8801a2c |
children | 60624e692b95 |
rev | line source |
---|---|
6092 | 1 // mmst implementation taken from the xine-mms plugin made by majormms (http://geocities.com/majormms/) |
2 // | |
3 // ported to mplayer by Abhijeet Phatak <abhijeetphatak@yahoo.com> | |
4 // date : 16 April 2002 | |
5 // | |
6 // information about the mms protocol can be find at http://get.to/sdp | |
7 // | |
8 | |
9 | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include <string.h> | |
13 #include <unistd.h> | |
14 #include <errno.h> | |
7880 | 15 #include <inttypes.h> |
6092 | 16 |
17 #include "config.h" | |
18 | |
19 #include "url.h" | |
20 #include "asf.h" | |
21 | |
22 #include "stream.h" | |
23 | |
24 #include "network.h" | |
25 | |
26 #define BUF_SIZE 102400 | |
27 | |
28 typedef struct | |
29 { | |
30 uint8_t buf[BUF_SIZE]; | |
31 int num_bytes; | |
32 | |
33 } command_t; | |
34 | |
7880 | 35 static int seq_num; |
36 static int num_stream_ids; | |
37 static int stream_ids[20]; | |
6092 | 38 |
39 static int get_data (int s, char *buf, size_t count); | |
40 | |
41 static void put_32 (command_t *cmd, uint32_t value) | |
42 { | |
43 cmd->buf[cmd->num_bytes ] = value % 256; | |
44 value = value >> 8; | |
45 cmd->buf[cmd->num_bytes+1] = value % 256 ; | |
46 value = value >> 8; | |
47 cmd->buf[cmd->num_bytes+2] = value % 256 ; | |
48 value = value >> 8; | |
49 cmd->buf[cmd->num_bytes+3] = value % 256 ; | |
50 | |
51 cmd->num_bytes += 4; | |
52 } | |
53 | |
54 static uint32_t get_32 (unsigned char *cmd, int offset) | |
55 { | |
56 uint32_t ret; | |
57 | |
58 ret = cmd[offset] ; | |
59 ret |= cmd[offset+1]<<8 ; | |
60 ret |= cmd[offset+2]<<16 ; | |
61 ret |= cmd[offset+3]<<24 ; | |
62 | |
63 return ret; | |
64 } | |
65 | |
66 static void send_command (int s, int command, uint32_t switches, | |
67 uint32_t extra, int length, | |
68 char *data) | |
69 { | |
70 command_t cmd; | |
71 int len8; | |
72 | |
73 len8 = (length + (length%8)) / 8; | |
74 | |
75 cmd.num_bytes = 0; | |
76 | |
77 put_32 (&cmd, 0x00000001); /* start sequence */ | |
78 put_32 (&cmd, 0xB00BFACE); /* #-)) */ | |
79 put_32 (&cmd, length + 32); | |
80 put_32 (&cmd, 0x20534d4d); /* protocol type "MMS " */ | |
81 put_32 (&cmd, len8 + 4); | |
82 put_32 (&cmd, seq_num); | |
83 seq_num++; | |
84 put_32 (&cmd, 0x0); /* unknown */ | |
85 put_32 (&cmd, 0x0); | |
86 put_32 (&cmd, len8+2); | |
87 put_32 (&cmd, 0x00030000 | command); /* dir | command */ | |
88 put_32 (&cmd, switches); | |
89 put_32 (&cmd, extra); | |
90 | |
91 memcpy (&cmd.buf[48], data, length); | |
92 | |
93 if (write (s, cmd.buf, length+48) != (length+48)) { | |
94 printf ("write error\n"); | |
95 } | |
96 } | |
97 | |
98 static void string_utf16(char *dest, char *src, int len) | |
99 { | |
100 int i; | |
101 | |
102 memset (dest, 0, 1000); | |
103 | |
104 for (i=0; i<len; i++) { | |
105 dest[i*2] = src[i]; | |
106 dest[i*2+1] = 0; | |
107 } | |
108 | |
109 dest[i*2] = 0; | |
110 dest[i*2+1] = 0; | |
111 } | |
112 | |
113 static void get_answer (int s) | |
114 { | |
115 char data[BUF_SIZE]; | |
116 int command = 0x1b; | |
117 | |
118 while (command == 0x1b) { | |
119 int len; | |
120 | |
121 len = read (s, data, BUF_SIZE) ; | |
122 if (!len) { | |
123 printf ("\nalert! eof\n"); | |
124 return; | |
125 } | |
126 | |
127 command = get_32 (data, 36) & 0xFFFF; | |
128 | |
129 if (command == 0x1b) | |
130 send_command (s, 0x1b, 0, 0, 0, data); | |
131 } | |
132 } | |
133 | |
134 static int get_data (int s, char *buf, size_t count) | |
135 { | |
136 ssize_t len, total; | |
137 total = 0; | |
138 | |
139 while (total < count) { | |
140 | |
141 len = read (s, &buf[total], count-total); | |
142 | |
143 if (len<0) { | |
144 perror ("read error:"); | |
145 return 0; | |
146 } | |
147 | |
148 total += len; | |
149 | |
150 if (len != 0) { | |
151 // printf ("[%d/%d]", total, count); | |
152 fflush (stdout); | |
153 } | |
154 | |
155 } | |
156 | |
157 return 1; | |
158 | |
159 } | |
160 | |
161 static int get_header (int s, uint8_t *header, streaming_ctrl_t *streaming_ctrl) | |
162 { | |
163 unsigned char pre_header[8]; | |
164 int header_len,i ; | |
165 | |
166 header_len = 0; | |
167 | |
168 while (1) { | |
169 if (!get_data (s, pre_header, 8)) { | |
170 printf ("pre-header read failed\n"); | |
171 return 0; | |
172 } | |
173 if (pre_header[4] == 0x02) { | |
174 | |
175 int packet_len; | |
176 | |
177 packet_len = (pre_header[7] << 8 | pre_header[6]) - 8; | |
178 | |
179 // printf ("asf header packet detected, len=%d\n", packet_len); | |
180 | |
181 if (!get_data (s, &header[header_len], packet_len)) { | |
182 printf ("header data read failed\n"); | |
183 return 0; | |
184 } | |
185 | |
186 header_len += packet_len; | |
187 | |
188 if ( (header[header_len-1] == 1) && (header[header_len-2]==1)) { | |
189 | |
190 | |
191 if( streaming_bufferize( streaming_ctrl, header, header_len )<0 ) { | |
192 return -1; | |
193 } | |
194 | |
195 // printf ("get header packet finished\n"); | |
196 | |
197 return (header_len); | |
198 | |
199 } | |
200 | |
201 } else { | |
202 | |
7880 | 203 int packet_len; |
6092 | 204 int command; |
205 char data[BUF_SIZE]; | |
206 | |
207 if (!get_data (s, &packet_len, 4)) { | |
208 printf ("packet_len read failed\n"); | |
209 return 0; | |
210 } | |
211 | |
212 packet_len = get_32 (&packet_len, 0) + 4; | |
213 | |
214 // printf ("command packet detected, len=%d\n", packet_len); | |
215 | |
216 if (!get_data (s, data, packet_len)) { | |
217 printf ("command data read failed\n"); | |
218 return 0; | |
219 } | |
220 | |
221 command = get_32 (data, 24) & 0xFFFF; | |
222 | |
223 // printf ("command: %02x\n", command); | |
224 | |
225 if (command == 0x1b) | |
226 send_command (s, 0x1b, 0, 0, 0, data); | |
227 | |
228 } | |
229 | |
230 // printf ("get header packet succ\n"); | |
231 } | |
232 } | |
233 | |
7880 | 234 static int interp_header (uint8_t *header, int header_len) |
6092 | 235 { |
236 int i; | |
7472
c4434bdf6e51
tons of warning fixes, also some 10l bugfixes, including Dominik's PVA bug
arpi
parents:
7309
diff
changeset
|
237 int packet_length=-1; |
6092 | 238 |
239 /* | |
240 * parse header | |
241 */ | |
242 | |
243 i = 30; | |
244 while (i<header_len) { | |
245 | |
246 uint64_t guid_1, guid_2, length; | |
247 | |
248 guid_2 = (uint64_t)header[i] | ((uint64_t)header[i+1]<<8) | |
249 | ((uint64_t)header[i+2]<<16) | ((uint64_t)header[i+3]<<24) | |
250 | ((uint64_t)header[i+4]<<32) | ((uint64_t)header[i+5]<<40) | |
251 | ((uint64_t)header[i+6]<<48) | ((uint64_t)header[i+7]<<56); | |
252 i += 8; | |
253 | |
254 guid_1 = (uint64_t)header[i] | ((uint64_t)header[i+1]<<8) | |
255 | ((uint64_t)header[i+2]<<16) | ((uint64_t)header[i+3]<<24) | |
256 | ((uint64_t)header[i+4]<<32) | ((uint64_t)header[i+5]<<40) | |
257 | ((uint64_t)header[i+6]<<48) | ((uint64_t)header[i+7]<<56); | |
258 i += 8; | |
259 | |
260 // printf ("guid found: %016llx%016llx\n", guid_1, guid_2); | |
261 | |
262 length = (uint64_t)header[i] | ((uint64_t)header[i+1]<<8) | |
263 | ((uint64_t)header[i+2]<<16) | ((uint64_t)header[i+3]<<24) | |
264 | ((uint64_t)header[i+4]<<32) | ((uint64_t)header[i+5]<<40) | |
265 | ((uint64_t)header[i+6]<<48) | ((uint64_t)header[i+7]<<56); | |
266 | |
267 i += 8; | |
268 | |
269 if ( (guid_1 == 0x6cce6200aa00d9a6) && (guid_2 == 0x11cf668e75b22630) ) { | |
270 printf ("header object\n"); | |
271 } else if ((guid_1 == 0x6cce6200aa00d9a6) && (guid_2 == 0x11cf668e75b22636)) { | |
272 printf ("data object\n"); | |
273 } else if ((guid_1 == 0x6553200cc000e48e) && (guid_2 == 0x11cfa9478cabdca1)) { | |
274 | |
275 packet_length = get_32(header, i+92-24); | |
276 | |
277 printf ("file object, packet length = %d (%d)\n", | |
278 packet_length, get_32(header, i+96-24)); | |
279 | |
280 | |
281 } else if ((guid_1 == 0x6553200cc000e68e) && (guid_2 == 0x11cfa9b7b7dc0791)) { | |
282 | |
283 int stream_id = header[i+48] | header[i+49] << 8; | |
284 | |
285 printf ("stream object, stream id: %d\n", stream_id); | |
286 | |
287 stream_ids[num_stream_ids] = stream_id; | |
288 num_stream_ids++; | |
289 | |
290 } else { | |
291 printf ("unknown object\n"); | |
292 } | |
293 | |
294 // printf ("length : %lld\n", length); | |
295 | |
296 i += length-24; | |
297 | |
298 } | |
299 | |
300 return packet_length; | |
301 | |
302 } | |
303 | |
304 | |
7880 | 305 static int get_media_packet (int s, int padding, streaming_ctrl_t *stream_ctrl) { |
6092 | 306 unsigned char pre_header[8]; |
7880 | 307 int i; |
6092 | 308 char data[BUF_SIZE]; |
7880 | 309 |
310 if (!get_data (s, pre_header, 8)) { | |
311 printf ("pre-header read failed\n"); | |
312 return 0; | |
313 } | |
314 | |
315 // for (i=0; i<8; i++) | |
316 // printf ("pre_header[%d] = %02x (%d)\n", | |
317 // i, pre_header[i], pre_header[i]); | |
318 | |
319 if (pre_header[4] == 0x04) { | |
320 | |
321 int packet_len; | |
322 | |
323 packet_len = (pre_header[7] << 8 | pre_header[6]) - 8; | |
324 | |
325 // printf ("asf media packet detected, len=%d\n", packet_len); | |
326 | |
327 if (!get_data (s, data, packet_len)) { | |
328 printf ("media data read failed\n"); | |
329 return 0; | |
330 } | |
331 | |
332 streaming_bufferize(stream_ctrl, data, padding); | |
333 | |
334 } else { | |
335 | |
336 int packet_len, command; | |
337 | |
338 if (!get_data (s, &packet_len, 4)) { | |
339 printf ("packet_len read failed\n"); | |
340 return 0; | |
341 } | |
342 | |
343 packet_len = get_32 (&packet_len, 0) + 4; | |
344 | |
345 if (!get_data (s, data, packet_len)) { | |
346 printf ("command data read failed\n"); | |
347 return 0; | |
348 } | |
349 | |
350 if ( (pre_header[7] != 0xb0) || (pre_header[6] != 0x0b) | |
351 || (pre_header[5] != 0xfa) || (pre_header[4] != 0xce) ) { | |
352 | |
353 printf ("missing signature\n"); | |
354 return -1; | |
355 } | |
356 | |
357 command = get_32 (data, 24) & 0xFFFF; | |
358 | |
359 printf ("\ncommand packet detected, len=%d cmd=0x%X\n", packet_len, command); | |
360 | |
361 if (command == 0x1b) | |
362 send_command (s, 0x1b, 0, 0, 0, data); | |
363 else if (command == 0x1e) { | |
364 printf ("everything done. Thank you for downloading a media file containing proprietary and patentend technology.\n"); | |
365 return 0; | |
366 } | |
367 else if (command == 0x21 ) { | |
368 // Looks like it's new in WMS9 | |
369 // Unknown command, but ignoring it seems to work. | |
370 return 0; | |
371 } | |
372 else if (command != 0x05) { | |
373 printf ("unknown command %02x\n", command); | |
374 return -1; | |
375 } | |
376 } | |
377 | |
378 // printf ("get media packet succ\n"); | |
379 | |
380 return 1; | |
381 } | |
6092 | 382 |
383 | |
7880 | 384 static int packet_length1; |
6092 | 385 |
386 int | |
387 asf_mmst_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) | |
388 { | |
7880 | 389 int len; |
390 | |
391 while( stream_ctrl->buffer_size==0 ) { | |
392 // buffer is empty - fill it! | |
393 int ret = get_media_packet( fd, packet_length1, stream_ctrl); | |
394 if( ret<0 ) { | |
395 printf("get_media_packet error : %s\n",strerror(errno)); | |
396 return -1; | |
397 } | |
398 } | |
399 | |
400 len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos; | |
401 if(len>size) len=size; | |
6092 | 402 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len ); |
403 stream_ctrl->buffer_pos += len; | |
404 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) { | |
405 free( stream_ctrl->buffer ); | |
406 stream_ctrl->buffer = NULL; | |
407 stream_ctrl->buffer_size = 0; | |
408 stream_ctrl->buffer_pos = 0; | |
409 } | |
7880 | 410 return len; |
6092 | 411 |
412 } | |
413 | |
414 int | |
415 asf_mmst_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) | |
416 { | |
417 return -1; | |
418 } | |
419 | |
420 int asf_mmst_streaming_start(stream_t *stream) | |
421 { | |
422 char str[1024]; | |
423 char data[1024]; | |
424 uint8_t asf_header[8192]; | |
425 int asf_header_len; | |
426 int len, i, packet_length; | |
427 char *path; | |
428 URL_t *url1 = stream->streaming_ctrl->url; | |
7880 | 429 int s; |
6092 | 430 |
7250
27a1315d6af4
Checked if the connection succeeded before writing in the socket.
bertrand
parents:
6092
diff
changeset
|
431 if( s>0 ) { |
27a1315d6af4
Checked if the connection succeeded before writing in the socket.
bertrand
parents:
6092
diff
changeset
|
432 close( stream->fd ); |
27a1315d6af4
Checked if the connection succeeded before writing in the socket.
bertrand
parents:
6092
diff
changeset
|
433 stream->fd = -1; |
27a1315d6af4
Checked if the connection succeeded before writing in the socket.
bertrand
parents:
6092
diff
changeset
|
434 } |
27a1315d6af4
Checked if the connection succeeded before writing in the socket.
bertrand
parents:
6092
diff
changeset
|
435 |
6092 | 436 /* parse url */ |
437 path = strchr(url1->file,'/') + 1; | |
438 | |
439 url1->port=1755; | |
440 s = connect2Server( url1->hostname, url1->port ); | |
7250
27a1315d6af4
Checked if the connection succeeded before writing in the socket.
bertrand
parents:
6092
diff
changeset
|
441 if( s<0 ) { |
27a1315d6af4
Checked if the connection succeeded before writing in the socket.
bertrand
parents:
6092
diff
changeset
|
442 return s; |
27a1315d6af4
Checked if the connection succeeded before writing in the socket.
bertrand
parents:
6092
diff
changeset
|
443 } |
6092 | 444 printf ("connected\n"); |
7880 | 445 |
446 seq_num=0; | |
6092 | 447 |
448 /* | |
449 * Send the initial connect info including player version no. Client GUID (random) and the host address being connected to. | |
450 * This command is sent at the very start of protocol initiation. It sends local information to the serve | |
451 * cmd 1 0x01 | |
452 * */ | |
453 | |
7880 | 454 sprintf (str, "\034\003NSPlayer/7.0.0.1956; {33715801-BAB3-9D85-24E9-03B90328270A}; Host: %s", url1->hostname); |
6092 | 455 string_utf16 (data, str, strlen(str)+2); |
456 // send_command(s, commandno ....) | |
457 send_command (s, 1, 0, 0x0004000b, strlen(str) * 2+8, data); | |
458 | |
459 len = read (s, data, BUF_SIZE) ; | |
460 | |
461 /*This sends details of the local machine IP address to a Funnel system at the server. | |
462 * Also, the TCP or UDP transport selection is sent. | |
463 * | |
464 * here 192.168.0.129 is local ip address TCP/UDP states the tronsport we r using | |
465 * and 1037 is the local TCP or UDP socket number | |
466 * cmd 2 0x02 | |
467 * */ | |
468 | |
469 string_utf16 (&data[8], "\002\000\\\\192.168.0.1\\TCP\\1037\0000", | |
470 28); | |
471 memset (data, 0, 8); | |
472 send_command (s, 2, 0, 0, 28*2+8, data); | |
473 | |
474 len = read (s, data, BUF_SIZE) ; | |
475 | |
476 /* This command sends file path (at server) and file name request to the server. | |
477 * 0x5 */ | |
478 | |
479 string_utf16 (&data[8], path, strlen(path)); | |
480 memset (data, 0, 8); | |
481 send_command (s, 5, 0, 0, strlen(path)*2+12, data); | |
482 | |
483 get_answer (s); | |
484 | |
485 /* The ASF header chunk request. Includes ?session' variable for pre header value. | |
486 * After this command is sent, | |
487 * the server replies with 0x11 command and then the header chunk with header data follows. | |
488 * 0x15 */ | |
489 | |
490 memset (data, 0, 40); | |
491 data[32] = 2; | |
492 | |
493 send_command (s, 0x15, 1, 0, 40, data); | |
494 | |
495 num_stream_ids = 0; | |
496 /* get_headers(s, asf_header); */ | |
497 | |
498 asf_header_len = get_header (s, asf_header, stream->streaming_ctrl); | |
499 // printf("---------------------------------- asf_header %d\n",asf_header); | |
500 packet_length = interp_header (asf_header, asf_header_len); | |
501 | |
502 | |
503 /* | |
504 * This command is the media stream MBR selector. Switches are always 6 bytes in length. | |
505 * After all switch elements, data ends with bytes [00 00] 00 20 ac 40 [02]. | |
506 * Where: | |
507 * [00 00] shows 0x61 0x00 (on the first 33 sent) or 0xff 0xff for ASF files, and with no ending data for WMV files. | |
508 * It is not yet understood what all this means. | |
509 * And the last [02] byte is probably the header ?session' value. | |
510 * | |
511 * 0x33 */ | |
512 | |
513 memset (data, 0, 40); | |
514 | |
515 for (i=1; i<num_stream_ids; i++) { | |
516 data [ (i-1) * 6 + 2 ] = 0xFF; | |
517 data [ (i-1) * 6 + 3 ] = 0xFF; | |
518 data [ (i-1) * 6 + 4 ] = stream_ids[i]; | |
519 data [ (i-1) * 6 + 5 ] = 0x00; | |
520 } | |
521 | |
522 send_command (s, 0x33, num_stream_ids, 0xFFFF | stream_ids[0] << 16, (num_stream_ids-1)*6+2 , data); | |
523 | |
524 get_answer (s); | |
525 | |
526 /* Start sending file from packet xx. | |
527 * This command is also used for resume downloads or requesting a lost packet. | |
528 * Also used for seeking by sending a play point value which seeks to the media time point. | |
529 * Includes ?session' value in pre header and the maximum media stream time. | |
530 * 0x07 */ | |
531 | |
532 memset (data, 0, 40); | |
533 | |
534 for (i=8; i<16; i++) | |
535 data[i] = 0xFF; | |
536 | |
537 data[20] = 0x04; | |
538 | |
539 send_command (s, 0x07, 1, 0xFFFF | stream_ids[0] << 16, 24, data); | |
540 | |
7880 | 541 stream->fd = s; |
542 stream->streaming_ctrl->streaming_read = asf_mmst_streaming_read; | |
543 stream->streaming_ctrl->streaming_seek = asf_mmst_streaming_seek; | |
544 stream->streaming_ctrl->buffering = 1; | |
545 stream->streaming_ctrl->status = streaming_playing_e; | |
6092 | 546 |
7880 | 547 packet_length1 = packet_length; |
548 printf("mmst packet_length = %d\n",packet_length); | |
6092 | 549 |
550 return 0; | |
551 } |