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.
37 #include "libmpdemux/asf.h"
40 #include "libmpdemux/demuxer.h"
41 #include "asf_mmst_streaming.h"
45 #include "ffmpeg_files/intreadwrite.h"
47 #include "libmpdemux/asfguid.h"
49 extern int network_bandwidth
;
51 static int asf_http_streaming_start(stream_t
*stream
, int *demuxer_type
);
53 static int asf_read_wrapper(int fd
, void *buffer
, int len
, streaming_ctrl_t
*stream_ctrl
) {
54 uint8_t *buf
= buffer
;
56 int got
= nop_streaming_read(fd
, buf
, len
, stream_ctrl
);
58 mp_tmsg(MSGT_NETWORK
, MSGL_ERR
, "Error while reading network stream.\n");
67 // We can try several protocol for asf streaming
68 // * first the UDP protcol, if there is a firewall, UDP
69 // packets will not come back, so the mmsu will fail.
70 // * Then we can try TCP, but if there is a proxy for
71 // internet connection, the TCP connection will not get
73 // * Then we can try HTTP.
75 // Note: Using WMP sequence MMSU then MMST and then HTTP.
77 static int asf_streaming_start( stream_t
*stream
, int *demuxer_type
) {
78 char *proto
= stream
->streaming_ctrl
->url
->protocol
;
80 int port
= stream
->streaming_ctrl
->url
->port
;
82 // Is protocol mms or mmsu?
83 if (!strcasecmp(proto
, "mmsu") || !strcasecmp(proto
, "mms"))
85 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/UDP...\n");
86 //fd = asf_mmsu_streaming_start( stream );
87 if( fd
>-1 ) return fd
; //mmsu support is not implemented yet - using this code
88 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/UDP failed\n");
89 if( fd
==-2 ) return -1;
92 //Is protocol mms or mmst?
93 if (!strcasecmp(proto
, "mmst") || !strcasecmp(proto
, "mms"))
95 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/TCP...\n");
96 fd
= asf_mmst_streaming_start( stream
);
97 stream
->streaming_ctrl
->url
->port
= port
;
98 if( fd
>-1 ) return fd
;
99 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/TCP failed\n");
100 if( fd
==-2 ) return -1;
103 //Is protocol http, http_proxy, or mms?
104 if (!strcasecmp(proto
, "http_proxy") || !strcasecmp(proto
, "http") ||
105 !strcasecmp(proto
, "mms") || !strcasecmp(proto
, "mmsh") ||
106 !strcasecmp(proto
, "mmshttp"))
108 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/HTTP...\n");
109 fd
= asf_http_streaming_start( stream
, demuxer_type
);
110 stream
->streaming_ctrl
->url
->port
= port
;
111 if( fd
>-1 ) return fd
;
112 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/HTTP failed\n");
113 if( fd
==-2 ) return -1;
120 static int asf_streaming(ASF_stream_chunck_t
*stream_chunck
, int *drop_packet
) {
122 printf("ASF stream chunck size=%d\n", stream_chunck->size);
123 printf("length: %d\n", length );
124 printf("0x%02X\n", stream_chunck->type );
126 if( drop_packet
!=NULL
) *drop_packet
= 0;
128 if( stream_chunck
->size
<8 ) {
129 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Ahhhh, stream_chunck size is too small: %d\n", stream_chunck
->size
);
132 if( stream_chunck
->size
!=stream_chunck
->size_confirm
) {
133 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"size_confirm mismatch!: %d %d\n", stream_chunck
->size
, stream_chunck
->size_confirm
);
137 printf(" type: 0x%02X\n", stream_chunck->type );
138 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
139 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
140 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
141 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
143 switch(stream_chunck
->type
) {
144 case ASF_STREAMING_CLEAR
: // $C Clear ASF configuration
145 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Clearing ASF stream configuration!\n");
146 if( drop_packet
!=NULL
) *drop_packet
= 1;
147 return stream_chunck
->size
;
149 case ASF_STREAMING_DATA
: // $D Data follows
150 // printf("=====> Data follows\n");
152 case ASF_STREAMING_END_TRANS
: // $E Transfer complete
153 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Transfer complete\n");
154 if( drop_packet
!=NULL
) *drop_packet
= 1;
155 return stream_chunck
->size
;
157 case ASF_STREAMING_HEADER
: // $H ASF header chunk follows
158 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF header chunk follows\n");
161 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Unknown stream type 0x%x\n", stream_chunck
->type
);
163 return stream_chunck
->size
+4;
166 static void close_s(stream_t
*stream
) {
167 closesocket(stream
->fd
);
171 static int max_idx(int s_count
, int *s_rates
, int bound
) {
172 int i
, best
= -1, rate
= -1;
173 for (i
= 0; i
< s_count
; i
++) {
174 if (s_rates
[i
] > rate
&& s_rates
[i
] <= bound
) {
182 static int asf_streaming_parse_header(int fd
, streaming_ctrl_t
* streaming_ctrl
) {
183 ASF_stream_chunck_t chunk
;
184 asf_http_streaming_ctrl_t
* asf_ctrl
= streaming_ctrl
->data
;
185 char* buffer
=NULL
, *chunk_buffer
=NULL
;
186 int i
,r
,size
,pos
= 0;
189 int chunk_size2read
= 0;
190 int bw
= streaming_ctrl
->bandwidth
;
191 int *v_rates
= NULL
, *a_rates
= NULL
;
192 int v_rate
= 0, a_rate
= 0, a_idx
= -1, v_idx
= -1;
194 if(asf_ctrl
== NULL
) return -1;
196 // The ASF header can be in several network chunks. For example if the content description
197 // is big, the ASF header will be split in 2 network chunk.
198 // So we need to retrieve all the chunk before starting to parse the header.
200 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
202 // Endian handling of the stream chunk
203 le2me_ASF_stream_chunck_t(&chunk
);
204 size
= asf_streaming( &chunk
, &r
) - sizeof(ASF_stream_chunck_t
);
205 if(r
) mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,"Warning: drop header ????\n");
207 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while parsing chunk header\n");
210 if (chunk
.type
!= ASF_STREAMING_HEADER
) {
211 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Didn't get a header as first chunk !!!!\n");
215 // audit: do not overflow buffer_size
216 if (size
> SIZE_MAX
- buffer_size
) return -1;
217 buffer
= malloc(size
+buffer_size
);
219 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Error: Can't allocate %d bytes buffer.\n",size
+buffer_size
);
222 if( chunk_buffer
!=NULL
) {
223 memcpy( buffer
, chunk_buffer
, buffer_size
);
224 free( chunk_buffer
);
226 chunk_buffer
= buffer
;
227 buffer
+= buffer_size
;
230 if (asf_read_wrapper(fd
, buffer
, size
, streaming_ctrl
) <= 0)
233 if( chunk_size2read
==0 ) {
234 ASF_header_t
*asfh
= (ASF_header_t
*)buffer
;
235 if(size
< (int)sizeof(ASF_header_t
)) {
236 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error: Chunk is too small.\n");
238 } else mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Got chunk\n");
239 chunk_size2read
= AV_RL64(&asfh
->objh
.size
);
240 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Size 2 read=%d\n", chunk_size2read
);
242 } while( buffer_size
<chunk_size2read
);
243 buffer
= chunk_buffer
;
246 start
= sizeof(ASF_header_t
);
248 pos
= find_asf_guid(buffer
, asf_file_header_guid
, start
, size
);
250 ASF_file_header_t
*fileh
= (ASF_file_header_t
*) &buffer
[pos
];
251 pos
+= sizeof(ASF_file_header_t
);
252 if (pos
> size
) goto len_err_out
;
254 if(fileh.packetsize != fileh.packetsize2) {
255 printf("Error packetsize check don't match\n");
259 asf_ctrl
->packet_size
= AV_RL32(&fileh
->max_packet_size
);
261 // preroll: time in ms to bufferize before playing
262 streaming_ctrl
->prebuffer_size
= (unsigned int)(((double)fileh
->preroll
/1000.0)*((double)fileh
->max_bitrate
/8.0));
266 while ((pos
= find_asf_guid(buffer
, asf_stream_header_guid
, pos
, size
)) >= 0)
268 ASF_stream_header_t
*streamh
= (ASF_stream_header_t
*)&buffer
[pos
];
269 pos
+= sizeof(ASF_stream_header_t
);
270 if (pos
> size
) goto len_err_out
;
271 switch(ASF_LOAD_GUID_PREFIX(streamh
->type
)) {
272 case 0xF8699E40 : // audio stream
273 if(asf_ctrl
->audio_streams
== NULL
){
274 asf_ctrl
->audio_streams
= malloc(sizeof(int));
275 asf_ctrl
->n_audio
= 1;
278 asf_ctrl
->audio_streams
= realloc(asf_ctrl
->audio_streams
,
279 asf_ctrl
->n_audio
*sizeof(int));
281 asf_ctrl
->audio_streams
[asf_ctrl
->n_audio
-1] = AV_RL16(&streamh
->stream_no
);
283 case 0xBC19EFC0 : // video stream
284 if(asf_ctrl
->video_streams
== NULL
){
285 asf_ctrl
->video_streams
= malloc(sizeof(int));
286 asf_ctrl
->n_video
= 1;
289 asf_ctrl
->video_streams
= realloc(asf_ctrl
->video_streams
,
290 asf_ctrl
->n_video
*sizeof(int));
292 asf_ctrl
->video_streams
[asf_ctrl
->n_video
-1] = AV_RL16(&streamh
->stream_no
);
297 // always allocate to avoid lots of ifs later
298 v_rates
= calloc(asf_ctrl
->n_video
, sizeof(int));
299 a_rates
= calloc(asf_ctrl
->n_audio
, sizeof(int));
301 pos
= find_asf_guid(buffer
, asf_stream_group_guid
, start
, size
);
303 // stream bitrate properties object
305 char *ptr
= &buffer
[pos
];
306 char *end
= &buffer
[size
];
308 mp_msg(MSGT_NETWORK
, MSGL_V
, "Stream bitrate properties object\n");
309 if (ptr
+ 2 > end
) goto len_err_out
;
310 stream_count
= AV_RL16(ptr
);
312 mp_msg(MSGT_NETWORK
, MSGL_V
, " stream count=[0x%x][%u]\n",
313 stream_count
, stream_count
);
314 for( i
=0 ; i
<stream_count
; i
++ ) {
318 if (ptr
+ 6 > end
) goto len_err_out
;
323 mp_msg(MSGT_NETWORK
, MSGL_V
,
324 " stream id=[0x%x][%u]\n", id
, id
);
325 mp_msg(MSGT_NETWORK
, MSGL_V
,
326 " max bitrate=[0x%x][%u]\n", rate
, rate
);
327 for (j
= 0; j
< asf_ctrl
->n_video
; j
++) {
328 if (id
== asf_ctrl
->video_streams
[j
]) {
329 mp_msg(MSGT_NETWORK
, MSGL_V
, " is video stream\n");
334 for (j
= 0; j
< asf_ctrl
->n_audio
; j
++) {
335 if (id
== asf_ctrl
->audio_streams
[j
]) {
336 mp_msg(MSGT_NETWORK
, MSGL_V
, " is audio stream\n");
345 // automatic stream selection based on bandwidth
346 if (bw
== 0) bw
= INT_MAX
;
347 mp_msg(MSGT_NETWORK
, MSGL_V
, "Max bandwidth set to %d\n", bw
);
349 if (asf_ctrl
->n_audio
) {
350 // find lowest-bitrate audio stream
353 for (i
= 0; i
< asf_ctrl
->n_audio
; i
++) {
354 if (a_rates
[i
] < a_rate
) {
359 if (max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
) < 0) {
360 // both audio and video are not possible, try video only next
365 // find best video stream
366 v_idx
= max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
);
368 v_rate
= v_rates
[v_idx
];
370 // find best audio stream
371 a_idx
= max_idx(asf_ctrl
->n_audio
, a_rates
, bw
- v_rate
);
376 if (a_idx
< 0 && v_idx
< 0) {
377 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Bandwidth too small, file cannot be played!\n");
381 if (*streaming_ctrl
->audio_id_ptr
> 0)
382 // a audio stream was forced
383 asf_ctrl
->audio_id
= *streaming_ctrl
->audio_id_ptr
;
385 asf_ctrl
->audio_id
= asf_ctrl
->audio_streams
[a_idx
];
386 else if (asf_ctrl
->n_audio
) {
387 mp_tmsg(MSGT_NETWORK
, MSGL_WARN
, "Bandwidth too small, deselected audio stream.\n");
388 *streaming_ctrl
->audio_id_ptr
= -2;
391 if (*streaming_ctrl
->video_id_ptr
> 0)
392 // a video stream was forced
393 asf_ctrl
->video_id
= *streaming_ctrl
->video_id_ptr
;
395 asf_ctrl
->video_id
= asf_ctrl
->video_streams
[v_idx
];
396 else if (asf_ctrl
->n_video
) {
397 mp_tmsg(MSGT_NETWORK
, MSGL_WARN
, "Bandwidth too small, deselected video stream.\n");
398 *streaming_ctrl
->video_id_ptr
= -2;
404 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Invalid length in ASF header!\n");
405 if (buffer
) free(buffer
);
406 if (v_rates
) free(v_rates
);
407 if (a_rates
) free(a_rates
);
411 static int asf_http_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*streaming_ctrl
) {
412 static ASF_stream_chunck_t chunk
;
413 int read
,chunk_size
= 0;
414 static int rest
= 0, drop_chunk
= 0, waiting
= 0;
415 asf_http_streaming_ctrl_t
*asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
418 if (rest
== 0 && waiting
== 0) {
419 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
422 // Endian handling of the stream chunk
423 le2me_ASF_stream_chunck_t(&chunk
);
424 chunk_size
= asf_streaming( &chunk
, &drop_chunk
);
426 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while parsing chunk header\n");
429 chunk_size
-= sizeof(ASF_stream_chunck_t
);
431 if(chunk
.type
!= ASF_STREAMING_HEADER
&& (!drop_chunk
)) {
432 if (asf_http_ctrl
->packet_size
< chunk_size
) {
433 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error: chunk_size > packet_size\n");
436 waiting
= asf_http_ctrl
->packet_size
;
438 waiting
= chunk_size
;
447 if ( waiting
>= chunk_size
) {
448 if (chunk_size
> size
){
449 rest
= chunk_size
- size
;
452 if (asf_read_wrapper(fd
, buffer
, chunk_size
, streaming_ctrl
) <= 0)
456 if (drop_chunk
) continue;
458 if (rest
== 0 && waiting
> 0 && size
-read
> 0) {
459 int s
= FFMIN(waiting
,size
-read
);
460 memset(buffer
+read
,0,s
);
470 static int asf_http_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*streaming_ctrl
) {
472 // to shut up gcc warning
478 static int asf_header_check( HTTP_header_t
*http_hdr
) {
479 ASF_obj_header_t
*objh
;
480 if( http_hdr
==NULL
) return -1;
481 if( http_hdr
->body
==NULL
|| http_hdr
->body_size
<sizeof(ASF_obj_header_t
) ) return -1;
483 objh
= (ASF_obj_header_t
*)http_hdr
->body
;
484 if( ASF_LOAD_GUID_PREFIX(objh
->guid
)==0x75B22630 ) return 0;
488 static int asf_http_streaming_type(char *content_type
, char *features
, HTTP_header_t
*http_hdr
) {
489 if( content_type
==NULL
) return ASF_Unknown_e
;
490 if( !strcasecmp(content_type
, "application/octet-stream") ||
491 !strcasecmp(content_type
, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
492 !strcasecmp(content_type
, "application/x-mms-framed") || // New in Corana, second request
493 !strcasecmp(content_type
, "video/x-ms-wmv") ||
494 !strcasecmp(content_type
, "video/x-ms-asf")) {
496 if( strstr(features
, "broadcast") ) {
497 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Live stream\n");
500 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Prerecorded\n");
501 return ASF_Prerecorded_e
;
504 // Ok in a perfect world, web servers should be well configured
505 // so we could used mime type to know the stream type,
506 // but guess what? All of them are not well configured.
507 // So we have to check for an asf header :(, but it works :p
508 if( http_hdr
->body_size
>sizeof(ASF_obj_header_t
) ) {
509 if( asf_header_check( http_hdr
)==0 ) {
510 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
511 return ASF_PlainText_e
;
512 } else if( (!strcasecmp(content_type
, "text/html")) ) {
513 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> HTML, MPlayer is not a browser...yet!\n");
514 return ASF_Unknown_e
;
516 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Redirector\n");
517 return ASF_Redirector_e
;
520 if( (!strcasecmp(content_type
, "audio/x-ms-wax")) ||
521 (!strcasecmp(content_type
, "audio/x-ms-wma")) ||
522 (!strcasecmp(content_type
, "video/x-ms-asf")) ||
523 (!strcasecmp(content_type
, "video/x-ms-afs")) ||
524 (!strcasecmp(content_type
, "video/x-ms-wmv")) ||
525 (!strcasecmp(content_type
, "video/x-ms-wma")) ) {
526 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"=====> ASF Redirector\n");
527 return ASF_Redirector_e
;
528 } else if( !strcasecmp(content_type
, "text/plain") ) {
529 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
530 return ASF_PlainText_e
;
532 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF unknown content-type: %s\n", content_type
);
533 return ASF_Unknown_e
;
537 return ASF_Unknown_e
;
540 static HTTP_header_t
*asf_http_request(streaming_ctrl_t
*streaming_ctrl
) {
541 HTTP_header_t
*http_hdr
;
543 URL_t
*server_url
= NULL
;
544 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
549 int offset_hi
=0, offset_lo
=0, length
=0;
550 int asf_nb_stream
=0, stream_id
;
553 if( streaming_ctrl
==NULL
) return NULL
;
554 url
= streaming_ctrl
->url
;
555 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
556 if( url
==NULL
|| asf_http_ctrl
==NULL
) return NULL
;
558 // Common header for all requests.
559 http_hdr
= http_new_header();
560 http_set_field( http_hdr
, "Accept: */*" );
561 http_set_field( http_hdr
, "User-Agent: NSPlayer/4.1.0.3856" );
562 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
564 // Check if we are using a proxy
565 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
566 server_url
= url_new( (url
->file
)+1 );
567 if( server_url
==NULL
) {
568 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"invalid proxy URL\n");
569 http_free( http_hdr
);
572 http_set_uri( http_hdr
, server_url
->url
);
573 sprintf( str
, "Host: %.220s:%d", server_url
->hostname
, server_url
->port
);
574 url_free( server_url
);
576 http_set_uri( http_hdr
, url
->file
);
577 sprintf( str
, "Host: %.220s:%d", url
->hostname
, url
->port
);
580 http_set_field( http_hdr
, str
);
581 http_set_field( http_hdr
, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
583 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
584 offset_hi
, offset_lo
, asf_http_ctrl
->request
, length
);
585 http_set_field( http_hdr
, str
);
587 switch( asf_http_ctrl
->streaming_type
) {
589 case ASF_Prerecorded_e
:
590 http_set_field( http_hdr
, "Pragma: xPlayStrm=1" );
592 ptr
+= sprintf( ptr
, "Pragma: stream-switch-entry=");
593 if(asf_http_ctrl
->n_audio
> 0) {
594 for( i
=0; i
<asf_http_ctrl
->n_audio
; i
++ ) {
595 stream_id
= asf_http_ctrl
->audio_streams
[i
];
596 if(stream_id
== asf_http_ctrl
->audio_id
) {
603 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
606 if(asf_http_ctrl
->n_video
> 0) {
607 for( i
=0; i
<asf_http_ctrl
->n_video
; i
++ ) {
608 stream_id
= asf_http_ctrl
->video_streams
[i
];
609 if(stream_id
== asf_http_ctrl
->video_id
) {
616 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
619 http_set_field( http_hdr
, str
);
620 sprintf( str
, "Pragma: stream-switch-count=%d", asf_nb_stream
);
621 http_set_field( http_hdr
, str
);
623 case ASF_Redirector_e
:
626 // First request goes here.
629 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"unknown ASF stream type\n");
632 http_set_field( http_hdr
, "Connection: Close" );
633 http_build_request( http_hdr
);
638 static int asf_http_parse_response(asf_http_streaming_ctrl_t
*asf_http_ctrl
, HTTP_header_t
*http_hdr
) {
639 char *content_type
, *pragma
;
640 char features
[64] = "\0";
642 if( http_response_parse(http_hdr
)<0 ) {
643 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Failed to parse HTTP response.\n");
646 switch( http_hdr
->status_code
) {
649 case 401: // Authentication required
650 return ASF_Authenticate_e
;
652 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d:%s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
656 content_type
= http_get_field( http_hdr
, "Content-Type");
657 //printf("Content-Type: [%s]\n", content_type);
659 pragma
= http_get_field( http_hdr
, "Pragma");
660 while( pragma
!=NULL
) {
661 char *comma_ptr
=NULL
;
663 //printf("Pragma: [%s]\n", pragma );
664 // The pragma line can get severals attributes
665 // separeted with a comma ','.
667 if( !strncasecmp( pragma
, "features=", 9) ) {
669 end
= strstr( pragma
, "," );
671 len
= strlen(pragma
);
673 len
= (unsigned int)(end
-pragma
);
675 if(len
> sizeof(features
) - 1) {
676 mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,"ASF HTTP PARSE WARNING : Pragma %s cut from %d bytes to %d\n",pragma
,len
,sizeof(features
) - 1);
677 len
= sizeof(features
) - 1;
679 strncpy( features
, pragma
, len
);
683 comma_ptr
= strstr( pragma
, "," );
684 if( comma_ptr
!=NULL
) {
685 pragma
= comma_ptr
+1;
686 if( pragma
[0]==' ' ) pragma
++;
688 } while( comma_ptr
!=NULL
);
689 pragma
= http_get_next_field( http_hdr
);
691 asf_http_ctrl
->streaming_type
= asf_http_streaming_type( content_type
, features
, http_hdr
);
695 static int asf_http_streaming_start( stream_t
*stream
, int *demuxer_type
) {
696 HTTP_header_t
*http_hdr
=NULL
;
697 URL_t
*url
= stream
->streaming_ctrl
->url
;
698 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
699 char buffer
[BUFFER_SIZE
];
705 asf_http_ctrl
= malloc(sizeof(asf_http_streaming_ctrl_t
));
706 if( asf_http_ctrl
==NULL
) {
707 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
710 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
711 asf_http_ctrl
->request
= 1;
712 asf_http_ctrl
->audio_streams
= asf_http_ctrl
->video_streams
= NULL
;
713 asf_http_ctrl
->n_audio
= asf_http_ctrl
->n_video
= 0;
714 stream
->streaming_ctrl
->data
= (void*)asf_http_ctrl
;
718 if( fd
>0 ) closesocket( fd
);
720 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
721 if( url
->port
==0 ) url
->port
= 8080;
723 if( url
->port
==0 ) url
->port
= 80;
725 fd
= connect2Server( url
->hostname
, url
->port
, 1);
726 if( fd
<0 ) return fd
;
728 http_hdr
= asf_http_request( stream
->streaming_ctrl
);
729 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request [%s]\n", http_hdr
->buffer
);
730 for(i
=0; i
< (int)http_hdr
->buffer_size
; ) {
731 int r
= send( fd
, http_hdr
->buffer
+i
, http_hdr
->buffer_size
-i
, 0 );
733 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"socket write error: %s\n",strerror(errno
));
738 http_free( http_hdr
);
739 http_hdr
= http_new_header();
741 i
= recv( fd
, buffer
, BUFFER_SIZE
, 0 );
742 //printf("read: %d\n", i );
747 http_response_append( http_hdr
, buffer
, i
);
748 } while( !http_is_header_entire( http_hdr
) );
749 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
750 http_hdr
->buffer
[http_hdr
->buffer_size
]='\0';
751 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Response [%s]\n", http_hdr
->buffer
);
753 ret
= asf_http_parse_response(asf_http_ctrl
, http_hdr
);
755 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Failed to parse header.\n");
758 switch( asf_http_ctrl
->streaming_type
) {
760 case ASF_Prerecorded_e
:
761 case ASF_PlainText_e
:
762 if( http_hdr
->body_size
>0 ) {
763 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
767 if( asf_http_ctrl
->request
==1 ) {
768 if( asf_http_ctrl
->streaming_type
!=ASF_PlainText_e
) {
769 // First request, we only got the ASF header.
770 ret
= asf_streaming_parse_header(fd
,stream
->streaming_ctrl
);
771 if(ret
< 0) goto err_out
;
772 if(asf_http_ctrl
->n_audio
== 0 && asf_http_ctrl
->n_video
== 0) {
773 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"No stream found.\n");
776 asf_http_ctrl
->request
++;
783 case ASF_Redirector_e
:
784 if( http_hdr
->body_size
>0 ) {
785 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
789 *demuxer_type
= DEMUXER_TYPE_PLAYLIST
;
792 case ASF_Authenticate_e
:
793 if( http_authenticate( http_hdr
, url
, &auth_retry
)<0 ) return -1;
794 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
799 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"unknown ASF streaming type\n");
802 // Check if we got a redirect.
806 if( asf_http_ctrl
->streaming_type
==ASF_PlainText_e
|| asf_http_ctrl
->streaming_type
==ASF_Redirector_e
) {
807 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
808 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
810 stream
->streaming_ctrl
->streaming_read
= asf_http_streaming_read
;
811 stream
->streaming_ctrl
->streaming_seek
= asf_http_streaming_seek
;
812 stream
->streaming_ctrl
->buffering
= 1;
814 stream
->streaming_ctrl
->status
= streaming_playing_e
;
815 stream
->close
= close_s
;
817 http_free( http_hdr
);
828 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
831 stream
->streaming_ctrl
= streaming_ctrl_new();
832 if( stream
->streaming_ctrl
==NULL
) {
835 stream
->streaming_ctrl
->audio_id_ptr
= &stream
->opts
->audio_id
;
836 stream
->streaming_ctrl
->video_id_ptr
= &stream
->opts
->video_id
;
837 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
838 url
= url_new(stream
->url
);
839 stream
->streaming_ctrl
->url
= check4proxies(url
);
842 mp_tmsg(MSGT_OPEN
, MSGL_INFO
, "STREAM_ASF, URL: %s\n", 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_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed, exiting.\n");
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
);
863 const stream_info_t stream_info_asf
= {
864 "mms and mms over http streaming",
866 "Bertrand, Reimar Doeffinger, Albeu",
867 "originally based on work by Majormms (is that code still there?)",
869 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL
},
871 0 // Urls are an option string