Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / stream / network.c
blobbb866318e098e0870d4a11beba7464ad3488b12a
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"
36 #include "help_mp.h"
38 #if HAVE_WINSOCK2_H
39 #include <winsock2.h>
40 #include <ws2tcpip.h>
41 #endif
43 #include "stream.h"
44 #include "libmpdemux/demuxer.h"
45 #include "m_config.h"
47 #include "network.h"
48 #include "tcp.h"
49 #include "http.h"
50 #include "cookies.h"
51 #include "url.h"
53 #include "version.h"
55 extern int stream_cache_size;
57 /* Variables for the command line option -user, -passwd, -bandwidth,
58 -user-agent and -nocookies */
60 char *network_username=NULL;
61 char *network_password=NULL;
62 int network_bandwidth=0;
63 int network_cookies_enabled = 0;
64 char *network_useragent=NULL;
66 /* IPv6 options */
67 int network_ipv4_only_proxy = 0;
70 const mime_struct_t mime_type_table[] = {
71 #ifdef CONFIG_LIBAVFORMAT
72 // Flash Video
73 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED},
74 // do not force any demuxer in this case!
75 // we want the lavf demuxer to be tried first (happens automatically anyway),
76 // but for mov reference files to work we must also try
77 // the native demuxer if lavf fails.
78 { "video/quicktime", 0 },
79 #endif
80 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
81 { "audio/mpeg", DEMUXER_TYPE_AUDIO },
82 // MPEG streaming
83 { "video/mpeg", DEMUXER_TYPE_UNKNOWN },
84 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN },
85 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN },
86 // AVI ??? => video/x-msvideo
87 { "video/x-msvideo", DEMUXER_TYPE_AVI },
88 // MOV => video/quicktime
89 { "video/quicktime", DEMUXER_TYPE_MOV },
90 // ASF
91 { "audio/x-ms-wax", DEMUXER_TYPE_ASF },
92 { "audio/x-ms-wma", DEMUXER_TYPE_ASF },
93 { "video/x-ms-asf", DEMUXER_TYPE_ASF },
94 { "video/x-ms-afs", DEMUXER_TYPE_ASF },
95 { "video/x-ms-wmv", DEMUXER_TYPE_ASF },
96 { "video/x-ms-wma", DEMUXER_TYPE_ASF },
97 { "application/x-mms-framed", DEMUXER_TYPE_ASF },
98 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF },
99 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN },
100 // Playlists
101 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST },
102 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST },
103 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST },
104 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST },
105 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST },
106 // Real Media
107 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
108 // OGG Streaming
109 { "application/x-ogg", DEMUXER_TYPE_OGG },
110 // NullSoft Streaming Video
111 { "video/nsv", DEMUXER_TYPE_NSV},
112 { "misc/ultravox", DEMUXER_TYPE_NSV},
113 { NULL, DEMUXER_TYPE_UNKNOWN},
117 streaming_ctrl_t *
118 streaming_ctrl_new(void) {
119 streaming_ctrl_t *streaming_ctrl;
120 streaming_ctrl = malloc(sizeof(streaming_ctrl_t));
121 if( streaming_ctrl==NULL ) {
122 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
123 return NULL;
125 memset( streaming_ctrl, 0, sizeof(streaming_ctrl_t) );
126 return streaming_ctrl;
129 void
130 streaming_ctrl_free( streaming_ctrl_t *streaming_ctrl ) {
131 if( streaming_ctrl==NULL ) return;
132 if( streaming_ctrl->url ) url_free( streaming_ctrl->url );
133 if( streaming_ctrl->buffer ) free( streaming_ctrl->buffer );
134 if( streaming_ctrl->data ) free( streaming_ctrl->data );
135 free( streaming_ctrl );
138 URL_t*
139 check4proxies( URL_t *url ) {
140 URL_t *url_out = NULL;
141 if( url==NULL ) return NULL;
142 url_out = url_new( url->url );
143 if( !strcasecmp(url->protocol, "http_proxy") ) {
144 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: http://%s:%d\n", url->hostname, url->port );
145 return url_out;
147 // Check if the http_proxy environment variable is set.
148 if( !strcasecmp(url->protocol, "http") ) {
149 char *proxy;
150 proxy = getenv("http_proxy");
151 if( proxy!=NULL ) {
152 // We got a proxy, build the URL to use it
153 int len;
154 char *new_url;
155 URL_t *tmp_url;
156 URL_t *proxy_url = url_new( proxy );
158 if( proxy_url==NULL ) {
159 mp_msg(MSGT_NETWORK,MSGL_WARN,
160 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout);
161 return url_out;
164 #ifdef HAVE_AF_INET6
165 if (network_ipv4_only_proxy && (gethostbyname(url->hostname)==NULL)) {
166 mp_msg(MSGT_NETWORK,MSGL_WARN,
167 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy);
168 url_free(proxy_url);
169 return url_out;
171 #endif
173 mp_msg(MSGT_NETWORK,MSGL_V,"Using HTTP proxy: %s\n", proxy_url->url );
174 len = strlen( proxy_url->hostname ) + strlen( url->url ) + 20; // 20 = http_proxy:// + port
175 new_url = malloc( len+1 );
176 if( new_url==NULL ) {
177 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
178 url_free(proxy_url);
179 return url_out;
181 sprintf(new_url, "http_proxy://%s:%d/%s", proxy_url->hostname, proxy_url->port, url->url );
182 tmp_url = url_new( new_url );
183 if( tmp_url==NULL ) {
184 free( new_url );
185 url_free( proxy_url );
186 return url_out;
188 url_free( url_out );
189 url_out = tmp_url;
190 free( new_url );
191 url_free( proxy_url );
194 return url_out;
198 http_send_request( URL_t *url, off_t pos ) {
199 HTTP_header_t *http_hdr;
200 URL_t *server_url;
201 char str[256];
202 int fd = -1;
203 int ret;
204 int proxy = 0; // Boolean
206 http_hdr = http_new_header();
208 if( !strcasecmp(url->protocol, "http_proxy") ) {
209 proxy = 1;
210 server_url = url_new( (url->file)+1 );
211 http_set_uri( http_hdr, server_url->url );
212 } else {
213 server_url = url;
214 http_set_uri( http_hdr, server_url->file );
216 if (server_url->port && server_url->port != 80)
217 snprintf(str, 256, "Host: %s:%d", server_url->hostname, server_url->port );
218 else
219 snprintf(str, 256, "Host: %s", server_url->hostname );
220 http_set_field( http_hdr, str);
221 if (network_useragent)
223 snprintf(str, 256, "User-Agent: %s", network_useragent);
224 http_set_field(http_hdr, str);
226 else
227 http_set_field( http_hdr, "User-Agent: MPlayer/"VERSION);
229 if( strcasecmp(url->protocol, "noicyx") )
230 http_set_field(http_hdr, "Icy-MetaData: 1");
232 if(pos>0) {
233 // Extend http_send_request with possibility to do partial content retrieval
234 snprintf(str, 256, "Range: bytes=%"PRId64"-", (int64_t)pos);
235 http_set_field(http_hdr, str);
238 if (network_cookies_enabled) cookies_set( http_hdr, server_url->hostname, server_url->url );
240 http_set_field( http_hdr, "Connection: close");
241 http_add_basic_authentication( http_hdr, url->username, url->password );
242 if( http_build_request( http_hdr )==NULL ) {
243 goto err_out;
246 if( proxy ) {
247 if( url->port==0 ) url->port = 8080; // Default port for the proxy server
248 fd = connect2Server( url->hostname, url->port,1 );
249 url_free( server_url );
250 server_url = NULL;
251 } else {
252 if( server_url->port==0 ) server_url->port = 80; // Default port for the web server
253 fd = connect2Server( server_url->hostname, server_url->port,1 );
255 if( fd<0 ) {
256 goto err_out;
258 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request: [%s]\n", http_hdr->buffer );
260 ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0 );
261 if( ret!=(int)http_hdr->buffer_size ) {
262 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest);
263 goto err_out;
266 http_free( http_hdr );
268 return fd;
269 err_out:
270 if (fd > 0) closesocket(fd);
271 http_free(http_hdr);
272 if (proxy && server_url)
273 url_free(server_url);
274 return -1;
277 HTTP_header_t *
278 http_read_response( int fd ) {
279 HTTP_header_t *http_hdr;
280 char response[BUFFER_SIZE];
281 int i;
283 http_hdr = http_new_header();
284 if( http_hdr==NULL ) {
285 return NULL;
288 do {
289 i = recv( fd, response, BUFFER_SIZE, 0 );
290 if( i<0 ) {
291 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ReadFailed);
292 http_free( http_hdr );
293 return NULL;
295 if( i==0 ) {
296 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_Read0CouldBeEOF);
297 http_free( http_hdr );
298 return NULL;
300 http_response_append( http_hdr, response, i );
301 } while( !http_is_header_entire( http_hdr ) );
302 if (http_response_parse( http_hdr ) < 0) {
303 http_free( http_hdr );
304 return NULL;
306 return http_hdr;
310 http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry) {
311 char *aut;
313 if( *auth_retry==1 ) {
314 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
315 return -1;
317 if( *auth_retry>0 ) {
318 if( url->username ) {
319 free( url->username );
320 url->username = NULL;
322 if( url->password ) {
323 free( url->password );
324 url->password = NULL;
328 aut = http_get_field(http_hdr, "WWW-Authenticate");
329 if( aut!=NULL ) {
330 char *aut_space;
331 aut_space = strstr(aut, "realm=");
332 if( aut_space!=NULL ) aut_space += 6;
333 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequiredFor, aut_space);
334 } else {
335 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_AuthRequired);
337 if( network_username ) {
338 url->username = strdup(network_username);
339 if( url->username==NULL ) {
340 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
341 return -1;
343 } else {
344 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_AuthFailed);
345 return -1;
347 if( network_password ) {
348 url->password = strdup(network_password);
349 if( url->password==NULL ) {
350 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
351 return -1;
353 } else {
354 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank);
356 (*auth_retry)++;
357 return 0;
361 http_seek( stream_t *stream, off_t pos ) {
362 HTTP_header_t *http_hdr = NULL;
363 int fd;
364 if( stream==NULL ) return 0;
366 if( stream->fd>0 ) closesocket(stream->fd); // need to reconnect to seek in http-stream
367 fd = http_send_request( stream->streaming_ctrl->url, pos );
368 if( fd<0 ) return 0;
370 http_hdr = http_read_response( fd );
372 if( http_hdr==NULL ) return 0;
374 if( mp_msg_test(MSGT_NETWORK,MSGL_V) )
375 http_debug_hdr( http_hdr );
377 switch( http_hdr->status_code ) {
378 case 200:
379 case 206: // OK
380 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") );
381 mp_msg(MSGT_NETWORK,MSGL_V,"Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") );
382 if( http_hdr->body_size>0 ) {
383 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
384 http_free( http_hdr );
385 return -1;
388 break;
389 default:
390 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_NW_ErrServerReturned, http_hdr->status_code, http_hdr->reason_phrase );
391 closesocket( fd );
392 fd = -1;
394 stream->fd = fd;
396 if( http_hdr ) {
397 http_free( http_hdr );
398 stream->streaming_ctrl->data = NULL;
401 stream->pos=pos;
403 return 1;
408 streaming_bufferize( streaming_ctrl_t *streaming_ctrl, char *buffer, int size) {
409 //printf("streaming_bufferize\n");
410 streaming_ctrl->buffer = malloc(size);
411 if( streaming_ctrl->buffer==NULL ) {
412 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
413 return -1;
415 memcpy( streaming_ctrl->buffer, buffer, size );
416 streaming_ctrl->buffer_size = size;
417 return size;
421 nop_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *stream_ctrl ) {
422 int len=0;
423 //printf("nop_streaming_read\n");
424 if( stream_ctrl->buffer_size!=0 ) {
425 int buffer_len = stream_ctrl->buffer_size-stream_ctrl->buffer_pos;
426 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
427 len = (size<buffer_len)?size:buffer_len;
428 memcpy( buffer, (stream_ctrl->buffer)+(stream_ctrl->buffer_pos), len );
429 stream_ctrl->buffer_pos += len;
430 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
431 if( stream_ctrl->buffer_pos>=stream_ctrl->buffer_size ) {
432 free( stream_ctrl->buffer );
433 stream_ctrl->buffer = NULL;
434 stream_ctrl->buffer_size = 0;
435 stream_ctrl->buffer_pos = 0;
436 //printf("buffer cleaned\n");
438 //printf("read %d bytes from buffer\n", len );
441 if( len<size ) {
442 int ret;
443 ret = recv( fd, buffer+len, size-len, 0 );
444 if( ret<0 ) {
445 mp_msg(MSGT_NETWORK,MSGL_ERR,"nop_streaming_read error : %s\n",strerror(errno));
447 len += ret;
448 //printf("read %d bytes from network\n", len );
451 return len;
455 nop_streaming_seek( int fd, off_t pos, streaming_ctrl_t *stream_ctrl ) {
456 return -1;
457 // To shut up gcc warning
458 fd++;
459 pos++;
460 stream_ctrl=NULL;
464 void fixup_network_stream_cache(stream_t *stream) {
465 if(stream->streaming_ctrl->buffering) {
466 if(stream_cache_size<0) {
467 // cache option not set, will use our computed value.
468 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
469 stream_cache_size = (stream->streaming_ctrl->prebuffer_size/1024)*5;
470 if( stream_cache_size<64 ) stream_cache_size = 64; // 16KBytes min buffer
472 mp_msg(MSGT_NETWORK,MSGL_INFO,MSGTR_MPDEMUX_NW_CacheSizeSetTo, stream_cache_size);