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 { "video/quicktime", DEMUXER_TYPE_LAVF_PREFERRED
},
76 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
77 { "audio/mpeg", DEMUXER_TYPE_AUDIO
},
79 { "video/mpeg", DEMUXER_TYPE_UNKNOWN
},
80 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN
},
81 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN
},
82 // AVI ??? => video/x-msvideo
83 { "video/x-msvideo", DEMUXER_TYPE_AVI
},
84 // MOV => video/quicktime
85 { "video/quicktime", DEMUXER_TYPE_MOV
},
87 { "audio/x-ms-wax", DEMUXER_TYPE_ASF
},
88 { "audio/x-ms-wma", DEMUXER_TYPE_ASF
},
89 { "video/x-ms-asf", DEMUXER_TYPE_ASF
},
90 { "video/x-ms-afs", DEMUXER_TYPE_ASF
},
91 { "video/x-ms-wmv", DEMUXER_TYPE_ASF
},
92 { "video/x-ms-wma", DEMUXER_TYPE_ASF
},
93 { "application/x-mms-framed", DEMUXER_TYPE_ASF
},
94 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF
},
95 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN
},
97 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST
},
98 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST
},
99 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST
},
100 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST
},
101 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST
},
103 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
105 { "application/x-ogg", DEMUXER_TYPE_OGG
},
106 // NullSoft Streaming Video
107 { "video/nsv", DEMUXER_TYPE_NSV
},
108 { "misc/ultravox", DEMUXER_TYPE_NSV
},
109 { NULL
, DEMUXER_TYPE_UNKNOWN
},
114 streaming_ctrl_new(void) {
115 streaming_ctrl_t
*streaming_ctrl
;
116 streaming_ctrl
= malloc(sizeof(streaming_ctrl_t
));
117 if( streaming_ctrl
==NULL
) {
118 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
121 memset( streaming_ctrl
, 0, sizeof(streaming_ctrl_t
) );
122 return streaming_ctrl
;
126 streaming_ctrl_free( streaming_ctrl_t
*streaming_ctrl
) {
127 if( streaming_ctrl
==NULL
) return;
128 if( streaming_ctrl
->url
) url_free( streaming_ctrl
->url
);
129 if( streaming_ctrl
->buffer
) free( streaming_ctrl
->buffer
);
130 if( streaming_ctrl
->data
) free( streaming_ctrl
->data
);
131 free( streaming_ctrl
);
135 check4proxies( URL_t
*url
) {
136 URL_t
*url_out
= NULL
;
137 if( url
==NULL
) return NULL
;
138 url_out
= url_new( url
->url
);
139 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
140 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: http://%s:%d\n", url
->hostname
, url
->port
);
143 // Check if the http_proxy environment variable is set.
144 if( !strcasecmp(url
->protocol
, "http") ) {
146 proxy
= getenv("http_proxy");
148 // We got a proxy, build the URL to use it
152 URL_t
*proxy_url
= url_new( proxy
);
154 if( proxy_url
==NULL
) {
155 mp_msg(MSGT_NETWORK
,MSGL_WARN
,
156 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout
);
161 if (network_ipv4_only_proxy
&& (gethostbyname(url
->hostname
)==NULL
)) {
162 mp_msg(MSGT_NETWORK
,MSGL_WARN
,
163 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy
);
169 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: %s\n", proxy_url
->url
);
170 len
= strlen( proxy_url
->hostname
) + strlen( url
->url
) + 20; // 20 = http_proxy:// + port
171 new_url
= malloc( len
+1 );
172 if( new_url
==NULL
) {
173 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
177 sprintf(new_url
, "http_proxy://%s:%d/%s", proxy_url
->hostname
, proxy_url
->port
, url
->url
);
178 tmp_url
= url_new( new_url
);
179 if( tmp_url
==NULL
) {
181 url_free( proxy_url
);
187 url_free( proxy_url
);
194 http_send_request( URL_t
*url
, off_t pos
) {
195 HTTP_header_t
*http_hdr
;
200 int proxy
= 0; // Boolean
202 http_hdr
= http_new_header();
204 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
206 server_url
= url_new( (url
->file
)+1 );
207 http_set_uri( http_hdr
, server_url
->url
);
210 http_set_uri( http_hdr
, server_url
->file
);
212 if (server_url
->port
&& server_url
->port
!= 80)
213 snprintf(str
, 256, "Host: %s:%d", server_url
->hostname
, server_url
->port
);
215 snprintf(str
, 256, "Host: %s", server_url
->hostname
);
216 http_set_field( http_hdr
, str
);
217 if (network_useragent
)
219 snprintf(str
, 256, "User-Agent: %s", network_useragent
);
220 http_set_field(http_hdr
, str
);
223 http_set_field( http_hdr
, "User-Agent: MPlayer/"VERSION
);
225 if( strcasecmp(url
->protocol
, "noicyx") )
226 http_set_field(http_hdr
, "Icy-MetaData: 1");
229 // Extend http_send_request with possibility to do partial content retrieval
230 snprintf(str
, 256, "Range: bytes=%"PRId64
"-", (int64_t)pos
);
231 http_set_field(http_hdr
, str
);
234 if (network_cookies_enabled
) cookies_set( http_hdr
, server_url
->hostname
, server_url
->url
);
236 http_set_field( http_hdr
, "Connection: close");
237 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
238 if( http_build_request( http_hdr
)==NULL
) {
243 if( url
->port
==0 ) url
->port
= 8080; // Default port for the proxy server
244 fd
= connect2Server( url
->hostname
, url
->port
,1 );
245 url_free( server_url
);
248 if( server_url
->port
==0 ) server_url
->port
= 80; // Default port for the web server
249 fd
= connect2Server( server_url
->hostname
, server_url
->port
,1 );
254 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request: [%s]\n", http_hdr
->buffer
);
256 ret
= send( fd
, http_hdr
->buffer
, http_hdr
->buffer_size
, 0 );
257 if( ret
!=(int)http_hdr
->buffer_size
) {
258 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest
);
262 http_free( http_hdr
);
266 if (fd
> 0) closesocket(fd
);
268 if (proxy
&& server_url
)
269 url_free(server_url
);
274 http_read_response( int fd
) {
275 HTTP_header_t
*http_hdr
;
276 char response
[BUFFER_SIZE
];
279 http_hdr
= http_new_header();
280 if( http_hdr
==NULL
) {
285 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
287 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ReadFailed
);
288 http_free( http_hdr
);
292 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_Read0CouldBeEOF
);
293 http_free( http_hdr
);
296 http_response_append( http_hdr
, response
, i
);
297 } while( !http_is_header_entire( http_hdr
) );
298 http_response_parse( http_hdr
);
303 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
306 if( *auth_retry
==1 ) {
307 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
310 if( *auth_retry
>0 ) {
311 if( url
->username
) {
312 free( url
->username
);
313 url
->username
= NULL
;
315 if( url
->password
) {
316 free( url
->password
);
317 url
->password
= NULL
;
321 aut
= http_get_field(http_hdr
, "WWW-Authenticate");
324 aut_space
= strstr(aut
, "realm=");
325 if( aut_space
!=NULL
) aut_space
+= 6;
326 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_AuthRequiredFor
, aut_space
);
328 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_AuthRequired
);
330 if( network_username
) {
331 url
->username
= strdup(network_username
);
332 if( url
->username
==NULL
) {
333 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
337 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
340 if( network_password
) {
341 url
->password
= strdup(network_password
);
342 if( url
->password
==NULL
) {
343 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
347 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank
);
354 http_seek( stream_t
*stream
, off_t pos
) {
355 HTTP_header_t
*http_hdr
= NULL
;
357 if( stream
==NULL
) return 0;
359 if( stream
->fd
>0 ) closesocket(stream
->fd
); // need to reconnect to seek in http-stream
360 fd
= http_send_request( stream
->streaming_ctrl
->url
, pos
);
363 http_hdr
= http_read_response( fd
);
365 if( http_hdr
==NULL
) return 0;
367 switch( http_hdr
->status_code
) {
370 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
371 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
372 if( http_hdr
->body_size
>0 ) {
373 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
374 http_free( http_hdr
);
380 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ErrServerReturned
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
387 http_free( http_hdr
);
388 stream
->streaming_ctrl
->data
= NULL
;
398 streaming_bufferize( streaming_ctrl_t
*streaming_ctrl
, char *buffer
, int size
) {
399 //printf("streaming_bufferize\n");
400 streaming_ctrl
->buffer
= malloc(size
);
401 if( streaming_ctrl
->buffer
==NULL
) {
402 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
405 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
406 streaming_ctrl
->buffer_size
= size
;
411 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
413 //printf("nop_streaming_read\n");
414 if( stream_ctrl
->buffer_size
!=0 ) {
415 int buffer_len
= stream_ctrl
->buffer_size
-stream_ctrl
->buffer_pos
;
416 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
417 len
= (size
<buffer_len
)?size
:buffer_len
;
418 memcpy( buffer
, (stream_ctrl
->buffer
)+(stream_ctrl
->buffer_pos
), len
);
419 stream_ctrl
->buffer_pos
+= len
;
420 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
421 if( stream_ctrl
->buffer_pos
>=stream_ctrl
->buffer_size
) {
422 free( stream_ctrl
->buffer
);
423 stream_ctrl
->buffer
= NULL
;
424 stream_ctrl
->buffer_size
= 0;
425 stream_ctrl
->buffer_pos
= 0;
426 //printf("buffer cleaned\n");
428 //printf("read %d bytes from buffer\n", len );
433 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
435 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
438 //printf("read %d bytes from network\n", len );
445 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {
447 // To shut up gcc warning
454 void fixup_network_stream_cache(stream_t
*stream
) {
455 if(stream
->streaming_ctrl
->buffering
) {
456 if(stream_cache_size
<0) {
457 // cache option not set, will use our computed value.
458 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
459 stream_cache_size
= (stream
->streaming_ctrl
->prebuffer_size
/1024)*5;
460 if( stream_cache_size
<64 ) stream_cache_size
= 64; // 16KBytes min buffer
462 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_CacheSizeSetTo
, stream_cache_size
);
468 streaming_stop( stream_t
*stream
) {
469 stream
->streaming_ctrl
->status
= streaming_stopped_e
;