Get rid of nonsensical limits on -geometry x, y,w and h values, they only
[mplayer/glamo.git] / stream / asf_streaming.c
blobc3e70a4aa2f3a503fb89d38150a5e5b6d1a9eee2
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 #if HAVE_WINSOCK2_H
13 #include <winsock2.h>
14 #endif
16 #include "url.h"
17 #include "http.h"
18 #include "libmpdemux/asf.h"
20 #include "stream.h"
21 #include "libmpdemux/demuxer.h"
23 #include "network.h"
24 #include "tcp.h"
26 #include "libavutil/intreadwrite.h"
28 #include "libmpdemux/asfguid.h"
30 extern int network_bandwidth;
32 int asf_mmst_streaming_start( stream_t *stream );
33 static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
35 static int asf_read_wrapper(int fd, void *buffer, int len, streaming_ctrl_t *stream_ctrl) {
36 uint8_t *buf = buffer;
37 while (len > 0) {
38 int got = nop_streaming_read(fd, buf, len, stream_ctrl);
39 if (got <= 0) {
40 mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
41 return got;
43 buf += got;
44 len -= got;
46 return 1;
49 // We can try several protocol for asf streaming
50 // * first the UDP protcol, if there is a firewall, UDP
51 // packets will not come back, so the mmsu will fail.
52 // * Then we can try TCP, but if there is a proxy for
53 // internet connection, the TCP connection will not get
54 // through
55 // * Then we can try HTTP.
56 //
57 // Note: Using WMP sequence MMSU then MMST and then HTTP.
59 static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
60 char *proto = stream->streaming_ctrl->url->protocol;
61 int fd = -1;
62 int port = stream->streaming_ctrl->url->port;
64 // Is protocol mms or mmsu?
65 if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
67 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
68 //fd = asf_mmsu_streaming_start( stream );
69 if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
70 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/UDP failed\n");
71 if( fd==-2 ) return -1;
74 //Is protocol mms or mmst?
75 if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
77 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
78 fd = asf_mmst_streaming_start( stream );
79 stream->streaming_ctrl->url->port = port;
80 if( fd>-1 ) return fd;
81 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n");
82 if( fd==-2 ) return -1;
85 //Is protocol http, http_proxy, or mms?
86 if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
87 !strcasecmp(proto, "mms") || !strcasecmp(proto, "mmsh") ||
88 !strcasecmp(proto, "mmshttp"))
90 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
91 fd = asf_http_streaming_start( stream, demuxer_type );
92 stream->streaming_ctrl->url->port = port;
93 if( fd>-1 ) return fd;
94 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
95 if( fd==-2 ) return -1;
98 //everything failed
99 return -1;
102 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
104 printf("ASF stream chunck size=%d\n", stream_chunck->size);
105 printf("length: %d\n", length );
106 printf("0x%02X\n", stream_chunck->type );
108 if( drop_packet!=NULL ) *drop_packet = 0;
110 if( stream_chunck->size<8 ) {
111 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
112 return -1;
114 if( stream_chunck->size!=stream_chunck->size_confirm ) {
115 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, stream_chunck->size, stream_chunck->size_confirm);
116 return -1;
119 printf(" type: 0x%02X\n", stream_chunck->type );
120 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
121 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
122 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
123 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
125 switch(stream_chunck->type) {
126 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
127 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
128 if( drop_packet!=NULL ) *drop_packet = 1;
129 return stream_chunck->size;
130 break;
131 case ASF_STREAMING_DATA: // $D Data follows
132 // printf("=====> Data follows\n");
133 break;
134 case ASF_STREAMING_END_TRANS: // $E Transfer complete
135 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
136 if( drop_packet!=NULL ) *drop_packet = 1;
137 return stream_chunck->size;
138 break;
139 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
140 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
141 break;
142 default:
143 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
145 return stream_chunck->size+4;
148 extern int audio_id;
149 extern int video_id;
151 static void close_s(stream_t *stream) {
152 closesocket(stream->fd);
153 stream->fd=-1;
156 static int max_idx(int s_count, int *s_rates, int bound) {
157 int i, best = -1, rate = -1;
158 for (i = 0; i < s_count; i++) {
159 if (s_rates[i] > rate && s_rates[i] <= bound) {
160 rate = s_rates[i];
161 best = i;
164 return best;
167 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
168 ASF_stream_chunck_t chunk;
169 asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
170 char* buffer=NULL, *chunk_buffer=NULL;
171 int i,r,size,pos = 0;
172 int start;
173 int buffer_size = 0;
174 int chunk_size2read = 0;
175 int bw = streaming_ctrl->bandwidth;
176 int *v_rates = NULL, *a_rates = NULL;
177 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
179 if(asf_ctrl == NULL) return -1;
181 // The ASF header can be in several network chunks. For example if the content description
182 // is big, the ASF header will be split in 2 network chunk.
183 // So we need to retrieve all the chunk before starting to parse the header.
184 do {
185 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
186 return -1;
187 // Endian handling of the stream chunk
188 le2me_ASF_stream_chunck_t(&chunk);
189 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
190 if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
191 if(size < 0){
192 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
193 return -1;
195 if (chunk.type != ASF_STREAMING_HEADER) {
196 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
197 return -1;
200 // audit: do not overflow buffer_size
201 if (size > SIZE_MAX - buffer_size) return -1;
202 buffer = malloc(size+buffer_size);
203 if(buffer == NULL) {
204 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
205 return -1;
207 if( chunk_buffer!=NULL ) {
208 memcpy( buffer, chunk_buffer, buffer_size );
209 free( chunk_buffer );
211 chunk_buffer = buffer;
212 buffer += buffer_size;
213 buffer_size += size;
215 if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
216 return -1;
218 if( chunk_size2read==0 ) {
219 ASF_header_t *asfh = (ASF_header_t *)buffer;
220 if(size < (int)sizeof(ASF_header_t)) {
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 chunk_size2read = AV_RL64(&asfh->objh.size);
225 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
227 } while( buffer_size<chunk_size2read);
228 buffer = chunk_buffer;
229 size = buffer_size;
231 start = sizeof(ASF_header_t);
233 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
234 if (pos >= 0) {
235 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
236 pos += sizeof(ASF_file_header_t);
237 if (pos > size) goto len_err_out;
239 if(fileh.packetsize != fileh.packetsize2) {
240 printf("Error packetsize check don't match\n");
241 return -1;
244 asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
245 // before playing.
246 // preroll: time in ms to bufferize before playing
247 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
250 pos = start;
251 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
253 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
254 pos += sizeof(ASF_stream_header_t);
255 if (pos > size) goto len_err_out;
256 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
257 case 0xF8699E40 : // audio stream
258 if(asf_ctrl->audio_streams == NULL){
259 asf_ctrl->audio_streams = malloc(sizeof(int));
260 asf_ctrl->n_audio = 1;
261 } else {
262 asf_ctrl->n_audio++;
263 asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
264 asf_ctrl->n_audio*sizeof(int));
266 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
267 break;
268 case 0xBC19EFC0 : // video stream
269 if(asf_ctrl->video_streams == NULL){
270 asf_ctrl->video_streams = malloc(sizeof(int));
271 asf_ctrl->n_video = 1;
272 } else {
273 asf_ctrl->n_video++;
274 asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
275 asf_ctrl->n_video*sizeof(int));
277 asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
278 break;
282 // always allocate to avoid lots of ifs later
283 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
284 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
286 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
287 if (pos >= 0) {
288 // stream bitrate properties object
289 int stream_count;
290 char *ptr = &buffer[pos];
291 char *end = &buffer[size];
293 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
294 if (ptr + 2 > end) goto len_err_out;
295 stream_count = AV_RL16(ptr);
296 ptr += 2;
297 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
298 stream_count, stream_count );
299 for( i=0 ; i<stream_count ; i++ ) {
300 uint32_t rate;
301 int id;
302 int j;
303 if (ptr + 6 > end) goto len_err_out;
304 id = AV_RL16(ptr);
305 ptr += 2;
306 rate = AV_RL32(ptr);
307 ptr += 4;
308 mp_msg(MSGT_NETWORK, MSGL_V,
309 " stream id=[0x%x][%u]\n", id, id);
310 mp_msg(MSGT_NETWORK, MSGL_V,
311 " max bitrate=[0x%x][%u]\n", rate, rate);
312 for (j = 0; j < asf_ctrl->n_video; j++) {
313 if (id == asf_ctrl->video_streams[j]) {
314 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
315 v_rates[j] = rate;
316 break;
319 for (j = 0; j < asf_ctrl->n_audio; j++) {
320 if (id == asf_ctrl->audio_streams[j]) {
321 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
322 a_rates[j] = rate;
323 break;
328 free(buffer);
330 // automatic stream selection based on bandwidth
331 if (bw == 0) bw = INT_MAX;
332 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
334 if (asf_ctrl->n_audio) {
335 // find lowest-bitrate audio stream
336 a_rate = a_rates[0];
337 a_idx = 0;
338 for (i = 0; i < asf_ctrl->n_audio; i++) {
339 if (a_rates[i] < a_rate) {
340 a_rate = a_rates[i];
341 a_idx = i;
344 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
345 // both audio and video are not possible, try video only next
346 a_idx = -1;
347 a_rate = 0;
350 // find best video stream
351 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
352 if (v_idx >= 0)
353 v_rate = v_rates[v_idx];
355 // find best audio stream
356 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
358 free(v_rates);
359 free(a_rates);
361 if (a_idx < 0 && v_idx < 0) {
362 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
363 return -1;
366 if (audio_id > 0)
367 // a audio stream was forced
368 asf_ctrl->audio_id = audio_id;
369 else if (a_idx >= 0)
370 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
371 else if (asf_ctrl->n_audio) {
372 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
373 audio_id = -2;
376 if (video_id > 0)
377 // a video stream was forced
378 asf_ctrl->video_id = video_id;
379 else if (v_idx >= 0)
380 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
381 else if (asf_ctrl->n_video) {
382 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
383 video_id = -2;
386 return 1;
388 len_err_out:
389 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
390 if (buffer) free(buffer);
391 if (v_rates) free(v_rates);
392 if (a_rates) free(a_rates);
393 return -1;
396 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
397 static ASF_stream_chunck_t chunk;
398 int read,chunk_size = 0;
399 static int rest = 0, drop_chunk = 0, waiting = 0;
400 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
402 while(1) {
403 if (rest == 0 && waiting == 0) {
404 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
405 return -1;
407 // Endian handling of the stream chunk
408 le2me_ASF_stream_chunck_t(&chunk);
409 chunk_size = asf_streaming( &chunk, &drop_chunk );
410 if(chunk_size < 0) {
411 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
412 return -1;
414 chunk_size -= sizeof(ASF_stream_chunck_t);
416 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
417 if (asf_http_ctrl->packet_size < chunk_size) {
418 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
419 return -1;
421 waiting = asf_http_ctrl->packet_size;
422 } else {
423 waiting = chunk_size;
426 } else if (rest){
427 chunk_size = rest;
428 rest = 0;
431 read = 0;
432 if ( waiting >= chunk_size) {
433 if (chunk_size > size){
434 rest = chunk_size - size;
435 chunk_size = size;
437 if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
438 return -1;
439 read = chunk_size;
440 waiting -= read;
441 if (drop_chunk) continue;
443 if (rest == 0 && waiting > 0 && size-read > 0) {
444 int s = FFMIN(waiting,size-read);
445 memset(buffer+read,0,s);
446 waiting -= s;
447 read += s;
449 break;
452 return read;
455 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
456 return -1;
457 // to shut up gcc warning
458 fd++;
459 pos++;
460 streaming_ctrl=NULL;
463 static int asf_header_check( HTTP_header_t *http_hdr ) {
464 ASF_obj_header_t *objh;
465 if( http_hdr==NULL ) return -1;
466 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
468 objh = (ASF_obj_header_t*)http_hdr->body;
469 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
470 return -1;
473 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
474 if( content_type==NULL ) return ASF_Unknown_e;
475 if( !strcasecmp(content_type, "application/octet-stream") ||
476 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
477 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
478 !strcasecmp(content_type, "video/x-ms-asf")) {
480 if( strstr(features, "broadcast") ) {
481 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
482 return ASF_Live_e;
483 } else {
484 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
485 return ASF_Prerecorded_e;
487 } else {
488 // Ok in a perfect world, web servers should be well configured
489 // so we could used mime type to know the stream type,
490 // but guess what? All of them are not well configured.
491 // So we have to check for an asf header :(, but it works :p
492 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
493 if( asf_header_check( http_hdr )==0 ) {
494 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
495 return ASF_PlainText_e;
496 } else if( (!strcasecmp(content_type, "text/html")) ) {
497 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
498 return ASF_Unknown_e;
499 } else {
500 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
501 return ASF_Redirector_e;
503 } else {
504 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
505 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
506 (!strcasecmp(content_type, "video/x-ms-asf")) ||
507 (!strcasecmp(content_type, "video/x-ms-afs")) ||
508 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
509 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
510 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
511 return ASF_Redirector_e;
512 } else if( !strcasecmp(content_type, "text/plain") ) {
513 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
514 return ASF_PlainText_e;
515 } else {
516 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
517 return ASF_Unknown_e;
521 return ASF_Unknown_e;
524 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
525 HTTP_header_t *http_hdr;
526 URL_t *url = NULL;
527 URL_t *server_url = NULL;
528 asf_http_streaming_ctrl_t *asf_http_ctrl;
529 char str[250];
530 char *ptr;
531 int i, enable;
533 int offset_hi=0, offset_lo=0, length=0;
534 int asf_nb_stream=0, stream_id;
536 // Sanity check
537 if( streaming_ctrl==NULL ) return NULL;
538 url = streaming_ctrl->url;
539 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
540 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
542 // Common header for all requests.
543 http_hdr = http_new_header();
544 http_set_field( http_hdr, "Accept: */*" );
545 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
546 http_add_basic_authentication( http_hdr, url->username, url->password );
548 // Check if we are using a proxy
549 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
550 server_url = url_new( (url->file)+1 );
551 if( server_url==NULL ) {
552 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
553 http_free( http_hdr );
554 return NULL;
556 http_set_uri( http_hdr, server_url->url );
557 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
558 url_free( server_url );
559 } else {
560 http_set_uri( http_hdr, url->file );
561 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
564 http_set_field( http_hdr, str );
565 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
566 sprintf(str,
567 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
568 offset_hi, offset_lo, asf_http_ctrl->request, length );
569 http_set_field( http_hdr, str );
571 switch( asf_http_ctrl->streaming_type ) {
572 case ASF_Live_e:
573 case ASF_Prerecorded_e:
574 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
575 ptr = str;
576 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
577 if(asf_http_ctrl->n_audio > 0) {
578 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
579 stream_id = asf_http_ctrl->audio_streams[i];
580 if(stream_id == asf_http_ctrl->audio_id) {
581 enable = 0;
582 } else {
583 enable = 2;
584 continue;
586 asf_nb_stream++;
587 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
590 if(asf_http_ctrl->n_video > 0) {
591 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
592 stream_id = asf_http_ctrl->video_streams[i];
593 if(stream_id == asf_http_ctrl->video_id) {
594 enable = 0;
595 } else {
596 enable = 2;
597 continue;
599 asf_nb_stream++;
600 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
603 http_set_field( http_hdr, str );
604 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
605 http_set_field( http_hdr, str );
606 break;
607 case ASF_Redirector_e:
608 break;
609 case ASF_Unknown_e:
610 // First request goes here.
611 break;
612 default:
613 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
616 http_set_field( http_hdr, "Connection: Close" );
617 http_build_request( http_hdr );
619 return http_hdr;
622 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
623 char *content_type, *pragma;
624 char features[64] = "\0";
625 size_t len;
626 if( http_response_parse(http_hdr)<0 ) {
627 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
628 return -1;
630 switch( http_hdr->status_code ) {
631 case 200:
632 break;
633 case 401: // Authentication required
634 return ASF_Authenticate_e;
635 default:
636 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
637 return -1;
640 content_type = http_get_field( http_hdr, "Content-Type");
641 //printf("Content-Type: [%s]\n", content_type);
643 pragma = http_get_field( http_hdr, "Pragma");
644 while( pragma!=NULL ) {
645 char *comma_ptr=NULL;
646 char *end;
647 //printf("Pragma: [%s]\n", pragma );
648 // The pragma line can get severals attributes
649 // separeted with a comma ','.
650 do {
651 if( !strncasecmp( pragma, "features=", 9) ) {
652 pragma += 9;
653 end = strstr( pragma, "," );
654 if( end==NULL ) {
655 len = strlen(pragma);
656 } else {
657 len = (unsigned int)(end-pragma);
659 if(len > sizeof(features) - 1) {
660 mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
661 len = sizeof(features) - 1;
663 strncpy( features, pragma, len );
664 features[len]='\0';
665 break;
667 comma_ptr = strstr( pragma, "," );
668 if( comma_ptr!=NULL ) {
669 pragma = comma_ptr+1;
670 if( pragma[0]==' ' ) pragma++;
672 } while( comma_ptr!=NULL );
673 pragma = http_get_next_field( http_hdr );
675 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
676 return 0;
679 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
680 HTTP_header_t *http_hdr=NULL;
681 URL_t *url = stream->streaming_ctrl->url;
682 asf_http_streaming_ctrl_t *asf_http_ctrl;
683 char buffer[BUFFER_SIZE];
684 int i, ret;
685 int fd = stream->fd;
686 int done;
687 int auth_retry = 0;
689 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
690 if( asf_http_ctrl==NULL ) {
691 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
692 return -1;
694 asf_http_ctrl->streaming_type = ASF_Unknown_e;
695 asf_http_ctrl->request = 1;
696 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
697 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
698 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
700 do {
701 done = 1;
702 if( fd>0 ) closesocket( fd );
704 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
705 if( url->port==0 ) url->port = 8080;
706 } else {
707 if( url->port==0 ) url->port = 80;
709 fd = connect2Server( url->hostname, url->port, 1);
710 if( fd<0 ) return fd;
712 http_hdr = asf_http_request( stream->streaming_ctrl );
713 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
714 for(i=0; i < (int)http_hdr->buffer_size ; ) {
715 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
716 if(r <0) {
717 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
718 goto err_out;
720 i += r;
722 http_free( http_hdr );
723 http_hdr = http_new_header();
724 do {
725 i = recv( fd, buffer, BUFFER_SIZE, 0 );
726 //printf("read: %d\n", i );
727 if( i<=0 ) {
728 perror("read");
729 goto err_out;
731 http_response_append( http_hdr, buffer, i );
732 } while( !http_is_header_entire( http_hdr ) );
733 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
734 http_hdr->buffer[http_hdr->buffer_size]='\0';
735 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
737 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
738 if( ret<0 ) {
739 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
740 goto err_out;
742 switch( asf_http_ctrl->streaming_type ) {
743 case ASF_Live_e:
744 case ASF_Prerecorded_e:
745 case ASF_PlainText_e:
746 if( http_hdr->body_size>0 ) {
747 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
748 goto err_out;
751 if( asf_http_ctrl->request==1 ) {
752 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
753 // First request, we only got the ASF header.
754 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
755 if(ret < 0) goto err_out;
756 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
757 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
758 goto err_out;
760 asf_http_ctrl->request++;
761 done = 0;
762 } else {
763 done = 1;
766 break;
767 case ASF_Redirector_e:
768 if( http_hdr->body_size>0 ) {
769 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
770 goto err_out;
773 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
774 done = 1;
775 break;
776 case ASF_Authenticate_e:
777 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
778 asf_http_ctrl->streaming_type = ASF_Unknown_e;
779 done = 0;
780 break;
781 case ASF_Unknown_e:
782 default:
783 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
784 goto err_out;
786 // Check if we got a redirect.
787 } while(!done);
789 stream->fd = fd;
790 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
791 stream->streaming_ctrl->streaming_read = nop_streaming_read;
792 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
793 } else {
794 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
795 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
796 stream->streaming_ctrl->buffering = 1;
798 stream->streaming_ctrl->status = streaming_playing_e;
799 stream->close = close_s;
801 http_free( http_hdr );
802 return 0;
804 err_out:
805 if (fd > 0)
806 closesocket(fd);
807 stream->fd = -1;
808 http_free(http_hdr);
809 return -1;
812 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
813 URL_t *url;
815 stream->streaming_ctrl = streaming_ctrl_new();
816 if( stream->streaming_ctrl==NULL ) {
817 return STREAM_ERROR;
819 stream->streaming_ctrl->bandwidth = network_bandwidth;
820 url = url_new(stream->url);
821 stream->streaming_ctrl->url = check4proxies(url);
822 url_free(url);
824 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_MPDEMUX_ASF_InfoStreamASFURL, stream->url);
825 if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
826 streaming_ctrl_free(stream->streaming_ctrl);
827 stream->streaming_ctrl = NULL;
828 return STREAM_UNSUPPORTED;
831 if(asf_streaming_start(stream, file_format) < 0) {
832 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_ASF_StreamingFailed);
833 streaming_ctrl_free(stream->streaming_ctrl);
834 stream->streaming_ctrl = NULL;
835 return STREAM_UNSUPPORTED;
838 if (*file_format != DEMUXER_TYPE_PLAYLIST)
839 *file_format = DEMUXER_TYPE_ASF;
840 stream->type = STREAMTYPE_STREAM;
841 fixup_network_stream_cache(stream);
842 return STREAM_OK;
845 const stream_info_t stream_info_asf = {
846 "mms and mms over http streaming",
847 "null",
848 "Bertrand, Reimar Doeffinger, Albeu",
849 "originally based on work by Majormms (is that code still there?)",
850 open_s,
851 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL},
852 NULL,
853 0 // Urls are an option string