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