8570
|
1 /*
|
|
2 * Copyright (C) 2000-2002 the xine project
|
|
3 *
|
|
4 * This file is part of xine, a free video player.
|
|
5 *
|
|
6 * xine is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * xine is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License
|
|
17 * along with this program; if not, write to the Free Software
|
|
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
19 *
|
|
20 * $Id$
|
|
21 *
|
|
22 * pnm protocol implementation
|
|
23 * based upon code from joschka
|
|
24 */
|
|
25
|
|
26 #include <unistd.h>
|
|
27 #include <stdio.h>
|
|
28 #include <assert.h>
|
|
29 //#include <sys/socket.h>
|
|
30 //#include <netinet/in.h>
|
|
31 //#include <netdb.h>
|
|
32 #include <string.h>
|
|
33 #include <sys/stat.h>
|
|
34 #include <fcntl.h>
|
|
35 #include <errno.h>
|
|
36 #include <stdlib.h>
|
|
37 #include <sys/time.h>
|
8584
|
38 #include <inttypes.h>
|
8570
|
39
|
|
40 #include "pnm.h"
|
|
41 //#include "libreal/rmff.h"
|
|
42
|
|
43 #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \
|
|
44 (((long)(unsigned char)(ch3) ) | \
|
|
45 ( (long)(unsigned char)(ch2) << 8 ) | \
|
|
46 ( (long)(unsigned char)(ch1) << 16 ) | \
|
|
47 ( (long)(unsigned char)(ch0) << 24 ) )
|
|
48
|
|
49
|
|
50 #define RMF_TAG FOURCC_TAG('.', 'R', 'M', 'F')
|
|
51 #define PROP_TAG FOURCC_TAG('P', 'R', 'O', 'P')
|
|
52 #define MDPR_TAG FOURCC_TAG('M', 'D', 'P', 'R')
|
|
53 #define CONT_TAG FOURCC_TAG('C', 'O', 'N', 'T')
|
|
54 #define DATA_TAG FOURCC_TAG('D', 'A', 'T', 'A')
|
|
55 #define INDX_TAG FOURCC_TAG('I', 'N', 'D', 'X')
|
|
56 #define PNA_TAG FOURCC_TAG('P', 'N', 'A', 0 )
|
|
57
|
|
58 /*
|
|
59 #define LOG
|
|
60 */
|
|
61
|
8880
|
62 #define BUF_SIZE 4096
|
|
63 #define HEADER_SIZE 4096
|
8570
|
64
|
|
65 struct pnm_s {
|
|
66
|
|
67 int s;
|
|
68
|
|
69 // char *host;
|
|
70 // int port;
|
|
71 char *path;
|
|
72 // char *url;
|
|
73
|
|
74 char buffer[BUF_SIZE]; /* scratch buffer */
|
|
75
|
|
76 /* receive buffer */
|
|
77 uint8_t recv[BUF_SIZE];
|
|
78 int recv_size;
|
|
79 int recv_read;
|
|
80
|
|
81 uint8_t header[HEADER_SIZE];
|
|
82 int header_len;
|
|
83 int header_read;
|
|
84 unsigned int seq_num[4]; /* two streams with two indices */
|
|
85 unsigned int seq_current[2]; /* seqs of last stream chunk read */
|
|
86 uint32_t ts_current; /* timestamp of current chunk */
|
|
87 uint32_t ts_last[2]; /* timestamps of last chunks */
|
|
88 unsigned int packet; /* number of last recieved packet */
|
|
89 };
|
|
90
|
|
91 /*
|
|
92 * utility macros
|
|
93 */
|
|
94
|
|
95 #define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
|
|
96 #define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
|
|
97 (((uint8_t*)(x))[1] << 16) | \
|
|
98 (((uint8_t*)(x))[2] << 8) | \
|
|
99 ((uint8_t*)(x))[3])
|
|
100
|
|
101 /* D means direct (no pointer) */
|
|
102 #define BE_16D(x) ((x & 0xff00) >> 8)|((x & 0x00ff) << 8)
|
|
103
|
|
104 /* sizes */
|
|
105 #define PREAMBLE_SIZE 8
|
|
106 #define CHECKSUM_SIZE 3
|
|
107
|
|
108
|
|
109 /* header of rm files */
|
|
110 #define RM_HEADER_SIZE 0x12
|
|
111 const unsigned char rm_header[]={
|
|
112 0x2e, 0x52, 0x4d, 0x46, /* object_id ".RMF" */
|
|
113 0x00, 0x00, 0x00, 0x12, /* header_size 0x12 */
|
|
114 0x00, 0x00, /* object_version 0x00 */
|
|
115 0x00, 0x00, 0x00, 0x00, /* file_version 0x00 */
|
|
116 0x00, 0x00, 0x00, 0x06 /* num_headers 0x06 */
|
|
117 };
|
|
118
|
|
119 /* data chunk header */
|
|
120 #define PNM_DATA_HEADER_SIZE 18
|
|
121 const unsigned char pnm_data_header[]={
|
|
122 'D','A','T','A',
|
|
123 0,0,0,0, /* data chunk size */
|
|
124 0,0, /* object version */
|
|
125 0,0,0,0, /* num packets */
|
|
126 0,0,0,0}; /* next data header */
|
|
127
|
|
128 /* pnm request chunk ids */
|
|
129
|
|
130 #define PNA_CLIENT_CAPS 0x03
|
|
131 #define PNA_CLIENT_CHALLANGE 0x04
|
|
132 #define PNA_BANDWIDTH 0x05
|
|
133 #define PNA_GUID 0x13
|
|
134 #define PNA_TIMESTAMP 0x17
|
|
135 #define PNA_TWENTYFOUR 0x18
|
|
136
|
|
137 #define PNA_CLIENT_STRING 0x63
|
|
138 #define PNA_PATH_REQUEST 0x52
|
|
139
|
|
140 const unsigned char pnm_challenge[] = "0990f6b4508b51e801bd6da011ad7b56";
|
|
141 const unsigned char pnm_timestamp[] = "[15/06/1999:22:22:49 00:00]";
|
|
142 const unsigned char pnm_guid[] = "3eac2411-83d5-11d2-f3ea-d7c3a51aa8b0";
|
|
143 const unsigned char pnm_response[] = "97715a899cbe41cee00dd434851535bf";
|
|
144 const unsigned char client_string[] = "WinNT_4.0_6.0.6.45_plus32_MP60_en-US_686l";
|
|
145
|
|
146 #define PNM_HEADER_SIZE 11
|
|
147 const unsigned char pnm_header[] = {
|
|
148 'P','N','A',
|
|
149 0x00, 0x0a,
|
|
150 0x00, 0x14,
|
|
151 0x00, 0x02,
|
|
152 0x00, 0x01 };
|
|
153
|
|
154 #define PNM_CLIENT_CAPS_SIZE 126
|
|
155 const unsigned char pnm_client_caps[] = {
|
|
156 0x07, 0x8a, 'p','n','r','v',
|
|
157 0, 0x90, 'p','n','r','v',
|
|
158 0, 0x64, 'd','n','e','t',
|
|
159 0, 0x46, 'p','n','r','v',
|
|
160 0, 0x32, 'd','n','e','t',
|
|
161 0, 0x2b, 'p','n','r','v',
|
|
162 0, 0x28, 'd','n','e','t',
|
|
163 0, 0x24, 'p','n','r','v',
|
|
164 0, 0x19, 'd','n','e','t',
|
|
165 0, 0x18, 'p','n','r','v',
|
|
166 0, 0x14, 's','i','p','r',
|
|
167 0, 0x14, 'd','n','e','t',
|
|
168 0, 0x24, '2','8','_','8',
|
|
169 0, 0x12, 'p','n','r','v',
|
|
170 0, 0x0f, 'd','n','e','t',
|
|
171 0, 0x0a, 's','i','p','r',
|
|
172 0, 0x0a, 'd','n','e','t',
|
|
173 0, 0x08, 's','i','p','r',
|
|
174 0, 0x06, 's','i','p','r',
|
|
175 0, 0x12, 'l','p','c','J',
|
|
176 0, 0x07, '0','5','_','6' };
|
|
177
|
|
178 const uint32_t pnm_default_bandwidth=10485800;
|
|
179 const uint32_t pnm_available_bandwidths[]={14400,19200,28800,33600,34430,57600,
|
|
180 115200,262200,393216,524300,1544000,10485800};
|
|
181
|
|
182 #define PNM_TWENTYFOUR_SIZE 16
|
|
183 unsigned char pnm_twentyfour[]={
|
|
184 0xd5, 0x42, 0xa3, 0x1b, 0xef, 0x1f, 0x70, 0x24,
|
|
185 0x85, 0x29, 0xb3, 0x8d, 0xba, 0x11, 0xf3, 0xd6 };
|
|
186
|
|
187 /* now other data follows. marked with 0x0000 at the beginning */
|
|
188 int after_chunks_length=6;
|
|
189 unsigned char after_chunks[]={
|
|
190 0x00, 0x00, /* mark */
|
|
191
|
|
192 0x50, 0x84, /* seems to be fixated */
|
|
193 0x1f, 0x3a /* varies on each request (checksum ?)*/
|
|
194 };
|
|
195
|
|
196 static void hexdump (char *buf, int length);
|
|
197
|
|
198 static int rm_write(int s, const char *buf, int len) {
|
|
199 int total, timeout;
|
|
200
|
|
201 total = 0; timeout = 30;
|
|
202 while (total < len){
|
|
203 int n;
|
|
204
|
|
205 n = write (s, &buf[total], len - total);
|
|
206
|
|
207 if (n > 0)
|
|
208 total += n;
|
|
209 else if (n < 0) {
|
|
210 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) {
|
|
211 sleep (1); timeout--;
|
|
212 } else
|
|
213 return -1;
|
|
214 }
|
|
215 }
|
|
216
|
|
217 return total;
|
|
218 }
|
|
219
|
|
220 static ssize_t rm_read(int fd, void *buf, size_t count) {
|
|
221
|
|
222 ssize_t ret, total;
|
|
223
|
|
224 total = 0;
|
|
225
|
|
226 while (total < count) {
|
|
227
|
|
228 fd_set rset;
|
|
229 struct timeval timeout;
|
|
230
|
|
231 FD_ZERO (&rset);
|
|
232 FD_SET (fd, &rset);
|
|
233
|
|
234 timeout.tv_sec = 3;
|
|
235 timeout.tv_usec = 0;
|
|
236
|
|
237 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) {
|
|
238 return -1;
|
|
239 }
|
|
240
|
|
241 ret=read (fd, ((uint8_t*)buf)+total, count-total);
|
|
242
|
|
243 if (ret<=0) {
|
|
244 printf ("input_pnm: read error.\n");
|
|
245 return ret;
|
|
246 } else
|
|
247 total += ret;
|
|
248 }
|
|
249
|
|
250 return total;
|
|
251 }
|
|
252
|
|
253 /*
|
|
254 * debugging utilities
|
|
255 */
|
|
256
|
|
257 static void hexdump (char *buf, int length) {
|
|
258
|
|
259 int i;
|
|
260
|
|
261 printf ("input_pnm: ascii>");
|
|
262 for (i = 0; i < length; i++) {
|
|
263 unsigned char c = buf[i];
|
|
264
|
|
265 if ((c >= 32) && (c <= 128))
|
|
266 printf ("%c", c);
|
|
267 else
|
|
268 printf (".");
|
|
269 }
|
|
270 printf ("\n");
|
|
271
|
|
272 printf ("input_pnm: hexdump> ");
|
|
273 for (i = 0; i < length; i++) {
|
|
274 unsigned char c = buf[i];
|
|
275
|
|
276 printf ("%02x", c);
|
|
277
|
|
278 if ((i % 16) == 15)
|
|
279 printf ("\npnm: ");
|
|
280
|
|
281 if ((i % 2) == 1)
|
|
282 printf (" ");
|
|
283
|
|
284 }
|
|
285 printf ("\n");
|
|
286 }
|
|
287
|
|
288 /*
|
|
289 * pnm_get_chunk gets a chunk from stream
|
|
290 * and returns number of bytes read
|
|
291 */
|
|
292
|
|
293 static unsigned int pnm_get_chunk(pnm_t *p,
|
|
294 unsigned int max,
|
|
295 unsigned int *chunk_type,
|
|
296 char *data, int *need_response) {
|
|
297
|
|
298 unsigned int chunk_size;
|
|
299 int n;
|
|
300 char *ptr;
|
|
301
|
|
302 /* get first PREAMBLE_SIZE bytes and ignore checksum */
|
|
303 rm_read (p->s, data, CHECKSUM_SIZE);
|
|
304 if (data[0] == 0x72)
|
|
305 rm_read (p->s, data, PREAMBLE_SIZE);
|
|
306 else
|
|
307 rm_read (p->s, data+CHECKSUM_SIZE, PREAMBLE_SIZE-CHECKSUM_SIZE);
|
|
308
|
|
309 *chunk_type = BE_32(data);
|
|
310 chunk_size = BE_32(data+4);
|
|
311
|
|
312 switch (*chunk_type) {
|
|
313 case PNA_TAG:
|
|
314 *need_response=0;
|
|
315 ptr=data+PREAMBLE_SIZE;
|
|
316 rm_read (p->s, ptr++, 1);
|
|
317
|
|
318 while(1) {
|
|
319 /* expecting following chunk format: 0x4f <chunk size> <data...> */
|
|
320
|
|
321 rm_read (p->s, ptr, 2);
|
|
322 if (*ptr == 'X') /* checking for server message */
|
|
323 {
|
|
324 printf("input_pnm: got a message from server:\n");
|
|
325 rm_read (p->s, ptr+2, 1);
|
|
326 n=BE_16(ptr+1);
|
|
327 rm_read (p->s, ptr+3, n);
|
|
328 ptr[3+n]=0;
|
|
329 printf("%s\n",ptr+3);
|
|
330 return -1;
|
|
331 }
|
|
332
|
|
333 if (*ptr == 'F') /* checking for server error */
|
|
334 {
|
|
335 printf("input_pnm: server error.\n");
|
|
336 return -1;
|
|
337 }
|
|
338 if (*ptr == 'i')
|
|
339 {
|
|
340 ptr+=2;
|
|
341 *need_response=1;
|
|
342 continue;
|
|
343 }
|
|
344 if (*ptr != 0x4f) break;
|
|
345 n=ptr[1];
|
|
346 rm_read (p->s, ptr+2, n);
|
|
347 ptr+=(n+2);
|
|
348 }
|
|
349 /* the checksum of the next chunk is ignored here */
|
|
350 rm_read (p->s, ptr+2, 1);
|
|
351 ptr+=3;
|
|
352 chunk_size=ptr-data;
|
|
353 break;
|
|
354 case RMF_TAG:
|
|
355 case DATA_TAG:
|
|
356 case PROP_TAG:
|
|
357 case MDPR_TAG:
|
|
358 case CONT_TAG:
|
|
359 if (chunk_size > max) {
|
|
360 printf("error: max chunk size exeeded (max was 0x%04x)\n", max);
|
|
361 n=rm_read (p->s, &data[PREAMBLE_SIZE], 0x100 - PREAMBLE_SIZE);
|
|
362 hexdump(data,n+PREAMBLE_SIZE);
|
|
363 return -1;
|
|
364 }
|
|
365 rm_read (p->s, &data[PREAMBLE_SIZE], chunk_size-PREAMBLE_SIZE);
|
|
366 break;
|
|
367 default:
|
|
368 *chunk_type = 0;
|
|
369 chunk_size = PREAMBLE_SIZE;
|
|
370 break;
|
|
371 }
|
|
372
|
|
373 return chunk_size;
|
|
374 }
|
|
375
|
|
376 /*
|
|
377 * writes a chunk to a buffer, returns number of bytes written
|
|
378 */
|
|
379
|
|
380 static int pnm_write_chunk(uint16_t chunk_id, uint16_t length,
|
|
381 const char *chunk, char *data) {
|
|
382
|
|
383 data[0]=(chunk_id>>8)%0xff;
|
|
384 data[1]=chunk_id%0xff;
|
|
385 data[2]=(length>>8)%0xff;
|
|
386 data[3]=length%0xff;
|
|
387 memcpy(&data[4],chunk,length);
|
|
388
|
|
389 return length+4;
|
|
390 }
|
|
391
|
|
392 /*
|
|
393 * constructs a request and sends it
|
|
394 */
|
|
395
|
|
396 static void pnm_send_request(pnm_t *p, uint32_t bandwidth) {
|
|
397
|
|
398 uint16_t i16;
|
|
399 int c=PNM_HEADER_SIZE;
|
|
400 char fixme[]={0,1};
|
|
401
|
|
402 memcpy(p->buffer,pnm_header,PNM_HEADER_SIZE);
|
|
403 c+=pnm_write_chunk(PNA_CLIENT_CHALLANGE,strlen(pnm_challenge),
|
|
404 pnm_challenge,&p->buffer[c]);
|
|
405 c+=pnm_write_chunk(PNA_CLIENT_CAPS,PNM_CLIENT_CAPS_SIZE,
|
|
406 pnm_client_caps,&p->buffer[c]);
|
|
407 c+=pnm_write_chunk(0x0a,0,NULL,&p->buffer[c]);
|
|
408 c+=pnm_write_chunk(0x0c,0,NULL,&p->buffer[c]);
|
|
409 c+=pnm_write_chunk(0x0d,0,NULL,&p->buffer[c]);
|
|
410 c+=pnm_write_chunk(0x16,2,fixme,&p->buffer[c]);
|
|
411 c+=pnm_write_chunk(PNA_TIMESTAMP,strlen(pnm_timestamp),
|
|
412 pnm_timestamp,&p->buffer[c]);
|
|
413 c+=pnm_write_chunk(PNA_BANDWIDTH,4,
|
|
414 (const char *)&pnm_default_bandwidth,&p->buffer[c]);
|
|
415 c+=pnm_write_chunk(0x08,0,NULL,&p->buffer[c]);
|
|
416 c+=pnm_write_chunk(0x0e,0,NULL,&p->buffer[c]);
|
|
417 c+=pnm_write_chunk(0x0f,0,NULL,&p->buffer[c]);
|
|
418 c+=pnm_write_chunk(0x11,0,NULL,&p->buffer[c]);
|
|
419 c+=pnm_write_chunk(0x10,0,NULL,&p->buffer[c]);
|
|
420 c+=pnm_write_chunk(0x15,0,NULL,&p->buffer[c]);
|
|
421 c+=pnm_write_chunk(0x12,0,NULL,&p->buffer[c]);
|
|
422 c+=pnm_write_chunk(PNA_GUID,strlen(pnm_guid),
|
|
423 pnm_guid,&p->buffer[c]);
|
|
424 c+=pnm_write_chunk(PNA_TWENTYFOUR,PNM_TWENTYFOUR_SIZE,
|
|
425 pnm_twentyfour,&p->buffer[c]);
|
|
426
|
|
427 /* data after chunks */
|
|
428 memcpy(&p->buffer[c],after_chunks,after_chunks_length);
|
|
429 c+=after_chunks_length;
|
|
430
|
|
431 /* client id string */
|
|
432 p->buffer[c]=PNA_CLIENT_STRING;
|
|
433 i16=BE_16D((strlen(client_string)-1)); /* dont know why do we have -1 here */
|
|
434 memcpy(&p->buffer[c+1],&i16,2);
|
|
435 memcpy(&p->buffer[c+3],client_string,strlen(client_string)+1);
|
|
436 c=c+3+strlen(client_string)+1;
|
|
437
|
|
438 /* file path */
|
|
439 p->buffer[c]=0;
|
|
440 p->buffer[c+1]=PNA_PATH_REQUEST;
|
|
441 i16=BE_16D(strlen(p->path));
|
|
442 memcpy(&p->buffer[c+2],&i16,2);
|
|
443 memcpy(&p->buffer[c+4],p->path,strlen(p->path));
|
|
444 c=c+4+strlen(p->path);
|
|
445
|
|
446 /* some trailing bytes */
|
|
447 p->buffer[c]='y';
|
|
448 p->buffer[c+1]='B';
|
|
449
|
|
450 rm_write(p->s,p->buffer,c+2);
|
|
451 }
|
|
452
|
|
453 /*
|
|
454 * pnm_send_response sends a response of a challenge
|
|
455 */
|
|
456
|
|
457 static void pnm_send_response(pnm_t *p, const char *response) {
|
|
458
|
|
459 int size=strlen(response);
|
|
460
|
|
461 p->buffer[0]=0x23;
|
|
462 p->buffer[1]=0;
|
|
463 p->buffer[2]=(unsigned char) size;
|
|
464
|
|
465 memcpy(&p->buffer[3], response, size);
|
|
466
|
|
467 rm_write (p->s, p->buffer, size+3);
|
|
468
|
|
469 }
|
|
470
|
|
471 /*
|
|
472 * get headers and challenge and fix headers
|
|
473 * write headers to p->header
|
|
474 * write challenge to p->buffer
|
|
475 *
|
|
476 * return 0 on error. != 0 on success
|
|
477 */
|
|
478
|
|
479 static int pnm_get_headers(pnm_t *p, int *need_response) {
|
|
480
|
|
481 uint32_t chunk_type;
|
|
482 uint8_t *ptr=p->header;
|
|
483 uint8_t *prop_hdr=NULL;
|
|
484 int chunk_size,size=0;
|
|
485 int nr;
|
|
486 /* rmff_header_t *h; */
|
|
487
|
|
488 *need_response=0;
|
|
489
|
|
490 while(1) {
|
|
491 if (HEADER_SIZE-size<=0)
|
|
492 {
|
|
493 printf("input_pnm: header buffer overflow. exiting\n");
|
|
494 return 0;
|
|
495 }
|
|
496 chunk_size=pnm_get_chunk(p,HEADER_SIZE-size,&chunk_type,ptr,&nr);
|
|
497 if (chunk_size < 0) return 0;
|
|
498 if (chunk_type == 0) break;
|
|
499 if (chunk_type == PNA_TAG)
|
|
500 {
|
|
501 memcpy(ptr, rm_header, RM_HEADER_SIZE);
|
|
502 chunk_size=RM_HEADER_SIZE;
|
|
503 *need_response=nr;
|
|
504 }
|
|
505 if (chunk_type == DATA_TAG)
|
|
506 chunk_size=0;
|
|
507 if (chunk_type == RMF_TAG)
|
|
508 chunk_size=0;
|
|
509 if (chunk_type == PROP_TAG)
|
|
510 prop_hdr=ptr;
|
|
511 size+=chunk_size;
|
|
512 ptr+=chunk_size;
|
|
513 }
|
|
514
|
8852
|
515 if (!prop_hdr) {
|
|
516 printf("input_pnm: error while parsing headers.\n");
|
|
517 return 0;
|
|
518 }
|
|
519
|
8570
|
520 /* set data offset */
|
|
521 size--;
|
|
522 prop_hdr[42]=(size>>24)%0xff;
|
|
523 prop_hdr[43]=(size>>16)%0xff;
|
|
524 prop_hdr[44]=(size>>8)%0xff;
|
|
525 prop_hdr[45]=(size)%0xff;
|
|
526 size++;
|
|
527
|
|
528 /* read challenge */
|
|
529 memcpy (p->buffer, ptr, PREAMBLE_SIZE);
|
|
530 rm_read (p->s, &p->buffer[PREAMBLE_SIZE], 64);
|
|
531
|
|
532 /* now write a data header */
|
|
533 memcpy(ptr, pnm_data_header, PNM_DATA_HEADER_SIZE);
|
|
534 size+=PNM_DATA_HEADER_SIZE;
|
|
535 /*
|
|
536 h=rmff_scan_header(p->header);
|
|
537 rmff_fix_header(h);
|
|
538 p->header_len=rmff_get_header_size(h);
|
|
539 rmff_dump_header(h, p->header, HEADER_SIZE);
|
|
540 */
|
|
541 p->header_len=size;
|
|
542
|
|
543 return 1;
|
|
544 }
|
|
545
|
|
546 /*
|
|
547 * determine correct stream number by looking at indices
|
|
548 */
|
|
549
|
|
550 static int pnm_calc_stream(pnm_t *p) {
|
|
551
|
|
552 char str0=0,str1=0;
|
|
553
|
|
554 /* looking at the first index to
|
|
555 * find possible stream types
|
|
556 */
|
|
557 if (p->seq_current[0]==p->seq_num[0]) str0=1;
|
|
558 if (p->seq_current[0]==p->seq_num[2]) str1=1;
|
|
559
|
|
560 switch (str0+str1) {
|
|
561 case 1: /* one is possible, good. */
|
|
562 if (str0)
|
|
563 {
|
|
564 p->seq_num[0]++;
|
|
565 p->seq_num[1]=p->seq_current[1]+1;
|
|
566 return 0;
|
|
567 } else
|
|
568 {
|
|
569 p->seq_num[2]++;
|
|
570 p->seq_num[3]=p->seq_current[1]+1;
|
|
571 return 1;
|
|
572 }
|
|
573 break;
|
|
574 case 0:
|
|
575 case 2: /* both types or none possible, not so good */
|
|
576 /* try to figure out by second index */
|
|
577 if ( (p->seq_current[1] == p->seq_num[1])
|
|
578 &&(p->seq_current[1] != p->seq_num[3]))
|
|
579 {
|
|
580 /* ok, only stream0 matches */
|
|
581 p->seq_num[0]=p->seq_current[0]+1;
|
|
582 p->seq_num[1]++;
|
|
583 return 0;
|
|
584 }
|
|
585 if ( (p->seq_current[1] == p->seq_num[3])
|
|
586 &&(p->seq_current[1] != p->seq_num[1]))
|
|
587 {
|
|
588 /* ok, only stream1 matches */
|
|
589 p->seq_num[2]=p->seq_current[0]+1;
|
|
590 p->seq_num[3]++;
|
|
591 return 1;
|
|
592 }
|
|
593 /* wow, both streams match, or not. */
|
|
594 /* now we try to decide by timestamps */
|
|
595 if (p->ts_current < p->ts_last[1])
|
|
596 return 0;
|
|
597 if (p->ts_current < p->ts_last[0])
|
|
598 return 1;
|
|
599 /* does not help, we guess type 0 */
|
|
600 #ifdef LOG
|
|
601 printf("guessing stream# 0\n");
|
|
602 #endif
|
|
603 p->seq_num[0]=p->seq_current[0]+1;
|
|
604 p->seq_num[1]=p->seq_current[1]+1;
|
|
605 return 0;
|
|
606 break;
|
|
607 }
|
|
608 printf("input_pnm: wow, something very nasty happened in pnm_calc_stream\n");
|
|
609 return 2;
|
|
610 }
|
|
611
|
|
612 /*
|
|
613 * gets a stream chunk and writes it to a recieve buffer
|
|
614 */
|
|
615
|
|
616 static int pnm_get_stream_chunk(pnm_t *p) {
|
|
617
|
|
618 int n;
|
|
619 char keepalive='!';
|
|
620 unsigned int fof1, fof2, stream;
|
|
621
|
|
622 /* send a keepalive */
|
|
623 /* realplayer seems to do that every 43th package */
|
|
624 if ((p->packet%43) == 42)
|
|
625 {
|
|
626 rm_write(p->s,&keepalive,1);
|
|
627 }
|
|
628
|
|
629 /* data chunks begin with: 'Z' <o> <o> <i1> 'Z' <i2>
|
|
630 * where <o> is the offset to next stream chunk,
|
|
631 * <i1> is a 16 bit index
|
|
632 * <i2> is a 8 bit index which counts from 0x10 to somewhere
|
|
633 */
|
|
634
|
|
635 n = rm_read (p->s, p->buffer, 8);
|
|
636 if (n<8) return 0;
|
|
637
|
|
638 /* skip 8 bytes if 0x62 is read */
|
|
639 if (p->buffer[0] == 0x62)
|
|
640 {
|
|
641 n = rm_read (p->s, p->buffer, 8);
|
|
642 if (n<8) return 0;
|
|
643 #ifdef LOG
|
|
644 printf("input_pnm: had to seek 8 bytes on 0x62\n");
|
|
645 #endif
|
|
646 }
|
|
647
|
|
648 /* a server message */
|
|
649 if (p->buffer[0] == 'X')
|
|
650 {
|
|
651 int size=BE_16(&p->buffer[1]);
|
|
652
|
|
653 rm_read (p->s, &p->buffer[8], size-5);
|
|
654 p->buffer[size+3]=0;
|
|
655 printf("input_pnm: got message from server while reading stream:\n%s\n", &p->buffer[3]);
|
|
656 return 0;
|
|
657 }
|
|
658 if (p->buffer[0] == 'F')
|
|
659 {
|
|
660 printf("input_pnm: server error.\n");
|
|
661 return 0;
|
|
662 }
|
|
663
|
|
664 /* skip bytewise to next chunk.
|
|
665 * seems, that we dont need that, if we send enough
|
|
666 * keepalives
|
|
667 */
|
|
668 n=0;
|
|
669 while (p->buffer[0] != 0x5a) {
|
|
670 int i;
|
|
671 for (i=1; i<8; i++) {
|
|
672 p->buffer[i-1]=p->buffer[i];
|
|
673 }
|
|
674 rm_read (p->s, &p->buffer[7], 1);
|
|
675 n++;
|
|
676 }
|
|
677
|
|
678 #ifdef LOG
|
|
679 if (n) printf("input_pnm: had to seek %i bytes to next chunk\n", n);
|
|
680 #endif
|
|
681
|
|
682 /* check for 'Z's */
|
|
683 if ((p->buffer[0] != 0x5a)||(p->buffer[7] != 0x5a))
|
|
684 {
|
|
685 printf("input_pnm: bad boundaries\n");
|
|
686 hexdump(p->buffer, 8);
|
|
687 return 0;
|
|
688 }
|
|
689
|
|
690 /* check offsets */
|
|
691 fof1=BE_16(&p->buffer[1]);
|
|
692 fof2=BE_16(&p->buffer[3]);
|
|
693 if (fof1 != fof2)
|
|
694 {
|
|
695 printf("input_pnm: frame offsets are different: 0x%04x 0x%04x\n",fof1,fof2);
|
|
696 return 0;
|
|
697 }
|
|
698
|
|
699 /* get first index */
|
|
700 p->seq_current[0]=BE_16(&p->buffer[5]);
|
|
701
|
|
702 /* now read the rest of stream chunk */
|
|
703 n = rm_read (p->s, &p->recv[5], fof1-5);
|
|
704 if (n<(fof1-5)) return 0;
|
|
705
|
|
706 /* get second index */
|
|
707 p->seq_current[1]=p->recv[5];
|
|
708
|
|
709 /* get timestamp */
|
|
710 p->ts_current=BE_32(&p->recv[6]);
|
|
711
|
|
712 /* get stream number */
|
|
713 stream=pnm_calc_stream(p);
|
|
714
|
|
715 /* saving timestamp */
|
|
716 p->ts_last[stream]=p->ts_current;
|
|
717
|
|
718 /* constructing a data packet header */
|
|
719
|
|
720 p->recv[0]=0; /* object version */
|
|
721 p->recv[1]=0;
|
|
722
|
|
723 fof2=BE_16(&fof2);
|
|
724 memcpy(&p->recv[2], &fof2, 2);
|
|
725 /*p->recv[2]=(fof2>>8)%0xff;*/ /* length */
|
|
726 /*p->recv[3]=(fof2)%0xff;*/
|
|
727
|
|
728 p->recv[4]=0; /* stream number */
|
|
729 p->recv[5]=stream;
|
|
730
|
|
731 p->recv[10]=p->recv[10] & 0xfe; /* streambox seems to do that... */
|
|
732
|
|
733 p->packet++;
|
|
734
|
|
735 p->recv_size=fof1;
|
|
736
|
|
737 return fof1;
|
|
738 }
|
|
739
|
|
740 // pnm_t *pnm_connect(const char *mrl) {
|
|
741 pnm_t *pnm_connect(int fd, char *path) {
|
|
742
|
|
743 pnm_t *p=malloc(sizeof(pnm_t));
|
8852
|
744 int need_response=0;
|
8570
|
745
|
|
746 p->path=strdup(path);
|
|
747 p->s=fd;
|
|
748
|
|
749 pnm_send_request(p,pnm_available_bandwidths[10]);
|
|
750 if (!pnm_get_headers(p, &need_response)) {
|
|
751 printf ("input_pnm: failed to set up stream\n");
|
|
752 free(p->path);
|
|
753 free(p);
|
|
754 return NULL;
|
|
755 }
|
|
756 if (need_response)
|
|
757 pnm_send_response(p, pnm_response);
|
|
758 p->ts_last[0]=0;
|
|
759 p->ts_last[1]=0;
|
|
760
|
|
761 /* copy header to recv */
|
|
762
|
|
763 memcpy(p->recv, p->header, p->header_len);
|
|
764 p->recv_size = p->header_len;
|
|
765 p->recv_read = 0;
|
|
766
|
|
767 return p;
|
|
768 }
|
|
769
|
|
770 int pnm_read (pnm_t *this, char *data, int len) {
|
|
771
|
|
772 int to_copy=len;
|
|
773 char *dest=data;
|
|
774 char *source=this->recv + this->recv_read;
|
|
775 int fill=this->recv_size - this->recv_read;
|
|
776
|
|
777 if (len < 0) return 0;
|
|
778 while (to_copy > fill) {
|
|
779
|
|
780 memcpy(dest, source, fill);
|
|
781 to_copy -= fill;
|
|
782 dest += fill;
|
|
783 this->recv_read=0;
|
|
784
|
|
785 if (!pnm_get_stream_chunk (this)) {
|
|
786 #ifdef LOG
|
|
787 printf ("input_pnm: %d of %d bytes provided\n", len-to_copy, len);
|
|
788 #endif
|
|
789 return len-to_copy;
|
|
790 }
|
|
791 source = this->recv;
|
|
792 fill = this->recv_size - this->recv_read;
|
|
793 }
|
|
794
|
|
795 memcpy(dest, source, to_copy);
|
|
796 this->recv_read += to_copy;
|
|
797
|
|
798 #ifdef LOG
|
|
799 printf ("input_pnm: %d bytes provided\n", len);
|
|
800 #endif
|
|
801
|
|
802 return len;
|
|
803 }
|
|
804
|
|
805 int pnm_peek_header (pnm_t *this, char *data) {
|
|
806
|
|
807 memcpy (data, this->header, this->header_len);
|
|
808 return this->header_len;
|
|
809 }
|
|
810
|
|
811 void pnm_close(pnm_t *p) {
|
|
812
|
|
813 if (p->s >= 0) close(p->s);
|
|
814 free(p->path);
|
|
815 free(p);
|
|
816 }
|
|
817
|