vo_gl: Try to get a quadbuffered visual for corresponding 3D mode
[mplayer/glamo.git] / stream / network.c
blob48d50d5de5cee9b55617646fc9701b4e3df1dc9a
1 /*
2 * Network layer for MPlayer
4 * Copyright (C) 2001 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 //#define DUMP2FILE
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include <errno.h>
31 #include <ctype.h>
33 #include "config.h"
35 #include "mp_msg.h"
37 #if HAVE_WINSOCK2_H
38 #include <winsock2.h>
39 #include <ws2tcpip.h>
40 #endif
42 #include "stream.h"
43 #include "libmpdemux/demuxer.h"
44 #include "m_config.h"
45 #include "mpcommon.h"
46 #include "network.h"
47 #include "tcp.h"
48 #include "http.h"
49 #include "cookies.h"
50 #include "url.h"
52 extern int stream_cache_size;
54 /* Variables for the command line option -user, -passwd, -bandwidth,
55 -user-agent and -nocookies */
57 char *network_username=NULL;
58 char *network_password=NULL;
59 int network_bandwidth=0;
60 int network_cookies_enabled = 0;
61 char *network_useragent=NULL;
62 char *network_referrer=NULL;
64 /* IPv6 options */
65 int network_ipv4_only_proxy = 0;
68 const mime_struct_t mime_type_table[] = {
69 #ifdef CONFIG_LIBAVFORMAT
70 // Flash Video
71 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
72 // do not force any demuxer in this case!
73 // we want the lavf demuxer to be tried first (happens automatically anyway),
74 // but for mov reference files to work we must also try
75 // the native demuxer if lavf fails.
76 { "video/quicktime", 0 },
77 #endif
78 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
79 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
80 // MPEG streaming
81 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
82 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
83 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
84 // AVI ??? => video/x-msvideo
85 { "video/x-msvideo", DEMUXER_TYPE_AVI },
86 // MOV => video/quicktime
87 { "video/quicktime", DEMUXER_TYPE_MOV },
88 // ASF
89 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
90 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
91 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
92 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
93 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
94 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
95 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
96 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
97 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
98 // Playlists
99 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
100 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
101 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
102 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
103 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
104 // Real Media
105 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
106 // OGG Streaming
107 { "application/x-ogg", DEMUXER_TYPE_OGG },
108 // NullSoft Streaming Video
109 { "video/nsv", DEMUXER_TYPE_NSV},
110 { "misc/ultravox", DEMUXER_TYPE_NSV},
111 { NULL, DEMUXER_TYPE_UNKNOWN},
115 streaming_ctrl_t *
116 streaming_ctrl_new(void) {
117 streaming_ctrl_t *streaming_ctrl;
118 streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
119 if( streaming_ctrl==NULL ) {
120 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
121 return NULL;
123 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
124 return streaming_ctrl;
127 void
128 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
129 if( streaming_ctrl==NULL ) return;
130 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
131 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
132 if( streaming_ctrl->data ) free( streaming_ctrl->data );
133 free( streaming_ctrl );
136 URL_t*
137 check4proxies( URL_t *url ) {
138 URL_t *url_out = NULL;
139 if( url==NULL ) return NULL;
140 url_out = url_new( url->url );
141 if( !strcasecmp(url->protocol, "http_proxy") ) {
142 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
143 return url_out;
145 // Check if the http_proxy environment variable is set.
146 if( !strcasecmp(url->protocol, "http") ) {
147 char *proxy;
148 proxy = getenv("http_proxy");
149 if( proxy!=NULL ) {
150 // We got a proxy, build the URL to use it
151 int len;
152 char *new_url;
153 URL_t *tmp_url;
154 URL_t *proxy_url = url_new( proxy );
156 if( proxy_url==NULL ) {
157 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
158 "Invalid proxy setting... Trying without proxy.\n");
159 return url_out;
162 #ifdef HAVE_AF_INET6
163 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
164 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
165 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
166 url_free(proxy_url);
167 return url_out;
169 #endif
171 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
172 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
173 new_url = malloc( len+1 );
174 if( new_url==NULL ) {
175 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
176 url_free(proxy_url);
177 return url_out;
179 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
180 tmp_url = url_new( new_url );
181 if( tmp_url==NULL ) {
182 free( new_url );
183 url_free( proxy_url );
184 return url_out;
186 url_free( url_out );
187 url_out = tmp_url;
188 free( new_url );
189 url_free( proxy_url );
192 return url_out;
196 http_send_request( URL_t *url, off_t pos ) {
197 HTTP_header_t *http_hdr;
198 URL_t *server_url;
199 char str[256];
200 int fd = -1;
201 int ret;
202 int proxy = 0; // Boolean
204 http_hdr = http_new_header();
206 if( !strcasecmp(url->protocol, "http_proxy") ) {
207 proxy = 1;
208 server_url = url_new( (url->file)+1 );
209 http_set_uri( http_hdr, server_url->url );
210 } else {
211 server_url = url;
212 http_set_uri( http_hdr, server_url->file );
214 if (server_url->port && server_url->port != 80)
215 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
216 else
217 snprintf(str, 256, "Host: %s", server_url->hostname );
218 http_set_field( http_hdr, str);
219 if (network_useragent)
220 snprintf(str, 256, "User-Agent: %s", network_useragent);
221 else
222 snprintf(str, 256, "User-Agent: %s", mplayer_version);
223 http_set_field(http_hdr, str);
225 if (network_referrer) {
226 char *referrer = NULL;
227 size_t len = strlen(network_referrer) + 10;
229 // Check len to ensure we don't do something really bad in case of an overflow
230 if (len > 10)
231 referrer = malloc(len);
233 if (referrer == NULL) {
234 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
235 } else {
236 snprintf(referrer, len, "Referer: %s", network_referrer);
237 http_set_field(http_hdr, referrer);
238 free(referrer);
242 if( strcasecmp(url->protocol, "noicyx") )
243 http_set_field(http_hdr, "Icy-MetaData: 1");
245 if(pos>0) {
246 // Extend http_send_request with possibility to do partial content retrieval
247 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
248 http_set_field(http_hdr, str);
251 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
253 http_set_field( http_hdr, "Connection: close");
254 http_add_basic_authentication( http_hdr, url->username, url->password );
255 if( http_build_request( http_hdr )==NULL ) {
256 goto err_out;
259 if( proxy ) {
260 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
261 fd = connect2Server( url->hostname, url->port,1 );
262 url_free( server_url );
263 server_url = NULL;
264 } else {
265 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
266 fd = connect2Server( server_url->hostname, server_url->port,1 );
268 if( fd<0 ) {
269 goto err_out;
271 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
273 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS );
274 if( ret!=(int)http_hdr->buffer_size ) {
275 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
276 goto err_out;
279 http_free( http_hdr );
281 return fd;
282 err_out:
283 if (fd > 0) closesocket(fd);
284 http_free(http_hdr);
285 if (proxy && server_url)
286 url_free(server_url);
287 return -1;
290 HTTP_header_t *
291 http_read_response( int fd ) {
292 HTTP_header_t *http_hdr;
293 char response[BUFFER_SIZE];
294 int i;
296 http_hdr = http_new_header();
297 if( http_hdr==NULL ) {
298 return NULL;
301 do {
302 i = recv( fd, response, BUFFER_SIZE, 0 );
303 if( i<0 ) {
304 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
305 http_free( http_hdr );
306 return NULL;
308 if( i==0 ) {
309 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
310 http_free( http_hdr );
311 return NULL;
313 http_response_append( http_hdr, response, i );
314 } while( !http_is_header_entire( http_hdr ) );
315 if (http_response_parse( http_hdr ) < 0) {
316 http_free( http_hdr );
317 return NULL;
319 return http_hdr;
323 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
324 char *aut;
326 #define MPDEMUX_NW_AuthFailed _(\
327 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
328 "username/password for a list of URLs, or form an URL like:\n"\
329 "http://username:password@hostname/file\n")
332 if( *auth_retry==1 ) {
333 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
334 return -1;
336 if( *auth_retry>0 ) {
337 if( url->username ) {
338 free( url->username );
339 url->username = NULL;
341 if( url->password ) {
342 free( url->password );
343 url->password = NULL;
347 aut = http_get_field(http_hdr, "WWW-Authenticate");
348 if( aut!=NULL ) {
349 char *aut_space;
350 aut_space = strstr(aut, "realm=");
351 if( aut_space!=NULL ) aut_space += 6;
352 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
353 } else {
354 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
356 if( network_username ) {
357 url->username = strdup(network_username);
358 if( url->username==NULL ) {
359 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
360 return -1;
362 } else {
363 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
364 return -1;
366 if( network_password ) {
367 url->password = strdup(network_password);
368 if( url->password==NULL ) {
369 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
370 return -1;
372 } else {
373 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
375 (*auth_retry)++;
376 return 0;
380 http_seek( stream_t *stream, off_t pos ) {
381 HTTP_header_t *http_hdr = NULL;
382 int fd;
383 if( stream==NULL ) return 0;
385 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
386 fd = http_send_request( stream->streaming_ctrl->url, pos );
387 if( fd<0 ) return 0;
389 http_hdr = http_read_response( fd );
391 if( http_hdr==NULL ) return 0;
393 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
394 http_debug_hdr( http_hdr );
396 switch( http_hdr->status_code ) {
397 case 200:
398 case 206: // OK
399 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
400 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
401 if( http_hdr->body_size>0 ) {
402 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
403 http_free( http_hdr );
404 return -1;
407 break;
408 default:
409 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
410 closesocket( fd );
411 fd = -1;
413 stream->fd = fd;
415 if( http_hdr ) {
416 http_free( http_hdr );
417 stream->streaming_ctrl->data = NULL;
420 stream->pos=pos;
422 return 1;
427 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
428 //printf("streaming_bufferize\n");
429 streaming_ctrl->buffer = malloc(size);
430 if( streaming_ctrl->buffer==NULL ) {
431 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
432 return -1;
434 memcpy( streaming_ctrl->buffer, buffer, size );
435 streaming_ctrl->buffer_size = size;
436 return size;
440 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
441 int len=0;
442 //printf("nop_streaming_read\n");
443 if( stream_ctrl->buffer_size!=0 ) {
444 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
445 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
446 len = (size<buffer_len)?size:buffer_len;
447 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
448 stream_ctrl->buffer_pos += len;
449 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
450 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
451 free( stream_ctrl->buffer );
452 stream_ctrl->buffer = NULL;
453 stream_ctrl->buffer_size = 0;
454 stream_ctrl->buffer_pos = 0;
455 //printf("buffer cleaned\n");
457 //printf("read %d bytes from buffer\n", len );
460 if( len<size ) {
461 int ret;
462 ret = recv( fd, buffer+len, size-len, 0 );
463 if( ret<0 ) {
464 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
466 len += ret;
467 //printf("read %d bytes from network\n", len );
470 return len;
474 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
475 return -1;
476 // To shut up gcc warning
477 fd++;
478 pos++;
479 stream_ctrl=NULL;
483 void fixup_network_stream_cache(stream_t *stream) {
484 if(stream->streaming_ctrl->buffering) {
485 if(stream_cache_size<0) {
486 // cache option not set, will use our computed value.
487 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
488 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
489 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
491 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", stream_cache_size);