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
;
65 char *network_referrer
=NULL
;
68 int network_ipv4_only_proxy
= 0;
71 const mime_struct_t mime_type_table
[] = {
72 #ifdef CONFIG_LIBAVFORMAT
74 { "video/x-flv", DEMUXER_TYPE_LAVF_PREFERRED
},
75 // do not force any demuxer in this case!
76 // we want the lavf demuxer to be tried first (happens automatically anyway),
77 // but for mov reference files to work we must also try
78 // the native demuxer if lavf fails.
79 { "video/quicktime", 0 },
81 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
82 { "audio/mpeg", DEMUXER_TYPE_AUDIO
},
84 { "video/mpeg", DEMUXER_TYPE_UNKNOWN
},
85 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN
},
86 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN
},
87 // AVI ??? => video/x-msvideo
88 { "video/x-msvideo", DEMUXER_TYPE_AVI
},
89 // MOV => video/quicktime
90 { "video/quicktime", DEMUXER_TYPE_MOV
},
92 { "audio/x-ms-wax", DEMUXER_TYPE_ASF
},
93 { "audio/x-ms-wma", DEMUXER_TYPE_ASF
},
94 { "video/x-ms-asf", DEMUXER_TYPE_ASF
},
95 { "video/x-ms-afs", DEMUXER_TYPE_ASF
},
96 { "video/x-ms-wmv", DEMUXER_TYPE_ASF
},
97 { "video/x-ms-wma", DEMUXER_TYPE_ASF
},
98 { "application/x-mms-framed", DEMUXER_TYPE_ASF
},
99 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF
},
100 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN
},
102 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST
},
103 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST
},
104 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST
},
105 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST
},
106 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST
},
108 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
110 { "application/x-ogg", DEMUXER_TYPE_OGG
},
111 // NullSoft Streaming Video
112 { "video/nsv", DEMUXER_TYPE_NSV
},
113 { "misc/ultravox", DEMUXER_TYPE_NSV
},
114 { NULL
, DEMUXER_TYPE_UNKNOWN
},
119 streaming_ctrl_new(void) {
120 streaming_ctrl_t
*streaming_ctrl
;
121 streaming_ctrl
= malloc(sizeof(streaming_ctrl_t
));
122 if( streaming_ctrl
==NULL
) {
123 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
126 memset( streaming_ctrl
, 0, sizeof(streaming_ctrl_t
) );
127 return streaming_ctrl
;
131 streaming_ctrl_free( streaming_ctrl_t
*streaming_ctrl
) {
132 if( streaming_ctrl
==NULL
) return;
133 if( streaming_ctrl
->url
) url_free( streaming_ctrl
->url
);
134 if( streaming_ctrl
->buffer
) free( streaming_ctrl
->buffer
);
135 if( streaming_ctrl
->data
) free( streaming_ctrl
->data
);
136 free( streaming_ctrl
);
140 check4proxies( URL_t
*url
) {
141 URL_t
*url_out
= NULL
;
142 if( url
==NULL
) return NULL
;
143 url_out
= url_new( url
->url
);
144 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
145 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: http://%s:%d\n", url
->hostname
, url
->port
);
148 // Check if the http_proxy environment variable is set.
149 if( !strcasecmp(url
->protocol
, "http") ) {
151 proxy
= getenv("http_proxy");
153 // We got a proxy, build the URL to use it
157 URL_t
*proxy_url
= url_new( proxy
);
159 if( proxy_url
==NULL
) {
160 mp_msg(MSGT_NETWORK
,MSGL_WARN
,
161 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout
);
166 if (network_ipv4_only_proxy
&& (gethostbyname(url
->hostname
)==NULL
)) {
167 mp_msg(MSGT_NETWORK
,MSGL_WARN
,
168 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy
);
174 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: %s\n", proxy_url
->url
);
175 len
= strlen( proxy_url
->hostname
) + strlen( url
->url
) + 20; // 20 = http_proxy:// + port
176 new_url
= malloc( len
+1 );
177 if( new_url
==NULL
) {
178 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
182 sprintf(new_url
, "http_proxy://%s:%d/%s", proxy_url
->hostname
, proxy_url
->port
, url
->url
);
183 tmp_url
= url_new( new_url
);
184 if( tmp_url
==NULL
) {
186 url_free( proxy_url
);
192 url_free( proxy_url
);
199 http_send_request( URL_t
*url
, off_t pos
) {
200 HTTP_header_t
*http_hdr
;
205 int proxy
= 0; // Boolean
207 http_hdr
= http_new_header();
209 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
211 server_url
= url_new( (url
->file
)+1 );
212 http_set_uri( http_hdr
, server_url
->url
);
215 http_set_uri( http_hdr
, server_url
->file
);
217 if (server_url
->port
&& server_url
->port
!= 80)
218 snprintf(str
, 256, "Host: %s:%d", server_url
->hostname
, server_url
->port
);
220 snprintf(str
, 256, "Host: %s", server_url
->hostname
);
221 http_set_field( http_hdr
, str
);
222 if (network_useragent
)
224 snprintf(str
, 256, "User-Agent: %s", network_useragent
);
225 http_set_field(http_hdr
, str
);
228 http_set_field( http_hdr
, "User-Agent: MPlayer/"VERSION
);
230 if (network_referrer
) {
231 char *referrer
= NULL
;
232 size_t len
= strlen(network_referrer
) + 10;
234 // Check len to ensure we don't do something really bad in case of an overflow
236 referrer
= malloc(len
);
238 if (referrer
== NULL
) {
239 mp_msg(MSGT_NETWORK
, MSGL_FATAL
, MSGTR_MemAllocFailed
);
241 snprintf(referrer
, len
, "Referer: %s", network_referrer
);
242 http_set_field(http_hdr
, referrer
);
247 if( strcasecmp(url
->protocol
, "noicyx") )
248 http_set_field(http_hdr
, "Icy-MetaData: 1");
251 // Extend http_send_request with possibility to do partial content retrieval
252 snprintf(str
, 256, "Range: bytes=%"PRId64
"-", (int64_t)pos
);
253 http_set_field(http_hdr
, str
);
256 if (network_cookies_enabled
) cookies_set( http_hdr
, server_url
->hostname
, server_url
->url
);
258 http_set_field( http_hdr
, "Connection: close");
259 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
260 if( http_build_request( http_hdr
)==NULL
) {
265 if( url
->port
==0 ) url
->port
= 8080; // Default port for the proxy server
266 fd
= connect2Server( url
->hostname
, url
->port
,1 );
267 url_free( server_url
);
270 if( server_url
->port
==0 ) server_url
->port
= 80; // Default port for the web server
271 fd
= connect2Server( server_url
->hostname
, server_url
->port
,1 );
276 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request: [%s]\n", http_hdr
->buffer
);
278 ret
= send( fd
, http_hdr
->buffer
, http_hdr
->buffer_size
, 0 );
279 if( ret
!=(int)http_hdr
->buffer_size
) {
280 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest
);
284 http_free( http_hdr
);
288 if (fd
> 0) closesocket(fd
);
290 if (proxy
&& server_url
)
291 url_free(server_url
);
296 http_read_response( int fd
) {
297 HTTP_header_t
*http_hdr
;
298 char response
[BUFFER_SIZE
];
301 http_hdr
= http_new_header();
302 if( http_hdr
==NULL
) {
307 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
309 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ReadFailed
);
310 http_free( http_hdr
);
314 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_Read0CouldBeEOF
);
315 http_free( http_hdr
);
318 http_response_append( http_hdr
, response
, i
);
319 } while( !http_is_header_entire( http_hdr
) );
320 if (http_response_parse( http_hdr
) < 0) {
321 http_free( http_hdr
);
328 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
331 if( *auth_retry
==1 ) {
332 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
335 if( *auth_retry
>0 ) {
336 if( url
->username
) {
337 free( url
->username
);
338 url
->username
= NULL
;
340 if( url
->password
) {
341 free( url
->password
);
342 url
->password
= NULL
;
346 aut
= http_get_field(http_hdr
, "WWW-Authenticate");
349 aut_space
= strstr(aut
, "realm=");
350 if( aut_space
!=NULL
) aut_space
+= 6;
351 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_AuthRequiredFor
, aut_space
);
353 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_AuthRequired
);
355 if( network_username
) {
356 url
->username
= strdup(network_username
);
357 if( url
->username
==NULL
) {
358 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
362 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
365 if( network_password
) {
366 url
->password
= strdup(network_password
);
367 if( url
->password
==NULL
) {
368 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
372 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank
);
379 http_seek( stream_t
*stream
, off_t pos
) {
380 HTTP_header_t
*http_hdr
= NULL
;
382 if( stream
==NULL
) return 0;
384 if( stream
->fd
>0 ) closesocket(stream
->fd
); // need to reconnect to seek in http-stream
385 fd
= http_send_request( stream
->streaming_ctrl
->url
, pos
);
388 http_hdr
= http_read_response( fd
);
390 if( http_hdr
==NULL
) return 0;
392 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) )
393 http_debug_hdr( http_hdr
);
395 switch( http_hdr
->status_code
) {
398 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
399 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
400 if( http_hdr
->body_size
>0 ) {
401 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
402 http_free( http_hdr
);
408 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ErrServerReturned
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
415 http_free( http_hdr
);
416 stream
->streaming_ctrl
->data
= NULL
;
426 streaming_bufferize( streaming_ctrl_t
*streaming_ctrl
, char *buffer
, int size
) {
427 //printf("streaming_bufferize\n");
428 streaming_ctrl
->buffer
= malloc(size
);
429 if( streaming_ctrl
->buffer
==NULL
) {
430 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
433 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
434 streaming_ctrl
->buffer_size
= size
;
439 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
441 //printf("nop_streaming_read\n");
442 if( stream_ctrl
->buffer_size
!=0 ) {
443 int buffer_len
= stream_ctrl
->buffer_size
-stream_ctrl
->buffer_pos
;
444 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
445 len
= (size
<buffer_len
)?size
:buffer_len
;
446 memcpy( buffer
, (stream_ctrl
->buffer
)+(stream_ctrl
->buffer_pos
), len
);
447 stream_ctrl
->buffer_pos
+= len
;
448 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
449 if( stream_ctrl
->buffer_pos
>=stream_ctrl
->buffer_size
) {
450 free( stream_ctrl
->buffer
);
451 stream_ctrl
->buffer
= NULL
;
452 stream_ctrl
->buffer_size
= 0;
453 stream_ctrl
->buffer_pos
= 0;
454 //printf("buffer cleaned\n");
456 //printf("read %d bytes from buffer\n", len );
461 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
463 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
466 //printf("read %d bytes from network\n", len );
473 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {
475 // To shut up gcc warning
482 void fixup_network_stream_cache(stream_t
*stream
) {
483 if(stream
->streaming_ctrl
->buffering
) {
484 if(stream_cache_size
<0) {
485 // cache option not set, will use our computed value.
486 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
487 stream_cache_size
= (stream
->streaming_ctrl
->prebuffer_size
/1024)*5;
488 if( stream_cache_size
<64 ) stream_cache_size
= 64; // 16KBytes min buffer
490 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_CacheSizeSetTo
, stream_cache_size
);