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"
45 #include "ffmpeg_files/intreadwrite.h"
47 #include "libmpdemux/asfguid.h"
49 extern int network_bandwidth
;
51 int asf_mmst_streaming_start( stream_t
*stream
);
52 static int asf_http_streaming_start(stream_t
*stream
, int *demuxer_type
);
54 static int asf_read_wrapper(int fd
, void *buffer
, int len
, streaming_ctrl_t
*stream_ctrl
) {
55 uint8_t *buf
= buffer
;
57 int got
= nop_streaming_read(fd
, buf
, len
, stream_ctrl
);
59 mp_tmsg(MSGT_NETWORK
, MSGL_ERR
, "Error while reading network stream.\n");
68 // We can try several protocol for asf streaming
69 // * first the UDP protcol, if there is a firewall, UDP
70 // packets will not come back, so the mmsu will fail.
71 // * Then we can try TCP, but if there is a proxy for
72 // internet connection, the TCP connection will not get
74 // * Then we can try HTTP.
76 // Note: Using WMP sequence MMSU then MMST and then HTTP.
78 static int asf_streaming_start( stream_t
*stream
, int *demuxer_type
) {
79 char *proto
= stream
->streaming_ctrl
->url
->protocol
;
81 int port
= stream
->streaming_ctrl
->url
->port
;
83 // Is protocol mms or mmsu?
84 if (!strcasecmp(proto
, "mmsu") || !strcasecmp(proto
, "mms"))
86 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/UDP...\n");
87 //fd = asf_mmsu_streaming_start( stream );
88 if( fd
>-1 ) return fd
; //mmsu support is not implemented yet - using this code
89 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/UDP failed\n");
90 if( fd
==-2 ) return -1;
93 //Is protocol mms or mmst?
94 if (!strcasecmp(proto
, "mmst") || !strcasecmp(proto
, "mms"))
96 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/TCP...\n");
97 fd
= asf_mmst_streaming_start( stream
);
98 stream
->streaming_ctrl
->url
->port
= port
;
99 if( fd
>-1 ) return fd
;
100 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/TCP failed\n");
101 if( fd
==-2 ) return -1;
104 //Is protocol http, http_proxy, or mms?
105 if (!strcasecmp(proto
, "http_proxy") || !strcasecmp(proto
, "http") ||
106 !strcasecmp(proto
, "mms") || !strcasecmp(proto
, "mmsh") ||
107 !strcasecmp(proto
, "mmshttp"))
109 mp_msg(MSGT_NETWORK
,MSGL_V
,"Trying ASF/HTTP...\n");
110 fd
= asf_http_streaming_start( stream
, demuxer_type
);
111 stream
->streaming_ctrl
->url
->port
= port
;
112 if( fd
>-1 ) return fd
;
113 mp_msg(MSGT_NETWORK
,MSGL_V
," ===> ASF/HTTP failed\n");
114 if( fd
==-2 ) return -1;
121 static int asf_streaming(ASF_stream_chunck_t
*stream_chunck
, int *drop_packet
) {
123 printf("ASF stream chunck size=%d\n", stream_chunck->size);
124 printf("length: %d\n", length );
125 printf("0x%02X\n", stream_chunck->type );
127 if( drop_packet
!=NULL
) *drop_packet
= 0;
129 if( stream_chunck
->size
<8 ) {
130 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Ahhhh, stream_chunck size is too small: %d\n", stream_chunck
->size
);
133 if( stream_chunck
->size
!=stream_chunck
->size_confirm
) {
134 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"size_confirm mismatch!: %d %d\n", stream_chunck
->size
, stream_chunck
->size_confirm
);
138 printf(" type: 0x%02X\n", stream_chunck->type );
139 printf(" size: %d (0x%02X)\n", stream_chunck->size, stream_chunck->size );
140 printf(" sequence_number: 0x%04X\n", stream_chunck->sequence_number );
141 printf(" unknown: 0x%02X\n", stream_chunck->unknown );
142 printf(" size_confirm: 0x%02X\n", stream_chunck->size_confirm );
144 switch(stream_chunck
->type
) {
145 case ASF_STREAMING_CLEAR
: // $C Clear ASF configuration
146 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Clearing ASF stream configuration!\n");
147 if( drop_packet
!=NULL
) *drop_packet
= 1;
148 return stream_chunck
->size
;
150 case ASF_STREAMING_DATA
: // $D Data follows
151 // printf("=====> Data follows\n");
153 case ASF_STREAMING_END_TRANS
: // $E Transfer complete
154 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Transfer complete\n");
155 if( drop_packet
!=NULL
) *drop_packet
= 1;
156 return stream_chunck
->size
;
158 case ASF_STREAMING_HEADER
: // $H ASF header chunk follows
159 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF header chunk follows\n");
162 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Unknown stream type 0x%x\n", stream_chunck
->type
);
164 return stream_chunck
->size
+4;
167 static void close_s(stream_t
*stream
) {
168 closesocket(stream
->fd
);
172 static int max_idx(int s_count
, int *s_rates
, int bound
) {
173 int i
, best
= -1, rate
= -1;
174 for (i
= 0; i
< s_count
; i
++) {
175 if (s_rates
[i
] > rate
&& s_rates
[i
] <= bound
) {
183 static int asf_streaming_parse_header(int fd
, streaming_ctrl_t
* streaming_ctrl
) {
184 ASF_stream_chunck_t chunk
;
185 asf_http_streaming_ctrl_t
* asf_ctrl
= streaming_ctrl
->data
;
186 char* buffer
=NULL
, *chunk_buffer
=NULL
;
187 int i
,r
,size
,pos
= 0;
190 int chunk_size2read
= 0;
191 int bw
= streaming_ctrl
->bandwidth
;
192 int *v_rates
= NULL
, *a_rates
= NULL
;
193 int v_rate
= 0, a_rate
= 0, a_idx
= -1, v_idx
= -1;
195 if(asf_ctrl
== NULL
) return -1;
197 // The ASF header can be in several network chunks. For example if the content description
198 // is big, the ASF header will be split in 2 network chunk.
199 // So we need to retrieve all the chunk before starting to parse the header.
201 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
203 // Endian handling of the stream chunk
204 le2me_ASF_stream_chunck_t(&chunk
);
205 size
= asf_streaming( &chunk
, &r
) - sizeof(ASF_stream_chunck_t
);
206 if(r
) mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,"Warning: drop header ????\n");
208 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while parsing chunk header\n");
211 if (chunk
.type
!= ASF_STREAMING_HEADER
) {
212 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Didn't get a header as first chunk !!!!\n");
216 // audit: do not overflow buffer_size
217 if (size
> SIZE_MAX
- buffer_size
) return -1;
218 buffer
= malloc(size
+buffer_size
);
220 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Error: Can't allocate %d bytes buffer.\n",size
+buffer_size
);
223 if( chunk_buffer
!=NULL
) {
224 memcpy( buffer
, chunk_buffer
, buffer_size
);
225 free( chunk_buffer
);
227 chunk_buffer
= buffer
;
228 buffer
+= buffer_size
;
231 if (asf_read_wrapper(fd
, buffer
, size
, streaming_ctrl
) <= 0)
234 if( chunk_size2read
==0 ) {
235 ASF_header_t
*asfh
= (ASF_header_t
*)buffer
;
236 if(size
< (int)sizeof(ASF_header_t
)) {
237 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error: Chunk is too small.\n");
239 } else mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Got chunk\n");
240 chunk_size2read
= AV_RL64(&asfh
->objh
.size
);
241 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Size 2 read=%d\n", chunk_size2read
);
243 } while( buffer_size
<chunk_size2read
);
244 buffer
= chunk_buffer
;
247 start
= sizeof(ASF_header_t
);
249 pos
= find_asf_guid(buffer
, asf_file_header_guid
, start
, size
);
251 ASF_file_header_t
*fileh
= (ASF_file_header_t
*) &buffer
[pos
];
252 pos
+= sizeof(ASF_file_header_t
);
253 if (pos
> size
) goto len_err_out
;
255 if(fileh.packetsize != fileh.packetsize2) {
256 printf("Error packetsize check don't match\n");
260 asf_ctrl
->packet_size
= AV_RL32(&fileh
->max_packet_size
);
262 // preroll: time in ms to bufferize before playing
263 streaming_ctrl
->prebuffer_size
= (unsigned int)(((double)fileh
->preroll
/1000.0)*((double)fileh
->max_bitrate
/8.0));
267 while ((pos
= find_asf_guid(buffer
, asf_stream_header_guid
, pos
, size
)) >= 0)
269 ASF_stream_header_t
*streamh
= (ASF_stream_header_t
*)&buffer
[pos
];
270 pos
+= sizeof(ASF_stream_header_t
);
271 if (pos
> size
) goto len_err_out
;
272 switch(ASF_LOAD_GUID_PREFIX(streamh
->type
)) {
273 case 0xF8699E40 : // audio stream
274 if(asf_ctrl
->audio_streams
== NULL
){
275 asf_ctrl
->audio_streams
= malloc(sizeof(int));
276 asf_ctrl
->n_audio
= 1;
279 asf_ctrl
->audio_streams
= realloc(asf_ctrl
->audio_streams
,
280 asf_ctrl
->n_audio
*sizeof(int));
282 asf_ctrl
->audio_streams
[asf_ctrl
->n_audio
-1] = AV_RL16(&streamh
->stream_no
);
284 case 0xBC19EFC0 : // video stream
285 if(asf_ctrl
->video_streams
== NULL
){
286 asf_ctrl
->video_streams
= malloc(sizeof(int));
287 asf_ctrl
->n_video
= 1;
290 asf_ctrl
->video_streams
= realloc(asf_ctrl
->video_streams
,
291 asf_ctrl
->n_video
*sizeof(int));
293 asf_ctrl
->video_streams
[asf_ctrl
->n_video
-1] = AV_RL16(&streamh
->stream_no
);
298 // always allocate to avoid lots of ifs later
299 v_rates
= calloc(asf_ctrl
->n_video
, sizeof(int));
300 a_rates
= calloc(asf_ctrl
->n_audio
, sizeof(int));
302 pos
= find_asf_guid(buffer
, asf_stream_group_guid
, start
, size
);
304 // stream bitrate properties object
306 char *ptr
= &buffer
[pos
];
307 char *end
= &buffer
[size
];
309 mp_msg(MSGT_NETWORK
, MSGL_V
, "Stream bitrate properties object\n");
310 if (ptr
+ 2 > end
) goto len_err_out
;
311 stream_count
= AV_RL16(ptr
);
313 mp_msg(MSGT_NETWORK
, MSGL_V
, " stream count=[0x%x][%u]\n",
314 stream_count
, stream_count
);
315 for( i
=0 ; i
<stream_count
; i
++ ) {
319 if (ptr
+ 6 > end
) goto len_err_out
;
324 mp_msg(MSGT_NETWORK
, MSGL_V
,
325 " stream id=[0x%x][%u]\n", id
, id
);
326 mp_msg(MSGT_NETWORK
, MSGL_V
,
327 " max bitrate=[0x%x][%u]\n", rate
, rate
);
328 for (j
= 0; j
< asf_ctrl
->n_video
; j
++) {
329 if (id
== asf_ctrl
->video_streams
[j
]) {
330 mp_msg(MSGT_NETWORK
, MSGL_V
, " is video stream\n");
335 for (j
= 0; j
< asf_ctrl
->n_audio
; j
++) {
336 if (id
== asf_ctrl
->audio_streams
[j
]) {
337 mp_msg(MSGT_NETWORK
, MSGL_V
, " is audio stream\n");
346 // automatic stream selection based on bandwidth
347 if (bw
== 0) bw
= INT_MAX
;
348 mp_msg(MSGT_NETWORK
, MSGL_V
, "Max bandwidth set to %d\n", bw
);
350 if (asf_ctrl
->n_audio
) {
351 // find lowest-bitrate audio stream
354 for (i
= 0; i
< asf_ctrl
->n_audio
; i
++) {
355 if (a_rates
[i
] < a_rate
) {
360 if (max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
) < 0) {
361 // both audio and video are not possible, try video only next
366 // find best video stream
367 v_idx
= max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
);
369 v_rate
= v_rates
[v_idx
];
371 // find best audio stream
372 a_idx
= max_idx(asf_ctrl
->n_audio
, a_rates
, bw
- v_rate
);
377 if (a_idx
< 0 && v_idx
< 0) {
378 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Bandwidth too small, file cannot be played!\n");
382 if (*streaming_ctrl
->audio_id_ptr
> 0)
383 // a audio stream was forced
384 asf_ctrl
->audio_id
= *streaming_ctrl
->audio_id_ptr
;
386 asf_ctrl
->audio_id
= asf_ctrl
->audio_streams
[a_idx
];
387 else if (asf_ctrl
->n_audio
) {
388 mp_tmsg(MSGT_NETWORK
, MSGL_WARN
, "Bandwidth too small, deselected audio stream.\n");
389 *streaming_ctrl
->audio_id_ptr
= -2;
392 if (*streaming_ctrl
->video_id_ptr
> 0)
393 // a video stream was forced
394 asf_ctrl
->video_id
= *streaming_ctrl
->video_id_ptr
;
396 asf_ctrl
->video_id
= asf_ctrl
->video_streams
[v_idx
];
397 else if (asf_ctrl
->n_video
) {
398 mp_tmsg(MSGT_NETWORK
, MSGL_WARN
, "Bandwidth too small, deselected video stream.\n");
399 *streaming_ctrl
->video_id_ptr
= -2;
405 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Invalid length in ASF header!\n");
406 if (buffer
) free(buffer
);
407 if (v_rates
) free(v_rates
);
408 if (a_rates
) free(a_rates
);
412 static int asf_http_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*streaming_ctrl
) {
413 static ASF_stream_chunck_t chunk
;
414 int read
,chunk_size
= 0;
415 static int rest
= 0, drop_chunk
= 0, waiting
= 0;
416 asf_http_streaming_ctrl_t
*asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
419 if (rest
== 0 && waiting
== 0) {
420 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
423 // Endian handling of the stream chunk
424 le2me_ASF_stream_chunck_t(&chunk
);
425 chunk_size
= asf_streaming( &chunk
, &drop_chunk
);
427 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while parsing chunk header\n");
430 chunk_size
-= sizeof(ASF_stream_chunck_t
);
432 if(chunk
.type
!= ASF_STREAMING_HEADER
&& (!drop_chunk
)) {
433 if (asf_http_ctrl
->packet_size
< chunk_size
) {
434 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error: chunk_size > packet_size\n");
437 waiting
= asf_http_ctrl
->packet_size
;
439 waiting
= chunk_size
;
448 if ( waiting
>= chunk_size
) {
449 if (chunk_size
> size
){
450 rest
= chunk_size
- size
;
453 if (asf_read_wrapper(fd
, buffer
, chunk_size
, streaming_ctrl
) <= 0)
457 if (drop_chunk
) continue;
459 if (rest
== 0 && waiting
> 0 && size
-read
> 0) {
460 int s
= FFMIN(waiting
,size
-read
);
461 memset(buffer
+read
,0,s
);
471 static int asf_http_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*streaming_ctrl
) {
473 // to shut up gcc warning
479 static int asf_header_check( HTTP_header_t
*http_hdr
) {
480 ASF_obj_header_t
*objh
;
481 if( http_hdr
==NULL
) return -1;
482 if( http_hdr
->body
==NULL
|| http_hdr
->body_size
<sizeof(ASF_obj_header_t
) ) return -1;
484 objh
= (ASF_obj_header_t
*)http_hdr
->body
;
485 if( ASF_LOAD_GUID_PREFIX(objh
->guid
)==0x75B22630 ) return 0;
489 static int asf_http_streaming_type(char *content_type
, char *features
, HTTP_header_t
*http_hdr
) {
490 if( content_type
==NULL
) return ASF_Unknown_e
;
491 if( !strcasecmp(content_type
, "application/octet-stream") ||
492 !strcasecmp(content_type
, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
493 !strcasecmp(content_type
, "application/x-mms-framed") || // New in Corana, second request
494 !strcasecmp(content_type
, "video/x-ms-wmv") ||
495 !strcasecmp(content_type
, "video/x-ms-asf")) {
497 if( strstr(features
, "broadcast") ) {
498 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Live stream\n");
501 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Prerecorded\n");
502 return ASF_Prerecorded_e
;
505 // Ok in a perfect world, web servers should be well configured
506 // so we could used mime type to know the stream type,
507 // but guess what? All of them are not well configured.
508 // So we have to check for an asf header :(, but it works :p
509 if( http_hdr
->body_size
>sizeof(ASF_obj_header_t
) ) {
510 if( asf_header_check( http_hdr
)==0 ) {
511 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
512 return ASF_PlainText_e
;
513 } else if( (!strcasecmp(content_type
, "text/html")) ) {
514 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> HTML, MPlayer is not a browser...yet!\n");
515 return ASF_Unknown_e
;
517 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Redirector\n");
518 return ASF_Redirector_e
;
521 if( (!strcasecmp(content_type
, "audio/x-ms-wax")) ||
522 (!strcasecmp(content_type
, "audio/x-ms-wma")) ||
523 (!strcasecmp(content_type
, "video/x-ms-asf")) ||
524 (!strcasecmp(content_type
, "video/x-ms-afs")) ||
525 (!strcasecmp(content_type
, "video/x-ms-wmv")) ||
526 (!strcasecmp(content_type
, "video/x-ms-wma")) ) {
527 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"=====> ASF Redirector\n");
528 return ASF_Redirector_e
;
529 } else if( !strcasecmp(content_type
, "text/plain") ) {
530 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
531 return ASF_PlainText_e
;
533 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF unknown content-type: %s\n", content_type
);
534 return ASF_Unknown_e
;
538 return ASF_Unknown_e
;
541 static HTTP_header_t
*asf_http_request(streaming_ctrl_t
*streaming_ctrl
) {
542 HTTP_header_t
*http_hdr
;
544 URL_t
*server_url
= NULL
;
545 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
550 int offset_hi
=0, offset_lo
=0, length
=0;
551 int asf_nb_stream
=0, stream_id
;
554 if( streaming_ctrl
==NULL
) return NULL
;
555 url
= streaming_ctrl
->url
;
556 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
557 if( url
==NULL
|| asf_http_ctrl
==NULL
) return NULL
;
559 // Common header for all requests.
560 http_hdr
= http_new_header();
561 http_set_field( http_hdr
, "Accept: */*" );
562 http_set_field( http_hdr
, "User-Agent: NSPlayer/4.1.0.3856" );
563 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
565 // Check if we are using a proxy
566 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
567 server_url
= url_new( (url
->file
)+1 );
568 if( server_url
==NULL
) {
569 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"invalid proxy URL\n");
570 http_free( http_hdr
);
573 http_set_uri( http_hdr
, server_url
->url
);
574 sprintf( str
, "Host: %.220s:%d", server_url
->hostname
, server_url
->port
);
575 url_free( server_url
);
577 http_set_uri( http_hdr
, url
->file
);
578 sprintf( str
, "Host: %.220s:%d", url
->hostname
, url
->port
);
581 http_set_field( http_hdr
, str
);
582 http_set_field( http_hdr
, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
584 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
585 offset_hi
, offset_lo
, asf_http_ctrl
->request
, length
);
586 http_set_field( http_hdr
, str
);
588 switch( asf_http_ctrl
->streaming_type
) {
590 case ASF_Prerecorded_e
:
591 http_set_field( http_hdr
, "Pragma: xPlayStrm=1" );
593 ptr
+= sprintf( ptr
, "Pragma: stream-switch-entry=");
594 if(asf_http_ctrl
->n_audio
> 0) {
595 for( i
=0; i
<asf_http_ctrl
->n_audio
; i
++ ) {
596 stream_id
= asf_http_ctrl
->audio_streams
[i
];
597 if(stream_id
== asf_http_ctrl
->audio_id
) {
604 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
607 if(asf_http_ctrl
->n_video
> 0) {
608 for( i
=0; i
<asf_http_ctrl
->n_video
; i
++ ) {
609 stream_id
= asf_http_ctrl
->video_streams
[i
];
610 if(stream_id
== asf_http_ctrl
->video_id
) {
617 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
620 http_set_field( http_hdr
, str
);
621 sprintf( str
, "Pragma: stream-switch-count=%d", asf_nb_stream
);
622 http_set_field( http_hdr
, str
);
624 case ASF_Redirector_e
:
627 // First request goes here.
630 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"unknown ASF stream type\n");
633 http_set_field( http_hdr
, "Connection: Close" );
634 http_build_request( http_hdr
);
639 static int asf_http_parse_response(asf_http_streaming_ctrl_t
*asf_http_ctrl
, HTTP_header_t
*http_hdr
) {
640 char *content_type
, *pragma
;
641 char features
[64] = "\0";
643 if( http_response_parse(http_hdr
)<0 ) {
644 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Failed to parse HTTP response.\n");
647 switch( http_hdr
->status_code
) {
650 case 401: // Authentication required
651 return ASF_Authenticate_e
;
653 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d:%s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
657 content_type
= http_get_field( http_hdr
, "Content-Type");
658 //printf("Content-Type: [%s]\n", content_type);
660 pragma
= http_get_field( http_hdr
, "Pragma");
661 while( pragma
!=NULL
) {
662 char *comma_ptr
=NULL
;
664 //printf("Pragma: [%s]\n", pragma );
665 // The pragma line can get severals attributes
666 // separeted with a comma ','.
668 if( !strncasecmp( pragma
, "features=", 9) ) {
670 end
= strstr( pragma
, "," );
672 len
= strlen(pragma
);
674 len
= (unsigned int)(end
-pragma
);
676 if(len
> sizeof(features
) - 1) {
677 mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,"ASF HTTP PARSE WARNING : Pragma %s cut from %d bytes to %d\n",pragma
,len
,sizeof(features
) - 1);
678 len
= sizeof(features
) - 1;
680 strncpy( features
, pragma
, len
);
684 comma_ptr
= strstr( pragma
, "," );
685 if( comma_ptr
!=NULL
) {
686 pragma
= comma_ptr
+1;
687 if( pragma
[0]==' ' ) pragma
++;
689 } while( comma_ptr
!=NULL
);
690 pragma
= http_get_next_field( http_hdr
);
692 asf_http_ctrl
->streaming_type
= asf_http_streaming_type( content_type
, features
, http_hdr
);
696 static int asf_http_streaming_start( stream_t
*stream
, int *demuxer_type
) {
697 HTTP_header_t
*http_hdr
=NULL
;
698 URL_t
*url
= stream
->streaming_ctrl
->url
;
699 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
700 char buffer
[BUFFER_SIZE
];
706 asf_http_ctrl
= malloc(sizeof(asf_http_streaming_ctrl_t
));
707 if( asf_http_ctrl
==NULL
) {
708 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
711 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
712 asf_http_ctrl
->request
= 1;
713 asf_http_ctrl
->audio_streams
= asf_http_ctrl
->video_streams
= NULL
;
714 asf_http_ctrl
->n_audio
= asf_http_ctrl
->n_video
= 0;
715 stream
->streaming_ctrl
->data
= (void*)asf_http_ctrl
;
719 if( fd
>0 ) closesocket( fd
);
721 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
722 if( url
->port
==0 ) url
->port
= 8080;
724 if( url
->port
==0 ) url
->port
= 80;
726 fd
= connect2Server( url
->hostname
, url
->port
, 1);
727 if( fd
<0 ) return fd
;
729 http_hdr
= asf_http_request( stream
->streaming_ctrl
);
730 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request [%s]\n", http_hdr
->buffer
);
731 for(i
=0; i
< (int)http_hdr
->buffer_size
; ) {
732 int r
= send( fd
, http_hdr
->buffer
+i
, http_hdr
->buffer_size
-i
, 0 );
734 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"socket write error: %s\n",strerror(errno
));
739 http_free( http_hdr
);
740 http_hdr
= http_new_header();
742 i
= recv( fd
, buffer
, BUFFER_SIZE
, 0 );
743 //printf("read: %d\n", i );
748 http_response_append( http_hdr
, buffer
, i
);
749 } while( !http_is_header_entire( http_hdr
) );
750 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
751 http_hdr
->buffer
[http_hdr
->buffer_size
]='\0';
752 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Response [%s]\n", http_hdr
->buffer
);
754 ret
= asf_http_parse_response(asf_http_ctrl
, http_hdr
);
756 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Failed to parse header.\n");
759 switch( asf_http_ctrl
->streaming_type
) {
761 case ASF_Prerecorded_e
:
762 case ASF_PlainText_e
:
763 if( http_hdr
->body_size
>0 ) {
764 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
768 if( asf_http_ctrl
->request
==1 ) {
769 if( asf_http_ctrl
->streaming_type
!=ASF_PlainText_e
) {
770 // First request, we only got the ASF header.
771 ret
= asf_streaming_parse_header(fd
,stream
->streaming_ctrl
);
772 if(ret
< 0) goto err_out
;
773 if(asf_http_ctrl
->n_audio
== 0 && asf_http_ctrl
->n_video
== 0) {
774 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"No stream found.\n");
777 asf_http_ctrl
->request
++;
784 case ASF_Redirector_e
:
785 if( http_hdr
->body_size
>0 ) {
786 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
790 *demuxer_type
= DEMUXER_TYPE_PLAYLIST
;
793 case ASF_Authenticate_e
:
794 if( http_authenticate( http_hdr
, url
, &auth_retry
)<0 ) return -1;
795 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
800 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"unknown ASF streaming type\n");
803 // Check if we got a redirect.
807 if( asf_http_ctrl
->streaming_type
==ASF_PlainText_e
|| asf_http_ctrl
->streaming_type
==ASF_Redirector_e
) {
808 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
809 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
811 stream
->streaming_ctrl
->streaming_read
= asf_http_streaming_read
;
812 stream
->streaming_ctrl
->streaming_seek
= asf_http_streaming_seek
;
813 stream
->streaming_ctrl
->buffering
= 1;
815 stream
->streaming_ctrl
->status
= streaming_playing_e
;
816 stream
->close
= close_s
;
818 http_free( http_hdr
);
829 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
832 stream
->streaming_ctrl
= streaming_ctrl_new();
833 if( stream
->streaming_ctrl
==NULL
) {
836 stream
->streaming_ctrl
->audio_id_ptr
= &stream
->opts
->audio_id
;
837 stream
->streaming_ctrl
->video_id_ptr
= &stream
->opts
->video_id
;
838 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
839 url
= url_new(stream
->url
);
840 stream
->streaming_ctrl
->url
= check4proxies(url
);
843 mp_tmsg(MSGT_OPEN
, MSGL_INFO
, "STREAM_ASF, URL: %s\n", stream
->url
);
844 if((!strncmp(stream
->url
, "http", 4)) && (*file_format
!=DEMUXER_TYPE_ASF
&& *file_format
!=DEMUXER_TYPE_UNKNOWN
)) {
845 streaming_ctrl_free(stream
->streaming_ctrl
);
846 stream
->streaming_ctrl
= NULL
;
847 return STREAM_UNSUPPORTED
;
850 if(asf_streaming_start(stream
, file_format
) < 0) {
851 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed, exiting.\n");
852 streaming_ctrl_free(stream
->streaming_ctrl
);
853 stream
->streaming_ctrl
= NULL
;
854 return STREAM_UNSUPPORTED
;
857 if (*file_format
!= DEMUXER_TYPE_PLAYLIST
)
858 *file_format
= DEMUXER_TYPE_ASF
;
859 stream
->type
= STREAMTYPE_STREAM
;
860 fixup_network_stream_cache(stream
);
864 const stream_info_t stream_info_asf
= {
865 "mms and mms over http streaming",
867 "Bertrand, Reimar Doeffinger, Albeu",
868 "originally based on work by Majormms (is that code still there?)",
870 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL
},
872 0 // Urls are an option string