Use MSGT_DECVIDEO in a video decoder.
[mplayer/glamo.git] / stream / asf_streaming.c
blobf00f5033729aff71381eb870cda7bec62814b3bf
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"
30 #if HAVE_WINSOCK2_H
31 #include <winsock2.h>
32 #endif
34 #include "url.h"
35 #include "http.h"
36 #include "libmpdemux/asf.h"
38 #include "stream.h"
39 #include "libmpdemux/demuxer.h"
40 #include "asf_mmst_streaming.h"
41 #include "network.h"
42 #include "tcp.h"
44 #include "libavutil/intreadwrite.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_msg(MSGT_NETWORK, MSGL_ERR, MSGTR_MPDEMUX_ASF_ErrReadingNetworkStream);
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_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_StreamChunkSize2Small, stream_chunck->size);
129 return -1;
131 if( stream_chunck->size!=stream_chunck->size_confirm ) {
132 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SizeConfirmMismatch, 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 extern int audio_id;
166 extern int video_id;
168 static void close_s(stream_t *stream) {
169 closesocket(stream->fd);
170 stream->fd=-1;
173 static int max_idx(int s_count, int *s_rates, int bound) {
174 int i, best = -1, rate = -1;
175 for (i = 0; i < s_count; i++) {
176 if (s_rates[i] > rate && s_rates[i] <= bound) {
177 rate = s_rates[i];
178 best = i;
181 return best;
184 static int asf_streaming_parse_header(int fd, streaming_ctrl_t* streaming_ctrl) {
185 ASF_stream_chunck_t chunk;
186 asf_http_streaming_ctrl_t* asf_ctrl = streaming_ctrl->data;
187 char* buffer=NULL, *chunk_buffer=NULL;
188 int i,r,size,pos = 0;
189 int start;
190 int buffer_size = 0;
191 int chunk_size2read = 0;
192 int bw = streaming_ctrl->bandwidth;
193 int *v_rates = NULL, *a_rates = NULL;
194 int v_rate = 0, a_rate = 0, a_idx = -1, v_idx = -1;
196 if(asf_ctrl == NULL) return -1;
198 // The ASF header can be in several network chunks. For example if the content description
199 // is big, the ASF header will be split in 2 network chunk.
200 // So we need to retrieve all the chunk before starting to parse the header.
201 do {
202 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
203 return -1;
204 // Endian handling of the stream chunk
205 le2me_ASF_stream_chunck_t(&chunk);
206 size = asf_streaming( &chunk, &r) - sizeof(ASF_stream_chunck_t);
207 if(r) mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_WarnDropHeader);
208 if(size < 0){
209 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
210 return -1;
212 if (chunk.type != ASF_STREAMING_HEADER) {
213 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoHeaderAtFirstChunk);
214 return -1;
217 // audit: do not overflow buffer_size
218 if (size > SIZE_MAX - buffer_size) return -1;
219 buffer = malloc(size+buffer_size);
220 if(buffer == NULL) {
221 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MPDEMUX_ASF_BufferMallocFailed,size+buffer_size);
222 return -1;
224 if( chunk_buffer!=NULL ) {
225 memcpy( buffer, chunk_buffer, buffer_size );
226 free( chunk_buffer );
228 chunk_buffer = buffer;
229 buffer += buffer_size;
230 buffer_size += size;
232 if (asf_read_wrapper(fd, buffer, size, streaming_ctrl) <= 0)
233 return -1;
235 if( chunk_size2read==0 ) {
236 ASF_header_t *asfh = (ASF_header_t *)buffer;
237 if(size < (int)sizeof(ASF_header_t)) {
238 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunk2Small);
239 return -1;
240 } else mp_msg(MSGT_NETWORK,MSGL_DBG2,"Got chunk\n");
241 chunk_size2read = AV_RL64(&asfh->objh.size);
242 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Size 2 read=%d\n", chunk_size2read);
244 } while( buffer_size<chunk_size2read);
245 buffer = chunk_buffer;
246 size = buffer_size;
248 start = sizeof(ASF_header_t);
250 pos = find_asf_guid(buffer, asf_file_header_guid, start, size);
251 if (pos >= 0) {
252 ASF_file_header_t *fileh = (ASF_file_header_t *) &buffer[pos];
253 pos += sizeof(ASF_file_header_t);
254 if (pos > size) goto len_err_out;
256 if(fileh.packetsize != fileh.packetsize2) {
257 printf("Error packetsize check don't match\n");
258 return -1;
261 asf_ctrl->packet_size = AV_RL32(&fileh->max_packet_size);
262 // before playing.
263 // preroll: time in ms to bufferize before playing
264 streaming_ctrl->prebuffer_size = (unsigned int)(((double)fileh->preroll/1000.0)*((double)fileh->max_bitrate/8.0));
267 pos = start;
268 while ((pos = find_asf_guid(buffer, asf_stream_header_guid, pos, size)) >= 0)
270 ASF_stream_header_t *streamh = (ASF_stream_header_t *)&buffer[pos];
271 pos += sizeof(ASF_stream_header_t);
272 if (pos > size) goto len_err_out;
273 switch(ASF_LOAD_GUID_PREFIX(streamh->type)) {
274 case 0xF8699E40 : // audio stream
275 if(asf_ctrl->audio_streams == NULL){
276 asf_ctrl->audio_streams = malloc(sizeof(int));
277 asf_ctrl->n_audio = 1;
278 } else {
279 asf_ctrl->n_audio++;
280 asf_ctrl->audio_streams = realloc(asf_ctrl->audio_streams,
281 asf_ctrl->n_audio*sizeof(int));
283 asf_ctrl->audio_streams[asf_ctrl->n_audio-1] = AV_RL16(&streamh->stream_no);
284 break;
285 case 0xBC19EFC0 : // video stream
286 if(asf_ctrl->video_streams == NULL){
287 asf_ctrl->video_streams = malloc(sizeof(int));
288 asf_ctrl->n_video = 1;
289 } else {
290 asf_ctrl->n_video++;
291 asf_ctrl->video_streams = realloc(asf_ctrl->video_streams,
292 asf_ctrl->n_video*sizeof(int));
294 asf_ctrl->video_streams[asf_ctrl->n_video-1] = AV_RL16(&streamh->stream_no);
295 break;
299 // always allocate to avoid lots of ifs later
300 v_rates = calloc(asf_ctrl->n_video, sizeof(int));
301 a_rates = calloc(asf_ctrl->n_audio, sizeof(int));
303 pos = find_asf_guid(buffer, asf_stream_group_guid, start, size);
304 if (pos >= 0) {
305 // stream bitrate properties object
306 int stream_count;
307 char *ptr = &buffer[pos];
308 char *end = &buffer[size];
310 mp_msg(MSGT_NETWORK, MSGL_V, "Stream bitrate properties object\n");
311 if (ptr + 2 > end) goto len_err_out;
312 stream_count = AV_RL16(ptr);
313 ptr += 2;
314 mp_msg(MSGT_NETWORK, MSGL_V, " stream count=[0x%x][%u]\n",
315 stream_count, stream_count );
316 for( i=0 ; i<stream_count ; i++ ) {
317 uint32_t rate;
318 int id;
319 int j;
320 if (ptr + 6 > end) goto len_err_out;
321 id = AV_RL16(ptr);
322 ptr += 2;
323 rate = AV_RL32(ptr);
324 ptr += 4;
325 mp_msg(MSGT_NETWORK, MSGL_V,
326 " stream id=[0x%x][%u]\n", id, id);
327 mp_msg(MSGT_NETWORK, MSGL_V,
328 " max bitrate=[0x%x][%u]\n", rate, rate);
329 for (j = 0; j < asf_ctrl->n_video; j++) {
330 if (id == asf_ctrl->video_streams[j]) {
331 mp_msg(MSGT_NETWORK, MSGL_V, " is video stream\n");
332 v_rates[j] = rate;
333 break;
336 for (j = 0; j < asf_ctrl->n_audio; j++) {
337 if (id == asf_ctrl->audio_streams[j]) {
338 mp_msg(MSGT_NETWORK, MSGL_V, " is audio stream\n");
339 a_rates[j] = rate;
340 break;
345 free(buffer);
347 // automatic stream selection based on bandwidth
348 if (bw == 0) bw = INT_MAX;
349 mp_msg(MSGT_NETWORK, MSGL_V, "Max bandwidth set to %d\n", bw);
351 if (asf_ctrl->n_audio) {
352 // find lowest-bitrate audio stream
353 a_rate = a_rates[0];
354 a_idx = 0;
355 for (i = 0; i < asf_ctrl->n_audio; i++) {
356 if (a_rates[i] < a_rate) {
357 a_rate = a_rates[i];
358 a_idx = i;
361 if (max_idx(asf_ctrl->n_video, v_rates, bw - a_rate) < 0) {
362 // both audio and video are not possible, try video only next
363 a_idx = -1;
364 a_rate = 0;
367 // find best video stream
368 v_idx = max_idx(asf_ctrl->n_video, v_rates, bw - a_rate);
369 if (v_idx >= 0)
370 v_rate = v_rates[v_idx];
372 // find best audio stream
373 a_idx = max_idx(asf_ctrl->n_audio, a_rates, bw - v_rate);
375 free(v_rates);
376 free(a_rates);
378 if (a_idx < 0 && v_idx < 0) {
379 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_Bandwidth2SmallCannotPlay);
380 return -1;
383 if (audio_id > 0)
384 // a audio stream was forced
385 asf_ctrl->audio_id = audio_id;
386 else if (a_idx >= 0)
387 asf_ctrl->audio_id = asf_ctrl->audio_streams[a_idx];
388 else if (asf_ctrl->n_audio) {
389 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedAudio);
390 audio_id = -2;
393 if (video_id > 0)
394 // a video stream was forced
395 asf_ctrl->video_id = video_id;
396 else if (v_idx >= 0)
397 asf_ctrl->video_id = asf_ctrl->video_streams[v_idx];
398 else if (asf_ctrl->n_video) {
399 mp_msg(MSGT_NETWORK, MSGL_WARN, MSGTR_MPDEMUX_ASF_Bandwidth2SmallDeselectedVideo);
400 video_id = -2;
403 return 1;
405 len_err_out:
406 mp_msg(MSGT_NETWORK, MSGL_FATAL, MSGTR_MPDEMUX_ASF_InvalidLenInHeader);
407 if (buffer) free(buffer);
408 if (v_rates) free(v_rates);
409 if (a_rates) free(a_rates);
410 return -1;
413 static int asf_http_streaming_read( int fd, char *buffer, int size, streaming_ctrl_t *streaming_ctrl ) {
414 static ASF_stream_chunck_t chunk;
415 int read,chunk_size = 0;
416 static int rest = 0, drop_chunk = 0, waiting = 0;
417 asf_http_streaming_ctrl_t *asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
419 while(1) {
420 if (rest == 0 && waiting == 0) {
421 if (asf_read_wrapper(fd, &chunk, sizeof(ASF_stream_chunck_t), streaming_ctrl) <= 0)
422 return -1;
424 // Endian handling of the stream chunk
425 le2me_ASF_stream_chunck_t(&chunk);
426 chunk_size = asf_streaming( &chunk, &drop_chunk );
427 if(chunk_size < 0) {
428 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrorParsingChunkHeader);
429 return -1;
431 chunk_size -= sizeof(ASF_stream_chunck_t);
433 if(chunk.type != ASF_STREAMING_HEADER && (!drop_chunk)) {
434 if (asf_http_ctrl->packet_size < chunk_size) {
435 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ErrChunkBiggerThanPacket);
436 return -1;
438 waiting = asf_http_ctrl->packet_size;
439 } else {
440 waiting = chunk_size;
443 } else if (rest){
444 chunk_size = rest;
445 rest = 0;
448 read = 0;
449 if ( waiting >= chunk_size) {
450 if (chunk_size > size){
451 rest = chunk_size - size;
452 chunk_size = size;
454 if (asf_read_wrapper(fd, buffer, chunk_size, streaming_ctrl) <= 0)
455 return -1;
456 read = chunk_size;
457 waiting -= read;
458 if (drop_chunk) continue;
460 if (rest == 0 && waiting > 0 && size-read > 0) {
461 int s = FFMIN(waiting,size-read);
462 memset(buffer+read,0,s);
463 waiting -= s;
464 read += s;
466 break;
469 return read;
472 static int asf_http_streaming_seek( int fd, off_t pos, streaming_ctrl_t *streaming_ctrl ) {
473 return -1;
474 // to shut up gcc warning
475 fd++;
476 pos++;
477 streaming_ctrl=NULL;
480 static int asf_header_check( HTTP_header_t *http_hdr ) {
481 ASF_obj_header_t *objh;
482 if( http_hdr==NULL ) return -1;
483 if( http_hdr->body==NULL || http_hdr->body_size<sizeof(ASF_obj_header_t) ) return -1;
485 objh = (ASF_obj_header_t*)http_hdr->body;
486 if( ASF_LOAD_GUID_PREFIX(objh->guid)==0x75B22630 ) return 0;
487 return -1;
490 static int asf_http_streaming_type(char *content_type, char *features, HTTP_header_t *http_hdr ) {
491 if( content_type==NULL ) return ASF_Unknown_e;
492 if( !strcasecmp(content_type, "application/octet-stream") ||
493 !strcasecmp(content_type, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
494 !strcasecmp(content_type, "application/x-mms-framed") || // New in Corana, second request
495 !strcasecmp(content_type, "video/x-ms-wmv") ||
496 !strcasecmp(content_type, "video/x-ms-asf")) {
498 if( strstr(features, "broadcast") ) {
499 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Live stream\n");
500 return ASF_Live_e;
501 } else {
502 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Prerecorded\n");
503 return ASF_Prerecorded_e;
505 } else {
506 // Ok in a perfect world, web servers should be well configured
507 // so we could used mime type to know the stream type,
508 // but guess what? All of them are not well configured.
509 // So we have to check for an asf header :(, but it works :p
510 if( http_hdr->body_size>sizeof(ASF_obj_header_t) ) {
511 if( asf_header_check( http_hdr )==0 ) {
512 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
513 return ASF_PlainText_e;
514 } else if( (!strcasecmp(content_type, "text/html")) ) {
515 mp_msg(MSGT_NETWORK,MSGL_V,"=====> HTML, MPlayer is not a browser...yet!\n");
516 return ASF_Unknown_e;
517 } else {
518 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Redirector\n");
519 return ASF_Redirector_e;
521 } else {
522 if( (!strcasecmp(content_type, "audio/x-ms-wax")) ||
523 (!strcasecmp(content_type, "audio/x-ms-wma")) ||
524 (!strcasecmp(content_type, "video/x-ms-asf")) ||
525 (!strcasecmp(content_type, "video/x-ms-afs")) ||
526 (!strcasecmp(content_type, "video/x-ms-wmv")) ||
527 (!strcasecmp(content_type, "video/x-ms-wma")) ) {
528 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ASFRedirector);
529 return ASF_Redirector_e;
530 } else if( !strcasecmp(content_type, "text/plain") ) {
531 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF Plain text\n");
532 return ASF_PlainText_e;
533 } else {
534 mp_msg(MSGT_NETWORK,MSGL_V,"=====> ASF unknown content-type: %s\n", content_type );
535 return ASF_Unknown_e;
539 return ASF_Unknown_e;
542 static HTTP_header_t *asf_http_request(streaming_ctrl_t *streaming_ctrl) {
543 HTTP_header_t *http_hdr;
544 URL_t *url = NULL;
545 URL_t *server_url = NULL;
546 asf_http_streaming_ctrl_t *asf_http_ctrl;
547 char str[250];
548 char *ptr;
549 int i, enable;
551 int offset_hi=0, offset_lo=0, length=0;
552 int asf_nb_stream=0, stream_id;
554 // Sanity check
555 if( streaming_ctrl==NULL ) return NULL;
556 url = streaming_ctrl->url;
557 asf_http_ctrl = (asf_http_streaming_ctrl_t*)streaming_ctrl->data;
558 if( url==NULL || asf_http_ctrl==NULL ) return NULL;
560 // Common header for all requests.
561 http_hdr = http_new_header();
562 http_set_field( http_hdr, "Accept: */*" );
563 http_set_field( http_hdr, "User-Agent: NSPlayer/4.1.0.3856" );
564 http_add_basic_authentication( http_hdr, url->username, url->password );
566 // Check if we are using a proxy
567 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
568 server_url = url_new( (url->file)+1 );
569 if( server_url==NULL ) {
570 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_InvalidProxyURL);
571 http_free( http_hdr );
572 return NULL;
574 http_set_uri( http_hdr, server_url->url );
575 sprintf( str, "Host: %.220s:%d", server_url->hostname, server_url->port );
576 url_free( server_url );
577 } else {
578 http_set_uri( http_hdr, url->file );
579 sprintf( str, "Host: %.220s:%d", url->hostname, url->port );
582 http_set_field( http_hdr, str );
583 http_set_field( http_hdr, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
584 sprintf(str,
585 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
586 offset_hi, offset_lo, asf_http_ctrl->request, length );
587 http_set_field( http_hdr, str );
589 switch( asf_http_ctrl->streaming_type ) {
590 case ASF_Live_e:
591 case ASF_Prerecorded_e:
592 http_set_field( http_hdr, "Pragma: xPlayStrm=1" );
593 ptr = str;
594 ptr += sprintf( ptr, "Pragma: stream-switch-entry=");
595 if(asf_http_ctrl->n_audio > 0) {
596 for( i=0; i<asf_http_ctrl->n_audio ; i++ ) {
597 stream_id = asf_http_ctrl->audio_streams[i];
598 if(stream_id == asf_http_ctrl->audio_id) {
599 enable = 0;
600 } else {
601 enable = 2;
602 continue;
604 asf_nb_stream++;
605 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
608 if(asf_http_ctrl->n_video > 0) {
609 for( i=0; i<asf_http_ctrl->n_video ; i++ ) {
610 stream_id = asf_http_ctrl->video_streams[i];
611 if(stream_id == asf_http_ctrl->video_id) {
612 enable = 0;
613 } else {
614 enable = 2;
615 continue;
617 asf_nb_stream++;
618 ptr += sprintf(ptr, "ffff:%x:%d ", stream_id, enable);
621 http_set_field( http_hdr, str );
622 sprintf( str, "Pragma: stream-switch-count=%d", asf_nb_stream );
623 http_set_field( http_hdr, str );
624 break;
625 case ASF_Redirector_e:
626 break;
627 case ASF_Unknown_e:
628 // First request goes here.
629 break;
630 default:
631 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamType);
634 http_set_field( http_hdr, "Connection: Close" );
635 http_build_request( http_hdr );
637 return http_hdr;
640 static int asf_http_parse_response(asf_http_streaming_ctrl_t *asf_http_ctrl, HTTP_header_t *http_hdr ) {
641 char *content_type, *pragma;
642 char features[64] = "\0";
643 size_t len;
644 if( http_response_parse(http_hdr)<0 ) {
645 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_Failed2ParseHTTPResponse);
646 return -1;
648 switch( http_hdr->status_code ) {
649 case 200:
650 break;
651 case 401: // Authentication required
652 return ASF_Authenticate_e;
653 default:
654 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_ServerReturn, http_hdr->status_code, http_hdr->reason_phrase);
655 return -1;
658 content_type = http_get_field( http_hdr, "Content-Type");
659 //printf("Content-Type: [%s]\n", content_type);
661 pragma = http_get_field( http_hdr, "Pragma");
662 while( pragma!=NULL ) {
663 char *comma_ptr=NULL;
664 char *end;
665 //printf("Pragma: [%s]\n", pragma );
666 // The pragma line can get severals attributes
667 // separeted with a comma ','.
668 do {
669 if( !strncasecmp( pragma, "features=", 9) ) {
670 pragma += 9;
671 end = strstr( pragma, "," );
672 if( end==NULL ) {
673 len = strlen(pragma);
674 } else {
675 len = (unsigned int)(end-pragma);
677 if(len > sizeof(features) - 1) {
678 mp_msg(MSGT_NETWORK,MSGL_WARN,MSGTR_MPDEMUX_ASF_ASFHTTPParseWarnCuttedPragma,pragma,len,sizeof(features) - 1);
679 len = sizeof(features) - 1;
681 strncpy( features, pragma, len );
682 features[len]='\0';
683 break;
685 comma_ptr = strstr( pragma, "," );
686 if( comma_ptr!=NULL ) {
687 pragma = comma_ptr+1;
688 if( pragma[0]==' ' ) pragma++;
690 } while( comma_ptr!=NULL );
691 pragma = http_get_next_field( http_hdr );
693 asf_http_ctrl->streaming_type = asf_http_streaming_type( content_type, features, http_hdr );
694 return 0;
697 static int asf_http_streaming_start( stream_t *stream, int *demuxer_type ) {
698 HTTP_header_t *http_hdr=NULL;
699 URL_t *url = stream->streaming_ctrl->url;
700 asf_http_streaming_ctrl_t *asf_http_ctrl;
701 char buffer[BUFFER_SIZE];
702 int i, ret;
703 int fd = stream->fd;
704 int done;
705 int auth_retry = 0;
707 asf_http_ctrl = malloc(sizeof(asf_http_streaming_ctrl_t));
708 if( asf_http_ctrl==NULL ) {
709 mp_msg(MSGT_NETWORK,MSGL_FATAL,MSGTR_MemAllocFailed);
710 return -1;
712 asf_http_ctrl->streaming_type = ASF_Unknown_e;
713 asf_http_ctrl->request = 1;
714 asf_http_ctrl->audio_streams = asf_http_ctrl->video_streams = NULL;
715 asf_http_ctrl->n_audio = asf_http_ctrl->n_video = 0;
716 stream->streaming_ctrl->data = (void*)asf_http_ctrl;
718 do {
719 done = 1;
720 if( fd>0 ) closesocket( fd );
722 if( !strcasecmp( url->protocol, "http_proxy" ) ) {
723 if( url->port==0 ) url->port = 8080;
724 } else {
725 if( url->port==0 ) url->port = 80;
727 fd = connect2Server( url->hostname, url->port, 1);
728 if( fd<0 ) return fd;
730 http_hdr = asf_http_request( stream->streaming_ctrl );
731 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Request [%s]\n", http_hdr->buffer );
732 for(i=0; i < (int)http_hdr->buffer_size ; ) {
733 int r = send( fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0 );
734 if(r <0) {
735 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_SocketWriteError,strerror(errno));
736 goto err_out;
738 i += r;
740 http_free( http_hdr );
741 http_hdr = http_new_header();
742 do {
743 i = recv( fd, buffer, BUFFER_SIZE, 0 );
744 //printf("read: %d\n", i );
745 if( i<=0 ) {
746 perror("read");
747 goto err_out;
749 http_response_append( http_hdr, buffer, i );
750 } while( !http_is_header_entire( http_hdr ) );
751 if( mp_msg_test(MSGT_NETWORK,MSGL_V) ) {
752 http_hdr->buffer[http_hdr->buffer_size]='\0';
753 mp_msg(MSGT_NETWORK,MSGL_DBG2,"Response [%s]\n", http_hdr->buffer );
755 ret = asf_http_parse_response(asf_http_ctrl, http_hdr);
756 if( ret<0 ) {
757 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_HeaderParseFailed);
758 goto err_out;
760 switch( asf_http_ctrl->streaming_type ) {
761 case ASF_Live_e:
762 case ASF_Prerecorded_e:
763 case ASF_PlainText_e:
764 if( http_hdr->body_size>0 ) {
765 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
766 goto err_out;
769 if( asf_http_ctrl->request==1 ) {
770 if( asf_http_ctrl->streaming_type!=ASF_PlainText_e ) {
771 // First request, we only got the ASF header.
772 ret = asf_streaming_parse_header(fd,stream->streaming_ctrl);
773 if(ret < 0) goto err_out;
774 if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) {
775 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_NoStreamFound);
776 goto err_out;
778 asf_http_ctrl->request++;
779 done = 0;
780 } else {
781 done = 1;
784 break;
785 case ASF_Redirector_e:
786 if( http_hdr->body_size>0 ) {
787 if( streaming_bufferize( stream->streaming_ctrl, http_hdr->body, http_hdr->body_size )<0 ) {
788 goto err_out;
791 *demuxer_type = DEMUXER_TYPE_PLAYLIST;
792 done = 1;
793 break;
794 case ASF_Authenticate_e:
795 if( http_authenticate( http_hdr, url, &auth_retry)<0 ) return -1;
796 asf_http_ctrl->streaming_type = ASF_Unknown_e;
797 done = 0;
798 break;
799 case ASF_Unknown_e:
800 default:
801 mp_msg(MSGT_NETWORK,MSGL_ERR,MSGTR_MPDEMUX_ASF_UnknownASFStreamingType);
802 goto err_out;
804 // Check if we got a redirect.
805 } while(!done);
807 stream->fd = fd;
808 if( asf_http_ctrl->streaming_type==ASF_PlainText_e || asf_http_ctrl->streaming_type==ASF_Redirector_e ) {
809 stream->streaming_ctrl->streaming_read = nop_streaming_read;
810 stream->streaming_ctrl->streaming_seek = nop_streaming_seek;
811 } else {
812 stream->streaming_ctrl->streaming_read = asf_http_streaming_read;
813 stream->streaming_ctrl->streaming_seek = asf_http_streaming_seek;
814 stream->streaming_ctrl->buffering = 1;
816 stream->streaming_ctrl->status = streaming_playing_e;
817 stream->close = close_s;
819 http_free( http_hdr );
820 return 0;
822 err_out:
823 if (fd > 0)
824 closesocket(fd);
825 stream->fd = -1;
826 http_free(http_hdr);
827 return -1;
830 static int open_s(stream_t *stream,int mode, void* opts, int* file_format) {
831 URL_t *url;
833 stream->streaming_ctrl = streaming_ctrl_new();
834 if( stream->streaming_ctrl==NULL ) {
835 return STREAM_ERROR;
837 stream->streaming_ctrl->bandwidth = network_bandwidth;
838 url = url_new(stream->url);
839 stream->streaming_ctrl->url = check4proxies(url);
840 url_free(url);
842 mp_msg(MSGT_OPEN, MSGL_INFO, MSGTR_MPDEMUX_ASF_InfoStreamASFURL, stream->url);
843 if((!strncmp(stream->url, "http", 4)) && (*file_format!=DEMUXER_TYPE_ASF && *file_format!=DEMUXER_TYPE_UNKNOWN)) {
844 streaming_ctrl_free(stream->streaming_ctrl);
845 stream->streaming_ctrl = NULL;
846 return STREAM_UNSUPPORTED;
849 if(asf_streaming_start(stream, file_format) < 0) {
850 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_ASF_StreamingFailed);
851 streaming_ctrl_free(stream->streaming_ctrl);
852 stream->streaming_ctrl = NULL;
853 return STREAM_UNSUPPORTED;
856 if (*file_format != DEMUXER_TYPE_PLAYLIST)
857 *file_format = DEMUXER_TYPE_ASF;
858 stream->type = STREAMTYPE_STREAM;
859 fixup_network_stream_cache(stream);
860 return STREAM_OK;
863 const stream_info_t stream_info_asf = {
864 "mms and mms over http streaming",
865 "null",
866 "Bertrand, Reimar Doeffinger, Albeu",
867 "originally based on work by Majormms (is that code still there?)",
868 open_s,
869 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL},
870 NULL,
871 0 // Urls are an option string