Mercurial > mplayer.hg
comparison stream/network.c @ 19271:64d82a45a05d
introduce new 'stream' directory for all stream layer related components and split them from libmpdemux
author | ben |
---|---|
date | Mon, 31 Jul 2006 17:39:17 +0000 |
parents | libmpdemux/network.c@5e767cabf4cd |
children | ab8d6b6deb63 |
comparison
equal
deleted
inserted
replaced
19270:7d39b911f0bd | 19271:64d82a45a05d |
---|---|
1 /* | |
2 * Network layer for MPlayer | |
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com> | |
4 * (C) 2001, MPlayer team. | |
5 */ | |
6 | |
7 //#define DUMP2FILE | |
8 | |
9 #include <stdio.h> | |
10 #include <stdlib.h> | |
11 #include <string.h> | |
12 #include <unistd.h> | |
13 | |
14 #include <errno.h> | |
15 #include <ctype.h> | |
16 | |
17 #include "config.h" | |
18 | |
19 #include "mp_msg.h" | |
20 #include "help_mp.h" | |
21 | |
22 #ifndef HAVE_WINSOCK2 | |
23 #define closesocket close | |
24 #else | |
25 #include <winsock2.h> | |
26 #include <ws2tcpip.h> | |
27 #endif | |
28 | |
29 #include "stream.h" | |
30 #include "demuxer.h" | |
31 #include "m_config.h" | |
32 | |
33 #include "network.h" | |
34 #include "http.h" | |
35 #include "cookies.h" | |
36 #include "url.h" | |
37 | |
38 #include "version.h" | |
39 | |
40 extern int stream_cache_size; | |
41 | |
42 extern int mp_input_check_interrupt(int time); | |
43 | |
44 /* Variables for the command line option -user, -passwd, -bandwidth, | |
45 -user-agent and -nocookies */ | |
46 | |
47 char *network_username=NULL; | |
48 char *network_password=NULL; | |
49 int network_bandwidth=0; | |
50 int network_cookies_enabled = 0; | |
51 char *network_useragent=NULL; | |
52 | |
53 /* IPv6 options */ | |
54 int network_prefer_ipv4 = 0; | |
55 int network_ipv4_only_proxy = 0; | |
56 | |
57 | |
58 mime_struct_t mime_type_table[] = { | |
59 // MP3 streaming, some MP3 streaming server answer with audio/mpeg | |
60 { "audio/mpeg", DEMUXER_TYPE_AUDIO }, | |
61 // MPEG streaming | |
62 { "video/mpeg", DEMUXER_TYPE_UNKNOWN }, | |
63 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN }, | |
64 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN }, | |
65 // AVI ??? => video/x-msvideo | |
66 { "video/x-msvideo", DEMUXER_TYPE_AVI }, | |
67 // MOV => video/quicktime | |
68 { "video/quicktime", DEMUXER_TYPE_MOV }, | |
69 // ASF | |
70 { "audio/x-ms-wax", DEMUXER_TYPE_ASF }, | |
71 { "audio/x-ms-wma", DEMUXER_TYPE_ASF }, | |
72 { "video/x-ms-asf", DEMUXER_TYPE_ASF }, | |
73 { "video/x-ms-afs", DEMUXER_TYPE_ASF }, | |
74 { "video/x-ms-wvx", DEMUXER_TYPE_ASF }, | |
75 { "video/x-ms-wmv", DEMUXER_TYPE_ASF }, | |
76 { "video/x-ms-wma", DEMUXER_TYPE_ASF }, | |
77 { "application/x-mms-framed", DEMUXER_TYPE_ASF }, | |
78 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF }, | |
79 { "application/octet-stream", DEMUXER_TYPE_ASF }, | |
80 // Playlists | |
81 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST }, | |
82 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST }, | |
83 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST }, | |
84 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST }, | |
85 // Real Media | |
86 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL }, | |
87 // OGG Streaming | |
88 { "application/x-ogg", DEMUXER_TYPE_OGG }, | |
89 // NullSoft Streaming Video | |
90 { "video/nsv", DEMUXER_TYPE_NSV}, | |
91 { "misc/ultravox", DEMUXER_TYPE_NSV}, | |
92 { NULL, DEMUXER_TYPE_UNKNOWN}, | |
93 }; | |
94 | |
95 | |
96 streaming_ctrl_t * | |
97 streaming_ctrl_new(void) { | |
98 streaming_ctrl_t *streaming_ctrl; | |
99 streaming_ctrl = malloc(sizeof(streaming_ctrl_t)); | |
100 if( streaming_ctrl==NULL ) { | |
101 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); | |
102 return NULL; | |
103 } | |
104 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) ); | |
105 return streaming_ctrl; | |
106 } | |
107 | |
108 void | |
109 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) { | |
110 if( streaming_ctrl==NULL ) return; | |
111 if( streaming_ctrl->url ) url_free( streaming_ctrl->url ); | |
112 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer ); | |
113 if( streaming_ctrl->data ) free( streaming_ctrl->data ); | |
114 free( streaming_ctrl ); | |
115 } | |
116 | |
117 | |
118 // Converts an address family constant to a string | |
119 | |
120 const char *af2String(int af) { | |
121 switch (af) { | |
122 case AF_INET: return "AF_INET"; | |
123 | |
124 #ifdef HAVE_AF_INET6 | |
125 case AF_INET6: return "AF_INET6"; | |
126 #endif | |
127 default: return "Unknown address family!"; | |
128 } | |
129 } | |
130 | |
131 | |
132 | |
133 // Connect to a server using a TCP connection, with specified address family | |
134 // return -2 for fatal error, like unable to resolve name, connection timeout... | |
135 // return -1 is unable to connect to a particular port | |
136 | |
137 int | |
138 connect2Server_with_af(char *host, int port, int af,int verb) { | |
139 int socket_server_fd; | |
140 int err, err_len; | |
141 int ret,count = 0; | |
142 fd_set set; | |
143 struct timeval tv; | |
144 union { | |
145 struct sockaddr_in four; | |
146 #ifdef HAVE_AF_INET6 | |
147 struct sockaddr_in6 six; | |
148 #endif | |
149 } server_address; | |
150 size_t server_address_size; | |
151 void *our_s_addr; // Pointer to sin_addr or sin6_addr | |
152 struct hostent *hp=NULL; | |
153 char buf[255]; | |
154 | |
155 #ifdef HAVE_WINSOCK2 | |
156 u_long val; | |
157 #endif | |
158 | |
159 socket_server_fd = socket(af, SOCK_STREAM, 0); | |
160 | |
161 | |
162 if( socket_server_fd==-1 ) { | |
163 // mp_msg(MSGT_NETWORK,MSGL_ERR,"Failed to create %s socket:\n", af2String(af)); | |
164 return -2; | |
165 } | |
166 | |
167 switch (af) { | |
168 case AF_INET: our_s_addr = (void *) &server_address.four.sin_addr; break; | |
169 #ifdef HAVE_AF_INET6 | |
170 case AF_INET6: our_s_addr = (void *) &server_address.six.sin6_addr; break; | |
171 #endif | |
172 default: | |
173 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af); | |
174 return -2; | |
175 } | |
176 | |
177 | |
178 memset(&server_address, 0, sizeof(server_address)); | |
179 | |
180 #ifndef HAVE_WINSOCK2 | |
181 #ifdef USE_ATON | |
182 if (inet_aton(host, our_s_addr)!=1) | |
183 #else | |
184 if (inet_pton(af, host, our_s_addr)!=1) | |
185 #endif | |
186 #else | |
187 if ( inet_addr(host)==INADDR_NONE ) | |
188 #endif | |
189 { | |
190 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ResolvingHostForAF, host, af2String(af)); | |
191 | |
192 #ifdef HAVE_GETHOSTBYNAME2 | |
193 hp=(struct hostent*)gethostbyname2( host, af ); | |
194 #else | |
195 hp=(struct hostent*)gethostbyname( host ); | |
196 #endif | |
197 if( hp==NULL ) { | |
198 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantResolv, af2String(af), host); | |
199 return -2; | |
200 } | |
201 | |
202 memcpy( our_s_addr, (void*)hp->h_addr_list[0], hp->h_length ); | |
203 } | |
204 #ifdef HAVE_WINSOCK2 | |
205 else { | |
206 unsigned long addr = inet_addr(host); | |
207 memcpy( our_s_addr, (void*)&addr, sizeof(addr) ); | |
208 } | |
209 #endif | |
210 | |
211 switch (af) { | |
212 case AF_INET: | |
213 server_address.four.sin_family=af; | |
214 server_address.four.sin_port=htons(port); | |
215 server_address_size = sizeof(server_address.four); | |
216 break; | |
217 #ifdef HAVE_AF_INET6 | |
218 case AF_INET6: | |
219 server_address.six.sin6_family=af; | |
220 server_address.six.sin6_port=htons(port); | |
221 server_address_size = sizeof(server_address.six); | |
222 break; | |
223 #endif | |
224 default: | |
225 mp_msg(MSGT_NETWORK,MSGL_ERR, MSGTR_MPDEMUX_NW_UnknownAF, af); | |
226 return -2; | |
227 } | |
228 | |
229 #if defined(USE_ATON) || defined(HAVE_WINSOCK2) | |
230 strncpy( buf, inet_ntoa( *((struct in_addr*)our_s_addr) ), 255); | |
231 #else | |
232 inet_ntop(af, our_s_addr, buf, 255); | |
233 #endif | |
234 if(verb) mp_msg(MSGT_NETWORK,MSGL_STATUS,MSGTR_MPDEMUX_NW_ConnectingToServer, host, buf , port ); | |
235 | |
236 // Turn the socket as non blocking so we can timeout on the connection | |
237 #ifndef HAVE_WINSOCK2 | |
238 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) | O_NONBLOCK ); | |
239 #else | |
240 val = 1; | |
241 ioctlsocket( socket_server_fd, FIONBIO, &val ); | |
242 #endif | |
243 if( connect( socket_server_fd, (struct sockaddr*)&server_address, server_address_size )==-1 ) { | |
244 #ifndef HAVE_WINSOCK2 | |
245 if( errno!=EINPROGRESS ) { | |
246 #else | |
247 if( (WSAGetLastError() != WSAEINPROGRESS) && (WSAGetLastError() != WSAEWOULDBLOCK) ) { | |
248 #endif | |
249 if(verb) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_CantConnect2Server, af2String(af)); | |
250 closesocket(socket_server_fd); | |
251 return -1; | |
252 } | |
253 } | |
254 tv.tv_sec = 0; | |
255 tv.tv_usec = 500000; | |
256 FD_ZERO( &set ); | |
257 FD_SET( socket_server_fd, &set ); | |
258 // When the connection will be made, we will have a writable fd | |
259 while((ret = select(socket_server_fd+1, NULL, &set, NULL, &tv)) == 0) { | |
260 if( ret<0 ) mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_SelectFailed); | |
261 else if(ret > 0) break; | |
262 else if(count > 30 || mp_input_check_interrupt(500)) { | |
263 if(count > 30) | |
264 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnTimeout); | |
265 else | |
266 mp_msg(MSGT_NETWORK,MSGL_V,"Connection interuppted by user\n"); | |
267 return -3; | |
268 } | |
269 count++; | |
270 FD_ZERO( &set ); | |
271 FD_SET( socket_server_fd, &set ); | |
272 tv.tv_sec = 0; | |
273 tv.tv_usec = 500000; | |
274 } | |
275 | |
276 // Turn back the socket as blocking | |
277 #ifndef HAVE_WINSOCK2 | |
278 fcntl( socket_server_fd, F_SETFL, fcntl(socket_server_fd, F_GETFL) & ~O_NONBLOCK ); | |
279 #else | |
280 val = 0; | |
281 ioctlsocket( socket_server_fd, FIONBIO, &val ); | |
282 #endif | |
283 // Check if there were any error | |
284 err_len = sizeof(int); | |
285 ret = getsockopt(socket_server_fd,SOL_SOCKET,SO_ERROR,&err,&err_len); | |
286 if(ret < 0) { | |
287 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_GetSockOptFailed,strerror(errno)); | |
288 return -2; | |
289 } | |
290 if(err > 0) { | |
291 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ConnectError,strerror(err)); | |
292 return -1; | |
293 } | |
294 | |
295 return socket_server_fd; | |
296 } | |
297 | |
298 // Connect to a server using a TCP connection | |
299 // return -2 for fatal error, like unable to resolve name, connection timeout... | |
300 // return -1 is unable to connect to a particular port | |
301 | |
302 | |
303 int | |
304 connect2Server(char *host, int port, int verb) { | |
305 #ifdef HAVE_AF_INET6 | |
306 int r; | |
307 int s = -2; | |
308 | |
309 r = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET:AF_INET6,verb); | |
310 if (r > -1) return r; | |
311 | |
312 s = connect2Server_with_af(host, port, network_prefer_ipv4 ? AF_INET6:AF_INET,verb); | |
313 if (s == -2) return r; | |
314 return s; | |
315 #else | |
316 return connect2Server_with_af(host, port, AF_INET,verb); | |
317 #endif | |
318 | |
319 | |
320 } | |
321 | |
322 URL_t* | |
323 check4proxies( URL_t *url ) { | |
324 URL_t *url_out = NULL; | |
325 if( url==NULL ) return NULL; | |
326 url_out = url_new( url->url ); | |
327 if( !strcasecmp(url->protocol, "http_proxy") ) { | |
328 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port ); | |
329 return url_out; | |
330 } | |
331 // Check if the http_proxy environment variable is set. | |
332 if( !strcasecmp(url->protocol, "http") ) { | |
333 char *proxy; | |
334 proxy = getenv("http_proxy"); | |
335 if( proxy!=NULL ) { | |
336 // We got a proxy, build the URL to use it | |
337 int len; | |
338 char *new_url; | |
339 URL_t *tmp_url; | |
340 URL_t *proxy_url = url_new( proxy ); | |
341 | |
342 if( proxy_url==NULL ) { | |
343 mp_msg(MSGT_NETWORK,MSGL_WARN, | |
344 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout); | |
345 return url_out; | |
346 } | |
347 | |
348 #ifdef HAVE_AF_INET6 | |
349 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) { | |
350 mp_msg(MSGT_NETWORK,MSGL_WARN, | |
351 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy); | |
352 url_free(proxy_url); | |
353 return url_out; | |
354 } | |
355 #endif | |
356 | |
357 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url ); | |
358 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port | |
359 new_url = malloc( len+1 ); | |
360 if( new_url==NULL ) { | |
361 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); | |
362 url_free(proxy_url); | |
363 return url_out; | |
364 } | |
365 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url ); | |
366 tmp_url = url_new( new_url ); | |
367 if( tmp_url==NULL ) { | |
368 free( new_url ); | |
369 url_free( proxy_url ); | |
370 return url_out; | |
371 } | |
372 url_free( url_out ); | |
373 url_out = tmp_url; | |
374 free( new_url ); | |
375 url_free( proxy_url ); | |
376 } | |
377 } | |
378 return url_out; | |
379 } | |
380 | |
381 int | |
382 http_send_request( URL_t *url, off_t pos ) { | |
383 HTTP_header_t *http_hdr; | |
384 URL_t *server_url; | |
385 char str[256]; | |
386 int fd; | |
387 int ret; | |
388 int proxy = 0; // Boolean | |
389 | |
390 http_hdr = http_new_header(); | |
391 | |
392 if( !strcasecmp(url->protocol, "http_proxy") ) { | |
393 proxy = 1; | |
394 server_url = url_new( (url->file)+1 ); | |
395 http_set_uri( http_hdr, server_url->url ); | |
396 } else { | |
397 server_url = url; | |
398 http_set_uri( http_hdr, server_url->file ); | |
399 } | |
400 if (server_url->port && server_url->port != 80) | |
401 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port ); | |
402 else | |
403 snprintf(str, 256, "Host: %s", server_url->hostname ); | |
404 http_set_field( http_hdr, str); | |
405 if (network_useragent) | |
406 { | |
407 snprintf(str, 256, "User-Agent: %s", network_useragent); | |
408 http_set_field(http_hdr, str); | |
409 } | |
410 else | |
411 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION); | |
412 | |
413 http_set_field(http_hdr, "Icy-MetaData: 1"); | |
414 | |
415 if(pos>0) { | |
416 // Extend http_send_request with possibility to do partial content retrieval | |
417 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos); | |
418 http_set_field(http_hdr, str); | |
419 } | |
420 | |
421 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url ); | |
422 | |
423 http_set_field( http_hdr, "Connection: close"); | |
424 http_add_basic_authentication( http_hdr, url->username, url->password ); | |
425 if( http_build_request( http_hdr )==NULL ) { | |
426 goto err_out; | |
427 } | |
428 | |
429 if( proxy ) { | |
430 if( url->port==0 ) url->port = 8080; // Default port for the proxy server | |
431 fd = connect2Server( url->hostname, url->port,1 ); | |
432 url_free( server_url ); | |
433 server_url = NULL; | |
434 } else { | |
435 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server | |
436 fd = connect2Server( server_url->hostname, server_url->port,1 ); | |
437 } | |
438 if( fd<0 ) { | |
439 goto err_out; | |
440 } | |
441 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer ); | |
442 | |
443 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 ); | |
444 if( ret!=(int)http_hdr->buffer_size ) { | |
445 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest); | |
446 goto err_out; | |
447 } | |
448 | |
449 http_free( http_hdr ); | |
450 | |
451 return fd; | |
452 err_out: | |
453 http_free(http_hdr); | |
454 if (proxy && server_url) | |
455 url_free(server_url); | |
456 return -1; | |
457 } | |
458 | |
459 HTTP_header_t * | |
460 http_read_response( int fd ) { | |
461 HTTP_header_t *http_hdr; | |
462 char response[BUFFER_SIZE]; | |
463 int i; | |
464 | |
465 http_hdr = http_new_header(); | |
466 if( http_hdr==NULL ) { | |
467 return NULL; | |
468 } | |
469 | |
470 do { | |
471 i = recv( fd, response, BUFFER_SIZE, 0 ); | |
472 if( i<0 ) { | |
473 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed); | |
474 http_free( http_hdr ); | |
475 return NULL; | |
476 } | |
477 if( i==0 ) { | |
478 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF); | |
479 http_free( http_hdr ); | |
480 return NULL; | |
481 } | |
482 http_response_append( http_hdr, response, i ); | |
483 } while( !http_is_header_entire( http_hdr ) ); | |
484 http_response_parse( http_hdr ); | |
485 return http_hdr; | |
486 } | |
487 | |
488 int | |
489 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) { | |
490 char *aut; | |
491 | |
492 if( *auth_retry==1 ) { | |
493 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed); | |
494 return -1; | |
495 } | |
496 if( *auth_retry>0 ) { | |
497 if( url->username ) { | |
498 free( url->username ); | |
499 url->username = NULL; | |
500 } | |
501 if( url->password ) { | |
502 free( url->password ); | |
503 url->password = NULL; | |
504 } | |
505 } | |
506 | |
507 aut = http_get_field(http_hdr, "WWW-Authenticate"); | |
508 if( aut!=NULL ) { | |
509 char *aut_space; | |
510 aut_space = strstr(aut, "realm="); | |
511 if( aut_space!=NULL ) aut_space += 6; | |
512 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space); | |
513 } else { | |
514 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired); | |
515 } | |
516 if( network_username ) { | |
517 url->username = strdup(network_username); | |
518 if( url->username==NULL ) { | |
519 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); | |
520 return -1; | |
521 } | |
522 } else { | |
523 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed); | |
524 return -1; | |
525 } | |
526 if( network_password ) { | |
527 url->password = strdup(network_password); | |
528 if( url->password==NULL ) { | |
529 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); | |
530 return -1; | |
531 } | |
532 } else { | |
533 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank); | |
534 } | |
535 (*auth_retry)++; | |
536 return 0; | |
537 } | |
538 | |
539 int | |
540 http_seek( stream_t *stream, off_t pos ) { | |
541 HTTP_header_t *http_hdr = NULL; | |
542 int fd; | |
543 if( stream==NULL ) return 0; | |
544 | |
545 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream | |
546 fd = http_send_request( stream->streaming_ctrl->url, pos ); | |
547 if( fd<0 ) return 0; | |
548 | |
549 http_hdr = http_read_response( fd ); | |
550 | |
551 if( http_hdr==NULL ) return 0; | |
552 | |
553 switch( http_hdr->status_code ) { | |
554 case 200: | |
555 case 206: // OK | |
556 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); | |
557 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); | |
558 if( http_hdr->body_size>0 ) { | |
559 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) { | |
560 http_free( http_hdr ); | |
561 return -1; | |
562 } | |
563 } | |
564 break; | |
565 default: | |
566 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase ); | |
567 close( fd ); | |
568 fd = -1; | |
569 } | |
570 stream->fd = fd; | |
571 | |
572 if( http_hdr ) { | |
573 http_free( http_hdr ); | |
574 stream->streaming_ctrl->data = NULL; | |
575 } | |
576 | |
577 stream->pos=pos; | |
578 | |
579 return 1; | |
580 } | |
581 | |
582 | |
583 int | |
584 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) { | |
585 //printf("streaming_bufferize\n"); | |
586 streaming_ctrl->buffer = malloc(size); | |
587 if( streaming_ctrl->buffer==NULL ) { | |
588 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed); | |
589 return -1; | |
590 } | |
591 memcpy( streaming_ctrl->buffer, buffer, size ); | |
592 streaming_ctrl->buffer_size = size; | |
593 return size; | |
594 } | |
595 | |
596 int | |
597 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) { | |
598 int len=0; | |
599 //printf("nop_streaming_read\n"); | |
600 if( stream_ctrl->buffer_size!=0 ) { | |
601 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos; | |
602 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size); | |
603 len = (size<buffer_len)?size:buffer_len; | |
604 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len ); | |
605 stream_ctrl->buffer_pos += len; | |
606 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos ); | |
607 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) { | |
608 free( stream_ctrl->buffer ); | |
609 stream_ctrl->buffer = NULL; | |
610 stream_ctrl->buffer_size = 0; | |
611 stream_ctrl->buffer_pos = 0; | |
612 //printf("buffer cleaned\n"); | |
613 } | |
614 //printf("read %d bytes from buffer\n", len ); | |
615 } | |
616 | |
617 if( len<size ) { | |
618 int ret; | |
619 ret = recv( fd, buffer+len, size-len, 0 ); | |
620 if( ret<0 ) { | |
621 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno)); | |
622 } | |
623 len += ret; | |
624 //printf("read %d bytes from network\n", len ); | |
625 } | |
626 | |
627 return len; | |
628 } | |
629 | |
630 int | |
631 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) { | |
632 return -1; | |
633 // To shut up gcc warning | |
634 fd++; | |
635 pos++; | |
636 stream_ctrl=NULL; | |
637 } | |
638 | |
639 | |
640 void fixup_network_stream_cache(stream_t *stream) { | |
641 if(stream->streaming_ctrl->buffering) { | |
642 if(stream_cache_size<0) { | |
643 // cache option not set, will use our computed value. | |
644 // buffer in KBytes, *5 because the prefill is 20% of the buffer. | |
645 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5; | |
646 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer | |
647 } | |
648 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size); | |
649 } | |
650 } | |
651 | |
652 | |
653 int | |
654 streaming_stop( stream_t *stream ) { | |
655 stream->streaming_ctrl->status = streaming_stopped_e; | |
656 return 0; | |
657 } |