mixer: support native audio driver mute
[mplayer.git] / stream / network.c
blob0961c8d12b002939a6308baf22003343e58439d1
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 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include <errno.h>
29 #include <ctype.h>
31 #include "config.h"
33 #include "mp_msg.h"
35 #if HAVE_WINSOCK2_H
36 #include <winsock2.h>
37 #include <ws2tcpip.h>
38 #endif
40 #include "stream.h"
41 #include "libmpdemux/demuxer.h"
42 #include "m_config.h"
43 #include "mpcommon.h"
44 #include "network.h"
45 #include "tcp.h"
46 #include "http.h"
47 #include "cookies.h"
48 #include "url.h"
50 extern int stream_cache_size;
52 /* Variables for the command line option -user, -passwd, -bandwidth,
53 -user-agent and -nocookies */
55 char *network_username=NULL;
56 char *network_password=NULL;
57 int network_bandwidth=0;
58 int network_cookies_enabled = 0;
59 char *network_useragent=NULL;
60 char *network_referrer=NULL;
61 char **network_http_header_fields=NULL;
63 /* IPv6 options */
64 int network_ipv4_only_proxy = 0;
67 const mime_struct_t mime_type_table[] = {
68 // Flash Video
69 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
70 // do not force any demuxer in this case!
71 // we want the lavf demuxer to be tried first (happens automatically anyway),
72 // but for mov reference files to work we must also try
73 // the native demuxer if lavf fails.
74 { "video/quicktime", 0 },
75 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
76 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
77 // MPEG streaming
78 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
79 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
80 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
81 // AVI ??? => video/x-msvideo
82 { "video/x-msvideo", DEMUXER_TYPE_AVI },
83 // MOV => video/quicktime
84 { "video/quicktime", DEMUXER_TYPE_MOV },
85 // ASF
86 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
87 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
88 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
89 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
90 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
91 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
92 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
93 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
94 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
95 // Playlists
96 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
97 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
98 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
99 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
100 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
101 // Real Media
102 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
103 // OGG Streaming
104 { "application/x-ogg", DEMUXER_TYPE_OGG },
105 // NullSoft Streaming Video
106 { "video/nsv", DEMUXER_TYPE_NSV},
107 { "misc/ultravox", DEMUXER_TYPE_NSV},
108 { NULL, DEMUXER_TYPE_UNKNOWN},
112 streaming_ctrl_t *
113 streaming_ctrl_new(void) {
114 streaming_ctrl_t *streaming_ctrl = calloc(1, sizeof(*streaming_ctrl));
115 if( streaming_ctrl==NULL ) {
116 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
117 return NULL;
119 return streaming_ctrl;
122 void
123 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
124 if( streaming_ctrl==NULL ) return;
125 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
126 free(streaming_ctrl->buffer);
127 free(streaming_ctrl->data);
128 free(streaming_ctrl);
131 URL_t*
132 check4proxies( URL_t *url ) {
133 URL_t *url_out = NULL;
134 if( url==NULL ) return NULL;
135 url_out = url_new( url->url );
136 if( !strcasecmp(url->protocol, "http_proxy") ) {
137 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
138 return url_out;
140 // Check if the http_proxy environment variable is set.
141 if( !strcasecmp(url->protocol, "http") ) {
142 char *proxy;
143 proxy = getenv("http_proxy");
144 if( proxy!=NULL ) {
145 // We got a proxy, build the URL to use it
146 int len;
147 char *new_url;
148 URL_t *tmp_url;
149 URL_t *proxy_url = url_new( proxy );
151 if( proxy_url==NULL ) {
152 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
153 "Invalid proxy setting... Trying without proxy.\n");
154 return url_out;
157 #ifdef HAVE_AF_INET6
158 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
159 mp_tmsg(MSGT_NETWORK,MSGL_WARN,
160 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
161 url_free(proxy_url);
162 return url_out;
164 #endif
166 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
167 len = make_http_proxy_url(proxy_url, url->url, NULL, 0) + 1;
168 new_url = malloc(len);
169 if( new_url==NULL ) {
170 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
171 url_free(proxy_url);
172 return url_out;
174 make_http_proxy_url(proxy_url, url->url, new_url, len);
175 tmp_url = url_new( new_url );
176 if( tmp_url==NULL ) {
177 free( new_url );
178 url_free( proxy_url );
179 return url_out;
181 url_free( url_out );
182 url_out = tmp_url;
183 free( new_url );
184 url_free( proxy_url );
187 return url_out;
191 http_send_request( URL_t *url, off_t pos ) {
192 HTTP_header_t *http_hdr;
193 URL_t *server_url;
194 char str[256];
195 int fd = -1;
196 int ret;
197 int proxy = 0; // Boolean
199 http_hdr = http_new_header();
201 if( !strcasecmp(url->protocol, "http_proxy") ) {
202 proxy = 1;
203 server_url = url_new( (url->file)+1 );
204 if (!server_url) {
205 mp_msg(MSGT_NETWORK, MSGL_ERR, "Invalid URL '%s' to proxify\n", url->file+1);
206 goto err_out;
208 http_set_uri( http_hdr, server_url->noauth_url );
209 } else {
210 server_url = url;
211 http_set_uri( http_hdr, server_url->file );
213 if (server_url->port && server_url->port != 80)
214 snprintf(str, sizeof(str), "Host: %s:%d", server_url->hostname, server_url->port );
215 else
216 snprintf(str, sizeof(str), "Host: %s", server_url->hostname );
217 http_set_field( http_hdr, str);
218 if (network_useragent)
219 snprintf(str, sizeof(str), "User-Agent: %s", network_useragent);
220 else
221 snprintf(str, sizeof(str), "User-Agent: %s", mplayer_version);
222 http_set_field(http_hdr, str);
224 if (network_referrer) {
225 char *referrer = NULL;
226 size_t len = strlen(network_referrer) + 10;
228 // Check len to ensure we don't do something really bad in case of an overflow
229 if (len > 10)
230 referrer = malloc(len);
232 if (referrer == NULL) {
233 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Memory allocation failed.\n");
234 } else {
235 snprintf(referrer, len, "Referer: %s", network_referrer);
236 http_set_field(http_hdr, referrer);
237 free(referrer);
241 if( strcasecmp(url->protocol, "noicyx") )
242 http_set_field(http_hdr, "Icy-MetaData: 1");
244 if(pos>0) {
245 // Extend http_send_request with possibility to do partial content retrieval
246 snprintf(str, sizeof(str), "Range: bytes=%"PRId64"-", (int64_t)pos);
247 http_set_field(http_hdr, str);
250 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
252 if (network_http_header_fields) {
253 int i=0;
254 while (network_http_header_fields[i])
255 http_set_field(http_hdr, network_http_header_fields[i++]);
258 http_set_field( http_hdr, "Connection: close");
259 if (proxy)
260 http_add_basic_proxy_authentication(http_hdr, url->username, url->password);
261 http_add_basic_authentication(http_hdr, server_url->username, server_url->password);
262 if( http_build_request( http_hdr )==NULL ) {
263 goto err_out;
266 if( proxy ) {
267 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
268 fd = connect2Server( url->hostname, url->port,1 );
269 url_free( server_url );
270 server_url = NULL;
271 } else {
272 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
273 fd = connect2Server( server_url->hostname, server_url->port,1 );
275 if( fd<0 ) {
276 goto err_out;
278 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
280 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, DEFAULT_SEND_FLAGS );
281 if( ret!=(int)http_hdr->buffer_size ) {
282 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while sending HTTP request: Didn't send all the request.\n");
283 goto err_out;
286 http_free( http_hdr );
288 return fd;
289 err_out:
290 if (fd > 0) closesocket(fd);
291 http_free(http_hdr);
292 if (proxy && server_url)
293 url_free(server_url);
294 return -1;
297 HTTP_header_t *
298 http_read_response( int fd ) {
299 HTTP_header_t *http_hdr;
300 char response[BUFFER_SIZE];
301 int i;
303 http_hdr = http_new_header();
304 if( http_hdr==NULL ) {
305 return NULL;
308 do {
309 i = recv( fd, response, BUFFER_SIZE, 0 );
310 if( i<0 ) {
311 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Read failed.\n");
312 http_free( http_hdr );
313 return NULL;
315 if( i==0 ) {
316 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"http_read_response read 0 (i.e. EOF).\n");
317 http_free( http_hdr );
318 return NULL;
320 http_response_append( http_hdr, response, i );
321 } while( !http_is_header_entire( http_hdr ) );
322 if (http_response_parse( http_hdr ) < 0) {
323 http_free( http_hdr );
324 return NULL;
326 return http_hdr;
330 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
331 char *aut;
333 #define MPDEMUX_NW_AuthFailed _(\
334 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
335 "username/password for a list of URLs, or form an URL like:\n"\
336 "http://username:password@hostname/file\n")
339 if( *auth_retry==1 ) {
340 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
341 return -1;
343 if( *auth_retry>0 ) {
344 free(url->username);
345 url->username = NULL;
346 free(url->password);
347 url->password = NULL;
350 aut = http_get_field(http_hdr, "WWW-Authenticate");
351 if( aut!=NULL ) {
352 char *aut_space;
353 aut_space = strstr(aut, "realm=");
354 if( aut_space!=NULL ) aut_space += 6;
355 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required for %s\n", aut_space);
356 } else {
357 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Authentication required.\n");
359 if( network_username ) {
360 url->username = strdup(network_username);
361 if( url->username==NULL ) {
362 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
363 return -1;
365 } else {
366 mp_tmsg(MSGT_NETWORK,MSGL_ERR,MPDEMUX_NW_AuthFailed);
367 return -1;
369 if( network_password ) {
370 url->password = strdup(network_password);
371 if( url->password==NULL ) {
372 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
373 return -1;
375 } else {
376 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"No password provided, trying blank password.\n");
378 (*auth_retry)++;
379 return 0;
383 http_seek( stream_t *stream, off_t pos ) {
384 HTTP_header_t *http_hdr = NULL;
385 int fd;
386 if( stream==NULL ) return 0;
388 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
389 fd = http_send_request( stream->streaming_ctrl->url, pos );
390 if( fd<0 ) return 0;
392 http_hdr = http_read_response( fd );
394 if( http_hdr==NULL ) return 0;
396 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
397 http_debug_hdr( http_hdr );
399 switch( http_hdr->status_code ) {
400 case 200:
401 case 206: // OK
402 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
403 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
404 if( http_hdr->body_size>0 ) {
405 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
406 http_free( http_hdr );
407 return -1;
410 break;
411 default:
412 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returns %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase );
413 closesocket( fd );
414 fd = -1;
416 stream->fd = fd;
418 if( http_hdr ) {
419 http_free( http_hdr );
420 stream->streaming_ctrl->data = NULL;
423 stream->pos=pos;
425 return 1;
430 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
431 //printf("streaming_bufferize\n");
432 streaming_ctrl->buffer = malloc(size);
433 if( streaming_ctrl->buffer==NULL ) {
434 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
435 return -1;
437 memcpy( streaming_ctrl->buffer, buffer, size );
438 streaming_ctrl->buffer_size = size;
439 return size;
443 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
444 int len=0;
445 //printf("nop_streaming_read\n");
446 if( stream_ctrl->buffer_size!=0 ) {
447 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
448 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
449 len = (size<buffer_len)?size:buffer_len;
450 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
451 stream_ctrl->buffer_pos += len;
452 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
453 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
454 free( stream_ctrl->buffer );
455 stream_ctrl->buffer = NULL;
456 stream_ctrl->buffer_size = 0;
457 stream_ctrl->buffer_pos = 0;
458 //printf("buffer cleaned\n");
460 //printf("read %d bytes from buffer\n", len );
463 if( len<size ) {
464 int ret;
465 ret = recv( fd, buffer+len, size-len, 0 );
466 if( ret<0 ) {
467 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
468 ret = 0;
469 } else if (ret == 0)
470 stream_ctrl->status = streaming_stopped_e;
471 len += ret;
472 //printf("read %d bytes from network\n", len );
475 return len;
479 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
480 return -1;
484 void fixup_network_stream_cache(stream_t *stream) {
485 if(stream->streaming_ctrl->buffering) {
486 if(stream_cache_size<0) {
487 // cache option not set, will use our computed value.
488 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
489 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
490 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
492 mp_tmsg(MSGT_NETWORK,MSGL_INFO,"Cache size set to %d KBytes\n", stream_cache_size);