2 * Network layer for MPlayer
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
28 #include "libmpdemux/demuxer.h"
39 extern int stream_cache_size
;
41 /* Variables for the command line option -user, -passwd, -bandwidth,
42 -user-agent and -nocookies */
44 char *network_username
=NULL
;
45 char *network_password
=NULL
;
46 int network_bandwidth
=0;
47 int network_cookies_enabled
= 0;
48 char *network_useragent
=NULL
;
51 int network_ipv4_only_proxy
= 0;
54 const mime_struct_t mime_type_table
[] = {
55 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
56 { "audio/mpeg", DEMUXER_TYPE_AUDIO
},
58 { "video/mpeg", DEMUXER_TYPE_UNKNOWN
},
59 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN
},
60 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN
},
61 // AVI ??? => video/x-msvideo
62 { "video/x-msvideo", DEMUXER_TYPE_AVI
},
63 // MOV => video/quicktime
64 { "video/quicktime", DEMUXER_TYPE_MOV
},
66 { "audio/x-ms-wax", DEMUXER_TYPE_ASF
},
67 { "audio/x-ms-wma", DEMUXER_TYPE_ASF
},
68 { "video/x-ms-asf", DEMUXER_TYPE_ASF
},
69 { "video/x-ms-afs", DEMUXER_TYPE_ASF
},
70 { "video/x-ms-wmv", DEMUXER_TYPE_ASF
},
71 { "video/x-ms-wma", DEMUXER_TYPE_ASF
},
72 { "application/x-mms-framed", DEMUXER_TYPE_ASF
},
73 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF
},
74 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN
},
76 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST
},
77 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST
},
78 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST
},
79 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST
},
80 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST
},
82 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
84 { "application/x-ogg", DEMUXER_TYPE_OGG
},
85 // NullSoft Streaming Video
86 { "video/nsv", DEMUXER_TYPE_NSV
},
87 { "misc/ultravox", DEMUXER_TYPE_NSV
},
88 #ifdef CONFIG_LIBAVFORMAT
90 { "video/x-flv", DEMUXER_TYPE_LAVF
},
92 { NULL
, DEMUXER_TYPE_UNKNOWN
},
97 streaming_ctrl_new(void) {
98 streaming_ctrl_t
*streaming_ctrl
;
99 streaming_ctrl
= malloc(sizeof(streaming_ctrl_t
));
100 if( streaming_ctrl
==NULL
) {
101 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
104 memset( streaming_ctrl
, 0, sizeof(streaming_ctrl_t
) );
105 return streaming_ctrl
;
109 streaming_ctrl_free( streaming_ctrl_t
*streaming_ctrl
) {
110 if( streaming_ctrl
==NULL
) return;
111 if( streaming_ctrl
->url
) url_free( streaming_ctrl
->url
);
112 if( streaming_ctrl
->buffer
) free( streaming_ctrl
->buffer
);
113 if( streaming_ctrl
->data
) free( streaming_ctrl
->data
);
114 free( streaming_ctrl
);
118 check4proxies( URL_t
*url
) {
119 URL_t
*url_out
= NULL
;
120 if( url
==NULL
) return NULL
;
121 url_out
= url_new( url
->url
);
122 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
123 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: http://%s:%d\n", url
->hostname
, url
->port
);
126 // Check if the http_proxy environment variable is set.
127 if( !strcasecmp(url
->protocol
, "http") ) {
129 proxy
= getenv("http_proxy");
131 // We got a proxy, build the URL to use it
135 URL_t
*proxy_url
= url_new( proxy
);
137 if( proxy_url
==NULL
) {
138 mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,
139 "Invalid proxy setting... Trying without proxy.\n");
144 if (network_ipv4_only_proxy
&& (gethostbyname(url
->hostname
)==NULL
)) {
145 mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,
146 "Could not resolve remote hostname for AF_INET. Trying without proxy.\n");
152 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: %s\n", proxy_url
->url
);
153 len
= strlen( proxy_url
->hostname
) + strlen( url
->url
) + 20; // 20 = http_proxy:// + port
154 new_url
= malloc( len
+1 );
155 if( new_url
==NULL
) {
156 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
160 sprintf(new_url
, "http_proxy://%s:%d/%s", proxy_url
->hostname
, proxy_url
->port
, url
->url
);
161 tmp_url
= url_new( new_url
);
162 if( tmp_url
==NULL
) {
164 url_free( proxy_url
);
170 url_free( proxy_url
);
177 http_send_request( URL_t
*url
, off_t pos
) {
178 HTTP_header_t
*http_hdr
;
183 int proxy
= 0; // Boolean
185 http_hdr
= http_new_header();
187 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
189 server_url
= url_new( (url
->file
)+1 );
190 http_set_uri( http_hdr
, server_url
->url
);
193 http_set_uri( http_hdr
, server_url
->file
);
195 if (server_url
->port
&& server_url
->port
!= 80)
196 snprintf(str
, 256, "Host: %s:%d", server_url
->hostname
, server_url
->port
);
198 snprintf(str
, 256, "Host: %s", server_url
->hostname
);
199 http_set_field( http_hdr
, str
);
200 if (network_useragent
)
202 snprintf(str
, 256, "User-Agent: %s", network_useragent
);
203 http_set_field(http_hdr
, str
);
206 http_set_field( http_hdr
, "User-Agent: MPlayer/"VERSION
);
208 if( strcasecmp(url
->protocol
, "noicyx") )
209 http_set_field(http_hdr
, "Icy-MetaData: 1");
212 // Extend http_send_request with possibility to do partial content retrieval
213 snprintf(str
, 256, "Range: bytes=%"PRId64
"-", (int64_t)pos
);
214 http_set_field(http_hdr
, str
);
217 if (network_cookies_enabled
) cookies_set( http_hdr
, server_url
->hostname
, server_url
->url
);
219 http_set_field( http_hdr
, "Connection: close");
220 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
221 if( http_build_request( http_hdr
)==NULL
) {
226 if( url
->port
==0 ) url
->port
= 8080; // Default port for the proxy server
227 fd
= connect2Server( url
->hostname
, url
->port
,1 );
228 url_free( server_url
);
231 if( server_url
->port
==0 ) server_url
->port
= 80; // Default port for the web server
232 fd
= connect2Server( server_url
->hostname
, server_url
->port
,1 );
237 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request: [%s]\n", http_hdr
->buffer
);
239 ret
= send( fd
, http_hdr
->buffer
, http_hdr
->buffer_size
, 0 );
240 if( ret
!=(int)http_hdr
->buffer_size
) {
241 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while sending HTTP request: Didn't send all the request.\n");
245 http_free( http_hdr
);
249 if (fd
> 0) closesocket(fd
);
251 if (proxy
&& server_url
)
252 url_free(server_url
);
257 http_read_response( int fd
) {
258 HTTP_header_t
*http_hdr
;
259 char response
[BUFFER_SIZE
];
262 http_hdr
= http_new_header();
263 if( http_hdr
==NULL
) {
268 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
270 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Read failed.\n");
271 http_free( http_hdr
);
275 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"http_read_response read 0 (i.e. EOF).\n");
276 http_free( http_hdr
);
279 http_response_append( http_hdr
, response
, i
);
280 } while( !http_is_header_entire( http_hdr
) );
281 http_response_parse( http_hdr
);
286 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
289 #define MPDEMUX_NW_AuthFailed _(\
290 "Authentication failed. Please use the -user and -passwd options to provide your\n"\
291 "username/password for a list of URLs, or form an URL like:\n"\
292 "http://username:password@hostname/file\n")
295 if( *auth_retry
==1 ) {
296 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,MPDEMUX_NW_AuthFailed
);
299 if( *auth_retry
>0 ) {
300 if( url
->username
) {
301 free( url
->username
);
302 url
->username
= NULL
;
304 if( url
->password
) {
305 free( url
->password
);
306 url
->password
= NULL
;
310 aut
= http_get_field(http_hdr
, "WWW-Authenticate");
313 aut_space
= strstr(aut
, "realm=");
314 if( aut_space
!=NULL
) aut_space
+= 6;
315 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"Authentication required for %s\n", aut_space
);
317 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"Authentication required.\n");
319 if( network_username
) {
320 url
->username
= strdup(network_username
);
321 if( url
->username
==NULL
) {
322 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
326 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,MPDEMUX_NW_AuthFailed
);
329 if( network_password
) {
330 url
->password
= strdup(network_password
);
331 if( url
->password
==NULL
) {
332 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
336 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"No password provided, trying blank password.\n");
343 http_seek( stream_t
*stream
, off_t pos
) {
344 HTTP_header_t
*http_hdr
= NULL
;
346 if( stream
==NULL
) return 0;
348 if( stream
->fd
>0 ) closesocket(stream
->fd
); // need to reconnect to seek in http-stream
349 fd
= http_send_request( stream
->streaming_ctrl
->url
, pos
);
352 http_hdr
= http_read_response( fd
);
354 if( http_hdr
==NULL
) return 0;
356 switch( http_hdr
->status_code
) {
359 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
360 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
361 if( http_hdr
->body_size
>0 ) {
362 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
363 http_free( http_hdr
);
369 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Server returns %d: %s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
376 http_free( http_hdr
);
377 stream
->streaming_ctrl
->data
= NULL
;
387 streaming_bufferize( streaming_ctrl_t
*streaming_ctrl
, char *buffer
, int size
) {
388 //printf("streaming_bufferize\n");
389 streaming_ctrl
->buffer
= malloc(size
);
390 if( streaming_ctrl
->buffer
==NULL
) {
391 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
394 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
395 streaming_ctrl
->buffer_size
= size
;
400 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
402 //printf("nop_streaming_read\n");
403 if( stream_ctrl
->buffer_size
!=0 ) {
404 int buffer_len
= stream_ctrl
->buffer_size
-stream_ctrl
->buffer_pos
;
405 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
406 len
= (size
<buffer_len
)?size
:buffer_len
;
407 memcpy( buffer
, (stream_ctrl
->buffer
)+(stream_ctrl
->buffer_pos
), len
);
408 stream_ctrl
->buffer_pos
+= len
;
409 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
410 if( stream_ctrl
->buffer_pos
>=stream_ctrl
->buffer_size
) {
411 free( stream_ctrl
->buffer
);
412 stream_ctrl
->buffer
= NULL
;
413 stream_ctrl
->buffer_size
= 0;
414 stream_ctrl
->buffer_pos
= 0;
415 //printf("buffer cleaned\n");
417 //printf("read %d bytes from buffer\n", len );
422 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
424 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
427 //printf("read %d bytes from network\n", len );
434 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {
436 // To shut up gcc warning
443 void fixup_network_stream_cache(stream_t
*stream
) {
444 if(stream
->streaming_ctrl
->buffering
) {
445 if(stream_cache_size
<0) {
446 // cache option not set, will use our computed value.
447 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
448 stream_cache_size
= (stream
->streaming_ctrl
->prebuffer_size
/1024)*5;
449 if( stream_cache_size
<64 ) stream_cache_size
= 64; // 16KBytes min buffer
451 mp_tmsg(MSGT_NETWORK
,MSGL_INFO
,"Cache size set to %d KBytes\n", stream_cache_size
);
457 streaming_stop( stream_t
*stream
) {
458 stream
->streaming_ctrl
->status
= streaming_stopped_e
;