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.
44 #include "libmpdemux/demuxer.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
;
67 int network_ipv4_only_proxy
= 0;
70 const mime_struct_t mime_type_table
[] = {
71 #ifdef CONFIG_LIBAVFORMAT
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 },
80 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
81 { "audio/mpeg", DEMUXER_TYPE_AUDIO
},
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
},
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
},
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
},
107 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
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
},
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
);
125 memset( streaming_ctrl
, 0, sizeof(streaming_ctrl_t
) );
126 return streaming_ctrl
;
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
);
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
);
147 // Check if the http_proxy environment variable is set.
148 if( !strcasecmp(url
->protocol
, "http") ) {
150 proxy
= getenv("http_proxy");
152 // We got a proxy, build the URL to use it
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
);
165 if (network_ipv4_only_proxy
&& (gethostbyname(url
->hostname
)==NULL
)) {
166 mp_msg(MSGT_NETWORK
,MSGL_WARN
,
167 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy
);
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
);
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
) {
185 url_free( proxy_url
);
191 url_free( proxy_url
);
198 http_send_request( URL_t
*url
, off_t pos
) {
199 HTTP_header_t
*http_hdr
;
204 int proxy
= 0; // Boolean
206 http_hdr
= http_new_header();
208 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
210 server_url
= url_new( (url
->file
)+1 );
211 http_set_uri( http_hdr
, 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
);
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
);
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");
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
) {
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
);
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 );
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
);
266 http_free( http_hdr
);
270 if (fd
> 0) closesocket(fd
);
272 if (proxy
&& server_url
)
273 url_free(server_url
);
278 http_read_response( int fd
) {
279 HTTP_header_t
*http_hdr
;
280 char response
[BUFFER_SIZE
];
283 http_hdr
= http_new_header();
284 if( http_hdr
==NULL
) {
289 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
291 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ReadFailed
);
292 http_free( http_hdr
);
296 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_Read0CouldBeEOF
);
297 http_free( http_hdr
);
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
);
310 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
313 if( *auth_retry
==1 ) {
314 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
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");
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
);
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
);
344 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
347 if( network_password
) {
348 url
->password
= strdup(network_password
);
349 if( url
->password
==NULL
) {
350 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
354 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank
);
361 http_seek( stream_t
*stream
, off_t pos
) {
362 HTTP_header_t
*http_hdr
= NULL
;
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
);
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
) {
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
);
390 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ErrServerReturned
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
397 http_free( http_hdr
);
398 stream
->streaming_ctrl
->data
= NULL
;
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
);
415 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
416 streaming_ctrl
->buffer_size
= size
;
421 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
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 );
443 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
445 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
448 //printf("read %d bytes from network\n", len );
455 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {
457 // To shut up gcc warning
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
);