subassconvert: do not escape likely ASS override tags
[mplayer.git] / stream / asf_streaming.c
blob14f9f97a74df3cd32092222ac853b84d6adfd03e
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 <libavutil/intreadwrite.h>
28 #include "config.h"
29 #include "mp_msg.h"
30 #include "options.h"
32 #if HAVE_WINSOCK2_H
33 #include <winsock2.h>
34 #endif
36 #include "url.h"
37 #include "http.h"
38 #include "libmpdemux/asf.h"
40 #include "stream.h"
41 #include "libmpdemux/demuxer.h"
42 #include "asf_mmst_streaming.h"
43 #include "network.h"
44 #include "tcp.h"
46 #include "libmpdemux/asfguid.h"
48 extern int network_bandwidth;
50 static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
52 static int asf_read_wrapper(int fd, void *buffer, int len, streaming_ctrl_t *stream_ctrl) {
53 uint8_t *buf = buffer;
54 while (len > 0) {
55 int got = nop_streaming_read(fd, buf, len, stream_ctrl);
56 if (got <= 0) {
57 mp_tmsg(MSGT_NETWORK, MSGL_ERR, "Error while reading network stream.\n");
58 return got;
60 buf += got;
61 len -= got;
63 return 1;
66 // We can try several protocol for asf streaming
67 // * first the UDP protcol, if there is a firewall, UDP
68 // packets will not come back, so the mmsu will fail.
69 // * Then we can try TCP, but if there is a proxy for
70 // internet connection, the TCP connection will not get
71 // through
72 // * Then we can try HTTP.
74 // Note: Using WMP sequence MMSU then MMST and then HTTP.
76 static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
77 char *proto = stream->streaming_ctrl->url->protocol;
78 int fd = -1;
79 int port = stream->streaming_ctrl->url->port;
81 // Is protocol mms or mmsu?
82 if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
84 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
85 //fd = asf_mmsu_streaming_start( stream );
86 if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
87 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/UDP failed\n");
88 if( fd==-2 ) return -1;
91 //Is protocol mms or mmst?
92 if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
94 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
95 fd = asf_mmst_streaming_start( stream );
96 stream->streaming_ctrl->url->port = port;
97 if( fd>-1 ) return fd;
98 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n");
99 if( fd==-2 ) return -1;
102 //Is protocol http, http_proxy, or mms?
103 if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
104 !strcasecmp(proto, "mms") || !strcasecmp(proto, "mmsh") ||
105 !strcasecmp(proto, "mmshttp"))
107 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
108 fd = asf_http_streaming_start( stream, demuxer_type );
109 stream->streaming_ctrl->url->port = port;
110 if( fd>-1 ) return fd;
111 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
112 if( fd==-2 ) return -1;
115 //everything failed
116 return -1;
119 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
121 printf("ASF stream chunck size=%d\n", stream_chunck->size);
122 printf("length: %d\n", length );
123 printf("0x%02X\n", stream_chunck->type );
125 if( drop_packet!=NULL ) *drop_packet = 0;
127 if( stream_chunck->size<8 ) {
128 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Ahhhh, stream_chunck size is too small: %d\n", stream_chunck->size);
129 return -1;
131 if( stream_chunck->size!=stream_chunck->size_confirm ) {
132 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"size_confirm mismatch!: %d %d\n", stream_chunck->size, stream_chunck->size_confirm);
133 return -1;
136 printf(" type: 0x%02X\n", stream_chunck->type );
137 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
138 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
139 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
140 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
142 switch(stream_chunck->type) {
143 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
144 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
145 if( drop_packet!=NULL ) *drop_packet = 1;
146 return stream_chunck->size;
147 break;
148 case ASF_STREAMING_DATA: // $D Data follows
149 // printf("=====> Data follows\n");
150 break;
151 case ASF_STREAMING_END_TRANS: // $E Transfer complete
152 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
153 if( drop_packet!=NULL ) *drop_packet = 1;
154 return stream_chunck->size;
155 break;
156 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
157 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
158 break;
159 default:
160 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
162 return stream_chunck->size+4;
165 static void close_s(stream_t *stream) {
166 closesocket(stream->fd);
167 stream->fd=-1;
170 static int max_idx(int s_count, int *s_rates, int bound) {
171 int i, best = -1, rate = -1;
172 for (i = 0; i < s_count; i++) {
173 if (s_rates[i] > rate && s_rates[i] <= bound) {
174 rate = s_rates[i];
175 best = i;
178 return best;
181 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
182 ASF_stream_chunck_t chunk;
183 asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
184 char* buffer=NULL, *chunk_buffer=NULL;
185 int i,r,size,pos = 0;
186 int start;
187 int buffer_size = 0;
188 int chunk_size2read = 0;
189 int bw = streaming_ctrl->bandwidth;
190 int *v_rates = NULL, *a_rates = NULL;
191 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
193 if(asf_ctrl == NULL) return -1;
195 // The ASF header can be in several network chunks. For example if the content description
196 // is big, the ASF header will be split in 2 network chunk.
197 // So we need to retrieve all the chunk before starting to parse the header.
198 do {
199 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
200 return -1;
201 // Endian handling of the stream chunk
202 le2me_ASF_stream_chunck_t(&chunk);
203 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
204 if(r) mp_tmsg(MSGT_NETWORK,MSGL_WARN,"Warning: drop header ????\n");
205 if(size < 0){
206 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while parsing chunk header\n");
207 return -1;
209 if (chunk.type != ASF_STREAMING_HEADER) {
210 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Didn't get a header as first chunk !!!!\n");
211 return -1;
214 // audit: do not overflow buffer_size
215 if (size > SIZE_MAX - buffer_size) return -1;
216 buffer = malloc(size+buffer_size);
217 if(buffer == NULL) {
218 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Error: Can't allocate %d bytes buffer.\n",size+buffer_size);
219 return -1;
221 if( chunk_buffer!=NULL ) {
222 memcpy( buffer, chunk_buffer, buffer_size );
223 free( chunk_buffer );
225 chunk_buffer = buffer;
226 buffer += buffer_size;
227 buffer_size += size;
229 if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
230 return -1;
232 if( chunk_size2read==0 ) {
233 ASF_header_t *asfh = (ASF_header_t *)buffer;
234 if(size < (int)sizeof(ASF_header_t)) {
235 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error: Chunk is too small.\n");
236 return -1;
237 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
238 chunk_size2read = AV_RL64(&asfh->objh.size);
239 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
241 } while( buffer_size<chunk_size2read);
242 buffer = chunk_buffer;
243 size = buffer_size;
245 start = sizeof(ASF_header_t);
247 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
248 if (pos >= 0) {
249 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
250 pos += sizeof(ASF_file_header_t);
251 if (pos > size) goto len_err_out;
253 if(fileh.packetsize != fileh.packetsize2) {
254 printf("Error packetsize check don't match\n");
255 return -1;
258 asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
259 // before playing.
260 // preroll: time in ms to bufferize before playing
261 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
264 pos = start;
265 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
267 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
268 pos += sizeof(ASF_stream_header_t);
269 if (pos > size) goto len_err_out;
270 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
271 case 0xF8699E40 : // audio stream
272 if(asf_ctrl->audio_streams == NULL){
273 asf_ctrl->audio_streams = malloc(sizeof(int));
274 asf_ctrl->n_audio = 1;
275 } else {
276 asf_ctrl->n_audio++;
277 asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
278 asf_ctrl->n_audio*sizeof(int));
280 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
281 break;
282 case 0xBC19EFC0 : // video stream
283 if(asf_ctrl->video_streams == NULL){
284 asf_ctrl->video_streams = malloc(sizeof(int));
285 asf_ctrl->n_video = 1;
286 } else {
287 asf_ctrl->n_video++;
288 asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
289 asf_ctrl->n_video*sizeof(int));
291 asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
292 break;
296 // always allocate to avoid lots of ifs later
297 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
298 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
300 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
301 if (pos >= 0) {
302 // stream bitrate properties object
303 int stream_count;
304 char *ptr = &buffer[pos];
305 char *end = &buffer[size];
307 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
308 if (ptr + 2 > end) goto len_err_out;
309 stream_count = AV_RL16(ptr);
310 ptr += 2;
311 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
312 stream_count, stream_count );
313 for( i=0 ; i<stream_count ; i++ ) {
314 uint32_t rate;
315 int id;
316 int j;
317 if (ptr + 6 > end) goto len_err_out;
318 id = AV_RL16(ptr);
319 ptr += 2;
320 rate = AV_RL32(ptr);
321 ptr += 4;
322 mp_msg(MSGT_NETWORK, MSGL_V,
323 " stream id=[0x%x][%u]\n", id, id);
324 mp_msg(MSGT_NETWORK, MSGL_V,
325 " max bitrate=[0x%x][%u]\n", rate, rate);
326 for (j = 0; j < asf_ctrl->n_video; j++) {
327 if (id == asf_ctrl->video_streams[j]) {
328 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
329 v_rates[j] = rate;
330 break;
333 for (j = 0; j < asf_ctrl->n_audio; j++) {
334 if (id == asf_ctrl->audio_streams[j]) {
335 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
336 a_rates[j] = rate;
337 break;
342 free(buffer);
344 // automatic stream selection based on bandwidth
345 if (bw == 0) bw = INT_MAX;
346 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
348 if (asf_ctrl->n_audio) {
349 // find lowest-bitrate audio stream
350 a_rate = a_rates[0];
351 a_idx = 0;
352 for (i = 0; i < asf_ctrl->n_audio; i++) {
353 if (a_rates[i] < a_rate) {
354 a_rate = a_rates[i];
355 a_idx = i;
358 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
359 // both audio and video are not possible, try video only next
360 a_idx = -1;
361 a_rate = 0;
364 // find best video stream
365 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
366 if (v_idx >= 0)
367 v_rate = v_rates[v_idx];
369 // find best audio stream
370 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
372 free(v_rates);
373 free(a_rates);
375 if (a_idx < 0 && v_idx < 0) {
376 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Bandwidth too small, file cannot be played!\n");
377 return -1;
380 if (*streaming_ctrl->audio_id_ptr > 0)
381 // a audio stream was forced
382 asf_ctrl->audio_id = *streaming_ctrl->audio_id_ptr;
383 else if (a_idx >= 0)
384 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
385 else if (asf_ctrl->n_audio) {
386 mp_tmsg(MSGT_NETWORK, MSGL_WARN, "Bandwidth too small, deselected audio stream.\n");
387 *streaming_ctrl->audio_id_ptr = -2;
390 if (*streaming_ctrl->video_id_ptr > 0)
391 // a video stream was forced
392 asf_ctrl->video_id = *streaming_ctrl->video_id_ptr;
393 else if (v_idx >= 0)
394 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
395 else if (asf_ctrl->n_video) {
396 mp_tmsg(MSGT_NETWORK, MSGL_WARN, "Bandwidth too small, deselected video stream.\n");
397 *streaming_ctrl->video_id_ptr = -2;
400 return 1;
402 len_err_out:
403 mp_tmsg(MSGT_NETWORK, MSGL_FATAL, "Invalid length in ASF header!\n");
404 free(buffer);
405 free(v_rates);
406 free(a_rates);
407 return -1;
410 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
411 static ASF_stream_chunck_t chunk;
412 int read,chunk_size = 0;
413 static int rest = 0, drop_chunk = 0, waiting = 0;
414 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
416 while(1) {
417 if (rest == 0 && waiting == 0) {
418 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
419 return -1;
421 // Endian handling of the stream chunk
422 le2me_ASF_stream_chunck_t(&chunk);
423 chunk_size = asf_streaming( &chunk, &drop_chunk );
424 if(chunk_size < 0) {
425 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error while parsing chunk header\n");
426 return -1;
428 chunk_size -= sizeof(ASF_stream_chunck_t);
430 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
431 if (asf_http_ctrl->packet_size < chunk_size) {
432 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Error: chunk_size > packet_size\n");
433 return -1;
435 waiting = asf_http_ctrl->packet_size;
436 } else {
437 waiting = chunk_size;
440 } else if (rest){
441 chunk_size = rest;
442 rest = 0;
445 read = 0;
446 if ( waiting >= chunk_size) {
447 if (chunk_size > size){
448 rest = chunk_size - size;
449 chunk_size = size;
451 if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
452 return -1;
453 read = chunk_size;
454 waiting -= read;
455 if (drop_chunk) continue;
457 if (rest == 0 && waiting > 0 && size-read > 0) {
458 int s = FFMIN(waiting,size-read);
459 memset(buffer+read,0,s);
460 waiting -= s;
461 read += s;
463 break;
466 return read;
469 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
470 return -1;
471 // to shut up gcc warning
472 fd++;
473 pos++;
474 streaming_ctrl=NULL;
477 static int asf_header_check( HTTP_header_t *http_hdr ) {
478 ASF_obj_header_t *objh;
479 if( http_hdr==NULL ) return -1;
480 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
482 objh = (ASF_obj_header_t*)http_hdr->body;
483 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
484 return -1;
487 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
488 if( content_type==NULL ) return ASF_Unknown_e;
489 if( !strcasecmp(content_type, "application/octet-stream") ||
490 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
491 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
492 !strcasecmp(content_type, "video/x-ms-wmv") ||
493 !strcasecmp(content_type, "video/x-ms-asf")) {
495 if( strstr(features, "broadcast") ) {
496 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
497 return ASF_Live_e;
498 } else {
499 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
500 return ASF_Prerecorded_e;
502 } else {
503 // Ok in a perfect world, web servers should be well configured
504 // so we could used mime type to know the stream type,
505 // but guess what? All of them are not well configured.
506 // So we have to check for an asf header :(, but it works :p
507 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
508 if( asf_header_check( http_hdr )==0 ) {
509 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
510 return ASF_PlainText_e;
511 } else if( (!strcasecmp(content_type, "text/html")) ) {
512 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
513 return ASF_Unknown_e;
514 } else {
515 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
516 return ASF_Redirector_e;
518 } else {
519 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
520 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
521 (!strcasecmp(content_type, "video/x-ms-asf")) ||
522 (!strcasecmp(content_type, "video/x-ms-afs")) ||
523 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
524 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
525 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"=====> ASF Redirector\n");
526 return ASF_Redirector_e;
527 } else if( !strcasecmp(content_type, "text/plain") ) {
528 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
529 return ASF_PlainText_e;
530 } else {
531 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
532 return ASF_Unknown_e;
536 return ASF_Unknown_e;
539 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
540 HTTP_header_t *http_hdr;
541 URL_t *url = NULL;
542 URL_t *server_url = NULL;
543 asf_http_streaming_ctrl_t *asf_http_ctrl;
544 char str[250];
545 char *ptr;
546 int i, enable;
548 int offset_hi=0, offset_lo=0, length=0;
549 int asf_nb_stream=0, stream_id;
551 // Sanity check
552 if( streaming_ctrl==NULL ) return NULL;
553 url = streaming_ctrl->url;
554 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
555 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
557 // Common header for all requests.
558 http_hdr = http_new_header();
559 http_set_field( http_hdr, "Accept: */*" );
560 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
561 http_add_basic_authentication( http_hdr, url->username, url->password );
563 // Check if we are using a proxy
564 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
565 server_url = url_new( (url->file)+1 );
566 if( server_url==NULL ) {
567 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"invalid proxy URL\n");
568 http_free( http_hdr );
569 return NULL;
571 http_set_uri( http_hdr, server_url->url );
572 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
573 url_free( server_url );
574 } else {
575 http_set_uri( http_hdr, url->file );
576 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
579 http_set_field( http_hdr, str );
580 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
581 sprintf(str,
582 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
583 offset_hi, offset_lo, asf_http_ctrl->request, length );
584 http_set_field( http_hdr, str );
586 switch( asf_http_ctrl->streaming_type ) {
587 case ASF_Live_e:
588 case ASF_Prerecorded_e:
589 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
590 ptr = str;
591 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
592 if(asf_http_ctrl->n_audio > 0) {
593 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
594 stream_id = asf_http_ctrl->audio_streams[i];
595 if(stream_id == asf_http_ctrl->audio_id) {
596 enable = 0;
597 } else {
598 enable = 2;
599 continue;
601 asf_nb_stream++;
602 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
605 if(asf_http_ctrl->n_video > 0) {
606 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
607 stream_id = asf_http_ctrl->video_streams[i];
608 if(stream_id == asf_http_ctrl->video_id) {
609 enable = 0;
610 } else {
611 enable = 2;
612 continue;
614 asf_nb_stream++;
615 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
618 http_set_field( http_hdr, str );
619 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
620 http_set_field( http_hdr, str );
621 break;
622 case ASF_Redirector_e:
623 break;
624 case ASF_Unknown_e:
625 // First request goes here.
626 break;
627 default:
628 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown ASF stream type\n");
631 http_set_field( http_hdr, "Connection: Close" );
632 http_build_request( http_hdr );
634 return http_hdr;
637 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
638 char *content_type, *pragma;
639 char features[64] = "\0";
640 size_t len;
641 if( http_response_parse(http_hdr)<0 ) {
642 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to parse HTTP response.\n");
643 return -1;
645 switch( http_hdr->status_code ) {
646 case 200:
647 break;
648 case 401: // Authentication required
649 return ASF_Authenticate_e;
650 default:
651 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Server returned %d:%s\n", http_hdr->status_code, http_hdr->reason_phrase);
652 return -1;
655 content_type = http_get_field( http_hdr, "Content-Type");
656 //printf("Content-Type: [%s]\n", content_type);
658 pragma = http_get_field( http_hdr, "Pragma");
659 while( pragma!=NULL ) {
660 char *comma_ptr=NULL;
661 char *end;
662 //printf("Pragma: [%s]\n", pragma );
663 // The pragma line can get severals attributes
664 // separeted with a comma ','.
665 do {
666 if( !strncasecmp( pragma, "features=", 9) ) {
667 pragma += 9;
668 end = strstr( pragma, "," );
669 if( end==NULL ) {
670 len = strlen(pragma);
671 } else {
672 len = (unsigned int)(end-pragma);
674 if(len > sizeof(features) - 1) {
675 mp_tmsg(MSGT_NETWORK,MSGL_WARN,"ASF HTTP PARSE WARNING : Pragma %s cut from %zd bytes to %zd\n",pragma,len,sizeof(features) - 1);
676 len = sizeof(features) - 1;
678 strncpy( features, pragma, len );
679 features[len]='\0';
680 break;
682 comma_ptr = strstr( pragma, "," );
683 if( comma_ptr!=NULL ) {
684 pragma = comma_ptr+1;
685 if( pragma[0]==' ' ) pragma++;
687 } while( comma_ptr!=NULL );
688 pragma = http_get_next_field( http_hdr );
690 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
691 return 0;
694 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
695 HTTP_header_t *http_hdr=NULL;
696 URL_t *url = stream->streaming_ctrl->url;
697 asf_http_streaming_ctrl_t *asf_http_ctrl;
698 char buffer[BUFFER_SIZE];
699 int i, ret;
700 int fd = stream->fd;
701 int done;
702 int auth_retry = 0;
704 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
705 if( asf_http_ctrl==NULL ) {
706 mp_tmsg(MSGT_NETWORK,MSGL_FATAL,"Memory allocation failed.\n");
707 return -1;
709 asf_http_ctrl->streaming_type = ASF_Unknown_e;
710 asf_http_ctrl->request = 1;
711 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
712 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
713 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
715 do {
716 done = 1;
717 if( fd>0 ) closesocket( fd );
719 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
720 if( url->port==0 ) url->port = 8080;
721 } else {
722 if( url->port==0 ) url->port = 80;
724 fd = connect2Server( url->hostname, url->port, 1);
725 if( fd<0 ) return fd;
727 http_hdr = asf_http_request( stream->streaming_ctrl );
728 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
729 for(i=0; i < (int)http_hdr->buffer_size ; ) {
730 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, DEFAULT_SEND_FLAGS );
731 if(r <0) {
732 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"socket write error: %s\n",strerror(errno));
733 goto err_out;
735 i += r;
737 http_free( http_hdr );
738 http_hdr = http_new_header();
739 do {
740 i = recv( fd, buffer, BUFFER_SIZE, 0 );
741 //printf("read: %d\n", i );
742 if( i<=0 ) {
743 perror("read");
744 goto err_out;
746 http_response_append( http_hdr, buffer, i );
747 } while( !http_is_header_entire( http_hdr ) );
748 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
749 http_hdr->buffer[http_hdr->buffer_size]='\0';
750 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
752 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
753 if( ret<0 ) {
754 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"Failed to parse header.\n");
755 goto err_out;
757 switch( asf_http_ctrl->streaming_type ) {
758 case ASF_Live_e:
759 case ASF_Prerecorded_e:
760 case ASF_PlainText_e:
761 if( http_hdr->body_size>0 ) {
762 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
763 goto err_out;
766 if( asf_http_ctrl->request==1 ) {
767 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
768 // First request, we only got the ASF header.
769 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
770 if(ret < 0) goto err_out;
771 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
772 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"No stream found.\n");
773 goto err_out;
775 asf_http_ctrl->request++;
776 done = 0;
777 } else {
778 done = 1;
781 break;
782 case ASF_Redirector_e:
783 if( http_hdr->body_size>0 ) {
784 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
785 goto err_out;
788 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
789 done = 1;
790 break;
791 case ASF_Authenticate_e:
792 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
793 asf_http_ctrl->streaming_type = ASF_Unknown_e;
794 done = 0;
795 break;
796 case ASF_Unknown_e:
797 default:
798 mp_tmsg(MSGT_NETWORK,MSGL_ERR,"unknown ASF streaming type\n");
799 goto err_out;
801 // Check if we got a redirect.
802 } while(!done);
804 stream->fd = fd;
805 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
806 stream->streaming_ctrl->streaming_read = nop_streaming_read;
807 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
808 } else {
809 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
810 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
811 stream->streaming_ctrl->buffering = 1;
813 stream->streaming_ctrl->status = streaming_playing_e;
814 stream->close = close_s;
816 http_free( http_hdr );
817 return 0;
819 err_out:
820 if (fd > 0)
821 closesocket(fd);
822 stream->fd = -1;
823 http_free(http_hdr);
824 return -1;
827 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
828 URL_t *url;
830 stream->streaming_ctrl = streaming_ctrl_new();
831 if( stream->streaming_ctrl==NULL ) {
832 return STREAM_ERROR;
834 stream->streaming_ctrl->audio_id_ptr = &stream->opts->audio_id;
835 stream->streaming_ctrl->video_id_ptr = &stream->opts->video_id;
836 stream->streaming_ctrl->bandwidth = network_bandwidth;
837 url = url_new(stream->url);
838 stream->streaming_ctrl->url = check4proxies(url);
839 url_free(url);
841 mp_tmsg(MSGT_OPEN, MSGL_INFO, "STREAM_ASF, URL: %s\n", stream->url);
842 if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
843 streaming_ctrl_free(stream->streaming_ctrl);
844 stream->streaming_ctrl = NULL;
845 return STREAM_UNSUPPORTED;
848 if(asf_streaming_start(stream, file_format) < 0) {
849 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed, exiting.\n");
850 streaming_ctrl_free(stream->streaming_ctrl);
851 stream->streaming_ctrl = NULL;
852 return STREAM_UNSUPPORTED;
855 if (*file_format != DEMUXER_TYPE_PLAYLIST)
856 *file_format = DEMUXER_TYPE_ASF;
857 stream->type = STREAMTYPE_STREAM;
858 return STREAM_OK;
861 const stream_info_t stream_info_asf = {
862 "mms and mms over http streaming",
863 "null",
864 "Bertrand, Reimar Doeffinger, Albeu",
865 "originally based on work by Majormms (is that code still there?)",
866 open_s,
867 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL},
868 NULL,
869 0 // Urls are an option string