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.
36 #include "libmpdemux/asf.h"
39 #include "libmpdemux/demuxer.h"
40 #include "asf_mmst_streaming.h"
44 #include "ffmpeg_files/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
;
55 int got
= nop_streaming_read(fd
, buf
, len
, stream_ctrl
);
57 mp_tmsg(MSGT_NETWORK
, MSGL_ERR
, "Error while reading network stream.\n");
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
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
;
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;
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_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Ahhhh, stream_chunck size is too small: %d\n", stream_chunck
->size
);
131 if( stream_chunck
->size
!=stream_chunck
->size_confirm
) {
132 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"size_confirm mismatch!: %d %d\n", stream_chunck
->size
, stream_chunck
->size_confirm
);
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
;
148 case ASF_STREAMING_DATA
: // $D Data follows
149 // printf("=====> Data follows\n");
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
;
156 case ASF_STREAMING_HEADER
: // $H ASF header chunk follows
157 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF header chunk follows\n");
160 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> Unknown stream type 0x%x\n", stream_chunck
->type
);
162 return stream_chunck
->size
+4;
165 static void close_s(stream_t
*stream
) {
166 closesocket(stream
->fd
);
170 static int max_idx(int s_count
, int *s_rates
, int bound
) {
171 int i
, best
= -1, rate
= -1;
172 for (i
= 0; i
< s_count
; i
++) {
173 if (s_rates
[i
] > rate
&& s_rates
[i
] <= bound
) {
181 static int asf_streaming_parse_header(int fd
, streaming_ctrl_t
* streaming_ctrl
) {
182 ASF_stream_chunck_t chunk
;
183 asf_http_streaming_ctrl_t
* asf_ctrl
= streaming_ctrl
->data
;
184 char* buffer
=NULL
, *chunk_buffer
=NULL
;
185 int i
,r
,size
,pos
= 0;
188 int chunk_size2read
= 0;
189 int bw
= streaming_ctrl
->bandwidth
;
190 int *v_rates
= NULL
, *a_rates
= NULL
;
191 int v_rate
= 0, a_rate
= 0, a_idx
= -1, v_idx
= -1;
193 if(asf_ctrl
== NULL
) return -1;
195 // The ASF header can be in several network chunks. For example if the content description
196 // is big, the ASF header will be split in 2 network chunk.
197 // So we need to retrieve all the chunk before starting to parse the header.
199 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
201 // Endian handling of the stream chunk
202 le2me_ASF_stream_chunck_t(&chunk
);
203 size
= asf_streaming( &chunk
, &r
) - sizeof(ASF_stream_chunck_t
);
204 if(r
) mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,"Warning: drop header ????\n");
206 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while parsing chunk header\n");
209 if (chunk
.type
!= ASF_STREAMING_HEADER
) {
210 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Didn't get a header as first chunk !!!!\n");
214 // audit: do not overflow buffer_size
215 if (size
> SIZE_MAX
- buffer_size
) return -1;
216 buffer
= malloc(size
+buffer_size
);
218 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Error: Can't allocate %d bytes buffer.\n",size
+buffer_size
);
221 if( chunk_buffer
!=NULL
) {
222 memcpy( buffer
, chunk_buffer
, buffer_size
);
223 free( chunk_buffer
);
225 chunk_buffer
= buffer
;
226 buffer
+= buffer_size
;
229 if (asf_read_wrapper(fd
, buffer
, size
, streaming_ctrl
) <= 0)
232 if( chunk_size2read
==0 ) {
233 ASF_header_t
*asfh
= (ASF_header_t
*)buffer
;
234 if(size
< (int)sizeof(ASF_header_t
)) {
235 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error: Chunk is too small.\n");
237 } else mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Got chunk\n");
238 chunk_size2read
= AV_RL64(&asfh
->objh
.size
);
239 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Size 2 read=%d\n", chunk_size2read
);
241 } while( buffer_size
<chunk_size2read
);
242 buffer
= chunk_buffer
;
245 start
= sizeof(ASF_header_t
);
247 pos
= find_asf_guid(buffer
, asf_file_header_guid
, start
, size
);
249 ASF_file_header_t
*fileh
= (ASF_file_header_t
*) &buffer
[pos
];
250 pos
+= sizeof(ASF_file_header_t
);
251 if (pos
> size
) goto len_err_out
;
253 if(fileh.packetsize != fileh.packetsize2) {
254 printf("Error packetsize check don't match\n");
258 asf_ctrl
->packet_size
= AV_RL32(&fileh
->max_packet_size
);
260 // preroll: time in ms to bufferize before playing
261 streaming_ctrl
->prebuffer_size
= (unsigned int)(((double)fileh
->preroll
/1000.0)*((double)fileh
->max_bitrate
/8.0));
265 while ((pos
= find_asf_guid(buffer
, asf_stream_header_guid
, pos
, size
)) >= 0)
267 ASF_stream_header_t
*streamh
= (ASF_stream_header_t
*)&buffer
[pos
];
268 pos
+= sizeof(ASF_stream_header_t
);
269 if (pos
> size
) goto len_err_out
;
270 switch(ASF_LOAD_GUID_PREFIX(streamh
->type
)) {
271 case 0xF8699E40 : // audio stream
272 if(asf_ctrl
->audio_streams
== NULL
){
273 asf_ctrl
->audio_streams
= malloc(sizeof(int));
274 asf_ctrl
->n_audio
= 1;
277 asf_ctrl
->audio_streams
= realloc(asf_ctrl
->audio_streams
,
278 asf_ctrl
->n_audio
*sizeof(int));
280 asf_ctrl
->audio_streams
[asf_ctrl
->n_audio
-1] = AV_RL16(&streamh
->stream_no
);
282 case 0xBC19EFC0 : // video stream
283 if(asf_ctrl
->video_streams
== NULL
){
284 asf_ctrl
->video_streams
= malloc(sizeof(int));
285 asf_ctrl
->n_video
= 1;
288 asf_ctrl
->video_streams
= realloc(asf_ctrl
->video_streams
,
289 asf_ctrl
->n_video
*sizeof(int));
291 asf_ctrl
->video_streams
[asf_ctrl
->n_video
-1] = AV_RL16(&streamh
->stream_no
);
296 // always allocate to avoid lots of ifs later
297 v_rates
= calloc(asf_ctrl
->n_video
, sizeof(int));
298 a_rates
= calloc(asf_ctrl
->n_audio
, sizeof(int));
300 pos
= find_asf_guid(buffer
, asf_stream_group_guid
, start
, size
);
302 // stream bitrate properties object
304 char *ptr
= &buffer
[pos
];
305 char *end
= &buffer
[size
];
307 mp_msg(MSGT_NETWORK
, MSGL_V
, "Stream bitrate properties object\n");
308 if (ptr
+ 2 > end
) goto len_err_out
;
309 stream_count
= AV_RL16(ptr
);
311 mp_msg(MSGT_NETWORK
, MSGL_V
, " stream count=[0x%x][%u]\n",
312 stream_count
, stream_count
);
313 for( i
=0 ; i
<stream_count
; i
++ ) {
317 if (ptr
+ 6 > end
) goto len_err_out
;
322 mp_msg(MSGT_NETWORK
, MSGL_V
,
323 " stream id=[0x%x][%u]\n", id
, id
);
324 mp_msg(MSGT_NETWORK
, MSGL_V
,
325 " max bitrate=[0x%x][%u]\n", rate
, rate
);
326 for (j
= 0; j
< asf_ctrl
->n_video
; j
++) {
327 if (id
== asf_ctrl
->video_streams
[j
]) {
328 mp_msg(MSGT_NETWORK
, MSGL_V
, " is video stream\n");
333 for (j
= 0; j
< asf_ctrl
->n_audio
; j
++) {
334 if (id
== asf_ctrl
->audio_streams
[j
]) {
335 mp_msg(MSGT_NETWORK
, MSGL_V
, " is audio stream\n");
344 // automatic stream selection based on bandwidth
345 if (bw
== 0) bw
= INT_MAX
;
346 mp_msg(MSGT_NETWORK
, MSGL_V
, "Max bandwidth set to %d\n", bw
);
348 if (asf_ctrl
->n_audio
) {
349 // find lowest-bitrate audio stream
352 for (i
= 0; i
< asf_ctrl
->n_audio
; i
++) {
353 if (a_rates
[i
] < a_rate
) {
358 if (max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
) < 0) {
359 // both audio and video are not possible, try video only next
364 // find best video stream
365 v_idx
= max_idx(asf_ctrl
->n_video
, v_rates
, bw
- a_rate
);
367 v_rate
= v_rates
[v_idx
];
369 // find best audio stream
370 a_idx
= max_idx(asf_ctrl
->n_audio
, a_rates
, bw
- v_rate
);
375 if (a_idx
< 0 && v_idx
< 0) {
376 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Bandwidth too small, file cannot be played!\n");
380 if (*streaming_ctrl
->audio_id_ptr
> 0)
381 // a audio stream was forced
382 asf_ctrl
->audio_id
= *streaming_ctrl
->audio_id_ptr
;
384 asf_ctrl
->audio_id
= asf_ctrl
->audio_streams
[a_idx
];
385 else if (asf_ctrl
->n_audio
) {
386 mp_tmsg(MSGT_NETWORK
, MSGL_WARN
, "Bandwidth too small, deselected audio stream.\n");
387 *streaming_ctrl
->audio_id_ptr
= -2;
390 if (*streaming_ctrl
->video_id_ptr
> 0)
391 // a video stream was forced
392 asf_ctrl
->video_id
= *streaming_ctrl
->video_id_ptr
;
394 asf_ctrl
->video_id
= asf_ctrl
->video_streams
[v_idx
];
395 else if (asf_ctrl
->n_video
) {
396 mp_tmsg(MSGT_NETWORK
, MSGL_WARN
, "Bandwidth too small, deselected video stream.\n");
397 *streaming_ctrl
->video_id_ptr
= -2;
403 mp_tmsg(MSGT_NETWORK
, MSGL_FATAL
, "Invalid length in ASF header!\n");
404 if (buffer
) free(buffer
);
405 if (v_rates
) free(v_rates
);
406 if (a_rates
) free(a_rates
);
410 static int asf_http_streaming_read( int fd
, char *buffer
, int size
, streaming_ctrl_t
*streaming_ctrl
) {
411 static ASF_stream_chunck_t chunk
;
412 int read
,chunk_size
= 0;
413 static int rest
= 0, drop_chunk
= 0, waiting
= 0;
414 asf_http_streaming_ctrl_t
*asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
417 if (rest
== 0 && waiting
== 0) {
418 if (asf_read_wrapper(fd
, &chunk
, sizeof(ASF_stream_chunck_t
), streaming_ctrl
) <= 0)
421 // Endian handling of the stream chunk
422 le2me_ASF_stream_chunck_t(&chunk
);
423 chunk_size
= asf_streaming( &chunk
, &drop_chunk
);
425 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error while parsing chunk header\n");
428 chunk_size
-= sizeof(ASF_stream_chunck_t
);
430 if(chunk
.type
!= ASF_STREAMING_HEADER
&& (!drop_chunk
)) {
431 if (asf_http_ctrl
->packet_size
< chunk_size
) {
432 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Error: chunk_size > packet_size\n");
435 waiting
= asf_http_ctrl
->packet_size
;
437 waiting
= chunk_size
;
446 if ( waiting
>= chunk_size
) {
447 if (chunk_size
> size
){
448 rest
= chunk_size
- size
;
451 if (asf_read_wrapper(fd
, buffer
, chunk_size
, streaming_ctrl
) <= 0)
455 if (drop_chunk
) continue;
457 if (rest
== 0 && waiting
> 0 && size
-read
> 0) {
458 int s
= FFMIN(waiting
,size
-read
);
459 memset(buffer
+read
,0,s
);
469 static int asf_http_streaming_seek( int fd
, off_t pos
, streaming_ctrl_t
*streaming_ctrl
) {
471 // to shut up gcc warning
477 static int asf_header_check( HTTP_header_t
*http_hdr
) {
478 ASF_obj_header_t
*objh
;
479 if( http_hdr
==NULL
) return -1;
480 if( http_hdr
->body
==NULL
|| http_hdr
->body_size
<sizeof(ASF_obj_header_t
) ) return -1;
482 objh
= (ASF_obj_header_t
*)http_hdr
->body
;
483 if( ASF_LOAD_GUID_PREFIX(objh
->guid
)==0x75B22630 ) return 0;
487 static int asf_http_streaming_type(char *content_type
, char *features
, HTTP_header_t
*http_hdr
) {
488 if( content_type
==NULL
) return ASF_Unknown_e
;
489 if( !strcasecmp(content_type
, "application/octet-stream") ||
490 !strcasecmp(content_type
, "application/vnd.ms.wms-hdr.asfv1") || // New in Corona, first request
491 !strcasecmp(content_type
, "application/x-mms-framed") || // New in Corana, second request
492 !strcasecmp(content_type
, "video/x-ms-wmv") ||
493 !strcasecmp(content_type
, "video/x-ms-asf")) {
495 if( strstr(features
, "broadcast") ) {
496 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Live stream\n");
499 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Prerecorded\n");
500 return ASF_Prerecorded_e
;
503 // Ok in a perfect world, web servers should be well configured
504 // so we could used mime type to know the stream type,
505 // but guess what? All of them are not well configured.
506 // So we have to check for an asf header :(, but it works :p
507 if( http_hdr
->body_size
>sizeof(ASF_obj_header_t
) ) {
508 if( asf_header_check( http_hdr
)==0 ) {
509 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
510 return ASF_PlainText_e
;
511 } else if( (!strcasecmp(content_type
, "text/html")) ) {
512 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> HTML, MPlayer is not a browser...yet!\n");
513 return ASF_Unknown_e
;
515 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Redirector\n");
516 return ASF_Redirector_e
;
519 if( (!strcasecmp(content_type
, "audio/x-ms-wax")) ||
520 (!strcasecmp(content_type
, "audio/x-ms-wma")) ||
521 (!strcasecmp(content_type
, "video/x-ms-asf")) ||
522 (!strcasecmp(content_type
, "video/x-ms-afs")) ||
523 (!strcasecmp(content_type
, "video/x-ms-wmv")) ||
524 (!strcasecmp(content_type
, "video/x-ms-wma")) ) {
525 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"=====> ASF Redirector\n");
526 return ASF_Redirector_e
;
527 } else if( !strcasecmp(content_type
, "text/plain") ) {
528 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF Plain text\n");
529 return ASF_PlainText_e
;
531 mp_msg(MSGT_NETWORK
,MSGL_V
,"=====> ASF unknown content-type: %s\n", content_type
);
532 return ASF_Unknown_e
;
536 return ASF_Unknown_e
;
539 static HTTP_header_t
*asf_http_request(streaming_ctrl_t
*streaming_ctrl
) {
540 HTTP_header_t
*http_hdr
;
542 URL_t
*server_url
= NULL
;
543 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
548 int offset_hi
=0, offset_lo
=0, length
=0;
549 int asf_nb_stream
=0, stream_id
;
552 if( streaming_ctrl
==NULL
) return NULL
;
553 url
= streaming_ctrl
->url
;
554 asf_http_ctrl
= (asf_http_streaming_ctrl_t
*)streaming_ctrl
->data
;
555 if( url
==NULL
|| asf_http_ctrl
==NULL
) return NULL
;
557 // Common header for all requests.
558 http_hdr
= http_new_header();
559 http_set_field( http_hdr
, "Accept: */*" );
560 http_set_field( http_hdr
, "User-Agent: NSPlayer/4.1.0.3856" );
561 http_add_basic_authentication( http_hdr
, url
->username
, url
->password
);
563 // Check if we are using a proxy
564 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
565 server_url
= url_new( (url
->file
)+1 );
566 if( server_url
==NULL
) {
567 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"invalid proxy URL\n");
568 http_free( http_hdr
);
571 http_set_uri( http_hdr
, server_url
->url
);
572 sprintf( str
, "Host: %.220s:%d", server_url
->hostname
, server_url
->port
);
573 url_free( server_url
);
575 http_set_uri( http_hdr
, url
->file
);
576 sprintf( str
, "Host: %.220s:%d", url
->hostname
, url
->port
);
579 http_set_field( http_hdr
, str
);
580 http_set_field( http_hdr
, "Pragma: xClientGUID={c77e7400-738a-11d2-9add-0020af0a3278}" );
582 "Pragma: no-cache,rate=1.000000,stream-time=0,stream-offset=%u:%u,request-context=%d,max-duration=%u",
583 offset_hi
, offset_lo
, asf_http_ctrl
->request
, length
);
584 http_set_field( http_hdr
, str
);
586 switch( asf_http_ctrl
->streaming_type
) {
588 case ASF_Prerecorded_e
:
589 http_set_field( http_hdr
, "Pragma: xPlayStrm=1" );
591 ptr
+= sprintf( ptr
, "Pragma: stream-switch-entry=");
592 if(asf_http_ctrl
->n_audio
> 0) {
593 for( i
=0; i
<asf_http_ctrl
->n_audio
; i
++ ) {
594 stream_id
= asf_http_ctrl
->audio_streams
[i
];
595 if(stream_id
== asf_http_ctrl
->audio_id
) {
602 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
605 if(asf_http_ctrl
->n_video
> 0) {
606 for( i
=0; i
<asf_http_ctrl
->n_video
; i
++ ) {
607 stream_id
= asf_http_ctrl
->video_streams
[i
];
608 if(stream_id
== asf_http_ctrl
->video_id
) {
615 ptr
+= sprintf(ptr
, "ffff:%x:%d ", stream_id
, enable
);
618 http_set_field( http_hdr
, str
);
619 sprintf( str
, "Pragma: stream-switch-count=%d", asf_nb_stream
);
620 http_set_field( http_hdr
, str
);
622 case ASF_Redirector_e
:
625 // First request goes here.
628 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"unknown ASF stream type\n");
631 http_set_field( http_hdr
, "Connection: Close" );
632 http_build_request( http_hdr
);
637 static int asf_http_parse_response(asf_http_streaming_ctrl_t
*asf_http_ctrl
, HTTP_header_t
*http_hdr
) {
638 char *content_type
, *pragma
;
639 char features
[64] = "\0";
641 if( http_response_parse(http_hdr
)<0 ) {
642 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Failed to parse HTTP response.\n");
645 switch( http_hdr
->status_code
) {
648 case 401: // Authentication required
649 return ASF_Authenticate_e
;
651 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Server returned %d:%s\n", http_hdr
->status_code
, http_hdr
->reason_phrase
);
655 content_type
= http_get_field( http_hdr
, "Content-Type");
656 //printf("Content-Type: [%s]\n", content_type);
658 pragma
= http_get_field( http_hdr
, "Pragma");
659 while( pragma
!=NULL
) {
660 char *comma_ptr
=NULL
;
662 //printf("Pragma: [%s]\n", pragma );
663 // The pragma line can get severals attributes
664 // separeted with a comma ','.
666 if( !strncasecmp( pragma
, "features=", 9) ) {
668 end
= strstr( pragma
, "," );
670 len
= strlen(pragma
);
672 len
= (unsigned int)(end
-pragma
);
674 if(len
> sizeof(features
) - 1) {
675 mp_tmsg(MSGT_NETWORK
,MSGL_WARN
,"ASF HTTP PARSE WARNING : Pragma %s cut from %zd bytes to %d\n",pragma
,len
,sizeof(features
) - 1);
676 len
= sizeof(features
) - 1;
678 strncpy( features
, pragma
, len
);
682 comma_ptr
= strstr( pragma
, "," );
683 if( comma_ptr
!=NULL
) {
684 pragma
= comma_ptr
+1;
685 if( pragma
[0]==' ' ) pragma
++;
687 } while( comma_ptr
!=NULL
);
688 pragma
= http_get_next_field( http_hdr
);
690 asf_http_ctrl
->streaming_type
= asf_http_streaming_type( content_type
, features
, http_hdr
);
694 static int asf_http_streaming_start( stream_t
*stream
, int *demuxer_type
) {
695 HTTP_header_t
*http_hdr
=NULL
;
696 URL_t
*url
= stream
->streaming_ctrl
->url
;
697 asf_http_streaming_ctrl_t
*asf_http_ctrl
;
698 char buffer
[BUFFER_SIZE
];
704 asf_http_ctrl
= malloc(sizeof(asf_http_streaming_ctrl_t
));
705 if( asf_http_ctrl
==NULL
) {
706 mp_tmsg(MSGT_NETWORK
,MSGL_FATAL
,"Memory allocation failed.\n");
709 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
710 asf_http_ctrl
->request
= 1;
711 asf_http_ctrl
->audio_streams
= asf_http_ctrl
->video_streams
= NULL
;
712 asf_http_ctrl
->n_audio
= asf_http_ctrl
->n_video
= 0;
713 stream
->streaming_ctrl
->data
= (void*)asf_http_ctrl
;
717 if( fd
>0 ) closesocket( fd
);
719 if( !strcasecmp( url
->protocol
, "http_proxy" ) ) {
720 if( url
->port
==0 ) url
->port
= 8080;
722 if( url
->port
==0 ) url
->port
= 80;
724 fd
= connect2Server( url
->hostname
, url
->port
, 1);
725 if( fd
<0 ) return fd
;
727 http_hdr
= asf_http_request( stream
->streaming_ctrl
);
728 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Request [%s]\n", http_hdr
->buffer
);
729 for(i
=0; i
< (int)http_hdr
->buffer_size
; ) {
730 int r
= send( fd
, http_hdr
->buffer
+i
, http_hdr
->buffer_size
-i
, DEFAULT_SEND_FLAGS
);
732 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"socket write error: %s\n",strerror(errno
));
737 http_free( http_hdr
);
738 http_hdr
= http_new_header();
740 i
= recv( fd
, buffer
, BUFFER_SIZE
, 0 );
741 //printf("read: %d\n", i );
746 http_response_append( http_hdr
, buffer
, i
);
747 } while( !http_is_header_entire( http_hdr
) );
748 if( mp_msg_test(MSGT_NETWORK
,MSGL_V
) ) {
749 http_hdr
->buffer
[http_hdr
->buffer_size
]='\0';
750 mp_msg(MSGT_NETWORK
,MSGL_DBG2
,"Response [%s]\n", http_hdr
->buffer
);
752 ret
= asf_http_parse_response(asf_http_ctrl
, http_hdr
);
754 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"Failed to parse header.\n");
757 switch( asf_http_ctrl
->streaming_type
) {
759 case ASF_Prerecorded_e
:
760 case ASF_PlainText_e
:
761 if( http_hdr
->body_size
>0 ) {
762 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
766 if( asf_http_ctrl
->request
==1 ) {
767 if( asf_http_ctrl
->streaming_type
!=ASF_PlainText_e
) {
768 // First request, we only got the ASF header.
769 ret
= asf_streaming_parse_header(fd
,stream
->streaming_ctrl
);
770 if(ret
< 0) goto err_out
;
771 if(asf_http_ctrl
->n_audio
== 0 && asf_http_ctrl
->n_video
== 0) {
772 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"No stream found.\n");
775 asf_http_ctrl
->request
++;
782 case ASF_Redirector_e
:
783 if( http_hdr
->body_size
>0 ) {
784 if( streaming_bufferize( stream
->streaming_ctrl
, http_hdr
->body
, http_hdr
->body_size
)<0 ) {
788 *demuxer_type
= DEMUXER_TYPE_PLAYLIST
;
791 case ASF_Authenticate_e
:
792 if( http_authenticate( http_hdr
, url
, &auth_retry
)<0 ) return -1;
793 asf_http_ctrl
->streaming_type
= ASF_Unknown_e
;
798 mp_tmsg(MSGT_NETWORK
,MSGL_ERR
,"unknown ASF streaming type\n");
801 // Check if we got a redirect.
805 if( asf_http_ctrl
->streaming_type
==ASF_PlainText_e
|| asf_http_ctrl
->streaming_type
==ASF_Redirector_e
) {
806 stream
->streaming_ctrl
->streaming_read
= nop_streaming_read
;
807 stream
->streaming_ctrl
->streaming_seek
= nop_streaming_seek
;
809 stream
->streaming_ctrl
->streaming_read
= asf_http_streaming_read
;
810 stream
->streaming_ctrl
->streaming_seek
= asf_http_streaming_seek
;
811 stream
->streaming_ctrl
->buffering
= 1;
813 stream
->streaming_ctrl
->status
= streaming_playing_e
;
814 stream
->close
= close_s
;
816 http_free( http_hdr
);
827 static int open_s(stream_t
*stream
,int mode
, void* opts
, int* file_format
) {
830 stream
->streaming_ctrl
= streaming_ctrl_new();
831 if( stream
->streaming_ctrl
==NULL
) {
834 stream
->streaming_ctrl
->audio_id_ptr
= &stream
->opts
->audio_id
;
835 stream
->streaming_ctrl
->video_id_ptr
= &stream
->opts
->video_id
;
836 stream
->streaming_ctrl
->bandwidth
= network_bandwidth
;
837 url
= url_new(stream
->url
);
838 stream
->streaming_ctrl
->url
= check4proxies(url
);
841 mp_tmsg(MSGT_OPEN
, MSGL_INFO
, "STREAM_ASF, URL: %s\n", stream
->url
);
842 if((!strncmp(stream
->url
, "http", 4)) && (*file_format
!=DEMUXER_TYPE_ASF
&& *file_format
!=DEMUXER_TYPE_UNKNOWN
)) {
843 streaming_ctrl_free(stream
->streaming_ctrl
);
844 stream
->streaming_ctrl
= NULL
;
845 return STREAM_UNSUPPORTED
;
848 if(asf_streaming_start(stream
, file_format
) < 0) {
849 mp_tmsg(MSGT_OPEN
, MSGL_ERR
, "Failed, exiting.\n");
850 streaming_ctrl_free(stream
->streaming_ctrl
);
851 stream
->streaming_ctrl
= NULL
;
852 return STREAM_UNSUPPORTED
;
855 if (*file_format
!= DEMUXER_TYPE_PLAYLIST
)
856 *file_format
= DEMUXER_TYPE_ASF
;
857 stream
->type
= STREAMTYPE_STREAM
;
858 fixup_network_stream_cache(stream
);
862 const stream_info_t stream_info_asf
= {
863 "mms and mms over http streaming",
865 "Bertrand, Reimar Doeffinger, Albeu",
866 "originally based on work by Majormms (is that code still there?)",
868 {"mms", "mmsu", "mmst", "http", "http_proxy", "mmsh", "mmshttp", NULL
},
870 0 // Urls are an option string