do not override *file_format if already set by asf_streaming_start()
[mplayer/glamo.git] / stream / asf_streaming.c
blob9aba5c2a2c68b570032f2fbe0de79ee4c1a52495
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <limits.h>
8 #include "config.h"
9 #include "mp_msg.h"
10 #include "help_mp.h"
12 #ifndef HAVE_WINSOCK2
13 #define closesocket close
14 #else
15 #include <winsock2.h>
16 #endif
18 #include "url.h"
19 #include "http.h"
20 #include "libmpdemux/asf.h"
22 #include "stream.h"
23 #include "libmpdemux/demuxer.h"
25 #include "network.h"
26 #include "tcp.h"
28 #ifdef ARCH_X86
29 #define ASF_LOAD_GUID_PREFIX(guid) (*(uint32_t *)(guid))
30 #else
31 #define ASF_LOAD_GUID_PREFIX(guid) \
32 ((guid)[3] << 24 | (guid)[2] << 16 | (guid)[1] << 8 | (guid)[0])
33 #endif
35 extern int network_bandwidth;
37 int asf_mmst_streaming_start( stream_t *stream );
38 static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
40 // We can try several protocol for asf streaming
41 // * first the UDP protcol, if there is a firewall, UDP
42 // packets will not come back, so the mmsu will fail.
43 // * Then we can try TCP, but if there is a proxy for
44 // internet connection, the TCP connection will not get
45 // through
46 // * Then we can try HTTP.
47 //
48 // Note: Using WMP sequence MMSU then MMST and then HTTP.
50 static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
51 char *proto = stream->streaming_ctrl->url->protocol;
52 int fd = -1;
53 int port = stream->streaming_ctrl->url->port;
55 // Is protocol mms or mmsu?
56 if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
58 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
59 //fd = asf_mmsu_streaming_start( stream );
60 if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
61 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/UDP failed\n");
62 if( fd==-2 ) return -1;
65 //Is protocol mms or mmst?
66 if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
68 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
69 fd = asf_mmst_streaming_start( stream );
70 stream->streaming_ctrl->url->port = port;
71 if( fd>-1 ) return fd;
72 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n");
73 if( fd==-2 ) return -1;
76 //Is protocol http, http_proxy, or mms?
77 if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
78 !strcasecmp(proto, "mms") || !strcasecmp(proto, "mmshttp"))
80 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
81 fd = asf_http_streaming_start( stream, demuxer_type );
82 stream->streaming_ctrl->url->port = port;
83 if( fd>-1 ) return fd;
84 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
85 if( fd==-2 ) return -1;
88 //everything failed
89 return -1;
92 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
93 /*
94 printf("ASF stream chunck size=%d\n", stream_chunck->size);
95 printf("length: %d\n", length );
96 printf("0x%02X\n", stream_chunck->type );
98 if( drop_packet!=NULL ) *drop_packet = 0;
100 if( stream_chunck->size<8 ) {
101 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
102 return -1;
104 if( stream_chunck->size!=stream_chunck->size_confirm ) {
105 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, stream_chunck->size, stream_chunck->size_confirm);
106 return -1;
109 printf(" type: 0x%02X\n", stream_chunck->type );
110 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
111 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
112 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
113 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
115 switch(stream_chunck->type) {
116 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
117 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
118 if( drop_packet!=NULL ) *drop_packet = 1;
119 return stream_chunck->size;
120 break;
121 case ASF_STREAMING_DATA: // $D Data follows
122 // printf("=====> Data follows\n");
123 break;
124 case ASF_STREAMING_END_TRANS: // $E Transfer complete
125 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
126 if( drop_packet!=NULL ) *drop_packet = 1;
127 return stream_chunck->size;
128 break;
129 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
130 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
131 break;
132 default:
133 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
135 return stream_chunck->size+4;
138 extern int find_asf_guid(char *buf, const char *guid, int cur_pos, int buf_len);
139 extern const char asf_file_header_guid[];
140 extern const char asf_stream_header_guid[];
141 extern const char asf_stream_group_guid[];
142 extern int audio_id;
143 extern int video_id;
145 static void close_s(stream_t *stream) {
146 close(stream->fd);
147 stream->fd=-1;
150 static int max_idx(int s_count, int *s_rates, int bound) {
151 int i, best = -1, rate = -1;
152 for (i = 0; i < s_count; i++) {
153 if (s_rates[i] > rate && s_rates[i] <= bound) {
154 rate = s_rates[i];
155 best = i;
158 return best;
161 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
162 ASF_header_t asfh;
163 ASF_stream_chunck_t chunk;
164 asf_http_streaming_ctrl_t* asf_ctrl = (asf_http_streaming_ctrl_t*) streaming_ctrl->data;
165 char* buffer=NULL, *chunk_buffer=NULL;
166 int i,r,size,pos = 0;
167 int start;
168 int buffer_size = 0;
169 int chunk_size2read = 0;
170 int bw = streaming_ctrl->bandwidth;
171 int *v_rates = NULL, *a_rates = NULL;
172 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
174 if(asf_ctrl == NULL) return -1;
176 // The ASF header can be in several network chunks. For example if the content description
177 // is big, the ASF header will be split in 2 network chunk.
178 // So we need to retrieve all the chunk before starting to parse the header.
179 do {
180 for( r=0; r < (int)sizeof(ASF_stream_chunck_t) ; ) {
181 i = nop_streaming_read(fd,((char*)&chunk)+r,sizeof(ASF_stream_chunck_t) - r,streaming_ctrl);
182 if(i <= 0) return -1;
183 r += i;
185 // Endian handling of the stream chunk
186 le2me_ASF_stream_chunck_t(&chunk);
187 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
188 if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
189 if(size < 0){
190 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
191 return -1;
193 if (chunk.type != ASF_STREAMING_HEADER) {
194 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
195 return -1;
198 // audit: do not overflow buffer_size
199 if (size > SIZE_MAX - buffer_size) return -1;
200 buffer = (char*) malloc(size+buffer_size);
201 if(buffer == NULL) {
202 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
203 return -1;
205 if( chunk_buffer!=NULL ) {
206 memcpy( buffer, chunk_buffer, buffer_size );
207 free( chunk_buffer );
209 chunk_buffer = buffer;
210 buffer += buffer_size;
211 buffer_size += size;
213 for(r = 0; r < size;) {
214 i = nop_streaming_read(fd,buffer+r,size-r,streaming_ctrl);
215 if(i < 0) {
216 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
217 return -1;
219 r += i;
222 if( chunk_size2read==0 ) {
223 if(size < (int)sizeof(asfh)) {
224 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
225 return -1;
226 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
227 memcpy(&asfh,buffer,sizeof(asfh));
228 le2me_ASF_header_t(&asfh);
229 chunk_size2read = asfh.objh.size;
230 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
232 } while( buffer_size<chunk_size2read);
233 buffer = chunk_buffer;
234 size = buffer_size;
236 if(asfh.cno > 256) {
237 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrSubChunkNumberInvalid);
238 return -1;
241 start = sizeof(asfh);
243 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
244 if (pos >= 0) {
245 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
246 pos += sizeof(ASF_file_header_t);
247 if (pos > size) goto len_err_out;
248 le2me_ASF_file_header_t(fileh);
250 if(fileh.packetsize != fileh.packetsize2) {
251 printf("Error packetsize check don't match\n");
252 return -1;
255 asf_ctrl->packet_size = fileh->max_packet_size;
256 // before playing.
257 // preroll: time in ms to bufferize before playing
258 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
261 pos = start;
262 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
264 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
265 pos += sizeof(ASF_stream_header_t);
266 if (pos > size) goto len_err_out;
267 le2me_ASF_stream_header_t(streamh);
268 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
269 case 0xF8699E40 : // audio stream
270 if(asf_ctrl->audio_streams == NULL){
271 asf_ctrl->audio_streams = malloc(sizeof(int));
272 asf_ctrl->n_audio = 1;
273 } else {
274 asf_ctrl->n_audio++;
275 asf_ctrl->audio_streams = (int*)realloc(asf_ctrl->audio_streams,
276 asf_ctrl->n_audio*sizeof(int));
278 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = streamh->stream_no;
279 break;
280 case 0xBC19EFC0 : // video stream
281 if(asf_ctrl->video_streams == NULL){
282 asf_ctrl->video_streams = malloc(sizeof(int));
283 asf_ctrl->n_video = 1;
284 } else {
285 asf_ctrl->n_video++;
286 asf_ctrl->video_streams = (int*)realloc(asf_ctrl->video_streams,
287 asf_ctrl->n_video*sizeof(int));
289 asf_ctrl->video_streams[asf_ctrl->n_video-1] = streamh->stream_no;
290 break;
294 // always allocate to avoid lots of ifs later
295 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
296 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
298 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
299 if (pos >= 0) {
300 // stream bitrate properties object
301 int stream_count;
302 char *ptr = &buffer[pos];
304 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
305 stream_count = le2me_16(*(uint16_t*)ptr);
306 ptr += sizeof(uint16_t);
307 if (ptr > &buffer[size]) goto len_err_out;
308 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
309 stream_count, stream_count );
310 for( i=0 ; i<stream_count ; i++ ) {
311 uint32_t rate;
312 int id;
313 int j;
314 id = le2me_16(*(uint16_t*)ptr);
315 ptr += sizeof(uint16_t);
316 if (ptr > &buffer[size]) goto len_err_out;
317 memcpy(&rate, ptr, sizeof(uint32_t));// workaround unaligment bug on sparc
318 ptr += sizeof(uint32_t);
319 if (ptr > &buffer[size]) goto len_err_out;
320 rate = le2me_32(rate);
321 mp_msg(MSGT_NETWORK, MSGL_V,
322 " stream id=[0x%x][%u]\n", id, id);
323 mp_msg(MSGT_NETWORK, MSGL_V,
324 " max bitrate=[0x%x][%u]\n", rate, rate);
325 for (j = 0; j < asf_ctrl->n_video; j++) {
326 if (id == asf_ctrl->video_streams[j]) {
327 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
328 v_rates[j] = rate;
329 break;
332 for (j = 0; j < asf_ctrl->n_audio; j++) {
333 if (id == asf_ctrl->audio_streams[j]) {
334 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
335 a_rates[j] = rate;
336 break;
341 free(buffer);
343 // automatic stream selection based on bandwidth
344 if (bw == 0) bw = INT_MAX;
345 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
347 if (asf_ctrl->n_audio) {
348 // find lowest-bitrate audio stream
349 a_rate = a_rates[0];
350 a_idx = 0;
351 for (i = 0; i < asf_ctrl->n_audio; i++) {
352 if (a_rates[i] < a_rate) {
353 a_rate = a_rates[i];
354 a_idx = i;
357 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
358 // both audio and video are not possible, try video only next
359 a_idx = -1;
360 a_rate = 0;
363 // find best video stream
364 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
365 if (v_idx >= 0)
366 v_rate = v_rates[v_idx];
368 // find best audio stream
369 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
371 free(v_rates);
372 free(a_rates);
374 if (a_idx < 0 && v_idx < 0) {
375 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
376 return -1;
379 if (audio_id > 0)
380 // a audio stream was forced
381 asf_ctrl->audio_id = audio_id;
382 else if (a_idx >= 0)
383 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
384 else if (asf_ctrl->n_audio) {
385 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
386 audio_id = -2;
389 if (video_id > 0)
390 // a video stream was forced
391 asf_ctrl->video_id = video_id;
392 else if (v_idx >= 0)
393 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
394 else if (asf_ctrl->n_video) {
395 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
396 video_id = -2;
399 return 1;
401 len_err_out:
402 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
403 if (buffer) free(buffer);
404 if (v_rates) free(v_rates);
405 if (a_rates) free(a_rates);
406 return -1;
409 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
410 static ASF_stream_chunck_t chunk;
411 int read,chunk_size = 0;
412 static int rest = 0, drop_chunk = 0, waiting = 0;
413 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
415 while(1) {
416 if (rest == 0 && waiting == 0) {
417 read = 0;
418 while(read < (int)sizeof(ASF_stream_chunck_t)){
419 int r = nop_streaming_read( fd, ((char*)&chunk) + read,
420 sizeof(ASF_stream_chunck_t)-read,
421 streaming_ctrl );
422 if(r <= 0){
423 if( r < 0)
424 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingChunkHeader);
425 return -1;
427 read += r;
430 // Endian handling of the stream chunk
431 le2me_ASF_stream_chunck_t(&chunk);
432 chunk_size = asf_streaming( &chunk, &drop_chunk );
433 if(chunk_size < 0) {
434 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
435 return -1;
437 chunk_size -= sizeof(ASF_stream_chunck_t);
439 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
440 if (asf_http_ctrl->packet_size < chunk_size) {
441 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
442 return -1;
444 waiting = asf_http_ctrl->packet_size;
445 } else {
446 waiting = chunk_size;
449 } else if (rest){
450 chunk_size = rest;
451 rest = 0;
454 read = 0;
455 if ( waiting >= chunk_size) {
456 if (chunk_size > size){
457 rest = chunk_size - size;
458 chunk_size = size;
460 while(read < chunk_size) {
461 int got = nop_streaming_read( fd,buffer+read,chunk_size-read,streaming_ctrl );
462 if(got <= 0) {
463 if(got < 0)
464 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingChunk);
465 return -1;
467 read += got;
469 waiting -= read;
470 if (drop_chunk) continue;
472 if (rest == 0 && waiting > 0 && size-read > 0) {
473 int s = FFMIN(waiting,size-read);
474 memset(buffer+read,0,s);
475 waiting -= s;
476 read += s;
478 break;
481 return read;
484 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
485 return -1;
486 // to shut up gcc warning
487 fd++;
488 pos++;
489 streaming_ctrl=NULL;
492 static int asf_header_check( HTTP_header_t *http_hdr ) {
493 ASF_obj_header_t *objh;
494 if( http_hdr==NULL ) return -1;
495 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
497 objh = (ASF_obj_header_t*)http_hdr->body;
498 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
499 return -1;
502 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
503 if( content_type==NULL ) return ASF_Unknown_e;
504 if( !strcasecmp(content_type, "application/octet-stream") ||
505 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
506 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
507 !strcasecmp(content_type, "video/x-ms-asf")) {
509 if( strstr(features, "broadcast") ) {
510 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
511 return ASF_Live_e;
512 } else {
513 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
514 return ASF_Prerecorded_e;
516 } else {
517 // Ok in a perfect world, web servers should be well configured
518 // so we could used mime type to know the stream type,
519 // but guess what? All of them are not well configured.
520 // So we have to check for an asf header :(, but it works :p
521 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
522 if( asf_header_check( http_hdr )==0 ) {
523 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
524 return ASF_PlainText_e;
525 } else if( (!strcasecmp(content_type, "text/html")) ) {
526 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
527 return ASF_Unknown_e;
528 } else {
529 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
530 return ASF_Redirector_e;
532 } else {
533 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
534 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
535 (!strcasecmp(content_type, "video/x-ms-asf")) ||
536 (!strcasecmp(content_type, "video/x-ms-afs")) ||
537 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
538 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
539 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
540 return ASF_Redirector_e;
541 } else if( !strcasecmp(content_type, "text/plain") ) {
542 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
543 return ASF_PlainText_e;
544 } else {
545 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
546 return ASF_Unknown_e;
550 return ASF_Unknown_e;
553 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
554 HTTP_header_t *http_hdr;
555 URL_t *url = NULL;
556 URL_t *server_url = NULL;
557 asf_http_streaming_ctrl_t *asf_http_ctrl;
558 char str[250];
559 char *ptr;
560 int i, enable;
562 int offset_hi=0, offset_lo=0, length=0;
563 int asf_nb_stream=0, stream_id;
565 // Sanity check
566 if( streaming_ctrl==NULL ) return NULL;
567 url = streaming_ctrl->url;
568 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
569 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
571 // Common header for all requests.
572 http_hdr = http_new_header();
573 http_set_field( http_hdr, "Accept: */*" );
574 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
575 http_add_basic_authentication( http_hdr, url->username, url->password );
577 // Check if we are using a proxy
578 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
579 server_url = url_new( (url->file)+1 );
580 if( server_url==NULL ) {
581 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
582 http_free( http_hdr );
583 return NULL;
585 http_set_uri( http_hdr, server_url->url );
586 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
587 url_free( server_url );
588 } else {
589 http_set_uri( http_hdr, url->file );
590 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
593 http_set_field( http_hdr, str );
594 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
595 sprintf(str,
596 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
597 offset_hi, offset_lo, asf_http_ctrl->request, length );
598 http_set_field( http_hdr, str );
600 switch( asf_http_ctrl->streaming_type ) {
601 case ASF_Live_e:
602 case ASF_Prerecorded_e:
603 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
604 ptr = str;
605 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
606 if(asf_http_ctrl->n_audio > 0) {
607 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
608 stream_id = asf_http_ctrl->audio_streams[i];
609 if(stream_id == asf_http_ctrl->audio_id) {
610 enable = 0;
611 } else {
612 enable = 2;
613 continue;
615 asf_nb_stream++;
616 ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
619 if(asf_http_ctrl->n_video > 0) {
620 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
621 stream_id = asf_http_ctrl->video_streams[i];
622 if(stream_id == asf_http_ctrl->video_id) {
623 enable = 0;
624 } else {
625 enable = 2;
626 continue;
628 asf_nb_stream++;
629 ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
632 http_set_field( http_hdr, str );
633 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
634 http_set_field( http_hdr, str );
635 break;
636 case ASF_Redirector_e:
637 break;
638 case ASF_Unknown_e:
639 // First request goes here.
640 break;
641 default:
642 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
645 http_set_field( http_hdr, "Connection: Close" );
646 http_build_request( http_hdr );
648 return http_hdr;
651 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
652 char *content_type, *pragma;
653 char features[64] = "\0";
654 size_t len;
655 if( http_response_parse(http_hdr)<0 ) {
656 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
657 return -1;
659 switch( http_hdr->status_code ) {
660 case 200:
661 break;
662 case 401: // Authentication required
663 return ASF_Authenticate_e;
664 default:
665 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
666 return -1;
669 content_type = http_get_field( http_hdr, "Content-Type");
670 //printf("Content-Type: [%s]\n", content_type);
672 pragma = http_get_field( http_hdr, "Pragma");
673 while( pragma!=NULL ) {
674 char *comma_ptr=NULL;
675 char *end;
676 //printf("Pragma: [%s]\n", pragma );
677 // The pragma line can get severals attributes
678 // separeted with a comma ','.
679 do {
680 if( !strncasecmp( pragma, "features=", 9) ) {
681 pragma += 9;
682 end = strstr( pragma, "," );
683 if( end==NULL ) {
684 len = strlen(pragma);
685 } else {
686 len = (unsigned int)(end-pragma);
688 if(len > sizeof(features) - 1) {
689 mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
690 len = sizeof(features) - 1;
692 strncpy( features, pragma, len );
693 features[len]='\0';
694 break;
696 comma_ptr = strstr( pragma, "," );
697 if( comma_ptr!=NULL ) {
698 pragma = comma_ptr+1;
699 if( pragma[0]==' ' ) pragma++;
701 } while( comma_ptr!=NULL );
702 pragma = http_get_next_field( http_hdr );
704 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
705 return 0;
708 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
709 HTTP_header_t *http_hdr=NULL;
710 URL_t *url = stream->streaming_ctrl->url;
711 asf_http_streaming_ctrl_t *asf_http_ctrl;
712 char buffer[BUFFER_SIZE];
713 int i, ret;
714 int fd = stream->fd;
715 int done;
716 int auth_retry = 0;
718 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
719 if( asf_http_ctrl==NULL ) {
720 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
721 return -1;
723 asf_http_ctrl->streaming_type = ASF_Unknown_e;
724 asf_http_ctrl->request = 1;
725 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
726 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
727 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
729 do {
730 done = 1;
731 if( fd>0 ) closesocket( fd );
733 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
734 if( url->port==0 ) url->port = 8080;
735 } else {
736 if( url->port==0 ) url->port = 80;
738 fd = connect2Server( url->hostname, url->port, 1);
739 if( fd<0 ) return fd;
741 http_hdr = asf_http_request( stream->streaming_ctrl );
742 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
743 for(i=0; i < (int)http_hdr->buffer_size ; ) {
744 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
745 if(r <0) {
746 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
747 goto err_out;
749 i += r;
751 http_free( http_hdr );
752 http_hdr = http_new_header();
753 do {
754 i = recv( fd, buffer, BUFFER_SIZE, 0 );
755 //printf("read: %d\n", i );
756 if( i<=0 ) {
757 perror("read");
758 goto err_out;
760 http_response_append( http_hdr, buffer, i );
761 } while( !http_is_header_entire( http_hdr ) );
762 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
763 http_hdr->buffer[http_hdr->buffer_size]='\0';
764 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
766 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
767 if( ret<0 ) {
768 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
769 goto err_out;
771 switch( asf_http_ctrl->streaming_type ) {
772 case ASF_Live_e:
773 case ASF_Prerecorded_e:
774 case ASF_PlainText_e:
775 if( http_hdr->body_size>0 ) {
776 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
777 goto err_out;
780 if( asf_http_ctrl->request==1 ) {
781 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
782 // First request, we only got the ASF header.
783 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
784 if(ret < 0) goto err_out;
785 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
786 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
787 goto err_out;
789 asf_http_ctrl->request++;
790 done = 0;
791 } else {
792 done = 1;
795 break;
796 case ASF_Redirector_e:
797 if( http_hdr->body_size>0 ) {
798 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
799 goto err_out;
802 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
803 done = 1;
804 break;
805 case ASF_Authenticate_e:
806 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
807 asf_http_ctrl->streaming_type = ASF_Unknown_e;
808 done = 0;
809 break;
810 case ASF_Unknown_e:
811 default:
812 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
813 goto err_out;
815 // Check if we got a redirect.
816 } while(!done);
818 stream->fd = fd;
819 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
820 stream->streaming_ctrl->streaming_read = nop_streaming_read;
821 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
822 } else {
823 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
824 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
825 stream->streaming_ctrl->buffering = 1;
827 stream->streaming_ctrl->status = streaming_playing_e;
828 stream->close = close_s;
830 http_free( http_hdr );
831 return 0;
833 err_out:
834 if (fd > 0)
835 closesocket(fd);
836 stream->fd = -1;
837 http_free(http_hdr);
838 return -1;
841 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
842 URL_t *url;
844 stream->streaming_ctrl = streaming_ctrl_new();
845 if( stream->streaming_ctrl==NULL ) {
846 return STREAM_ERROR;
848 stream->streaming_ctrl->bandwidth = network_bandwidth;
849 url = url_new(stream->url);
850 stream->streaming_ctrl->url = check4proxies(url);
851 url_free(url);
853 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_MPDEMUX_ASF_InfoStreamASFURL, stream->url);
854 if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
855 streaming_ctrl_free(stream->streaming_ctrl);
856 stream->streaming_ctrl = NULL;
857 return STREAM_UNSUPPORTED;
860 if(asf_streaming_start(stream, file_format) < 0) {
861 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_ASF_StreamingFailed);
862 streaming_ctrl_free(stream->streaming_ctrl);
863 stream->streaming_ctrl = NULL;
864 return STREAM_UNSUPPORTED;
867 if (*file_format != DEMUXER_TYPE_PLAYLIST)
868 *file_format = DEMUXER_TYPE_ASF;
869 stream->type = STREAMTYPE_STREAM;
870 fixup_network_stream_cache(stream);
871 return STREAM_OK;
874 const stream_info_t stream_info_asf = {
875 "mms and mms over http streaming",
876 "null",
877 "Bertrand, Reimar Doeffinger, Albeu",
878 "originally based on work by Majormms (is that code still there?)",
879 open_s,
880 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL},
881 NULL,
882 0 // Urls are an option string