Merge svn changes up to r28204
[mplayer/glamo.git] / stream / asf_streaming.c
blob98859cfe9ecdad8beb2a5b937e9de31e4f8891f7
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 #ifdef 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 "libavutil/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_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
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.
57 //
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, "mmshttp"))
90 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
91 fd = asf_http_streaming_start( stream, demuxer_type );
92 stream->streaming_ctrl->url->port = port;
93 if( fd>-1 ) return fd;
94 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
95 if( fd==-2 ) return -1;
98 //everything failed
99 return -1;
102 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
104 printf("ASF stream chunck size=%d\n", stream_chunck->size);
105 printf("length: %d\n", length );
106 printf("0x%02X\n", stream_chunck->type );
108 if( drop_packet!=NULL ) *drop_packet = 0;
110 if( stream_chunck->size<8 ) {
111 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
112 return -1;
114 if( stream_chunck->size!=stream_chunck->size_confirm ) {
115 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, stream_chunck->size, stream_chunck->size_confirm);
116 return -1;
119 printf(" type: 0x%02X\n", stream_chunck->type );
120 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
121 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
122 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
123 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
125 switch(stream_chunck->type) {
126 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
127 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
128 if( drop_packet!=NULL ) *drop_packet = 1;
129 return stream_chunck->size;
130 break;
131 case ASF_STREAMING_DATA: // $D Data follows
132 // printf("=====> Data follows\n");
133 break;
134 case ASF_STREAMING_END_TRANS: // $E Transfer complete
135 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
136 if( drop_packet!=NULL ) *drop_packet = 1;
137 return stream_chunck->size;
138 break;
139 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
140 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
141 break;
142 default:
143 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
145 return stream_chunck->size+4;
148 static void close_s(stream_t *stream) {
149 close(stream->fd);
150 stream->fd=-1;
153 static int max_idx(int s_count, int *s_rates, int bound) {
154 int i, best = -1, rate = -1;
155 for (i = 0; i < s_count; i++) {
156 if (s_rates[i] > rate && s_rates[i] <= bound) {
157 rate = s_rates[i];
158 best = i;
161 return best;
164 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
165 ASF_stream_chunck_t chunk;
166 asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
167 char* buffer=NULL, *chunk_buffer=NULL;
168 int i,r,size,pos = 0;
169 int start;
170 int buffer_size = 0;
171 int chunk_size2read = 0;
172 int bw = streaming_ctrl->bandwidth;
173 int *v_rates = NULL, *a_rates = NULL;
174 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
176 if(asf_ctrl == NULL) return -1;
178 // The ASF header can be in several network chunks. For example if the content description
179 // is big, the ASF header will be split in 2 network chunk.
180 // So we need to retrieve all the chunk before starting to parse the header.
181 do {
182 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
183 return -1;
184 // Endian handling of the stream chunk
185 le2me_ASF_stream_chunck_t(&chunk);
186 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
187 if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
188 if(size < 0){
189 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
190 return -1;
192 if (chunk.type != ASF_STREAMING_HEADER) {
193 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
194 return -1;
197 // audit: do not overflow buffer_size
198 if (size > SIZE_MAX - buffer_size) return -1;
199 buffer = malloc(size+buffer_size);
200 if(buffer == NULL) {
201 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
202 return -1;
204 if( chunk_buffer!=NULL ) {
205 memcpy( buffer, chunk_buffer, buffer_size );
206 free( chunk_buffer );
208 chunk_buffer = buffer;
209 buffer += buffer_size;
210 buffer_size += size;
212 if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
213 return -1;
215 if( chunk_size2read==0 ) {
216 ASF_header_t *asfh = (ASF_header_t *)buffer;
217 if(size < (int)sizeof(ASF_header_t)) {
218 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
219 return -1;
220 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
221 chunk_size2read = AV_RL64(&asfh->objh.size);
222 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
224 } while( buffer_size<chunk_size2read);
225 buffer = chunk_buffer;
226 size = buffer_size;
228 start = sizeof(ASF_header_t);
230 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
231 if (pos >= 0) {
232 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
233 pos += sizeof(ASF_file_header_t);
234 if (pos > size) goto len_err_out;
236 if(fileh.packetsize != fileh.packetsize2) {
237 printf("Error packetsize check don't match\n");
238 return -1;
241 asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
242 // before playing.
243 // preroll: time in ms to bufferize before playing
244 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
247 pos = start;
248 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
250 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
251 pos += sizeof(ASF_stream_header_t);
252 if (pos > size) goto len_err_out;
253 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
254 case 0xF8699E40 : // audio stream
255 if(asf_ctrl->audio_streams == NULL){
256 asf_ctrl->audio_streams = malloc(sizeof(int));
257 asf_ctrl->n_audio = 1;
258 } else {
259 asf_ctrl->n_audio++;
260 asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
261 asf_ctrl->n_audio*sizeof(int));
263 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
264 break;
265 case 0xBC19EFC0 : // video stream
266 if(asf_ctrl->video_streams == NULL){
267 asf_ctrl->video_streams = malloc(sizeof(int));
268 asf_ctrl->n_video = 1;
269 } else {
270 asf_ctrl->n_video++;
271 asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
272 asf_ctrl->n_video*sizeof(int));
274 asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
275 break;
279 // always allocate to avoid lots of ifs later
280 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
281 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
283 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
284 if (pos >= 0) {
285 // stream bitrate properties object
286 int stream_count;
287 char *ptr = &buffer[pos];
288 char *end = &buffer[size];
290 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
291 if (ptr + 2 > end) goto len_err_out;
292 stream_count = AV_RL16(ptr);
293 ptr += 2;
294 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
295 stream_count, stream_count );
296 for( i=0 ; i<stream_count ; i++ ) {
297 uint32_t rate;
298 int id;
299 int j;
300 if (ptr + 6 > end) goto len_err_out;
301 id = AV_RL16(ptr);
302 ptr += 2;
303 rate = AV_RL32(ptr);
304 ptr += 4;
305 mp_msg(MSGT_NETWORK, MSGL_V,
306 " stream id=[0x%x][%u]\n", id, id);
307 mp_msg(MSGT_NETWORK, MSGL_V,
308 " max bitrate=[0x%x][%u]\n", rate, rate);
309 for (j = 0; j < asf_ctrl->n_video; j++) {
310 if (id == asf_ctrl->video_streams[j]) {
311 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
312 v_rates[j] = rate;
313 break;
316 for (j = 0; j < asf_ctrl->n_audio; j++) {
317 if (id == asf_ctrl->audio_streams[j]) {
318 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
319 a_rates[j] = rate;
320 break;
325 free(buffer);
327 // automatic stream selection based on bandwidth
328 if (bw == 0) bw = INT_MAX;
329 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
331 if (asf_ctrl->n_audio) {
332 // find lowest-bitrate audio stream
333 a_rate = a_rates[0];
334 a_idx = 0;
335 for (i = 0; i < asf_ctrl->n_audio; i++) {
336 if (a_rates[i] < a_rate) {
337 a_rate = a_rates[i];
338 a_idx = i;
341 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
342 // both audio and video are not possible, try video only next
343 a_idx = -1;
344 a_rate = 0;
347 // find best video stream
348 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
349 if (v_idx >= 0)
350 v_rate = v_rates[v_idx];
352 // find best audio stream
353 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
355 free(v_rates);
356 free(a_rates);
358 if (a_idx < 0 && v_idx < 0) {
359 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
360 return -1;
363 if (*streaming_ctrl->audio_id_ptr > 0)
364 // a audio stream was forced
365 asf_ctrl->audio_id = *streaming_ctrl->audio_id_ptr;
366 else if (a_idx >= 0)
367 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
368 else if (asf_ctrl->n_audio) {
369 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
370 *streaming_ctrl->audio_id_ptr = -2;
373 if (*streaming_ctrl->video_id_ptr > 0)
374 // a video stream was forced
375 asf_ctrl->video_id = *streaming_ctrl->video_id_ptr;
376 else if (v_idx >= 0)
377 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
378 else if (asf_ctrl->n_video) {
379 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
380 *streaming_ctrl->video_id_ptr = -2;
383 return 1;
385 len_err_out:
386 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
387 if (buffer) free(buffer);
388 if (v_rates) free(v_rates);
389 if (a_rates) free(a_rates);
390 return -1;
393 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
394 static ASF_stream_chunck_t chunk;
395 int read,chunk_size = 0;
396 static int rest = 0, drop_chunk = 0, waiting = 0;
397 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
399 while(1) {
400 if (rest == 0 && waiting == 0) {
401 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
402 return -1;
404 // Endian handling of the stream chunk
405 le2me_ASF_stream_chunck_t(&chunk);
406 chunk_size = asf_streaming( &chunk, &drop_chunk );
407 if(chunk_size < 0) {
408 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
409 return -1;
411 chunk_size -= sizeof(ASF_stream_chunck_t);
413 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
414 if (asf_http_ctrl->packet_size < chunk_size) {
415 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
416 return -1;
418 waiting = asf_http_ctrl->packet_size;
419 } else {
420 waiting = chunk_size;
423 } else if (rest){
424 chunk_size = rest;
425 rest = 0;
428 read = 0;
429 if ( waiting >= chunk_size) {
430 if (chunk_size > size){
431 rest = chunk_size - size;
432 chunk_size = size;
434 if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
435 return -1;
436 read = chunk_size;
437 waiting -= read;
438 if (drop_chunk) continue;
440 if (rest == 0 && waiting > 0 && size-read > 0) {
441 int s = FFMIN(waiting,size-read);
442 memset(buffer+read,0,s);
443 waiting -= s;
444 read += s;
446 break;
449 return read;
452 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
453 return -1;
454 // to shut up gcc warning
455 fd++;
456 pos++;
457 streaming_ctrl=NULL;
460 static int asf_header_check( HTTP_header_t *http_hdr ) {
461 ASF_obj_header_t *objh;
462 if( http_hdr==NULL ) return -1;
463 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
465 objh = (ASF_obj_header_t*)http_hdr->body;
466 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
467 return -1;
470 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
471 if( content_type==NULL ) return ASF_Unknown_e;
472 if( !strcasecmp(content_type, "application/octet-stream") ||
473 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
474 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
475 !strcasecmp(content_type, "video/x-ms-asf")) {
477 if( strstr(features, "broadcast") ) {
478 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
479 return ASF_Live_e;
480 } else {
481 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
482 return ASF_Prerecorded_e;
484 } else {
485 // Ok in a perfect world, web servers should be well configured
486 // so we could used mime type to know the stream type,
487 // but guess what? All of them are not well configured.
488 // So we have to check for an asf header :(, but it works :p
489 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
490 if( asf_header_check( http_hdr )==0 ) {
491 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
492 return ASF_PlainText_e;
493 } else if( (!strcasecmp(content_type, "text/html")) ) {
494 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
495 return ASF_Unknown_e;
496 } else {
497 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
498 return ASF_Redirector_e;
500 } else {
501 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
502 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
503 (!strcasecmp(content_type, "video/x-ms-asf")) ||
504 (!strcasecmp(content_type, "video/x-ms-afs")) ||
505 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
506 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
507 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
508 return ASF_Redirector_e;
509 } else if( !strcasecmp(content_type, "text/plain") ) {
510 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
511 return ASF_PlainText_e;
512 } else {
513 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
514 return ASF_Unknown_e;
518 return ASF_Unknown_e;
521 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
522 HTTP_header_t *http_hdr;
523 URL_t *url = NULL;
524 URL_t *server_url = NULL;
525 asf_http_streaming_ctrl_t *asf_http_ctrl;
526 char str[250];
527 char *ptr;
528 int i, enable;
530 int offset_hi=0, offset_lo=0, length=0;
531 int asf_nb_stream=0, stream_id;
533 // Sanity check
534 if( streaming_ctrl==NULL ) return NULL;
535 url = streaming_ctrl->url;
536 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
537 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
539 // Common header for all requests.
540 http_hdr = http_new_header();
541 http_set_field( http_hdr, "Accept: */*" );
542 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
543 http_add_basic_authentication( http_hdr, url->username, url->password );
545 // Check if we are using a proxy
546 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
547 server_url = url_new( (url->file)+1 );
548 if( server_url==NULL ) {
549 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
550 http_free( http_hdr );
551 return NULL;
553 http_set_uri( http_hdr, server_url->url );
554 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
555 url_free( server_url );
556 } else {
557 http_set_uri( http_hdr, url->file );
558 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
561 http_set_field( http_hdr, str );
562 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
563 sprintf(str,
564 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
565 offset_hi, offset_lo, asf_http_ctrl->request, length );
566 http_set_field( http_hdr, str );
568 switch( asf_http_ctrl->streaming_type ) {
569 case ASF_Live_e:
570 case ASF_Prerecorded_e:
571 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
572 ptr = str;
573 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
574 if(asf_http_ctrl->n_audio > 0) {
575 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
576 stream_id = asf_http_ctrl->audio_streams[i];
577 if(stream_id == asf_http_ctrl->audio_id) {
578 enable = 0;
579 } else {
580 enable = 2;
581 continue;
583 asf_nb_stream++;
584 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
587 if(asf_http_ctrl->n_video > 0) {
588 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
589 stream_id = asf_http_ctrl->video_streams[i];
590 if(stream_id == asf_http_ctrl->video_id) {
591 enable = 0;
592 } else {
593 enable = 2;
594 continue;
596 asf_nb_stream++;
597 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
600 http_set_field( http_hdr, str );
601 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
602 http_set_field( http_hdr, str );
603 break;
604 case ASF_Redirector_e:
605 break;
606 case ASF_Unknown_e:
607 // First request goes here.
608 break;
609 default:
610 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
613 http_set_field( http_hdr, "Connection: Close" );
614 http_build_request( http_hdr );
616 return http_hdr;
619 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
620 char *content_type, *pragma;
621 char features[64] = "\0";
622 size_t len;
623 if( http_response_parse(http_hdr)<0 ) {
624 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
625 return -1;
627 switch( http_hdr->status_code ) {
628 case 200:
629 break;
630 case 401: // Authentication required
631 return ASF_Authenticate_e;
632 default:
633 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
634 return -1;
637 content_type = http_get_field( http_hdr, "Content-Type");
638 //printf("Content-Type: [%s]\n", content_type);
640 pragma = http_get_field( http_hdr, "Pragma");
641 while( pragma!=NULL ) {
642 char *comma_ptr=NULL;
643 char *end;
644 //printf("Pragma: [%s]\n", pragma );
645 // The pragma line can get severals attributes
646 // separeted with a comma ','.
647 do {
648 if( !strncasecmp( pragma, "features=", 9) ) {
649 pragma += 9;
650 end = strstr( pragma, "," );
651 if( end==NULL ) {
652 len = strlen(pragma);
653 } else {
654 len = (unsigned int)(end-pragma);
656 if(len > sizeof(features) - 1) {
657 mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
658 len = sizeof(features) - 1;
660 strncpy( features, pragma, len );
661 features[len]='\0';
662 break;
664 comma_ptr = strstr( pragma, "," );
665 if( comma_ptr!=NULL ) {
666 pragma = comma_ptr+1;
667 if( pragma[0]==' ' ) pragma++;
669 } while( comma_ptr!=NULL );
670 pragma = http_get_next_field( http_hdr );
672 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
673 return 0;
676 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
677 HTTP_header_t *http_hdr=NULL;
678 URL_t *url = stream->streaming_ctrl->url;
679 asf_http_streaming_ctrl_t *asf_http_ctrl;
680 char buffer[BUFFER_SIZE];
681 int i, ret;
682 int fd = stream->fd;
683 int done;
684 int auth_retry = 0;
686 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
687 if( asf_http_ctrl==NULL ) {
688 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
689 return -1;
691 asf_http_ctrl->streaming_type = ASF_Unknown_e;
692 asf_http_ctrl->request = 1;
693 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
694 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
695 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
697 do {
698 done = 1;
699 if( fd>0 ) closesocket( fd );
701 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
702 if( url->port==0 ) url->port = 8080;
703 } else {
704 if( url->port==0 ) url->port = 80;
706 fd = connect2Server( url->hostname, url->port, 1);
707 if( fd<0 ) return fd;
709 http_hdr = asf_http_request( stream->streaming_ctrl );
710 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
711 for(i=0; i < (int)http_hdr->buffer_size ; ) {
712 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
713 if(r <0) {
714 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
715 goto err_out;
717 i += r;
719 http_free( http_hdr );
720 http_hdr = http_new_header();
721 do {
722 i = recv( fd, buffer, BUFFER_SIZE, 0 );
723 //printf("read: %d\n", i );
724 if( i<=0 ) {
725 perror("read");
726 goto err_out;
728 http_response_append( http_hdr, buffer, i );
729 } while( !http_is_header_entire( http_hdr ) );
730 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
731 http_hdr->buffer[http_hdr->buffer_size]='\0';
732 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
734 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
735 if( ret<0 ) {
736 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
737 goto err_out;
739 switch( asf_http_ctrl->streaming_type ) {
740 case ASF_Live_e:
741 case ASF_Prerecorded_e:
742 case ASF_PlainText_e:
743 if( http_hdr->body_size>0 ) {
744 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
745 goto err_out;
748 if( asf_http_ctrl->request==1 ) {
749 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
750 // First request, we only got the ASF header.
751 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
752 if(ret < 0) goto err_out;
753 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
754 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
755 goto err_out;
757 asf_http_ctrl->request++;
758 done = 0;
759 } else {
760 done = 1;
763 break;
764 case ASF_Redirector_e:
765 if( http_hdr->body_size>0 ) {
766 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
767 goto err_out;
770 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
771 done = 1;
772 break;
773 case ASF_Authenticate_e:
774 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
775 asf_http_ctrl->streaming_type = ASF_Unknown_e;
776 done = 0;
777 break;
778 case ASF_Unknown_e:
779 default:
780 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
781 goto err_out;
783 // Check if we got a redirect.
784 } while(!done);
786 stream->fd = fd;
787 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
788 stream->streaming_ctrl->streaming_read = nop_streaming_read;
789 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
790 } else {
791 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
792 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
793 stream->streaming_ctrl->buffering = 1;
795 stream->streaming_ctrl->status = streaming_playing_e;
796 stream->close = close_s;
798 http_free( http_hdr );
799 return 0;
801 err_out:
802 if (fd > 0)
803 closesocket(fd);
804 stream->fd = -1;
805 http_free(http_hdr);
806 return -1;
809 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
810 URL_t *url;
812 stream->streaming_ctrl = streaming_ctrl_new();
813 if( stream->streaming_ctrl==NULL ) {
814 return STREAM_ERROR;
816 stream->streaming_ctrl->audio_id_ptr = &stream->opts->audio_id;
817 stream->streaming_ctrl->video_id_ptr = &stream->opts->video_id;
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