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.
43 #include "libmpdemux/demuxer.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
;
65 int network_ipv4_only_proxy
= 0;
68 const mime_struct_t mime_type_table
[] = {
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 },
78 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
79 { "audio/mpeg", DEMUXER_TYPE_AUDIO
},
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
},
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
},
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
},
105 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
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
},
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");
123 memset( streaming_ctrl
, 0, sizeof(streaming_ctrl_t
) );
124 return streaming_ctrl
;
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
);
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
);
145 // Check if the http_proxy environment variable is set.
146 if( !strcasecmp(url
->protocol
, "http") ) {
148 proxy
= getenv("http_proxy");
150 // We got a proxy, build the URL to use it
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");
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");
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");
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
) {
183 url_free( proxy_url
);
189 url_free( proxy_url
);
196 http_send_request( URL_t
*url
, off_t pos
) {
197 HTTP_header_t
*http_hdr
;
202 int proxy
= 0; // Boolean
204 http_hdr
= http_new_header();
206 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
208 server_url
= url_new( (url
->file
)+1 );
210 mp_msg(MSGT_NETWORK
, MSGL_ERR
, "Invalid URL '%s' to proxify\n", url
->file
+1);
213 http_set_uri( http_hdr
, server_url
->url
);
216 http_set_uri( http_hdr
, server_url
->file
);
218 if (server_url
->port
&& server_url
->port
!= 80)
219 snprintf(str
, 256, "Host: %s:%d", server_url
->hostname
, server_url
->port
);
221 snprintf(str
, 256, "Host: %s", server_url
->hostname
);
222 http_set_field( http_hdr
, str
);
223 if (network_useragent
)
224 snprintf(str
, 256, "User-Agent: %s", network_useragent
);
226 snprintf(str
, 256, "User-Agent: %s", mplayer_version
);
227 http_set_field(http_hdr
, str
);
229 if (network_referrer
) {
230 char *referrer
= NULL
;
231 size_t len
= strlen(network_referrer
) + 10;
233 // Check len to ensure we don't do something really bad in case of an overflow
235 referrer
= malloc(len
);
237 if (referrer
== NULL
) {
238 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Memory allocation failed.\n");
240 snprintf(referrer
, len
, "Referer: %s", network_referrer
);
241 http_set_field(http_hdr
, referrer
);
246 if( strcasecmp(url
->protocol
, "noicyx") )
247 http_set_field(http_hdr
, "Icy-MetaData: 1");
250 // Extend http_send_request with possibility to do partial content retrieval
251 snprintf(str
, 256, "Range: bytes=%"PRId64
"-", (int64_t)pos
);
252 http_set_field(http_hdr
, str
);
255 if (network_cookies_enabled
) cookies_set( http_hdr
, server_url
->hostname
, server_url
->url
);
257 http_set_field( http_hdr
, "Connection: close");
258 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
259 if( http_build_request( http_hdr
)==NULL
) {
264 if( url
->port
==0 ) url
->port
= 8080; // Default port for the proxy server
265 fd
= connect2Server( url
->hostname
, url
->port
,1 );
266 url_free( server_url
);
269 if( server_url
->port
==0 ) server_url
->port
= 80; // Default port for the web server
270 fd
= connect2Server( server_url
->hostname
, server_url
->port
,1 );
275 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request: [%s]\n", http_hdr
->buffer
);
277 ret
= send( fd
, http_hdr
->buffer
, http_hdr
->buffer_size
, DEFAULT_SEND_FLAGS
);
278 if( ret
!=(int)http_hdr
->buffer_size
) {
279 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while sending HTTP request: Didn't send all the request.\n");
283 http_free( http_hdr
);
287 if (fd
> 0) closesocket(fd
);
289 if (proxy
&& server_url
)
290 url_free(server_url
);
295 http_read_response( int fd
) {
296 HTTP_header_t
*http_hdr
;
297 char response
[BUFFER_SIZE
];
300 http_hdr
= http_new_header();
301 if( http_hdr
==NULL
) {
306 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
308 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Read failed.\n");
309 http_free( http_hdr
);
313 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"http_read_response read 0 (i.e. EOF).\n");
314 http_free( http_hdr
);
317 http_response_append( http_hdr
, response
, i
);
318 } while( !http_is_header_entire( http_hdr
) );
319 if (http_response_parse( http_hdr
) < 0) {
320 http_free( http_hdr
);
327 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
330 #define MPDEMUX_NW_AuthFailed _(\
331 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
332 "username/password for a list of URLs, or form an URL like:\n"\
333 "http://username:password@hostname/file\n")
336 if( *auth_retry
==1 ) {
337 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,MPDEMUX_NW_AuthFailed
);
340 if( *auth_retry
>0 ) {
341 if( url
->username
) {
342 free( url
->username
);
343 url
->username
= NULL
;
345 if( url
->password
) {
346 free( url
->password
);
347 url
->password
= NULL
;
351 aut
= http_get_field(http_hdr
, "WWW-Authenticate");
354 aut_space
= strstr(aut
, "realm=");
355 if( aut_space
!=NULL
) aut_space
+= 6;
356 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"Authentication required for %s\n", aut_space
);
358 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"Authentication required.\n");
360 if( network_username
) {
361 url
->username
= strdup(network_username
);
362 if( url
->username
==NULL
) {
363 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
367 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,MPDEMUX_NW_AuthFailed
);
370 if( network_password
) {
371 url
->password
= strdup(network_password
);
372 if( url
->password
==NULL
) {
373 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
377 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"No password provided, trying blank password.\n");
384 http_seek( stream_t
*stream
, off_t pos
) {
385 HTTP_header_t
*http_hdr
= NULL
;
387 if( stream
==NULL
) return 0;
389 if( stream
->fd
>0 ) closesocket(stream
->fd
); // need to reconnect to seek in http-stream
390 fd
= http_send_request( stream
->streaming_ctrl
->url
, pos
);
393 http_hdr
= http_read_response( fd
);
395 if( http_hdr
==NULL
) return 0;
397 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) )
398 http_debug_hdr( http_hdr
);
400 switch( http_hdr
->status_code
) {
403 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
404 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
405 if( http_hdr
->body_size
>0 ) {
406 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
407 http_free( http_hdr
);
413 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Server returns %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
420 http_free( http_hdr
);
421 stream
->streaming_ctrl
->data
= NULL
;
431 streaming_bufferize( streaming_ctrl_t
*streaming_ctrl
, char *buffer
, int size
) {
432 //printf("streaming_bufferize\n");
433 streaming_ctrl
->buffer
= malloc(size
);
434 if( streaming_ctrl
->buffer
==NULL
) {
435 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
438 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
439 streaming_ctrl
->buffer_size
= size
;
444 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
446 //printf("nop_streaming_read\n");
447 if( stream_ctrl
->buffer_size
!=0 ) {
448 int buffer_len
= stream_ctrl
->buffer_size
-stream_ctrl
->buffer_pos
;
449 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
450 len
= (size
<buffer_len
)?size
:buffer_len
;
451 memcpy( buffer
, (stream_ctrl
->buffer
)+(stream_ctrl
->buffer_pos
), len
);
452 stream_ctrl
->buffer_pos
+= len
;
453 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
454 if( stream_ctrl
->buffer_pos
>=stream_ctrl
->buffer_size
) {
455 free( stream_ctrl
->buffer
);
456 stream_ctrl
->buffer
= NULL
;
457 stream_ctrl
->buffer_size
= 0;
458 stream_ctrl
->buffer_pos
= 0;
459 //printf("buffer cleaned\n");
461 //printf("read %d bytes from buffer\n", len );
466 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
468 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
471 //printf("read %d bytes from network\n", len );
478 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {
480 // To shut up gcc warning
487 void fixup_network_stream_cache(stream_t
*stream
) {
488 if(stream
->streaming_ctrl
->buffering
) {
489 if(stream_cache_size
<0) {
490 // cache option not set, will use our computed value.
491 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
492 stream_cache_size
= (stream
->streaming_ctrl
->prebuffer_size
/1024)*5;
493 if( stream_cache_size
<64 ) stream_cache_size
= 64; // 16KBytes min buffer
495 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"Cache size set to %d KBytes\n", stream_cache_size
);