Mercurial > mplayer.hg
annotate stream/pnm.c @ 25697:43d2ca88edc1
Use AV_RB* instead of custom variants.
author | reimar |
---|---|
date | Sun, 13 Jan 2008 12:57:44 +0000 |
parents | f159db1e77db |
children | 6fe76fc5d7fa |
rev | line source |
---|---|
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 | |
19614 | 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
8570 | 19 * |
20 * $Id$ | |
21 * | |
22 * pnm protocol implementation | |
23 * based upon code from joschka | |
24 */ | |
25 | |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
26 #include "config.h" |
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
27 |
8570 | 28 #include <unistd.h> |
29 #include <stdio.h> | |
30 #include <assert.h> | |
31 #include <string.h> | |
32 #include <sys/stat.h> | |
33 #include <fcntl.h> | |
34 #include <errno.h> | |
35 #include <stdlib.h> | |
36 #include <sys/time.h> | |
8584 | 37 #include <inttypes.h> |
10281 | 38 #ifndef HAVE_WINSOCK2 |
39 #define closesocket close | |
40 #include <sys/socket.h> | |
41 //#include <netinet/in.h> | |
42 //#include <netdb.h> | |
43 #else | |
44 #include <winsock2.h> | |
45 #endif | |
46 | |
25697 | 47 #include "libavutil/intreadwrite.h" |
48 | |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
49 #include "stream.h" |
19312
ab8d6b6deb63
proper inclusion of demuxer.h (including libmpdemux in Makefile only was to make previous split easier)
ben
parents:
19271
diff
changeset
|
50 #include "libmpdemux/demuxer.h" |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
51 #include "help_mp.h" |
17092 | 52 #include "osdep/timer.h" |
15614
a4a46131ee71
Change header order to avoid compile error because of STREAM_SEEK
reimar
parents:
15585
diff
changeset
|
53 |
8570 | 54 #include "pnm.h" |
19335
2a9d669e5ff6
isolated tcp socket code from network.c to a dedicated file
ben
parents:
19312
diff
changeset
|
55 #include "tcp.h" |
8570 | 56 //#include "libreal/rmff.h" |
57 | |
15585 | 58 extern int network_bandwidth; |
59 | |
8570 | 60 #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \ |
61 (((long)(unsigned char)(ch3) ) | \ | |
62 ( (long)(unsigned char)(ch2) << 8 ) | \ | |
63 ( (long)(unsigned char)(ch1) << 16 ) | \ | |
64 ( (long)(unsigned char)(ch0) << 24 ) ) | |
65 | |
66 | |
67 #define RMF_TAG FOURCC_TAG('.', 'R', 'M', 'F') | |
68 #define PROP_TAG FOURCC_TAG('P', 'R', 'O', 'P') | |
69 #define MDPR_TAG FOURCC_TAG('M', 'D', 'P', 'R') | |
70 #define CONT_TAG FOURCC_TAG('C', 'O', 'N', 'T') | |
71 #define DATA_TAG FOURCC_TAG('D', 'A', 'T', 'A') | |
72 #define INDX_TAG FOURCC_TAG('I', 'N', 'D', 'X') | |
73 #define PNA_TAG FOURCC_TAG('P', 'N', 'A', 0 ) | |
74 | |
75 /* | |
76 #define LOG | |
77 */ | |
78 | |
8880 | 79 #define BUF_SIZE 4096 |
80 #define HEADER_SIZE 4096 | |
8570 | 81 |
82 struct pnm_s { | |
83 | |
84 int s; | |
85 | |
86 // char *host; | |
87 // int port; | |
88 char *path; | |
89 // char *url; | |
90 | |
91 char buffer[BUF_SIZE]; /* scratch buffer */ | |
92 | |
93 /* receive buffer */ | |
94 uint8_t recv[BUF_SIZE]; | |
95 int recv_size; | |
96 int recv_read; | |
97 | |
98 uint8_t header[HEADER_SIZE]; | |
99 int header_len; | |
100 int header_read; | |
101 unsigned int seq_num[4]; /* two streams with two indices */ | |
102 unsigned int seq_current[2]; /* seqs of last stream chunk read */ | |
103 uint32_t ts_current; /* timestamp of current chunk */ | |
104 uint32_t ts_last[2]; /* timestamps of last chunks */ | |
105 unsigned int packet; /* number of last recieved packet */ | |
106 }; | |
107 | |
108 /* D means direct (no pointer) */ | |
109 #define BE_16D(x) ((x & 0xff00) >> 8)|((x & 0x00ff) << 8) | |
110 | |
111 /* sizes */ | |
112 #define PREAMBLE_SIZE 8 | |
113 #define CHECKSUM_SIZE 3 | |
114 | |
115 | |
116 /* header of rm files */ | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
117 static const unsigned char rm_header[]={ |
8570 | 118 0x2e, 0x52, 0x4d, 0x46, /* object_id ".RMF" */ |
119 0x00, 0x00, 0x00, 0x12, /* header_size 0x12 */ | |
120 0x00, 0x00, /* object_version 0x00 */ | |
121 0x00, 0x00, 0x00, 0x00, /* file_version 0x00 */ | |
122 0x00, 0x00, 0x00, 0x06 /* num_headers 0x06 */ | |
123 }; | |
124 | |
125 /* data chunk header */ | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
126 static const unsigned char pnm_data_header[]={ |
8570 | 127 'D','A','T','A', |
128 0,0,0,0, /* data chunk size */ | |
129 0,0, /* object version */ | |
130 0,0,0,0, /* num packets */ | |
131 0,0,0,0}; /* next data header */ | |
132 | |
133 /* pnm request chunk ids */ | |
134 | |
135 #define PNA_CLIENT_CAPS 0x03 | |
136 #define PNA_CLIENT_CHALLANGE 0x04 | |
137 #define PNA_BANDWIDTH 0x05 | |
138 #define PNA_GUID 0x13 | |
139 #define PNA_TIMESTAMP 0x17 | |
140 #define PNA_TWENTYFOUR 0x18 | |
141 | |
142 #define PNA_CLIENT_STRING 0x63 | |
143 #define PNA_PATH_REQUEST 0x52 | |
144 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
145 static const unsigned char pnm_challenge[] = "0990f6b4508b51e801bd6da011ad7b56"; |
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
146 static const unsigned char pnm_timestamp[] = "[15/06/1999:22:22:49 00:00]"; |
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
147 static const unsigned char pnm_guid[] = "3eac2411-83d5-11d2-f3ea-d7c3a51aa8b0"; |
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
148 static const unsigned char pnm_response[] = "97715a899cbe41cee00dd434851535bf"; |
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
149 static const unsigned char client_string[] = "WinNT_9.0_6.0.6.45_plus32_MP60_en-US_686l"; |
8570 | 150 |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
151 static const unsigned char pnm_header[] = { |
8570 | 152 'P','N','A', |
153 0x00, 0x0a, | |
154 0x00, 0x14, | |
155 0x00, 0x02, | |
156 0x00, 0x01 }; | |
157 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
158 static const unsigned char pnm_client_caps[] = { |
8570 | 159 0x07, 0x8a, 'p','n','r','v', |
160 0, 0x90, 'p','n','r','v', | |
161 0, 0x64, 'd','n','e','t', | |
162 0, 0x46, 'p','n','r','v', | |
163 0, 0x32, 'd','n','e','t', | |
164 0, 0x2b, 'p','n','r','v', | |
165 0, 0x28, 'd','n','e','t', | |
166 0, 0x24, 'p','n','r','v', | |
167 0, 0x19, 'd','n','e','t', | |
168 0, 0x18, 'p','n','r','v', | |
169 0, 0x14, 's','i','p','r', | |
170 0, 0x14, 'd','n','e','t', | |
171 0, 0x24, '2','8','_','8', | |
172 0, 0x12, 'p','n','r','v', | |
173 0, 0x0f, 'd','n','e','t', | |
174 0, 0x0a, 's','i','p','r', | |
175 0, 0x0a, 'd','n','e','t', | |
176 0, 0x08, 's','i','p','r', | |
177 0, 0x06, 's','i','p','r', | |
178 0, 0x12, 'l','p','c','J', | |
179 0, 0x07, '0','5','_','6' }; | |
180 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
181 static const uint32_t pnm_default_bandwidth=10485800; |
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
182 static const uint32_t pnm_available_bandwidths[]={14400,19200,28800,33600,34430,57600, |
8570 | 183 115200,262200,393216,524300,1544000,10485800}; |
184 | |
25695 | 185 static const unsigned char pnm_twentyfour[]={ |
8570 | 186 0xd5, 0x42, 0xa3, 0x1b, 0xef, 0x1f, 0x70, 0x24, |
187 0x85, 0x29, 0xb3, 0x8d, 0xba, 0x11, 0xf3, 0xd6 }; | |
188 | |
189 /* now other data follows. marked with 0x0000 at the beginning */ | |
25695 | 190 static const unsigned char after_chunks[]={ |
8570 | 191 0x00, 0x00, /* mark */ |
192 | |
193 0x50, 0x84, /* seems to be fixated */ | |
194 0x1f, 0x3a /* varies on each request (checksum ?)*/ | |
195 }; | |
196 | |
197 static void hexdump (char *buf, int length); | |
198 | |
199 static int rm_write(int s, const char *buf, int len) { | |
200 int total, timeout; | |
201 | |
202 total = 0; timeout = 30; | |
203 while (total < len){ | |
204 int n; | |
205 | |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
8880
diff
changeset
|
206 n = send (s, &buf[total], len - total, 0); |
8570 | 207 |
208 if (n > 0) | |
209 total += n; | |
210 else if (n < 0) { | |
10281 | 211 #ifndef HAVE_WINSOCK2 |
8570 | 212 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) { |
10281 | 213 #else |
214 if ((timeout>0) && ((errno == EAGAIN) || (WSAGetLastError() == WSAEINPROGRESS))) { | |
215 #endif | |
16372
b313a38c69cb
replace sleep with usec_sleep, required for recent mingw versions, patch by Robert Swain <robert.swain at gmail.com>
faust3
parents:
15626
diff
changeset
|
216 usec_sleep (1000000); timeout--; |
8570 | 217 } else |
218 return -1; | |
219 } | |
220 } | |
221 | |
222 return total; | |
223 } | |
224 | |
225 static ssize_t rm_read(int fd, void *buf, size_t count) { | |
226 | |
227 ssize_t ret, total; | |
228 | |
229 total = 0; | |
230 | |
231 while (total < count) { | |
232 | |
233 fd_set rset; | |
234 struct timeval timeout; | |
235 | |
236 FD_ZERO (&rset); | |
237 FD_SET (fd, &rset); | |
238 | |
239 timeout.tv_sec = 3; | |
240 timeout.tv_usec = 0; | |
241 | |
242 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) { | |
243 return -1; | |
244 } | |
245 | |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
8880
diff
changeset
|
246 ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0); |
8570 | 247 |
248 if (ret<=0) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
249 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: read error.\n"); |
8570 | 250 return ret; |
251 } else | |
252 total += ret; | |
253 } | |
254 | |
255 return total; | |
256 } | |
257 | |
258 /* | |
259 * debugging utilities | |
260 */ | |
261 | |
262 static void hexdump (char *buf, int length) { | |
263 | |
264 int i; | |
265 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
266 mp_msg(MSGT_OPEN, MSGL_INFO, "input_pnm: ascii>"); |
8570 | 267 for (i = 0; i < length; i++) { |
268 unsigned char c = buf[i]; | |
269 | |
270 if ((c >= 32) && (c <= 128)) | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
271 mp_msg(MSGT_OPEN, MSGL_INFO, "%c", c); |
8570 | 272 else |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
273 mp_msg(MSGT_OPEN, MSGL_INFO, "."); |
8570 | 274 } |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
275 mp_msg(MSGT_OPEN, MSGL_INFO, "\n"); |
8570 | 276 |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
277 mp_msg(MSGT_OPEN, MSGL_INFO, "input_pnm: hexdump> "); |
8570 | 278 for (i = 0; i < length; i++) { |
279 unsigned char c = buf[i]; | |
280 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
281 mp_msg(MSGT_OPEN, MSGL_INFO, "%02x", c); |
8570 | 282 |
283 if ((i % 16) == 15) | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
284 mp_msg(MSGT_OPEN, MSGL_INFO, "\npnm: "); |
8570 | 285 |
286 if ((i % 2) == 1) | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
287 mp_msg(MSGT_OPEN, MSGL_INFO, " "); |
8570 | 288 |
289 } | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
290 mp_msg(MSGT_OPEN, MSGL_INFO, "\n"); |
8570 | 291 } |
292 | |
293 /* | |
294 * pnm_get_chunk gets a chunk from stream | |
295 * and returns number of bytes read | |
296 */ | |
297 | |
15076
1a02a23202c2
Stop streaming if we got a server error or message on pnm streaming.
rtognimp
parents:
14164
diff
changeset
|
298 static int pnm_get_chunk(pnm_t *p, |
8570 | 299 unsigned int max, |
300 unsigned int *chunk_type, | |
301 char *data, int *need_response) { | |
302 | |
303 unsigned int chunk_size; | |
14164 | 304 unsigned int n; |
8570 | 305 char *ptr; |
306 | |
14164 | 307 if (max < PREAMBLE_SIZE) |
308 return -1; | |
309 | |
8570 | 310 /* get first PREAMBLE_SIZE bytes and ignore checksum */ |
311 rm_read (p->s, data, CHECKSUM_SIZE); | |
312 if (data[0] == 0x72) | |
313 rm_read (p->s, data, PREAMBLE_SIZE); | |
314 else | |
315 rm_read (p->s, data+CHECKSUM_SIZE, PREAMBLE_SIZE-CHECKSUM_SIZE); | |
316 | |
14164 | 317 max -= PREAMBLE_SIZE; |
318 | |
25697 | 319 *chunk_type = AV_RB32(data); |
320 chunk_size = AV_RB32(data+4); | |
8570 | 321 |
322 switch (*chunk_type) { | |
323 case PNA_TAG: | |
324 *need_response=0; | |
325 ptr=data+PREAMBLE_SIZE; | |
14164 | 326 if (max < 1) |
327 return -1; | |
8570 | 328 rm_read (p->s, ptr++, 1); |
14164 | 329 max -= 1; |
8570 | 330 |
331 while(1) { | |
332 /* expecting following chunk format: 0x4f <chunk size> <data...> */ | |
333 | |
14164 | 334 if (max < 2) |
335 return -1; | |
8570 | 336 rm_read (p->s, ptr, 2); |
14164 | 337 max -= 2; |
8570 | 338 if (*ptr == 'X') /* checking for server message */ |
339 { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
340 mp_msg(MSGT_OPEN, MSGL_WARN, "input_pnm: got a message from server:\n"); |
14164 | 341 if (max < 1) |
342 return -1; | |
8570 | 343 rm_read (p->s, ptr+2, 1); |
14164 | 344 max = -1; |
25697 | 345 n=AV_RB16(ptr+1); |
14164 | 346 if (max < n) |
347 return -1; | |
8570 | 348 rm_read (p->s, ptr+3, n); |
14164 | 349 max -= n; |
8570 | 350 ptr[3+n]=0; |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
351 mp_msg(MSGT_OPEN, MSGL_WARN, "%s\n",ptr+3); |
8570 | 352 return -1; |
353 } | |
354 | |
355 if (*ptr == 'F') /* checking for server error */ | |
356 { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
357 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: server error.\n"); |
8570 | 358 return -1; |
359 } | |
360 if (*ptr == 'i') | |
361 { | |
362 ptr+=2; | |
363 *need_response=1; | |
364 continue; | |
365 } | |
366 if (*ptr != 0x4f) break; | |
367 n=ptr[1]; | |
14164 | 368 if (max < n) |
369 return -1; | |
8570 | 370 rm_read (p->s, ptr+2, n); |
14164 | 371 max -= n; |
8570 | 372 ptr+=(n+2); |
373 } | |
374 /* the checksum of the next chunk is ignored here */ | |
14164 | 375 if (max < 1) |
376 return -1; | |
8570 | 377 rm_read (p->s, ptr+2, 1); |
378 ptr+=3; | |
379 chunk_size=ptr-data; | |
380 break; | |
381 case RMF_TAG: | |
382 case DATA_TAG: | |
383 case PROP_TAG: | |
384 case MDPR_TAG: | |
385 case CONT_TAG: | |
14164 | 386 if (chunk_size > max || chunk_size < PREAMBLE_SIZE) { |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
387 mp_msg(MSGT_OPEN, MSGL_ERR, "error: max chunk size exceded (max was 0x%04x)\n", max); |
14164 | 388 #ifdef LOG |
8570 | 389 n=rm_read (p->s, &data[PREAMBLE_SIZE], 0x100 - PREAMBLE_SIZE); |
390 hexdump(data,n+PREAMBLE_SIZE); | |
14164 | 391 #endif |
8570 | 392 return -1; |
393 } | |
394 rm_read (p->s, &data[PREAMBLE_SIZE], chunk_size-PREAMBLE_SIZE); | |
395 break; | |
396 default: | |
397 *chunk_type = 0; | |
398 chunk_size = PREAMBLE_SIZE; | |
399 break; | |
400 } | |
401 | |
402 return chunk_size; | |
403 } | |
404 | |
405 /* | |
406 * writes a chunk to a buffer, returns number of bytes written | |
407 */ | |
408 | |
409 static int pnm_write_chunk(uint16_t chunk_id, uint16_t length, | |
410 const char *chunk, char *data) { | |
411 | |
412 data[0]=(chunk_id>>8)%0xff; | |
413 data[1]=chunk_id%0xff; | |
414 data[2]=(length>>8)%0xff; | |
415 data[3]=length%0xff; | |
416 memcpy(&data[4],chunk,length); | |
417 | |
418 return length+4; | |
419 } | |
420 | |
421 /* | |
422 * constructs a request and sends it | |
423 */ | |
424 | |
425 static void pnm_send_request(pnm_t *p, uint32_t bandwidth) { | |
426 | |
427 uint16_t i16; | |
25696 | 428 int c=sizeof(pnm_header); |
8570 | 429 char fixme[]={0,1}; |
430 | |
25696 | 431 memcpy(p->buffer,pnm_header,sizeof(pnm_header)); |
8570 | 432 c+=pnm_write_chunk(PNA_CLIENT_CHALLANGE,strlen(pnm_challenge), |
433 pnm_challenge,&p->buffer[c]); | |
25696 | 434 c+=pnm_write_chunk(PNA_CLIENT_CAPS,sizeof(pnm_client_caps), |
8570 | 435 pnm_client_caps,&p->buffer[c]); |
436 c+=pnm_write_chunk(0x0a,0,NULL,&p->buffer[c]); | |
437 c+=pnm_write_chunk(0x0c,0,NULL,&p->buffer[c]); | |
438 c+=pnm_write_chunk(0x0d,0,NULL,&p->buffer[c]); | |
439 c+=pnm_write_chunk(0x16,2,fixme,&p->buffer[c]); | |
440 c+=pnm_write_chunk(PNA_TIMESTAMP,strlen(pnm_timestamp), | |
441 pnm_timestamp,&p->buffer[c]); | |
442 c+=pnm_write_chunk(PNA_BANDWIDTH,4, | |
443 (const char *)&pnm_default_bandwidth,&p->buffer[c]); | |
444 c+=pnm_write_chunk(0x08,0,NULL,&p->buffer[c]); | |
445 c+=pnm_write_chunk(0x0e,0,NULL,&p->buffer[c]); | |
446 c+=pnm_write_chunk(0x0f,0,NULL,&p->buffer[c]); | |
447 c+=pnm_write_chunk(0x11,0,NULL,&p->buffer[c]); | |
448 c+=pnm_write_chunk(0x10,0,NULL,&p->buffer[c]); | |
449 c+=pnm_write_chunk(0x15,0,NULL,&p->buffer[c]); | |
450 c+=pnm_write_chunk(0x12,0,NULL,&p->buffer[c]); | |
451 c+=pnm_write_chunk(PNA_GUID,strlen(pnm_guid), | |
452 pnm_guid,&p->buffer[c]); | |
25696 | 453 c+=pnm_write_chunk(PNA_TWENTYFOUR,sizeof(pnm_twentyfour), |
8570 | 454 pnm_twentyfour,&p->buffer[c]); |
455 | |
456 /* data after chunks */ | |
25696 | 457 memcpy(&p->buffer[c],after_chunks,sizeof(after_chunks)); |
458 c+=sizeof(after_chunks); | |
8570 | 459 |
460 /* client id string */ | |
461 p->buffer[c]=PNA_CLIENT_STRING; | |
11000 | 462 i16=BE_16D((strlen(client_string)-1)); /* don't know why do we have -1 here */ |
8570 | 463 memcpy(&p->buffer[c+1],&i16,2); |
464 memcpy(&p->buffer[c+3],client_string,strlen(client_string)+1); | |
465 c=c+3+strlen(client_string)+1; | |
466 | |
467 /* file path */ | |
468 p->buffer[c]=0; | |
469 p->buffer[c+1]=PNA_PATH_REQUEST; | |
470 i16=BE_16D(strlen(p->path)); | |
471 memcpy(&p->buffer[c+2],&i16,2); | |
472 memcpy(&p->buffer[c+4],p->path,strlen(p->path)); | |
473 c=c+4+strlen(p->path); | |
474 | |
475 /* some trailing bytes */ | |
476 p->buffer[c]='y'; | |
477 p->buffer[c+1]='B'; | |
478 | |
479 rm_write(p->s,p->buffer,c+2); | |
480 } | |
481 | |
482 /* | |
483 * pnm_send_response sends a response of a challenge | |
484 */ | |
485 | |
486 static void pnm_send_response(pnm_t *p, const char *response) { | |
487 | |
488 int size=strlen(response); | |
489 | |
490 p->buffer[0]=0x23; | |
491 p->buffer[1]=0; | |
492 p->buffer[2]=(unsigned char) size; | |
493 | |
494 memcpy(&p->buffer[3], response, size); | |
495 | |
496 rm_write (p->s, p->buffer, size+3); | |
497 | |
498 } | |
499 | |
500 /* | |
501 * get headers and challenge and fix headers | |
502 * write headers to p->header | |
503 * write challenge to p->buffer | |
504 * | |
505 * return 0 on error. != 0 on success | |
506 */ | |
507 | |
508 static int pnm_get_headers(pnm_t *p, int *need_response) { | |
509 | |
510 uint32_t chunk_type; | |
511 uint8_t *ptr=p->header; | |
512 uint8_t *prop_hdr=NULL; | |
513 int chunk_size,size=0; | |
514 int nr; | |
515 /* rmff_header_t *h; */ | |
516 | |
517 *need_response=0; | |
518 | |
519 while(1) { | |
520 if (HEADER_SIZE-size<=0) | |
521 { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
522 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: header buffer overflow. exiting\n"); |
8570 | 523 return 0; |
524 } | |
525 chunk_size=pnm_get_chunk(p,HEADER_SIZE-size,&chunk_type,ptr,&nr); | |
526 if (chunk_size < 0) return 0; | |
527 if (chunk_type == 0) break; | |
528 if (chunk_type == PNA_TAG) | |
529 { | |
25696 | 530 memcpy(ptr, rm_header, sizeof(rm_header)); |
531 chunk_size=sizeof(rm_header); | |
8570 | 532 *need_response=nr; |
533 } | |
534 if (chunk_type == DATA_TAG) | |
535 chunk_size=0; | |
536 if (chunk_type == RMF_TAG) | |
537 chunk_size=0; | |
538 if (chunk_type == PROP_TAG) | |
539 prop_hdr=ptr; | |
540 size+=chunk_size; | |
541 ptr+=chunk_size; | |
542 } | |
543 | |
8852 | 544 if (!prop_hdr) { |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
545 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: error while parsing headers.\n"); |
8852 | 546 return 0; |
547 } | |
548 | |
8570 | 549 /* set data offset */ |
550 size--; | |
551 prop_hdr[42]=(size>>24)%0xff; | |
552 prop_hdr[43]=(size>>16)%0xff; | |
553 prop_hdr[44]=(size>>8)%0xff; | |
554 prop_hdr[45]=(size)%0xff; | |
555 size++; | |
556 | |
557 /* read challenge */ | |
558 memcpy (p->buffer, ptr, PREAMBLE_SIZE); | |
559 rm_read (p->s, &p->buffer[PREAMBLE_SIZE], 64); | |
560 | |
561 /* now write a data header */ | |
25696 | 562 memcpy(ptr, pnm_data_header, sizeof(pnm_data_header)); |
563 size+=sizeof(pnm_data_header); | |
8570 | 564 /* |
565 h=rmff_scan_header(p->header); | |
566 rmff_fix_header(h); | |
567 p->header_len=rmff_get_header_size(h); | |
568 rmff_dump_header(h, p->header, HEADER_SIZE); | |
569 */ | |
570 p->header_len=size; | |
571 | |
572 return 1; | |
573 } | |
574 | |
575 /* | |
576 * determine correct stream number by looking at indices | |
577 */ | |
578 | |
579 static int pnm_calc_stream(pnm_t *p) { | |
580 | |
581 char str0=0,str1=0; | |
582 | |
583 /* looking at the first index to | |
584 * find possible stream types | |
585 */ | |
586 if (p->seq_current[0]==p->seq_num[0]) str0=1; | |
587 if (p->seq_current[0]==p->seq_num[2]) str1=1; | |
588 | |
589 switch (str0+str1) { | |
590 case 1: /* one is possible, good. */ | |
591 if (str0) | |
592 { | |
593 p->seq_num[0]++; | |
594 p->seq_num[1]=p->seq_current[1]+1; | |
595 return 0; | |
596 } else | |
597 { | |
598 p->seq_num[2]++; | |
599 p->seq_num[3]=p->seq_current[1]+1; | |
600 return 1; | |
601 } | |
602 break; | |
603 case 0: | |
604 case 2: /* both types or none possible, not so good */ | |
605 /* try to figure out by second index */ | |
606 if ( (p->seq_current[1] == p->seq_num[1]) | |
607 &&(p->seq_current[1] != p->seq_num[3])) | |
608 { | |
609 /* ok, only stream0 matches */ | |
610 p->seq_num[0]=p->seq_current[0]+1; | |
611 p->seq_num[1]++; | |
612 return 0; | |
613 } | |
614 if ( (p->seq_current[1] == p->seq_num[3]) | |
615 &&(p->seq_current[1] != p->seq_num[1])) | |
616 { | |
617 /* ok, only stream1 matches */ | |
618 p->seq_num[2]=p->seq_current[0]+1; | |
619 p->seq_num[3]++; | |
620 return 1; | |
621 } | |
622 /* wow, both streams match, or not. */ | |
623 /* now we try to decide by timestamps */ | |
624 if (p->ts_current < p->ts_last[1]) | |
625 return 0; | |
626 if (p->ts_current < p->ts_last[0]) | |
627 return 1; | |
628 /* does not help, we guess type 0 */ | |
629 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
630 mp_msg(MSGT_OPEN, MSGL_INFO, "guessing stream# 0\n"); |
8570 | 631 #endif |
632 p->seq_num[0]=p->seq_current[0]+1; | |
633 p->seq_num[1]=p->seq_current[1]+1; | |
634 return 0; | |
635 break; | |
636 } | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
637 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: wow, something very nasty happened in pnm_calc_stream\n"); |
8570 | 638 return 2; |
639 } | |
640 | |
641 /* | |
642 * gets a stream chunk and writes it to a recieve buffer | |
643 */ | |
644 | |
645 static int pnm_get_stream_chunk(pnm_t *p) { | |
646 | |
647 int n; | |
648 char keepalive='!'; | |
649 unsigned int fof1, fof2, stream; | |
650 | |
651 /* send a keepalive */ | |
652 /* realplayer seems to do that every 43th package */ | |
653 if ((p->packet%43) == 42) | |
654 { | |
655 rm_write(p->s,&keepalive,1); | |
656 } | |
657 | |
658 /* data chunks begin with: 'Z' <o> <o> <i1> 'Z' <i2> | |
659 * where <o> is the offset to next stream chunk, | |
660 * <i1> is a 16 bit index | |
661 * <i2> is a 8 bit index which counts from 0x10 to somewhere | |
662 */ | |
663 | |
664 n = rm_read (p->s, p->buffer, 8); | |
15077
a893e0bfa9d6
"Fix" for pnm EOF detection (stop on read errors)
rtognimp
parents:
15076
diff
changeset
|
665 if (n<0) return -1; |
8570 | 666 if (n<8) return 0; |
667 | |
668 /* skip 8 bytes if 0x62 is read */ | |
669 if (p->buffer[0] == 0x62) | |
670 { | |
671 n = rm_read (p->s, p->buffer, 8); | |
672 if (n<8) return 0; | |
673 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
674 mp_msg(MSGT_OPEN, MSGL_WARN, "input_pnm: had to seek 8 bytes on 0x62\n"); |
8570 | 675 #endif |
676 } | |
677 | |
678 /* a server message */ | |
679 if (p->buffer[0] == 'X') | |
680 { | |
25697 | 681 int size=AV_RB16(&p->buffer[1]); |
8570 | 682 |
683 rm_read (p->s, &p->buffer[8], size-5); | |
684 p->buffer[size+3]=0; | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
685 mp_msg(MSGT_OPEN, MSGL_WARN, "input_pnm: got message from server while reading stream:\n%s\n", &p->buffer[3]); |
15076
1a02a23202c2
Stop streaming if we got a server error or message on pnm streaming.
rtognimp
parents:
14164
diff
changeset
|
686 return -1; |
8570 | 687 } |
688 if (p->buffer[0] == 'F') | |
689 { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
690 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: server error.\n"); |
15076
1a02a23202c2
Stop streaming if we got a server error or message on pnm streaming.
rtognimp
parents:
14164
diff
changeset
|
691 return -1; |
8570 | 692 } |
693 | |
694 /* skip bytewise to next chunk. | |
11000 | 695 * seems, that we don't need that, if we send enough |
8570 | 696 * keepalives |
697 */ | |
698 n=0; | |
699 while (p->buffer[0] != 0x5a) { | |
700 int i; | |
701 for (i=1; i<8; i++) { | |
702 p->buffer[i-1]=p->buffer[i]; | |
703 } | |
704 rm_read (p->s, &p->buffer[7], 1); | |
705 n++; | |
706 } | |
707 | |
708 #ifdef LOG | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
709 if (n) mp_msg(MSGT_OPEN, MSGL_WARN, "input_pnm: had to seek %i bytes to next chunk\n", n); |
8570 | 710 #endif |
711 | |
712 /* check for 'Z's */ | |
713 if ((p->buffer[0] != 0x5a)||(p->buffer[7] != 0x5a)) | |
714 { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
715 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: bad boundaries\n"); |
8570 | 716 hexdump(p->buffer, 8); |
717 return 0; | |
718 } | |
719 | |
720 /* check offsets */ | |
25697 | 721 fof1=AV_RB16(&p->buffer[1]); |
722 fof2=AV_RB16(&p->buffer[3]); | |
8570 | 723 if (fof1 != fof2) |
724 { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
725 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: frame offsets are different: 0x%04x 0x%04x\n",fof1,fof2); |
8570 | 726 return 0; |
727 } | |
728 | |
729 /* get first index */ | |
25697 | 730 p->seq_current[0]=AV_RB16(&p->buffer[5]); |
8570 | 731 |
732 /* now read the rest of stream chunk */ | |
733 n = rm_read (p->s, &p->recv[5], fof1-5); | |
734 if (n<(fof1-5)) return 0; | |
735 | |
736 /* get second index */ | |
737 p->seq_current[1]=p->recv[5]; | |
738 | |
739 /* get timestamp */ | |
25697 | 740 p->ts_current=AV_RB32(&p->recv[6]); |
8570 | 741 |
742 /* get stream number */ | |
743 stream=pnm_calc_stream(p); | |
744 | |
745 /* saving timestamp */ | |
746 p->ts_last[stream]=p->ts_current; | |
747 | |
748 /* constructing a data packet header */ | |
749 | |
750 p->recv[0]=0; /* object version */ | |
751 p->recv[1]=0; | |
752 | |
25697 | 753 fof2=AV_RB16(&fof2); |
8570 | 754 memcpy(&p->recv[2], &fof2, 2); |
755 /*p->recv[2]=(fof2>>8)%0xff;*/ /* length */ | |
756 /*p->recv[3]=(fof2)%0xff;*/ | |
757 | |
758 p->recv[4]=0; /* stream number */ | |
759 p->recv[5]=stream; | |
760 | |
761 p->recv[10]=p->recv[10] & 0xfe; /* streambox seems to do that... */ | |
762 | |
763 p->packet++; | |
764 | |
765 p->recv_size=fof1; | |
766 | |
767 return fof1; | |
768 } | |
769 | |
770 // pnm_t *pnm_connect(const char *mrl) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
771 static pnm_t *pnm_connect(int fd, char *path) { |
8570 | 772 |
773 pnm_t *p=malloc(sizeof(pnm_t)); | |
8852 | 774 int need_response=0; |
8570 | 775 |
776 p->path=strdup(path); | |
777 p->s=fd; | |
778 | |
779 pnm_send_request(p,pnm_available_bandwidths[10]); | |
780 if (!pnm_get_headers(p, &need_response)) { | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
781 mp_msg(MSGT_OPEN, MSGL_ERR, "input_pnm: failed to set up stream\n"); |
8570 | 782 free(p->path); |
783 free(p); | |
784 return NULL; | |
785 } | |
786 if (need_response) | |
787 pnm_send_response(p, pnm_response); | |
788 p->ts_last[0]=0; | |
789 p->ts_last[1]=0; | |
790 | |
791 /* copy header to recv */ | |
792 | |
793 memcpy(p->recv, p->header, p->header_len); | |
794 p->recv_size = p->header_len; | |
795 p->recv_read = 0; | |
796 | |
797 return p; | |
798 } | |
799 | |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
800 static int pnm_read (pnm_t *this, char *data, int len) { |
8570 | 801 |
802 int to_copy=len; | |
803 char *dest=data; | |
804 char *source=this->recv + this->recv_read; | |
805 int fill=this->recv_size - this->recv_read; | |
15076
1a02a23202c2
Stop streaming if we got a server error or message on pnm streaming.
rtognimp
parents:
14164
diff
changeset
|
806 int retval; |
8570 | 807 |
808 if (len < 0) return 0; | |
809 while (to_copy > fill) { | |
810 | |
811 memcpy(dest, source, fill); | |
812 to_copy -= fill; | |
813 dest += fill; | |
814 this->recv_read=0; | |
815 | |
15076
1a02a23202c2
Stop streaming if we got a server error or message on pnm streaming.
rtognimp
parents:
14164
diff
changeset
|
816 if ((retval = pnm_get_stream_chunk (this)) <= 0) { |
8570 | 817 #ifdef LOG |
15626
941b1a71351f
printf converted to mp_msg; made static many unnecessarily global symbols
nicodvb
parents:
15614
diff
changeset
|
818 mp_msg(MSGT_OPEN, MSGL_INFO, "input_pnm: %d of %d bytes provided\n", len-to_copy, len); |
8570 | 819 #endif |
15076
1a02a23202c2
Stop streaming if we got a server error or message on pnm streaming.
rtognimp
parents:
14164
diff
changeset
|
820 if (retval < 0) |
1a02a23202c2
Stop streaming if we got a server error or message on pnm streaming.
rtognimp
parents:
14164
diff
changeset
|
821 return retval; |
1a02a23202c2
Stop streaming if we got a server error or message on pnm streaming.
rtognimp
parents:
14164
diff
changeset
|
822 else |
8570 | 823 return len-to_copy; |
824 } | |
825 source = this->recv; | |
826 fill = this->recv_size - this->recv_read; | |
827 } | |
828 | |
829 memcpy(dest, source, to_copy); | |
830 this->recv_read += to_copy; | |
831 | |
832 #ifdef LOG | |
15585 | 833 mp_msg(MSGT_OPEN, MSGL_INFO, "input_pnm: %d bytes provided\n", len); |
8570 | 834 #endif |
835 | |
836 return len; | |
837 } | |
838 | |
15585 | 839 static int pnm_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) { |
840 return pnm_read(stream_ctrl->data, buffer, size); | |
841 } | |
842 | |
843 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) { | |
844 int fd; | |
845 pnm_t *pnm; | |
846 URL_t *url; | |
847 | |
848 mp_msg(MSGT_OPEN, MSGL_INFO, "STREAM_PNM, URL: %s\n", stream->url); | |
849 stream->streaming_ctrl = streaming_ctrl_new(); | |
850 if(stream->streaming_ctrl==NULL) { | |
851 return STREAM_ERROR; | |
852 } | |
853 stream->streaming_ctrl->bandwidth = network_bandwidth; | |
854 url = url_new(stream->url); | |
855 stream->streaming_ctrl->url = check4proxies(url); | |
856 //url_free(url); | |
857 | |
858 fd = connect2Server( stream->streaming_ctrl->url->hostname, | |
859 stream->streaming_ctrl->url->port ? stream->streaming_ctrl->url->port : 7070,1 ); | |
860 | |
861 if(fd<0) | |
862 goto fail; | |
863 | |
864 pnm = pnm_connect(fd,stream->streaming_ctrl->url->file); | |
865 if(!pnm) | |
866 goto fail; | |
867 stream->type = STREAMTYPE_STREAM; | |
868 stream->fd=fd; | |
869 stream->streaming_ctrl->data=pnm; | |
870 stream->streaming_ctrl->streaming_read = pnm_streaming_read; | |
871 //stream->streaming_ctrl->streaming_seek = nop_streaming_seek; | |
872 stream->streaming_ctrl->prebuffer_size = 8*1024; // 8 KBytes | |
873 stream->streaming_ctrl->buffering = 1; | |
874 stream->streaming_ctrl->status = streaming_playing_e; | |
875 *file_format = DEMUXER_TYPE_REAL; | |
876 fixup_network_stream_cache(stream); | |
877 return STREAM_OK; | |
878 | |
879 fail: | |
880 streaming_ctrl_free(stream->streaming_ctrl); | |
881 stream->streaming_ctrl = NULL; | |
24257 | 882 return STREAM_UNSUPPORTED; |
15585 | 883 } |
884 | |
885 | |
25211 | 886 const stream_info_t stream_info_pnm = { |
15585 | 887 "RealNetworks pnm", |
888 "pnm", | |
889 "Arpi, xine team", | |
890 "ported from xine", | |
891 open_s, | |
892 {"pnm", NULL}, //pnm as fallback | |
893 NULL, | |
894 0 // Urls are an option string | |
895 }; |