Replace libavutil internal header #includes with MPlayer copies
[mplayer.git] / stream / asf_streaming.c
blob77c0b1ea09f06a587275d6fdb43a5fb2aa0a6ce5
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"
11 #include "options.h"
13 #if HAVE_WINSOCK2_H
14 #include <winsock2.h>
15 #endif
17 #include "url.h"
18 #include "http.h"
19 #include "libmpdemux/asf.h"
21 #include "stream.h"
22 #include "libmpdemux/demuxer.h"
24 #include "network.h"
25 #include "tcp.h"
27 #include "ffmpeg_files/intreadwrite.h"
29 #include "libmpdemux/asfguid.h"
31 extern int network_bandwidth;
33 int asf_mmst_streaming_start( stream_t *stream );
34 static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
36 static int asf_read_wrapper(int fd, void *buffer, int len, streaming_ctrl_t *stream_ctrl) {
37 uint8_t *buf = buffer;
38 while (len > 0) {
39 int got = nop_streaming_read(fd, buf, len, stream_ctrl);
40 if (got <= 0) {
41 mp_tmsg(MSGT_NETWORK, MSGL_ERR, "Error while reading network stream.\n");
42 return got;
44 buf += got;
45 len -= got;
47 return 1;
50 // We can try several protocol for asf streaming
51 // * first the UDP protcol, if there is a firewall, UDP
52 // packets will not come back, so the mmsu will fail.
53 // * Then we can try TCP, but if there is a proxy for
54 // internet connection, the TCP connection will not get
55 // through
56 // * Then we can try HTTP.
58 // Note: Using WMP sequence MMSU then MMST and then HTTP.
60 static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
61 char *proto = stream->streaming_ctrl->url->protocol;
62 int fd = -1;
63 int port = stream->streaming_ctrl->url->port;
65 // Is protocol mms or mmsu?
66 if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
68 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
69 //fd = asf_mmsu_streaming_start( stream );
70 if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
71 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/UDP failed\n");
72 if( fd==-2 ) return -1;
75 //Is protocol mms or mmst?
76 if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
78 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
79 fd = asf_mmst_streaming_start( stream );
80 stream->streaming_ctrl->url->port = port;
81 if( fd>-1 ) return fd;
82 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n");
83 if( fd==-2 ) return -1;
86 //Is protocol http, http_proxy, or mms?
87 if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
88 !strcasecmp(proto, "mms") || !strcasecmp(proto, "mmsh") ||
89 !strcasecmp(proto, "mmshttp"))
91 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
92 fd = asf_http_streaming_start( stream, demuxer_type );
93 stream->streaming_ctrl->url->port = port;
94 if( fd>-1 ) return fd;
95 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
96 if( fd==-2 ) return -1;
99 //everything failed
100 return -1;
103 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
105 printf("ASF stream chunck size=%d\n", stream_chunck->size);
106 printf("length: %d\n", length );
107 printf("0x%02X\n", stream_chunck->type );
109 if( drop_packet!=NULL ) *drop_packet = 0;
111 if( stream_chunck->size<8 ) {
112 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Ahhhh, stream_chunck size is too small: %d\n", stream_chunck->size);
113 return -1;
115 if( stream_chunck->size!=stream_chunck->size_confirm ) {
116 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"size_confirm mismatch!: %d %d\n", stream_chunck->size, stream_chunck->size_confirm);
117 return -1;
120 printf(" type: 0x%02X\n", stream_chunck->type );
121 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
122 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
123 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
124 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
126 switch(stream_chunck->type) {
127 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
128 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
129 if( drop_packet!=NULL ) *drop_packet = 1;
130 return stream_chunck->size;
131 break;
132 case ASF_STREAMING_DATA: // $D Data follows
133 // printf("=====> Data follows\n");
134 break;
135 case ASF_STREAMING_END_TRANS: // $E Transfer complete
136 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
137 if( drop_packet!=NULL ) *drop_packet = 1;
138 return stream_chunck->size;
139 break;
140 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
141 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
142 break;
143 default:
144 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
146 return stream_chunck->size+4;
149 static void close_s(stream_t *stream) {
150 closesocket(stream->fd);
151 stream->fd=-1;
154 static int max_idx(int s_count, int *s_rates, int bound) {
155 int i, best = -1, rate = -1;
156 for (i = 0; i < s_count; i++) {
157 if (s_rates[i] > rate && s_rates[i] <= bound) {
158 rate = s_rates[i];
159 best = i;
162 return best;
165 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
166 ASF_stream_chunck_t chunk;
167 asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
168 char* buffer=NULL, *chunk_buffer=NULL;
169 int i,r,size,pos = 0;
170 int start;
171 int buffer_size = 0;
172 int chunk_size2read = 0;
173 int bw = streaming_ctrl->bandwidth;
174 int *v_rates = NULL, *a_rates = NULL;
175 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
177 if(asf_ctrl == NULL) return -1;
179 // The ASF header can be in several network chunks. For example if the content description
180 // is big, the ASF header will be split in 2 network chunk.
181 // So we need to retrieve all the chunk before starting to parse the header.
182 do {
183 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
184 return -1;
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_tmsg(MSGT_NETWORK,MSGL_WARN,"Warning: drop header ????\n");
189 if(size < 0){
190 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while parsing chunk header\n");
191 return -1;
193 if (chunk.type != ASF_STREAMING_HEADER) {
194 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Didn't get a header as first chunk !!!!\n");
195 return -1;
198 // audit: do not overflow buffer_size
199 if (size > SIZE_MAX - buffer_size) return -1;
200 buffer = malloc(size+buffer_size);
201 if(buffer == NULL) {
202 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Error: Can't allocate %d bytes buffer.\n",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 if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
214 return -1;
216 if( chunk_size2read==0 ) {
217 ASF_header_t *asfh = (ASF_header_t *)buffer;
218 if(size < (int)sizeof(ASF_header_t)) {
219 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error: Chunk is too small.\n");
220 return -1;
221 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
222 chunk_size2read = AV_RL64(&asfh->objh.size);
223 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
225 } while( buffer_size<chunk_size2read);
226 buffer = chunk_buffer;
227 size = buffer_size;
229 start = sizeof(ASF_header_t);
231 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
232 if (pos >= 0) {
233 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
234 pos += sizeof(ASF_file_header_t);
235 if (pos > size) goto len_err_out;
237 if(fileh.packetsize != fileh.packetsize2) {
238 printf("Error packetsize check don't match\n");
239 return -1;
242 asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
243 // before playing.
244 // preroll: time in ms to bufferize before playing
245 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
248 pos = start;
249 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
251 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
252 pos += sizeof(ASF_stream_header_t);
253 if (pos > size) goto len_err_out;
254 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
255 case 0xF8699E40 : // audio stream
256 if(asf_ctrl->audio_streams == NULL){
257 asf_ctrl->audio_streams = malloc(sizeof(int));
258 asf_ctrl->n_audio = 1;
259 } else {
260 asf_ctrl->n_audio++;
261 asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
262 asf_ctrl->n_audio*sizeof(int));
264 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
265 break;
266 case 0xBC19EFC0 : // video stream
267 if(asf_ctrl->video_streams == NULL){
268 asf_ctrl->video_streams = malloc(sizeof(int));
269 asf_ctrl->n_video = 1;
270 } else {
271 asf_ctrl->n_video++;
272 asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
273 asf_ctrl->n_video*sizeof(int));
275 asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
276 break;
280 // always allocate to avoid lots of ifs later
281 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
282 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
284 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
285 if (pos >= 0) {
286 // stream bitrate properties object
287 int stream_count;
288 char *ptr = &buffer[pos];
289 char *end = &buffer[size];
291 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
292 if (ptr + 2 > end) goto len_err_out;
293 stream_count = AV_RL16(ptr);
294 ptr += 2;
295 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
296 stream_count, stream_count );
297 for( i=0 ; i<stream_count ; i++ ) {
298 uint32_t rate;
299 int id;
300 int j;
301 if (ptr + 6 > end) goto len_err_out;
302 id = AV_RL16(ptr);
303 ptr += 2;
304 rate = AV_RL32(ptr);
305 ptr += 4;
306 mp_msg(MSGT_NETWORK, MSGL_V,
307 " stream id=[0x%x][%u]\n", id, id);
308 mp_msg(MSGT_NETWORK, MSGL_V,
309 " max bitrate=[0x%x][%u]\n", rate, rate);
310 for (j = 0; j < asf_ctrl->n_video; j++) {
311 if (id == asf_ctrl->video_streams[j]) {
312 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
313 v_rates[j] = rate;
314 break;
317 for (j = 0; j < asf_ctrl->n_audio; j++) {
318 if (id == asf_ctrl->audio_streams[j]) {
319 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
320 a_rates[j] = rate;
321 break;
326 free(buffer);
328 // automatic stream selection based on bandwidth
329 if (bw == 0) bw = INT_MAX;
330 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
332 if (asf_ctrl->n_audio) {
333 // find lowest-bitrate audio stream
334 a_rate = a_rates[0];
335 a_idx = 0;
336 for (i = 0; i < asf_ctrl->n_audio; i++) {
337 if (a_rates[i] < a_rate) {
338 a_rate = a_rates[i];
339 a_idx = i;
342 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
343 // both audio and video are not possible, try video only next
344 a_idx = -1;
345 a_rate = 0;
348 // find best video stream
349 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
350 if (v_idx >= 0)
351 v_rate = v_rates[v_idx];
353 // find best audio stream
354 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
356 free(v_rates);
357 free(a_rates);
359 if (a_idx < 0 && v_idx < 0) {
360 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Bandwidth too small, file cannot be played!\n");
361 return -1;
364 if (*streaming_ctrl->audio_id_ptr > 0)
365 // a audio stream was forced
366 asf_ctrl->audio_id = *streaming_ctrl->audio_id_ptr;
367 else if (a_idx >= 0)
368 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
369 else if (asf_ctrl->n_audio) {
370 mp_tmsg(MSGT_NETWORK, MSGL_WARN, "Bandwidth too small, deselected audio stream.\n");
371 *streaming_ctrl->audio_id_ptr = -2;
374 if (*streaming_ctrl->video_id_ptr > 0)
375 // a video stream was forced
376 asf_ctrl->video_id = *streaming_ctrl->video_id_ptr;
377 else if (v_idx >= 0)
378 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
379 else if (asf_ctrl->n_video) {
380 mp_tmsg(MSGT_NETWORK, MSGL_WARN, "Bandwidth too small, deselected video stream.\n");
381 *streaming_ctrl->video_id_ptr = -2;
384 return 1;
386 len_err_out:
387 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Invalid length in ASF header!\n");
388 if (buffer) free(buffer);
389 if (v_rates) free(v_rates);
390 if (a_rates) free(a_rates);
391 return -1;
394 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
395 static ASF_stream_chunck_t chunk;
396 int read,chunk_size = 0;
397 static int rest = 0, drop_chunk = 0, waiting = 0;
398 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
400 while(1) {
401 if (rest == 0 && waiting == 0) {
402 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
403 return -1;
405 // Endian handling of the stream chunk
406 le2me_ASF_stream_chunck_t(&chunk);
407 chunk_size = asf_streaming( &chunk, &drop_chunk );
408 if(chunk_size < 0) {
409 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while parsing chunk header\n");
410 return -1;
412 chunk_size -= sizeof(ASF_stream_chunck_t);
414 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
415 if (asf_http_ctrl->packet_size < chunk_size) {
416 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error: chunk_size > packet_size\n");
417 return -1;
419 waiting = asf_http_ctrl->packet_size;
420 } else {
421 waiting = chunk_size;
424 } else if (rest){
425 chunk_size = rest;
426 rest = 0;
429 read = 0;
430 if ( waiting >= chunk_size) {
431 if (chunk_size > size){
432 rest = chunk_size - size;
433 chunk_size = size;
435 if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
436 return -1;
437 read = chunk_size;
438 waiting -= read;
439 if (drop_chunk) continue;
441 if (rest == 0 && waiting > 0 && size-read > 0) {
442 int s = FFMIN(waiting,size-read);
443 memset(buffer+read,0,s);
444 waiting -= s;
445 read += s;
447 break;
450 return read;
453 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
454 return -1;
455 // to shut up gcc warning
456 fd++;
457 pos++;
458 streaming_ctrl=NULL;
461 static int asf_header_check( HTTP_header_t *http_hdr ) {
462 ASF_obj_header_t *objh;
463 if( http_hdr==NULL ) return -1;
464 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
466 objh = (ASF_obj_header_t*)http_hdr->body;
467 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
468 return -1;
471 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
472 if( content_type==NULL ) return ASF_Unknown_e;
473 if( !strcasecmp(content_type, "application/octet-stream") ||
474 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
475 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
476 !strcasecmp(content_type, "video/x-ms-asf")) {
478 if( strstr(features, "broadcast") ) {
479 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
480 return ASF_Live_e;
481 } else {
482 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
483 return ASF_Prerecorded_e;
485 } else {
486 // Ok in a perfect world, web servers should be well configured
487 // so we could used mime type to know the stream type,
488 // but guess what? All of them are not well configured.
489 // So we have to check for an asf header :(, but it works :p
490 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
491 if( asf_header_check( http_hdr )==0 ) {
492 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
493 return ASF_PlainText_e;
494 } else if( (!strcasecmp(content_type, "text/html")) ) {
495 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
496 return ASF_Unknown_e;
497 } else {
498 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
499 return ASF_Redirector_e;
501 } else {
502 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
503 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
504 (!strcasecmp(content_type, "video/x-ms-asf")) ||
505 (!strcasecmp(content_type, "video/x-ms-afs")) ||
506 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
507 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
508 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"=====> ASF Redirector\n");
509 return ASF_Redirector_e;
510 } else if( !strcasecmp(content_type, "text/plain") ) {
511 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
512 return ASF_PlainText_e;
513 } else {
514 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
515 return ASF_Unknown_e;
519 return ASF_Unknown_e;
522 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
523 HTTP_header_t *http_hdr;
524 URL_t *url = NULL;
525 URL_t *server_url = NULL;
526 asf_http_streaming_ctrl_t *asf_http_ctrl;
527 char str[250];
528 char *ptr;
529 int i, enable;
531 int offset_hi=0, offset_lo=0, length=0;
532 int asf_nb_stream=0, stream_id;
534 // Sanity check
535 if( streaming_ctrl==NULL ) return NULL;
536 url = streaming_ctrl->url;
537 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
538 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
540 // Common header for all requests.
541 http_hdr = http_new_header();
542 http_set_field( http_hdr, "Accept: */*" );
543 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
544 http_add_basic_authentication( http_hdr, url->username, url->password );
546 // Check if we are using a proxy
547 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
548 server_url = url_new( (url->file)+1 );
549 if( server_url==NULL ) {
550 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"invalid proxy URL\n");
551 http_free( http_hdr );
552 return NULL;
554 http_set_uri( http_hdr, server_url->url );
555 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
556 url_free( server_url );
557 } else {
558 http_set_uri( http_hdr, url->file );
559 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
562 http_set_field( http_hdr, str );
563 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
564 sprintf(str,
565 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
566 offset_hi, offset_lo, asf_http_ctrl->request, length );
567 http_set_field( http_hdr, str );
569 switch( asf_http_ctrl->streaming_type ) {
570 case ASF_Live_e:
571 case ASF_Prerecorded_e:
572 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
573 ptr = str;
574 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
575 if(asf_http_ctrl->n_audio > 0) {
576 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
577 stream_id = asf_http_ctrl->audio_streams[i];
578 if(stream_id == asf_http_ctrl->audio_id) {
579 enable = 0;
580 } else {
581 enable = 2;
582 continue;
584 asf_nb_stream++;
585 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
588 if(asf_http_ctrl->n_video > 0) {
589 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
590 stream_id = asf_http_ctrl->video_streams[i];
591 if(stream_id == asf_http_ctrl->video_id) {
592 enable = 0;
593 } else {
594 enable = 2;
595 continue;
597 asf_nb_stream++;
598 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
601 http_set_field( http_hdr, str );
602 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
603 http_set_field( http_hdr, str );
604 break;
605 case ASF_Redirector_e:
606 break;
607 case ASF_Unknown_e:
608 // First request goes here.
609 break;
610 default:
611 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown ASF stream type\n");
614 http_set_field( http_hdr, "Connection: Close" );
615 http_build_request( http_hdr );
617 return http_hdr;
620 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
621 char *content_type, *pragma;
622 char features[64] = "\0";
623 size_t len;
624 if( http_response_parse(http_hdr)<0 ) {
625 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to parse HTTP response.\n");
626 return -1;
628 switch( http_hdr->status_code ) {
629 case 200:
630 break;
631 case 401: // Authentication required
632 return ASF_Authenticate_e;
633 default:
634 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returned %d:%s\n", http_hdr->status_code, http_hdr->reason_phrase);
635 return -1;
638 content_type = http_get_field( http_hdr, "Content-Type");
639 //printf("Content-Type: [%s]\n", content_type);
641 pragma = http_get_field( http_hdr, "Pragma");
642 while( pragma!=NULL ) {
643 char *comma_ptr=NULL;
644 char *end;
645 //printf("Pragma: [%s]\n", pragma );
646 // The pragma line can get severals attributes
647 // separeted with a comma ','.
648 do {
649 if( !strncasecmp( pragma, "features=", 9) ) {
650 pragma += 9;
651 end = strstr( pragma, "," );
652 if( end==NULL ) {
653 len = strlen(pragma);
654 } else {
655 len = (unsigned int)(end-pragma);
657 if(len > sizeof(features) - 1) {
658 mp_tmsg(MSGT_NETWORK,MSGL_WARN,"ASF HTTP PARSE WARNING : Pragma %s cut from %d bytes to %d\n",pragma,len,sizeof(features) - 1);
659 len = sizeof(features) - 1;
661 strncpy( features, pragma, len );
662 features[len]='\0';
663 break;
665 comma_ptr = strstr( pragma, "," );
666 if( comma_ptr!=NULL ) {
667 pragma = comma_ptr+1;
668 if( pragma[0]==' ' ) pragma++;
670 } while( comma_ptr!=NULL );
671 pragma = http_get_next_field( http_hdr );
673 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
674 return 0;
677 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
678 HTTP_header_t *http_hdr=NULL;
679 URL_t *url = stream->streaming_ctrl->url;
680 asf_http_streaming_ctrl_t *asf_http_ctrl;
681 char buffer[BUFFER_SIZE];
682 int i, ret;
683 int fd = stream->fd;
684 int done;
685 int auth_retry = 0;
687 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
688 if( asf_http_ctrl==NULL ) {
689 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
690 return -1;
692 asf_http_ctrl->streaming_type = ASF_Unknown_e;
693 asf_http_ctrl->request = 1;
694 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
695 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
696 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
698 do {
699 done = 1;
700 if( fd>0 ) closesocket( fd );
702 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
703 if( url->port==0 ) url->port = 8080;
704 } else {
705 if( url->port==0 ) url->port = 80;
707 fd = connect2Server( url->hostname, url->port, 1);
708 if( fd<0 ) return fd;
710 http_hdr = asf_http_request( stream->streaming_ctrl );
711 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
712 for(i=0; i < (int)http_hdr->buffer_size ; ) {
713 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
714 if(r <0) {
715 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"socket write error: %s\n",strerror(errno));
716 goto err_out;
718 i += r;
720 http_free( http_hdr );
721 http_hdr = http_new_header();
722 do {
723 i = recv( fd, buffer, BUFFER_SIZE, 0 );
724 //printf("read: %d\n", i );
725 if( i<=0 ) {
726 perror("read");
727 goto err_out;
729 http_response_append( http_hdr, buffer, i );
730 } while( !http_is_header_entire( http_hdr ) );
731 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
732 http_hdr->buffer[http_hdr->buffer_size]='\0';
733 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
735 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
736 if( ret<0 ) {
737 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to parse header.\n");
738 goto err_out;
740 switch( asf_http_ctrl->streaming_type ) {
741 case ASF_Live_e:
742 case ASF_Prerecorded_e:
743 case ASF_PlainText_e:
744 if( http_hdr->body_size>0 ) {
745 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
746 goto err_out;
749 if( asf_http_ctrl->request==1 ) {
750 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
751 // First request, we only got the ASF header.
752 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
753 if(ret < 0) goto err_out;
754 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
755 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"No stream found.\n");
756 goto err_out;
758 asf_http_ctrl->request++;
759 done = 0;
760 } else {
761 done = 1;
764 break;
765 case ASF_Redirector_e:
766 if( http_hdr->body_size>0 ) {
767 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
768 goto err_out;
771 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
772 done = 1;
773 break;
774 case ASF_Authenticate_e:
775 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
776 asf_http_ctrl->streaming_type = ASF_Unknown_e;
777 done = 0;
778 break;
779 case ASF_Unknown_e:
780 default:
781 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown ASF streaming type\n");
782 goto err_out;
784 // Check if we got a redirect.
785 } while(!done);
787 stream->fd = fd;
788 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
789 stream->streaming_ctrl->streaming_read = nop_streaming_read;
790 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
791 } else {
792 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
793 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
794 stream->streaming_ctrl->buffering = 1;
796 stream->streaming_ctrl->status = streaming_playing_e;
797 stream->close = close_s;
799 http_free( http_hdr );
800 return 0;
802 err_out:
803 if (fd > 0)
804 closesocket(fd);
805 stream->fd = -1;
806 http_free(http_hdr);
807 return -1;
810 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
811 URL_t *url;
813 stream->streaming_ctrl = streaming_ctrl_new();
814 if( stream->streaming_ctrl==NULL ) {
815 return STREAM_ERROR;
817 stream->streaming_ctrl->audio_id_ptr = &stream->opts->audio_id;
818 stream->streaming_ctrl->video_id_ptr = &stream->opts->video_id;
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_tmsg(MSGT_OPEN, MSGL_INFO, "STREAM_ASF, URL: %s\n", 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_tmsg(MSGT_OPEN, MSGL_ERR, "Failed, exiting.\n");
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