Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / stream / asf_streaming.c
blob856cc1cd124e164ef279d5f3bbaf3a310a8daf81
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 #ifndef HAVE_WINSOCK2
13 #define closesocket close
14 #else
15 #include <winsock2.h>
16 #endif
18 #include "url.h"
19 #include "http.h"
20 #include "libmpdemux/asf.h"
22 #include "stream.h"
23 #include "libmpdemux/demuxer.h"
25 #include "network.h"
26 #include "tcp.h"
28 #include "libavutil/intreadwrite.h"
30 #ifdef ARCH_X86
31 #define ASF_LOAD_GUID_PREFIX(guid) (*(uint32_t *)(guid))
32 #else
33 #define ASF_LOAD_GUID_PREFIX(guid) \
34 ((guid)[3] << 24 | (guid)[2] << 16 | (guid)[1] << 8 | (guid)[0])
35 #endif
37 extern int network_bandwidth;
39 int asf_mmst_streaming_start( stream_t *stream );
40 static int asf_http_streaming_start(stream_t *stream, int *demuxer_type);
42 static int asf_read_wrapper(int fd, void *buffer, int len, streaming_ctrl_t *stream_ctrl) {
43 uint8_t *buf = buffer;
44 while (len > 0) {
45 int got = nop_streaming_read(fd, buf, len, stream_ctrl);
46 if (got <= 0) {
47 mp_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
48 return got;
50 buf += got;
51 len -= got;
53 return 1;
56 // We can try several protocol for asf streaming
57 // * first the UDP protcol, if there is a firewall, UDP
58 // packets will not come back, so the mmsu will fail.
59 // * Then we can try TCP, but if there is a proxy for
60 // internet connection, the TCP connection will not get
61 // through
62 // * Then we can try HTTP.
63 //
64 // Note: Using WMP sequence MMSU then MMST and then HTTP.
66 static int asf_streaming_start( stream_t *stream, int *demuxer_type) {
67 char *proto = stream->streaming_ctrl->url->protocol;
68 int fd = -1;
69 int port = stream->streaming_ctrl->url->port;
71 // Is protocol mms or mmsu?
72 if (!strcasecmp(proto, "mmsu") || !strcasecmp(proto, "mms"))
74 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/UDP...\n");
75 //fd = asf_mmsu_streaming_start( stream );
76 if( fd>-1 ) return fd; //mmsu support is not implemented yet - using this code
77 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/UDP failed\n");
78 if( fd==-2 ) return -1;
81 //Is protocol mms or mmst?
82 if (!strcasecmp(proto, "mmst") || !strcasecmp(proto, "mms"))
84 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/TCP...\n");
85 fd = asf_mmst_streaming_start( stream );
86 stream->streaming_ctrl->url->port = port;
87 if( fd>-1 ) return fd;
88 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/TCP failed\n");
89 if( fd==-2 ) return -1;
92 //Is protocol http, http_proxy, or mms?
93 if (!strcasecmp(proto, "http_proxy") || !strcasecmp(proto, "http") ||
94 !strcasecmp(proto, "mms") || !strcasecmp(proto, "mmshttp"))
96 mp_msg(MSGT_NETWORK,MSGL_V,"Trying ASF/HTTP...\n");
97 fd = asf_http_streaming_start( stream, demuxer_type );
98 stream->streaming_ctrl->url->port = port;
99 if( fd>-1 ) return fd;
100 mp_msg(MSGT_NETWORK,MSGL_V," ===> ASF/HTTP failed\n");
101 if( fd==-2 ) return -1;
104 //everything failed
105 return -1;
108 static int asf_streaming(ASF_stream_chunck_t *stream_chunck, int *drop_packet ) {
110 printf("ASF stream chunck size=%d\n", stream_chunck->size);
111 printf("length: %d\n", length );
112 printf("0x%02X\n", stream_chunck->type );
114 if( drop_packet!=NULL ) *drop_packet = 0;
116 if( stream_chunck->size<8 ) {
117 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
118 return -1;
120 if( stream_chunck->size!=stream_chunck->size_confirm ) {
121 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, stream_chunck->size, stream_chunck->size_confirm);
122 return -1;
125 printf(" type: 0x%02X\n", stream_chunck->type );
126 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
127 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
128 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
129 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
131 switch(stream_chunck->type) {
132 case ASF_STREAMING_CLEAR: // $C Clear ASF configuration
133 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Clearing ASF stream configuration!\n");
134 if( drop_packet!=NULL ) *drop_packet = 1;
135 return stream_chunck->size;
136 break;
137 case ASF_STREAMING_DATA: // $D Data follows
138 // printf("=====> Data follows\n");
139 break;
140 case ASF_STREAMING_END_TRANS: // $E Transfer complete
141 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Transfer complete\n");
142 if( drop_packet!=NULL ) *drop_packet = 1;
143 return stream_chunck->size;
144 break;
145 case ASF_STREAMING_HEADER: // $H ASF header chunk follows
146 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF header chunk follows\n");
147 break;
148 default:
149 mp_msg(MSGT_NETWORK,MSGL_V,"=====> Unknown stream type 0x%x\n", stream_chunck->type );
151 return stream_chunck->size+4;
154 extern int find_asf_guid(char *buf, const char *guid, int cur_pos, int buf_len);
155 extern const char asf_file_header_guid[];
156 extern const char asf_stream_header_guid[];
157 extern const char asf_stream_group_guid[];
158 extern int audio_id;
159 extern int video_id;
161 static void close_s(stream_t *stream) {
162 close(stream->fd);
163 stream->fd=-1;
166 static int max_idx(int s_count, int *s_rates, int bound) {
167 int i, best = -1, rate = -1;
168 for (i = 0; i < s_count; i++) {
169 if (s_rates[i] > rate && s_rates[i] <= bound) {
170 rate = s_rates[i];
171 best = i;
174 return best;
177 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
178 ASF_stream_chunck_t chunk;
179 asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
180 char* buffer=NULL, *chunk_buffer=NULL;
181 int i,r,size,pos = 0;
182 int start;
183 int buffer_size = 0;
184 int chunk_size2read = 0;
185 int bw = streaming_ctrl->bandwidth;
186 int *v_rates = NULL, *a_rates = NULL;
187 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
189 if(asf_ctrl == NULL) return -1;
191 // The ASF header can be in several network chunks. For example if the content description
192 // is big, the ASF header will be split in 2 network chunk.
193 // So we need to retrieve all the chunk before starting to parse the header.
194 do {
195 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
196 return -1;
197 // Endian handling of the stream chunk
198 le2me_ASF_stream_chunck_t(&chunk);
199 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
200 if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
201 if(size < 0){
202 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
203 return -1;
205 if (chunk.type != ASF_STREAMING_HEADER) {
206 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
207 return -1;
210 // audit: do not overflow buffer_size
211 if (size > SIZE_MAX - buffer_size) return -1;
212 buffer = malloc(size+buffer_size);
213 if(buffer == NULL) {
214 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
215 return -1;
217 if( chunk_buffer!=NULL ) {
218 memcpy( buffer, chunk_buffer, buffer_size );
219 free( chunk_buffer );
221 chunk_buffer = buffer;
222 buffer += buffer_size;
223 buffer_size += size;
225 if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
226 return -1;
228 if( chunk_size2read==0 ) {
229 ASF_header_t *asfh = (ASF_header_t *)buffer;
230 if(size < (int)sizeof(ASF_header_t)) {
231 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
232 return -1;
233 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
234 chunk_size2read = AV_RL64(&asfh->objh.size);
235 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
237 } while( buffer_size<chunk_size2read);
238 buffer = chunk_buffer;
239 size = buffer_size;
241 start = sizeof(ASF_header_t);
243 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
244 if (pos >= 0) {
245 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
246 pos += sizeof(ASF_file_header_t);
247 if (pos > size) goto len_err_out;
249 if(fileh.packetsize != fileh.packetsize2) {
250 printf("Error packetsize check don't match\n");
251 return -1;
254 asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
255 // before playing.
256 // preroll: time in ms to bufferize before playing
257 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
260 pos = start;
261 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
263 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
264 pos += sizeof(ASF_stream_header_t);
265 if (pos > size) goto len_err_out;
266 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
267 case 0xF8699E40 : // audio stream
268 if(asf_ctrl->audio_streams == NULL){
269 asf_ctrl->audio_streams = malloc(sizeof(int));
270 asf_ctrl->n_audio = 1;
271 } else {
272 asf_ctrl->n_audio++;
273 asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
274 asf_ctrl->n_audio*sizeof(int));
276 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
277 break;
278 case 0xBC19EFC0 : // video stream
279 if(asf_ctrl->video_streams == NULL){
280 asf_ctrl->video_streams = malloc(sizeof(int));
281 asf_ctrl->n_video = 1;
282 } else {
283 asf_ctrl->n_video++;
284 asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
285 asf_ctrl->n_video*sizeof(int));
287 asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
288 break;
292 // always allocate to avoid lots of ifs later
293 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
294 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
296 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
297 if (pos >= 0) {
298 // stream bitrate properties object
299 int stream_count;
300 char *ptr = &buffer[pos];
301 char *end = &buffer[size];
303 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
304 if (ptr + 2 > end) goto len_err_out;
305 stream_count = AV_RL16(ptr);
306 ptr += 2;
307 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
308 stream_count, stream_count );
309 for( i=0 ; i<stream_count ; i++ ) {
310 uint32_t rate;
311 int id;
312 int j;
313 if (ptr + 6 > end) goto len_err_out;
314 id = AV_RL16(ptr);
315 ptr += 2;
316 rate = AV_RL32(ptr);
317 ptr += 4;
318 mp_msg(MSGT_NETWORK, MSGL_V,
319 " stream id=[0x%x][%u]\n", id, id);
320 mp_msg(MSGT_NETWORK, MSGL_V,
321 " max bitrate=[0x%x][%u]\n", rate, rate);
322 for (j = 0; j < asf_ctrl->n_video; j++) {
323 if (id == asf_ctrl->video_streams[j]) {
324 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
325 v_rates[j] = rate;
326 break;
329 for (j = 0; j < asf_ctrl->n_audio; j++) {
330 if (id == asf_ctrl->audio_streams[j]) {
331 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
332 a_rates[j] = rate;
333 break;
338 free(buffer);
340 // automatic stream selection based on bandwidth
341 if (bw == 0) bw = INT_MAX;
342 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
344 if (asf_ctrl->n_audio) {
345 // find lowest-bitrate audio stream
346 a_rate = a_rates[0];
347 a_idx = 0;
348 for (i = 0; i < asf_ctrl->n_audio; i++) {
349 if (a_rates[i] < a_rate) {
350 a_rate = a_rates[i];
351 a_idx = i;
354 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
355 // both audio and video are not possible, try video only next
356 a_idx = -1;
357 a_rate = 0;
360 // find best video stream
361 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
362 if (v_idx >= 0)
363 v_rate = v_rates[v_idx];
365 // find best audio stream
366 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
368 free(v_rates);
369 free(a_rates);
371 if (a_idx < 0 && v_idx < 0) {
372 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
373 return -1;
376 if (audio_id > 0)
377 // a audio stream was forced
378 asf_ctrl->audio_id = audio_id;
379 else if (a_idx >= 0)
380 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
381 else if (asf_ctrl->n_audio) {
382 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
383 audio_id = -2;
386 if (video_id > 0)
387 // a video stream was forced
388 asf_ctrl->video_id = video_id;
389 else if (v_idx >= 0)
390 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
391 else if (asf_ctrl->n_video) {
392 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
393 video_id = -2;
396 return 1;
398 len_err_out:
399 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
400 if (buffer) free(buffer);
401 if (v_rates) free(v_rates);
402 if (a_rates) free(a_rates);
403 return -1;
406 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
407 static ASF_stream_chunck_t chunk;
408 int read,chunk_size = 0;
409 static int rest = 0, drop_chunk = 0, waiting = 0;
410 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
412 while(1) {
413 if (rest == 0 && waiting == 0) {
414 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
415 return -1;
417 // Endian handling of the stream chunk
418 le2me_ASF_stream_chunck_t(&chunk);
419 chunk_size = asf_streaming( &chunk, &drop_chunk );
420 if(chunk_size < 0) {
421 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
422 return -1;
424 chunk_size -= sizeof(ASF_stream_chunck_t);
426 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
427 if (asf_http_ctrl->packet_size < chunk_size) {
428 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
429 return -1;
431 waiting = asf_http_ctrl->packet_size;
432 } else {
433 waiting = chunk_size;
436 } else if (rest){
437 chunk_size = rest;
438 rest = 0;
441 read = 0;
442 if ( waiting >= chunk_size) {
443 if (chunk_size > size){
444 rest = chunk_size - size;
445 chunk_size = size;
447 if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
448 return -1;
449 read = chunk_size;
450 waiting -= read;
451 if (drop_chunk) continue;
453 if (rest == 0 && waiting > 0 && size-read > 0) {
454 int s = FFMIN(waiting,size-read);
455 memset(buffer+read,0,s);
456 waiting -= s;
457 read += s;
459 break;
462 return read;
465 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
466 return -1;
467 // to shut up gcc warning
468 fd++;
469 pos++;
470 streaming_ctrl=NULL;
473 static int asf_header_check( HTTP_header_t *http_hdr ) {
474 ASF_obj_header_t *objh;
475 if( http_hdr==NULL ) return -1;
476 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
478 objh = (ASF_obj_header_t*)http_hdr->body;
479 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
480 return -1;
483 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
484 if( content_type==NULL ) return ASF_Unknown_e;
485 if( !strcasecmp(content_type, "application/octet-stream") ||
486 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
487 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
488 !strcasecmp(content_type, "video/x-ms-asf")) {
490 if( strstr(features, "broadcast") ) {
491 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
492 return ASF_Live_e;
493 } else {
494 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
495 return ASF_Prerecorded_e;
497 } else {
498 // Ok in a perfect world, web servers should be well configured
499 // so we could used mime type to know the stream type,
500 // but guess what? All of them are not well configured.
501 // So we have to check for an asf header :(, but it works :p
502 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
503 if( asf_header_check( http_hdr )==0 ) {
504 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
505 return ASF_PlainText_e;
506 } else if( (!strcasecmp(content_type, "text/html")) ) {
507 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
508 return ASF_Unknown_e;
509 } else {
510 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
511 return ASF_Redirector_e;
513 } else {
514 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
515 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
516 (!strcasecmp(content_type, "video/x-ms-asf")) ||
517 (!strcasecmp(content_type, "video/x-ms-afs")) ||
518 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
519 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
520 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
521 return ASF_Redirector_e;
522 } else if( !strcasecmp(content_type, "text/plain") ) {
523 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
524 return ASF_PlainText_e;
525 } else {
526 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
527 return ASF_Unknown_e;
531 return ASF_Unknown_e;
534 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
535 HTTP_header_t *http_hdr;
536 URL_t *url = NULL;
537 URL_t *server_url = NULL;
538 asf_http_streaming_ctrl_t *asf_http_ctrl;
539 char str[250];
540 char *ptr;
541 int i, enable;
543 int offset_hi=0, offset_lo=0, length=0;
544 int asf_nb_stream=0, stream_id;
546 // Sanity check
547 if( streaming_ctrl==NULL ) return NULL;
548 url = streaming_ctrl->url;
549 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
550 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
552 // Common header for all requests.
553 http_hdr = http_new_header();
554 http_set_field( http_hdr, "Accept: */*" );
555 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
556 http_add_basic_authentication( http_hdr, url->username, url->password );
558 // Check if we are using a proxy
559 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
560 server_url = url_new( (url->file)+1 );
561 if( server_url==NULL ) {
562 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
563 http_free( http_hdr );
564 return NULL;
566 http_set_uri( http_hdr, server_url->url );
567 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
568 url_free( server_url );
569 } else {
570 http_set_uri( http_hdr, url->file );
571 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
574 http_set_field( http_hdr, str );
575 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
576 sprintf(str,
577 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
578 offset_hi, offset_lo, asf_http_ctrl->request, length );
579 http_set_field( http_hdr, str );
581 switch( asf_http_ctrl->streaming_type ) {
582 case ASF_Live_e:
583 case ASF_Prerecorded_e:
584 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
585 ptr = str;
586 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
587 if(asf_http_ctrl->n_audio > 0) {
588 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
589 stream_id = asf_http_ctrl->audio_streams[i];
590 if(stream_id == asf_http_ctrl->audio_id) {
591 enable = 0;
592 } else {
593 enable = 2;
594 continue;
596 asf_nb_stream++;
597 ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
600 if(asf_http_ctrl->n_video > 0) {
601 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
602 stream_id = asf_http_ctrl->video_streams[i];
603 if(stream_id == asf_http_ctrl->video_id) {
604 enable = 0;
605 } else {
606 enable = 2;
607 continue;
609 asf_nb_stream++;
610 ptr += sprintf(ptr, "ffff:%d:%d ", stream_id, enable);
613 http_set_field( http_hdr, str );
614 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
615 http_set_field( http_hdr, str );
616 break;
617 case ASF_Redirector_e:
618 break;
619 case ASF_Unknown_e:
620 // First request goes here.
621 break;
622 default:
623 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
626 http_set_field( http_hdr, "Connection: Close" );
627 http_build_request( http_hdr );
629 return http_hdr;
632 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
633 char *content_type, *pragma;
634 char features[64] = "\0";
635 size_t len;
636 if( http_response_parse(http_hdr)<0 ) {
637 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
638 return -1;
640 switch( http_hdr->status_code ) {
641 case 200:
642 break;
643 case 401: // Authentication required
644 return ASF_Authenticate_e;
645 default:
646 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
647 return -1;
650 content_type = http_get_field( http_hdr, "Content-Type");
651 //printf("Content-Type: [%s]\n", content_type);
653 pragma = http_get_field( http_hdr, "Pragma");
654 while( pragma!=NULL ) {
655 char *comma_ptr=NULL;
656 char *end;
657 //printf("Pragma: [%s]\n", pragma );
658 // The pragma line can get severals attributes
659 // separeted with a comma ','.
660 do {
661 if( !strncasecmp( pragma, "features=", 9) ) {
662 pragma += 9;
663 end = strstr( pragma, "," );
664 if( end==NULL ) {
665 len = strlen(pragma);
666 } else {
667 len = (unsigned int)(end-pragma);
669 if(len > sizeof(features) - 1) {
670 mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
671 len = sizeof(features) - 1;
673 strncpy( features, pragma, len );
674 features[len]='\0';
675 break;
677 comma_ptr = strstr( pragma, "," );
678 if( comma_ptr!=NULL ) {
679 pragma = comma_ptr+1;
680 if( pragma[0]==' ' ) pragma++;
682 } while( comma_ptr!=NULL );
683 pragma = http_get_next_field( http_hdr );
685 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
686 return 0;
689 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
690 HTTP_header_t *http_hdr=NULL;
691 URL_t *url = stream->streaming_ctrl->url;
692 asf_http_streaming_ctrl_t *asf_http_ctrl;
693 char buffer[BUFFER_SIZE];
694 int i, ret;
695 int fd = stream->fd;
696 int done;
697 int auth_retry = 0;
699 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
700 if( asf_http_ctrl==NULL ) {
701 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
702 return -1;
704 asf_http_ctrl->streaming_type = ASF_Unknown_e;
705 asf_http_ctrl->request = 1;
706 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
707 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
708 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
710 do {
711 done = 1;
712 if( fd>0 ) closesocket( fd );
714 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
715 if( url->port==0 ) url->port = 8080;
716 } else {
717 if( url->port==0 ) url->port = 80;
719 fd = connect2Server( url->hostname, url->port, 1);
720 if( fd<0 ) return fd;
722 http_hdr = asf_http_request( stream->streaming_ctrl );
723 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
724 for(i=0; i < (int)http_hdr->buffer_size ; ) {
725 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
726 if(r <0) {
727 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
728 goto err_out;
730 i += r;
732 http_free( http_hdr );
733 http_hdr = http_new_header();
734 do {
735 i = recv( fd, buffer, BUFFER_SIZE, 0 );
736 //printf("read: %d\n", i );
737 if( i<=0 ) {
738 perror("read");
739 goto err_out;
741 http_response_append( http_hdr, buffer, i );
742 } while( !http_is_header_entire( http_hdr ) );
743 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
744 http_hdr->buffer[http_hdr->buffer_size]='\0';
745 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
747 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
748 if( ret<0 ) {
749 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
750 goto err_out;
752 switch( asf_http_ctrl->streaming_type ) {
753 case ASF_Live_e:
754 case ASF_Prerecorded_e:
755 case ASF_PlainText_e:
756 if( http_hdr->body_size>0 ) {
757 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
758 goto err_out;
761 if( asf_http_ctrl->request==1 ) {
762 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
763 // First request, we only got the ASF header.
764 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
765 if(ret < 0) goto err_out;
766 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
767 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
768 goto err_out;
770 asf_http_ctrl->request++;
771 done = 0;
772 } else {
773 done = 1;
776 break;
777 case ASF_Redirector_e:
778 if( http_hdr->body_size>0 ) {
779 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
780 goto err_out;
783 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
784 done = 1;
785 break;
786 case ASF_Authenticate_e:
787 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
788 asf_http_ctrl->streaming_type = ASF_Unknown_e;
789 done = 0;
790 break;
791 case ASF_Unknown_e:
792 default:
793 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
794 goto err_out;
796 // Check if we got a redirect.
797 } while(!done);
799 stream->fd = fd;
800 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
801 stream->streaming_ctrl->streaming_read = nop_streaming_read;
802 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
803 } else {
804 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
805 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
806 stream->streaming_ctrl->buffering = 1;
808 stream->streaming_ctrl->status = streaming_playing_e;
809 stream->close = close_s;
811 http_free( http_hdr );
812 return 0;
814 err_out:
815 if (fd > 0)
816 closesocket(fd);
817 stream->fd = -1;
818 http_free(http_hdr);
819 return -1;
822 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
823 URL_t *url;
825 stream->streaming_ctrl = streaming_ctrl_new();
826 if( stream->streaming_ctrl==NULL ) {
827 return STREAM_ERROR;
829 stream->streaming_ctrl->bandwidth = network_bandwidth;
830 url = url_new(stream->url);
831 stream->streaming_ctrl->url = check4proxies(url);
832 url_free(url);
834 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_MPDEMUX_ASF_InfoStreamASFURL, stream->url);
835 if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
836 streaming_ctrl_free(stream->streaming_ctrl);
837 stream->streaming_ctrl = NULL;
838 return STREAM_UNSUPPORTED;
841 if(asf_streaming_start(stream, file_format) < 0) {
842 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_ASF_StreamingFailed);
843 streaming_ctrl_free(stream->streaming_ctrl);
844 stream->streaming_ctrl = NULL;
845 return STREAM_UNSUPPORTED;
848 if (*file_format != DEMUXER_TYPE_PLAYLIST)
849 *file_format = DEMUXER_TYPE_ASF;
850 stream->type = STREAMTYPE_STREAM;
851 fixup_network_stream_cache(stream);
852 return STREAM_OK;
855 const stream_info_t stream_info_asf = {
856 "mms and mms over http streaming",
857 "null",
858 "Bertrand, Reimar Doeffinger, Albeu",
859 "originally based on work by Majormms (is that code still there?)",
860 open_s,
861 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmshttp", NULL},
862 NULL,
863 0 // Urls are an option string