2 * Network layer for MPlayer
3 * by Bertrand BAUDET <bertrand_baudet@yahoo.com>
4 * (C) 2001, MPlayer team.
23 #define closesocket close
30 #include "libmpdemux/demuxer.h"
41 extern int stream_cache_size
;
43 /* Variables for the command line option -user, -passwd, -bandwidth,
44 -user-agent and -nocookies */
46 char *network_username
=NULL
;
47 char *network_password
=NULL
;
48 int network_bandwidth
=0;
49 int network_cookies_enabled
= 0;
50 char *network_useragent
=NULL
;
53 int network_ipv4_only_proxy
= 0;
56 const mime_struct_t mime_type_table
[] = {
57 // MP3 streaming, some MP3 streaming server answer with audio/mpeg
58 { "audio/mpeg", DEMUXER_TYPE_AUDIO
},
60 { "video/mpeg", DEMUXER_TYPE_UNKNOWN
},
61 { "video/x-mpeg", DEMUXER_TYPE_UNKNOWN
},
62 { "video/x-mpeg2", DEMUXER_TYPE_UNKNOWN
},
63 // AVI ??? => video/x-msvideo
64 { "video/x-msvideo", DEMUXER_TYPE_AVI
},
65 // MOV => video/quicktime
66 { "video/quicktime", DEMUXER_TYPE_MOV
},
68 { "audio/x-ms-wax", DEMUXER_TYPE_ASF
},
69 { "audio/x-ms-wma", DEMUXER_TYPE_ASF
},
70 { "video/x-ms-asf", DEMUXER_TYPE_ASF
},
71 { "video/x-ms-afs", DEMUXER_TYPE_ASF
},
72 { "video/x-ms-wmv", DEMUXER_TYPE_ASF
},
73 { "video/x-ms-wma", DEMUXER_TYPE_ASF
},
74 { "application/x-mms-framed", DEMUXER_TYPE_ASF
},
75 { "application/vnd.ms.wms-hdr.asfv1", DEMUXER_TYPE_ASF
},
76 { "application/octet-stream", DEMUXER_TYPE_UNKNOWN
},
78 { "video/x-ms-wmx", DEMUXER_TYPE_PLAYLIST
},
79 { "video/x-ms-wvx", DEMUXER_TYPE_PLAYLIST
},
80 { "audio/x-scpls", DEMUXER_TYPE_PLAYLIST
},
81 { "audio/x-mpegurl", DEMUXER_TYPE_PLAYLIST
},
82 { "audio/x-pls", DEMUXER_TYPE_PLAYLIST
},
84 // { "audio/x-pn-realaudio", DEMUXER_TYPE_REAL },
86 { "application/x-ogg", DEMUXER_TYPE_OGG
},
87 // NullSoft Streaming Video
88 { "video/nsv", DEMUXER_TYPE_NSV
},
89 { "misc/ultravox", DEMUXER_TYPE_NSV
},
90 #ifdef USE_LIBAVFORMAT
92 { "video/x-flv", DEMUXER_TYPE_LAVF
},
94 { NULL
, DEMUXER_TYPE_UNKNOWN
},
99 streaming_ctrl_new(void) {
100 streaming_ctrl_t
*streaming_ctrl
;
101 streaming_ctrl
= malloc(sizeof(streaming_ctrl_t
));
102 if( streaming_ctrl
==NULL
) {
103 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
106 memset( streaming_ctrl
, 0, sizeof(streaming_ctrl_t
) );
107 return streaming_ctrl
;
111 streaming_ctrl_free( streaming_ctrl_t
*streaming_ctrl
) {
112 if( streaming_ctrl
==NULL
) return;
113 if( streaming_ctrl
->url
) url_free( streaming_ctrl
->url
);
114 if( streaming_ctrl
->buffer
) free( streaming_ctrl
->buffer
);
115 if( streaming_ctrl
->data
) free( streaming_ctrl
->data
);
116 free( streaming_ctrl
);
120 check4proxies( URL_t
*url
) {
121 URL_t
*url_out
= NULL
;
122 if( url
==NULL
) return NULL
;
123 url_out
= url_new( url
->url
);
124 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
125 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: http://%s:%d\n", url
->hostname
, url
->port
);
128 // Check if the http_proxy environment variable is set.
129 if( !strcasecmp(url
->protocol
, "http") ) {
131 proxy
= getenv("http_proxy");
133 // We got a proxy, build the URL to use it
137 URL_t
*proxy_url
= url_new( proxy
);
139 if( proxy_url
==NULL
) {
140 mp_msg(MSGT_NETWORK
,MSGL_WARN
,
141 MSGTR_MPDEMUX_NW_InvalidProxySettingTryingWithout
);
146 if (network_ipv4_only_proxy
&& (gethostbyname(url
->hostname
)==NULL
)) {
147 mp_msg(MSGT_NETWORK
,MSGL_WARN
,
148 MSGTR_MPDEMUX_NW_CantResolvTryingWithoutProxy
);
154 mp_msg(MSGT_NETWORK
,MSGL_V
,"Using HTTP proxy: %s\n", proxy_url
->url
);
155 len
= strlen( proxy_url
->hostname
) + strlen( url
->url
) + 20; // 20 = http_proxy:// + port
156 new_url
= malloc( len
+1 );
157 if( new_url
==NULL
) {
158 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
162 sprintf(new_url
, "http_proxy://%s:%d/%s", proxy_url
->hostname
, proxy_url
->port
, url
->url
);
163 tmp_url
= url_new( new_url
);
164 if( tmp_url
==NULL
) {
166 url_free( proxy_url
);
172 url_free( proxy_url
);
179 http_send_request( URL_t
*url
, off_t pos
) {
180 HTTP_header_t
*http_hdr
;
185 int proxy
= 0; // Boolean
187 http_hdr
= http_new_header();
189 if( !strcasecmp(url
->protocol
, "http_proxy") ) {
191 server_url
= url_new( (url
->file
)+1 );
192 http_set_uri( http_hdr
, server_url
->url
);
195 http_set_uri( http_hdr
, server_url
->file
);
197 if (server_url
->port
&& server_url
->port
!= 80)
198 snprintf(str
, 256, "Host: %s:%d", server_url
->hostname
, server_url
->port
);
200 snprintf(str
, 256, "Host: %s", server_url
->hostname
);
201 http_set_field( http_hdr
, str
);
202 if (network_useragent
)
204 snprintf(str
, 256, "User-Agent: %s", network_useragent
);
205 http_set_field(http_hdr
, str
);
208 http_set_field( http_hdr
, "User-Agent: MPlayer/"VERSION
);
210 http_set_field(http_hdr
, "Icy-MetaData: 1");
213 // Extend http_send_request with possibility to do partial content retrieval
214 snprintf(str
, 256, "Range: bytes=%"PRId64
"-", (int64_t)pos
);
215 http_set_field(http_hdr
, str
);
218 if (network_cookies_enabled
) cookies_set( http_hdr
, server_url
->hostname
, server_url
->url
);
220 http_set_field( http_hdr
, "Connection: close");
221 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
222 if( http_build_request( http_hdr
)==NULL
) {
227 if( url
->port
==0 ) url
->port
= 8080; // Default port for the proxy server
228 fd
= connect2Server( url
->hostname
, url
->port
,1 );
229 url_free( server_url
);
232 if( server_url
->port
==0 ) server_url
->port
= 80; // Default port for the web server
233 fd
= connect2Server( server_url
->hostname
, server_url
->port
,1 );
238 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request: [%s]\n", http_hdr
->buffer
);
240 ret
= send( fd
, http_hdr
->buffer
, http_hdr
->buffer_size
, 0 );
241 if( ret
!=(int)http_hdr
->buffer_size
) {
242 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ErrSendingHTTPRequest
);
246 http_free( http_hdr
);
250 if (fd
> 0) closesocket(fd
);
252 if (proxy
&& server_url
)
253 url_free(server_url
);
258 http_read_response( int fd
) {
259 HTTP_header_t
*http_hdr
;
260 char response
[BUFFER_SIZE
];
263 http_hdr
= http_new_header();
264 if( http_hdr
==NULL
) {
269 i
= recv( fd
, response
, BUFFER_SIZE
, 0 );
271 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ReadFailed
);
272 http_free( http_hdr
);
276 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_Read0CouldBeEOF
);
277 http_free( http_hdr
);
280 http_response_append( http_hdr
, response
, i
);
281 } while( !http_is_header_entire( http_hdr
) );
282 http_response_parse( http_hdr
);
287 http_authenticate(HTTP_header_t
*http_hdr
, URL_t
*url
, int *auth_retry
) {
290 if( *auth_retry
==1 ) {
291 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
294 if( *auth_retry
>0 ) {
295 if( url
->username
) {
296 free( url
->username
);
297 url
->username
= NULL
;
299 if( url
->password
) {
300 free( url
->password
);
301 url
->password
= NULL
;
305 aut
= http_get_field(http_hdr
, "WWW-Authenticate");
308 aut_space
= strstr(aut
, "realm=");
309 if( aut_space
!=NULL
) aut_space
+= 6;
310 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_AuthRequiredFor
, aut_space
);
312 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_AuthRequired
);
314 if( network_username
) {
315 url
->username
= strdup(network_username
);
316 if( url
->username
==NULL
) {
317 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
321 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_AuthFailed
);
324 if( network_password
) {
325 url
->password
= strdup(network_password
);
326 if( url
->password
==NULL
) {
327 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
331 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_NoPasswdProvidedTryingBlank
);
338 http_seek( stream_t
*stream
, off_t pos
) {
339 HTTP_header_t
*http_hdr
= NULL
;
341 if( stream
==NULL
) return 0;
343 if( stream
->fd
>0 ) closesocket(stream
->fd
); // need to reconnect to seek in http-stream
344 fd
= http_send_request( stream
->streaming_ctrl
->url
, pos
);
347 http_hdr
= http_read_response( fd
);
349 if( http_hdr
==NULL
) return 0;
351 switch( http_hdr
->status_code
) {
354 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Type: [%s]\n", http_get_field(http_hdr
, "Content-Type") );
355 mp_msg(MSGT_NETWORK
,MSGL_V
,"Content-Length: [%s]\n", http_get_field(http_hdr
, "Content-Length") );
356 if( http_hdr
->body_size
>0 ) {
357 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
358 http_free( http_hdr
);
364 mp_msg(MSGT_NETWORK
,MSGL_ERR
,MSGTR_MPDEMUX_NW_ErrServerReturned
, http_hdr
->status_code
, http_hdr
->reason_phrase
);
371 http_free( http_hdr
);
372 stream
->streaming_ctrl
->data
= NULL
;
382 streaming_bufferize( streaming_ctrl_t
*streaming_ctrl
, char *buffer
, int size
) {
383 //printf("streaming_bufferize\n");
384 streaming_ctrl
->buffer
= malloc(size
);
385 if( streaming_ctrl
->buffer
==NULL
) {
386 mp_msg(MSGT_NETWORK
,MSGL_FATAL
,MSGTR_MemAllocFailed
);
389 memcpy( streaming_ctrl
->buffer
, buffer
, size
);
390 streaming_ctrl
->buffer_size
= size
;
395 nop_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*stream_ctrl
) {
397 //printf("nop_streaming_read\n");
398 if( stream_ctrl
->buffer_size
!=0 ) {
399 int buffer_len
= stream_ctrl
->buffer_size
-stream_ctrl
->buffer_pos
;
400 //printf("%d bytes in buffer\n", stream_ctrl->buffer_size);
401 len
= (size
<buffer_len
)?size
:buffer_len
;
402 memcpy( buffer
, (stream_ctrl
->buffer
)+(stream_ctrl
->buffer_pos
), len
);
403 stream_ctrl
->buffer_pos
+= len
;
404 //printf("buffer_pos = %d\n", stream_ctrl->buffer_pos );
405 if( stream_ctrl
->buffer_pos
>=stream_ctrl
->buffer_size
) {
406 free( stream_ctrl
->buffer
);
407 stream_ctrl
->buffer
= NULL
;
408 stream_ctrl
->buffer_size
= 0;
409 stream_ctrl
->buffer_pos
= 0;
410 //printf("buffer cleaned\n");
412 //printf("read %d bytes from buffer\n", len );
417 ret
= recv( fd
, buffer
+len
, size
-len
, 0 );
419 mp_msg(MSGT_NETWORK
,MSGL_ERR
,"nop_streaming_read error : %s\n",strerror(errno
));
422 //printf("read %d bytes from network\n", len );
429 nop_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*stream_ctrl
) {
431 // To shut up gcc warning
438 void fixup_network_stream_cache(stream_t
*stream
) {
439 if(stream
->streaming_ctrl
->buffering
) {
440 if(stream_cache_size
<0) {
441 // cache option not set, will use our computed value.
442 // buffer in KBytes, *5 because the prefill is 20% of the buffer.
443 stream_cache_size
= (stream
->streaming_ctrl
->prebuffer_size
/1024)*5;
444 if( stream_cache_size
<64 ) stream_cache_size
= 64; // 16KBytes min buffer
446 mp_msg(MSGT_NETWORK
,MSGL_INFO
,MSGTR_MPDEMUX_NW_CacheSizeSetTo
, stream_cache_size
);
452 streaming_stop( stream_t
*stream
) {
453 stream
->streaming_ctrl
->status
= streaming_stopped_e
;