Fix:
[mplayer/glamo.git] / libmpdemux / asf_streaming.c
blob2ddfbecc145a915983e02ddfb127603f3d105914
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 "asf.h"
22 #include "stream.h"
23 #include "demuxer.h"
25 #include "network.h"
27 #ifdef ARCH_X86
28 #define ASF_LOAD_GUID_PREFIX(guid) (*(uint32_t *)(guid))
29 #else
30 #define ASF_LOAD_GUID_PREFIX(guid) \
31 ((guid)[3] << 24 | (guid)[2] << 16 | (guid)[1] << 8 | (guid)[0])
32 #endif
34 extern int network_bandwidth;
36 int asf_mmst_streaming_start( stream_t *stream );
37 static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
39 // We can try several protocol for asf streaming
40 // * first the UDP protcol, if there is a firewall, UDP
41 // packets will not come back, so the mmsu will fail.
42 // * Then we can try TCP, but if there is a proxy for
43 // internet connection, the TCP connection will not get
44 // through
45 // * Then we can try HTTP.
46 //
47 // Note: Using WMP sequence MMSU then MMST and then HTTP.
49 static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
50 char *proto = stream->streaming_ctrl->url->protocol;
51 int fd = -1;
52 int port = stream->streaming_ctrl->url->port;
54 // Is protocol mms or mmsu?
55 if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
57 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
58 //fd = asf_mmsu_streaming_start( stream );
59 if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
60 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/UDP failed\n");
61 if( fd==-2 ) return -1;
64 //Is protocol mms or mmst?
65 if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
67 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
68 fd = asf_mmst_streaming_start( stream );
69 stream->streaming_ctrl->url->port = port;
70 if( fd>-1 ) return fd;
71 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n");
72 if( fd==-2 ) return -1;
75 //Is protocol http, http_proxy, or mms?
76 if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
77 !strcasecmp(proto, "mms") || !strcasecmp(proto, "mmshttp"))
79 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
80 fd = asf_http_streaming_start( stream, demuxer_type );
81 stream->streaming_ctrl->url->port = port;
82 if( fd>-1 ) return fd;
83 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
84 if( fd==-2 ) return -1;
87 //everything failed
88 return -1;
91 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
92 /*
93 printf("ASF stream chunck size=%d\n", stream_chunck->size);
94 printf("length: %d\n", length );
95 printf("0x%02X\n", stream_chunck->type );
97 if( drop_packet!=NULL ) *drop_packet = 0;
99 if( stream_chunck->size<8 ) {
100 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
101 return -1;
103 if( stream_chunck->size!=stream_chunck->size_confirm ) {
104 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, stream_chunck->size, stream_chunck->size_confirm);
105 return -1;
108 printf(" type: 0x%02X\n", stream_chunck->type );
109 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
110 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
111 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
112 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
114 switch(stream_chunck->type) {
115 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
116 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
117 if( drop_packet!=NULL ) *drop_packet = 1;
118 return stream_chunck->size;
119 break;
120 case ASF_STREAMING_DATA: // $D Data follows
121 // printf("=====> Data follows\n");
122 break;
123 case ASF_STREAMING_END_TRANS: // $E Transfer complete
124 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
125 if( drop_packet!=NULL ) *drop_packet = 1;
126 return stream_chunck->size;
127 break;
128 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
129 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
130 break;
131 default:
132 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
134 return stream_chunck->size+4;
137 extern int find_asf_guid(char *buf, const char *guid, int cur_pos, int buf_len);
138 extern const char asf_file_header_guid[];
139 extern const char asf_stream_header_guid[];
140 extern const char asf_stream_group_guid[];
141 extern int audio_id;
142 extern int video_id;
144 static void close_s(stream_t *stream) {
145 close(stream->fd);
146 stream->fd=-1;
149 static int max_idx(int s_count, int *s_rates, int bound) {
150 int i, best = -1, rate = -1;
151 for (i = 0; i < s_count; i++) {
152 if (s_rates[i] > rate && s_rates[i] <= bound) {
153 rate = s_rates[i];
154 best = i;
157 return best;
160 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
161 ASF_header_t asfh;
162 ASF_stream_chunck_t chunk;
163 asf_http_streaming_ctrl_t* asf_ctrl = (asf_http_streaming_ctrl_t*) streaming_ctrl->data;
164 char* buffer=NULL, *chunk_buffer=NULL;
165 int i,r,size,pos = 0;
166 int start;
167 int buffer_size = 0;
168 int chunk_size2read = 0;
169 int bw = streaming_ctrl->bandwidth;
170 int *v_rates = NULL, *a_rates = NULL;
171 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
173 if(asf_ctrl == NULL) return -1;
175 // The ASF header can be in several network chunks. For example if the content description
176 // is big, the ASF header will be split in 2 network chunk.
177 // So we need to retrieve all the chunk before starting to parse the header.
178 do {
179 for( r=0; r < (int)sizeof(ASF_stream_chunck_t) ; ) {
180 i = nop_streaming_read(fd,((char*)&chunk)+r,sizeof(ASF_stream_chunck_t) - r,streaming_ctrl);
181 if(i <= 0) return -1;
182 r += i;
184 // Endian handling of the stream chunk
185 le2me_ASF_stream_chunck_t(&chunk);
186 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
187 if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
188 if(size < 0){
189 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
190 return -1;
192 if (chunk.type != ASF_STREAMING_HEADER) {
193 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
194 return -1;
197 buffer = (char*) malloc(size+buffer_size);
198 if(buffer == NULL) {
199 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
200 return -1;
202 if( chunk_buffer!=NULL ) {
203 memcpy( buffer, chunk_buffer, buffer_size );
204 free( chunk_buffer );
206 chunk_buffer = buffer;
207 buffer += buffer_size;
208 buffer_size += size;
210 for(r = 0; r < size;) {
211 i = nop_streaming_read(fd,buffer+r,size-r,streaming_ctrl);
212 if(i < 0) {
213 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
214 return -1;
216 r += i;
219 if( chunk_size2read==0 ) {
220 if(size < (int)sizeof(asfh)) {
221 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
222 return -1;
223 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
224 memcpy(&asfh,buffer,sizeof(asfh));
225 le2me_ASF_header_t(&asfh);
226 chunk_size2read = asfh.objh.size;
227 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
229 } while( buffer_size<chunk_size2read);
230 buffer = chunk_buffer;
231 size = buffer_size;
233 if(asfh.cno > 256) {
234 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrSubChunkNumberInvalid);
235 return -1;
238 start = sizeof(asfh);
240 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
241 if (pos >= 0) {
242 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
243 pos += sizeof(ASF_file_header_t);
244 if (pos > size) goto len_err_out;
245 le2me_ASF_file_header_t(fileh);
247 if(fileh.packetsize != fileh.packetsize2) {
248 printf("Error packetsize check don't match\n");
249 return -1;
252 asf_ctrl->packet_size = fileh->max_packet_size;
253 // before playing.
254 // preroll: time in ms to bufferize before playing
255 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
258 pos = start;
259 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
261 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
262 pos += sizeof(ASF_stream_header_t);
263 if (pos > size) goto len_err_out;
264 le2me_ASF_stream_header_t(streamh);
265 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
266 case 0xF8699E40 : // audio stream
267 if(asf_ctrl->audio_streams == NULL){
268 asf_ctrl->audio_streams = (int*)malloc(sizeof(int));
269 asf_ctrl->n_audio = 1;
270 } else {
271 asf_ctrl->n_audio++;
272 asf_ctrl->audio_streams = (int*)realloc(asf_ctrl->audio_streams,
273 asf_ctrl->n_audio*sizeof(int));
275 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = streamh->stream_no;
276 break;
277 case 0xBC19EFC0 : // video stream
278 if(asf_ctrl->video_streams == NULL){
279 asf_ctrl->video_streams = (int*)malloc(sizeof(int));
280 asf_ctrl->n_video = 1;
281 } else {
282 asf_ctrl->n_video++;
283 asf_ctrl->video_streams = (int*)realloc(asf_ctrl->video_streams,
284 asf_ctrl->n_video*sizeof(int));
286 asf_ctrl->video_streams[asf_ctrl->n_video-1] = streamh->stream_no;
287 break;
291 // always allocate to avoid lots of ifs later
292 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
293 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
295 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
296 if (pos >= 0) {
297 // stream bitrate properties object
298 int stream_count;
299 char *ptr = &buffer[pos];
301 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
302 stream_count = le2me_16(*(uint16_t*)ptr);
303 ptr += sizeof(uint16_t);
304 if (ptr > &buffer[size]) goto len_err_out;
305 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
306 stream_count, stream_count );
307 for( i=0 ; i<stream_count ; i++ ) {
308 uint32_t rate;
309 int id;
310 int j;
311 id = le2me_16(*(uint16_t*)ptr);
312 ptr += sizeof(uint16_t);
313 if (ptr > &buffer[size]) goto len_err_out;
314 memcpy(&rate, ptr, sizeof(uint32_t));// workaround unaligment bug on sparc
315 ptr += sizeof(uint32_t);
316 if (ptr > &buffer[size]) goto len_err_out;
317 rate = le2me_32(rate);
318 mp_msg(MSGT_NETWORK, MSGL_V,
319 " stream id=[0x%x][%u]\n", id, id);
320 mp_msg(MSGT_NETWORK, MSGL_V,
321 " max bitrate=[0x%x][%u]\n", rate, rate);
322 for (j = 0; j < asf_ctrl->n_video; j++) {
323 if (id == asf_ctrl->video_streams[j]) {
324 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
325 v_rates[j] = rate;
326 break;
329 for (j = 0; j < asf_ctrl->n_audio; j++) {
330 if (id == asf_ctrl->audio_streams[j]) {
331 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
332 a_rates[j] = rate;
333 break;
338 free(buffer);
340 // automatic stream selection based on bandwidth
341 if (bw == 0) bw = INT_MAX;
342 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
344 if (asf_ctrl->n_audio) {
345 // find lowest-bitrate audio stream
346 a_rate = a_rates[0];
347 a_idx = 0;
348 for (i = 0; i < asf_ctrl->n_audio; i++) {
349 if (a_rates[i] < a_rate) {
350 a_rate = a_rates[i];
351 a_idx = i;
354 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
355 // both audio and video are not possible, try video only next
356 a_idx = -1;
357 a_rate = 0;
360 // find best video stream
361 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
362 if (v_idx >= 0)
363 v_rate = v_rates[v_idx];
365 // find best audio stream
366 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
368 free(v_rates);
369 free(a_rates);
371 if (a_idx < 0 && v_idx < 0) {
372 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
373 return -1;
376 if (audio_id > 0)
377 // a audio stream was forced
378 asf_ctrl->audio_id = audio_id;
379 else if (a_idx >= 0)
380 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
381 else if (asf_ctrl->n_audio) {
382 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
383 audio_id = -2;
386 if (video_id > 0)
387 // a video stream was forced
388 asf_ctrl->video_id = video_id;
389 else if (v_idx >= 0)
390 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
391 else if (asf_ctrl->n_video) {
392 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
393 video_id = -2;
396 return 1;
398 len_err_out:
399 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
400 if (buffer) free(buffer);
401 if (v_rates) free(v_rates);
402 if (a_rates) free(a_rates);
403 return -1;
406 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
407 static ASF_stream_chunck_t chunk;
408 int read,chunk_size = 0;
409 static int rest = 0, drop_chunk = 0, waiting = 0;
410 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
412 while(1) {
413 if (rest == 0 && waiting == 0) {
414 read = 0;
415 while(read < (int)sizeof(ASF_stream_chunck_t)){
416 int r = nop_streaming_read( fd, ((char*)&chunk) + read,
417 sizeof(ASF_stream_chunck_t)-read,
418 streaming_ctrl );
419 if(r <= 0){
420 if( r < 0)
421 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingChunkHeader);
422 return -1;
424 read += r;
427 // Endian handling of the stream chunk
428 le2me_ASF_stream_chunck_t(&chunk);
429 chunk_size = asf_streaming( &chunk, &drop_chunk );
430 if(chunk_size < 0) {
431 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
432 return -1;
434 chunk_size -= sizeof(ASF_stream_chunck_t);
436 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
437 if (asf_http_ctrl->packet_size < chunk_size) {
438 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
439 return -1;
441 waiting = asf_http_ctrl->packet_size;
442 } else {
443 waiting = chunk_size;
446 } else if (rest){
447 chunk_size = rest;
448 rest = 0;
451 read = 0;
452 if ( waiting >= chunk_size) {
453 if (chunk_size > size){
454 rest = chunk_size - size;
455 chunk_size = size;
457 while(read < chunk_size) {
458 int got = nop_streaming_read( fd,buffer+read,chunk_size-read,streaming_ctrl );
459 if(got <= 0) {
460 if(got < 0)
461 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrReadingChunk);
462 return -1;
464 read += got;
466 waiting -= read;
467 if (drop_chunk) continue;
469 if (rest == 0 && waiting > 0 && size-read > 0) {
470 int s = MIN(waiting,size-read);
471 memset(buffer+read,0,s);
472 waiting -= s;
473 read += s;
475 break;
478 return read;
481 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
482 return -1;
483 // to shut up gcc warning
484 fd++;
485 pos++;
486 streaming_ctrl=NULL;
489 static int asf_header_check( HTTP_header_t *http_hdr ) {
490 ASF_obj_header_t *objh;
491 if( http_hdr==NULL ) return -1;
492 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
494 objh = (ASF_obj_header_t*)http_hdr->body;
495 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
496 return -1;
499 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
500 if( content_type==NULL ) return ASF_Unknown_e;
501 if( !strcasecmp(content_type, "application/octet-stream") ||
502 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
503 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
504 !strcasecmp(content_type, "video/x-ms-asf")) {
506 if( strstr(features, "broadcast") ) {
507 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
508 return ASF_Live_e;
509 } else {
510 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
511 return ASF_Prerecorded_e;
513 } else {
514 // Ok in a perfect world, web servers should be well configured
515 // so we could used mime type to know the stream type,
516 // but guess what? All of them are not well configured.
517 // So we have to check for an asf header :(, but it works :p
518 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
519 if( asf_header_check( http_hdr )==0 ) {
520 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
521 return ASF_PlainText_e;
522 } else if( (!strcasecmp(content_type, "text/html")) ) {
523 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
524 return ASF_Unknown_e;
525 } else {
526 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
527 return ASF_Redirector_e;
529 } else {
530 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
531 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
532 (!strcasecmp(content_type, "video/x-ms-asf")) ||
533 (!strcasecmp(content_type, "video/x-ms-afs")) ||
534 (!strcasecmp(content_type, "video/x-ms-wvx")) ||
535 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
536 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
537 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
538 return ASF_Redirector_e;
539 } else if( !strcasecmp(content_type, "text/plain") ) {
540 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
541 return ASF_PlainText_e;
542 } else {
543 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
544 return ASF_Unknown_e;
548 return ASF_Unknown_e;
551 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
552 HTTP_header_t *http_hdr;
553 URL_t *url = NULL;
554 URL_t *server_url = NULL;
555 asf_http_streaming_ctrl_t *asf_http_ctrl;
556 char str[250];
557 char *ptr;
558 int i, enable;
560 int offset_hi=0, offset_lo=0, length=0;
561 int asf_nb_stream=0, stream_id;
563 // Sanity check
564 if( streaming_ctrl==NULL ) return NULL;
565 url = streaming_ctrl->url;
566 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
567 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
569 // Common header for all requests.
570 http_hdr = http_new_header();
571 http_set_field( http_hdr, "Accept: */*" );
572 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
573 http_add_basic_authentication( http_hdr, url->username, url->password );
575 // Check if we are using a proxy
576 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
577 server_url = url_new( (url->file)+1 );
578 if( server_url==NULL ) {
579 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
580 http_free( http_hdr );
581 return NULL;
583 http_set_uri( http_hdr, server_url->url );
584 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
585 url_free( server_url );
586 } else {
587 http_set_uri( http_hdr, url->file );
588 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
591 http_set_field( http_hdr, str );
592 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
593 sprintf(str,
594 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
595 offset_hi, offset_lo, asf_http_ctrl->request, length );
596 http_set_field( http_hdr, str );
598 switch( asf_http_ctrl->streaming_type ) {
599 case ASF_Live_e:
600 case ASF_Prerecorded_e:
601 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
602 ptr = str;
603 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
604 if(asf_http_ctrl->n_audio > 0) {
605 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
606 stream_id = asf_http_ctrl->audio_streams[i];
607 if(stream_id == asf_http_ctrl->audio_id) {
608 enable = 0;
609 } else {
610 enable = 2;
611 continue;
613 asf_nb_stream++;
614 ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
617 if(asf_http_ctrl->n_video > 0) {
618 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
619 stream_id = asf_http_ctrl->video_streams[i];
620 if(stream_id == asf_http_ctrl->video_id) {
621 enable = 0;
622 } else {
623 enable = 2;
624 continue;
626 asf_nb_stream++;
627 ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
630 http_set_field( http_hdr, str );
631 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
632 http_set_field( http_hdr, str );
633 break;
634 case ASF_Redirector_e:
635 break;
636 case ASF_Unknown_e:
637 // First request goes here.
638 break;
639 default:
640 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
643 http_set_field( http_hdr, "Connection: Close" );
644 http_build_request( http_hdr );
646 return http_hdr;
649 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
650 char *content_type, *pragma;
651 char features[64] = "\0";
652 size_t len;
653 if( http_response_parse(http_hdr)<0 ) {
654 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
655 return -1;
657 switch( http_hdr->status_code ) {
658 case 200:
659 break;
660 case 401: // Authentication required
661 return ASF_Authenticate_e;
662 default:
663 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
664 return -1;
667 content_type = http_get_field( http_hdr, "Content-Type");
668 //printf("Content-Type: [%s]\n", content_type);
670 pragma = http_get_field( http_hdr, "Pragma");
671 while( pragma!=NULL ) {
672 char *comma_ptr=NULL;
673 char *end;
674 //printf("Pragma: [%s]\n", pragma );
675 // The pragma line can get severals attributes
676 // separeted with a comma ','.
677 do {
678 if( !strncasecmp( pragma, "features=", 9) ) {
679 pragma += 9;
680 end = strstr( pragma, "," );
681 if( end==NULL ) {
682 len = strlen(pragma);
683 } else {
684 len = (unsigned int)(end-pragma);
686 if(len > sizeof(features) - 1) {
687 mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
688 len = sizeof(features) - 1;
690 strncpy( features, pragma, len );
691 features[len]='\0';
692 break;
694 comma_ptr = strstr( pragma, "," );
695 if( comma_ptr!=NULL ) {
696 pragma = comma_ptr+1;
697 if( pragma[0]==' ' ) pragma++;
699 } while( comma_ptr!=NULL );
700 pragma = http_get_next_field( http_hdr );
702 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
703 return 0;
706 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
707 HTTP_header_t *http_hdr=NULL;
708 URL_t *url = stream->streaming_ctrl->url;
709 asf_http_streaming_ctrl_t *asf_http_ctrl;
710 char buffer[BUFFER_SIZE];
711 int i, ret;
712 int fd = stream->fd;
713 int done;
714 int auth_retry = 0;
716 asf_http_ctrl = (asf_http_streaming_ctrl_t*)malloc(sizeof(asf_http_streaming_ctrl_t));
717 if( asf_http_ctrl==NULL ) {
718 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
719 return -1;
721 asf_http_ctrl->streaming_type = ASF_Unknown_e;
722 asf_http_ctrl->request = 1;
723 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
724 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
725 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
727 do {
728 done = 1;
729 if( fd>0 ) closesocket( fd );
731 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
732 if( url->port==0 ) url->port = 8080;
733 } else {
734 if( url->port==0 ) url->port = 80;
736 fd = connect2Server( url->hostname, url->port, 1);
737 if( fd<0 ) return fd;
739 http_hdr = asf_http_request( stream->streaming_ctrl );
740 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
741 for(i=0; i < (int)http_hdr->buffer_size ; ) {
742 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
743 if(r <0) {
744 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
745 return -1;
747 i += r;
749 http_free( http_hdr );
750 http_hdr = http_new_header();
751 do {
752 i = recv( fd, buffer, BUFFER_SIZE, 0 );
753 //printf("read: %d\n", i );
754 if( i<=0 ) {
755 perror("read");
756 http_free( http_hdr );
757 return -1;
759 http_response_append( http_hdr, buffer, i );
760 } while( !http_is_header_entire( http_hdr ) );
761 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
762 http_hdr->buffer[http_hdr->buffer_size]='\0';
763 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
765 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
766 if( ret<0 ) {
767 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
768 http_free( http_hdr );
769 return -1;
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 http_free( http_hdr );
778 return -1;
781 if( asf_http_ctrl->request==1 ) {
782 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
783 // First request, we only got the ASF header.
784 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
785 if(ret < 0) return -1;
786 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
787 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
788 return -1;
790 asf_http_ctrl->request++;
791 done = 0;
792 } else {
793 done = 1;
796 break;
797 case ASF_Redirector_e:
798 if( http_hdr->body_size>0 ) {
799 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
800 http_free( http_hdr );
801 return -1;
804 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
805 done = 1;
806 break;
807 case ASF_Authenticate_e:
808 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
809 asf_http_ctrl->streaming_type = ASF_Unknown_e;
810 done = 0;
811 break;
812 case ASF_Unknown_e:
813 default:
814 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
815 closesocket(fd);
816 http_free( http_hdr );
817 return -1;
819 // Check if we got a redirect.
820 } while(!done);
822 stream->fd = fd;
823 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
824 stream->streaming_ctrl->streaming_read = nop_streaming_read;
825 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
826 } else {
827 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
828 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
829 stream->streaming_ctrl->buffering = 1;
831 stream->streaming_ctrl->status = streaming_playing_e;
832 stream->close = close_s;
834 http_free( http_hdr );
835 return 0;
838 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
839 URL_t *url;
841 stream->streaming_ctrl = streaming_ctrl_new();
842 if( stream->streaming_ctrl==NULL ) {
843 return STREAM_ERROR;
845 stream->streaming_ctrl->bandwidth = network_bandwidth;
846 url = url_new(stream->url);
847 stream->streaming_ctrl->url = check4proxies(url);
848 url_free(url);
850 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_MPDEMUX_ASF_InfoStreamASFURL, stream->url);
851 if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
852 streaming_ctrl_free(stream->streaming_ctrl);
853 stream->streaming_ctrl = NULL;
854 return STREAM_UNSUPORTED;
857 if(asf_streaming_start(stream, file_format) < 0) {
858 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_ASF_StreamingFailed);
859 streaming_ctrl_free(stream->streaming_ctrl);
860 stream->streaming_ctrl = NULL;
861 return STREAM_UNSUPORTED;
864 *file_format = DEMUXER_TYPE_ASF;
865 stream->type = STREAMTYPE_STREAM;
866 fixup_network_stream_cache(stream);
867 return STREAM_OK;
870 stream_info_t stream_info_asf = {
871 "mms and mms over http streaming",
872 "null",
873 "Bertrand, Reimar Doeffinger, Albeu",
874 "originally based on work by Majormms (is that code still there?)",
875 open_s,
876 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL},
877 NULL,
878 0 // Urls are an option string