codec-cfg does not depend on codecs.conf.h, it is used to generate it.
[mplayer/glamo.git] / stream / asf_streaming.c
blob64a8152ee3b0430f589c3c6a5e05aedbc1bb5d01
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, "mmshttp"))
89 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
90 fd = asf_http_streaming_start( stream, demuxer_type );
91 stream->streaming_ctrl->url->port = port;
92 if( fd>-1 ) return fd;
93 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
94 if( fd==-2 ) return -1;
97 //everything failed
98 return -1;
101 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
103 printf("ASF stream chunck size=%d\n", stream_chunck->size);
104 printf("length: %d\n", length );
105 printf("0x%02X\n", stream_chunck->type );
107 if( drop_packet!=NULL ) *drop_packet = 0;
109 if( stream_chunck->size<8 ) {
110 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
111 return -1;
113 if( stream_chunck->size!=stream_chunck->size_confirm ) {
114 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, stream_chunck->size, stream_chunck->size_confirm);
115 return -1;
118 printf(" type: 0x%02X\n", stream_chunck->type );
119 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
120 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
121 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
122 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
124 switch(stream_chunck->type) {
125 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
126 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
127 if( drop_packet!=NULL ) *drop_packet = 1;
128 return stream_chunck->size;
129 break;
130 case ASF_STREAMING_DATA: // $D Data follows
131 // printf("=====> Data follows\n");
132 break;
133 case ASF_STREAMING_END_TRANS: // $E Transfer complete
134 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
135 if( drop_packet!=NULL ) *drop_packet = 1;
136 return stream_chunck->size;
137 break;
138 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
139 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
140 break;
141 default:
142 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
144 return stream_chunck->size+4;
147 extern int audio_id;
148 extern int video_id;
150 static void close_s(stream_t *stream) {
151 close(stream->fd);
152 stream->fd=-1;
155 static int max_idx(int s_count, int *s_rates, int bound) {
156 int i, best = -1, rate = -1;
157 for (i = 0; i < s_count; i++) {
158 if (s_rates[i] > rate && s_rates[i] <= bound) {
159 rate = s_rates[i];
160 best = i;
163 return best;
166 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
167 ASF_stream_chunck_t chunk;
168 asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
169 char* buffer=NULL, *chunk_buffer=NULL;
170 int i,r,size,pos = 0;
171 int start;
172 int buffer_size = 0;
173 int chunk_size2read = 0;
174 int bw = streaming_ctrl->bandwidth;
175 int *v_rates = NULL, *a_rates = NULL;
176 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
178 if(asf_ctrl == NULL) return -1;
180 // The ASF header can be in several network chunks. For example if the content description
181 // is big, the ASF header will be split in 2 network chunk.
182 // So we need to retrieve all the chunk before starting to parse the header.
183 do {
184 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
185 return -1;
186 // Endian handling of the stream chunk
187 le2me_ASF_stream_chunck_t(&chunk);
188 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
189 if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
190 if(size < 0){
191 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
192 return -1;
194 if (chunk.type != ASF_STREAMING_HEADER) {
195 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
196 return -1;
199 // audit: do not overflow buffer_size
200 if (size > SIZE_MAX - buffer_size) return -1;
201 buffer = malloc(size+buffer_size);
202 if(buffer == NULL) {
203 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
204 return -1;
206 if( chunk_buffer!=NULL ) {
207 memcpy( buffer, chunk_buffer, buffer_size );
208 free( chunk_buffer );
210 chunk_buffer = buffer;
211 buffer += buffer_size;
212 buffer_size += size;
214 if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
215 return -1;
217 if( chunk_size2read==0 ) {
218 ASF_header_t *asfh = (ASF_header_t *)buffer;
219 if(size < (int)sizeof(ASF_header_t)) {
220 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
221 return -1;
222 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
223 chunk_size2read = AV_RL64(&asfh->objh.size);
224 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
226 } while( buffer_size<chunk_size2read);
227 buffer = chunk_buffer;
228 size = buffer_size;
230 start = sizeof(ASF_header_t);
232 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
233 if (pos >= 0) {
234 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
235 pos += sizeof(ASF_file_header_t);
236 if (pos > size) goto len_err_out;
238 if(fileh.packetsize != fileh.packetsize2) {
239 printf("Error packetsize check don't match\n");
240 return -1;
243 asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
244 // before playing.
245 // preroll: time in ms to bufferize before playing
246 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
249 pos = start;
250 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
252 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
253 pos += sizeof(ASF_stream_header_t);
254 if (pos > size) goto len_err_out;
255 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
256 case 0xF8699E40 : // audio stream
257 if(asf_ctrl->audio_streams == NULL){
258 asf_ctrl->audio_streams = malloc(sizeof(int));
259 asf_ctrl->n_audio = 1;
260 } else {
261 asf_ctrl->n_audio++;
262 asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
263 asf_ctrl->n_audio*sizeof(int));
265 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
266 break;
267 case 0xBC19EFC0 : // video stream
268 if(asf_ctrl->video_streams == NULL){
269 asf_ctrl->video_streams = malloc(sizeof(int));
270 asf_ctrl->n_video = 1;
271 } else {
272 asf_ctrl->n_video++;
273 asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
274 asf_ctrl->n_video*sizeof(int));
276 asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
277 break;
281 // always allocate to avoid lots of ifs later
282 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
283 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
285 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
286 if (pos >= 0) {
287 // stream bitrate properties object
288 int stream_count;
289 char *ptr = &buffer[pos];
290 char *end = &buffer[size];
292 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
293 if (ptr + 2 > end) goto len_err_out;
294 stream_count = AV_RL16(ptr);
295 ptr += 2;
296 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
297 stream_count, stream_count );
298 for( i=0 ; i<stream_count ; i++ ) {
299 uint32_t rate;
300 int id;
301 int j;
302 if (ptr + 6 > end) goto len_err_out;
303 id = AV_RL16(ptr);
304 ptr += 2;
305 rate = AV_RL32(ptr);
306 ptr += 4;
307 mp_msg(MSGT_NETWORK, MSGL_V,
308 " stream id=[0x%x][%u]\n", id, id);
309 mp_msg(MSGT_NETWORK, MSGL_V,
310 " max bitrate=[0x%x][%u]\n", rate, rate);
311 for (j = 0; j < asf_ctrl->n_video; j++) {
312 if (id == asf_ctrl->video_streams[j]) {
313 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
314 v_rates[j] = rate;
315 break;
318 for (j = 0; j < asf_ctrl->n_audio; j++) {
319 if (id == asf_ctrl->audio_streams[j]) {
320 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
321 a_rates[j] = rate;
322 break;
327 free(buffer);
329 // automatic stream selection based on bandwidth
330 if (bw == 0) bw = INT_MAX;
331 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
333 if (asf_ctrl->n_audio) {
334 // find lowest-bitrate audio stream
335 a_rate = a_rates[0];
336 a_idx = 0;
337 for (i = 0; i < asf_ctrl->n_audio; i++) {
338 if (a_rates[i] < a_rate) {
339 a_rate = a_rates[i];
340 a_idx = i;
343 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
344 // both audio and video are not possible, try video only next
345 a_idx = -1;
346 a_rate = 0;
349 // find best video stream
350 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
351 if (v_idx >= 0)
352 v_rate = v_rates[v_idx];
354 // find best audio stream
355 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
357 free(v_rates);
358 free(a_rates);
360 if (a_idx < 0 && v_idx < 0) {
361 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
362 return -1;
365 if (audio_id > 0)
366 // a audio stream was forced
367 asf_ctrl->audio_id = audio_id;
368 else if (a_idx >= 0)
369 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
370 else if (asf_ctrl->n_audio) {
371 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
372 audio_id = -2;
375 if (video_id > 0)
376 // a video stream was forced
377 asf_ctrl->video_id = video_id;
378 else if (v_idx >= 0)
379 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
380 else if (asf_ctrl->n_video) {
381 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
382 video_id = -2;
385 return 1;
387 len_err_out:
388 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
389 if (buffer) free(buffer);
390 if (v_rates) free(v_rates);
391 if (a_rates) free(a_rates);
392 return -1;
395 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
396 static ASF_stream_chunck_t chunk;
397 int read,chunk_size = 0;
398 static int rest = 0, drop_chunk = 0, waiting = 0;
399 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
401 while(1) {
402 if (rest == 0 && waiting == 0) {
403 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
404 return -1;
406 // Endian handling of the stream chunk
407 le2me_ASF_stream_chunck_t(&chunk);
408 chunk_size = asf_streaming( &chunk, &drop_chunk );
409 if(chunk_size < 0) {
410 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
411 return -1;
413 chunk_size -= sizeof(ASF_stream_chunck_t);
415 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
416 if (asf_http_ctrl->packet_size < chunk_size) {
417 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
418 return -1;
420 waiting = asf_http_ctrl->packet_size;
421 } else {
422 waiting = chunk_size;
425 } else if (rest){
426 chunk_size = rest;
427 rest = 0;
430 read = 0;
431 if ( waiting >= chunk_size) {
432 if (chunk_size > size){
433 rest = chunk_size - size;
434 chunk_size = size;
436 if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
437 return -1;
438 read = chunk_size;
439 waiting -= read;
440 if (drop_chunk) continue;
442 if (rest == 0 && waiting > 0 && size-read > 0) {
443 int s = FFMIN(waiting,size-read);
444 memset(buffer+read,0,s);
445 waiting -= s;
446 read += s;
448 break;
451 return read;
454 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
455 return -1;
456 // to shut up gcc warning
457 fd++;
458 pos++;
459 streaming_ctrl=NULL;
462 static int asf_header_check( HTTP_header_t *http_hdr ) {
463 ASF_obj_header_t *objh;
464 if( http_hdr==NULL ) return -1;
465 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
467 objh = (ASF_obj_header_t*)http_hdr->body;
468 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
469 return -1;
472 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
473 if( content_type==NULL ) return ASF_Unknown_e;
474 if( !strcasecmp(content_type, "application/octet-stream") ||
475 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
476 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
477 !strcasecmp(content_type, "video/x-ms-asf")) {
479 if( strstr(features, "broadcast") ) {
480 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
481 return ASF_Live_e;
482 } else {
483 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
484 return ASF_Prerecorded_e;
486 } else {
487 // Ok in a perfect world, web servers should be well configured
488 // so we could used mime type to know the stream type,
489 // but guess what? All of them are not well configured.
490 // So we have to check for an asf header :(, but it works :p
491 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
492 if( asf_header_check( http_hdr )==0 ) {
493 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
494 return ASF_PlainText_e;
495 } else if( (!strcasecmp(content_type, "text/html")) ) {
496 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
497 return ASF_Unknown_e;
498 } else {
499 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
500 return ASF_Redirector_e;
502 } else {
503 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
504 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
505 (!strcasecmp(content_type, "video/x-ms-asf")) ||
506 (!strcasecmp(content_type, "video/x-ms-afs")) ||
507 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
508 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
509 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
510 return ASF_Redirector_e;
511 } else if( !strcasecmp(content_type, "text/plain") ) {
512 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
513 return ASF_PlainText_e;
514 } else {
515 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
516 return ASF_Unknown_e;
520 return ASF_Unknown_e;
523 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
524 HTTP_header_t *http_hdr;
525 URL_t *url = NULL;
526 URL_t *server_url = NULL;
527 asf_http_streaming_ctrl_t *asf_http_ctrl;
528 char str[250];
529 char *ptr;
530 int i, enable;
532 int offset_hi=0, offset_lo=0, length=0;
533 int asf_nb_stream=0, stream_id;
535 // Sanity check
536 if( streaming_ctrl==NULL ) return NULL;
537 url = streaming_ctrl->url;
538 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
539 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
541 // Common header for all requests.
542 http_hdr = http_new_header();
543 http_set_field( http_hdr, "Accept: */*" );
544 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
545 http_add_basic_authentication( http_hdr, url->username, url->password );
547 // Check if we are using a proxy
548 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
549 server_url = url_new( (url->file)+1 );
550 if( server_url==NULL ) {
551 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
552 http_free( http_hdr );
553 return NULL;
555 http_set_uri( http_hdr, server_url->url );
556 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
557 url_free( server_url );
558 } else {
559 http_set_uri( http_hdr, url->file );
560 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
563 http_set_field( http_hdr, str );
564 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
565 sprintf(str,
566 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
567 offset_hi, offset_lo, asf_http_ctrl->request, length );
568 http_set_field( http_hdr, str );
570 switch( asf_http_ctrl->streaming_type ) {
571 case ASF_Live_e:
572 case ASF_Prerecorded_e:
573 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
574 ptr = str;
575 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
576 if(asf_http_ctrl->n_audio > 0) {
577 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
578 stream_id = asf_http_ctrl->audio_streams[i];
579 if(stream_id == asf_http_ctrl->audio_id) {
580 enable = 0;
581 } else {
582 enable = 2;
583 continue;
585 asf_nb_stream++;
586 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
589 if(asf_http_ctrl->n_video > 0) {
590 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
591 stream_id = asf_http_ctrl->video_streams[i];
592 if(stream_id == asf_http_ctrl->video_id) {
593 enable = 0;
594 } else {
595 enable = 2;
596 continue;
598 asf_nb_stream++;
599 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
602 http_set_field( http_hdr, str );
603 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
604 http_set_field( http_hdr, str );
605 break;
606 case ASF_Redirector_e:
607 break;
608 case ASF_Unknown_e:
609 // First request goes here.
610 break;
611 default:
612 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
615 http_set_field( http_hdr, "Connection: Close" );
616 http_build_request( http_hdr );
618 return http_hdr;
621 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
622 char *content_type, *pragma;
623 char features[64] = "\0";
624 size_t len;
625 if( http_response_parse(http_hdr)<0 ) {
626 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
627 return -1;
629 switch( http_hdr->status_code ) {
630 case 200:
631 break;
632 case 401: // Authentication required
633 return ASF_Authenticate_e;
634 default:
635 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
636 return -1;
639 content_type = http_get_field( http_hdr, "Content-Type");
640 //printf("Content-Type: [%s]\n", content_type);
642 pragma = http_get_field( http_hdr, "Pragma");
643 while( pragma!=NULL ) {
644 char *comma_ptr=NULL;
645 char *end;
646 //printf("Pragma: [%s]\n", pragma );
647 // The pragma line can get severals attributes
648 // separeted with a comma ','.
649 do {
650 if( !strncasecmp( pragma, "features=", 9) ) {
651 pragma += 9;
652 end = strstr( pragma, "," );
653 if( end==NULL ) {
654 len = strlen(pragma);
655 } else {
656 len = (unsigned int)(end-pragma);
658 if(len > sizeof(features) - 1) {
659 mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
660 len = sizeof(features) - 1;
662 strncpy( features, pragma, len );
663 features[len]='\0';
664 break;
666 comma_ptr = strstr( pragma, "," );
667 if( comma_ptr!=NULL ) {
668 pragma = comma_ptr+1;
669 if( pragma[0]==' ' ) pragma++;
671 } while( comma_ptr!=NULL );
672 pragma = http_get_next_field( http_hdr );
674 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
675 return 0;
678 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
679 HTTP_header_t *http_hdr=NULL;
680 URL_t *url = stream->streaming_ctrl->url;
681 asf_http_streaming_ctrl_t *asf_http_ctrl;
682 char buffer[BUFFER_SIZE];
683 int i, ret;
684 int fd = stream->fd;
685 int done;
686 int auth_retry = 0;
688 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
689 if( asf_http_ctrl==NULL ) {
690 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
691 return -1;
693 asf_http_ctrl->streaming_type = ASF_Unknown_e;
694 asf_http_ctrl->request = 1;
695 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
696 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
697 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
699 do {
700 done = 1;
701 if( fd>0 ) closesocket( fd );
703 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
704 if( url->port==0 ) url->port = 8080;
705 } else {
706 if( url->port==0 ) url->port = 80;
708 fd = connect2Server( url->hostname, url->port, 1);
709 if( fd<0 ) return fd;
711 http_hdr = asf_http_request( stream->streaming_ctrl );
712 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
713 for(i=0; i < (int)http_hdr->buffer_size ; ) {
714 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
715 if(r <0) {
716 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
717 goto err_out;
719 i += r;
721 http_free( http_hdr );
722 http_hdr = http_new_header();
723 do {
724 i = recv( fd, buffer, BUFFER_SIZE, 0 );
725 //printf("read: %d\n", i );
726 if( i<=0 ) {
727 perror("read");
728 goto err_out;
730 http_response_append( http_hdr, buffer, i );
731 } while( !http_is_header_entire( http_hdr ) );
732 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
733 http_hdr->buffer[http_hdr->buffer_size]='\0';
734 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
736 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
737 if( ret<0 ) {
738 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
739 goto err_out;
741 switch( asf_http_ctrl->streaming_type ) {
742 case ASF_Live_e:
743 case ASF_Prerecorded_e:
744 case ASF_PlainText_e:
745 if( http_hdr->body_size>0 ) {
746 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
747 goto err_out;
750 if( asf_http_ctrl->request==1 ) {
751 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
752 // First request, we only got the ASF header.
753 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
754 if(ret < 0) goto err_out;
755 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
756 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
757 goto err_out;
759 asf_http_ctrl->request++;
760 done = 0;
761 } else {
762 done = 1;
765 break;
766 case ASF_Redirector_e:
767 if( http_hdr->body_size>0 ) {
768 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
769 goto err_out;
772 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
773 done = 1;
774 break;
775 case ASF_Authenticate_e:
776 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
777 asf_http_ctrl->streaming_type = ASF_Unknown_e;
778 done = 0;
779 break;
780 case ASF_Unknown_e:
781 default:
782 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
783 goto err_out;
785 // Check if we got a redirect.
786 } while(!done);
788 stream->fd = fd;
789 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
790 stream->streaming_ctrl->streaming_read = nop_streaming_read;
791 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
792 } else {
793 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
794 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
795 stream->streaming_ctrl->buffering = 1;
797 stream->streaming_ctrl->status = streaming_playing_e;
798 stream->close = close_s;
800 http_free( http_hdr );
801 return 0;
803 err_out:
804 if (fd > 0)
805 closesocket(fd);
806 stream->fd = -1;
807 http_free(http_hdr);
808 return -1;
811 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
812 URL_t *url;
814 stream->streaming_ctrl = streaming_ctrl_new();
815 if( stream->streaming_ctrl==NULL ) {
816 return STREAM_ERROR;
818 stream->streaming_ctrl->bandwidth = network_bandwidth;
819 url = url_new(stream->url);
820 stream->streaming_ctrl->url = check4proxies(url);
821 url_free(url);
823 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_MPDEMUX_ASF_InfoStreamASFURL, stream->url);
824 if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
825 streaming_ctrl_free(stream->streaming_ctrl);
826 stream->streaming_ctrl = NULL;
827 return STREAM_UNSUPPORTED;
830 if(asf_streaming_start(stream, file_format) < 0) {
831 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_ASF_StreamingFailed);
832 streaming_ctrl_free(stream->streaming_ctrl);
833 stream->streaming_ctrl = NULL;
834 return STREAM_UNSUPPORTED;
837 if (*file_format != DEMUXER_TYPE_PLAYLIST)
838 *file_format = DEMUXER_TYPE_ASF;
839 stream->type = STREAMTYPE_STREAM;
840 fixup_network_stream_cache(stream);
841 return STREAM_OK;
844 const stream_info_t stream_info_asf = {
845 "mms and mms over http streaming",
846 "null",
847 "Bertrand, Reimar Doeffinger, Albeu",
848 "originally based on work by Majormms (is that code still there?)",
849 open_s,
850 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL},
851 NULL,
852 0 // Urls are an option string