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.
41 #include "libmpdemux/demuxer.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
;
64 int network_ipv4_only_proxy
= 0;
67 const mime_struct_t mime_type_table
[] = {
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
},
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
},
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
},
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
},
102 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
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
},
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");
119 return streaming_ctrl
;
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
);
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
);
140 // Check if the http_proxy environment variable is set.
141 if( !strcasecmp(url
->protocol
, "http") ) {
143 proxy
= getenv("http_proxy");
145 // We got a proxy, build the URL to use it
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");
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");
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");
174 make_http_proxy_url(proxy_url
, url
->url
, new_url
, len
);
175 tmp_url
= url_new( new_url
);
176 if( tmp_url
==NULL
) {
178 url_free( proxy_url
);
184 url_free( proxy_url
);
191 http_send_request( URL_t
*url
, off_t pos
) {
192 HTTP_header_t
*http_hdr
;
197 int proxy
= 0; // Boolean
199 http_hdr
= http_new_header();
201 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
203 server_url
= url_new( (url
->file
)+1 );
205 mp_msg(MSGT_NETWORK
, MSGL_ERR
, "Invalid URL '%s' to proxify\n", url
->file
+1);
208 http_set_uri( http_hdr
, server_url
->noauth_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
);
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
);
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
230 referrer
= malloc(len
);
232 if (referrer
== NULL
) {
233 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Memory allocation failed.\n");
235 snprintf(referrer
, len
, "Referer: %s", network_referrer
);
236 http_set_field(http_hdr
, referrer
);
241 if( strcasecmp(url
->protocol
, "noicyx") )
242 http_set_field(http_hdr
, "Icy-MetaData: 1");
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
) {
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");
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
) {
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
);
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 );
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");
286 http_free( http_hdr
);
290 if (fd
> 0) closesocket(fd
);
292 if (proxy
&& server_url
)
293 url_free(server_url
);
298 http_read_response( int fd
) {
299 HTTP_header_t
*http_hdr
;
300 char response
[BUFFER_SIZE
];
303 http_hdr
= http_new_header();
304 if( http_hdr
==NULL
) {
309 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
311 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Read failed.\n");
312 http_free( http_hdr
);
316 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"http_read_response read 0 (i.e. EOF).\n");
317 http_free( http_hdr
);
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
);
330 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
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
);
343 if( *auth_retry
>0 ) {
345 url
->username
= NULL
;
347 url
->password
= NULL
;
350 aut
= http_get_field(http_hdr
, "WWW-Authenticate");
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
);
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");
366 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,MPDEMUX_NW_AuthFailed
);
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");
376 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"No password provided, trying blank password.\n");
383 http_seek( stream_t
*stream
, off_t pos
) {
384 HTTP_header_t
*http_hdr
= NULL
;
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
);
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
) {
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
);
412 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Server returns %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
419 http_free( http_hdr
);
420 stream
->streaming_ctrl
->data
= NULL
;
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");
437 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
438 streaming_ctrl
->buffer_size
= size
;
443 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
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 );
465 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
467 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
470 stream_ctrl
->status
= streaming_stopped_e
;
472 //printf("read %d bytes from network\n", len );
479 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {
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
);