Merge svn changes up to r30475
[mplayer/glamo.git] / stream / asf_streaming.c
blobc76ccb5d92a303605ca42f834c12b37b6e7289e5
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <limits.h>
26 #include "config.h"
27 #include "mp_msg.h"
28 #include "help_mp.h"
29 #include "options.h"
31 #if HAVE_WINSOCK2_H
32 #include <winsock2.h>
33 #endif
35 #include "url.h"
36 #include "http.h"
37 #include "libmpdemux/asf.h"
39 #include "stream.h"
40 #include "libmpdemux/demuxer.h"
42 #include "network.h"
43 #include "tcp.h"
45 #include "ffmpeg_files/intreadwrite.h"
47 #include "libmpdemux/asfguid.h"
49 extern int network_bandwidth;
51 int asf_mmst_streaming_start( stream_t *stream );
52 static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
54 static int asf_read_wrapper(int fd, void *buffer, int len, streaming_ctrl_t *stream_ctrl) {
55 uint8_t *buf = buffer;
56 while (len > 0) {
57 int got = nop_streaming_read(fd, buf, len, stream_ctrl);
58 if (got <= 0) {
59 mp_tmsg(MSGT_NETWORK, MSGL_ERR, "Error while reading network stream.\n");
60 return got;
62 buf += got;
63 len -= got;
65 return 1;
68 // We can try several protocol for asf streaming
69 // * first the UDP protcol, if there is a firewall, UDP
70 // packets will not come back, so the mmsu will fail.
71 // * Then we can try TCP, but if there is a proxy for
72 // internet connection, the TCP connection will not get
73 // through
74 // * Then we can try HTTP.
76 // Note: Using WMP sequence MMSU then MMST and then HTTP.
78 static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
79 char *proto = stream->streaming_ctrl->url->protocol;
80 int fd = -1;
81 int port = stream->streaming_ctrl->url->port;
83 // Is protocol mms or mmsu?
84 if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
86 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
87 //fd = asf_mmsu_streaming_start( stream );
88 if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
89 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/UDP failed\n");
90 if( fd==-2 ) return -1;
93 //Is protocol mms or mmst?
94 if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
96 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
97 fd = asf_mmst_streaming_start( stream );
98 stream->streaming_ctrl->url->port = port;
99 if( fd>-1 ) return fd;
100 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n");
101 if( fd==-2 ) return -1;
104 //Is protocol http, http_proxy, or mms?
105 if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
106 !strcasecmp(proto, "mms") || !strcasecmp(proto, "mmsh") ||
107 !strcasecmp(proto, "mmshttp"))
109 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
110 fd = asf_http_streaming_start( stream, demuxer_type );
111 stream->streaming_ctrl->url->port = port;
112 if( fd>-1 ) return fd;
113 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
114 if( fd==-2 ) return -1;
117 //everything failed
118 return -1;
121 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
123 printf("ASF stream chunck size=%d\n", stream_chunck->size);
124 printf("length: %d\n", length );
125 printf("0x%02X\n", stream_chunck->type );
127 if( drop_packet!=NULL ) *drop_packet = 0;
129 if( stream_chunck->size<8 ) {
130 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Ahhhh, stream_chunck size is too small: %d\n", stream_chunck->size);
131 return -1;
133 if( stream_chunck->size!=stream_chunck->size_confirm ) {
134 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"size_confirm mismatch!: %d %d\n", stream_chunck->size, stream_chunck->size_confirm);
135 return -1;
138 printf(" type: 0x%02X\n", stream_chunck->type );
139 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
140 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
141 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
142 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
144 switch(stream_chunck->type) {
145 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
146 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
147 if( drop_packet!=NULL ) *drop_packet = 1;
148 return stream_chunck->size;
149 break;
150 case ASF_STREAMING_DATA: // $D Data follows
151 // printf("=====> Data follows\n");
152 break;
153 case ASF_STREAMING_END_TRANS: // $E Transfer complete
154 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
155 if( drop_packet!=NULL ) *drop_packet = 1;
156 return stream_chunck->size;
157 break;
158 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
159 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
160 break;
161 default:
162 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
164 return stream_chunck->size+4;
167 static void close_s(stream_t *stream) {
168 closesocket(stream->fd);
169 stream->fd=-1;
172 static int max_idx(int s_count, int *s_rates, int bound) {
173 int i, best = -1, rate = -1;
174 for (i = 0; i < s_count; i++) {
175 if (s_rates[i] > rate && s_rates[i] <= bound) {
176 rate = s_rates[i];
177 best = i;
180 return best;
183 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
184 ASF_stream_chunck_t chunk;
185 asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
186 char* buffer=NULL, *chunk_buffer=NULL;
187 int i,r,size,pos = 0;
188 int start;
189 int buffer_size = 0;
190 int chunk_size2read = 0;
191 int bw = streaming_ctrl->bandwidth;
192 int *v_rates = NULL, *a_rates = NULL;
193 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
195 if(asf_ctrl == NULL) return -1;
197 // The ASF header can be in several network chunks. For example if the content description
198 // is big, the ASF header will be split in 2 network chunk.
199 // So we need to retrieve all the chunk before starting to parse the header.
200 do {
201 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
202 return -1;
203 // Endian handling of the stream chunk
204 le2me_ASF_stream_chunck_t(&chunk);
205 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
206 if(r) mp_tmsg(MSGT_NETWORK,MSGL_WARN,"Warning: drop header ????\n");
207 if(size < 0){
208 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while parsing chunk header\n");
209 return -1;
211 if (chunk.type != ASF_STREAMING_HEADER) {
212 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Didn't get a header as first chunk !!!!\n");
213 return -1;
216 // audit: do not overflow buffer_size
217 if (size > SIZE_MAX - buffer_size) return -1;
218 buffer = malloc(size+buffer_size);
219 if(buffer == NULL) {
220 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Error: Can't allocate %d bytes buffer.\n",size+buffer_size);
221 return -1;
223 if( chunk_buffer!=NULL ) {
224 memcpy( buffer, chunk_buffer, buffer_size );
225 free( chunk_buffer );
227 chunk_buffer = buffer;
228 buffer += buffer_size;
229 buffer_size += size;
231 if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
232 return -1;
234 if( chunk_size2read==0 ) {
235 ASF_header_t *asfh = (ASF_header_t *)buffer;
236 if(size < (int)sizeof(ASF_header_t)) {
237 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error: Chunk is too small.\n");
238 return -1;
239 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
240 chunk_size2read = AV_RL64(&asfh->objh.size);
241 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
243 } while( buffer_size<chunk_size2read);
244 buffer = chunk_buffer;
245 size = buffer_size;
247 start = sizeof(ASF_header_t);
249 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
250 if (pos >= 0) {
251 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
252 pos += sizeof(ASF_file_header_t);
253 if (pos > size) goto len_err_out;
255 if(fileh.packetsize != fileh.packetsize2) {
256 printf("Error packetsize check don't match\n");
257 return -1;
260 asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
261 // before playing.
262 // preroll: time in ms to bufferize before playing
263 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
266 pos = start;
267 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
269 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
270 pos += sizeof(ASF_stream_header_t);
271 if (pos > size) goto len_err_out;
272 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
273 case 0xF8699E40 : // audio stream
274 if(asf_ctrl->audio_streams == NULL){
275 asf_ctrl->audio_streams = malloc(sizeof(int));
276 asf_ctrl->n_audio = 1;
277 } else {
278 asf_ctrl->n_audio++;
279 asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
280 asf_ctrl->n_audio*sizeof(int));
282 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
283 break;
284 case 0xBC19EFC0 : // video stream
285 if(asf_ctrl->video_streams == NULL){
286 asf_ctrl->video_streams = malloc(sizeof(int));
287 asf_ctrl->n_video = 1;
288 } else {
289 asf_ctrl->n_video++;
290 asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
291 asf_ctrl->n_video*sizeof(int));
293 asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
294 break;
298 // always allocate to avoid lots of ifs later
299 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
300 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
302 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
303 if (pos >= 0) {
304 // stream bitrate properties object
305 int stream_count;
306 char *ptr = &buffer[pos];
307 char *end = &buffer[size];
309 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
310 if (ptr + 2 > end) goto len_err_out;
311 stream_count = AV_RL16(ptr);
312 ptr += 2;
313 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
314 stream_count, stream_count );
315 for( i=0 ; i<stream_count ; i++ ) {
316 uint32_t rate;
317 int id;
318 int j;
319 if (ptr + 6 > end) goto len_err_out;
320 id = AV_RL16(ptr);
321 ptr += 2;
322 rate = AV_RL32(ptr);
323 ptr += 4;
324 mp_msg(MSGT_NETWORK, MSGL_V,
325 " stream id=[0x%x][%u]\n", id, id);
326 mp_msg(MSGT_NETWORK, MSGL_V,
327 " max bitrate=[0x%x][%u]\n", rate, rate);
328 for (j = 0; j < asf_ctrl->n_video; j++) {
329 if (id == asf_ctrl->video_streams[j]) {
330 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
331 v_rates[j] = rate;
332 break;
335 for (j = 0; j < asf_ctrl->n_audio; j++) {
336 if (id == asf_ctrl->audio_streams[j]) {
337 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
338 a_rates[j] = rate;
339 break;
344 free(buffer);
346 // automatic stream selection based on bandwidth
347 if (bw == 0) bw = INT_MAX;
348 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
350 if (asf_ctrl->n_audio) {
351 // find lowest-bitrate audio stream
352 a_rate = a_rates[0];
353 a_idx = 0;
354 for (i = 0; i < asf_ctrl->n_audio; i++) {
355 if (a_rates[i] < a_rate) {
356 a_rate = a_rates[i];
357 a_idx = i;
360 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
361 // both audio and video are not possible, try video only next
362 a_idx = -1;
363 a_rate = 0;
366 // find best video stream
367 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
368 if (v_idx >= 0)
369 v_rate = v_rates[v_idx];
371 // find best audio stream
372 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
374 free(v_rates);
375 free(a_rates);
377 if (a_idx < 0 && v_idx < 0) {
378 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Bandwidth too small, file cannot be played!\n");
379 return -1;
382 if (*streaming_ctrl->audio_id_ptr > 0)
383 // a audio stream was forced
384 asf_ctrl->audio_id = *streaming_ctrl->audio_id_ptr;
385 else if (a_idx >= 0)
386 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
387 else if (asf_ctrl->n_audio) {
388 mp_tmsg(MSGT_NETWORK, MSGL_WARN, "Bandwidth too small, deselected audio stream.\n");
389 *streaming_ctrl->audio_id_ptr = -2;
392 if (*streaming_ctrl->video_id_ptr > 0)
393 // a video stream was forced
394 asf_ctrl->video_id = *streaming_ctrl->video_id_ptr;
395 else if (v_idx >= 0)
396 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
397 else if (asf_ctrl->n_video) {
398 mp_tmsg(MSGT_NETWORK, MSGL_WARN, "Bandwidth too small, deselected video stream.\n");
399 *streaming_ctrl->video_id_ptr = -2;
402 return 1;
404 len_err_out:
405 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Invalid length in ASF header!\n");
406 if (buffer) free(buffer);
407 if (v_rates) free(v_rates);
408 if (a_rates) free(a_rates);
409 return -1;
412 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
413 static ASF_stream_chunck_t chunk;
414 int read,chunk_size = 0;
415 static int rest = 0, drop_chunk = 0, waiting = 0;
416 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
418 while(1) {
419 if (rest == 0 && waiting == 0) {
420 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
421 return -1;
423 // Endian handling of the stream chunk
424 le2me_ASF_stream_chunck_t(&chunk);
425 chunk_size = asf_streaming( &chunk, &drop_chunk );
426 if(chunk_size < 0) {
427 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while parsing chunk header\n");
428 return -1;
430 chunk_size -= sizeof(ASF_stream_chunck_t);
432 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
433 if (asf_http_ctrl->packet_size < chunk_size) {
434 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error: chunk_size > packet_size\n");
435 return -1;
437 waiting = asf_http_ctrl->packet_size;
438 } else {
439 waiting = chunk_size;
442 } else if (rest){
443 chunk_size = rest;
444 rest = 0;
447 read = 0;
448 if ( waiting >= chunk_size) {
449 if (chunk_size > size){
450 rest = chunk_size - size;
451 chunk_size = size;
453 if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
454 return -1;
455 read = chunk_size;
456 waiting -= read;
457 if (drop_chunk) continue;
459 if (rest == 0 && waiting > 0 && size-read > 0) {
460 int s = FFMIN(waiting,size-read);
461 memset(buffer+read,0,s);
462 waiting -= s;
463 read += s;
465 break;
468 return read;
471 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
472 return -1;
473 // to shut up gcc warning
474 fd++;
475 pos++;
476 streaming_ctrl=NULL;
479 static int asf_header_check( HTTP_header_t *http_hdr ) {
480 ASF_obj_header_t *objh;
481 if( http_hdr==NULL ) return -1;
482 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
484 objh = (ASF_obj_header_t*)http_hdr->body;
485 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
486 return -1;
489 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
490 if( content_type==NULL ) return ASF_Unknown_e;
491 if( !strcasecmp(content_type, "application/octet-stream") ||
492 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
493 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
494 !strcasecmp(content_type, "video/x-ms-wmv") ||
495 !strcasecmp(content_type, "video/x-ms-asf")) {
497 if( strstr(features, "broadcast") ) {
498 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
499 return ASF_Live_e;
500 } else {
501 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
502 return ASF_Prerecorded_e;
504 } else {
505 // Ok in a perfect world, web servers should be well configured
506 // so we could used mime type to know the stream type,
507 // but guess what? All of them are not well configured.
508 // So we have to check for an asf header :(, but it works :p
509 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
510 if( asf_header_check( http_hdr )==0 ) {
511 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
512 return ASF_PlainText_e;
513 } else if( (!strcasecmp(content_type, "text/html")) ) {
514 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
515 return ASF_Unknown_e;
516 } else {
517 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
518 return ASF_Redirector_e;
520 } else {
521 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
522 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
523 (!strcasecmp(content_type, "video/x-ms-asf")) ||
524 (!strcasecmp(content_type, "video/x-ms-afs")) ||
525 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
526 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
527 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"=====> ASF Redirector\n");
528 return ASF_Redirector_e;
529 } else if( !strcasecmp(content_type, "text/plain") ) {
530 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
531 return ASF_PlainText_e;
532 } else {
533 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
534 return ASF_Unknown_e;
538 return ASF_Unknown_e;
541 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
542 HTTP_header_t *http_hdr;
543 URL_t *url = NULL;
544 URL_t *server_url = NULL;
545 asf_http_streaming_ctrl_t *asf_http_ctrl;
546 char str[250];
547 char *ptr;
548 int i, enable;
550 int offset_hi=0, offset_lo=0, length=0;
551 int asf_nb_stream=0, stream_id;
553 // Sanity check
554 if( streaming_ctrl==NULL ) return NULL;
555 url = streaming_ctrl->url;
556 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
557 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
559 // Common header for all requests.
560 http_hdr = http_new_header();
561 http_set_field( http_hdr, "Accept: */*" );
562 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
563 http_add_basic_authentication( http_hdr, url->username, url->password );
565 // Check if we are using a proxy
566 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
567 server_url = url_new( (url->file)+1 );
568 if( server_url==NULL ) {
569 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"invalid proxy URL\n");
570 http_free( http_hdr );
571 return NULL;
573 http_set_uri( http_hdr, server_url->url );
574 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
575 url_free( server_url );
576 } else {
577 http_set_uri( http_hdr, url->file );
578 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
581 http_set_field( http_hdr, str );
582 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
583 sprintf(str,
584 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
585 offset_hi, offset_lo, asf_http_ctrl->request, length );
586 http_set_field( http_hdr, str );
588 switch( asf_http_ctrl->streaming_type ) {
589 case ASF_Live_e:
590 case ASF_Prerecorded_e:
591 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
592 ptr = str;
593 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
594 if(asf_http_ctrl->n_audio > 0) {
595 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
596 stream_id = asf_http_ctrl->audio_streams[i];
597 if(stream_id == asf_http_ctrl->audio_id) {
598 enable = 0;
599 } else {
600 enable = 2;
601 continue;
603 asf_nb_stream++;
604 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
607 if(asf_http_ctrl->n_video > 0) {
608 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
609 stream_id = asf_http_ctrl->video_streams[i];
610 if(stream_id == asf_http_ctrl->video_id) {
611 enable = 0;
612 } else {
613 enable = 2;
614 continue;
616 asf_nb_stream++;
617 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
620 http_set_field( http_hdr, str );
621 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
622 http_set_field( http_hdr, str );
623 break;
624 case ASF_Redirector_e:
625 break;
626 case ASF_Unknown_e:
627 // First request goes here.
628 break;
629 default:
630 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown ASF stream type\n");
633 http_set_field( http_hdr, "Connection: Close" );
634 http_build_request( http_hdr );
636 return http_hdr;
639 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
640 char *content_type, *pragma;
641 char features[64] = "\0";
642 size_t len;
643 if( http_response_parse(http_hdr)<0 ) {
644 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to parse HTTP response.\n");
645 return -1;
647 switch( http_hdr->status_code ) {
648 case 200:
649 break;
650 case 401: // Authentication required
651 return ASF_Authenticate_e;
652 default:
653 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returned %d:%s\n", http_hdr->status_code, http_hdr->reason_phrase);
654 return -1;
657 content_type = http_get_field( http_hdr, "Content-Type");
658 //printf("Content-Type: [%s]\n", content_type);
660 pragma = http_get_field( http_hdr, "Pragma");
661 while( pragma!=NULL ) {
662 char *comma_ptr=NULL;
663 char *end;
664 //printf("Pragma: [%s]\n", pragma );
665 // The pragma line can get severals attributes
666 // separeted with a comma ','.
667 do {
668 if( !strncasecmp( pragma, "features=", 9) ) {
669 pragma += 9;
670 end = strstr( pragma, "," );
671 if( end==NULL ) {
672 len = strlen(pragma);
673 } else {
674 len = (unsigned int)(end-pragma);
676 if(len > sizeof(features) - 1) {
677 mp_tmsg(MSGT_NETWORK,MSGL_WARN,"ASF HTTP PARSE WARNING : Pragma %s cut from %d bytes to %d\n",pragma,len,sizeof(features) - 1);
678 len = sizeof(features) - 1;
680 strncpy( features, pragma, len );
681 features[len]='\0';
682 break;
684 comma_ptr = strstr( pragma, "," );
685 if( comma_ptr!=NULL ) {
686 pragma = comma_ptr+1;
687 if( pragma[0]==' ' ) pragma++;
689 } while( comma_ptr!=NULL );
690 pragma = http_get_next_field( http_hdr );
692 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
693 return 0;
696 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
697 HTTP_header_t *http_hdr=NULL;
698 URL_t *url = stream->streaming_ctrl->url;
699 asf_http_streaming_ctrl_t *asf_http_ctrl;
700 char buffer[BUFFER_SIZE];
701 int i, ret;
702 int fd = stream->fd;
703 int done;
704 int auth_retry = 0;
706 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
707 if( asf_http_ctrl==NULL ) {
708 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
709 return -1;
711 asf_http_ctrl->streaming_type = ASF_Unknown_e;
712 asf_http_ctrl->request = 1;
713 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
714 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
715 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
717 do {
718 done = 1;
719 if( fd>0 ) closesocket( fd );
721 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
722 if( url->port==0 ) url->port = 8080;
723 } else {
724 if( url->port==0 ) url->port = 80;
726 fd = connect2Server( url->hostname, url->port, 1);
727 if( fd<0 ) return fd;
729 http_hdr = asf_http_request( stream->streaming_ctrl );
730 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
731 for(i=0; i < (int)http_hdr->buffer_size ; ) {
732 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
733 if(r <0) {
734 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"socket write error: %s\n",strerror(errno));
735 goto err_out;
737 i += r;
739 http_free( http_hdr );
740 http_hdr = http_new_header();
741 do {
742 i = recv( fd, buffer, BUFFER_SIZE, 0 );
743 //printf("read: %d\n", i );
744 if( i<=0 ) {
745 perror("read");
746 goto err_out;
748 http_response_append( http_hdr, buffer, i );
749 } while( !http_is_header_entire( http_hdr ) );
750 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
751 http_hdr->buffer[http_hdr->buffer_size]='\0';
752 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
754 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
755 if( ret<0 ) {
756 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to parse header.\n");
757 goto err_out;
759 switch( asf_http_ctrl->streaming_type ) {
760 case ASF_Live_e:
761 case ASF_Prerecorded_e:
762 case ASF_PlainText_e:
763 if( http_hdr->body_size>0 ) {
764 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
765 goto err_out;
768 if( asf_http_ctrl->request==1 ) {
769 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
770 // First request, we only got the ASF header.
771 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
772 if(ret < 0) goto err_out;
773 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
774 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"No stream found.\n");
775 goto err_out;
777 asf_http_ctrl->request++;
778 done = 0;
779 } else {
780 done = 1;
783 break;
784 case ASF_Redirector_e:
785 if( http_hdr->body_size>0 ) {
786 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
787 goto err_out;
790 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
791 done = 1;
792 break;
793 case ASF_Authenticate_e:
794 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
795 asf_http_ctrl->streaming_type = ASF_Unknown_e;
796 done = 0;
797 break;
798 case ASF_Unknown_e:
799 default:
800 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown ASF streaming type\n");
801 goto err_out;
803 // Check if we got a redirect.
804 } while(!done);
806 stream->fd = fd;
807 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
808 stream->streaming_ctrl->streaming_read = nop_streaming_read;
809 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
810 } else {
811 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
812 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
813 stream->streaming_ctrl->buffering = 1;
815 stream->streaming_ctrl->status = streaming_playing_e;
816 stream->close = close_s;
818 http_free( http_hdr );
819 return 0;
821 err_out:
822 if (fd > 0)
823 closesocket(fd);
824 stream->fd = -1;
825 http_free(http_hdr);
826 return -1;
829 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
830 URL_t *url;
832 stream->streaming_ctrl = streaming_ctrl_new();
833 if( stream->streaming_ctrl==NULL ) {
834 return STREAM_ERROR;
836 stream->streaming_ctrl->audio_id_ptr = &stream->opts->audio_id;
837 stream->streaming_ctrl->video_id_ptr = &stream->opts->video_id;
838 stream->streaming_ctrl->bandwidth = network_bandwidth;
839 url = url_new(stream->url);
840 stream->streaming_ctrl->url = check4proxies(url);
841 url_free(url);
843 mp_tmsg(MSGT_OPEN, MSGL_INFO, "STREAM_ASF, URL: %s\n", stream->url);
844 if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
845 streaming_ctrl_free(stream->streaming_ctrl);
846 stream->streaming_ctrl = NULL;
847 return STREAM_UNSUPPORTED;
850 if(asf_streaming_start(stream, file_format) < 0) {
851 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed, exiting.\n");
852 streaming_ctrl_free(stream->streaming_ctrl);
853 stream->streaming_ctrl = NULL;
854 return STREAM_UNSUPPORTED;
857 if (*file_format != DEMUXER_TYPE_PLAYLIST)
858 *file_format = DEMUXER_TYPE_ASF;
859 stream->type = STREAMTYPE_STREAM;
860 fixup_network_stream_cache(stream);
861 return STREAM_OK;
864 const stream_info_t stream_info_asf = {
865 "mms and mms over http streaming",
866 "null",
867 "Bertrand, Reimar Doeffinger, Albeu",
868 "originally based on work by Majormms (is that code still there?)",
869 open_s,
870 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL},
871 NULL,
872 0 // Urls are an option string